def get_smiles_path(x, mult=0, db= 'database'): """ Returns a smiles based path for database directory. Note that smiles strings are not unique. Even the canonical smiles strings are unique only for the same code that generates the smiles string. """ import iotools as io if type(x) is pybel.Molecule: if mult == 0: mult = x.spin s = x.write(format='can').strip().split()[0] s = s + '_m' + str(mult) elif type(x) is str: if '_m' in x: s = x else: s = get_smiles(x) mult = get_mult(s) s = s + '_m' + str(mult) formula = get_formula(s) # formula_noH = get_formula(s, stoichemetry=True, hydrogens=False) # elements_noH = get_formula(s, stoichemetry=False, hydrogens=False) s = get_smiles_filename(s) # dirs = db, elements_noH, formula_noH, formula, s dirs = db, formula, s return io.join_path(*dirs)
def run_extrapolation(s, parameters): lines = io.read_file(parameters['qctemplate'],aslines=True) smilesname = ob.get_smiles_filename(s) filename = smilesname + '_cbs.ene' qcdir = parameters['qcdirectory'] directories = [] msg = '' for line in lines: if 'directories=' in line: exec(line) ndir = len(directories) energies=[0.]*ndir for i, edir in enumerate(directories): efile = io.join_path(*[edir,smilesname+'.ene']) if io.check_file(efile,verbose=True): energies[i] = float(io.read_file(efile, aslines=False)) print('Reading energy from {0} = {1}'.format(edir,energies[i])) for line in lines: if 'energy=' in line: energy = 0 exec(line) print('Extrapolation based on formula: {0}'.format(line)) print('Extrapolated energy = {0}'.format(energy)) if 'filename=' in line: exec(line) if len(directories) < 1: print('You have to specifies directories as a list in the template file') if energy: msg += 'Extrapolation successful' if parameters['writefiles']: if qcdir: filename = io.join_path(*[qcdir,filename]) io.write_file(str(energy), filename) msg += 'Extrapolation enegy file {0}'.format(filename) else: msg += 'Extrapolation failed' return msg
def get_unique_path(x, mult=0, method=''): """ Returns a portable unique path based on inchikey for database directory. >>> import os >>> if os.path.sep == '/': print get_unique_path('C',method='pm6') database/C/C/CH4/VNWKTOKETHGBQD-UHFFFAOYSA-N1/pm6 """ import iotools as io mol = get_mol(x) if mult == 0: mult = mol.spin formula = get_formula(mol) formula_noH = get_formula(mol, stoichemetry=True, hydrogens=False) elements_noH = get_formula(mol, stoichemetry=False, hydrogens=False) uniquekey = get_inchi_key(mol, mult) dirs = 'database', elements_noH, formula_noH, formula, uniquekey, method return io.join_path(*dirs)
def get_smiles_path(x, mult=0, method='',basis='',geopath=''): """ Returns a smiles based path for database directory. Note that smiles strings are not unique. Even the canonical smiles strings are unique only for the same code that generates the smiles string. """ import iotools as io mol = get_mol(x) if mult == 0: mult = mol.spin s = get_smiles_filename(mol) if mult > 1: s = s + "-m{0}".format(mult) formula = get_formula(mol) formula_noH = get_formula(mol, stoichemetry=True, hydrogens=False) elements_noH = get_formula(mol, stoichemetry=False, hydrogens=False) dirs = 'database', elements_noH, formula_noH, formula, s,geopath, method, basis return io.join_path(*dirs)
def run(s): """ A driver function to run quantum chemistry and thermochemistry calculations based on command line options: --qcmethod --qcpackage """ global parameters runqc = parameters['runqc'] parseqc = parameters['parseqc'] runthermo = parameters['runthermo'] runanharmonic = parameters['anharmonic'] msg = "***************************************\n" msg += "{0}\n".format(s) mult = ob.get_mult(s) mol = ob.get_mol(s) smilesname = ob.get_smiles_filename(s) smilesdir = ob.get_smiles_path(mol, mult, method='', basis='', geopath='') qcdirectory = io.join_path(*[smilesdir, parameters['qcdirectory']]) qctemplate = io.get_path(parameters['qctemplate']) qcpackage = parameters['qcpackage'] qcscript = io.get_path(parameters['qcscript']) qclog = smilesname + '_' + qcpackage + '.out' xyzpath = parameters['xyzpath'] xyzfile = None if xyzpath: if io.check_file(xyzpath): xyzfile = xyzpath elif io.check_file(io.join_path(*(smilesdir, xyzpath))): xyzfile = io.join_path(*(smilesdir, xyzpath)) elif io.check_dir(xyzpath): try: xyzfile = next(io.find_files(xyzpath, '*.xyz')) except StopIteration: msg += "xyz file not found in {0}".format(xyzpath) elif io.check_dir(io.join_path(*(smilesdir, xyzpath))): xyzpath = io.join_path(*(smilesdir, xyzpath)) try: xyzfile = next(io.find_files(xyzpath, '*.xyz')) except StopIteration: msg += "xyz file not found in {0}".format(xyzpath) else: msg += "xyz path not found {0}".format(xyzpath) return msg if xyzfile: msg += "Using xyz file in '{0}'\n".format(xyzfile) xyz = io.read_file(xyzfile) coords = ob.get_coordinates_array(xyz) mol = ob.set_xyz(mol, coords) print(msg) msg = '' io.mkdir(qcdirectory) cwd = io.pwd() if io.check_dir(qcdirectory, 1): io.cd(qcdirectory) msg += "cd '{0}'\n".format(qcdirectory) else: msg += ('I/O error, {0} directory not found.\n'.format(qcdirectory)) return -1 print(msg) msg = '' available_packages = [ 'nwchem', 'molpro', 'mopac', 'gaussian', 'extrapolation' ] if runqc: if qcpackage in available_packages: print('Running {0}'.format(qcpackage)) msg += qc.run(s, qctemplate, parameters, mult) elif qcpackage == 'qcscript': msg += "Running qcscript...\n" geofile = smilesname + '.geo' geo = ob.get_geo(mol) io.write_file(geo, geofile) if io.check_file(geofile, 1): msg += qc.run_qcscript(qcscript, qctemplate, geofile, mult) else: msg = '{0} package not implemented\n'.format(qcpackage) msg += 'Available packages are {0}'.format(available_packages) exit(msg) print(msg) msg = '' if parseqc: if io.check_file(qclog, timeout=1, verbose=False): out = io.read_file(qclog, aslines=False) d = qc.parse_output(out, smilesname, parameters['writefiles']) pprint(d) if runthermo: groupstext = tc.get_new_groups() io.write_file(groupstext, 'new.groups') msg += "Parsing qc logfile '{0}'\n".format(io.get_path(qclog)) newmsg, xyz, freqs, zpe, deltaH, afreqs, xmat = qc.parse_qclog( qclog, qcpackage, anharmonic=runanharmonic) msg += newmsg if xyz is not None: msg += "Optimized xyz in Angstroms:\n{0} \n".format(xyz) else: runthermo = False if freqs is not None: msg += "Harmonic frequencies in cm-1:\n {0} \n".format(freqs) else: runthermo = False if afreqs: msg += "Anharmonic frequencies in cm-1:\n {0}\n".format(afreqs) else: runanharmonic = False if zpe: msg += 'ZPE = {0} kcal/mol\n'.format(zpe) else: runthermo = False if deltaH is not None: msg += 'deltaH = {0} kcal/mol\n'.format(deltaH) else: runthermo = False if xmat is not None: msg += 'Xmat = {0} kcal/mol\n'.format(xmat) else: runanharmonic = False if runthermo: msg += tc.write_chemkin_polynomial(mol, zpe, xyz, freqs, deltaH, parameters) io.cd(cwd) print(msg) return