Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions feos-core/src/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ pub struct PengRobinsonRecord {
pc: f64,
/// acentric factor
acentric_factor: f64,
/// molar weight
molarweight: f64,
}

impl PengRobinsonRecord {
/// Create a new pure substance record for the Peng-Robinson equation of state.
pub fn new(tc: f64, pc: f64, acentric_factor: f64) -> Self {
pub fn new(tc: f64, pc: f64, acentric_factor: f64, molarweight: f64) -> Self {
Self {
tc,
pc,
acentric_factor,
molarweight,
}
}
}
Expand All @@ -43,7 +46,8 @@ impl std::fmt::Display for PengRobinsonRecord {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PengRobinsonRecord(tc={} K", self.tc)?;
write!(f, ", pc={} Pa", self.pc)?;
write!(f, ", acentric factor={}", self.acentric_factor)
write!(f, ", acentric factor={}", self.acentric_factor)?;
write!(f, ", molar weight={})", self.molarweight)
}
}

Expand Down Expand Up @@ -93,9 +97,10 @@ impl PengRobinsonParameters {
tc: tc[i],
pc: pc[i],
acentric_factor: acentric_factor[i],
molarweight: molarweight[i],
};
let id = Identifier::default();
PureRecord::new(id, molarweight[i], record)
PureRecord::new(id, record)
})
.collect();
PengRobinsonParameters::from_records(records, None)
Expand All @@ -120,8 +125,8 @@ impl Parameter for PengRobinsonParameters {
let mut kappa = Array1::zeros(n);

for (i, record) in pure_records.iter().enumerate() {
molarweight[i] = record.molarweight;
let r = &record.model_record;
molarweight[i] = r.molarweight;
tc[i] = r.tc;
a[i] = 0.45724 * r.tc.powi(2) * KB_A3 / r.pc;
b[i] = 0.07780 * r.tc * KB_A3 / r.pc;
Expand Down
8 changes: 4 additions & 4 deletions feos-core/src/equation_of_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ pub trait Components {
/// and a residual Helmholtz energy model.
#[derive(Clone)]
pub struct EquationOfState<I, R> {
pub ideal_gas: Arc<I>,
pub ideal_gas: I,
pub residual: Arc<R>,
}

impl<I, R> EquationOfState<I, R> {
/// Return a new [EquationOfState] with the given ideal gas
/// and residual models.
pub fn new(ideal_gas: Arc<I>, residual: Arc<R>) -> Self {
pub fn new(ideal_gas: I, residual: Arc<R>) -> Self {
Self {
ideal_gas,
residual,
Expand All @@ -45,7 +45,7 @@ impl<I, R> EquationOfState<I, R> {
impl<I: IdealGas> EquationOfState<I, NoResidual> {
/// Return a new [EquationOfState] that only consists of
/// an ideal gas models.
pub fn ideal_gas(ideal_gas: Arc<I>) -> Self {
pub fn ideal_gas(ideal_gas: I) -> Self {
let residual = Arc::new(NoResidual(ideal_gas.components()));
Self {
ideal_gas,
Expand All @@ -66,7 +66,7 @@ impl<I: Components, R: Components> Components for EquationOfState<I, R> {

fn subset(&self, component_list: &[usize]) -> Self {
Self::new(
Arc::new(self.ideal_gas.subset(component_list)),
self.ideal_gas.subset(component_list),
Arc::new(self.residual.subset(component_list)),
)
}
Expand Down
11 changes: 5 additions & 6 deletions feos-core/src/joback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ mod tests {
);
assert_relative_eq!(jr.e, 0.0);

let pr = PureRecord::new(Identifier::default(), 1.0, jr);
let joback = Arc::new(Joback::new(Arc::new(JobackParameters::new_pure(pr)?)));
let pr = PureRecord::new(Identifier::default(), jr);
let joback = Joback::new(Arc::new(JobackParameters::new_pure(pr)?));
let eos = Arc::new(EquationOfState::ideal_gas(joback));
let state = State::new_nvt(
&eos,
Expand All @@ -357,17 +357,16 @@ mod tests {
fn c_p_comparison() -> EosResult<()> {
let record1 = PureRecord::new(
Identifier::default(),
1.0,
JobackRecord::new(1.0, 0.2, 0.03, 0.004, 0.005),
);
let record2 = PureRecord::new(
Identifier::default(),
1.0,
JobackRecord::new(-5.0, 0.4, 0.03, 0.002, 0.001),
);
let parameters = Arc::new(JobackParameters::new_binary(vec![record1, record2], None)?);
let joback = Arc::new(Joback::new(parameters));
let eos = Arc::new(EquationOfState::ideal_gas(joback.clone()));
let joback = Joback::new(parameters.clone());
let ideal_gas = Joback::new(parameters);
let eos = Arc::new(EquationOfState::ideal_gas(ideal_gas));
let temperature = 300.0 * KELVIN;
let volume = METER.powi::<P3>();
let moles = &arr1(&[1.0, 3.0]) * MOL;
Expand Down
7 changes: 3 additions & 4 deletions feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ mod tests {
fn validate_residual_properties() -> EosResult<()> {
let mixture = pure_record_vec();
let propane = mixture[0].clone();
let parameters = PengRobinsonParameters::new_pure(propane)?;
let residual = Arc::new(PengRobinson::new(Arc::new(parameters)));
let parameters = Arc::new(PengRobinsonParameters::new_pure(propane)?);
let residual = Arc::new(PengRobinson::new(parameters));
let joback_parameters = Arc::new(JobackParameters::new_pure(PureRecord::new(
Identifier::default(),
1.0,
JobackRecord::new(0.0, 0.0, 0.0, 0.0, 0.0),
))?);
let ideal_gas = Arc::new(Joback::new(joback_parameters));
let ideal_gas = Joback::new(joback_parameters);
let eos = Arc::new(EquationOfState::new(ideal_gas, residual.clone()));

let sr = StateBuilder::new(&residual)
Expand Down
2 changes: 1 addition & 1 deletion feos-core/src/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
fn from_model_records(model_records: Vec<Self::Pure>) -> Result<Self, ParameterError> {
let pure_records = model_records
.into_iter()
.map(|r| PureRecord::new(Default::default(), Default::default(), r))
.map(|r| PureRecord::new(Default::default(), r))
.collect();
Self::from_records(pure_records, None)
}
Expand Down
19 changes: 6 additions & 13 deletions feos-core/src/parameter/model_record.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::identifier::Identifier;
use super::segment::SegmentRecord;
use super::{IdentifierOption, ParameterError};
use conv::ValueInto;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
Expand All @@ -13,16 +12,14 @@ use std::path::Path;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PureRecord<M> {
pub identifier: Identifier,
pub molarweight: f64,
pub model_record: M,
}

impl<M> PureRecord<M> {
/// Create a new `PureRecord`.
pub fn new(identifier: Identifier, molarweight: f64, model_record: M) -> Self {
pub fn new(identifier: Identifier, model_record: M) -> Self {
Self {
identifier,
molarweight,
model_record,
}
}
Expand All @@ -33,19 +30,16 @@ impl<M> PureRecord<M> {
/// and the ideal gas record.
pub fn from_segments<S, T>(identifier: Identifier, segments: S) -> Result<Self, ParameterError>
where
T: Copy + ValueInto<f64>,
M: FromSegments<T>,
S: IntoIterator<Item = (SegmentRecord<M>, T)>,
{
let mut molarweight = 0.0;
let mut model_segments = Vec::new();
for (s, n) in segments {
molarweight += s.molarweight * n.value_into().unwrap();
model_segments.push((s.model_record, n));
}
let model_segments: Vec<_> = segments
.into_iter()
.map(|(s, n)| (s.model_record, n))
.collect();
let model_record = M::from_segments(&model_segments)?;

Ok(Self::new(identifier, molarweight, model_record))
Ok(Self::new(identifier, model_record))
}

/// Create pure substance parameters from a json file.
Expand Down Expand Up @@ -104,7 +98,6 @@ where
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PureRecord(")?;
write!(f, "\n\tidentifier={},", self.identifier)?;
write!(f, "\n\tmolarweight={},", self.molarweight)?;
write!(f, "\n\tmodel_record={},", self.model_record)?;
write!(f, "\n)")
}
Expand Down
9 changes: 7 additions & 2 deletions feos-core/src/python/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ pub struct PyPengRobinsonRecord(PengRobinsonRecord);
#[pymethods]
impl PyPengRobinsonRecord {
#[new]
fn new(tc: f64, pc: f64, acentric_factor: f64) -> Self {
Self(PengRobinsonRecord::new(tc, pc, acentric_factor))
fn new(tc: f64, pc: f64, acentric_factor: f64, molarweight: f64) -> Self {
Self(PengRobinsonRecord::new(
tc,
pc,
acentric_factor,
molarweight,
))
}

fn __repr__(&self) -> PyResult<String> {
Expand Down
22 changes: 2 additions & 20 deletions feos-core/src/python/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,8 @@ macro_rules! impl_pure_record {
impl PyPureRecord {
#[new]
#[pyo3(text_signature = "(identifier, molarweight, model_record)")]
fn new(
identifier: PyIdentifier,
molarweight: f64,
model_record: $py_model_record,
) -> PyResult<Self> {
Ok(Self(PureRecord::new(
identifier.0,
molarweight,
model_record.0,
)))
fn new(identifier: PyIdentifier, model_record: $py_model_record) -> PyResult<Self> {
Ok(Self(PureRecord::new(identifier.0, model_record.0)))
}

#[getter]
Expand All @@ -421,16 +413,6 @@ macro_rules! impl_pure_record {
self.0.identifier = identifier.0;
}

#[getter]
fn get_molarweight(&self) -> f64 {
self.0.molarweight
}

#[setter]
fn set_molarweight(&mut self, molarweight: f64) {
self.0.molarweight = molarweight;
}

#[getter]
fn get_model_record(&self) -> $py_model_record {
$py_model_record(self.0.model_record.clone())
Expand Down
2 changes: 1 addition & 1 deletion feos-dft/src/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<F> Deref for DFT<F> {

impl<F> DFT<F> {
pub fn ideal_gas<I>(self, ideal_gas: I) -> DFT<EquationOfState<I, F>> {
DFT(EquationOfState::new(Arc::new(ideal_gas), Arc::new(self.0)))
DFT(EquationOfState::new(ideal_gas, Arc::new(self.0)))
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/pcsaft/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::fmt::Write;
/// PC-SAFT pure-component parameters.
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct PcSaftRecord {
/// molar weight
pub molarweight: f64,
/// Segment number
pub m: f64,
/// Segment diameter in units of Angstrom
Expand Down Expand Up @@ -44,11 +46,13 @@ pub struct PcSaftRecord {

impl FromSegments<f64> for PcSaftRecord {
fn from_segments(segments: &[(Self, f64)]) -> Result<Self, ParameterError> {
let mut molarweight = 0.0;
let mut m = 0.0;
let mut sigma3 = 0.0;
let mut epsilon_k = 0.0;

segments.iter().for_each(|(s, n)| {
molarweight += s.molarweight * n;
m += s.m * n;
sigma3 += s.m * s.sigma.powi(3) * n;
epsilon_k += s.m * s.epsilon_k * n;
Expand Down Expand Up @@ -143,6 +147,7 @@ impl FromSegments<f64> for PcSaftRecord {
viscosity = viscosity.map(|v| [v[0] - 0.5 * m.ln(), v[1], v[2], v[3]]);

Ok(Self {
molarweight,
m,
sigma: (sigma3 / m).cbrt(),
epsilon_k: epsilon_k / m,
Expand Down Expand Up @@ -225,6 +230,7 @@ impl std::fmt::Display for PcSaftRecord {

impl PcSaftRecord {
pub fn new(
molarweight: f64,
m: f64,
sigma: f64,
epsilon_k: f64,
Expand Down Expand Up @@ -256,6 +262,7 @@ impl PcSaftRecord {
))
};
PcSaftRecord {
molarweight,
m,
sigma,
epsilon_k,
Expand Down Expand Up @@ -399,7 +406,7 @@ impl Parameter for PcSaftParameters {
viscosity.push(r.viscosity);
diffusion.push(r.diffusion);
thermal_conductivity.push(r.thermal_conductivity);
molarweight[i] = record.molarweight;
molarweight[i] = r.molarweight;
}

let mu2 = &mu * &mu / (&m * &sigma * &sigma * &sigma * &epsilon_k)
Expand Down Expand Up @@ -544,7 +551,7 @@ impl PcSaftParameters {
o,
"\n|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|",
component,
record.molarweight,
record.model_record.molarweight,
record.model_record.m,
record.model_record.sigma,
record.model_record.epsilon_k,
Expand Down
7 changes: 7 additions & 0 deletions src/pcsaft/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl PyPcSaftRecord {
text_signature = "(m, sigma, epsilon_k, mu=None, q=None, kappa_ab=None, epsilon_k_ab=None, na=None, nb=None, nc=None, viscosity=None, diffusion=None, thermal_conductivity=None)"
)]
fn new(
molarweight: f64,
m: f64,
sigma: f64,
epsilon_k: f64,
Expand All @@ -68,6 +69,7 @@ impl PyPcSaftRecord {
thermal_conductivity: Option<[f64; 4]>,
) -> Self {
Self(PcSaftRecord::new(
molarweight,
m,
sigma,
epsilon_k,
Expand All @@ -84,6 +86,11 @@ impl PyPcSaftRecord {
))
}

#[getter]
fn get_molarweight(&self) -> f64 {
self.0.molarweight
}

#[getter]
fn get_m(&self) -> f64 {
self.0.m
Expand Down
Loading