feos_core/state/
statevec.rs

1#[cfg(feature = "ndarray")]
2use super::Contributions;
3use super::State;
4#[cfg(feature = "ndarray")]
5use crate::equation_of_state::{Molarweight, Residual, Total};
6#[cfg(feature = "ndarray")]
7use ndarray::{Array1, Array2};
8
9#[cfg(feature = "ndarray")]
10use quantity::{
11    Density, MassDensity, MolarEnergy, MolarEntropy, Moles, Pressure, SpecificEnergy,
12    SpecificEntropy, Temperature,
13};
14use std::iter::FromIterator;
15use std::ops::Deref;
16
17/// A list of states for a simple access to properties
18/// of multiple states.
19pub struct StateVec<'a, E>(pub Vec<&'a State<E>>);
20
21impl<'a, E> FromIterator<&'a State<E>> for StateVec<'a, E> {
22    fn from_iter<I: IntoIterator<Item = &'a State<E>>>(iter: I) -> Self {
23        Self(iter.into_iter().collect())
24    }
25}
26
27impl<'a, E> IntoIterator for StateVec<'a, E> {
28    type Item = &'a State<E>;
29    type IntoIter = std::vec::IntoIter<Self::Item>;
30
31    fn into_iter(self) -> Self::IntoIter {
32        self.0.into_iter()
33    }
34}
35
36impl<'a, E> Deref for StateVec<'a, E> {
37    type Target = Vec<&'a State<E>>;
38
39    fn deref(&self) -> &Self::Target {
40        &self.0
41    }
42}
43
44#[cfg(feature = "ndarray")]
45impl<E: Residual> StateVec<'_, E> {
46    pub fn temperature(&self) -> Temperature<Array1<f64>> {
47        Temperature::from_shape_fn(self.0.len(), |i| self.0[i].temperature)
48    }
49
50    pub fn pressure(&self) -> Pressure<Array1<f64>> {
51        Pressure::from_shape_fn(self.0.len(), |i| self.0[i].pressure(Contributions::Total))
52    }
53
54    pub fn compressibility(&self) -> Array1<f64> {
55        Array1::from_shape_fn(self.0.len(), |i| {
56            self.0[i].compressibility(Contributions::Total)
57        })
58    }
59
60    pub fn density(&self) -> Density<Array1<f64>> {
61        Density::from_shape_fn(self.0.len(), |i| self.0[i].density)
62    }
63
64    pub fn moles(&self) -> Moles<Array2<f64>> {
65        Moles::from_shape_fn((self.0.len(), self.0[0].eos.components()), |(i, j)| {
66            self.0[i].moles.get(j)
67        })
68    }
69
70    pub fn molefracs(&self) -> Array2<f64> {
71        Array2::from_shape_fn((self.0.len(), self.0[0].eos.components()), |(i, j)| {
72            self.0[i].molefracs[j]
73        })
74    }
75}
76
77#[cfg(feature = "ndarray")]
78impl<E: Residual + Molarweight> StateVec<'_, E> {
79    pub fn mass_density(&self) -> MassDensity<Array1<f64>> {
80        MassDensity::from_shape_fn(self.0.len(), |i| self.0[i].mass_density())
81    }
82
83    pub fn massfracs(&self) -> Array2<f64> {
84        Array2::from_shape_fn((self.0.len(), self.0[0].eos.components()), |(i, j)| {
85            self.0[i].massfracs()[j]
86        })
87    }
88}
89
90#[cfg(feature = "ndarray")]
91impl<E: Total> StateVec<'_, E> {
92    pub fn molar_enthalpy(&self, contributions: Contributions) -> MolarEnergy<Array1<f64>> {
93        MolarEnergy::from_shape_fn(self.0.len(), |i| self.0[i].molar_enthalpy(contributions))
94    }
95
96    pub fn molar_entropy(&self, contributions: Contributions) -> MolarEntropy<Array1<f64>> {
97        MolarEntropy::from_shape_fn(self.0.len(), |i| self.0[i].molar_entropy(contributions))
98    }
99}
100
101#[cfg(feature = "ndarray")]
102impl<E: Total + Molarweight> StateVec<'_, E> {
103    pub fn specific_enthalpy(&self, contributions: Contributions) -> SpecificEnergy<Array1<f64>> {
104        SpecificEnergy::from_shape_fn(self.0.len(), |i| self.0[i].specific_enthalpy(contributions))
105    }
106
107    pub fn specific_entropy(&self, contributions: Contributions) -> SpecificEntropy<Array1<f64>> {
108        SpecificEntropy::from_shape_fn(self.0.len(), |i| self.0[i].specific_entropy(contributions))
109    }
110}