Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 2cdc65e

Browse files
authored
Remove ChemicalRecord and make PcSaftBinary generic
1 parent 68db198 commit 2cdc65e

File tree

7 files changed

+406
-321
lines changed

7 files changed

+406
-321
lines changed

src/core/phase_equilibria.rs

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'a, R: ResidualHelmholtzEnergy<N>, D: DualNum<f64> + Copy, const N: usize>
264264
mod test {
265265
use super::*;
266266
use crate::eos::pcsaft::test::pcsaft;
267-
use crate::eos::{ChemicalRecord, GcPcSaft, GcPcSaftParameters, Joback};
267+
use crate::eos::{GcPcSaft, GcPcSaftParameters, Joback};
268268
use crate::EquationOfStateAD;
269269
use approx::assert_relative_eq;
270270
use feos_core::{Contributions, DensityInitialization, EosResult, PhaseEquilibrium};
@@ -328,44 +328,49 @@ mod test {
328328
Ok(())
329329
}
330330

331-
fn acetone() -> ChemicalRecord<f64> {
331+
fn acetone_pentane_parameters() -> GcPcSaftParameters<f64, 2> {
332+
let mut groups1 = HashMap::new();
333+
groups1.insert("CH3", 2.0);
334+
groups1.insert(">C=O", 1.0);
335+
let mut bonds1 = HashMap::new();
336+
bonds1.insert(["CH3", ">C=O"], 2.0);
337+
let mut groups2 = HashMap::new();
338+
groups2.insert("CH3", 2.0);
339+
groups2.insert("CH2", 3.0);
340+
let mut bonds2 = HashMap::new();
341+
bonds2.insert(["CH3", "CH2"], 2.0);
342+
bonds2.insert(["CH2", "CH2"], 2.0);
343+
344+
GcPcSaftParameters::from_groups([&groups1, &groups2], [&bonds1, &bonds2])
345+
}
346+
347+
fn acetone_groups() -> HashMap<&'static str, f64> {
332348
let mut groups = HashMap::new();
333349
groups.insert("CH3", 2.0);
334350
groups.insert(">C=O", 1.0);
335-
let mut bonds = HashMap::new();
336-
bonds.insert(["CH3", ">C=O"], 2.0);
337-
ChemicalRecord::new(groups, bonds)
351+
groups
338352
}
339353

340-
fn pentane() -> ChemicalRecord<f64> {
354+
fn pentane_groups() -> HashMap<&'static str, f64> {
341355
let mut groups = HashMap::new();
342356
groups.insert("CH3", 2.0);
343357
groups.insert("CH2", 3.0);
344-
let mut bonds = HashMap::new();
345-
bonds.insert(["CH3", "CH2"], 2.0);
346-
bonds.insert(["CH2", "CH2"], 2.0);
347-
ChemicalRecord::new(groups, bonds)
358+
groups
348359
}
349360

350361
#[test]
351362
fn test_dew_point() -> EosResult<()> {
352-
let params = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
353-
acetone(),
354-
pentane(),
355-
]));
363+
let params = GcPcSaft(acetone_pentane_parameters());
356364
let joback = [
357-
Joback(Joback::from_chemical_record(&acetone())),
358-
Joback(Joback::from_chemical_record(&pentane())),
365+
Joback(Joback::from_group_counts(&acetone_groups())),
366+
Joback(Joback::from_group_counts(&pentane_groups())),
359367
];
360368

361369
let mut params_dual = params.params::<Dual64>();
362370
params_dual.groups[0].eps = 1.0;
363371
let joback_dual = joback.map(|j| j.params());
364372

365-
let mut params_h = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
366-
acetone(),
367-
pentane(),
368-
]));
373+
let mut params_h = GcPcSaft(acetone_pentane_parameters());
369374
let h = 1e-7;
370375
params_h.0.groups[0] += h;
371376

