def test_specie_cifwriter(self): si4 = Specie("Si", 4) si3 = Specie("Si", 3) n = DummySpecie("X", -3) coords = list() coords.append(np.array([0.5, 0.5, 0.5])) coords.append(np.array([0.75, 0.5, 0.75])) coords.append(np.array([0, 0, 0])) lattice = Lattice( np.array([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]])) struct = Structure(lattice, [n, {si3: 0.5, n: 0.5}, si4], coords) writer = CifWriter(struct) ans = """# generated using pymatgen data_X1.5Si1.5 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 3.84019793 _cell_length_b 3.84019899 _cell_length_c 3.84019793 _cell_angle_alpha 119.99999086 _cell_angle_beta 90.00000000 _cell_angle_gamma 60.00000914 _symmetry_Int_Tables_number 1 _chemical_formula_structural X1.5Si1.5 _chemical_formula_sum 'X1.5 Si1.5' _cell_volume 40.04479464 _cell_formula_units_Z 1 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_type_symbol _atom_type_oxidation_number X3- -3.0 Si3+ 3.0 Si4+ 4.0 loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy X3- X1 1 0.500000 0.500000 0.500000 1 X3- X2 1 0.750000 0.500000 0.750000 0.5 Si3+ Si3 1 0.750000 0.500000 0.750000 0.5 Si4+ Si4 1 0.000000 0.000000 0.000000 1 """ for l1, l2 in zip(str(writer).split("\n"), ans.split("\n")): self.assertEqual(l1.strip(), l2.strip()) # test that mixed valence works properly s2 = Structure.from_str(ans, "cif") self.assertEqual(struct.composition, s2.composition)
def test_oxidation_states(self): ox = smact.oxidation_states.Oxidation_state_probability_finder() self.assertAlmostEqual( ox.compound_probability([Specie('Fe', +3), Specie('O', -2)]), 0.74280230326) self.assertAlmostEqual( ox.pair_probability(Species('Fe', +3), Species('O', -2)), 0.74280230326) self.assertEqual(len(ox.get_included_species()), 173)
def main(): for i in range(8): s: Structure = Structure.from_file('POSCAR') s.replace(i, species=Specie('Ca'), properties={'selective_dynamics': [True] * 3}) for j in range(7, i, -1): s.replace(j, species=Specie('Ca'), properties={'selective_dynamics': [True] * 3}) os.mkdir(f'{i}{j}') s.to('POSCAR', f'{i}{j}/POSCAR') s.copy() s.replace(j, species=Specie('Bi'), properties={'selective_dynamics': [True] * 3})
def structure(self): """ Transform from LammpsData file to a pymatgen structure object Return: A pymatgen structure object """ species_map = {} for i, sp in enumerate(self.masses['mass']): for el in Element: if abs(el.atomic_mass - sp) < 0.05: species_map[i + 1] = el if (len(species_map.keys()) != len(self.masses['mass'])): raise RuntimeError('Psudo atom exists in data file') xhi, yhi, zhi = self.box_bounds[0][1] - self.box_bounds[0][0], self.box_bounds[1][1] - self.box_bounds[1][0], \ self.box_bounds[2][1] - self.box_bounds[0][0] xy, xz, yz = self.box_tilt if self.box_tilt is not None else [0.0, 0.0, 0.0] lattice = Lattice([[xhi, 0, 0], [xy, yhi, 0], [xz, yz, zhi]]) species = [] coords = self.atoms[['x', 'y', 'z']].values for i in range(1, len(self.atoms['type']) + 1): if 'q' in self.atoms: if self.atoms['q'][i] != 0: species.append(Specie(species_map[self.atoms['type'][i]].symbol, self.atoms['q'][i])) else: species.append(species_map[self.atoms['type'][i]]) else: species.append(species_map[self.atoms['type'][i]]) return Structure(lattice, species, coords, coords_are_cartesian=True)
def ternary_smact_combos(position1, position2, position3, threshold=8): """ Combinatorially generate Pymatgen Species compositions using SMACT when up to three different lists are needed to draw species from (e.g. Ternary metal halides.) Args: position(n) (list of species): Species to be considered iteratively for each position. threshold (int): Max stoichiometry threshold. Returns: species_comps (list): Compositions as tuples of Pymatgen Species objects. """ initial_comps_list = [] for sp1, sp2, an in tqdm(itertools.product(position1, position2, position3)): e1, oxst1 = sp1.symbol, int(sp1.oxi_state) eneg1 = Element(e1).pauling_eneg e2, oxst2 = sp2.symbol, int(sp2.oxi_state) eneg2 = Element(e2).pauling_eneg e3, oxst3 = an.symbol, int(an.oxi_state) eneg3 = Element(e3).pauling_eneg symbols = [e1, e2, e3] ox_states = [oxst1, oxst2, oxst3] cn_e, cn_r = neutral_ratios(ox_states, threshold=threshold) if cn_e: enegs = [eneg1, eneg2, eneg3] eneg_ok = pauling_test(ox_states, enegs, symbols=symbols, repeat_cations=False) if eneg_ok: for ratio in cn_r: comp = (symbols, ox_states, list(ratio)) initial_comps_list.append(comp) print('Number of compositions before reduction: {}'.format( len(initial_comps_list))) # Create a list of pymatgen species for each comp print('Converting to Pymatgen Species...') species_comps = [] for i in tqdm(initial_comps_list): comp = {} for sym, ox, ratio in zip(i[0], i[1], i[2]): comp[Specie(sym, ox)] = ratio comp_list = [[key] * val for key, val in comp.items()] comp_list = [item for sublist in comp_list for item in sublist] species_comps.append(comp_list) # Sort and ditch duplicates print( 'Ditching duplicates (sorry to have got your hopes up with the big numbers)...' ) for i in species_comps: i.sort() i.sort(key=lambda x: x.oxi_state, reverse=True) species_comps = list(set([tuple(i) for i in species_comps])) print('Total number of new compounds unique compositions: {0}'.format( len(species_comps))) return species_comps
def get_unique_species(structures, ordering='ptable', reverse=False, cation_only=True, metal_only=True): """Given a set of pymatgen structures, in the form of dictionaries where the Structure is keyed as 'structure', returns a list of all the different Species present in that set. Args: structures (list): Dictionaries containing pymatgen Structures. ordering('string'): How to order the Species: ptable: order by periodic table position. Can be set to None. reverse (bool): Whether to reverse the ordering (descending order). cation_only (bool): Whether to only consider species in positive oxidation states. metal_only (bool): Whether to only consider metal elements. Returns: species_list (list): Unique species that are exhibited in the structures. """ # Initially comb through the structures for all unique species species_list = [] for i in structures: for sp in i['structure'].composition: species_list.append((sp)) species_list = list(set(species_list)) ordered_el = ordered_elements(1, 103) # Turn into tuples for easy sorting species_list = [(i.symbol, i.oxi_state) for i in species_list] if ordering == 'ptable': species_list.sort(key=lambda x: (ordered_el.index(x[0]), x[1]), reverse=reverse) print("Species ordered by periodic table position.") else: print('Did not reorder the list of species...') # Turn back into Species objects species_list = [Specie(i[0], i[1]) for i in species_list] if metal_only: print('Metals only: ON') species_list = [i for i in species_list if (i.symbol in metals)] if cation_only: print('Cations only: ON') species_list = [i for i in species_list if (i.oxi_state > 0)] print("First species: {0} last species: {1}".format( species_list[0], species_list[-1])) return species_list
def get_items(self): """ Gets all structure predictions ready to run Returns: Generator of request and relevant structure templates to run structure prediction tasks """ self.logger.info("Structure Prediction Builder started") requests = self.requests.query(criteria={"state": "READY"}) for request in requests: elements = request["elements"] oxi_states = request["element_oxidation_states"] threshold = request["threshold"] max_num_subs = request["max_num_subs"] original_species = [ Specie(e, o) for e, o in zip(elements, map(int, oxi_states)) ] request["original_species"] = original_species all_chemsys_to_consider = list( self.find_all_chemsys(original_species, threshold, max_num_subs)) self.logger.info( f"Considering the following chemical systems: {all_chemsys_to_consider}" ) templates = [ struct for struct in self.structure_templates.query( {"chemsys": { "$in": all_chemsys_to_consider }}) ] self.logger.info( f"Acquired {len(templates)} structure templates for {original_species}" ) yield {"request": request, "templates": templates}
def get_I_mads(struc): # Add oxidation states try: struc = bva.get_oxi_state_decorated_structure(struc) #print (struc) except: struc.add_oxidation_state_by_guess() # Check if a specific species is present if (Specie('I',-1) in struc.species): ews = EwaldSummation(struc) print (ews) I_indices = [n for n,site in enumerate(struc) if site.specie.symbol == 'I'] I_mads = np.array([ews.get_site_energy(n) for n in I_indices]) print (I_indices) print (I_mads) return I_mads return ews.total_energy print (ews.total_energy)
def sort_species(species_list, ordering): """Given a list of pymatgen Species, will order them according to a given rule and return the ordered list. ordering is a string that can take values: 'ptable': order by periodic table position. Args: species_list (list): Pymatgen species objects """ ordered_el = ordered_elements(1, 103) # Turn into tuples for easy sorting species_list = [(i.symbol, i.oxi_state) for i in species_list] if ordering == 'ptable': species_list.sort(key=lambda x: (ordered_el.index(x[0]), x[1])) print("Species ordered by periodic table position.") else: print('Did not reorder the list of species...') # Turn back into Species objects species_list = [Specie(i[0], i[1]) for i in species_list] print("First species: {0} last species: {1}".format( species_list[0], species_list[-1])) return species_list
Thus always use the conventional unit cell. """ # Calculation the Lattice Constant at 0 Kelvin import os import subprocess from pymatgen import Structure, Lattice, Specie from pymatgen.symmetry.analyzer import SpacegroupAnalyzer from pmg_lammps import RelaxSet, LammpsRun, LammpsData, LammpsPotentials supercell = (5, 5, 5) a = 4.1990858 # From evaluation of potential lattice = Lattice.from_parameters(a, a, a, 90, 90, 90) mg = Specie('Mg', 1.4) o = Specie('O', -1.4) atoms = [mg, o] sites = [[0, 0, 0], [0.5, 0.5, 0.5]] structure = Structure.from_spacegroup(225, lattice, atoms, sites) spga = SpacegroupAnalyzer(structure) structure = spga.get_primitive_standard_structure() print(structure) directory = 'runs/lattice' lammps_potentials = LammpsPotentials( pair={ (mg, mg): '1309362.2766468062 0.104 0.0', (mg, o): '9892.357 0.20199 0.0', (o, o): '2145.7345 0.3 30.2222'
def test_get_incar(self): incar = self.paramset.get_incar(self.struct) self.assertEqual(incar['LDAUU'], [5.3, 0, 0]) self.assertAlmostEqual(incar['EDIFF'], 0.0012) incar = self.mitparamset.get_incar(self.struct) self.assertEqual(incar['LDAUU'], [4.0, 0, 0]) self.assertAlmostEqual(incar['EDIFF'], 0.0012) incar_gga = self.mitggaparam.get_incar(self.struct) self.assertNotIn("LDAU", incar_gga) incar_static = self.mpstaticparamset.get_incar(self.struct) self.assertEqual(incar_static["NSW"], 0) incar_nscfl = self.mpnscfparamsetl.get_incar(self.struct) self.assertEqual(incar_nscfl["NBANDS"], 60) incar_nscfu = self.mpnscfparamsetu.get_incar(self.struct) self.assertEqual(incar_nscfu["ISYM"], 0) incar_hse = self.mphseparamset.get_incar(self.struct) self.assertEqual(incar_hse['LHFCALC'], True) self.assertEqual(incar_hse['HFSCREEN'], 0.2) incar_hse_bsl = self.mpbshseparamsetl.get_incar(self.struct) self.assertEqual(incar_hse_bsl['LHFCALC'], True) self.assertEqual(incar_hse_bsl['HFSCREEN'], 0.2) self.assertEqual(incar_hse_bsl['NSW'], 0) incar_hse_bsu = self.mpbshseparamsetu.get_incar(self.struct) self.assertEqual(incar_hse_bsu['LHFCALC'], True) self.assertEqual(incar_hse_bsu['HFSCREEN'], 0.2) self.assertEqual(incar_hse_bsu['NSW'], 0) incar_diel = self.mpdielparamset.get_incar(self.struct) self.assertEqual(incar_diel['IBRION'], 8) self.assertEqual(incar_diel['LEPSILON'], True) si = 14 coords = list() coords.append(np.array([0, 0, 0])) coords.append(np.array([0.75, 0.5, 0.75])) #Silicon structure for testing. latt = Lattice( np.array([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]])) struct = Structure(latt, [si, si], coords) incar = self.paramset.get_incar(struct) self.assertNotIn("LDAU", incar) incar = self.mithseparamset.get_incar(self.struct) self.assertTrue(incar['LHFCALC']) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) struct = Structure(lattice, ["Fe", "Mn"], coords) incar = self.paramset.get_incar(struct) self.assertNotIn('LDAU', incar) #check fluorides struct = Structure(lattice, ["Fe", "F"], coords) incar = self.paramset.get_incar(struct) self.assertEqual(incar['LDAUU'], [5.3, 0]) self.assertEqual(incar['MAGMOM'], [5, 0.6]) struct = Structure(lattice, ["Fe", "F"], coords) incar = self.mitparamset.get_incar(struct) self.assertEqual(incar['LDAUU'], [4.0, 0]) #Make sure this works with species. struct = Structure(lattice, ["Fe2+", "O2-"], coords) incar = self.paramset.get_incar(struct) self.assertEqual(incar['LDAUU'], [5.3, 0]) struct = Structure(lattice, ["Fe", "Mn"], coords, site_properties={'magmom': (5.2, -4.5)}) incar = self.paramset.get_incar(struct) self.assertEqual(incar['MAGMOM'], [-4.5, 5.2]) incar = self.mpstaticparamset.get_incar(struct) self.assertEqual(incar['MAGMOM'], [-4.5, 5.2]) incar = self.mitparamset_unsorted.get_incar(struct) self.assertEqual(incar['MAGMOM'], [5.2, -4.5]) struct = Structure(lattice, [Specie("Fe", 2, {'spin': 4.1}), "Mn"], coords) incar = self.paramset.get_incar(struct) self.assertEqual(incar['MAGMOM'], [5, 4.1]) incar = self.mpnscfparamsetl.get_incar(struct) self.assertEqual(incar.get('MAGMOM', None), None) struct = Structure(lattice, ["Mn3+", "Mn4+"], coords) incar = self.mitparamset.get_incar(struct) self.assertEqual(incar['MAGMOM'], [4, 3]) incar = self.mpnscfparamsetu.get_incar(struct) self.assertEqual(incar.get('MAGMOM', None), None) self.assertEqual( self.userparamset.get_incar(struct)['MAGMOM'], [100, 0.6]) #sulfide vs sulfate test coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) coords.append([0.25, 0.5, 0]) struct = Structure(lattice, ["Fe", "Fe", "S"], coords) incar = self.mitparamset.get_incar(struct) self.assertEqual(incar['LDAUU'], [1.9, 0]) #Make sure Matproject sulfides are ok. self.assertNotIn('LDAUU', self.paramset.get_incar(struct)) self.assertNotIn('LDAUU', self.mpstaticparamset.get_incar(struct)) struct = Structure(lattice, ["Fe", "S", "O"], coords) incar = self.mitparamset.get_incar(struct) self.assertEqual(incar['LDAUU'], [4.0, 0, 0]) #Make sure Matproject sulfates are ok. self.assertEqual(self.paramset.get_incar(struct)['LDAUU'], [5.3, 0, 0]) self.assertEqual( self.mpnscfparamsetl.get_incar(struct)['LDAUU'], [5.3, 0, 0]) self.assertEqual( self.userparamset.get_incar(struct)['MAGMOM'], [10, -5, 0.6])
from pymatgen import MPRester, Structure, Specie 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') s = mpr.get_structures('ZrO2') ortho: Structure = mpr.get_structure_by_material_id('mp-685097') tetra: Structure = mpr.get_structure_by_material_id('mp-1018721') for j, i in enumerate(mono.sites): if str(i.specie) == 'Hf': print(i.specie) mono.replace(j, species=Specie('Zr')) break for j, i in enumerate(mono.sites): if str(i.specie) == 'Hf': mono.replace(j, species=Specie('Zr')) break for j, i in enumerate(tetra.sites): if str(i.specie) == 'Hf': tetra.replace(j, species=Specie('Zr')) break for j, i in enumerate(ortho.sites): if str(i.specie) == 'Hf': ortho.replace(j, species=Specie('Zr')) break for j, i in enumerate(ortho.sites): if str(i.specie) == 'Hf': ortho.replace(j, species=Specie('Zr')) break
def test_get_incar(self): incar = self.mpset.incar self.assertEqual(incar['LDAUU'], [5.3, 0, 0]) self.assertAlmostEqual(incar['EDIFF'], 0.0012) incar = self.mitset.incar self.assertEqual(incar['LDAUU'], [4.0, 0, 0]) self.assertAlmostEqual(incar['EDIFF'], 0.0012) si = 14 coords = list() coords.append(np.array([0, 0, 0])) coords.append(np.array([0.75, 0.5, 0.75])) #Silicon structure for testing. latt = Lattice( np.array([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]])) struct = Structure(latt, [si, si], coords) incar = MPRelaxSet(struct).incar self.assertNotIn("LDAU", incar) coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) lattice = Lattice([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]) struct = Structure(lattice, ["Fe", "Mn"], coords) incar = MPRelaxSet(struct).incar self.assertNotIn('LDAU', incar) #check fluorides struct = Structure(lattice, ["Fe", "F"], coords) incar = MPRelaxSet(struct).incar self.assertEqual(incar['LDAUU'], [5.3, 0]) self.assertEqual(incar['MAGMOM'], [5, 0.6]) struct = Structure(lattice, ["Fe", "F"], coords) incar = MITRelaxSet(struct).incar self.assertEqual(incar['LDAUU'], [4.0, 0]) #Make sure this works with species. struct = Structure(lattice, ["Fe2+", "O2-"], coords) incar = MPRelaxSet(struct).incar self.assertEqual(incar['LDAUU'], [5.3, 0]) struct = Structure(lattice, ["Fe", "Mn"], coords, site_properties={'magmom': (5.2, -4.5)}) incar = MPRelaxSet(struct).incar self.assertEqual(incar['MAGMOM'], [-4.5, 5.2]) incar = MITRelaxSet(struct, sort_structure=False).incar self.assertEqual(incar['MAGMOM'], [5.2, -4.5]) struct = Structure(lattice, [Specie("Fe", 2, {'spin': 4.1}), "Mn"], coords) incar = MPRelaxSet(struct).incar self.assertEqual(incar['MAGMOM'], [5, 4.1]) struct = Structure(lattice, ["Mn3+", "Mn4+"], coords) incar = MITRelaxSet(struct).incar self.assertEqual(incar['MAGMOM'], [4, 3]) userset = MPRelaxSet( struct, user_incar_settings={'MAGMOM': { "Fe": 10, "S": -5, "Mn3+": 100 }}) self.assertEqual(userset.incar['MAGMOM'], [100, 0.6]) #sulfide vs sulfate test coords = list() coords.append([0, 0, 0]) coords.append([0.75, 0.5, 0.75]) coords.append([0.25, 0.5, 0]) struct = Structure(lattice, ["Fe", "Fe", "S"], coords) incar = MITRelaxSet(struct).incar self.assertEqual(incar['LDAUU'], [1.9, 0]) #Make sure Matproject sulfides are ok. self.assertNotIn('LDAUU', MPRelaxSet(struct).incar) struct = Structure(lattice, ["Fe", "S", "O"], coords) incar = MITRelaxSet(struct).incar self.assertEqual(incar['LDAUU'], [4.0, 0, 0]) #Make sure Matproject sulfates are ok. self.assertEqual(MPRelaxSet(struct).incar['LDAUU'], [5.3, 0, 0])
def test_specie_cifwriter(self): si4 = Specie("Si", 4) si3 = Specie("Si", 3) n = Specie("N", -3) coords = list() coords.append(np.array([0, 0, 0])) coords.append(np.array([0.75, 0.5, 0.75])) coords.append(np.array([0.5, 0.5, 0.5])) lattice = Lattice( np.array([[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]])) struct = Structure(lattice, [si4, {si3: 0.5, n: 0.5}, n], coords) writer = CifWriter(struct) ans = """#\#CIF1.1 ########################################################################## # Crystallographic Information Format file # Produced by PyCifRW module # # This is a CIF file. CIF has been adopted by the International # Union of Crystallography as the standard for data archiving and # transmission. # # For information on this file format, follow the CIF links at # http://www.iucr.org ########################################################################## data_Si1.5N1.5 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 3.8401979337 _cell_length_b 3.84019899434 _cell_length_c 3.84019793372 _cell_angle_alpha 119.999990864 _cell_angle_beta 90.0 _cell_angle_gamma 60.0000091373 _chemical_name_systematic 'Generated by pymatgen' _symmetry_Int_Tables_number 1 _chemical_formula_structural Si1.5N1.5 _chemical_formula_sum 'Si1.5 N1.5' _cell_volume 40.0447946443 _cell_formula_units_Z 0 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_type_symbol _atom_type_oxidation_number Si4+ 4.0 N3- -3.0 Si3+ 3.0 loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_attached_hydrogens _atom_site_B_iso_or_equiv _atom_site_occupancy Si4+ Si1 1 0.000000 0.000000 0.000000 0 . 1 N3- N2 1 0.750000 0.500000 0.750000 0 . 0.5 Si3+ Si3 1 0.750000 0.500000 0.750000 0 . 0.5 N3- N4 1 0.500000 0.500000 0.500000 0 . 1 """ for l1, l2 in zip(str(writer).split("\n"), ans.split("\n")): self.assertEqual(l1.strip(), l2.strip())
def test_descriptor_ionic_radii_from_composition(self): cscl = Composition({Specie("Cs", 1): 1, Specie("Cl", -1): 1}) ionic_radii = get_pymatgen_descriptor(cscl, "ionic_radii") self.assertEqual(ionic_radii, [1.81, 1.67]) ionic_radii_2 = get_pymatgen_descriptor("Cs+1Cl-1", "ionic_radii") self.assertEqual(ionic_radii, ionic_radii_2)
def structure_from_string(data): """ Parses a rndstr.in or lat.in file into pymatgen's Structure format. :param data: contents of a rndstr.in or lat.in file :return: Structure object """ data = data.splitlines() data = [x.split() for x in data if x] # remove empty lines # following specification/terminology given in manual if len(data[0]) == 6: # lattice parameters a, b, c, alpha, beta, gamma = map(float, data[0]) coord_system = Lattice.from_parameters(a, b, c, alpha, beta, gamma).matrix lattice_vecs = np.array([ [data[1][0], data[1][1], data[1][2]], [data[2][0], data[2][1], data[2][2]], [data[3][0], data[3][1], data[3][2]] ], dtype=float) first_species_line = 4 else: coord_system = np.array([ [data[0][0], data[0][1], data[0][2]], [data[1][0], data[1][1], data[1][2]], [data[2][0], data[2][1], data[2][2]] ], dtype=float) lattice_vecs = np.array([ [data[3][0], data[3][1], data[3][2]], [data[4][0], data[4][1], data[4][2]], [data[5][0], data[5][1], data[5][2]] ], dtype=float) first_species_line = 6 scaled_matrix = np.matmul(coord_system, lattice_vecs) lattice = Lattice(scaled_matrix) all_coords = [] all_species = [] for l in data[first_species_line:]: all_coords.append(np.array([l[0], l[1], l[2]], dtype=float)) species_strs = "".join(l[3:]) # join multiple strings back together species_strs = species_strs.replace(" ", "") # trim any white space species_strs = species_strs.split(",") # comma-delimited species = {} for species_str in species_strs: species_str = species_str.split('=') if len(species_str) == 1: # assume occupancy is 1.0 species_str = [species_str[0], 1.0] try: species[Specie(species_str[0])] = float(species_str[1]) except Exception: species[DummySpecie(species_str[0])] = float(species_str[1]) all_species.append(species) return Structure(lattice, all_species, all_coords)
def test_find_codopant(self): self.assertEqual(_find_codopant(Specie("Fe", 2), 1), Specie("Cu", 1)) self.assertEqual(_find_codopant(Specie("Fe", 2), 3), Specie("In", 3))
#!/usr/bin/env python import json import os from pymatgen import Specie, PMGJSONEncoder from pymatgen.analysis.bond_valence import BondValenceParam param = [] species_set = [] with open('bvparm2011', 'r') as f: for l in f: toks = l.split() sp1 = Specie(toks[0], int(toks[1])) sp2 = Specie(toks[2], int(toks[3])) species = frozenset([sp1, sp2]) if species not in species_set and toks[7] != "unchecked": param.append( BondValenceParam([sp1, sp2], float(toks[4]), float(toks[5]))) species_set.append(species) with open(os.path.join("..", "pymatgen", "analysis", "bvparm2011.json"), "w") as f: json.dump(param, f, cls=PMGJSONEncoder)