Skip to content
This repository was archived by the owner on Jun 14, 2022. It is now read-only.

Commit 9cd78e6

Browse files
authored
New struct StateVec and simpler interfaces for phase equilibria (#48)
* New struct `StateVec` and simpler interfaces for phase equilibria * fixes * move StateVec to state module and fix some documentation issues * add FromIterator implementation to StateVec
1 parent 2d4bee1 commit 9cd78e6

16 files changed

Lines changed: 644 additions & 1202 deletions

build_wheel/src/cubic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use feos_core::cubic::PengRobinson;
2-
use feos_core::python::cubic::PyPengRobinsonParameters;
2+
use feos_core::python::cubic::{PyPengRobinsonParameters, PyPengRobinsonRecord, PyPureRecord};
33
use feos_core::*;
44
use numpy::convert::ToPyArray;
55
use numpy::{PyArray1, PyArray2};
@@ -38,16 +38,16 @@ impl_virial_coefficients!(PyPengRobinson);
3838

3939
impl_state!(PengRobinson, PyPengRobinson);
4040
impl_state_molarweight!(PengRobinson, PyPengRobinson);
41-
impl_vle_state!(PengRobinson, PyPengRobinson);
41+
impl_phase_equilibrium!(PengRobinson, PyPengRobinson);
4242

4343
#[pymodule]
4444
pub fn cubic(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
4545
m.add_class::<PyPengRobinson>()?;
4646
m.add_class::<PyPengRobinsonParameters>()?;
47+
m.add_class::<PyPengRobinsonRecord>()?;
48+
m.add_class::<PyPureRecord>()?;
4749
m.add_class::<PyState>()?;
48-
m.add_class::<PyPhaseDiagramPure>()?;
49-
m.add_class::<PyPhaseDiagramBinary>()?;
50-
m.add_class::<PyPhaseDiagramHetero>()?;
50+
m.add_class::<PyPhaseDiagram>()?;
5151
m.add_class::<PyPhaseEquilibrium>()?;
5252
Ok(())
5353
}

build_wheel/src/user_defined.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use feos_core::python::user_defined::*;
21
use feos_core::python::statehd::*;
2+
use feos_core::python::user_defined::*;
33
use feos_core::*;
44
use numpy::convert::ToPyArray;
55
use numpy::{PyArray1, PyArray2};
@@ -10,7 +10,6 @@ use quantity::si::*;
1010
use std::collections::HashMap;
1111
use std::rc::Rc;
1212

13-
1413
/// Equation of state implemented as python class.
1514
///
1615
/// Parameters
@@ -43,7 +42,7 @@ impl_equation_of_state!(PyUserDefinedEos);
4342
impl_virial_coefficients!(PyUserDefinedEos);
4443
impl_state!(PyEoSObj, PyUserDefinedEos);
4544
impl_state_molarweight!(PyEoSObj, PyUserDefinedEos);
46-
impl_vle_state!(PyEoSObj, PyUserDefinedEos);
45+
impl_phase_equilibrium!(PyEoSObj, PyUserDefinedEos);
4746

4847
#[pymodule]
4948
pub fn user_defined(_py: Python, m: &PyModule) -> PyResult<()> {
@@ -61,9 +60,7 @@ pub fn user_defined(_py: Python, m: &PyModule) -> PyResult<()> {
6160
m.add_class::<PyUserDefinedEos>()?;
6261
m.add_class::<PyState>()?;
6362
m.add_class::<PyPhaseEquilibrium>()?;
64-
m.add_class::<PyPhaseDiagramPure>()?;
65-
m.add_class::<PyPhaseDiagramBinary>()?;
66-
m.add_class::<PyPhaseDiagramHetero>()?;
63+
m.add_class::<PyPhaseDiagram>()?;
6764
m.add_class::<PyPhaseEquilibrium>()?;
6865
Ok(())
6966
}

example/user_defined_eos.ipynb

Lines changed: 74 additions & 74 deletions
Large diffs are not rendered by default.

src/errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ pub enum EosError {
1919
#[error("Undetermined state: {0}.")]
2020
UndeterminedState(String),
2121
#[error("System is supercritical.")]
22-
SuperCritical(),
22+
SuperCritical,
2323
#[error("No phase split according to stability analysis.")]
2424
NoPhaseSplit,
25+
#[error("Wrong input units. Expected {0}, got {1}")]
26+
WrongUnits(String, String),
2527
#[error(transparent)]
2628
QuantityError(#[from] QuantityError),
2729
#[error(transparent)]

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use quantity::si::*;
77
use quantity::*;
88

9+
/// Print messages with level `Verbosity::Iter` or higher.
910
#[macro_export]
1011
macro_rules! log_iter {
1112
($verbosity:expr, $($arg:tt)*) => {
@@ -15,6 +16,7 @@ macro_rules! log_iter {
1516
}
1617
}
1718

19+
/// Print messages with level `Verbosity::Result` or higher.
1820
#[macro_export]
1921
macro_rules! log_result {
2022
($verbosity:expr, $($arg:tt)*) => {
@@ -38,10 +40,9 @@ pub use equation_of_state::{
3840
};
3941
pub use errors::{EosError, EosResult};
4042
pub use phase_equilibria::{
41-
PhaseDiagramBinary, PhaseDiagramHetero, PhaseDiagramPure, PhaseEquilibrium, SolverOptions,
42-
Verbosity,
43+
PhaseDiagram, PhaseDiagramHetero, PhaseEquilibrium, SolverOptions, Verbosity,
4344
};
44-
pub use state::{Contributions, DensityInitialization, State, StateBuilder, StateHD};
45+
pub use state::{Contributions, DensityInitialization, State, StateBuilder, StateHD, StateVec};
4546

4647
#[cfg(feature = "python")]
4748
pub mod python;

src/phase_equilibria/bubble_dew.rs

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{equation_of_state::EquationOfState, EosUnit};
99
use ndarray::*;
1010
use num_dual::linalg::{norm, LU};
1111
use quantity::{QuantityArray1, QuantityScalar};
12+
use std::convert::TryFrom;
1213
use std::rc::Rc;
1314

1415
const MAX_ITER_INNER: usize = 5;
@@ -68,36 +69,12 @@ where
6869
/// # Bubble and dew point calculations
6970
impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
7071
/// Calculate a phase equilibrium for a given temperature
71-
/// and composition of the liquid phase.
72-
pub fn bubble_point_tx(
72+
/// or pressure and composition of the liquid phase.
73+
pub fn bubble_point(
7374
eos: &Rc<E>,
74-
temperature: QuantityScalar<U>,
75-
pressure: Option<QuantityScalar<U>>,
76-
liquid_molefracs: &Array1<f64>,
77-
vapor_molefracs: Option<&Array1<f64>>,
78-
options: (SolverOptions, SolverOptions),
79-
) -> EosResult<Self>
80-
where
81-
QuantityScalar<U>: std::fmt::Display,
82-
{
83-
Self::bubble_dew_point_with_options(
84-
eos,
85-
TPSpec::Temperature(temperature),
86-
pressure,
87-
liquid_molefracs,
88-
vapor_molefracs,
89-
true,
90-
options,
91-
)
92-
}
93-
94-
/// Calculate a phase equilibrium for a given pressure
95-
/// and composition of the liquid phase.
96-
pub fn bubble_point_px(
97-
eos: &Rc<E>,
98-
pressure: QuantityScalar<U>,
99-
temperature: Option<QuantityScalar<U>>,
75+
temperature_or_pressure: QuantityScalar<U>,
10076
liquid_molefracs: &Array1<f64>,
77+
tp_init: Option<QuantityScalar<U>>,
10178
vapor_molefracs: Option<&Array1<f64>>,
10279
options: (SolverOptions, SolverOptions),
10380
) -> EosResult<Self>
@@ -106,8 +83,8 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
10683
{
10784
Self::bubble_dew_point_with_options(
10885
eos,
109-
TPSpec::Pressure(pressure),
110-
temperature,
86+
TPSpec::try_from(temperature_or_pressure)?,
87+
tp_init,
11188
liquid_molefracs,
11289
vapor_molefracs,
11390
true,
@@ -116,36 +93,12 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
11693
}
11794

11895
/// Calculate a phase equilibrium for a given temperature
119-
/// and composition of the vapor phase.
120-
pub fn dew_point_tx(
96+
/// or pressure and composition of the vapor phase.
97+
pub fn dew_point(
12198
eos: &Rc<E>,
122-
temperature: QuantityScalar<U>,
123-
pressure: Option<QuantityScalar<U>>,
124-
vapor_molefracs: &Array1<f64>,
125-
liquid_molefracs: Option<&Array1<f64>>,
126-
options: (SolverOptions, SolverOptions),
127-
) -> EosResult<Self>
128-
where
129-
QuantityScalar<U>: std::fmt::Display,
130-
{
131-
Self::bubble_dew_point_with_options(
132-
eos,
133-
TPSpec::Temperature(temperature),
134-
pressure,
135-
vapor_molefracs,
136-
liquid_molefracs,
137-
false,
138-
options,
139-
)
140-
}
141-
142-
/// Calculate a phase equilibrium for a given pressure
143-
/// and composition of the vapor phase.
144-
pub fn dew_point_px(
145-
eos: &Rc<E>,
146-
pressure: QuantityScalar<U>,
147-
temperature: Option<QuantityScalar<U>>,
99+
temperature_or_pressure: QuantityScalar<U>,
148100
vapor_molefracs: &Array1<f64>,
101+
tp_init: Option<QuantityScalar<U>>,
149102
liquid_molefracs: Option<&Array1<f64>>,
150103
options: (SolverOptions, SolverOptions),
151104
) -> EosResult<Self>
@@ -154,8 +107,8 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
154107
{
155108
Self::bubble_dew_point_with_options(
156109
eos,
157-
TPSpec::Pressure(pressure),
158-
temperature,
110+
TPSpec::try_from(temperature_or_pressure)?,
111+
tp_init,
159112
vapor_molefracs,
160113
liquid_molefracs,
161114
false,

src/phase_equilibria/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ mod phase_diagram_pure;
1313
mod stability_analysis;
1414
mod tp_flash;
1515
mod vle_pure;
16-
pub use phase_diagram_binary::{PhaseDiagramBinary, PhaseDiagramHetero};
17-
pub use phase_diagram_pure::PhaseDiagramPure;
16+
pub use phase_diagram_binary::PhaseDiagramHetero;
17+
pub use phase_diagram_pure::PhaseDiagram;
1818

1919
/// Level of detail in the iteration output.
2020
#[derive(Copy, Clone, PartialOrd, PartialEq)]

0 commit comments

Comments
 (0)