Skip to content

Commit

Permalink
[ctml2yaml] Add Shomate species thermo
Browse files Browse the repository at this point in the history
Fixes error when speciesData has an id attribute other than species_data
and the phase is StoichSubstance.
  • Loading branch information
bryanwweber committed Oct 7, 2019
1 parent d012a38 commit 62c068e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
42 changes: 38 additions & 4 deletions interfaces/cython/cantera/ctml2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,28 @@ class SpeciesThermo:

def __init__(self, thermo):
thermo_type = thermo[0].tag
if thermo_type not in ["NASA", "NASA9", "const_cp"]:
if thermo_type not in ["NASA", "NASA9", "const_cp", "Shomate"]:
raise TypeError("Unknown thermo model type: '{}'".format(thermo[0].tag))
func = getattr(self, thermo_type)
self.thermo_attribs = func(thermo)

def Shomate(self, thermo):
"""Process a Shomate polynomial from XML to a dictionary."""
thermo_attribs = BlockMap({"model": "Shomate", "data": []})
temperature_ranges = set()
model_nodes = thermo.findall("Shomate")
for node in model_nodes:
temperature_ranges.add(float(node.get("Tmin")))
temperature_ranges.add(float(node.get("Tmax")))
coeffs = node.find("floatArray").text.replace("\n", " ").strip().split(",")
thermo_attribs["data"].append(FlowList(map(float, coeffs)))
if len(temperature_ranges) != len(model_nodes) + 1:
raise ValueError(
"The midpoint temperature is not consistent between Shomate entries"
)
thermo_attribs["temperature-ranges"] = FlowList(sorted(temperature_ranges))
return thermo_attribs

def NASA(self, thermo):
"""Process a NASA 7 thermo entry from XML to a dictionary."""
thermo_attribs = BlockMap({"model": "NASA7", "data": []})
Expand Down Expand Up @@ -957,11 +974,28 @@ def convert(inpfile, outfile):
act_cross_params[this_phase.phase_attribs["thermo"]].extend(
list(ac_coeff_node.iterfind("crossFluidParameters"))
)

# The density previously associated with the phase has been moved
# to the species definition in the YAML format. StoichSubstance is
# the only model I know of that uses this node
phase_thermo_node = phase_node.find("thermo")
if phase_thermo_node.get("model") == "StoichSubstance":
for den_node in phase_thermo_node:
if den_node.tag == "density":
for spec in this_phase.phase_attribs["species"]:
den_node = phase_thermo_node.find("density")
if den_node is None:
den_node = phase_thermo_node.find("molar-density")
if den_node is None:
den_node = phase_thermo_node.find("molar-volume")
if den_node is None:
raise ValueError(
"Phase node '{}' is missing a density node.".format(
this_phase.phase_attribs["name"]
)
)
for spec_or_dict in this_phase.phase_attribs["species"]:
if isinstance(spec_or_dict, str):
const_density_specs[spec_or_dict] = den_node
else:
for spec in list(spec_or_dict.values())[0]:
const_density_specs[spec] = den_node

# Species
Expand Down
6 changes: 6 additions & 0 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,9 @@ def test_water_IAPWS95_thermo(self):
# Path(self.test_work_dir).joinpath("HMW_NaCl_sp1977_alt.yaml"))
# ctmlPhase, yamlPhase = self.checkConversion("HMW_NaCl_sp1977_alt")
# self.checkThermo(ctmlPhase, yamlPhase, [300, 500, 1300, 2000])

def test_NaCl_solid_phase(self):
ctml2yaml.convert(Path(self.test_data_dir).joinpath("NaCl_Solid.xml"),
Path(self.test_work_dir).joinpath("NaCl_Solid.yaml"))
ctmlPhase, yamlPhase = self.checkConversion("NaCl_Solid")
self.checkThermo(ctmlPhase, yamlPhase, [300, 500, 1300, 2000])
9 changes: 4 additions & 5 deletions test/data/NaCl_Solid.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

<?xml version="1.0"?>
<ctml>
<validate reactions="yes" species="yes"/>
<!-- phase NaCl(S) -->

<!-- phase NaCl(S) -->
<phase dim="3" id="NaCl(S)">
<elementArray datasrc="elements.xml">
O H C Fe Ca N Na Cl
Expand All @@ -16,7 +15,7 @@
<kinetics model="none"/>
</phase>

<!-- species definitions -->
<!-- species definitions -->
<speciesData id="species_NaCl(S)">

<!-- species NaCl(S) -->
Expand All @@ -27,7 +26,7 @@
<floatArray size="7">
50.72389, 6.672267, -2.517167,
10.15934, -0.200675, -427.2115,
130.3973
130.3973
</floatArray>
</Shomate>
</thermo>
Expand Down

0 comments on commit 62c068e

Please sign in to comment.