@@ -432,23 +437,17 @@ mod test {
432437

433438
#[test]
434439
fn test_bubble_point() -> EosResult<()> {
435-
let params = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
436-
acetone(),
437-
pentane(),
438-
]));
440+
let params = GcPcSaft(acetone_pentane_parameters());
439441
let joback = [
440-
Joback(Joback::from_chemical_record(&acetone())),
441-
Joback(Joback::from_chemical_record(&pentane())),
442+
Joback(Joback::from_group_counts(&acetone_groups())),
443+
Joback(Joback::from_group_counts(&pentane_groups())),
442444
];
443445

444446
let mut params_dual = params.params::<Dual64>();
445447
params_dual.groups[0].eps = 1.0;
446448
let joback_dual = joback.map(|j| j.params());
447449

448-
let mut params_h = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
449-
acetone(),
450-
pentane(),
451-
]));
450+
let mut params_h = GcPcSaft(acetone_pentane_parameters());
452451
let h = 1e-7;
453452
params_h.0.groups[0] += h;
454453

@@ -515,23 +514,17 @@ mod test {
515514

516515
#[test]
517516
fn test_tp_flash() -> EosResult<()> {
518-
let params = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
519-
acetone(),
520-
pentane(),
521-
]));
517+
let params = GcPcSaft(acetone_pentane_parameters());
522518
let joback = [
523-
Joback(Joback::from_chemical_record(&acetone())),
524-
Joback(Joback::from_chemical_record(&pentane())),
519+
Joback(Joback::from_group_counts(&acetone_groups())),
520+
Joback(Joback::from_group_counts(&pentane_groups())),
525521
];
526522

527523
let mut params_dual = params.params::<Dual64>();
528524
params_dual.groups[0].eps = 1.0;
529525
let joback_dual = joback.map(|j| j.params());
530526

531-
let mut params_h = GcPcSaft(GcPcSaftParameters::from_chemical_records(&[
532-
acetone(),
533-
pentane(),
534-
]));
527+
let mut params_h = GcPcSaft(acetone_pentane_parameters());
535528
let h = 1e-5;
536529
params_h.0.groups[0] += h;
537530

src/eos/gc_pcsaft.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::pcsaft::{A0, A1, A2, AD, B0, B1, B2, BD, CD};
2-
use crate::eos::ChemicalRecord;
32
use crate::{ParametersAD, ResidualHelmholtzEnergy};
43
use nalgebra::{SMatrix, SVector};
54
use num_dual::DualNum;
@@ -196,20 +195,19 @@ impl<D: DualNum<f64> + Copy, const N: usize> GcPcSaftParameters<D, N> {
196195
}
197196

