From 27bd9940c46834da70df9127e75476dc9bf00471 Mon Sep 17 00:00:00 2001 From: Sean Kavanagh Date: Mon, 18 Nov 2024 11:03:35 -0500 Subject: [PATCH 1/2] Use sets to handle mixed valence species and add test --- pymatgen/analysis/defects/generators.py | 8 ++++---- tests/conftest.py | 9 +++++++++ tests/test_generators.py | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pymatgen/analysis/defects/generators.py b/pymatgen/analysis/defects/generators.py index 08bb8f17..c2f5c316 100644 --- a/pymatgen/analysis/defects/generators.py +++ b/pymatgen/analysis/defects/generators.py @@ -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, @@ -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) diff --git a/tests/conftest.py b/tests/conftest.py index 743a599b..bffe53c8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ from collections import defaultdict from pathlib import Path +import numpy as np import pytest from monty.serialization import loadfn @@ -22,6 +23,14 @@ def test_dir(): 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): diff --git a/tests/test_generators.py b/tests/test_generators.py index 565dc02a..abf439b9 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -54,6 +54,12 @@ def test_antisite_generator(gan_struct) -> None: def_names = [defect.name for defect in anti_gen] 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( From 1bbb981abf61db35ec453e816f69f2634f2703a6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:06:39 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/conftest.py | 1 + tests/test_generators.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index bffe53c8..b709ed9d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,6 +23,7 @@ def test_dir(): 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( diff --git a/tests/test_generators.py b/tests/test_generators.py index abf439b9..702cafd0 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -54,6 +54,7 @@ def test_antisite_generator(gan_struct) -> None: def_names = [defect.name for defect in anti_gen] 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]