From 1b7959ea3eb3612f5fa401b79a6e5b0a033428bc Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Tue, 21 Mar 2023 11:19:11 +0000 Subject: [PATCH 1/2] #2793 ruff --- pybamm/discretisations/discretisation.py | 18 ++++++++++++++++-- .../test_discretisation.py | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pybamm/discretisations/discretisation.py b/pybamm/discretisations/discretisation.py index 0f841e9ad5..5dfd2405cc 100644 --- a/pybamm/discretisations/discretisation.py +++ b/pybamm/discretisations/discretisation.py @@ -297,9 +297,23 @@ def set_variable_slices(self, variables): # Add to slices y_slices[variable].append(slice(start, end)) y_slices_explicit[variable].append(slice(start, end)) + # Add to bounds - lower_bounds.extend([variable.bounds[0].evaluate()] * (end - start)) - upper_bounds.extend([variable.bounds[1].evaluate()] * (end - start)) + def evaluate_bound(bound, side): + if bound.has_symbol_of_classes(pybamm.InputParameter): + if side == "lower": + return -np.inf + elif side == "upper": + return np.inf + else: + return bound.evaluate() + + lower_bounds.extend( + [evaluate_bound(variable.bounds[0], "lower")] * (end - start) + ) + upper_bounds.extend( + [evaluate_bound(variable.bounds[1], "upper")] * (end - start) + ) # Increment start start = end diff --git a/tests/unit/test_discretisations/test_discretisation.py b/tests/unit/test_discretisations/test_discretisation.py index 9b19c773ab..40edce35dc 100644 --- a/tests/unit/test_discretisations/test_discretisation.py +++ b/tests/unit/test_discretisations/test_discretisation.py @@ -142,6 +142,13 @@ def test_discretise_slicing(self): with self.assertRaisesRegex(TypeError, "y_slices should be"): disc.y_slices = 1 + # bounds with an InputParameter + a = pybamm.InputParameter("a") + v = pybamm.Variable("v", domain=whole_cell, bounds=(0, a)) + disc.set_variable_slices([v]) + np.testing.assert_array_equal(disc.bounds[0], [0] * 100) + np.testing.assert_array_equal(disc.bounds[1], [np.inf] * 100) + def test_process_symbol_base(self): # create discretisation mesh = get_mesh_for_testing() From fca4dea3812b2821f2dfc415358f062c75747247 Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Tue, 21 Mar 2023 11:24:05 +0000 Subject: [PATCH 2/2] #2793 update test --- CHANGELOG.md | 1 + tests/unit/test_discretisations/test_discretisation.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16fd0a486a..b3bb9a48fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Bug fixes +- Fixed a bug where variable bounds could not contain `InputParameters` ([#2795](https://github.com/pybamm-team/PyBaMM/pull/2795)) - Improved `model.latexify()` to have a cleaner and more readable output ([#2764](https://github.com/pybamm-team/PyBaMM/pull/2764)) - Fixed electrolyte conservation in the case of concentration-dependent transference number ([#2758](https://github.com/pybamm-team/PyBaMM/pull/2758)) - Fixed `plot_voltage_components` so that the sum of overpotentials is now equal to the voltage ([#2740](https://github.com/pybamm-team/PyBaMM/pull/2740)) diff --git a/tests/unit/test_discretisations/test_discretisation.py b/tests/unit/test_discretisations/test_discretisation.py index 40edce35dc..aed16f9c9a 100644 --- a/tests/unit/test_discretisations/test_discretisation.py +++ b/tests/unit/test_discretisations/test_discretisation.py @@ -144,9 +144,10 @@ def test_discretise_slicing(self): # bounds with an InputParameter a = pybamm.InputParameter("a") - v = pybamm.Variable("v", domain=whole_cell, bounds=(0, a)) + b = pybamm.InputParameter("b") + v = pybamm.Variable("v", domain=whole_cell, bounds=(a, b)) disc.set_variable_slices([v]) - np.testing.assert_array_equal(disc.bounds[0], [0] * 100) + np.testing.assert_array_equal(disc.bounds[0], [-np.inf] * 100) np.testing.assert_array_equal(disc.bounds[1], [np.inf] * 100) def test_process_symbol_base(self):