class TestReaction(unittest.TestCase): def setUp(self): self.reaction = Reaction("CC+[O]O_[CH2]C+OO") self.reaction2 = Reaction(rmg_reaction=RMGReaction( reactants=[RMGMolecule(smiles="CC"), RMGMolecule(smiles="[O]O")], products=[RMGMolecule(smiles="[CH2]C"), RMGMolecule(smiles="OO")])) def test_label(self): self.assertEqual(self.reaction.label, "CC+[O]O_[CH2]C+OO") self.assertEqual(self.reaction2.get_label(), "CC+[O]O_C[CH2]+OO") def test_rmg_reaction(self): test_reaction = RMGReaction( reactants=[RMGMolecule(smiles="CC"), RMGMolecule(smiles="[O]O")], products=[RMGMolecule(smiles="[CH2]C"), RMGMolecule(smiles="OO")]) self.assertTrue( test_reaction.is_isomorphic(self.reaction.get_rmg_reaction())) self.assertTrue( test_reaction.is_isomorphic(self.reaction2.get_rmg_reaction())) def test_databases(self): rmg_database, ts_databases = self.reaction.load_databases() self.assertIsInstance(rmg_database, RMGDatabase) self.assertIsInstance(ts_databases, dict) self.assertIsInstance(ts_databases["H_Abstraction"], TransitionStates) rmg_database, ts_databases = self.reaction2.load_databases() self.assertIsInstance(rmg_database, RMGDatabase) self.assertIsInstance(ts_databases, dict) self.assertIsInstance(ts_databases["H_Abstraction"], TransitionStates) def test_disance_data(self): d12 = 1.38 d13 = 2.43 # d13 in reactionTest is smaller than the distance in baseTest.py because # d13 is edited to be smaller in reaction.py. The value returned from the database # is ~2.53 but is reduced to 2.43 when called from the reaction object itself d23 = 1.16 self.assertAlmostEquals(d12, self.reaction.distance_data.distances["d12"], places=1) self.assertAlmostEquals(d13, self.reaction.distance_data.distances["d13"], places=1) self.assertAlmostEquals(d23, self.reaction.distance_data.distances["d23"], places=1) self.assertAlmostEquals(d12, self.reaction2.distance_data.distances["d12"], places=1) self.assertAlmostEquals(d13, self.reaction2.distance_data.distances["d13"], places=1) self.assertAlmostEquals(d23, self.reaction2.distance_data.distances["d23"], places=1) def test_generate_reactants_and_products(self): reactants, products = self.reaction.generate_reactants_and_products() self.assertIsInstance(reactants, list) self.assertIsInstance(products, list) self.assertEquals(len(reactants), 2) self.assertAlmostEquals(len(products), 2) reactants, products = self.reaction2.generate_reactants_and_products() self.assertIsInstance(reactants, list) self.assertIsInstance(products, list) self.assertEquals(len(reactants), 2) self.assertAlmostEquals(len(products), 2) def test_labeled_reaction(self): test_reaction = RMGReaction( reactants=[RMGMolecule(smiles="CC"), RMGMolecule(smiles="[O]O")], products=[RMGMolecule(smiles="[CH2]C"), RMGMolecule(smiles="OO")]) labeled_reaction, reaction_family = self.reaction.get_labeled_reaction( ) self.assertEquals(reaction_family.lower(), "h_abstraction") self.assertTrue(test_reaction.is_isomorphic(labeled_reaction)) merged = labeled_reaction.reactants[0].merge( labeled_reaction.reactants[1]) self.assertTrue(merged.get_labeled_atoms("*1")[0].is_carbon()) self.assertTrue(merged.get_labeled_atoms("*2")[0].is_hydrogen()) self.assertTrue(merged.get_labeled_atoms("*3")[0].is_oxygen) merged = labeled_reaction.products[0].merge( labeled_reaction.products[1]) self.assertTrue(merged.get_labeled_atoms("*3")[0].is_carbon()) self.assertTrue(merged.get_labeled_atoms("*2")[0].is_hydrogen()) self.assertTrue(merged.get_labeled_atoms("*1")[0].is_oxygen) labeled_reaction, reaction_family = self.reaction2.get_labeled_reaction( ) self.assertEquals(reaction_family.lower(), "h_abstraction") self.assertTrue(test_reaction.is_isomorphic(labeled_reaction)) merged = labeled_reaction.reactants[0].merge( labeled_reaction.reactants[1]) self.assertTrue(merged.get_labeled_atoms("*1")[0].is_carbon()) self.assertTrue(merged.get_labeled_atoms("*2")[0].is_hydrogen()) self.assertTrue(merged.get_labeled_atoms("*3")[0].is_oxygen) merged = labeled_reaction.products[0].merge( labeled_reaction.products[1]) self.assertTrue(merged.get_labeled_atoms("*3")[0].is_carbon()) self.assertTrue(merged.get_labeled_atoms("*2")[0].is_hydrogen()) self.assertTrue(merged.get_labeled_atoms("*1")[0].is_oxygen) def test_rmg_complexes(self): self.reaction.get_labeled_reaction() self.reaction.get_rmg_complexes() self.assertEquals(len(self.reaction.complexes), 2) self.assertEquals( len(self.reaction.complexes["forward"].get_all_labeled_atoms()), 3) self.assertEquals( len(self.reaction.complexes["reverse"].get_all_labeled_atoms()), 3) self.reaction2.get_labeled_reaction() self.reaction2.get_rmg_complexes() self.assertEquals(len(self.reaction2.complexes), 2) self.assertEquals( len(self.reaction2.complexes["forward"].get_all_labeled_atoms()), 3) self.assertEquals( len(self.reaction2.complexes["reverse"].get_all_labeled_atoms()), 3) def test_ts(self): self.assertEquals(len(self.reaction.ts), 2) self.assertEquals(len(self.reaction.ts["forward"]), 1) self.assertEquals(len(self.reaction.ts["reverse"]), 1) self.assertIsInstance(self.reaction.ts["forward"][0], TS) self.assertIsInstance(self.reaction.ts["reverse"][0], TS) self.assertEquals(len(self.reaction2.ts), 2) self.assertEquals(len(self.reaction2.ts["forward"]), 1) self.assertEquals(len(self.reaction2.ts["reverse"]), 1) self.assertIsInstance(self.reaction2.ts["forward"][0], TS) self.assertIsInstance(self.reaction2.ts["reverse"][0], TS) def test_reaction_families(self): # R_Addition_MultipleBond reaction = Reaction("C#C+[OH]_[CH]=CO") _, family = reaction.get_labeled_reaction() self.assertEqual(family.lower(), "R_Addition_MultipleBond".lower()) # intra_H_migration reaction = Reaction("C[CH]O_CC[O]") _, family = reaction.get_labeled_reaction() self.assertEqual(family.lower(), "intra_H_migration".lower())
class JobTest(unittest.TestCase): def setUp(self): os.environ["PATH"] = os.path.expandvars( "$AUTOTST/test/bin:") + os.environ["PATH"] os.environ["TEST_STATUS"] = "None" self.reaction = Reaction("CC+[O]O_[CH2]C+OO") self.calculator = Gaussian( directory=os.path.expandvars("$AUTOTST/test")) self.job = Job(reaction=self.reaction, calculator=self.calculator, partition="test", username="******", exclude="test", account="test") self.job2 = Job(reaction=self.reaction, calculator=self.calculator, partition="test", username="******", exclude="test", account=["test"]) def test_setup(self): self.assertEqual(self.job.username, "test") self.assertEqual(self.job.exclude, "test") self.assertEqual(self.job.partition, "test") self.assertEqual(self.job.account, "test") self.assertEqual(self.job.label, self.reaction.label) def test_setup2(self): job = Job(directory=".") self.assertEqual(job.directory, ".") self.assertEqual(job.scratch, ".") def test_read_log(self): path = os.path.expandvars("$AUTOTST/test/bin/log-files/CC_0.log") atoms = self.job.read_log(path) self.assertEqual(len(atoms), 8) carbon_count = 0 hydrogen_count = 0 for atom in atoms: if atom.symbol == "H": hydrogen_count += 1 elif atom.symbol == "C": carbon_count += 1 self.assertEqual(hydrogen_count, 6) self.assertEqual(carbon_count, 2) def test_write_input(self): self.assertTrue(True) def test_check_complete(self): ### I don't know how to create alaises in a python script os.environ["TEST_STATUS"] = "None" self.assertFalse(self.job.check_complete("test1")) self.assertTrue(self.job.check_complete("test2")) def test_submit(self): result = self.job.submit("echo testing") self.assertTrue(result) ### For conformers def test_submit_conformer(self): self.reaction.generate_reactants_and_products() conformer = list(self.reaction.reactants[0].conformers.values())[0][0] label = self.job.submit_conformer(conformer) self.assertEqual(label, f"{conformer.smiles}_{conformer.index}") def test_submit_conformer2(self): self.reaction.generate_reactants_and_products() conformer = list(self.reaction.reactants[0].conformers.values())[0][0] label = self.job2.submit_conformer(conformer) self.assertEqual(label, f"{conformer.smiles}_{conformer.index}") def test_calculate_conformer(self): conformer = Conformer(smiles='CC', index=0) result = self.job.calculate_conformer(conformer=conformer) self.assertTrue(result) def test_calculate_species(self): self.reaction.generate_reactants_and_products() for species in self.reaction.reactants + self.reaction.products: self.job.calculate_species(species) for smiles in species.conformers.keys(): self.assertTrue( os.path.exists( os.path.join( os.path.expandvars("$AUTOTST/test/species/"), smiles, smiles + ".log"))) def test_submit_transitionstate(self): ts = self.reaction.ts["forward"][0] ts.get_molecules() for opt_type in ["shell", "center", "overall"]: label = self.job.submit_transitionstate(ts, opt_type=opt_type) if opt_type == "overall": self.assertEqual( label, f"{ts.reaction_label}_{ts.direction}_{ts.index}") else: self.assertEqual( label, f"{ts.reaction_label}_{ts.direction}_{opt_type}_{ts.index}" ) def test_submit_transitionstate2(self): ts = self.reaction.ts["forward"][0] ts.get_molecules() for opt_type in ["shell", "center", "overall"]: label = self.job2.submit_transitionstate(ts, opt_type=opt_type) if opt_type == "overall": self.assertEqual( label, f"{ts.reaction_label}_{ts.direction}_{ts.index}") else: self.assertEqual( label, f"{ts.reaction_label}_{ts.direction}_{opt_type}_{ts.index}" ) def test_calculate_transitionstate(self): ts = self.reaction.ts["forward"][0] ts.get_molecules() result = self.job.calculate_transitionstate(ts) self.assertTrue(result) def test_calculate_reaction(self): del self.reaction.ts["reverse"] result = self.job.calculate_reaction() self.assertTrue(result)
class JobTest(unittest.TestCase): def setUp(self): self.reaction = Reaction("CC+[O]O_[CH2]C+OO") self.calculator = Gaussian( directory=os.path.expandvars("$AUTOTST/test")) self.job = Job(reaction=self.reaction, calculator=self.calculator, partition="test") os.environ["PATH"] = os.path.expandvars( "$AUTOTST/test/bin:") + os.environ["PATH"] def test_read_log(self): path = os.path.expandvars("$AUTOTST/test/bin/log-files/CC_0.log") atoms = self.job.read_log(path) self.assertEqual(len(atoms), 8) carbon_count = 0 hydrogen_count = 0 for atom in atoms: if atom.symbol == "H": hydrogen_count += 1 elif atom.symbol == "C": carbon_count += 1 self.assertEqual(hydrogen_count, 6) self.assertEqual(carbon_count, 2) def test_write_input(self): self.assert_(True) def test_check_complete(self): ### I don't know how to create alaises in a python script self.assertFalse(self.job.check_complete("test1")) self.assertTrue(self.job.check_complete("test2")) ### For conformers def test_submit_conformer(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/species")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/species")) self.reaction.generate_reactants_and_products() conformer = self.reaction.reactants[0].conformers.values()[0][0] label = self.job.submit_conformer(conformer) self.assertEquals(label, "{}_{}".format(conformer.smiles, conformer.index)) def test_calculate_conformer(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/species")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/species")) conformer = Conformer(smiles='CC', index=0) result = self.job.calculate_conformer(conformer=conformer) self.assertTrue(result) conformer = Conformer(smiles='CC(Cl)(Cl)Cl', index=0) result = self.job.calculate_conformer(conformer=conformer) self.assertFalse(result) def test_calculate_species(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/species")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/species")) self.reaction.generate_reactants_and_products() for species in self.reaction.reactants + self.reaction.products: self.job.calculate_species(species) for smiles in species.conformers.keys(): self.assertTrue( os.path.exists( os.path.join( os.path.expandvars("$AUTOTST/test/species/"), smiles, smiles + ".log"))) def test_submit_transitionstate(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/ts")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/ts")) ts = self.reaction.ts["forward"][0] ts.get_molecules() for opt_type in ["shell", "center", "overall"]: label = self.job.submit_transitionstate(ts, opt_type=opt_type) if opt_type == "overall": self.assertEqual( label, "{}_{}_{}".format(ts.reaction_label, ts.direction, ts.index)) else: self.assertEqual( label, "{}_{}_{}_{}".format(ts.reaction_label, ts.direction, opt_type, ts.index)) def test_calculate_transitionstate(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/ts")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/ts")) ts = self.reaction.ts["forward"][0] ts.get_molecules() result = self.job.calculate_transitionstate(ts) self.assertTrue(result) def test_calculate_reaction(self): del self.reaction.ts["reverse"] result = self.job.calculate_reaction() self.assertTrue(result) def tearDown(self): if os.path.exists(os.path.expandvars("$AUTOTST/test/species")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/species")) if os.path.exists(os.path.expandvars("$AUTOTST/test/ts")): shutil.rmtree(os.path.expandvars("$AUTOTST/test/ts"))
class TestStatMech(unittest.TestCase): def setUp(self): self.reaction = Reaction("CC+[O]O_[CH2]C+OO") self.reaction.get_labeled_reaction() self.reaction.generate_reactants_and_products() directory = os.path.expandvars("$AUTOTST/test") if not os.path.exists( os.path.join(directory, "ts", self.reaction.label)): os.makedirs(os.path.join(directory, "ts", self.reaction.label)) if not os.path.exists( os.path.join(directory, "ts", self.reaction.label, self.reaction.label + ".log")): shutil.copy( os.path.join(directory, "bin", "log-files", self.reaction.label + "_forward_0.log"), os.path.join(directory, "ts", self.reaction.label, self.reaction.label + ".log")) for sp in self.reaction.reactants + self.reaction.products: for smiles in sp.conformers.keys(): if not os.path.exists( os.path.join(directory, "species", smiles)): os.makedirs(os.path.join(directory, "species", smiles)) if not os.path.exists( os.path.join(directory, "species", smiles, smiles + ".log")): shutil.copy( os.path.join(directory, "bin", "log-files", smiles + "_0.log"), os.path.join(directory, "species", smiles, smiles + ".log")) self.statmech = StatMech(reaction=self.reaction, directory=directory) def tearDown(self): directory = os.path.expandvars("$AUTOTST/test") if os.path.exists(os.path.join(directory, "ts")): shutil.rmtree(os.path.join(directory, "ts")) if os.path.exists(os.path.join(directory, "species")): shutil.rmtree(os.path.join(directory, "species")) for head, _, files in os.walk(os.path.expandvars("$AUTOTST")): for fi in files: if fi.endswith(".symm"): os.remove(os.path.join(head, fi)) def test_get_atoms(self): ts = self.reaction.ts["forward"][0] atom_dict = self.statmech.get_atoms(ts) self.assertEqual(atom_dict["H"], 7) self.assertEqual(atom_dict["C"], 2) self.assertEqual(atom_dict["O"], 2) #def test_get_bonds(self): ### For somereason, this test doesn't work on travis... # ts = self.reaction.ts["forward"][0] # bond_dict = self.statmech.get_bonds(ts) # self.assertEqual(bond_dict["O-O"], 1) # self.assertEqual(bond_dict["O-H"], 1) # self.assertEqual(bond_dict["C-C"], 1) # self.assertEqual(bond_dict["C-H"], 6) def test_write_conformer_file(self): species = self.reaction.reactants[0] conformer = species.conformers.values()[0][0] self.assertTrue(self.statmech.write_conformer_file(conformer)) self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "species", conformer.smiles, conformer.smiles + ".py"))) # Running it again to see if it recognizes that a .py file was already written self.assertTrue(self.statmech.write_conformer_file(conformer)) def test_write_species_file(self): species = self.reaction.reactants[0] self.statmech.write_species_files(species) for smiles in species.conformers.keys(): self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "species", smiles, smiles + ".py"))) def test_write_ts_input(self): ts = self.reaction.ts["forward"][0] self.assertTrue(self.statmech.write_ts_input(ts)) self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "ts", self.reaction.label, self.reaction.label + ".py"))) self.assertTrue(self.statmech.write_ts_input(ts)) def test_write_kinetics_input(self): self.statmech.write_kinetics_input() self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "ts", self.reaction.label, self.reaction.label + ".kinetics.py"))) def test_write_files(self): self.statmech.write_files() for mol in self.reaction.reactants + self.reaction.products: for confs in mol.conformers.values(): conf = confs[0] self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "species", conf.smiles, conf.smiles + ".py"))) self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "ts", self.reaction.label, self.reaction.label + ".py"))) self.assertTrue( os.path.exists( os.path.join(self.statmech.directory, "ts", self.reaction.label, self.reaction.label + ".kinetics.py"))) def test_run(self): self.statmech.write_files() self.statmech.run() self.assertIsInstance(self.statmech.kinetics_job.reaction, RMGReaction) self.assertIsInstance(self.statmech.kinetics_job.reaction.kinetics, Arrhenius) def test_set_results(self): self.test_run() self.statmech.set_results() self.assertTrue( self.reaction.rmg_reaction.isIsomorphic( self.statmech.kinetics_job.reaction)) self.assertTrue( self.statmech.reaction.rmg_reaction.isIsomorphic( self.statmech.kinetics_job.reaction))