Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
70c3e74
Merge feos-ad functionalities into feos
prehner Aug 18, 2025
81b045e
rebase
prehner Aug 18, 2025
9ab473a
use first_derivative from quantity for virial coefficients
prehner Aug 18, 2025
3be6e39
Minor optimizations for the ideal gas models
prehner Aug 18, 2025
afaced3
Fix robustness issue in vle_pure_t and revive Estimator interface wit…
prehner Aug 18, 2025
58a038f
fix import
prehner Aug 18, 2025
f0fb586
fix Cargo.toml
prehner Aug 19, 2025
8e9757a
moved GcPcSaftAD and therefore concluded the feos-ad integration
prehner Aug 19, 2025
25c789e
update workflow
prehner Aug 19, 2025
81fc7f8
fix python for gc_pcsaft
prehner Aug 19, 2025
6640601
fixed for gc-pcsaft
prehner Aug 25, 2025
982f421
newest lints
prehner Aug 25, 2025
ba7d6ec
use derivatives with units for state properties
prehner Aug 25, 2025
6112b2a
remove nshare dependency
prehner Aug 25, 2025
3dd4d15
fix dft for PC-SAFT
prehner Aug 29, 2025
71eb3ac
Updated SAFT-VR Mie model
g-bauer Aug 27, 2025
15ff86e
Updated PeTS
g-bauer Aug 27, 2025
23b9348
fix test
prehner Aug 29, 2025
2826fa4
finalized adaption of PC-SAFT family
prehner Aug 30, 2025
24aeafe
update to pyo3 0.26
prehner Aug 30, 2025
9cfafbc
python bindings for the libraries that are already updated
prehner Aug 30, 2025
f80b34e
make ndarray optional (a bit of a mess in the polar terms though :/)
prehner Sep 3, 2025
38147a9
fix dft
prehner Sep 3, 2025
79c76d9
fix sign in hc term
prehner Sep 3, 2025
5d9dc3d
implement AD for all state constructors
prehner Sep 3, 2025
5664a09
updated saftvrqmie and UV
prehner Sep 4, 2025
c2fca34
bikeshedding for the IdealGas traits
prehner Sep 5, 2025
424b778
more ideal gas bikeshedding
prehner Sep 5, 2025
687212a
final(?) ideal gas bikeshed
prehner Sep 5, 2025
4738d5c
fixed stability_analysis
prehner Sep 9, 2025
b6dcf3f
fix markdown and String representations of states
prehner Sep 9, 2025
043875b
fix some regression in the parameter handling in python
prehner Sep 9, 2025
e182cc9
add getters for Identifier
prehner Sep 9, 2025
3d631bc
remove some string allocations
prehner Sep 9, 2025
987bd7f
rebase
prehner Sep 12, 2025
01e98ab
pretty significant bug, no idea why it did not show earlier
prehner Sep 12, 2025
0277e02
fixes for num_dual change
prehner Sep 17, 2025
e575ae6
Fix bubble Newton step for bubble points at given pressure
prehner Sep 17, 2025
b03222a
Add tests for newton methods within bubble and dew point iterations
prehner Sep 17, 2025
f6be6aa
reactivate (and fix) PC-SAFT tests
prehner Sep 17, 2025
d352ed7
update num-dual and quantity dependencies
prehner Sep 28, 2025
bb8f5e5
get rid of most Arcs
prehner Sep 28, 2025
935b651
remove the ResidualConst trait
prehner Sep 29, 2025
6472647
replace PureProperty and BinaryProperty with new Estimator struct
prehner Sep 29, 2025
d51c8d9
rebase
prehner Oct 10, 2025
898c917
adding final docstrings to feos-core
prehner Oct 10, 2025
ba952f5
removed stuff that accidentally ended up here
prehner Oct 10, 2025
3ed1307
make python package usable without the "ad" feature
prehner Oct 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more ideal gas bikeshedding
  • Loading branch information
prehner committed Sep 12, 2025
commit 424b77863ca4d6a50ef779250ebe36976c2faa19
34 changes: 2 additions & 32 deletions crates/feos-core/src/equation_of_state/ideal_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,12 @@ use num_dual::DualNum;

