def test_to_from_dict(self): d = self.mol.to_dict mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule) propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, charge=1, site_properties={"magmom": [0.5, -0.5, 1, 2, 3]} ) d = propertied_mol.to_dict self.assertEqual(d["sites"][0]["properties"]["magmom"], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") self.assertEqual(mol.charge, 1)
def test_to_from_file_string(self): for fmt in ["xyz", "json", "g03", "yaml"]: s = self.mol.to(fmt=fmt) self.assertIsNotNone(s) m = IMolecule.from_str(s, fmt=fmt) self.assertEqual(m, self.mol) self.assertIsInstance(m, IMolecule) self.mol.to(filename="CH4_testing.xyz") self.assertTrue(os.path.exists("CH4_testing.xyz")) os.remove("CH4_testing.xyz") self.mol.to(filename="CH4_testing.yaml") self.assertTrue(os.path.exists("CH4_testing.yaml")) mol = Molecule.from_file("CH4_testing.yaml") self.assertEqual(self.mol, mol) os.remove("CH4_testing.yaml")
def test_to_from_dict(self): d = self.mol.to_dict mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule) propertied_mol = Molecule( ["C", "H", "H", "H", "H"], self.coords, charge=1, site_properties={'magmom': [0.5, -0.5, 1, 2, 3]}) d = propertied_mol.to_dict self.assertEqual(d['sites'][0]['properties']['magmom'], 0.5) mol = Molecule.from_dict(d) self.assertEqual(propertied_mol, mol) self.assertEqual(mol[0].magmom, 0.5) self.assertEqual(mol.formula, "H4 C1") self.assertEqual(mol.charge, 1)
def make_json(mol_dir, dihed_atoms_file, json_file): """ For a molecule rotation directory, mol_dir, this finds the energy, xyz, and degree of rotation and places those into json_file. """ mol_name = str(mol_dir.split('/')[-1]) try: log_fn = [x for x in os.listdir(dpath) if x.endswith('deg.log')][0] log_path = os.path.join(dpath, log_fn) except IndexError: print("Error. No log file for {} at {} degrees.".format( mol_name, degree)) return None if check_normal_optimization(log_path): mol = GaussianOutput(log_path) num_electrons = mol.electrons[0] eigens = list(mol.eigenvalues.values())[0] h**o = eigens[num_electrons - 1] * 27.2114 lumo = eigens[num_electrons] * 27.2114 homo_lumo_gap = lumo - h**o energy = mol.final_energy * 27.2114 imol = IMolecule.from_file(log_path) with open(dihed_atoms_file, 'r') as lines: data = lines.readlines() dihedral1 = data[0].split(',')[0] d1atom1_num = int(dihedral1.split()[0]) d1atom2_num = int(dihedral1.split()[1]) d1atom3_num = int(dihedral1.split()[2]) d1atom4_num = int(dihedral1.split()[3]) di_angle = imol.get_dihedral(d1atom1_num, d1atom2_num, d1atom3_num, d1atom4_num) json_data = { "molecule_name": mol_name, "dihedral_angle": di_angle, "energy": energy, "h**o": h**o, "lumo": lumo, "homo_lumo_gap": homo_lumo_gap } write_json(json_data, json_file) print("Data collected for {}".format(mol_name)) else: normal = 0 print("Error. GeomOpt calculation did not properly terminate for {}". format(mol_name))
def test_prop(self): self.assertEqual(self.mol.charge, 0) self.assertEqual(self.mol.spin_multiplicity, 1) self.assertEqual(self.mol.nelectrons, 10) self.assertArrayAlmostEqual(self.mol.center_of_mass, [0, 0, 0]) self.assertRaises(ValueError, Molecule, ["C", "H", "H", "H", "H"], self.coords, charge=1, spin_multiplicity=1) mol = Molecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertEqual(mol.spin_multiplicity, 2) self.assertEqual(mol.nelectrons, 9) #Triplet O2 mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) self.assertEqual(mol.spin_multiplicity, 3)
def test_get_centered_molecule(self): mol = IMolecule(["O"] * 2, [[0, 0, 0], [0, 0, 1.2]], spin_multiplicity=3) centered = mol.get_centered_molecule() self.assertArrayAlmostEqual(centered.center_of_mass, [0, 0, 0])
def test_equal(self): mol = IMolecule(["C", "H", "H", "H", "H"], self.coords, charge=1) self.assertNotEqual(mol, self.mol)
def make_json(mol_dir, json_file, omega_file=None): """ For a molecule directory, mol_dir, this finds the molecule_name, smiles, and tuned omega for the polymer. It then finds the optimized structures, energies, homos, lumos, and runtimes for each degree of rotation and places those into a json_file. """ mol_name = mol_dir.split('/')[-1] xyz_dict, energy_dict, homo_dict, lumo_dict, runtime_dict = {}, {}, {}, {}, {} mol_smiles, omega = None, None if os.path.isfile(omega_file): with open(omega_file, 'r') as fn: try: omega_data = fn.readlines()[-1].split()[1] omega = "0{}".format(omega_data.split('.')[1]) except IndexError: print('Error. wtuning for {} may not have finished'.format( mol_name)) return None deg_folders = [ deg for deg in os.listdir(mol_dir) if os.path.isdir(os.path.join(mol_dir, deg)) ] for deg in deg_folders: dpath = os.path.join(mol_dir, deg) if os.path.isdir(dpath): degree = str((deg.split('_')[-1])[:-3]) try: log_fn = [ x for x in os.listdir(dpath) if x.endswith('deg.log') ][0] log_path = os.path.join(dpath, log_fn) except IndexError: print("Error. No log file for {} at {} degrees.".format( mol_name, degree)) continue if check_normal_optimization(log_path): runtime = runtime_from_log(log_path) mol = IMolecule.from_file(log_path) mol_xyz = mol.to(fmt="xyz") out_mol = GaussianOutput(log_path) num_electrons = out_mol.electrons[0] eigens = list(out_mol.eigenvalues.values())[0] h**o = eigens[num_electrons - 1] * 27.2114 lumo = eigens[num_electrons] * 27.2114 energy = out_mol.final_energy * 27.2114 if mol_smiles is None: mol_smiles = pmgmol_to_rdmol(mol)[1] else: print( "Error. Optimization did not properly terminate for {} at {} degrees." .format(mol_name, degree)) continue xyz_dict[degree] = mol_xyz energy_dict[degree] = energy homo_dict[degree] = h**o lumo_dict[degree] = lumo runtime_dict[degree] = runtime json_data = { # Polymer data "molecule_name": mol_name, "smiles": mol_smiles, "tuned_omega": omega, # Rotation dictionaries "structures": xyz_dict, "energies": energy_dict, "homos": homo_dict, "lumos": lumo_dict, "runtimes": runtime_dict } write_json(json_data, json_file) print("Data collected for {}".format(mol_name))
def test_to_from_dict(self): d = self.mol.to_dict mol2 = IMolecule.from_dict(d) self.assertEqual(type(mol2), IMolecule)