[Input] Fix Species.listFromYaml when reading from a subsection

This commit is contained in:
Ray Speth 2019-06-21 14:57:45 -04:00
parent 674701b0c2
commit d705ff760a
2 changed files with 22 additions and 4 deletions

View file

@ -1,4 +1,5 @@
from os.path import join as pjoin
import pathlib
import os
import numpy as np
import gc
@ -1048,6 +1049,14 @@ class TestSpecies(utilities.CanteraTest):
self.assertEqual(species[1].composition, {'H': 1, 'O': 2})
self.assertNear(species[0].thermo.h(300), 100)
def test_listFromYaml_section(self):
species = ct.Species.listFromYaml(
pathlib.Path('../data/ideal-gas.yaml').read_text(),
'species')
self.assertEqual(species[0].name, 'O2')
self.assertEqual(species[1].composition, {'N': 1, 'O': 1})
def test_listFromXml(self):
p = os.path.dirname(__file__)
with open(pjoin(p, '..', 'data', 'h2o2.xml')) as f:

View file

@ -44,7 +44,11 @@ cdef class Species:
containing the species defined in the GRI 3.0 mechanism::
S = ct.Species.listFromFile('gri30.yaml')
S = ct.Species.listFromYaml(open('path/to/gri30.yaml').read())
import pathlib
S = ct.Species.listFromYaml(
pathlib.Path('path/to/gri30.yaml').read_text(),
section='species')
"""
def __cinit__(self, *args, init=True, **kwargs):
@ -164,13 +168,18 @@ cdef class Species:
return species
@staticmethod
def listFromYaml(text):
def listFromYaml(text, section=None):
"""
Create a list of Species objects from all the species defined in a YAML
string.
string. If ``text`` is a YAML mapping, the ``section`` name of the list
to be read must be specified. If ``text`` is a YAML list, no ``section``
name should be supplied.
"""
root = AnyMapFromYamlString(stringify(text))
cxx_species = CxxGetSpecies(root[stringify("items")])
# ``items`` is the pseudo-key used to access a list when it is at the
# top level of a YAML document
cxx_species = CxxGetSpecies(root[stringify(section or "items")])
species = []
for a in cxx_species:
b = Species(init=False)