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.list()), truth_array)) os.remove(posixpath.join(self.file_location, "POSCAR_test")) struct = self.structure.copy() struct.add_tag(selective_dynamics=[True, True, True]) write_poscar( structure=struct, filename=posixpath.join(self.file_location, "POSCAR_test"), cartesian=False, ) test_atoms_new = read_atoms( posixpath.join(self.file_location, "POSCAR_test")) self.assertEqual(test_atoms, test_atoms_new) os.remove(posixpath.join(self.file_location, "POSCAR_test"))
def setUpClass(cls): state.update({ 'resource_paths': os.path.join(os.path.dirname(os.path.abspath(__file__)), "../static") }) cls.execution_path = os.path.dirname(os.path.abspath(__file__)) cls.project = Project(os.path.join(cls.execution_path, "test_vasp")) cls.job = cls.project.create_job("Vasp", "trial") cls.job_spin = cls.project.create_job("Vasp", "spin") cls.job_spin.structure = CrystalStructure("Fe", BravaisBasis="bcc", a=2.83) cls.job_spin.structure = cls.job_spin.structure.repeat(2) cls.job_spin.structure[2] = "Se" cls.job_spin.structure[3] = "O" cls.job_metadyn = cls.project.create_job("VaspMetadyn", "trial_metadyn") cls.job_complete = Vasp( project=ProjectHDFio(project=cls.project, file_name="vasp_complete"), job_name="vasp_complete", ) poscar_file = posixpath.join( cls.execution_path, "../static/vasp_test_files/full_job_sample/POSCAR") cls.job_complete.structure = read_atoms(poscar_file, species_from_potcar=True) poscar_file = posixpath.join( cls.execution_path, "../static/vasp_test_files/poscar_samples/POSCAR_metadyn") cls.job_metadyn.structure = read_atoms(poscar_file)
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 test_parse_charge_vol(self): filename = os.path.join(self.file_location, "../static/dft/bader_files/ACF.dat") struct = read_atoms( os.path.join(self.file_location, "../static/vasp_test_files/bader_test/POSCAR")) charges, volumes = parse_charge_vol_file(structure=struct, filename=filename) self.assertTrue(np.array_equal(charges, [0.438202, 0.438197, 7.143794])) self.assertTrue( np.array_equal(volumes, [287.284690, 297.577878, 415.155432]))
def test_manip_contcar(self): for f in self.file_list: if "CONTCAR_Mg" in f: struct = read_atoms(f) Mg_indices = struct.select_index("Mg") add_pos = np.zeros_like(struct.positions) max_Mg = np.argmax(struct.positions[Mg_indices, 2]) init_z = struct.positions[max_Mg, 2] add_pos[np.argsort(vasp_sorter(struct))[max_Mg], 2] += 5.0 manip_contcar(filename=f, new_filename="manip_file", add_pos=add_pos) new_struct = read_atoms("manip_file") Mg_indices = new_struct.select_index("Mg") max_Mg = np.argmax(new_struct.positions[Mg_indices, 2]) final_z = new_struct.positions[max_Mg, 2] self.assertEqual(round(final_z - init_z, 3), 5.0) os.remove("manip_file") break positions = np.ones((3, 3)) positions[0] = [5.0, 5.0, 5.0] positions[1] = [5.0, 5.7, 5.7] positions[2] = [5.0, -5.7, -5.7] struct = Atoms(["O", "H", "H"], positions=positions, cell=10.0 * np.eye(3)) write_poscar(structure=struct, filename="simple_water") add_pos = np.zeros_like(positions) poscar_order = np.argsort(vasp_sorter(struct)) add_pos[poscar_order[struct.select_index("O")], 2] += 3 manip_contcar("simple_water", "simple_water_new", add_pos) new_struct = read_atoms("simple_water_new") self.assertEqual(new_struct.positions[new_struct.select_index("O"), 2], 8) os.remove("simple_water") os.remove("simple_water_new")
def collect_output_log(self, dir_name="randSpgOut"): """ general purpose routine to extract output from logfile Args: file_name (str): output.log - optional """ self._lst_of_struct = [[ file_name.replace('-', '_'), read_atoms(filename=posixpath.join(self.working_directory, dir_name, file_name)) ] for file_name in os.listdir( posixpath.join(self.working_directory, dir_name))] for structure_name, structure in self._lst_of_struct: with self.project_hdf5.open("output/structures/" + structure_name) as h5: structure.to_hdf(h5)
def test_read_atoms(self): for f in self.file_list: if f.split("/")[-1] == "POSCAR_velocity": atoms, velocities = read_atoms(filename=f, return_velocities=True) self.assertEqual(len(atoms), 19) self.assertEqual(np.shape(velocities), (19, 3)) self.assertEqual(len(atoms.selective_dynamics), 19) self.assertEqual(len(atoms.select_index("Mg")), 10) self.assertIsInstance(atoms.selective_dynamics, SparseList) neon_indices = atoms.select_index("Ne") hydrogen_indices = atoms.select_index("H") oxygen_indices = atoms.select_index("O") truth_array = np.empty_like(atoms.positions[neon_indices], dtype=bool) truth_array[:, :] = True sel_dyn = np.array(atoms.selective_dynamics.list()) self.assertTrue( np.array_equal(sel_dyn[neon_indices], np.logical_not(truth_array))) truth_array = np.empty_like(atoms.positions[oxygen_indices], dtype=bool) truth_array[:, :] = True self.assertTrue( np.array_equal(sel_dyn[oxygen_indices], truth_array)) truth_array = np.empty_like(atoms.positions[hydrogen_indices], dtype=bool) truth_array[:, :] = True self.assertTrue( np.array_equal(sel_dyn[hydrogen_indices], truth_array)) velocities_neon = np.zeros_like( np.array(velocities)[neon_indices]) self.assertTrue( np.array_equal( np.array(velocities)[neon_indices], velocities_neon)) if f.split("/")[-1] == "POSCAR_no_species": atoms = read_atoms(filename=f) self.assertEqual(len(atoms), 33) self.assertEqual(len(atoms.selective_dynamics), 33) elif f.split("/")[-1] != "POSCAR_spoilt": 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) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") atoms_new, velocities = read_atoms( filename=f, return_velocities=True) self.assertEqual(w[-1].category, UserWarning) warning_string = ( "The velocities are either not available or they are incomplete/corrupted. " "Returning empty list instead") self.assertEqual(str(w[-1].message), warning_string) self.assertEqual(atoms_new, atoms) self.assertEqual(velocities, list()) if f.split("/")[-1] == "POSCAR_scaled": self.assertEqual(len(atoms), 256) self.assertEqual(len(atoms.select_index("Cu")), 256) cell = np.eye(3) * 4.0 * 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.0 * 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.assertIsInstance(atoms.selective_dynamics, SparseList) truth_array = np.empty_like(atoms.positions, dtype=bool) truth_array[:] = [True, True, True] truth_array[0] = [False, False, False] truth_array[-4:] = [False, False, False] self.assertTrue( np.array_equal(atoms.selective_dynamics.list(), truth_array))