Skip to content

Commit

Permalink
Fixed: Ck2yaml crashes if reaction equation has both ThreeBodyReactio…
Browse files Browse the repository at this point in the history
…n and Falloff reaction types

Bug: check_duplicate_reactions breaks if sent a third body reaction and falloff reaction because r1.third_body (or r2) is None and has no attribute upper.

Fix: This changes the third body reaction type to have a .third_body of M and then in check_duplicate_reactions check that r1 and r2 are not a mixture of three body reaction type and something else.

Bug: if blank space in header, parser crashes due to regular expression search to determine indent size

Fix: check that the comment is not empty before trying to determine indent size

Improvement: inform user which reaction contains multiple reaction types

Added Tests for both issues fixed
  • Loading branch information
tsikes authored and bryanwweber committed Jun 2, 2020
1 parent f973215 commit b23f184
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
32 changes: 20 additions & 12 deletions interfaces/cython/cantera/ck2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,29 +1051,30 @@ def read_kinetics_entry(self, entry, surface):
parser=self)

def parse_expression(expression, dest):
falloff3b = None
third_body_name = None
third_body = False # simple third body reaction (non-falloff)
photon = False
for stoichiometry, species, kind in expression:
if kind == 'third-body':
third_body = True
third_body_name = 'M'
elif kind == 'falloff3b':
falloff3b = 'M'
third_body_name = 'M'
elif kind.startswith('falloff3b:'):
falloff3b = kind.split()[1]
third_body_name = kind.split()[1]
elif kind == 'photon':
photon = True
else:
dest.append((stoichiometry, self.species_dict[species]))

return falloff3b, third_body, photon
return third_body_name, third_body, photon

falloff_3b_r, third_body, photon_r = parse_expression(reactants, reaction.reactants)
falloff_3b_p, third_body, photon_p = parse_expression(products, reaction.products)
third_body_name_r, third_body, photon_r = parse_expression(reactants, reaction.reactants)
third_body_name_p, third_body, photon_p = parse_expression(products, reaction.products)

if falloff_3b_r != falloff_3b_p:
if third_body_name_r != third_body_name_p:
raise InputError('Third bodies do not match: "{}" and "{}" in'
' reaction entry:\n\n{}', falloff_3b_r, falloff_3b_p, entry)
' reaction entry:\n\n{}', third_body_name_r, third_body_name_p, entry)

if photon_r:
raise InputError('Reactant photon not supported. '
Expand All @@ -1085,7 +1086,7 @@ def parse_expression(expression, dest):
'removed.'.format(entry.strip()))
reaction.reversible = False

reaction.third_body = falloff_3b_r
reaction.third_body = third_body_name_r

# Determine the appropriate units for k(T) and k(T,P) based on the number of reactants
# This assumes elementary kinetics for all reactions
Expand Down Expand Up @@ -1281,8 +1282,8 @@ def parse_expression(expression, dest):
tests = [cheb_coeffs, pdep_arrhenius, low_rate, high_rate, third_body,
surface]
if sum(bool(t) for t in tests) > 1:
raise InputError('Reaction entry contains parameters for more than '
'one reaction type.')
raise InputError('Reaction {} contains parameters for more than '
'one reaction type.', original_reaction)

if cheb_coeffs:
if Tmin is None or Tmax is None:
Expand Down Expand Up @@ -1396,7 +1397,8 @@ def readline():
tokens = line.split() or ['']
if inHeader and not line.strip():
header.append(comment.rstrip())
indent = min(indent, re.search('[^ ]', comment).start())
if comment.strip() != '': # skip indent calculation if empty
indent = min(indent, re.search('[^ ]', comment).start())

if tokens[0].upper().startswith('ELEM'):
inHeader = False
Expand Down Expand Up @@ -1806,6 +1808,12 @@ def check_duplicate_reactions(self):
for r1,r2 in itertools.combinations(reactions, 2):
if r1.duplicate and r2.duplicate:
pass # marked duplicate reaction
elif (type(r1.kinetics) == ThreeBody and
type(r2.kinetics) != ThreeBody):
pass
elif (type(r1.kinetics) != ThreeBody and
type(r2.kinetics) == ThreeBody):
pass
elif (r1.third_body.upper() == 'M' and
r1.kinetics.efficiencies.get(r2.third_body) == 0):
pass # explicit zero efficiency
Expand Down
10 changes: 10 additions & 0 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,16 @@ def test_surface_mech2(self):
self.assertEqual(gas.n_reactions, 0)
self.assertEqual(surf.n_reactions, 15)

