-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathstepsize_dg2d.jl
215 lines (183 loc) · 9.81 KB
/
stepsize_dg2d.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).
# Since these FMAs can increase the performance of many numerical algorithms,
# we need to opt-in explicitly.
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.
@muladd begin
#! format: noindent
function max_dt(u, t, mesh::TreeMesh{2},
constant_speed::False, equations, dg::DG, cache)
# to avoid a division by zero if the speed vanishes everywhere,
# e.g. for steady-state linear advection
max_scaled_speed = nextfloat(zero(t))
@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)
max_lambda1 = max_lambda2 = zero(max_scaled_speed)
for j in eachnode(dg), i in eachnode(dg)
u_node = get_node_vars(u, equations, dg, i, j, element)
lambda1, lambda2 = max_abs_speeds(u_node, equations)
max_lambda1 = max(max_lambda1, lambda1)
max_lambda2 = max(max_lambda2, lambda2)
end
inv_jacobian = cache.elements.inverse_jacobian[element]
max_scaled_speed = max(max_scaled_speed,
inv_jacobian * (max_lambda1 + max_lambda2))
end
return 2 / (nnodes(dg) * max_scaled_speed)
end
function max_dt(u, t, mesh::TreeMesh{2},
constant_speed::True, equations, dg::DG, cache)
# to avoid a division by zero if the speed vanishes everywhere,
# e.g. for steady-state linear advection
max_scaled_speed = nextfloat(zero(t))
@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)
max_lambda1, max_lambda2 = max_abs_speeds(equations)
inv_jacobian = cache.elements.inverse_jacobian[element]
max_scaled_speed = max(max_scaled_speed,
inv_jacobian * (max_lambda1 + max_lambda2))
end
return 2 / (nnodes(dg) * max_scaled_speed)
end
function max_dt(u, t, mesh::ParallelTreeMesh{2},
constant_speed::False, equations, dg::DG, cache)
# call the method accepting a general `mesh::TreeMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), TreeMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
function max_dt(u, t, mesh::ParallelTreeMesh{2},
constant_speed::True, equations, dg::DG, cache)
# call the method accepting a general `mesh::TreeMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), TreeMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
function max_dt(u, t,
mesh::Union{StructuredMesh{2}, UnstructuredMesh2D, P4estMesh{2},
T8codeMesh{2}, StructuredMeshView{2}},
constant_speed::False, equations, dg::DG, cache)
# to avoid a division by zero if the speed vanishes everywhere,
# e.g. for steady-state linear advection
max_scaled_speed = nextfloat(zero(t))
@unpack contravariant_vectors, inverse_jacobian = cache.elements
@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)
max_lambda1 = max_lambda2 = zero(max_scaled_speed)
for j in eachnode(dg), i in eachnode(dg)
u_node = get_node_vars(u, equations, dg, i, j, element)
lambda1, lambda2 = max_abs_speeds(u_node, equations)
# Local speeds transformed to the reference element
Ja11, Ja12 = get_contravariant_vector(1, contravariant_vectors, i, j,
element)
lambda1_transformed = abs(Ja11 * lambda1 + Ja12 * lambda2)
Ja21, Ja22 = get_contravariant_vector(2, contravariant_vectors, i, j,
element)
lambda2_transformed = abs(Ja21 * lambda1 + Ja22 * lambda2)
inv_jacobian = abs(inverse_jacobian[i, j, element])
max_lambda1 = max(max_lambda1, lambda1_transformed * inv_jacobian)
max_lambda2 = max(max_lambda2, lambda2_transformed * inv_jacobian)
end
max_scaled_speed = max(max_scaled_speed, max_lambda1 + max_lambda2)
end
return 2 / (nnodes(dg) * max_scaled_speed)
end
function max_dt(u, t,
mesh::Union{StructuredMesh{2}, UnstructuredMesh2D, P4estMesh{2},
T8codeMesh{2}, StructuredMeshView{2}},
constant_speed::True, equations, dg::DG, cache)
@unpack contravariant_vectors, inverse_jacobian = cache.elements
# to avoid a division by zero if the speed vanishes everywhere,
# e.g. for steady-state linear advection
max_scaled_speed = nextfloat(zero(t))
max_lambda1, max_lambda2 = max_abs_speeds(equations)
@batch reduction=(max, max_scaled_speed) for element in eachelement(dg, cache)
for j in eachnode(dg), i in eachnode(dg)
# Local speeds transformed to the reference element
Ja11, Ja12 = get_contravariant_vector(1, contravariant_vectors, i, j,
element)
lambda1_transformed = abs(Ja11 * max_lambda1 + Ja12 * max_lambda2)
Ja21, Ja22 = get_contravariant_vector(2, contravariant_vectors, i, j,
element)
lambda2_transformed = abs(Ja21 * max_lambda1 + Ja22 * max_lambda2)
inv_jacobian = abs(inverse_jacobian[i, j, element])
max_scaled_speed = max(max_scaled_speed,
inv_jacobian *
(lambda1_transformed + lambda2_transformed))
end
end
return 2 / (nnodes(dg) * max_scaled_speed)
end
function max_dt(u, t, mesh::ParallelP4estMesh{2},
constant_speed::False, equations, dg::DG, cache)
# call the method accepting a general `mesh::P4estMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), P4estMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
function max_dt(u, t, mesh::ParallelP4estMesh{2},
constant_speed::True, equations, dg::DG, cache)
# call the method accepting a general `mesh::P4estMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), P4estMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
function max_dt(u, t, mesh::ParallelT8codeMesh{2},
constant_speed::False, equations, dg::DG, cache)
# call the method accepting a general `mesh::T8codeMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), T8codeMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
function max_dt(u, t, mesh::ParallelT8codeMesh{2},
constant_speed::True, equations, dg::DG, cache)
# call the method accepting a general `mesh::T8codeMesh{2}`
# TODO: MPI, we should improve this; maybe we should dispatch on `u`
# and create some MPI array type, overloading broadcasting and mapreduce etc.
# Then, this specific array type should also work well with DiffEq etc.
dt = invoke(max_dt,
Tuple{typeof(u), typeof(t), T8codeMesh{2},
typeof(constant_speed), typeof(equations), typeof(dg),
typeof(cache)},
u, t, mesh, constant_speed, equations, dg, cache)
# Base.min instead of min needed, see comment in src/auxiliary/math.jl
dt = MPI.Allreduce!(Ref(dt), Base.min, mpi_comm())[]
return dt
end
end # @muladd