# ODE solver takes long time

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

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:

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?

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.

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

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

1 Like

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

2 Likes