Repository: pybamm-team/PyBaMM (python)

Issue:
[Bug]: "electrode diffusivity" -> "particle diffusivity" is still breaking change on `develop` when using pre-24.5 pybamm.ParameterValues
### PyBaMM Version

develop

### Python Version

3.9.13

### Describe the bug

Since #4072 re-inverted the logic fix in #3871, the release candidate on `develop` causes the rename of "electrode diffusivity" -> "particle diffusivity" to be a breaking change. Parameter sets using the old key names do not simulate in the 24.5 RC.

### Steps to Reproduce

```python
import pybamm

parameter_values = pybamm.ParameterValues("Chen2020")

# Dummy a pybamm.ParameterValues object with a legacy parameter name
parameter_values.update(
    {
        "Negative electrode diffusivity [m2.s-1]": parameter_values["Negative particle diffusivity [m2.s-1]"],
    },
    check_already_exists=False,
)
del parameter_values["Negative particle diffusivity [m2.s-1]"]

# Try to run a simulation with the old-style parameter set
model = pybamm.lithium_ion.DFN()
experiment = pybamm.Experiment(
    [
        "Discharge at 1C until 3 V",
    ]
)
sim = pybamm.Simulation(
    model,
    parameter_values=parameter_values,
    experiment=experiment,
)

# KeyError provoked here on call into pybamm.ParameterValues with new key name
sol = sim.solve()
```

### Relevant log output

_No response_

Pull request:
Corrects "electrode diffusivity" error catch
# Description

This reverts an incorrect change applied in #4179. This adds to the error catch for the old parameter name `"{domain} electrode diffusivity [m2.s-1]"` to use the new name, likewise if the new name `"{domain} particle diffusivity [m2.s-1]" `is provided the error catch uses the old name.

I've confirmed it fixes the MWE in #4176, and currently works with the old parameter name within [PyBOP](https://github.com/pybop-team/PyBOP/actions/runs/9944318010). A quick MWE to test this fix is below:

```python
import pybamm
# create the model
model = pybamm.lithium_ion.DFN()

# set the default model parameters
param = model.default_parameter_values

# use both parameter names
neg_old = param["Negative electrode diffusivity [m2.s-1]"] = 2.0
pos_new = param["Positive particle diffusivity [m2.s-1]"] = 2.0
assert neg_old == pos_new
```

Fixes #4176

## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CHANGELOG.md) to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Optimization (back-end change that speeds up the code)
- [X] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [ ] No style issues: `$ pre-commit run` (or `$ nox -s pre-commit`) (see [CONTRIBUTING.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) for how to set this up to run automatically when committing locally, in just two lines of code)
- [ ] All tests pass: `$ python run-tests.py --all` (or `$ nox -s tests`)
- [ ] The documentation builds: `$ python run-tests.py --doctest` (or `$ nox -s doctests`)

You can run integration tests, unit tests, and doctests together at once, using `$ python run-tests.py --quick` (or `$ nox -s quick`).

## Further checks:

- [ ] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works


Patch:
diff --git a/pybamm/util.py b/pybamm/util.py
index 0fe02c835..130cb5ba4 100644
--- a/pybamm/util.py
+++ b/pybamm/util.py
@@ -57,14 +57,22 @@ class FuzzyDict(dict):
         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(


Test patch:
diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py
index 21673a44f..abcdb4dca 100644
--- a/tests/unit/test_util.py
+++ b/tests/unit/test_util.py
@@ -34,6 +34,11 @@ class TestUtil:
                 "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,
             }
         )
@@ -56,6 +61,16 @@ class TestUtil:
         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]"]