def alignSVD(self, mol, ref_list=None, tar_list=None): if type(mol) is str: try: mol = qtk.Molecule(mol) except: qtk.exit("error when reading molecule file: %s" % mol) assert issubclass(mol.__class__, qtk.Molecule) if not ref_list: ref_list = [i for i in range(self.N)] if not tar_list: tar_list = copy.deepcopy(ref_list) lst_a = self.R[ref_list] lst_b = mol.R[tar_list] center_a = np.mean(lst_a, axis=0) center_b = np.mean(lst_b, axis=0) #na = len(lst_a) na = self.N #nb = len(lst_b) nb = mol.N crd_a = self.R - np.kron(center_a, np.ones((self.N, 1))) crd_b = mol.R - np.kron(center_b, np.ones((mol.N, 1))) ref_a = lst_a - np.kron(center_a, np.ones((len(lst_a), 1))) ref_b = lst_b - np.kron(center_a, np.ones((len(lst_b), 1))) H = np.dot(np.transpose(ref_a), ref_b) U, s, V = np.linalg.svd(H) R = np.dot(np.transpose(V), np.transpose(U)) self.R = np.transpose( np.dot(R, np.transpose(crd_a))) + \ np.kron(center_b, np.ones((na, 1)) )
def QMInp(molecule, **kwargs_in): kwargs = copy.deepcopy(kwargs_in) inp_dict = { 'cpmd': [cpmd.inp, 'inp'], 'vasp': [vasp.inp, ''], 'espresso': [espresso.inp, 'inp'], 'abinit': [abinit.inp, 'inp'], 'nwchem': [nwchem.inp, 'inp'], 'gaussian': [gaussian.inp, 'com'], 'horton': [horton.inp, 'inp'], 'ofdft': [ofdft.inp, 'inp'], 'bigdft': [bigdft.inp, 'yaml'], } if type(molecule) is str: molecule = qtk.Molecule(molecule, **kwargs) if 'program' not in kwargs: kwargs['program'] = qtk.setting.qmcode p_str = kwargs['program'].lower() p = inp_dict[p_str][0] if 'charge' in kwargs: molecule.charge = kwargs['charge'] kwargs['extension'] = inp_dict[p_str][1] return p(molecule, **kwargs)
def __init__(self, cube_file=None, **kwargs): if cube_file: if not os.path.exists(cube_file): ut.exit("CUBE file:%s not found" % cube_file) self.path, self.name = os.path.split(cube_file) if 'format' not in kwargs: ext = os.path.splitext(self.name)[1] if ext == '.cube': kwargs['format'] = 'cube' elif ext == '.casino' or ext == '.dat': kwargs['format'] = 'casino' elif self.name == 'CHGCAR' or ext == '.vasp': kwargs['format'] = 'vasp' elif ext == '.fchk': kwargs['format'] = 'gaussian' else: qtk.exit("unknown extension %s" % ext) if kwargs['format'] == 'cube': self.data, self.zcoord, self.grid, self.coords\ = rq.read_cube(cube_file) elif kwargs['format'] == 'vasp': self.data, self.zcoord, self.grid = read_vasp(cube_file) elif kwargs['format'] == 'casino': self.data, self.zcoord, self.grid = read_casino(cube_file) elif kwargs['format'] == 'gaussian': self.data, self.zcoord, self.grid = \ read_gaussian(cube_file, **kwargs) self.molecule = qtk.Molecule() self.shape = self.data.shape self.molecule.R = self.zcoord[:, 1:4] * 0.529177249 self.molecule.Z = self.zcoord[:, 0] self.molecule.N = len(self.zcoord) self.molecule.type_list = [ut.Z2n(z) for z in self.molecule.Z] self.interp = None def vec(i): return self.grid[i, 1:] self.dV = np.dot(vec(1), np.cross(vec(2), vec(3))) self.V = self.dV * self.grid[1, 0] * self.grid[2, 0] * self.grid[3, 0] else: self.grid = np.zeros([4, 4]) self.molecule = qtk.Molecule()
def __init__(self, qmout, **kwargs): PlanewaveOutput.__init__(self, qmout, **kwargs) self.info = '' if qmout: root = os.path.split(os.path.abspath(qmout))[0] mol_path = os.path.join(root, 'GEOMETRY.xyz') try: self.molecule = qtk.Molecule(mol_path) except: pass self.getEt(qmout)
def __init__(self, traj=None): if traj: self.molecule = qtk.Molecule(traj) stem, ext = os.path.splitext(traj) pdb = re.sub('\.xyz', '', traj) + '_qtk_tmp.pdb' self.molecule.write(pdb, format='pdb') Trajectory.__init__(self, traj, pdb) os.remove(pdb) self.type_list = self.molecule.type_list self.Z = self.molecule.Z
def getMolecule(*args, **kwargs): if 'scheme' not in kwargs: kwargs['scheme'] = 'cheml_qmn' if kwargs['scheme'] == 'cheml_qmn': dataset = args[0] index = args[1] R_full = dataset.R[index] Z_full = np.atleast_2d(dataset.Z[index]).T Z = Z_full[Z_full > 0] R = R_full[(Z_full).ravel() > 0] * 0.529177249 ZR = np.hstack([np.atleast_2d(Z).T, R]) mol = qtk.Molecule() mol.build(ZR) return mol
def setup(**kwargs): path = os.path.realpath(__file__) path = re.sub('[a-zA-Z0-9\._\-]*$', '', path) mols = [] if 'mol' not in kwargs: mol_list = glob.glob(path + 'test_data/molecules/*') else: mol_list = glob.glob(path + 'test_data/molecules/' + kwargs['mol']) print path print mol_list assert len(mol_list) > 0 for mol_file in mol_list: mols.append(qtk.Molecule(mol_file)) assert len(mol_list) == len(mols) return mols
def __init__(self, output=None, **kwargs): self.Et = np.nan self.energies = {} self.nuclear_repulsion = np.nan self.scf_step = np.nan self.unit = 'Eh' self.molecule = qtk.Molecule() if output: #self.path = qtk.getPath(output) if output: self.path, self.name = os.path.split(output) else: self.path = qtk.getPath(output) _file = qtk.fileStrip(output) self.stem, self.ext = os.path.splitext(_file) self.path = os.path.abspath(self.path)
def Molecules(file_name, **kwargs): """read nested xyz file and return molecule list""" xyz = open(file_name, 'r') content = xyz.readlines() content = [line.replace('\t', ' ') for line in content] xyz.close() itr = 0 more_data = True mols = [] while more_data: try: N = int(content[itr]) prop_list = content[itr + 1] try: prop_list = np.array(prop_list.split(' ')).astype(float) except Exception as err: qtk.warning(str(err)) prop_list = prop_list coord_list = content[itr + 2:itr + N + 2] coord = [ filter(None, [a for a in entry.split(' ')]) for entry in coord_list ] type_list = list(np.array(coord)[:, 0]) type_list = [str(elem) for elem in type_list] Z = np.array([qtk.n2Z(elem) for elem in type_list]) R = np.array(coord)[:, 1:4].astype(float) mol_data = {} for var in ['N', 'type_list', 'Z', 'R']: mol_data[str(var)] = eval(var) itr += N + 2 mols.append(qtk.Molecule(molecule_data=mol_data)) except Exception as err: qtk.progress( "Molecules", "%d molecules have been loaded with message %s." % (len(mols), str(err))) more_data = False return mols
def generate(cell_x, cell_y, **kwargs): """ planar graphene generator generate graphene using 4-atom cubic basis kwargs: bond_length """ if 'bond_length' in kwargs: bond_length = kwargs['bond_length'] else: bond_length = 1.4210 x1 = bond_length x2 = x1 + 0.5 * bond_length y2 = bond_length * np.sqrt(3) / 2 x3 = x2 + bond_length y3 = y2 base = np.array([ [0.0, 0.0, 0.0], [x1, 0.0, 0.0], [x2, y2, 0.0], [x3, y3, 0.0], ]) graphene = qtk.Molecule() graphene.R = copy.deepcopy(base) for x in range(cell_x): shift_x = x * bond_length * 3 for y in range(cell_y): if x > 0 or y > 0: shift_y = y * bond_length * np.sqrt(3) cell = base + np.array([shift_x, shift_y, 0]) graphene.R = np.vstack(np.array([graphene.R, cell])) graphene.sort_coord() graphene.N = len(graphene.R) graphene.Z = [6 for i in range(graphene.N)] return graphene
# coding: utf-8 # In[1]: import qctoolkit as qtk import numpy as np from glob import glob from datetime import datetime import os import sys # In[2]: mol_name = 'coronene' mol_base = qtk.Molecule(mol_name + '.xyz') mol_data = np.load('data_' + mol_name + '.npz') d1E_data = np.load('data_' + mol_name + '_1st.npz') ev_data = mol_data['v'] ew_data = mol_data['w'] d1E_Z = d1E_data['arr_0'] N_heavy = int(np.ones(mol_base.N)[mol_base.Z > 1].sum()) qtk.setting.quiet = True ccs_BN = [ qtk.CCS(mol_base, yml) for yml in sorted(glob('ccs/coronene_BN*.yml')) ] qtk.setting.quiet = False # In[23]:
def SecondOrderRun(inp, program=qtk.setting.qmcode, **kwargs): if program == 'cpmd': inpdir, inpname, psinp, new_run, kwargs\ = qmDir(inp, **kwargs) if new_run: if 'inp_base' in kwargs: _inp_base = kwargs['inp_base'] else: _inp_base = None if 'dl' in kwargs: dl = kwargs['dl'] else: dl = 0.05 dln = '_%03d' % (dl * 100) inpname_dl = inpdir + '/' + psinp + dln + '.inp' inpname_L = inpdir + '/' + psinp + '_100' + '.inp' ref_i = inpdir + '/' + psinp + '_000-d0.inp' ref_dl = inpdir + '/' + psinp + '_000-dl.inp' tar_i = inpdir + '/' + psinp + '_100-d0.inp' tar_dl = inpdir + '/' + psinp + '_100-dl.inp' if 'rst' in kwargs: rst = kwargs['rst'] del kwargs['rst'] else: rst = 'RESTART.1' ref = kwargs['ref_path'] if 'ref_inp' in kwargs: rip = kwargs['ref_inp'] else: refroot = re.sub('.*/', '', ref) rip = ref + '/' + refroot + '.inp' rst_src = ref + '/' + rst if not os.path.exists(ref): qtk.exit("SecondOrderRun: ref_path=" + ref + " not found") if not os.path.exists(rst_src): qtk.exit("SecondOrderRun: RESTART file="+\ rst_src+" not found") if not os.path.exists(rip): qtk.exit("SecondOrderRun: ref_inp=" + rip + " not found") rst_trg = inpdir + '/RESTART_d0' os.link(rst_src, rst_trg) # prepare input file of dl full SCF if qtk.matching(inpname, r'.*_0*\.psp'): shutil.copy(inpname, inpname_dl) shutil.copy(inpname, inpname_L) qtk.replace(inpname_dl, '_0*', dln) qtk.replace(inpname_L, '_0*', '_100') else: xyz_i = qtk.Molecule() xyz_f = qtk.Molecule() xyz_i.read_cpmdinp(rip) xyz_f.read_cpmdinp(inpname) alpath = alp.PathScan(xyz_i, xyz_f, 'cpmd', inp_base=_inp_base) inp_dl1 = alpath.l(dl) inp_L = alpath.l(1) inp_dl1.setCutoff(20) inp_L.setCutoff(20) inp_dl1.write(inpname_dl) inp_L.write(inpname_L) os.link(inpdir + '/RESTART_d0', inpdir + '/RESTART') qdl = qtk.QMRun(inpname_dl, 'cpmd', inplace=True, restart=True, save_restart=True, **kwargs) os.rename(inpdir + '/RESTART.1', inpdir + '/RESTART_dl') # restart at lambda=0 os.rename(inpname_L, tar_i) qti = qtk.QMRun(tar_i, 'cpmd', inplace=True, save_restart=True, maxstep=1, restart=True, **kwargs) # os.rename(inpname , ref_i) # qri = qtk.QMRun(ref_i, 'cpmd', inplace=True, # save_restart=True, # maxstep=1, restart=True, **kwargs) # restart at lambda=dl os.remove(inpdir + '/RESTART') shutil.copy(tar_i, tar_dl) os.link(inpdir + '/RESTART_dl', inpdir + '/RESTART') qtd = qtk.QMRun(tar_dl, 'cpmd', inplace=True, save_restart=True, maxstep=1, restart=True, **kwargs) shutil.copy(ref_i, ref_dl) qrd = qtk.QMRun(ref_dl, 'cpmd', inplace=True, save_restart=True, maxstep=1, restart=True, **kwargs) # clean up os.remove(inpdir + '/RESTART') os.remove(inpdir + '/RESTART_dl') os.remove(inpdir + '/RESTART')
def __init__(self, qmout, **kwargs): PlanewaveOutput.__init__(self, qmout, **kwargs) out_file = open(qmout) data = out_file.readlines() out_file.close() EStrList = filter(lambda x: 'Etotal' in x, data) EList = filter(lambda x: 'ETOT' in x, data) self.scf_step = len(EList) if self.scf_step > 0: Et_list = [float(filter(None, s.split(' '))[2]) for s in EList] self.Et = Et_list[-1] elif len(EStrList) > 0: EStr = EStrList[-1] self.Et = float(EStr.split(' ')[-1]) if len(EStrList) > 0: EStr = EStrList[-1] detailInd = data.index(EStr) self.detail = data[detailInd - 7:detailInd] GStr = filter(lambda x: 'G(1)' in x, data)[-1] ind_g = len(data) - data[::-1].index(GStr) - 1 G_lattice = [] for i in range(3): G_lattice.append( np.fromstring(data[ind_g + i].split('=')[-1], sep=' ')) self.G_lattice = np.array(G_lattice) xangst = filter(lambda x: 'xangst' in x, data)[-1] angst_n = len(data) - data[::-1].index(xangst) - 1 xcart = filter(lambda x: 'xcart' in x, data)[-1] cart_n = len(data) - data[::-1].index(xcart) - 1 Rstr = copy.deepcopy(data[angst_n:cart_n]) Rstr[0] = Rstr[0].replace('xangst', '') R = [[float(r) for r in filter(None, s.split(' '))] for s in Rstr] N = len(R) ZstrOriginal = filter(lambda x: ' typat' in x, data)[-1] Zstr = ZstrOriginal.replace('typat', '') Zind = [int(z) for z in filter(None, Zstr.split(' '))] ZindItr = data.index(ZstrOriginal) while len(Zind) != N: ZindItr += 1 ZindNewStr = filter(None, data[ZindItr].split(' ')) ZindNew = [int(z) for z in ZindNewStr] Zind.extend(ZindNew) NZnuc = filter(lambda x: 'ntypat' in x, data)[-1] NZnuc = int(filter(None, NZnuc.split(' '))[-1]) Znuc = filter(lambda x: 'znucl ' in x, data)[-1] line_znuc = len(data) - data[::-1].index(Znuc) Znuc = filter(None, Znuc.replace('znucl', '').split(' ')) Znuc = [float(z) for z in Znuc] while len(Znuc) < NZnuc: Znuc_new = filter(None, data[line_znuc].split(' ')) Znuc_new = [float(z) for z in Znuc_new] Znuc.extend(Znuc_new) line_znuc = line_znuc + 1 build = [] for i in range(N): Z = [Znuc[Zind[i] - 1]] Z.extend(R[i]) build.append(Z) self.molecule = qtk.Molecule() self.molecule.build(build) if self.scf_step > 0: fStr = filter(lambda x: 'tesian forces (hartree/b' in x, data)[-1] fInd = data.index(fStr) fData = data[fInd + 1:fInd + 1 + N] force = [] for f in fData: fStr = filter(None, f.split(' '))[1:] force.append([float(fs) for fs in fStr]) self.force = np.array(force) self.occupation = [] try: r1p = re.compile(r'^[ a-z]{17} +[ 0-9.E+-]+$') r2p = re.compile(r'^ +[a-z]+ +.*$') report = filter(r2p.match, filter(r1p.match, data)) occ_ptn_lst = filter(lambda x: ' occ ' in x, report) if len(occ_ptn_lst) > 0: occ_pattern = occ_ptn_lst[-1] occ_pattern_ind = len(report) - report[::-1].index(occ_pattern) occ_pattern_end = report[occ_pattern_ind] occ_ind_start = len(data) - data[::-1].index(occ_pattern) - 1 occ_ind_end = len(data) - data[::-1].index(occ_pattern_end) - 1 for i in range(occ_ind_start, occ_ind_end): for occ in filter(None, data[i].split(' ')): try: self.occupation.append(float(occ)) except Exception as err: pass except Exception as err: qtk.warning("error when extracting occupation number with" +\ " error message: %s" % str(err)) cell_pattern = re.compile(r'^ R.*=.* G.*=.*$') cell_list = filter(cell_pattern.match, data) cell = [] for cstr in cell_list: cell.append([float(c) for c in filter(None, cstr.split(' '))[1:4]]) self.lattice = np.array(cell) / 1.889726124993 self.celldm = qtk.lattice2celldm(self.lattice) self.molecule.R_scale = qtk.xyz2fractional(self.molecule.R, self.celldm) eigStr = os.path.join(os.path.split(qmout)[0], '*_EIG') eigFileList = sorted(glob.glob(eigStr)) if len(eigFileList) != 0: if 'eig_index' in kwargs: eig_i = kwargs['eig_index'] else: eig_i = -1 if len(eigFileList) > 1: qtk.warning( "more than one o_EIG files found " + \ "loading last file with name: %s" % eigFileList[eig_i] ) eigFile = open(eigFileList[eig_i]) eigData = eigFile.readlines() eigFile.close() unitStr = filter(lambda x: 'Eigenvalues' in x, eigData)[0] unitStr = unitStr.replace('(', '').replace(')', '') unit = filter(None, unitStr.split(' '))[1] spinList = filter(lambda x: 'SPIN' in x, eigData) if len(spinList) != 0: spinFactor = 2 maxInd = eigData.index(spinList[-1]) else: spinFactor = 1 maxInd = len(eigData) ind = [] for kStr in filter(lambda x: 'kpt#' in x, eigData): ind.append(eigData.index(kStr)) band = [] kpoints = [] if spinFactor == 1: for i in range(len(ind)): wcoord = eigData[ind[i]].split('wtk=')[-1].split(', kpt=') weight = float(wcoord[0]) cStr = filter(None, wcoord[1].split('(')[0].split(' ')) coord = [float(c) for c in cStr] coord.append(weight) kpoints.append(coord) s = ind[i] + 1 if i < len(ind) - 1: e = ind[i + 1] else: e = len(eigData) eig_i = filter(None, ''.join(eigData[s:e]).split(' ')) band.append([ qtk.convE(float(ew), '%s-eV' % unit)[0] for ew in eig_i ]) self.band = np.array(band) self.kpoints = np.array(kpoints) self.mo_eigenvalues = np.array(band[0]).copy() if len(self.occupation) > 0: diff = np.diff(self.occupation) ind = np.array(range(len(diff))) pos = diff[np.where(abs(diff) > 0.5)] mask = np.in1d(diff, pos) if len(ind[mask]) > 0: N_state = ind[mask][0] else: qtk.warning("occupation number not available... " + \ "try to use molecule object with closed shell assumption" ) N_state = self.molecule.getValenceElectrons() / 2 - 1 vb = max(self.band[:, N_state]) cb = min(self.band[:, N_state + 1]) vb_pos = np.argmax(self.band[:, N_state]) cb_pos = np.argmin(self.band[:, N_state + 1]) self.Eg = cb - vb if vb_pos == cb_pos: self.Eg_direct = True else: self.Eg_direct = False self.fermi_index = N_state else: qtk.warning("spin polarized band data " +\ "extraction is not yet implemented") else: qtk.warning('no k-point information (o_EIG file) found')
def __init__(self, xyz_i, xyz_f, program=qtk.setting.qmcode, **kwargs): if 'name' in kwargs: self.name = kwargs['name'] elif type(xyz_i)==str and type(xyz_f)==str: ref_stem, _ = os.path.splitext(xyz_i) tar_stem, _ = os.path.splitext(xyz_f) self.name = ref_stem + '-' + tar_stem else: self.name = 'A2B' self.program = program if type(xyz_i) == str: self.initial = qtk.Molecule() self.initial.read(xyz_i) elif type(xyz_i) == qtk.Molecule: self.initial = xyz_i if type(xyz_f) == str: self.final = qtk.Molecule() self.final.read(xyz_f) elif type(xyz_f) == qtk.Molecule: self.final = xyz_f self.inp_base = qtk.QMInp(xyz_i, program) if 'inp_base' in kwargs: if kwargs['inp_base'] is not None: inp_tmp = copy.deepcopy(kwargs['inp_base']) self.inp_base.inp.setting = inp_tmp.inp.setting # !!!!!!!!!!!!!!!!!!!!!!! # construct mutation list # tar_list: list of matching location tar_list = [i for i in range(self.final.N)] self.mutation_ind = [] self.mutation_ref = [] self.mutation_tar = [] self.mutation_crd = [] # loop through every atom in initial system for i in range(self.initial.N): Zi = self.initial.type_list[i] Ri = self.initial.R[i] to_void = True # loop through every atom in final system for j in range(self.final.N): # every atom-pair distance Rj = self.final.R[j] diff = np.linalg.norm(Ri-Rj) # search for atoms at same location if diff < 10E-6: to_void = False tar_list[j] = False Zj = self.final.type_list[j] if Zi != Zj: self.mutation_ind.append(i) self.mutation_ref.append(Zi) self.mutation_tar.append(Zj) self.mutation_crd.append(Ri) # when no distance matched, set atom target to void if to_void: self.mutation_ind.append(i) self.mutation_ref.append(Zi) self.mutation_tar.append('VOID') self.mutation_crd.append(Ri) # loop through every target atom for non-matching atoms N = self.initial.N for j in range(self.final.N): if tar_list[j]: self.mutation_ind.append(N) self.mutation_ref.append('VOID') self.mutation_tar.append(self.final.type_list[j]) self.mutation_crd.append(self.final.R[j])
shutil.rmtree('inp') os.makedirs('inp') dopt = [[False, 2.1163, 2.2244], [1.5190, False, 1.9307], [1.5677, 1.8524, False]] xyz_file = glob.glob('structure/*.xyz') inp = qtk.QMInp(xyz_file[0], 'cpmd') inp.setCelldm([20, 15, 15, 0, 0, 0]) inp.setShift([7.5, 7.5, 7.5]) inp.saveDensity() ref_list = [] tar_list = [] name_list = [] for xyz in xyz_file: mol1 = qtk.Molecule() mol1.read(xyz) ref_list.append(mol1) atom, coord = mol1.extract(1) tars = [] for d_tar in [1, 1.5, 2, 2.5]: mol2 = qtk.Molecule() mol2.read(xyz) mol2.stretch(1, [2, 1], d_tar - coord[0]) tars.append(mol2) tar_list.append(tars) stem, ext = os.path.splitext(xyz) stem = re.sub('.*\/', '', stem) name_list.append(re.sub('-eq', '', stem))
def __init__(self, qmout, **kwargs): PlanewaveOutput.__init__(self, qmout, **kwargs) out_file = open(qmout) data = out_file.readlines() out_file.close() Et_pattern = re.compile("^.*total energy *=.*$") f_pattern = re.compile("^.*atom.*type.*force.*=.*$") Et_list = filter(Et_pattern.match, data) f_list = filter(f_pattern.match, data) if len(Et_list) > 0: Et_str = filter(Et_pattern.match, data)[-1] Et = float(Et_str.split()[-2]) self.Et, self.unit = qtk.convE(Et, 'Ry-Eh') out_folder = os.path.split(os.path.abspath(qmout))[0] save = glob.glob(os.path.join(out_folder, '*.save')) # extract force information if len(f_list) > 0: fstr = [ filter(None, fstr.split('=')[-1].split(' ')) for fstr in f_list ] # atomic unit force, HF/au, converted from Ry/au self.force = 0.5 * np.array([[float(comp) for comp in atm] for atm in fstr]) # extract band structure from xml files if save: save = save[0] try: data_xml = os.path.join(save, 'data-file.xml') xml_file = open(data_xml) tree = ET.parse(xml_file) xml_file.close() self.xml = tree.getroot() kpoints = [] band = [] # extract celldm celldm = [] cellVec = [] for i in range(1, 4): cellvStr = filter( None, self.xml[2][4][i].text.replace('\n', '').split(' ')) cellVec.append([float(v) for v in cellvStr]) self.celldm = qtk.cellVec2celldm(cellVec) # extract structure R = [] N = int(self.xml[3][0].text.replace('\n', '')) Nsp = int(self.xml[3][1].text.replace('\n', '')) for i in range(N): RiStr = self.xml[3][5 + Nsp + i].get('tau') Ri = [float(r) * 0.529177249 for r in RiStr.split(' ')] ni = self.xml[3][5 + Nsp + i].get('SPECIES') Z = [qtk.n2Z(ni)] Z.extend(Ri) R.append(Z) self.molecule = qtk.Molecule() self.molecule.build(R) # access data for each kpoint for k in self.xml[-2]: k_str = k[0].text coord = [float(c) for c in k_str.split()] weight = float(k[1].text.split()[0]) coord.append(weight) kpoints.append(coord) ev_file = os.path.join(save, k[2].attrib['iotk_link']) k_xml_file = open(ev_file) k_xml = ET.parse(k_xml_file) k_xml_file.close() ev_k = k_xml.getroot() ev_str = ev_k[2].text.split() ev = [qtk.convE(float(entry), 'Eh-eV')[0]\ for entry in ev_str] band.append(ev) occ_str = ev_k[3].text.split() occ = [float(entry) for entry in occ_str] self.kpoints = np.array(kpoints) self.mo_eigenvalues = copy.deepcopy(band[0]) self.band = np.array(band) self.occupation = occ diff = np.diff(occ) pos = diff[np.where(abs(diff) > 0.5)] mask = np.in1d(diff, pos) ind = np.array(range(len(diff))) if len(ind[mask]) > 0: N_state = ind[mask][0] vb = max(self.band[:, N_state]) cb = min(self.band[:, N_state + 1]) vb_pos = np.argmax(self.band[:, N_state]) cb_pos = np.argmin(self.band[:, N_state + 1]) self.Eg = cb - vb if vb_pos == cb_pos: self.Eg_direct = True else: self.Eg_direct = False cell = [] for i in range(1, 4): vec = filter( None, self.xml[2][4][i].text.replace('\n', '').split(' ')) cell.append([float(v) for v in vec]) self.lattice = np.array(cell) / 1.889726124993 self.celldm = qtk.lattice2celldm(self.lattice) self.molecule.R_scale = qtk.xyz2fractional( self.molecule.R, self.celldm) except IOError: qtk.warning('xml file of job %s not found' % qmout) else: qtk.warning('job %s not finished' % qmout)
def __init__(self, qmout=None, **kwargs): GaussianBasisOutput.__init__(self, qmout, **kwargs) if qmout: outfile = open(qmout) data = outfile.readlines() pattern = re.compile(" *R *M *S *D *=") try: report = filter(lambda x: "\\" in x, data) except Exception as e: qtk.exit("error when access final report with message %s" % str(e)) final_str = ''.join(report) final_str = final_str.replace('\n', '') final_list = final_str.split('\\') try: rmsd = filter(pattern.match, final_list)[0] except Exception as e: qtk.exit("something wrong when accessing final energy" +\ " with error message: %s" % str(e)) ind = final_list.index(rmsd) - 1 Et_str = final_list[ind] term = Et_str.split('=')[0].replace(' ', '') E_list = [ 'HF', 'CCSD', 'CCSD(T)', 'MP4SDQ', 'MP4DQ' 'MP4D', 'MP3', 'MP2', 'HF' ] while term not in E_list: ind = ind - 1 Et_str = final_list[ind] term = Et_str.split('=')[0].replace(' ', '') self.Et = float(Et_str.split('=')[1].replace(' ', '')) self.detail = final_list EJStr = filter(lambda x: 'EJ=' in x, data) if len(EJStr) > 0: EJStr = EJStr[-1] EJind = data.index(EJStr) EJStr = data[EJind].replace(' ', '') EComponents = filter(None, EJStr.split('E')) tags_dict = { 'T': 'Ekin', 'V': 'Eext', 'J': 'Eee', 'K': 'Ex', 'Nuc': 'Enn', } for EStr in EComponents: tag, E = EStr.split('=') try: self.energies[tags_dict[tag]] = float(E) except Exception as e: qtk.warning("FORTRAN float overflow " + \ "when extracting energy components for " +\ qmout + " with error message %s" % str(e)) self.energies[tags_dict[tag]] = np.nan crdStr = filter(lambda x: 'Angstroms' in x, data)[-1] ind = len(data) - data[::-1].index(crdStr) + 2 ZR = [] while True: if not data[ind].startswith(' ---'): crdData = [ float(c) for c in filter(None, data[ind].split(' ')) ] crd = [crdData[1]] crd.extend(crdData[3:]) ZR.append(crd) ind = ind + 1 else: break self.molecule = qtk.Molecule() self.molecule.build(ZR) self.nuclear_repulsion = self.molecule.nuclear_repulsion() force = [] fStr_list = filter(lambda x: 'Forces (Hartrees' in x, data) if len(fStr_list) > 0: fStr = fStr_list[-1] ind = len(data) - data[::-1].index(fStr) + 2 for i in range(self.molecule.N): fLst = filter(None, data[ind + i].split(' '))[2:] force.append([float(s) for s in fLst]) self.force = np.array(force) else: self.force = np.nan dipole = [] uStr_list = filter(lambda x: 'Debye)' in x, data) if len(uStr_list) > 0: uStr = uStr_list[-1] ind = len(data) - data[::-1].index(uStr) dipoleStr = filter(None, data[ind].split(' ')) for i in [1, 3, 5]: dipole.append(float(dipoleStr[i])) self.dipole = np.array(dipole) else: self.dipole = np.nan qp = [] qStr_list = filter(lambda x: ' Quadrupole moment' in x, data) if len(qStr_list) > 0: qStr = qStr_list[-1] ind = len(data) - data[::-1].index(qStr) for i in range(2): tmp = dipoleStr = filter(None, data[ind + i].split(' ')) for j in [1, 3, 5]: qp.append(float(tmp[j])) xx, yy, zz, xy, xz, yz = qp self.quadrupole = np.array([ [xx, xy, xz], [xy, yy, yz], [xz, yz, zz], ]) else: self.quadrupole = np.nan read_fchk = True if 'read_fchk' in kwargs: read_fchk = kwargs['read_fchk'] if read_fchk: fchk = os.path.join(self.path, self.stem) + ".fchk" if os.path.exists(fchk): if 'debug' in kwargs and kwargs['debug']: self.getMO(fchk) else: try: self.getMO(fchk) except Exception as e: qtk.warning("something wrong while loading fchk file"+\ " with error message: %s" % str(e))
def getBasis(): ###################################### # extract basis function information # ###################################### basis_dict = {"S": 0, "P": 1, "D": 2, "F": 3, "G": 4, "H": 5} basis_P = re.compile(r" *[0-9]+ [A-Z] [0-9]\.[0-9]{8}") batom_P = re.compile(r"^ [0-9A-Za-z\-_\.]+ *\([A-Z][a-z]*\)") bname_P = re.compile(r"\((.*)\)") coord_P = re.compile(r"^ [0-9A-Za-z\.\-_]+ +[- ][0-9\.]{9,}") basisStr = filter(basis_P.match, data) batomStr = filter(batom_P.match, data) coordStr = filter(coord_P.match, data) # change 'Sulphur' to 'Sulfur' for NWChem format # 'Sulphur' 'Sulfur' def atomNameConv(old, new): _matched = filter(lambda x: old in x, batomStr) if _matched: _matched = _matched[0] _s = batomStr.index(_matched) batomStr[_s] = re.sub(old, new, batomStr[_s]) _s = data.index(_matched) data[_s] = re.sub(old, new, data[_s]) atomNameConv('Sulphur', 'Sulfur') atomNameConv('Aluminium', 'Aluminum') _exponents = [float(filter(None, s.split(' '))\ [2]) for s in basisStr] _coefficients = [float(filter(None, s.split(' '))\ [3]) for s in basisStr] _N = [int(filter(None, s.split(' '))[0])\ for s in basisStr] _type = [filter(None, s.split(' '))[1]\ for s in basisStr] _bfnInd = [data.index(batom) for batom in batomStr] _bfnEndPtn = re.compile(r" Summary of \"") _bfnEndStr = filter(_bfnEndPtn.match, data)[0] _bfnInd.append(data.index(_bfnEndStr)) _ao_keys = [0] for ind in range(len(_bfnInd) - 1): _key = _ao_keys[-1] for i in range(_bfnInd[ind] + 4, _bfnInd[ind + 1]): if len(data[i]) > 1: _key = _key + 1 _ao_keys.append(_key) _atoms = [getattr(pt, bname_P.match( filter(None, s.split(' '))[1]).group(1).lower()).symbol\ for s in batomStr] self.type_list = [re.split(r'[\._]', filter(None, s.split(' '))[0])[0].title()\ for s in coordStr] self.type_list_unique = list( collections.OrderedDict.fromkeys(self.type_list)) self.R = np.array([filter(None, s.split(' '))[1:4]\ for s in coordStr]).astype(float) self.N = len(self.R) self.Z = [qtk.n2Z(e) for e in self.type_list] self.R_bohr = 1.889725989 * self.R ZR = [] for i in range(self.N): vec = [self.Z[i]] vec.extend(self.R[i]) ZR.append(vec) self.molecule = qtk.Molecule() self.molecule.build(ZR) _N.append(0) self.basis = [] for i in range(len(self.type_list)): e = self.type_list[i] center = self.R_bohr[i] ind = self.type_list_unique.index(e) bfn_base = {} bfn_base['atom'] = e bfn_base['center'] = center bfn_base['index'] = i exp = [] cef = [] for g in range(_ao_keys[ind], _ao_keys[ind + 1]): exp.append(_exponents[g]) cef.append(_coefficients[g]) if _N[g] != _N[g + 1] or g + 1 >= _ao_keys[ind + 1]: bfn = copy.deepcopy(bfn_base) bfn['exponents'] = copy.deepcopy(exp) bfn['coefficients'] = copy.deepcopy(cef) if _type[g] in basis_dict: _bfnList = self.basisList(basis_dict[_type[g]]) for bStr in _bfnList: bfn['type'] = _type[g].lower() + bStr self.basis.append(copy.deepcopy(bfn)) exp = [] cef = []
# coding: utf-8 # In[ ]: import qctoolkit as qtk import numpy as np import os import glob # In[ ]: # construct dummy base molecule template base = qtk.Molecule() base.celldm = [20, 10, 10, 0, 0, 0] base.isolated = True hf = base.copy() hf.build([[1, 5., 5., 5.], [9, 6., 5., 5.]]) hf.name = 'hf_ref' hcl = base.copy() hcl.build([[1, 5., 5., 5.], [17, 6., 5., 5.]]) hcl.name = 'hcl_tar' # basic setup prefix = 'production_shifted_' ref_root = os.path.abspath('%srefs' % prefix) qmsettings = { 'program': 'cpmd', 'omp': 2, 'cutoff': 200, 'wf_convergence': 1E-7,