Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kavanase committed Jun 18, 2024
1 parent 30290e1 commit d0b7fb8
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 193 deletions.
5 changes: 4 additions & 1 deletion doped/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ def __init__(
self.error_tolerance = error_tolerance
self.bulk_path = bulk_path
self.subfolder = subfolder
self.bulk_band_gap_vr = bulk_band_gap_vr
if bulk_band_gap_vr and not isinstance(bulk_band_gap_vr, Vasprun):
self.bulk_band_gap_vr = get_vasprun(bulk_band_gap_vr, parse_projected_eigen=False)
else:
self.bulk_band_gap_vr = bulk_band_gap_vr
self.processes = processes
self.json_filename = json_filename
self.parse_projected_eigen = parse_projected_eigen
Expand Down
11 changes: 3 additions & 8 deletions doped/thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,10 +1357,7 @@ def _parse_fermi_dos(
fdos_band_gap = fdos.get_gap(tol=1e-4, abs_tol=True) # rounds the DOS outputs to 4 dp

if isinstance(bulk_dos, str):
if hasattr(self, "_fermi_dos_hash") and calculate_md5(bulk_dos) == self._fermi_dos_hash:
return self.fermi_dos # already parsed, don't waste time re-parsing, & no need to re-warn

bulk_dos = get_vasprun(bulk_dos, parse_dos=True)
bulk_dos = get_vasprun(bulk_dos, parse_dos=True) # converted to fdos in next block

if isinstance(bulk_dos, Vasprun): # either supplied Vasprun or parsed from string there
fdos_band_gap, _cbm, fdos_vbm, _ = bulk_dos.eigenvalue_band_properties
Expand Down Expand Up @@ -1500,8 +1497,6 @@ def get_equilibrium_fermi_level(
"""
if bulk_dos is not None:
self.fermi_dos = self._parse_fermi_dos(bulk_dos, skip_check=skip_check)
if isinstance(bulk_dos, str):
self._fermi_dos_hash = calculate_md5(bulk_dos) # to avoid re-parsing unnecessarily
elif not hasattr(self, "fermi_dos"):
raise ValueError(
"No bulk DOS calculation (`bulk_dos`) provided or previously parsed to "
Expand Down Expand Up @@ -1721,8 +1716,6 @@ def get_quenched_fermi_level_and_concentrations(

if bulk_dos is not None:
self.fermi_dos = self._parse_fermi_dos(bulk_dos, skip_check=kwargs.get("skip_check", False))
if isinstance(bulk_dos, str):
self._fermi_dos_hash = calculate_md5(bulk_dos) # to avoid re-parsing unnecessarily
elif not hasattr(self, "fermi_dos"):
raise ValueError(
"No bulk DOS calculation (`bulk_dos`) provided or previously parsed to "
Expand Down Expand Up @@ -1767,6 +1760,8 @@ def get_quenched_fermi_level_and_concentrations(
# gap not 0
)
assert not isinstance(annealing_fermi_level, tuple) # float w/ return_concs=False, for typing
self.fermi_dos = orig_fermi_dos # reset to original DOS for quenched calculations

annealing_defect_concentrations = self.get_equilibrium_concentrations(
chempots=chempots,
limit=limit,
Expand Down
2 changes: 1 addition & 1 deletion examples/CdTe/CdTe_example_defect_dict.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/CdTe/CdTe_example_thermo.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/Sb2Si2Te6/Sb2Si2Te6_example_defect_dict.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/Sb2Si2Te6/Sb2Si2Te6_example_thermo.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/YTOS/YTOS_example_defect_dict.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/YTOS/YTOS_example_thermo.json

Large diffs are not rendered by default.

310 changes: 157 additions & 153 deletions examples/thermodynamics_tutorial.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/Sb2Se3/Sb2Se3_O_example_thermo.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/Sb2Se3/defect/Sb2Se3_O_example_defect_dict.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/V2O5/V2O5_example_defect_dict.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/V2O5/V2O5_example_thermo.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/data/ZnS/ZnS_thermo.json

Large diffs are not rendered by default.

Binary file modified tests/data/remote_baseline_plots/CdTe_LZ_Te_rich_Fermi_levels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/remote_baseline_plots/CdTe_LZ_Te_rich_concentrations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/data/solid_solution/bulk/voronoi_nodes.json

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from doped.generation import DefectsGenerator, get_defect_name_from_defect, get_defect_name_from_entry
from doped.utils.eigenvalues import get_eigenvalue_analysis
from doped.utils.parsing import (
Vasprun,
get_defect_site_idxs_and_unrelaxed_structure,
get_defect_type_and_composition_diff,
get_orientational_degeneracy,
Expand Down Expand Up @@ -223,7 +224,7 @@ def _check_default_CdTe_DefectsParser_outputs(
assert CdTe_dp.error_tolerance == 0.05
assert CdTe_dp.bulk_path == self.CdTe_BULK_DATA_DIR # automatically determined
assert CdTe_dp.subfolder == "vasp_ncl" # automatically determined
assert CdTe_dp.bulk_band_gap_path is None
assert CdTe_dp.bulk_band_gap_vr is None

self._check_DefectsParser(CdTe_dp)
assert (
Expand Down Expand Up @@ -446,7 +447,7 @@ def test_DefectsParser_CdTe_custom_settings(self):
dielectric=[9.13, 9.13, 9.13],
error_tolerance=0.01,
skip_corrections=False,
bulk_band_gap_path=self.CdTe_BULK_DATA_DIR,
bulk_band_gap_vr=f"{self.CdTe_BULK_DATA_DIR}/vasprun.xml",
processes=4,
json_filename="test_pop.json",
)
Expand Down Expand Up @@ -476,7 +477,7 @@ def test_DefectsParser_CdTe_custom_settings(self):
assert dp.output_path == self.CdTe_EXAMPLE_DIR
assert dp.dielectric == [9.13, 9.13, 9.13]
assert dp.error_tolerance == 0.01
assert dp.bulk_band_gap_path == self.CdTe_BULK_DATA_DIR
assert isinstance(dp.bulk_band_gap_vr, Vasprun)
assert dp.processes == 4
assert dp.json_filename == "test_pop.json"

Expand Down Expand Up @@ -1058,7 +1059,7 @@ def test_SrTiO3_diff_ISYM_bulk_defect_and_concentration_funcs(self):

# test get_quenched_fermi_level_and_concentrations
fermi_level, e_conc, h_conc, conc_df = thermo.get_quenched_fermi_level_and_concentrations(
bulk_dos_vr=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
bulk_dos=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
annealing_temperature=300,
per_charge=False,
)
Expand All @@ -1072,7 +1073,7 @@ def test_SrTiO3_diff_ISYM_bulk_defect_and_concentration_funcs(self):
assert conc_df.index.name == "Defect"

fermi_level, e_conc, h_conc, conc_df = thermo.get_quenched_fermi_level_and_concentrations(
bulk_dos_vr=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
bulk_dos=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
annealing_temperature=300,
skip_formatting=True,
)
Expand All @@ -1091,7 +1092,7 @@ def test_SrTiO3_diff_ISYM_bulk_defect_and_concentration_funcs(self):
assert list(conc_df.iloc[i]) == row

fermi_level, e_conc, h_conc, conc_df = thermo.get_quenched_fermi_level_and_concentrations(
bulk_dos_vr=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
bulk_dos=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
annealing_temperature=300,
per_site=True,
)
Expand All @@ -1110,7 +1111,7 @@ def test_SrTiO3_diff_ISYM_bulk_defect_and_concentration_funcs(self):
assert list(conc_df.iloc[i]) == row

fermi_level, e_conc, h_conc, conc_df = thermo.get_quenched_fermi_level_and_concentrations(
bulk_dos_vr=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
bulk_dos=f"{self.SrTiO3_DATA_DIR}/bulk_sp333/vasprun.xml",
annealing_temperature=300,
per_site=True,
per_charge=False,
Expand Down
27 changes: 15 additions & 12 deletions tests/test_thermodynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from monty.serialization import dumpfn, loadfn

from doped.generation import _sort_defect_entries
from doped.thermodynamics import DefectThermodynamics, scissor_dos
from doped.thermodynamics import DefectThermodynamics, get_fermi_dos, scissor_dos
from doped.utils.parsing import get_vasprun
from doped.utils.symmetry import _get_sga, point_symmetry

Expand Down Expand Up @@ -321,13 +321,13 @@ def _check_defect_thermo(
print([str(warning.message) for warning in w]) # for debugging
assert not w

with pytest.raises(TypeError) as exc:
with pytest.raises(ValueError) as exc:
defect_thermo.get_equilibrium_fermi_level()
assert "missing 1 required positional argument: 'bulk_dos_vr'" in str(exc.value)
assert "No bulk DOS calculation (`bulk_dos`) provided" in str(exc.value)

with pytest.raises(TypeError) as exc:
with pytest.raises(ValueError) as exc:
defect_thermo.get_quenched_fermi_level_and_concentrations()
assert "missing 1 required positional argument: 'bulk_dos_vr'" in str(exc.value)
assert "No bulk DOS calculation (`bulk_dos`) provided" in str(exc.value)

if defect_thermo.chempots is None:
with pytest.raises(ValueError) as exc:
Expand Down Expand Up @@ -1956,10 +1956,11 @@ def setUpClass(cls):
cls.defect_thermo = DefectThermodynamics(cls.defect_dict)
cls.defect_thermo.chempots = cls.CdTe_chempots

cls.fermi_dos = loadfn(
os.path.join(cls.CdTe_EXAMPLE_DIR, "CdTe_prim_k181818_NKRED_2_fermi_dos.json")
cls.fermi_dos = get_fermi_dos(
os.path.join(cls.CdTe_EXAMPLE_DIR, "CdTe_prim_k181818_NKRED_2_vasprun.xml.gz")
)
cls.anneal_temperatures = np.arange(200, 1401, 50)
cls.reduced_anneal_temperatures = np.arange(200, 1401, 100) # for quicker testing

cls.annealing_dict = {}

Expand Down Expand Up @@ -2012,6 +2013,7 @@ def test_calculated_fermi_levels(self):
)
quenched_fermi_levels = np.array([v["fermi_level"] for k, v in self.annealing_dict.items()])
assert np.isclose(np.mean(self.anneal_temperatures[12:16]), 875)
# remember this is LZ thermo, not FNV thermo shown in thermodynamics tutorial
assert np.isclose(np.mean(quenched_fermi_levels[12:16]), 0.318674, atol=1e-3)

ax.plot(
Expand Down Expand Up @@ -2094,10 +2096,10 @@ def test_calculated_fermi_level_k10(self):
"""
k10_dos_vr_path = os.path.join(self.module_path, "data/CdTe/CdTe_prim_k101010_dos_vr.xml.gz")

for i, bulk_dos_vr in enumerate([k10_dos_vr_path, get_vasprun(k10_dos_vr_path, parse_dos=True)]):
for i, bulk_dos in enumerate([k10_dos_vr_path, get_vasprun(k10_dos_vr_path, parse_dos=True)]):
print(f"Testing k10 DOS with thermo for {'str input' if i == 0 else 'DOS object input'}")
quenched_fermi_levels = []
for anneal_temp in self.anneal_temperatures:
for anneal_temp in self.reduced_anneal_temperatures:
gap_shift = belas_linear_fit(anneal_temp) - 1.5
(
fermi_level,
Expand All @@ -2106,15 +2108,16 @@ def test_calculated_fermi_level_k10(self):
conc_df,
) = self.defect_thermo.get_quenched_fermi_level_and_concentrations(
# quenching to 300K (default)
bulk_dos_vr=bulk_dos_vr,
bulk_dos=bulk_dos,
limit="Te-rich",
annealing_temperature=anneal_temp,
delta_gap=gap_shift,
)
quenched_fermi_levels += [fermi_level]

# (approx) same result as with k181818 NKRED=2 (0.319104 eV with this dos)
assert np.isclose(np.mean(quenched_fermi_levels[12:16]), 0.318674, atol=1e-3)
# (approx) same result as with k181818 NKRED=2 (0.31825 eV with this DOS)
# remember this is LZ thermo, not FNV thermo shown in thermodynamics tutorial
assert np.isclose(np.mean(quenched_fermi_levels[6:8]), 0.31825, atol=1e-3)

@custom_mpl_image_compare(filename="CdTe_LZ_Te_rich_concentrations.png")
def test_calculated_concentrations(self):
Expand Down

0 comments on commit d0b7fb8

Please sign in to comment.