From 1c7a19fcd849288aee7b851150e53fdbd7aebafa Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Fri, 19 Feb 2021 14:52:47 +0000 Subject: [PATCH 1/2] #1393 end simulation when experiment infeasible --- pybamm/simulation.py | 28 +++++++++++++++++---------- tests/integration/test_experiments.py | 16 +++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/pybamm/simulation.py b/pybamm/simulation.py index df2516e57a..ab6b15d28b 100644 --- a/pybamm/simulation.py +++ b/pybamm/simulation.py @@ -447,6 +447,7 @@ def solve(self, t_eval=None, solver=None, check_model=True, **kwargs): idx = 0 num_cycles = len(self.experiment.cycle_lengths) + feasible = True # simulation will stop if experiment is infeasible for cycle_num, cycle_length in enumerate(self.experiment.cycle_lengths): pybamm.logger.info( f"Cycle {cycle_num+1}/{num_cycles} ({timer.time()} elapsed) " @@ -504,21 +505,28 @@ def solve(self, t_eval=None, solver=None, check_model=True, **kwargs): self._solution.termination == "final time" or "[experiment]" in self._solution.termination ): - pybamm.logger.warning( - "\n\n\tExperiment is infeasible: '{}' ".format( - self._solution.termination - ) - + "was triggered during '{}'. ".format( - self.experiment.operating_conditions_strings[idx] - ) - + "Try reducing current, shortening the time interval, " - "or reducing the period.\n\n" - ) + feasible = False break # Increment index for next iteration idx += 1 + # Break if the experiment is infeasible + if feasible is False: + pybamm.logger.warning( + "\n\n\tExperiment is infeasible: '{}' ".format( + self._solution.termination + ) + + "was triggered during '{}'. ".format( + self.experiment.operating_conditions_strings[idx] + ) + + "The returned solution only contains the first " + "{} cycles. ".format(cycle_num) + + "Try reducing the current, shortening the time interval, " + "or reducing the period.\n\n" + ) + break + # At the final step of the inner loop we save the cycle cycle_solution.steps = steps all_cycle_solutions.append(cycle_solution) diff --git a/tests/integration/test_experiments.py b/tests/integration/test_experiments.py index 44189d699d..314f34ce95 100644 --- a/tests/integration/test_experiments.py +++ b/tests/integration/test_experiments.py @@ -68,6 +68,22 @@ def test_gitt(self): [cap / 20] * 11 + [0] * 10 + ([cap / 20] * 10 + [0] * 10) * 9, ) + def test_infeasible(self): + experiment = pybamm.Experiment( + [ + ("Discharge at 1C for 0.5 hours",), + ] + * 4 + ) + model = pybamm.lithium_ion.SPM() + sim = pybamm.Simulation( + model, experiment=experiment, solver=pybamm.CasadiSolver() + ) + sol = sim.solve() + # this experiment fails during the third cycle (i.e. is infeasible) + # so we should just return the successful cycles (2 in this case) + self.assertEqual(len(sol.cycles), 2) + if __name__ == "__main__": print("Add -v for more debug output") From f7f2dde1cd0607c73399f4298d737dd73d6878f4 Mon Sep 17 00:00:00 2001 From: Robert Timms Date: Mon, 22 Feb 2021 10:03:31 +0000 Subject: [PATCH 2/2] #1393 changelog --- CHANGELOG.md | 1 + CONTRIBUTING.md | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b221376864..36c0eeb1bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ ## Bug fixes +- Simulations now stop when an experiment becomes infeasible ([#1395](https://github.com/pybamm-team/PyBaMM/pull/1395)) - Added a check for domains in `Concatenation` ([#1368](https://github.com/pybamm-team/PyBaMM/pull/1368)) - Differentiation now works even when the differentiation variable is a constant ([#1294](https://github.com/pybamm-team/PyBaMM/pull/1294)) - Fixed a bug where the event time and state were no longer returned as part of the solution ([#1344](https://github.com/pybamm-team/PyBaMM/pull/1344)) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 08c8316acb..4a38f574e1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -204,7 +204,7 @@ If you want to check integration tests as well as unit tests, type ```bash tox -e tests # (GNU/Linux and MacOS) # -python -m tox -e windows-tests (Windows) +python -m tox -e windows-tests # (Windows) ``` When you commit anything to PyBaMM, these checks will also be run automatically (see [infrastructure](#infrastructure)). @@ -216,7 +216,7 @@ To test all example scripts and notebooks, type ```bash tox -e examples # (GNU/Linux and MacOS) # -python -m tox -e windows-examples (Windows) +python -m tox -e windows-examples # (Windows) ``` If notebooks fail because of changes to pybamm, it can be a bit of a hassle to debug. In these cases, you can create a temporary export of a notebook's Python content using @@ -340,7 +340,7 @@ Where possible, notebooks are tested daily. A list of slow notebooks (which time ## Citations -We aim to recognize all contributions by automatically generating citations to the relevant papers on which different parts of the code are built. +We aim to recognize all contributions by automatically generating citations to the relevant papers on which different parts of the code are built. These will change depending on what models and solvers you use. Adding the command