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) 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::*;