Skip to content

Commit 4a5a573

Browse files
committed
add 3D
1 parent 5da0850 commit 4a5a573

16 files changed

+771
-20
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Chmy = "33a72cf0-4690-46d7-b987-06506c2248b9"
1212
[extensions]
1313
ChmyExt1D = ["Chmy", "KernelAbstractions"]
1414
ChmyExt2D = ["Chmy", "KernelAbstractions"]
15+
ChmyExt3D = ["Chmy", "KernelAbstractions"]
1516

1617
[compat]
1718
Chmy = "0.1"

examples/2D/2D_linear_chmy.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function main(;backend=CPU(), nx=400, ny=400, stag=true)
1111
Δx = Lx / nx
1212
Δy = Lx / ny
1313

14-
x = range(0, stop=Lx, length=nx)
15-
1614
grid = UniformGrid(arch; origin=(0.0, 0.0), extent=(Lx, Lx), dims=(nx, ny))
1715

1816
# Courant number

examples/2D/2D_linear_rotation_chmy.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ using KernelAbstractions
44
using Plots
55

66
function main(;backend=CPU(), nx=400, ny=400)
7-
# backend=CPU()
8-
# nx=400
9-
# ny=400
107

118
arch = Arch(backend)
129

1310
Lx = 1.0
1411
Δx = Lx / nx
1512
Δy = Lx / ny
1613

17-
x = range(0, stop=Lx, length=nx)
18-
1914
grid = UniformGrid(arch; origin=(0.0, 0.0), extent=(Lx, Lx), dims=(nx, ny))
2015

2116
# Courant number

examples/3D/3D_linear.jl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using FiniteDiffWENO5
2+
using GLMakie
3+
4+
function main(; nx=50, ny=50, nz=50)
5+
6+
L = 1.0
7+
Δx = L / nx
8+
Δy = L / ny
9+
Δz = L / nz
10+
11+
x = range(0, stop=L, length=nx)
12+
y = range(0, stop=L, length=ny)
13+
z = range(0, stop=L, length=nz)
14+
15+
# Courant number
16+
CFL = 0.7
17+
period = 1
18+
19+
# 3D grid
20+
X = reshape(x, 1, 1, nx) .* ones(ny, nz, 1)
21+
Y = reshape(y, 1, ny, 1) .* ones(nx, 1, nz)
22+
Z = reshape(z, nz, 1, 1) .* ones(1, ny, nx)
23+
24+
vx0 = ones(size(X))
25+
vy0 = ones(size(Y))
26+
vz0 = zeros(size(X)) # Rotation in XY plane only
27+
28+
v = (; x=vx0, y=vy0, z=vz0)
29+
30+
x0 = 1/4
31+
c = 0.08
32+
33+
u0 = zeros(ny, nx, nz)
34+
for I in CartesianIndices((ny, nx, nz))
35+
u0[I] = exp(-((X[I]-x0)^2 + (Y[I]-x0)^2 + (Z[I]-x0)^2) / c^2)
36+
end
37+
38+
u = copy(u0)
39+
weno = WENOScheme(u; boundary=(2, 2, 2, 2, 2, 2), stag=false, multithreading=true)
40+
41+
Δt = CFL * min(Δx, Δy, Δz)^(5/3)
42+
tmax = period * L / max(maximum(abs.(vx0)), maximum(abs.(vy0)), maximum(abs.(vz0)))
43+
t = 0
44+
counter = 0
45+
46+
f = Figure(size = (800, 600))
47+
ax = Axis(f[1, 1], title = "t = $(round(t, digits=2))")
48+
49+
u_obser = Observable(u[:, :, div(nz, 2)])
50+
51+
heatmap!(ax, u_obser, colormap = :viridis)
52+
Colorbar(f[1, 2], label = "u")
53+
display(f)
54+
55+
while t < tmax
56+
WENO_step!(u, v, weno, Δt, Δx, Δy, Δz)
57+
58+
t += Δt
59+
if t + Δt > tmax
60+
Δt = tmax - t
61+
end
62+
63+
if counter % 50 == 0
64+
u_obser[] = u[:, :, div(nz, 2)]
65+
ax.title = "t = $(round(t, digits=2))"
66+
sleep(0.01)
67+
end
68+
69+
counter += 1
70+
end
71+
end
72+
73+
main()
74+

