# Three Body Work-Precision Diagrams

##### Chris Rackauckas
using OrdinaryDiffEq, ODE, ODEInterfaceDiffEq, LSODA, Sundials, DiffEqDevTools, Plots; gr()

## Define the ThreeBody Problem
const threebody_μ = parse(Float64,"0.012277471")
const threebody_μ′ = 1 - threebody_μ

f = (du,u,p,t) -> begin
@inbounds begin
# 1 = y₁
# 2 = y₂
# 3 = y₁'
# 4 = y₂'
D₁ = ((u[1]+threebody_μ)^2 + u[2]^2)^(3/2)
D₂ = ((u[1]-threebody_μ′)^2 + u[2]^2)^(3/2)
du[1] = u[3]
du[2] = u[4]
du[3] = u[1] + 2u[4] - threebody_μ′*(u[1]+threebody_μ)/D₁ - threebody_μ*(u[1]-threebody_μ′)/D₂
du[4] = u[2] - 2u[3] - threebody_μ′*u[2]/D₁ - threebody_μ*u[2]/D₂
end
end

t₀ = 0.0; T = parse(Float64,"17.0652165601579625588917206249")
tspan = (t₀,2T)

prob = ODEProblem(f,[0.994, 0.0, 0.0, parse(Float64,"-2.00158510637908252240537862224")],tspan)

test_sol = TestSolution(T,[prob.u0])
abstols = 1.0 ./ 10.0 .^ (3:13); reltols = 1.0 ./ 10.0 .^ (0:10);


See that it's periodic in the chosen timespan:

sol = solve(prob,Vern9(),abstol=1e-14,reltol=1e-14)
@show sol[1] - sol[end]

sol[1] - sol[end] = [-5.52535e-11, -1.64275e-10, -2.68023e-8, -8.60003e-9]

@show sol[end] - prob.u0;

sol[end] - prob.u0 = [5.52535e-11, 1.64275e-10, 2.68023e-8, 8.60003e-9]

apr = appxtrue(sol,test_sol)
@show sol[end]

sol[end] = [0.994, 1.64275e-10, 2.68023e-8, -2.00159]

@show apr.u[end]

apr.u[end] = [0.994, 1.64275e-10, 2.68023e-8, -2.00159]

@show apr.errors

apr.errors = Dict(:final=>8.90547e-9)
Dict{Symbol,Float64} with 1 entry:
:final => 8.90547e-9


This three-body problem is known to be a tough problem. Let's see how the algorithms fair at standard tolerances.

### 5th Order Runge-Kutta Methods

setups = [Dict(:alg=>DP5())
#Dict(:alg=>ode45()) #fails
Dict(:alg=>BS5())
Dict(:alg=>Tsit5())
Dict(:alg=>dopri5())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,save_everystep=false,numruns=100)
plot(wp)


#### Full save, but no dense

setups = [Dict(:alg=>DP5(),:dense=>false)
#Dict(:alg=>ode45()) # Fails
Dict(:alg=>BS5(),:dense=>false)
Dict(:alg=>Tsit5(),:dense=>false)
Dict(:alg=>dopri5())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,numruns=100)
plot(wp)


#### Dense

setups = [Dict(:alg=>DP5())
#Dict(:alg=>ode45()) #fails
Dict(:alg=>BS5())
Dict(:alg=>Tsit5())
Dict(:alg=>dopri5())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,numruns=100)
plot(wp)


In these tests we see that most of the algorithms are close,with BS5 and DP5 showing much better than Tsit5. ode45 errors.

### Higher Order Algorithms

setups = [Dict(:alg=>DP5())
Dict(:alg=>Vern6())
Dict(:alg=>Vern7())
Dict(:alg=>TanYam7())
Dict(:alg=>Vern8())
Dict(:alg=>DP8())
Dict(:alg=>dop853())
Dict(:alg=>Vern9())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,save_everystep=false,numruns=100)
plot(wp)

setups = [Dict(:alg=>DP5())
Dict(:alg=>Vern6())
Dict(:alg=>Vern7())
Dict(:alg=>TanYam7())
Dict(:alg=>Vern8())
Dict(:alg=>DP8())
Dict(:alg=>dop853())
Dict(:alg=>Vern9())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,dense=false,numruns=100,verbose=false)
plot(wp)

setups = [Dict(:alg=>DP5())
Dict(:alg=>Vern6())
Dict(:alg=>Vern7())
Dict(:alg=>TanYam7())
Dict(:alg=>Vern8())
Dict(:alg=>DP8())
Dict(:alg=>dop853())
Dict(:alg=>Vern9())];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,numruns=100)
plot(wp)


