def write_molden(filename, xcc, symbs, freqs, evecs): '''evecs NOT in mass-scaled!''' natoms = len(symbs) nfreqs = len(freqs) STRING = "[Molden Format]\n" STRING += "[FR-COORD] # Coordinates in bohr\n" for at in range(natoms): symbol = symbs[at] x, y, z = fncs.xyz(xcc, at) STRING += " %2s %+11.6f %+11.6f %+11.6f \n" % (symbol, x, y, z) if freqs not in [None, []]: STRING += "[FREQ] # Frequencies in cm^-1\n" for idx in range(nfreqs): freq = fncs.afreq2cm(freqs[idx]) STRING += " %9.4f\n" % freq if evecs not in [None, []]: STRING += "[FR-NORM-COORD] # Displacements in bohr\n" for idx in range(nfreqs): STRING += "vibration %i\n" % (idx + 1) evec = evecs[idx] for at in range(natoms): # It may fail with frozen atoms... try: vx, vy, vz = fncs.xyz(evec, at) except: vx, vy, vz = 0.0, 0.0, 0.0 STRING += " %+9.3f %+9.3f %+9.3f\n" % (vx, vy, vz) with open(filename, 'w') as asdf: asdf.write(STRING)
def rst2xyz(rst, xyz, onlyhess=True, Eref=None): tpath, tcommon, drst = read_rst(rst) thelist = [(data[0], label) for (label, data) in drst.items()] thelist.sort() (ch, mtp, atonums, masses, mu) = tcommon symbols = fncs.atonums2symbols(atonums) natoms = len(symbols) string = "" for s_i, label in thelist: s_i, E_i, xms_i, gms_i, Fms_i, v0_i, v1_i, t_i = drst[label] if onlyhess and Fms_i is None: continue if Eref is not None: E_i = (E_i - Eref) * KCALMOL energy = "%+.4f kcal/mol" % E_i else: energy = "%+.6f hartree" % E_i string += " %i\n" % natoms string += " * E = %s ; s = %7.4f (%s)\n" % (energy, s_i, label) xcc_i = fncs.ms2cc_x(xms_i, masses, mu) for idx, symbol in enumerate(symbols): x, y, z = fncs.xyz(xcc_i, idx) x *= ANGSTROM y *= ANGSTROM z *= ANGSTROM string += " %2s %+12.8f %+12.8f %+12.8f\n" % (symbol, x, y, z) with open(xyz, 'w') as asdf: asdf.write(string)
def test_hsconstraints(lzmat, zmatvals, constr, which="hard"): if constr is None or len(constr) == 0: return True # the xcc xcc = None # check constraints for ic, icdomain in constr: if ic in zmatvals: icvalue = zmatvals[ic] if icvalue < 0.0: icvalue = icvalue % 360 else: # Get atoms icatoms = [int(at) - 1 for at in ic.split("-")] nicatoms = len(icatoms) # Get xcc for each atom if xcc is None: xcc = intl.zmat2xcc(lzmat, zmatvals) xyzs = [fncs.xyz(xcc, at) for at in icatoms] # calculate value in xcc if nicatoms == 2: icvalue = fncs.distance(*xyzs) * ANGSTROM elif nicatoms == 3: icvalue = np.rad2deg(fncs.angle(*xyzs)) % 360 elif nicatoms == 4: icvalue = np.rad2deg(fncs.dihedral(*xyzs)) % 360 else: raise Exception # in domain? boolean = fncs.float_in_domain(icvalue, icdomain) if which == "hard" and boolean is False: return False if which == "soft" and boolean is True: return True # (hard) all of them are fulfilled if which == "hard": return True # (soft) all of them failed if which == "soft": return False
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 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 puckering_rjs(xvec,atoms_in_ring): ''' JACS 1975, 97:6, 1354-1358; eq (4) ''' gc = np.array(fncs.get_centroid(xvec,atoms_in_ring)) list_Rj = [] for at in atoms_in_ring: rj = np.array(fncs.xyz(xvec,at)) Rj = rj-gc list_Rj.append(Rj) return list_Rj, gc
def string4xyzfile(xcc,symbols,info=""): string = "%i\n"%len(symbols) string += info if not info.endswith("\n"): string += "\n" for at,symbol in enumerate(symbols): x,y,z = fncs.xyz(xcc,at) x *= pc.ANGSTROM y *= pc.ANGSTROM z *= pc.ANGSTROM string += "%2s %+12.8f %+12.8f %+12.8f\n"%(symbol,x,y,z) return string
def create_xyz(xcc,symbols,inpvars): xyzfile = inpvars._zmatfile+".xyz" print(tvars.IBS+" - xyz file: %s"%xyzfile) if not os.path.exists(xyzfile): string = "%i\nxyz file from %s\n"%(len(symbols),inpvars._zmatfile) for at,symbol in enumerate(symbols): x,y,z = fncs.xyz(xcc,at) x *= pc.ANGSTROM y *= pc.ANGSTROM z *= pc.ANGSTROM string += "%2s %+12.8f %+12.8f %+12.8f\n"%(symbol,x,y,z) with open(xyzfile,'w') as asdf: asdf.write(string)
def write_molden_allconfs(dataconfs,Eref,allsymbols,folder): print(tvars.IBS2+"Generating xyz file with all geometries...") print(tvars.IBS2+" filename: %s"%(folder+tvars.ALLCONFS)) string = "" for idx,conftuple in enumerate(dataconfs): vec,V0,V1,G,weight,Qrv,ifreq,zmat,zmatvals,log = conftuple # convert to xyz V0_rel = (V0-Eref)*pc.KCALMOL description = " * E = %+7.3f kcal/mol ; (%i) %s"%(V0_rel,idx+1,str(vec)) symbols = [symbol for symbol in allsymbols if symbol.upper() not in ("X","XX")] natoms = len(symbols) xcc = intl.zmat2xcc(zmat,zmatvals) # remove dummy atoms (if there is any) xcc = fncs.flatten_llist( [fncs.xyz(xcc,idx) for idx,symbol in enumerate(allsymbols) \ if symbol.upper() not in ("X","XX")]) # add to string string += string4xyzfile(xcc,symbols,info=description) with open(folder+tvars.ALLCONFS,'w') as asdf: asdf.write(string) print("")
def info_string(self, ib=0): root_mass = sum(fncs.symbols2masses(self._symbols)) string = "Molecular formula : %s\n" % self._mform string += "Number of atoms : %i\n" % self._natoms string += "Number of electrons : %i\n" % self._nel string += "Vibrational DOFs : %i\n" % self._nvdof string += "Charge : %i\n" % self._ch string += "Multiplicity : %i\n" % self._mtp string += "Electronic energy (V0): %.8f hartree\n" % self._V0 string += "Total mass [root] : %.4f amu\n" % (root_mass * AMU) string += "Total mass : %.4f amu\n" % (self._mass * AMU) if self._pgroup is not None: string += "Point group symmetry : %s\n" % (self._pgroup) if self._rotsigma is not None: string += "Rotational sym num : %i\n" % (self._rotsigma) string += "Cartesian coordinates (Angstrom):\n" for at, symbol in enumerate(self._symbols): mass = self._masses[at] * AMU x, y, z = fncs.xyz(self._xcc, at) x *= ANGSTROM y *= ANGSTROM z *= ANGSTROM string += " %2s %+12.8f %+12.8f %+12.8f [%7.3f amu]\n" % ( symbol, x, y, z, mass) try: str2 = "Moments and product of inertia (au):\n" if len(self._imoms) == 1: str2 += " %+10.3E\n" % self._imoms[0] if len(self._imoms) == 3: prodinert = self._imoms[0] * self._imoms[1] * self._imoms[2] dataline = (self._imoms[0], self._imoms[1], self._imoms[2], prodinert) str2 += " %+10.3E %+10.3E %+10.3E [%10.3E]\n" % dataline string += str2 except: pass try: str2 = "Vibrational frequencies [1/cm] (scaled by %.3f):\n" % self._fscal for idx in range(0, len(self._ccfreqs), 6): str2 += " %s\n"%(" ".join("%8.2f"%fncs.afreq2cm(freq) \ for freq in self._ccfreqs[idx:idx+6])) if len(self._ccfreqs) != 0: string += str2 except: pass try: str2 = "Vibrational zero-point energies [kcal/mol]:\n" for idx in range(0, len(self._cczpes), 6): str2 += " %s\n"%(" ".join("%8.2f"%(zpe*KCALMOL) \ for zpe in self._cczpes[idx:idx+6])) zpe_au = self._cczpe zpe_kcal = self._cczpe * KCALMOL zpe_eV = self._cczpe * EV zpe_cm = self._cczpe * H2CM str2 += "Vibrational zero-point energy: %+14.8f hartree = \n" % zpe_au str2 += " %+14.2f kcal/mol = \n" % zpe_kcal str2 += " %+14.2f eV = \n" % zpe_eV str2 += " %+14.2f cm^-1 \n" % zpe_cm str2 += "V0 + zero-point energy (V1) : %+14.8f hartree\n" % self._ccV1 if self._cczpe != 0.0: string += str2 except: pass # add blank spaces string = "\n".join([" " * ib + line for line in string.split("\n")]) return 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_gtsfile(xcc, atonums, ch, mtp, E, pgroup, rotsigma, gcc, Fcc, gtsfile, freqs=None, level=""): if gcc is None: gcc = [] if Fcc is None: Fcc = [] if freqs is None: freqs = [] nat = len(atonums) str_gts = "" # write level of calculation if level != "": str_gts += "# level: %s\n" % level # Write basic data str_gts += "# Charge, multiplicity, energy [hartree],\n" str_gts += "# point group and rotational symmetry number\n" str_gts += "start_basic\n" str_gts += " charge %i\n" % ch str_gts += " multiplicity %i\n" % mtp str_gts += " energy %-+16.8f # Total energy in hartree\n" % E str_gts += " pointgroup %-5s # Point group\n" % pgroup str_gts += " rotsigma %-5i # Rotational sigma\n" % rotsigma str_gts += "end_basic\n\n" # Write atomic numbers and cartesian coordinates str_gts += "# Atomic number and non-scaled cartesian coordinates [bohr]\n" str_gts += "start_cc\n" for at in range(nat): atonum = atonums[at] xx, yy, zz = fncs.xyz(xcc, at) str_gts += " %03i %+15.8E %+15.8E %+15.8E\n" % (atonum, xx, yy, zz) str_gts += "end_cc\n\n" # Write cartesian gradiend if len(gcc) != 0: str_gts += "# Non-scaled cartesian gradient [hartree/bohr]\n" str_gts += "start_grad\n" for at in range(nat): gxx, gyy, gzz = fncs.xyz(gcc, at) str_gts += " %+15.8E %+15.8E %+15.8E\n" % (gxx, gyy, gzz) str_gts += "end_grad\n\n" # Write force constant matrix (i.e. hessian matrix) if len(Fcc) != 0: # matrix in triangular form if len(Fcc) == 3 * nat: Fcc = fncs.matrix2lowt(Fcc) str_gts += "# Low triangular part of force constant (hessian) matrix [hartree/bohr^2]\n" str_gts += "# i.e.: F_11, F_21, F_22, F_13, F_23, F_33...\n" str_gts += "start_hess\n" for idx in range(0, len(Fcc), 5): line = " ".join(["%+15.8E" % Fij for Fij in Fcc[idx:idx + 5]]) str_gts += " %s\n" % line str_gts += "end_hess\n\n" # Write freqs elif len(freqs) != 0: str_gts += "start_freqs\n" for idx in range(0, len(freqs), 5): line = " ".join(["%7.2f" % (freq) for freq in freqs[idx:idx + 5]]) str_gts += " %s\n" % line str_gts += "end_freqs\n\n" # write with open(gtsfile, 'w') as asdf: asdf.write(str_gts)
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 elif len(eargs) == 5: spc_template, folder, clean, frozen, oniom_layers = eargs # no template?? if spc_template is None: raise Exc.NoTemplateGiven(Exception) # lines for GRADIENT (if any) gradient_lines = None if key4a in spc_template and key4b in spc_template: gradient_lines = spc_template.split(key4a)[1].split(key4b)[0] spc_template = spc_template.replace(gradient_lines, "").replace(key4b, "") gradient_lines = gradient_lines.strip() # lines for HESSIAN (if any) hessian_lines = None if key5a in spc_template and key5b in spc_template: hessian_lines = spc_template.split(key5a)[1].split(key5b)[0] spc_template = spc_template.replace(hessian_lines, "").replace(key5b, "") hessian_lines = hessian_lines.strip() # create folder if folder is not None: if not os.path.exists(folder): os.mkdir(folder) # 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):] #--------------------------------# # Gradient & Hessian computation # #--------------------------------# # (a) via key3 if key3 in line: if bhessian: inkey3 = "! EnGrad Freq " else: inkey3 = "! EnGrad " pos = line.find(key3) line = line[0:pos] + inkey3 + line[pos + len(key3):] # (b) via key4 & key5 if key4a in line: line = line.replace(key4a, gradient_lines) if key5a in line: if bhessian: line = line.replace(key5a, hessian_lines) else: continue #--------------------------------# # Add \n to line if not line.endswith("\n"): line += "\n" string_ifile += line with open(ifile, 'w') as asdf: asdf.write(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 = ITF.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