Sundials: High memory usage

I am using the package Sundials, which belongs to DifferentialEquations. I have a residual function with zero memory allocations, but the IDA solver of Sundials has a quite high memory usage. I have the following code for calculation one simulation step (50ms):

function next_step(s, integrator, dt)
    KitePodModels.on_timer(s.kcu)
    KiteModels.set_depower_steering(s, 0.236, get_steering(s.kcu))
    Sundials.step!(integrator, dt, true)
    t = integrator.t
    v_ro = 0.0
    set_v_reel_out(s, v_ro, t)
end

I am simulating a particle system with 11 particles, aerodynamics forces attached to particles and segments, connecting the particles.

When calling this function 100 times I get:
0.435539 seconds (780.35 k allocations: 168.247 MiB, 5.12% gc time)
So 7800 allocations per time step… Why?
The callback function is called in average 1491 times per timestep.

Is there a way to reduce it, or is this just the way Sundials works?

You can find the complete code here: https://github.com/ufechner7/KiteModels.jl/tree/initial

If you check out the code you should be able to reproduce the problem with the following command:

julia> include("test/test_steady_state.jl")
  0.555643 seconds (780.35 k allocations: 168.247 MiB, 2.39% gc time)

To be honest, the code is already more than 5 times faster than Python/Numba… But I want more!
I reduced the time that is spent in the callback function a lot, but now the time that is spent in the solver is limiting the overall performance.

OK, I fixed one error in my code (I had defined two variables as algebraic that are not) and added the parameter max_order = 3 for IDA. Much better results now:

julia> include("test/test_steady_state.jl")
  0.152172 seconds (253.25 k allocations: 55.238 MiB)
Average number of callbacks per time step: 474.42

So my code is now more than 15 times faster than Python/Numba and about 31 times faster than realtime… Quite happy! :slight_smile:

2 Likes