diff --git a/src/pyEQL/utils.py b/src/pyEQL/utils.py index 2d4dee88..6cff2cf2 100644 --- a/src/pyEQL/utils.py +++ b/src/pyEQL/utils.py @@ -7,6 +7,7 @@ """ import logging +import re from collections import UserDict from functools import lru_cache from typing import Any @@ -76,6 +77,12 @@ def standardize_formula(formula: str): for char in [r"‑", r"‐", r"‒", r"–", r"—", r"−"]: # noqa: RUF001 formula = formula.replace(char, "-") + # Do not modify any dimers etc (Phreeqc reports a small amount of + # "(CO2)2" in a water solution with C(4), for example. + _POLYMER_RE = re.compile(r"^\([A-Za-z0-9+-]+\)\d+$") + if _POLYMER_RE.match(formula): + return formula + sform = Ion.from_formula(formula).reduced_formula # TODO - manual formula adjustments. May be implemented upstream in pymatgen in the future diff --git a/tests/test_utils.py b/tests/test_utils.py index 9a2800e7..8d9ea121 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -62,6 +62,10 @@ def test_standardize_formula(): assert standardize_formula("(NH4)2SO4") == "(NH4)2SO4(aq)" assert standardize_formula("NH4SO4-") == "NH4SO4[-1]" + # Polymers should be left intact + assert standardize_formula("(CO2)2") == "(CO2)2" + assert standardize_formula("(H2O)3") == "(H2O)3" + def test_formula_dict(): """