class mp_query(): def __init__(self, api_key): self.mpr = MPRester(api_key) def find_access_strings(self, search): data = self.mpr.get_data(search) material_ids = [datum['material_id'] for datum in data] return material_ids def mp_structure(self, material_id): struct = self.mpr.get_structure_by_material_id(material_id) struct = SpacegroupAnalyzer( struct).get_conventional_standard_structure() return struct def make_structures(self, search): material_ids = self.find_access_strings(search) structures = [] for ID in material_ids: struct = self.mp_structure(ID) structures.append(struct) return structures
def get_mp_structure(cls, mpid): """ Get a structure from MP. :param mpid: Materials Project id. :return: Structure """ m = MPRester() return m.get_structure_by_material_id(mpid)
def submit_tests(names=None, params=None): sma = SubmissionMongoAdapter.auto_load() # note: TiO2 is duplicated twice purposely, duplicate check should catch this compounds = { "Si": 149, "Al": 134, "ZnO": 2133, "FeO": 18905, "LiCoO2": 601860, "LiFePO4": 585433, "GaAs": 2534, "Ge": 32, "PbTe": 19717, "YbO": 1216, "SiC": 567551, "Fe3C": 510623, "SiO2": 547211, "Na2O": 2352, "InSb (unstable)": 10148, "Sb2O5": 1705, "N2O5": 554368, "BaTiO3": 5020, "Rb2O": 1394, "TiO2": 554278, "TiO2 (2)": 554278, 'BaNbTePO8': 560794, "AgCl": 22922, "AgCl (2)": 570858, "SiO2 (2)": 555211, "Mg2SiO4": 2895, "CO2": 20066, "PbSO4": 22298, "SrTiO3": 5532, "FeAl": 2658, "AlFeCo2": 10884, "NaCoO2": 554427, "ReO3": 547271, "LaH2": 24153, "SiH3I": 28538, "LiBH4": 30209, "H8S5N2": 28143, "LiOH": 23856, "SrO2": 2697, "Mn": 35, "Hg4Pt": 2312, "PdF4": 13868, "Gd2WO6": 651333, 'MnO2': 19395, 'VO2': 504800 } mpr = MPRester() for name, sid in compounds.iteritems(): if not names or name in names: sid = mpr.get_materials_id_from_task_id("mp-{}".format(sid)) s = mpr.get_structure_by_material_id(sid, final=False) snl = StructureNL(s, 'Anubhav Jain <*****@*****.**>') parameters = {'priority': 10} if name == 'Si' else None if params: parameters.update(params) sma.submit_snl(snl, '*****@*****.**', parameters=parameters)
comp = Composition(phase).as_dict() file_dir = './' + phase + '/' # checking if path already exists - if not create it if not os.path.exists(file_dir): os.makedirs(file_dir) else: print(f'{file_dir} already exists\n-----') # writing materials ID in file mid_file = open(file_dir + 'material-ID', 'w') struct = mpr.get_structure_by_material_id(stable_phases[phase], final=False, conventional_unit_cell=True) # getting space group of structure spacegroup = struct.get_space_group_info() # prova = mpr.get_entry_by_material_id(stable_phases[phase] , inc_structure = 'final').as_dict() print( f'Structure acquired :\n - Reduced formula = {phase} , Space group = {spacegroup}, Materials ID = {stable_phases[phase]} \n --------' ) #print(struct) print('') list_file.write(f'{phase} : {spacegroup} {stable_phases[phase]}\n') mid_file.write(f'{stable_phases[phase]}') mid_file.close() # POSCAR
rest = MPRester('XaCJrv4nBIeuy3kd') params_dict = {} structures = {} psps = os.listdir( '/home/bcomer3/sparc/ase_sparc/pysparcx/sparc/pseudos/LDA_pseudos/') available_psps = [a.split('.')[0] for a in psps] # metals metals = ['Al', 'Ru', 'Cu', 'Bi'] for metal in metals: struc_l = rest.get_entries(metal, sort_by_e_above_hull=True) struc_l = struc_l[0] struct = rest.get_structure_by_material_id(struc_l.entry_id) atoms = AseAtomsAdaptor.get_atoms(struct) # switch to ASE for millers in [(1, 1, 1)]: slab = surface(atoms, millers, 4, vacuum=6) slab *= (2, 2, 1) n_metal = metal if millers == (1, 1, 1): if metal == 'Cu': add_adsorbate(slab, molecule('CO'), 2.5, (0, 1.64)) n_metal += '_COads' structure = AseAtomsAdaptor.get_structure(slab) add_to_dict(structure, n_metal, millers) metals = ['In', 'Rb', 'Sr', 'Te']
class MPData(Dataset): """ The MPData dataset is a wrapper for a dataset from Materials Project. atom_init.json: a JSON file that stores the initialization vector for each element. ID.cif: a CIF file that recodes the crystal structure, where ID is the unique ID for the crystal. Parameters ---------- criteria: dict Mongo-like query language for MP structures atom_init_file_dir: str dir of the atom_init_file.json max_num_nbr: int The maximum number of neighbors while constructing the crystal graph radius: float The cutoff radius for searching neighbors dmin: float The minimum distance for constructing GaussianDistance step: float The step size for constructing GaussianDistance random_seed: int Random seed for shuffling the dataset Returns ------- atom_fea: torch.Tensor shape (n_i, atom_fea_len) nbr_fea: torch.Tensor shape (n_i, M, nbr_fea_len) nbr_fea_idx: torch.LongTensor shape (n_i, M) target: torch.Tensor shape (1, ) cif_id: str or int """ def __init__(self, criteria, atom_init_file_dir, api_key="VIqD4QUxH6wNpyc5", max_num_nbr=12, radius=8, dmin=0, step=0.2, random_seed=123): self.api = MPRester(api_key) mp_list = [ i['material_id'] for i in self.api.query(criteria=criteria, properties=['material_id']) ] self.max_num_nbr, self.radius = max_num_nbr, radius atom_init_file = atom_init_file_dir self.ari = AtomCustomJSONInitializer(atom_init_file) self.gdf = GaussianDistance(dmin=dmin, dmax=self.radius, step=step) self.id_prop_data = [(target, cif_id) for target, cif_id in enumerate(mp_list)] def __len__(self): return len(self.id_prop_data) @functools.lru_cache(maxsize=None) def __getitem__(self, idx): target, cif_id = self.id_prop_data[idx] crystal = self.api.get_structure_by_material_id(cif_id) atom_fea = np.vstack([ self.ari.get_atom_fea(crystal[i].specie.number) for i in range(len(crystal)) ]) atom_fea = torch.Tensor(atom_fea) all_nbrs = crystal.get_all_neighbors(self.radius, include_index=True) all_nbrs = [sorted(nbrs, key=lambda x: x[1]) for nbrs in all_nbrs] nbr_fea_idx = np.array([ list( map(lambda x: x[2], nbr[:self.max_num_nbr]) for nbr in all_nbrs) ]) nbr_fea = np.array([list(map(lambda x: x[1], nbr[: self.max_num_nbr])) \ for nbr in all_nbrs] ) atom_fea = torch.Tensor(atom_fea) nbr_fea = torch.Tensor(nbr_fea) nbr_fea_idx = torch.LongTensor(nbr_fea_idx) target = torch.Tensor([float(target)]) return (atom_fea, nbr_fea, nbr_fea_idx), target, cif_id
def cif_lib_build(self, crystal_system, size_limit=None): ''' function to build cif and pdf library based on space group symbol Parameters ---------- crystal_system: str name of crystal system. It capitalized, like CUBIC. space group symbol will be generated by get_symbol_list method size_list : int optional. Uppder limit of data pulled out per symbol ''' self.crystal_system = crystal_system space_group_symbol = self.get_symbol_list(crystal_system) if isinstance(space_group_symbol, list): space_group_symbol_set = space_group_symbol else: space_group_symbol_set = list(spac_group_symbol) ## changing dir data_dir = os.path.join(self.working_dir, crystal_system) self.data_dir = data_dir self._makedirs(data_dir) os.chdir(data_dir) if os.getcwd() == data_dir: print('Library will be built at %s' % data_dir) else: e = 'Werid, return' raise RuntimeError(e) # summary lists missed_list = [] # reference m_id_list = [] # reference for searchs have been done in the past time_info = time.strftime('%Y-%m-%d') # create dirs, cif and calculated dir cif_dir = os.path.join(data_dir, 'cif_data') self._makedirs(cif_dir) self.cif_dir = cif_dir # looping for space_group_symbol in space_group_symbol_set: print('Building library with space_group symbol: {}'.format(space_group_symbol)) ## search query m = MPRester(self.API_key) search = m.query(criteria = {"spacegroup.symbol": space_group_symbol}, properties = ["material_id"]) if search: ## crazy looping if size_limit: dim = 400 # 400 data sets per symbol else: dim = len(search) print('Pull out %s data sets' % dim) print('Now, starts to save cif and compute pdf...') for i in range(dim): # part 1: grab cif files from data base m_id = search[i]['material_id'] m_id_list.append(m_id) m_struc = m.get_structure_by_material_id(m_id) m_formula = m_struc.formula m_name = m_formula.replace(' ', '') # material name cif_w = CifWriter(m_struc) cif_name = '{}_{}.cif'.format(space_group_symbol, m_name) cif_w_name = os.path.join(cif_dir, cif_name) if os.path.isfile(cif_w_name): print('already have {}, skip'.format(cif_name)) pass # skip files already exist else: cif_w.write_file(cif_w_name) print('{} has been saved'.format(cif_name)) else: print('Hmm, no reasult. Something wrong') missed_list.append(space_group_symbol) pass m_id_list_name = '{}_{}_material_id.txt'.format(crystal_system, time_info) m_id_list_w_name = os.path.join(data_dir, m_id_list_name) np.savetxt(m_id_list_w_name, m_id_list) print('''SUMMARY: for {} cystsal sytem, Symbols {} can't be found from data base'''.format(crystal_system, missed_list)) return cif_dir
from pymatgen.io.pwscf import PWInput from pymatgen import MPRester, Structure mpr = MPRester() s: Structure = mpr.get_structure_by_material_id('mp-550893') s.lattice s.make_supercell([[-1, -1, 1], [-1, 1, -1], [1, -1, -1]]) pwin = PWInput(s, pseudo={ 'Hf': 'Hf.pbe-spn-kjpaw_psl.1.0.0.UPF', 'O': 'O.pbe-n-kjpaw_psl.1.0.0.UPF' }, kpoints_grid=(6, 6, 6)) pwin.write_file('Hf-scf.in')
#this is to build a database for cnn to use import pandas as pd from pymatgen import Structure from pymatgen import MPRester import matplotlib.pyplot as plt api_key = 'x3NlvC67Z9tPykwGz' mpr = MPRester(api_key) df = pd.read_excel( r'D:\FYP_files\database\data_after_processing\CGCNN_bulkmodulus_traindata.xlsx' ) #this is how to get the cif files for i in range( 0, 13110 ): #this is the number of data in 2019/12/1, there must be more datas structure = mpr.get_structure_by_material_id("%s" % df.iat[i, 0]) structure.to( "cif", "D:/FYP_files/Deep_learning/cgcnn-master/data/example/%s.cif" % df.iat[i, 0]) print(i) # python main.py --train-ratio 0.6 --val-ratio 0.2 --test-ratio 0.2 data/sample-regression # python predict.py pre-trained/formation-energy-per-atom.pth.tar data/sample-regression #python init.py --idx 2
# 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] PbTe_coeff_file = '../test_files/PbTe/fort.123' Si_coeff_file = "../test_files/Si/Si_fort.123"
options.dimension = len(system) for entry in mp_entries: accept = False if type(system) is list: if len(entry.composition) >= options.dimension: eng = pd.get_e_above_hull(entry) if eng <= options.cutoff: accept = True else: eng = entry.energy_per_atom accept = True if accept: struc = output_struc(entry, eng, tol=1e-2) strucs.append(struc) else: strucs.append(mpr.get_structure_by_material_id(options.id)) if options.format == 'poscar': filename = 'MPR.vasp' else: filename = 'MPR.cif' if os.path.isfile(filename): os.remove(filename) for struc in strucs: if options.format == 'poscar': content = struc.to(fmt='poscar') else: content = str(CifWriter(struc, symprec=0.01)) with open(filename, 'a+') as f:
PlotDict = {} for index in MI_List: for key in MI_lib.keys(): if any((index == x).all() for x in MI_lib[key]): if key not in PlotDict.keys(): PlotDict[key] = 0 PlotDict[key] += 1 print(index, MI_lib[key]) import matplotlib.pyplot as plt plt.bar(PlotDict.keys(), PlotDict.values(), color='g') plt.show() def MillerIndexLibrary(structure): MI_lib = {} UniqueIndices = get_symmetrically_distinct_miller_indices(structure, max_index=4) for Index in UniqueIndices: MI_lib[str(Index)] = get_sym_fam( structure).get_equivalent_miller_indices(Index) return MI_lib MPR = MPRester("2d5wyVmhDCpPMAkq") S = MPR.get_structure_by_material_id('mp-1143', conventional_unit_cell='True') print(S) MI_lib = MillerIndexLibrary(S) print(MI_lib) surfscreen(S, MI_lib)
def v_ij(self, ri, rj, arr): a_ij = 1 beta = 0.33675 n = 22.956 c = 4.8381 d = 2.0417 r_ij = np.linalg.norm(ri, rj) ksi = lambda: sum([ self.fc(norm(ri, rk)) * g_theta(Functional.get_angle(ri, rj, rk)) * math.exp(self.lambda2**3 * (norm(ri, rj) - norm(ri, rk))**3) for rk in arr ]) b_ij = lambda: 1 + beta**n * ksi()**n g_theta = lambda theta: 1 + c**2 / d**2 - c**2 / (d**2 + (math.cos(theta))**2) return self.fc(r_ij) * (a_ij * b_ij() * self.fa(r_ij)) mpr = MPRester('DhmFQPuibZo8JtXn') s: Structure = mpr.get_structure_by_material_id('mp-149') for i in s.get_all_neighbors(3, True): for j in i: print(j) #f = Functional() #x = np.logspace(-5, 2, 1000) #y = [f.fc(r) for r in x] #plt.plot(x, y) #plt.show()
q_pool = [] for x,y in enumerate(q): q_pool.append([q[x]['energy_per_atom'],q[x]['volume'],q[x]['material_id'],q[x]['pretty_formula']]) q_pool = sorted(q_pool, key = lambda x:x[0], reverse=True) q_min = [] for m,n in enumerate(q_pool): min_ = q_pool[-1][0] if q_pool[m][0] - min_ <= 0.05: q_min.append(q_pool[m]) q_min = sorted(q_min, key = lambda x:x[1], reverse=True) print('Min energy struct') print(q_min[-1]) mp_structure = mpr.get_structure_by_material_id(q_min[-1][2],conventional_unit_cell=True) ase_atoms = bridge.get_atoms(mp_structure) ase_atoms.write('POSCAR_'+str(q_min[-1][3])+"_"+str(q_min[-1][2]),format='vasp') ''' for no, entry in enumerate(pm_atom_list): ase_atoms = bridge.get_atoms(entry) mk_dir = str(name)+'-'+str(pm_mat_id[no]) os.makedirs(mk_dir) os.chdir(mk_dir) ase_atoms = bridge.get_atoms(entry) ase_atoms.write('POSCAR',format='vasp') vol_k = int(15**3/entry.volume) k=pm_vasp.Kpoints.automatic_density_by_vol(structure=entry,kppvol=vol_k) k.write_file('KPOINTS')
num_fin = len(df_entries) createFolder('results/plots/pourbaix') with open('results/pbx_analysis.log', 'w') as f: for idx in range(num_ini, num_fin): formula = df_entries['formula'][idx] mpid = df_entries['mp_id'][idx] print('%03d_%s' % (idx + 1.0, formula)) f.writelines('%03d_%s\n' % (idx + 1.0, formula)) try: struc = mpr.get_structure_by_material_id(mpid) elements = [str(atom) for atom in struc.species[:2]] entries = mpr.get_pourbaix_entries(elements) entry_ids = [e for e in entries if e.entry_id == mpid] if len(entry_ids) == 1: pbx = PourbaixDiagram(entries) plotter = PourbaixPlotter(pbx) entry = entry_ids[0] plt = plotter.plot_entry_stability(entry) plt.savefig('results/plots/pourbaix/%03d_%s.png' % (idx + 1.0, formula), dpi=300) elif len(entry_ids) > 1: f.writelines('%03d_%s: number of entry > 1 \n' % (idx + 1.0, formula))
def get_mp_structure(cls, mpid): m = MPRester() return m.get_structure_by_material_id(mpid)
def compute_environments(chemenv_configuration): string_sources = {'cif': {'string': 'a Cif file', 'regexp': '.*\.cif$'}, 'mp': {'string': 'the Materials Project database', 'regexp': 'mp-[0-9]+$'}} questions = {'c': 'cif'} if chemenv_configuration.has_materials_project_access: questions['m'] = 'mp' lgf = LocalGeometryFinder() lgf.setup_parameters() allcg = AllCoordinationGeometries() strategy_class = strategies_class_lookup[chemenv_configuration.package_options['default_strategy']['strategy']] #TODO: Add the possibility to change the parameters and save them in the chemenv_configuration default_strategy = strategy_class() default_strategy.setup_options(chemenv_configuration.package_options['default_strategy']['strategy_options']) max_dist_factor = chemenv_configuration.package_options['default_max_distance_factor'] firsttime = True while True: if len(questions) > 1: found = False print('Enter the source from which the structure is coming or <q> to quit :') for key_character, qq in questions.items(): print(' - <{}> for a structure from {}'.format(key_character, string_sources[qq]['string'])) test = input(' ... ') if test == 'q': break if test not in list(questions.keys()): for key_character, qq in questions.items(): if re.match(string_sources[qq]['regexp'], str(test)) is not None: found = True source_type = qq if not found: print('Wrong key, try again ...') continue else: source_type = questions[test] else: found = False source_type = list(questions.values())[0] if found and len(questions) > 1: input_source = test if source_type == 'cif': if not found: input_source = input('Enter path to cif file : ') cp = CifParser(input_source) structure = cp.get_structures()[0] elif source_type == 'mp': if not found: input_source = input('Enter materials project id (e.g. "mp-1902") : ') a = MPRester(chemenv_configuration.materials_project_api_key) structure = a.get_structure_by_material_id(input_source) lgf.setup_structure(structure) print('Computing environments for {} ... '.format(structure.composition.reduced_formula)) se = lgf.compute_structure_environments_detailed_voronoi(maximum_distance_factor=max_dist_factor) print('Computing environments finished') while True: test = input('See list of environments determined for each (unequivalent) site ? ' '("y" or "n", "d" with details, "g" to see the grid) : ') strategy = default_strategy if test in ['y', 'd', 'g']: strategy.set_structure_environments(se) for eqslist in se.equivalent_sites: site = eqslist[0] isite = se.structure.index(site) try: if strategy.uniquely_determines_coordination_environments: ces = strategy.get_site_coordination_environments(site) else: ces = strategy.get_site_coordination_environments_fractions(site) except NeighborsNotComputedChemenvError: continue if ces is None: continue if len(ces) == 0: continue comp = site.species_and_occu #ce = strategy.get_site_coordination_environment(site) if strategy.uniquely_determines_coordination_environments: ce = ces[0] if ce is None: continue thecg = allcg.get_geometry_from_mp_symbol(ce[0]) mystring = 'Environment for site #{} {} ({}) : {} ({})\n'.format(str(isite), comp.get_reduced_formula_and_factor()[0], str(comp), thecg.name, ce[0]) else: mystring = 'Environments for site #{} {} ({}) : \n'.format(str(isite), comp.get_reduced_formula_and_factor()[0], str(comp)) for ce in ces: cg = allcg.get_geometry_from_mp_symbol(ce[0]) csm = ce[1]['other_symmetry_measures']['csm_wcs_ctwcc'] mystring += ' - {} ({}): {:.2f} % (csm : {:2f})\n'.format(cg.name, cg.mp_symbol, 100.0*ce[2], csm) if test in ['d', 'g'] and strategy.uniquely_determines_coordination_environments: if thecg.mp_symbol != UNCLEAR_ENVIRONMENT_SYMBOL: mystring += ' <Continuous symmetry measures> ' mingeoms = se.ce_list[isite][thecg.coordination_number][0].minimum_geometries() for mingeom in mingeoms: csm = mingeom[1]['other_symmetry_measures']['csm_wcs_ctwcc'] mystring += '{} : {:.2f} '.format(mingeom[0], csm) print(mystring) if test == 'g': test = input('Enter index of site(s) for which you want to see the grid of parameters : ') indices = list(map(int, test.split())) print(indices) for isite in indices: se.plot_environments(isite, additional_condition=se.AC.ONLY_ACB) if no_vis: test = input('Go to next structure ? ("y" to do so)') if test == 'y': break continue test = input('View structure with environments ? ("y" for the unit cell or "m" for a supercell or "n") : ') if test in ['y', 'm']: if test == 'm': mydeltas = [] test = input('Enter multiplicity (e.g. 3 2 2) : ') nns = test.split() for i0 in range(int(nns[0])): for i1 in range(int(nns[1])): for i2 in range(int(nns[2])): mydeltas.append(np.array([1.0*i0, 1.0*i1, 1.0*i2], np.float)) else: mydeltas = [np.zeros(3, np.float)] if firsttime: vis = StructureVis(show_polyhedron=False, show_unit_cell=True) vis.show_help = False firsttime = False vis.set_structure(se.structure) strategy.set_structure_environments(se) for isite, site in enumerate(se.structure): try: ces = strategy.get_site_coordination_environments(site) except NeighborsNotComputedChemenvError: continue if len(ces) == 0: continue ce = strategy.get_site_coordination_environment(site) if ce is not None and ce[0] != UNCLEAR_ENVIRONMENT_SYMBOL: for mydelta in mydeltas: psite = PeriodicSite(site._species, site._fcoords + mydelta, site._lattice, properties=site._properties) vis.add_site(psite) neighbors = strategy.get_site_neighbors(psite) draw_cg(vis, psite, neighbors, cg=lgf.cg.get_geometry_from_mp_symbol(ce[0]), perm=ce[1]['permutation']) vis.show() test = input('Go to next structure ? ("y" to do so) : ') if test == 'y': break print('')
from pymatgen.io.pwscf import PWInput from pymatgen import MPRester mpr = MPRester() s = mpr.get_structure_by_material_id('mp-20459') # s.to('POSCAR', 'POSCAR') PWInput.from_file() pwin = PWInput(s, pseudo={ 'Pb': 'Pb.pbe-dn-kjpaw_psl.1.0.0.UPF', 'Ti': 'Ti.pbe-spn-kjpaw_psl.1.0.0.UPF', 'O': 'O.pbe-n-kjpaw_psl.1.0.0.UPF' }, kpoints_grid=(6, 6, 4), control={ 'outdir': 'output', 'prefix': 'scf' }) pwin.write_file('PTO-scf.in')
def get_mp_structure(cls, mpid): m = MPRester() return m.get_structure_by_material_id(mpid)
params_dict = {} structures = {} psps = os.listdir('/home/bcomer3/sparc/ase_sparc/pysparcx/sparc/pseudos/LDA_pseudos/') available_psps = [a.split('.')[0] for a in psps] # metals metals = ['Pt', 'Sc', 'La', 'Na', 'Au', 'Os', 'V', 'Zn', 'Bi', 'Co', 'Pd'] sizes = [(4,2,2), (4,3,2), (3,1,1), (3,2,1), (1,1,7), (2,2,2), (2,1,1),(2,2,1), (1,1,1), (2,1,1), (2,2,1)] for size, metal in zip(sizes, metals): struc_l = rest.get_entries(metal, sort_by_e_above_hull=True) struc_l = struc_l[0] struct = rest.get_structure_by_material_id(struc_l.entry_id) if metal == 'Pt': struct.add_site_property('magmom', [0] * len(struct)) struct *= size if metal in ['Pt', 'Sc']: del struct[0] if metal == 'Hf': del struct[50] del struct[35] del struct[100] #print(len(struct)) if metal == 'Sc': print(len(struct)) add_to_dict(struct) # oxides
class CompositionToStructureFromMP(ConversionFeaturizer): """ Featurizer to get a Structure object from Materials Project using the composition alone. The most stable entry from Materials Project is selected, or NaN if no entry is found in the Materials Project. Args: target_col_id (str or None): The column in which the converted data will be written. If the column already exists then an error will be thrown unless `overwrite_data` is set to `True`. If `target_col_id` begins with an underscore the data will be written to the column: `"{}_{}".format(col_id, target_col_id[1:])`, where `col_id` is the column being featurized. If `target_col_id` is set to None then the data will be written "in place" to the `col_id` column (this will only work if `overwrite_data=True`). overwrite_data (bool): Overwrite any data in `target_column` if it exists. map_key (str): Materials API key """ def __init__(self, target_col_id='structure', overwrite_data=False, mapi_key=None): super().__init__(target_col_id, overwrite_data) self.mpr = MPRester(mapi_key) self.set_n_jobs(1) def featurize(self, comp): """ Get the most stable structure from Materials Project Args: comp (`pymatgen.core.composition.Composition`): A composition. Returns: (`pymatgen.core.structure.Structure`): A Structure object. """ entries = self.mpr.get_data(comp.reduced_formula, prop="energy_per_atom") if len(entries) > 0: most_stable_entry = \ sorted(entries, key=lambda e: e['energy_per_atom'])[0] s = self.mpr.get_structure_by_material_id( most_stable_entry["material_id"]) return [s] return [float("nan")] def citations(self): return [ "@article{doi:10.1063/1.4812323, author = {Jain,Anubhav and Ong," "Shyue Ping and Hautier,Geoffroy and Chen,Wei and Richards, " "William Davidson and Dacek,Stephen and Cholia,Shreyas " "and Gunter,Dan and Skinner,David and Ceder,Gerbrand " "and Persson,Kristin A. }, title = {Commentary: The Materials " "Project: A materials genome approach to accelerating materials " "innovation}, journal = {APL Materials}, volume = {1}, number = " "{1}, pages = {011002}, year = {2013}, doi = {10.1063/1.4812323}, " "URL = {https://doi.org/10.1063/1.4812323}, " "eprint = {https://doi.org/10.1063/1.4812323}}", "@article{Ong2015, author = {Ong, Shyue Ping and Cholia, " "Shreyas and Jain, Anubhav and Brafman, Miriam and Gunter, Dan " "and Ceder, Gerbrand and Persson, Kristin a.}, doi = " "{10.1016/j.commatsci.2014.10.037}, issn = {09270256}, " "journal = {Computational Materials Science}, month = {feb}, " "pages = {209--215}, publisher = {Elsevier B.V.}, title = " "{{The Materials Application Programming Interface (API): A simple, " "flexible and efficient API for materials data based on " "REpresentational State Transfer (REST) principles}}, " "url = {http://linkinghub.elsevier.com/retrieve/pii/S0927025614007113}, " "volume = {97}, year = {2015} } " ] def implementors(self): return ["Anubhav Jain"]
from pymatgen import MPRester, Structure import sys mpr = MPRester('DhmFQPuibZo8JtXn') s: Structure = mpr.get_structure_by_material_id(sys.argv[1]) s.to('POSCAR', 'POSCAR')
from pymatgen import MPRester, Structure from pymatgen.command_line.gulp_caller import GulpCaller, GulpIO from matplotlib import pyplot mpr = MPRester() mono: Structure = mpr.get_structure_by_material_id('mp-352') ortho: Structure = mpr.get_structure_by_material_id('mp-685097') tetra: Structure = mpr.get_structure_by_material_id('mp-1018721') gio = GulpIO() gc = GulpCaller() for i in [mono, ortho, tetra]: arr = [] arr2 = [] i.apply_strain(-.05) for j in range(15): gin = gio.buckingham_input(i, ['conv'], library='morse.lib', alib=True) gout = gc.run(gin) arr.append(i.density) arr2.append(gio.get_energy(gout)) i.apply_strain(-.01) i.to('POSCAR', i.get_space_group_info()[0].replace('/', '_') + '.vasp') if i.get_space_group_info()[0] == 'P4_2/nmc': pyplot.plot(arr, [r * 2 for r in arr2], label=i.get_space_group_info()[0]) else: pyplot.plot(arr, arr2, label=i.get_space_group_info()[0]) pyplot.legend() pyplot.show()
class CompositionToStructureFromMP(ConversionFeaturizer): """ Featurizer to get a Structure object from Materials Project using the composition alone. The most stable entry from Materials Project is selected, or NaN if no entry is found in the Materials Project. Args: target_col_id (str or None): The column in which the converted data will be written. If the column already exists then an error will be thrown unless `overwrite_data` is set to `True`. If `target_col_id` begins with an underscore the data will be written to the column: `"{}_{}".format(col_id, target_col_id[1:])`, where `col_id` is the column being featurized. If `target_col_id` is set to None then the data will be written "in place" to the `col_id` column (this will only work if `overwrite_data=True`). overwrite_data (bool): Overwrite any data in `target_column` if it exists. map_key (str): Materials API key """ def __init__(self, target_col_id='structure', overwrite_data=False, mapi_key=None): super().__init__(target_col_id, overwrite_data) self.mpr = MPRester(mapi_key) def featurize(self, comp): """ Get the most stable structure from Materials Project Args: comp (`pymatgen.core.composition.Composition`): A composition. Returns: (`pymatgen.core.structure.Structure`): A Structure object. """ entries = self.mpr.get_data(comp.reduced_formula, prop="energy_per_atom") if len(entries) > 0: most_stable_entry = \ sorted(entries, key=lambda e: e['energy_per_atom'])[0] s = self.mpr.get_structure_by_material_id( most_stable_entry["material_id"]) return[s] return [float("nan")] def citations(self): return [ "@article{doi:10.1063/1.4812323, author = {Jain,Anubhav and Ong," "Shyue Ping and Hautier,Geoffroy and Chen,Wei and Richards, " "William Davidson and Dacek,Stephen and Cholia,Shreyas " "and Gunter,Dan and Skinner,David and Ceder,Gerbrand " "and Persson,Kristin A. }, title = {Commentary: The Materials " "Project: A materials genome approach to accelerating materials " "innovation}, journal = {APL Materials}, volume = {1}, number = " "{1}, pages = {011002}, year = {2013}, doi = {10.1063/1.4812323}, " "URL = {https://doi.org/10.1063/1.4812323}, " "eprint = {https://doi.org/10.1063/1.4812323}}", "@article{Ong2015, author = {Ong, Shyue Ping and Cholia, " "Shreyas and Jain, Anubhav and Brafman, Miriam and Gunter, Dan " "and Ceder, Gerbrand and Persson, Kristin a.}, doi = " "{10.1016/j.commatsci.2014.10.037}, issn = {09270256}, " "journal = {Computational Materials Science}, month = {feb}, " "pages = {209--215}, publisher = {Elsevier B.V.}, title = " "{{The Materials Application Programming Interface (API): A simple, " "flexible and efficient API for materials data based on " "REpresentational State Transfer (REST) principles}}, " "url = {http://linkinghub.elsevier.com/retrieve/pii/S0927025614007113}, " "volume = {97}, year = {2015} } "] def implementors(self): return ["Anubhav Jain"]
def compute_environments(chemenv_configuration): string_sources = { 'cif': { 'string': 'a Cif file', 'regexp': '.*\.cif$' }, 'mp': { 'string': 'the Materials Project database', 'regexp': 'mp-[0-9]+$' } } questions = {'c': 'cif'} if chemenv_configuration.has_materials_project_access: questions['m'] = 'mp' lgf = LocalGeometryFinder() lgf.setup_parameters() allcg = AllCoordinationGeometries() strategy_class = strategies_class_lookup[ chemenv_configuration.package_options['default_strategy']['strategy']] #TODO: Add the possibility to change the parameters and save them in the chemenv_configuration default_strategy = strategy_class() default_strategy.setup_options( chemenv_configuration.package_options['default_strategy'] ['strategy_options']) max_dist_factor = chemenv_configuration.package_options[ 'default_max_distance_factor'] firsttime = True while True: if len(questions) > 1: found = False print( 'Enter the source from which the structure is coming or <q> to quit :' ) for key_character, qq in questions.items(): print(' - <{}> for a structure from {}'.format( key_character, string_sources[qq]['string'])) test = input(' ... ') if test == 'q': break if test not in list(questions.keys()): for key_character, qq in questions.items(): if re.match(string_sources[qq]['regexp'], str(test)) is not None: found = True source_type = qq if not found: print('Wrong key, try again ...') continue else: source_type = questions[test] else: found = False source_type = list(questions.values())[0] if found and len(questions) > 1: input_source = test if source_type == 'cif': if not found: input_source = input('Enter path to cif file : ') cp = CifParser(input_source) structure = cp.get_structures()[0] elif source_type == 'mp': if not found: input_source = input( 'Enter materials project id (e.g. "mp-1902") : ') a = MPRester(chemenv_configuration.materials_project_api_key) structure = a.get_structure_by_material_id(input_source) lgf.setup_structure(structure) print('Computing environments for {} ... '.format( structure.composition.reduced_formula)) se = lgf.compute_structure_environments( maximum_distance_factor=max_dist_factor) print('Computing environments finished') while True: test = input( 'See list of environments determined for each (unequivalent) site ? ' '("y" or "n", "d" with details, "g" to see the grid) : ') strategy = default_strategy if test in ['y', 'd', 'g']: strategy.set_structure_environments(se) for eqslist in se.equivalent_sites: site = eqslist[0] isite = se.structure.index(site) try: if strategy.uniquely_determines_coordination_environments: ces = strategy.get_site_coordination_environments( site) else: ces = strategy.get_site_coordination_environments_fractions( site) except NeighborsNotComputedChemenvError: continue if ces is None: continue if len(ces) == 0: continue comp = site.species_and_occu #ce = strategy.get_site_coordination_environment(site) if strategy.uniquely_determines_coordination_environments: ce = ces[0] if ce is None: continue thecg = allcg.get_geometry_from_mp_symbol(ce[0]) mystring = 'Environment for site #{} {} ({}) : {} ({})\n'.format( str(isite), comp.get_reduced_formula_and_factor()[0], str(comp), thecg.name, ce[0]) else: mystring = 'Environments for site #{} {} ({}) : \n'.format( str(isite), comp.get_reduced_formula_and_factor()[0], str(comp)) for ce in ces: cg = allcg.get_geometry_from_mp_symbol(ce[0]) csm = ce[1]['other_symmetry_measures'][ 'csm_wcs_ctwcc'] mystring += ' - {} ({}): {:.2f} % (csm : {:2f})\n'.format( cg.name, cg.mp_symbol, 100.0 * ce[2], csm) if test in [ 'd', 'g' ] and strategy.uniquely_determines_coordination_environments: if thecg.mp_symbol != UNCLEAR_ENVIRONMENT_SYMBOL: mystring += ' <Continuous symmetry measures> ' mingeoms = se.ce_list[isite][ thecg. coordination_number][0].minimum_geometries() for mingeom in mingeoms: csm = mingeom[1]['other_symmetry_measures'][ 'csm_wcs_ctwcc'] mystring += '{} : {:.2f} '.format( mingeom[0], csm) print(mystring) if test == 'g': test = input( 'Enter index of site(s) for which you want to see the grid of parameters : ' ) indices = list(map(int, test.split())) print(indices) for isite in indices: se.plot_environments(isite, additional_condition=se.AC.ONLY_ACB) if no_vis: test = input('Go to next structure ? ("y" to do so)') if test == 'y': break continue test = input( 'View structure with environments ? ("y" for the unit cell or "m" for a supercell or "n") : ' ) if test in ['y', 'm']: if test == 'm': mydeltas = [] test = input('Enter multiplicity (e.g. 3 2 2) : ') nns = test.split() for i0 in range(int(nns[0])): for i1 in range(int(nns[1])): for i2 in range(int(nns[2])): mydeltas.append( np.array([1.0 * i0, 1.0 * i1, 1.0 * i2], np.float)) else: mydeltas = [np.zeros(3, np.float)] if firsttime: vis = StructureVis(show_polyhedron=False, show_unit_cell=True) vis.show_help = False firsttime = False vis.set_structure(se.structure) strategy.set_structure_environments(se) for isite, site in enumerate(se.structure): try: ces = strategy.get_site_coordination_environments(site) except NeighborsNotComputedChemenvError: continue if len(ces) == 0: continue ce = strategy.get_site_coordination_environment(site) if ce is not None and ce[0] != UNCLEAR_ENVIRONMENT_SYMBOL: for mydelta in mydeltas: psite = PeriodicSite(site._species, site._fcoords + mydelta, site._lattice, properties=site._properties) vis.add_site(psite) neighbors = strategy.get_site_neighbors(psite) draw_cg(vis, psite, neighbors, cg=lgf.allcg.get_geometry_from_mp_symbol( ce[0]), perm=ce[1]['permutation']) vis.show() test = input('Go to next structure ? ("y" to do so) : ') if test == 'y': break print('')
def submit_tests(names=None, params=None): sma = SubmissionMongoAdapter.auto_load() # note: TiO2 is duplicated twice purposely, duplicate check should catch this compounds = {"Si": 149, "Al": 134, "ZnO": 2133, "FeO": 18905, "LiCoO2": 601860, "LiFePO4": 585433, "GaAs": 2534, "Ge": 32, "PbTe": 19717, "YbO": 1216, "SiC": 567551, "Fe3C": 510623, "SiO2": 547211, "Na2O": 2352, "InSb (unstable)": 10148, "Sb2O5": 1705, "N2O5": 554368, "BaTiO3": 5020, "Rb2O": 1394, "TiO2": 554278, "TiO2 (2)": 554278, 'BaNbTePO8': 560794, "AgCl": 22922, "AgCl (2)": 570858, "SiO2 (2)": 555211, "Mg2SiO4": 2895, "CO2": 20066, "PbSO4": 22298, "SrTiO3": 5532, "FeAl": 2658, "AlFeCo2": 10884, "NaCoO2": 554427, "ReO3": 547271, "LaH2": 24153, "SiH3I": 28538, "LiBH4": 30209, "H8S5N2": 28143, "LiOH": 23856, "SrO2": 2697, "Mn": 35, "Hg4Pt": 2312, "PdF4": 13868, "Gd2WO6": 651333, 'MnO2': 19395, 'VO2': 504800} mpr = MPRester() for name, sid in compounds.iteritems(): if not names or name in names: sid = mpr.get_materials_id_from_task_id("mp-{}".format(sid)) s = mpr.get_structure_by_material_id(sid, final=False) snl = StructureNL(s, 'Anubhav Jain <*****@*****.**>') parameters = {'priority': 10} if name == 'Si' else None if params: parameters.update(params) sma.submit_snl(snl, '*****@*****.**', parameters=parameters)