def test(self, structure): failures = [] if self.is_valid: if not structure.is_valid(): failures.append("IS_VALID=False") if self.potcar_exists: elements = structure.composition.elements if set(elements).intersection(set(self.NO_POTCARS)): failures.append("POTCAR_EXISTS=False") if self.max_natoms: if structure.num_sites > self.max_natoms: failures.append("MAX_NATOMS=Exceeded") if self.is_ordered: if not structure.is_ordered: failures.append("IS_ORDERED=False") if self.not_in_MP: mpr = MPRester(self.MAPI_KEY) mpids = mpr.find_structure(structure) if mpids: if self.require_bandstructure: for mpid in mpids: try: bs = mpr.get_bandstructure_by_material_id(mpid) if bs: failures.append("NOT_IN_MP=False ({})".format(mpid)) except: pass else: failures.append("NOT_IN_MP=False ({})".format(mpids[0])) return True if not failures else False
def test(self, structure): failures = [] if self.is_valid: if not structure.is_valid(): failures.append("IS_VALID=False") if self.potcar_exists: elements = structure.composition.elements if set(elements).intersection(set(self.NO_POTCARS)): failures.append("POTCAR_EXISTS=False") if self.max_natoms: if structure.num_sites > self.max_natoms: failures.append("MAX_NATOMS=Exceeded") if self.is_ordered: if not structure.is_ordered: failures.append("IS_ORDERED=False") if self.not_in_MP: mpr = MPRester(self.MAPI_KEY) mpids = mpr.find_structure(structure) if mpids: if self.require_bandstructure: for mpid in mpids: try: bs = mpr.get_bandstructure_by_material_id(mpid) if bs: failures.append("NOT_IN_MP=False ({})".format(mpid)) except: pass else: failures.append("NOT_IN_MP=False ({})".format(mpids[0])) return True if not failures else False
import json from pymatgen import MPRester mpr = MPRester('fn8WQGgT9rvZAh6H') bs=mpr.get_bandstructure_by_material_id('mp-3315').as_dict() #,line_mode=True) with open("test.json", "w") as outfile: json.dump(bs, outfile, indent=4)
#!/usr/bin/env python # -*- coding: utf-8 -*- import math from pymatgen import MPRester from pymatgen.electronic_structure.plotter import BSPlotter a = MPRester("wuoepAHZnBtMv4gC") bs = a.get_bandstructure_by_material_id("mp-3748") print(bs.is_metal()) print(bs.get_band_gap()) print(bs.get_direct_band_gap()) plotter = BSPlotter(bs) plotter.get_plot().show()
def plot_band_structure(mpid, band_index, width, k_highlight, pattern): """ - Plots two bands (which one is indicated by band_index) - Highlights a window of width `width` at k `k_highlight` """ mpr = MPRester('4JzM8sNnMkyVJceK') bs = mpr.get_bandstructure_by_material_id(mpid) dos = mpr.get_dos_by_material_id(mpid) kpoints = pymatgen.Kpoints(bs) doscar = pymatgen.Doscar(dos) fermi_energy = doscar.fermi_energy eigenval = pymatgen.Eigenval(bs, fermi_energy) bands = eigenval.spin_up lower_fermi_band_index = np.argmax(np.nanmax(bands, axis=1) > 0) - 1 """ segment_sizes = kpoints.segment_sizes segmented_lower_band = np.split(bands[lower_fermi_band_index+band_index], np.cumsum(segment_sizes))[:-1] segmented_upper_band = np.split(bands[lower_fermi_band_index+band_index+1], np.cumsum(segment_sizes))[:-1] segmented_kpoints = np.split(eigenval.k_points, np.cumsum(segment_sizes))[:-1] last_k = 0 """ k = eigenval.k_points band_l = bands[lower_fermi_band_index + band_index] band_u = bands[lower_fermi_band_index + band_index + 1] #for k, band_l, band_u in zip(segmented_kpoints, segmented_lower_band, segmented_upper_band): #k_1D = np.array(path_1D(k)) + last_k k_1D = np.array(path_1D(k)) plt.plot(k_1D, band_l, 'k') plt.plot(k_1D, band_u, 'k') #last_k = np.max(k_1D) plt.axvspan(float(k_highlight), float(k_highlight) + width, color='red', alpha=0.5) # Collect axis ticks (high symmetry points) from KPOINTS.gz last_k = 0 label_k = [] label_names = [] #NOTE: Labels for none are question marks for left_k, right_k in pymatgen.chunks(kpoints.k_points, 2): if len(label_names) == 0: label_k.append(last_k) #label_names.append(left_k[0]) #elif label_names[-1] != left_k[0]: #label_names[-1] += (';' + left_k[0]) last_k += np.linalg.norm(right_k[1] - left_k[1]) label_k.append(last_k) #label_names.append(right_k[0]) #plt.xticks(label_k, label_names) #plt.xticks(label_k) plt.ylabel('Energy') plt.xlabel('k') plt.grid(True) plt.title('material ' + mpid) plt.savefig('misc/' + pattern + '_search_result.png') plt.show()
default=10, help='number of trees in annoy index') opt = parser.parse_args() print(opt) # Annoy index stores the electronic band structure with each vector corresponding to a sliding window. annoyindex = AnnoyIndex(int(2 * opt.dimensions), metric='angular') # A lookuptable is necessary to link annoy vectors to their correct material and k-space position. lookuptable = [] for mpid in ["mp-2090"]: mpr = MPRester('4JzM8sNnMkyVJceK') bs = mpr.get_bandstructure_by_material_id(mpid) dos = mpr.get_dos_by_material_id(mpid) kpoints = pymatgen.Kpoints(bs) doscar = pymatgen.Doscar(dos) if doscar.converged == False: continue # skip calculations that did not converge fermi_energy = doscar.fermi_energy eigenval = pymatgen.Eigenval(bs, fermi_energy) bands = eigenval.spin_up # lower_fermi_band_index indicates the band just below Fermi level lower_fermi_band_index = np.argmax(np.nanmax(bands, axis=1) > 0) - 1
# plot(np.array(bs.bands[sp])[iband-1,:].T-bs.efermi) # from MP # plot(np.array(bs.bands[sp])[iband-2,:].T-bs.efermi) # from MP # plot(np.array(bs.bands[sp])[iband-3,:].T-bs.efermi) # from MP plot(en, color='b') # interpolated by BoltzTraP show() if __name__ == "__main__": # user inputs PbTe_id = 'mp-19717' # valence_idx = 9 Si_id = 'mp-149' # valence_idx = 4 GaAs_id = 'mp-2534' # valence_idx = ? SnSe2_id = "mp-665" Si_bs = api.get_bandstructure_by_material_id(Si_id) bs = Vasprun('GaAs_28electron_ncsf_line_vasprun.xml').get_band_structure( kpoints_filename='GaAs_28electron_KPOINTS', line_mode=True) GaAs_st = api.get_structure_by_material_id(GaAs_id) bs.structure = GaAs_st # Si_bs.structure = api.get_structure_by_material_id(Si_id) Si_bs.structure = Vasprun("../test_files/Si/vasprun.xml").final_structure print(bs.get_sym_eq_kpoints([0.5, 0.5, 0.5])) print(bs.get_sym_eq_kpoints([0.5, 0., 0.5])) # vbm_idx = bs.get_vbm()['band_index'][Spin.up][0] vbm_idx, _ = get_bindex_bspin(Si_bs.get_vbm(), is_cbm=False) print('vbm band index (vbm_idx): {}'.format(vbm_idx)) ibands = [1, 2] # in this notation, 1 is the last valence band ibands = [i + vbm_idx for i in ibands]
comp = Composition("Fe2O3") # 获得带有材料标识的结构, 假设你想找到所有与fe2o3类似的化学计量结构 anon_formula = comp.anonymized_formula # We need to convert the formula to the dict form used in the database. anon_formula = { m.group(1): int(m.group(2)) for m in re.finditer(r"([A-Z]+)(\d+)", anon_formula) } data = mpr.query({"anonymous_formula": anon_formula}, properties=["task_id", "pretty_formula", "structure"]) print(len(data)) #Should show ~600 data.比较慢,多等一会儿。 # data now contains a list of dict. This shows you what each dict has. # Note that the mp id is named "task_id" in the database itself. pprint.pprint(data[0]) # # 得到“能带结构”(Getting band structures) # 能带结构是相当大的物体。不建议你一次下载大量的绷带结构,而是直接下载你需要的。Band structures are fairly large objects. It is not recommended that you download large quantities of bandstructures in one shot, but rather just download the ones you need. bs = mpr.get_bandstructure_by_material_id( "mp-20470") #这个是material上对应的编号,需要自己去查找写自己需要的 from pymatgen.electronic_structure.plotter import BSPlotter # %matplotlib inline plotter = BSPlotter(bs) plotter.show() elastic_data = mpr.query( {"elasticity": { "$exists": True }}, properties=["task_id", "pretty_formula", "elasticity"]) print(len(elastic_data)) pprint.pprint(elastic_data[0]) # # More resources # In general, almost any data can be obtained from MP using the MPRester, either via the high-level functions or the very powerful "query" method. # For more complex queries, you can refer to the documentation for the Materials API at https://github.com/materialsproject/mapidoc. # # Fitting structures
import numpy as np import matplotlib.pyplot as plt import pymatgen from pymatgen import MPRester import sys sys.path.append('/home/will/Research/model_fitting/si_model_fitting') import chkinfo hf2ev = 27.2114 if __name__=='__main__': mpr = MPRester(api_key='D9uwc2I45fGbd8FU') bs = mpr.get_bandstructure_by_material_id('mp-149', line_mode=False) mproj_kpts = np.array([k._ccoords for k in bs.kpoints]) xinds = np.nonzero(np.dot(mproj_kpts,[1,0,0])/np.linalg.norm(mproj_kpts, axis=1)==-1)[0] key = list(bs.bands.keys())[0] mproj_bands = bs.bands[key] vbm = 3 cbm = 4 print(mproj_bands.shape) print(np.round(mproj_kpts[[0,10,90]], 3)) xkpts = np.abs(mproj_kpts[xinds,0]) print(xinds) print(np.round(xkpts,3)) print(bs.kpoints[0].__dict__.keys()) plt.plot(xkpts, mproj_bands[:6,xinds].T) plt.axhline(y=mproj_bands[vbm,0], ls=':') plt.axhline(y=mproj_bands[cbm,90], ls=':')