|
9 | 9 | import logging
|
10 | 10 | from collections import UserDict
|
11 | 11 | from functools import lru_cache
|
| 12 | +from typing import Any |
12 | 13 |
|
13 | 14 | from iapws import IAPWS95, IAPWS97
|
14 | 15 | from pymatgen.core.ion import Ion
|
@@ -59,7 +60,39 @@ def standardize_formula(formula: str):
|
59 | 60 | be enclosed in square brackets to remove any ambiguity in the meaning of the formula. For example, 'Na+',
|
60 | 61 | 'Na+1', and 'Na[+]' will all standardize to "Na[+1]"
|
61 | 62 | """
|
62 |
| - return Ion.from_formula(formula).reduced_formula |
| 63 | + sform = Ion.from_formula(formula).reduced_formula |
| 64 | + |
| 65 | + # TODO - manual formula adjustments. May be implemented upstream in pymatgen in the future |
| 66 | + # thanks to @xiaoxiaozhu123 for pointing out these issues in |
| 67 | + # https://github.com/KingsburyLab/pyEQL/issues/136 |
| 68 | + |
| 69 | + # ammonia |
| 70 | + if sform == "H4N[+1]": |
| 71 | + sform = "NH4[+1]" |
| 72 | + elif sform == "H3N(aq)": |
| 73 | + sform = "NH3(aq)" |
| 74 | + # phosphoric acid system |
| 75 | + elif sform == "PH3O4(aq)": |
| 76 | + sform = "H3PO4(aq)" |
| 77 | + elif sform == "PHO4[-2]": |
| 78 | + sform = "HPO4[-2]" |
| 79 | + elif sform == "P(HO2)2[-1]": |
| 80 | + sform = "H2PO4[-1]" |
| 81 | + # thiocyanate |
| 82 | + elif sform == "CSN[-1]": |
| 83 | + sform = "SCN[-1]" |
| 84 | + # triiodide |
| 85 | + elif sform == "I[-0.33333333]": |
| 86 | + sform = "I3[-1]" |
| 87 | + # formate |
| 88 | + elif sform == "HCOO[-1]": |
| 89 | + sform = "HCO2[-1]" |
| 90 | + # oxalate |
| 91 | + elif sform == "CO2[-1]": |
| 92 | + sform = "C2O4[-2]" |
| 93 | + |
| 94 | + # TODO - consider adding recognition of special formulas like MeOH for methanol or Cit for citrate |
| 95 | + return sform |
63 | 96 |
|
64 | 97 |
|
65 | 98 | def format_solutes_dict(solute_dict: dict, units: str):
|
@@ -115,18 +148,18 @@ class FormulaDict(UserDict):
|
115 | 148 | formula notation (e.g., "Na+", "Na+1", "Na[+]" all have the same effect)
|
116 | 149 | """
|
117 | 150 |
|
118 |
| - def __getitem__(self, key): |
| 151 | + def __getitem__(self, key) -> Any: |
119 | 152 | return super().__getitem__(standardize_formula(key))
|
120 | 153 |
|
121 |
| - def __setitem__(self, key, value): |
| 154 | + def __setitem__(self, key, value) -> None: |
122 | 155 | super().__setitem__(standardize_formula(key), value)
|
123 | 156 | # sort contents anytime an item is set
|
124 | 157 | self.data = dict(sorted(self.items(), key=lambda x: x[1], reverse=True))
|
125 | 158 |
|
126 | 159 | # Necessary to define this so that .get() works properly in python 3.12+
|
127 | 160 | # see https://github.com/python/cpython/issues/105524
|
128 |
| - def __contains__(self, key): |
| 161 | + def __contains__(self, key) -> bool: |
129 | 162 | return standardize_formula(key) in self.data
|
130 | 163 |
|
131 |
| - def __delitem__(self, key): |
| 164 | + def __delitem__(self, key) -> None: |
132 | 165 | super().__delitem__(standardize_formula(key))
|
0 commit comments