From 553db2b246ea6a9ffe5d99f781d6cbbc08770329 Mon Sep 17 00:00:00 2001 From: Jimmy Shen <14003693+jmmshn@users.noreply.github.com> Date: Tue, 21 May 2024 15:25:32 -0700 Subject: [PATCH] Kumagai Correction (#196) * plot fed * plot fef * plot fed * plot fed * plot fed * plot fed * remove uid * remove uid * add pydefect back * test lint --- .../analysis/defects/corrections/kumagai.py | 2 +- pyproject.toml | 3 +- tests/test_corrections.py | 31 ++++++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pymatgen/analysis/defects/corrections/kumagai.py b/pymatgen/analysis/defects/corrections/kumagai.py index 18d99130..422c7ed4 100644 --- a/pymatgen/analysis/defects/corrections/kumagai.py +++ b/pymatgen/analysis/defects/corrections/kumagai.py @@ -40,7 +40,7 @@ def _check_import_pydefect() -> None: """Import pydefect if it is installed.""" - if __has_pydefect__: + if not __has_pydefect__: msg = "vise/pydefect is not installed. Please install it first." raise ModuleNotFoundError( msg, diff --git a/pyproject.toml b/pyproject.toml index ed6b4ff3..0d2284f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,12 +35,13 @@ dev = ["pre-commit>=2.12.1"] docs = [ "jupyter-book>=0.13.1", ] -optional = ["pydefect>=0.6.2", "dscribe>=2.0.0", "numba"] +optional = ["pydefect", "dscribe>=2.0.0", "numba"] strict = [ "pymatgen==2024.5.1", "dscribe==2.1.0", "mp-pyrho==0.4.4", + "pydefect==0.9.4", ] tests = ["pytest==8.2.0", "pytest-cov==5.0.0", "nbmake==1.5.3"] diff --git a/tests/test_corrections.py b/tests/test_corrections.py index 649ab441..e3c2db71 100644 --- a/tests/test_corrections.py +++ b/tests/test_corrections.py @@ -3,6 +3,7 @@ get_freysoldt_correction, plot_plnr_avg, ) +from pymatgen.analysis.defects.corrections.kumagai import get_efnv_correction, get_structure_with_pot def test_freysoldt(data_Mg_Ga) -> None: @@ -71,17 +72,23 @@ def test_freysoldt_sxdefect_compare(v_N_GaN) -> None: assert results[q] == pytest.approx(references[q], abs=1e-3) -# def test_kumagai(test_dir): -# sb = get_structure_with_pot(test_dir / "Mg_Ga" / "bulk_sc") -# sd0 = get_structure_with_pot(test_dir / "Mg_Ga" / "q=0") -# sd1 = get_structure_with_pot(test_dir / "Mg_Ga" / "q=1") +def test_kumagai(test_dir): + sb = get_structure_with_pot(test_dir / "Mg_Ga" / "bulk_sc") + sd0 = get_structure_with_pot(test_dir / "Mg_Ga" / "q=0") + sd1 = get_structure_with_pot(test_dir / "Mg_Ga" / "q=1") -# res0 = get_efnv_correction( -# 0, sd0, sb, dielectric_tensor=[[1, 0, 0], [0, 1, 0], [0, 0, 1]] -# ) -# assert res0.correction_energy == pytest.approx(0, abs=1e-4) + res0 = get_efnv_correction( + 0, sd0, sb, dielectric_tensor=[[1, 0, 0], [0, 1, 0], [0, 0, 1]] + ) + assert res0.correction_energy == pytest.approx(0, abs=1e-4) + + res1 = get_efnv_correction( + 1, sd1, sb, dielectric_tensor=[[1, 0, 0], [0, 1, 0], [0, 0, 1]] + ) + assert res1.correction_energy > 0 -# res1 = get_efnv_correction( -# 1, sd1, sb, dielectric_tensor=[[1, 0, 0], [0, 1, 0], [0, 0, 1]] -# ) -# assert res1.correction_energy > 0 +def test_kumagai_missing(): + from pymatgen.analysis.defects.corrections import kumagai + kumagai.__has_pydefect__ = False + with pytest.raises(ImportError): + kumagai._check_import_pydefect()