Skip to content

Commit 526714e

Browse files
committed
2D first draft
1 parent 6302f1e commit 526714e

File tree

12 files changed

+344
-52
lines changed

12 files changed

+344
-52
lines changed
Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ function main(nx=400)
4646

4747

4848
u = copy(u0_vec)
49-
weno = WENOScheme(u; boundary=(2, 2), stag=true)
49+
weno = WENOScheme(u; boundary=(2, 2), stag=true, multithreading=false)
5050

5151
# advection velocity
52-
a = ones(nx+1) .* 1
52+
a = (;x=ones(nx+1))
5353

5454
# grid size
5555
Δx = x[2] - x[1]
5656
Δt = CFL*Δx^(5/3)
5757

58-
tmax = period * (Lx + Δx) / maximum(abs.(a))
58+
tmax = period * (Lx + Δx) / maximum(abs.(a.x))
5959

6060
t = 0
6161

@@ -76,4 +76,65 @@ function main(nx=400)
7676
display(f)
7777
end
7878

79-
main()
79+
# main()
80+
81+
82+
nx = 400
83+
84+
x_min = -1.0
85+
x_max = 1.0
86+
Lx = x_max - x_min
87+
88+
x = range(x_min, stop=x_max, length=nx)
89+
90+
# Courant number
91+
CFL = 0.4
92+
period = 4
93+
94+
# Parameters for Shu test
95+
z = -0.7
96+
δ = 0.005
97+
β = log(2)/(36*δ^2)
98+
a = 0.5
99+
α = 10
100+
101+
# Functions
102+
G(x, β, z) = exp.(-β .* (x .- z).^2)
103+
F(x, α, a) = sqrt.(max.(1 .- α^2 .* (x .- a).^2, 0.0))
104+
105+
# Grid x assumed defined
106+
u0_vec = zeros(length(x))
107+
108+
# Gaussian-like smooth bump at x in [-0.8, -0.6]
109+
idx = (x .>= -0.8) .& (x .<= -0.6)
110+
u0_vec[idx] .= (1/6) .* (G(x[idx], β, z - δ) .+ 4 .* G(x[idx], β, z) .+ G(x[idx], β, z + δ))
111+
112+
# Heaviside step at x in [-0.4, -0.2]
113+
idx = (x .>= -0.4) .& (x .<= -0.2)
114+
u0_vec[idx] .= 1.0
115+
116+
# Piecewise linear ramp at x in [0, 0.2]
117+
# Triangular spike at x=0.1, base width 0.2
118+
idx = abs.(x .- 0.1) .<= 0.1
119+
u0_vec[idx] .= 1 .- 10 .* abs.(x[idx] .- 0.1)
120+
121+
# Elliptic/smooth bell at x in [0.4, 0.6]
122+
idx = (x .>= 0.4) .& (x .<= 0.6)
123+
u0_vec[idx] .= (1/6) .* (F(x[idx], α, a - δ) .+ 4 .* F(x[idx], α, a) .+ F(x[idx], α, a + δ))
124+
125+
126+
u = copy(u0_vec)
127+
weno = WENOScheme(u; boundary=(2, 2), stag=true, multithreading=false)
128+
129+
# advection velocity
130+
a = (;x=ones(nx+1))
131+
132+
# grid size
133+
Δx = x[2] - x[1]
134+
Δt = CFL*Δx^(5/3)
135+
136+
tmax = period * (Lx + Δx) / maximum(abs.(a.x))
137+
138+
t = 0
139+
@btime WENO_step!($u, $a, $weno, $Δt, $Δx)
140+
# 9.583 μs (0 allocations: 0 bytes)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ function main(backend=CPU(), nx=200)
7878
end
7979
end
8080

81+
8182
f = plot(x, u0_vec, label="Exact", linestyle=:dash)
82-
scatter!(f, x, u, label="WENO5", title="1D Linear Advection after $period periods", xlabel="x", ylabel="u", legend=:topright)
83+
scatter!(f, x, u, label="WENO5", title="1D linear advection after $period periods", xlabel="x", ylabel="u", legend=:topright, markersize=2)
84+
# save fig
85+
8386
display(f)
8487
end
8588