/// Ideal gas Helmholtz energy contribution that allows calculating derivatives
/// with respect to model parameters.
pub trait IdealGasAD<D>: Clone {
pub trait IdealGas<D = f64> {
/// Implementation of an ideal gas model in terms of the
/// logarithm of the cubic thermal de Broglie wavelength
/// in units ln(A³) for each component in the system.
fn ln_lambda3_ad<D2: DualNum<f64, Inner = D> + Copy>(&self, temperature: D2) -> D2;

/// Implementation of an ideal gas model in terms of the
/// logarithm of the cubic thermal de Broglie wavelength
/// in units ln(A³) for each component in the system.
fn ln_lambda3(&self, temperature: D) -> D;

/// The name of the ideal gas model.
fn ideal_gas_model(&self) -> &'static str;
}

/// Ideal gas Helmholtz energy contribution without automatic differentiation.
pub trait IdealGas {
/// Implementation of an ideal gas model in terms of the
/// logarithm of the cubic thermal de Broglie wavelength
/// in units ln(A³) for each component in the system.
fn ln_lambda3<D: DualNum<f64> + Copy>(&self, temperature: D) -> D;
fn ln_lambda3<D2: DualNum<f64, Inner = D> + Copy>(&self, temperature: D2) -> D2;

/// The name of the ideal gas model.
fn ideal_gas_model(&self) -> &'static str;
}

impl<T: IdealGas, D: DualNum<f64> + Copy> IdealGasAD<D> for &T {
fn ln_lambda3_ad<D2: DualNum<f64> + Copy>(&self, temperature: D2) -> D2 {
IdealGas::ln_lambda3(*self, temperature)
}

fn ln_lambda3(&self, temperature: D) -> D {
IdealGas::ln_lambda3(*self, temperature)
}

fn ideal_gas_model(&self) -> &'static str {
T::ideal_gas_model(self)
}
}
76 changes: 18 additions & 58 deletions crates/feos-core/src/equation_of_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use quantity::{Energy, MolarEnergy, MolarWeight, Moles, Temperature, Volume};
mod ideal_gas;
mod residual;

pub use ideal_gas::{IdealGas, IdealGasAD};
pub use ideal_gas::IdealGas;
pub use residual::{Molarweight, NoResidual, Residual, ResidualConst, ResidualDyn, Subset};

/// An equation of state consisting of an ideal gas model
Expand Down Expand Up @@ -112,19 +112,17 @@ pub trait Total<N: Dim = Dyn, D: DualNum<f64> + Copy = f64>: Residual<N, D>
where
DefaultAllocator: Allocator<N>,
{
type IdealGas<'a>: IdealGasAD<D>
where
Self: 'a;
type IdealGas: IdealGas<D>;

fn ideal_gas_model(&self) -> &'static str;

fn ideal_gas<'a>(&'a self) -> impl Iterator<Item = Self::IdealGas<'a>>;
fn ideal_gas(&self) -> impl Iterator<Item = &Self::IdealGas>;

fn ln_lambda3<D2: DualNum<f64, Inner = D> + Copy>(&self, temperature: D2) -> OVector<D2, N> {
OVector::from_iterator_generic(
N::from_usize(self.components()),
U1,
self.ideal_gas().map(|i| i.ln_lambda3_ad(temperature)),
self.ideal_gas().map(|i| i.ln_lambda3(temperature)),
)
}

Expand All @@ -142,12 +140,12 @@ where
} else {
r.ln() - 1.0
};
res += r * (i.ln_lambda3_ad(temperature) + ln_rho_m1)
res += r * (i.ln_lambda3(temperature) + ln_rho_m1)
}
res * molar_volume * temperature
}

fn ideal_gas_helmholtz_energy_unit<D2: DualNum<f64, Inner = D> + Copy>(
fn ideal_gas_helmholtz_energy<D2: DualNum<f64, Inner = D> + Copy>(
&self,
temperature: Temperature<D2>,
volume: Volume<D2>,
Expand All @@ -162,78 +160,40 @@ where
&molefracs,
)) * total_moles
}

fn ideal_gas_molar_helmholtz_energy_0(
&self,
temperature: D,
molar_volume: D,
molefracs: &OVector<D, N>,
) -> D {
let partial_density = molefracs / molar_volume;
let mut res = D::from(0.0);
for (i, &r) in self.ideal_gas().zip(partial_density.iter()) {
let ln_rho_m1 = if r.re() == 0.0 {
D::from(0.0)
} else {
r.ln() - 1.0
};
res += r * (i.ln_lambda3(temperature) + ln_rho_m1)
}
res * molar_volume * temperature
}

