Skip to content

Commit d027466

Browse files
authored
Ion: handle dissolved gas formulas (#3275)
* Ion: handle dissolved gas formulas * Ion: update docstrings of formula/reduced_formula
1 parent c8d41e6 commit d027466

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pymatgen/core/ion.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ def from_formula(cls, formula: str) -> Ion:
7575

7676
@property
7777
def formula(self) -> str:
78-
"""Returns a formula string, with elements sorted by electronegativity,
79-
e.g., Li4 Fe4 P4 O16.
78+
"""Returns a formula string with appended charge. The
79+
charge is written with the sign preceding the magnitude, e.g.,
80+
'Ca1 +2'. Uncharged species have "(aq)" appended, e.g. "O2 (aq)".
8081
"""
8182
formula = super().formula
8283
return f"{formula} {charge_string(self.charge, brackets=False)}"
@@ -166,14 +167,20 @@ def get_reduced_formula_and_factor(self, iupac_ordering: bool = False, hydrates:
166167
# butanol
167168
elif formula == "H10C4O":
168169
formula = "C4H9OH"
170+
elif formula == "O" and factor % 3 == 0:
171+
formula = "O3"
172+
factor /= 3
173+
elif formula in ["O", "N", "F", "Cl", "H"] and factor % 2 == 0:
174+
formula += "2"
175+
factor /= 2
169176

170177
return formula, factor
171178

172179
@property
173180
def reduced_formula(self) -> str:
174181
"""Returns a reduced formula string with appended charge. The
175182
charge is placed in brackets with the sign preceding the magnitude, e.g.,
176-
'Ca[+2]'.
183+
'Ca[+2]'. Uncharged species have "(aq)" appended, e.g. "O2(aq)".
177184
"""
178185
reduced_formula = super().reduced_formula
179186
charge = self._charge / self.get_reduced_composition_and_factor()[1]

tests/core/test_ion.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def test_special_formulas(self):
5656
("Cl-", "Cl[-1]"),
5757
("H+", "H[+1]"),
5858
("F-", "F[-1]"),
59+
("F2", "F2(aq)"),
60+
("H2", "H2(aq)"),
61+
("O3", "O3(aq)"),
62+
("O2", "O2(aq)"),
63+
("N2", "N2(aq)"),
5964
("H4O4", "H2O2(aq)"),
6065
("OH-", "OH[-1]"),
6166
("CH3COO-", "CH3COO[-1]"),
@@ -74,6 +79,12 @@ def test_special_formulas(self):
7479

7580
assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=False) == ("Fe(OH)4", 1)
7681
assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=False) == ("Zr(OH)4", 1)
82+
assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=True) == ("ZrO2.2H2O", 1)
83+
assert Ion.from_formula("O").get_reduced_formula_and_factor(hydrates=False) == ("O", 1)
84+
assert Ion.from_formula("O2").get_reduced_formula_and_factor(hydrates=False) == ("O2", 1)
85+
assert Ion.from_formula("O3").get_reduced_formula_and_factor(hydrates=False) == ("O3", 1)
86+
assert Ion.from_formula("O6").get_reduced_formula_and_factor(hydrates=False) == ("O3", 2)
87+
assert Ion.from_formula("N8").get_reduced_formula_and_factor(hydrates=False) == ("N2", 4)
7788

7889
def test_formula(self):
7990
correct_formulas = [

0 commit comments

Comments
 (0)