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 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))