Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix chemkin file duplicate flags #1856

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions rmgpy/chemkin.pyx
Expand Up @@ -1969,22 +1969,22 @@ def mark_duplicate_reaction(test_reaction, reaction_list):
# duplicates of one another.
# RHW question: why can't TemplateReaction be duplicate of LibraryReaction, in Chemkin terms? I guess it shouldn't happen in RMG.
continue
same_dir_match = (reaction1.reactants == reaction2.reactants and reaction1.products == reaction2.products)
opposite_dir_match = (reaction1.products == reaction2.reactants and reaction1.reactants == reaction2.products)
same_dir_match = (sorted(reaction1.reactants) == sorted(reaction2.reactants) and sorted(reaction1.products) == sorted(reaction2.products))
opposite_dir_match = (sorted(reaction1.products) == sorted(reaction2.reactants) and sorted(reaction1.reactants) == sorted(reaction2.products))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may need to be an edit here to catch errors like
InputFileError thrown by Kinetics::checkDuplicates: Undeclared duplicate reactions detected: Reaction ####: A <=> A Reaction ####: 2 A <=> 2 B

that results when you use either the ck2cti or ck2yaml.

if (same_dir_match or opposite_dir_match) and (reaction1.specific_collider == reaction2.specific_collider):
if reaction1.duplicate and reaction2.duplicate:
if reaction1.kinetics.is_pressure_dependent() != reaction2.kinetics.is_pressure_dependent():
# Reactions with mixed pressure dependence do not need to be marked duplicate in Chemkin
logging.warning('Marked reaction {0} as not duplicate because of mixed pressure dependence '
'for saving to Chemkin file.'.format(reaction1))
reaction1.duplicate = False
reaction2.duplicate = False
logging.warning('Reaction {0} is marked as duplicate but for Chemkin file it '
'doesn\'t need to be because of mixed pressure dependence'
'.'.format(reaction1))
# But leave it alone in case it's a DUPLICATE of some _other_ reaction.
elif opposite_dir_match and not reaction1.reversible and not reaction2.reversible:
# Irreversible reactions in opposite directions do not need to be marked duplicate in Chemkin
logging.warning('Marked reaction {0} as not duplicate because they are irreversible '
'in opposite directions for saving to Chemkin file.'.format(reaction1))
reaction1.duplicate = False
reaction2.duplicate = False
logging.warning('Reaction {0} is marked as duplicate but for Chemkin file it '
'doesn\'t need to be because they are irreversible '
'in opposite directions.'.format(reaction1))
# But leave it alone in case it's a DUPLICATE of some _other_ reaction.
else:
if (reaction1.kinetics.is_pressure_dependent() == reaction2.kinetics.is_pressure_dependent()
and ((reaction1.reversible and reaction2.reversible)
Expand Down Expand Up @@ -2150,7 +2150,7 @@ def save_chemkin_file(path, species, reactions, verbose=True, check_for_duplicat
f.write('\n')
f.write('END\n\n')
f.close()
logging.info("Chemkin file contains {0} reactions.".format(_chemkin_reaction_count))
logging.info("Chemkin surface file contains {0} reactions.".format(_chemkin_reaction_count))
_chemkin_reaction_count = None


Expand Down
17 changes: 17 additions & 0 deletions test/rmgpy/chemkinTest.py
Expand Up @@ -503,6 +503,21 @@ def test_mark_duplicate_reactions(self):

assert duplicate_flags == expected_flags

@pytest.mark.skip(reason="WIP")
def test_unmark_duplicate_reactions(self):
"""Test that we can properly REMOVE duplicate reaction flags for Chemkin."""

"""
We can't do this properly yet.
See https://github.com/ReactionMechanismGenerator/RMG-Py/pull/1856
"""
s1 = Species().from_smiles('CC')
s2 = Species().from_smiles('[CH3]')
s3 = Species().from_smiles('[OH]')
s4 = Species().from_smiles('C[CH2]')
s5 = Species().from_smiles('O')
s6 = Species().from_smiles('[H]')

# Try initializing with duplicate=True
reaction_list = [
Reaction(reactants=[s1], products=[s2, s2], duplicate=True, kinetics=Arrhenius()),
Expand Down Expand Up @@ -549,6 +564,8 @@ def test_mark_duplicate_reactions(self):
),
]

expected_flags = [True, True, False, False, True, True, False, False]

mark_duplicate_reactions(reaction_list)
duplicate_flags = [rxn.duplicate for rxn in reaction_list]

Expand Down