use crate::parameters::*;
use feos_core::joback::JobackRecord;
use feos_core::parameter::*;
use feos_core::python::joback::PyJobackRecord;
use feos_core::python::parameter::*;
use feos_core::{impl_json_handling, impl_parameter, impl_pure_record};
use ndarray::Array2;
use numpy::{PyArray2, ToPyArray};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use std::convert::TryFrom;
use std::rc::Rc;
#[pyclass(name = "PetsRecord", unsendable)]
#[pyo3(
text_signature = "(sigma, epsilon_k, viscosity=None, diffusion=None, thermal_conductivity=None)"
)]
#[derive(Clone)]
pub struct PyPetsRecord(PetsRecord);
#[pymethods]
impl PyPetsRecord {
#[new]
fn new(
sigma: f64,
epsilon_k: f64,
viscosity: Option<[f64; 4]>,
diffusion: Option<[f64; 5]>,
thermal_conductivity: Option<[f64; 4]>,
) -> Self {
Self(PetsRecord::new(
sigma,
epsilon_k,
viscosity,
diffusion,
thermal_conductivity,
))
}
#[getter]
fn get_sigma(&self) -> f64 {
self.0.sigma
}
#[getter]
fn get_epsilon_k(&self) -> f64 {
self.0.epsilon_k
}
#[getter]
fn get_viscosity(&self) -> Option<[f64; 4]> {
self.0.viscosity
}
#[getter]
fn get_diffusion(&self) -> Option<[f64; 5]> {
self.0.diffusion
}
#[getter]
fn get_thermal_conductivity(&self) -> Option<[f64; 4]> {
self.0.thermal_conductivity
}
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
}
impl_json_handling!(PyPetsRecord);
impl_pure_record!(PetsRecord, PyPetsRecord, JobackRecord, PyJobackRecord);
#[pyclass(name = "PetsParameters", unsendable)]
#[pyo3(
text_signature = "(pure_records, binary_records=None, substances=None, search_option='Name')"
)]
#[derive(Clone)]
pub struct PyPetsParameters(pub Rc<PetsParameters>);
#[pymethods]
impl PyPetsParameters {
#[pyo3(
text_signature = "(sigma, epsilon_k, k_ij=None, molarweight=None, viscosity=None, diffusion=None, thermal_conductivity=None)"
)]
#[staticmethod]
fn from_lists(
sigma: Vec<f64>,
epsilon_k: Vec<f64>,
k_ij: Option<&PyArray2<f64>>,
molarweight: Option<Vec<f64>>,
viscosity: Option<Vec<[f64; 4]>>,
diffusion: Option<Vec<[f64; 5]>>,
thermal_conductivity: Option<Vec<[f64; 4]>>,
) -> PyResult<Self> {
let n = sigma.len();
let input_length = vec![
Some(sigma.len()),
Some(epsilon_k.len()),
k_ij.as_ref().map_or(None, |v| Some(v.shape()[0])),
k_ij.as_ref().map_or(None, |v| Some(v.shape()[1])),
molarweight.as_ref().map_or(None, |v| Some(v.len())),
viscosity.as_ref().map_or(None, |v| Some(v.len())),
diffusion.as_ref().map_or(None, |v| Some(v.len())),
thermal_conductivity
.as_ref()
.map_or(None, |v| Some(v.len())),
]
.iter()
.filter_map(|&v| v)
.all(|v| v == n);
if !input_length {
return Err(PyValueError::new_err(
"shape of arguments could not be used together.",
));
}
let pure_records = (0..n)
.map(|i| {
let identifier =
Identifier::new(format!("{}", i).as_str(), None, None, None, None, None);
let model_record = PetsRecord::new(
sigma[i],
epsilon_k[i],
viscosity.as_ref().map_or(None, |v| Some(v[i])),
diffusion.as_ref().map_or(None, |v| Some(v[i])),
thermal_conductivity.as_ref().map_or(None, |v| Some(v[i])),
);
PureRecord::new(
identifier,
molarweight.as_ref().map_or(1.0, |v| v[i]),
model_record,
None,
)
})
.collect();
let binary = match k_ij {
Some(v) => v.to_owned_array().mapv(f64::into),
None => Array2::from_shape_fn((n, n), |(_, _)| PetsBinaryRecord::from(0.0)),
};
Ok(Self(Rc::new(PetsParameters::from_records(
pure_records,
binary,
))))
}
#[pyo3(
text_signature = "(sigma, epsilon_k, molarweight=None, viscosity=None, diffusion=None, thermal_conductivity=None)"
)]
#[staticmethod]
fn from_values(
sigma: f64,
epsilon_k: f64,
molarweight: Option<f64>,
viscosity: Option<[f64; 4]>,
diffusion: Option<[f64; 5]>,
thermal_conductivity: Option<[f64; 4]>,
) -> Self {
let pure_record = PureRecord::new(
Identifier::new(format!("{}", 1).as_str(), None, None, None, None, None),
molarweight.map_or(1.0, |v| v),
PetsRecord::new(
sigma,
epsilon_k,
viscosity.map_or(None, |v| Some(v)),
diffusion.map_or(None, |v| Some(v)),
thermal_conductivity.map_or(None, |v| Some(v)),
),
None,
);
Self(Rc::new(PetsParameters::new_pure(pure_record)))
}
#[getter]
fn get_pure_records(&self) -> Vec<PyPureRecord> {
self.0
.pure_records
.iter()
.map(|r| PyPureRecord(r.clone()))
.collect()
}
#[getter]
fn get_k_ij<'py>(&self, py: Python<'py>) -> &'py PyArray2<f64> {
self.0.k_ij.view().to_pyarray(py)
}
fn _repr_markdown_(&self) -> String {
self.0.to_markdown()
}
fn __repr__(&self) -> PyResult<String> {
Ok(self.0.to_string())
}
}
impl_parameter!(PetsParameters, PyPetsParameters);