198197
impl<D: DualNum<f64> + Copy, const N: usize> GcPcSaftParameters<D, N> {
199-
pub fn from_chemical_records(records: &[ChemicalRecord<D>; N]) -> Self {
200-
let groups = SMatrix::from(
201-
records
202-
.each_ref()
203-
.map(|r| GROUPS.map(|g| *r.groups.get(g).unwrap_or(&D::zero()))),
204-
);
198+
pub fn from_groups(
199+
group_map: [&HashMap<&'static str, D>; N],
200+
bond_map: [&HashMap<[&'static str; 2], D>; N],
201+
) -> Self {
202+
let groups =
203+
SMatrix::from(group_map.map(|r| GROUPS.map(|g| *r.get(g).unwrap_or(&D::zero()))));
205204
let group_indices: HashMap<_, _> = GROUPS
206205
.into_iter()
207206
.enumerate()
208207
.map(|(g, i)| (i, g))
209208
.collect();
210-
let bonds = records.each_ref().map(|r| {
211-
r.bonds
212-
.iter()
209+
let bonds = bond_map.map(|r| {
210+
r.iter()
213211
.map(|([g1, g2], &c)| ([group_indices[g1], group_indices[g2]], c))
214212
.collect()
215213
});
@@ -465,8 +463,8 @@ fn triplet_integral<D: DualNum<f64> + Copy>(mij1: D, mij2: D, eta: D) -> D {
465463
#[cfg(test)]
466464
pub mod test {
467465
use super::{
468-
ChemicalRecord as ChemicalRecordAD, GcPcSaft as GcPcSaftAD, GcPcSaftParameters,
469-
ResidualHelmholtzEnergy, EPSILON_K, GROUPS, M, MU, SIGMA,
466+
GcPcSaft as GcPcSaftAD, GcPcSaftParameters, ResidualHelmholtzEnergy, EPSILON_K, GROUPS, M,
467+
MU, SIGMA,
470468
};
471469
use approx::assert_relative_eq;
472470
use feos::gc_pcsaft::{GcPcSaft, GcPcSaftEosParameters, GcPcSaftRecord};
@@ -519,8 +517,7 @@ pub mod test {
519517
bonds.insert(["CH3", ">C=O"], 1.0);
520518
bonds.insert([">C=O", "CH2"], 1.0);
521519
bonds.insert(["CH2", "CH3"], 1.0);
522-
let cr = ChemicalRecordAD { groups, bonds };
523-
let params = GcPcSaftParameters::from_chemical_records(&[cr]);
520+
let params = GcPcSaftParameters::from_groups([&groups], [&bonds]);
524521
Ok((params, eos))
525522
}
526523

src/eos/ideal_gas.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::eos::ChemicalRecord;
21
use crate::{IdealGasAD, ParametersAD};
32
use num_dual::DualNum;
3+
use std::collections::HashMap;
44

55
const RGAS: f64 = 6.022140857 * 1.38064852;
66
const T0: f64 = 298.15;
@@ -49,10 +49,8 @@ impl Joback {
4949
[a - 37.93, b + 0.21, c - 3.91e-4, d + 2.06e-7, D::zero()]
5050
}
5151

52-
pub fn from_chemical_record<D: DualNum<f64> + Copy>(
53-
group_counts: &ChemicalRecord<D>,
54-
) -> [D; 5] {
55-
Self::from_groups(GROUPS.map(|g| *group_counts.groups.get(g).unwrap_or(&D::zero())))
52+
pub fn from_group_counts<D: DualNum<f64> + Copy>(group_counts: &HashMap<&str, D>) -> [D; 5] {
53+
Self::from_groups(GROUPS.map(|g| *group_counts.get(g).unwrap_or(&D::zero())))
5654
}
5755
}
5856

src/eos/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
//! A collection of equation of state models.
2-
use std::collections::HashMap;
32
43
mod gc_pcsaft;
54
pub(crate) mod ideal_gas;
65
pub(crate) mod pcsaft;
76
pub use gc_pcsaft::{GcPcSaft, GcPcSaftParameters};
87
pub use ideal_gas::Joback;
98
pub use pcsaft::{PcSaftBinary, PcSaftPure};
10-
11-
/// Input for group-contribution models that allows for derivatives.
12-
pub struct ChemicalRecord<D> {
13-
pub groups: HashMap<&'static str, D>,
14-
pub bonds: HashMap<[&'static str; 2], D>,
15-
}
16-
17-
impl<D> ChemicalRecord<D> {
18-
pub fn new(groups: HashMap<&'static str, D>, bonds: HashMap<[&'static str; 2], D>) -> Self {
19-
Self { groups, bonds }
20-
}
21-
}

src/eos/pcsaft/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub mod test {
121121
Ok((PcSaftPure(params), eos))
122122
}
123123

124-
pub fn pcsaft_binary() -> EosResult<(PcSaftBinary, Arc<PcSaft>)> {
124+
pub fn pcsaft_binary() -> EosResult<(PcSaftBinary<8>, Arc<PcSaft>)> {
125125
let params = [
126126
[1.5, 3.4, 180.0, 2.2, 0.03, 2500., 2.0, 1.0],
127127
[2.5, 3.6, 250.0, 1.2, 0.015, 1500., 1.0, 2.0],

0 commit comments

Comments
 (0)