Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
trixi-framework
GitHub Repository: trixi-framework/Trixi.jl
Path: blob/main/src/callbacks_step/averaging_dg2d.jl
2055 views
1
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).
2
# Since these FMAs can increase the performance of many numerical algorithms,
3
# we need to opt-in explicitly.
4
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.
5
@muladd begin
6
#! format: noindent
7
8
# Create arrays with DGSEM-specific structure to store the mean values and set them all to 0
9
function initialize_mean_values(mesh::TreeMesh{2},
10
equations::AbstractCompressibleEulerEquations{2},
11
dg::DGSEM, cache)
12
uEltype = eltype(cache.elements)
13
v_mean = zeros(uEltype,
14
(ndims(equations), nnodes(dg), nnodes(dg),
15
nelements(cache.elements)))
16
c_mean = zeros(uEltype, (nnodes(dg), nnodes(dg), nelements(cache.elements)))
17
rho_mean = zeros(uEltype, size(c_mean))
18
vorticity_mean = zeros(uEltype, size(c_mean))
19
20
return (; v_mean, c_mean, rho_mean, vorticity_mean)
21
end
22
23
# Create cache which holds the vorticity for the previous time step. This is needed due to the
24
# trapezoidal rule
25
function create_cache(::Type{AveragingCallback}, mesh::TreeMesh{2},
26
equations::AbstractCompressibleEulerEquations{2}, dg::DGSEM,
27
cache)
28
# Cache vorticity from previous time step
29
uEltype = eltype(cache.elements)
30
vorticity_prev = zeros(uEltype, (nnodes(dg), nnodes(dg), nelements(cache.elements)))
31
return (; vorticity_prev)
32
end
33
34
# Calculate vorticity for the initial solution and store it in the cache
35
function initialize_cache!(averaging_callback_cache, u,
36
mesh::TreeMesh{2},
37
equations::AbstractCompressibleEulerEquations{2},
38
dg::DGSEM, cache)
39
@unpack vorticity_prev = averaging_callback_cache
40
41
# Calculate vorticity for initial solution
42
calc_vorticity!(vorticity_prev, u, mesh, equations, dg, cache)
43
44
return nothing
45
end
46
47
# Update mean values using the trapezoidal rule
48
function calc_mean_values!(mean_values, averaging_callback_cache, u, u_prev,
49
integration_constant,
50
mesh::TreeMesh{2},
51
equations::AbstractCompressibleEulerEquations{2},
52
dg::DGSEM, cache)
53
@unpack v_mean, c_mean, rho_mean, vorticity_mean = mean_values
54
@unpack vorticity_prev = averaging_callback_cache
55
56
@threaded for element in eachelement(dg, cache)
57
for j in eachnode(dg), i in eachnode(dg)
58
vorticity = calc_vorticity_node(u, mesh, equations, dg, cache, i, j,
59
element)
60
vorticity_prev_node = vorticity_prev[i, j, element]
61
vorticity_prev[i, j, element] = vorticity # Cache current vorticity for the next time step
62
63
u_node_prim = cons2prim(get_node_vars(u, equations, dg, i, j, element),
64
equations)
65
u_prev_node_prim = cons2prim(get_node_vars(u_prev, equations, dg, i, j,
66
element), equations)
67
68
rho, v1, v2, p = u_node_prim
69
rho_prev, v1_prev, v2_prev, p_prev = u_prev_node_prim
70
71
c = sqrt(equations.gamma * p / rho)
72
c_prev = sqrt(equations.gamma * p_prev / rho_prev)
73
74
# Calculate the contribution to the mean values using the trapezoidal rule
75
vorticity_mean[i, j, element] += integration_constant *
76
(vorticity_prev_node + vorticity)
77
v_mean[1, i, j, element] += integration_constant * (v1_prev + v1)
78
v_mean[2, i, j, element] += integration_constant * (v2_prev + v2)
79
c_mean[i, j, element] += integration_constant * (c_prev + c)
80
rho_mean[i, j, element] += integration_constant * (rho_prev + rho)
81
end
82
end
83
84
return nothing
85
end
86
end # @muladd
87
88