PyBaMM icon indicating copy to clipboard operation
PyBaMM copied to clipboard

3937 events with idaklu output variables

Open pipliggins opened this issue 1 year ago • 1 comments

Description

Simulations using the IDAKLU solver with output variables specified which terminated with an event rather than the final time step would crash trying to find the termination event.

Termination events are found in the Solution class by using the state vector of the final time-step to evaluate each potential termination event and find the minimum (the event which caused the termination). When output variables are specified in the IDAKLU solver only those variables are output, not the entire state vector, so the events could not be evaluated outside the solver.

This PR adds a check to see if the events specified in a model can be calculated using the values of the output variables rather than the state vector. If they can, the events are calculated using these values after the solve. If not, an error is raised warning that events will not be able to be calculated with the current output variables, and provides a list of variables to choose from to add to the output_variables list, e.g.:

model = pybamm.lithium_ion.DFN()
parameter_values = pybamm.ParameterValues("Chen2020")

sim2 = pybamm.Simulation(
  model,
  parameter_values=parameter_values,
  solver=pybamm.IDAKLUSolver(output_variables=["Cell temperature [K]"]),
)
sim2.solve(np.linspace(0, 3600, 1000))

raises

ValueError: IDA KLU solver: Event 'Minimum voltage [V]' cannot be calculated using the current output variables.
Add one of the following to `output_variables`:
Positive current collector potential [V]
Local voltage [V]
Terminal voltage [V]
Voltage [V]
Battery voltage [V]

Fixes #3937

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Optimization (back-end change that speeds up the code)
  • [x] Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • [x] No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • [ ] All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • [x] The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • [x] Code is commented, particularly in hard-to-understand areas
  • [x] Tests added that prove fix is effective or that feature works

pipliggins avatar Jun 07 '24 14:06 pipliggins

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.58%. Comparing base (b4f8380) to head (e8ef393). Report is 329 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4150      +/-   ##
===========================================
+ Coverage    99.55%   99.58%   +0.02%     
===========================================
  Files          288      288              
  Lines        21790    21825      +35     
===========================================
+ Hits         21693    21734      +41     
+ Misses          97       91       -6     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jun 07 '24 15:06 codecov[bot]