예제 #1
0
    def test_has_species(self):
        """Test determining whether a species is in a `SmactStructure`."""
        s1 = SmactStructure(*self._gen_empty_structure([('Ba', 2, 2), ('O', -2, 1), ('F', -1, 2)]))

        self.assertTrue(s1.has_species(('Ba', 2)))
        self.assertFalse(s1.has_species(('Ba', 3)))
        self.assertFalse(s1.has_species(('Ca', 2)))
예제 #2
0
    def test_ele_stoics(self):
        """Test acquiring element stoichiometries."""
        s1 = SmactStructure(*self._gen_empty_structure([('Fe', 2, 1), ('Fe', 3, 2), ('O', -2, 4)]))
        s1_stoics = {'Fe': 3, 'O': 4}
        s2 = SmactStructure(*self._gen_empty_structure([('Ba', 2, 2), ('O', -2, 1), ('F', -1, 2)]))
        s2_stoics = {'Ba': 2, 'O': 1, 'F': 2}

        for test, expected in [(s1, s1_stoics), (s2, s2_stoics)]:
            with self.subTest(species=test.species):
                self.assertEqual(SmactStructure._get_ele_stoics(test.species), expected)
예제 #3
0
    def test_from_mp(self):
        """Test downloading structures from materialsproject.org."""
        # TODO Needs ensuring that the structure query gets the same
        # structure as we have downloaded.
        api_key = os.environ.get("MPI_KEY")

        for comp, species in self.TEST_SPECIES.items():
            with self.subTest(comp=comp):
                comp_file = os.path.join(files_dir, f"{comp}.txt")
                local_struct = SmactStructure.from_file(comp_file)
                mp_struct = SmactStructure.from_mp(species, api_key)
                self.assertEqual(local_struct, mp_struct)
예제 #4
0
    def test_from_py_struct(self):
        """Test generation of SmactStructure from a pymatgen Structure."""
        with open(TEST_PY_STRUCT, 'r') as f:
            d = json.load(f)
            py_structure = pymatgen.Structure.from_dict(d)

        with ignore_warnings(smact.structure_prediction.logger):
            s1 = SmactStructure.from_py_struct(py_structure)

        s2 = SmactStructure.from_file(os.path.join(files_dir, "CaTiO3.txt"))

        self.assertStructAlmostEqual(s1, s2)
예제 #5
0
    def test_equality(self):
        """Test equality determination of `SmactStructure`."""
        struct_files = [os.path.join(files_dir, f"{x}.txt") for x in ["CaTiO3", "NaCl"]]
        CaTiO3 = SmactStructure.from_file(struct_files[0])
        NaCl = SmactStructure.from_file(struct_files[1])

        with self.subTest(msg="Testing equality of same object."):
            self.assertEqual(CaTiO3, CaTiO3)

        with self.subTest(msg="Testing inequality of different types."):
            self.assertNotEqual(CaTiO3, "CaTiO3")

        with self.subTest(msg="Testing inequality of different objects."):
            self.assertNotEqual(CaTiO3, NaCl)
예제 #6
0
 def test_as_poscar(self):
     """Test POSCAR generation."""
     for comp in self.TEST_SPECIES.keys():
         with self.subTest(comp=comp):
             comp_file = os.path.join(files_dir, f"{comp}.txt")
             with open(comp_file, "r") as f:
                 struct = SmactStructure.from_file(comp_file)
                 self.assertEqual(struct.as_poscar(), f.read())
예제 #7
0
    def test_smactStruc_from_file(self):
        """Test the `from_file` method of `SmactStructure`."""
        with open(TEST_STRUCT, 'rb') as f:
            s1 = pickle.load(f)

        s2 = SmactStructure.from_file(TEST_POSCAR)

        self.assertEqual(s1, s2)
예제 #8
0
    def test_ion_mutation(self):
        """Test mutating an ion of a SmactStructure."""
        ca_file = os.path.join(files_dir, "CaTiO3.txt")
        ba_file = os.path.join(files_dir, "BaTiO3.txt")

        CaTiO3 = SmactStructure.from_file(ca_file)
        BaTiO3 = SmactStructure.from_file(ba_file)

        with self.subTest(s1="CaTiO3", s2="BaTiO3"):
            mutation = self.test_mutator._mutate_structure(CaTiO3, "Ca2+", "Ba2+")
            self.assertEqual(mutation, BaTiO3)

        na_file = os.path.join(files_dir, "NaCl.txt")
        NaCl = SmactStructure.from_file(na_file)

        with self.subTest(s1="Na1+Cl1-", s2="Na2+Cl1-"):
            with self.assertRaises(ValueError):
                self.test_mutator._mutate_structure(NaCl, "Na1+", "Na2+")
