예제 #1
0
    def test_init(self):
        filepath = os.path.join(test_dir, 'POSCAR')
        p = Poscar.from_file(filepath)
        original_s = p.structure

        modifier = StructureEditor(original_s)
        modifier.add_oxidation_state_by_element({"Li": 1, "Fe": 2,
                                                 "P": 5, "O":-2})
        s = modifier.modified_structure
        ham = EwaldSummation(s)
        self.assertAlmostEqual(ham.real_space_energy, -354.91294268, 4,
                               "Real space energy incorrect!")
        self.assertAlmostEqual(ham.reciprocal_space_energy, 25.475754801, 4)
        self.assertAlmostEqual(ham.point_energy, -790.463835033, 4,
                               "Point space energy incorrect!")
        self.assertAlmostEqual(ham.total_energy, -1119.90102291, 2,
                               "Total space energy incorrect!")
        self.assertAlmostEqual(ham.forces[0,0], -1.98818620e-01, 4,
                               "Forces incorrect")
        self.assertAlmostEqual(sum(sum(abs(ham.forces))), 915.925354346, 4,
                               "Forces incorrect")
        self.assertAlmostEqual(sum(sum(ham.real_space_energy_matrix)),
                               - 354.91294268, 4,
                               "Real space energy matrix incorrect!")
        self.assertAlmostEqual(sum(sum(ham.reciprocal_space_energy_matrix)),
                               25.475754801, 4,
                               "Reciprocal space energy matrix incorrect!")
        self.assertAlmostEqual(sum(ham.point_energy_matrix), -790.463835033,
                               4, "Point space energy matrix incorrect!")
        self.assertAlmostEqual(sum(sum(ham.total_energy_matrix)),
                               - 1119.90102291, 2,
                               "Total space energy matrix incorrect!")
        #note that forces are not individually tested, but should work fine.

        self.assertRaises(ValueError, EwaldSummation, original_s)
        #try sites with charge.
        charges = []
        for site in original_s:
            if site.specie.symbol == "Li":
                charges.append(1)
            elif site.specie.symbol == "Fe":
                charges.append(2)
            elif site.specie.symbol == "P":
                charges.append(5)
            else:
                charges.append(-2)

        editor = StructureEditor(original_s)
        editor.add_site_property('charge', charges)
        ham2 = EwaldSummation(editor.modified_structure)
        self.assertAlmostEqual(ham2.real_space_energy, -354.91294268, 4,
                               "Real space energy incorrect!")
 def test_add_oxidation_states(self):
     si = Element("Si")
     fe = Element("Fe")
     coords = list()
     coords.append([0, 0, 0])
     coords.append([0.75, 0.5, 0.75])
     lattice = Lattice.cubic(10)
     s = Structure(lattice, [si, fe], coords)
     oxidation_states = {"Fe": 2, "Si": -4}
     mod = StructureEditor(s)
     mod.add_oxidation_state_by_element(oxidation_states)
     mod_s = mod.modified_structure
     for site in mod_s:
         for k in site.species_and_occu.keys():
             self.assertEqual(k.oxi_state, oxidation_states[k.symbol],
                              "Wrong oxidation state assigned!")
     oxidation_states = {"Fe": 2}
     self.assertRaises(ValueError, mod.add_oxidation_state_by_element,
                       oxidation_states)
     mod.add_oxidation_state_by_site([2, -4])
     mod_s = mod.modified_structure
     self.assertEqual(mod_s[0].specie.oxi_state, 2)
     self.assertRaises(ValueError, mod.add_oxidation_state_by_site,
                       [1])
 def apply_transformation(self, structure):
     editor = StructureEditor(structure)
     editor.add_oxidation_state_by_element(self.oxi_states)
     return editor.modified_structure