feos_core/state/
statevec.rs

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