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
store parameters as Rc
  • Loading branch information
prehner committed Nov 26, 2021
commit 185ddfd45dfbb4e84eae0bdf5fe9417840b6b519
10 changes: 5 additions & 5 deletions src/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use quantity::si::{SIArray1, SIUnit};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::f64::consts::SQRT_2;
use std::rc::Rc;

const KB_A3: f64 = 13806490.0;

Expand All @@ -33,7 +34,6 @@ impl std::fmt::Display for PengRobinsonRecord {
}

/// Peng-Robinson parameters for one ore more substances.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct PengRobinsonParameters {
tc: Array1<f64>,
a: Array1<f64>,
Expand Down Expand Up @@ -143,11 +143,11 @@ impl Parameter for PengRobinsonParameters {
}

pub struct PengRobinson {
parameters: PengRobinsonParameters,
parameters: Rc<PengRobinsonParameters>,
}

impl PengRobinson {
pub fn new(parameters: PengRobinsonParameters) -> Self {
pub fn new(parameters: Rc<PengRobinsonParameters>) -> Self {
Self { parameters }
}
}
Expand All @@ -158,7 +158,7 @@ impl EquationOfState for PengRobinson {
}

fn subset(&self, component_list: &[usize]) -> Self {
Self::new(self.parameters.subset(component_list))
Self::new(Rc::new(self.parameters.subset(component_list)))
}

fn compute_max_density(&self, moles: &Array1<f64>) -> f64 {
Expand Down Expand Up @@ -269,7 +269,7 @@ mod tests {
let pc = propane.model_record.clone().unwrap().pc;
let parameters =
PengRobinsonParameters::from_records(vec![propane.clone()], Array2::zeros((1, 1)));
let pr = Rc::new(PengRobinson::new(parameters));
let pr = Rc::new(PengRobinson::new(Rc::new(parameters)));
let cp = State::critical_point(&pr, None, None, VLEOptions::default())?;
println!("{} {}", cp.temperature, cp.pressure(Contributions::Total));
assert_relative_eq!(cp.temperature, tc * KELVIN, max_relative = 1e-4);
Expand Down
4 changes: 2 additions & 2 deletions src/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ where
///
/// The [FromSegments] trait needs to be implemented for both the model record
/// and the ideal gas record.
fn from_segment_records(
fn from_segments(
mut pure_records: Vec<PureRecord<Self::Pure, Self::IdealGas>>,
segment_records: Vec<SegmentRecord<Self::Pure, Self::IdealGas>>,
binary_segment_records: Option<Vec<BinaryRecord<String, Self::Binary>>>,
Expand Down Expand Up @@ -280,7 +280,7 @@ where
})
.transpose()?;

Self::from_segment_records(pure_records, segment_records, binary_records)
Self::from_segments(pure_records, segment_records, binary_records)
}

fn subset(&self, component_list: &[usize]) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion src/python/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl_pure_record!(
text_signature = "(pure_records, binary_records=None, substances=None, search_option='Name')"
)]
#[derive(Clone)]
pub struct PyPengRobinsonParameters(pub PengRobinsonParameters);
pub struct PyPengRobinsonParameters(pub Rc<PengRobinsonParameters>);

impl_parameter!(PengRobinsonParameters, PyPengRobinsonParameters);

Expand Down
28 changes: 14 additions & 14 deletions src/python/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,10 @@ macro_rules! impl_parameter {
pure_records: Vec<PyPureRecord>,
binary_records: &PyArray2<f64>,
) -> Self {
Self(<$parameter>::from_records(
Self(Rc::new(<$parameter>::from_records(
pure_records.into_iter().map(|pr| pr.0).collect(),
binary_records.to_owned_array(),
))
binary_records.to_owned_array().mapv(f64::into),
)))
}

/// Creates parameters for a pure component from a pure record.
Expand All @@ -547,7 +547,7 @@ macro_rules! impl_parameter {
#[staticmethod]
#[pyo3(text_signature = "(pure_record)")]
fn new_pure(pure_record: PyPureRecord) -> Self {
Self(<$parameter>::new_pure(pure_record.0))
Self(Rc::new(<$parameter>::new_pure(pure_record.0)))
}

/// Creates parameters for a binary system from pure records and an optional
Expand All @@ -562,10 +562,10 @@ macro_rules! impl_parameter {
#[staticmethod]
#[pyo3(text_signature = "(pure_records, binary_record)")]
fn new_binary(pure_records: Vec<PyPureRecord>, binary_record: Option<f64>) -> Self {
Self(<$parameter>::new_binary(
Self(Rc::new(<$parameter>::new_binary(
pure_records.into_iter().map(|pr| pr.0).collect(),
binary_record,
))
binary_record.map(f64::into),
)))
}

/// Creates parameters from json files.
Expand Down Expand Up @@ -595,12 +595,12 @@ macro_rules! impl_parameter {
Some(o) => IdentifierOption::try_from(o)?,
None => IdentifierOption::Name,
};
Ok(Self(<$parameter>::from_json(
Ok(Self(Rc::new(<$parameter>::from_json(
&substances,
pure_path,
binary_path,
io,
)?))
)?)))
}
}
};
Expand Down Expand Up @@ -629,11 +629,11 @@ macro_rules! impl_parameter_from_segments {
segment_records: Vec<PySegmentRecord>,
binary_segment_records: Option<Vec<PyBinarySegmentRecord>>,
) -> Result<Self, ParameterError> {
Ok(Self(<$parameter>::from_segments(
Ok(Self(Rc::new(<$parameter>::from_segments(
pure_records.into_iter().map(|pr| pr.0).collect(),
segment_records.into_iter().map(|sr| sr.0).collect(),
binary_segment_records.map(|r| r.into_iter().map(|r| r.0).collect()),
)?))
binary_segment_records.map(|r| r.into_iter().map(|r| BinaryRecord{id1:r.0.id1,id2:r.0.id2,model_record:r.0.model_record.into()}).collect()),
)?)))
}

/// Creates parameters using segments from json file.
Expand Down Expand Up @@ -666,13 +666,13 @@ macro_rules! impl_parameter_from_segments {
Some(o) => IdentifierOption::try_from(o)?,
None => IdentifierOption::Name,
};
Ok(Self(<$parameter>::from_json_segments(
Ok(Self(Rc::new(<$parameter>::from_json_segments(
&substances,
pure_path,
segments_path,
binary_path,
io,
)?))
)?)))
}
}
};
Expand Down