Skip to content

Commit

Permalink
Refactor reaction generation and processing for reactEdge phase
Browse files Browse the repository at this point in the history
Motivation:
- processNewReactions requires a newSpecies argument which was not
being properly identified after multiprocessing changes in #1459

Background:
- The newSpecies argument is retained from early RMG when reactions
were generated right after adding a new species to the core.
- Hence, newSpecies referred to the newly added core species which
was used to generate the new reactions.
- The overall model generation algorithm has since changed, so
identifying newSpecies is not as straightforward.
- The multiprocessing PR changed newSpecies to be any core species,
missing the original purpose of identifying the species from which
the reaction was generated.

Changes:
- Use the species tuples created during reaction generation to
keep track of which species were used to generate the reaction
- Update unit tests which are affected by the changes in return values
from react and react_all
  • Loading branch information
mliu49 committed Jul 19, 2019
1 parent c2f9495 commit 43dbba7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
21 changes: 13 additions & 8 deletions rmgpy/rmg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,19 @@ def enlarge(self, newObject=None, reactEdge=False,
numOldEdgeReactions -= len(reactionsMovedFromEdge)

else:
# We are reacting the edge
rxns = react_all(self.core.species, numOldCoreSpecies,
unimolecularReact, bimolecularReact, trimolecularReact=trimolecularReact, procnum=procnum)

spcs = [self.retrieve_species(rxn) for rxn in rxns]

for rxn, spc in zip(rxns, spcs):
self.processNewReactions([rxn], spc, generateThermo=False)
# Generate reactions between all core species which have not been
# reacted yet and exceed the reaction filter thresholds
rxnLists, spcsTuples = react_all(self.core.species, numOldCoreSpecies,
unimolecularReact, bimolecularReact,
trimolecularReact=trimolecularReact,
procnum=procnum)

for rxnList, spcTuple in zip(rxnLists, spcsTuples):
if rxnList:
# Identify a core species which was used to generate the reaction
# This is only used to determine the reaction direction for processing
spc = spcTuple[0]
self.processNewReactions(rxnList, spc, generateThermo=False)

################################################################
# Begin processing the new species and reactions
Expand Down
7 changes: 4 additions & 3 deletions rmgpy/rmg/modelTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# #
###############################################################################

import itertools
import os
import unittest

Expand Down Expand Up @@ -142,8 +143,8 @@ class item:
spcs = [Species().fromSMILES('CC'), Species().fromSMILES('[CH3]')]
spcTuples = [((spcA, spc), ['H_Abstraction']) for spc in spcs]

rxns = list(react(spcTuples, procnum))
rxns += list(react([((spcs[0], spcs[1]), ['H_Abstraction'])], procnum))
rxns = list(itertools.chain.from_iterable(react(spcTuples, procnum)))
rxns += list(itertools.chain.from_iterable(react([((spcs[0], spcs[1]), ['H_Abstraction'])], procnum)))

for rxn in rxns:
cerm.makeNewReaction(rxn)
Expand Down Expand Up @@ -246,7 +247,7 @@ def testMakeNewReaction(self):
spcs = [Species().fromSMILES('CC'), Species().fromSMILES('[CH3]')]
spcTuples = [((spcA, spc), ['H_Abstraction']) for spc in spcs]

rxns = list(react(spcTuples, procnum))
rxns = list(itertools.chain.from_iterable(react(spcTuples, procnum)))

cerm = CoreEdgeReactionModel()

Expand Down
3 changes: 2 additions & 1 deletion rmgpy/rmg/parreactTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
This module contains unit tests of the rmgpy.parallel module.
"""

import itertools
import os
import sys
import unittest
Expand Down Expand Up @@ -93,7 +94,7 @@ def generate():
spcTuples = [(spcA, spc) for spc in spcs]
procnum = 2

reactionList = list(react(spcTuples, procnum))
reactionList = list(itertools.chain.from_iterable(react(spcTuples, procnum)))

if not reactionList: return False

Expand Down
5 changes: 2 additions & 3 deletions rmgpy/rmg/react.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"""
Contains functions for generating reactions.
"""
import itertools
import logging

from rmgpy.data.rmg import getDB
Expand Down Expand Up @@ -67,7 +66,7 @@ def react(spc_tuples, procnum=1):
p.close()
p.join()

return itertools.chain.from_iterable(reactions)
return reactions


def _react_species_star(args):
Expand Down Expand Up @@ -147,5 +146,5 @@ def react_all(core_spc_list, numOldCoreSpecies, unimolecularReact, bimolecularRe
else:
spc_fam_tuples.append((spc_tuple, ))

return list(react(spc_fam_tuples, procnum))
return react(spc_fam_tuples, procnum), [fam_tuple[0] for fam_tuple in spc_fam_tuples]

25 changes: 17 additions & 8 deletions rmgpy/rmg/reactTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# #
###############################################################################

import itertools
import os
import unittest
import numpy as np
Expand Down Expand Up @@ -76,7 +77,7 @@ def testReact(self):
spcs = [Species().fromSMILES('CC'), Species().fromSMILES('[CH3]')]
spc_tuples = [((spc_a, spc), ['H_Abstraction']) for spc in spcs]

reaction_list = list(react(spc_tuples, procnum))
reaction_list = list(itertools.chain.from_iterable(react(spc_tuples, procnum)))
self.assertIsNotNone(reaction_list)
self.assertEqual(len(reaction_list), 3)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reaction_list]))
Expand All @@ -93,7 +94,7 @@ def testReactParallel(self):
spcs = [Species().fromSMILES('CC'), Species().fromSMILES('[CH3]')]
spc_tuples = [((spc_a, spc), ['H_Abstraction']) for spc in spcs]

reaction_list = list(react(spc_tuples, procnum))
reaction_list = list(itertools.chain.from_iterable(react(spc_tuples, procnum)))
self.assertIsNotNone(reaction_list)
self.assertEqual(len(reaction_list), 3)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reaction_list]))
Expand All @@ -112,10 +113,14 @@ def testReactAll(self):
]

n = len(spcs)
reaction_list = react_all(spcs, n, np.ones(n), np.ones([n, n]), np.ones([n, n, n]), procnum)
reaction_list, spc_tuples = react_all(spcs, n, np.ones(n), np.ones([n, n]), np.ones([n, n, n]), procnum)
self.assertIsNotNone(reaction_list)
self.assertEqual(len(reaction_list), 44)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reaction_list]))
self.assertEqual(len(reaction_list), 34)
self.assertEqual(len(spc_tuples), 34)

flat_rxn_list = list(itertools.chain.from_iterable(reaction_list))
self.assertEqual(len(flat_rxn_list), 44)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in flat_rxn_list]))

def testReactAllParallel(self):
"""
Expand All @@ -133,10 +138,14 @@ def testReactAllParallel(self):
]

n = len(spcs)
reaction_list = react_all(spcs, n, np.ones(n), np.ones([n, n]), np.ones([n, n, n]), procnum)
reaction_list, spc_tuples = react_all(spcs, n, np.ones(n), np.ones([n, n]), np.ones([n, n, n]), procnum)
self.assertIsNotNone(reaction_list)
self.assertEqual(len(reaction_list), 44)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reaction_list]))
self.assertEqual(len(reaction_list), 94)
self.assertEqual(len(spc_tuples), 94)

flat_rxn_list = list(itertools.chain.from_iterable(reaction_list))
self.assertEqual(len(flat_rxn_list), 44)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in flat_rxn_list]))

def tearDown(self):
"""
Expand Down

0 comments on commit 43dbba7

Please sign in to comment.