Skip to content

Commit

Permalink
Corrects "electrode diffusivity" error catch (pybamm-team#4267)
Browse files Browse the repository at this point in the history
* fix: error catch for electrode diffusivity

* refactor: update diffusivity error catch and test

* tests: up coverage for diffusivity name catches

* Update pybamm/util.py

Co-authored-by: Agriya Khetarpal <[email protected]>

---------

Co-authored-by: Eric G. Kratz <[email protected]>
Co-authored-by: Ferran Brosa Planella <[email protected]>
Co-authored-by: Agriya Khetarpal <[email protected]>
  • Loading branch information
4 people authored Jul 23, 2024
1 parent 668e563 commit ce407ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,22 @@ def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError as error:
if "particle diffusivity" in key:
warn(
f"The parameter '{key.replace('particle', 'electrode')}' "
f"has been renamed to '{key}'",
DeprecationWarning,
stacklevel=2,
if "electrode diffusivity" in key or "particle diffusivity" in key:
old_term, new_term = (
("electrode", "particle")
if "electrode diffusivity" in key
else ("particle", "electrode")
)
return super().__getitem__(key.replace("particle", "electrode"))
alternative_key = key.replace(old_term, new_term)

if old_term == "electrode":
warn(
f"The parameter '{alternative_key}' has been renamed to '{key}' and will be removed in a future release. Using '{key}'",
DeprecationWarning,
stacklevel=2,
)

return super().__getitem__(alternative_key)
if key in ["Negative electrode SOC", "Positive electrode SOC"]:
domain = key.split(" ")[0]
raise KeyError(
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def test_fuzzy_dict(self):
"SEI current": 3,
"Lithium plating current": 4,
"A dimensional variable [m]": 5,
"Positive particle diffusivity [m2.s-1]": 6,
}
)
d2 = pybamm.FuzzyDict(
{
"Positive electrode diffusivity [m2.s-1]": 6,
}
)
Expand All @@ -56,6 +61,16 @@ def test_fuzzy_dict(self):
with pytest.raises(KeyError, match="Upper voltage"):
d.__getitem__("Open-circuit voltage at 100% SOC [V]")

assert (
d2["Positive particle diffusivity [m2.s-1]"]
== d["Positive particle diffusivity [m2.s-1]"]
)

assert (
d2["Positive electrode diffusivity [m2.s-1]"]
== d["Positive electrode diffusivity [m2.s-1]"]
)

with pytest.warns(DeprecationWarning):
assert (
d["Positive electrode diffusivity [m2.s-1]"]
Expand Down

0 comments on commit ce407ae

Please sign in to comment.