예제 #9
0
def generate_test_structure(comp: str) -> bool:
    """Generate a pickled test structure for comparison."""
    poscar_file = os.path.join(files_dir, f"{comp}.txt")
    s = SmactStructure.from_file(poscar_file)

    with open(TEST_STRUCT, 'wb') as f:
        pickle.dump(s, f)

    with open(TEST_POSCAR, 'w') as f:
        f.write(s.as_poscar())

    return True
예제 #10
0
    def setUpClass(cls):
        """Set up the test initial structure and mutator."""
        cls.test_struct = SmactStructure.from_file(TEST_POSCAR)

        cls.test_mutator = CationMutator.from_json(lambda_json=TEST_LAMBDA_JSON)
        cls.test_pymatgen_mutator = CationMutator.from_json(
          lambda_json=None, alpha=lambda x, y: -5
        )

        # 5 random test species -> 5! test pairs
        cls.test_species = sample(cls.test_pymatgen_mutator.specs, 5)
        cls.test_pairs = list(itertools.combinations_with_replacement(cls.test_species, 2))

        cls.pymatgen_sp = SubstitutionProbability(lambda_table=None, alpha=-5)
예제 #11
0
    def test_db_interface(self):
        """Test interfacing with database."""
        with self.subTest(msg="Instantiating database."):
            self.db = StructureDB(self.TEST_DB)

        with self.subTest(msg="Adding table."):
            try:
                self.db.add_table(self.TEST_TABLE)
            except Exception as e:
                self.fail(e)

        struct_file = os.path.join(files_dir, "CaTiO3.txt")
        struct = SmactStructure.from_file(struct_file)

        with self.subTest(msg="Adding structure to table."):
            try:
                self.db.add_struct(struct, self.TEST_TABLE)
            except Exception as e:
                self.fail(e)

        with self.subTest(msg="Getting structure from table."):
            struct_list = self.db.get_structs(struct.composition(), self.TEST_TABLE)
            self.assertEqual(len(struct_list), 1)
            self.assertEqual(struct_list[0], struct)

        struct_files = [os.path.join(files_dir, f"{x}.txt") for x in ["NaCl", "Fe"]]
        structs = [SmactStructure.from_file(fname) for fname in struct_files]

        with self.subTest(msg="Adding multiple structures to table."):
            try:
                self.db.add_structs(structs, self.TEST_TABLE)
            except Exception as e:
                self.fail(e)

        test_with_species_args = [
          [("Na", 1)],
          [("Cl", -1)],
          [("Na", 1), ("Cl", -1)],
          [("Cl", -1), ("Na", 1)],
          [("Cl", -1)],
          [("Na", 1), ("Cl", 1)],
          [("O", -2)],
          [("Ca", 2), ("Ti", 4), ("O", -2)],
        ]

        test_with_species_exp = [
          [structs[0]],
          [structs[0]],
          [structs[0]],
          [structs[0]],
          [structs[0]],
          [],
          [struct],
          [struct],
        ]

        for spec, expected in zip(test_with_species_args, test_with_species_exp):
            with self.subTest(msg=f"Retrieving species with {spec}"):
                self.assertEqual(self.db.get_with_species(spec, self.TEST_TABLE), expected)

        with open(TEST_MP_DATA, "rb") as f:
            mp_data = pickle.load(f)

        with self.subTest(msg="Testing adding downloaded MP structures."):
            added: int = self.db.add_mp_icsd(self.TEST_MP_TABLE, mp_data)
            self.assertEqual(added, 3)
예제 #12
0
    def test_smactStruc_comp_key(self):
        """Test generation of a composition key for `SmactStructure`s."""
        s1 = SmactStructure(*self._gen_empty_structure([('Ba', 2, 2), ('O', -2, 1), ('F', -1, 2)]))
        s2 = SmactStructure(*self._gen_empty_structure([('Fe', 2, 1), ('Fe', 3, 2), ('O', -2, 4)]))

        Ba = Species('Ba', 2)
        O = Species('O', -2)
        F = Species('F', -1)
        Fe2 = Species('Fe', 2)
        Fe3 = Species('Fe', 3)

        s3 = SmactStructure(*self._gen_empty_structure([(Ba, 2), (O, 1), (F, 2)]))
        s4 = SmactStructure(*self._gen_empty_structure([(Fe2, 1), (Fe3, 2), (O, 4)]))

        Ba_2OF_2 = "Ba_2_2+F_2_1-O_1_2-"
        Fe_3O_4 = "Fe_2_3+Fe_1_2+O_4_2-"
        self.assertEqual(s1.composition(), Ba_2OF_2)
        self.assertEqual(s2.composition(), Fe_3O_4)
        self.assertEqual(s3.composition(), Ba_2OF_2)
        self.assertEqual(s4.composition(), Fe_3O_4)