def test_apply_transformation(self): trans = MagOrderingTransformation({"Fe": 5}) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 3) f = SpacegroupAnalyzer(alls[0]["structure"], 0.1) self.assertEqual(f.get_space_group_number(), 31) model = IsingModel(5, 5) trans = MagOrderingTransformation({"Fe": 5}, energy_model=model) alls2 = trans.apply_transformation(s, 10) # Ising model with +J penalizes similar neighbor magmom. self.assertNotEqual(alls[0]["structure"], alls2[0]["structure"]) self.assertEqual(alls[0]["structure"], alls2[2]["structure"]) s = self.get_structure('Li2O') # Li2O doesn't have magnetism of course, but this is to test the # enumeration. trans = MagOrderingTransformation({"Li+": 1}, max_cell_size=3) alls = trans.apply_transformation(s, 100) # TODO: check this is correct, unclear what len(alls) should be self.assertEqual(len(alls), 12) trans = MagOrderingTransformation({"Ni": 5}) alls = trans.apply_transformation(self.NiO.get_primitive_structure(), return_ranked_list=10) self.assertEqual(self.NiO_AFM_111.lattice, alls[0]["structure"].lattice) self.assertEqual(self.NiO_AFM_001.lattice, alls[1]["structure"].lattice)
def test_apply_transformation(self): trans = MagOrderingTransformation({"Fe": 5}) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 3) f = SpacegroupAnalyzer(alls[0]["structure"], 0.1) self.assertEqual(f.get_space_group_number(), 31) model = IsingModel(5, 5) trans = MagOrderingTransformation({"Fe": 5}, energy_model=model) alls2 = trans.apply_transformation(s, 10) # Ising model with +J penalizes similar neighbor magmom. self.assertNotEqual(alls[0]["structure"], alls2[0]["structure"]) self.assertEqual(alls[0]["structure"], alls2[2]["structure"]) s = self.get_structure('Li2O') # Li2O doesn't have magnetism of course, but this is to test the # enumeration. trans = MagOrderingTransformation({"Li+": 1}, max_cell_size=3) alls = trans.apply_transformation(s, 100) # TODO: check this is correct, unclear what len(alls) should be self.assertEqual(len(alls), 12) trans = MagOrderingTransformation({"Ni": 5}) alls = trans.apply_transformation(self.NiO.get_primitive_structure(), return_ranked_list=10) self.assertArrayAlmostEqual(self.NiO_AFM_111.lattice.parameters, alls[0]["structure"].lattice.parameters) self.assertArrayAlmostEqual(self.NiO_AFM_001.lattice.parameters, alls[1]["structure"].lattice.parameters)
def test_apply_transformation(self): trans = MagOrderingTransformation({"Fe": 5}) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 3) f = SymmetryFinder(alls[0]["structure"], 0.1) self.assertEqual(f.get_spacegroup_number(), 31) model = IsingModel(5, 5) trans = MagOrderingTransformation({"Fe": 5}, energy_model=model) alls2 = trans.apply_transformation(s, 10) #Ising model with +J penalizes similar neighbor magmom. self.assertNotEqual(alls[0]["structure"], alls2[0]["structure"]) self.assertEqual(alls[0]["structure"], alls2[2]["structure"]) from pymatgen.io.smartio import read_structure s = read_structure(os.path.join(test_dir, 'Li2O.cif')) #Li2O doesn't have magnetism of course, but this is to test the # enumeration. trans = MagOrderingTransformation({"Li+": 1}, max_cell_size=3) alls = trans.apply_transformation(s, 100) self.assertEqual(len(alls), 10)
def test_ferrimagnetic(self): trans = MagOrderingTransformation({"Fe": 5}, 0.75, max_cell_size=1) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 2)
def test_ferrimagnetic(self): trans = MagOrderingTransformation({"Fe": 5}, 0.75, max_cell_size=1) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 2)
def test_ferrimagnetic(self): trans = MagOrderingTransformation({"Fe": 5}, order_parameter=0.75, max_cell_size=1) p = Poscar.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "POSCAR.LiFePO4"), check_for_POTCAR=False) s = p.structure a = SpacegroupAnalyzer(s, 0.1) s = a.get_refined_structure() alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 1)
def test_zero_spin_case(self): # ensure that zero spin case maintains sites and formula s = self.get_structure('Li2O') trans = MagOrderingTransformation({"Li+": 0.0}, 0.5) alls = trans.apply_transformation(s) # Ensure s does not have a spin property self.assertFalse('spin' in s.sites[1].specie._properties) # ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[1].specie._properties)
def test_zero_spin_case(self): #ensure that zero spin case maintains sites and formula s = self.get_structure('Li2O') trans = MagOrderingTransformation({"Li+": 0.0}, 0.5) alls = trans.apply_transformation(s) #Ensure s does not have a spin property self.assertFalse('spin' in s.sites[0].specie._properties) #ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[0].specie._properties)
def test_zero_spin_case(self): # ensure that zero spin case maintains sites and formula s = self.get_structure('Li2O') trans = MagOrderingTransformation({"Li+": 0.0}, order_parameter=0.5) alls = trans.apply_transformation(s) Li_site = alls.indices_from_symbol('Li')[0] # Ensure s does not have a spin property self.assertFalse('spin' in s.sites[Li_site].specie._properties) # ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[Li_site].specie._properties) self.assertEqual(alls.sites[Li_site].specie._properties['spin'], 0)
def test_zero_spin_case(self): # ensure that zero spin case maintains sites and formula s = self.get_structure('Li2O') trans = MagOrderingTransformation({"Li+": 0.0}, order_parameter=0.5) alls = trans.apply_transformation(s) Li_site = alls.indices_from_symbol('Li')[0] # Ensure s does not have a spin property self.assertFalse('spin' in s.sites[Li_site].specie._properties) # ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[Li_site].specie._properties) self.assertEqual(alls.sites[Li_site].specie._properties['spin'], 0)
def test_zero_spin_case(self): #ensure that zero spin case maintains sites and formula from pymatgen.io.smartio import read_structure s = read_structure(os.path.join(test_dir, 'Li2O.cif')) trans = MagOrderingTransformation({"Li+": 0.0}, 0.5) alls = trans.apply_transformation(s) #compositions will not be equal due to spin assignment #structure representations will be the same self.assertEqual(str(s), str(alls)) #Ensure s does not have a spin property self.assertFalse('spin' in s.sites[0].specie._properties) #ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[0].specie._properties)
def test_zero_spin_case(self): #ensure that zero spin case maintains sites and formula from pymatgen.io.smartio import read_structure s = read_structure(os.path.join(test_dir, 'Li2O.cif')) trans = MagOrderingTransformation({"Li+": 0.0}, 0.5) alls = trans.apply_transformation(s) #compositions will not be equal due to spin assignment #structure representations will be the same self.assertEqual(str(s), str(alls)) #Ensure s does not have a spin property self.assertFalse('spin' in s.sites[0].specie._properties) #ensure sites are assigned a spin property in alls self.assertTrue('spin' in alls.sites[0].specie._properties)
def test_apply_transformation(self): trans = MagOrderingTransformation({"Fe": 5}) p = Poscar.from_file(os.path.join(test_dir, 'POSCAR.LiFePO4'), check_for_POTCAR=False) s = p.structure alls = trans.apply_transformation(s, 10) self.assertEqual(len(alls), 3) f = SpacegroupAnalyzer(alls[0]["structure"], 0.1) self.assertEqual(f.get_spacegroup_number(), 31) model = IsingModel(5, 5) trans = MagOrderingTransformation({"Fe": 5}, energy_model=model) alls2 = trans.apply_transformation(s, 10) #Ising model with +J penalizes similar neighbor magmom. self.assertNotEqual(alls[0]["structure"], alls2[0]["structure"]) self.assertEqual(alls[0]["structure"], alls2[2]["structure"]) s = self.get_structure('Li2O') #Li2O doesn't have magnetism of course, but this is to test the # enumeration. trans = MagOrderingTransformation({"Li+": 1}, max_cell_size=3) alls = trans.apply_transformation(s, 100) self.assertEqual(len(alls), 10)
def test_advanced_usage(self): # test spin on just one oxidation state magtypes = {"Fe2+": 5} trans = MagOrderingTransformation(magtypes) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertIsInstance(alls, Structure) self.assertEqual(str(alls[0].specie), "Fe2+,spin=5") self.assertEqual(str(alls[2].specie), "Fe3+") # test multiple order parameters # this should only order on Fe3+ site, but assign spin to both magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(1, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) # using this 'sorted' syntax because exact order of sites in first # returned structure varies between machines: we just want to ensure # that the order parameter is accurate self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) self.assertEqual(str(alls[0].specie), "Fe2+,spin=5") # this should give same results as previously # but with opposite sign on Fe2+ site magtypes = {"Fe2+": -5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(1, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=-5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) # while this should order on both sites magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.25, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) # add coordination numbers to our test case # don't really care what these are for the test case cns = [6, 6, 6, 6, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0] self.Fe3O4.add_site_property('cn', cns) # this should give FM ordering on cn=4 sites, and AFM ordering on cn=6 sites magtypes = {"Fe": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe", site_constraint_name="cn", site_constraints=6), MagOrderParameterConstraint(1.0, species_constraints="Fe", site_constraint_name="cn", site_constraints=4) ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4) alls.sort(key=lambda x: x.properties['cn'], reverse=True) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0, 4)]), sorted(["Fe,spin=-5", "Fe,spin=-5", "Fe,spin=5", "Fe,spin=5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(4, 6)]), sorted(["Fe,spin=5", "Fe,spin=5"])) # now ordering on both sites, equivalent to order_parameter = 0.5 magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=10) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=5"])) self.assertEqual(len(alls), 4) # now mixed orderings where neither are equal or 1 magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.25, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=100) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) self.assertEqual(len(alls), 2) # now order on multiple species magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints=["Fe2+", "Fe3+"]), ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=10) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0, 2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=5"])) self.assertEqual(len(alls), 6)
def test_advanced_usage(self): # test spin on just one oxidation state magtypes = {"Fe2+": 5} trans = MagOrderingTransformation(magtypes) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertIsInstance(alls, Structure) self.assertEqual(str(alls[0].specie), "Fe2+,spin=5") self.assertEqual(str(alls[2].specie), "Fe3+") # test multiple order parameters # this should only order on Fe3+ site, but assign spin to both magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(1, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) # using this 'sorted' syntax because exact order of sites in first # returned structure varies between machines: we just want to ensure # that the order parameter is accurate self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) self.assertEqual(str(alls[0].specie), "Fe2+,spin=5") # this should give same results as previously # but with opposite sign on Fe2+ site magtypes = {"Fe2+": -5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(1, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=-5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) # while this should order on both sites magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.25, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) # add coordination numbers to our test case # don't really care what these are for the test case cns = [6, 6, 6, 6, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0] self.Fe3O4.add_site_property('cn', cns) # this should give FM ordering on cn=4 sites, and AFM ordering on cn=6 sites magtypes = {"Fe": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe", site_constraint_name="cn", site_constraints=6), MagOrderParameterConstraint(1.0, species_constraints="Fe", site_constraint_name="cn", site_constraints=4) ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4) alls.sort(key=lambda x: x.properties['cn'], reverse=True) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(0, 4)]), sorted(["Fe,spin=-5", "Fe,spin=-5", "Fe,spin=5", "Fe,spin=5"])) self.assertEqual(sorted([str(alls[idx].specie) for idx in range(4,6)]), sorted(["Fe,spin=5", "Fe,spin=5"])) # now ordering on both sites, equivalent to order_parameter = 0.5 magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.5, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=10) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=5"])) self.assertEqual(len(alls), 4) # now mixed orderings where neither are equal or 1 magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints="Fe2+"), MagOrderParameterConstraint(0.25, species_constraints="Fe3+") ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=100) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=-5"])) self.assertEqual(len(alls), 2) # now order on multiple species magtypes = {"Fe2+": 5, "Fe3+": 5} order_parameters = [ MagOrderParameterConstraint(0.5, species_constraints=["Fe2+", "Fe3+"]), ] trans = MagOrderingTransformation(magtypes, order_parameter=order_parameters) alls = trans.apply_transformation(self.Fe3O4_oxi, return_ranked_list=10) struct = alls[0]["structure"] self.assertEqual(sorted([str(struct[idx].specie) for idx in range(0,2)]), sorted(["Fe2+,spin=5", "Fe2+,spin=-5"])) self.assertEqual(sorted([str(struct[idx].specie) for idx in range(2, 6)]), sorted(["Fe3+,spin=5", "Fe3+,spin=-5", "Fe3+,spin=-5", "Fe3+,spin=5"])) self.assertEqual(len(alls), 6)