From 16c0dac3fbda92f691ec2acaf23a873e85cf1166 Mon Sep 17 00:00:00 2001 From: Ryan Kingsbury Date: Mon, 9 Oct 2023 10:41:58 -0400 Subject: [PATCH] Ion: default hydrates=False in reduced_formula (#3350) * Ion: default hydrates=False in reduced_formula --------- Co-authored-by: Janosh Riebesell --- pymatgen/core/ion.py | 8 ++++---- tests/core/test_ion.py | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pymatgen/core/ion.py b/pymatgen/core/ion.py index 8fad6297f04..d7966552971 100644 --- a/pymatgen/core/ion.py +++ b/pymatgen/core/ion.py @@ -92,7 +92,7 @@ def anonymized_formula(self) -> str: chg_str = charge_string(self._charge, brackets=False) return anon_formula + chg_str - def get_reduced_formula_and_factor(self, iupac_ordering: bool = False, hydrates: bool = True) -> tuple[str, float]: + def get_reduced_formula_and_factor(self, iupac_ordering: bool = False, hydrates: bool = False) -> tuple[str, float]: """Calculates a reduced formula and factor. Similar to Composition.get_reduced_formula_and_factor except that O-H formulas @@ -183,10 +183,10 @@ def reduced_formula(self) -> str: charge is placed in brackets with the sign preceding the magnitude, e.g., 'Ca[+2]'. Uncharged species have "(aq)" appended, e.g. "O2(aq)". """ - reduced_formula = super().reduced_formula - charge = self._charge / self.get_reduced_composition_and_factor()[1] + formula, factor = self.get_reduced_formula_and_factor() + charge = self._charge / factor chg_str = charge_string(charge) - return reduced_formula + chg_str + return formula + chg_str @property def alphabetical_formula(self) -> str: diff --git a/tests/core/test_ion.py b/tests/core/test_ion.py index 1ca426be066..6a33675344e 100644 --- a/tests/core/test_ion.py +++ b/tests/core/test_ion.py @@ -70,15 +70,14 @@ def test_special_formulas(self): ("C2H6O", "C2H5OH(aq)"), ("C3H8O", "C3H7OH(aq)"), ("C4H10O", "C4H9OH(aq)"), - ("Fe(OH)4+", "FeO2.2H2O[+1]"), - ("Zr(OH)4", "ZrO2.2H2O(aq)"), + ("Fe(OH)4+", "Fe(OH)4[+1]"), + ("Zr(OH)4", "Zr(OH)4(aq)"), ] for tup in special_formulas: assert Ion.from_formula(tup[0]).reduced_formula == tup[1] - assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=False) == ("Fe(OH)4", 1) - assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=False) == ("Zr(OH)4", 1) + assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=True) == ("FeO2.2H2O", 1) assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=True) == ("ZrO2.2H2O", 1) assert Ion.from_formula("O").get_reduced_formula_and_factor(hydrates=False) == ("O", 1) assert Ion.from_formula("O2").get_reduced_formula_and_factor(hydrates=False) == ("O2", 1)