# Solving same DiffEq repeatedly but with different initial conditions

**URL:** <https://discourse.julialang.org/t/solving-same-diffeq-repeatedly-but-with-different-initial-conditions/49402>\
**Category:** Performance\
**Tags:** diffeq\
**Created:** [November 1, 2020, 7:34am UTC](https://discourse.julialang.org/t/solving-same-diffeq-repeatedly-but-with-different-initial-conditions/49402 "2020-11-01T07:34:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://discourse.julialang.org/u/ash)\
**Post date:** [November 1, 2020, 7:34am UTC](https://discourse.julialang.org/t/solving-same-diffeq-repeatedly-but-with-different-initial-conditions/49402/1 "2020-11-01T07:34:27Z")

</div>

I have a set of coupled ODEs that I’d like to solve, which is simple enough using DifferentialEquations.jl. My problem is that I need to solve the same ODE repeatedly for many different initial conditions parametrized by an array `x`.

Currently I just do this in a loop. I’m sure there’s a smarter way but I haven’t quite found it in the documentation yet. This is very inefficient of course since I need to keep setting up the problem again every time.

Could someone give me some pointers?

Here is a minimal non-working example of what I’m doing:

```julia
using Elliptic.Jacobi

function dn_ab!(du,u,p,t)
    du[1] .= +im*p[1].*u[1] .+ im.*u[2].*dn.(t,real(p[2]))
    du[2] .= -im*p[1].*u[2] .+ im.*u[1].*dn.(t,real(p[2]))
end

function ab_dn_t0(x)
    A = +χ .+ 0.5.*Ω*λ.*(x - xs) .- π/4
    B = -χ .+ 0.5.*Ω*λ.*(x - xs).- π/4
    return [2*im*sin(A); 2*cos(B)]
end

# How do you get rid of this whole loop?
tspan = (0.0,abs(minimum(c.box.t)))
for i in eachindex(x)
    u0 = ab_dn_t0(x[i])
    prob = ODEProblem(dn_ab!, u0, tspan, [λ, m])
    sol = solve(prob, Tsit5(), saveat=t) # t is an array
   # do some processing on sol and save the result
end
# do some more processing on the saved result for all x

```

---

<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:** [November 1, 2020, 10:59am UTC](https://discourse.julialang.org/t/solving-same-diffeq-repeatedly-but-with-different-initial-conditions/49402/2 "2020-11-01T10:59:17Z")

</div>

> [@ash](#):
>
> Currently I just do this in a loop. I’m sure there’s a smarter way but I haven’t quite found it in the documentation yet. This is very inefficient of course since I need to keep setting up the problem again every time.

You can use the integrator interface with `reinit`.

[https://diffeq.sciml.ai/stable/basics/integrator/](https://diffeq.sciml.ai/stable/basics/integrator/)

Or you can use the ensemble interface to auto-parallelize it.

[https://diffeq.sciml.ai/stable/features/ensemble/](https://diffeq.sciml.ai/stable/features/ensemble/)

This includes automated GPU parallelism with DiffEqGPU.jl

> **[GitHub - SciML/DiffEqGPU.jl: GPU-acceleration routines for...](https://github.com/SciML/DiffEqGPU.jl)**
>
> GPU-acceleration routines for DifferentialEquations.jl and the broader SciML scientific machine learning ecosystem

---

<div class="post-metadata">

**Author:** ![ash](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ash](https://discourse.julialang.org/u/ash)\
**Post date:** [November 2, 2020, 8:14pm UTC](https://discourse.julialang.org/t/solving-same-diffeq-repeatedly-but-with-different-initial-conditions/49402/3 "2020-11-02T20:14:46Z")

</div>

> [@ChrisRackauckas](#):
>
> You can use the integrator interface with `reinit` .

Thanks Chris, that resolves it for my use case.

> [@ChrisRackauckas](#):
>
> Or you can use the ensemble interface to auto-parallelize it.

That’s wonderful, I’ll definitely move to this at some point if my system gets large enough.
