diff --git a/docs/examples/features/demo_hybrid.jl b/docs/examples/features/demo_hybrid.jl index 104df29aa..1e6bbd923 100644 --- a/docs/examples/features/demo_hybrid.jl +++ b/docs/examples/features/demo_hybrid.jl @@ -11,7 +11,7 @@ import DisplayAs #hide using TestParticle, OrdinaryDiffEq, StaticArrays import TestParticle as TP -using LinearAlgebra, Random +using LinearAlgebra, Random, Printf, Markdown using CairoMakie CairoMakie.activate!(type = "png") #hide @@ -62,7 +62,8 @@ tspan = (0.0, 30 * T_gyro) param_fo = prepare(E_field, B_field; species = Proton) prob_fo = ODEProblem(trace, u0, tspan, param_fo) -sol_fo = solve(prob_fo, Vern9()) +stats_fo = @timed solve(prob_fo, Vern9(); save_everystep = true) +sol_fo = stats_fo.value; ## Verify trapping: z should oscillate, not diverge z_fo = [u[3] for u in sol_fo.u] @@ -79,7 +80,8 @@ stateinit_gc, param_gc = TP.prepare_gc( u0, bottle_E_static, bottle_B_static; species = Proton ) prob_gc = ODEProblem(trace_gc!, stateinit_gc, tspan, param_gc) -sol_gc = solve(prob_gc, Vern9()); +stats_gc = @timed solve(prob_gc, Vern9()) +sol_gc = stats_gc.value; # ## Step 3: Hybrid Solver # @@ -100,7 +102,8 @@ alg = AdaptiveHybrid(; Random.seed!(1234) ## Set verbose = true to see the dynamic switching -sol = TP.solve(TraceHybridProblem(u0, tspan, p), alg; verbose = false)[1]; +stats_hybrid = @timed TP.solve(TraceHybridProblem(u0, tspan, p), alg; verbose = false) +sol = stats_hybrid.value[1]; # ## Step 4: Compute Adiabaticity # @@ -251,3 +254,15 @@ Legend(f[3, 1:4], ax_ts; orientation = :horizontal) rowsize!(f.layout, 1, Relative(0.55)) f = DisplayAs.PNG(f) #hide + +# ## Performance Comparison +# +# Finally, we compare the execution time and memory allocations of the three solvers. + +io = IOBuffer() #hide +println(io, "| Solver | Time (s) | Allocations (bytes) |") #hide +println(io, "| :--- | :--- | :--- |") #hide +Printf.@printf(io, "| Full Orbit | %.2e | %.2e |\n", stats_fo.time, stats_fo.bytes) #hide +Printf.@printf(io, "| Guiding Center | %.2e | %.2e |\n", stats_gc.time, stats_gc.bytes) #hide +Printf.@printf(io, "| Hybrid | %.2e | %.2e |\n", stats_hybrid.time, stats_hybrid.bytes) #hide +Markdown.parse(String(take!(io))) #hide