Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
small refactors and updated example notebooks
  • Loading branch information
prehner committed Jun 20, 2025
commit 6a8bacaa80eb2ef41d2d7f166cc33a5d30a11f5f
2 changes: 1 addition & 1 deletion crates/feos-core/src/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::f64::consts::SQRT_2;
const KB_A3: f64 = 13806490.0;

/// Peng-Robinson parameters for a single substance.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PengRobinsonRecord {
/// critical temperature in Kelvin
tc: f64,
Expand Down
26 changes: 13 additions & 13 deletions crates/feos-core/src/parameter/association.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Binary, BinaryRecord, GroupCount, Pure};
use super::{BinaryParameters, BinaryRecord, GroupCount, PureParameters};
use crate::{FeosError, FeosResult, parameter::PureRecord};
use arrayvec::ArrayString;
use ndarray::Array1;
Expand Down Expand Up @@ -103,8 +103,8 @@ pub struct AssociationParameters<A> {
pub sites_a: Array1<AssociationSite<Option<A>>>,
pub sites_b: Array1<AssociationSite<Option<A>>>,
pub sites_c: Array1<AssociationSite<Option<A>>>,
pub binary_ab: Vec<Binary<A, ()>>,
pub binary_cc: Vec<Binary<A, ()>>,
pub binary_ab: Vec<BinaryParameters<A, ()>>,
pub binary_cc: Vec<BinaryParameters<A, ()>>,
}

impl<A: Clone> AssociationParameters<A> {
Expand Down Expand Up @@ -175,14 +175,14 @@ impl<A: Clone> AssociationParameters<A> {
)));
}
if let (Some(x), Some(y)) = (indices_a.get(&(i, a)), indices_b.get(&(j, b))) {
binary_ab.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_ab.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
}
if let (Some(y), Some(x)) = (indices_b.get(&(i, a)), indices_a.get(&(j, b))) {
binary_ab.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_ab.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
}
if let (Some(x), Some(y)) = (indices_c.get(&(i, a)), indices_c.get(&(j, b))) {
binary_cc.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_cc.push(Binary::new(*y, *x, record.parameters.clone(), ()));
binary_cc.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
binary_cc.push(BinaryParameters::new(*y, *x, record.parameters.clone(), ()));
}
}
}
Expand All @@ -199,9 +199,9 @@ impl<A: Clone> AssociationParameters<A> {
}

pub fn new_hetero<P, C: GroupCount>(
pure_records: &[Pure<P, C>],
pure_records: &[PureParameters<P, C>],
association_sites: &[Vec<AssociationRecord<A>>],
binary_records: &[Binary<Vec<BinaryAssociationRecord<A>>, ()>],
binary_records: &[BinaryParameters<Vec<BinaryAssociationRecord<A>>, ()>],
) -> FeosResult<Self> {
let mut sites_a = Vec::new();
let mut sites_b = Vec::new();
Expand Down Expand Up @@ -269,14 +269,14 @@ impl<A: Clone> AssociationParameters<A> {
)));
}
if let (Some(x), Some(y)) = (indices_a.get(&(i, a)), indices_b.get(&(j, b))) {
binary_ab.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_ab.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
}
if let (Some(y), Some(x)) = (indices_b.get(&(i, a)), indices_a.get(&(j, b))) {
binary_ab.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_ab.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
}
if let (Some(x), Some(y)) = (indices_c.get(&(i, a)), indices_c.get(&(j, b))) {
binary_cc.push(Binary::new(*x, *y, record.parameters.clone(), ()));
binary_cc.push(Binary::new(*y, *x, record.parameters.clone(), ()));
binary_cc.push(BinaryParameters::new(*x, *y, record.parameters.clone(), ()));
binary_cc.push(BinaryParameters::new(*y, *x, record.parameters.clone(), ()));
}
}
}
Expand Down
68 changes: 0 additions & 68 deletions crates/feos-core/src/parameter/chemical_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,71 +187,3 @@ impl GroupCount for () {
1.0
}
}

// pub trait CountType: Copy {
// fn apply_count(self, x: f64) -> f64;
// }

// impl CountType for usize {
// fn apply_count(self, x: f64) -> f64 {
// self as f64 * x
// }
// }

// impl CountType for f64 {
// fn apply_count(self, x: f64) -> f64 {
// self * x
// }
// }

// /// Trait that enables parameter generation from generic molecular representations.
// pub trait SegmentCount {
// type Count: CountType;

// fn identifier(&self) -> Cow<Identifier>;

// /// Count the number of occurences of each individual segment identifier in the
// /// molecule.
// ///
// /// The map contains the segment identifier as key and the count as value.
// fn segment_count(&self) -> Cow<HashMap<String, Self::Count>>;

