From f47e98a594a5b43b8b41b38eaea49eca8f30406c Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 7 Feb 2018 21:27:21 -0500 Subject: [PATCH] [ck2cti] Fix over-zealous detection of new input file sections Species names starting with the short form of input file section names (e.g. 'tran') were incorrectly identified as indicating the start of that section if they occurred at the start of a line. --- interfaces/cython/cantera/ck2cti.py | 23 +++++++++++------ .../cython/cantera/test/test_convert.py | 25 ++++++++++--------- test/data/species-names.inp | 7 +++++- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/interfaces/cython/cantera/ck2cti.py b/interfaces/cython/cantera/ck2cti.py index 0264b7dc2..812b44cdb 100644 --- a/interfaces/cython/cantera/ck2cti.py +++ b/interfaces/cython/cantera/ck2cti.py @@ -1549,7 +1549,8 @@ class Parser(object): tokens = tokens[1:] while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() == 'SPEC': + start = line.strip().upper().split() + if start and start[0] in ('SPEC', 'SPECIES'): self.warn('"ELEMENTS" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1572,7 +1573,9 @@ class Parser(object): inHeader = False while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() in ('REAC', 'TRAN', 'THER'): + start = line.strip().upper().split() + if start and start[0] in ('REAC', 'REACTIONS', 'TRAN', + 'TRANSPORT', 'THER', 'THERMO'): self.warn('"SPECIES" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1620,7 +1623,9 @@ class Parser(object): inHeader = False while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() in ('REAC', 'THER'): + start = line.strip().upper().split() + if start and start[0] in ('REAC', 'REACTIONS', 'THER', + 'THERMO'): self.warn('"SITE" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1659,7 +1664,8 @@ class Parser(object): entry = [] while line is not None and not get_index(line, 'END') == 0: # Grudging support for implicit end of section - if line.strip()[:4].upper() in ('REAC', 'TRAN'): + start = line.strip().upper().split() + if start and start[0] in ('REAC', 'REACTIONS', 'TRAN', 'TRANSPORT'): self.warn('"THERMO" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1728,7 +1734,8 @@ class Parser(object): current = [] while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() in ('REAC', 'TRAN'): + start = line.strip().upper().split() + if start and start[0] in ('REAC', 'REACTIONS', 'TRAN', 'TRANSPORT'): self.warn('"THERMO" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1819,7 +1826,8 @@ class Parser(object): reactions = self.reactions while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() == 'TRAN': + start = line.strip().upper().split() + if start and start[0] in ('TRAN', 'TRANSPORT'): self.warn('"REACTIONS" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False @@ -1878,7 +1886,8 @@ class Parser(object): transport_start_line = self.line_number while line is not None and not contains(line, 'END'): # Grudging support for implicit end of section - if line.strip()[:4].upper() == 'REAC': + start = line.strip().upper().split() + if start and start[0] in ('REAC', 'REACTIONS'): self.warn('"TRANSPORT" section implicitly ended by start of ' 'next section on line {0}.'.format(self.line_number)) advance = False diff --git a/interfaces/cython/cantera/test/test_convert.py b/interfaces/cython/cantera/test/test_convert.py index 359aefc1e..39879fef9 100644 --- a/interfaces/cython/cantera/test/test_convert.py +++ b/interfaces/cython/cantera/test/test_convert.py @@ -142,7 +142,7 @@ class chemkinConverterTest(utilities.CanteraTest): outName=pjoin(self.test_work_dir, 'species-names.cti'), quiet=True) gas = ct.Solution('species-names.cti') - self.assertEqual(gas.n_species, 7) + self.assertEqual(gas.n_species, 8) self.assertEqual(gas.species_name(0), '(Parens)') self.assertEqual(gas.species_name(1), '@#$%^-2') self.assertEqual(gas.species_index('co:lons:'), 2) @@ -151,18 +151,19 @@ class chemkinConverterTest(utilities.CanteraTest): self.assertEqual(gas.species_name(5), 'eq=uals') self.assertEqual(gas.species_name(6), 'plus') - self.assertEqual(gas.n_reactions, 10) + self.assertEqual(gas.n_reactions, 11) nu = gas.product_stoich_coeffs() - gas.reactant_stoich_coeffs() - self.assertEqual(list(nu[:,0]), [-1, -1, 0, 2, 0, 0, 0]) - self.assertEqual(list(nu[:,1]), [-2, 3, 0, -1, 0, 0, 0]) - self.assertEqual(list(nu[:,2]), [-1, 0, 0, 0, 1, 0, 0]) - self.assertEqual(list(nu[:,3]), [3, 0, 0, 0, -2, -1, 0]) - self.assertEqual(list(nu[:,4]), [2, 0, 0, 0, -1, 0, -1]) - self.assertEqual(list(nu[:,5]), [1, 0, 0, 0, 1, -1, -1]) - self.assertEqual(list(nu[:,6]), [2, 0, -1, 0, 0, -1, 0]) - self.assertEqual(list(nu[:,7]), [0, 0, 0, 0, -1, 1, 0]) - self.assertEqual(list(nu[:,8]), [0, 0, 0, 0, -1, 1, 0]) - self.assertEqual(list(nu[:,9]), [0, 0, 0, 0, -1, 1, 0]) + self.assertEqual(list(nu[:,0]), [-1, -1, 0, 2, 0, 0, 0, 0]) + self.assertEqual(list(nu[:,1]), [-2, 3, 0, -1, 0, 0, 0, 0]) + self.assertEqual(list(nu[:,2]), [-1, 0, 0, 0, 1, 0, 0, 0]) + self.assertEqual(list(nu[:,3]), [3, 0, 0, 0, -2, -1, 0, 0]) + self.assertEqual(list(nu[:,4]), [2, 0, 0, 0, -1, 0, -1, 0]) + self.assertEqual(list(nu[:,5]), [1, 0, 0, 0, 1, -1, -1, 0]) + self.assertEqual(list(nu[:,6]), [2, 0, -1, 0, 0, -1, 0, 0]) + self.assertEqual(list(nu[:,7]), [0, 0, 0, 0, -1, 1, 0, 0]) + self.assertEqual(list(nu[:,8]), [0, 0, 0, 0, -1, 1, 0, 0]) + self.assertEqual(list(nu[:,9]), [0, 0, 0, 0, -1, 1, 0, 0]) + self.assertEqual(list(nu[:,10]), [0, 0, -1, 0, 2, 0, 0, -1]) def test_unterminatedSections(self): with self.assertRaises(ck2cti.InputParseError): diff --git a/test/data/species-names.inp b/test/data/species-names.inp index 8c0ff5d6c..6e9705370 100644 --- a/test/data/species-names.inp +++ b/test/data/species-names.inp @@ -2,7 +2,7 @@ Elements H C End SPEC -(Parens) @#$%^-2 co:lons: [xy2]*{.} plus+ eq=uals plus +(Parens) @#$%^-2 co:lons: [xy2]*{.} plus+ eq=uals plus trans_butene end thermo @@ -23,6 +23,10 @@ co:lons: C 1H 4 G 200.000 3500.000 1000.000 1 7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2 -9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3 -4.84743026E-08 1.66693956E-11-1.02466476E+04-4.64130376E+00 4 +trans_butene C 1H 4 G 200.000 3500.000 1000.000 1 + 7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2 +-9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3 +-4.84743026E-08 1.66693956E-11-1.02466476E+04-4.64130376E+00 4 plus+ C 1H 4 G 200.000 3500.000 1000.000 1 7.48514950E-02 1.33909467E-02-5.73285809E-06 1.22292535E-09-1.01815230E-13 2 -9.46834459E+03 1.84373180E+01 5.14987613E+00-1.36709788E-02 4.91800599E-05 3 @@ -51,5 +55,6 @@ plus+ (+(Parens)) = eq=uals (+(Parens)) 9.999e9 9.9 999.9 LOW / 8.888e8 8.8 888.8 / plus+ (+plus+) = eq=uals (+plus+) 9.999e9 9.9 999.9 LOW / 8.888e8 8.8 888.8 / +trans_butene + co:lons: = 2 plus+ 9.999e9 9.9 999.9 end