Skip to content

Commit 27bd994

Browse files
committed
Use sets to handle mixed valence species and add test
1 parent e2cb285 commit 27bd994

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

pymatgen/analysis/defects/generators.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -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

+9
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
@@ -22,6 +23,14 @@ def test_dir():
2223
def gan_struct(test_dir):
2324
return Structure.from_file(test_dir / "GaN.vasp")
2425

26+
@pytest.fixture(scope="session")
27+
def mixed_valence_struct(test_dir):
28+
return Structure(
29+
lattice=np.array([[3.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 3.0]]),
30+
species=["Cu+", "Cu+", "Cu2+", "O2-"],
31+
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]],
32+
)
33+
2534

2635
@pytest.fixture(scope="session")
2736
def stable_entries_Mg_Ga_N(test_dir):

tests/test_generators.py

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def test_antisite_generator(gan_struct) -> None:
5454
def_names = [defect.name for defect in anti_gen]
5555
assert sorted(def_names) == ["Ga_N", "N_Ga"]
5656

57+
def test_mixed_valence_antisite_generator(mixed_valence_struct) -> None:
58+
anti_gen = AntiSiteGenerator().get_defects(mixed_valence_struct)
59+
def_names = [defect.name for defect in anti_gen]
60+
assert "Cu_Cu" not in def_names
61+
assert set(def_names) == {"Cu_O", "O_Cu"}
62+
5763

5864
def test_interstitial_generator(gan_struct) -> None:
5965
gen = InterstitialGenerator().get_defects(

0 commit comments

Comments
 (0)