examples/3D/3D_linear_chmy.jl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using FiniteDiffWENO5
2+
using Chmy
3+
using KernelAbstractions
4+
using GLMakie
5+
6+
function main(;backend=CPU(), nx=50, ny=50, nz=50)
7+
8+
arch = Arch(backend)
9+
10+
Lx = 1.0
11+
Δx = Lx / nx
12+
Δy = Lx / ny
13+
Δz = Lx / nz
14+
15+
grid = UniformGrid(arch; origin=(0.0, 0.0, 0.0), extent=(Lx, Lx, Lx), dims=(nx, ny, nz))
16+
17+
# Courant number
18+
CFL = 0.7
19+
period = 1
20+
21+
# 3D grid
22+
x = range(0, length=nx, stop=Lx)
23+
y = range(0, length=ny, stop=Lx)
24+
z = range(0, length=nz, stop=Lx)
25+
26+
X = reshape(x, nx, 1, 1)
27+
Y = reshape(y, 1, ny, 1)
28+
Z = reshape(z, 1, 1, nz)
29+
30+
X3D = X .+ 0 .* Y .+ 0 .* Z
31+
Y3D = 0 .* X .+ Y .+ 0 .* Z
32+
Z3D = 0 .* X .+ 0 .* Y .+ Z
33+
34+
vx0 = ones(size(X3D))
35+
vy0 = ones(size(Y3D))
36+
vz0 = zeros(size(Z3D)) # Rotation in XY plane only
37+
38+
v = (; x=vx0, y=vy0, z=vz0)
39+
40+
x0 = 1/4
41+
c = 0.08
42+
43+
u0 = zeros(ny, nx, nz)
44+
for I in CartesianIndices((ny, nx, nz))
45+
u0[I] = exp(-((X3D[I]-x0)^2 + (Y3D[I]-x0)^2 + (Z3D[I]-x0)^2) / c^2)
46+
end
47+
48+
u = copy(u0)
49+
weno = WENOScheme(u; boundary=(2, 2, 2, 2, 2, 2), stag=false, multithreading=true)
50+
51+
Δt = CFL * min(Δx, Δy, Δz)^(5/3)
52+
tmax = period * Lx / max(maximum(abs.(vx0)), maximum(abs.(vy0)), maximum(abs.(vz0)))
53+
t = 0
54+
counter = 0
55+
56+
f = Figure(size = (800, 600))
57+
ax = Axis(f[1, 1], title = "t = $(round(t, digits=2))")
58+
59+
u_obser = Observable(u[:, :, div(nz, 2)])
60+
61+
heatmap!(ax, u_obser, colormap = :viridis)
62+
Colorbar(f[1, 2], label = "u")
63+
display(f)
64+
65+
while t < tmax
66+
WENO_step!(u, v, weno, Δt, Δx, Δy, Δz)
67+
68+
t += Δt
69+
if t + Δt > tmax
70+
Δt = tmax - t
71+
end
72+
73+
if counter % 10 == 0
74+
u_obser[] = u[:, :, div(nz, 2)]
75+
ax.title = "t = $(round(t, digits=2))"
76+
sleep(0.01)
77+
end
78+
79+
counter += 1
80+
end
81+
82+
return u
83+
end
84+
85+
u = main()
86+

ext/ChmyExt1D.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ module ChmyExt1D
112112
"""
113113
WENO_step!(u::T_field, v::NamedTuple{names, <:Tuple{<:T_field}}, weno::FiniteDiffWENO5.WENOScheme, Δt, Δx, grid::StructuredGrid, arch) where T_field <: AbstractField{<:Real} where names
114114
115-
Advance the solution `u` by one time step using the 3rd-order Runge-Kutta method with WENO5 spatial discretization using Chmy.jl fields.
115+
Advance the solution `u` by one time step using the 3rd-order Runge-Kutta method with WENO5 spatial discretization using Chmy.jl fields in 1D.
116116
117117
# Arguments
118118
- `u::T_field`: The current solution field to be updated in place.
119-
- `v::NamedTuple{names, <:Tuple{<:T_field}}`: The velocity field (can be staggered or not based on `weno.stag`).
119+
- `v::NamedTuple{names, <:Tuple{<:T_field}}`: The velocity field (can be staggered or not based on `weno.stag`). Needs to be a NamedTuple with field `:x`.
120120
- `weno::WENOScheme`: The WENO scheme structure containing necessary parameters and fields.
121121
- `Δt`: The time step size.
122122
- `Δx`: The spatial grid size.

ext/ChmyExt2D.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ module ChmyExt2D
140140
end
141141

142142
"""
143-
WENO_step!(u::T_field, v::NamedTuple{names, <:Tuple{<:T_field}}, weno::FiniteDiffWENO5.WENOScheme, Δt, Δx, grid::StructuredGrid, arch) where T_field <: AbstractField{<:Real} where names
143+
WENO_step!(u::T_field, v, weno::FiniteDiffWENO5.WENOScheme, Δt, Δx, grid::StructuredGrid, arch) where T_field <: AbstractField{<:Real} where names
144144
145-
Advance the solution `u` by one time step using the 3rd-order Runge-Kutta method with WENO5 spatial discretization using Chmy.jl fields.
145+
Advance the solution `u` by one time step using the 3rd-order Runge-Kutta method with WENO5 spatial discretization using Chmy.jl fields in 2D.
146146
147147
# Arguments
148148
- `u::T_field`: The current solution field to be updated in place.
149-
- `v::NamedTuple{names, <:Tuple{<:T_field}}`: The velocity field (can be staggered or not based on `weno.stag`).
149+
- `v::NamedTuple{names, <:Tuple{<:T_field}}`: The velocity field (can be staggered or not based on `weno.stag`). Needs to be a NamedTuple with fields `:x` and `:y`.
150150
- `weno::WENOScheme`: The WENO scheme structure containing necessary parameters and fields.
151151
- `Δt`: The time step size.
152152
- `Δx`: The spatial grid size.

0 commit comments

Comments
 (0)