Skip to content

Commit

Permalink
Ion: special handling of CH4, NH4, N3-, SCN-, formate, oxalate
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury committed Jul 20, 2024
1 parent 009f645 commit 34ff05a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
32 changes: 30 additions & 2 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 @@ -173,8 +174,9 @@ 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)

# 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")):
# 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 Down Expand Up @@ -213,6 +215,32 @@ def get_reduced_formula_and_factor(
elif formula == "O" and factor % 3 == 0:
formula = "O3"
factor /= 3
# ammonia
elif formula == "H4N[+1]":
formula = "NH4[+1]"
elif formula == "H3N(aq)":
formula = "NH3(aq)"
# methane
elif formula == "H4C(aq)":
formula = "CH4(aq)"
# thiocyanate
elif formula == "CSN[-1]":
formula = "SCN[-1]"
# 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 = "P[-1]"
# formate # codespell:ignore
elif formula == "HCOO[-1]":
formula = "HCO2[-1]"
# oxalate
elif formula == "CO2[-1]":
formula = "C2O4[-2]"

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

import pytest
from pymatgen.core import Composition, Element

from pymatgen.core.composition import Composition
from pymatgen.core.element import Element
from pymatgen.core.ion import Ion


Expand Down Expand Up @@ -66,6 +68,11 @@ def test_special_formulas(self):
("CH3COOH", "CH3COOH(aq)"),
("CH3OH", "CH3OH(aq)"),
("H4CO", "CH3OH(aq)"),
("CH4", "CH4(aq)"),
("NH4", "NH4[+1]"),
("NH3", "NH3(aq)"),
("N3-", "N3[-1]"),
("HCOO-", "HCOO[-1]"),
("C2H6O", "C2H5OH(aq)"),
("C3H8O", "C3H7OH(aq)"),
("C4H10O", "C4H9OH(aq)"),
Expand Down

0 comments on commit 34ff05a

Please sign in to comment.