Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.

Commit b75dc1f

Browse files
g-bauerprehner
andauthored
Use pyo3 v 0.16 and Python compatible enums (#41)
* Removed PyContributions and PyVerbosity and made existing enums python compatible * remove pyproto from utils * Rename `Residual` and `ResidualP` (#43) Co-authored-by: Philipp Rehner <prehner@ethz.ch> Co-authored-by: Philipp Rehner <69816385+prehner@users.noreply.github.com>
1 parent d10adbe commit b75dc1f

File tree

13 files changed

+190
-301
lines changed

13 files changed

+190
-301
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ exclude = ["/.github/*", "*.ipynb", "/docs"]
1717
rustdoc-args = [ "--html-in-header", "./docs-header.html" ]
1818

1919
[dependencies]
20-
quantity = "0.4"
20+
quantity = "0.5"
2121
approx = "0.4"
22-
num-dual = { version = "0.4", features = ["ndarray"] }
22+
num-dual = { version = "0.5", features = ["linalg"] }
2323
ndarray = { version = "0.15", features = ["serde"] }
2424
num-traits = "0.2"
2525
thiserror = "1.0"
2626
serde = { version = "1.0", features = ["derive"] }
2727
serde_json = "1.0"
2828
indexmap = "1.7"
2929
either = "1.6"
30-
numpy = { version = "0.15", optional = true }
31-
pyo3 = { version = "0.15", optional = true }
30+
numpy = { version = "0.16", optional = true }
31+
pyo3 = { version = "0.16", optional = true }
3232

3333
[features]
3434
default = []

build_wheel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
feos-core = { path = "..", features = ["python"] }
13-
pyo3 = { version = "0.15", features = ["extension-module", "abi3", "abi3-py36"] }
13+
pyo3 = { version = "0.16", features = ["extension-module", "abi3", "abi3-py37"] }
1414

src/phase_equilibria/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use phase_diagram_pure::PhaseDiagramPure;
1818

1919
/// Level of detail in the iteration output.
2020
#[derive(Copy, Clone, PartialOrd, PartialEq)]
21+
#[cfg_attr(feature = "python", pyo3::pyclass)]
2122
pub enum Verbosity {
2223
/// Do not print output.
2324
None,

src/phase_equilibria/vle_pure.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
260260
let density = 0.75 * eos.max_density(None)?;
261261
let liquid = State::new_nvt(eos, temperature, U::reference_moles() / density, &m)?;
262262
let z = liquid.compressibility(Contributions::Total);
263-
let mu = liquid.chemical_potential(Contributions::Residual);
263+
let mu = liquid.chemical_potential(Contributions::ResidualNvt);
264264
let p = temperature
265265
* density
266266
* U::gas_constant()
@@ -427,7 +427,8 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
427427
(0..eos.components())
428428
.map(|i| {
429429
let pure_eos = Rc::new(eos.subset(&[i]));
430-
PhaseEquilibrium::pure_t(&pure_eos, temperature, None, SolverOptions::default()).ok()
430+
PhaseEquilibrium::pure_t(&pure_eos, temperature, None, SolverOptions::default())
431+
.ok()
431432
})
432433
.collect()
433434
}

src/python/cubic.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::joback::JobackRecord;
33
use crate::parameter::{IdentifierOption, Parameter, ParameterError, PureRecord};
44
use crate::python::joback::PyJobackRecord;
55
use crate::python::parameter::{PyBinaryRecord, PyChemicalRecord, PyIdentifier};
6-
use crate::python::{PyContributions, PyVerbosity};
76
use crate::*;
87
use numpy::convert::ToPyArray;
98
use numpy::{PyArray1, PyArray2};
@@ -25,10 +24,7 @@ impl PyPengRobinsonRecord {
2524
fn new(tc: f64, pc: f64, acentric_factor: f64) -> Self {
2625
Self(PengRobinsonRecord::new(tc, pc, acentric_factor))
2726
}
28-
}
2927

30-
#[pyproto]
31-
impl pyo3::class::basic::PyObjectProtocol for PyPengRobinsonRecord {
3228
fn __repr__(&self) -> PyResult<String> {
3329
Ok(self.0.to_string())
3430
}
@@ -113,8 +109,8 @@ impl_vle_state!(PengRobinson, PyPengRobinson);
113109
#[pymodule]
114110
pub fn cubic(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
115111
m.add_class::<PyIdentifier>()?;
116-
m.add_class::<PyVerbosity>()?;
117-
m.add_class::<PyContributions>()?;
112+
m.add_class::<Verbosity>()?;
113+
m.add_class::<Contributions>()?;
118114
m.add_class::<PyChemicalRecord>()?;
119115
m.add_class::<PyJobackRecord>()?;
120116

src/python/joback.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ impl PyJobackRecord {
3838
fn new(a: f64, b: f64, c: f64, d: f64, e: f64) -> Self {
3939
Self(JobackRecord::new(a, b, c, d, e))
4040
}
41-
}
4241

43-
#[pyproto]
44-
impl pyo3::class::basic::PyObjectProtocol for PyJobackRecord {
4542
fn __repr__(&self) -> PyResult<String> {
4643
Ok(self.0.to_string())
4744
}

src/python/mod.rs

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::{Contributions, EosError, Verbosity};
1+
use crate::EosError;
22
use pyo3::exceptions::PyRuntimeError;
33
use pyo3::prelude::*;
44
use pyo3::{wrap_pymodule, PyErr};
5-
use quantity::python::PyInit_quantity;
5+
use quantity::python::__PYO3_PYMODULE_DEF_QUANTITY;
66

77
mod cubic;
88
mod equation_of_state;
@@ -14,83 +14,8 @@ mod statehd;
1414
mod user_defined;
1515
mod utils;
1616

17-
pub use cubic::PyInit_cubic;
18-
pub use user_defined::PyInit_user_defined;
19-
20-
/// Helmholtz energy contributions to consider
21-
/// when computing a property.
22-
#[pyclass(name = "Contributions")]
23-
#[derive(Copy, Clone)]
24-
pub struct PyContributions(pub Contributions);
25-
26-
#[pymethods]
27-
impl PyContributions {
28-
/// Only compute ideal gas contribution.
29-
#[classattr]
30-
#[allow(non_snake_case)]
31-
pub fn IdealGas() -> Self {
32-
Self(Contributions::IdealGas)
33-
}
34-
35-
/// Only compute residual contribution with respect
36-
/// to an ideal gas contribution which is defined at
37-
/// T, V, {n}.
38-
///
39-
/// See also
40-
/// --------
41-
/// ResidualP: to use an ideal gas reference defined at T, p, {n}
42-
#[classattr]
43-
#[allow(non_snake_case)]
44-
pub fn Residual() -> Self {
45-
Self(Contributions::Residual)
46-
}
47-
48-
/// Only compute residual contribution with respect
49-
/// to an ideal gas contribution which is defined at
50-
/// T, p, {n}.
51-
///
52-
/// See also
53-
/// --------
54-
/// Residual: to use an ideal gas reference defined at T, V, {n}
55-
#[classattr]
56-
#[allow(non_snake_case)]
57-
pub fn ResidualP() -> Self {
58-
Self(Contributions::ResidualP)
59-
}
60-
61-
/// Compute all contributions
62-
///
63-
/// Note
64-
/// ----
65-
/// This is the default for most properties.
66-
#[classattr]
67-
#[allow(non_snake_case)]
68-
pub fn Total() -> Self {
69-
Self(Contributions::Total)
70-
}
71-
}
72-
73-
/// Verbosity levels for iterative solvers.
74-
#[pyclass(name = "Verbosity")]
75-
#[derive(Copy, Clone)]
76-
pub struct PyVerbosity(pub Verbosity);
77-
78-
#[pymethods]
79-
impl PyVerbosity {
80-
/// Print a status message at the end of the iteration.
81-
#[classattr]
82-
#[allow(non_snake_case)]
83-
pub fn Result() -> Self {
84-
Self(Verbosity::Result)
85-
}
86-
87-
/// Print a detailed progress of the iteration.
88-
#[classattr]
89-
#[allow(non_snake_case)]
90-
pub fn Iter() -> Self {
91-
Self(Verbosity::Iter)
92-
}
93-
}
17+
pub use cubic::__PYO3_PYMODULE_DEF_CUBIC;
18+
pub use user_defined::__PYO3_PYMODULE_DEF_USER_DEFINED;
9419

9520
impl From<EosError> for PyErr {
9621
fn from(e: EosError) -> PyErr {

src/python/parameter.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ impl PyIdentifier {
111111
fn set_formula(&mut self, formula: &str) {
112112
self.0.formula = Some(formula.to_string());
113113
}
114-
}
115114

116-
#[pyproto]
117-
impl pyo3::class::basic::PyObjectProtocol for PyIdentifier {
118115
fn __repr__(&self) -> PyResult<String> {
119116
Ok(self.0.to_string())
120117
}
@@ -205,10 +202,7 @@ impl PyChemicalRecord {
205202
.to_object(py),
206203
}
207204
}
208-
}
209205

210-
#[pyproto]
211-
impl pyo3::class::basic::PyObjectProtocol for PyChemicalRecord {
212206
fn __repr__(&self) -> PyResult<String> {
213207
Ok(self.0.to_string())
214208
}
@@ -271,10 +265,7 @@ impl PyBinaryRecord {
271265
fn set_model_record(&mut self, model_record: f64) {
272266
self.0.model_record = model_record;
273267
}
274-
}
275268

276-
#[pyproto]
277-
impl pyo3::class::basic::PyObjectProtocol for PyBinaryRecord {
278269
fn __repr__(&self) -> PyResult<String> {
279270
Ok(self.0.to_string())
280271
}
@@ -337,10 +328,7 @@ impl PyBinarySegmentRecord {
337328
fn set_model_record(&mut self, model_record: f64) {
338329
self.0.model_record = model_record;
339330
}
340-
}
341331

342-
#[pyproto]
343-
impl pyo3::class::basic::PyObjectProtocol for PyBinarySegmentRecord {
344332
fn __repr__(&self) -> PyResult<String> {
345333
Ok(self.0.to_string())
346334
}
@@ -428,10 +416,7 @@ macro_rules! impl_pure_record {
428416
fn set_ideal_gas_record(&mut self, ideal_gas_record: $py_ideal_gas_record) {
429417
self.0.ideal_gas_record = Some(ideal_gas_record.0);
430418
}
431-
}
432419

433-
#[pyproto]
434-
impl pyo3::class::basic::PyObjectProtocol for PyPureRecord {
435420
fn __repr__(&self) -> PyResult<String> {
436421
Ok(self.0.to_string())
437422
}
@@ -540,10 +525,7 @@ macro_rules! impl_segment_record {
540525
fn set_ideal_gas_record(&mut self, ideal_gas_record: $py_ideal_gas_record) {
541526
self.0.ideal_gas_record = Some(ideal_gas_record.0);
542527
}
543-
}
544528

545-
#[pyproto]
546-
impl pyo3::class::basic::PyObjectProtocol for PySegmentRecord {
547529
fn __repr__(&self) -> PyResult<String> {
548530
Ok(self.0.to_string())
549531
}

0 commit comments

Comments
 (0)