Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/pyEQL/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import logging
import re
from collections import UserDict
from functools import lru_cache
from typing import Any
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down
Loading