From 51c412082eae8bce864e552ad279c84d426db816 Mon Sep 17 00:00:00 2001 From: Robert Timms <43040151+rtimms@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:27:01 +0100 Subject: [PATCH] #4018 check indepedent vars in events (#4019) * #4018 check indepedent vars in events * #4018 changelog --- CHANGELOG.md | 1 + pybamm/discretisations/discretisation.py | 1 + .../test_discretisations/test_discretisation.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8322f2a654..ea458b4b60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ## Bug Fixes +- Fixed a bug where independent variables were removed from models even if they appeared in events ([#4019](https://github.com/pybamm-team/PyBaMM/pull/4019)) - Fix bug with upwind and downwind schemes producing the wrong discretised system ([#3979](https://github.com/pybamm-team/PyBaMM/pull/3979)) - Allow evaluation of an `Interpolant` object with a number ([#3932](https://github.com/pybamm-team/PyBaMM/pull/3932)) - `plot_voltage_components` now works even if the time does not start at 0 ([#3915](https://github.com/pybamm-team/PyBaMM/pull/3915)) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 68c2e9f19a..92e9e4e24f 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -1103,6 +1103,7 @@ def remove_independent_variables_from_rhs(self, model): # only check children of variables, this will skip the variable itself # and catch any other cases + [child for var in model.variables.values() for child in var.children] + + [event.expression for event in model.events] ) all_vars_in_eqns = unpacker.unpack_list_of_symbols(eqns_to_check) all_vars_in_eqns = [var.name for var in all_vars_in_eqns] diff --git a/tests/unit/test_discretisations/test_discretisation.py b/tests/unit/test_discretisations/test_discretisation.py index 48795c9318..e9da61e001 100644 --- a/tests/unit/test_discretisations/test_discretisation.py +++ b/tests/unit/test_discretisations/test_discretisation.py @@ -1241,6 +1241,22 @@ def test_independent_rhs(self): disc.process_model(model) self.assertEqual(len(model.rhs), 2) + def test_independent_rhs_with_event(self): + a = pybamm.Variable("a") + b = pybamm.Variable("b") + c = pybamm.Variable("c") + model = pybamm.BaseModel() + model.rhs = {a: b, b: c, c: -c} + model.initial_conditions = { + a: pybamm.Scalar(0), + b: pybamm.Scalar(1), + c: pybamm.Scalar(1), + } + model.events = [pybamm.Event("a=1", a - 1)] + disc = pybamm.Discretisation() + disc.process_model(model) + self.assertEqual(len(model.rhs), 3) + if __name__ == "__main__": print("Add -v for more debug output")