mirror of
https://github.com/Cantera/cantera.git
synced 2026-08-11 13:34:53 -05:00
Allow float input of integer geometry flags in ck2yaml
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
This commit is contained in:
@@ -687,17 +687,30 @@ class Sri:
|
||||
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)
|
||||
@@ -1890,7 +1903,7 @@ class Parser:
|
||||
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(
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -332,17 +332,36 @@ class ck2yamlTest(utilities.CanteraTest):
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user