Skip to content

Commit

Permalink
Ion: fix CO2- and I3- parsing errors; enhance tests (#3991)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury authored Aug 11, 2024
1 parent 1bfd330 commit b28c937
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion tests/core/test_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"),
Expand All @@ -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)"),
Expand All @@ -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)
Expand Down

0 comments on commit b28c937

Please sign in to comment.