From f1e5183431480d19c977edd5bfff743cd6614af2 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 8 Jul 2013 01:35:15 +0000 Subject: [PATCH] [ck2cti] Fix to allow species names starting with special characters --- interfaces/python/ck2cti.py | 10 ++++++++-- test/data/species-names.inp | 27 +++++++++++++++++++++++++++ test/python/testConvert.py | 16 ++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/data/species-names.inp diff --git a/interfaces/python/ck2cti.py b/interfaces/python/ck2cti.py index f916c6baf..a0462dbc0 100755 --- a/interfaces/python/ck2cti.py +++ b/interfaces/python/ck2cti.py @@ -807,6 +807,12 @@ def fortFloat(s): s = s.replace('E ', 'E+').replace('e ', 'e+') return float(s) +def isnumberlike(text): + """ Returns true if `text` contains only the digits 0-9 and '.' """ + for char in text: + if not char.isdigit() and char != '.': + return False + return True def get_index(seq, value): """ @@ -1064,10 +1070,10 @@ class Parser(object): for term in expression.split('+'): term = term.strip() - if not term[0].isalpha(): + if isnumberlike(term[0]): # This allows for for non-unity stoichiometric coefficients, e.g. # 2A=B+C or .85A+.15B=>C - j = [i for i,c in enumerate(term) if c.isalpha()][0] + j = [i for i,c in enumerate(term) if not isnumberlike(c)][0] if term[:j].isdigit(): stoichiometry = int(term[:j]) else: diff --git a/test/data/species-names.inp b/test/data/species-names.inp new file mode 100644 index 000000000..5cc1932d4 --- /dev/null +++ b/test/data/species-names.inp @@ -0,0 +1,27 @@ +Elements +H C +End +SPECIES +(Parens) @#$%^-2 [xy2]*{.} +end + +thermo + 300.000 1000.000 5000.000 +(Parens) 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 +@#$%^-2 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 +[xy2]*{.} 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 +end + +reactions +(Parens) + @#$%^-2 = 2 [xy2]*{.} 1.2345e12 1.0 200.0 +2(Parens) + [xy2]*{.} = 3@#$%^-2 5.4321e10 1.0 500.0 +end diff --git a/test/python/testConvert.py b/test/python/testConvert.py index 28a50bdf4..cb700b6dc 100644 --- a/test/python/testConvert.py +++ b/test/python/testConvert.py @@ -100,6 +100,22 @@ class chemkinConverterTest(utilities.CanteraTest): outName='h2o2_missingThermo.cti', quiet=True)) + def test_pathologicalSpeciesNames(self): + convertMech('../data/species-names.inp', + outName='species-names.cti', quiet=True) + gas = ct.IdealGasMix('species-names.cti') + + self.assertEqual(gas.nSpecies(), 3) + self.assertEqual(gas.speciesName(0), '(Parens)') + self.assertEqual(gas.speciesName(1), '@#$%^-2') + self.assertEqual(gas.speciesName(2), '[xy2]*{.}') + + self.assertEqual(gas.nReactions(), 2) + nu = gas.productStoichCoeffs() - gas.reactantStoichCoeffs() + self.assertEqual(list(nu[:,0]), [-1, -1, 2]) + self.assertEqual(list(nu[:,1]), [-2, 3, -1]) + + def test_nasa9(self): convertMech('../data/nasa9-test.inp', thermoFile='../data/nasa9-test-therm.dat',