-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdiffusion.rs
More file actions
95 lines (85 loc) · 2.73 KB
/
diffusion.rs
File metadata and controls
95 lines (85 loc) · 2.73 KB
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
use super::{DataSet, EstimatorError, Phase};
use feos_core::{DensityInitialization, EntropyScaling, EosUnit, State, Residual};
use itertools::izip;
use ndarray::{arr1, Array1};
use quantity::si::{SIArray1, SIUnit};
use std::collections::HashMap;
use std::sync::Arc;
/// Store experimental diffusion data.
#[derive(Clone)]
pub struct Diffusion {
pub target: SIArray1,
temperature: SIArray1,
pressure: SIArray1,
initial_density: Vec<DensityInitialization>,
}
impl Diffusion {
/// Create a new data set for experimental diffusion data.
pub fn new(
target: SIArray1,
temperature: SIArray1,
pressure: SIArray1,
phase: Option<&Vec<Phase>>,
) -> Result<Self, EstimatorError> {
let n = temperature.len();
Ok(Self {
target,
temperature,
pressure,
initial_density: phase.map_or(vec![DensityInitialization::None; n], |phase| {
phase.iter().map(|&p| p.into()).collect()
}),
})
}
/// Return temperature.
pub fn temperature(&self) -> SIArray1 {
self.temperature.clone()
}
/// Return pressure.
pub fn pressure(&self) -> SIArray1 {
self.pressure.clone()
}
}
impl<E: Residual + EntropyScaling> DataSet<E> for Diffusion {
fn target(&self) -> &SIArray1 {
&self.target
}
fn target_str(&self) -> &str {
"diffusion"
}
fn input_str(&self) -> Vec<&str> {
vec!["temperature", "pressure"]
}
fn predict(&self, eos: &Arc<E>) -> Result<SIArray1, EstimatorError> {
let moles = arr1(&[1.0]) * SIUnit::reference_moles();
let ts = self
.temperature
.to_reduced(SIUnit::reference_temperature())
.unwrap();
let ps = self
.pressure
.to_reduced(SIUnit::reference_pressure())
.unwrap();
let res = izip!(&ts, &ps, &self.initial_density)
.map(|(&t, &p, &initial_density)| {
State::new_npt(
eos,
t * SIUnit::reference_temperature(),
p * SIUnit::reference_pressure(),
&moles,
initial_density,
)?
.diffusion()?
.to_reduced(SIUnit::reference_diffusion())
.map_err(EstimatorError::from)
})
.collect::<Result<Vec<f64>, EstimatorError>>();
Ok(Array1::from_vec(res?) * SIUnit::reference_diffusion())
}
fn get_input(&self) -> HashMap<String, SIArray1> {
let mut m = HashMap::with_capacity(1);
m.insert("temperature".to_owned(), self.temperature());
m.insert("pressure".to_owned(), self.pressure());
m
}
}