Phase properties and derivatives¶
Homogeneous-state properties¶
torch_flash.properties
¶
Homogeneous-state thermodynamic property calculations.
PhaseResponseDerivatives
dataclass
¶
PhaseResponseDerivatives(
molar_volume: Tensor,
isothermal_compressibility: Tensor,
thermal_expansion_coefficient: Tensor,
isothermal_compressibility_temperature_derivative: Tensor,
thermal_expansion_temperature_derivative: Tensor,
)
Autodiff response functions used by the two derivative criteria.
Attributes:
| Name | Type | Description |
|---|---|---|
molar_volume |
Tensor
|
Homogeneous physical molar volume in m3/mol. |
isothermal_compressibility |
Tensor
|
|
thermal_expansion_coefficient |
Tensor
|
|
isothermal_compressibility_temperature_derivative |
Tensor
|
|
thermal_expansion_temperature_derivative |
Tensor
|
|
Notes
The derivatives are assembled from first and second PyTorch autodiff
derivatives of the model's explicit P(T, V, x) function. The supplied
homogeneous root is evaluated once per state; no finite-difference step is
used. Matching leading state batches are supported.
ThermodynamicDerivatives
dataclass
¶
ThermodynamicDerivatives(
dfugacity_coefficient_dlogits: Tensor,
dfugacity_coefficient_dindependent_composition: Tensor,
dfugacity_coefficient_dtemperature: Tensor,
dfugacity_coefficient_dpressure: Tensor,
dfugacity_coefficient_dmoles: Tensor,
dlog_fugacity_coefficient_dlogits: Tensor,
dlog_fugacity_coefficient_dindependent_composition: Tensor,
dlog_fugacity_coefficient_dtemperature: Tensor,
dlog_fugacity_coefficient_dpressure: Tensor,
dlog_fugacity_coefficient_dmoles: Tensor,
dfugacity_dlogits: Tensor,
dfugacity_dindependent_composition: Tensor,
dfugacity_dtemperature: Tensor,
dfugacity_dpressure: Tensor,
dfugacity_dmoles: Tensor,
dlog_fugacity_dlogits: Tensor,
dlog_fugacity_dindependent_composition: Tensor,
dlog_fugacity_dtemperature: Tensor,
dlog_fugacity_dpressure: Tensor,
dlog_fugacity_dmoles: Tensor,
dchemical_potential_dlogits: Tensor,
dchemical_potential_dindependent_composition: Tensor,
dchemical_potential_dtemperature: Tensor,
dchemical_potential_dpressure: Tensor,
dchemical_potential_dmoles: Tensor,
dreduced_chemical_potential_dlogits: Tensor,
dreduced_chemical_potential_dindependent_composition: Tensor,
dreduced_chemical_potential_dtemperature: Tensor,
dreduced_chemical_potential_dpressure: Tensor,
dreduced_chemical_potential_dmoles: Tensor,
dmolar_volume_dlogits: Tensor,
dmolar_volume_dindependent_composition: Tensor,
dmolar_volume_dtemperature: Tensor,
dmolar_volume_dpressure: Tensor,
dmolar_volume_dmoles: Tensor,
dgibbs_dtemperature: Tensor,
dgibbs_dpressure: Tensor,
)
First homogeneous-state derivatives.
Component derivatives are provided in both unconstrained softmax-logit
coordinates and n-1 independent mole fractions, where the last mole
fraction is 1 - sum(x_independent). Temperature and pressure
derivatives hold composition fixed.
Mole-number derivatives hold T and P fixed and are evaluated at
n_i = x_i mol (a one-mole total basis). Because all returned
properties are intensive, the corresponding derivative at another total
amount is this value divided by that amount in mol.
Fugacity derivatives have Pa per coordinate units.
dlog_fugacity_* differentiates the dimensionless
ln(f_i / p_standard). Chemical-potential derivatives have J/mol per
coordinate units; the reduced variants differentiate mu_i / (R*T).
Molar-volume derivatives have m3/mol per coordinate units.
Attributes:
| Name | Type | Description |
|---|---|---|
dfugacity_coefficient_dlogits |
Tensor
|
Jacobian of dimensionless |
dfugacity_coefficient_dindependent_composition |
Tensor
|
Jacobian of |
dfugacity_coefficient_dtemperature, dfugacity_coefficient_dpressure |
Fixed-composition derivatives of |
|
dfugacity_coefficient_dmoles |
Tensor
|
Unit-total-mole-basis derivative of |
dlog_fugacity_coefficient_dlogits |
Tensor
|
Jacobian of |
dlog_fugacity_coefficient_dindependent_composition |
Tensor
|
Jacobian of |
dlog_fugacity_coefficient_dtemperature, dlog_fugacity_coefficient_dpressure |
Fixed-composition derivatives of |
|
dlog_fugacity_coefficient_dmoles |
Tensor
|
Unit-basis mole-number derivative of |
dfugacity_dlogits, dfugacity_dindependent_composition |
Fugacity Jacobians in Pa with respect to the two composition coordinate systems. |
|
dfugacity_dtemperature, dfugacity_dpressure, dfugacity_dmoles |
Fugacity derivatives in Pa/K, dimensionless Pa/Pa, and Pa/mol on the documented one-mole basis. |
|
dlog_fugacity_dlogits, dlog_fugacity_dindependent_composition |
Jacobians of dimensionless |
|
dlog_fugacity_dtemperature, dlog_fugacity_dpressure, dlog_fugacity_dmoles |
Temperature, pressure, and unit-basis mole-number derivatives of dimensionless log fugacity. |
|
dchemical_potential_dlogits, dchemical_potential_dindependent_composition |
Chemical-potential Jacobians in J/mol per composition coordinate. |
|
dchemical_potential_dtemperature, dchemical_potential_dpressure |
Fixed-composition derivatives in J/(mol K) and J/(mol Pa). |
|
dchemical_potential_dmoles |
Tensor
|
Unit-basis mole-number Jacobian in J/mol2. |
dreduced_chemical_potential_dlogits |
Tensor
|
Jacobian of |
dreduced_chemical_potential_dindependent_composition |
Tensor
|
Jacobian of |
dreduced_chemical_potential_dtemperature |
Tensor
|
Fixed-composition derivative of |
dreduced_chemical_potential_dpressure |
Tensor
|
Fixed-composition derivative of |
dreduced_chemical_potential_dmoles |
Tensor
|
Unit-basis mole-number Jacobian of |
dmolar_volume_dlogits, dmolar_volume_dindependent_composition |
Molar-volume Jacobians in m3/mol per composition coordinate. |
|
dmolar_volume_dtemperature, dmolar_volume_dpressure |
Fixed-composition derivatives in m3/(mol K) and m3/(mol Pa). |
|
dmolar_volume_dmoles |
Tensor
|
Unit-basis mole-number derivative in m3/mol2. |
dgibbs_dtemperature, dgibbs_dpressure |
Fixed-composition total molar Gibbs derivatives in J/(mol K) and J/(mol Pa). |
Notes
Composition Jacobians place the property/component output axis before the coordinate axis. Scalar temperature and pressure derivatives retain only the property/component output shape.
ThermalProperties
dataclass
¶
ThermalProperties(
molar_enthalpy: Tensor,
molar_internal_energy: Tensor,
molar_entropy: Tensor,
molar_helmholtz_energy: Tensor,
molar_gibbs_energy: Tensor,
reduced_helmholtz_energy: Tensor,
reduced_gibbs_energy: Tensor,
reduced_residual_helmholtz_energy: Tensor,
reduced_residual_gibbs_energy: Tensor,
isobaric_heat_capacity: Tensor,
isochoric_heat_capacity: Tensor,
joule_thomson_coefficient: Tensor,
speed_of_sound: Tensor | None,
residual_enthalpy: Tensor,
residual_entropy: Tensor,
)
Caloric and response properties of one homogeneous phase.
All quantities are molar SI except the Joule-Thomson coefficient (K/Pa)
and speed of sound (m/s). reduced_* free energies are dimensionless
molar quantities divided by R*T.
Attributes:
| Name | Type | Description |
|---|---|---|
molar_enthalpy, molar_internal_energy |
Total energies in J/mol. |
|
molar_entropy |
Tensor
|
Total entropy in J/(mol K). |
molar_helmholtz_energy, molar_gibbs_energy |
Total free energies in J/mol. |
|
reduced_helmholtz_energy, reduced_gibbs_energy |
Dimensionless total free energies divided by |
|
reduced_residual_helmholtz_energy, reduced_residual_gibbs_energy |
Dimensionless EOS departure free energies. |
|
isobaric_heat_capacity, isochoric_heat_capacity |
|
|
joule_thomson_coefficient |
Tensor
|
Isenthalpic pressure response in K/Pa. |
speed_of_sound |
Tensor | None
|
Speed in m/s, or |
residual_enthalpy |
Tensor
|
EOS departure enthalpy in J/mol. |
residual_entropy |
Tensor
|
EOS departure entropy in J/(mol K). |
identify_flash_phases
¶
identify_flash_phases(
phases: tuple[PhaseProperties, ...],
*,
ambiguity_relative_tolerance: float = DEFAULT_AMBIGUITY_RELATIVE_TOLERANCE,
) -> tuple[PhaseProperties, ...]
Fill wholly unavailable multiphase identities by molar-volume ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phases
|
tuple[PhaseProperties, ...]
|
Equilibrium homogeneous-phase properties for one flash result. Every molar volume must be a finite positive scalar when density ordering is required. Existing identifications produced by a named method are treated as authoritative and are not mixed with this fallback. |
required |
ambiguity_relative_tolerance
|
float
|
Non-negative relative separation required between the largest and
second-largest molar volumes. If their ratio is no greater than
|
DEFAULT_AMBIGUITY_RELATIVE_TOLERANCE
|
Returns:
| Type | Description |
|---|---|
tuple[PhaseProperties, ...]
|
The original tuple when it is empty, contains one phase, or at least
one phase already has an available named-method identification.
Otherwise, copies of the supplied properties carry
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the ambiguity tolerance is negative or nonfinite, or if fallback ordering is required but any molar volume is batched, nonfinite, or nonpositive. |
Notes
This is a conservative last-resort labeling heuristic for phases already returned by a converged flash. It does not alter compositions or phase fractions, solve equilibrium, distinguish multiple liquid types, or prove that the largest-volume phase is thermodynamically vapor.
Source code in src/torch_flash/properties/phase_identification.py
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |
identify_phase
¶
identify_phase(
model: object,
state: ChemicalState,
phase: PhaseKind = "stable",
*,
method: PhaseIdentificationCriterion = DEFAULT_PHASE_IDENTIFICATION_METHOD,
threshold: float = DEFAULT_VOLUME_TO_COVOLUME_THRESHOLD,
pseudo_critical_temperature_factor: float = DEFAULT_PSEUDO_CRITICAL_TEMPERATURE_FACTOR,
ambiguity_relative_tolerance: float = DEFAULT_AMBIGUITY_RELATIVE_TOLERANCE,
pip_denominator_relative_tolerance: float | None = None,
) -> PhaseIdentification
Identify one homogeneous state with a selected literature criterion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Homogeneous-state model exposing the properties required by
|
required |
state
|
ChemicalState
|
One scalar temperature in K, scalar pressure in Pa, and one normalized composition vector. |
required |
phase
|
PhaseKind
|
Homogeneous EoS root-selection request. It is separate from the returned physical identity and does not perform a flash. |
'stable'
|
method
|
PhaseIdentificationCriterion
|
Literature criterion used for the label. The default remains
Pedersen's |
DEFAULT_PHASE_IDENTIFICATION_METHOD
|
threshold
|
float
|
Positive dimensionless |
DEFAULT_VOLUME_TO_COVOLUME_THRESHOLD
|
pseudo_critical_temperature_factor
|
float
|
Positive multiplier in Li's volume-weighted pseudo-critical temperature, used only by that method. |
DEFAULT_PSEUDO_CRITICAL_TEMPERATURE_FACTOR
|
ambiguity_relative_tolerance
|
float
|
Non-negative relative ambiguity band around the selected separator.
The returned liquid/vapor label still follows the exact inequality;
this value changes only the |
DEFAULT_AMBIGUITY_RELATIVE_TOLERANCE
|
pip_denominator_relative_tolerance
|
float | None
|
Optional scaled singularity guard forwarded to
:func: |
None
|
Returns:
| Type | Description |
|---|---|
PhaseIdentification
|
Physical identity, method, native criterion tensor, decision
threshold, and ambiguity flag. A method whose required model
properties are unavailable returns |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the state is batched, a numerical option is invalid, the method name is unknown, or an evaluated non-PIP criterion is invalid. |
TypeError
|
If a mandatory model operation for the selected available criterion is missing. |
Notes
Every returned criterion tensor remains connected to the PyTorch graph; only the string label and ambiguity flag require a detached scalar decision. Phase identification is post-processing: it neither establishes stability nor changes phase compositions, fractions, or phase count.
Source code in src/torch_flash/properties/phase_identification.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |
li_pseudo_critical_temperature
¶
li_pseudo_critical_temperature(
model: object,
composition: Tensor,
*,
factor: float = DEFAULT_PSEUDO_CRITICAL_TEMPERATURE_FACTOR,
critical_volume: Tensor | None = None,
) -> Tensor
Return Li's volume-weighted pseudo-critical temperature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Model exposing per-component critical temperatures and, unless supplied explicitly, critical molar volumes. |
required |
composition
|
Tensor
|
Mole fractions with components on the final axis. |
required |
factor
|
float
|
Dimensionless tuning factor |
DEFAULT_PSEUDO_CRITICAL_TEMPERATURE_FACTOR
|
critical_volume
|
Tensor | None
|
Optional per-component critical molar volumes in m3/mol. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Pseudo-critical temperature in K with the composition batch shape. |
Notes
This implements Tc = r1*sum(xj*Vcj*Tcj)/sum(xj*Vcj). A phase is
labeled vapor when its actual temperature exceeds this value.
Source code in src/torch_flash/properties/phase_identification.py
negative_flash_residual
¶
negative_flash_residual(
model: object, state: ChemicalState
) -> Tensor
Evaluate Perschke's dimensionless negative-flash residual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Component parameter source exposing critical temperature in K, critical pressure in Pa, and acentric factor tensors. These are the quantities used by the Wilson equilibrium-ratio estimate. |
required |
state
|
ChemicalState
|
Temperature in K, pressure in Pa, and normalized composition. Matching leading state batches are supported; components occupy the final composition axis. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Perschke's dimensionless residual .. math:: with the leading state shape. Positive values are vapor-like and negative values are liquid-like under this diagnostic. The tensor retains its PyTorch graph to differentiable state inputs and component parameters used by the Wilson estimate. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any required critical-temperature, critical-pressure, or acentric-factor tensor is unavailable. |
Notes
This function evaluates a homogeneous-state diagnostic only. It does not perform a negative flash, select an EoS root, establish phase stability, or determine the number of equilibrium phases.
Source code in src/torch_flash/properties/phase_identification.py
phase_identification_parameter
¶
phase_identification_parameter(
model: object,
state: ChemicalState,
phase: PhaseKind = "stable",
*,
denominator_relative_tolerance: float | None = None,
) -> Tensor
Evaluate the Venkatarathnam-Oellrich phase-identification parameter.
The dimensionless parameter is
.. math::
\Pi = V\left[
\frac{(\partial^2 P/\partial V\partial T)_x}
{(\partial P/\partial T)_{V,x}}
{(\partial P/\partial T)_{V,x}}
\frac{(\partial^2 P/\partial V^2)_{T,x}}
{(\partial P/\partial V)_{T,x}}
\right].
Venkatarathnam and Oellrich classify a mechanically stable homogeneous
state as liquid or liquid-like when Pi > 1 and vapor-like when
Pi <= 1. The method labels a supplied homogeneous state; it does not
select an EoS root, perform a stability test, or determine the equilibrium
phase count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Homogeneous-state model implementing |
required |
state
|
ChemicalState
|
Temperature in K, pressure in Pa, and normalized composition. Matching leading state batches are supported; components occupy the final composition axis. |
required |
phase
|
PhaseKind
|
Homogeneous EoS root used only to obtain the evaluation volume. The
default |
'stable'
|
denominator_relative_tolerance
|
float | None
|
Optional non-negative singularity threshold. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Dimensionless |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the model does not expose differentiable |
ValueError
|
If the state shapes are inconsistent, any molar volume or pressure
derivative is nonfinite, any volume is nonpositive, |
Notes
Nested torch.func.jvp calls evaluate the first and second directional
derivatives of the batched explicit pressure. Because each output state
depends only on the matching input state, all-one temperature or volume
tangents recover the independent Jacobian diagonals without constructing
a dense cross-state Jacobian. No finite-difference step or cubic-specific
derivative formula is used.
The paper describes an additional high-temperature inversion correction for isolated superheated states. It also states that this correction is unnecessary when identifying saturated split phases after a converged flash. This function returns the local parameter itself and does not perform a remote temperature root search.
Source code in src/torch_flash/properties/phase_identification.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
phase_response_derivatives
¶
phase_response_derivatives(
model: object,
state: ChemicalState,
phase: PhaseKind = "stable",
) -> PhaseResponseDerivatives
Evaluate batched homogeneous response functions with PyTorch autodiff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Homogeneous-state model implementing differentiable
|
required |
state
|
ChemicalState
|
Temperature in K, pressure in Pa, and normalized composition. Temperature and pressure share the leading state shape; components occupy the final composition axis. Scalar and leading-batch states are both supported. |
required |
phase
|
PhaseKind
|
Homogeneous EoS root request used to obtain the evaluation volume. Root selection remains separate from the returned physical response. |
'stable'
|
Returns:
| Type | Description |
|---|---|
PhaseResponseDerivatives
|
Molar volume, isothermal compressibility, thermal-expansion coefficient, and their constant-pressure temperature derivatives. Every field has the leading state shape and remains connected to state tensors and trainable model parameters. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the model lacks the required pressure or molar-volume operation. |
ValueError
|
If state or volume shapes are inconsistent, any volume or derivative
is nonfinite, any volume is nonpositive, or a selected root has
|
Notes
Nested :func:torch.func.jvp calls evaluate the independent first and
second pressure-response derivatives. For a leading batch, each output
state depends only on the matching input state, so all-one temperature or
volume tangents recover the Jacobian diagonals without constructing a
dense cross-state Jacobian. The implementation is mathematically
equivalent to differentiating each scalar response with
:func:torch.func.grad and :func:torch.func.jacrev, and is tested
against that reverse-mode scalar oracle.
Source code in src/torch_flash/properties/phase_identification.py
volume_to_covolume_ratio
¶
volume_to_covolume_ratio(
model: object,
state: ChemicalState,
phase: PhaseKind = "stable",
) -> Tensor
Return the differentiable cubic-family V/b phase diagnostic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
object
|
Model implementing |
required |
state
|
ChemicalState
|
Temperature in K, pressure in Pa, and mole fractions. |
required |
phase
|
PhaseKind
|
Homogeneous root-selection request. |
'stable'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Positive dimensionless EOS volume-to-covolume ratio. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the model lacks root selection or a mixture covolume. |
ValueError
|
If the evaluated covolume or ratio is nonpositive/nonfinite. |
Notes
Leading batch dimensions are supported. Cubic volume translations are excluded because Pedersen's criterion uses the volume entering the cubic repulsive term.
Source code in src/torch_flash/properties/phase_identification.py
fugacities_tv
¶
fugacities_tv(
model: HelmholtzStateModel,
temperature: Tensor,
volume: Tensor,
moles: Tensor,
) -> Tensor
Return component fugacities from an explicit T-V-n state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
HelmholtzStateModel
|
Model exposing an extensive residual Helmholtz energy. |
required |
temperature
|
Tensor
|
Scalar positive temperature in K. |
required |
volume
|
Tensor
|
Scalar positive total physical volume in m3. |
required |
moles
|
Tensor
|
Strictly positive component amounts in mol with shape
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Component fugacities in Pa, obtained by exponentiating
:func: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the state is not scalar/vector shaped or contains nonpositive or nonfinite inputs. |
Source code in src/torch_flash/properties/state.py
log_fugacities_tv
¶
log_fugacities_tv(
model: HelmholtzStateModel,
temperature: Tensor,
volume: Tensor,
moles: Tensor,
) -> Tensor
Return ln(f_i/p_standard) from an explicit T-V-n state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
HelmholtzStateModel
|
Model exposing an extensive residual Helmholtz energy. |
required |
temperature
|
Tensor
|
Scalar positive temperature in K. |
required |
volume
|
Tensor
|
Scalar positive total physical volume in m3. |
required |
moles
|
Tensor
|
Strictly positive component amounts in mol. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Dimensionless component |
Raises:
| Type | Description |
|---|---|
ValueError
|
If state shapes or positivity/finite-value requirements are violated. |
Notes
volume is the total physical volume in m3 and moles are component
amounts in mol. The identity
ln(f_i/p_standard) = ln(n_i*R*T/(V*p_standard)) + d(A^R/RT)/dn_i
provides an independent Helmholtz-route check of the usual TP fugacity calculation. The model's residual Helmholtz energy must be extensive and referenced to an ideal gas at the same physical volume.
Source code in src/torch_flash/properties/state.py
phase_properties
¶
phase_properties(
model: StateModel,
state: ChemicalState,
phase: PhaseKind = "stable",
*,
caloric: bool = True,
standard_state: StandardState | None = None,
) -> PhaseProperties
Evaluate a homogeneous state, without any equilibrium calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
StateModel
|
Homogeneous-state model. |
required |
state
|
ChemicalState
|
Temperature in K, pressure in Pa, and mole fractions. |
required |
phase
|
PhaseKind
|
Liquid, vapor, or stable-root request. |
'stable'
|
caloric
|
bool
|
Evaluate scalar-state residual enthalpy and entropy when possible. |
True
|
standard_state
|
StandardState | None
|
Optional component standard chemical potentials. |
None
|
Returns:
| Type | Description |
|---|---|
PhaseProperties
|
Homogeneous properties, reference-aware chemical potentials, residual free energies, and physical phase-identification diagnostics. |
Notes
Fugacities are returned in Pa and logarithmic fugacities are
ln(f_i / p_standard) with p_standard = 1 bar. Chemical potentials
and total free energies use a zero ideal-gas standard chemical potential
at that pressure unless standard_state is supplied. The corresponding
dimensionless chemical potential is mu_i / (R*T); it is the
thermodynamically meaningful alternative to ln(mu_i), which is not
generally defined for a dimensional, reference-dependent quantity that
may be negative.
Absolute caloric reference terms are deliberately not invented; returned enthalpy and entropy are residual quantities.
The reduced molar free energies are
reduced_gibbs_energy = g/(R*T) and
reduced_helmholtz_energy = a/(R*T), with a = g - P*v. The
reference-independent departures follow
g^R/(R*T) = sum(x_i*ln(phi_i)) and
a^R/(R*T) = g^R/(R*T) - Z + 1 + ln(Z), where
Z = P*v/(R*T).
At an exactly zero mole fraction, the logarithmic component properties
use the smallest positive value of the tensor dtype as a finite trace
limit. state_derivatives instead requires a strictly positive,
interior composition because logarithmic composition derivatives are
singular on the simplex boundary.
Source code in src/torch_flash/properties/state.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
state_derivatives
¶
state_derivatives(
model: StateModel,
state: ChemicalState,
phase: PhaseKind = "stable",
*,
standard_state: StandardState | None = None,
) -> ThermodynamicDerivatives
Differentiate homogeneous TP properties with PyTorch autodiff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
StateModel
|
Homogeneous-state model to differentiate. |
required |
state
|
ChemicalState
|
One scalar temperature-pressure state. Temperature is in K, pressure in Pa, and composition is a strictly positive mole-fraction vector. |
required |
phase
|
PhaseKind
|
Root-selection request held fixed throughout every derivative. |
'stable'
|
standard_state
|
StandardState | None
|
Optional component standard chemical potentials. When omitted, chemical potentials contain only the ideal-mixing/fugacity departure relative to 1 bar. |
None
|
Returns:
| Type | Description |
|---|---|
ThermodynamicDerivatives
|
Jacobians with respect to unconstrained logits, |
Raises:
| Type | Description |
|---|---|
ValueError
|
If temperature or pressure is not scalar, composition is not a vector, or a mole fraction is zero or negative. |
Notes
The phase root is not differentiated through a root-switching event. Derivatives near coalescing roots or the composition-simplex boundary can be ill-conditioned even when the underlying property value is finite.
Source code in src/torch_flash/properties/state.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
molar_enthalpy_of_mixing
¶
molar_enthalpy_of_mixing(
model: StateModel,
temperature: Tensor,
pressure: Tensor,
composition: Tensor,
phase: PhaseKind = "stable",
) -> Tensor
Return the isothermal-isobaric molar enthalpy change on mixing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
StateModel
|
Homogeneous-state model used for the mixture and pure-component reference states. |
required |
temperature
|
Tensor
|
Finite positive scalar temperature in K and pressure in Pa. |
required |
pressure
|
Tensor
|
Finite positive scalar temperature in K and pressure in Pa. |
required |
composition
|
Tensor
|
Nonnegative mole fractions with components on the final axis. Leading dimensions are evaluated as a batch of independent compositions at the supplied temperature and pressure. |
required |
phase
|
PhaseKind
|
Root-selection request applied independently to the mixture and every pure-component reference state. |
'stable'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Molar enthalpy of mixing in J/mol. The result has the leading shape of
.. math:: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If temperature, pressure, composition, or root selection is invalid. |
Notes
Ideal-gas component enthalpies cancel exactly between the mixture and its unmixed pure-component reference states. The implementation therefore evaluates only residual enthalpies from
.. math::
h^\mathrm{R}
= -R T^2
\left(\frac{\partial \sum_i x_i \ln\phi_i}
{\partial T}\right)_{P,\mathbf{x}},
with the temperature derivative obtained by PyTorch automatic differentiation. This preserves derivatives with respect to equation-of- state parameters and avoids requiring an arbitrary ideal-gas caloric reference.
The definition matches the fixed-temperature, fixed-pressure enthalpy change on mixing used to parameterize E-PPR78 by Xu, Lasala, Privat, and Jaubert, International Journal of Greenhouse Gas Control 56 (2017) 126-154, doi:10.1016/j.ijggc.2016.11.015. The requested homogeneous root must represent the experimental single-phase state; this function does not perform a flash or silently average phase enthalpies inside a two-phase region.
Source code in src/torch_flash/properties/thermal.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
thermal_properties
¶
thermal_properties(
model: StateModel,
state: ChemicalState,
standard_state: CaloricStandardState,
phase: PhaseKind = "stable",
*,
reference_pressure: float = STANDARD_PRESSURE,
molar_mass: Tensor | None = None,
) -> ThermalProperties
Evaluate Pedersen Chapter 8 properties at a specified state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
StateModel
|
Homogeneous-state model. |
required |
state
|
ChemicalState
|
One scalar TP state with temperature in K, pressure in Pa, and one composition vector. |
required |
standard_state
|
CaloricStandardState
|
Component ideal-gas heat capacity, enthalpy, and entropy functions. |
required |
phase
|
PhaseKind
|
Root-selection request held fixed during differentiation. |
'stable'
|
reference_pressure
|
float
|
Positive ideal-gas entropy reference pressure in Pa. |
STANDARD_PRESSURE
|
molar_mass
|
Tensor | None
|
Optional scalar mixture or component molar masses in kg/mol. When
omitted, the model's |
None
|
Returns:
| Type | Description |
|---|---|
ThermalProperties
|
Caloric energies, response functions, residual terms, and optional speed of sound in SI units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If state shapes, reference pressure, standard-state component count, or molar-mass data are invalid. |
InvalidStateError
|
If response functions violate positive heat-capacity or mechanical stability requirements. |
Notes
No phase-equilibrium calculation is performed. The requested phase root
remains fixed while differentiating. A volume-translated cubic receives
Pedersen's P*DeltaV enthalpy correction (Eq. 8.12).
The entropy pressure term in Eq. 8.17 is interpreted as
-R*ln(P/Pref); the glyph in the printed 2024 edition can resemble
T, but dimensional consistency and the ideal-gas identity require
the gas constant. Helmholtz energy follows a = g - P*v. Reduced total
free energies use the supplied caloric reference, while reduced residual
free energies are reference-independent EoS departures.
Source code in src/torch_flash/properties/thermal.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
Ideal-gas and chemical-potential standard states¶
torch_flash.standard_state
¶
Ideal-gas standard-state contributions and caloric reference choices.
The polynomial integrations implement Pedersen, Phase Behavior of Petroleum
Reservoir Fluids, 3rd ed. (2024), Eqs. 8.2-8.4,
doi:10.1201/9780429457418. The named coefficients are frozen from the Poling
data bank distributed by chemicals 1.5.2 and trace back to Poling et al.,
The Properties of Gases and Liquids, 5th ed. (2001).
StandardState
¶
Bases: Protocol
Protocol for component standard chemical potentials.
Implementations return one value per modeled component, retain leading temperature batch dimensions, and use the same dtype and device as the input temperature.
Methods:
| Name | Description |
|---|---|
chemical_potential |
Return component standard chemical potentials in J/mol. |
chemical_potential
¶
IdealGasPolynomial
¶
IdealGasPolynomial(
heat_capacity_coefficients: Tensor,
reference_enthalpy: Tensor,
reference_entropy: Tensor,
*,
reference_temperature: float = 298.15,
trainable: bool = False,
)
Bases: Module
Polynomial ideal-gas heat-capacity standard state.
For m columns, the heat capacity is
sum(c[j]*T**j, j=0..m-1) in J/(mol K). Four columns recover
Pedersen's Eq. 8.4; five columns support the Poling data bank. Reference
enthalpies and entropies make the otherwise arbitrary caloric datum
explicit and allow the coefficients to be fitted with PyTorch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heat_capacity_coefficients
|
Tensor
|
Matrix of coefficients in ascending temperature power, with one row per component and SI units that make the evaluated heat capacity J/(mol K). |
required |
reference_enthalpy
|
Tensor
|
Component enthalpies in J/mol at |
required |
reference_entropy
|
Tensor
|
Component standard-pressure entropies in J/(mol K) at the reference. |
required |
reference_temperature
|
float
|
Positive caloric reference temperature in K. |
298.15
|
trainable
|
bool
|
Register heat-capacity coefficients as trainable parameters. |
False
|
Source code in src/torch_flash/standard_state.py
heat_capacity
¶
Evaluate component ideal-gas heat capacities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
Tensor
|
Temperature in K with arbitrary leading batch dimensions. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Heat capacities in J/(mol K), with a final component axis. |
Source code in src/torch_flash/standard_state.py
enthalpy
¶
Integrate component ideal-gas enthalpies from the reference state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
Tensor
|
Temperature in K. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Component enthalpies in J/mol with a final component axis. |
Source code in src/torch_flash/standard_state.py
entropy
¶
Integrate component ideal-gas entropies at standard pressure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
Tensor
|
Positive temperature in K. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Component standard-state entropies in J/(mol K). |
Source code in src/torch_flash/standard_state.py
chemical_potential
¶
Return ideal-gas standard chemical potentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
Tensor
|
Temperature in K. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/torch_flash/standard_state.py
ideal_gas_polynomial
¶
ideal_gas_polynomial(
names: tuple[str, ...] | list[str],
parameter_set: ParameterSource,
*,
dtype: dtype | None = None,
device: device | str | None = None,
reference_temperature: float | None = None,
trainable: bool = False,
) -> IdealGasPolynomial
Construct an ideal-gas polynomial from versioned parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
tuple[str, ...] | list[str]
|
Component names in the desired output order. Aliases are canonicalized through the component database. |
required |
parameter_set
|
ParameterSource
|
Bundled identifier, YAML path, mapping, or loaded standard-state parameter set. |
required |
dtype
|
dtype | None
|
Tensor placement. Omitted values use the configured runtime defaults. |
None
|
device
|
dtype | None
|
Tensor placement. Omitted values use the configured runtime defaults. |
None
|
reference_temperature
|
float | None
|
Caloric reference temperature in K. When omitted, the parameter set's default is required. |
None
|
trainable
|
bool
|
Register heat-capacity coefficients as trainable parameters. Reference enthalpies and entropies remain buffers. |
False
|
Returns:
| Type | Description |
|---|---|
IdealGasPolynomial
|
Standard-state module ordered according to |
Raises:
| Type | Description |
|---|---|
ParameterDatabaseError
|
If the source has the wrong model kind or malformed/missing entries. |
KeyError
|
If a requested component is absent from the parameter set. |
Source code in src/torch_flash/standard_state.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
poling_ideal_gas
¶
poling_ideal_gas(
names: tuple[str, ...] | list[str],
*,
dtype: dtype | None = None,
device: device | str | None = None,
reference_temperature: float = 273.15,
trainable: bool = False,
) -> IdealGasPolynomial
Construct a named Poling ideal-gas standard state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
tuple[str, ...] | list[str]
|
Component names in desired tensor order. |
required |
dtype
|
dtype | None
|
Tensor placement. |
None
|
device
|
dtype | None
|
Tensor placement. |
None
|
reference_temperature
|
float
|
Caloric reference temperature in K. |
273.15
|
trainable
|
bool
|
Register heat-capacity coefficients as trainable parameters. |
False
|
Returns:
| Type | Description |
|---|---|
IdealGasPolynomial
|
Poling coefficient model for the selected components. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a selected component has no frozen Poling coefficient record. |
Notes
The tabulated fits cover 50-1000 K for gases through propane and 200-1000 K for C4-C10. Argon and helium are the monatomic ideal-gas limits. This function deliberately does not extrapolate or clip temperatures; callers remain responsible for checking those source ranges in their application.