Skip to content

Thermodynamical functions ​

ChemistryLab.ADIM_MATH_FUNCTIONS Constant
julia
ADIM_MATH_FUNCTIONS

The mathematical functions that take a dimensionless argument.

Used to extend SymbolicFunc and NumericFunc, which are this package's own types, so that a compiled or symbolic expression can be written with them. It is not used to extend them to DynamicQuantities.Quantity: that was type piracy, it applied to every package loaded beside this one, and it replaced a dimension error by an answer that depended on SI normalization. See _adim, which restores the check at the two places that needed one.

ChemistryLab.AbstractFunc Type
julia
AbstractFunc

Abstract type for objects that can be called like functions.

ChemistryLab.NumericFunc Type
julia
NumericFunc{N, F, R, Q} <: AbstractFunc

Closure-backed thermodynamic function for models that cannot be represented as symbolic expressions (e.g. HKF (Helgeson et al., 1981), or any other numeric model). Calling convention is identical to SymbolicFunc: f(; T=..., P=..., unit=false).

Variable values are resolved in order: kwarg > refs > _NF_DEFAULT_REFS. refs stores Quantity values so that unit information is preserved.

Fields

  • compiled: closure (vars...) → value in SI units.

  • vars: names of the positional arguments (e.g. (:T, :P)).

  • refs: NamedTuple of default variable values as Quantity (e.g. (T=298.15u"K", P=1e5u"Pa")).

  • unit: output unit (DynamicQuantities Quantity).

ChemistryLab.NumericFunc Method
julia
NumericFunc(f, unit)

Backward-compatible constructor: assumes vars = (:T, :P) and empty refs.

ChemistryLab.NumericFunc Method
julia
NumericFunc(f, vars, unit)

Construct a NumericFunc with no refs (fallback to _NF_DEFAULT_REFS).

ChemistryLab.SymbolicFunc Type
julia
SymbolicFunc(expr::Expr, vars=[:T, :P, :t, :x, :y, :z]; units, output_unit, kwargs...) -> SymbolicFunc

Create a SymbolicFunc from an expression. units and output_unit are those of ThermoFactory; the other keywords are the values of its parameters and the reference values of its variables.

ChemistryLab.SymbolicFunc Type
julia
SymbolicFunc

Thermodynamic function with symbolic expression and compiled evaluation.

ChemistryLab.SymbolicFunc Method
julia
SymbolicFunc(x::Number) -> SymbolicFunc

Create a constant SymbolicFunc from a number (unitless).

ChemistryLab.SymbolicFunc Method
julia
SymbolicFunc(x::Quantity) -> SymbolicFunc

Create a constant SymbolicFunc from a quantity.

ChemistryLab.SymbolicFunc Method
julia
SymbolicFunc(sym::Symbol; units, output_unit, kwargs...) -> SymbolicFunc

Create a SymbolicFunc from a single symbol. units and output_unit are those of ThermoFactory; the other keywords are the values of its parameters and the reference values of its variable.

ChemistryLab.ThermoFactory Type
julia
ThermoFactory(expr, vars=[:T, :P, :t, :x, :y, :z]; units=nothing, output_unit=nothing) -> ThermoFactory

Create a ThermoFactory from a symbolic expression.

Arguments

  • expr: symbolic expression (Expr or Symbol).

  • vars: list of variable symbols (default: T, P, t, x, y, z).

  • units: dictionary mapping symbols to their units; a symbol it does not list is dimensionless.

  • output_unit: the unit of the expression's value (dimensionless when nothing).

ChemistryLab.ThermoFactory Type
julia
ThermoFactory{Q}

Factory for creating SymbolicFunc instances from expressions. Units for each variable/parameter and the output unit are stored explicitly, so no symbolic unit propagation is needed.

ChemistryLab.ThermoFactory Method
julia
(factory::ThermoFactory)(; kwargs...)

Create a SymbolicFunc with caching for optimal performance. Safe to call from several threads at once.

Base.show Method
julia
Base.show(io::IO, ::MIME"text/plain", sf::SymbolicFunc)

Detailed string representation of a SymbolicFunc.

Base.show Method
julia
Base.show(io::IO, ::MIME"text/plain", factory::ThermoFactory)

Detailed string representation of a ThermoFactory.

Base.show Method
julia
Base.show(io::IO, sf::SymbolicFunc)

Compact string representation of a SymbolicFunc.

Base.show Method
julia
Base.show(io::IO, factory::ThermoFactory)

Compact string representation of a ThermoFactory.

ChemistryLab.apply_symbolic Method
julia
apply_symbolic(op, sf::SymbolicFunc)

Apply unary operation.

ChemistryLab.combine_symbolic Method
julia
combine_symbolic(op, x::Number, sf::SymbolicFunc)

Combine scalar with SymbolicFunc.

ChemistryLab.combine_symbolic Method
julia
combine_symbolic(op, sf::SymbolicFunc, x::Number)

Combine SymbolicFunc with scalar.

ChemistryLab.combine_symbolic Method
julia
combine_symbolic(op, sf1::SymbolicFunc, sf2::SymbolicFunc)

Combine two SymbolicFuncs.

ChemistryLab.compile_symbolic Method
julia
compile_symbolic(symbolic_expr, var_symbols)

Compile a symbolic expression to RuntimeGeneratedFunction.

ChemistryLab.derivative Method
julia
derivative(sf::SymbolicFunc, var::Symbol) -> SymbolicFunc

Compute the analytical derivative of sf with respect to variable var using Symbolics.jl and return a new SymbolicFunc for the result.

The output unit is inferred as sf.unit / unit_of_var, where unit_of_var is read from sf.refs (defaulting to dimensionless if var has no ref entry).

Examples

julia
Cp = SymbolicFunc(:(a + b*T + c/T^2); a=25.0, b=8e-3, c=-1.5e5)
dCp_dT = derivative(Cp, :T)   # ∂Cp/∂T
dCp_dT(T = 500.0)
ChemistryLab.extract_vars_params Method
julia
extract_vars_params(expr, vars) -> (Vector{Symbol}, Vector{Symbol})

Identify variables and parameters in an expression.

Arguments

  • expr: symbolic expression to analyze.

  • vars: list of symbols considered as variables (others are parameters).

Returns

  • Tuple of (variables, parameters) found in the expression.
ChemistryLab.get_unit Method
julia
get_unit(factory::ThermoFactory, sym::Symbol) -> Quantity

Get the unit of a specific variable or parameter in the factory.

ChemistryLab.get_unit Method
julia
get_unit(factory::ThermoFactory) -> Quantity

Get the output unit of the expression managed by the factory.