Exemple #1
0
class TestSphinxStructure(unittest.TestCase):
    """
    Testing routines in the sphinx/structure module.
    """
    def setUp(self):
        self.file_location = os.path.dirname(os.path.abspath(__file__))
        structure_directory = "../static/sphinx/sphinx_test_files"
        file_list = ["structure_1.sx", "structure_2.sx"]
        self.file_list = [
            posixpath.join(self.file_location, structure_directory, f)
            for f in file_list
        ]
        atom_numbers = np.random.randint(low=1, high=99, size=(1, 3)).flatten()
        cell = 10.0 * np.eye(3)
        pos = 0.5 * np.ones((3, 3)) - 0.5 * np.eye(3)
        self.structure = Atoms(numbers=atom_numbers, cell=cell, positions=pos)
        self.assertIsInstance(self.structure, Atoms)
        self.structure.repeat([2, 2, 2])
        self.element_list = self.structure.get_chemical_elements()

    def test_read_atoms(self):
        for i, f in enumerate(self.file_list):
            atoms = read_atoms(filename=f)
            self.assertIsInstance(atoms, Atoms)
            if i == 0:
                self.assertEqual(atoms.get_chemical_formula(), "Mg5")
                self.assertTrue(
                    np.allclose(
                        atoms.cell / BOHR_TO_ANGSTROM,
                        [
                            [18.0936435257, 0.0, 0.0],
                            [-12.0624290171, 20.8927399203, 0.0],
                            [0.0, 0.0, 39.1932378013],
                        ],
                    ))
            if i == 1:
                self.assertEqual(atoms.get_chemical_formula(), "C2Mg5")
                self.assertTrue(
                    np.allclose(
                        atoms.cell / BOHR_TO_ANGSTROM,
                        [
                            [18.09364353, 0.00000000, 0.00000000],
                            [-6.03121451, 20.89273992, -0.00000000],
                            [0.00000000, 0.00000000, 39.19323780],
                        ],
                    ))

    def tearDown(self):
        pass
Exemple #2
0
class TestVaspStructure(unittest.TestCase):
    """
    Testing routines in the vasp/structure module.
    """
    def setUp(self):
        self.file_location = os.path.dirname(os.path.abspath(__file__))
        poscar_directory = os.path.join(
            self.file_location, "../static/vasp_test_files/poscar_samples")
        file_list = os.listdir(poscar_directory)
        self.file_list = [
            posixpath.join(poscar_directory, f) for f in file_list
        ]
        atom_numbers = np.random.randint(low=1, high=99, size=(1, 3)).flatten()
        cell = 10.0 * np.eye(3)
        pos = 0.5 * np.ones((3, 3)) - 0.5 * np.eye(3)
        self.structure = Atoms(numbers=atom_numbers, cell=cell, positions=pos)
        self.assertIsInstance(self.structure, Atoms)
        self.structure.repeat([2, 2, 2])
        self.element_list = self.structure.get_chemical_elements()

    def test_atoms_from_string(self):
        for poscar_file in self.file_list:
            with open(poscar_file, "r") as f:
                lines = f.readlines()
                atoms = atoms_from_string(string=lines)
                self.assertIsInstance(atoms, Atoms)

    def test_read_atoms(self):
        for f in self.file_list:
            atoms = read_atoms(filename=f)
            self.assertIsInstance(atoms, Atoms)
            if f.split("/")[-1] == "POSCAR_1":
                self.assertEqual(len(atoms), 744)
                self.assertEqual(len(atoms.select_index("H")), 432)
                self.assertEqual(len(atoms.select_index("O")), 216)
                self.assertEqual(len(atoms.select_index("Mg")), 96)
            if f.split("/")[-1] == "POSCAR_scaled":
                self.assertEqual(len(atoms), 256)
                self.assertEqual(len(atoms.select_index("Cu")), 256)
                cell = np.eye(3) * 4. * 3.63
                self.assertTrue(np.array_equal(atoms.cell, cell))
                self.assertEqual(atoms.get_spacegroup()["Number"], 225)
            if f.split("/")[-1] == "POSCAR_volume_scaled":
                self.assertEqual(len(atoms), 256)
                self.assertEqual(len(atoms.select_index("Cu")), 256)
                cell = np.eye(3) * 4. * 3.63
                self.assertTrue(np.array_equal(atoms.cell, cell))
                self.assertEqual(atoms.get_spacegroup()["Number"], 225)
            if f.split("/")[-1] == "POSCAR_random":
                self.assertEqual(len(atoms), 33)
                self.assertEqual(len(atoms.selective_dynamics), 33)
                self.assertEqual(len(atoms.select_index("Zn")), 1)
                self.assertFalse(
                    np.array_equal(atoms.selective_dynamics[0],
                                   [True, True, True]))
                self.assertTrue(
                    np.array_equal(atoms.selective_dynamics[0],
                                   [False, False, False]))
                self.assertTrue(
                    np.array_equal(atoms.selective_dynamics[-5],
                                   [True, True, True]))
                self.assertTrue(
                    np.array_equal(atoms.selective_dynamics[-4],
                                   [False, False, False]))

    def test_write_poscar(self):
        write_poscar(structure=self.structure,
                     filename=posixpath.join(self.file_location,
                                             "POSCAR_test"))
        test_atoms = read_atoms(
            posixpath.join(self.file_location, "POSCAR_test"))
        self.assertEqual(self.structure.get_chemical_formula(),
                         test_atoms.get_chemical_formula())
        struct = self.structure.copy()
        struct.add_tag(selective_dynamics=[True, True, True])
        write_poscar(structure=struct,
                     filename=posixpath.join(self.file_location,
                                             "POSCAR_test"))
        test_atoms = read_atoms(
            posixpath.join(self.file_location, "POSCAR_test"))
        truth_array = np.empty_like(struct.positions, dtype=bool)
        truth_array[:] = [True, True, True]
        self.assertTrue(
            np.array_equal(np.array(test_atoms.selective_dynamics),
                           truth_array))
        os.remove(posixpath.join(self.file_location, "POSCAR_test"))

    def test_vasp_sorter(self):
        write_poscar(structure=self.structure,
                     filename=posixpath.join(self.file_location,
                                             "POSCAR_test"))
        test_atoms = read_atoms(
            posixpath.join(self.file_location, "POSCAR_test"))
        vasp_order = vasp_sorter(self.structure)
        self.assertEqual(len(self.structure), len(test_atoms))
        self.assertEqual(self.structure[vasp_order], test_atoms)
        os.remove(posixpath.join(self.file_location, "POSCAR_test"))

    def tearDown(self):
        pass