StaticArrays being used with DifferentialEquation callbacks

I’ve been improving performance of my differential equations with @SVector and @MVectorbut I’ve got stuck when it comes to using it with callbacks specifically PresetTimeCallback.

The idea is there is a large spike in volume (u[1]) at time 25 but then it goes “back to normal” at the next time hence the two callbacks. Then these two callbacks go into a CallbackSet. It is working but I would like to know if there’s a way to use StaticArrays.jl with the callbacks. Any thoughts?

big_volume_jump = 25.0

function big_volume_jump!(integrator)
    integrator.u[1] += 250.0
end

function return_volume_to_normal!(integrator)
    integrator.u[1] -= 250.0
end

big_volume_jump_callback = PresetTimeCallback(big_volume_time, big_volume_jump!)
resume_volume_callback = PresetTimeCallback(big_volume_time + 0.01, return_volume_to_normal!)

callbacks = CallbackSet(big_volume_jump_callback, resume_volume_callback)

StaticArrays are immutable, so you have to build a new one:

function big_volume_jump!(integrator)
    integrator.u = vcat(SA[integrator.u[1] + 250.0],integrator.u[2:end])
end

or using StaticArrays.setindex can be helpful.

2 Likes

Thank you for the quick response Chris.

My intuition of this is we are applying the spike to u[1] then taking all other u’s and stacking them ontop given they aren’t being affected by the callback.

Is this correct?

Also is there not a way to use @SVector instead of the vcat and @SArray?

You can use @SVector if you want to write the whole thing out:

function big_volume_jump!(integrator)
    integrator.u = @SVector [...]
end

I was just trying to show styles where you only define one piece.