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

Commit

Permalink
Use pyo3 v 0.16 and Python compatible enums (#41)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Mar 10, 2022
1 parent d10adbe commit b75dc1f
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 301 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ exclude = ["/.github/*", "*.ipynb", "/docs"]
rustdoc-args = [ "--html-in-header", "./docs-header.html" ]

[dependencies]
quantity = "0.4"
quantity = "0.5"
approx = "0.4"
num-dual = { version = "0.4", features = ["ndarray"] }
num-dual = { version = "0.5", features = ["linalg"] }
ndarray = { version = "0.15", features = ["serde"] }
num-traits = "0.2"
thiserror = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
indexmap = "1.7"
either = "1.6"
numpy = { version = "0.15", optional = true }
pyo3 = { version = "0.15", optional = true }
numpy = { version = "0.16", optional = true }
pyo3 = { version = "0.16", optional = true }

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion build_wheel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["cdylib"]

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

1 change: 1 addition & 0 deletions src/phase_equilibria/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use phase_diagram_pure::PhaseDiagramPure;

/// Level of detail in the iteration output.
#[derive(Copy, Clone, PartialOrd, PartialEq)]
#[cfg_attr(feature = "python", pyo3::pyclass)]
pub enum Verbosity {
/// Do not print output.
None,
Expand Down
5 changes: 3 additions & 2 deletions src/phase_equilibria/vle_pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
let density = 0.75 * eos.max_density(None)?;
let liquid = State::new_nvt(eos, temperature, U::reference_moles() / density, &m)?;
let z = liquid.compressibility(Contributions::Total);
let mu = liquid.chemical_potential(Contributions::Residual);
let mu = liquid.chemical_potential(Contributions::ResidualNvt);
let p = temperature
* density
* U::gas_constant()
Expand Down Expand Up @@ -427,7 +427,8 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
(0..eos.components())
.map(|i| {
let pure_eos = Rc::new(eos.subset(&[i]));
PhaseEquilibrium::pure_t(&pure_eos, temperature, None, SolverOptions::default()).ok()
PhaseEquilibrium::pure_t(&pure_eos, temperature, None, SolverOptions::default())
.ok()
})
.collect()
}
Expand Down
8 changes: 2 additions & 6 deletions src/python/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::joback::JobackRecord;
use crate::parameter::{IdentifierOption, Parameter, ParameterError, PureRecord};
use crate::python::joback::PyJobackRecord;
use crate::python::parameter::{PyBinaryRecord, PyChemicalRecord, PyIdentifier};
use crate::python::{PyContributions, PyVerbosity};
use crate::*;
use numpy::convert::ToPyArray;
use numpy::{PyArray1, PyArray2};
Expand All @@ -25,10 +24,7 @@ impl PyPengRobinsonRecord {
fn new(tc: f64, pc: f64, acentric_factor: f64) -> Self {
Self(PengRobinsonRecord::new(tc, pc, acentric_factor))
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyPengRobinsonRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -113,8 +109,8 @@ impl_vle_state!(PengRobinson, PyPengRobinson);
#[pymodule]
pub fn cubic(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<PyIdentifier>()?;
m.add_class::<PyVerbosity>()?;
m.add_class::<PyContributions>()?;
m.add_class::<Verbosity>()?;
m.add_class::<Contributions>()?;
m.add_class::<PyChemicalRecord>()?;
m.add_class::<PyJobackRecord>()?;

Expand Down
3 changes: 0 additions & 3 deletions src/python/joback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ impl PyJobackRecord {
fn new(a: f64, b: f64, c: f64, d: f64, e: f64) -> Self {
Self(JobackRecord::new(a, b, c, d, e))
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyJobackRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down
83 changes: 4 additions & 79 deletions src/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{Contributions, EosError, Verbosity};
use crate::EosError;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::{wrap_pymodule, PyErr};
use quantity::python::PyInit_quantity;
use quantity::python::__PYO3_PYMODULE_DEF_QUANTITY;

mod cubic;
mod equation_of_state;
Expand All @@ -14,83 +14,8 @@ mod statehd;
mod user_defined;
mod utils;

pub use cubic::PyInit_cubic;
pub use user_defined::PyInit_user_defined;

/// Helmholtz energy contributions to consider
/// when computing a property.
#[pyclass(name = "Contributions")]
#[derive(Copy, Clone)]
pub struct PyContributions(pub Contributions);

#[pymethods]
impl PyContributions {
/// Only compute ideal gas contribution.
#[classattr]
#[allow(non_snake_case)]
pub fn IdealGas() -> Self {
Self(Contributions::IdealGas)
}

/// Only compute residual contribution with respect
/// to an ideal gas contribution which is defined at
/// T, V, {n}.
///
/// See also
/// --------
/// ResidualP: to use an ideal gas reference defined at T, p, {n}
#[classattr]
#[allow(non_snake_case)]
pub fn Residual() -> Self {
Self(Contributions::Residual)
}

/// Only compute residual contribution with respect
/// to an ideal gas contribution which is defined at
/// T, p, {n}.
///
/// See also
/// --------
/// Residual: to use an ideal gas reference defined at T, V, {n}
#[classattr]
#[allow(non_snake_case)]
pub fn ResidualP() -> Self {
Self(Contributions::ResidualP)
}

/// Compute all contributions
///
/// Note
/// ----
/// This is the default for most properties.
#[classattr]
#[allow(non_snake_case)]
pub fn Total() -> Self {
Self(Contributions::Total)
}
}

/// Verbosity levels for iterative solvers.
#[pyclass(name = "Verbosity")]
#[derive(Copy, Clone)]
pub struct PyVerbosity(pub Verbosity);

#[pymethods]
impl PyVerbosity {
/// Print a status message at the end of the iteration.
#[classattr]
#[allow(non_snake_case)]
pub fn Result() -> Self {
Self(Verbosity::Result)
}

/// Print a detailed progress of the iteration.
#[classattr]
#[allow(non_snake_case)]
pub fn Iter() -> Self {
Self(Verbosity::Iter)
}
}
pub use cubic::__PYO3_PYMODULE_DEF_CUBIC;
pub use user_defined::__PYO3_PYMODULE_DEF_USER_DEFINED;

impl From<EosError> for PyErr {
fn from(e: EosError) -> PyErr {
Expand Down
18 changes: 0 additions & 18 deletions src/python/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ impl PyIdentifier {
fn set_formula(&mut self, formula: &str) {
self.0.formula = Some(formula.to_string());
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyIdentifier {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -205,10 +202,7 @@ impl PyChemicalRecord {
.to_object(py),
}
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyChemicalRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -271,10 +265,7 @@ impl PyBinaryRecord {
fn set_model_record(&mut self, model_record: f64) {
self.0.model_record = model_record;
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyBinaryRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -337,10 +328,7 @@ impl PyBinarySegmentRecord {
fn set_model_record(&mut self, model_record: f64) {
self.0.model_record = model_record;
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyBinarySegmentRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -428,10 +416,7 @@ macro_rules! impl_pure_record {
fn set_ideal_gas_record(&mut self, ideal_gas_record: $py_ideal_gas_record) {
self.0.ideal_gas_record = Some(ideal_gas_record.0);
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PyPureRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down Expand Up @@ -540,10 +525,7 @@ macro_rules! impl_segment_record {
fn set_ideal_gas_record(&mut self, ideal_gas_record: $py_ideal_gas_record) {
self.0.ideal_gas_record = Some(ideal_gas_record.0);
}
}

#[pyproto]
impl pyo3::class::basic::PyObjectProtocol for PySegmentRecord {
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
Expand Down
Loading

0 comments on commit b75dc1f

Please sign in to comment.