Skip to content

Commit

Permalink
Fix antisite generation in mixed-valence systems (#210)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kavanase and pre-commit-ci[bot] authored Jan 18, 2025
1 parent c2aed9c commit 722b987
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pymatgen/analysis/defects/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def __init__(
def generate(
self,
structure: Structure,
rm_species: list[str | Species] | None = None,
rm_species: set[str | Species] | list[str | Species] | None = None,
**kwargs,
) -> Generator[Vacancy, None, None]:
"""Generate a vacancy defects.
Expand All @@ -103,10 +103,10 @@ def generate(
Returns:
Generator[Vacancy, None, None]: Generator that yields a list of ``Vacancy`` objects.
"""
all_species = [*map(_element_str, structure.composition.elements)]
rm_species = all_species if rm_species is None else [*map(str, rm_species)]
all_species = {*map(_element_str, structure.composition.elements)}
rm_species = all_species if rm_species is None else {*map(str, rm_species)}

if not set(rm_species).issubset(all_species):
if not rm_species.issubset(all_species):
msg = f"rm_species({rm_species}) must be a subset of the structure's species ({all_species})."
raise ValueError(
msg,
Expand Down Expand Up @@ -235,7 +235,7 @@ def generate(
structure: The bulk structure the anti-site defects are generated from.
**kwargs: Additional keyword arguments for the ``Substitution.generate`` function.
"""
all_species = [*map(_element_str, structure.composition.elements)]
all_species = {*map(_element_str, structure.composition.elements)}
subs = collections.defaultdict(list)
for u, v in combinations(all_species, 2):
subs[u].append(v)
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import defaultdict
from pathlib import Path
import numpy as np

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


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


@pytest.fixture(scope="session")
def stable_entries_Mg_Ga_N(test_dir):
return loadfn(test_dir / "stable_entries_Mg_Ga_N.json")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ccd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def hd0(v_ga):
)
assert hd0.spin_index == 1
assert pytest.approx(hd0.distortions[1]) == 0.0
assert pytest.approx(hd0.omega_eV) == 0.03268045792725
assert pytest.approx(hd0.omega_eV, rel=1e-4) == 0.03268045792725
assert hd0.defect_band == [(138, 0, 1), (138, 1, 1)]
assert hd0._get_ediff(output_order="bks").shape == (216, 2, 2)
assert hd0._get_ediff(output_order="skb").shape == (2, 2, 216)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def test_antisite_generator(gan_struct) -> None:
assert sorted(def_names) == ["Ga_N", "N_Ga"]


def test_mixed_valence_antisite_generator(mixed_valence_struct) -> None:
anti_gen = AntiSiteGenerator().get_defects(mixed_valence_struct)
def_names = [defect.name for defect in anti_gen]
assert "Cu_Cu" not in def_names
assert set(def_names) == {"Cu_O", "O_Cu"}


def test_interstitial_generator(gan_struct) -> None:
gen = InterstitialGenerator().get_defects(
gan_struct, insertions={"Mg": [[0, 0, 0]]}
Expand Down

0 comments on commit 722b987

Please sign in to comment.