Skip to content

Commit

Permalink
[ctml2yaml] Retain zero values in the YAML output
Browse files Browse the repository at this point in the history
As demonstrated in Cantera#683, sometimes the zero values are intentional and
meaningful. Rework the SpeciesTransport class to not use this function.
  • Loading branch information
bryanwweber committed Nov 26, 2019
1 parent 3b4b970 commit 41e78a4
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions interfaces/cython/cantera/ctml2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,6 @@ def get_float_or_units(node: etree.Element) -> Union[str, float]:
return value


def check_float_neq_zero(value: float, name: str) -> Dict[str, float]:
"""Check that the text value associated with a tag is non-zero.
If the value is not zero, return a dictionary with the key ``name``
and the value. If the value is zero, return an empty dictionary.
Calling functions can use this function to ``update`` a dictionary of
attributes without adding keys whose values are zero.
"""
if not np.isclose(value, 0.0):
return {name: value}
else:
return {}


def split_species_value_string(node: etree.Element) -> Dict[str, float]:
"""Split a string of species:value pairs into a dictionary.
Expand Down Expand Up @@ -1387,23 +1373,30 @@ class SpeciesTransport:
}

def __init__(self, transport: etree.Element):
transport_attribs = BlockMap({})
self.attribs = BlockMap({})
transport_model = transport.get("model")
if transport_model not in self._species_transport_mapping:
if transport_model not in self.species_transport_mapping:
raise TypeError(
"Unknown transport model type: '{}'".format(transport.get("model"))
)
transport_attribs["model"] = self.species_transport_mapping[transport_model]
transport_attribs["geometry"] = transport.findtext("string[@title='geometry']")
for tag, name in self.transport_properties_mapping.items():
value = float(transport.findtext(tag, default=0.0))
transport_attribs.update(check_float_neq_zero(value, name))

self.transport_attribs = transport_attribs
self.attribs["model"] = self.species_transport_mapping[transport_model]
self.attribs["geometry"] = transport.findtext("string[@title='geometry']")
for prop_node in transport:
if prop_node.tag == "string":
continue
# Don't use get_float_or_units because the units of the gas_transport
# parameters are assumed to be customary units in YAML.
value = float(clean_node_text(prop_node))
name = self.transport_properties_mapping.get(prop_node.tag)
if name is None:
raise TypeError(
"Unknown transport property node: '{}'".format(prop_node.tag)
)
self.attribs[name] = value

@classmethod
def to_yaml(cls, representer, data):
return representer.represent_dict(data.transport_attribs)
return representer.represent_dict(data.attribs)


class Species:
Expand Down

0 comments on commit 41e78a4

Please sign in to comment.