DiffPlz

Differentiate Please module.

This module provides the DiffPlz class, which is used to obtain all the derivatives of a thermodynamic expression with respect to temperature (T), volume (V) (could be pressure (P) too), and the number of moles of components (n[i], n[j]). It handles both direct and cross derivatives, including second derivatives and cross derivatives between temperature, volume, and number of moles.

Use the class as.

import thermodiff as td

sol = td.DiffPlz(expression, internal_functions, indexes, name="f")
class DiffPlz(expression, internal_functions: List[Function] | None = None, indexes: List[Idx] | None = None, name: str = 'f', derivatives=None)[source]

Bases: object

Class to obtaine all the derivatives of a thermodynamic expression.

The class will obtain the derivatives respect to:
  • Temperature (T)

  • Volume (V) or pressure (P)

  • Number of moles i (n[i])

  • Number of moles j (n[j])

For that, your expression must be defined in terms of these variables. Import them from thermodiff.thermovars:

from thermodiff import n, T, V

On the other hand, if you expression contains index-based variables. You can’t use the indexes i and j directly since they are reserved for the derivatives. Instead, please use k, l, m. You can import them from thermodiff.thermovars as well:

from thermodiff import k, l, m

Or, you can define new symbols with different names, but make sure they are not i or j.

from sympy import symbols

o, p, q = symbols("o p q", cls=sp.Idx)
Parameters:
  • expression (sympy expression) – The thermodynamic expression to differentiate.

  • internal_functions (list of sympy functions) – List of sympy functions that are used in the expression.

  • indexes (list of sympy Idx) – List of sympy Idx that are used in the expression. These are usually the indexes for the number of moles, like [k, l, m].

  • name (str, optional) – Name of the DiffClass instance, by default “f”. It could be for example the name of the thermodynamic function you are working with, like “G^E” or “A^r”.

  • derivatives (str or iterable of str, optional) – Derivative attributes to compute. By default every derivative is computed. When a selection is given, only the selected derivatives (and the lower order ones they are built from) are differentiated; the attributes that were not requested are set to None. The names understood by thermodiff.derivatives.resolve_derivatives() are accepted.

name

Name of the Function to be differentiated instance.

Type:

str

expression

The thermodynamic expression to differentiate.

Type:

SymPy expression

internal_functions

List of SymPy functions that are used in the expression.

Type:

list of SymPy functions

indexes

List of SymPy Idx that are used in the expression. These are usually the indexes for the number of moles, like [k, l, m].

Type:

list of SymPy Idx

dt

First derivative of the expression with respect to temperature (T).

Type:

SymPy expression

dt2

Second derivative of the expression with respect to temperature (T).

Type:

SymPy expression

dv

First derivative of the expression with respect to volume (V).

Type:

SymPy expression

dv2

Second derivative of the expression with respect to volume (V).

Type:

SymPy expression

dp

First derivative of the expression with respect to pressure (P).

Type:

SymPy expression

dp2

Second derivative of the expression with respect to pressure (P).

Type:

SymPy expression

dni

First derivative of the expression with respect to number of moles i (n[i]).

Type:

SymPy expression

dnidnj

Second derivative of the expression with respect to number of moles i (n[i]) and j (n[j]).

Type:

SymPy expression

dtdv

Cross second derivative respect to temperature(T) and volume (V).

Type:

SymPy expression

dtdp

Cross second derivative respect to temperature(T) and pressure (P).

Type:

SymPy expression

dtdni

Cross second derivative respect to temperature(T) and number of moles i (n[i]).

Type:

SymPy expression

dvdni

Cross second derivative volume (V) and number of moles i (n[i]).

Type:

SymPy expression

dvdp

Cross second derivative volume (V) and pressure (P).

Type:

SymPy expression

dpdni

Cross second derivative pressure (P) and number of moles i (n[i]).

Type:

SymPy expression

arguments

List of symbols (variables) that appear in the expression.

Type:

List[sp.Symbol]

derivatives

The derivative attributes that were selected for computation, or None when every derivative was computed.

Type:

list of str or None

diff_ni(expression: Expr, index: Idx) → Expr[source]

Obtain the compositional derivative dn[index].

Parameters:
  • expression (sympy expression) – The thermodynamic expression to differentiate.

  • index (sp.Idx) – The index of the number of moles to differentiate with respect to.

Returns:

The compositional derivative dn[index].

Return type:

sympy expression

diff_dnidnj(expression: Expr, index: Idx) → Expr[source]

Obtain the second compositional derivative.

Parameters:
  • expression (sympy expression) – The thermodynamic expression of first compositional derivative.

  • index (sp.Idx) – The index of the number of moles to differentiate with respect to.

Returns:

The second compositional derivative.

Return type:

sympy expression

clean_plz(derivs_to_clean: list[str] | None = None)[source]

Clean the DiffPlz instance.

This method applies known simplification patterns to the differentiated expressions in the DiffPlz instance.

Parameters:

derivs_to_clean (list[str], optional) – List of derivative keys to clean. If None, all derivatives will be cleaned. The possible keys are: “dT”, “dV”, “dP”, “dni”, “dT2”, “dV2”, “dP2”, “dnidnj”, “dTn”, “dVn”, “dPn”, “dTV”, “dTP”, “dVP”.

latex_readable_plz() → dict[source]

Return a clean LaTeX representation of all derivatives.

Uses latex_utils module for the heavy lifting. This method is now a thin orchestration layer.

to_fortran(*args, **kwargs)[source]

Generate a Fortran subroutine for this instance.

This is a thin wrapper around thermodiff.fortran.to_fortran(). See that function for the accepted arguments.

Returns:

The free-form Fortran source code.

Return type:

str