Skip to content

Commit

Permalink
Allow float input of integer geometry flags in ck2yaml
Browse files Browse the repository at this point in the history
If `permissive` is given, `float` geometry flags are cast to `int` and are accepted if the values are equal, otherwise, `InputError` is raised. If `permissive` is not given, existing behavior is unchanged.

Add tests to check for correct behavior with and without permissive, for float rounding error, and for character inputs.

Closes #1395
  • Loading branch information
corykinney committed Jan 24, 2023
1 parent f6744ec commit a90dc63
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
25 changes: 19 additions & 6 deletions interfaces/cython/cantera/ck2yaml.py
Expand Up @@ -687,17 +687,30 @@ def reduce(self, output):
class TransportData:
geometry_flags = ['atom', 'linear', 'nonlinear']

def __init__(self, label, geometry, well_depth, collision_diameter,
def __init__(self, parser, label, geometry, well_depth, collision_diameter,
dipole_moment, polarizability, z_rot, note=''):

try:
geometry = int(geometry)
except ValueError:
raise InputError(
"Bad geometry flag '{}' for species '{}', is the flag a float "
"or character? It should be an integer.", geometry, label)
try:
geometry = float(geometry)
except ValueError:
raise InputError(
"Invalid geometry flag '{}' for species '{}'. "
"Flag should be an integer.", geometry, label) from None
if geometry == int(geometry):
geometry = int(geometry)
parser.warn("Incorrect geometry flag syntax for species {0}. "
"If --permissive was given, the flag was automatically "
"converted to an integer.".format(label))
else:
raise InputError(
"Invalid float geometry flag '{}' for species '{}'. "
"Flag should be an integer.", geometry, label) from None
if geometry not in (0, 1, 2):
raise InputError("Bad geometry flag '{}' for species '{}'.", geometry, label)
raise InputError("Invalid geometry flag value '{}' for species '{}'. "
"Flag value should be 0, 1, or 2.", geometry, label)

self.geometry = self.geometry_flags[int(geometry)]
self.well_depth = float(well_depth)
Expand Down Expand Up @@ -1890,7 +1903,7 @@ def parse_transport_data(self, lines, filename, line_offset):
line_offset + i, filename, original_line, len(data)-1)

if self.species_dict[speciesName].transport is None:
self.species_dict[speciesName].transport = TransportData(*data, note=comment)
self.species_dict[speciesName].transport = TransportData(self, *data, note=comment)
else:
self.warn('Ignoring duplicate transport data'
' for species "{}" on line {} of "{}".'.format(
Expand Down
9 changes: 9 additions & 0 deletions test/data/h2o2-character-geometry-tran.dat
@@ -0,0 +1,9 @@
AR 0 136.500 3.330 0.000 0.000 0.000
H 0 145.000 2.050 0.000 0.000 0.000
H2 1 38.000 2.920 0.000 0.790 280.000
H2O 2 572.400 2.605 1.844 0.000 4.000
H2O2 2 107.400 3.458 0.000 0.000 3.800
HO2 a 107.400 3.458 0.000 0.000 1.000 ! *
O 0 80.000 2.750 0.000 0.000 0.000
O2 1 107.400 3.458 0.000 1.600 3.800
OH 1 80.000 2.750 0.000 0.000 0.000
9 changes: 9 additions & 0 deletions test/data/h2o2-float-arithmetic-error-geometry-tran.dat
@@ -0,0 +1,9 @@
AR 0 136.500 3.330 0.000 0.000 0.000
H 0 145.000 2.050 0.000 0.000 0.000
H2 0.9999999999999999 38.000 2.920 0.000 0.790 280.000 ! *
H2O 1.9999999999999999 572.400 2.605 1.844 0.000 4.000 ! *
H2O2 2 107.400 3.458 0.000 0.000 3.800
HO2 2 107.400 3.458 0.000 0.000 1.000
O 0 80.000 2.750 0.000 0.000 0.000
O2 1 107.400 3.458 0.000 1.600 3.800
OH 1 80.000 2.750 0.000 0.000 0.000
8 changes: 4 additions & 4 deletions test/data/h2o2-float-geometry-tran.dat
@@ -1,9 +1,9 @@
AR 0 136.500 3.330 0.000 0.000 0.000
H 0.00 145.000 2.050 0.000 0.000 0.000
H2 1.00 38.000 2.920 0.000 0.790 280.000
H2O 2.00 572.400 2.605 1.844 0.000 4.000
H 0.00 145.000 2.050 0.000 0.000 0.000 ! *
H2 1.00 38.000 2.920 0.000 0.790 280.000 ! *
H2O 2.00 572.400 2.605 1.844 0.000 4.000 ! *
H2O2 2 107.400 3.458 0.000 0.000 3.800
HO2 2 107.400 3.458 0.000 0.000 1.000 ! *
HO2 2 107.400 3.458 0.000 0.000 1.000
O 0 80.000 2.750 0.000 0.000 0.000
O2 1 107.400 3.458 0.000 1.600 3.800
OH 1 80.000 2.750 0.000 0.000 0.000
23 changes: 21 additions & 2 deletions test/python/test_convert.py
Expand Up @@ -332,17 +332,36 @@ def test_transport_duplicate_species(self):
output='h2o2_transport_duplicate_species', permissive=True)

def test_transport_bad_geometry(self):
with self.assertRaisesRegex(ck2yaml.InputError, 'geometry flag'):
with self.assertRaisesRegex(ck2yaml.InputError, 'Invalid geometry flag value'):
self.convert('h2o2.inp',
transport='h2o2-bad-geometry-tran.dat',
output='h2o2_transport_bad_geometry')

with self.assertRaisesRegex(ck2yaml.InputError, 'Invalid geometry flag \''):
self.convert('h2o2.inp',
transport='h2o2-character-geometry-tran.dat',
output='h2o2_transport_character_geometry')

def test_transport_float_geometry(self):
with self.assertRaisesRegex(ck2yaml.InputError, 'geometry flag'):
with self.assertRaisesRegex(ck2yaml.InputError, 'Incorrect geometry flag syntax'):
self.convert('h2o2.inp',
transport='h2o2-float-geometry-tran.dat',
output='h2o2_transport_float_geometry')

output = self.convert('h2o2.inp',
transport='h2o2-float-geometry-tran.dat',
output='h2o2_transport_float_geometry', permissive=True)

gas = ct.Solution(output)
self.assertTrue(gas.species("H").transport.geometry == 'atom')
self.assertTrue(gas.species("H2").transport.geometry == 'linear')
self.assertTrue(gas.species("H2O").transport.geometry == 'nonlinear')

with self.assertRaisesRegex(ck2yaml.InputError, 'Invalid float geometry flag'):
self.convert('h2o2.inp',
transport='h2o2-float-arithmetic-error-geometry-tran.dat',
output='h2o2_transport_float_geometry', permissive=True)

def test_empty_reaction_section(self):
output = self.convert('h2o2_emptyReactions.inp')
gas = ct.Solution(output)
Expand Down

0 comments on commit a90dc63

Please sign in to comment.