Skip to content

Commit

Permalink
[Test] Add tests for species molecular weight
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanwweber authored and speth committed Jun 24, 2022
1 parent a3cd673 commit 5536217
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
83 changes: 72 additions & 11 deletions interfaces/cython/cantera/test/test_thermo.py
Expand Up @@ -1234,18 +1234,73 @@ def test_standalone(self):
self.assertEqual(c['H'], 4)
self.assertNear(s.molecular_weight, 16.043)

def test_molecular_weight_with_unstable_element(self):
s = ct.Species("CPo", {"C": 1, "Po": 1})
with pytest.raises(ct.CanteraError, match="has no stable isotopes"):
s.molecular_weight

def test_molecular_weight_with_electron(self):
yaml = """
name: Li+[elyt]
composition: {Li: 1, E: -1}
thermo:
model: constant-cp
h0: -278.49 kJ/mol
s0: 13.4 J/mol/K
"""
s = ct.Species.from_yaml(yaml)
assert np.isclose(s.molecular_weight, 6.939451420091127)

def test_custom_element_weight_is_set_on_species(self):
gas = ct.Solution("ideal-gas.yaml", "element-override")
# Check that the molecular weight stored in the phase definition is the same
# as the one on the element
self.assertNear(gas["AR"].molecular_weights[0], gas.species("AR").molecular_weight)
# Check that the custom value is actually used
self.assertNear(gas.species("AR").molecular_weight, 36.0)

def test_species_can_be_added_to_phase(self):
s = ct.Species.from_dict({
"name": "AR2",
"composition": {"Ar": 2},
"thermo": {"model": "constant-cp", "h0": 100}
})
# Access the molecular weight to make sure it's been computed by the Species
self.assertNear(s.molecular_weight, 39.95 * 2)
# This should not cause an error because the existing Ar definition is the
# default molecular weight
self.gas.add_species(s)
self.assertNear(
self.gas["AR2"].molecular_weights[0],
self.gas.species("AR2").molecular_weight
)

def test_species_cannot_be_added_to_phase_custom_element(self):
s = ct.Species.from_dict({
"name": "AR2",
"composition": {"Ar": 2},
"thermo": {"model": "constant-cp", "h0": 100}
})
# Access the molecular weight to make sure it's been computed by the Species
self.assertNear(s.molecular_weight, 39.95 * 2)
gas = ct.Solution("ideal-gas.yaml", "element-override")
# The error here is because the weight of the Argon element has been changed in
# the phase definition, but the molecular weight of the species has already been
# computed
with pytest.raises(ct.CanteraError, match="Molecular weight.*changed"):
gas.add_species(s)

def test_species_cant_be_added_to_phase_custom_element(self):
s = ct.Species.from_dict({
"name": "AR2",
"composition": {"Ar": 2},
"thermo": {"model": "constant-cp", "h0": 100}
})
# DO NOT access the molecular weight to make sure it's not been computed by the Species
gas = ct.Solution("ideal-gas.yaml", "element-override")
gas.add_species(s)
self.assertNear(
gas["AR2"].molecular_weights[0],
gas.species("AR2").molecular_weight
)
self.assertNear(s.molecular_weight, gas.species("AR2").molecular_weight)

def test_defaults(self):
s = ct.Species('H2')
self.assertEqual(s.size, 1.0)
Expand Down Expand Up @@ -1639,13 +1694,6 @@ def test_case_sensitive_names(self):
with self.assertRaisesRegex(ct.CanteraError, 'Unknown species'):
gas.Y = 'h2:1.0, o2:1.0'

gas_cti = """ideal_gas(
name="gas",
elements=" S C Cs ",
species=" nasa: all ",
options=["skip_undeclared_elements"],
initial_state=state(temperature=300, pressure=(1, "bar"))
)"""
gas_yaml = """
phases:
- name: gas
Expand All @@ -1662,6 +1710,19 @@ def test_case_sensitive_names(self):
with self.assertRaises(ValueError):
gas.species_index('cs')

def test_unstable_element_in_phase(self):
gas_yaml = """
phases:
- name: gas
thermo: ideal-gas
elements: [Po]
species:
- name: Po
composition: {Po: 1}
"""
with pytest.raises(ct.CanteraError, match="has no stable isotopes"):
ct.Solution(yaml=gas_yaml)


class TestElement(utilities.CanteraTest):
@classmethod
Expand Down
4 changes: 3 additions & 1 deletion interfaces/cython/cantera/thermo.pyx
Expand Up @@ -2050,7 +2050,9 @@ cdef class PureFluid(ThermoPhase):
def __get__(self):
return self.s, self.v, self.Q


# TODO: Remove these helper methods when support for Python 3.8 is dropped. Python 3.9
# allows the classmethod and property decorators to be chained, so these can be
# implemented as properties in the Element class.
def _element_symbols():
syms = elementSymbols()
return tuple(pystr(s) for s in syms)
Expand Down

0 comments on commit 5536217

Please sign in to comment.