From d6a3ea4aa7dbc310b3707a58f655d6beceeff0bf Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 7 May 2014 21:57:22 +0000 Subject: [PATCH] [ck2cti] Allow stoichiometric coefficients ending in decimal points Fixes Issue 220. --- interfaces/cython/cantera/ck2cti.py | 17 +++++++++-------- interfaces/cython/cantera/test/test_convert.py | 13 +++++++++++++ test/data/float-stoich.inp | 12 ++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 test/data/float-stoich.inp diff --git a/interfaces/cython/cantera/ck2cti.py b/interfaces/cython/cantera/ck2cti.py index 924f013cc..ab8a89bb9 100644 --- a/interfaces/cython/cantera/ck2cti.py +++ b/interfaces/cython/cantera/ck2cti.py @@ -40,9 +40,6 @@ import numpy as np import re import itertools -reFloat = re.compile(r'\+?\d*\.?\d+([eEdD][-+]?\d+)?$') -reInt = re.compile(r'\+?\d+$') - QUANTITY_UNITS = {'MOL': 'mol', 'MOLE': 'mol', 'MOLES': 'mol', @@ -1086,12 +1083,16 @@ class Parser(object): j = reaction.find(token) i = len(token) reaction = reaction[:j] + ' '*i + reaction[j+i:] - if reInt.match(token): + if token == '+': + continue + + try: locs[j] = int(token), 'coeff' - elif reFloat.match(token): - locs[j] = float(token), 'coeff' - elif token != '+': - raise InputParseError('Unexpected token "{0}" in reaction expression "{1}".'.format(token, reaction)) + except ValueError: + try: + locs[j] = float(token), 'coeff' + except ValueError: + raise InputParseError('Unexpected token "{0}" in reaction expression "{1}".'.format(token, reaction)) reactants = [] products = [] diff --git a/interfaces/cython/cantera/test/test_convert.py b/interfaces/cython/cantera/test/test_convert.py index b855ee7e8..0e1c83e61 100644 --- a/interfaces/cython/cantera/test/test_convert.py +++ b/interfaces/cython/cantera/test/test_convert.py @@ -251,6 +251,19 @@ class chemkinConverterTest(utilities.CanteraTest): self.checkKinetics(default, custom, [300, 800, 1450, 2800], [5e3, 1e5, 2e6], 1e-7) + def test_float_stoich_coeffs(self): + convertMech('../data/float-stoich.inp', + thermoFile='../data/dummy-thermo.dat', + outName='float-stoich.cti', quiet=True) + gas = ct.Solution('float-stoich.cti') + + R = gas.reactant_stoich_coeffs() + P = gas.product_stoich_coeffs() + self.assertArrayNear(R[:,0], [0, 1.5, 0.5, 0]) + self.assertArrayNear(P[:,0], [1, 0, 0, 1]) + self.assertArrayNear(R[:,1], [1, 0, 0, 1]) + self.assertArrayNear(P[:,1], [0, 0.33, 1.67, 0]) + def test_transport_normal(self): convertMech('../../data/inputs/h2o2.inp', transportFile='../../data/transport/gri30_tran.dat', diff --git a/test/data/float-stoich.inp b/test/data/float-stoich.inp new file mode 100644 index 000000000..2d75a4564 --- /dev/null +++ b/test/data/float-stoich.inp @@ -0,0 +1,12 @@ +ELEMENTS +H C +END + +SPECIES +H R1A R1B P1 +END + +REACTIONS +1.5R1A+.5R1B => 1.H+P1 1e12 0.0 20000.0 +1.0H+1.P1=>.33R1A+1.67R1B 5e13 -2.0 5000.0 +END