Skip to content

Commit

Permalink
Ion: default hydrates=False in reduced_formula (materialsproject#3350)
Browse files Browse the repository at this point in the history
* Ion: default hydrates=False in reduced_formula

---------

Co-authored-by: Janosh Riebesell <[email protected]>
  • Loading branch information
rkingsbury and janosh authored Oct 9, 2023
1 parent cf3ae23 commit 16c0dac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pymatgen/core/ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def anonymized_formula(self) -> str:
chg_str = charge_string(self._charge, brackets=False)
return anon_formula + chg_str

def get_reduced_formula_and_factor(self, iupac_ordering: bool = False, hydrates: bool = True) -> tuple[str, float]:
def get_reduced_formula_and_factor(self, iupac_ordering: bool = False, hydrates: bool = False) -> tuple[str, float]:
"""Calculates a reduced formula and factor.
Similar to Composition.get_reduced_formula_and_factor except that O-H formulas
Expand Down Expand Up @@ -183,10 +183,10 @@ def reduced_formula(self) -> str:
charge is placed in brackets with the sign preceding the magnitude, e.g.,
'Ca[+2]'. Uncharged species have "(aq)" appended, e.g. "O2(aq)".
"""
reduced_formula = super().reduced_formula
charge = self._charge / self.get_reduced_composition_and_factor()[1]
formula, factor = self.get_reduced_formula_and_factor()
charge = self._charge / factor
chg_str = charge_string(charge)
return reduced_formula + chg_str
return formula + chg_str

@property
def alphabetical_formula(self) -> str:
Expand Down
7 changes: 3 additions & 4 deletions tests/core/test_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ def test_special_formulas(self):
("C2H6O", "C2H5OH(aq)"),
("C3H8O", "C3H7OH(aq)"),
("C4H10O", "C4H9OH(aq)"),
("Fe(OH)4+", "FeO2.2H2O[+1]"),
("Zr(OH)4", "ZrO2.2H2O(aq)"),
("Fe(OH)4+", "Fe(OH)4[+1]"),
("Zr(OH)4", "Zr(OH)4(aq)"),
]

for tup in special_formulas:
assert Ion.from_formula(tup[0]).reduced_formula == tup[1]

assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=False) == ("Fe(OH)4", 1)
assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=False) == ("Zr(OH)4", 1)
assert Ion.from_formula("Fe(OH)4+").get_reduced_formula_and_factor(hydrates=True) == ("FeO2.2H2O", 1)
assert Ion.from_formula("Zr(OH)4").get_reduced_formula_and_factor(hydrates=True) == ("ZrO2.2H2O", 1)
assert Ion.from_formula("O").get_reduced_formula_and_factor(hydrates=False) == ("O", 1)
assert Ion.from_formula("O2").get_reduced_formula_and_factor(hydrates=False) == ("O2", 1)
Expand Down

0 comments on commit 16c0dac

Please sign in to comment.