From 43dbba7d45707c567b23494b39c64eb7a41e9e54 Mon Sep 17 00:00:00 2001 From: Max Liu Date: Wed, 17 Jul 2019 18:59:05 -0400 Subject: [PATCH] Refactor reaction generation and processing for reactEdge phase 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 --- rmgpy/rmg/model.py | 21 +++++++++++++-------- rmgpy/rmg/modelTest.py | 7 ++++--- rmgpy/rmg/parreactTest.py | 3 ++- rmgpy/rmg/react.py | 5 ++--- rmgpy/rmg/reactTest.py | 25 +++++++++++++++++-------- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/rmgpy/rmg/model.py b/rmgpy/rmg/model.py index 81873620506..f317e76b2e2 100644 --- a/rmgpy/rmg/model.py +++ b/rmgpy/rmg/model.py @@ -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 diff --git a/rmgpy/rmg/modelTest.py b/rmgpy/rmg/modelTest.py index b81c606584f..72b1fbc966d 100644 --- a/rmgpy/rmg/modelTest.py +++ b/rmgpy/rmg/modelTest.py @@ -28,6 +28,7 @@ # # ############################################################################### +import itertools import os import unittest @@ -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) @@ -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() diff --git a/rmgpy/rmg/parreactTest.py b/rmgpy/rmg/parreactTest.py index a6b360ca416..74b8fad9ad3 100644 --- a/rmgpy/rmg/parreactTest.py +++ b/rmgpy/rmg/parreactTest.py @@ -32,6 +32,7 @@ This module contains unit tests of the rmgpy.parallel module. """ +import itertools import os import sys import unittest @@ -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 diff --git a/rmgpy/rmg/react.py b/rmgpy/rmg/react.py index db901702ca2..8c06c4dc962 100644 --- a/rmgpy/rmg/react.py +++ b/rmgpy/rmg/react.py @@ -31,7 +31,6 @@ """ Contains functions for generating reactions. """ -import itertools import logging from rmgpy.data.rmg import getDB @@ -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): @@ -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] diff --git a/rmgpy/rmg/reactTest.py b/rmgpy/rmg/reactTest.py index e64c4452db5..ab9fa8c57a0 100644 --- a/rmgpy/rmg/reactTest.py +++ b/rmgpy/rmg/reactTest.py @@ -28,6 +28,7 @@ # # ############################################################################### +import itertools import os import unittest import numpy as np @@ -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])) @@ -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])) @@ -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): """ @@ -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): """