Skip to main content

feos_core/state/
statevec.rs

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