|
| 1 | +#!/usr/bin/julia -- |
| 2 | + |
| 3 | +import DifferentialEquations |
| 4 | +import PyPlot |
| 5 | +const DE = DifferentialEquations |
| 6 | +const plt = PyPlot |
| 7 | +include("L96m.jl") |
| 8 | + |
| 9 | + |
| 10 | +################################################################################ |
| 11 | +# constants section ############################################################ |
| 12 | +################################################################################ |
| 13 | +const RPAD = 42 |
| 14 | +const LPAD_INTEGER = 7 |
| 15 | +const LPAD_FLOAT = 13 |
| 16 | +const SOLVER = DE.Tsit5() |
| 17 | + |
| 18 | +const T = 4 # integration time |
| 19 | +const T_compile = 1e-10 # force JIT compilation |
| 20 | +const T_conv = 100 # converging integration time |
| 21 | +#const T_learn = 15 # time to gather training data for GP |
| 22 | +#const T_hist = 10000 # time to gather histogram statistics |
| 23 | + |
| 24 | +# integrator parameters |
| 25 | +const dtmax = 1e-3 # maximum step size |
| 26 | +#const tau = 1e-3 # maximum step size for histogram statistics |
| 27 | +#const dt_conv = 0.01 # maximum step size for converging to attractor |
| 28 | +const reltol = 1e-3 # relative tolerance |
| 29 | +const abstol = 1e-6 # absolute tolerance |
| 30 | + |
| 31 | +const k = 1 # index of the slow variable to save etc. |
| 32 | +const j = 1 # index of the fast variable to save/plot etc. |
| 33 | + |
| 34 | +const hx = -0.8 |
| 35 | + |
| 36 | +################################################################################ |
| 37 | +# IC section ################################################################### |
| 38 | +################################################################################ |
| 39 | +l96 = L96m(hx = hx, J = 8) |
| 40 | + |
| 41 | +z00 = Array{Float64}(undef, l96.K + l96.K * l96.J) |
| 42 | + |
| 43 | +# method 1 |
| 44 | +z00[1:l96.K] .= rand(l96.K) * 15 .- 5 |
| 45 | +for k_ in 1:l96.K |
| 46 | + z00[l96.K+1 + (k_-1)*l96.J : l96.K + k_*l96.J] .= z00[k_] |
| 47 | +end |
| 48 | + |
| 49 | +################################################################################ |
| 50 | +# main section ################################################################# |
| 51 | +################################################################################ |
| 52 | + |
| 53 | +# force compilation of functions used in numerical integration |
| 54 | +print(rpad("(JIT compilation)", RPAD)) |
| 55 | +elapsed_jit = @elapsed begin |
| 56 | + pb_jit = DE.ODEProblem(full, z00, (0.0, T_compile), l96) |
| 57 | + DE.solve(pb_jit, SOLVER, reltol = reltol, abstol = abstol, dtmax = dtmax) |
| 58 | + pb_jit = DE.ODEProblem(balanced, z00[1:l96.K], (0.0, T_compile), l96) |
| 59 | + DE.solve(pb_jit, SOLVER, reltol = reltol, abstol = abstol, dtmax = dtmax) |
| 60 | +end |
| 61 | +println(" " ^ (LPAD_INTEGER + 6), |
| 62 | + "\t\telapsed:", lpad(elapsed_jit, LPAD_FLOAT)) |
| 63 | + |
| 64 | +# full L96m integration (converging to attractor) |
| 65 | +print(rpad("(full, converging)", RPAD)) |
| 66 | +elapsed_conv = @elapsed begin |
| 67 | + pb_conv = DE.ODEProblem(full, z00, (0.0, T_conv), l96) |
| 68 | + sol_conv = DE.solve(pb_conv, |
| 69 | + SOLVER, |
| 70 | + reltol = reltol, |
| 71 | + abstol = abstol, |
| 72 | + dtmax = dtmax |
| 73 | + ) |
| 74 | +end |
| 75 | +println("steps:", lpad(length(sol_conv.t), LPAD_INTEGER), |
| 76 | + "\t\telapsed:", lpad(elapsed_conv, LPAD_FLOAT)) |
| 77 | +z0 = sol_conv[:,end] |
| 78 | + |
| 79 | +# full L96m integration |
| 80 | +print(rpad("(full)", RPAD)) |
| 81 | +elapsed_dns = @elapsed begin |
| 82 | + pb_dns = DE.ODEProblem(full, z0, (0.0, T), l96) |
| 83 | + sol_dns = DE.solve(pb_dns, |
| 84 | + SOLVER, |
| 85 | + reltol = reltol, |
| 86 | + abstol = abstol, |
| 87 | + dtmax = dtmax |
| 88 | + ) |
| 89 | +end |
| 90 | +println("steps:", lpad(length(sol_dns.t), LPAD_INTEGER), |
| 91 | + "\t\telapsed:", lpad(elapsed_dns, LPAD_FLOAT)) |
| 92 | + |
| 93 | +# balanced L96m integration |
| 94 | +print(rpad("(balanced)", RPAD)) |
| 95 | +elapsed_bal = @elapsed begin |
| 96 | + pb_bal = DE.ODEProblem(balanced, z0[1:l96.K], (0.0, T), l96) |
| 97 | + sol_bal = DE.solve(pb_bal, |
| 98 | + SOLVER, |
| 99 | + reltol = reltol, |
| 100 | + abstol = abstol, |
| 101 | + dtmax = dtmax |
| 102 | + ) |
| 103 | +end |
| 104 | +println("steps:", lpad(length(sol_bal.t), LPAD_INTEGER), |
| 105 | + "\t\telapsed:", lpad(elapsed_bal, LPAD_FLOAT)) |
| 106 | + |
| 107 | +################################################################################ |
| 108 | +# plot section ################################################################# |
| 109 | +################################################################################ |
| 110 | +# plot DNS |
| 111 | +plt.plot(sol_dns.t, sol_dns[k,:], label = "DNS") |
| 112 | +plt.plot(sol_dns.t, sol_dns[l96.K + (k-1)*l96.J + j,:], |
| 113 | + lw = 0.6, alpha = 0.6, color="gray") |
| 114 | + |
| 115 | +# plot balanced |
| 116 | +plt.plot(sol_bal.t, sol_bal[k,:], label = "balanced") |
| 117 | + |
| 118 | +plt.legend() |
| 119 | +plt.show() |
| 120 | + |
| 121 | + |
0 commit comments