pybamm-team / PyBaMM

Fast and flexible physics-based battery models in Python
https://www.pybamm.org/
BSD 3-Clause "New" or "Revised" License
1.04k stars 520 forks source link

[Roadmap area] Solver improvements #3910

Open brosaplanella opened 5 months ago

brosaplanella commented 5 months ago

I'd like to continue making pybamm easier to work with for the parameter inference libraries like PyBOP (https://github.com/pybop-team/PyBOP). At the moment I think this includes:

Originally posted by @martinjrobins in https://github.com/pybamm-team/PyBaMM/issues/3839#issuecomment-1999464816

rtimms commented 2 months ago

Not solver, but this is related to speed-up: https://github.com/pybamm-team/PyBaMM/issues/4058

martinjrobins commented 1 month ago

This is my attempt at a roadmap for PyBaMM's solvers, comments / suggestions welcome

Sensitivities

Multithreaded/GPU support:

Post Processing optimisation and refactoring

Solver refactoring

Solver documentation

MarcBerliner commented 1 month ago

Here are my thoughts on improving the solver. Please let me know if you have comments or questions @martinjrobins and others.

Solver options

Initialization

Time stepping

In PyBaMM, the solver currently stops at all values in the t_eval vector. It seems that we use t_eval and set some dt for a few distinct purposes:

  1. To enforce time-dependent discontinuities within a single step, like with a drive cycle
  2. To set the data collection frequency (as in the period experiment kwarg)
  3. Setting a small dt to force better solver convergence (take smaller time steps)

These three reasons for setting a dt are all valid, but stopping the simulation at all time steps can have a drastic impact on the adaptive time stepping and performance. For example, consider a full C/10 discharge with

If we compare the solver stats,

Even though we solve the same system, the dense t_eval b. is nearly 4x slower! To address these issues, I propose the following changes that align the time-stepping options with Julia's differential equation solvers (see Output Control):

martinjrobins commented 1 month ago

yea, agree that it would be great to get rid of IDASetStopTime! or at least reduce the number of times we use it. Would this play nicely with the casadi solver?

MarcBerliner commented 1 month ago

Would this play nicely with the casadi solver?

If we can access IDA's internal time steps via Casadi, I think we can do most of this stuff

agriyakhetarpal commented 3 weeks ago

Corollary from the PyBaMM developer meeting on 05/08/2024 as a part of PyBaMM running in WASM:

martinjrobins commented 3 weeks ago

@agriyakhetarpal: FYI, I've compiled IDA with KLU using enscripten in the past, see this repo: https://github.com/martinjrobins/diffeq-runtime. However I agree that focusing on the casadi solver for now is the best approach since you have it already compiled and in Pyodide

agriyakhetarpal commented 3 weeks ago

Thanks for the resource, @martinjrobins! I see you've built a static lib for KLU and also used NO_LAPACK – should work well. I'll tinker with the settings and maybe I can reduce the binary size a bit :)

MarcBerliner commented 4 days ago

To improve the speed of our ODE models, I'd like to add CVODE from SUNDIALS to our suite of C solvers in addition to IDA. I have a few questions about the implementation, and I'd like to hear your thoughts @jsbrittain, @martinjrobins, @pipliggins, and others.

And just FYI, I don't plan on starting this work for at least a couple of weeks.