Skip to content

Commit

Permalink
Add unitTest for generating reactions with multiprocessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajocher committed Aug 30, 2018
1 parent d620441 commit 97ce8a1
Showing 1 changed file with 8 additions and 93 deletions.
101 changes: 8 additions & 93 deletions rmgpy/rmg/reactTest.py
Expand Up @@ -40,7 +40,7 @@
from rmgpy.species import Species

from rmgpy.rmg.main import RMG
from rmgpy.rmg.react import react, reactAll, deflate, deflateReaction
from rmgpy.rmg.react import react, reactAll

###################################################

Expand Down Expand Up @@ -79,67 +79,20 @@ def testReact(self):
self.assertIsNotNone(reactionList)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reactionList]))

def testDeflate(self):
def testReactMultiproc(self):
"""
Test that reaction deflate function works.
Test that reaction generation from the available families works with python multiprocessing.
"""
molA = Species().fromSMILES('[OH]')
molB = Species().fromSMILES('CC')
molC = Species().fromSMILES('[CH3]')

reactants = [molA, molB]

# both reactants were already part of the core:
reactantIndices = [1, 2]

rxn = Reaction(reactants=[molA, molB], products=[molC],
pairs=[(molA, molC), (molB, molC)])

deflate([rxn], reactants, reactantIndices)

for spc, t in zip(rxn.reactants, [int, int]):
self.assertTrue(isinstance(spc, t))
self.assertEquals(rxn.reactants, reactantIndices)
for spc in rxn.products:
self.assertTrue(isinstance(spc, Species))

# one of the reactants was not yet part of the core:
reactantIndices = [-1, 2]

rxn = Reaction(reactants=[molA, molB], products=[molC],
pairs=[(molA, molC), (molB, molC)])

deflate([rxn], reactants, reactantIndices)

for spc, t in zip(rxn.reactants, [Species, int]):
self.assertTrue(isinstance(spc, t))
for spc in rxn.products:
self.assertTrue(isinstance(spc, Species))

def testReactStoreIndices(self):
"""
Test that reaction generation keeps track of the original species indices.
"""

indices = {'[OH]':1, 'CC':2, '[CH3]':3}

# make it bidirectional so that we can look-up indices as well:
revd=dict([reversed(i) for i in indices.items()])
indices.update(revd)

spcA = Species(index=indices['[OH]']).fromSMILES('[OH]')
spcs = [Species(index=indices['CC']).fromSMILES('CC'),
Species(index=indices['[CH3]']).fromSMILES('[CH3]')]
import rmgpy.rmg.main
rmgpy.rmg.main.maxproc = 2

spcA = Species().fromSMILES('[OH]')
spcs = [Species().fromSMILES('CC'), Species().fromSMILES('[CH3]')]
spcTuples = [(spcA, spc) for spc in spcs]

reactionList = list(react(*spcTuples))
self.assertIsNotNone(reactionList)
self.assertEquals(len(reactionList), 3)
for rxn in reactionList:
for i, reactant in enumerate(rxn.reactants):
rxn.reactants[i] = Molecule().fromSMILES(indices[reactant])
self.assertTrue(rxn.isBalanced())
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in reactionList]))

def testReactAll(self):
"""
Expand All @@ -157,44 +110,6 @@ def testReactAll(self):
self.assertIsNotNone(rxns)
self.assertTrue(all([isinstance(rxn, TemplateReaction) for rxn in rxns]))

def testDeflateReaction(self):
"""
Test if the deflateReaction function works.
"""

molA = Species().fromSMILES('[OH]')
molB = Species().fromSMILES('CC')
molC = Species().fromSMILES('[CH3]')

# both reactants were already part of the core:
reactantIndices = [1, 2]
molDict = {molA.molecule[0]: 1, molB.molecule[0]: 2}

rxn = Reaction(reactants=[molA, molB], products=[molC],
pairs=[(molA, molC), (molB, molC)])

deflateReaction(rxn, molDict)

for spc, t in zip(rxn.reactants, [int, int]):
self.assertTrue(isinstance(spc, t))
self.assertEquals(rxn.reactants, reactantIndices)
for spc in rxn.products:
self.assertTrue(isinstance(spc, Species))

# one of the reactants was not yet part of the core:
reactantIndices = [-1, 2]
molDict = {molA.molecule[0]: molA, molB.molecule[0]: 2}

rxn = Reaction(reactants=[molA, molB], products=[molC],
pairs=[(molA, molC), (molB, molC)])

deflateReaction(rxn, molDict)

for spc, t in zip(rxn.reactants, [Species, int]):
self.assertTrue(isinstance(spc, t), 'Species {} is not of type {}'.format(spc,t))
for spc in rxn.products:
self.assertTrue(isinstance(spc, Species))


def tearDown(self):
"""
Expand Down

0 comments on commit 97ce8a1

Please sign in to comment.