# ODE solver takes long time

**URL:** <https://discourse.julialang.org/t/ode-solver-takes-long-time/40741>\
**Category:** General Usage\
**Created:** [June 4, 2020, 4:02pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741 "2020-06-04T16:02:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [June 4, 2020, 4:02pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741/1 "2020-06-04T16:02:54Z")

</div>

I have an ODE that I am trying to solve as follows:

```julia
L = 39  
c = 0.0            
Δx = 0.2                  
Δt = 0.1  
Tspan = 100 
t_vec = 0:Δt:Tspan      
x_vec = 0:Δx:L          
N = length(x_vec)       
p = c         
u0 = 0.2 * sin.((2π / L) * x_vec)

prob = ODEProblem(DerSen, u0, tspan, p)
sol = solve(prob, RK4(), saveat = Δt)

```

Where:

```julia
function DerSen(du,u,p,t)

for i=1:Nx

    if i==2
        du[i]=-(u[i+1]^2-0)/(4*Δx)-p*(u[i+1]-0)/(2*Δx)-(u[i+1]-2*u[i]+0)/(Δx^2)-(u[i]-0+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i==3  
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(0-4*u[i-1]+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i>=4 && i<=Nx-3 
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(u[i-2]-4*u[i-1]+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i==Nx-2 
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(u[i-2]-4*u[i-1]+6*u[i]-4*u[i+1]+0)/(Δx^4)
    end

    if i==Nx-1
        du[i]=-(0-u[i-1]^2)/(4*Δx)-p*(0-u[i-1])/(2*Δx)-(0-2*u[i]+u[i-1])/(Δx^2)-(u[i-2]-4*u[i-1]+6*u[i]-0+u[i])/(Δx^4)
    end
end
    return du
end

```

when `Δx` becomes lower than `0.5` , the ODE solver takes an infinite time to solve the same problem. Any suggestions?

---

<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:** [June 4, 2020, 4:32pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741/2 "2020-06-04T16:32:28Z")

</div>

Is there a reason adaptive RK4 is used here? This seems like just a generally really bad idea: it looks like a second order upwinding.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [June 4, 2020, 5:44pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741/3 "2020-06-04T17:44:50Z")

</div>

What would be a better solver? I chose RK4() to be consistent with results from literatures.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 4, 2020, 5:47pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741/4 "2020-06-04T17:47:36Z")

</div>

You should check out `DifferentialEquations`. They have state of the art performance for tons of stuff. [https://github.com/SciML/DifferentialEquations.jl](https://github.com/SciML/DifferentialEquations.jl)

---

<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:** [June 4, 2020, 5:52pm UTC](https://discourse.julialang.org/t/ode-solver-takes-long-time/40741/5 "2020-06-04T17:52:05Z")

</div>

> [@AhmedAlreweny](#):
>
> I chose RK4() to be consistent with results from literatures.

I assume to match the literature here you want to do `adaptive=false` as well, but more of the iterature on doing this efficiently would use an SSPRK method
