-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmod.rs
More file actions
369 lines (331 loc) · 12.9 KB
/
Copy pathmod.rs
File metadata and controls
369 lines (331 loc) · 12.9 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#![allow(clippy::excessive_precision)]
#![allow(clippy::needless_range_loop)]
use super::parameters::UVParameters;
use feos_core::{parameter::Parameter, Components, EosError, EosResult, HelmholtzEnergy, Residual};
use ndarray::Array1;
use std::f64::consts::FRAC_PI_6;
use std::sync::Arc;
pub(crate) mod attractive_perturbation_bh;
pub(crate) mod attractive_perturbation_uvb3;
pub(crate) mod attractive_perturbation_wca;
pub(crate) mod hard_sphere_bh;
pub(crate) mod hard_sphere_wca;
pub(crate) mod reference_perturbation_bh;
pub(crate) mod reference_perturbation_uvb3;
pub(crate) mod reference_perturbation_wca;
pub(crate) mod ufraction;
use attractive_perturbation_bh::AttractivePerturbationBH;
use attractive_perturbation_uvb3::AttractivePerturbationUVB3;
use attractive_perturbation_wca::AttractivePerturbationWCA;
use hard_sphere_bh::HardSphereBH;
use hard_sphere_wca::HardSphereWCA;
use reference_perturbation_bh::ReferencePerturbationBH;
use reference_perturbation_uvb3::ReferencePerturbationUVB3;
use reference_perturbation_wca::ReferencePerturbationWCA;
/// Type of perturbation.
#[derive(Clone)]
#[cfg_attr(feature = "python", pyo3::pyclass)]
pub enum Perturbation {
BarkerHenderson,
WeeksChandlerAndersen,
}
/// Order of the highest virial coefficient included in the model.
#[derive(Clone)]
#[cfg_attr(feature = "python", pyo3::pyclass)]
pub enum VirialOrder {
Second,
Third,
}
/// Configuration options for uv-theory
#[derive(Clone)]
pub struct UVTheoryOptions {
pub max_eta: f64,
pub perturbation: Perturbation,
pub virial_order: VirialOrder,
}
impl Default for UVTheoryOptions {
fn default() -> Self {
Self {
max_eta: 0.5,
perturbation: Perturbation::WeeksChandlerAndersen,
virial_order: VirialOrder::Second,
}
}
}
/// uv-theory equation of state
pub struct UVTheory {
parameters: Arc<UVParameters>,
options: UVTheoryOptions,
contributions: Vec<Box<dyn HelmholtzEnergy>>,
}
impl UVTheory {
/// uv-theory with default options (WCA).
pub fn new(parameters: Arc<UVParameters>) -> EosResult<Self> {
Self::with_options(parameters, UVTheoryOptions::default())
}
/// uv-theory with provided options.
pub fn with_options(
parameters: Arc<UVParameters>,
options: UVTheoryOptions,
) -> EosResult<Self> {
let mut contributions: Vec<Box<dyn HelmholtzEnergy>> = Vec::with_capacity(3);
match options.perturbation {
Perturbation::BarkerHenderson => match options.virial_order {
VirialOrder::Second => {
contributions.push(Box::new(HardSphereBH {
parameters: parameters.clone(),
}));
contributions.push(Box::new(ReferencePerturbationBH {
parameters: parameters.clone(),
}));
contributions.push(Box::new(AttractivePerturbationBH {
parameters: parameters.clone(),
}));
}
VirialOrder::Third => {
return Err(EosError::Error(
"Third virial coefficient is not implemented for Barker-Henderson"
.to_string(),
))
}
},
Perturbation::WeeksChandlerAndersen => {
contributions.push(Box::new(HardSphereWCA {
parameters: parameters.clone(),
}));
match options.virial_order {
VirialOrder::Second => {
contributions.push(Box::new(ReferencePerturbationWCA {
parameters: parameters.clone(),
}));
contributions.push(Box::new(AttractivePerturbationWCA {
parameters: parameters.clone(),
}));
}
VirialOrder::Third => {
if parameters.sigma.len() > 1 {
return Err(EosError::Error(
"Third virial coefficient is not implemented for mixtures!"
.to_string(),
));
}
if parameters.att[0] != 6.0 {
return Err(EosError::Error(
"Third virial coefficient is not implemented for attractive exponents other than 6!"
.to_string(),
));
}
contributions.push(Box::new(ReferencePerturbationUVB3 {
parameters: parameters.clone(),
}));
contributions.push(Box::new(AttractivePerturbationUVB3 {
parameters: parameters.clone(),
}));
}
}
}
}
Ok(Self {
parameters,
options,
contributions,
})
}
}
impl Components for UVTheory {
fn components(&self) -> usize {
self.parameters.pure_records.len()
}
fn subset(&self, component_list: &[usize]) -> Self {
Self::with_options(
Arc::new(self.parameters.subset(component_list)),
self.options.clone(),
)
.expect("Not defined for mixture")
}
}
impl Residual for UVTheory {
fn compute_max_density(&self, moles: &Array1<f64>) -> f64 {
self.options.max_eta * moles.sum()
/ (FRAC_PI_6 * self.parameters.sigma.mapv(|v| v.powi(3)) * moles).sum()
}
fn contributions(&self) -> &[Box<dyn HelmholtzEnergy>] {
&self.contributions
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::uvtheory::parameters::utils::test_parameters_mixture;
use crate::uvtheory::parameters::*;
use approx::assert_relative_eq;
use feos_core::parameter::{Identifier, Parameter, PureRecord};
use feos_core::State;
use ndarray::arr1;
use quantity::si::{ANGSTROM, KELVIN, MOL, NAV, RGAS};
#[test]
fn helmholtz_energy_pure_wca() -> EosResult<()> {
let sig = 3.7039;
let eps_k = 150.03;
let parameters = UVParameters::new_simple(24.0, 6.0, sig, eps_k)?;
let eos = Arc::new(UVTheory::new(Arc::new(parameters))?);
let reduced_temperature = 4.0;
let reduced_density = 1.0;
let temperature = reduced_temperature * eps_k * KELVIN;
let moles = arr1(&[2.0]) * MOL;
let volume = (sig * ANGSTROM).powi(3) / reduced_density * NAV * 2.0 * MOL;
let s = State::new_nvt(&eos, temperature, volume, &moles).unwrap();
let a = (s.residual_helmholtz_energy() / s.total_moles)
.to_reduced(RGAS * temperature)
.unwrap();
assert_relative_eq!(a, 2.972986567516, max_relative = 1e-12); //wca
Ok(())
}
#[test]
fn helmholtz_energy_pure_bh() -> EosResult<()> {
let eps_k = 150.03;
let sig = 3.7039;
let rep = 24.0;
let att = 6.0;
let parameters = UVParameters::new_simple(rep, att, sig, eps_k)?;
let options = UVTheoryOptions {
max_eta: 0.5,
perturbation: Perturbation::BarkerHenderson,
virial_order: VirialOrder::Second,
};
let eos = Arc::new(UVTheory::with_options(Arc::new(parameters), options)?);
let reduced_temperature = 4.0;
let reduced_density = 1.0;
let temperature = reduced_temperature * eps_k * KELVIN;
let moles = arr1(&[2.0]) * MOL;
let volume = (sig * ANGSTROM).powi(3) / reduced_density * NAV * 2.0 * MOL;
let s = State::new_nvt(&eos, temperature, volume, &moles).unwrap();
let a = (s.residual_helmholtz_energy() / s.total_moles)
.to_reduced(RGAS * temperature)
.unwrap();
assert_relative_eq!(a, 2.993577305779432, max_relative = 1e-12);
Ok(())
}
#[test]
fn helmholtz_energy_pure_uvb3() -> EosResult<()> {
let eps_k = 150.03;
let sig = 3.7039;
let rep = 12.0;
let att = 6.0;
let parameters = UVParameters::new_simple(rep, att, sig, eps_k)?;
let options = UVTheoryOptions {
max_eta: 0.5,
perturbation: Perturbation::WeeksChandlerAndersen,
virial_order: VirialOrder::Third,
};
let eos = Arc::new(UVTheory::with_options(Arc::new(parameters), options)?);
let reduced_temperature = 4.0;
let reduced_density = 0.5;
let temperature = reduced_temperature * eps_k * KELVIN;
let moles = arr1(&[2.0]) * MOL;
let volume = (sig * ANGSTROM).powi(3) / reduced_density * NAV * 2.0 * MOL;
let s = State::new_nvt(&eos, temperature, volume, &moles).unwrap();
let a = s
.residual_helmholtz_energy()
.to_reduced(RGAS * temperature * s.total_moles)
.unwrap();
dbg!(a);
assert_relative_eq!(a, 0.37659379124271003, max_relative = 1e-12);
Ok(())
}
#[test]
fn helmholtz_energy_mixtures_bh() -> EosResult<()> {
// Mixture of equal components --> result must be the same as for pure fluid ///
// component 1
let rep1 = 24.0;
let eps_k1 = 150.03;
let sig1 = 3.7039;
let r1 = UVRecord::new(rep1, 6.0, sig1, eps_k1);
let i = Identifier::new(None, None, None, None, None, None);
// compontent 2
let rep2 = 24.0;
let eps_k2 = 150.03;
let sig2 = 3.7039;
let r2 = UVRecord::new(rep2, 6.0, sig2, eps_k2);
let j = Identifier::new(None, None, None, None, None, None);
//////////////
let pr1 = PureRecord::new(i, 1.0, r1);
let pr2 = PureRecord::new(j, 1.0, r2);
let pure_records = vec![pr1, pr2];
let uv_parameters = UVParameters::new_binary(pure_records, None)?;
// state
let reduced_temperature = 4.0;
let eps_k_x = (eps_k1 + eps_k2) / 2.0; // Check rule!!
let t_x = reduced_temperature * eps_k_x * KELVIN;
let sig_x = (sig1 + sig2) / 2.0; // Check rule!!
let reduced_density = 1.0;
let moles = arr1(&[1.7, 0.3]) * MOL;
let total_moles = moles.sum();
let volume = (sig_x * ANGSTROM).powi(3) / reduced_density * NAV * total_moles;
// EoS
let options = UVTheoryOptions {
max_eta: 0.5,
perturbation: Perturbation::BarkerHenderson,
virial_order: VirialOrder::Second,
};
let eos_bh = Arc::new(UVTheory::with_options(Arc::new(uv_parameters), options)?);
let state_bh = State::new_nvt(&eos_bh, t_x, volume, &moles).unwrap();
let a_bh = state_bh
.residual_helmholtz_energy()
.to_reduced(RGAS * t_x * state_bh.total_moles)
.unwrap();
assert_relative_eq!(a_bh, 2.993577305779432, max_relative = 1e-12);
Ok(())
}
#[test]
fn helmholtz_energy_wca_mixture() -> EosResult<()> {
let p = test_parameters_mixture(
arr1(&[12.0, 12.0]),
arr1(&[6.0, 6.0]),
arr1(&[1.0, 1.0]),
arr1(&[1.0, 0.5]),
);
// state
let reduced_temperature = 1.0;
let t_x = reduced_temperature * p.epsilon_k[0] * KELVIN;
let reduced_density = 0.9;
let moles = arr1(&[0.4, 0.6]) * MOL;
let total_moles = moles.sum();
let volume = (p.sigma[0] * ANGSTROM).powi(3) / reduced_density * NAV * total_moles;
// EoS
let eos_wca = Arc::new(UVTheory::new(Arc::new(p))?);
let state_wca = State::new_nvt(&eos_wca, t_x, volume, &moles).unwrap();
let a_wca = state_wca
.residual_helmholtz_energy()
.to_reduced(RGAS * t_x * state_wca.total_moles)
.unwrap();
assert_relative_eq!(a_wca, -0.597791038364405, max_relative = 1e-5);
Ok(())
}
#[test]
fn helmholtz_energy_wca_mixture_different_sigma() -> EosResult<()> {
let p = test_parameters_mixture(
arr1(&[12.0, 12.0]),
arr1(&[6.0, 6.0]),
arr1(&[1.0, 2.0]),
arr1(&[1.0, 0.5]),
);
// state
let reduced_temperature = 1.5;
let t_x = reduced_temperature * p.epsilon_k[0] * KELVIN;
let sigma_x_3 = (0.4 + 0.6 * 8.0) * ANGSTROM.powi(3);
let density = 0.52000000000000002 / sigma_x_3;
let moles = arr1(&[0.4, 0.6]) * MOL;
let total_moles = moles.sum();
let volume = NAV * total_moles / density;
// EoS
let eos_wca = Arc::new(UVTheory::new(Arc::new(p))?);
let state_wca = State::new_nvt(&eos_wca, t_x, volume, &moles).unwrap();
let a_wca = state_wca
.residual_helmholtz_energy()
.to_reduced(RGAS * t_x * state_wca.total_moles)
.unwrap();
assert_relative_eq!(a_wca, -0.034206207363139396, max_relative = 1e-5);
Ok(())
}
}