def kmc_savedata(kmcof, data): # Molecules and same length molecules = sorted(data[0][2].keys()) len1 = max([len(string) for string in molecules + ["time", "-" * 10]]) ss = "%%%is" % len1 # string for gnuplot data file string = "" string += "# Time in ps; populations in molecules\n" string += "\n\n" # string for gnuplot string gpstr = "datafile='%s'\n" % kmcof gpstr += "plot\\\n" # data for each temperature for ii, (stemp, xvalues, yvalues) in enumerate(data): # add line in gnuplot script gpstr += " datafile index %i using 1:2 with points,\\\n" % ii # add data in gnuplot file string += "# T = %s K\n" % stemp string += "#" + ss % "time" + " ".join([ss % mol for mol in molecules]) + "\n" for jj, time in enumerate(xvalues): time = 1e12 * SECOND * time string += " %10.3E " % time for molecule in molecules: pop = yvalues[molecule][jj] / NA string += " %10.3E " % pop string += "\n" string += "\n\n" # write files write_file(kmcof, string) write_file("plotkmc.gp", gpstr)
def write_alldata(filename, ltemp, dall): variables = [ "gibbs", "pfn", "anh", "zct", "sct", "cvt", "cagtst", "cagcvt", "rcons" ] # write variables in order string = "" for variable in variables: if variable not in dall.keys(): continue string += "#-------------------------#\n" string += "# Adding data for: %6s #\n" % variable string += "#-------------------------#\n" for key, data in sorted(dall[variable].items()): string += "start_%s %s\n" % (variable, key) if variable == "pfn": V0, V1 = data[0:2] data = data[2] string += " %.8f\n" % V0 string += " %.8f\n" % V1 if variable == "gibbs": string += "".join(" %7.2f %.8f\n" % (T, value) for T, value in zip(ltemp, data)) else: string += "".join(" %7.2f %.5E\n" % (T, value) for T, value in zip(ltemp, data)) string += "end_%s\n\n" % variable write_file(filename, string)
def write_frozen(filename,frozen_xcc,frozen_symbols): if len(frozen_symbols) == 0: return string = "" for idx,symbol in enumerate(frozen_symbols): x,y,z = xyz(frozen_xcc,idx) string += " %-2s %+9.5f %+9.5f %+9.5f\n"%(symbol,x,y,z) write_file(filename,string)
def write_chem(dchem,filename=PN.IFILE5): string = PH.INFO_pifchem+"\n" # nice formatting ml1,ml2,ml3,ml4 = 3,3,3,3 for rcname,(Rs,TS,Ps) in dchem.items(): if TS is None: TS = "" l1 = len(rcname) l2 = len(" + ".join(Rs)) l3 = len(TS) l4 = len(" + ".join(Ps)) if l1 > ml1: ml1 = l1 if l2 > ml2: ml2 = l2 if l3 > ml3: ml3 = l3 if l4 > ml4: ml4 = l4 # remove blanks in string string = "\n".join([line.strip() for line in string.split("\n")]) # add to string for label,(Rs,TS,Ps) in sorted(dchem.items()): Rs = " + ".join(Rs) Ps = " + ".join(Ps) if TS is None: TS = " " # nice formating label = ("%%-%is"%ml1)%label Rs = ("%%-%is"%ml2)%Rs TS = ("%%-%is"%ml3)%TS Ps = ("%%-%is"%ml4)%Ps # add to string string += "%s : %s --> %s --> %s\n"%(label,Rs,TS,Ps) string += "\n" write_file(filename,string)
def pilgrim_spc(xcc,symbols,bhessian,mname,eargs): # expand extra-args if len(eargs) == 1: spc_template = eargs[0] folder, clean = None, False elif len(eargs) == 2: spc_template, folder = eargs clean = False elif len(eargs) == 3: spc_template, folder, clean = eargs # no template?? if spc_template is None: raise Exc.NoTemplateGiven(Exception) # create folder if folder is not None: if not os.path.exists(folder): os.mkdir(folder) # Calculate gradient&hessian or just gradient if bhessian: inkey3 = "! EnGrad Freq " else : inkey3 = "! EnGrad " # names of files wname, ifile, ofile, engrad, hess, gbw, err = ITF.iofiles(mname,folder) # Input string_ifile = "" for line in spc_template.split("\n"): if key1 in line: line = "" for idx,symbol in enumerate(symbols): x,y,z = fncs.xyz(xcc,idx) linedata = (symbol,x*pc.ANGSTROM,y*pc.ANGSTROM,z*pc.ANGSTROM) line += "%2s %+11.6f %+11.6f %+11.6f\n"%linedata if key2 in line: pos = line.find(key2) line = line[0:pos] + wname + line[pos+len(key2):] if key3 in line: pos = line.find(key3) line = line[0:pos] + inkey3 + line[pos+len(key3):] # Add \n to line if not line.endswith("\n"): line += "\n" string_ifile += line ff.write_file(ifile,string_ifile) # Send calculation status = ITF.execute(ifile,ofile,err) # # Sleep 0.5 seconds, so Orca can write the files # time.sleep(0.5) # Check output exception = Exc.CalcFails(Exception) exception._var = ofile if not ITF.normal_termination(ofile): raise exception # Read data xcc, atonums, ch, mtp, E, gcc, Fcc, masses, clevel = ff.read_orca(ofile) # Remove files if clean: files = os.listdir(folder) files = [fff for fff in files if fff.startswith(name) ] files = [fff for fff in files if not fff.endswith(".inp") ] files = [fff for fff in files if not fff.endswith(".out") ] files = [fff for fff in files if not fff.endswith(".engrad")] files = [fff for fff in files if not fff.endswith(".hess") ] for fff in files: os.remove(folder+fff) return xcc, atonums, ch, mtp, E, gcc, Fcc, masses
def write_dlevel(ddlevel,filename=PN.IFILE7): string = "" ml = max([len(ctc) for ctc in ddlevel.keys()]+[1]) for key,points in sorted(ddlevel.items()): string += "%%-%is "%ml%key if len(points) != 0: string += "{%s}"%",".join(points) string += "\n" write_file(filename,string)
def write_track(dict_track,filename=PN.IFILE0): if dict_track == {}: string = "# NO DATA INSIDE %s\n"%PN.UFOLDER else: # lengths mlkey = max([len(key) for key in dict_track.keys() ]) mlval = max([len(val) for val in dict_track.values()]) # format of each line lformat = "%%%is --> %%%is\n"%(mlkey,mlval) string = "".join([lformat%(ifile,ffile) for ifile,ffile in sorted(dict_track.items())]) write_file(filename,string)
def write_ctc(dctc,dimasses={},filename=PN.IFILE1): string = "" #------------------------# # write info in dimasses # #------------------------# if dimasses != {}: string += "#------------------------------------------#\n" string += "# Definition of the isotopic masses #\n" string += "#------------------------------------------#\n" string += "start_isomass\n" toloop = sorted([(imass,label) for label,imass in dimasses.items()]) for imass,label in toloop: imass *= AMU string += " %-5s = %12.7f\n"%(label,imass) string += "end_isomass\n" string += "\n" #------------------------# # write info in dctc # #------------------------# string0 = "" string1 = "" stringn = "" for name,ctc in sorted(dctc.items()): string_ctc = "start_ctc %s\n"%name for line in ctc.get_piflines().split("\n"): if line == "": continue string_ctc+= " "+line+"\n" string_ctc += "end_ctc\n\n" if int(ctc._type) == 0: string0 += string_ctc elif int(ctc._type) == 1: string1 += string_ctc else : stringn += string_ctc # Generate string with all data if string0 != "": string += "#------------------------------------------#\n" string += "# Cluster of torsional conformers: MINIMA #\n" string += "#------------------------------------------#\n" string += string0 if string1 != "": string += "#------------------------------------------#\n" string += "# Cluster of torsional conformers: SADDLES #\n" string += "#------------------------------------------#\n" string += string1 if stringn != "": string += "#------------------------------------------#\n" string += "# Cluster of torsional conformers: OTHERS #\n" string += "#------------------------------------------#\n" string += stringn string += PH.INFO_pifstruc write_file(filename,string)
def write_kmc(kmctuple, filename=PN.IFILE6): ipopulations, rateconstants, numbersteps, volume, timeunits, excess = kmctuple # get l1 and l2 for nice formatting try: l1 = max(len(ctc) for ctc in ipopulations.keys()) + 7 except: l1 = 7 try: l2 = max(len(reac) for reac in rateconstants.keys()) + 3 except: l2 = 3 # Write step for printing string = "# KMC Parameters\n" string += "psteps %-5i # print data each nstp steps\n" % numbersteps string += "volume %8.2E # simulation volume (mL)\n" % (volume * ML) if timeunits in "fs,ps,mcs,ms,s,min,hr".split(","): string += "timeunits %-5s # units for time variable\n" % timeunits else: string += "timeunits %-5s # units for time variable\n" % "ps" string += "\n" # Concentration of each species string += "# Initial (non-zero) populations (number of molecules)\n" for specie, ipop in sorted(ipopulations.items()): specie = "pop(%s)_0" % specie string += ("%%-%is %%.2e" % l1) % (specie, ipop) if specie in excess: string += " excess" string += "\n" string += "\n" # Type of rate constant to use string += "# Selection of the rate constant to use\n" for reaction, (rctype, weight, rcdata) in sorted(rateconstants.items()): reaction = "k(%s)" % reaction if weight == 1: string += ("%%-%is %%s " % l2) % (reaction, rctype.lower()) else: string += ("%%-%is*%%i %%s " % l2) % (reaction, weight, rctype.lower()) if rcdata is not None: string += " ".join(["%.3e" % coef for coef in rcdata]) string += "\n" string += "\n" write_file(filename, string)
def write_tes(dtesLL, dtesHL, filename=PN.IFILE4): dstring = {} for case, dtes in [("LL", dtesLL), ("HL", dtesHL)]: # loop for each software for software in sorted(dtes.keys()): # initialize dictionary dstring[software] = dstring.get(software, {}) dstring[software][case] = dstring[software].get(case, "") # block case? if case == "LL": blockkey = "meppoint" if case == "HL": blockkey = "highlevel" # generate string string = "" for ctc in sorted(dtes[software].keys()): string += "#- - - - - - - - - - - - - -#\n" string += "start_%s %s %s\n" % (blockkey, ctc, software) string += dtes[software][ctc] string += "end_%s\n" % blockkey string += "#- - - - - - - - - - - - - -#\n" string += "\n" dstring[software][case] += string # Final string final_string = "" for software in dstring.keys(): final_string += "#===========================%-10s=#\n" % ("=" * 10) final_string += "# INPUT FILE TEMPLATES FOR: %-10s #\n" % ( software.upper()) final_string += "#===========================%-10s=#\n" % ("=" * 10) final_string += "\n" for case in dstring[software].keys(): if case == "LL": final_string += "#-------------------------#\n" final_string += "# MEP POINT CALCULATIONS #\n" final_string += "#-------------------------#\n" if case == "HL": final_string += "#-------------------------#\n" final_string += "# HIGH-LEVEL CALCULATIONS #\n" final_string += "#-------------------------#\n" final_string += "\n" final_string += dstring[software][case] final_string += "\n" write_file(filename, final_string)
def write_alldata(filename,ltemp,dall): variables = ["gibbs1cm3","gibbs1bar","pfn","ctr","anh","zct","sct","cvt","cagtst","cagcvt","cfs","rcons"] # write variables in order string = "" for variable in variables: if variable not in dall.keys(): continue string += "#-------------------------#\n" string += "# Adding data for: %6s #\n"%variable string += "#-------------------------#\n" # add info about variable if variable == "gibbs1cm3": string += "# Gibbs free energy in hartree (ref: 1 molec/cc)\n" if variable == "gibbs1bar": string += "# Gibbs free energy in hartree (ref: 1 bar)\n" if variable == "pfn" : string += "# V0, V0+zpe and partition functions\n" if variable == "ctr" : string += "# transl, rovib and elec partition functions\n" if variable == "anh" : string += "# anharmonicity\n" if variable == "zct" : string += "# ZCT transmission coefficient\n" if variable == "sct" : string += "# SCT transmission coefficient\n" if variable == "cagtst" : string += "# CAG/TST coefficient\n" if variable == "cagcvt" : string += "# CAG/CVT coefficient\n" if variable == "cvt" : string += "# CVT coefficient\n" if variable == "cfs" : string += "# TST correction factors/coefficients\n" if variable == "rcons" : string += "# Rate constants (in au)\n" for key,data in sorted(dall[variable].items()): if data is None: continue string += "start_%s %s\n"%(variable,key) if variable == "pfn": V0,V1 = data[0:2] data = data[2] string += " %.8f\n"%V0 string += " %.8f\n"%V1 string += "".join(" %7.2f %.5E\n"%(T,value) for T,value in zip(ltemp,data)) elif variable == "ctr": PIB,RRHO,ELE = data for idx,T in enumerate(ltemp): string += " %7.2f %.5E %.5E %.5E\n"%(T,PIB[idx],RRHO[idx],ELE[idx]) elif variable in ["gibbs1cm3","gibbs1bar"]: string += "".join(" %7.2f %.8f\n"%(T,value) for T,value in zip(ltemp,data)) else: for T,val in zip(ltemp,data): string += " %7.2f %s\n"%(T,fncs.eformat(val,5)) #string += "".join(" %7.2f %.5E\n"%(T,val) for T,val in zip(ltemp,data)) string += "end_%s\n\n"%variable write_file(filename,string)
def write_highlevelfile(filename,dhighlvl): # if exists, read it before dhighlvl_0 = read_highlevelfile(filename) # update with new for key,value in dhighlvl.items(): # new key if key not in dhighlvl_0.keys(): dhighlvl_0[key] = value continue # old key and "sp" if type(value) == float: dhighlvl_0[key] = value continue # old key and path points for slabel,E in value.items(): dhighlvl_0[key][slabel] = E # save with correct name and del dhighlvl = dhighlvl_0 del dhighlvl_0 # for nice formatting ml = max([len(key) for key in dhighlvl.keys()]) # write string (two parts: one for sp, one for MEP points) string_sp = "" string_path = "" for key,value in sorted(dhighlvl.items()): key = "%%-%is"%ml%key # stationary points if type(value) == float: string_sp += "%s %-6s %-+14.8f\n"%(key,"sp",value) # path points if type(value) == dict: for slabel,E in sorted(value.items()): string_path += "%s %-6s %-+14.8f\n"%(key,slabel,E) string_path += "\n" string = string_sp + "\n" + string_path write_file(filename,string)
def pilgrim_spc(xcc, symbols, bhessian, mname, eargs): # initialize folder = None clean = False oniomH = [] oniomM = [] oniomL = [] frozen = ([], []) # expand extra-args if len(eargs) == 1: spc_template = eargs[0] elif len(eargs) == 2: spc_template, folder = eargs elif len(eargs) == 3: spc_template, folder, clean = eargs elif len(eargs) == 5: spc_template, folder, clean, frozen, oniom_layers = eargs oniomH, oniomM, oniomL = oniom_layers # no template?? if spc_template is None: raise Exc.NoTemplateGiven(Exception) # create folder if folder is not None: if not os.path.exists(folder): os.mkdir(folder) # Calculate gradient&hessian or just gradient if bhessian: inkey3 = "freq=noraman " else: inkey3 = "force " # names of files wname, ifile, ofile, chk, fchk, err = ITF.iofiles(mname, folder) # Input string_ifile = "" for line in spc_template.split("\n"): if key1 in line: line = "" for idx, symbol in enumerate(symbols): x, y, z = fncs.xyz(xcc, idx) if idx in oniomH: layer = " H" elif idx in oniomM: layer = " M" elif idx in oniomL: layer = " L" else: layer = " " linedata = (symbol, x * pc.ANGSTROM, y * pc.ANGSTROM, z * pc.ANGSTROM, layer) line += "%2s %+11.6f %+11.6f %+11.6f %s\n" % linedata # add frozen atoms frozen_symbols, frozen_xcc = frozen for idx, symbol in enumerate(frozen_symbols): x, y, z = fncs.xyz(frozen_xcc, idx) # oniom layer? at = len(symbols) + idx if at in oniomH: layer = " H" elif at in oniomM: layer = " M" elif at in oniomL: layer = " L" else: layer = " " linedata = (symbol, x * pc.ANGSTROM, y * pc.ANGSTROM, z * pc.ANGSTROM, layer) line += "%2s -1 %+11.6f %+11.6f %+11.6f %s\n" % linedata if key2 in line: pos = line.find(key2) line = line[0:pos] + wname + line[pos + len(key2):] if key3 in line: pos = line.find(key3) line = line[0:pos] + inkey3 + line[pos + len(key3):] # Add \n to line if not line.endswith("\n"): line += "\n" string_ifile += line ff.write_file(ifile, string_ifile) # Send calculation status = ITF.execute(ifile, ofile, err) # Check output exception = Exc.CalcFails(Exception) exception._var = ofile if not ITF.normal_termination(ofile): raise exception # (a) chk exists! if os.path.exists(chk): # Generate fchk status = ITF.genfchk(chk, fchk, err) # Read data xcc, atonums, ch, mtp, E, gcc, Fcc, masses, clevel = ff.read_fchk(fchk) # (b) chk does not exist! else: xcc, atonums, ch, mtp, E, gcc, Fcc, masses, clevel = ITF.read_gauout( ofile) # Remove files if clean: files = os.listdir(folder) files = [fff for fff in files if fff.startswith(name)] files = [fff for fff in files if not fff.endswith(".gjf")] files = [fff for fff in files if not fff.endswith(".log")] files = [fff for fff in files if not fff.endswith(".out")] files = [fff for fff in files if not fff.endswith(".chk")] files = [fff for fff in files if not fff.endswith(".fchk")] for fff in files: os.remove(folder + fff) return xcc, atonums, ch, mtp, E, gcc, Fcc, masses
def write_path(dpath,filename=PN.IFILE3): string = "\n" for target,pathvars in sorted(dpath.items()): string += pathvars.string4pif(target) string += "\n" write_file(filename,string)
def write_temp(Tlist,filename=PN.IFILE2): string = "" for idx in range(0,len(Tlist),7): line = " ".join(["%7.2f"%T for T in Tlist[idx:idx+7]]) string += " " + line + "\n" write_file(filename,string)