# StaticArrays being used with DifferentialEquation callbacks

**URL:** <https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178>\
**Category:** Performance\
**Created:** [July 6, 2021, 8:46pm UTC](https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178 "2021-07-06T20:46:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)\
**Post date:** [July 6, 2021, 8:46pm UTC](https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178/1 "2021-07-06T20:46:24Z")

</div>

I’ve been improving performance of my differential equations with `@SVector` and `@MVector`but 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?

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 6, 2021, 9:43pm UTC](https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178/2 "2021-07-06T21:43:01Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![LionMod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lionmod/32/22149_2.png) [@LionMod](https://discourse.julialang.org/u/LionMod)\
**Post date:** [July 7, 2021, 7:04am UTC](https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178/3 "2021-07-07T07:04:06Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 7, 2021, 9:47am UTC](https://discourse.julialang.org/t/staticarrays-being-used-with-differentialequation-callbacks/64178/4 "2021-07-07T09:47:50Z")

</div>

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

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

```

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