From 20f9b1f39735cf9aa8b15787b27cd19094b162d4 Mon Sep 17 00:00:00 2001 From: tulioricci Date: Wed, 15 May 2024 11:06:56 -0500 Subject: [PATCH] Add function to compute mole fractions from mass and vice-versa (#1041) --- mirgecom/eos.py | 15 +++++++++++++++ test/test_eos.py | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mirgecom/eos.py b/mirgecom/eos.py index e9f447c93..03e2ba81b 100644 --- a/mirgecom/eos.py +++ b/mirgecom/eos.py @@ -572,6 +572,8 @@ class PyrometheusMixture(MixtureEOS): .. automethod:: species_enthalpies .. automethod:: get_species_source_terms .. automethod:: get_temperature_seed + .. automethod:: mole_fractions_from_mass_fractions + .. automethod:: mass_fractions_from_mole_fractions """ def __init__(self, pyrometheus_mech, temperature_guess=300.0): @@ -870,3 +872,16 @@ def get_species_source_terms(self, cv: ConservedVars, temperature: DOFArray): return make_conserved(dim, rho_source, energy_source, mom_source, species_sources) + + def mole_fractions_from_mass_fractions(self, species_mass_fractions): + """Get mole fractions from mass fractions.""" + mix_mol_weight = \ + self._pyrometheus_mech.get_mix_molecular_weight(species_mass_fractions) + return self._pyrometheus_mech.get_mole_fractions(mix_mol_weight, + species_mass_fractions) + + def mass_fractions_from_mole_fractions(self, species_mole_fractions): + """Get mass fractions from mole fractions.""" + mol_weights = self.get_species_molecular_weights() + mmw = sum(species_mole_fractions*mol_weights) + return species_mole_fractions*mol_weights/mmw diff --git a/test/test_eos.py b/test/test_eos.py index a4b7867e5..d3d48092d 100644 --- a/test/test_eos.py +++ b/test/test_eos.py @@ -100,13 +100,16 @@ def inf_norm(x): cantera_soln.TPX = tempin, pressure, x can_t, can_rho, can_y = cantera_soln.TDY + can_x = cantera_soln.X can_p = cantera_soln.P pin = can_p * ones tin = can_t * ones - yin = can_y * ones + xin = can_x * ones rhoin = can_rho * ones + yin = eos.mass_fractions_from_mole_fractions(xin) + # First, check density mass = eos.get_density(pin, tin, yin) abs_err_m = inf_norm(mass - can_rho) @@ -173,13 +176,24 @@ def inf_norm(x): cantera_soln.TPX = tempin, pressure, x can_t, can_rho, can_y = cantera_soln.TDY + can_x = cantera_soln.X can_p = cantera_soln.P pin = can_p * ones tin = can_t * ones - yin = can_y * ones + xin = can_x * ones rhoin = can_rho * ones + yin = eos.mass_fractions_from_mole_fractions(xin) + for i in range(nspecies): + abs_err_y = inf_norm(yin[i] - can_y[i]) + assert abs_err_y < 1.0e-14 + + xin_p = eos.mole_fractions_from_mass_fractions(yin) + for i in range(nspecies): + abs_err_x = inf_norm(xin_p[i] - can_x[i]) + assert abs_err_x < 1.0e-14 + mass = eos.get_density(pin, tin, yin) abs_err_m = inf_norm(mass - can_rho) assert abs_err_m/can_rho < 1.0e-14