def get_data_from_maintext(mtext): key_geom1 = 'Z-Matrix orientation' key_geom2 = 'Input orientation' key_geom3 = 'Standard orientation' key_force = " Forces (" key_zmat = 'Final structure in terms of initial Z-matrix:' key_end = "------------------" key_1bar = "\\" key_oniom = "ONIOM: extrapolated energy" # (a) Find cartesian coordinates if key_geom1 in mtext: geom = mtext.split(key_geom1)[-1] elif key_geom2 in mtext: geom = mtext.split(key_geom2)[-1] elif key_geom3 in mtext: geom = mtext.split(key_geom3)[-1] else : geom = None if geom is not None: # convert to list of lines and get the lines associated to geometry geom = "\n".join(geom.split("\n")[5:]) idx = geom.find(key_end) geom = geom[:idx].strip() # convert to list of floats geom = [line.split() for line in geom.split("\n") if line.strip() != ""] xcc = [[float(x),float(y),float(z)] for (_,atnum,_,x,y,z) in geom] xcc = flatten_llist(xcc) else: xcc = None # (b) Find forces --> gradient if key_force in mtext: force = mtext.split(key_force)[-1] # convert to list of lines and get the lines associated to forces force = "\n".join(force.split("\n")[3:]) idx = force.find(key_end) force = force[:idx] # convert to list of floats force = [line.split() for line in force.split("\n") if line.strip() != ""] gcc = [[-float(gx),-float(gy),-float(gz)] for (_,atnum,gx,gy,gz) in force] gcc = flatten_llist(gcc) else: gcc = None # (c) Find z-matrix if key_zmat in mtext: lines_zmat = mtext.split(key_zmat)[-1].strip().split("\n") for idx,line in enumerate(lines_zmat): line = line.strip() if line == "" or key_1bar in line: idx1 = idx break zmat = [line.strip() for line in lines_zmat[:idx1] if "Variables:" not in line] else: zmat = None # (d) ONIOM energy? E_ONIOM = None for line in mtext.split("\n")[::-1]: if key_oniom in line: E_ONIOM = float(line.split()[-1]) break # Convert xcc to bohr if xcc is not None: xcc = [xi/ANGSTROM for xi in xcc] # Return data return xcc, gcc, zmat, E_ONIOM
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 get_data_from_archive(summary): # Keywords to look for key_hag = "#" key_1bar ='\\' key_2bar ='\\\\' key_ver ='Version' key_en1 ='State=' key_en2 ='RMSD=' key_imag ='NImag=' # logical variables Lzmat = False Lxyz = False Lhess = False # (a) the command line idx1 = summary.find(key_hag,0)+len(key_hag) idx2 = summary.find(key_2bar,idx1) commands = summary[idx1:idx2] # (b) the comment line idx3 = summary.find(key_2bar,idx2)+len(key_2bar) idx4 = summary.find(key_2bar,idx3) comment = summary[idx3:idx4] # (c) charge and multiplicity idx5 = summary.find(key_2bar,idx4)+len(key_2bar) idx6 = summary.find(key_1bar,idx5) ch,mtp = [int(value) for value in summary[idx5:idx6].split(",")] # (d) z-matrix or Cartesian coordinates idx7 = summary.find(key_ver,idx6) geom = [string for string in summary[idx6+len(key_1bar):idx7].split(key_1bar) if string != ""] if len(geom[0]) <= 4: Lzmat = True zmat = list(geom) symbols = [line.split(",")[0] for line in zmat if "=" not in line] xcc = None else: Lxyz = True zmat = None symbols = [line.split(",")[0] for line in geom] # sometimes, this line has 5 elements instead of 4 # for this reason, coordinates are extracted with [-3:] # instead of [1:] xyz = [line.split(",")[-3:] for line in geom] xyz = [[float(x),float(y),float(z)] for (x,y,z) in xyz] xcc = flatten_llist(xyz) # (e) Energy and other info idx8a = summary.find(key_ver,idx7) idx8b = summary.find(key_en1,idx7) idx8 = max(idx8a,idx8b) idx9 = summary.find(key_1bar,idx8)+len(key_1bar) idx10 = summary.find(key_en2,idx9) str_energies = summary[idx9:idx10].replace(key_1bar," ") energies = str_energies.split() energies = [line.split("=") for line in energies] # remove S**2 (for open-shell) energies = [(float(energy),level.strip()) for level,energy in energies if not level.strip().startswith("S2")] # (f) Hessian matrix Lhess = key_imag in summary if Lhess: idx11=summary.find(key_imag,0)+len(key_imag) idx12=summary.find(key_2bar,idx11) num_imag = int(summary[idx11:idx12]) idx12 += len(key_2bar) idx13=summary.find(key_2bar,idx12) # low-triangle hessian Fcc = [float(value) for value in summary[idx12:idx13].split(",")] else: num_imag = -1 Fcc = None # (g) Gradient may appera after hessian matrix if Lhess: idx13 += len(key_2bar) idx14=summary.find(key_2bar,idx13) gcc = [float(value) for value in summary[idx13:idx14].split(",")] else: gcc = None # Convert xcc to bohr if xcc is not None: xcc = [xi/ANGSTROM for xi in xcc] return commands,comment,ch,mtp,symbols,xcc,gcc,Fcc,energies,num_imag,zmat
def read_gauout(filename): # check extension end_out = (filename.lower()).endswith(".out") end_log = (filename.lower()).endswith(".log") if (not end_out) and (not end_log): raise Exc.FileType(Exception) key = 'GINC' fin = '@' str_end = 'Normal termination' str_atoms = 'NAtoms' str_geom1a = 'Z-Matrix orientation' str_geom1b = 'Input orientation' str_geom2 = 'Standard orientation' str_nosym = 'Nosym' str_zmat = 'Final structure in terms of initial Z-matrix' start_archives = [] end_archives = [] str_endfiles = [] latom = [] idx_last_zmat = -1 list_zmat_backup = [] # More then one archive file for instance Link1 filex = open(filename, 'r') nlin = 0 for linea in filex: nlin += 1 if str_end in linea: str_endfiles.append(nlin) if key in linea: start_archives.append(nlin) if fin in linea: end_archives.append(nlin) if str_atoms in linea: latom = linea.split() filex.close() # It does not consider an archive file right after the main archive file # For some reason gaussian sometimes print one archive after the other str_tmp = str_endfiles[:] num_tmp = len(str_tmp) if num_tmp > 1: for i in range(num_tmp - 1, 0, -1): if abs(str_tmp[i - 1] - str_tmp[i]) < 300: del str_endfiles[i] del start_archives[i] del end_archives[i] # # number_archives = len(start_archives) if number_archives == 0: return -1 str_beginfiles = str_endfiles[:] str_beginfiles.insert(0, 0) str_beginfiles.pop() natoms = int(latom[1]) if number_archives == 1 and start_archives[0] == 0: return str_geom1s = [] str_geom2s = [] str_nosyms = [] str_zmats = [] for i in range(number_archives): str_geom1s.append(0) str_geom2s.append(0) str_nosyms.append(0) str_zmats.append(0) start_archive = start_archives[i] end_archive = end_archives[i] pos_beginfile = str_beginfiles[i] pos_endfile = str_endfiles[i] filex = open(filename, 'r') nlin = 0 for linea in filex: nlin += 1 if nlin >= pos_beginfile and nlin <= pos_endfile: if str_nosym.lower() in linea.lower(): str_nosyms[i] = nlin if str_geom1a in linea or str_geom1b in linea: str_geom1s[i] = nlin if str_geom2 in linea: str_geom2s[i] = nlin if str_zmat in linea: str_zmats[i] = nlin filex.close() # position of the geometries pos_nosym = str_nosyms[i] pos_geom1 = str_geom1s[i] pos_geom2 = str_geom2s[i] pos_zmat1 = str_zmats[i] num_imag,charge,mult,commands,list_level,list_energy,atomic_number,ccgeom,list_zmat,\ hess_data,hess_full,grad=\ archive(filename,natoms,start_archive,end_archive,\ pos_beginfile,pos_endfile,pos_nosym,pos_geom1,pos_geom2,pos_zmat1) # Saves the last available z-matrix if str_zmats[i] != 0: idx_last_zmat = i list_zmat_backup = list_zmat[:] # atomic mass atomic_mass = atonums2masses(atomic_number) ccgeom = flatten_llist(ccgeom) energy = list_energy[-1] level = list_level[-1] # from Angstrom to Bohr ccgeom = [xx / ANGSTROM for xx in ccgeom] return ccgeom, atomic_number, charge, mult, energy, grad, hess_data, atomic_mass, level