Skip to content

Commit

Permalink
Minor tests updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kavanase committed Jan 21, 2025
1 parent 10bf094 commit 042f885
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
5 changes: 3 additions & 2 deletions tests/test_chemical_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def test_vaspruns(self):

with pytest.raises(FileNotFoundError) as e:
chemical_potentials.CompetingPhasesAnalyzer("ZrO2", entries="path", subfolder="vasp_std")
assert "No vasprun files have been parsed, suggesting issues with parsing!" in str(e.value)
assert "No such file or directory" in str(e.value)

with pytest.raises(TypeError) as e:
chemical_potentials.CompetingPhasesAnalyzer("ZrO2", entries=0, subfolder="vasp_std")
Expand Down Expand Up @@ -916,7 +916,8 @@ def test_general_cpa_reloading(self):

class TestChemicalPotentialGrid(unittest.TestCase):
def setUp(self):
self.chempots = loadfn("../examples/Cu2SiSe3/Cu2SiSe3_chempots.json")
self.EXAMPLE_DIR = os.path.join(cwd, "../examples")
self.chempots = loadfn(os.path.join(self.EXAMPLE_DIR, "Cu2SiSe3/Cu2SiSe3_chempots.json"))
self.grid = chemical_potentials.ChemicalPotentialGrid(self.chempots)

def test_init(self):
Expand Down
23 changes: 5 additions & 18 deletions tests/test_fermisolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,16 @@ def test_get_py_sc_fermi_dos(self):
parameters.
"""
dos = Dos(
energies=np.array([0.0, 0.5, 1.0, 1.5, 2.0]),
energies=np.array([0.0, 0.5, 1.0, 1.5, 2.0, 2.5]),
densities={
Spin.up: np.array([1.0, 2.0, 0.0, 3.0, 4.0]),
Spin.down: np.array([0.5, 1.0, 0.0, 1.5, 2.0]),
Spin.up: np.array([1.0, 2.0, 0.0, 0.0, 3.0, 4.0]),
Spin.down: np.array([0.5, 1.0, 0.0, 0.0, 1.5, 2.0]),
},
efermi=0.5,
efermi=0.75,
)
fermi_dos = FermiDos(dos, structure=self.CdTe_fermi_dos.structure)
e_cbm, e_vbm = fermi_dos.get_cbm_vbm(tol=1e-4, abs_tol=True)
assert np.isclose(e_vbm, 0.5)
assert np.isclose(e_cbm, 1.5)
gap = fermi_dos.get_gap(tol=1e-4, abs_tol=True)
assert np.isclose(gap, 1.0)

from py_sc_fermi.dos import DOS

# https://github.com/bjmorgan/py-sc-fermi/pull/39
def _n0_index(self) -> int:
return np.where(self._edos >= self.bandgap)[0][0]

DOS._n0_index = _n0_index

# Test with default values
pyscfermi_dos = _get_py_sc_fermi_dos_from_fermi_dos(fermi_dos)
Expand All @@ -85,8 +74,6 @@ def _n0_index(self) -> int:
assert pyscfermi_dos.spin_polarised
np.testing.assert_array_equal(pyscfermi_dos.edos, fermi_dos.energies - e_vbm)

print(pyscfermi_dos._p0_index(), pyscfermi_dos._n0_index()) # for debugging

# test carrier concentrations (indirectly tests DOS densities, this is the relevant property
# from the DOS objects):
pyscfermi_scale = 1e24 / fermi_dos.volume
Expand Down Expand Up @@ -136,7 +123,7 @@ def test_get_py_sc_fermi_dos_from_CdTe_dos(self):
assert pyscfermi_dos.nelect == self.CdTe_fermi_dos.nelecs
assert pyscfermi_dos.nelect == 18
assert np.isclose(pyscfermi_dos.bandgap, self.CdTe_fermi_dos.get_gap(tol=1e-4, abs_tol=True))
assert np.isclose(pyscfermi_dos.bandgap, 1.5308, atol=1e-3)
assert np.isclose(pyscfermi_dos.bandgap, 1.5256, atol=1e-3)
assert not pyscfermi_dos.spin_polarised # SOC DOS

e_vbm = self.CdTe_fermi_dos.get_cbm_vbm(tol=1e-4, abs_tol=True)[1]
Expand Down
42 changes: 22 additions & 20 deletions tests/test_thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,11 @@ def _check_defect_thermo(
defect_thermo.bulk_formula == "CdTe"
and defect_thermo.dist_tol == 1.5
and len(defect_thermo.defect_entries) < 20
and len(defect_thermo.defect_entries) > 3
): # CdTe example defects
self._check_CdTe_example_dist_tol(defect_thermo, 3)
self._set_and_check_dist_tol(1.0, defect_thermo, 4)
self._set_and_check_dist_tol(0.5, defect_thermo, 5)
self._set_and_check_dist_tol(1.0, defect_thermo, 4)
self._set_and_check_dist_tol(0.5, defect_thermo, 5)

# test mismatching chempot warnings:
print("Checking mismatching chempots")
Expand Down Expand Up @@ -2404,31 +2405,32 @@ def test_defect_thermo_direct_from_parsing(self):
Test ``DefectThermodynamics`` directly from ``DefectsParser`` parsing
(i.e. before any saving/loading to/from ``json``).
"""
dp = DefectsParser(self.CdTe_EXAMPLE_DIR, dielectric=9.13)
dp = DefectsParser(f"{self.CdTe_EXAMPLE_DIR}/v_Cd_example_data", dielectric=9.13)
thermo_from_dp = dp.get_defect_thermodynamics()
self._check_defect_thermo(thermo_from_dp, dp.defect_dict) # checks and compares attributes

