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
remove the ResidualConst trait
  • Loading branch information
prehner committed Sep 29, 2025
commit 935b651e9c9f03488212865718508aa1ffefd285
27 changes: 17 additions & 10 deletions crates/feos-core/src/equation_of_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use num_dual::DualNum;
use quantity::{Energy, MolarEnergy, Moles, Temperature, Volume};

mod residual;
pub use residual::{Molarweight, NoResidual, Residual, ResidualConst, ResidualDyn, Subset};
pub use residual::{Molarweight, NoResidual, Residual, ResidualDyn, Subset};

/// An equation of state consisting of an ideal gas model
/// and a residual Helmholtz energy model.
Expand Down Expand Up @@ -79,10 +79,13 @@ impl<I: Clone, R: Subset> Subset for EquationOfState<Vec<I>, R> {
}
}

impl<I: Clone, R: ResidualConst<N, D>, D: DualNum<f64> + Copy, const N: usize> ResidualConst<N, D>
for EquationOfState<[I; N], R>
impl<I: Clone, R: Residual<Const<N>, D>, D: DualNum<f64> + Copy, const N: usize>
Residual<Const<N>, D> for EquationOfState<[I; N], R>
{
const NAME: &str = R::NAME;
fn components(&self) -> usize {
N
}

type Real = EquationOfState<[I; N], R::Real>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = EquationOfState<[I; N], R::Lifted<D2>>;
fn re(&self) -> Self::Real {
Expand All @@ -96,6 +99,14 @@ impl<I: Clone, R: ResidualConst<N, D>, D: DualNum<f64> + Copy, const N: usize> R
self.residual.compute_max_density(molefracs)
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, Const<N>>,
) -> Vec<(&'static str, D)> {
self.residual
.reduced_helmholtz_energy_density_contributions(state)
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, Const<N>>) -> D {
self.residual
.reduced_residual_helmholtz_energy_density(state)
Expand Down Expand Up @@ -184,12 +195,8 @@ impl<
}
}

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>
impl<I: IdealGas<D> + Clone, R: Residual<Const<N>, D>, D: DualNum<f64> + Copy, const N: usize>
Total<Const<N>, D> for EquationOfState<[I; N], R>
{
type IdealGas = I;

Expand Down
50 changes: 1 addition & 49 deletions crates/feos-core/src/equation_of_state/residual.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{FeosError, FeosResult, ReferenceSystem, state::StateHD};
use nalgebra::{
Const, DVector, DefaultAllocator, Dim, Dyn, OMatrix, OVector, SVector, U1, allocator::Allocator,
};
use nalgebra::{DVector, DefaultAllocator, Dim, Dyn, OMatrix, OVector, U1, allocator::Allocator};
use num_dual::{DualNum, Gradients, partial, partial2, second_derivative, third_derivative};
use quantity::ad::first_derivative;
use quantity::*;
Expand Down Expand Up @@ -83,52 +81,6 @@ impl<C: Deref<Target = T> + Clone, T: ResidualDyn, D: DualNum<f64> + Copy> Resid
}
}

pub trait ResidualConst<const N: usize, D: DualNum<f64> + Copy>: Clone {
const NAME: &str;
type Real: ResidualConst<N, f64>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy>: ResidualConst<N, D2>;
fn re(&self) -> Self::Real;
fn lift<D2: DualNum<f64, Inner = D> + Copy>(&self) -> Self::Lifted<D2>;
fn compute_max_density(&self, molefracs: &SVector<D, N>) -> D;
fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, Const<N>>) -> D;
}

