Skip to content

Commit

Permalink
fix Ion parsing bug introduced by commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury committed Jul 20, 2024
1 parent 27d90b7 commit 009f645
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def get_reduced_formula_and_factor(
el_amt_dict = {k: int(round(v)) for k, v in comp.get_el_amt_dict().items()}
formula, factor = reduce_formula(el_amt_dict, iupac_ordering=iupac_ordering)

if (self.composition.get("H") and self.composition.get("O")) is not None:
# This line checks specifically that the contains an equal amount of O and H. When that is the case, they should be displayed as "OH" rather than "HO".
if (self.composition.get("H") == self.composition.get("O")):
formula = formula.replace("HO", "OH")

if nH2O > 0:
Expand All @@ -187,6 +188,13 @@ def get_reduced_formula_and_factor(
elif formula == "H2CO":
formula = "CH3COOH"
factor /= 2
# phosphoric acid system
elif formula == "PH3O4(aq)":
formula = "H3PO4(aq)"
elif formula == "PHO4[-2]":
formula = "HPO4[-2]"
elif formula == "P(HO2)2[-1]":
formula = "H2PO4[-1]"
# acetate
elif formula == "H3(CO)2":
formula = "CH3COO"
Expand Down
1 change: 1 addition & 0 deletions tests/core/test_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_special_formulas(self):
("N2", "N2(aq)"),
("H4O4", "H2O2(aq)"),
("OH-", "OH[-1]"),
("H2PO4-", "H2PO4[-1]"),
("CH3COO-", "CH3COO[-1]"),
("CH3COOH", "CH3COOH(aq)"),
("CH3OH", "CH3OH(aq)"),
Expand Down

0 comments on commit 009f645

Please sign in to comment.