From 5d26a264f45468910736b7e5ab5c08dfc5bcc631 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 8 Jul 2013 01:35:27 +0000 Subject: [PATCH] [ck2cti] Add limited support for unterminated sections Each section of the input file should end with 'END'. Other utilities that work with these files don't always require this, so we allow unterminated sections if the '--permissive' flag is used, otherwise these are considered errors. --- interfaces/python/ck2cti.py | 56 +++++++++++++++++++++++++++-- test/data/unterminated-sections.inp | 30 ++++++++++++++++ test/python/testConvert.py | 13 +++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 test/data/unterminated-sections.inp diff --git a/interfaces/python/ck2cti.py b/interfaces/python/ck2cti.py index 630a00cd5..e26338059 100755 --- a/interfaces/python/ck2cti.py +++ b/interfaces/python/ck2cti.py @@ -1331,6 +1331,7 @@ class Parser(object): return None, None line, comment = readline() + advance = True while line is not None: tokens = line.split() @@ -1338,6 +1339,14 @@ class Parser(object): index = get_index(tokens, 'ELEMENTS') tokens = tokens[index+1:] while not contains(line, 'END'): + # Grudging support for implicit end of section + if contains(line, 'SPECIES'): + self.warn('"ELEMENTS" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + tokens.pop() + break + line, comment = readline() tokens.extend(line.split()) @@ -1351,6 +1360,15 @@ class Parser(object): index = get_index(tokens, 'SPECIES') tokens = tokens[index+1:] while not contains(line, 'END'): + # Grudging support for implicit end of section + if (contains(line, 'REACTIONS') or contains(line, 'TRAN') or + contains(line, 'THERM')): + self.warn('"SPECIES" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + tokens.pop() + break + line, comment = readline() tokens.extend(line.split()) @@ -1369,6 +1387,14 @@ class Parser(object): entryLength = None entry = [] while not get_index(line, 'END') == 0: + # Grudging support for implicit end of section + if (contains(line, 'REACTIONS') or contains(line, 'TRAN')): + self.warn('"THERMO" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + tokens.pop() + break + line, comment = readline() if not line: continue @@ -1413,6 +1439,14 @@ class Parser(object): TintDefault = float(line.split()[1]) thermo = [] while not contains(line, 'END'): + # Grudging support for implicit end of section + if contains(line, 'REACTIONS') or contains(line, 'TRAN'): + self.warn('"THERMO" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + tokens.pop() + break + if len(line) >= 80 and line[79] in ['1', '2', '3', '4']: thermo.append(line) if line[79] == '4': @@ -1454,6 +1488,13 @@ class Parser(object): line, comment = readline() while line is not None and not contains(line, 'END'): + # Grudging support for implicit end of section + if contains(line, 'TRAN'): + self.warn('"REACTIONS" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + break + lineStartsWithComment = not line and comment line = line.strip() comment = comment.strip() @@ -1513,14 +1554,25 @@ class Parser(object): elif contains(line, 'TRAN'): line, comment = readline() - while not contains(line, 'END'): + while line is not None and not contains(line, 'END'): + # Grudging support for implicit end of section + if contains(line, 'REACTIONS'): + self.warn('"TRANSPORT" section implicitly ended by start of ' + 'next section on line {0}.'.format(self.line_number)) + advance = False + tokens.pop() + break + if comment: transportLines.append('!'.join((line, comment))) else: transportLines.append(line) line, comment = readline() - line, comment = readline() + if advance: + line, comment = readline() + else: + advance = True self.checkDuplicateReactions() diff --git a/test/data/unterminated-sections.inp b/test/data/unterminated-sections.inp new file mode 100644 index 000000000..f3dfe6f0b --- /dev/null +++ b/test/data/unterminated-sections.inp @@ -0,0 +1,30 @@ +Elements +H C + +SPECIES +foo bar baz + +thermo + 300.000 1000.000 5000.000 +foo 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 +bar 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 +baz 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 + +reactions +foo + bar = 2 baz 1.2345e12 1.0 200.0 +2foo + baz = 3bar 5.4321e10 1.0 500.0 + +transport +foo 2 357.000 5.180 0.000 0.000 1.000 +bar 2 266.800 4.982 0.000 0.000 1.000 +baz 2 266.800 4.982 0.000 0.000 1.000 +! some comment diff --git a/test/python/testConvert.py b/test/python/testConvert.py index 352acf9d5..71d654ed3 100644 --- a/test/python/testConvert.py +++ b/test/python/testConvert.py @@ -115,6 +115,19 @@ class chemkinConverterTest(utilities.CanteraTest): self.assertEqual(list(nu[:,0]), [-1, -1, 2]) self.assertEqual(list(nu[:,1]), [-2, 3, -1]) + def test_unterminatedSections(self): + self.assertRaises(ck2cti.InputParseError, + lambda: convertMech('../data/unterminated-sections.inp', + outName='unterminated-sections.cti', + quiet=True)) + + convertMech('../data/unterminated-sections.inp', + outName='unterminated-sections.cti', + quiet=True, permissive=True) + + gas = ct.IdealGasMix('unterminated-sections.cti') + self.assertEqual(gas.nSpecies(), 3) + self.assertEqual(gas.nReactions(), 2) def test_nasa9(self): convertMech('../data/nasa9-test.inp',