-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpython.rs
More file actions
240 lines (214 loc) · 6.33 KB
/
python.rs
File metadata and controls
240 lines (214 loc) · 6.33 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
//! Python bindings for the SAFT-VRQ Mie equation of state.
use crate::saftvrqmie::eos::FeynmanHibbsOrder;
use crate::saftvrqmie::parameters::{
SaftVRQMieBinaryRecord, SaftVRQMieParameters, SaftVRQMieRecord,
};
use feos_core::joback::JobackRecord;
use feos_core::parameter::{
BinaryRecord, Identifier, IdentifierOption, Parameter, ParameterError, PureRecord,
};
use feos_core::python::joback::PyJobackRecord;
use feos_core::python::parameter::PyIdentifier;
use feos_core::*;
use ndarray::Array2;
use numpy::{PyArray2, PyReadonlyArray2, ToPyArray};
use pyo3::exceptions::{PyIOError, PyTypeError};
use pyo3::prelude::*;
use quantity::python::PySINumber;
use std::convert::{TryFrom, TryInto};
use std::sync::Arc;
/// Create a set of Saft-VRQ Mie parameters from records.
#[pyclass(name = "SaftVRQMieRecord")]
#[pyo3(
text_signature = "(m, sigma, epsilon_k, viscosity=None, diffusion=None, thermal_conductivity=None)"
)]
#[derive(Clone)]
pub struct PySaftVRQMieRecord(SaftVRQMieRecord);
#[pymethods]
impl PySaftVRQMieRecord {
#[new]
fn new(
m: f64,
sigma: f64,
epsilon_k: f64,
lr: f64,
la: f64,
viscosity: Option<[f64; 4]>,
) -> Self {
Self(SaftVRQMieRecord::new(
m, sigma, epsilon_k, lr, la, viscosity, None, None,
))
}
#[getter]
fn get_m(&self) -> f64 {
self.0.m
}
#[getter]
fn get_sigma(&self) -> f64 {
self.0.sigma
}
#[getter]
fn get_epsilon_k(&self) -> f64 {
self.0.epsilon_k
}
#[getter]
fn get_lr(&self) -> f64 {
self.0.lr
}
#[getter]
fn get_la(&self) -> f64 {
self.0.la
}
#[getter]
fn get_viscosity(&self) -> Option<[f64; 4]> {
self.0.viscosity
}
#[getter]
fn get_diffusion(&self) -> Option<[f64; 5]> {
self.0.diffusion
}
#[getter]
fn get_thermal_conductivity(&self) -> Option<[f64; 4]> {
self.0.thermal_conductivity
}
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
}
/// Create a set of Saft-VRQ Mie parameters from records.
#[pyclass(name = "SaftVRQMieBinaryRecord")]
#[pyo3(text_signature = "(k_ij, l_ij)")]
#[derive(Clone)]
pub struct PySaftVRQMieBinaryRecord(SaftVRQMieBinaryRecord);
#[pymethods]
impl PySaftVRQMieBinaryRecord {
#[new]
fn new(k_ij: f64, l_ij: f64) -> Self {
Self(SaftVRQMieBinaryRecord { k_ij, l_ij })
}
#[getter]
fn get_k_ij(&self) -> f64 {
self.0.k_ij
}
#[getter]
fn get_l_ij(&self) -> f64 {
self.0.l_ij
}
#[setter]
fn set_k_ij(&mut self, k_ij: f64) {
self.0.k_ij = k_ij
}
#[setter]
fn set_l_ij(&mut self, l_ij: f64) {
self.0.l_ij = l_ij
}
}
/// Create a set of SAFT-VRQ Mie parameters from records.
///
/// Parameters
/// ----------
/// pure_records : List[PureRecord]
/// pure substance records.
/// binary_records : List[BinaryRecord], optional
/// binary saft parameter records
/// substances : List[str], optional
/// The substances to use. Filters substances from `pure_records` according to
/// `search_option`.
/// When not provided, all entries of `pure_records` are used.
/// search_option : {'Name', 'Cas', 'Inchi', 'IupacName', 'Formula', 'Smiles'}, optional, defaults to 'Name'.
/// Identifier that is used to search substance.
///
/// Returns
/// -------
/// SaftVRQMieParameters
#[pyclass(name = "SaftVRQMieParameters")]
#[pyo3(
text_signature = "(pure_records, binary_records=None, substances=None, search_option='Name')"
)]
#[derive(Clone)]
pub struct PySaftVRQMieParameters(pub Arc<SaftVRQMieParameters>);
impl_json_handling!(PySaftVRQMieRecord);
impl_pure_record!(
SaftVRQMieRecord,
PySaftVRQMieRecord,
JobackRecord,
PyJobackRecord
);
impl_binary_record!(SaftVRQMieBinaryRecord, PySaftVRQMieBinaryRecord);
impl_parameter!(SaftVRQMieParameters, PySaftVRQMieParameters);
#[pymethods]
impl PySaftVRQMieParameters {
#[getter]
fn get_k_ij<'py>(&self, py: Python<'py>) -> &'py PyArray2<f64> {
self.0.k_ij.view().to_pyarray(py)
}
#[getter]
fn get_l_ij<'py>(&self, py: Python<'py>) -> &'py PyArray2<f64> {
self.0.l_ij.view().to_pyarray(py)
}
/// Generate energy and force tables to be used with LAMMPS' `pair_style table` command.
///
/// Parameters
/// ----------
/// temperature : SINumber
/// temperature at which the Feynman-Hibbs corrected Mie potential
/// is evaluated.
/// n : int
/// total number of points to calculate in the table between r_min and r_max.
/// r_min : SINumber
/// minimum distance (included)
/// r_max : SINumber
/// maximum distance (included)
///
/// Raises
/// ------
/// IOError
/// if there are issues with writing to a file.
///
/// Notes
/// -----
///
/// For a given `temperature`, `n` values between `r_min` and `r_max` (both including) are tabulated.
///
/// Files for all pure substances and all unique pairs are generated,
/// where filenames use either the "name" field of the identifier or the index if no name is present.
///
/// Example
/// -------
///
/// For a hydrogen-neon mixture at 30 K, three files will be created.
///
/// - "hydrogen_30K.table" for H-H interactions,
/// - "neon_30K.table" for Ne-Ne interactions,
/// - "hydrogen_neon_30K.table" for H-Ne interactions.
#[pyo3(text_signature = "($self, temperature, n, r_min, r_max)")]
fn lammps_tables(
&self,
temperature: PySINumber,
n: usize,
r_min: PySINumber,
r_max: PySINumber,
) -> PyResult<()> {
self.0
.lammps_tables(temperature.into(), n, r_min.into(), r_max.into())
.map_err(|e| PyIOError::new_err(e))
}
fn _repr_markdown_(&self) -> String {
self.0.to_markdown()
}
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
}
#[pymodule]
pub fn saftvrqmie(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<PyIdentifier>()?;
m.add_class::<IdentifierOption>()?;
m.add_class::<PyJobackRecord>()?;
m.add_class::<FeynmanHibbsOrder>()?;
m.add_class::<PySaftVRQMieRecord>()?;
m.add_class::<PyPureRecord>()?;
m.add_class::<PyBinaryRecord>()?;
m.add_class::<PySaftVRQMieParameters>()?;
Ok(())
}