def test_apply_transformation(self): enum_trans = EnumerateStructureTransformation(refine_structure=True) enum_trans2 = EnumerateStructureTransformation(refine_structure=True, sort_criteria="nsites") p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) struct = p.structure expected_ans = [1, 3, 1] for i, frac in enumerate([0.25, 0.5, 0.75]): trans = SubstitutionTransformation({'Fe': {'Fe': frac}}) s = trans.apply_transformation(struct) oxitrans = OxidationStateDecorationTransformation( {'Li': 1, 'Fe': 2, 'P': 5, 'O': -2}) s = oxitrans.apply_transformation(s) alls = enum_trans.apply_transformation(s, 100) self.assertEqual(len(alls), expected_ans[i]) self.assertIsInstance(trans.apply_transformation(s), Structure) for ss in alls: self.assertIn("energy", ss) alls = enum_trans2.apply_transformation(s, 100) self.assertEqual(len(alls), expected_ans[i]) self.assertIsInstance(trans.apply_transformation(s), Structure) for ss in alls: self.assertIn("num_sites", ss) # make sure it works for non-oxidation state decorated structure trans = SubstitutionTransformation({'Fe': {'Fe': 0.5}}) s = trans.apply_transformation(struct) alls = enum_trans.apply_transformation(s, 100) self.assertEqual(len(alls), 3) self.assertIsInstance(trans.apply_transformation(s), Structure) for s in alls: self.assertNotIn("energy", s)
def test_apply_transformation(self): enum_trans = EnumerateStructureTransformation(refine_structure=True) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) struct = p.structure expected_ans = [1, 3, 1] for i, frac in enumerate([0.25, 0.5, 0.75]): trans = SubstitutionTransformation({'Fe': {'Fe': frac}}) s = trans.apply_transformation(struct) oxitrans = OxidationStateDecorationTransformation({'Li': 1, 'Fe': 2, 'P': 5, 'O': -2}) s = oxitrans.apply_transformation(s) alls = enum_trans.apply_transformation(s, 100) self.assertEquals(len(alls), expected_ans[i]) self.assertIsInstance(trans.apply_transformation(s), Structure) for s in alls: self.assertIn("energy", s) #make sure it works for non-oxidation state decorated structure trans = SubstitutionTransformation({'Fe': {'Fe': 0.5}}) s = trans.apply_transformation(struct) alls = enum_trans.apply_transformation(s, 100) self.assertEquals(len(alls), 3) self.assertIsInstance(trans.apply_transformation(s), Structure) for s in alls: self.assertNotIn("energy", s)
def test_apply_transformations_best_first(self): p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR.LiFePO4"), check_for_POTCAR=False) t1 = OxidationStateDecorationTransformation({ "Li": 1, "Fe": 2, "P": 5, "O": -2 }) s = t1.apply_transformation(p.structure) t = PartialRemoveSpecieTransformation( "Li+", 0.5, PartialRemoveSpecieTransformation.ALGO_BEST_FIRST) self.assertEqual(len(t.apply_transformation(s)), 26)
def test_transmute(self): if QeTransmuterTest.qe is None: self.skipTest("No MongoDB present") crit = {} trans = [ SubstitutionTransformation({"Zn": "Mg"}), OxidationStateDecorationTransformation({ "B": 3, "O": -2, "Mg": 2, "Tb": 3 }), PartialRemoveSpecieTransformation( "Mg2+", 0.5, algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE) ] self.qep = QeTransmuter(QeTransmuterTest.qe, crit, trans, extend_collection=10) trans_structures = self.qep.transformed_structures self.assertEqual(len(trans_structures), 3) for s in trans_structures: self.assertEqual(s.final_structure.composition.reduced_formula, "Tb2Mg(BO2)10")
def test_apply_transformation(self): t = OxidationStateDecorationTransformation({"Li": 1, "O": -2}) 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", "O", "O"], coords) s = t.apply_transformation(struct) self.assertEqual(s[0].species_string, "Li+") self.assertEqual(s[2].species_string, "O2-") d = t.as_dict() self.assertEqual( type(OxidationStateDecorationTransformation.from_dict(d)), OxidationStateDecorationTransformation, )
def generate_ewald_orderings(path, choose_file, oxidation_states, num_structures): """ DESCRIPTION: Given a disordered CIF structure with at least one crystallographic site that is shared by more than one element, all permutations will have their electrostatic energy calculated via an Ewald summation, given that all ion charges are specified. Ordered CIF structures will be generated, postpended with a number that indicates the stability ranking of the structure. For example, if a CIF file called "Na2Mn2Fe(VO4)3.cif" is inputted with num_structures=3, then the function will generate 3 ordered output files, "Na2Mn2Fe(VO4)3-ewald-1", "Na2Mn2Fe(VO4)3-ewald-2", and "Na2Mn2Fe(VO4)3-ewald-3", with "Na2Mn2Fe(VO4)3-ewald-1" being the most stable and "Na2Mn2Fe(VO4)3-ewald-3" being the least stable. Note that this function does not take into account the symmetry of the crystal, and thus it may give several structures which are symmetrically identical under a space group. Use "find_unique_structures" to isolate unique orderings. PARAMETERS: path: string The file path to the CIF file. The CIF file must be a disordered structure, or else an error will occur. A disordered structure will have one site that is occupied by more than one element, with occupancies less than 1: i.e. Fe at 0,0,0.5 with an occupancy of 0.75, and Mn at the same site 0,0,0.5 with an occupancy of 0.25. This function cannot handle multivalent elements, for example it cannot handle a structure that has Mn in both the 2+ and 3+ redox state. choose_file: boolean Setting this parameter to True brings up the file explorer dialog for the user to manually select the CIF file oxidation_states: dictionary A dictionary that maps each element in the structure to a particular oxidation state. E.g. {"Fe": 3, "Mn": 2, "O": -2, "V": 5, "Na": 1, "Al":3} Make sure that all elements in the structure is assigned an oxidation state. It is ok to add more elements than needed. num_structures: int The number of strcutures to be outputted by the function. There can be hundreds or thousands of candidate structures, however in practice only the first few (or even only the first, most stable) structures are needed. RETURNS: None """ # open file dialog if file is to be manually chosen if choose_file: root = tk.Tk() root.withdraw() path = filedialog.askopenfilename() #Read cif file cryst = Structure.from_file(path) analyzer = SpacegroupAnalyzer(cryst) space_group = analyzer.get_space_group_symbol() print(space_group) symm_struct = analyzer.get_symmetrized_structure() cryst = TransformedStructure(symm_struct) #Create Oxidation State Transform oxidation_transform = OxidationStateDecorationTransformation( oxidation_states) #Create Ewald Ordering Transform object which will be passed into the transmuter ordering_transform = OrderDisorderedStructureTransformation( symmetrized_structures=True) # apply the order-disorder transform on the structure for any site that has fractional occupancies transmuter = StandardTransmuter([cryst], [oxidation_transform, ordering_transform], extend_collection=num_structures) print("Ewald optimization successful!") num_structures = len(transmuter.transformed_structures) for i in range(num_structures): newCryst = transmuter.transformed_structures[i].final_structure #Save to CIF structure_name = os.path.splitext(os.path.basename(path))[0] save_directory = structure_name if not os.path.isdir(save_directory): os.mkdir(save_directory) filename = structure_name + '/' + structure_name + '-ewald' + '-%i' % ( i + 1) w = CifWriter(newCryst) w.write_file(filename + '.cif') print("Cif file saved to {}.cif".format(filename)) #Save to POSCAR poscar = Poscar(newCryst) poscar.write_file(filename) print("POSCAR file saved to {}".format(filename))