Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.
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
make model_record not optional
  • Loading branch information
prehner committed Jan 8, 2022
commit a57a3c0ba696ea818265eb4c8f8a5a101d7a3312
24 changes: 8 additions & 16 deletions src/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl PengRobinsonParameters {
acentric_factor: acentric_factor[i],
};
let id = Identifier::new("1", None, None, None, None, None);
PureRecord::new(id, molarweight[i], Some(record), None)
PureRecord::new(id, molarweight[i], record, None)
})
.collect();
Ok(PengRobinsonParameters::from_records(
Expand Down Expand Up @@ -131,19 +131,11 @@ impl Parameter for PengRobinsonParameters {

for (i, record) in pure_records.iter().enumerate() {
molarweight[i] = record.molarweight;
match &record.model_record {
Some(r) => {
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;
kappa[i] =
0.37464 + (1.54226 - 0.26992 * r.acentric_factor) * r.acentric_factor;
}
None => panic!(
"No Peng-Robinson parameters for {} found.",
record.identifier.cas
),
};
let r = &record.model_record;
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;
kappa[i] = 0.37464 + (1.54226 - 0.26992 * r.acentric_factor) * r.acentric_factor;
}

let joback_records = pure_records
Expand Down Expand Up @@ -312,8 +304,8 @@ mod tests {
fn peng_robinson() -> EosResult<()> {
let mixture = pure_record_vec();
let propane = mixture[0].clone();
let tc = propane.model_record.clone().unwrap().tc;
let pc = propane.model_record.clone().unwrap().pc;
let tc = propane.model_record.tc;
let pc = propane.model_record.pc;
let parameters =
PengRobinsonParameters::from_records(vec![propane.clone()], Array2::zeros((1, 1)));
let pr = Rc::new(PengRobinson::new(Rc::new(parameters)));
Expand Down
42 changes: 23 additions & 19 deletions src/parameter/chemical_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ use std::borrow::Cow;
use std::collections::{HashMap, HashSet};

// Auxiliary structure used to deserialize chemical records without bond information.
#[derive(Deserialize)]
#[serde(untagged)]
enum ChemicalRecordJSON {
List {
identifier: Identifier,
segments: Vec<String>,
bonds: Option<Vec<[usize; 2]>>,
},
Count {
identifier: Identifier,
segments: HashMap<String, f64>,
bonds: Option<HashMap<[String; 2], f64>>,
},
#[derive(Serialize, Deserialize)]
struct ChemicalRecordJSON {
identifier: Identifier,
segments: Vec<String>,
bonds: Option<Vec<[usize; 2]>>,
}

/// Chemical information of a substance.
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(from = "ChemicalRecordJSON")]
#[serde(untagged)]
#[serde(into = "ChemicalRecordJSON")]
pub enum ChemicalRecord {
List {
identifier: Identifier,
Expand All @@ -41,17 +33,29 @@ pub enum ChemicalRecord {

impl From<ChemicalRecordJSON> for ChemicalRecord {
fn from(record: ChemicalRecordJSON) -> Self {
Self::new(record.identifier, record.segments, record.bonds)
}
}

impl From<ChemicalRecord> for ChemicalRecordJSON {
fn from(record: ChemicalRecord) -> Self {
match record {
ChemicalRecordJSON::List {
ChemicalRecord::List {
identifier,
segments,
bonds,
} => Self::new(identifier, segments, bonds),
ChemicalRecordJSON::Count {
} => Self {
identifier,
segments,
bonds,
} => Self::new_count(identifier, segments, bonds),
bonds: Some(bonds),
},
ChemicalRecord::Count {
identifier: _,
segments: _,
bonds: _,
} => panic!(
"Only chemical records with detailed structural information can be serialized."
),
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/parameter/model_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use serde::{Deserialize, Serialize};
pub struct PureRecord<M, I> {
pub identifier: Identifier,
pub molarweight: f64,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub model_record: Option<M>,
pub model_record: M,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub ideal_gas_record: Option<I>,
Expand All @@ -20,7 +18,7 @@ impl<M, I> PureRecord<M, I> {
pub fn new(
identifier: Identifier,
molarweight: f64,
model_record: Option<M>,
model_record: M,
ideal_gas_record: Option<I>,
) -> Self {
Self {
Expand Down Expand Up @@ -49,7 +47,7 @@ impl<M, I> PureRecord<M, I> {
model_segments.push((s.model_record, n));
ideal_gas_segments.push(s.ideal_gas_record.map(|ig| (ig, n)));
}
let model_record = Some(M::from_segments(&model_segments));
let model_record = M::from_segments(&model_segments);

let ideal_gas_segments: Option<Vec<_>> = ideal_gas_segments.into_iter().collect();
let ideal_gas_record = ideal_gas_segments.as_deref().map(I::from_segments);
Expand All @@ -67,9 +65,7 @@ where
write!(f, "PureRecord(")?;
write!(f, "\n\tidentifier={},", self.identifier)?;
write!(f, "\n\tmolarweight={},", self.molarweight)?;
if let Some(m) = self.model_record.as_ref() {
write!(f, "\n\tmodel_record={},", m)?;
}
write!(f, "\n\tmodel_record={},", self.model_record)?;
if let Some(i) = self.ideal_gas_record.as_ref() {
write!(f, "\n\tideal_gas_record={},", i)?;
}
Expand Down
16 changes: 7 additions & 9 deletions src/python/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ macro_rules! impl_pure_record {
/// The identifier of the pure component.
/// molarweight : float
/// The molar weight (in g/mol) of the pure component.
/// model_record : ModelRecord, optional
/// model_record : ModelRecord
/// The pure component model parameters.
/// ideal_gas_record: IdealGasRecord, optional
/// The pure component parameters for the ideal gas model.
Expand All @@ -368,9 +368,7 @@ macro_rules! impl_pure_record {
/// -------
/// PureRecord
#[pyclass(name = "PureRecord")]
#[pyo3(
text_signature = "(identifier, molarweight, model_record=None, ideal_gas_record=None)"
)]
#[pyo3(text_signature = "(identifier, molarweight, model_record, ideal_gas_record=None)")]
#[derive(Clone)]
pub struct PyPureRecord(pub PureRecord<$model_record, $ideal_gas_record>);

Expand All @@ -380,13 +378,13 @@ macro_rules! impl_pure_record {
fn new(
identifier: PyIdentifier,
molarweight: f64,
model_record: Option<$py_model_record>,
model_record: $py_model_record,
ideal_gas_record: Option<$py_ideal_gas_record>,
) -> PyResult<Self> {
Ok(Self(PureRecord::new(
identifier.0,
molarweight,
model_record.map(|mr| mr.0),
model_record.0,
ideal_gas_record.map(|ig| ig.0),
)))
}
Expand All @@ -412,13 +410,13 @@ macro_rules! impl_pure_record {
}

#[getter]
fn get_model_record(&self) -> Option<$py_model_record> {
self.0.model_record.clone().map($py_model_record)
fn get_model_record(&self) -> $py_model_record {
$py_model_record(self.0.model_record.clone())
}

#[setter]
fn set_model_record(&mut self, model_record: $py_model_record) {
self.0.model_record = Some(model_record.0);
self.0.model_record = model_record.0;
}

#[getter]
Expand Down