use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use crate::eos::{
ChaoSeaderSpecies, CubicEos, PhaseId, RegularSolutionSet,
regular_solution_ln_nu as regular_solution_ln_nu_rs,
};
use crate::refinery::{
RefineryError, lee_kesler_reduced as lee_kesler_reduced_rs,
peneloux_shift as peneloux_shift_rs, refinery_error_is_input,
};
use crate::types::Component;
fn refinery_err(e: RefineryError) -> PyErr {
if refinery_error_is_input(&e) {
PyValueError::new_err(e.to_string())
} else {
PyRuntimeError::new_err(e.to_string())
}
}
fn parse_phase(s: &str) -> PyResult<PhaseId> {
match s.to_ascii_lowercase().as_str() {
"vapor" | "v" | "gas" => Ok(PhaseId::Vapor),
"liquid" | "l" => Ok(PhaseId::Liquid),
other => Err(PyValueError::new_err(format!(
"phase must be 'vapor' or 'liquid' (got {other:?})"
))),
}
}
#[pyfunction]
pub fn refinery_lee_kesler_reduced(
tr: f64,
pr: f64,
omega: f64,
phase: &str,
) -> PyResult<(f64, f64, f64, f64)> {
let d = lee_kesler_reduced_rs(tr, pr, omega, parse_phase(phase)?).map_err(refinery_err)?;
Ok((d.z, d.h_dep_rt, d.s_dep_r, d.ln_phi))
}
#[pyfunction]
#[pyo3(signature = (t, p, tc, pc, omega, set=RegularSolutionSet::GraysonStreed1963,
species=ChaoSeaderSpecies::Normal))]
pub fn regular_solution_ln_nu(
t: f64,
p: f64,
tc: f64,
pc: f64,
omega: f64,
set: RegularSolutionSet,
species: ChaoSeaderSpecies,
) -> f64 {
let comp = Component {
tc,
pc,
omega,
..Component::default()
};
regular_solution_ln_nu_rs(set, t, p, &comp, species)
}
#[pyfunction]
#[pyo3(signature = (eos, tc, pc, omega, zra=0.0))]
pub fn refinery_peneloux_shift(
eos: CubicEos,
tc: f64,
pc: f64,
omega: f64,
zra: f64,
) -> PyResult<f64> {
let comp = Component {
tc,
pc,
omega,
zra,
..Component::default()
};
peneloux_shift_rs(eos, &comp).map_err(refinery_err)
}
pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<RegularSolutionSet>()?;
m.add_function(wrap_pyfunction!(refinery_lee_kesler_reduced, m)?)?;
m.add_function(wrap_pyfunction!(regular_solution_ln_nu, m)?)?;
m.add_function(wrap_pyfunction!(refinery_peneloux_shift, m)?)?;
Ok(())
}