Skip to content

Commit

Permalink
fixes for new formula detection
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury committed Jul 20, 2024
1 parent 17135b7 commit d21005f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
49 changes: 23 additions & 26 deletions src/pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import TYPE_CHECKING

from monty.json import MSONable

from pymatgen.core.composition import Composition, reduce_formula
from pymatgen.util.string import Stringify, charge_string, formula_double_format

Expand Down Expand Up @@ -177,7 +178,6 @@ def get_reduced_formula_and_factor(
# 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:
formula += f".{nH2O}H2O"

Expand All @@ -190,12 +190,12 @@ def get_reduced_formula_and_factor(
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]"
elif formula == "PH3O4":
formula = "H3PO4"
elif formula == "PHO4":
formula = "HPO4"
elif formula == "P(HO2)2":
formula = "H2PO4"
# acetate
elif formula == "H3(CO)2":
formula = "CH3COO"
Expand All @@ -215,30 +215,27 @@ def get_reduced_formula_and_factor(
formula = "O3"
factor /= 3
# ammonia
elif formula == "H4N[+1]":
formula = "NH4[+1]"
elif formula == "H3N(aq)":
formula = "NH3(aq)"
elif formula == "H4N":
formula = "NH4"
elif formula == "H3N":
formula = "NH3"
# methane
elif formula == "H4C(aq)":
formula = "CH4(aq)"
elif formula == "H4C":
formula = "CH4"
# thiocyanate
elif formula == "CSN[-1]":
formula = "SCN[-1]"
elif formula == "CSN":
formula = "SCN"
# triiodide, nitride, an phosphide
elif formula == "I[-0.33333333]":
formula = "I3[-1]"
elif formula == "N[-0.33333333]":
formula = "N3[-1]"
elif formula == "P[-0.33333333]":
formula = "P3[-1]"
elif formula in ["I", "N", "P"] and self.charge == -1:
formula += "3"
factor /= 3
# formate # codespell:ignore
elif formula == "HCOO[-1]":
formula = "HCO2[-1]"
elif formula == "HCOO":
formula = "HCO2"
# oxalate
elif formula == "CO2[-1]":
formula = "C2O4[-2]"

elif formula == "CO2":
formula = "C2O4"
factor *= 2
# diatomic gases
elif formula in {"O", "N", "F", "Cl", "H"} and factor % 2 == 0:
formula += "2"
Expand Down
7 changes: 4 additions & 3 deletions tests/core/test_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import TestCase

import pytest

from pymatgen.core import Composition, Element
from pymatgen.core.ion import Ion

Expand Down Expand Up @@ -46,6 +47,7 @@ def test_charge_from_formula(self):
assert Ion.from_formula("SO42-").charge == -1
assert Ion.from_formula("SO4--").charge == -2
assert Ion.from_formula("SO4[--]").charge == -2
assert Ion.from_formula("N3-").charge == -1

assert Ion.from_formula("Na[+-+]").charge == 1

Expand All @@ -68,17 +70,16 @@ def test_special_formulas(self):
("CH3OH", "CH3OH(aq)"),
("H4CO", "CH3OH(aq)"),
("CH4", "CH4(aq)"),
("NH4", "NH4[+1]"),
("NH4+", "NH4[+1]"),
("NH3", "NH3(aq)"),
("N3-", "N3[-1]"),
("HCOO-", "HCOO[-1]"),
("HCOO-", "HCO2[-1]"),
("C2H6O", "C2H5OH(aq)"),
("C3H8O", "C3H7OH(aq)"),
("C4H10O", "C4H9OH(aq)"),
("Fe(OH)4+", "Fe(OH)4[+1]"),
("Zr(OH)4", "Zr(OH)4(aq)"),
]

for tup in special_formulas:
assert Ion.from_formula(tup[0]).reduced_formula == tup[1]

Expand Down

0 comments on commit d21005f

Please sign in to comment.