From a90dc63ab818fea77c10c795663d20e984f025cf Mon Sep 17 00:00:00 2001 From: Cory Kinney Date: Mon, 26 Sep 2022 15:01:42 -0400 Subject: [PATCH 1/2] 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 --- interfaces/cython/cantera/ck2yaml.py | 25 ++++++++++++++----- test/data/h2o2-character-geometry-tran.dat | 9 +++++++ ...2-float-arithmetic-error-geometry-tran.dat | 9 +++++++ test/data/h2o2-float-geometry-tran.dat | 8 +++--- test/python/test_convert.py | 23 +++++++++++++++-- 5 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 test/data/h2o2-character-geometry-tran.dat create mode 100644 test/data/h2o2-float-arithmetic-error-geometry-tran.dat diff --git a/interfaces/cython/cantera/ck2yaml.py b/interfaces/cython/cantera/ck2yaml.py index 83519bc37a..bc42527b84 100644 --- a/interfaces/cython/cantera/ck2yaml.py +++ b/interfaces/cython/cantera/ck2yaml.py @@ -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) @@ -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( diff --git a/test/data/h2o2-character-geometry-tran.dat b/test/data/h2o2-character-geometry-tran.dat new file mode 100644 index 0000000000..e50a0d56b5 --- /dev/null +++ b/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 diff --git a/test/data/h2o2-float-arithmetic-error-geometry-tran.dat b/test/data/h2o2-float-arithmetic-error-geometry-tran.dat new file mode 100644 index 0000000000..ec4efa7508 --- /dev/null +++ b/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 diff --git a/test/data/h2o2-float-geometry-tran.dat b/test/data/h2o2-float-geometry-tran.dat index dbfefc4cc6..45955e52d2 100644 --- a/test/data/h2o2-float-geometry-tran.dat +++ b/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 diff --git a/test/python/test_convert.py b/test/python/test_convert.py index 06cdfb2217..108780f45d 100644 --- a/test/python/test_convert.py +++ b/test/python/test_convert.py @@ -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) From 824d8901c58b5df5045593484099350254a70305 Mon Sep 17 00:00:00 2001 From: Cory Kinney Date: Mon, 23 Jan 2023 21:03:38 -0500 Subject: [PATCH 2/2] Add Pycharm to .gitignore Ignore `.idea/` folder associated with PyCharm IDE data --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8501cdf623..b2b47c0746 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ stage/ .sconf_temp .vs .vscode +.idea/ .ipynb_checkpoints/ cantera.conf* config.log