Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removed unit generics from feos-core python and fixed formats
  • Loading branch information
g-bauer committed Jan 20, 2023
commit 58778ae4a0f04e3ba50c09bdb02d1427909a1b58
7 changes: 5 additions & 2 deletions feos-core/src/density_iteration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::equation_of_state::EquationOfState;
use crate::errors::{EosError, EosResult};
use crate::state::State;
use crate::EosUnit;
use quantity::si::{SINumber, SIArray1, SIUnit};
use quantity::si::{SIArray1, SINumber, SIUnit};
use std::sync::Arc;

pub fn density_iteration<E: EquationOfState>(
Expand Down Expand Up @@ -129,7 +129,10 @@ pub fn density_iteration<E: EquationOfState>(
// Newton step
rho += delta_rho;
if error.to_reduced(SIUnit::reference_pressure())?.abs()
< f64::max(abstol, (rho * reltol).to_reduced(SIUnit::reference_density())?)
< f64::max(
abstol,
(rho * reltol).to_reduced(SIUnit::reference_density())?,
)
{
break 'iteration;
}
Expand Down
6 changes: 1 addition & 5 deletions feos-core/src/joback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ impl Joback {
}

/// Directly calculates the ideal gas heat capacity from the Joback model.
pub fn c_p(
&self,
temperature: SINumber,
molefracs: &Array1<f64>,
) -> EosResult<SINumber> {
pub fn c_p(&self, temperature: SINumber, molefracs: &Array1<f64>) -> EosResult<SINumber> {
let t = temperature.to_reduced(SIUnit::reference_temperature())?;
let mut c_p = 0.0;
for (j, &x) in self.records.iter().zip(molefracs.iter()) {
Expand Down
4 changes: 3 additions & 1 deletion feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ pub use errors::{EosError, EosResult};
pub use phase_equilibria::{
PhaseDiagram, PhaseDiagramHetero, PhaseEquilibrium, SolverOptions, Verbosity,
};
pub use state::{Contributions, DensityInitialization, State, StateBuilder, StateHD, StateVec, Derivative};
pub use state::{
Contributions, DensityInitialization, Derivative, State, StateBuilder, StateHD, StateVec,
};

#[cfg(feature = "python")]
pub mod python;
Expand Down
15 changes: 7 additions & 8 deletions feos-core/src/phase_equilibria/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::equation_of_state::EquationOfState;
use crate::errors::{EosError, EosResult};
use crate::state::{Contributions, DensityInitialization, State};
use crate::EosUnit;
use quantity::si::{SINumber, SIArray1, SIUnit};
use quantity::si::{SIArray1, SINumber, SIUnit};
use std::fmt;
use std::fmt::Write;
use std::sync::Arc;
Expand Down Expand Up @@ -261,20 +261,19 @@ impl<E: EquationOfState, const N: usize> PhaseEquilibrium<E, N> {
Ok(())
}

pub fn update_chemical_potential(
&mut self,
chemical_potential: &SIArray1,
) -> EosResult<()> {
pub fn update_chemical_potential(&mut self, chemical_potential: &SIArray1) -> EosResult<()> {
for s in self.0.iter_mut() {
s.update_chemical_potential(chemical_potential)?;
}
Ok(())
}

pub(super) fn total_gibbs_energy(&self) -> SINumber {
self.0.iter().fold(0.0 * SIUnit::reference_energy(), |acc, s| {
acc + s.gibbs_energy(Contributions::Total)
})
self.0
.iter()
.fold(0.0 * SIUnit::reference_energy(), |acc, s| {
acc + s.gibbs_energy(Contributions::Total)
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion feos-core/src/phase_equilibria/phase_diagram_pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use super::{PhaseEquilibrium, SolverOptions};
use crate::equation_of_state::EquationOfState;
use crate::errors::EosResult;
use crate::state::{State, StateVec};
use crate::EosUnit;
#[cfg(feature = "rayon")]
use ndarray::{Array1, ArrayView1, Axis};
use quantity::si::{SINumber, SIArray1};
use quantity::si::{SIArray1, SINumber, SIUnit};
#[cfg(feature = "rayon")]
use rayon::{prelude::*, ThreadPool};
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion feos-core/src/phase_equilibria/tp_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::errors::{EosError, EosResult};
use crate::state::{Contributions, DensityInitialization, State};
use ndarray::*;
use num_dual::linalg::norm;
use quantity::si::{SINumber, SIArray1};
use quantity::si::{SIArray1, SINumber};
use std::sync::Arc;

const MAX_ITER_TP: usize = 400;
Expand Down
8 changes: 4 additions & 4 deletions feos-core/src/python/phase_equilibria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! impl_phase_equilibrium {
/// A thermodynamic two phase equilibrium state.
#[pyclass(name = "PhaseEquilibrium")]
#[derive(Clone)]
pub struct PyPhaseEquilibrium(PhaseEquilibrium<SIUnit, $eos, 2>);
pub struct PyPhaseEquilibrium(PhaseEquilibrium<$eos, 2>);

#[pymethods]
impl PyPhaseEquilibrium {
Expand Down Expand Up @@ -330,7 +330,7 @@ macro_rules! impl_phase_equilibrium {
/// A thermodynamic three phase equilibrium state.
#[pyclass(name = "ThreePhaseEquilibrium")]
#[derive(Clone)]
struct PyThreePhaseEquilibrium(PhaseEquilibrium<SIUnit, $eos, 3>);
struct PyThreePhaseEquilibrium(PhaseEquilibrium<$eos, 3>);

#[pymethods]
impl PyPhaseEquilibrium {
Expand Down Expand Up @@ -473,7 +473,7 @@ macro_rules! impl_phase_equilibrium {
/// -------
/// PhaseDiagram : the resulting phase diagram
#[pyclass(name = "PhaseDiagram")]
pub struct PyPhaseDiagram(PhaseDiagram<SIUnit, $eos, 2>);
pub struct PyPhaseDiagram(PhaseDiagram<$eos, 2>);

#[pymethods]
impl PyPhaseDiagram {
Expand Down Expand Up @@ -921,7 +921,7 @@ macro_rules! impl_phase_equilibrium {

/// Phase diagram for a binary mixture exhibiting a heteroazeotrope.
#[pyclass(name = "PhaseDiagramHetero")]
pub struct PyPhaseDiagramHetero(PhaseDiagramHetero<SIUnit, $eos>);
pub struct PyPhaseDiagramHetero(PhaseDiagramHetero<$eos>);

#[pymethods]
impl PyPhaseDiagram {
Expand Down
10 changes: 5 additions & 5 deletions feos-core/src/python/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ macro_rules! impl_state {
#[pyclass(name = "State")]
#[derive(Clone)]
#[pyo3(text_signature = "(eos, temperature=None, volume=None, density=None, partial_density=None, total_moles=None, moles=None, molefracs=None, pressure=None, molar_enthalpy=None, molar_entropy=None, molar_internal_energy=None, density_initialization=None, initial_temperature=None)")]
pub struct PyState(pub State<SIUnit, $eos>);
pub struct PyState(pub State<$eos>);

#[pymethods]
impl PyState {
Expand Down Expand Up @@ -1022,15 +1022,15 @@ macro_rules! impl_state {
/// -------
/// StateVec
#[pyclass(name = "StateVec")]
pub struct PyStateVec(Vec<State<SIUnit, $eos>>);
pub struct PyStateVec(Vec<State<$eos>>);

impl From<StateVec<'_, SIUnit, $eos>> for PyStateVec {
fn from(vec: StateVec<SIUnit, $eos>) -> Self {
impl From<StateVec<'_, $eos>> for PyStateVec {
fn from(vec: StateVec<$eos>) -> Self {
Self(vec.into_iter().map(|s| s.clone()).collect())
}
}

impl<'a> From<&'a PyStateVec> for StateVec<'a, SIUnit, $eos> {
impl<'a> From<&'a PyStateVec> for StateVec<'a, $eos> {
fn from(vec: &'a PyStateVec) -> Self {
Self(vec.0.iter().collect())
}
Expand Down
2 changes: 1 addition & 1 deletion feos-core/src/python/user_defined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl PyEoSObj {
}
}

impl MolarWeight<SIUnit> for PyEoSObj {
impl MolarWeight for PyEoSObj {
fn molar_weight(&self) -> SIArray1 {
let gil = Python::acquire_gil();
let py = gil.python();
Expand Down
11 changes: 8 additions & 3 deletions feos-core/src/state/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ impl Cache {
derivative: Derivative,
f: F,
) -> f64 {
if let Some(&value) = self.map.get(&PartialDerivative::SecondMixed(derivative, derivative)) {
if let Some(&value) = self
.map
.get(&PartialDerivative::SecondMixed(derivative, derivative))
{
self.hit += 1;
value
} else {
Expand All @@ -64,8 +67,10 @@ impl Cache {
self.map.insert(PartialDerivative::Zeroth, value.re);
self.map
.insert(PartialDerivative::First(derivative), value.v1[0]);
self.map
.insert(PartialDerivative::SecondMixed(derivative, derivative), value.v2[0]);
self.map.insert(
PartialDerivative::SecondMixed(derivative, derivative),
value.v2[0],
);
value.v2[0]
}
}
Expand Down
6 changes: 4 additions & 2 deletions feos-core/src/state/critical_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ndarray::{arr1, arr2, Array1, Array2};
use num_dual::linalg::{norm, smallest_ev, LU};
use num_dual::{Dual, Dual3, Dual64, DualNum, DualVec64, HyperDual, StaticVec};
use num_traits::{One, Zero};
use quantity::si::{SINumber, SIArray1, SIUnit};
use quantity::si::{SIArray1, SINumber, SIUnit};
use std::convert::TryFrom;
use std::sync::Arc;

Expand Down Expand Up @@ -409,7 +409,9 @@ impl<E: EquationOfState> State<E> {
let mut rho = match density_initialization {
DensityInitialization::Vapor => 1e-5 * max_density,
DensityInitialization::Liquid => max_density,
DensityInitialization::InitialDensity(rho) => rho.to_reduced(SIUnit::reference_density())?,
DensityInitialization::InitialDensity(rho) => {
rho.to_reduced(SIUnit::reference_density())?
}
DensityInitialization::None => unreachable!(),
};
let n = moles.to_reduced(SIUnit::reference_moles())?;
Expand Down
14 changes: 5 additions & 9 deletions feos-core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ impl<E> Clone for State<E> {
fn clone(&self) -> Self {
Self {
eos: self.eos.clone(),
total_moles: self.total_moles.clone(),
temperature: self.temperature.clone(),
volume: self.volume.clone(),
total_moles: self.total_moles,
temperature: self.temperature,
volume: self.volume,
moles: self.moles.clone(),
partial_density: self.partial_density.clone(),
density: self.density.clone(),
density: self.density,
molefracs: self.molefracs.clone(),
reduced_temperature: self.reduced_temperature,
reduced_volume: self.reduced_volume,
Expand Down Expand Up @@ -746,11 +746,7 @@ fn is_close(x: SINumber, y: SINumber, atol: SINumber, rtol: f64) -> bool {
(x - y).abs() <= atol + rtol * y.abs()
}

fn newton<E: EquationOfState, F>(
mut x0: SINumber,
mut f: F,
atol: SINumber,
) -> EosResult<State<E>>
fn newton<E: EquationOfState, F>(mut x0: SINumber, mut f: F, atol: SINumber) -> EosResult<State<E>>
where
F: FnMut(SINumber) -> EosResult<(SINumber, SINumber, State<E>)>,
{
Expand Down