fn ideal_gas_helmholtz_energy_0(
&self,
temperature: Temperature<D>,
volume: Volume<D>,
moles: &Moles<OVector<D, N>>,
) -> Energy<D> {
let total_moles = moles.sum();
let molefracs = moles / total_moles;
let molar_volume = volume / total_moles;
MolarEnergy::from_reduced(self.ideal_gas_molar_helmholtz_energy_0(
temperature.into_reduced(),
molar_volume.into_reduced(),
&molefracs,
)) * total_moles
}
}

impl<
I: IdealGas + Clone + 'static,
C: Deref<Target = EquationOfState<Vec<I>, R>> + Clone,
R: ResidualDyn + 'static,
D: DualNum<f64> + Copy,
> Total<Dyn, D> for C
> Total<Dyn, f64> for C
{
type IdealGas<'a>
= &'a I
where
Self: 'a;
type IdealGas = I;

fn ideal_gas_model(&self) -> &'static str {
self.ideal_gas[0].ideal_gas_model()
}

fn ideal_gas<'a>(&'a self) -> impl Iterator<Item = Self::IdealGas<'a>> {
fn ideal_gas(&self) -> impl Iterator<Item = &Self::IdealGas> {
self.ideal_gas.iter()
}
}

impl<I: IdealGasAD<D>, R: ResidualConst<N, D> + 'static, D: DualNum<f64> + Copy, const N: usize>
Total<Const<N>, D> for EquationOfState<[I; N], R>
impl<
I: IdealGas<D> + Clone,
R: ResidualConst<N, D> + 'static,
D: DualNum<f64> + Copy,
const N: usize,
> Total<Const<N>, D> for EquationOfState<[I; N], R>
{
type IdealGas<'a>
= I
where
I: 'a;
type IdealGas = I;

fn ideal_gas_model(&self) -> &'static str {
self.ideal_gas[0].ideal_gas_model()
}

