# Problem with DifferentialEquations & StaticArrays

**URL:** <https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129>\
**Category:** Numerics\
**Tags:** diffeq\
**Created:** [May 8, 2020, 7:11pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129 "2020-05-08T19:11:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [May 8, 2020, 7:11pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129/1 "2020-05-08T19:11:53Z")

</div>

Hello,

I would like to solve the Lorenz-96 problem with static arrays. The state size is `40`. I have followed the tutorial [Optimizing DiffEq Code](https://tutorials.sciml.ai/html/introduction/03-optimizing_diffeq_code.html) but I have the following error message:

```julia
using StaticArrays
using DifferentialEquations

function lorenz96ode!(du,u,p,t)
    F = 8.0
    n = size(u,1)
    du[1] = (u[2]-u[end-1])*u[end] - u[1] + F
    du[2] = (u[3]-u[end])*u[1] - u[2] + F
    du[end] = (u[1] - u[end-2])*u[end-1] - u[end] + F

    @inbounds for i=3:n-1
        du[i] = (u[i+1] - u[i-2])*u[i-1] - u[i] + F
    end
    SVector{40,Float64}(du)
end
u0 = SVector{40, Float64}(randn(40))
tspan = (0.0,100.0)
prob = ODEProblem(lorenz96ode!,u0,tspan)
 sol = solve(prob, Tsit5())

```

MethodError: no method matching OrdinaryDiffEq.Tsit5Cache(::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::SArray{Tuple{40},Float64,1,40}, ::MArray{Tuple{40},Float64,1,40}, ::MArray{Tuple{40},Float64,1,40}, ::MArray{Tuple{40},Float64,1,40}, ::OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64})  
Closest candidates are:  
OrdinaryDiffEq.Tsit5Cache(::#217#uType, ::#217#uType, ::#218#rateType, ::#218#rateType, ::#218#rateType, ::#218#rateType, ::#218#rateType, ::#218#rateType, ::#218#rateType, !Matched::#217#uType, !Matched::#217#uType, ::#219#uNoUnitsType, ::#220#TabType) where {#217#uType, #218#rateType, #219#uNoUnitsType, #220#TabType} at /home/mat/.julia/packages/OrdinaryDiffEq/5igHh/src/caches/low\_order\_rk\_caches.jl:272

---

<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:** [May 8, 2020, 7:19pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129/2 "2020-05-08T19:19:53Z")

</div>

You can’t mutate static arrays. This should just be done with standard arrays.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [May 8, 2020, 7:23pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129/3 "2020-05-08T19:23:42Z")

</div>

If I use a standard array for the initial condition, the code runs. Is there any advantage to use StaticArrays in the rhs then?

```julia
using StaticArrays
using DifferentialEquations

function lorenz96ode!(du,u,p,t)
    F = 8.0
    n = size(u,1)
    du[1] = (u[2]-u[end-1])*u[end] - u[1] + F
    du[2] = (u[3]-u[end])*u[1] - u[2] + F
    du[end] = (u[1] - u[end-2])*u[end-1] - u[end] + F

    @inbounds for i=3:n-1
        du[i] = (u[i+1] - u[i-2])*u[i-1] - u[i] + F
    end
    SVector{40,Float64}(du)
end
u0 = randn(40)
tspan = (0.0,100.0)
prob = ODEProblem(lorenz96ode!,u0,tspan)
 sol = solve(prob, Tsit5())

```

---

<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:** [May 8, 2020, 8:11pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129/4 "2020-05-08T20:11:38Z")

</div>

> [@mleprovost](#):
>
> If I use a standard array for the initial condition, the code runs. Is it there any advantage to use StaticArrays in the rhs then?

Not at that size. I have found that static arrays will begin to loose their performance benefit at around 20ish ODEs, so lower than it used to be, and I am not sure what happened in their dispatches that caused that. Definitely not 100 anymore and we should update the docs.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [May 8, 2020, 8:18pm UTC](https://discourse.julialang.org/t/problem-with-differentialequations-staticarrays/39129/5 "2020-05-08T20:18:24Z")

</div>

Thank you for your explanation.
