From e5896bcff993df5edd64d9576a6bdb16f38dafc1 Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Fri, 15 Jul 2022 14:17:23 +0200 Subject: [PATCH 1/3] made binary parameters in from_records python routine an Option and added panic with message when parsing missing identifiers --- feos-core/src/parameter/mod.rs | 16 +++++++++++-- feos-core/src/python/cubic.rs | 1 + feos-core/src/python/parameter.rs | 40 ++++++++++++++++++------------- src/gc_pcsaft/dft/parameter.rs | 1 - src/pcsaft/python.rs | 1 + 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/feos-core/src/parameter/mod.rs b/feos-core/src/parameter/mod.rs index bee40af34..2df800c86 100644 --- a/feos-core/src/parameter/mod.rs +++ b/feos-core/src/parameter/mod.rs @@ -92,8 +92,20 @@ where }; let n = pure_records.len(); Array2::from_shape_fn([n, n], |(i, j)| { - let id1 = pure_records[i].identifier.as_string(search_option).unwrap(); - let id2 = pure_records[j].identifier.as_string(search_option).unwrap(); + let id1 = pure_records[i] + .identifier + .as_string(search_option) + .expect(&format!( + "No identifier for given search_option for pure record {}.", + i + )); + let id2 = pure_records[j] + .identifier + .as_string(search_option) + .expect(&format!( + "No identifier for given search_option for pure record {}.", + j + )); binary_map .get(&(id1.clone(), id2.clone())) .or_else(|| binary_map.get(&(id2, id1))) diff --git a/feos-core/src/python/cubic.rs b/feos-core/src/python/cubic.rs index d23f9f83d..a6f51d4ec 100644 --- a/feos-core/src/python/cubic.rs +++ b/feos-core/src/python/cubic.rs @@ -6,6 +6,7 @@ use crate::parameter::{ use crate::python::joback::PyJobackRecord; use crate::python::parameter::PyIdentifier; use crate::*; +use ndarray::Array2; use numpy::PyReadonlyArray2; use pyo3::exceptions::PyTypeError; use pyo3::prelude::*; diff --git a/feos-core/src/python/parameter.rs b/feos-core/src/python/parameter.rs index b9b87e8c6..e486db91a 100644 --- a/feos-core/src/python/parameter.rs +++ b/feos-core/src/python/parameter.rs @@ -542,7 +542,7 @@ macro_rules! impl_parameter { /// ---------- /// pure_records : [PureRecord] /// A list of pure component parameters. - /// binary_records : numpy.ndarray[float] or List[BinaryRecord] + /// binary_records : numpy.ndarray[float] or List[BinaryRecord], optional /// A matrix of binary interaction parameters or a list /// containing records for binary interactions. /// search_option : IdentifierOption, optional, defaults to IdentifierOption.Name @@ -551,25 +551,33 @@ macro_rules! impl_parameter { #[pyo3(text_signature = "(pure_records, binary_records, search_option)")] fn from_records( pure_records: Vec, - binary_records: &PyAny, + binary_records: Option<&PyAny>, search_option: Option, ) -> PyResult { let prs = pure_records.into_iter().map(|pr| pr.0).collect(); - let brs = if let Ok(br) = binary_records.extract::>() { - Ok(br.to_owned_array().mapv(|r| r.try_into().unwrap())) - } else if let Ok(br) = binary_records.extract::>() { - let brs: Vec<_> = br.into_iter().map(|br| br.0).collect(); - Ok(<$parameter>::binary_matrix_from_records( - &prs, - &brs, - search_option.unwrap_or(IdentifierOption::Name), - )) + if let Some(binary_records) = binary_records { + let brs = if let Ok(br) = binary_records.extract::>() { + Ok(br.to_owned_array().mapv(|r| r.try_into().unwrap())) + } else if let Ok(br) = binary_records.extract::>() { + let brs: Vec<_> = br.into_iter().map(|br| br.0).collect(); + Ok(<$parameter>::binary_matrix_from_records( + &prs, + &brs, + search_option.unwrap_or(IdentifierOption::Name), + )) + } else { + Err(PyErr::new::(format!( + "Could not parse binary input!" + ))) + }; + Ok(Self(Rc::new(<$parameter>::from_records(prs, brs.unwrap())))) } else { - Err(PyErr::new::(format!( - "Could not parse binary input!" - ))) - }; - Ok(Self(Rc::new(<$parameter>::from_records(prs, brs.unwrap())))) + let n = prs.len(); + Ok(Self(Rc::new(<$parameter>::from_records( + prs, + Array2::from_elem([n, n], <$parameter as Parameter>::Binary::default()), + )))) + } } /// Creates parameters for a pure component from a pure record. diff --git a/src/gc_pcsaft/dft/parameter.rs b/src/gc_pcsaft/dft/parameter.rs index c70f7f33a..0cd2c36d0 100644 --- a/src/gc_pcsaft/dft/parameter.rs +++ b/src/gc_pcsaft/dft/parameter.rs @@ -8,7 +8,6 @@ use indexmap::IndexMap; use ndarray::{Array1, Array2}; use petgraph::dot::{Config, Dot}; use petgraph::graph::{Graph, UnGraph}; -use std::fmt::Write; /// psi Parameter for heterosegmented DFT (Mairhofer2018) const PSI_GC_DFT: f64 = 1.5357; diff --git a/src/pcsaft/python.rs b/src/pcsaft/python.rs index 95a0619d9..a8eaaa640 100644 --- a/src/pcsaft/python.rs +++ b/src/pcsaft/python.rs @@ -8,6 +8,7 @@ use feos_core::parameter::{ use feos_core::python::joback::PyJobackRecord; use feos_core::python::parameter::*; use feos_core::*; +use ndarray::Array2; use numpy::{PyArray2, PyReadonlyArray2, ToPyArray}; use pyo3::exceptions::PyTypeError; use pyo3::prelude::*; From 1269db580a7fa9efa73efbf47df8cf713e95e573 Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Fri, 5 Aug 2022 11:52:54 +0200 Subject: [PATCH 2/3] updated changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e824157a8..a5eacf094 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Made binary parameters in `from_records` Python routine an `Option`. [#35](https://github.com/feos-org/feos/pull/35) +- Added panic with message when parsing missing Identifiers variants. [#35](https://github.com/feos-org/feos/pull/35) ## [0.2.1] - 2022-05-13 ### Fixed From a9ebd558ee3e67db8dffbc9919fb4790051faa4f Mon Sep 17 00:00:00 2001 From: Gernot Bauer Date: Mon, 8 Aug 2022 12:16:11 +0200 Subject: [PATCH 3/3] moved changelog entries to core crate --- CHANGELOG.md | 3 --- feos-core/CHANGELOG.md | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5eacf094..e824157a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -### Changed -- Made binary parameters in `from_records` Python routine an `Option`. [#35](https://github.com/feos-org/feos/pull/35) -- Added panic with message when parsing missing Identifiers variants. [#35](https://github.com/feos-org/feos/pull/35) ## [0.2.1] - 2022-05-13 ### Fixed diff --git a/feos-core/CHANGELOG.md b/feos-core/CHANGELOG.md index 33c06cafe..f84d3dab4 100644 --- a/feos-core/CHANGELOG.md +++ b/feos-core/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +### Changed +- Made binary parameters in `from_records` Python routine an `Option`. [#35](https://github.com/feos-org/feos/pull/35) +- Added panic with message when parsing missing Identifiers variants. [#35](https://github.com/feos-org/feos/pull/35) + ### Fixed - Avoid panicking when calculating `ResidualNpt` properties of states with negative pressures. [#42](https://github.com/feos-org/feos/pull/42)