Skip to content

Commit

Permalink
Add function to compute mole fractions from mass and vice-versa (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
tulioricci authored May 15, 2024
1 parent 5bb7d3b commit 20f9b1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 15 additions & 0 deletions mirgecom/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
18 changes: 16 additions & 2 deletions test/test_eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 20f9b1f

Please sign in to comment.