diff --git a/src/pymatgen/core/ion.py b/src/pymatgen/core/ion.py index fb2dd3a527a..b6ab85d4e03 100644 --- a/src/pymatgen/core/ion.py +++ b/src/pymatgen/core/ion.py @@ -226,7 +226,7 @@ def get_reduced_formula_and_factor( elif formula == "CSN": formula = "SCN" # triiodide, nitride, an phosphide - elif formula in ["I", "N", "P"] and self.charge == -1: + elif (formula in ["N", "P"] and self.charge == -1) or (formula == "I" and self.charge == 1 / 3): formula += "3" factor /= 3 # formate # codespell:ignore @@ -235,7 +235,7 @@ def get_reduced_formula_and_factor( # oxalate elif formula == "CO2": formula = "C2O4" - factor *= 2 + factor /= 2 # diatomic gases elif formula in {"O", "N", "F", "Cl", "H"} and factor % 2 == 0: formula += "2" diff --git a/tests/core/test_ion.py b/tests/core/test_ion.py index 77f55e2a20b..5f513ad72ea 100644 --- a/tests/core/test_ion.py +++ b/tests/core/test_ion.py @@ -56,6 +56,7 @@ def test_special_formulas(self): ("Cl-", "Cl[-1]"), ("H+", "H[+1]"), ("F-", "F[-1]"), + ("I-", "I[-1]"), ("F2", "F2(aq)"), ("H2", "H2(aq)"), ("O3", "O3(aq)"), @@ -69,6 +70,7 @@ def test_special_formulas(self): ("CH3COOH", "CH3COOH(aq)"), ("CH3OH", "CH3OH(aq)"), ("H4CO", "CH3OH(aq)"), + ("CO2-", "C2O4[-2]"), ("CH4", "CH4(aq)"), ("NH4+", "NH4[+1]"), ("NH3", "NH3(aq)"), @@ -81,7 +83,9 @@ def test_special_formulas(self): ("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(tup[0]).reduced_formula == tup[1] + ), f"Expected {tup[1]} but got {Ion.from_formula(tup[0]).reduced_formula}" 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)