// /// Count the number of occurences of each individual segment in the
// /// molecule.
// ///
// /// The map contains the segment record as key and the count as value.
// #[expect(clippy::type_complexity)]
// fn segment_map<M: Clone, A: Clone>(
// &self,
// segment_records: &[SegmentRecord<M, A>],
// ) -> FeosResult<Vec<(SegmentRecord<M, A>, Self::Count)>> {
// let count = self.segment_count();
// let queried: HashSet<_> = count.keys().collect();
// let mut segments: HashMap<_, SegmentRecord<M, A>> = segment_records
// .iter()
// .map(|r| (&r.identifier, r.clone()))
// .collect();
// let available = segments.keys().copied().collect();
// if !queried.is_subset(&available) {
// let missing: Vec<_> = queried.difference(&available).collect();
// let msg = format!("{:?}", missing);
// return Err(FeosError::ComponentsNotFound(msg));
// };
// Ok(count
// .iter()
// .map(|(s, c)| (segments.remove(s).unwrap(), *c))
// .collect())
// }
// }

// impl SegmentCount for ChemicalRecord {
// type Count = usize;

// fn identifier(&self) -> Cow<Identifier> {
// Cow::Borrowed(&self.identifier)
// }

// fn segment_count(&self) -> Cow<HashMap<String, usize>> {
// Cow::Owned(self.segment_count())
// }
// }
67 changes: 35 additions & 32 deletions crates/feos-core/src/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,19 @@ pub use association::{AssociationParameters, AssociationRecord, BinaryAssociatio
pub use chemical_record::{ChemicalRecord, GroupCount};
pub use identifier::{Identifier, IdentifierOption};
pub use model_record::{
BinaryRecord, BinarySegmentRecord, FromSegments, FromSegmentsBinary, ModelRecord, PureRecord,
BinaryRecord, BinarySegmentRecord, FromSegments, FromSegmentsBinary, PureRecord, Record,
SegmentRecord,
};

#[derive(Clone)]
pub struct Pure<M, C> {
pub struct PureParameters<M, C> {
pub identifier: String,
pub model_record: M,
pub count: C,
pub component_index: usize,
}

#[derive(Clone, Copy)]
pub struct Binary<B, C> {
pub id1: usize,
pub id2: usize,
pub model_record: B,
pub count: C,
}

impl<B, C> Binary<B, C> {
pub fn new(id1: usize, id2: usize, model_record: B, count: C) -> Self {
Self {
id1,
id2,
model_record,
count,
}
}
}

impl<M: Clone> Pure<M, ()> {
impl<M: Clone> PureParameters<M, ()> {
fn from_pure_record(model_record: M, component_index: usize) -> Self {
Self {
identifier: "".into(),
Expand All @@ -63,7 +44,7 @@ impl<M: Clone> Pure<M, ()> {
}
}

impl<M: Clone, C> Pure<M, C> {
impl<M: Clone, C> PureParameters<M, C> {
fn from_segment_record<A>(
segment: &SegmentRecord<M, A>,
count: C,
Expand All @@ -78,10 +59,29 @@ impl<M: Clone, C> Pure<M, C> {
}
}

#[derive(Clone, Copy)]
pub struct BinaryParameters<B, C> {
pub id1: usize,
pub id2: usize,
pub model_record: B,
pub count: C,
}

impl<B, C> BinaryParameters<B, C> {
pub fn new(id1: usize, id2: usize, model_record: B, count: C) -> Self {
Self {
id1,
id2,
model_record,
count,
}
}
}

pub struct ParametersBase<P, B, A, Bo, C> {
pub pure: Vec<Pure<P, C>>,
pub binary: Vec<Binary<B, ()>>,
pub bonds: Vec<Binary<Bo, C>>,
pub pure: Vec<PureParameters<P, C>>,
pub binary: Vec<BinaryParameters<B, ()>>,
pub bonds: Vec<BinaryParameters<Bo, C>>,
pub association: AssociationParameters<A>,
pub identifiers: Vec<Identifier>,
pub molar_weight: MolarWeight<Array1<f64>>,
Expand Down Expand Up @@ -163,14 +163,17 @@ impl<P: Clone, B: Clone, A: Clone> Parameters<P, B, A> {
.map(|(i, pr)| {
(
(pr.identifier, pr.molarweight),
Pure::from_pure_record(pr.model_record, i),
PureParameters::from_pure_record(pr.model_record, i),
)
})
.unzip();
let (identifiers, molar_weight): (_, Vec<_>) = identifiers.into_iter().unzip();
let binary_records = binary_records
.into_iter()
.filter_map(|br| br.model_record.map(|m| Binary::new(br.id1, br.id2, m, ())))
.filter_map(|br| {
br.model_record
.map(|m| BinaryParameters::new(br.id1, br.id2, m, ()))
})
.collect();

Ok(Self {
Expand Down Expand Up @@ -537,7 +540,7 @@ impl<P: Clone, B: Clone, A: Clone, Bo: Clone, C: GroupCount + Default>
)));
};
molar_weight[i] += segment.molarweight * c.into_f64();
groups.push(Pure::from_segment_record(segment, *c, i));
groups.push(PureParameters::from_segment_record(segment, *c, i));
association_sites.push(segment.association_sites.clone());
}
for ([a, b], c) in bond_counts {
Expand All @@ -553,7 +556,7 @@ impl<P: Clone, B: Clone, A: Clone, Bo: Clone, C: GroupCount + Default>
"No bond record found for {id1}-{id2}"
)));
};
bonds.push(Binary::new(a + n, b + n, bond.clone(), c));
bonds.push(BinaryParameters::new(a + n, b + n, bond.clone(), c));
}
}

Expand All @@ -574,10 +577,10 @@ impl<P: Clone, B: Clone, A: Clone, Bo: Clone, C: GroupCount + Default>
let id2 = &s2.identifier;
if let Some(&br) = binary_segment_records_map.get(&(id1, id2)) {
if let Some(br) = &br.model_record {
binary_records.push(Binary::new(i1, i2, br.clone(), ()));
binary_records.push(BinaryParameters::new(i1, i2, br.clone(), ()));
}
if !br.association_sites.is_empty() {
binary_association_records.push(Binary::new(
binary_association_records.push(BinaryParameters::new(
i1,
i2,
br.association_sites.clone(),
Expand Down
8 changes: 4 additions & 4 deletions crates/feos-core/src/parameter/model_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::path::Path;

/// A collection of parameters with an arbitrary identifier.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ModelRecord<I, M, A> {
pub struct Record<I, M, A> {
pub identifier: I,
#[serde(skip_serializing_if = "f64::is_zero")]
#[serde(default)]
Expand All @@ -27,12 +27,12 @@ pub struct ModelRecord<I, M, A> {
}

/// A collection of parameters of a pure substance.
pub type PureRecord<M, A> = ModelRecord<Identifier, M, A>;
pub type PureRecord<M, A> = Record<Identifier, M, A>;

/// Parameters describing an individual segment of a molecule.
pub type SegmentRecord<M, A> = ModelRecord<String, M, A>;
pub type SegmentRecord<M, A> = Record<String, M, A>;

impl<I, M, A> ModelRecord<I, M, A> {
impl<I, M, A> Record<I, M, A> {
/// Create a new `ModelRecord`.
pub fn new(identifier: I, molarweight: f64, model_record: M) -> Self {
Self::with_association(identifier, molarweight, model_record, vec![])
Expand Down
4 changes: 2 additions & 2 deletions crates/feos/src/ideal_gas/dippr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use feos_core::parameter::{Parameters, Pure};
use feos_core::parameter::{Parameters, PureParameters};
use feos_core::{Components, FeosResult, IdealGas};
use ndarray::Array1;
use num_dual::DualNum;
Expand Down Expand Up @@ -117,7 +117,7 @@ pub type DipprParameters = Parameters<DipprRecord, (), ()>;

/// Ideal gas equations of state based on DIPPR equations for
/// ideal gas heat capacities.
pub struct Dippr(Vec<Pure<DipprRecord, ()>>);
pub struct Dippr(Vec<PureParameters<DipprRecord, ()>>);

impl Dippr {
pub fn new(parameters: DipprParameters) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions crates/feos/src/ideal_gas/joback.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! 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, Pure};
use feos_core::parameter::{FromSegments, Parameters, PureParameters};
use feos_core::{Components, FeosResult, IdealGas, ReferenceSystem};
use ndarray::Array1;
use num_dual::*;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub type JobackParameters = Parameters<JobackRecord, (), ()>;
/// - T = 289.15 K
/// - p = 1e5 Pa
/// - V = 1e-30 A³
pub struct Joback(Vec<Pure<JobackRecord, ()>>);
pub struct Joback(Vec<PureParameters<JobackRecord, ()>>);

impl Joback {
pub fn new(parameters: JobackParameters) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/feos/src/pcsaft/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl FromSegmentsBinary for PcSaftBinaryRecord {
/// Parameter set required for the PC-SAFT equation of state and Helmholtz energy functional.
pub type PcSaftParameters = Parameters<PcSaftRecord, PcSaftBinaryRecord, PcSaftAssociationRecord>;

// The PC-SAFT parameters in an easier accessible format.
/// The PC-SAFT parameters in an easier accessible format.
pub struct PcSaftPars {
pub m: Array1<f64>,
pub sigma: Array1<f64>,
Expand Down
Loading