[ck2cti] Fix reactions with pathologically named third bodies

This changes the order in which tokens are identified to be strictly
descending in length, so that third bodies are identified correctly
even when the third body expression could potentially be interpreted
as containing a standalone species name.
This commit is contained in:
Ray Speth 2017-07-11 23:49:23 -04:00
parent 55a8910686
commit 6d591b82ef
2 changed files with 6 additions and 9 deletions

View file

@ -1179,7 +1179,7 @@ class Parser(object):
Ea = float(tokens[-1])
reaction = ''.join(tokens[:-3]) + '\n'
# Identify species tokens in the reaction expression in order of
# Identify tokens in the reaction expression in order of
# decreasing length
locs = {}
for i in range(self.Slen, 0, -1):
@ -1188,14 +1188,8 @@ class Parser(object):
if test in self.species_tokens:
reaction = reaction[:j] + ' '*(i-1) + reaction[j+i-1:]
locs[j] = test[:-1], 'species'
# Identify other tokens in the reaction expression in order of
# descending length
for i in range(self.Slen, 0, -1):
for j in range(len(reaction)-i+1):
test = reaction[j:j+i]
if test in self.other_tokens:
reaction = reaction[:j] + ' '*i + reaction[j+i:]
elif test in self.other_tokens:
reaction = reaction[:j] + '\n'*i + reaction[j+i:]
locs[j] = test, self.other_tokens[test]
# Anything that's left should be a stoichiometric coefficient or a '+'

View file

@ -160,6 +160,9 @@ class chemkinConverterTest(utilities.CanteraTest):
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])
def test_unterminatedSections(self):
with self.assertRaises(ck2cti.InputParseError):