Skip to content

Commit

Permalink
Update PyO3 to 0.21 (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner committed Apr 22, 2024
1 parent 83088e2 commit b78e80a
Show file tree
Hide file tree
Showing 34 changed files with 266 additions and 244 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Added SAFT-VR Mie equation of state.[#237](https://github.com/feos-org/feos/pull/237)
- Added SAFT-VR Mie equation of state. [#237](https://github.com/feos-org/feos/pull/237)
- Added ePC-SAFT equation of state. [#229](https://github.com/feos-org/feos/pull/229)

### Changed
Expand All @@ -20,6 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- Removed `VirialOrder` from `uvtheory` module. Orders are now variants of the existing `Perturbation` enum. [#226](https://github.com/feos-org/feos/pull/226)

### Packaging
- Updated `quantity` dependency to 0.8. [#238](https://github.com/feos-org/feos/pull/238)
- Updated `num-dual` dependency to 0.9. [#238](https://github.com/feos-org/feos/pull/238)
- Updated `numpy` and `PyO3` dependencies to 0.21. [#238](https://github.com/feos-org/feos/pull/238)

## [0.6.1] - 2024-01-11
- Python only: Release the changes introduced in `feos-core` 0.6.1.

Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ members = ["feos-core", "feos-dft", "feos-derive"]
crate-type = ["rlib", "cdylib"]

[dependencies]
quantity = { version = "0.7", optional = true }
num-dual = "0.8"
quantity = { version = "0.8", optional = true }
num-dual = "0.9"
feos-core = { version = "0.6", path = "feos-core" }
feos-dft = { version = "0.6", path = "feos-dft", optional = true }
feos-derive = { version = "0.4", path = "feos-derive" }
numpy = { version = "0.20", optional = true }
numpy = { version = "0.21", optional = true }
ndarray = { version = "0.15", features = ["approx"] }
petgraph = { version = "0.6", optional = true }
thiserror = "1.0"
Expand All @@ -42,7 +42,7 @@ itertools = "0.12"
typenum = "1.16"

[dependencies.pyo3]
version = "0.20"
version = "0.21"
features = ["extension-module", "abi3", "abi3-py37"]
optional = true

Expand Down
6 changes: 5 additions & 1 deletion feos-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Added comparison for pressures of both phases in `PyPhaseDiagram.to_dict` to make the method work with `spinodal` constructor. [#224](https://github.com/feos-org/feos/pull/224)
- Enforce the total moles when providing `State::new` with molefracs that do not add up to 1. [#227](https://github.com/feos-org/feos/pull/227)
-

### Packaging
- Updated `quantity` dependency to 0.8. [#238](https://github.com/feos-org/feos/pull/238)
- Updated `num-dual` dependency to 0.9. [#238](https://github.com/feos-org/feos/pull/238)
- Updated `numpy` and `PyO3` dependencies to 0.21. [#238](https://github.com/feos-org/feos/pull/238)


## [0.6.1] 2024-01-11
Expand Down
8 changes: 4 additions & 4 deletions feos-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ rustdoc-args = [ "--html-in-header", "./docs-header.html" ]
features = [ "rayon" ]

[dependencies]
quantity = { version = "0.7", optional = true }
num-dual = { version = "0.8", features = ["linalg"] }
quantity = { version = "0.8", optional = true }
num-dual = { version = "0.9", features = ["linalg"] }
ndarray = { version = "0.15", features = ["serde", "approx-0_5"] }
nalgebra = "0.32"
num-traits = "0.2"
Expand All @@ -28,8 +28,8 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
indexmap = "2.0"
conv = "0.3"
numpy = { version = "0.20", optional = true }
pyo3 = { version = "0.20", optional = true }
numpy = { version = "0.21", optional = true }
pyo3 = { version = "0.21", optional = true }
rayon = { version = "1.5", optional = true }
typenum = "1.16"
approx = "0.5"
Expand Down
8 changes: 4 additions & 4 deletions feos-core/src/python/parameter/fragmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ impl_json_handling!(PySmartsRecord);
impl PyChemicalRecord {
#[staticmethod]
pub fn from_smiles(
py: Python<'_>,
identifier: &PyAny,
identifier: &Bound<'_, PyAny>,
smarts: Vec<PySmartsRecord>,
) -> PyResult<Self> {
let py = identifier.py();
let identifier = if let Ok(smiles) = identifier.extract::<String>() {
Identifier::new(None, None, None, Some(&smiles), None, None)
} else if let Ok(identifier) = identifier.extract::<PyIdentifier>() {
Expand All @@ -113,7 +113,7 @@ fn fragment_molecule(
smiles: &str,
smarts: Vec<PySmartsRecord>,
) -> PyResult<(Vec<String>, Vec<[usize; 2]>)> {
let chem = py.import("rdkit.Chem")?;
let chem = py.import_bound("rdkit.Chem")?;
let mol = chem.call_method1("MolFromSmiles", (smiles,))?;
let atoms = mol.call_method0("GetNumHeavyAtoms")?.extract::<usize>()?;

Expand Down Expand Up @@ -155,7 +155,7 @@ fn fragment_molecule(
.for_each(|(_, m)| m.retain(|m| !(m.len() == 1 && large_segments.contains(&m[0]))));

let bonds = mol.call_method0("GetBonds")?;
let builtins = py.import("builtins")?;
let builtins = py.import_bound("builtins")?;
let bonds = builtins
.call_method1("list", (bonds,))?
.extract::<Vec<&PyAny>>()?;
Expand Down
54 changes: 31 additions & 23 deletions feos-core/src/python/parameter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ macro_rules! impl_binary_record {
#[pymethods]
impl PyBinaryRecord {
#[new]
fn new(id1: PyIdentifier, id2: PyIdentifier, model_record: &PyAny) -> PyResult<Self> {
fn new(
id1: PyIdentifier,
id2: PyIdentifier,
model_record: &Bound<'_, PyAny>,
) -> PyResult<Self> {
if let Ok(mr) = model_record.extract::<f64>() {
Ok(Self(BinaryRecord::new(id1.0, id2.0, mr.try_into()?)))
} else if let Ok(mr) = model_record.extract::<$py_model_record>() {
Expand Down Expand Up @@ -264,16 +268,16 @@ macro_rules! impl_binary_record {
}

#[getter]
fn get_model_record(&self, py: Python) -> PyObject {
if let Ok(mr) = f64::try_from(self.0.model_record.clone()) {
mr.to_object(py)
fn get_model_record<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
Ok(if let Ok(mr) = f64::try_from(self.0.model_record.clone()) {
pyo3::types::PyFloat::new_bound(py, mr).into_any()
} else {
$py_model_record(self.0.model_record.clone()).into_py(py)
}
Bound::new(py, $py_model_record(self.0.model_record.clone()))?.into_any()
})
}

#[setter]
fn set_model_record(&mut self, model_record: &PyAny) -> PyResult<()> {
fn set_model_record(&mut self, model_record: &Bound<'_, PyAny>) -> PyResult<()> {
if let Ok(mr) = model_record.extract::<f64>() {
self.0.model_record = mr.try_into()?;
} else if let Ok(mr) = model_record.extract::<$py_model_record>() {
Expand Down Expand Up @@ -558,6 +562,8 @@ macro_rules! impl_parameter {
impl_parameter!($parameter, $py_parameter, $py_model_record, PyNoBinaryModelRecord);
};
($parameter:ty, $py_parameter:ty, $py_model_record:ty, $py_binary_model_record:ty) => {
use pyo3::pybacked::*;

#[pymethods]
impl $py_parameter {
/// Creates parameters from records.
Expand All @@ -578,14 +584,14 @@ macro_rules! impl_parameter {
)]
fn from_records(
pure_records: Vec<PyPureRecord>,
binary_records: Option<&PyAny>,
binary_records: Option<&Bound<'_, PyAny>>,
identifier_option: IdentifierOption,
) -> PyResult<Self> {
let prs: Vec<_> = pure_records.into_iter().map(|pr| pr.0).collect();
let binary_records = binary_records
.map(|binary_records| {
if let Ok(br) = binary_records.extract::<PyReadonlyArray2<f64>>() {
Ok(Some(br.to_owned_array().mapv(|r| r.try_into().unwrap())))
Ok(Some(br.as_array().mapv(|r| r.try_into().unwrap())))
} else if let Ok(br) = binary_records.extract::<Vec<PyBinaryRecord>>() {
let brs: Vec<_> = br.into_iter().map(|br| br.0).collect();
Ok(<$parameter>::binary_matrix_from_records(
Expand Down Expand Up @@ -628,7 +634,7 @@ macro_rules! impl_parameter {
#[pyo3(text_signature = "(pure_records, binary_record=None)")]
fn new_binary(
pure_records: Vec<PyPureRecord>,
binary_record: Option<&PyAny>,
binary_record: Option<&Bound<'_, PyAny>>,
) -> PyResult<Self> {
let prs = pure_records.into_iter().map(|pr| pr.0).collect();
let br = binary_record
Expand Down Expand Up @@ -678,11 +684,12 @@ macro_rules! impl_parameter {
text_signature = "(substances, pure_path, binary_path=None, identifier_option)"
)]
fn from_json(
substances: Vec<&str>,
substances: Vec<PyBackedStr>,
pure_path: String,
binary_path: Option<String>,
identifier_option: IdentifierOption,
) -> Result<Self, ParameterError> {
let substances = substances.iter().map(|s| &**s).collect();
Ok(Self(Arc::new(<$parameter>::from_json(
substances,
pure_path,
Expand All @@ -708,13 +715,14 @@ macro_rules! impl_parameter {
text_signature = "(input, binary_path=None, identifier_option)"
)]
fn from_multiple_json(
input: Vec<(Vec<&str>, &str)>,
binary_path: Option<&str>,
input: Vec<(Vec<PyBackedStr>, PyBackedStr)>,
binary_path: Option<PyBackedStr>,
identifier_option: Option<IdentifierOption>,
) -> Result<Self, ParameterError> {
let input: Vec<(Vec<&str>, &str)> = input.iter().map(|(c, f)| (c.iter().map(|c| &**c).collect(), &**f)).collect();
Ok(Self(Arc::new(<$parameter>::from_multiple_json(
&input,
binary_path,
binary_path.as_deref(),
identifier_option.unwrap_or(IdentifierOption::Name),
)?)))
}
Expand All @@ -730,11 +738,11 @@ macro_rules! impl_parameter {
}

#[getter]
fn get_binary_records<'py>(&self, py: Python<'py>) -> Option<&'py PyArray2<f64>> {
fn get_binary_records<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray2<f64>>> {
self.0
.records()
.1
.map(|r| r.mapv(|r| f64::try_from(r).unwrap()).view().to_pyarray(py))
.map(|r| r.mapv(|r| f64::try_from(r).unwrap()).view().to_pyarray_bound(py))
}
}
};
Expand All @@ -743,6 +751,8 @@ macro_rules! impl_parameter {
#[macro_export]
macro_rules! impl_parameter_from_segments {
($parameter:ty, $py_parameter:ty) => {
use pyo3::pybacked::*;

#[pymethods]
impl $py_parameter {
/// Creates parameters from segment records.
Expand Down Expand Up @@ -790,12 +800,13 @@ macro_rules! impl_parameter_from_segments {
text_signature = "(substances, pure_path, segments_path, binary_path=None, identifier_option)"
)]
fn from_json_segments(
substances: Vec<&str>,
substances: Vec<PyBackedStr>,
pure_path: String,
segments_path: String,
binary_path: Option<String>,
identifier_option: IdentifierOption,
) -> PyResult<Self> {
let substances: Vec<_> = substances.iter().map(|s| &**s).collect();
Ok(Self(Arc::new(<$parameter>::from_json_segments(
&substances,
pure_path,
Expand Down Expand Up @@ -824,15 +835,14 @@ macro_rules! impl_parameter_from_segments {
#[staticmethod]
#[pyo3(text_signature = "(identifier, smarts_records, segment_records, binary_segment_records=None)")]
fn from_smiles(
py: Python<'_>,
identifier: Vec<&PyAny>,
identifier: Vec<Bound<'_,PyAny>>,
smarts_records: Vec<PySmartsRecord>,
segment_records: Vec<PySegmentRecord>,
binary_segment_records: Option<Vec<PyBinarySegmentRecord>>,
) -> PyResult<Self> {
let chemical_records: Vec<_> = identifier
.into_iter()
.map(|i| PyChemicalRecord::from_smiles(py, i, smarts_records.clone()))
.map(|i| PyChemicalRecord::from_smiles(&i, smarts_records.clone()))
.collect::<PyResult<_>>()?;
Self::from_segments(chemical_records, segment_records, binary_segment_records)
}
Expand All @@ -857,8 +867,7 @@ macro_rules! impl_parameter_from_segments {
text_signature = "(identifier, smarts_path, segments_path, binary_path=None)"
)]
fn from_json_smiles(
py: Python<'_>,
identifier: Vec<&PyAny>,
identifier: Vec<Bound<'_,PyAny>>,
smarts_path: String,
segments_path: String,
binary_path: Option<String>,
Expand All @@ -867,7 +876,6 @@ macro_rules! impl_parameter_from_segments {
let segment_records = PySegmentRecord::from_json(&segments_path)?;
let binary_segment_records = binary_path.map(|p| PyBinarySegmentRecord::from_json(&p)).transpose()?;
Self::from_smiles(
py,
identifier,
smarts_records,
segment_records,
Expand Down
12 changes: 6 additions & 6 deletions feos-core/src/python/phase_equilibria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ macro_rules! impl_phase_equilibrium {
/// PhaseEquilibrium
#[staticmethod]
#[pyo3(text_signature = "(eos, temperature_or_pressure, liquid_molefracs, tp_init=None, vapor_molefracs=None, max_iter_inner=None, max_iter_outer=None, tol_inner=None, tol_outer=None, verbosity=None)")]
pub fn bubble_point(
pub fn bubble_point<'py>(
eos: $py_eos,
temperature_or_pressure: PySINumber,
liquid_molefracs: &PyArray1<f64>,
liquid_molefracs: &Bound<'py, PyArray1<f64>>,
tp_init: Option<PySINumber>,
vapor_molefracs: Option<&PyArray1<f64>>,
vapor_molefracs: Option<&Bound<'py, PyArray1<f64>>>,
max_iter_inner: Option<usize>,
max_iter_outer: Option<usize>,
tol_inner: Option<f64>,
Expand Down Expand Up @@ -232,12 +232,12 @@ macro_rules! impl_phase_equilibrium {
/// PhaseEquilibrium
#[staticmethod]
#[pyo3(text_signature = "(eos, temperature_or_pressure, vapor_molefracs, tp_init=None, liquid_molefracs=None, max_iter_inner=None, max_iter_outer=None, tol_inner=None, tol_outer=None, verbosity=None)")]
pub fn dew_point(
pub fn dew_point<'py>(
eos: $py_eos,
temperature_or_pressure: PySINumber,
vapor_molefracs: &PyArray1<f64>,
vapor_molefracs: &Bound<'py, PyArray1<f64>>,
tp_init: Option<PySINumber>,
liquid_molefracs: Option<&PyArray1<f64>>,
liquid_molefracs: Option<&Bound<'py, PyArray1<f64>>>,
max_iter_inner: Option<usize>,
max_iter_outer: Option<usize>,
tol_inner: Option<f64>,
Expand Down
Loading

0 comments on commit b78e80a

Please sign in to comment.