Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list offending elements in BVAnalyzer.get_valences error message #3225

Merged
merged 3 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pymatgen/analysis/bond_valence.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ def get_valences(self, structure: Structure):
Raises:
A ValueError if the valences cannot be determined.
"""
els = [Element(el.symbol) for el in structure.composition.elements]
els = [Element(el.symbol) for el in structure.elements]

if not set(els).issubset(set(BV_PARAMS)):
raise ValueError("Structure contains elements not in set of BV parameters!")
if diff := set(els) - set(BV_PARAMS):
raise ValueError(f"Structure contains elements not in set of BV parameters: {diff}")

# Perform symmetry determination and get sites grouped by symmetry.
if self.symm_tol:
Expand Down
13 changes: 9 additions & 4 deletions tests/analysis/test_bond_valence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os

import pytest
from pytest import approx

from pymatgen.analysis.bond_valence import BVAnalyzer, calculate_bv_sum, calculate_bv_sum_unordered
Expand All @@ -15,7 +16,7 @@ class TestBVAnalyzer(PymatgenTest):
def setUp(self):
self.analyzer = BVAnalyzer()

def test_get_valence(self):
def test_get_valences(self):
struct = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "LiMn2O4.json"))
ans = [1, 1, 3, 3, 4, 4, -2, -2, -2, -2, -2, -2, -2, -2]
assert self.analyzer.get_valences(struct) == ans
Expand All @@ -31,11 +32,15 @@ def test_get_valence(self):
struct = self.get_structure("NaFePO4")
assert self.analyzer.get_valences(struct) == ans

# trigger ValueError Structure contains elements not in set of BV parameters
with pytest.raises(ValueError, match="Structure contains elements not in set of BV parameters: {Element Xe}"):
self.analyzer.get_valences(self.get_structure("Li10GeP2S12").replace_species({"Li": "Xe"}, in_place=False))

def test_get_oxi_state_structure(self):
struct = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "LiMn2O4.json"))
news = self.analyzer.get_oxi_state_decorated_structure(struct)
assert Species("Mn", 3) in news.composition.elements
assert Species("Mn", 4) in news.composition.elements
oxi_struct = self.analyzer.get_oxi_state_decorated_structure(struct)
assert Species("Mn", 3) in oxi_struct.composition.elements
assert Species("Mn", 4) in oxi_struct.composition.elements


class TestBondValenceSum(PymatgenTest):
Expand Down
2 changes: 1 addition & 1 deletion tests/files/.pytest-split-durations
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
"tests/analysis/test_bond_dissociation.py::TestBondDissociation::test_pc_neutral_pcm_65": 0.0004544589319266379,
"tests/analysis/test_bond_dissociation.py::TestBondDissociation::test_tfsi_neg_no_pcm": 0.0004278330015949905,
"tests/analysis/test_bond_valence.py::TestBVAnalyzer::test_get_oxi_state_structure": 0.005586749990470707,
"tests/analysis/test_bond_valence.py::TestBVAnalyzer::test_get_valence": 0.05632162501569837,
"tests/analysis/test_bond_valence.py::TestBVAnalyzer::test_get_valences": 0.05632162501569837,
"tests/analysis/test_bond_valence.py::TestBondValenceSum::test_calculate_bv_sum": 0.0013919160119257867,
"tests/analysis/test_bond_valence.py::TestBondValenceSum::test_calculate_bv_sum_unordered": 0.0008953339420258999,
"tests/analysis/test_chempot_diagram.py::TestChemicalPotentialDiagram::test_border_hyperplanes": 0.07804387499345466,
Expand Down