Skip to content

Commit 722b987

Browse files
Fix antisite generation in mixed-valence systems (#210)
* Use sets to handle mixed valence species and add test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Typing and formatting * Fix test_ccd.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c2aed9c commit 722b987

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

pymatgen/analysis/defects/generators.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
def generate(
9191
self,
9292
structure: Structure,
93-
rm_species: list[str | Species] | None = None,
93+
rm_species: set[str | Species] | list[str | Species] | None = None,
9494
**kwargs,
9595
) -> Generator[Vacancy, None, None]:
9696
"""Generate a vacancy defects.
@@ -103,10 +103,10 @@ def generate(
103103
Returns:
104104
Generator[Vacancy, None, None]: Generator that yields a list of ``Vacancy`` objects.
105105
"""
106-
all_species = [*map(_element_str, structure.composition.elements)]
107-
rm_species = all_species if rm_species is None else [*map(str, rm_species)]
106+
all_species = {*map(_element_str, structure.composition.elements)}
107+
rm_species = all_species if rm_species is None else {*map(str, rm_species)}
108108

109-
if not set(rm_species).issubset(all_species):
109+
if not rm_species.issubset(all_species):
110110
msg = f"rm_species({rm_species}) must be a subset of the structure's species ({all_species})."
111111
raise ValueError(
112112
msg,
@@ -235,7 +235,7 @@ def generate(
235235
structure: The bulk structure the anti-site defects are generated from.
236236
**kwargs: Additional keyword arguments for the ``Substitution.generate`` function.
237237
"""
238-
all_species = [*map(_element_str, structure.composition.elements)]
238+
all_species = {*map(_element_str, structure.composition.elements)}
239239
subs = collections.defaultdict(list)
240240
for u, v in combinations(all_species, 2):
241241
subs[u].append(v)

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import defaultdict
22
from pathlib import Path
3+
import numpy as np
34

45
import pytest
56
from monty.serialization import loadfn
@@ -23,6 +24,15 @@ def gan_struct(test_dir):
2324
return Structure.from_file(test_dir / "GaN.vasp")
2425

2526

27+
@pytest.fixture(scope="session")
28+
def mixed_valence_struct(test_dir):
29+
return Structure(
30+
lattice=np.array([[3.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 3.0]]),
31+
species=["Cu+", "Cu+", "Cu2+", "O2-"],
32+
coords=[[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.5, 0.5, 0.0], [0.5, 0.0, 0.5]],
33+
)
34+
35+
2636
@pytest.fixture(scope="session")
2737
def stable_entries_Mg_Ga_N(test_dir):
2838
return loadfn(test_dir / "stable_entries_Mg_Ga_N.json")

tests/test_ccd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def hd0(v_ga):
2424
)
2525
assert hd0.spin_index == 1
2626
assert pytest.approx(hd0.distortions[1]) == 0.0
27-
assert pytest.approx(hd0.omega_eV) == 0.03268045792725
27+
assert pytest.approx(hd0.omega_eV, rel=1e-4) == 0.03268045792725
2828
assert hd0.defect_band == [(138, 0, 1), (138, 1, 1)]
2929
assert hd0._get_ediff(output_order="bks").shape == (216, 2, 2)
3030
assert hd0._get_ediff(output_order="skb").shape == (2, 2, 216)

tests/test_generators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def test_antisite_generator(gan_struct) -> None:
5555
assert sorted(def_names) == ["Ga_N", "N_Ga"]
5656

5757

58+
def test_mixed_valence_antisite_generator(mixed_valence_struct) -> None:
59+
anti_gen = AntiSiteGenerator().get_defects(mixed_valence_struct)
60+
def_names = [defect.name for defect in anti_gen]
61+
assert "Cu_Cu" not in def_names
62+
assert set(def_names) == {"Cu_O", "O_Cu"}
63+
64+
5865
def test_interstitial_generator(gan_struct) -> None:
5966
gen = InterstitialGenerator().get_defects(
6067
gan_struct, insertions={"Mg": [[0, 0, 0]]}

0 commit comments

Comments
 (0)