|
| 1 | +# # Implicit time-integration |
| 2 | + |
| 3 | +# ## Necessary packages |
| 4 | +using NewtonKrylov |
| 5 | +using CairoMakie |
| 6 | +using OffsetArrays |
| 7 | + |
| 8 | +include(joinpath(dirname(pathof(NewtonKrylov)), "..", "examples", "implicit.jl")) |
| 9 | +include(joinpath(dirname(pathof(NewtonKrylov)), "..", "examples", "halovector.jl")) |
| 10 | + |
| 11 | +# ## Diffusion 2D |
| 12 | + |
| 13 | +# ### Boundary |
| 14 | + |
| 15 | +function bc_periodic!(u) |
| 16 | + N, M = size(u) |
| 17 | + N = N - 2 |
| 18 | + M = M - 2 |
| 19 | + |
| 20 | + ## (wrap around) |
| 21 | + u[0, :] .= u[N, :] |
| 22 | + u[N + 1, :] .= u[1, :] |
| 23 | + u[:, 0] .= u[:, N] |
| 24 | + u[:, N + 1] .= u[:, 1] |
| 25 | + return nothing |
| 26 | +end |
| 27 | + |
| 28 | +function bc_zero!(u) |
| 29 | + N, M = size(u) |
| 30 | + N = N - 2 |
| 31 | + M = M - 2 |
| 32 | + |
| 33 | + u[0, :] .= 0 |
| 34 | + u[N + 1, :] .= 0 |
| 35 | + u[:, 0] .= 0 |
| 36 | + u[:, N + 1] .= 0 |
| 37 | + return nothing |
| 38 | +end |
| 39 | + |
| 40 | + |
| 41 | +function diffusion!(du::HaloVector, u::HaloVector, p, t) |
| 42 | + return diffusion!(du.data, u.data, p, t) |
| 43 | +end |
| 44 | + |
| 45 | +function diffusion!(du, u, (a, Δx, Δy, bc!), _) |
| 46 | + N, M = size(u) |
| 47 | + N = N - 2 |
| 48 | + M = M - 2 |
| 49 | + |
| 50 | + ## Enforce boundary conditions |
| 51 | + bc!(u) |
| 52 | + |
| 53 | + for i in 1:N |
| 54 | + for j in 1:M |
| 55 | + du[i, j] = a * ( |
| 56 | + (u[i + 1, j] - 2 * u[i, j] + u[i - 1, j]) / Δx^2 + |
| 57 | + (u[i, j + 1] - 2 * u[i, j] + u[i, j - 1]) / Δy^2 |
| 58 | + ) |
| 59 | + end |
| 60 | + end |
| 61 | + return |
| 62 | +end |
| 63 | + |
| 64 | +a = 0.01 |
| 65 | + |
| 66 | +N = M = 40 |
| 67 | + |
| 68 | +Δx = 1 / (N + 1) # x-grid spacing |
| 69 | +Δy = 1 / (M + 1) # y-grid spacing |
| 70 | + |
| 71 | +## TODO: This is a time-step from explicit |
| 72 | +Δt = Δx^2 * Δy^2 / (2.0 * a * (Δx^2 + Δy^2)) # Largest stable time step |
| 73 | + |
| 74 | +let |
| 75 | + N = M = 12 |
| 76 | + u₀ = HaloVector(OffsetArray(zeros(N + 2, M + 2), 0:(N + 1), 0:(M + 1))) |
| 77 | + jacobian(G_Euler!, diffusion!, u₀, (a, Δx, Δy, bc_zero!), Δt, 0.0) |
| 78 | +end |
| 79 | + |
| 80 | +xs = 0.0:Δx:1.0 |
| 81 | +ys = 0.0:Δy:1.0 |
| 82 | + |
| 83 | +function f(x, y) |
| 84 | + return sin(π * x) .* sin(π * y) |
| 85 | +end |
| 86 | + |
| 87 | +u₀ = let |
| 88 | + _u₀ = zeros(N + 2, M + 2) |
| 89 | + _u₀ .= f.(xs, ys') |
| 90 | + HaloVector(OffsetArray(_u₀, 0:(N + 1), 0:(M + 1))) |
| 91 | +end |
| 92 | + |
| 93 | +function plot_state(t, u, xs, ys) |
| 94 | + data = @lift $u.data.parent |
| 95 | + |
| 96 | + xs_line = @lift @view $data[:, M ÷ 2] |
| 97 | + ys_line = @lift @view $data[N ÷ 2, :] |
| 98 | + |
| 99 | + fig = Figure() |
| 100 | + |
| 101 | + ax = Axis(fig[1, 1]) |
| 102 | + heatmap!(ax, xs, ys, data) |
| 103 | + |
| 104 | + ax1 = Axis(fig[2, 1]) |
| 105 | + lines!(ax1, xs, xs_line) |
| 106 | + |
| 107 | + ax2 = Axis(fig[2, 2]) |
| 108 | + lines!(ax2, ys, ys_line) |
| 109 | + |
| 110 | + fig[0, :] = Label(fig, @lift("t = $($t)")) |
| 111 | + |
| 112 | + return fig |
| 113 | +end |
| 114 | + |
| 115 | +fig = plot_state(Observable(0.0), Observable(u₀), xs, ys) |
| 116 | + |
| 117 | +function create_video_implicit(filename, G!, f!, xs, ys, u, p, Δt, t_stop, frame_kwargs) |
| 118 | + ts = 0.0:Δt:t_stop |
| 119 | + _u = Observable(u) |
| 120 | + _t = Observable(first(ts)) |
| 121 | + |
| 122 | + fig = plot_state(_t, _u, xs, ys) |
| 123 | + return record(fig, filename; frame_kwargs...) do io |
| 124 | + function callback(__u) |
| 125 | + _u[] = u |
| 126 | + _t[] += Δt |
| 127 | + ## autolimits!(ax) # update limits |
| 128 | + recordframe!(io) |
| 129 | + return yield() |
| 130 | + end |
| 131 | + solve(G!, f!, u, p, Δt, ts; callback, verbose = 1, krylov_kwargs = (; verbose = 1, reorthogonalization = true)) |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +create_video_implicit( |
| 136 | + joinpath(@__DIR__, "implicit_euler.mp4"), |
| 137 | + G_Euler!, diffusion!, xs, ys, copy(u₀), (a, Δx, Δy, bc_zero!), Δt, 10.0, (; framerate = 30) |
| 138 | +) |
| 139 | + |
| 140 | +create_video_implicit( |
| 141 | + joinpath(@__DIR__, "implicit_midpoint.mp4"), |
| 142 | + G_Midpoint!, diffusion!, xs, ys, copy(u₀), (a, Δx, Δy, bc_zero!), Δt, 10.0, (; framerate = 30) |
| 143 | +) |
| 144 | + |
| 145 | +create_video_implicit( |
| 146 | + joinpath(@__DIR__, "implicit_trapezoid.mp4"), |
| 147 | + G_Trapezoid!, diffusion!, xs, ys, copy(u₀), (a, Δx, Δy, bc_zero!), Δt, 10.0, (; framerate = 30) |
| 148 | +) |
| 149 | + |
| 150 | +## TODO: |
| 151 | +create_video_implicit( |
| 152 | + joinpath(@__DIR__, "implicit_euler_periodic.mp4"), |
| 153 | + G_Trapezoid!, diffusion!, xs, ys, copy(u₀), (a, Δx, Δy, bc_periodic!), Δt, 2 * Δt, (; framerate = 30) |
| 154 | +) |
0 commit comments