# Preallocate u and du in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911>\
**Category:** General Usage\
**Tags:** ordinarydiffeq\
**Created:** [September 15, 2023, 6:17pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911 "2023-09-15T18:17:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dom](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Dom](https://discourse.julialang.org/u/Dom)\
**Post date:** [September 15, 2023, 6:17pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/1 "2023-09-15T18:17:54Z")

</div>

Hi all,

we are currently writing a simulation engine which is a massive ODE with \>\>100k elements and therefore need to be very memory conscious.  
We need to pre-allocate the u and du vectors and have DifferentialEquations.jl use them in-place. Currently there seems to be no way to supply u and du, only u0, which is not used internally.

This is how we would like to use it:

```julia
using OrdinaryDiffEq
function lorenz!(du,u,p,t)
 du[1] = 10.0(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]
end
u = [1.0;0.0;0.0]
du = [0.0; 0.0; 0.0]
tspan = (0.0,100.0)

# ODEProblem doesnt accept u and du, only u0
prob = ODEProblem(lorenz!,u, du, tspan)

integrator = init(prob,Tsit5())
step!(integrator,1)

```

Is there a way to make this work?

---

<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:** [September 15, 2023, 6:27pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/2 "2023-09-15T18:27:20Z")

</div>

> [@Dom](#):
>
> `integrator = init(prob,Tsit5())`

That allocates all of the necessary vectors. Then `step!(integrator)` is non-allocating.

How else would you know what vectors to allocate? Different methods need different vectors of different things, so that’s the general form to figure out what needs to be cached for a method.

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [September 15, 2023, 6:31pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/3 "2023-09-15T18:31:53Z")

</div>

I am not sure whether that’s the problem but Tsit5 will probably need to allocate a lot more memory than just u and du since it is a higher Order method right?  
So it might be that you have to resort to lower order methods like BS3 or perhaps even Euler?  
There are also multistep methods such as VCABM although I don’t know how much they need to allocate maybe someone more knowledgeable will be able to estimate this

---

<div class="post-metadata">

**Author:** ![Dom](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Dom](https://discourse.julialang.org/u/Dom)\
**Post date:** [September 15, 2023, 6:44pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/4 "2023-09-15T18:44:16Z")

</div>

Thanks for the quick answer!

I know internally that the lorenz! function needs du and u to be a vector with three elements - those are the elements I use, regardless of the method use.

There should be a way for me to allocate those vectors?

---

<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:** [September 15, 2023, 6:45pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/5 "2023-09-15T18:45:52Z")

</div>

> [@Dom](#):
>
> I know internally that the lorenz! function needs du and u to be a vector with three elements - those are the elements I use, regardless of the method use.

But how many vectors? For Tsit5 you need 7, for Ven7 you need over 20. `init` is how you can allocate the cache you find in `integrator.cache`.

---

<div class="post-metadata">

**Author:** ![Dom](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Dom](https://discourse.julialang.org/u/Dom)\
**Post date:** [September 15, 2023, 7:04pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/6 "2023-09-15T19:04:49Z")

</div>

yes, I see that. I could still save some allocations when pre-allocating du and u.

Maybe I just don’t fully understand what you are doing there.

---

<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:** [September 15, 2023, 7:12pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/7 "2023-09-15T19:12:25Z")

</div>

> [@Dom](#):
>
> yes, I see that. I could still save some allocations when pre-allocating du and u.

Which allocations?

---

<div class="post-metadata">

**Author:** ![Dom](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Dom](https://discourse.julialang.org/u/Dom)\
**Post date:** [September 15, 2023, 7:17pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/8 "2023-09-15T19:17:23Z")

</div>

we could avoid the allocation of u0.

---

<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:** [September 15, 2023, 7:19pm UTC](https://discourse.julialang.org/t/preallocate-u-and-du-in-differentialequations-jl/103911/9 "2023-09-15T19:19:29Z")

</div>

> **[Common Solver Options (Solve Keyword Arguments) · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts/)**
>
> Documentation for DifferentialEquations.jl.

> - `alias_u0`: allows the solver to alias the initial condition array that is contained in the problem struct. Defaults to false.
