def test_kp(): box = [[2.715, 2.715, 0], [0, 2.715, 2.715], [2.715, 0, 2.715]] coords = [[0, 0, 0], [0.25, 0.25, 0.25]] elements = ["Si", "Si"] Si = Atoms(lattice_mat=box, coords=coords, elements=elements) lattice_mat = Si.lattice_mat kp = Kpoints3D().automatic_length_mesh(lattice_mat=lattice_mat, length=20) td = kp.to_dict() fd = Kpoints3D.from_dict(td) sym = kp.high_symm_path(Si)._path x, y = kp.interpolated_points(Si) kpath = generate_kpath(kpath=[[0, 0, 0], [0, 0.5, 0.5]], num_k=5) kps = generate_kgrid(grid=[5, 5, 5]) new_file, filename = tempfile.mkstemp() kp.write_file(filename) sym = kp.high_symm_path(s1)._path sym = kp.high_symm_path(s2)._path sym = kp.high_symm_path(s3)._path sym = kp.high_symm_path(s4)._path sym = kp.high_symm_path(s5)._path sym = kp.high_symm_path(s6)._path sym = kp.high_symm_path(s7)._path assert (len(x), x[0][0], y[0], kpath[0][0], kps[0][0]) == ( 166, 0, "\Gamma", 0.0, 0.0, ) kp = Kpoints3D().kpath(atoms=Si, unique_kp_only=True)
def get_wien_kpoints(atoms=None, write_file=False, line_density=1, filename="MyKpoints"): """Get WIEN2k style kpoints for bandstructure calculation.""" uniqe_lbl = [] uniqe_k = [] kpoints, labels = Kpoints3D().interpolated_points( atoms, line_density=line_density) kp = Kpoints3D().high_kpath(atoms)["kpoints"] for i, j in kp.items(): uniqe_lbl.append(i) uniqe_k.append(j) legend = uniqe_lbl BS = uniqe_k data = [] BS = array(BS, dtype=float) Nt = 500 dl = zeros(len(BS) - 1) for i in range(len(BS) - 1): dr = BS[i + 1] - BS[i] dl[i] = sqrt(dot(dr, dr)) dN = dl / sum(dl) * Nt Ni = [int(round(dn)) for dn in dN] Mi = zeros(len(Ni), dtype=int) for i in range(len(Ni)): tmp = np.concatenate((BS[i + 1], BS[i])) fracts = 1 // array(list(filter(lambda x: x > 1e-6, tmp))) fact = int(reduce(lambda x, y: x * y // gcd(x, y), fracts)) if Ni[i] % fact == 0: Mi[i] = Ni[i] else: Mi[i] = Ni[i] * fact for p in range(len(Ni)): NAME = legend[p] for i in range(Ni[p]): kint = Mi[p] * BS[p] + (BS[p + 1] - BS[p]) * i * Mi[p] // Ni[p] if i > 0: NAME = " " data.append([NAME, kint[0], kint[1], kint[2], Mi[p], 1.0]) NAME = legend[-1] kint = BS[-1] * Mi[-1] data.append([NAME, kint[0], kint[1], kint[2], Mi[-1], 1.0]) if write_file: f = open(filename, "w") for i in data: line = (str(i[0]) + str(" ") + str(i[1]) + str(" ") + str(i[2]) + str(" ") + str(i[3]) + str(" ") + str(i[4]) + str(" ") + str(i[5]) + str("\n")) f.write(line) line = str("END \n") f.write(line) f.close() return data
def get_ibz_kp(lines=""): """Read the Kpoints in the line-mode.""" kp_labels = [] all_kp = [] kp_labels_points = [] for ii, i in enumerate(lines): if ii > 2: tmp = i.split() k = [tmp[0], tmp[1], tmp[2]] all_kp.append(k) if len(tmp) == 5: tmp = str("$") + str(tmp[4]) + str("$") if len(kp_labels) == 0: kp_labels.append(tmp) kp_labels_points.append(ii - 3) elif tmp != kp_labels[-1]: kp_labels.append(tmp) kp_labels_points.append(ii - 3) labels = [] for i in range(len(all_kp)): labels.append("") for i, j in zip(kp_labels, kp_labels_points): labels[j] = i all_kp = np.array(all_kp, dtype="float") kpoints = Kpoints3D(kpoints=all_kp, labels=labels, kpoint_mode="linemode") return kpoints
def get_ibz_kp(self): """Get high-symmetry k-points.""" frac_k_points, k_points_labels = Kpoints3D().interpolated_points( self.atoms ) lines = [] for i, j in zip(frac_k_points, k_points_labels): if j != "": line = ( str(j) + str(" ") + str(i[0]) + str(" ") + str(i[1]) + str(" ") + str(i[2]) ) line = line.replace("\\", "") lines.append(line) lines1 = lines lines2 = lines[1:] pairs = zip(lines1, lines2) lines = [] for i in pairs: line = i[0] + str(" ") + str(i[1]) # +'\n' if i[0] != i[1]: lines.append(line) return lines
def test_inputs(): box = [[2.715, 2.715, 0], [0, 2.715, 2.715], [2.715, 0, 2.715]] coords = [[0, 0, 0], [0.25, 0.25, 0.25]] elements = ["Si", "Si"] Si = Atoms(lattice_mat=box, coords=coords, elements=elements) print(Si) kp = Kpoints3D().automatic_length_mesh(lattice_mat=Si.lattice_mat, length=20) new_file, filename = tempfile.mkstemp() qe = QEinfile(Si, kp) qe.write_file(filename) kp = Kpoints3D().kpath(atoms=Si) qe = QEinfile(Si, kp) qe.write_file(filename) sp = qe.atomic_species_string() sp = qe.atomic_cell_params() assert qe.input_params['system_params']['nat'] == 2
def from_dict(self, info={}): """Load the class from a dictionary.""" return VaspJob( poscar=Poscar.from_dict(info["poscar"]), kpoints=Kpoints.from_dict(info["kpoints"]), incar=Incar.from_dict(info["incar"]), potcar=Potcar.from_dict(info["potcar"]), vasp_cmd=info["vasp_cmd"], copy_files=info["copy_files"], attempts=info["attempts"], output_file=info["output_file"], stderr_file=info["stderr_file"], jobname=info["jobname"], )
def get_bandstructure_plot( self, atoms=None, efermi=0.0, filename="bs.png", yrange=[-4, 4] ): """Get bandstructure plot..""" kpoints, labels = Kpoints3D().interpolated_points(atoms) # _path # print ('kpath',kpoints) eigs = self.band_structure_eigs(kpath=kpoints, efermi=efermi).T for i, ii in enumerate(eigs): plt.plot(ii, color="b") plt.ylim(yrange) plt.savefig(filename) plt.close()
def mesh_bands( self, dim=[1, 1, 1], filename="band.conf", factor=521.471, line_density=20, ): """ Use for making phonon bandstructure plot. After running phonopy -p bandd.conf, bandplot -o PBAND.png """ kpoints = Kpoints3D().kpath(self.atoms, line_density=line_density) all_kp = kpoints._kpoints labels = kpoints._labels all_labels = "" all_lines = "" for lb in labels: if lb == "": lb = None all_labels = all_labels + str(lb) + str(" ") for k in all_kp: all_lines = (all_lines + str(k[0]) + str(" ") + str(k[1]) + str(" ") + str(k[2]) + str(" ")) file = open(filename, "w") line = str("FREQUENCY_CONVERSION_FACTOR = ") + str(factor) + "\n" file.write(line) ax_line = self.prim_axis() file.write(ax_line) line = str("ATOM_NAME = ") + str(self.tag) + "\n" file.write(line) line = str("DIM = ") + " ".join(map(str, dim)) + "\n" file.write(line) line = str("FORCE_CONSTANTS = READ") + "\n" file.write(line) line = str("BAND= ") + str(all_lines) + "\n" file.write(line) line = str("BAND_LABELS= ") + str(all_labels) + "\n" file.write(line) file.close()
def get_mesh_kp(lines=""): """Read Kpoints as grid.""" grid = [int(i) for i in lines[3].split()] kpts = generate_kgrid(grid) kpoints = Kpoints3D(kpoints=np.array(kpts)) return kpoints
def get_bandstruct( w=[], atoms={}, ef=0, line_density=1, ylabel="Energy (cm-1)", font=22, filename="bands.png", savefig=True, neigs=None, max_nk=None, tol=None, ): """Compare bandstructures using quantum algos.""" info = {} kpoints = Kpoints().kpath(atoms, line_density=line_density) labels = kpoints.to_dict()["labels"] kpts = kpoints.to_dict()["kpoints"] print("kpts", len(kpts)) eigvals_q = [] eigvals_np = [] for ii, i in enumerate(kpts): if max_nk is not None and ii == max_nk: break # For reducing CI/CD time print("breaking here", ii, max_nk) else: try: print("kp=", ii, i) hk = get_hk_tb(w=w, k=i) HS = HermitianSolver(hk) vqe_vals, _ = HS.run_vqd() np_vals, _ = HS.run_numpy() print("np_vals", np_vals) print("vqe_vals", vqe_vals) eigvals_q.append(vqe_vals) eigvals_np.append(np_vals) # break if (neigs is not None and isinstabce(neigs, int) and neigs == len(eigvals_q)): break except Exception as exp: print(exp) pass eigvals_q = 3.14 * np.array(eigvals_q) eigvals_np = 3.14 * np.array(eigvals_np) for ii, i in enumerate(eigvals_q.T - ef): if ii == 0: plt.plot(i, c="b", label="VQD") else: plt.plot(i, c="b") for ii, i in enumerate(eigvals_np.T - ef): if ii == 0: plt.plot(i, c="r", label="Numpy") else: plt.plot(i, c="r") new_kp = [] new_labels = [] count = 0 kp = np.arange(len(kpts)) for i, j in zip(kp, labels): if j != "": if count > 1 and count < len(labels) - 1: if labels[count] != labels[count + 1]: new_kp.append(i) new_labels.append("$" + str(j) + "$") else: new_kp.append(i) new_labels.append("$" + str(j) + "$") count += 1 info["eigvals_q"] = list(eigvals_q.tolist()) info["eigvals_np"] = list(eigvals_np.tolist()) info["kpts"] = list(kpts) info["new_kp"] = list(np.array(new_kp).tolist()) info["new_labels"] = list(new_labels) info["ef"] = ef print(info) if tol is not None: plt.ylim([tol, np.max(eigvals_q)]) plt.rcParams.update({"font.size": font}) plt.xticks(new_kp, new_labels) plt.ylabel(ylabel) plt.legend() plt.tight_layout() if savefig: plt.savefig(filename) plt.close() else: plt.show() return info
def runjob(self): """Provide main function for running a generic VASP calculation.""" # poscar=self.poscar # incar=self.incar # kpoints=self.kpoints # copy_files=self.copy_files # cwd = str(os.getcwd()) if self.jobname == "": jobname = str(self.poscar.comment) # job_dir = str(self.jobname) run_file = (str(os.getcwd()) + str("/") + str(self.jobname) + str(".json")) run_dir = str(os.getcwd()) + str("/") + str(self.jobname) if self.poscar.comment.startswith("Surf"): [a, b, c] = self.kpoints.kpts[0] # self.kpoints.kpts = [[a, b, 1]] self.kpoints = Kpoints3D(kpoints=[[a, b, 1]]) try: pol = self.poscar.atoms.check_polar if pol: COM = self.poscar.atoms.get_center_of_mass() print("COM=", COM) print("Found polar surface,setting dipole corrections") self.incar.update({ "LDIPOL": ".TRUE.", "IDIPOL": 3, "ISYM": 0, "DIPOL": str(COM[0]) + str(" ") + str(COM[2]) + str(" ") + str(COM[2]), }) print( "Polar surface encountered in run_job", self.poscar.comment, ) except Exception: pass wait = False json_file = str(self.jobname) + str(".json") print( "json should be here=", str(os.getcwd()) + str("/") + str(json_file), ) print("json should be=", json_file, run_file, os.getcwd()) if os.path.exists(str(os.getcwd()) + str("/") + str(json_file)): try: data_cal = loadjson( str(os.getcwd()) + str("/") + str(json_file)) tmp_outcar = (str(os.getcwd()) + str("/") + str(json_file.split(".json")[0]) + str("/OUTCAR")) print("outcar is", tmp_outcar) wait = Outcar(tmp_outcar).converged # True print("outcar status", wait) if wait: f_energy = data_cal[0]["final_energy"] contcar = (str(os.getcwd()) + str("/") + str(json_file.split(".json")[0]) + str("/CONTCAR")) return f_energy, contcar except Exception: pass attempt = 0 while not wait: attempt = attempt + 1 if attempt == self.attempts: wait = True # print("Setting up POTCAR") # if self.potcar is None: # new_symb = list(set(self.poscar.atoms.elements)) # self.potcar = Potcar(elements=new_symb, pot_type=self.pot_type) if not os.path.exists(run_dir): print("Starting new job") os.makedirs(run_dir) os.chdir(run_dir) self.poscar.write_file("POSCAR") else: os.chdir(run_dir) if os.path.isfile("OUTCAR"): try: wait = Outcar( "OUTCAR" ).converged # Vasprun("vasprun.xml").converged # wait=Vasprun("vasprun.xml").converged except Exception: pass try: self.potcar.write_file("POTCAR") print("FOUND OLD CONTCAR in", os.getcwd()) copy_cmd = str("cp CONTCAR POSCAR") self.poscar.write_file("POSCAR") # pos = Poscar.from_file("CONTCAR") print("copy_cmd=", copy_cmd) if ("ELAST" not in jobname and "LEPSILON" not in jobname): # Because in ELASTIC calculations # structures are deformed os.system(copy_cmd) # time.sleep(3) except Exception: pass self.incar.write_file("INCAR") self.potcar.write_file("POTCAR") self.kpoints.write_file("KPOINTS") for i in self.copy_files: print("copying", i) shutil.copy2(i, "./") self.run() # .wait() print("Queue 1") if os.path.isfile("OUTCAR"): try: wait = Outcar( "OUTCAR").converged # Vasprun("vasprun.xml").converged except Exception: pass print("End of the first loop", os.getcwd(), wait) f_energy = "na" # enp = "na" contcar = str(os.getcwd()) + str("/") + str("CONTCAR") final_str = Poscar.from_file(contcar).atoms vrun = Vasprun("vasprun.xml") f_energy = float(vrun.final_energy) # enp = float(f_energy) / float(final_str.num_atoms) # natoms = final_str.num_atoms os.chdir("../") if wait: data_cal = [] data_cal.append({ "jobname": self.jobname, "poscar": self.poscar.atoms.to_dict(), "incar": self.incar.to_dict(), "kpoints": self.kpoints.to_dict(), "final_energy": (f_energy), "contcar": final_str.to_dict(), }) json_file = str(self.jobname) + str(".json") f_json = open(json_file, "w") f_json.write(json.dumps(data_cal)) f_json.close() print("Wrote json file", f_energy) return f_energy, contcar
def test_extra_spgs(): from jarvis.db.figshare import data d = data("dft_3d") few_spgs = { "JVASP-4663": 119, "JVASP-4666": 194, "JVASP-588": 160, "JVASP-4669": 191, "JVASP-4672": 141, "JVASP-581": 164, "JVASP-4687": 139, "JVASP-4693": 62, "JVASP-4696": 187, "JVASP-4711": 225, "JVASP-4714": 2, "JVASP-4723": 60, "JVASP-32": 167, "JVASP-107": 186, "JVASP-4756": 63, "JVASP-96": 216, "JVASP-4334": 123, "JVASP-4222": 11, "JVASP-4804": 156, "JVASP-329": 129, "JVASP-4852": 163, "JVASP-155": 61, "JVASP-4340": 51, "JVASP-4343": 15, "JVASP-4361": 14, "JVASP-137": 72, "JVASP-4879": 166, "JVASP-4885": 19, "JVASP-4894": 31, "JVASP-4216": 12, "JVASP-4918": 121, "JVASP-4948": 25, "JVASP-4957": 221, "JVASP-4960": 65, "JVASP-5059": 189, "JVASP-5071": 66, "JVASP-5074": 150, "JVASP-5086": 74, "JVASP-4388": 64, "JVASP-4391": 136, "JVASP-5155": 127, "JVASP-5185": 43, "JVASP-5197": 59, "JVASP-5212": 29, "JVASP-164": 176, "JVASP-5224": 137, "JVASP-5227": 148, "JVASP-4234": 193, "JVASP-5257": 7, "JVASP-5266": 140, "JVASP-4397": 33, "JVASP-5317": 8, "JVASP-5332": 13, "JVASP-5353": 16, "JVASP-5371": 4, "JVASP-5407": 229, "JVASP-5416": 147, "JVASP-5509": 52, "JVASP-5536": 70, "JVASP-5560": 71, "JVASP-5680": 28, "JVASP-5740": 6, "JVASP-5839": 162, "JVASP-5863": 79, "JVASP-110": 99, "JVASP-579": 38, "JVASP-4501": 5, "JVASP-91": 227, "JVASP-41": 154, "JVASP-4516": 96, "JVASP-4564": 82, "JVASP-4645": 130, "JVASP-152": 55, "JVASP-4792": 88, "JVASP-5041": 107, "JVASP-5425": 87, "JVASP-5464": 115, "JVASP-5650": 157, "JVASP-4450": 36, "JVASP-22520": 152, "JVASP-22523": 205, "JVASP-11998": 53, "JVASP-22528": 58, "JVASP-22533": 41, "JVASP-12091": 32, "JVASP-22541": 215, "JVASP-22543": 97, "JVASP-22549": 9, "JVASP-12103": 138, "JVASP-22575": 40, "JVASP-22602": 180, "JVASP-22611": 182, "JVASP-12022": 85, "JVASP-22637": 111, "JVASP-12139": 10, "JVASP-22709": 92, "JVASP-12060": 20, "JVASP-12064": 1, "JVASP-12194": 185, "JVASP-13885": 128, "JVASP-14096": 56, "JVASP-14020": 122, "JVASP-13904": 54, "JVASP-13783": 135, "JVASP-14213": 26, "JVASP-14034": 23, "JVASP-14158": 113, "JVASP-14256": 21, "JVASP-32150": 217, "JVASP-28392": 224, "JVASP-32180": 146, "JVASP-32197": 57, "JVASP-31813": 67, "JVASP-31819": 219, "JVASP-29262": 39, "JVASP-29281": 102, "JVASP-31825": 226, "JVASP-29425": 143, "JVASP-29441": 42, "JVASP-29520": 73, "JVASP-29526": 18, "JVASP-29555": 149, "JVASP-29597": 151, "JVASP-29705": 174, "JVASP-31921": 199, "JVASP-33147": 161, "JVASP-33220": 46, "JVASP-33344": 114, "JVASP-30263": 44, "JVASP-33832": 155, "JVASP-30458": 69, "JVASP-30461": 144, "JVASP-30518": 132, "JVASP-36540": 198, "JVASP-36548": 220, "JVASP-36568": 3, "JVASP-36573": 145, "JVASP-35061": 131, "JVASP-35137": 125, "JVASP-35222": 204, "JVASP-35344": 109, "JVASP-42053": 173, "JVASP-40216": 91, "JVASP-38594": 165, "JVASP-37330": 86, "JVASP-37573": 84, "JVASP-36714": 47, "JVASP-36754": 98, "JVASP-59313": 230, "JVASP-46893": 95, "JVASP-46896": 76, "JVASP-45779": 30, "JVASP-45831": 158, "JVASP-46446": 35, "JVASP-44393": 159, "JVASP-44773": 22, "JVASP-47741": 78, "JVASP-47811": 181, "JVASP-48055": 94, "JVASP-48916": 34, "JVASP-49907": 190, "JVASP-50342": 223, "JVASP-50360": 68, "JVASP-50431": 37, "JVASP-52902": 142, "JVASP-52377": 24, "JVASP-50791": 214, "JVASP-54512": 108, "JVASP-56567": 213, "JVASP-54867": 126, "JVASP-55180": 81, "JVASP-57780": 212, "JVASP-57807": 118, "JVASP-57816": 50, "JVASP-57100": 197, "JVASP-57138": 116, "JVASP-58233": 124, "JVASP-59682": 200, "JVASP-20180": 206, "JVASP-21448": 203, "JVASP-40468": 90, "JVASP-42914": 17, "JVASP-21594": 100, "JVASP-21706": 188, "JVASP-22783": 218, "JVASP-24006": 202, "JVASP-30879": 83, "JVASP-31186": 49, "JVASP-21031": 110, "JVASP-21116": 192, "JVASP-25245": 134, "JVASP-7066": 169, "JVASP-13017": 112, "JVASP-58953": 105, "JVASP-8682": 183, "JVASP-9902": 80, "JVASP-34882": 208, "JVASP-34330": 179, "JVASP-34502": 48, "JVASP-62982": 178, } """ mem=[] spgs=[] pos=[] info=defaultdict() for i in d: a=Atoms.from_dict(i['atoms']) spg=Spacegroup3D(a).space_group_number if spg not in spgs: spgs.append(spg) pos.append(i['jid']) info[i['jid']]=spg print (info) if len(spgs)==230: break """ for i, j in few_spgs.items(): for ii in d: if ii["jid"] == i: a = Atoms.from_dict(ii["atoms"]) spg = Spacegroup3D(a).space_group_number assert j == spg lattice_mat = a.lattice_mat kp = Kpoints3D().automatic_length_mesh(lattice_mat=lattice_mat, length=40) sym = kp.high_symm_path(a)._path # print (ii['jid'],a) # TODO: following line failing for JVASP-4222 # x, y = kp.interpolated_points(a) break