thermo_from_dp.to_json("test_thermo_from_dp.json")
reloaded_thermo_from_dp = loadfn("test_thermo_from_dp.json")
for thermo, dp_type in [(thermo_from_dp, "orig"), (reloaded_thermo_from_dp, "reloaded")]:
# previously this ``dataclass`` subclass object was not being serialized correctly (being
# reloaded just as a dict) due to ``asdict()`` usage:
entry = next(
i
for i in thermo.defect_entries.values()
if "kumagai_charge_correction" in i.corrections_metadata
)
assert str(
def test_efnv_json_saving(self):
"""
Test that the `pydefect_ExtendedFnvCorrection` objects are correctly
serialized/deserialized when saving/loading to/from json.
"""
# previously this ``dataclass`` subclass object was not being serialized correctly (being
# reloaded just as a dict) due to ``asdict()`` usage:
entry = next(
i
for i in self.CdTe_defect_thermo.defect_entries.values()
if "kumagai_charge_correction" in i.corrections_metadata
)
assert (
str(
type(
entry.corrections_metadata["kumagai_charge_correction"][
"pydefect_ExtendedFnvCorrection"
]
)
) == (
"<class 'pydefect.corrections.efnv_correction.ExtendedFnvCorrection'>"
), f"Checking {dp_type}"

if_present_rm("test_thermo_from_dp.json")
)
== "<class 'pydefect.corrections.efnv_correction.ExtendedFnvCorrection'>"
)

def test_formation_energy_mult_degen(self):
cdte_defect_thermo = DefectThermodynamics.from_json(
Expand Down Expand Up @@ -2768,7 +2770,7 @@ def _check_CdTe_mismatch_fermi_dos_warning(output, w):
print([str(warn.message) for warn in w]) # for debugging
assert not output
assert any(
"The VBM eigenvalue of the bulk DOS calculation (1.54 eV, band gap = 1.53 eV) differs "
"The VBM eigenvalue of the bulk DOS calculation (1.55 eV, band gap = 1.53 eV) differs "
"by >0.05 eV from `DefectThermodynamics.vbm/gap` (1.65 eV, band gap = 1.50 eV;"
in str(warn.message)
for warn in w
Expand Down

0 comments on commit 042f885

Please sign in to comment.