Skip to main content

vle_thermo/
lib.rs

1//! # VLE Engine — Vapor-Liquid Equilibrium Thermodynamic Calculator
2//!
3//! This crate is the computational core of the VLE project, a modernization of two
4//! legacy thermodynamic codebases (VB6 ~15,000 lines + Pascal ~2,500 lines) into a
5//! high-performance Rust library with Python bindings via PyO3.
6//!
7//! ## What this library does
8//!
9//! VLE (Vapor-Liquid Equilibrium) calculations predict how chemical mixtures split
10//! between liquid and vapor phases at given temperature and pressure conditions.
11//! This is fundamental to chemical engineering — every distillation column, flash
12//! drum, and separation process relies on VLE predictions.
13//!
14//! The library supports:
15//! - **22+ cubic equations of state** (EOS) for modeling PVT behavior of gases and liquids
16//! - **5 activity coefficient models** for liquid-phase non-ideality (van Laar, Wilson, etc.)
17//! - **11 mixing rules** for combining pure-component parameters into mixture parameters
18//! - **6 saturation pressure correlations** for estimating boiling points
19//! - **6 flash calculation types**: bubble point (T/P), dew point (T/P), isothermal flash,
20//!   adiabatic flash
21//! - **Parameter regression**: kij (binary interaction) and Aij (activity model) fitting
22//!
23//! ## Why these modules exist
24//!
25//! The module structure mirrors the mathematical layers of a VLE calculation:
26//!
27//! 1. **`eos`** — Equation of state definitions. An EOS is a mathematical model
28//!    (e.g., Peng-Robinson, Soave-Redlich-Kwong) that relates pressure, volume, and
29//!    temperature for a substance. Each variant uses a different α(T) function to
30//!    capture how molecular attractions change with temperature. The 22+ variants come
31//!    from merging 19 VB6 models with 3 additional Pascal models (Schmidt-Wenzel,
32//!    Patel-Teja) that handle polar and asymmetric molecules better via a third
33//!    parameter.
34//!
35//! 2. **`activity`** — Activity coefficient models. These capture liquid-phase
36//!    non-ideality (deviations from Raoult's law) using empirical correlations fit
37//!    to experimental data. Essential for polar/associating mixtures (e.g., water +
38//!    alcohol) where cubic EOS alone gives poor liquid-phase predictions.
39//!
40//! 3. **`mixing`** — Mixing rules. When applying a pure-component EOS to a mixture,
41//!    you need rules for combining the individual a, b (and sometimes c) parameters
42//!    into mixture-average values. Classical rules (quadratic in composition) work
43//!    for simple mixtures; advanced rules like Wong-Sandler incorporate activity
44//!    coefficient information for better accuracy with non-ideal systems.
45//!
46//! 4. **`saturation`** — Saturation pressure models. These correlations (Antoine,
47//!    Riedel, etc.) estimate pure-component boiling pressure at a given temperature.
48//!    Flash calculations use these for initial K-value estimates to seed iterative
49//!    convergence.
50//!
51//! 5. **`types`** — Core data structures shared across all modules. `Component`
52//!    holds pure-component properties (critical T/P, acentric factor, etc.),
53//!    `Mixture` groups components with their compositions, `Flow` represents a
54//!    process stream with phase-specific data, `Tolerances` controls convergence
55//!    criteria, and `ReferenceState` defines the thermodynamic reference for
56//!    enthalpy/entropy calculations.
57//!
58//! ## Origin and academic references
59//!
60//! Based on the thesis: *"Desarrollo de un Programa Computacional para el Cálculo
61//! del Equilibrio Líquido Vapor de Mezclas Multicomponentes bajo el Ambiente Windows"*
62//! (Jackson & Mendible, USB, 1999). Additional models from Reference (4): Da Silva &
63//! Báez (1989) — Mac Pascal program contributing Schmidt-Wenzel, Patel-Teja, and
64//! Chao-Seader models. All code derived from (4) is annotated with citation comments.
65//!
66//! ## Internal units
67//!
68//! All calculations use these canonical units (matching the legacy codebases):
69//! - Temperature: **K** (Kelvin, absolute)
70//! - Pressure: **kPa** (absolute — never gauge)
71//! - Molar energy: **kJ/kmol**
72//! - Molar entropy: **kJ/(kmol·K)**
73//! - Molar volume: **cm³/mol**
74//! - Amount: **kmol**
75//! - Gas constant R: **8.31451 kJ/(kmol·K)**
76
77// `pub mod eos` does two things:
78// 1. `mod eos` tells Rust to look for a file named `eos.rs` (or `eos/mod.rs`)
79//    and include it as a sub-module of this library.
80// 2. `pub` makes the module public, so code outside this crate (e.g., the
81//    Python bindings or other Rust crates) can access it. Without `pub`,
82//    the module would only be usable within this crate.
83pub mod activity;
84pub mod eos;
85pub mod mixing;
86pub mod numerics;
87pub mod saturation;
88pub mod types;
89pub mod virial;
90
91// Python bindings live in their own module, gated behind the `python` feature.
92// Maturin enables this feature when building the wheel; `cargo add vle-thermo`
93// (pure-Rust consumer) does not, so PyO3 stays out of the dependency closure.
94// The module's `#[pymodule] fn _engine` is the entry point CPython dlopen's;
95// see engine/src/py_bindings.rs for the binding surface and the rule that
96// every milestone from M5 forward must wire up its new functions here.
97#[cfg(feature = "python")]
98mod py_bindings;
99
100// `pub use` re-exports an item from a sub-module, making it available
101// directly at this crate's top level. Without these lines, users would
102// have to write the full path: `vle_thermo::eos::CubicEos`. With them,
103// they can write the shorter `vle_thermo::CubicEos` instead. It's purely
104// a convenience — the types still live in their original modules, but
105// `pub use` creates a public shortcut at the crate root.
106pub use activity::ActivityModel;
107pub use eos::CubicEos;
108pub use mixing::MixingRule;
109pub use saturation::SatPressureModel;
110
111// Re-export all core data structures from types module.
112pub use types::*;