Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/examples/tree_3d_dgsem/elixir_navierstokes_convergence.jl
2055 views
1
using OrdinaryDiffEqLowStorageRK
2
using Trixi
3
4
###############################################################################
5
# semidiscretization of the ideal compressible Navier-Stokes equations
6
7
prandtl_number() = 0.72
8
mu() = 0.01
9
10
equations = CompressibleEulerEquations3D(1.4)
11
equations_parabolic = CompressibleNavierStokesDiffusion3D(equations, mu = mu(),
12
Prandtl = prandtl_number(),
13
gradient_variables = GradientVariablesPrimitive())
14
15
# Create DG solver with polynomial degree = 3 and (local) Lax-Friedrichs/Rusanov flux as surface flux
16
17
# Up to version 0.13.0, `max_abs_speed_naive` was used as the default wave speed estimate of
18
# `const flux_lax_friedrichs = FluxLaxFriedrichs(), i.e., `FluxLaxFriedrichs(max_abs_speed = max_abs_speed_naive)`.
19
# In the `StepsizeCallback`, though, the less diffusive `max_abs_speeds` is employed which is consistent with `max_abs_speed`.
20
# Thus, we exchanged in PR#2458 the default wave speed used in the LLF flux to `max_abs_speed`.
21
# To ensure that every example still runs we specify explicitly `FluxLaxFriedrichs(max_abs_speed_naive)`.
22
# We remark, however, that the now default `max_abs_speed` is in general recommended due to compliance with the
23
# `StepsizeCallback` (CFL-Condition) and less diffusion.
24
solver = DGSEM(polydeg = 3, surface_flux = FluxLaxFriedrichs(max_abs_speed_naive),
25
volume_integral = VolumeIntegralWeakForm())
26
27
coordinates_min = (-1.0, -1.0, -1.0) # minimum coordinates (min(x), min(y), min(z))
28
coordinates_max = (1.0, 1.0, 1.0) # maximum coordinates (max(x), max(y), max(z))
29
30
# Create a uniformly refined mesh with periodic boundaries
31
mesh = TreeMesh(coordinates_min, coordinates_max,
32
initial_refinement_level = 3,
33
periodicity = (true, false, true),
34
n_cells_max = 50_000) # set maximum capacity of tree data structure
35
36
# Note: the initial condition cannot be specialized to `CompressibleNavierStokesDiffusion3D`
37
# since it is called by both the parabolic solver (which passes in `CompressibleNavierStokesDiffusion3D`)
38
# and by the initial condition (which passes in `CompressibleEulerEquations3D`).
39
# This convergence test setup was originally derived by Andrew Winters (@andrewwinters5000)
40
function initial_condition_navier_stokes_convergence_test(x, t, equations)
41
# Constants. OBS! Must match those in `source_terms_navier_stokes_convergence_test`
42
c = 2.0
43
A1 = 0.5
44
A2 = 1.0
45
A3 = 0.5
46
47
# Convenience values for trig. functions
48
pi_x = pi * x[1]
49
pi_y = pi * x[2]
50
pi_z = pi * x[3]
51
pi_t = pi * t
52
53
rho = c + A1 * sin(pi_x) * cos(pi_y) * sin(pi_z) * cos(pi_t)
54
v1 = A2 * sin(pi_x) * log(x[2] + 2.0) * (1.0 - exp(-A3 * (x[2] - 1.0))) * sin(pi_z) *
55
cos(pi_t)
56
v2 = v1
57
v3 = v1
58
p = rho^2
59
60
return prim2cons(SVector(rho, v1, v2, v3, p), equations)
61
end
62
63
@inline function source_terms_navier_stokes_convergence_test(u, x, t, equations)
64
# TODO: parabolic
65
# we currently need to hardcode these parameters until we fix the "combined equation" issue
66
# see also https://github.com/trixi-framework/Trixi.jl/pull/1160
67
inv_gamma_minus_one = inv(equations.gamma - 1)
68
Pr = prandtl_number()
69
mu_ = mu()
70
71
# Constants. OBS! Must match those in `initial_condition_navier_stokes_convergence_test`
72
c = 2.0
73
A1 = 0.5
74
A2 = 1.0
75
A3 = 0.5
76
77
# Convenience values for trig. functions
78
pi_x = pi * x[1]
79
pi_y = pi * x[2]
80
pi_z = pi * x[3]
81
pi_t = pi * t
82
83
# Define auxiliary functions for the strange function of the y variable
84
# to make expressions easier to read
85
g = log(x[2] + 2.0) * (1.0 - exp(-A3 * (x[2] - 1.0)))
86
g_y = (A3 * log(x[2] + 2.0) * exp(-A3 * (x[2] - 1.0)) +
87
(1.0 - exp(-A3 * (x[2] - 1.0))) / (x[2] + 2.0))
88
g_yy = (2.0 * A3 * exp(-A3 * (x[2] - 1.0)) / (x[2] + 2.0) -
89
(1.0 - exp(-A3 * (x[2] - 1.0))) / ((x[2] + 2.0)^2) -
90
A3^2 * log(x[2] + 2.0) * exp(-A3 * (x[2] - 1.0)))
91
92
# Density and its derivatives
93
rho = c + A1 * sin(pi_x) * cos(pi_y) * sin(pi_z) * cos(pi_t)
94
rho_t = -pi * A1 * sin(pi_x) * cos(pi_y) * sin(pi_z) * sin(pi_t)
95
rho_x = pi * A1 * cos(pi_x) * cos(pi_y) * sin(pi_z) * cos(pi_t)
96
rho_y = -pi * A1 * sin(pi_x) * sin(pi_y) * sin(pi_z) * cos(pi_t)
97
rho_z = pi * A1 * sin(pi_x) * cos(pi_y) * cos(pi_z) * cos(pi_t)
98
rho_xx = -pi^2 * (rho - c)
99
rho_yy = -pi^2 * (rho - c)
100
rho_zz = -pi^2 * (rho - c)
101
102
# Velocities and their derivatives
103
# v1 terms
104
v1 = A2 * sin(pi_x) * g * sin(pi_z) * cos(pi_t)
105
v1_t = -pi * A2 * sin(pi_x) * g * sin(pi_z) * sin(pi_t)
106
v1_x = pi * A2 * cos(pi_x) * g * sin(pi_z) * cos(pi_t)
107
v1_y = A2 * sin(pi_x) * g_y * sin(pi_z) * cos(pi_t)
108
v1_z = pi * A2 * sin(pi_x) * g * cos(pi_z) * cos(pi_t)
109
v1_xx = -pi^2 * v1
110
v1_yy = A2 * sin(pi_x) * g_yy * sin(pi_z) * cos(pi_t)
111
v1_zz = -pi^2 * v1
112
v1_xy = pi * A2 * cos(pi_x) * g_y * sin(pi_z) * cos(pi_t)
113
v1_xz = pi^2 * A2 * cos(pi_x) * g * cos(pi_z) * cos(pi_t)
114
v1_yz = pi * A2 * sin(pi_x) * g_y * cos(pi_z) * cos(pi_t)
115
# v2 terms (simplifies from ansatz)
116
v2 = v1
117
v2_t = v1_t
118
v2_x = v1_x
119
v2_y = v1_y
120
v2_z = v1_z
121
v2_xx = v1_xx
122
v2_yy = v1_yy
123
v2_zz = v1_zz
124
v2_xy = v1_xy
125
v2_yz = v1_yz
126
# v3 terms (simplifies from ansatz)
127
v3 = v1
128
v3_t = v1_t
129
v3_x = v1_x
130
v3_y = v1_y
131
v3_z = v1_z
132
v3_xx = v1_xx
133
v3_yy = v1_yy
134
v3_zz = v1_zz
135
v3_xz = v1_xz
136
v3_yz = v1_yz
137
138
# Pressure and its derivatives
139
p = rho^2
140
p_t = 2.0 * rho * rho_t
141
p_x = 2.0 * rho * rho_x
142
p_y = 2.0 * rho * rho_y
143
p_z = 2.0 * rho * rho_z
144
145
# Total energy and its derivatives; simiplifies from ansatz that v2 = v1 and v3 = v1
146
E = p * inv_gamma_minus_one + 1.5 * rho * v1^2
147
E_t = p_t * inv_gamma_minus_one + 1.5 * rho_t * v1^2 + 3.0 * rho * v1 * v1_t
148
E_x = p_x * inv_gamma_minus_one + 1.5 * rho_x * v1^2 + 3.0 * rho * v1 * v1_x
149
E_y = p_y * inv_gamma_minus_one + 1.5 * rho_y * v1^2 + 3.0 * rho * v1 * v1_y
150
E_z = p_z * inv_gamma_minus_one + 1.5 * rho_z * v1^2 + 3.0 * rho * v1 * v1_z
151
152
# Divergence of Fick's law ∇⋅∇q = kappa ∇⋅∇T; simplifies because p = rho², so T = p/rho = rho
153
kappa = equations.gamma * inv_gamma_minus_one / Pr
154
q_xx = kappa * rho_xx # kappa T_xx
155
q_yy = kappa * rho_yy # kappa T_yy
156
q_zz = kappa * rho_zz # kappa T_zz
157
158
# Stress tensor and its derivatives (exploit symmetry)
159
tau11 = 4.0 / 3.0 * v1_x - 2.0 / 3.0 * (v2_y + v3_z)
160
tau12 = v1_y + v2_x
161
tau13 = v1_z + v3_x
162
tau22 = 4.0 / 3.0 * v2_y - 2.0 / 3.0 * (v1_x + v3_z)
163
tau23 = v2_z + v3_y
164
tau33 = 4.0 / 3.0 * v3_z - 2.0 / 3.0 * (v1_x + v2_y)
165
166
tau11_x = 4.0 / 3.0 * v1_xx - 2.0 / 3.0 * (v2_xy + v3_xz)
167
tau12_x = v1_xy + v2_xx
168
tau13_x = v1_xz + v3_xx
169
170
tau12_y = v1_yy + v2_xy
171
tau22_y = 4.0 / 3.0 * v2_yy - 2.0 / 3.0 * (v1_xy + v3_yz)
172
tau23_y = v2_yz + v3_yy
173
174
tau13_z = v1_zz + v3_xz
175
tau23_z = v2_zz + v3_yz
176
tau33_z = 4.0 / 3.0 * v3_zz - 2.0 / 3.0 * (v1_xz + v2_yz)
177
178
# Compute the source terms
179
# Density equation
180
du1 = (rho_t + rho_x * v1 + rho * v1_x
181
+ rho_y * v2 + rho * v2_y
182
+ rho_z * v3 + rho * v3_z)
183
# x-momentum equation
184
du2 = (rho_t * v1 + rho * v1_t + p_x + rho_x * v1^2
185
+ 2.0 * rho * v1 * v1_x
186
+ rho_y * v1 * v2
187
+ rho * v1_y * v2
188
+ rho * v1 * v2_y
189
+ rho_z * v1 * v3
190
+ rho * v1_z * v3
191
+ rho * v1 * v3_z -
192
mu_ * (tau11_x + tau12_y + tau13_z))
193
# y-momentum equation
194
du3 = (rho_t * v2 + rho * v2_t + p_y + rho_x * v1 * v2
195
+ rho * v1_x * v2
196
+ rho * v1 * v2_x
197
+ rho_y * v2^2
198
+ 2.0 * rho * v2 * v2_y
199
+ rho_z * v2 * v3
200
+ rho * v2_z * v3
201
+ rho * v2 * v3_z -
202
mu_ * (tau12_x + tau22_y + tau23_z))
203
# z-momentum equation
204
du4 = (rho_t * v3 + rho * v3_t + p_z + rho_x * v1 * v3
205
+ rho * v1_x * v3
206
+ rho * v1 * v3_x
207
+ rho_y * v2 * v3
208
+ rho * v2_y * v3
209
+ rho * v2 * v3_y
210
+ rho_z * v3^2
211
+ 2.0 * rho * v3 * v3_z -
212
mu_ * (tau13_x + tau23_y + tau33_z))
213
# Total energy equation
214
du5 = (E_t + v1_x * (E + p) + v1 * (E_x + p_x)
215
+ v2_y * (E + p) + v2 * (E_y + p_y)
216
+ v3_z * (E + p) + v3 * (E_z + p_z) -
217
# stress tensor and temperature gradient from x-direction
218
mu_ * (q_xx + v1_x * tau11 + v2_x * tau12 + v3_x * tau13
219
+ v1 * tau11_x + v2 * tau12_x + v3 * tau13_x) -
220
# stress tensor and temperature gradient terms from y-direction
221
mu_ * (q_yy + v1_y * tau12 + v2_y * tau22 + v3_y * tau23
222
+ v1 * tau12_y + v2 * tau22_y + v3 * tau23_y) -
223
# stress tensor and temperature gradient terms from z-direction
224
mu_ * (q_zz + v1_z * tau13 + v2_z * tau23 + v3_z * tau33
225
+ v1 * tau13_z + v2 * tau23_z + v3 * tau33_z))
226
227
return SVector(du1, du2, du3, du4, du5)
228
end
229
230
initial_condition = initial_condition_navier_stokes_convergence_test
231
232
# BC types
233
velocity_bc_top_bottom = NoSlip() do x, t, equations_parabolic
234
u_cons = initial_condition_navier_stokes_convergence_test(x, t, equations_parabolic)
235
return SVector(u_cons[2] / u_cons[1], u_cons[3] / u_cons[1], u_cons[4] / u_cons[1])
236
end
237
heat_bc_top_bottom = Adiabatic((x, t, equations_parabolic) -> 0.0)
238
boundary_condition_top_bottom = BoundaryConditionNavierStokesWall(velocity_bc_top_bottom,
239
heat_bc_top_bottom)
240
241
# define inviscid boundary conditions
242
boundary_conditions = (; x_neg = boundary_condition_periodic,
243
x_pos = boundary_condition_periodic,
244
y_neg = boundary_condition_slip_wall,
245
y_pos = boundary_condition_slip_wall,
246
z_neg = boundary_condition_periodic,
247
z_pos = boundary_condition_periodic)
248
249
# define viscous boundary conditions
250
boundary_conditions_parabolic = (; x_neg = boundary_condition_periodic,
251
x_pos = boundary_condition_periodic,
252
y_neg = boundary_condition_top_bottom,
253
y_pos = boundary_condition_top_bottom,
254
z_neg = boundary_condition_periodic,
255
z_pos = boundary_condition_periodic)
256
257
semi = SemidiscretizationHyperbolicParabolic(mesh, (equations, equations_parabolic),
258
initial_condition, solver;
259
boundary_conditions = (boundary_conditions,
260
boundary_conditions_parabolic),
261
source_terms = source_terms_navier_stokes_convergence_test)
262
263
###############################################################################
264
# ODE solvers, callbacks etc.
265
266
# Create ODE problem with time span `tspan`
267
tspan = (0.0, 1.0)
268
ode = semidiscretize(semi, tspan)
269
270
summary_callback = SummaryCallback()
271
alive_callback = AliveCallback(alive_interval = 10)
272
analysis_interval = 100
273
analysis_callback = AnalysisCallback(semi, interval = analysis_interval)
274
callbacks = CallbackSet(summary_callback, alive_callback, analysis_callback)
275
276
###############################################################################
277
# run the simulation
278
279
time_int_tol = 1e-8
280
sol = solve(ode, RDPK3SpFSAL49(); abstol = time_int_tol, reltol = time_int_tol, dt = 1e-5,
281
ode_default_options()..., callback = callbacks)
282
283