Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1393 end simulation when experiment infeasible #1395

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
28 changes: 18 additions & 10 deletions pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) "
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/test_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down