impl<T: ResidualConst<N, D>, const N: usize, D: DualNum<f64> + Copy> Residual<Const<N>, D> for T {
fn components(&self) -> usize {
N
}

type Real = T::Real;

type Lifted<D2: DualNum<f64, Inner = D> + Copy> = T::Lifted<D2>;

fn re(&self) -> Self::Real {
T::re(self)
}

fn lift<D2: DualNum<f64, Inner = D> + Copy>(&self) -> Self::Lifted<D2> {
T::lift(self)
}

fn compute_max_density(&self, molefracs: &SVector<D, N>) -> D {
T::compute_max_density(self, molefracs)
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, Const<N>>,
) -> Vec<(&'static str, D)> {
vec![(
T::NAME,
T::reduced_residual_helmholtz_energy_density(self, state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, Const<N>>) -> D {
T::reduced_residual_helmholtz_energy_density(self, state)
}
}

/// A residual Helmholtz energy model.
pub trait Residual<N: Dim = Dyn, D: DualNum<f64> + Copy = f64>: Clone
where
Expand Down
3 changes: 1 addition & 2 deletions crates/feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ mod parameter_fit;
mod phase_equilibria;
mod state;
pub use equation_of_state::{
EquationOfState, IdealGas, Molarweight, NoResidual, Residual, ResidualConst, ResidualDyn,
Subset, Total,
EquationOfState, IdealGas, Molarweight, NoResidual, Residual, ResidualDyn, Subset, Total,
};
pub use errors::{FeosError, FeosResult};
pub use parameter_fit::{BinaryModel, ParametersAD, PureModel};
Expand Down
18 changes: 15 additions & 3 deletions crates/feos/src/gc_pcsaft/eos/ad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::dispersion::{A0, A1, A2, B0, B1, B2};
use super::polar::{AD, BD, CD};
use feos_core::{ResidualConst, StateHD};
use feos_core::{Residual, StateHD};
use nalgebra::{Const, SMatrix, SVector};
use num_dual::DualNum;
use quantity::{JOULE, KB, KELVIN};
Expand Down Expand Up @@ -220,8 +220,10 @@ impl<D: DualNum<f64> + Copy, const N: usize> GcPcSaftADParameters<D, N> {
#[derive(Clone)]
pub struct GcPcSaftAD<D, const N: usize>(pub GcPcSaftADParameters<D, N>);

impl<D: DualNum<f64> + Copy, const N: usize> ResidualConst<N, D> for GcPcSaftAD<D, N> {
const NAME: &str = "gc-PC-SAFT";
impl<D: DualNum<f64> + Copy, const N: usize> Residual<Const<N>, D> for GcPcSaftAD<D, N> {
fn components(&self) -> usize {
N
}

type Real = GcPcSaftAD<f64, N>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = GcPcSaftAD<D2, N>;
Expand All @@ -246,6 +248,16 @@ impl<D: DualNum<f64> + Copy, const N: usize> ResidualConst<N, D> for GcPcSaftAD<
((msigma3 * molefracs).into_scalar() * FRAC_PI_6).recip() * MAX_ETA
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, Const<N>>,
) -> Vec<(&'static str, D)> {
vec![(
"gc-PC-SAFT",
self.reduced_residual_helmholtz_energy_density(state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, Const<N>>) -> D {
let GcPcSaftADParameters { groups, bonds } = &self.0;
let density = &state.partial_density;
Expand Down
15 changes: 12 additions & 3 deletions crates/feos/src/ideal_gas/joback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ pub mod test_ad {
use super::{Joback, JobackParameters, JobackRecord};
use approx::assert_relative_eq;
use feos_core::{Contributions::IdealGas, EquationOfState, FeosResult, State};
use feos_core::{ResidualConst, StateHD};
use feos_core::{Residual, StateHD};
use nalgebra::{SVector, U1};
use num_dual::DualNum;
use quantity::{KELVIN, KILO, METER, MOL};
Expand All @@ -342,8 +342,10 @@ pub mod test_ad {
#[derive(Clone, Copy)]
struct NoResidual;

impl<D: DualNum<f64> + Copy> ResidualConst<1, D> for NoResidual {
const NAME: &str = "";
impl<D: DualNum<f64> + Copy> Residual<U1, D> for NoResidual {
fn components(&self) -> usize {
1
}

type Real = Self;

Expand All @@ -361,6 +363,13 @@ pub mod test_ad {
D::from(1.0)
}

fn reduced_helmholtz_energy_density_contributions(
&self,
_: &StateHD<D, U1>,
) -> Vec<(&'static str, D)> {
vec![]
}

fn reduced_residual_helmholtz_energy_density(&self, _: &StateHD<D, U1>) -> D {
D::from(0.0)
}
Expand Down
36 changes: 31 additions & 5 deletions crates/feos/src/pcsaft/eos/pcsaft_binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::dispersion::{A0, A1, A2, B0, B1, B2};
use super::polar::{AD, BD, CD};
use feos_core::{ParametersAD, ResidualConst, StateHD};
use feos_core::{ParametersAD, Residual, StateHD};
use nalgebra::{SVector, U2};
use num_dual::{DualNum, DualSVec, DualSVec64, DualVec, jacobian};
use std::f64::consts::{FRAC_PI_6, PI};
Expand Down Expand Up @@ -361,8 +361,11 @@ fn helmholtz_energy_density<D: DualNum<f64> + Copy>(
}
}

impl<D: DualNum<f64> + Copy> ResidualConst<2, D> for PcSaftBinary<D, 4> {
const NAME: &str = "PC-SAFT (binary, non-assoc)";
impl<D: DualNum<f64> + Copy> Residual<U2, D> for PcSaftBinary<D, 4> {
fn components(&self) -> usize {
2
}

type Real = PcSaftBinary<f64, 4>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = PcSaftBinary<D2, 4>;
fn re(&self) -> Self::Real {
Expand All @@ -389,6 +392,16 @@ impl<D: DualNum<f64> + Copy> ResidualConst<2, D> for PcSaftBinary<D, 4> {
((m1 * sigma1.powi(3) * x1 + m2 * sigma2.powi(3) * x2) * FRAC_PI_6).recip() * MAX_ETA
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, U2>,
) -> Vec<(&'static str, D)> {
vec![(
"PC-SAFT (binary, non-assoc)",
self.reduced_residual_helmholtz_energy_density(state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, U2>) -> D {
let ([p1, p2], kij) = self.0;
let [m1, sigma1, epsilon_k1, mu1] = p1;
Expand All @@ -405,8 +418,11 @@ impl<D: DualNum<f64> + Copy> ResidualConst<2, D> for PcSaftBinary<D, 4> {
}
}

impl<D: DualNum<f64> + Copy> ResidualConst<2, D> for PcSaftBinary<D, 8> {
const NAME: &str = "PC-SAFT (binary)";
impl<D: DualNum<f64> + Copy> Residual<U2, D> for PcSaftBinary<D, 8> {
fn components(&self) -> usize {
2
}

type Real = PcSaftBinary<f64, 8>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = PcSaftBinary<D2, 8>;
fn re(&self) -> Self::Real {
Expand All @@ -433,6 +449,16 @@ impl<D: DualNum<f64> + Copy> ResidualConst<2, D> for PcSaftBinary<D, 8> {
((m1 * sigma1.powi(3) * x1 + m2 * sigma2.powi(3) * x2) * FRAC_PI_6).recip() * MAX_ETA
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, U2>,
) -> Vec<(&'static str, D)> {
vec![(
"PC-SAFT (binary)",
self.reduced_residual_helmholtz_energy_density(state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, U2>) -> D {
let ([p1, p2], kij) = self.0;
let [
Expand Down
36 changes: 31 additions & 5 deletions crates/feos/src/pcsaft/eos/pcsaft_pure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::dispersion::{A0, A1, A2, B0, B1, B2};
use super::polar::{AD, BD, CD};
use feos_core::{ParametersAD, ResidualConst, StateHD};
use feos_core::{ParametersAD, Residual, StateHD};
use nalgebra::{SVector, U1};
use num_dual::{DualNum, DualSVec64};
use std::f64::consts::{FRAC_PI_6, PI};
Expand Down Expand Up @@ -112,8 +112,11 @@ fn helmholtz_energy_density<D: DualNum<f64> + Copy>(
non_assoc + assoc
}

impl<D: DualNum<f64> + Copy> ResidualConst<1, D> for PcSaftPure<D, 8> {
const NAME: &str = "PC-SAFT (pure)";
impl<D: DualNum<f64> + Copy> Residual<U1, D> for PcSaftPure<D, 8> {
fn components(&self) -> usize {
1
}

type Real = PcSaftPure<f64, 8>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = PcSaftPure<D2, 8>;
fn re(&self) -> Self::Real {
Expand All @@ -128,14 +131,27 @@ impl<D: DualNum<f64> + Copy> ResidualConst<1, D> for PcSaftPure<D, 8> {
(m * sigma.powi(3) * FRAC_PI_6).recip() * MAX_ETA
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, U1>,
) -> Vec<(&'static str, D)> {
vec![(
"PC-SAFT (pure)",
self.reduced_residual_helmholtz_energy_density(state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, U1>) -> D {
let density = state.partial_density.data.0[0][0];
helmholtz_energy_density(&self.0, state.temperature, density)
}
}

impl<D: DualNum<f64> + Copy> ResidualConst<1, D> for PcSaftPure<D, 4> {
const NAME: &str = "PC-SAFT (pure, non-assoc)";
impl<D: DualNum<f64> + Copy> Residual<U1, D> for PcSaftPure<D, 4> {
fn components(&self) -> usize {
1
}

type Real = PcSaftPure<f64, 4>;
type Lifted<D2: DualNum<f64, Inner = D> + Copy> = PcSaftPure<D2, 4>;
fn re(&self) -> Self::Real {
Expand All @@ -150,6 +166,16 @@ impl<D: DualNum<f64> + Copy> ResidualConst<1, D> for PcSaftPure<D, 4> {
(m * sigma.powi(3) * FRAC_PI_6).recip() * MAX_ETA
}

fn reduced_helmholtz_energy_density_contributions(
&self,
state: &StateHD<D, U1>,
) -> Vec<(&'static str, D)> {
vec![(
"PC-SAFT (pure, non-assoc)",
self.reduced_residual_helmholtz_energy_density(state),
)]
}

fn reduced_residual_helmholtz_energy_density(&self, state: &StateHD<D, U1>) -> D {
let density = state.partial_density.data.0[0][0];
let [m, sigma, epsilon_k, mu] = self.0;
Expand Down
Loading