From b2de25ed4dcd22d487150c127a9487d622208ef9 Mon Sep 17 00:00:00 2001 From: tomtranter Date: Fri, 6 Nov 2020 13:35:00 +0000 Subject: [PATCH 1/4] make quickplot use scaling from solutions --- pybamm/plotting/quick_plot.py | 18 +++++++++++------- tests/unit/test_quick_plot.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pybamm/plotting/quick_plot.py b/pybamm/plotting/quick_plot.py index 9348b8907d..a1ec43b5eb 100644 --- a/pybamm/plotting/quick_plot.py +++ b/pybamm/plotting/quick_plot.py @@ -146,16 +146,20 @@ def __init__( else: raise ValueError("spatial unit '{}' not recognized".format(spatial_unit)) - # Set length scales - self.length_scales = { - domain: scale.evaluate() * self.spatial_factor - for domain, scale in models[0].length_scales.items() - } + try: + # Set length scales + self.length_scales = { + domain: scale.evaluate() * self.spatial_factor + for domain, scale in models[0].length_scales.items() + } + except KeyError: + # Length scales are probably function of input + # Use the evaluated length scales from the first solution + self.length_scales = solutions[0].length_scales_eval # Time parameters - model_timescale_in_seconds = models[0].timescale_eval self.ts_seconds = [ - solution.t * model_timescale_in_seconds for solution in solutions + solution.t * solution.timescale_eval for solution in solutions ] min_t = np.min([t[0] for t in self.ts_seconds]) max_t = np.max([t[-1] for t in self.ts_seconds]) diff --git a/tests/unit/test_quick_plot.py b/tests/unit/test_quick_plot.py index 6308864f9f..6d100586c1 100644 --- a/tests/unit/test_quick_plot.py +++ b/tests/unit/test_quick_plot.py @@ -452,6 +452,36 @@ def test_failure(self): with self.assertRaisesRegex(TypeError, "solutions must be"): pybamm.QuickPlot(1) + def test_model_with_inputs(self): + chemistry = pybamm.parameter_sets.Chen2020 + parameter_values = pybamm.ParameterValues(chemistry=chemistry) + model = pybamm.lithium_ion.SPMe() + parameter_values.update({"Electrode height [m]": "[input]"}) + solver = pybamm.CasadiSolver(mode='safe') + sim1 = pybamm.Simulation(model, parameter_values=parameter_values, + solver=solver) + inputs1 = {"Electrode height [m]": 1.00} + sol1 = sim1.solve(t_eval=np.linspace(0, 1000, 101), inputs=inputs1) + sim2 = pybamm.Simulation(model, parameter_values=parameter_values, + solver=solver) + inputs2 = {"Electrode height [m]": 2.00} + sol2 = sim2.solve(t_eval=np.linspace(0, 1000, 101), inputs=inputs2) + output_variables = [ + "Terminal voltage [V]", + "Current [A]", + "Negative electrode potential [V]", + "Positive electrode potential [V]", + "Electrolyte potential [V]", + "Electrolyte concentration", + "Negative particle surface concentration", + "Positive particle surface concentration", + ] + quick_plot = pybamm.QuickPlot(solutions=[sol1, sol2], + output_variables=output_variables) + quick_plot.dynamic_plot(testing=True) + quick_plot.slider_update(1) + pybamm.close_plots() + if __name__ == "__main__": print("Add -v for more debug output") From 96ab7e0c029f18a84c23d1921c057405cb8b0169 Mon Sep 17 00:00:00 2001 From: tomtranter Date: Fri, 6 Nov 2020 13:39:13 +0000 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ccaa72388..44cacd9f06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - Raise error if the boundary condition at the origin in a spherical domain is other than no-flux ([#1175](https://github.com/pybamm-team/PyBaMM/pull/1175)) - Fix boundary conditions at r = 0 for Creating Models notebooks ([#1173](https://github.com/pybamm-team/PyBaMM/pull/1173)) - Make sure simulation solves when evaluated timescale is a function of an input parameter ([#1218](https://github.com/pybamm-team/PyBaMM/pull/1218)) +- Quickplot now works when timescale or lengthscale is a function of an input parameter ([#1234](https://github.com/pybamm-team/PyBaMM/pull/1234)) ## Breaking changes From caf75226e5237e0238ff43bc830ba88c0f2e17ad Mon Sep 17 00:00:00 2001 From: tomtranter Date: Mon, 9 Nov 2020 10:47:51 +0000 Subject: [PATCH 3/4] update changelog order --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44cacd9f06..9c890af623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,13 +21,13 @@ ## Bug fixes +- Quickplot now works when timescale or lengthscale is a function of an input parameter ([#1234](https://github.com/pybamm-team/PyBaMM/pull/1234)) - Fix bug that was slowing down creation of the EC reaction SEI submodel ([#1227](https://github.com/pybamm-team/PyBaMM/pull/1227)) - Add missing separator thermal parameters for the Ecker parameter set ([#1226](https://github.com/pybamm-team/PyBaMM/pull/1226)) +- Make sure simulation solves when evaluated timescale is a function of an input parameter ([#1218](https://github.com/pybamm-team/PyBaMM/pull/1218)) - Raise error if saving to MATLAB with variable names that MATLAB can't read, and give option of providing alternative variable names ([#1206](https://github.com/pybamm-team/PyBaMM/pull/1206)) - Raise error if the boundary condition at the origin in a spherical domain is other than no-flux ([#1175](https://github.com/pybamm-team/PyBaMM/pull/1175)) - Fix boundary conditions at r = 0 for Creating Models notebooks ([#1173](https://github.com/pybamm-team/PyBaMM/pull/1173)) -- Make sure simulation solves when evaluated timescale is a function of an input parameter ([#1218](https://github.com/pybamm-team/PyBaMM/pull/1218)) -- Quickplot now works when timescale or lengthscale is a function of an input parameter ([#1234](https://github.com/pybamm-team/PyBaMM/pull/1234)) ## Breaking changes From 1483504ed949640fee1e3a72cd8178b7162b2f7f Mon Sep 17 00:00:00 2001 From: tomtranter Date: Mon, 9 Nov 2020 14:57:46 +0000 Subject: [PATCH 4/4] remove unused variables --- pybamm/plotting/quick_plot.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/pybamm/plotting/quick_plot.py b/pybamm/plotting/quick_plot.py index a1ec43b5eb..3e76654f8a 100644 --- a/pybamm/plotting/quick_plot.py +++ b/pybamm/plotting/quick_plot.py @@ -146,17 +146,6 @@ def __init__( else: raise ValueError("spatial unit '{}' not recognized".format(spatial_unit)) - try: - # Set length scales - self.length_scales = { - domain: scale.evaluate() * self.spatial_factor - for domain, scale in models[0].length_scales.items() - } - except KeyError: - # Length scales are probably function of input - # Use the evaluated length scales from the first solution - self.length_scales = solutions[0].length_scales_eval - # Time parameters self.ts_seconds = [ solution.t * solution.timescale_eval for solution in solutions