def test_third_body_plus_falloff_reactions(self):
self.convert('third_body_plus_falloff_reaction.inp')
gas = ct.Solution('third_body_plus_falloff_reaction' + self.ext)
self.assertEqual(gas.n_reactions, 2)

def test_blank_line_in_header(self):
self.convert('blank_line_in_header.inp')
gas = ct.Solution('blank_line_in_header' + self.ext)
self.assertEqual(gas.n_reactions, 1)


class ck2ctiTest(converterTestCommon, utilities.CanteraTest):
ext = '.cti'
Expand Down
29 changes: 29 additions & 0 deletions test/data/blank_line_in_header.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
!Title Test

ELEMENTS
H C
END

SPECIES
CH3 CH2 H
END

THERMO ALL
300.000 1000.000 6000.000
H H 1 C 0 O 0 G 200.0 6000.0 1000.0 1
2.49985211E+00 2.34582548E-07-1.16171641E-10 2.25708298E-14-1.52992005E-18 2
2.54738024E+04-4.45864645E-01 2.49975925E+00 6.73824499E-07 1.11807261E-09 3
-3.70192126E-12 2.14233822E-15 2.54737665E+04-4.45574009E-01 2.62191345E+04 4
CH2 H 2 C 1 N 0 G 200.0 6000.0 1000.0 1
2.81412990E+00 3.79513443E-03-7.38424949E-07 7.19948955E-11-2.63188525E-15 2
4.61859709E+04 6.52945537E+00 3.74842895E+00 1.07655977E-03 3.62462012E-06 3
-4.46165519E-09 1.95224874E-12 4.58990472E+04 1.63509483E+00 4.70886746E+04 4
CH3 H 3 C 1 O 0 G 200.0 6000.0 1000.0 1
2.92198336E+00 5.37478625E-03-1.99748367E-06 2.97584806E-10-1.71860088E-14 2
1.65446753E+04 5.25396822E+00 3.61264389E+00 3.09209041E-03 9.25474611E-07 3
-1.65776770E-09 6.07243533E-13 1.63849955E+04 1.79994780E+00 1.76047468E+04 4
END

REACTIONS
CH3=CH2+H 6.E14 0.0 0.0
END
30 changes: 30 additions & 0 deletions test/data/third_body_plus_falloff_reaction.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ELEMENTS
H C
END

SPECIES
CH3 CH2 H
END

THERMO
300.000 1000.000 6000.000
H H 1 C 0 O 0 G 200.0 6000.0 1000.0 1
2.49985211E+00 2.34582548E-07-1.16171641E-10 2.25708298E-14-1.52992005E-18 2
2.54738024E+04-4.45864645E-01 2.49975925E+00 6.73824499E-07 1.11807261E-09 3
-3.70192126E-12 2.14233822E-15 2.54737665E+04-4.45574009E-01 2.62191345E+04 4
CH2 H 2 C 1 N 0 G 200.0 6000.0 1000.0 1
2.81412990E+00 3.79513443E-03-7.38424949E-07 7.19948955E-11-2.63188525E-15 2
4.61859709E+04 6.52945537E+00 3.74842895E+00 1.07655977E-03 3.62462012E-06 3
-4.46165519E-09 1.95224874E-12 4.58990472E+04 1.63509483E+00 4.70886746E+04 4
CH3 H 3 C 1 O 0 G 200.0 6000.0 1000.0 1
2.92198336E+00 5.37478625E-03-1.99748367E-06 2.97584806E-10-1.71860088E-14 2
1.65446753E+04 5.25396822E+00 3.61264389E+00 3.09209041E-03 9.25474611E-07 3
-1.65776770E-09 6.07243533E-13 1.63849955E+04 1.79994780E+00 1.76047468E+04 4
END

REACTIONS CAL/MOLE MOLES
H+CH2+M=CH3+M 6.E14 0.0 0.0
H+CH2(+M)<=>CH3(+M) 6.0E+14 .000 .00
LOW / 1.040E+26 -2.760 1600.00/
TROE/ .5620 91.00 5836.00 8552.00/
END

0 comments on commit b23f184

Please sign in to comment.