This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffusion.rs
More file actions
88 lines (78 loc) · 2.6 KB
/
Copy pathdiffusion.rs
File metadata and controls
88 lines (78 loc) · 2.6 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
use super::{DataSet, EstimatorError, Loss};
use feos_core::{DensityInitialization, EntropyScaling, EosUnit, EquationOfState, State};
use ndarray::{arr1, Array1};
use quantity::{QuantityArray1, QuantityScalar};
use std::collections::HashMap;
use std::rc::Rc;
/// Store experimental diffusion data.
#[derive(Clone)]
pub struct Diffusion<U: EosUnit> {
pub target: QuantityArray1<U>,
temperature: QuantityArray1<U>,
pressure: QuantityArray1<U>,
datapoints: usize,
}
impl<U: EosUnit> Diffusion<U> {
/// Create a new data set for experimental diffusion data.
pub fn new(
target: QuantityArray1<U>,
temperature: QuantityArray1<U>,
pressure: QuantityArray1<U>,
) -> Result<Self, EstimatorError> {
let datapoints = target.len();
Ok(Self {
target,
temperature,
pressure,
datapoints,
})
}
/// Return temperature.
pub fn temperature(&self) -> QuantityArray1<U> {
self.temperature.clone()
}
/// Return pressure.
pub fn pressure(&self) -> QuantityArray1<U> {
self.pressure.clone()
}
}
impl<U: EosUnit, E: EquationOfState + EntropyScaling<U>> DataSet<U, E> for Diffusion<U> {
fn target(&self) -> QuantityArray1<U> {
self.target.clone()
}
fn target_str(&self) -> &str {
"diffusion"
}
fn input_str(&self) -> Vec<&str> {
vec!["temperature", "pressure"]
}
fn predict(&self, eos: &Rc<E>) -> Result<QuantityArray1<U>, EstimatorError>
where
QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
{
let unit = self.target.get(0);
let mut prediction = Array1::zeros(self.datapoints) * unit;
let moles = arr1(&[1.0]) * U::reference_moles();
for i in 0..self.datapoints {
let t = self.temperature.get(i);
let p = self.pressure.get(i);
let state = State::new_npt(eos, t, p, &moles, DensityInitialization::None)?;
prediction.try_set(i, state.diffusion()?)?;
}
Ok(prediction)
}
fn cost(&self, eos: &Rc<E>, loss: Loss) -> Result<Array1<f64>, EstimatorError>
where
QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
{
let mut cost = self.relative_difference(eos)?;
loss.apply(&mut cost.view_mut());
Ok(cost / self.datapoints as f64)
}
fn get_input(&self) -> HashMap<String, QuantityArray1<U>> {
let mut m = HashMap::with_capacity(1);
m.insert("temperature".to_owned(), self.temperature());
m.insert("pressure".to_owned(), self.pressure());
m
}
}