From 34ff05aca8f59309c86d6339fe910e47d8849382 Mon Sep 17 00:00:00 2001 From: Ryan Kingsbury Date: Sat, 20 Jul 2024 09:33:31 -0400 Subject: [PATCH] Ion: special handling of CH4, NH4, N3-, SCN-, formate, oxalate --- src/pymatgen/core/ion.py | 32 ++++++++++++++++++++++++++++++-- tests/core/test_ion.py | 9 ++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/pymatgen/core/ion.py b/src/pymatgen/core/ion.py index 7ba9ceabaff..439617fdfcb 100644 --- a/src/pymatgen/core/ion.py +++ b/src/pymatgen/core/ion.py @@ -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 @@ -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: @@ -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 diff --git a/tests/core/test_ion.py b/tests/core/test_ion.py index 52aaa199b05..3b59a461664 100644 --- a/tests/core/test_ion.py +++ b/tests/core/test_ion.py @@ -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 @@ -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)"),