Skip to content

Commit

Permalink
Merge pull request #106 from KingsburyLab/bugfix
Browse files Browse the repository at this point in the history
Solute.from_formula: fix oxi_states bug (#103)
  • Loading branch information
rkingsbury authored Feb 25, 2024
2 parents 6fea057 + db5c4c7 commit 8e7279a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
## [0.12.1] - 2024-02-25

### Fixed

- `Solute.from_formula`: Fix bug in which an uncaught exception could occur when
if `pymatgen` failed to guess the oxidation state of a solute. (Issue #103 - thanks to @xiaoxiaozhu123 for reporting).
- `Solution.get_total_amount`: Bugfix that would cause the method to fail if
mass-based units such as mg/L or ppm were requested.

Expand Down
11 changes: 4 additions & 7 deletions src/pyEQL/solute.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,10 @@ def from_formula(cls, formula: str):
mw = f"{float(pmg_ion.weight / factor)} g/mol" # weight is a FloatWithUnit
chemsys = pmg_ion.chemical_system
# store only the most likely oxi_state guesses
oxi_states = pmg_ion.oxi_state_guesses(all_oxi_states=True)
# TODO - hack to work around a pymatgen bug in Composition
# https://github.com/materialsproject/pymatgen/issues/3324
if oxi_states == []:
oxi_states = {els[0]: 0.0} if rform in ["O2(aq)", "O3(aq)", "Cl2(aq)", "F2(aq)"] else {}
else:
oxi_states = oxi_states[0]
try:
oxi_states = pmg_ion.oxi_state_guesses(all_oxi_states=True)[0]
except (IndexError, ValueError):
oxi_states = {}

return cls(
rform,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_solute.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def test_from_formula():
assert s.n_atoms == 1
assert s.n_elements == 1
assert s.oxi_state_guesses == {"Mg": 2.0}
# test behavior when oxi_state_guesses fails
assert Solute.from_formula("Br[-0.33333333]").oxi_state_guesses == {}
assert s.molecular_weight == "24.305 g/mol"
s2 = Solute.from_formula("O6")
assert s2.formula == "O3(aq)"
Expand Down

0 comments on commit 8e7279a

Please sign in to comment.