In this test we see Vern7 and Vern8 shine.

### Other Algorithms

Once again we separate ODE.jl because it fails. We also separate Sundials' CVODE_Adams since it fails at high tolerances.

#setups = [Dict(:alg=>ode78())
#wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,dense=false,numruns=100)

setups = [Dict(:alg=>DP5())
#Dict(:alg=>lsoda())
Dict(:alg=>Vern8())
Dict(:alg=>ddeabm())
Dict(:alg=>odex())
Dict(:alg=>ARKODE(Sundials.Explicit(),order=6))
];
wp = WorkPrecisionSet(prob,abstols,reltols,setups;appxsol=test_sol,save_everystep=false,numruns=100)
plot(wp)


Again, on cheap function calculations the Adams methods are shown to not be efficient once the error is sufficiently small. Also, as seen in other places, the extrapolation methods do not fare as well as the Runge-Kutta methods.

### Conclusion

As in the other tests, the OrdinaryDiffEq.jl algorithms with the Verner Efficient methods are the most efficient solvers at stringent tolerances for most of the tests, while the order 5 methods do well at cruder tolerances. ODE.jl fails to run the test problems without erroring.

using DiffEqBenchmarks
DiffEqBenchmarks.bench_footer(WEAVE_ARGS[:folder],WEAVE_ARGS[:file])


## Appendix

These benchmarks are a part of the DiffEqBenchmarks.jl repository, found at: https://github.com/JuliaDiffEq/DiffEqBenchmarks.jl

To locally run this tutorial, do the following commands:

using DiffEqBenchmarks
DiffEqBenchmarks.weave_file("NonStiffODE","ThreeBody_wpd.jmd")

Computer Information:

Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, haswell)


Package Information:

Status: /home/crackauckas/.julia/environments/v1.1/Project.toml
[c52e3926-4ff0-5f6e-af25-54175e0327b1] Atom 0.8.7
[bcd4f6db-9728-5f36-b5f7-82caef46ccdb] DelayDiffEq 5.3.0
[bb2cbb15-79fc-5d1e-9bf1-8ae49c7c1650] DiffEqBenchmarks 0.1.0
[459566f4-90b8-5000-8ac3-15dfb0a30def] DiffEqCallbacks 2.5.2
[f3b72e0c-5b89-59e1-b016-84e28bfd966d] DiffEqDevTools 2.8.0
[aae7a2af-3d4f-5e19-a356-7da93b79d9d0] DiffEqFlux 0.5.0
[78ddff82-25fc-5f2b-89aa-309469cbf16f] DiffEqMonteCarlo 0.14.0
[77a26b50-5914-5dd7-bc55-306e6241c503] DiffEqNoiseProcess 3.3.1
[055956cb-9e8b-5191-98cc-73ae4a59e68a] DiffEqPhysics 3.1.0
[a077e3f3-b75c-5d7f-a0c6-6bc4c8ec64a9] DiffEqProblemLibrary 4.1.0
[41bf760c-e81c-5289-8e54-58b1f1f8abe2] DiffEqSensitivity 3.2.2
[0c46a032-eb83-5123-abaf-570d42b7fbaa] DifferentialEquations 6.4.0
[b305315f-e792-5b7a-8f41-49f472929428] Elliptic 0.5.0
[7f56f5a3-f504-529b-bc02-0b1fe5e64312] LSODA 0.4.0
[c030b06c-0b6d-57c2-b091-7029874bd033] ODE 2.4.0
[54ca160b-1b9f-5127-a996-1867f4bc2a2c] ODEInterface 0.4.5
[09606e27-ecf5-54fc-bb29-004bd9f985bf] ODEInterfaceDiffEq 3.3.0
[1dea7af3-3e70-54e6-95c3-0bf5283fa5ed] OrdinaryDiffEq 5.8.1
[2dcacdae-9679-587a-88bb-8b444fb7085b] ParallelDataTransfer 0.5.0
[65888b18-ceab-5e60-b2b9-181511a3b968] ParameterizedFunctions 4.1.1
[91a5bcdd-55d7-5caf-9e0b-520d859cae80] Plots 0.25.1
[d330b81b-6aea-500a-939a-2ce795aea3ee] PyPlot 2.8.1
[731186ca-8d62-57ce-b412-fbd966d074cd] RecursiveArrayTools 0.20.0
[e88e6eb3-aa80-5325-afca-941959d7151f] Zygote 0.3.1