fn ideal_gas<'a>(&'a self) -> impl Iterator<Item = Self::IdealGas<'a>> {
self.ideal_gas.clone().into_iter()
fn ideal_gas(&self) -> impl Iterator<Item = &Self::IdealGas> {
self.ideal_gas.iter()
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ mod parameter_fit;
mod phase_equilibria;
mod state;
pub use equation_of_state::{
EquationOfState, IdealGas, IdealGasAD, Molarweight, NoResidual, Residual, ResidualConst,
ResidualDyn, Subset, Total,
EquationOfState, IdealGas, Molarweight, NoResidual, Residual, ResidualConst, ResidualDyn,
Subset, Total,
};
pub use errors::{FeosError, FeosResult};
pub use parameter_fit::{BinaryModel, ParametersAD, PureModel};
Expand Down
20 changes: 13 additions & 7 deletions crates/feos-core/src/state/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
let ideal_gas = || {
quantity::ad::gradient_copy(
partial2(
|n, &t, &v| self.eos.ideal_gas_helmholtz_energy_unit(t, v, &n),
|n, &t, &v| self.eos.ideal_gas_helmholtz_energy(t, v, &n),
&self.temperature,
&self.volume,
),
Expand All @@ -37,7 +37,7 @@ where
let ideal_gas = || {
quantity::ad::partial_hessian_copy(
partial(
|(n, t), &v| self.eos.ideal_gas_helmholtz_energy_unit(t, v, &n),
|(n, t), &v| self.eos.ideal_gas_helmholtz_energy(t, v, &n),
&self.volume,
),
(&self.moles, self.temperature),
Expand Down Expand Up @@ -80,7 +80,7 @@ where
let ideal_gas = || {
-quantity::ad::first_derivative(
partial2(
|t, &v, n| self.eos.ideal_gas_helmholtz_energy_unit(t, v, n),
|t, &v, n| self.eos.ideal_gas_helmholtz_energy(t, v, n),
&self.volume,
&self.moles,
),
Expand Down Expand Up @@ -111,7 +111,7 @@ where
let ideal_gas = || {
-quantity::ad::second_derivative(
partial2(
|t, &v, n| self.eos.ideal_gas_helmholtz_energy_unit(t, v, n),
|t, &v, n| self.eos.ideal_gas_helmholtz_energy(t, v, n),
&self.volume,
&self.moles,
),
Expand All @@ -131,7 +131,7 @@ where
let ideal_gas = || {
-quantity::ad::third_derivative(
partial2(
|t, &v, n| self.eos.ideal_gas_helmholtz_energy_unit(t, v, n),
|t, &v, n| self.eos.ideal_gas_helmholtz_energy(t, v, n),
&self.volume,
&self.moles,
),
Expand Down Expand Up @@ -165,8 +165,14 @@ where
pub fn helmholtz_energy(&self, contributions: Contributions) -> Energy<D> {
let residual = || self.residual_helmholtz_energy();
let ideal_gas = || {
self.eos
.ideal_gas_helmholtz_energy_0(self.temperature, self.volume, &self.moles)
quantity::ad::zeroth_derivative(
partial2(
|t, &v, n| self.eos.ideal_gas_helmholtz_energy(t, v, n),
&self.volume,
&self.moles,
),
self.temperature,
)
};
Self::contributions(ideal_gas, residual, contributions)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/feos-derive/src/ideal_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn impl_ideal_gas(
});
quote! {
impl IdealGas for IdealGasModel {
fn ln_lambda3<D: DualNum<f64> + Copy>(&self, temperature: D) -> D {
fn ln_lambda3<D: DualNum<f64, Inner=f64> + Copy>(&self, temperature: D) -> D {
match self {
#(#ln_lambda3,)*
}
Expand Down
31 changes: 5 additions & 26 deletions crates/feos/src/ideal_gas/joback.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! Implementation of the ideal gas heat capacity (de Broglie wavelength)
//! of [Joback and Reid, 1987](https://doi.org/10.1080/00986448708960487).
use feos_core::parameter::{FromSegments, Parameters};
use feos_core::{FeosResult, IdealGas, IdealGasAD, ReferenceSystem};
use feos_core::{FeosResult, IdealGas, ReferenceSystem};
use nalgebra::DVector;
use num_dual::*;
use quantity::{MolarEntropy, Temperature};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::Mul;

/// Coefficients used in the Joback model.
///
Expand Down Expand Up @@ -95,11 +94,11 @@ impl Joback {
.sum();
Ok(c_p / RGAS * quantity::RGAS)
}
}

fn ln_lambda3<D: DualNum<f64> + Copy + Mul<D1, Output = D>, D1: Copy>(
[a, b, c, d, e]: [D1; 5],
temperature: D,
) -> D {
impl<D: DualNum<f64> + Copy> IdealGas<D> for Joback<D> {
fn ln_lambda3<D2: DualNum<f64, Inner = D> + Copy>(&self, temperature: D2) -> D2 {
let [a, b, c, d, e] = self.0.each_ref().map(D2::from_inner);
let t = temperature;
let t2 = t * t;
let t4 = t2 * t2;
Expand All @@ -116,12 +115,6 @@ impl Joback {
+ (t / T0).ln() * a;
(h - t * s) / (t * RGAS) + f
}
}

impl IdealGas for Joback {
fn ln_lambda3<D: DualNum<f64> + Copy>(&self, temperature: D) -> D {
Self::ln_lambda3(self.0, temperature)
}

fn ideal_gas_model(&self) -> &'static str {
"Ideal gas (Joback)"
Expand Down Expand Up @@ -166,20 +159,6 @@ impl<D: DualNum<f64> + Copy> Joback<D> {
}
}

impl<D: DualNum<f64> + Copy> IdealGasAD<D> for Joback<D> {
fn ln_lambda3_ad<D2: DualNum<f64, Inner = D> + Copy>(&self, temperature: D2) -> D2 {
Joback::ln_lambda3(self.0.each_ref().map(D2::from_inner), temperature)
}

fn ln_lambda3(&self, temperature: D) -> D {
Joback::ln_lambda3(self.0, temperature)
}

fn ideal_gas_model(&self) -> &'static str {
"Ideal gas (Joback)"
}
}

const RGAS: f64 = 6.022140857 * 1.38064852;
const T0: f64 = 298.15;
const T0_2: f64 = T0 * T0;
Expand Down