-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathamr_dg2d.jl
303 lines (251 loc) · 12 KB
/
amr_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# TODO: Taal, dimension agnostic
# Refine elements in the DG solver based on a list of cell_ids that should be refined
function refine!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
equations, dg::DGSEM, cache, cells_to_refine)
# Return early if there is nothing to do
if isempty(cells_to_refine)
return
end
# Determine for each existing element whether it needs to be refined
needs_refinement = falses(nelements(dg, cache))
# The "Ref(...)" is such that we can vectorize the search but not the array that is searched
elements_to_refine = searchsortedfirst.(Ref(cache.elements.cell_ids[1:nelements(dg, cache)]),
cells_to_refine)
needs_refinement[elements_to_refine] .= true
# Retain current solution data
old_n_elements = nelements(dg, cache)
old_u_ode = copy(u_ode)
GC.@preserve old_u_ode begin # OBS! If we don't GC.@preserve old_u_ode, it might be GC'ed
old_u = wrap_array(old_u_ode, mesh, equations, dg, cache)
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)
# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
@assert nelements(dg, cache) > old_n_elements
resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
u = wrap_array(u_ode, mesh, equations, dg, cache)
# Loop over all elements in old container and either copy them or refine them
element_id = 1
for old_element_id in 1:old_n_elements
if needs_refinement[old_element_id]
# Refine element and store solution directly in new data structure
refine_element!(u, element_id, old_u, old_element_id,
adaptor, equations, dg)
element_id += 2^ndims(mesh)
else
# Copy old element data to new element container
@views u[:, .., element_id] .= old_u[:, .., old_element_id]
element_id += 1
end
end
@boundscheck begin
# If everything is correct, we should have processed all elements.
# Depending on whether the last element processed above had to be refined or not,
# the counter `element_id` can have two different values at the end.
@assert element_id == nelements(dg, cache) + 1 || element_id == nelements(dg, cache) + 2^ndims(mesh) "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end
end # GC.@preserve old_u_ode
# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# Initialize new mortar containers
mortars = init_mortars(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg), dg.mortar)
copy!(cache.mortars, mortars)
# Sanity check
if isperiodic(mesh.tree) && nmortars(mortars) == 0
@assert ninterfaces(interfaces) == 2 * nelements(dg, cache) ("For 2D and periodic domains and conforming elements, the number of interfaces must be twice the number of elements")
end
return nothing
end
# TODO: Taal compare performance of different implementations
# Refine solution data u for an element, using L2 projection (interpolation)
function refine_element!(u::AbstractArray{<:Any,4}, element_id,
old_u, old_element_id,
adaptor::LobattoLegendreAdaptorL2, equations, dg)
@unpack forward_upper, forward_lower = adaptor
# Store new element ids
lower_left_id = element_id
lower_right_id = element_id + 1
upper_left_id = element_id + 2
upper_right_id = element_id + 3
@boundscheck begin
@assert old_element_id >= 1
@assert size(old_u, 1) == nvariables(equations)
@assert size(old_u, 2) == nnodes(dg)
@assert size(old_u, 3) == nnodes(dg)
@assert size(old_u, 4) >= old_element_id
@assert element_id >= 1
@assert size( u, 1) == nvariables(equations)
@assert size( u, 2) == nnodes(dg)
@assert size( u, 3) == nnodes(dg)
@assert size( u, 4) >= element_id + 3
end
# Interpolate to lower left element
for j in eachnode(dg), i in eachnode(dg)
acc = zero(get_node_vars(u, equations, dg, i, j, element_id))
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, old_element_id) * forward_lower[i, k] * forward_lower[j, l]
end
set_node_vars!(u, acc, equations, dg, i, j, lower_left_id)
end
# Interpolate to lower right element
for j in eachnode(dg), i in eachnode(dg)
acc = zero(get_node_vars(u, equations, dg, i, j, element_id))
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, old_element_id) * forward_upper[i, k] * forward_lower[j, l]
end
set_node_vars!(u, acc, equations, dg, i, j, lower_right_id)
end
# Interpolate to upper left element
for j in eachnode(dg), i in eachnode(dg)
acc = zero(get_node_vars(u, equations, dg, i, j, element_id))
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, old_element_id) * forward_lower[i, k] * forward_upper[j, l]
end
set_node_vars!(u, acc, equations, dg, i, j, upper_left_id)
end
# Interpolate to upper right element
for j in eachnode(dg), i in eachnode(dg)
acc = zero(get_node_vars(u, equations, dg, i, j, element_id))
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, old_element_id) * forward_upper[i, k] * forward_upper[j, l]
end
set_node_vars!(u, acc, equations, dg, i, j, upper_right_id)
end
return nothing
end
# TODO: Taal, dimension agnostic
# Coarsen elements in the DG solver based on a list of cell_ids that should be removed
function coarsen!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
equations, dg::DGSEM, cache, child_cells_to_coarsen)
# Return early if there is nothing to do
if isempty(child_cells_to_coarsen)
return
end
# Determine for each old element whether it needs to be removed
to_be_removed = falses(nelements(dg, cache))
# The "Ref(...)" is such that we can vectorize the search but not the array that is searched
elements_to_remove = searchsortedfirst.(Ref(cache.elements.cell_ids[1:nelements(dg, cache)]),
child_cells_to_coarsen)
to_be_removed[elements_to_remove] .= true
# Retain current solution data
old_n_elements = nelements(dg, cache)
old_u_ode = copy(u_ode)
GC.@preserve old_u_ode begin # OBS! If we don't GC.@preserve old_u_ode, it might be GC'ed
old_u = wrap_array(old_u_ode, mesh, equations, dg, cache)
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)
# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
@assert nelements(dg, cache) < old_n_elements
resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
u = wrap_array(u_ode, mesh, equations, dg, cache)
# Loop over all elements in old container and either copy them or coarsen them
skip = 0
element_id = 1
for old_element_id in 1:old_n_elements
# If skip is non-zero, we just coarsened 2^ndims elements and need to omit the following elements
if skip > 0
skip -= 1
continue
end
if to_be_removed[old_element_id]
# If an element is to be removed, sanity check if the following elements
# are also marked - otherwise there would be an error in the way the
# cells/elements are sorted
@assert all(to_be_removed[old_element_id:(old_element_id+2^ndims(mesh)-1)]) "bad cell/element order"
# Coarsen elements and store solution directly in new data structure
coarsen_elements!(u, element_id, old_u, old_element_id,
adaptor, equations, dg)
element_id += 1
skip = 2^ndims(mesh) - 1
else
# Copy old element data to new element container
@views u[:, .., element_id] .= old_u[:, .., old_element_id]
element_id += 1
end
end
@boundscheck begin
# If everything is correct, we should have processed all elements.
@assert element_id == nelements(dg, cache) + 1 "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end
end # GC.@preserve old_u_ode
# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# Initialize new mortar containers
mortars = init_mortars(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg), dg.mortar)
copy!(cache.mortars, mortars)
# Sanity check
if isperiodic(mesh.tree) && nmortars(mortars) == 0
@assert ninterfaces(interfaces) == 2 * nelements(dg, cache) ("For 2D and periodic domains and conforming elements, the number of interfaces must be twice the number of elements")
end
return nothing
end
# TODO: Taal compare performance of different implementations
# Coarsen solution data u for four elements, using L2 projection
function coarsen_elements!(u::AbstractArray{<:Any,4}, element_id, old_u, old_element_id,
adaptor::LobattoLegendreAdaptorL2, equations, dg)
@unpack reverse_upper, reverse_lower = adaptor
# Store old element ids
lower_left_id = old_element_id
lower_right_id = old_element_id + 1
upper_left_id = old_element_id + 2
upper_right_id = old_element_id + 3
@boundscheck begin
@assert old_element_id >= 1
@assert size(old_u, 1) == nvariables(equations)
@assert size(old_u, 2) == nnodes(dg)
@assert size(old_u, 3) == nnodes(dg)
@assert size(old_u, 4) >= old_element_id + 3
@assert element_id >= 1
@assert size( u, 1) == nvariables(equations)
@assert size( u, 2) == nnodes(dg)
@assert size( u, 3) == nnodes(dg)
@assert size( u, 4) >= element_id
end
for j in eachnode(dg), i in eachnode(dg)
acc = zero(get_node_vars(u, equations, dg, i, j, element_id))
# Project from lower left element
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, lower_left_id) * reverse_lower[i, k] * reverse_lower[j, l]
end
# Project from lower right element
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, lower_right_id) * reverse_upper[i, k] * reverse_lower[j, l]
end
# Project from upper left element
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, upper_left_id) * reverse_lower[i, k] * reverse_upper[j, l]
end
# Project from upper right element
for l in eachnode(dg), k in eachnode(dg)
acc += get_node_vars(old_u, equations, dg, k, l, upper_right_id) * reverse_upper[i, k] * reverse_upper[j, l]
end
# Update value
set_node_vars!(u, acc, equations, dg, i, j, element_id)
end
end
# this method is called when an `IndicatorThreeLevel` is constructed
function create_cache(::Type{IndicatorThreeLevel}, mesh::TreeMesh{2}, equations, dg::DG, cache)
indicator_value = Vector{real(dg)}(undef, nelements(dg, cache))
return (; indicator_value)
end