Skip to content

Commit

Permalink
add test for composition error
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Jan 25, 2025
1 parent 5b00031 commit 39a761d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/pymatgen/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ def reduce_formula(
sym_amt: Mapping[str, float],
iupac_ordering: bool = False,
) -> tuple[str, int]:
"""Helper function to reduce `sym_amt` to a reduced formula and factor.
"""Helper function to reduce a symbol-amount mapping.
Args:
sym_amt (dict[str, float]): Symbol to amount mapping.
Expand All @@ -1352,10 +1352,10 @@ def reduce_formula(
if all(int(i) == i for i in sym_amt.values()):
factor = abs(gcd(*(int(i) for i in sym_amt.values())))

poly_anions: list[str] = []
# If the composition contains polyanion
poly_anions: list[str] = []
if len(syms) >= 3 and get_el_sp(syms[-1]).X - get_el_sp(syms[-2]).X < 1.65:
poly_sym_amt = {syms[i]: sym_amt[syms[i]] / factor for i in [-2, -1]}
poly_sym_amt: dict[str, float] = {syms[i]: sym_amt[syms[i]] / factor for i in (-2, -1)}
poly_form, poly_factor = reduce_formula(poly_sym_amt, iupac_ordering=iupac_ordering)

if poly_factor != 1:
Expand Down
8 changes: 7 additions & 1 deletion tests/core/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pytest import approx

from pymatgen.core import Composition, DummySpecies, Element, Species
from pymatgen.core.composition import ChemicalPotential
from pymatgen.core.composition import ChemicalPotential, CompositionError
from pymatgen.util.testing import PymatgenTest


Expand Down Expand Up @@ -860,6 +860,12 @@ def test_isotopes(self):
assert "Deuterium" in [elem.long_name for elem in composition.elements]


def test_composition_error():
error = CompositionError("Composition error")
assert isinstance(error, CompositionError)
assert str(error) == "Composition error"


class TestChemicalPotential:
def test_init(self):
dct = {"Fe": 1, Element("Fe"): 1}
Expand Down

0 comments on commit 39a761d

Please sign in to comment.