From dfdd12db1faf5f0de44747e4a0074c210303fd7c Mon Sep 17 00:00:00 2001 From: Philipp Rehner Date: Tue, 11 Jul 2023 11:26:01 +0200 Subject: [PATCH] Serialization and documentation of binary association records --- src/association/mod.rs | 12 ++++++++++++ src/pcsaft/parameters.rs | 18 ++++++++++++++---- src/pcsaft/python.rs | 28 +++------------------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/association/mod.rs b/src/association/mod.rs index 86b0ffeba..06da46f9f 100644 --- a/src/association/mod.rs +++ b/src/association/mod.rs @@ -93,13 +93,25 @@ impl fmt::Display for AssociationRecord { } } +/// Binary association parameters. #[derive(Serialize, Deserialize, Clone, Copy)] pub struct BinaryAssociationRecord { + /// Cross-association association volume parameter. + #[serde(skip_serializing_if = "Option::is_none")] pub kappa_ab: Option, + /// Cross-association energy parameter. + #[serde(skip_serializing_if = "Option::is_none")] pub epsilon_k_ab: Option, + /// Indices of sites that the record refers to. + #[serde(skip_serializing_if = "is_default_site_indices")] + #[serde(default)] pub site_indices: [usize; 2], } +fn is_default_site_indices([i, j]: &[usize; 2]) -> bool { + *i == 0 && *j == 0 +} + impl BinaryAssociationRecord { pub fn new( kappa_ab: Option, diff --git a/src/pcsaft/parameters.rs b/src/pcsaft/parameters.rs index 2738b61ef..fd6a98ad5 100644 --- a/src/pcsaft/parameters.rs +++ b/src/pcsaft/parameters.rs @@ -6,6 +6,7 @@ use feos_core::parameter::{ }; use ndarray::{Array, Array1, Array2}; use num_dual::DualNum; +use num_traits::Zero; use quantity::si::{JOULE, KB, KELVIN}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -265,9 +266,15 @@ impl PcSaftRecord { } } +/// PC-SAFT binary interaction parameters. #[derive(Serialize, Deserialize, Clone, Default)] pub struct PcSaftBinaryRecord { + /// Binary dispersion interaction parameter + #[serde(skip_serializing_if = "f64::is_zero")] + #[serde(default)] pub k_ij: f64, + /// Binary association parameters + #[serde(flatten)] association: Option, } @@ -313,16 +320,19 @@ impl> FromSegmentsBinary for PcSaftBinaryRecord { impl std::fmt::Display for PcSaftBinaryRecord { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "PcSaftBinaryRecord(k_ij={}", self.k_ij)?; + let mut tokens = vec![]; + if !self.k_ij.is_zero() { + tokens.push(format!("k_ij={}", self.k_ij)); + } if let Some(association) = self.association { if let Some(kappa_ab) = association.kappa_ab { - write!(f, ", kappa_ab={}", kappa_ab)?; + tokens.push(format!("kappa_ab={}", kappa_ab)); } if let Some(epsilon_k_ab) = association.epsilon_k_ab { - write!(f, ", epsilon_k_ab={}", epsilon_k_ab)?; + tokens.push(format!("epsilon_k_ab={}", epsilon_k_ab)); } } - write!(f, ")") + write!(f, "PcSaftBinaryRecord({})", tokens.join(", ")) } } diff --git a/src/pcsaft/python.rs b/src/pcsaft/python.rs index 90caa218d..089f215d3 100644 --- a/src/pcsaft/python.rs +++ b/src/pcsaft/python.rs @@ -12,7 +12,7 @@ use pyo3::prelude::*; use std::convert::{TryFrom, TryInto}; use std::sync::Arc; -/// Create a set of PC-Saft parameters from records. +/// Create a new set of PC-SAFT pure component parameters. #[pyclass(name = "PcSaftRecord")] #[pyo3( text_signature = "(m, sigma, epsilon_k, mu=None, q=None, kappa_ab=None, epsilon_k_ab=None, na=None, nb=None, viscosity=None, diffusion=None, thermal_conductivity=None)" @@ -131,9 +131,6 @@ impl_pure_record!(PcSaftRecord, PyPcSaftRecord); impl_segment_record!(PcSaftRecord, PyPcSaftRecord); #[pyclass(name = "PcSaftBinaryRecord")] -#[pyo3( - text_signature = "(pure_records, binary_records=None, substances=None, search_option='Name')" -)] #[derive(Clone)] pub struct PyPcSaftBinaryRecord(PcSaftBinaryRecord); @@ -149,30 +146,11 @@ impl PyPcSaftBinaryRecord { } } +impl_json_handling!(PyPcSaftBinaryRecord); + impl_binary_record!(PcSaftBinaryRecord, PyPcSaftBinaryRecord); -/// Create a set of PC-SAFT parameters from records. -/// -/// Parameters -/// ---------- -/// pure_records : List[PureRecord] -/// pure substance records. -/// binary_records : List[BinaryRecord], optional -/// binary saft parameter records -/// substances : List[str], optional -/// The substances to use. Filters substances from `pure_records` according to -/// `search_option`. -/// When not provided, all entries of `pure_records` are used. -/// search_option : {'Name', 'Cas', 'Inchi', 'IupacName', 'Formula', 'Smiles'}, optional, defaults to 'Name'. -/// Identifier that is used to search substance. -/// -/// Returns -/// ------- -/// PcSaftParameters #[pyclass(name = "PcSaftParameters")] -#[pyo3( - text_signature = "(pure_records, binary_records=None, substances=None, search_option='Name')" -)] #[derive(Clone)] pub struct PyPcSaftParameters(pub Arc);