def read_normal_log(ofile, inpvars, case): # keylvl from inpvars according to case if case == "LL": keylvl = inpvars._keyll elif case == "HL": keylvl = inpvars._keyhl else: raise Exception # reading data exception_line = "ERROR! Level '%s' not found in archive section!\n" % keylvl try: logdata = gau.read_gaussian_log(ofile, target_level=keylvl) except LevelNotFound: print(" " * 11 + exception_line) sys.exit() commands, comment, ch, mtp, symbols, xcc, gcc, Fcc, energy, statusFRQ, ozmat, level = logdata # it may happen that in FRQ calculation zmatrix is not found! try: lzmat, zmatvals, zmatatoms = gau.convert_zmat(ozmat)[0] # put angles in (0,360) (sometimes Gaussian returns an angle > 360) for ic in inpvars._tic: zmatvals[ic] %= 360 # Save info vec = TorPESpoint(tfh.zmat2vec(zmatvals, inpvars._tic), inpvars._tlimit) except: zmatvals = None zmatatoms = None vec = None print(" energy (%s) = %.6f hartree" % (level, energy)) print(" final vector: %s" % str(vec)) if statusFRQ >= 0: print(" num imag frq: %i" % statusFRQ) return statusFRQ, vec, zmatvals, zmatatoms
def log_data(log, inpvars, fscal, folder="./"): if inpvars._ts: fmode = -1 else: fmode = 0 # name from log name = log.split(".")[-2] point = TorPESpoint(name, inpvars._tlimit) # Read log file logdata = gau.read_gaussian_log(folder + log) # prepare data logdata = prepare_log_data(logdata) ch, mtp, V0, symbols, symbols_wo, xcc_wo, gcc_wo, Fcc_wo, lzmat, zmatvals = logdata # string for fccards string_fccards = gau.get_fccards_string(gcc_wo, Fcc_wo) # generate Molecule instance molecule = Molecule() molecule.setvar(xcc=xcc_wo, Fcc=Fcc_wo, V0=V0) molecule.setvar(fscal=fscal) molecule.setvar(symbols=symbols_wo) molecule.setvar(ch=ch, mtp=mtp) molecule.prepare() molecule.setup() molecule.ana_freqs() # Calculate partition functions for the temperatures qtot, V1, qis = molecule.calc_pfns(inpvars._temps, fmode=fmode) qtr, qrot, qvib, qele = qis Qrv = np.array([xx * yy for xx, yy in zip(qrot, qvib)]) # Calculate partition functions at target temperature qtot, V1, qis = molecule.calc_pfns([inpvars._temp], fmode=fmode) # remove rotsigma for Gibbs gibbs = V1 - pc.KB * inpvars._temp * np.log(qtot[0] * molecule._rotsigma) # imaginary frequency if len(molecule._ccimag) == 0: ifreq = None elif len(molecule._ccimag) == 1: ifreq = [ifreq for ifreq, ivec in list(molecule._ccimag)][0] else: ifreq = None # weight for this conformer if inpvars._enantio and molecule._pgroup.lower() == "c1": weight = 2 else: weight = 1 # pack and return data conf_tuple = (point, V0, V1, gibbs, weight, Qrv, ifreq, lzmat, zmatvals, log) return conf_tuple, symbols, string_fccards
def get_logs_vecs(inpvars, dcorr, svec2log={}): # conformers in folder logs = folder_logs(inpvars._dirll) if len(logs) == 0: return svec2log # find equivalent points for log in logs: svec = log.split(".")[1] if svec in svec2log: continue svec2log[svec] = log if inpvars._enantio: # (a) read log zmatlines = gau.read_gaussian_log(inpvars._dirll + log)[10] (lzmat, zmatvals, zmatatoms), symbols = gau.convert_zmat(zmatlines) # (b) enantiomer svec2 = str(enantiovec(lzmat, zmatvals, dcorr, inpvars)) svec2log[svec2] = log # return data return svec2log
def get_imag_freq(log, fscal): try: # Read log file logdata = gau.read_gaussian_log(log) logdata = prepare_log_data(logdata) ch, mtp, V0, symbols, symbols_wo, xcc_wo, gcc_wo, Fcc_wo, lzmat, zmatvals = logdata # diagonalize Fcc molecule = Molecule() molecule.setvar(xcc=xcc_wo, Fcc=Fcc_wo, V0=V0) molecule.setvar(fscal=fscal) molecule.setvar(symbols=symbols_wo) molecule.setvar(ch=ch, mtp=mtp) molecule.prepare() molecule.setup() molecule.ana_freqs() ifreq = molecule._ccimag[0][0] except: ifreq = None # return return ifreq
def gen_guess_zmatrix(vec, i_zmatvals, inpvars, log, distance): # read z-matrix from closest domain use_default = True try: logdata = gau.read_gaussian_log(inpvars._dirll + log) zmatlines = logdata[10] (lzmat, zmatvals, zmatatoms), symbols = gau.convert_zmat(zmatlines) sprint("using Z-matrix from stored file (%s)" % log, tvars.NIBS2 + 4) sprint("distance to stored Z-matrix: %.0f degrees" % distance, tvars.NIBS2 + 4) use_default = False except: use_default = True # get closest z-matrix if use_default: sprint("using default Z-matrix (%s)" % inpvars._zmatfile, tvars.NIBS2 + 4) zmatvals = {k: v for (k, v) in i_zmatvals.items()} # Apply vector to current z-matrix for angle, ic in zip(vec._fvec, inpvars._tic): zmatvals[ic] = angle # return zmatvals return zmatvals
def status_from_log(logfile, inpvars): ''' About statusOPT, it returns: * -1 if the calculation failed * 0 if the calculation succeeded About statusFRQ, it returns: * -1 if the frequencies were not found in output (or fails) * num_imag if the frequencies were found ''' assert os.path.exists(logfile) # Log file does not end with Normal termination? if not gau.normal_termination(logfile): statusOPT = -1 statusFRQ = -1 vec = None # Ends with normal termination else: logdata = gau.read_gaussian_log(logfile) statusOPT = 0 statusFRQ = logdata[9] zmatlines = logdata[10] if zmatlines is None: statusOPT = -1 statusFRQ = -1 vec = None else: (lzmat, zmatvals, zmatatoms), symbols = gau.convert_zmat(zmatlines) # put angles in (0,360) (sometimes Gaussian returns an angle > 360) for ic in inpvars._tic: zmatvals[ic] %= 360 # final vector vec = TorPESpoint(tfh.zmat2vec(zmatvals, inpvars._tic), inpvars._tlimit) # Return data return statusOPT, statusFRQ, vec
# no files? if len(logs) == 0: sprint("There are no log files inside folder '%s'" % folder, NIBS2, 1) return #----------------# # Read log files # #----------------# sprint("Reading log files...", NIBS2, 1) geoms = [] weights = {} strconfs = [] allconfs = [] denantio = {} for log, point in zip(logs, points): logdata = gau.read_gaussian_log(folder + log, target_level=None) ch = logdata[2] mtp = logdata[3] symbols = logdata[4] xcc = logdata[5] gcc = logdata[6] Fcc = logdata[7] V0 = logdata[8] zmat = logdata[10] if zmat is None: zmat = gau.zmat_from_loginp(folder + log) (lzmat, zmatvals, zmatatoms), symbols = gau.convert_zmat(zmat)
def highlevel_reopt(inpvars,cmatrix,\ lowerconformer=0,\ upperconformer=float("inf"),\ calculate=False,\ lNH2=[]): dirll, dirhl = inpvars._dirll , inpvars._dirhl if not os.path.exists(dirll): print(" Folder NOT FOUND: %s\n"%dirll) return # Create folders if not os.path.exists(dirhl): try : mkdir_recursive(dirhl) except: pp.print_createdir(dirhl,tvars.NIBS2); exit() # Check Gaussian software if calculate: end_program = itf.check_executable() if end_program: return # print status of tests pp.print_tests(inpvars._tests,1) pp.print_ifreqconstr(inpvars._ifqrangeHL) #------------------------# # Templates for Gaussian # #------------------------# # HL opt & HL freq templates for MIN if not inpvars._ts: file1,file2 = tvars.TEMPLMINOPTHL,tvars.TEMPLMINFRQHL # HL opt & HL freq templates for TS else : file1,file2 = tvars.TEMPLTSOPTHL,tvars.TEMPLTSFRQHL # (a) do templates exist?? if not os.path.exists(file1): pp.print_filenotfound(file1,tvars.NIBS2,1); raise exc.END if not os.path.exists(file2): pp.print_filenotfound(file2,tvars.NIBS2,1); raise exc.END # (b) read templates with open(file1,'r') as asdf: loptHL = asdf.readlines() pp.print_template(loptHL,file1,"HL optimization",tvars.NIBS2) with open(file2,'r') as asdf: lfrqHL = asdf.readlines() pp.print_template(lfrqHL,file2,"HL frequency calculation",tvars.NIBS2) # (c) temporal folder if not os.path.exists(inpvars._tmphl): try : mkdir_recursive(inpvars._tmphl) except: pp.print_createdir(inpvars._tmphl,tvars.NIBS2); exit() # read LL energies if not os.path.exists(tvars.ENERGYSUMLL): sprint("- file NOT found: %s"%tvars.ENERGYSUMLL,tvars.NIBS2) sprint(" execute %s using --msho ll"%tvars.PROGNAMEnopy,tvars.NIBS2) sprint("") return sprint("- reading file: %s"%tvars.ENERGYSUMLL,tvars.NIBS2) sprint("") llenergies, lltemp = rw.read_llenergies() # check temperature if abs(lltemp-inpvars._temp) > 1e-3: sprint(" something went wrong!",tvars.NIBS2) sprint("") sprint(" temperature in file : %.3f K"%(lltemp),tvars.NIBS2) sprint(" temperature in %s: %.3f K"%(tvars.IFILE,inpvars._temp),tvars.NIBS2) sprint("") return sprint(" temperature for Gibbs free energy: %.3f K"%lltemp,tvars.NIBS2) # check number of conformers logs = [fname for fname in os.listdir(inpvars._dirll) if fname.endswith(".log")] if len(logs) != len(llenergies): sprint(" something went wrong!",tvars.NIBS2) sprint(" number of conformer differs!",tvars.NIBS2) sprint("") sprint(" # of conformers in file : %i"%len(llenergies),tvars.NIBS2) sprint(" # of conformers in LL folder: %i"%len(logs),tvars.NIBS2) sprint("") return sprint(" number of low-level conformers : %i"%len(llenergies),tvars.NIBS2) sprint("") # check conformers svecs_llenergies = [svec for count,V0,G,svec in llenergies] for log in logs: svec_i = log.split(".")[1] if svec_i not in svecs_llenergies: sprint(" something went wrong!",tvars.NIBS2) sprint("") sprint(" conformer NOT included: %s"%log,tvars.NIBS2) sprint("") return #---------------------# # Create HL gjf files # #---------------------# nopt = 0 nfrq = 0 nsp = 0 minV0 = llenergies[0][0] for count,relV0_kcalmol,relG_kcalmol,svec in llenergies: if not (lowerconformer <= count <= upperconformer): continue # read LL/HL-correlation file correlations = rw.read_llhlcorr() # LL log file log_prec = "prec.%s.log"%svec log_stoc = "stoc.%s.log"%svec if os.path.exists(inpvars._dirll+log_prec): log = log_prec prefix1 = "optprec." prefix2 = "frqprec." prefix3 = "prec." elif os.path.exists(inpvars._dirll+log_stoc): log = log_stoc prefix1 = "optstoc." prefix2 = "frqstoc." prefix3 = "stoc." else: raise Exception # read log file conftuple,symbols,string_fccards = itf.log_data(log,inpvars,inpvars._freqscalLL,dirll) # unpack conftuple vecA,V0,V1,G,weight,Qrv,ifreq,lzmat,zmatvals,log = conftuple sprint("(%i) %s [%s]"%(count,str(vecA),prefix3[:-1]),tvars.NIBS2) sprint("") # ONLY CONFORMERS IN ENERGY WINDOWS sprint("LL electronic energy: %.4f kcal/mol"%relV0_kcalmol,tvars.NIBS2+4) # ONLY CONFORMERS BELOW HLCUTOFF IN GIBBS if inpvars._hlcutoff is not None: relG = (G-minG)*KCALMOL sprint("Gibbs energy: %.3f kcal/mol"%relG,tvars.NIBS2+4) if relG > inpvars._hlcutoff: sprint("hlcutoff : %.3f kcal/mol"%inpvars._hlcutoff,tvars.NIBS2+4) sprint("HL optimization will not be carried out!",tvars.NIBS2+4) sprint() continue sprint() # SKIP IF ALREADY CALCULATED if str(vecA) in correlations.keys(): vecB = correlations[str(vecA)] # possible names for log files saved_log1 = inpvars._dirhl+"prec.%s.log"%str(vecB) saved_log2 = inpvars._dirhl+"stoc.%s.log"%str(vecB) # check existence bool1 = os.path.exists(saved_log1) bool2 = os.path.exists(saved_log2) # exists? then skip if bool1 or bool2: sprint("already in %s"%inpvars._dirhl,tvars.NIBS2+4) sprint("optimized conformer: %s"%str(vecB),tvars.NIBS2+4) sprint() continue sprint() #-----------------# # OPT CALCULATION # #-----------------# # Perform HL optimization data = (loptHL,prefix1+str(vecA),inpvars,lzmat,zmatvals,"HL",string_fccards) if calculate: sprint("optimization...",tvars.NIBS2+4) # execute Gaussian ofileOPT,statusOPT,dummy,vecB,zmatvals,zmatatoms = itf.execute(*data) sprint() # opt ends ok? if statusOPT == -1: continue else: sprint("optimization file:",tvars.NIBS2+4) generated, ifile, ofile, err = itf.generate_gjf(*data) sprint("- %s"%ifile,tvars.NIBS2+8) if generated: sprint("- generated!",tvars.NIBS2+8,1) nopt += 1 continue sprint("- not generated!",tvars.NIBS2+8,1) logdata = gau.read_gaussian_log(ofile) commands,comment,ch,mtp,symbols,xcc,gcc,Fcc,energy,statusFRQ,ozmat,level = logdata lzmat,zmatvals,zmatatoms = gau.convert_zmat(ozmat)[0] for ic in inpvars._tic: zmatvals[ic] %= 360 vecB = TorPESpoint(tfh.zmat2vec(zmatvals,inpvars._tic),inpvars._tlimit) # The optimization succedeed! sprint("analysing geometry...",tvars.NIBS2+4) sprint("- optimized point: %s"%str(vecB),tvars.NIBS+8,1) # NH2 inversion zmatvals,inversion = tfh.correct_NH2_inversion(lzmat,zmatvals,zmatatoms,lNH2) if inversion: svecB1 = str(vecB) vecB = TorPESpoint(tfh.zmat2vec(zmatvals,inpvars._tic),inpvars._tlimit) svecB2 = str(vecB) if svecB1 != svecB2: sprint("- NH2 inversion detected! Exchanging H atoms...",tvars.NIBS+8) sprint("- final vector: %s"%svecB2,tvars.NIBS+8,1) bools = tfh.precheck_geom(lzmat,zmatvals,cmatrix,inpvars) if (inpvars._tests[1][0] == 1) and not bools[0]: sprint("- connectivity test is negative!",tvars.NIBS+8,1) continue if (inpvars._tests[1][1] == 1) and not bools[1]: sprint("- torsion angle out of domain!",tvars.NIBS+8,1) continue if (inpvars._tests[1][2] == 1) and not bools[2]: sprint("- hard constraint test is negative!",tvars.NIBS+8,1) continue if (inpvars._tests[1][3] == 1) and not bools[3]: sprint("- soft constraint test is negative!",tvars.NIBS+8,1) continue # Already saved? pointsHL = tfh.folder_points(inpvars._dirhl) inlist, equalto = vecB.is_in_list(pointsHL,inpvars._epsdeg) if inlist: # exists? sprint("already in %s (%s)"%(inpvars._dirhl,str(equalto)),tvars.NIBS2+4) sprint() # rename stoc --> prec if required! for ext in "log,zmat".split(","): # possible names for log files saved_log1 = inpvars._dirhl+"prec.%s.%s"%(str(equalto),ext) saved_log2 = inpvars._dirhl+"stoc.%s.%s"%(str(equalto),ext) # check existence bool1 = os.path.exists(saved_log1) bool2 = os.path.exists(saved_log2) # if this is prec and the one obtained is stoc, replace name!! if bool2 and "prec" in prefix3: move(saved_log2,saved_log1) # save LL-->HL correlation correlations[str(vecA)] = str(vecB) rw.add_llhlcorr(str(vecA),str(vecB)) continue sprint() #-----------------# # FRQ CALCULATION # #-----------------# data = (lfrqHL,prefix2+str(vecA),inpvars,lzmat,zmatvals,"HL") if not calculate: sprint("frequency file:",tvars.NIBS2+4) generated, ifile, ofile, err = itf.generate_gjf(*data) sprint("- %s"%ifile,tvars.NIBS2+8) if generated: sprint("- generated!\n",tvars.NIBS2+8,1) nfrq += 1 continue # log with normal termination! else: sprint("- not generated!\n",tvars.NIBS2+8,1) statusFRQ,vecB_frq,dummy1,dummy2 = itf.read_normal_log(ofile,inpvars,"HL") ofileFRQ = ofile # Perform HL frequency else: sprint("frequency calculation...",tvars.NIBS2+4) ofileFRQ,dummy,statusFRQ,vecB_frq,dummy,dummy = itf.execute(*data) #============# # Save files # #============# bool1 = (statusFRQ == 0 and not inpvars._ts) bool2 = (statusFRQ == 1 and inpvars._ts) # check imag freq if bool2 and inpvars._ifqrangeHL != []: ifreq = abs( fncs.afreq2cm(itf.get_imag_freq(ofileFRQ,inpvars._freqscalHL)) ) isok = fncs.float_in_domain(ifreq,inpvars._ifqrangeHL) if not isok: sprint("imaginary frequency is: %.2fi cm^-1"%ifreq,tvars.NIBS2+4) bool2 = False if bool1 or bool2: nsp += 1 # create z-matrix file dst = inpvars._dirhl+prefix3+"%s.zmat"%str(vecB) write_zmat(dst,lzmat, zmatvals) # copy frq file src = ofileFRQ dst = inpvars._dirhl+prefix3+"%s.log"%str(vecB) if not os.path.exists(dst): copyfile(src, dst) # save LL-->HL correlation correlations[str(vecA)] = str(vecB) rw.add_llhlcorr(str(vecA),str(vecB)) sprint() # Print number of calculations pp.print_numcalcs(count,nopt,nfrq,nsp) if not calculate: sprint("Number of opt gjf files generated: %i"%nopt,tvars.NIBS2) sprint("Number of frq gjf files generated: %i"%nfrq,tvars.NIBS2) sprint() else: # rewrite correlations to remove repetitions correlations = rw.read_llhlcorr() rw.write_llhlcorr(correlations)
def fccards_from_log(log): gcc, Fcc = gau.read_gaussian_log(log)[6:8] return gau.get_fccards_string(gcc, Fcc)