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

Casadi vertcat is slow if using larger number of time steps #1996

Open
valentinsulzer opened this issue Mar 25, 2022 · 6 comments
Open

Casadi vertcat is slow if using larger number of time steps #1996

valentinsulzer opened this issue Mar 25, 2022 · 6 comments
Labels
difficulty: hard Will take several weeks priority: low No existing plans to resolve

Comments

@valentinsulzer
Copy link
Member

If using many time steps, it doesn't take too long to integrate the model, but it takes a long time to create the solution:

import pybamm
import numpy as np

pybamm.set_logging_level("INFO")

model = pybamm.lithium_ion.DFN() # options={"particle": "quadratic profile"})

param = model.default_parameter_values
# param["Negative electrode diffusivity [m2.s-1]"] = pybamm.InputParameter("Ds_n")
# param["Positive electrode diffusivity [m2.s-1]"] = pybamm.InputParameter("Ds_p")
# param["Electrolyte diffusivity [m2.s-1]"] = pybamm.InputParameter("De")

param["Current function [A]"] = param['Nominal cell capacity [A.h]']/10

solver = pybamm.CasadiSolver("fast")
simulation = pybamm.Simulation(model, parameter_values=param, solver=solver)


t_max = 60*60*10
t_eval = np.linspace(0, t_max, num=(t_max//2))

simulation.solve(t_eval)

Timings are as follows:

Set-up time: 71.993 ms, Solve time: 2.827 s (of which integration time: 127.908 ms), Total time: 2.899 s

Most of the additional solve time is being spent in this vertcat

y_sol = casadi.vertcat(casadi_sol["xf"], casadi_sol["zf"])

It's strange because the solution arrays are being created by the solver really fast, but then just vertcat takes a long time. Maybe there is a faster way to do vertcat?

@dion-w
Copy link
Contributor

dion-w commented Mar 28, 2022

From the casadi documentation i found: Concatenation means stacking matrices horizontally or vertically. Due to the column-major way of storing elements in CasADi, it is most efficient to stack matrices horizontally. Also someone in this forum gave a suggestion in his second point about a possible acceleration instead of using vercat. I have no experience with casadi whatsoever.

@valentinsulzer
Copy link
Member Author

@dion-w I don't really understand the solutions above ...

One idea I had is to create an object pybamm.no_memory_vertcat(a,b) which stores the two vectors and then can be indexed as needed, taking the relevant index from a or b (shifted) as needed

valentinsulzer added a commit that referenced this issue Jul 29, 2022
valentinsulzer added a commit that referenced this issue Jul 29, 2022
valentinsulzer added a commit that referenced this issue Jul 29, 2022
valentinsulzer added a commit that referenced this issue Aug 1, 2022
@valentinsulzer valentinsulzer mentioned this issue Aug 1, 2022
8 tasks
valentinsulzer added a commit that referenced this issue Aug 2, 2022
@rtimms rtimms added difficulty: hard Will take several weeks priority: low No existing plans to resolve labels May 15, 2023
@rtimms
Copy link
Contributor

rtimms commented Nov 22, 2024

@MarcBerliner is this still a bottleneck? not sure if your recent changes cover this? or if it will be fixed when we switch to IDAKLU as the default

@MarcBerliner
Copy link
Member

is this still a bottleneck? not sure if your recent changes cover this? or if it will be fixed when we switch to IDAKLU as the default

@rtimms the timings still look about the same for the CasadiSolver, but this is not an issue with the IDAKLUSolver since we store everything directly as numpy arrays, which handle concatenation better. Here's the IDAKLUSolver timings:

Set-up time: 43.910 ms, Solve time: 126.159 ms (of which integration time: 125.623 ms), Total time: 170.069 ms

Updated code for timing IDAKLUSolver:

solver = pybamm.IDAKLUSolver()
simulation = pybamm.Simulation(model, parameter_values=param, solver=solver)

t_max = 60*60*10
t_eval = np.linspace(0, t_max, num=(t_max//2))

simulation.solve([0, t_max], t_interp=t_eval)

@rtimms
Copy link
Contributor

rtimms commented Nov 22, 2024

great, thanks! i’ll leave this open for now but not a priority since we will eventually drop the casadi solver anyway

@MarcBerliner
Copy link
Member

great, thanks! i’ll leave this open for now but not a priority since we will eventually drop the casadi solver anyway

Sure. I don't think it's a difficult fix if we ever want/need to patch this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: hard Will take several weeks priority: low No existing plans to resolve
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants