def test_grids_joblib_roundtrip(tmpdir, grids): tmpdir.chdir() print(tmpdir) with open("tmp.joblib", mode="wb") as f: joblib.dump(grids, f, compress=3) with open("tmp.joblib", mode="rb") as f: actual = joblib.load(f) assert_dataclass_almost_equal(actual, grids)
def test_make_defect_structure_info(structures, def_str_info): perf, initial, final = structures info = MakeDefectStructureInfo(perf, initial, final, dist_tol=0.2, symprec=0.1, neighbor_cutoff_factor=1.2) actual = info.defect_structure_info print(actual) assert_dataclass_almost_equal(actual, def_str_info)
def test_assert_dataclass_almost_equal_float(): with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(1.0), 1.0) assert_dataclass_almost_equal(TestData(1.0), TestData(1.0)) with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(1.0), TestData(2.0)) with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(1.0), TestData(1.01), digit=2) assert_dataclass_almost_equal(TestData(1.0), TestData(1.01), digit=1)
def test_perturbed_structure(ortho_conventional, mocker): mock = mocker.patch( "pydefect.input_maker.defect_entries_maker.random_3d_vector") mock.return_value = ([0.0, 0.0, 0.35], 1.0) structure = copy_to_structure(ortho_conventional) actual = perturb_structure(structure, center=[0.0, 0.0, 0.0], cutoff=2.5-1e-5) expected_structure = structure expected_structure[0].frac_coords = [0.0, 0.0, 0.05] expected_site = PerturbedSite(element='H', distance=0.0, initial_coords=(0.0, 0.0, 0.0), perturbed_coords=(0.0, 0.0, 0.05), displacement=1.0) mock.assert_called_with(defaults.displace_distance) assert actual[0] == expected_structure assert_dataclass_almost_equal(actual[1][0], expected_site)
def test_assert_dataclass_almost_equal_dataclass_attribute(): assert_dataclass_almost_equal(TestData(d=Data(1.0)), TestData(d=Data(1.0))) with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(d=Data(1.0)), TestData(d=Data(1.01)), digit=2) assert_dataclass_almost_equal(TestData(d=Data(1.0)), TestData(d=Data(1.001)), digit=2)
def test_grids_from_chgcar(grids, chgcar): actual = Grids.from_chgcar(chgcar) assert_dataclass_almost_equal(actual, grids)
def test_grids_np_save_load_roundtrip(tmpdir, grids): tmpdir.chdir() print(tmpdir) grids.dump() actual = grids.from_file() assert_dataclass_almost_equal(actual, grids)
def test_assert_dataclass_almost_equal_nested_iterable(): assert_dataclass_almost_equal(TestData(c=[(1.0,)]), TestData(c=[(1.0,)])) with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(c=[(1.0,)]), TestData(c=[(2.0,)]), digit=2)
def test_assert_dataclass_almost_equal_iterable(): assert_dataclass_almost_equal(TestData(b=[1.0]), TestData(b=[1.0])) with pytest.raises(AssertionError): assert_dataclass_almost_equal(TestData(b=[1.0]), TestData(b=[2.0]))
def test_make_band_edge_orbital_infos(mocker): mock_procar = mocker.Mock(spec=Procar, autospec=True) mock_vasprun = mocker.Mock(spec=Vasprun, autospec=True) mock_str_info = mocker.Mock(spec=DefectStructureInfo, autospec=True) mock_vasprun.actual_kpoints = [[0.0, 0.0, 0.0]] mock_vasprun.actual_kpoints_weights = [1.0] mock_vasprun.final_structure = Structure(Lattice.cubic(1), species=["H", "H", "He"], coords=[[0] * 3] * 3) mock_vasprun.eigenvalues = { Spin.up: np.array([ [ [-3.01, 1.], [-2.90, 1.], # vbm [8.01, 0.] ], [[-3.01, 1.], [-2.99, 1.], [7.90, 0.]] ]), # cbm # shift one band for testing magnetization Spin.down: np.array([[[-2.99, 1.], [7.99, 0.], [10.00, 0.]], [[-3.01, 1.], [8.00, 0.], [10.00, 0.]]]) } mock_vasprun.efermi = 20.0 # s: 1, p: 3, d: 5, f: 7 = 16 orbitals mock_procar.data = { Spin.up: np.array([ [ [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 0th band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 0th band, 2nd atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], # 1st kpt, 0th band, 3rd atom [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 1st band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] ], [ [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 2nd kpt, 0th band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] ] ]), Spin.down: np.array([ [ [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 0th band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 0th band, 2nd atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], # 1st kpt, 0th band, 3rd atom [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 1st kpt, 1st band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] ], [ [ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], # 2nd kpt, 0th band, 1st atom [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] ] ]), } mock_str_info.neighbor_atom_indices = [0] actual = make_band_edge_orbital_infos(mock_procar, mock_vasprun, vbm=0.0, cbm=5.0, str_info=mock_str_info) expected = BandEdgeOrbitalInfos( orbital_infos=[[[ OrbitalInfo(energy=-2.9, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=1.0, participation_ratio=1.0), OrbitalInfo(energy=8.01, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0) ], [ OrbitalInfo(energy=-2.99, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=1.0, participation_ratio=1.0), OrbitalInfo(energy=7.90, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0) ]], [[ OrbitalInfo(energy=7.99, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0), OrbitalInfo(energy=10.00, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0) ], [ OrbitalInfo(energy=8.00, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0), OrbitalInfo(energy=10.00, orbitals={ "H": [1.0, 0.0, 0.0, 0.0], "He": [0.0, 0.0, 0.0, 0.0] }, occupation=0.0, participation_ratio=1.0) ]]], kpt_coords=[(0.0, 0.0, 0.0)], kpt_weights=[1.0], lowest_band_index=1, fermi_level=20.0) assert_dataclass_almost_equal(actual, expected)