examples/2D/2D_linear.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using FiniteDiffWENO5
2+
using Plots
3+
4+
function main(;nx=400, ny=400)
5+
6+
Lx = 1.0
7+
Δx = Lx / nx
8+
Δy = Lx / ny
9+
10+
x = range(0, stop=Lx, length=nx)
11+
12+
# Courant number
13+
CFL = 0.7
14+
period = 1
15+
16+
# Grid x assumed defined
17+
x = range(0, length=nx, stop= Lx)
18+
y = range(0, length=ny, stop= Lx)
19+
grid = (x .* ones(ny)', ones(nx) .* y')
20+
21+
w = π
22+
vx0 = w .* (grid[1] .- Lx/2)
23+
vy0 = -w .* (grid[2] .- Lx/2)
24+
25+
v = (;x=vy0, y=vx0)
26+
27+
x0 = 1/4
28+
c = 0.08
29+
30+
u0 = zeros(ny, nx)
31+
32+
for I in CartesianIndices((ny, nx))
33+
u0[I] = sign(exp(-((grid[1][I] - x0)^2 + (grid[2][I]' - x0)^2) / c^2) - 0.5) * 0.5 + 0.5
34+
end
35+
36+
u = copy(u0)
37+
weno = WENOScheme(u; boundary=(2, 2, 2, 2), stag=false, multithreading=true)
38+
39+
40+
# grid size
41+
Δt = CFL*min(Δx, Δy)^(5/3)
42+
43+
tmax = period / (w/(2*π))
44+
45+
t = 0
46+
counter = 0
47+
48+
while t < tmax
49+
WENO_step!(u, v, weno, Δt, Δx, Δy)
50+
51+
52+
t += Δt
53+
54+
if t + Δt > tmax
55+
Δt = tmax - t
56+
end
57+
58+
if counter % 100 == 0
59+
heatmap(x, y, u, title="t = $(round(t, digits=2))", clims=(0,1))
60+
gui()
61+
end
62+
63+
counter += 1
64+
65+
end
66+
67+
end
68+
69+
main(nx=400, ny=400)

ext/ChmyExt1D.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module ChmyExt1D
105105
min(v.x[i], 0) * fr.x[i]
106106
) * Δx_
107107
else
108-
du[i] = @muladd max(v[i], 0) * (fl.x[i+1] - fl.x[i]) * Δx_ + min(v[i], 0) * (fr.x[i+1] - fr.x[i]) * Δx_
108+
du[i] = @muladd max(v.x[i], 0) * (fl.x[i+1] - fl.x[i]) * Δx_ + min(v.x[i], 0) * (fr.x[i+1] - fr.x[i]) * Δx_
109109
end
110110
end
111111

@@ -122,7 +122,7 @@ module ChmyExt1D
122122
- `Δx`: The spatial grid size.
123123
- `grid::StructuredGrid`: The computational grid.
124124
"""
125-
function WENO_step!(u::T_field, v::NamedTuple{names, <:Tuple{<:T_field}}, weno::FiniteDiffWENO5.WENOScheme, Δt, Δx, grid::StructuredGrid, arch) where T_field <: AbstractVector{<:Real} where names
125+
function WENO_step!(u::T_field, v::NamedTuple{(:x,), <:Tuple{<:T_field}}, weno::FiniteDiffWENO5.WENOScheme, Δt, Δx, grid::StructuredGrid, arch) where T_field <: AbstractVector{<:Real}
126126

127127
backend = get_backend(u)
128128

src/1D/semi_discretisation_1D.jl

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11

22

3-
@inline function left_index(i, d, nx, ::Val{0})
4-
# Dirichlet (clamped to domain)
5-
return clamp(i - d, 1, nx)
6-
end
7-
8-
@inline function left_index(i, d, nx, ::Val{1})
9-
# Neumann (mirror the boundary value)
10-
return max(i - d, 1)
11-
end
12-
13-
@inline function left_index(i, d, nx, ::Val{2})
14-
# Periodic (wrap around)
15-
return mod1(i - d, nx)
16-
end
17-
18-
@inline function right_index(i, d, nx, ::Val{0})
19-
return clamp(i + d, 1, nx) # Dirichlet
20-
end
21-
22-
@inline function right_index(i, d, nx, ::Val{1})
23-
return min(i + d, nx) # Neumann
24-
end
25-
26-
@inline function right_index(i, d, nx, ::Val{2})
27-
return mod1(i + d, nx) # Periodic
28-
end
29-
303
function WENO_flux!(fl, fr, u, weno, nx)
31-
@unpack boundary, χ, γ, ζ, ϵ = weno
4+
@unpack boundary, χ, γ, ζ, ϵ, multithreading = weno
325

336
bL = Val(boundary[1])
347
bR = Val(boundary[2])
358

36-
@inbounds for i in 1:nx+1
9+
@inbounds @maybe_threads multithreading for i in axes(fl.x,1)
3710
iwww = left_index(i, 3, nx, bL)
3811
iww = left_index(i, 2, nx, bL)
3912
iw = left_index(i, 1, nx, bL)
@@ -56,21 +29,21 @@ end
5629

5730
function semi_discretisation_weno5!(du::T, v, weno::WENOScheme, Δx_) where T <: AbstractArray{<:Real, 1}
5831

59-
@unpack fl, fr, stag = weno
32+
@unpack fl, fr, stag, multithreading = weno
6033

6134
# use staggered grid or not for the velocities
6235
if stag
63-
@inbounds for i = axes(du, 1)
36+
@inbounds @maybe_threads multithreading for i = axes(du, 1)
6437
du[i] = @muladd (
65-
max(v[i+1], 0) * fl.x[i+1] +
66-
min(v[i+1], 0) * fr.x[i+1] -
67-
max(v[i], 0) * fl.x[i] -
68-
min(v[i], 0) * fr.x[i]
38+
max(v.x[i+1], 0) * fl.x[i+1] +
39+
min(v.x[i+1], 0) * fr.x[i+1] -
40+
max(v.x[i], 0) * fl.x[i] -
41+
min(v.x[i], 0) * fr.x[i]
6942
) * Δx_
7043
end
7144
else
72-
@inbounds for i = axes(du, 1)
73-
du[i] = @muladd max(v[i], 0) * (fl.x[i+1] - fl.x[i]) * Δx_ + min(v[i], 0) * (fr.x[i+1] - fr.x[i]) * Δx_
45+
@inbounds @maybe_threads multithreading for i = axes(du, 1)
46+
du[i] = @muladd max(v.x[i], 0) * (fl.x[i+1] - fl.x[i]) * Δx_ + min(v.x[i], 0) * (fr.x[i+1] - fr.x[i]) * Δx_
7447
end
7548
end
7649
end

src/1D/time_stepping.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ function WENO_step!(u::T, v, weno::WENOScheme, Δt, Δx) where T <: AbstractVect
1616
nx = size(u, 1)
1717
Δx_ = inv(Δx)
1818

19-
@unpack ut, du, stag, fl, fr = weno
19+
@unpack ut, du, stag, fl, fr, multithreading = weno
2020

2121
WENO_flux!(fl, fr, u, weno, nx)
2222
semi_discretisation_weno5!(du, v, weno, Δx_)
2323

24-
@inbounds for i = axes(ut, 1)
24+
@inbounds @maybe_threads multithreading for i = axes(ut, 1)
2525
ut[i] = @muladd u[i] - Δt * du[i]
2626
end
2727

2828
WENO_flux!(fl, fr, ut, weno, nx)
2929
semi_discretisation_weno5!(du, v, weno, Δx_)
3030

31-
@inbounds for i = axes(ut, 1)
31+
@inbounds @maybe_threads multithreading for i = axes(ut, 1)
3232
ut[i] = @muladd 0.75 * u[i] + 0.25 * ut[i] - 0.25 * Δt * du[i]
3333
end
3434

3535
WENO_flux!(fl, fr, ut, weno, nx)
3636
semi_discretisation_weno5!(du, v, weno, Δx_)
3737

38-
@inbounds for i = axes(u, 1)
38+
@inbounds @maybe_threads multithreading for i = axes(u, 1)
3939
u[i] = @muladd 1.0/3.0 * u[i] + 2.0/3.0 * ut[i] - (2.0/3.0) * Δt * du[i]
4040
end
4141

0 commit comments

Comments
 (0)