Skip to main content

Differentiable phase-equilibrium flash calculations built on PyTorch

Project description

torch-flash

Tests Coverage Supported OS Docs PyPI Python License: LGPL-2.1

torch-flash is a typed, differentiable thermodynamics and phase-equilibrium library built on PyTorch. Homogeneous-state properties are independent of equilibrium solvers: a supplied composition, temperature, and pressure can be evaluated directly and differentiated for optimization, machine learning, or coupled simulation.

The current model coverage, assumptions, and limitations are documented in Model scope.

Installation

python -m pip install torch-flash

Optional integrations and tools used to execute the repository studies are available through extras:

python -m pip install "torch-flash[external,notebooks]"

The project uses Pixi for reproducible development environments:

pixi install -e test
pixi run -e test test-cov

Quick start

import torch

from torch_flash import (
    ChemicalState,
    component_set,
    configure,
    peng_robinson_1978,
    phase_properties,
    state_derivatives,
    two_phase_flash,
)

runtime = configure(device="cpu", dtype=torch.float64)
model = peng_robinson_1978(component_set(("methane", "n_butane")))
state = ChemicalState(
    temperature=runtime.tensor(270.0),       # K
    pressure=runtime.tensor(3.0e6),          # Pa
    composition=runtime.tensor([0.5, 0.5]),  # mole fractions
)

# Evaluate the supplied homogeneous state without solving an equilibrium.
properties = phase_properties(model, state, phase="stable")
print(properties.compressibility_factor)
print(properties.fugacities, properties.log_fugacities)
print(properties.chemical_potentials)
print(properties.molar_helmholtz_energy, properties.molar_gibbs_energy)
print(properties.reduced_helmholtz_energy, properties.reduced_gibbs_energy)

# Derivatives are evaluated with PyTorch automatic differentiation.
derivatives = state_derivatives(model, state)
print(derivatives.dfugacity_dpressure)
print(derivatives.dchemical_potential_dtemperature)

# Stability-tested two-phase TP flash.
equilibrium = two_phase_flash(model, state)
print(equilibrium.phase_fractions)
print(equilibrium.phase_kinds, equilibrium.phase_regime)
for phase in equilibrium.phases:
    print(phase.composition)

Thermodynamic inputs and outputs use SI units. Float64 is strongly recommended for phase-equilibrium calculations. Device, dtype, CPU-thread, and deterministic-execution policies are set before model construction; see Runtime configuration.

Capabilities

  • SRK, PR76, and PR78 cubic equations of state, with attractive and co-volume binary interactions, PPR78 group-contribution BIPs, and Pedersen or Péneloux/Whitson volume translation (Soave, 1972; Peng and Robinson, 1976; Jaubert and Mutelet, 2004).
  • NRTL, Wilson, original VLE-UNIFAC, and Huron–Vidal mixing rules (Renon and Prausnitz, 1968; Huron and Vidal, 1979; Hansen et al., 1991).
  • SRK-CPA association models with configurable site schemes, cross-association rules, hydrocarbon/water parameters, and heavy-cut adapters (Kontogeorgis et al., 1996).
  • Native GERG-2008 and EOS-CG-2021 multifluid Helmholtz models (Kunz and Wagner, 2012; Neumann et al., 2023).
  • Fugacity, chemical potential, compressibility, molar volume, Helmholtz and Gibbs energies, caloric and response properties, and PyTorch derivatives at a specified state.
  • Tangent-plane stability, two-phase TP flash, fixed-phase-count multiphase flash, phase identification, bubble/dew calculations, phase-envelope continuation, and a binary critical-point solver (Michelsen, 1982).
  • Model-neutral pseudo-component splitting, characterization, lumping, and model-specific property adapters.
  • Differentiable LBC and Pedersen corresponding-states viscosity models.
  • Trainable interaction parameters and Helmholtz coefficients for reparameterization against user data.

The package does not currently provide automatic global phase-count discovery, reactive equilibrium, or a general multicomponent critical-locus arclength solver. Fixed-phase-count multiphase calculations require physically meaningful initialization. Detailed model-specific boundaries are stated in Model scope.

Parameter databases

Component properties and bundled model parameterizations are stored in versioned YAML with canonical component names, declared SI units, model identity, and source metadata. Models may use packaged parameter sets, custom YAML files, typed parameter objects, or user-supplied tensors:

from torch_flash import (
    available_parameter_sets,
    component_set,
    cubic_eos,
    cubic_interaction_parameters,
    multifluid_eos,
)

components = component_set(("methane", "n_decane"))
interactions = cubic_interaction_parameters(
    components,
    "binary-interaction.segovia-2017-methane-n-decane",
)
pr78 = cubic_eos(
    components,
    "cubic.pr-1978",
    kij=interactions.kij,
    lij=interactions.lij,
)
gerg = multifluid_eos("gerg2008", ("CO2", "CH4"))
print(available_parameter_sets(model_kind="cubic"))

Parsed database documents are cached; model instances and their tensors are not shared. Custom schemas, canonical naming, units, cache controls, and trainable-parameter conventions are described in Component and model parameter databases.

Bundled parameters, repository-only scientific data, and independently generated software baselines have distinct provenance and redistribution conditions. See Licensing and scientific-data provenance. Research studies and test data are not included in the PyPI wheel or source distribution.

Documentation

Generative-AI disclosure

Following the transparency and responsibility principles in the CNPq Policy for Integrity in Scientific Activity, generative-AI tools—OpenAI Codex and GPT models—assisted code implementation, refactoring, and documentation. Human contributors remain responsible for scientific choices, source verification, tests, review, and released artifacts. Contributions produced with AI assistance are held to the same review and reproducibility requirements as all other work.

License

Source code is distributed under the GNU Lesser General Public License v2.1. Third-party notices and data-specific terms are recorded in THIRD_PARTY_NOTICES.md.

Institutional Support

torch-flash receives institutional support from the Laboratório Nacional de Computação Científica (LNCC), a research unit of the Ministério da Ciência, Tecnologia e Inovação (MCTI), Brazil, and is partially developed at the Universidade do Estado de Santa Catarina (UDESC). The project is developed by the ThermoPhase — Fluid and Complex Systems Research Group.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_flash-0.1.0.tar.gz (192.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_flash-0.1.0-py3-none-any.whl (224.7 kB view details)

Uploaded Python 3

File details

Details for the file torch_flash-0.1.0.tar.gz.

File metadata

  • Download URL: torch_flash-0.1.0.tar.gz
  • Upload date:
  • Size: 192.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for torch_flash-0.1.0.tar.gz
Algorithm Hash digest
SHA256 15d327c96b483dfaa4a7d2a95c735e503f8b4047f3c3669be6184dbf3d8d4310
MD5 af56fb0ef45c7ce926d4e700c3e5a036
BLAKE2b-256 00ed52ee5c376dd321fc4b25ab1513951e7354dc7ce40008c51137e92553e31f

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_flash-0.1.0.tar.gz:

Publisher: publish-pypi.yml on ThermoPhase-FCSRG/torch-flash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_flash-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: torch_flash-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 224.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for torch_flash-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b997b9cd9a0d93daa0d29950220026ddb5caeacd17a6d764ac90b2fc4aade71
MD5 7616a0f75b4540bdbb77a295d81da066
BLAKE2b-256 60387ed1c9e5c02a6ed142d305d982f28bb06fbf1b9ba02907bba9714d772235

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_flash-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on ThermoPhase-FCSRG/torch-flash

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page