Another gasmixture refactor to make the code less fortrany

This commit is contained in:
Alex Selimov 2026-04-19 06:46:35 -04:00
parent 9e156bc8e3
commit 0910872a55

View file

@ -9,6 +9,8 @@ use crate::{
pub struct MixtureComponent { pub struct MixtureComponent {
//kg-moles component/kg_mixture //kg-moles component/kg_mixture
n: f64, n: f64,
// Coefficients
a: Vec<f64>,
s: SpeciesThermoData, s: SpeciesThermoData,
} }
@ -16,7 +18,6 @@ pub struct GasMixture {
pub(crate) nsum: f64, pub(crate) nsum: f64,
pub(crate) gasses: Vec<MixtureComponent>, pub(crate) gasses: Vec<MixtureComponent>,
pub(crate) condensed: Vec<MixtureComponent>, pub(crate) condensed: Vec<MixtureComponent>,
pub(crate) coeffs: Matrix<f64>,
pub(crate) elements: HashMap<String, usize>, pub(crate) elements: HashMap<String, usize>,
pub(crate) binitial: Vec<f64>, pub(crate) binitial: Vec<f64>,
} }
@ -46,48 +47,30 @@ impl GasMixture {
} }
} }
// Now build the coefficients
let mut coeffs = Matrix::new(ei, species.len(), 0.0);
for (j, s) in species.iter().enumerate() {
s.elements.iter().for_each(|e| {
// Safe to unwrap because elements should always have e
let i = *elements.get(&e.element).unwrap();
coeffs.set(i, j, e.count);
});
}
let binitial = get_b_current(&elements, species, &ns); let binitial = get_b_current(&elements, species, &ns);
// Now separate SpeciesThermoData in gas and condensed MixtureComponents // Now separate SpeciesThermoData in gas and condensed MixtureComponents
let gasses = ns let (gasses, condensed) = ns
.iter() .iter()
.zip(species.iter()) .zip(species.iter())
.filter_map(|(n, s)| match s.phase { .map(|(n, s)| {
Phase::Gas => Some(MixtureComponent { let mut a = vec![0.0; elements.len()];
s.elements.iter().for_each(|e| {
let i = elements.get(&e.element).unwrap();
a[*i] += e.count;
});
MixtureComponent {
n: *n, n: *n,
a,
s: s.clone(), s: s.clone(),
}), }
Phase::Condensed => None,
}) })
.collect(); .partition(|c| matches!(c.s.phase, Phase::Gas));
let condensed = ns
.iter()
.zip(species.iter())
.filter_map(|(n, s)| match s.phase {
Phase::Gas => None,
Phase::Condensed => Some(MixtureComponent {
n: *n,
s: s.clone(),
}),
})
.collect();
GasMixture { GasMixture {
nsum, nsum,
gasses, gasses,
condensed, condensed,
coeffs,
elements, elements,
binitial, binitial,
} }
@ -199,12 +182,12 @@ mod test {
assert_vec_delta!(expected_ns, ns, 1e-12); assert_vec_delta!(expected_ns, ns, 1e-12);
assert_delta!(gas.nsum, 0.04997527057027948, 1e-12); assert_delta!(gas.nsum, 0.04997527057027948, 1e-12);
assert_delta!(gas.coeffs.get(0, 0).unwrap(), 2.0, 1e-12); assert_delta!(gas.gasses[0].a[0], 2.0, 1e-12);
assert_delta!(gas.coeffs.get(0, 1).unwrap(), 0.0, 1e-12); assert_delta!(gas.gasses[1].a[0], 0.0, 1e-12);
assert_delta!(gas.coeffs.get(0, 2).unwrap(), 2.0, 1e-12); assert_delta!(gas.gasses[2].a[0], 2.0, 1e-12);
assert_delta!(gas.coeffs.get(1, 0).unwrap(), 0.0, 1e-12); assert_delta!(gas.gasses[0].a[1], 0.0, 1e-12);
assert_delta!(gas.coeffs.get(1, 1).unwrap(), 2.0, 1e-12); assert_delta!(gas.gasses[1].a[1], 2.0, 1e-12);
assert_delta!(gas.coeffs.get(1, 2).unwrap(), 1.0, 1e-12); assert_delta!(gas.gasses[2].a[1], 1.0, 1e-12);
let expected_b = [0.06663369409370597, 0.05830448233199272]; let expected_b = [0.06663369409370597, 0.05830448233199272];
assert_vec_delta!(gas.binitial, expected_b, 1e-12); assert_vec_delta!(gas.binitial, expected_b, 1e-12);