Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ion: fix CO2- and I3- parsing errors; enhance tests #3991

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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