[ck2cti] Preserve comments in the REACTIONS section
This commit is contained in:
parent
21e9892909
commit
a3bba51f06
3 changed files with 32 additions and 25 deletions
|
|
@ -249,6 +249,7 @@ class Reaction(object):
|
|||
self.duplicate = duplicate
|
||||
self.fwdOrders = fwdOrders if fwdOrders is not None else {}
|
||||
self.thirdBody = thirdBody
|
||||
self.comment = ''
|
||||
|
||||
def _coeff_string(self, coeffs):
|
||||
L = []
|
||||
|
|
@ -857,6 +858,7 @@ class Parser(object):
|
|||
self.speciesList = []
|
||||
self.speciesDict = {}
|
||||
self.reactions = []
|
||||
self.finalReactionComment = ''
|
||||
|
||||
def warn(self, message):
|
||||
if self.warning_as_error:
|
||||
|
|
@ -1558,7 +1560,7 @@ class Parser(object):
|
|||
|
||||
lineStartsWithComment = not line and comment
|
||||
line = line.strip()
|
||||
comment = comment.strip()
|
||||
comment = comment.rstrip()
|
||||
|
||||
if '=' in line and not lineStartsWithComment:
|
||||
# Finish previous record
|
||||
|
|
@ -1580,37 +1582,23 @@ class Parser(object):
|
|||
kineticsList.append(kinetics)
|
||||
commentsList.append(comments)
|
||||
|
||||
if not kineticsList:
|
||||
pass
|
||||
elif kineticsList[0] == '' and commentsList[-1] == '':
|
||||
# True for mechanism files generated from RMG-Py
|
||||
# We don't actually know whether comments belong to the
|
||||
# previous or next reaction, but to keep them positioned
|
||||
# correctly, we associate them with the next reaction (and
|
||||
# keep track of the final trailing comment separately)
|
||||
if kineticsList and kineticsList[0] == '':
|
||||
kineticsList.pop(0)
|
||||
commentsList.pop(-1)
|
||||
elif kineticsList[0] == '' and commentsList[0] == '':
|
||||
# True for mechanism files generated from RMG-Java
|
||||
kineticsList.pop(0)
|
||||
commentsList.pop(0)
|
||||
else:
|
||||
# In reality, comments can occur anywhere in the mechanism
|
||||
# file (e.g. either or both of before and after the
|
||||
# reaction equation)
|
||||
# If we can't tell what semantics we are using, then just
|
||||
# throw the comments away
|
||||
# (This is better than failing to load the mechanism file at
|
||||
# all, which would likely occur otherwise)
|
||||
if kineticsList[0] == '':
|
||||
kineticsList.pop(0)
|
||||
if len(kineticsList) != len(commentsList):
|
||||
commentsList = ['' for kinetics in kineticsList]
|
||||
self.finalReactionComment = commentsList.pop()
|
||||
|
||||
self.setupKinetics()
|
||||
for kinetics, comments, line_number in zip(kineticsList, commentsList, startLines):
|
||||
for kinetics, comment, line_number in zip(kineticsList, commentsList, startLines):
|
||||
try:
|
||||
reaction,revReaction = self.readKineticsEntry(kinetics)
|
||||
except Exception as e:
|
||||
logging.error('Error reading reaction entry starting on line {0}:'.format(line_number))
|
||||
raise
|
||||
reaction.line_number = line_number
|
||||
reaction.comment = comment
|
||||
self.reactions.append(reaction)
|
||||
if revReaction is not None:
|
||||
revReaction.line_number = line_number
|
||||
|
|
@ -1772,9 +1760,13 @@ class Parser(object):
|
|||
lines.append(delimiterLine)
|
||||
|
||||
for i,r in enumerate(self.reactions):
|
||||
lines.extend('# '+c for c in r.comment.split('\n') if c)
|
||||
lines.append('\n# Reaction {0}'.format(i+1))
|
||||
lines.append(r.to_cti())
|
||||
|
||||
# Comment after the last reaction
|
||||
lines.extend('# '+c for c in self.finalReactionComment.split('\n') if c)
|
||||
|
||||
lines.append('')
|
||||
|
||||
f = open(outName, 'w')
|
||||
|
|
|
|||
|
|
@ -319,6 +319,21 @@ class chemkinConverterTest(utilities.CanteraTest):
|
|||
self.assertEqual(gas.n_species, 9)
|
||||
self.assertEqual(gas.n_reactions, 0)
|
||||
|
||||
def test_reaction_comments1(self):
|
||||
convertMech('../data/pdep-test.inp',
|
||||
outName='pdep_test.cti', quiet=True)
|
||||
text = open('pdep_test.cti').read()
|
||||
self.assertIn('Single PLOG reaction', text)
|
||||
self.assertIn('PLOG with duplicate rates and negative A-factors', text)
|
||||
|
||||
def test_reaction_comments2(self):
|
||||
convertMech('../data/explicit-third-bodies.inp',
|
||||
thermoFile='../data/dummy-thermo.dat',
|
||||
outName='explicit_third_bodies.cti', quiet=True)
|
||||
text = open('explicit_third_bodies.cti').read()
|
||||
self.assertIn('An end of line comment', text)
|
||||
self.assertIn('A comment after the last reaction', text)
|
||||
|
||||
|
||||
class CtmlConverterTest(utilities.CanteraTest):
|
||||
def test_sofc(self):
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ END
|
|||
|
||||
REACTIONS
|
||||
|
||||
R1A+R1B+m = P1+H+M 3.0E19 -2.0 1900
|
||||
R1A+R1B+m = P1+H+M 3.0E19 -2.0 1900 ! An end of line comment
|
||||
DUPLICATE
|
||||
|
||||
R1A+R1B(+ M ) = P1+H(+m) 1.0E18 -2.0 1000
|
||||
|
|
@ -34,5 +34,5 @@ R1A+R1B(+R2) = P1+H(+ R2) 2.0E18 -3.0 1200
|
|||
R1A+R1B(+SP)X) = P1+H(+SP)X) 3.0E18 -1.0 1400
|
||||
LOW/1.0E25 -1.0 0/
|
||||
DUPLICATE
|
||||
|
||||
! A comment after the last reaction
|
||||
END
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue