From d464ad41e098101172bc420089927214db1d89e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Z=C3=BCgner?= Date: Fri, 26 Jul 2024 17:07:06 +0200 Subject: [PATCH] Fix chemical system method for different oxidation states (#3915) * Fix chemical system when the same element appears in multiple oxidation states --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Matthew Horton --- src/pymatgen/core/composition.py | 2 +- tests/core/test_composition.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pymatgen/core/composition.py b/src/pymatgen/core/composition.py index 8eaa6a5400d..9d889d83780 100644 --- a/src/pymatgen/core/composition.py +++ b/src/pymatgen/core/composition.py @@ -487,7 +487,7 @@ def chemical_system(self) -> str: sorted alphabetically and joined by dashes, by convention for use in database keys. """ - return "-".join(sorted(el.symbol for el in self.elements)) + return "-".join(sorted(self.chemical_system_set)) @property def num_atoms(self) -> float: diff --git a/tests/core/test_composition.py b/tests/core/test_composition.py index 1cf2c688e31..7c9b4dcdd33 100644 --- a/tests/core/test_composition.py +++ b/tests/core/test_composition.py @@ -701,6 +701,7 @@ def test_elements(self): def test_chemical_system(self): assert Composition({"Na": 1, "Cl": 1}).chemical_system == "Cl-Na" assert Composition({"Na+": 1, "Cl-": 1}).chemical_system == "Cl-Na" + assert Composition({"Na+": 1, "Na2+": 1, "Cl-": 1}).chemical_system == "Cl-Na" def test_chemical_system_set(self): assert Composition({"Na": 1, "Cl": 1}).chemical_system_set == {"Cl", "Na"}