forked from agdestein/IncompressibleNavierStokes.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RayleighBenard2D.jl
160 lines (139 loc) · 3.91 KB
/
RayleighBenard2D.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# # Rayleigh-Bénard convection (2D)
#
# A hot and a cold plate generate a convection cell in a box.
#md using CairoMakie
using GLMakie #!md
using IncompressibleNavierStokes
# Output directory for saving results
outdir = joinpath(@__DIR__, "output", "RayleighBenard2D")
# Hardware
backend = CPU()
## using CUDA, CUDSS
## backend = CUDABackend()
# Define observer function to track Nusselt numbers
# on top and bottom plates.
function nusseltplot(state; setup)
state isa Observable || (state = Observable(state))
(; Δ, Δu) = setup.grid
Δy1 = Δu[2][1:1] |> sum
Δy2 = Δu[2][end-1:end-1] |> sum
## Observe Nusselt numbers
Nu1 = Observable(Point2f[])
Nu2 = Observable(Point2f[])
on(state) do (; temp, t)
dTdy = @. (temp[:, 2] - temp[:, 1]) / Δy1
Nu = sum((.-dTdy.*Δ[1])[2:end-1])
push!(Nu1[], Point2f(t, Nu))
dTdy = @. (temp[:, end-1] - temp[:, end-2]) / Δy2
Nu = sum((.-dTdy.*Δ[1])[2:end-1])
push!(Nu2[], Point2f(t, Nu))
(Nu1, Nu2) .|> notify ## Update plot
end
## Plot Nu history
fig = Figure()
ax = Axis(fig[1, 1]; title = "Nusselt number", xlabel = "t", ylabel = "Nu")
lines!(ax, Nu1; label = "Lower plate")
lines!(ax, Nu2; label = "Upper plate")
axislegend(ax)
on(_ -> autolimits!(ax), Nu2)
fig
end
# Define observer function to track average temperature.
function averagetemp(state; setup)
state isa Observable || (state = Observable(state))
(; xp, Δ, Ip) = setup.grid
ix = Ip.indices[1]
Ty = lift(state) do (; temp)
Ty = sum(temp[ix, :] .* Δ[1][ix]; dims = 1) ./ sum(Δ[1][ix])
Array(Ty)[:]
end
Ty0 = copy(Ty[])
yy = Array(xp[2])
fig = Figure()
ax = Axis(fig[1, 1]; title = "Average temperature", xlabel = "T", ylabel = "y")
lines!(ax, Ty0, yy; label = "t = 0")
lines!(ax, Ty, yy; label = "t = t")
axislegend(ax)
on(_ -> autolimits!(ax), Ty)
fig
end
# Instabilities should depend on the floating point precision.
# Try both `Float32` and `Float64`.
T = Float32
# Temperature equation setup.
temperature = temperature_equation(;
Pr = T(0.71),
Ra = T(1e7),
Ge = T(1.0),
dodissipation = true,
boundary_conditions = (
(SymmetricBC(), SymmetricBC()),
(DirichletBC(T(1)), DirichletBC(T(0))),
),
gdir = 2,
nondim_type = 1,
)
# Grid
n = 100
x = tanh_grid(T(0), T(2), 2n, T(1.2)), tanh_grid(T(0), T(1), n, T(1.2))
plotgrid(x...)
# Setup
setup = Setup(;
x,
boundary_conditions = ((DirichletBC(), DirichletBC()), (DirichletBC(), DirichletBC())),
Re = 1 / temperature.α1,
temperature,
backend,
);
# Initial conditions
ustart = velocityfield(setup, (dim, x, y) -> zero(x));
tempstart = temperaturefield(setup, (x, y) -> one(y) / 2 + max(sinpi(20 * x) / 100, 0));
# Processors
GLMakie.closeall() #!md
processors = (;
rtp = realtimeplotter(;
screen = GLMakie.Screen(), #!md
setup,
fieldname = :temperature,
colorrange = (T(0), T(1)),
size = (600, 350),
colormap = :seaborn_icefire_gradient,
nupdate = 20,
),
nusselt = realtimeplotter(;
screen = GLMakie.Screen(), #!md
setup,
plot = nusseltplot,
nupdate = 20,
),
avg = realtimeplotter(;
screen = GLMakie.Screen(), #!md
setup,
plot = averagetemp,
nupdate = 50,
),
log = timelogger(; nupdate = 1000),
)
# Solve equation
state, outputs = solve_unsteady(;
setup,
ustart,
tempstart,
tlims = (T(0), T(20)),
Δt = T(1e-2),
processors,
);
#md # ```@raw html
#md # <video src="/RayleighBenard2D.mp4" controls="controls" autoplay="autoplay" loop="loop"></video>
#md # ```
# Nusselt numbers
outputs.nusselt
# Average temperature
outputs.avg
#md # ## Copy-pasteable code
#md #
#md # Below is the full code for this example stripped of comments and output.
#md #
#md # ```julia
#md # CODE_CONTENT
#md # ```