From 8d7ae3a6e481bb812a5364123d3515ef0348beca Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Thu, 23 Jul 2020 16:53:59 -0400 Subject: [PATCH 1/3] #1108 change t_eval list to linspace --- pybamm/solvers/base_solver.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pybamm/solvers/base_solver.py b/pybamm/solvers/base_solver.py index b529805f6b..3bdda498ae 100644 --- a/pybamm/solvers/base_solver.py +++ b/pybamm/solvers/base_solver.py @@ -510,6 +510,19 @@ def solve(self, model, t_eval=None, external_variables=None, inputs=None): t_eval = np.array([0]) else: raise ValueError("t_eval cannot be None") + # If t_eval is provided as [t0, tf] return the solution at 100 points + elif isinstance(t_eval, list): + if len(t_eval) == 1 and self.algebraic_solver is True: + pass + elif len(t_eval) != 2: + raise pybamm.SolverError( + "'t_eval' can be provided as an array of times at which to " + "return the solution, or as a list [t0, tf] where t0 is the " + "initial time and tf is the final time, but has been provided " + "as a list of length {}.".format(len(t_eval)) + ) + else: + t_eval = np.linspace(t_eval[0], t_eval[-1], 100) # Make sure t_eval is monotonic if (np.diff(t_eval) < 0).any(): From 63be9949d82a3094df387fd6239357f42529cfaa Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Fri, 24 Jul 2020 14:16:14 -0400 Subject: [PATCH 2/3] #1108 remove duplicate --- pybamm/simulation.py | 13 +------------ tests/unit/test_simulation.py | 2 +- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/pybamm/simulation.py b/pybamm/simulation.py index 299b3291b4..e2dec93842 100644 --- a/pybamm/simulation.py +++ b/pybamm/simulation.py @@ -330,17 +330,6 @@ def solve( solver = self.solver if self.operating_mode in ["without experiment", "drive cycle"]: - # If t_eval is provided as [t0, tf] return the solution at 100 points - if isinstance(t_eval, list): - if len(t_eval) != 2: - raise pybamm.SolverError( - "'t_eval' can be provided as an array of times at which to " - "return the solution, or as a list [t0, tf] where t0 is the " - "initial time and tf is the final time, but has been provided " - "as a list of length {}.".format(len(t_eval)) - ) - else: - t_eval = np.linspace(t_eval[0], t_eval[-1], 100) if self.operating_mode == "without experiment": if t_eval is None: @@ -403,13 +392,13 @@ def solve( pybamm.SolverWarning, ) - self.t_eval = t_eval self._solution = solver.solve( self.built_model, t_eval, external_variables=external_variables, inputs=inputs, ) + self.t_eval = self._solution.t * self.model.timescale.evaluate() elif self.operating_mode == "with experiment": if t_eval is not None: diff --git a/tests/unit/test_simulation.py b/tests/unit/test_simulation.py index 03037cb32c..868f3a0812 100644 --- a/tests/unit/test_simulation.py +++ b/tests/unit/test_simulation.py @@ -370,7 +370,7 @@ def test_t_eval(self): # tets list gets turned into np.linspace(t0, tf, 100) sim.solve(t_eval=[0, 10]) - np.testing.assert_array_equal(sim.t_eval, np.linspace(0, 10, 100)) + np.testing.assert_array_almost_equal(sim.t_eval, np.linspace(0, 10, 100)) if __name__ == "__main__": From 81c4e958c188e7816678d9e8b3ea66dcddc68df6 Mon Sep 17 00:00:00 2001 From: Valentin Sulzer Date: Fri, 24 Jul 2020 17:19:12 -0400 Subject: [PATCH 3/3] #1108 changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1090184dd7..3be8da0acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Features +- Added support for sensitivity calculations to the casadi solver ([#1109](https://github.com/pybamm-team/PyBaMM/pull/1109)) - Added support for index 1 semi-explicit dae equations and sensitivity calculations to JAX BDF solver ([#1107](https://github.com/pybamm-team/PyBaMM/pull/1107)) - Allowed keyword arguments to be passed to `Simulation.plot()` ([#1099](https://github.com/pybamm-team/PyBaMM/pull/1099)) @@ -9,6 +10,9 @@ ## Bug fixes +- `t_eval` now gets changed to a `linspace` if a list of length 2 is passed ([#1113](https://github.com/pybamm-team/PyBaMM/pull/1113)) +- Fixed bug when setting a function with an `InputParameter` ([#1111](https://github.com/pybamm-team/PyBaMM/pull/1111)) + ## Breaking changes - Renamed `quick_plot_vars` to `output_variables` in `Simulation` to be consistent with `QuickPlot`. Passing `quick_plot_vars` to `Simulation.plot()` has been deprecated and `output_variables` should be passed instead ([#1099](https://github.com/pybamm-team/PyBaMM/pull/1099))