Skip to content

Commit

Permalink
Ion: bugfix for CO2(aq) reduced formula (#4099)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury authored Oct 10, 2024
1 parent 7e42635 commit 668f1aa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def get_reduced_formula_and_factor(
elif formula == "HCOO":
formula = "HCO2"
# oxalate
elif formula == "CO2":
elif formula == "CO2" and self.charge == -2:
formula = "C2O4"
factor /= 2
# diatomic gases
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 @@ -39,6 +39,8 @@ def test_charge_from_formula(self):
assert Ion.from_formula("Ca++").charge == 2
assert Ion.from_formula("Ca[++]").charge == 2
assert Ion.from_formula("Ca2+").charge == 1
assert Ion.from_formula("C2O4-2").charge == -2
assert Ion.from_formula("CO2").charge == 0

assert Ion.from_formula("Cl-").charge == -1
assert Ion.from_formula("Cl[-]").charge == -1
Expand Down Expand Up @@ -70,7 +72,9 @@ def test_special_formulas(self):
("CH3COOH", "CH3COOH(aq)"),
("CH3OH", "CH3OH(aq)"),
("H4CO", "CH3OH(aq)"),
("CO2-", "C2O4[-2]"),
("C2O4--", "C2O4[-2]"),
("CO2", "CO2(aq)"),
("CO3--", "CO3[-2]"),
("CH4", "CH4(aq)"),
("NH4+", "NH4[+1]"),
("NH3", "NH3(aq)"),
Expand Down

0 comments on commit 668f1aa

Please sign in to comment.