Example #1
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)
Example #2
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())
Example #3
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)
Example #4
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+")
Example #5
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
Example #6
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)
Example #7
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)
Example #8
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)
Example #9
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)