Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept floating point representations of valid integer geometry flags in ck2yaml #1396

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ stage/
.sconf_temp
.vs
.vscode
.idea/
.ipynb_checkpoints/
cantera.conf*
config.log
Expand Down
25 changes: 19 additions & 6 deletions interfaces/cython/cantera/ck2yaml.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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