|
7 | 7 | import os
|
8 | 8 | import unittest
|
9 | 9 | import warnings
|
| 10 | +from copy import deepcopy |
10 | 11 |
|
11 | 12 | # Check if py_sc_fermi is available
|
12 | 13 | from importlib.util import find_spec
|
@@ -886,17 +887,14 @@ def test_parse_and_check_grid_like_chempots(self):
|
886 | 887 |
|
887 | 888 | def test_parse_and_check_grid_like_chempots_invalid_chempots(self):
|
888 | 889 | """
|
889 |
| - Test that ValueError is raised when chempots is None. |
| 890 | + Test that ``ValueError`` is raised when ``chempots`` is ``None``. |
890 | 891 | """
|
891 | 892 | # Temporarily remove chempots from defect_thermodynamics
|
892 |
| - original_chempots = self.defect_thermodynamics.chempots |
893 |
| - self.defect_thermodynamics.chempots = None |
| 893 | + solver = deepcopy(self.solver_doped) |
| 894 | + solver.defect_thermodynamics.chempots = None |
894 | 895 |
|
895 | 896 | with pytest.raises(ValueError):
|
896 |
| - self.solver_doped._parse_and_check_grid_like_chempots() |
897 |
| - |
898 |
| - # Restore original chempots |
899 |
| - self.defect_thermodynamics.chempots = original_chempots |
| 897 | + solver._parse_and_check_grid_like_chempots() |
900 | 898 |
|
901 | 899 |
|
902 | 900 | class TestFermiSolverWithLoadedData3D(unittest.TestCase):
|
@@ -991,52 +989,34 @@ def test_scan_chemical_potential_grid(self, mock_tqdm):
|
991 | 989 | ].drop_duplicates()
|
992 | 990 | assert len(unique_chempot_sets) > 0
|
993 | 991 |
|
994 |
| - def test_scan_chemical_potential_grid_no_chempots(self): |
995 |
| - """ |
996 |
| - Test that ``ValueError`` is raised when no chempots are provided and |
997 |
| - none are set. |
998 |
| - """ |
999 |
| - # TODO: Check if changing to deepcopy and remove is significantly slower, if not do this instead |
1000 |
| - # (less code) |
1001 |
| - # Temporarily remove chempots from defect_thermodynamics |
1002 |
| - original_chempots = self.defect_thermodynamics.chempots |
1003 |
| - self.defect_thermodynamics.chempots = None |
1004 |
| - |
1005 |
| - with pytest.raises(ValueError): |
1006 |
| - self.solver_doped.scan_chemical_potential_grid( |
1007 |
| - n_points=5, |
1008 |
| - annealing_temperature=800, |
1009 |
| - quenched_temperature=300, |
1010 |
| - ) |
1011 |
| - |
1012 |
| - # Restore original chempots |
1013 |
| - self.defect_thermodynamics.chempots = original_chempots |
1014 |
| - |
1015 | 992 | def test_scan_chemical_potential_grid_wrong_chempots(self):
|
1016 | 993 | """
|
1017 |
| - Test the error message when chemical potentials in the wrong format |
1018 |
| - (i.e. just one user-provided chemical potential limit) are provided. |
| 994 | + Test that ``ValueError`` is raised when no chempots are provided and |
| 995 | + None are available in ``self.defect_thermodynamics``, or only a single |
| 996 | + limit is provided. |
1019 | 997 | """
|
1020 | 998 | # Temporarily remove chempots from defect_thermodynamics
|
1021 |
| - original_chempots = self.defect_thermodynamics.chempots |
1022 |
| - self.defect_thermodynamics.chempots = None |
1023 |
| - |
1024 |
| - with pytest.raises(ValueError) as exc: |
1025 |
| - self.solver_doped.scan_chemical_potential_grid( |
1026 |
| - n_points=5, |
1027 |
| - chempots={"Cu": -0.5, "Si": -1.0, "Se": 2}, |
1028 |
| - annealing_temperature=800, |
1029 |
| - quenched_temperature=300, |
1030 |
| - ) |
1031 |
| - print(str(exc.value)) |
1032 |
| - assert ( |
1033 |
| - "Only one chemical potential limit is present in " |
1034 |
| - "`chempots`/`self.defect_thermodynamics.chempots`, which makes no sense for a chemical " |
1035 |
| - "potential grid scan" |
1036 |
| - ) in str(exc.value) |
1037 |
| - |
1038 |
| - # Restore original chempots |
1039 |
| - self.defect_thermodynamics.chempots = original_chempots |
| 999 | + solver = deepcopy(self.solver_doped) |
| 1000 | + solver.defect_thermodynamics.chempots = None |
| 1001 | + |
| 1002 | + for chempot_kwargs in [ |
| 1003 | + {}, |
| 1004 | + {"chempots": {"Cu": -0.5, "Si": -1.0, "Se": 2}}, |
| 1005 | + ]: |
| 1006 | + print(f"Testing with {chempot_kwargs}") |
| 1007 | + with pytest.raises(ValueError) as exc: |
| 1008 | + solver.scan_chemical_potential_grid( |
| 1009 | + n_points=5, |
| 1010 | + annealing_temperature=800, |
| 1011 | + quenched_temperature=300, |
| 1012 | + **chempot_kwargs, |
| 1013 | + ) |
| 1014 | + print(str(exc.value)) |
| 1015 | + assert ( |
| 1016 | + "Only one chemical potential limit is present in " |
| 1017 | + "`chempots`/`self.defect_thermodynamics.chempots`, which makes no sense for a chemical " |
| 1018 | + "potential grid scan" |
| 1019 | + ) in str(exc.value) |
1040 | 1020 |
|
1041 | 1021 |
|
1042 | 1022 | # TODO: Add explicit type check for `min_max_X` functions, like:
|
|
0 commit comments