Ejemplo n.º 1
0
    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"],
        )
Ejemplo n.º 2
0
    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"])
Ejemplo n.º 3
0
 def test_undo_and_redo_last_change(self):
     trans = [
         SubstitutionTransformation({"Li": "Na"}),
         SubstitutionTransformation({"Fe": "Mn"})
     ]
     ts = TransformedStructure(self.structure, trans)
     self.assertEqual("NaMnPO4",
                      ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("NaFePO4",
                      ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("LiFePO4",
                      ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.undo_last_change)
     ts.redo_next_change()
     self.assertEqual("NaFePO4",
                      ts.final_structure.composition.reduced_formula)
     ts.redo_next_change()
     self.assertEqual("NaMnPO4",
                      ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.redo_next_change)
     #Make sure that this works with filters.
     f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
     ts.append_filter(f3)
     ts.undo_last_change()
     ts.redo_next_change()
Ejemplo n.º 4
0
 def test_to_from_dict(self):
     species1 = ["Si5+", "Mg2+"]
     f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
     d = f1.as_dict()
     self.assertIsInstance(ContainsSpecieFilter.from_dict(d),
                           ContainsSpecieFilter)
Ejemplo n.º 5
0
    def test_filtering(self):
        coords = [[0, 0, 0], [0.75, 0.75, 0.75], [0.5, 0.5, 0.5],
                  [0.25, 0.25, 0.25]]
        lattice = Lattice([[3.0, 0.0, 0.0], [1.0, 3.0, 0.00],
                           [0.00, -2.0, 3.0]])
        s = Structure(
            lattice,
            [
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
            ],
            coords,
        )

        species1 = [Species("Si", 5), Species("Mg", 2)]
        f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
        self.assertFalse(f1.test(s), "Incorrect filter")
        f2 = ContainsSpecieFilter(species1, strict_compare=False, AND=False)
        self.assertTrue(f2.test(s), "Incorrect filter")
        species2 = [Species("Si", 4), Species("Mg", 2)]
        f3 = ContainsSpecieFilter(species2, strict_compare=True, AND=False)
        self.assertTrue(f3.test(s), "Incorrect filter")
        f4 = ContainsSpecieFilter(species2, strict_compare=False, AND=False)
        self.assertTrue(f4.test(s), "Incorrect filter")

        species3 = [Species("Si", 5), Species("O", -2)]
        f5 = ContainsSpecieFilter(species3, strict_compare=True, AND=True)
        self.assertFalse(f5.test(s), "Incorrect filter")
        f6 = ContainsSpecieFilter(species3, strict_compare=False, AND=True)
        self.assertTrue(f6.test(s), "Incorrect filter")
        species4 = [Species("Si", 4), Species("Mg", 2)]
        f7 = ContainsSpecieFilter(species4, strict_compare=True, AND=True)
        self.assertFalse(f7.test(s), "Incorrect filter")
        f8 = ContainsSpecieFilter(species4, strict_compare=False, AND=True)
        self.assertFalse(f8.test(s), "Incorrect filter")
Ejemplo n.º 6
0
 def test_to_from_dict(self):
     species1 = ['Si5+', 'Mg2+']
     f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
     d = f1.as_dict()
     self.assertIsInstance(ContainsSpecieFilter.from_dict(d),
                           ContainsSpecieFilter)
Ejemplo n.º 7
0
    def test_filtering(self):
        coords = [[0, 0, 0], [0.75, 0.75, 0.75], [0.5, 0.5, 0.5],
                  [0.25, 0.25, 0.25]]
        lattice = Lattice([[3.0, 0.0, 0.0],
                           [1.0, 3.0, 0.00],
                           [0.00, -2.0, 3.0]])
        s = Structure(lattice,
                      [{"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                       {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                       {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                       {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25}], coords)

        species1 = [Specie('Si', 5), Specie('Mg', 2)]
        f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
        self.assertFalse(f1.test(s), 'Incorrect filter')
        f2 = ContainsSpecieFilter(species1, strict_compare=False, AND=False)
        self.assertTrue(f2.test(s), 'Incorrect filter')
        species2 = [Specie('Si', 4), Specie('Mg', 2)]
        f3 = ContainsSpecieFilter(species2, strict_compare=True, AND=False)
        self.assertTrue(f3.test(s), 'Incorrect filter')
        f4 = ContainsSpecieFilter(species2, strict_compare=False, AND=False)
        self.assertTrue(f4.test(s), 'Incorrect filter')

        species3 = [Specie('Si', 5), Specie('O', -2)]
        f5 = ContainsSpecieFilter(species3, strict_compare=True, AND=True)
        self.assertFalse(f5.test(s), 'Incorrect filter')
        f6 = ContainsSpecieFilter(species3, strict_compare=False, AND=True)
        self.assertTrue(f6.test(s), 'Incorrect filter')
        species4 = [Specie('Si', 4), Specie('Mg', 2)]
        f7 = ContainsSpecieFilter(species4, strict_compare=True, AND=True)
        self.assertFalse(f7.test(s), 'Incorrect filter')
        f8 = ContainsSpecieFilter(species4, strict_compare=False, AND=True)
        self.assertFalse(f8.test(s), 'Incorrect filter')
Ejemplo n.º 8
0
 def test_to_from_dict(self):
     species1 = ["Si5+", "Mg2+"]
     f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
     d = f1.to_dict
     self.assertIsInstance(ContainsSpecieFilter.from_dict(d), ContainsSpecieFilter)
Ejemplo n.º 9
0
    def test_filtering(self):
        coords = list()
        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.0, 0.0, 0.0], [1.0, 3.0, 0.00], [0.00, -2.0, 3.0]])
        s = Structure(
            lattice,
            [
                {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
                {"Si4+": 0.5, "O2-": 0.25, "P5+": 0.25},
            ],
            coords,
        )

        species1 = [Specie("Si", 5), Specie("Mg", 2)]
        f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
        self.assertFalse(f1.test(s), "Incorrect filter")
        f2 = ContainsSpecieFilter(species1, strict_compare=False, AND=False)
        self.assertTrue(f2.test(s), "Incorrect filter")
        species2 = [Specie("Si", 4), Specie("Mg", 2)]
        f3 = ContainsSpecieFilter(species2, strict_compare=True, AND=False)
        self.assertTrue(f3.test(s), "Incorrect filter")
        f4 = ContainsSpecieFilter(species2, strict_compare=False, AND=False)
        self.assertTrue(f4.test(s), "Incorrect filter")

        species3 = [Specie("Si", 5), Specie("O", -2)]
        f5 = ContainsSpecieFilter(species3, strict_compare=True, AND=True)
        self.assertFalse(f5.test(s), "Incorrect filter")
        f6 = ContainsSpecieFilter(species3, strict_compare=False, AND=True)
        self.assertTrue(f6.test(s), "Incorrect filter")
        species4 = [Specie("Si", 4), Specie("Mg", 2)]
        f7 = ContainsSpecieFilter(species4, strict_compare=True, AND=True)
        self.assertFalse(f7.test(s), "Incorrect filter")
        f8 = ContainsSpecieFilter(species4, strict_compare=False, AND=True)
        self.assertFalse(f8.test(s), "Incorrect filter")
Ejemplo n.º 10
0
 def test_append_filter(self):
     f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
     self.trans.append_filter(f3)
Ejemplo n.º 11
0
 def test_to_from_dict(self):
     species1 = ['Si5+', 'Mg2+']
     f1 = ContainsSpecieFilter(species1, strict_compare=True, AND=False)
     d = f1.to_dict
     self.assertIsInstance(ContainsSpecieFilter.from_dict(d),
                           ContainsSpecieFilter)