def test_transmuter(self): tsc = PoscarTransmuter.from_filenames( [os.path.join(self.TEST_FILES_DIR, "POSCAR")]) tsc.append_transformation(RemoveSpeciesTransformation("O")) self.assertEqual(len(tsc[0].final_structure), 8) tsc.append_transformation( SubstitutionTransformation({ "Fe": { "Fe2+": 0.25, "Mn3+": 0.75 }, "P": "P5+" })) tsc.append_transformation(OrderDisorderedStructureTransformation(), extend_collection=50) self.assertEqual(len(tsc), 4) t = SuperTransformation([ SubstitutionTransformation({"Fe2+": "Mg2+"}), SubstitutionTransformation({"Fe2+": "Zn2+"}), SubstitutionTransformation({"Fe2+": "Be2+"}), ]) tsc.append_transformation(t, extend_collection=True) self.assertEqual(len(tsc), 12) for x in tsc: # should be 4 trans + starting structure self.assertEqual( len(x), 5, "something might be wrong with the number of transformations in the history", ) # test the filter tsc.apply_filter( ContainsSpecieFilter(["Zn2+", "Be2+", "Mn4+"], strict_compare=True, AND=False)) self.assertEqual(len(tsc), 8) self.assertEqual( tsc.transformed_structures[0].as_dict()["history"][-1]["@class"], "ContainsSpecieFilter", ) tsc.apply_filter(ContainsSpecieFilter(["Be2+"])) self.assertEqual(len(tsc), 4) # Test set_parameter and add_tag. tsc.set_parameter("para1", "hello") self.assertEqual( tsc.transformed_structures[0].as_dict()["other_parameters"] ["para1"], "hello", ) tsc.add_tags(["world", "universe"]) self.assertEqual( tsc.transformed_structures[0].as_dict()["other_parameters"] ["tags"], ["world", "universe"], )
def test_apply_transformation(self): t = RemoveSpeciesTransformation(["Li+"]) coords = [] coords.append([0, 0, 0]) coords.append([0.75, 0.75, 0.75]) coords.append([0.5, 0.5, 0.5]) coords.append([0.25, 0.25, 0.25]) lattice = Lattice([ [3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603], ]) struct = Structure(lattice, ["Li+", "Li+", "O2-", "O2-"], coords) s = t.apply_transformation(struct) self.assertEqual(s.composition.formula, "O2") d = t.as_dict() self.assertEqual(type(RemoveSpeciesTransformation.from_dict(d)), RemoveSpeciesTransformation)
def test_transmuter(self): tsc = PoscarTransmuter.from_filenames( [os.path.join(test_dir, "POSCAR")]) tsc.append_transformation(RemoveSpeciesTransformation('O')) self.assertEqual(len(tsc[0].final_structure), 8) tsc.append_transformation( SubstitutionTransformation({ "Fe": { "Fe2+": 0.25, "Mn3+": .75 }, "P": "P5+" })) tsc.append_transformation(OrderDisorderedStructureTransformation(), extend_collection=50) self.assertEqual(len(tsc), 4) t = SuperTransformation([ SubstitutionTransformation({"Fe2+": "Mg2+"}), SubstitutionTransformation({"Fe2+": "Zn2+"}), SubstitutionTransformation({"Fe2+": "Be2+"}) ]) tsc.append_transformation(t, extend_collection=True) self.assertEqual(len(tsc), 12) for x in tsc: self.assertEqual( len(x), 5, 'something might be wrong with the number of transformations in the history' ) #should be 4 trans + starting structure #test the filter tsc.apply_filter( ContainsSpecieFilter(['Zn2+', 'Be2+', 'Mn4+'], strict_compare=True, AND=False)) self.assertEqual(len(tsc), 8) self.assertEqual( tsc.get_transformed_structures()[0].as_dict()['history'][-1] ['@class'], 'ContainsSpecieFilter') tsc.apply_filter(ContainsSpecieFilter(['Be2+'])) self.assertEqual(len(tsc), 4) #Test set_parameter and add_tag. tsc.set_parameter("para1", "hello") self.assertEqual( tsc.transformed_structures[0].as_dict()['other_parameters'] ['para1'], 'hello') tsc.add_tags(["world", "universe"]) self.assertEqual( tsc.transformed_structures[0].as_dict()['other_parameters'] ['tags'], ["world", "universe"])
def metal_envt(struc): # Counts the Me environment in a structure # argument : pymatgen.core.structure # return : 2elt list of (7 element list) : the i th element is the # propotion of A with i A neighbors A = "Mg" B = "Mn" C = "Na" # neighborA = [0 for i in range(0, 7, 1)] # neighborB = [0 for i in range(0, 7, 1)] # A_indices = struc.indices_from_symbol(A) nbC = len(struc.indices_from_symbol(C)) neighborC = np.zeros(6) #print("{0} {1}".format(nbC,C)) sCopy = RemoveSpeciesTransformation(["O"]).apply_transformation(struc) # print(sCopy) mini = MinimumDistanceNN(tol=0.3) for i, site in enumerate(sCopy): # print(str(i)) # print(site) siteName = site.species_string if siteName == C: neigList = mini.get_nn_info(sCopy, i) #print("{0} closest neighboors \n".format(len(neigList))) coordA = 0 coordB = 0 for neighbor in neigList: # index = neighbor['site_index'] neighborName = neighbor['site'].species_string #print(" ( {0} at {1} ) ".format(neighborName,index)) if neighborName == A: coordA += 1 if neighborName == B: coordB += 1 neighborC[coordA] += 1 #print("neighborC list :" , neighborC) if nbC == 0: normalizedNeighborC = [0 for i in neighborC] else: normalizedNeighborC = [i / nbC for i in neighborC] print("coordination {0} : {1} \nnormalized by {2} : {3} \n".format( C, neighborC, nbC, normalizedNeighborC)) return normalizedNeighborC
from pymatgen.io.cif import CifParser from pymatgen.transformations.standard_transformations import RemoveSpeciesTransformation from pymatgen.transformations.standard_transformations import SubstitutionTransformation if __name__ == '__main__': # Read in a LiFePO4 structure from a cif. parser = CifParser('/Users/derek/Downloads/LiFePO4_mp-19017_computed.cif') struct = parser.get_structures()[0] t = RemoveSpeciesTransformation(["Li"]) modified_structure = t.apply_transformation(struct) t2 = SubstitutionTransformation({"Li", "Na"}) print(modified_structure)
def metalBondCount(struc): # Counts the metal metal bounds in a structure # argument pymatgen.core.structure # return [ normalizedBonds, normalizedAA, normalizedBB] # normalizedBond = 3 elt list =[ nb AA bonds, nb BB bonds, nb AB bonds ] / nb of metal # normalizedAA = 7 element list : the ith elt is the number of A with i A-neighbor # (ie if there are 1/4 of the Mg surrounded by 3 Mg, normalizedAA[3] = 0.25) # normalizedBB = idem for B # Logically, if nbA = nbB, normalizedAA = normalizedBB A = "Mg" B = "Mn" countAA = 0 countAB = 0 countBB = 0 neighborAA = [0 for i in range(7)] neighborBB = [0 for i in range(7)] A_indices = struc.indices_from_symbol(A) B_indices = struc.indices_from_symbol(B) nbA = len(A_indices) nbB = len(B_indices) nbTot = nbA + nbB print("{0} {1}, {2} {3} : {4}".format(nbA, A, nbB, B, nbTot)) sCopy = RemoveSpeciesTransformation(["Na", "O"]).apply_transformation(struc) # struc.remove_species('O') mini = MinimumDistanceNN(tol=0.3) for i, site in enumerate(sCopy): # print(str(i)) neigList = mini.get_nn_info(sCopy, i) #print("{0} closest neighboors \n".format(len(neigList))) coord = 0 for neighbor in neigList: index = neighbor['site_index'] name = neighbor['site'].species_string # print(" ( {0} at {1} ) ".format(name,index)) if site.species_string == A: if name == A: countAA += 1 coord += 1 if name == B: countAB += 1 if site.species_string == B: if name == A: countAB += 1 if name == B: countBB += 1 coord += 1 if site.species_string == A: neighborAA[coord] += 1 elif site.species_string == B: neighborBB[coord] += 1 bonds = [countAA // 2, countBB // 2, countAB // 2] normalizedBonds = [i / nbTot for i in bonds] print("[{3}{3}, {4}{4}, {3}{4}] = {0} normalized by {1} : {2} \n".format( bonds, nbTot, normalizedBonds, A, B)) if nbA > 0: normalizedAA = [i / nbA for i in neighborAA] print( "[Nombre de {0} avec [1-6] {0} autour] = {1} normalized by {2} : {3} \n" .format(A, neighborAA, nbA, normalizedAA)) else: normalizedAA = neighborAA if nbB > 0: normalizedBB = [i / nbB for i in neighborBB] print( "[Nombre de {0} avec [1-6] {0} autour] = {1} normalized by {2} : {3} \n" .format(B, neighborBB, nbB, normalizedBB)) else: normalizedBB = neighborBB return [normalizedBonds, normalizedAA, normalizedBB]