-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmodel_record.rs
More file actions
344 lines (310 loc) · 10.7 KB
/
model_record.rs
File metadata and controls
344 lines (310 loc) · 10.7 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use super::{AssociationRecord, BinaryAssociationRecord, Identifier, IdentifierOption};
use crate::FeosResult;
use crate::errors::FeosError;
use indexmap::IndexSet;
use num_traits::Zero;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::fs::File;
use std::io::BufReader;
use std::ops::Deref;
use std::path::Path;
/// A collection of parameters with an arbitrary identifier.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Record<I, M, A> {
pub identifier: I,
#[serde(skip_serializing_if = "f64::is_zero")]
#[serde(default)]
pub molarweight: f64,
#[serde(flatten)]
pub model_record: M,
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default = "Vec::new")]
pub association_sites: Vec<AssociationRecord<A>>,
}
/// A collection of parameters of a pure substance.
pub type PureRecord<M, A> = Record<Identifier, M, A>;
/// Parameters describing an individual segment of a molecule.
pub type SegmentRecord<M, A> = Record<String, M, A>;
impl<I, M, A> Record<I, M, A> {
/// Create a new `ModelRecord`.
pub fn new(identifier: I, molarweight: f64, model_record: M) -> Self {
Self::with_association(identifier, molarweight, model_record, vec![])
}
/// Create a new `ModelRecord` including association information.
pub fn with_association(
identifier: I,
molarweight: f64,
model_record: M,
association_sites: Vec<AssociationRecord<A>>,
) -> Self {
Self {
identifier,
molarweight,
model_record,
association_sites,
}
}
/// Update the `PureRecord` from segment counts.
///
/// The [FromSegments] trait needs to be implemented for the model record.
pub fn from_segments<S>(identifier: I, segments: S) -> FeosResult<Self>
where
M: FromSegments,
S: IntoIterator<Item = (SegmentRecord<M, A>, f64)>,
{
let mut molarweight = 0.0;
let mut model_segments = Vec::new();
let association_sites = segments
.into_iter()
.flat_map(|(s, n)| {
molarweight += s.molarweight * n;
model_segments.push((s.model_record, n));
s.association_sites.into_iter().map(move |record| {
AssociationRecord::with_id(
record.id,
record.parameters,
record.na * n,
record.nb * n,
record.nc * n,
)
})
})
.collect();
let model_record = M::from_segments(&model_segments)?;
Ok(Self::with_association(
identifier,
molarweight,
model_record,
association_sites,
))
}
}
impl<M, A> PureRecord<M, A> {
/// Create pure substance parameters from a json file.
pub fn from_json<P, S>(
substances: &[S],
file: P,
identifier_option: IdentifierOption,
) -> FeosResult<Vec<Self>>
where
P: AsRef<Path>,
S: Deref<Target = str>,
M: DeserializeOwned,
A: DeserializeOwned,
{
// create list of substances
let mut queried: HashSet<&str> = substances.iter().map(|s| s.deref()).collect();
// raise error on duplicate detection
if queried.len() != substances.len() {
return Err(FeosError::IncompatibleParameters(
"A substance was defined more than once.".to_string(),
));
}
let f = File::open(file)?;
let reader = BufReader::new(f);
// use stream in the future
let file_records: Vec<Self> = serde_json::from_reader(reader)?;
let mut records: HashMap<&str, Self> = HashMap::with_capacity(substances.len());
// build map, draining list of queried substances in the process
for record in file_records {
if let Some(id) = record.identifier.as_str(identifier_option) {
queried.take(id).map(|id| records.insert(id, record));
}
// all parameters parsed
if queried.is_empty() {
break;
}
}
// report missing parameters
if !queried.is_empty() {
return Err(FeosError::ComponentsNotFound(format!("{queried:?}")));
};
// collect into vec in correct order
Ok(substances
.iter()
.map(|s| records.remove(s.deref()).unwrap())
.collect())
}
/// Creates parameters from substance information stored in multiple json files.
pub fn from_multiple_json<P, S>(
input: &[(Vec<S>, P)],
identifier_option: IdentifierOption,
) -> FeosResult<Vec<Self>>
where
P: AsRef<Path>,
S: Deref<Target = str>,
M: DeserializeOwned,
A: DeserializeOwned,
{
// total number of substances queried
let nsubstances = input
.iter()
.fold(0, |acc, (substances, _)| acc + substances.len());
// queried substances with removed duplicates
let queried: IndexSet<String> = input
.iter()
.flat_map(|(substances, _)| substances)
.map(|substance| substance.to_string())
.collect();
// check if there are duplicates
if queried.len() != nsubstances {
return Err(FeosError::IncompatibleParameters(
"A substance was defined more than once.".to_string(),
));
}
let mut records: Vec<Self> = Vec::with_capacity(nsubstances);
// collect parameters from files into single map
for (substances, file) in input {
records.extend(Self::from_json(substances, file, identifier_option)?);
}
Ok(records)
}
}
impl<M, A> SegmentRecord<M, A> {
/// Read a list of `SegmentRecord`s from a JSON file.
pub fn from_json<P: AsRef<Path>>(file: P) -> FeosResult<Vec<Self>>
where
M: DeserializeOwned,
A: DeserializeOwned,
{
Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
}
}
impl<M: Serialize, A: Serialize> fmt::Display for PureRecord<M, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).unwrap().replace("\"", "");
let s = s.replace(",", ", ").replace(":", ": ");
write!(f, "PureRecord({})", &s[1..s.len() - 1])
}
}
impl<M: Serialize, A: Serialize> fmt::Display for SegmentRecord<M, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).unwrap().replace("\"", "");
let s = s.replace(",", ", ").replace(":", ": ");
write!(f, "SegmentRecord({})", &s[1..s.len() - 1])
}
}
/// Trait for models that implement a homosegmented group contribution
/// method
pub trait FromSegments: Clone {
/// Constructs the record from a list of segment records with their
/// number of occurences.
fn from_segments(segments: &[(Self, f64)]) -> FeosResult<Self>;
}
/// Trait for models that implement a homosegmented group contribution
/// method and have a combining rule for binary interaction parameters.
pub trait FromSegmentsBinary: Clone {
/// Constructs the binary record from a list of segment records with
/// their number of occurences.
fn from_segments_binary(segments: &[(Self, f64, f64)]) -> FeosResult<Self>;
}
impl FromSegmentsBinary for () {
fn from_segments_binary(_: &[(Self, f64, f64)]) -> FeosResult<Self> {
Ok(())
}
}
/// A collection of parameters that model interactions between two substances.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BinaryRecord<I, B, A> {
/// Identifier of the first component
pub id1: I,
/// Identifier of the second component
pub id2: I,
/// Binary interaction parameter(s)
#[serde(flatten)]
pub model_record: Option<B>,
/// Binary association records
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default = "Vec::new")]
pub association_sites: Vec<BinaryAssociationRecord<A>>,
}
/// A collection of parameters that model interactions between two segments.
pub type BinarySegmentRecord<M, A> = BinaryRecord<String, M, A>;
impl<I, B, A> BinaryRecord<I, B, A> {
/// Crates a new `BinaryRecord`.
pub fn new(id1: I, id2: I, model_record: Option<B>) -> Self {
Self::with_association(id1, id2, model_record, vec![])
}
/// Crates a new `BinaryRecord` including association sites.
pub fn with_association(
id1: I,
id2: I,
model_record: Option<B>,
association_sites: Vec<BinaryAssociationRecord<A>>,
) -> Self {
Self {
id1,
id2,
model_record,
association_sites,
}
}
/// Read a list of `BinaryRecord`s from a JSON file.
pub fn from_json<P: AsRef<Path>>(file: P) -> FeosResult<Vec<Self>>
where
I: DeserializeOwned,
B: DeserializeOwned,
A: DeserializeOwned,
{
Ok(serde_json::from_reader(BufReader::new(File::open(file)?))?)
}
}
impl<I: Serialize + Clone, B: Serialize + Clone, A: Serialize + Clone> fmt::Display
for BinaryRecord<I, B, A>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).unwrap().replace("\"", "");
let s = s.replace(",", ", ").replace(":", ": ");
write!(f, "BinaryRecord({})", &s[1..s.len() - 1])
}
}
#[cfg(test)]
mod test {
use super::*;
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
struct TestModelRecordSegments {
a: f64,
}
#[test]
fn deserialize() {
let r = r#"
{
"identifier": {
"cas": "123-4-5"
},
"molarweight": 16.0426,
"a": 0.1
}
"#;
let record: PureRecord<TestModelRecordSegments, ()> =
serde_json::from_str(r).expect("Unable to parse json.");
assert_eq!(record.identifier.cas, Some("123-4-5".into()))
}
#[test]
fn deserialize_list() {
let r = r#"
[
{
"identifier": {
"cas": "1"
},
"molarweight": 1.0,
"a": 1.0
},
{
"identifier": {
"cas": "2"
},
"molarweight": 2.0,
"a": 2.0
}
]"#;
let records: Vec<PureRecord<TestModelRecordSegments, ()>> =
serde_json::from_str(r).expect("Unable to parse json.");
assert_eq!(records[0].identifier.cas, Some("1".into()));
assert_eq!(records[1].identifier.cas, Some("2".into()))
}
}