-
Notifications
You must be signed in to change notification settings - Fork 22
/
liquid_density.rs
152 lines (135 loc) · 4.29 KB
/
liquid_density.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use super::{DataSet, EstimatorError};
use feos_core::si::{MassDensity, Moles, Pressure, Temperature, KILOGRAM, METER};
use feos_core::{DensityInitialization, PhaseEquilibrium, Residual, SolverOptions, State};
use ndarray::{arr1, Array1};
use std::sync::Arc;
use typenum::P3;
/// Liquid mass density data as function of pressure and temperature.
#[derive(Clone)]
pub struct LiquidDensity {
/// mass density
pub target: Array1<f64>,
/// unit of mass density
unit: MassDensity,
/// temperature
temperature: Temperature<Array1<f64>>,
/// pressure
pressure: Pressure<Array1<f64>>,
}
impl LiquidDensity {
/// A new data set for liquid densities with pressures and temperatures as input.
pub fn new(
target: MassDensity<Array1<f64>>,
temperature: Temperature<Array1<f64>>,
pressure: Pressure<Array1<f64>>,
) -> Self {
let unit = KILOGRAM / METER.powi::<P3>();
Self {
target: (target / unit).to_reduced(),
unit,
temperature,
pressure,
}
}
/// Returns temperature of data points.
pub fn temperature(&self) -> &Temperature<Array1<f64>> {
&self.temperature
}
/// Returns pressure of data points.
pub fn pressure(&self) -> &Pressure<Array1<f64>> {
&self.pressure
}
}
impl<E: Residual> DataSet<E> for LiquidDensity {
fn target(&self) -> &Array1<f64> {
&self.target
}
fn target_str(&self) -> &str {
"liquid density"
}
fn input_str(&self) -> Vec<&str> {
vec!["temperature", "pressure"]
}
fn predict(&self, eos: &Arc<E>) -> Result<Array1<f64>, EstimatorError> {
let moles = Moles::from_reduced(arr1(&[1.0]));
Ok(self
.temperature
.into_iter()
.zip(&self.pressure)
.map(|(t, p)| {
let state = State::new_npt(eos, t, p, &moles, DensityInitialization::Liquid);
if let Ok(s) = state {
(s.mass_density() / self.unit).into_value()
} else {
f64::NAN
}
})
.collect())
}
// fn get_input(&self) -> HashMap<String, SIArray1> {
// let mut m = HashMap::with_capacity(2);
// m.insert("temperature".to_owned(), self.temperature());
// m.insert("pressure".to_owned(), self.pressure());
// m
// }
}
/// Store experimental data of liquid densities calculated for phase equilibria.
#[derive(Clone)]
pub struct EquilibriumLiquidDensity {
pub target: Array1<f64>,
/// unit of mass density
unit: MassDensity,
/// temperature
temperature: Temperature<Array1<f64>>,
/// options for VLE solver
solver_options: SolverOptions,
}
impl EquilibriumLiquidDensity {
/// A new data set for liquid densities with pressures and temperatures as input.
pub fn new(
target: MassDensity<Array1<f64>>,
temperature: Temperature<Array1<f64>>,
vle_options: Option<SolverOptions>,
) -> Self {
let unit = KILOGRAM / METER.powi::<P3>();
Self {
target: (target / unit).to_reduced(),
unit,
temperature,
solver_options: vle_options.unwrap_or_default(),
}
}
/// Returns temperature of data points.
pub fn temperature(&self) -> &Temperature<Array1<f64>> {
&self.temperature
}
}
impl<E: Residual> DataSet<E> for EquilibriumLiquidDensity {
fn target(&self) -> &Array1<f64> {
&self.target
}
fn target_str(&self) -> &str {
"equilibrium liquid density"
}
fn input_str(&self) -> Vec<&str> {
vec!["temperature"]
}
fn predict(&self, eos: &Arc<E>) -> Result<Array1<f64>, EstimatorError> {
Ok(self
.temperature
.into_iter()
.map(|t| {
if let Ok(state) = PhaseEquilibrium::pure(eos, t, None, self.solver_options) {
(state.liquid().mass_density() / self.unit).into_value()
} else {
f64::NAN
}
})
.collect())
}
// fn get_input(&self) -> HashMap<String, SIArray1> {
// let mut m = HashMap::with_capacity(2);
// m.insert("temperature".to_owned(), self.temperature());
// m
// }
}