@show VERSION
@time using DifferentialEquations
@time using Plots
@time using Chain
@time using ProgressMeter
VERSION = v"1.7.0-DEV.706" 9.360202 seconds (13.40 M allocations: 1.095 GiB, 2.57% gc time) 6.200569 seconds (7.26 M allocations: 528.094 MiB, 13.20% gc time) 0.010302 seconds (2.33 k allocations: 195.133 KiB) 0.015177 seconds (7.06 k allocations: 521.172 KiB)
Wave equaiton: $\displaystyle \frac{\partial^2 u}{\partial t^2} = \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} $
# the wave equation on the square with free boundary condition
f!(du, v, u, p, t) = @. du = v
function g!(dv, v, u, p, t)
h⁻² = p[1]
@. @views dv[2:end-1, 2:end-1] = h⁻²*(
u[1:end-2,2:end-1] + u[3:end,2:end-1] + u[2:end-1,1:end-2] + u[2:end-1,3:end] - 4u[2:end-1,2:end-1])
@. @views dv[1, 2:end-1] = h⁻²*(u[2, 2:end-1] + u[1, 1:end-2] + u[1, 3:end] - 3u[1, 2:end-1])
@. @views dv[end,2:end-1] = h⁻²*(u[end-1,2:end-1] + u[end,1:end-2] + u[end,3:end] - 3u[end,2:end-1])
@. @views dv[2:end-1,1 ] = h⁻²*(u[2:end-1,2 ] + u[1:end-2,1 ] + u[3:end,1 ] - 3u[2:end-1,1 ])
@. @views dv[2:end-1,end] = h⁻²*(u[2:end-1,end-1] + u[1:end-2,end] + u[3:end,end] - 3u[2:end-1,end])
dv[1, 1 ] = h⁻²*(u[1,2] + u[2,1] - 2u[1,1])
dv[end,1 ] = h⁻²*(u[1,2] + u[2,1] - 2u[1,1])
dv[1, end] = h⁻²*(u[1,2] + u[2,1] - 2u[1,1])
dv[end,end] = h⁻²*(u[1,2] + u[2,1] - 2u[1,1])
end
x = y = range(-1, 1; length=201)
h = step(x)
p = (1/h^2,)
f(x, y) = exp(-(x^2+y^2)/2)
u0 = f.(x', y)
v0 = zero(u0)
tspan = (0.0, 10.0)
@time prob = DynamicalODEProblem(g!, f!, v0, u0, tspan, p)
@time sol = solve(prob)
@time sol = solve(prob)
@time sol = solve(prob);
0.347139 seconds (749.36 k allocations: 67.840 MiB, 7.16% gc time, 96.44% compilation time) 15.037719 seconds (28.63 M allocations: 4.939 GiB, 10.83% gc time) 5.901299 seconds (23.58 k allocations: 3.332 GiB, 19.00% gc time) 6.859141 seconds (23.58 k allocations: 3.332 GiB, 29.44% gc time)
x = y = range(-1, 1; length=201)
h = step(x)
p = (1/h^2,)
f(x, y) = exp(-(x^2+y^2)/2)
u0 = f.(x', y)
v0 = zero(u0)
tspan = (0.0, 20.0)
@time prob = DynamicalODEProblem(g!, f!, v0, u0, tspan, p)
@time sol = solve(prob);
0.000116 seconds (79 allocations: 4.016 KiB) 13.939082 seconds (47.74 k allocations: 6.755 GiB, 23.20% gc time)
umin = minimum(minimum.(u.x[2] for u in sol.u))
umax = maximum(maximum.(u.x[2] for u in sol.u))
umin, umax
(0.28439680508251614, 1.025285466975724)
L = 201
ts = range(tspan...; length=L) |> r -> [fill(r[1], 20); r; fill(r[end], 20)]
prog = Progress(length(ts), 0)
@gif for t in ts
heatmap(x, y, sol(t).x[2]'; clim=(umin, umax), colorbar=false, size=(300, 316), c=:CMRmap)
title!("t = $(round(t, digits=1))"; titlefontsize=12, aspectratio=1)
next!(prog)
end
Progress: 100%|█████████████████████████████████████████| Time: 0:00:55mmmmm9m
┌ Info: Saved animation to
│ fn = C:\Users\genkuroki\OneDrive\Math\Math0052\tmp.gif
└ @ Plots C:\Users\genkuroki\.julia\packages\Plots\z5Msu\src\animation.jl:104
L = 201
ts = range(tspan...; length=L) |> r -> [fill(r[1], 20); r; fill(r[end], 20)]
prog = Progress(length(ts), 0)
@gif for t in ts
surface(x, y, sol(t).x[2]'; zlim=(umin, umax), colorbar=false, c=:CMRmap)
title!("t = $(round(t, digits=1))"; titlefontsize=12)
next!(prog)
end
Progress: 100%|█████████████████████████████████████████| Time: 0:00:25
┌ Info: Saved animation to
│ fn = C:\Users\genkuroki\OneDrive\Math\Math0052\tmp.gif
└ @ Plots C:\Users\genkuroki\.julia\packages\Plots\z5Msu\src\animation.jl:104
g(x, y) = exp(-x^2/2-y^2/4)
u0 = g.(x', y)
v0 = zero(u0)
tspan = (0.0, 20.0)
@time prob = DynamicalODEProblem(g!, f!, v0, u0, tspan, p)
@time sol = solve(prob)
umax = maximum(maximum.(u.x[2] for u in sol.u))
umin = minimum(minimum.(u.x[2] for u in sol.u))
L = 201
ts = range(tspan...; length=L) |> r -> [fill(r[1], 20); r; fill(r[end], 20)]
prog = Progress(length(ts), 0)
@gif for t in ts
surface(x, y, sol(t).x[2]'; zlim=(umin, umax), colorbar=false, c=:CMRmap)
title!("t = $(round(t, digits=1))"; titlefontsize=12)
next!(prog)
end
Progress: 100%|█████████████████████████████████████████| Time: 0:00:29
0.000098 seconds (79 allocations: 4.016 KiB) 18.903642 seconds (48.06 k allocations: 6.801 GiB, 16.91% gc time)
┌ Info: Saved animation to │ fn = C:\Users\genkuroki\OneDrive\Math\Math0052\tmp.gif └ @ Plots C:\Users\genkuroki\.julia\packages\Plots\z5Msu\src\animation.jl:104