MFC
Exascale flow solver
Loading...
Searching...
No Matches
m_amr_regrid.fpp.f90
Go to the documentation of this file.
1# 1 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
2!>
3!!@file
4!!@brief Contains module m_amr_regrid
5
6# 1 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 1
7# 1 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 1
8# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
9# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
10# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
11# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
12# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
13# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
14
15# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
16# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
17# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
18
19# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
20# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
21! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
22! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
23# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
24# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
25# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
26
27# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
28# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
29
30# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
31
32# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
33
34# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
35
36# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
37
38# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
39
40# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
41
42# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
43
44# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
45! New line at end of file is required for FYPP
46# 2 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
47# 1 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 1
48# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
49# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
50# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
51# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
52# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
53# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
54
55# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
56# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
57# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
58
59# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
60# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
61! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
62! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
63# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
64# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
65# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
66
67# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
68# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
69
70# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
71
72# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
73
74# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
75
76# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
77
78# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
79
80# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
81
82# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
83
84# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
85! New line at end of file is required for FYPP
86# 2 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 2
87
88# 4 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
89# 5 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
90# 6 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
91# 7 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
92# 8 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
93
94# 20 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
95
96# 49 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
97
98# 54 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
99
100# 59 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
101
102# 64 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
103
104# 69 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
105
106# 74 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
107
108# 87 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
109
110# 101 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
111
112# 106 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
113
114# 111 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
115
116# 116 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
117
118# 121 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
119
120# 126 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
121
122# 131 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
123
124# 136 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
125
126# 141 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
127
128# 171 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
129
130# 231 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
131
132# 245 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
133
134# 270 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
135
136# 281 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
137
138# 283 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
139# 294 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
140
141# 343 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
142
143# 353 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
144
145# 363 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
146
147# 372 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
148
149# 389 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
150
151# 399 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
152
153# 406 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
154
155# 412 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
156
157# 418 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
158
159# 424 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
160
161# 430 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
162
163# 436 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
164! New line at end of file is required for FYPP
165# 3 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
166# 1 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 1
167# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
168# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
169# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
170# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
171# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
172# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
173
174# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
175# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
176# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
177
178# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
179# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
180! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
181! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
182# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
183# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
184# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
185
186# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
187# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
188
189# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
190
191# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
192
193# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
194
195# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
196
197# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
198
199# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
200
201# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
202
203# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
204! New line at end of file is required for FYPP
205# 2 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 2
206
207# 7 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
208
209# 17 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
210
211# 22 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
212
213# 27 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
214
215# 32 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
216
217# 37 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
218
219# 42 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
220
221# 47 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
222
223# 52 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
224
225# 57 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
226
227# 62 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
228
229# 73 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
230
231# 78 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
232
233# 83 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
234
235# 88 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
236
237# 103 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
238
239# 131 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
240
241# 161 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
242
243# 176 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
244
245# 194 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
246
247# 216 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
248
249# 245 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
250
251# 260 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
252
253# 270 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
254
255# 279 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
256
257# 295 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
258
259# 305 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
260
261# 312 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
262! New line at end of file is required for FYPP
263# 4 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
264
265! GPU parallel region (scalar reductions, maxval/minval)
266# 23 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
267
268! GPU parallel loop over threads (most common GPU macro)
269# 44 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
270
271! Required closing for GPU_PARALLEL_LOOP
272# 56 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
273
274! Mark routine for device compilation
275# 113 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
276
277! Declare device-resident data
278# 131 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
279
280! Inner loop within a GPU parallel region
281# 146 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
282
283! Scoped GPU data region
284# 165 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
285
286! Host code with device pointers (for MPI with GPU buffers)
287# 191 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
288
289! Allocate device memory (unscoped)
290# 205 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
291
292! Free device memory
293# 217 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
294
295! Atomic operation on device
296# 229 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
297
298! End atomic capture block
299# 240 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
300
301! Copy data between host and device
302# 252 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
303
304! Synchronization barrier
305# 264 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
306
307! Import GPU library module (openacc or omp_lib)
308# 273 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
309
310! Emit code only for AMD compiler
311# 280 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
312
313! Emit code for non-Cray compilers
314# 287 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
315
316! Emit code only for Cray compiler
317# 294 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
318
319! Cray compiler directive, emitted from Fypp so the formatter cannot turn it into a comment
320# 301 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
321
322! Emit code for non-NVIDIA compilers
323# 308 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
324
325# 310 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
326# 311 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
327! New line at end of file is required for FYPP
328# 2 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 2
329
330# 14 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
331
332! Caution: This macro requires the use of a binding script to set CUDA_VISIBLE_DEVICES, such that we have one GPU device per MPI
333! rank. That's because for both cudaMemAdvise (preferred location) and cudaMemPrefetchAsync we use location = device_id = 0. For an
334! example see misc/nvidia_uvm/bind.sh.
335# 52 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
336
337! Allocate and create GPU device memory
338# 72 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
339
340! Free GPU device memory and deallocate
341# 80 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
342
343! Cray-specific GPU pointer setup for vector fields
344# 104 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
345
346! Cray-specific GPU pointer setup for scalar fields
347# 120 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
348
349! Cray-specific GPU pointer teardown for scalar fields - exact inverse of ACC_SETUP_SFs. Removes the
350! present-table entries ACC_SETUP_SFs added (the scalar_field descriptor and its %sf copyin). REQUIRED when
351! an ACC_SETUP'd field is freed MID-RUN (e.g. AMR slot free on regrid/restart): Cray 'exit data delete'
352! decrements the reference counter, so the lone @:DEALLOCATE(arg%sf) only undoes the @:ALLOCATE create ref
353! and leaves the descriptor + the ACC_SETUP %sf copyin dangling. Call BEFORE @:DEALLOCATE(arg%sf).
354# 140 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
355
356! Cray-specific GPU pointer setup for acoustic source spatials
357# 165 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
358
359! Whether stress index idx is a shear component. Spelled out because NVHPC 26.9 at -O2 vectorizes any(idx == shear_indices)
360! inside a loop to always true, which doubled the normal-stress elastic energy.
361# 171 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
362
363# 177 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
364
365# 184 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
366! New line at end of file is required for FYPP
367# 6 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp" 2
368
369!> @brief Dynamic regrid for the AMR level set: density-gradient tagging, clustering (m_amr_cluster), box shaping, hierarchical
370!! child nesting (m_amr_nest), and slot rebuild with cross-rank fine-state migration.
372
373#ifdef MFC_MPI
374 use mpi
375#endif
376
379 use m_constants, only: mapcells
380 use m_mpi_proxy, only: s_mpi_abort
382 use m_amr_wave
390 & s_amr_free_slot, s_amr_reduce_xchg_flag, s_amr_reconcile_slots, s_amr_assign_block_owners, &
392 & f_amr_seam_dim, f_amr_boxes_overlap, s_set_amr_fine_geometry, s_interpolate_coarse_to_fine, s_amr_setup_ib, &
393 & s_amr_l1_fill_exchange, s_amr_l1_fill_consume, s_amr_parent_fill_exchange, s_amr_parent_fill_consume, &
394 & s_amr_fill_wave_done, amr_fw_dev
397
398 implicit none
399
400 private
402
403 !> Padded global coarse-index bbox of the Lagrangian bubble cloud; blocks and regrid boxes stay clear of it
404 integer :: lag_supp_lo(3), lag_supp_hi(3)
405 logical :: lag_supp_on = .false.
406 !> Migration send/receive pools (freed after the regrid); amr_mig_win(:,old block) = payload start in amr_mig_rq, fine window
407 !! lo(3), hi(3) (start 0 = none)
408 real(wp), allocatable :: amr_mig_sq(:), amr_mig_rq(:)
409 integer, allocatable :: amr_mig_win(:,:)
410
411contains
412
413 !> Abort on same-level seam topologies no halo reconciles: partial-transverse face adjacency or box intersection (both arise
414 !! only from IB body-bbox expansion). Each rank tests its own blocks against all blocks.
415 impure subroutine s_amr_check_seam_topology()
416
417 integer :: ix, xb, yb, d, t
418 logical :: adj, tover
419
421 do ix = 1, amr_n_my
422 xb = amr_my_blk(ix)
423 do yb = 1, amr_num_blocks
424 if (xb == yb) cycle
425 if (amr_block_level(xb) /= amr_block_level(yb)) cycle
427 & amr_region_hi_all(:,yb))) then
428 write (0, '(A,6I6,A,6I6)') ' [amr] intersecting blocks (lo,hi): ', amr_region_lo_all(:,xb), &
429 & amr_region_hi_all(:,xb), ' |', amr_region_lo_all(:,yb), amr_region_hi_all(:,yb)
430 call s_mpi_abort("AMR: two same-level blocks INTERSECT (the child IB body-bbox expansion route can " &
431 & // "produce this - it has no overlap-merge pass): the overlapping cells would be " &
432 & // "restricted and refluxed twice, silently breaking conservation. Adjust the body/" &
433 & // "regrid inputs so body-expanded child boxes merge or separate.")
434 end if
435 do d = 1, num_dims
436 adj = amr_region_lo_all(d, yb) == amr_region_hi_all(d, xb) + 1
437 if (.not. adj) cycle
438 tover = .true.
439 do t = 1, num_dims
440 if (t /= d) tover = tover .and. amr_region_lo_all(t, xb) <= amr_region_hi_all(t, &
441 & yb) .and. amr_region_lo_all(t, yb) <= amr_region_hi_all(t, xb)
442 end do
443 if (tover .and. f_amr_seam_dim(xb, yb) == 0) then
444 write (0, '(A,6I6,A,6I6)') ' [amr] touching blocks (lo,hi): ', amr_region_lo_all(:,xb), &
445 & amr_region_hi_all(:,xb), ' |', amr_region_lo_all(:,yb), amr_region_hi_all(:,yb)
446 call s_mpi_abort("AMR: two same-level blocks touch with PARTIAL transverse overlap (IB body-bbox " &
447 & // "expansion can produce this): the fine-fine seam halo only reconciles exact-match " &
448 & // "faces, so the shared-face flux would silently leak. Adjust amr_block/regrid inputs " // "so body-expanded boxes merge or separate.")
449 end if
450 end do
451 end do
452 end do
453
454 end subroutine s_amr_check_seam_topology
455
456 !> Abort if a box exceeds its level's slot cap, amr_maxc_fit/amr_ref_ratio**(lev-1) coarse cells (it would overrun the fine
457 !! coord arrays); floor of 1 as in s_amr_tile_box.
458 impure subroutine s_amr_check_box_caps(boxes, nboxes, box_level)
459
460 type(t_box), intent(in) :: boxes(:)
461 integer, intent(in) :: nboxes, box_level(:)
462 integer :: k, d, lev, cap, span
463
464 do k = 1, nboxes
465 lev = box_level(k)
466 do d = 1, num_dims
467 cap = max(amr_maxc_fit(d)/amr_ref_ratio**(lev - 1), 1)
468 span = boxes(k)%hi(d) - boxes(k)%lo(d) + 1
469 if (span > cap) then
470 if (proc_rank == 0) print '(A,I0,A,I0,A,I0,A,I0)', ' [amr] box cap violated: level ', lev, ' dim ', d, &
471 & ' span ', span, ' > cap ', cap
472 call s_mpi_abort("AMR regrid: a fine box exceeds the slot cap for its level (span and cap printed above). " &
473 & // "The fine coord arrays are sized to amr_ref_ratio*amr_maxc_fit, so this would write " &
474 & // "past x_cb and corrupt the heap. A box emitter did not route through s_amr_tile_box.")
475 end if
476 end do
477 end do
478
479 end subroutine s_amr_check_box_caps
480
481 !> Abort if two same-level boxes overlap (the overlap carry-forward and exchange plans assume disjointness). All levels share
482 !! the coarse index space; an x-lo sweep keeps it cheap.
483 impure subroutine s_amr_check_box_disjoint(boxes, nboxes, box_level)
484
485 type(t_box), intent(in) :: boxes(:)
486 integer, intent(in) :: nboxes, box_level(:)
487 integer(8), allocatable :: key(:)
488 integer, allocatable :: ord(:)
489 integer :: i, j, k, kk
490
491 allocate (ord(nboxes)); key = [(int(boxes(k)%lo(1), 8), k=1, nboxes)]
492 call s_amr_sort_by_key(key, nboxes, ord)
493 do i = 1, nboxes
494 k = ord(i)
495 if (box_level(k) < 1) cycle
496 do j = i + 1, nboxes
497 kk = ord(j)
498 if (boxes(kk)%lo(1) > boxes(k)%hi(1)) exit
499 if (box_level(kk) /= box_level(k)) cycle
500 if (all(boxes(k)%lo <= boxes(kk)%hi .and. boxes(kk)%lo <= boxes(k)%hi)) then
501 if (proc_rank == 0) print '(A,I0,A,I0,A,I0)', ' [amr] same-level box overlap: level ', box_level(k), &
502 & ' boxes ', k, ' and ', kk
503 call s_mpi_abort("AMR regrid: two same-level boxes overlap (indices printed above). The overlap " &
504 & // "carry-forward and the exchange plans both assume same-level disjointness.")
505 end if
506 end do
507 end do
508
509 end subroutine s_amr_check_box_disjoint
510
511 !> True iff global level-0 cell (gi, gj, gk) lies inside any acoustic source support bbox.
512 pure logical function f_in_acoustic_support(gi, gj, gk) result(insup)
513
514 integer, intent(in) :: gi, gj, gk
515 integer :: s
516
517 insup = .false.
518 do s = 1, num_source
519 if (gi >= acoustic_supp_lo(1, s) .and. gi <= acoustic_supp_hi(1, s) .and. (n_glb == 0 .or. (gj >= acoustic_supp_lo(2, &
520 & s) .and. gj <= acoustic_supp_hi(2, s))) .and. (p_glb == 0 .or. (gk >= acoustic_supp_lo(3, &
521 & s) .and. gk <= acoustic_supp_hi(3, s)))) then
522 insup = .true.; return
523 end if
524 end do
525
526 end function f_in_acoustic_support
527
528 !> Clip a box (global indices) clear of every acoustic source support; may empty it (hi < lo).
529 impure subroutine s_amr_clip_box_from_sources(lo, hi)
530
531 integer, intent(inout) :: lo(3), hi(3)
532 integer :: s
533
534 do s = 1, num_source
536 end do
537 do s = 1, num_source
538 if (hi(1) < lo(1) .or. hi(2) < lo(2) .or. hi(3) < lo(3)) return
540 & s))) call s_mpi_abort('amr regrid: acoustic source exclusion clip failed (internal error)')
541 end do
542
543 end subroutine s_amr_clip_box_from_sources
544
545 !> Collectively recompute the global bubble-cloud exclusion bbox, padded by pad_cells; lag_supp_on = false when no rank holds a
546 !! bubble.
547 impure subroutine s_amr_compute_lag_supp(pad_cells)
548
549 integer, intent(in) :: pad_cells
550 real(wp), dimension(3) :: pmin_loc, pmax_loc, pmin_glb, pmax_glb
551 integer :: d
552
553 call s_lag_cloud_bbox_local(pmin_loc, pmax_loc)
554 do d = 1, 3
555 call s_mpi_allreduce_min(pmin_loc(d), pmin_glb(d))
556 call s_mpi_allreduce_max(pmax_loc(d), pmax_glb(d))
557 end do
558 lag_supp_on = pmin_glb(1) <= pmax_glb(1)
559 if (.not. lag_supp_on) return
560 call s_amr_phys_to_cells(pmin_glb, pmax_glb, pad_cells, lag_supp_lo, lag_supp_hi)
561
562 end subroutine s_amr_compute_lag_supp
563
564 !> True iff global level-0 cell (gi, gj, gk) lies inside the Lagrangian-cloud exclusion bbox.
565 pure logical function f_in_lag_support(gi, gj, gk) result(insup)
566
567 integer, intent(in) :: gi, gj, gk
568
569 insup = .false.
570 if (.not. lag_supp_on) return
571 insup = gi >= lag_supp_lo(1) .and. gi <= lag_supp_hi(1) .and. (n_glb == 0 .or. (gj >= lag_supp_lo(2) &
572 & .and. gj <= lag_supp_hi(2))) .and. (p_glb == 0 .or. (gk >= lag_supp_lo(3) &
573 & .and. gk <= lag_supp_hi(3)))
574
575 end function f_in_lag_support
576
577 !> Clip a box (global indices) clear of one support bbox along the axis/side keeping the most extent (ties: lower axis, begin
578 !! side); may empty it.
579 pure subroutine s_amr_clip_box_from_supp(lo, hi, slo, shi)
580
581 integer, intent(inout) :: lo(3), hi(3)
582 integer, intent(in) :: slo(3), shi(3)
583 integer :: d, best_d, best_side, best_ext, ext_l, ext_r
584
585 if (hi(1) < lo(1) .or. hi(2) < lo(2) .or. hi(3) < lo(3)) return
586 if (.not. f_amr_boxes_overlap(lo, hi, slo, shi)) return
587 best_d = 1; best_side = 1; best_ext = -1
588 do d = 1, num_dims
589 ext_l = slo(d) - lo(d)
590 ext_r = hi(d) - shi(d)
591 if (ext_l > best_ext) then; best_ext = ext_l; best_d = d; best_side = 1; end if
592 if (ext_r > best_ext) then; best_ext = ext_r; best_d = d; best_side = 2; end if
593 end do
594 if (best_side == 1) then
595 hi(best_d) = slo(best_d) - 1
596 else
597 lo(best_d) = shi(best_d) + 1
598 end if
599
600 end subroutine s_amr_clip_box_from_supp
601
602 !> Clip a box clear of the acoustic source and bubble-cloud supports.
603 impure subroutine s_amr_clip_box_clear(lo, hi)
604
605 integer, intent(inout) :: lo(3), hi(3)
606
607 if (acoustic_source) call s_amr_clip_box_from_sources(lo, hi)
608 if (bubbles_lagrange .and. lag_supp_on) call s_amr_clip_box_from_supp(lo, hi, lag_supp_lo, lag_supp_hi)
609
610 end subroutine s_amr_clip_box_clear
611
612 !> This rank's tagged cells as global level-0 coordinates; rank interiors are disjoint, so a SUM reduction counts each cell
613 !! once.
614 impure subroutine s_amr_local_tags(tag_grid, sidx, tags, ntag)
615
616 logical, intent(in) :: tag_grid(0:,0:,0:)
617 integer, intent(in) :: sidx(3)
618 integer, allocatable, intent(out) :: tags(:,:)
619 integer, intent(out) :: ntag
620 integer :: ci, cj, ck
621
622 ntag = 0
623 do ck = 0, p; do cj = 0, n; do ci = 0, m
624 if (tag_grid(ci, cj, ck)) ntag = ntag + 1
625 end do; end do; end do
626 allocate (tags(3, max(ntag, 1)))
627 ntag = 0
628 do ck = 0, p; do cj = 0, n; do ci = 0, m
629 if (tag_grid(ci, cj, ck)) then
630 ntag = ntag + 1
631 tags(1, ntag) = ci + sidx(1)
632 tags(2, ntag) = 0
633 tags(3, ntag) = 0
634 if (n_glb > 0) tags(2, ntag) = cj + sidx(2)
635 if (p_glb > 0) tags(3, ntag) = ck + sidx(3)
636 end if
637 end do; end do; end do
638
639 end subroutine s_amr_local_tags
640
641 !> Regrid: tag by density gradient, cluster, shape, nest and rebuild the slots, carrying old data into the overlaps. Called
642 !! between steps; no-op if nothing is tagged or the box set is unchanged.
643 impure subroutine s_amr_regrid(q_cons_base)
644
645 type(scalar_field), dimension(sys_size), intent(inout) :: q_cons_base
646 logical, allocatable :: tag_grid(:,:,:)
647 type(t_box), allocatable :: boxes(:)
648 integer :: sidx(3), nboxes
649 integer :: old_np
650 ! heap: O(global boxes) arrays overflow the default stack
651 integer, allocatable :: box_level(:)
652 integer, allocatable :: old_ilo(:,:), old_ext(:,:)
653 integer, allocatable :: old_level(:)
654 logical, allocatable :: old_owns(:)
655 logical :: same
656 integer :: i
657
658 allocate (box_level(amr_max_blocks), old_ilo(3, amr_max_blocks), old_ext(3, amr_max_blocks), old_level(amr_max_blocks), &
659 & old_owns(amr_max_blocks))
660
661 ! valid coarse ghosts at rank boundaries: the tag sweep and rebuild prolongation read across them
662
663 call s_phase_tic(ph_rg_halo)
664 call s_amr_exchange_coarse_cons_halo(q_cons_base)
665 do i = 1, sys_size
666
667# 304 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
668#if defined(MFC_OpenACC)
669# 304 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
670!$acc update host(q_cons_base(i)%sf)
671# 304 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
672#elif defined(MFC_OpenMP)
673# 304 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
674!$omp target update from(q_cons_base(i)%sf)
675# 304 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
676#endif
677# 305 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
678 end do
679 call s_phase_toc(ph_rg_halo)
680
681 ! cloud pad: smearing (mapCells) + stencil (2) + drift (amr_buf)
682 if (bubbles_lagrange) call s_amr_compute_lag_supp(mapcells + 2 + amr_buf)
683
684 call s_phase_tic(ph_rg_tag)
685 call s_amr_regrid_tag_cells(q_cons_base, tag_grid, sidx)
686 call s_phase_toc(ph_rg_tag)
687 call s_phase_tic(ph_rg_clus)
688 call s_amr_regrid_cluster_tags(tag_grid, sidx, boxes, nboxes)
689 call s_phase_toc(ph_rg_clus)
690 if (nboxes == 0) call s_amr_regrid_dormant_seed(boxes, nboxes) ! nothing tagged: retract to one small block
691 call s_amr_regrid_shape_boxes(boxes, nboxes)
692 if (nboxes == 0) return ! every box was confined to the domain margin; keep the current blocks
693 call s_phase_tic(ph_rg_clus)
694 call s_amr_regrid_nest_children(boxes, nboxes, box_level)
695 if (amr_snap > 0) call s_amr_regrid_snap_boxes(boxes, nboxes, box_level)
696 call s_amr_check_box_caps(boxes, nboxes, box_level) ! invariant: no box may exceed its level's slot cap
697 call s_amr_check_box_disjoint(boxes, nboxes, box_level) ! invariant: same-level boxes are pairwise disjoint
698 call s_amr_report_coverage(boxes, nboxes, box_level)
699 call s_amr_regrid_boxes_unchanged(boxes, nboxes, box_level, same)
700 call s_phase_toc(ph_rg_clus)
701 if (same) return ! identical box set and levels: keep the live slots
702 call s_amr_regrid_stash_migrate(boxes, nboxes, box_level, old_np, old_ilo, old_ext, old_level, old_owns)
703 call s_amr_regrid_rebuild_slots(q_cons_base, boxes, nboxes, old_np, old_ilo, old_ext, old_level)
704
705 end subroutine s_amr_regrid
706
707 !> Print the share of the base domain covered by each level's boxes (rank 0, rank_time_wrt).
708 impure subroutine s_amr_report_coverage(boxes, nboxes, box_level)
709
710 type(t_box), intent(in) :: boxes(:)
711 integer, intent(in) :: nboxes, box_level(:)
712 real(wp) :: cover(1:max(amr_max_level, 1)), base
713 integer :: k
714
715 if (.not. rank_time_wrt .or. proc_rank /= 0) return
716 cover = 0._wp
717 do k = 1, nboxes
718 cover(box_level(k)) = cover(box_level(k)) + real(product(merge(boxes(k)%hi - boxes(k)%lo + 1, 1, amr_dim)), wp)
719 end do
720 base = real(m_glb + 1, wp)*real(n_glb + 1, wp)*real(max(p_glb + 1, 1), wp)
721 print '(A,I0,A,*(F8.4))', ' [amr-cov] boxes ', nboxes, ' level coverage (share of domain) ', cover/base
722
723 end subroutine s_amr_report_coverage
724
725 !> Regrid phase 1: density-gradient tag field, skipping the global boundary cells; tags over acoustic and bubble-cloud supports
726 !! are suppressed. sidx = this rank's global start offsets.
727 impure subroutine s_amr_regrid_tag_cells(q_cons_base, tag_grid, sidx)
728
729 type(scalar_field), dimension(sys_size), intent(in) :: q_cons_base
730 logical, allocatable, intent(inout) :: tag_grid(:,:,:)
731 integer, intent(out) :: sidx(3)
732 integer :: tg_lo(3), tg_hi(3), ci, cj, ck, h(3)
733 real(wp) :: g
734 real(wp), allocatable :: r(:,:,:) !< the tagged variable, one evaluation per cell
735
736 sidx = amr_sidx
737 tg_lo = merge(merge(1, 0, sidx == 0), 0, amr_dim)
738 tg_hi = merge(merge(amr_ext - 1, amr_ext, sidx + amr_ext == [m_glb, n_glb, p_glb]), 0, amr_dim)
739 allocate (tag_grid(0:m,0:n,0:p)); tag_grid = .false.
740 h = merge(1, 0, amr_dim)
741 allocate (r(tg_lo(1) - h(1):tg_hi(1) + h(1),tg_lo(2) - h(2):tg_hi(2) + h(2),tg_lo(3) - h(3):tg_hi(3) + h(3)))
742 do ck = lbound(r, 3), ubound(r, 3)
743 do cj = lbound(r, 2), ubound(r, 2)
744 do ci = lbound(r, 1), ubound(r, 1)
745 r(ci, cj, ck) = f_amr_tag_val_sf(q_cons_base, ci, cj, ck)
746 end do
747 end do
748 end do
749 do ck = tg_lo(3), tg_hi(3)
750 do cj = tg_lo(2), tg_hi(2)
751 do ci = tg_lo(1), tg_hi(1)
752 ! jump of the tagged variable, max over the active dimensions
753 g = f_amr_tag_ind(r(ci + 1, cj, ck), r(ci, cj, ck), r(ci - 1, cj, ck))
754 if (n_glb > 0) g = max(g, f_amr_tag_ind(r(ci, cj + 1, ck), r(ci, cj, ck), r(ci, cj - 1, ck)))
755 if (p_glb > 0) g = max(g, f_amr_tag_ind(r(ci, cj, ck + 1), r(ci, cj, ck), r(ci, cj, ck - 1)))
756 if (g > amr_tag_eps) tag_grid(ci, cj, ck) = .true.
757 ! acoustic source support stays coarse
758 if (acoustic_source .and. tag_grid(ci, cj, ck)) then
759 if (f_in_acoustic_support(ci + sidx(1), cj + sidx(2), ck + sidx(3))) tag_grid(ci, cj, ck) = .false.
760 end if
761 ! bubble cloud stays coarse (two-way coupling lives there)
762 if (bubbles_lagrange .and. tag_grid(ci, cj, ck)) then
763 if (f_in_lag_support(ci + sidx(1), cj + sidx(2), ck + sidx(3))) tag_grid(ci, cj, ck) = .false.
764 end if
765 end do
766 end do
767 end do
768
769 end subroutine s_amr_regrid_tag_cells
770
771 !> Regrid phase 2: cluster this rank's tags (rank-invariant, via signature reductions) into separated boxes; nboxes = 0 if
772 !! nothing is tagged.
773 impure subroutine s_amr_regrid_cluster_tags(tag_grid, sidx, boxes, nboxes)
774
775 logical, allocatable, intent(inout) :: tag_grid(:,:,:)
776 integer, intent(in) :: sidx(3)
777 type(t_box), allocatable, intent(out) :: boxes(:)
778 integer, intent(out) :: nboxes
779 integer, allocatable :: tags(:,:)
780 integer :: ntag
781
782 call s_amr_local_tags(tag_grid, sidx, tags, ntag)
783 deallocate (tag_grid)
784 call s_amr_cluster(tags, ntag, boxes, nboxes, .true.)
785 deallocate (tags)
786
787 end subroutine s_amr_regrid_cluster_tags
788
789 !> One coarse cell at the domain center, which shaping pads into the dormant block that replaces the hierarchy.
790 impure subroutine s_amr_regrid_dormant_seed(boxes, nboxes)
791
792 type(t_box), allocatable, intent(inout) :: boxes(:)
793 integer, intent(out) :: nboxes
794
795 if (allocated(boxes)) deallocate (boxes)
796 allocate (boxes(1))
797 boxes(1)%lo = merge([m_glb, n_glb, p_glb]/2, 0, amr_dim)
798 boxes(1)%hi = boxes(1)%lo
799 nboxes = 1
800
801 end subroutine s_amr_regrid_dormant_seed
802
803 !> True iff two same-level blocks can coexist: disjoint, and either >= 2 empty coarse cells apart or face-adjacent with an exact
804 !! transverse match. Mirrors s_amr_check_seam_topology.
805 pure logical function f_amr_boxes_compatible(alo, ahi, blo, bhi) result(ok)
806
807 integer, intent(in) :: alo(3), ahi(3), blo(3), bhi(3)
808 integer :: d, t, gap
809 logical :: tmatch, tover
810
811 ok = .true.
812 if (all(alo <= bhi .and. blo <= ahi)) then; ok = .false.; return; end if
813 do d = 1, 3
814 if (.not. amr_dim(d)) cycle
815 gap = max(blo(d) - ahi(d), alo(d) - bhi(d)) - 1 ! empty cells along d (< 0: overlap)
816 if (gap >= 2 .or. gap < 0) cycle
817 tover = .true.; tmatch = .true.
818 do t = 1, 3
819 if (t == d .or. .not. amr_dim(t)) cycle
820 tover = tover .and. alo(t) <= bhi(t) .and. blo(t) <= ahi(t)
821 tmatch = tmatch .and. alo(t) == blo(t) .and. ahi(t) == bhi(t)
822 end do
823 if (tover .and. .not. (gap == 0 .and. tmatch)) then; ok = .false.; return; end if
824 end do
825
826 end function f_amr_boxes_compatible
827
828 !> Hysteresis (amr_snap > 0): a box within amr_snap cells per face of a live same-level block takes that block's box.
829 !! All-or-none: the snapped set must stay disjoint, compatible and singly nested (within amr_cpat_mar), else the fresh boxes
830 !! stand.
831 impure subroutine s_amr_regrid_snap_boxes(boxes, nboxes, box_level)
832
833 type(t_box), intent(inout) :: boxes(:)
834 integer, intent(in) :: nboxes, box_level(:)
835 type(t_box), allocatable :: snapped(:)
836 integer :: k, kk, ks, npar, nsnap, mlo(3), mhi(3)
837 logical :: ok
838
839 allocate (snapped(nboxes)); snapped(1:nboxes) = boxes(1:nboxes)
840 nsnap = 0
841 do k = 1, nboxes
842 do ks = 1, amr_num_blocks
843 if (amr_block_level(ks) /= box_level(k)) cycle
844 if (all(abs(amr_region_lo_all(:,ks) - boxes(k)%lo) <= amr_snap) .and. all(abs(amr_region_hi_all(:, &
845 & ks) - boxes(k)%hi) <= amr_snap)) then
846 ! with IB the old block must contain the fresh box (a lagging body margin would be lost)
847 if (ib .and. .not. (all(amr_region_lo_all(:,ks) <= boxes(k)%lo) .and. all(amr_region_hi_all(:, &
848 & ks) >= boxes(k)%hi))) cycle
849 if (any(amr_region_lo_all(:,ks) /= boxes(k)%lo) .or. any(amr_region_hi_all(:, &
850 & ks) /= boxes(k)%hi)) nsnap = nsnap + 1
851 snapped(k)%lo = amr_region_lo_all(:,ks); snapped(k)%hi = amr_region_hi_all(:,ks)
852 exit
853 end if
854 end do
855 end do
856 ok = nsnap > 0
857 do k = 1, nboxes
858 if (.not. ok) exit
859 do kk = k + 1, nboxes
860 if (box_level(kk) /= box_level(k)) cycle
861 if (all(snapped(k)%lo <= snapped(kk)%hi .and. snapped(kk)%lo <= snapped(k)%hi)) then
862 ok = .false.; exit
863 end if
864 if (.not. f_amr_boxes_compatible(snapped(k)%lo, snapped(k)%hi, snapped(kk)%lo, snapped(kk)%hi)) then
865 ok = .false.; exit
866 end if
867 end do
868 end do
869 ! a level >= 2 box lies inside exactly one parent box, inset by the nesting margin
870 do k = 1, nboxes
871 if (.not. ok) exit
872 if (box_level(k) < 2) cycle
873 npar = 0
874 do kk = 1, nboxes
875 if (box_level(kk) /= box_level(k) - 1) cycle
876 if (.not. all(snapped(k)%lo <= snapped(kk)%hi .and. snapped(kk)%lo <= snapped(k)%hi)) cycle
877 npar = npar + 1
878 call s_amr_nest_window(snapped(kk), mlo, mhi)
879 if (any(snapped(k)%lo < mlo) .or. any(snapped(k)%hi > mhi)) ok = .false.
880 end do
881 if (npar /= 1) ok = .false.
882 end do
883 if (ok) boxes(1:nboxes) = snapped(1:nboxes)
884 deallocate (snapped)
885
886 end subroutine s_amr_regrid_snap_boxes
887
888 !> Append to out(n+1:) the pieces of `box` that stay `gap` empty cells clear of [glo:ghi] and of each other, cutting dim by dim;
889 !! the part inside the block's span is dropped. The caller guarantees `box` overlaps the gap-grown block.
890 pure subroutine s_amr_box_carve(box, glo, ghi, gap, out, n)
891
892 type(t_box), intent(in) :: box
893 integer, intent(in) :: glo(3), ghi(3), gap
894 type(t_box), intent(inout) :: out(:)
895 integer, intent(inout) :: n
896 type(t_box) :: rem
897 integer :: d
898
899 rem = box
900 do d = 1, 3
901 if (.not. amr_dim(d)) cycle
902 if (rem%lo(d) < glo(d) - gap) then
903 n = n + 1; out(n) = rem; out(n)%hi(d) = glo(d) - gap - 1
904 end if
905 if (rem%hi(d) > ghi(d) + gap) then
906 n = n + 1; out(n) = rem; out(n)%lo(d) = ghi(d) + gap + 1
907 end if
908 rem%lo(d) = max(rem%lo(d), glo(d)); rem%hi(d) = min(rem%hi(d), ghi(d))
909 if (rem%lo(d) > rem%hi(d)) return
910 end do
911
912 end subroutine s_amr_box_carve
913
914 !> Abort if ranks hold different box lists (each rank sees only nearby IB bodies; a mismatch hangs the ownership collectives).
915 impure subroutine s_amr_check_boxes_agree(boxes, nboxes)
916
917 type(t_box), intent(in) :: boxes(:)
918 integer, intent(in) :: nboxes
919 real(wp) :: chk, cmin, cmax
920 integer :: k
921
922 chk = real(nboxes, wp)
923 do k = 1, nboxes
924 chk = chk + real(modulo(k*7919, 1009), wp)*real(sum(boxes(k)%lo*(/1, 131, 17161/)) + 3*sum(boxes(k)%hi*(/1, 131, &
925 & 17161/)), wp)
926 end do
927 call s_mpi_allreduce_min(chk, cmin)
928 call s_mpi_allreduce_max(chk, cmax)
929 if (cmin /= cmax) then
930 call s_mpi_abort('amr regrid with ib: the ranks built different block lists, because they hold different sets of ' &
931 & // 'immersed bodies; raise ib_neighborhood_radius until every rank sees every body')
932 end if
933
934 end subroutine s_amr_check_boxes_agree
935
936 !> Fold body group j into group i (bbox and body-extent unions) and move the last group into slot j.
937 pure subroutine s_amr_merge_body_groups(glo, ghi, clo, chi, ng, i, j)
938
939 integer, intent(inout) :: glo(:,:), ghi(:,:), clo(:,:), chi(:,:), ng
940 integer, intent(in) :: i, j
941
942 glo(:,i) = min(glo(:,i), glo(:,j)); ghi(:,i) = max(ghi(:,i), ghi(:,j))
943 clo(:,i) = min(clo(:,i), clo(:,j)); chi(:,i) = max(chi(:,i), chi(:,j))
944 glo(:,j) = glo(:,ng); ghi(:,j) = ghi(:,ng); clo(:,j) = clo(:,ng); chi(:,j) = chi(:,ng); ng = ng - 1
945
946 end subroutine s_amr_merge_body_groups
947
948 !> One margin-padded box (glo:ghi) and body extent (clo:chi) per immersed body, identical on every rank.
949 impure subroutine s_amr_gather_body_groups(glo, ghi, clo, chi, ng, mrg, nbody)
950
951 integer, intent(inout) :: glo(:,:), ghi(:,:), clo(:,:), chi(:,:)
952 integer, intent(out) :: ng
953 integer, intent(in) :: mrg, nbody
954 integer, parameter :: box_unset = -1000000000
955 integer, allocatable :: gbx(:,:)
956 integer :: i, gid, ierr, blo(3), bhi(3), cl(3), ch(3)
957
958 ! Every rank must build identical boxes though it sees only nearby bodies whose positions differ in the last digits: each
959 ! rank fills the boxes of its bodies by global id and one MAX reduction shares them.
960
961 allocate (gbx(12, nbody)); gbx = box_unset
962 do i = 1, num_ibs
963 gid = patch_ib(i)%gbl_patch_id
964 if (gid < 1 .or. gid > nbody) gid = i
965 call s_amr_body_bbox(i, mrg, blo, bhi); call s_amr_body_bbox(i, 0, cl, ch)
966 gbx(1:3,gid) = blo; gbx(4:6,gid) = bhi; gbx(7:9,gid) = cl; gbx(10:12,gid) = ch
967 end do
968#ifdef MFC_MPI
969 call mpi_allreduce(mpi_in_place, gbx, size(gbx), mpi_integer, mpi_max, mpi_comm_world, ierr)
970#endif
971 ng = 0
972 do gid = 1, nbody
973 if (gbx(1, gid) <= box_unset) cycle
974 blo = gbx(1:3,gid); bhi = gbx(4:6,gid)
975 ! blocks stay buff_size inside the domain
976 if (any(amr_dim .and. (blo < buff_size .or. bhi > [m_glb, n_glb, p_glb] - buff_size))) then
977 call s_mpi_abort('amr dynamic regrid with ib: the immersed body plus its containment ' &
978 & // 'margin does not fit inside the refinable domain interior (blocks stay buff_size off the edges)')
979 end if
980 ng = ng + 1; glo(:,ng) = blo; ghi(:,ng) = bhi; clo(:,ng) = gbx(7:9,gid); chi(:,ng) = gbx(10:12,gid)
981 end do
982 deallocate (gbx)
983
984 end subroutine s_amr_gather_body_groups
985
986 !> Separate body groups that face each other across < gap empty cells or overlap, reject groups over the block cap, and order
987 !! them canonically.
988 impure subroutine s_amr_resolve_body_groups(glo, ghi, clo, chi, ng, gap)
989
990 integer, intent(inout) :: glo(:,:), ghi(:,:), clo(:,:), chi(:,:), ng
991 integer, intent(in) :: gap
992 integer :: e(3), d, dsep, nsep, lowb, upb, need, avail_l, avail_u, tl, tu, blo(3), bhi(3)
993 integer :: i, j, g
994 logical :: merged
995
996 ! trim the margins apart (floored at 3 cells beyond the body: image-point stencil and moving-body guard), else merge
997
998 merged = .true.
999 do while (merged)
1000 merged = .false.
1001 outer: do i = 1, ng - 1
1002 do j = i + 1, ng
1003 e = 0
1004 do d = 1, 3
1005 if (amr_dim(d)) e(d) = max(glo(d, j) - ghi(d, i), glo(d, i) - ghi(d, j)) - 1
1006 end do
1007 nsep = count(amr_dim .and. e >= 0) ! dimensions in which the two boxes are apart
1008 if (nsep == 0) then
1009 call s_amr_merge_body_groups(glo, ghi, clo, chi, ng, i, j)
1010 merged = .true.; exit outer
1011 else if (nsep == 1) then
1012 dsep = 0
1013 do d = 1, 3
1014 if (amr_dim(d) .and. e(d) >= 0) dsep = d
1015 end do
1016 if (e(dsep) < gap) then
1017 need = gap - e(dsep)
1018 lowb = merge(i, j, ghi(dsep, i) < ghi(dsep, j)); upb = merge(j, i, lowb == i)
1019 avail_l = max(0, ghi(dsep, lowb) - (chi(dsep, lowb) + 3))
1020 avail_u = max(0, (clo(dsep, upb) - 3) - glo(dsep, upb))
1021 if (avail_l + avail_u >= need) then
1022 tl = min(need, avail_l); tu = need - tl
1023 ghi(dsep, lowb) = ghi(dsep, lowb) - tl; glo(dsep, upb) = glo(dsep, upb) + tu
1024 else
1025 call s_amr_merge_body_groups(glo, ghi, clo, chi, ng, i, j)
1026 merged = .true.; exit outer
1027 end if
1028 end if
1029 end if
1030 end do
1031 end do outer
1032 end do
1033 do g = 1, ng
1034 if (any(amr_dim .and. ghi(:,g) - glo(:,g) + 1 > amr_maxc_fit)) then
1035 write (0, '(A,I0,A,I0,A,3I6,A,3I6,A,3I6,A,I0)') ' [amr] body group ', g, ' of ', ng, ' lo ', glo(:,g), ' hi ', &
1036 & ghi(:,g), ' cap ', amr_maxc_fit, ' num_ibs ', num_ibs
1037 call s_mpi_abort('amr dynamic regrid with ib: a body (or bodies closer than the block gap) plus ' &
1038 & // 'margin exceeds the per-rank block size cap; use fewer ranks or a larger amr_maxc_fit')
1039 end if
1040 end do
1041
1042 ! canonical order by low corner: patch_ib order differs per rank and the carve depends on it
1043 do i = 2, ng
1044 blo = glo(:,i); bhi = ghi(:,i); j = i - 1
1045 do while (j >= 1)
1046 if (.not. (glo(3, j) > blo(3) .or. (glo(3, j) == blo(3) .and. (glo(2, j) > blo(2) .or. (glo(2, &
1047 & j) == blo(2) .and. glo(1, j) > blo(1)))))) exit
1048 glo(:,j + 1) = glo(:,j); ghi(:,j + 1) = ghi(:,j); j = j - 1
1049 end do
1050 glo(:,j + 1) = blo; ghi(:,j + 1) = bhi
1051 end do
1052
1053 end subroutine s_amr_resolve_body_groups
1054
1055 !> Regrid phase 3a (IB): tile the boxes around the immersed bodies. Each body (bbox + margin; close bodies share one) owns a
1056 !! block; boxes give up that block plus a gap ring, the remainder tiles to <= amr_maxc_fit, and body blocks are appended last.
1057 !! The ring keeps body blocks off other blocks, since the seam halo and reflux need exact-match transverse faces.
1058 impure subroutine s_amr_carve_tile_around_bodies(boxes, nboxes, out, nt, capt)
1059
1060 type(t_box), intent(in) :: boxes(:)
1061 integer, intent(in) :: nboxes
1062 type(t_box), intent(inout) :: out(:)
1063 integer, intent(inout) :: nt, capt
1064 ! empty coarse cells between blocks off a shared grid (one cell gives both the same reflux outside cell)
1065 integer, parameter :: gap = 2
1066 type(t_box), allocatable :: pieces(:), rest(:)
1067 integer, allocatable :: glo(:,:), ghi(:,:), clo(:,:), chi(:,:)
1068 integer :: nbody, ng, mrg, g, k, p, npc, nn
1069 logical, allocatable :: hit(:)
1070
1071 ! margin as in s_amr_expand_box_over_bodies: image-point stencil plus nested-level insets
1072
1073 mrg = max(amr_buf, 4) + max(0, amr_max_level - 1)*amr_cpat_mar
1074 nbody = max(num_gbl_ibs, num_ibs, 1)
1075 allocate (glo(3, nbody), ghi(3, nbody), clo(3, nbody), chi(3, nbody), hit(nbody))
1076 call s_amr_gather_body_groups(glo, ghi, clo, chi, ng, mrg, nbody)
1077 call s_amr_resolve_body_groups(glo, ghi, clo, chi, ng, gap)
1078
1079 allocate (pieces(amr_max_blocks), rest(amr_max_blocks))
1080 hit = .false.
1081 do k = 1, nboxes
1082 npc = 1; pieces(1) = boxes(k)
1083 do g = 1, ng
1084 nn = 0
1085 do p = 1, npc
1086 if (f_amr_boxes_overlap(pieces(p)%lo, pieces(p)%hi, glo(:,g) - gap, ghi(:,g) + gap)) then
1087 if (f_amr_boxes_overlap(pieces(p)%lo, pieces(p)%hi, glo(:,g), ghi(:,g))) hit(g) = .true.
1088 if (nn + 2*num_dims > amr_max_blocks) call s_mpi_abort('amr regrid: too many pieces around a body')
1089 call s_amr_box_carve(pieces(p), glo(:,g), ghi(:,g), gap, rest, nn)
1090 else
1091 nn = nn + 1; rest(nn) = pieces(p)
1092 end if
1093 end do
1094 pieces(1:nn) = rest(1:nn); npc = nn
1095 end do
1096 do p = 1, npc
1097 call s_amr_tile_box(pieces(p)%lo, pieces(p)%hi, out, nt, amr_max_blocks, capt)
1098 end do
1099 end do
1100
1101 do g = 1, ng
1102 if (.not. hit(g)) cycle
1103 if (nt >= amr_max_blocks) then
1104 capt = 1
1105 else
1106 nt = nt + 1; out(nt)%lo = glo(:,g); out(nt)%hi = ghi(:,g)
1107 end if
1108 end do
1109
1110 end subroutine s_amr_carve_tile_around_bodies
1111
1112 !> Regrid phase 3: pad and clamp each box, clip it clear of the acoustic/bubble supports, then tile oversized boxes (IB: after
1113 !! carving out the body blocks).
1114 impure subroutine s_amr_regrid_shape_boxes(boxes, nboxes)
1115
1116 type(t_box), allocatable, intent(inout) :: boxes(:)
1117 integer, intent(inout) :: nboxes
1118 type(t_box), allocatable :: tiled(:)
1119 integer :: lo(3), hi(3), k, kk, ntl, capt, pad
1120
1121 pad = amr_buf
1122 k = 0
1123 do kk = 1, nboxes
1124 lo = merge(max(boxes(kk)%lo - pad, buff_size), 0, amr_dim)
1125 hi = merge(min(boxes(kk)%hi + pad, [m_glb, n_glb, p_glb] - buff_size), 0, amr_dim)
1126 ! the supports are coarse-grid only; clipping only shrinks, so boxes stay disjoint
1127 call s_amr_clip_box_clear(lo, hi)
1128 if (any(hi < lo)) cycle ! confined to the domain margin
1129 k = k + 1; boxes(k)%lo = lo; boxes(k)%hi = hi
1130 end do
1131 nboxes = k
1132 if (nboxes == 0) return
1133
1134 ! tile boxes larger than amr_maxc_fit (IB: after carving out body blocks, so a body is never split)
1135 allocate (tiled(amr_max_blocks))
1136 ntl = 0; capt = 0
1137 if (ib) then
1138 call s_amr_carve_tile_around_bodies(boxes, nboxes, tiled, ntl, capt)
1139 else
1140 do kk = 1, nboxes
1141 call s_amr_tile_box(boxes(kk)%lo, boxes(kk)%hi, tiled, ntl, amr_max_blocks, capt)
1142 end do
1143 end if
1144 if (capt == 1 .and. proc_rank == 0) print '(A,I0)', ' [amr] WARNING: tiling capped at amr_max_blocks = ', amr_max_blocks
1145 deallocate (boxes); call move_alloc(tiled, boxes)
1146 nboxes = ntl
1147 if (ib) call s_amr_check_boxes_agree(boxes, nboxes)
1148
1149 ! a body block cannot both contain the body and exclude a source/cloud: fail closed
1150 if (ib .and. (acoustic_source .or. (bubbles_lagrange .and. lag_supp_on))) then
1151 do k = 1, nboxes
1152 lo = boxes(k)%lo; hi = boxes(k)%hi
1153 call s_amr_clip_box_clear(lo, hi)
1154 if (any(lo /= boxes(k)%lo) .or. any(hi /= boxes(k)%hi)) then
1155 call s_mpi_abort('amr regrid: a block must contain an immersed body AND stay ' &
1156 & // 'clear of an acoustic source support / Lagrangian bubble cloud - the ' &
1157 & // 'constraints conflict; move the body, source, or cloud apart')
1158 end if
1159 end do
1160 end if
1161
1162 end subroutine s_amr_regrid_shape_boxes
1163
1164 !> True iff the box set and levels equal the live slots (a rebuild would reproduce them).
1165 impure subroutine s_amr_regrid_boxes_unchanged(boxes, nboxes, box_level, same)
1166
1167 type(t_box), intent(in) :: boxes(:)
1168 integer, intent(in) :: nboxes, box_level(:)
1169 logical, intent(out) :: same
1170 integer :: k, ks
1171
1172 same = .false.
1173 if (nboxes == amr_num_blocks) then
1174 same = .true.
1175 do k = 1, nboxes
1176 if (any(boxes(k)%lo /= amr_slots(k)%region%lo) .or. any(boxes(k)%hi /= amr_slots(k)%region%hi) .or. box_level(k) &
1177 & /= amr_block_level(k)) same = .false.
1178 end do
1179 end if
1180
1181 end subroutine s_amr_regrid_boxes_unchanged
1182
1183 !> Pack fine window [l, h] of an owned old block's stash (device store) into its wire slice buf; gi fastest, then gj, gk, ii.
1184 impure subroutine s_amr_mig_pack_device(loc, l1, l2, l3, h1, h2, h3, buf)
1185
1186 integer, intent(in) :: loc, l1, l2, l3, h1, h2, h3
1187 real(wp), intent(inout), contiguous :: buf(:)
1188 integer :: ii, gk, gj, gi, n1, n2, n3
1189
1190 n1 = h1 - l1 + 1; n2 = h2 - l2 + 1; n3 = h3 - l3 + 1
1191
1192# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1193
1194# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1195#if defined(MFC_OpenACC)
1196# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1197!$acc parallel loop collapse(4) gang vector default(present) copyout(buf)
1198# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1199#elif defined(MFC_OpenMP)
1200# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1201
1202# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1203
1204# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1205
1206# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1207!$omp target teams loop defaultmap(firstprivate:scalar) bind(teams,parallel) collapse(4) defaultmap(tofrom:aggregate) defaultmap(tofrom:allocatable) defaultmap(tofrom:pointer) map(from:buf)
1208# 818 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1209#endif
1210# 819 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1211 do ii = 1, sys_size
1212 do gk = l3, h3
1213 do gj = l2, h2
1214 do gi = l1, h1
1215 buf(1 + gi - l1 + n1*(gj - l2 + n2*(gk - l3 + n3*(ii - 1)))) = real(amr_stor_st(gi, gj, gk, ii, loc), wp)
1216 end do
1217 end do
1218 end do
1219 end do
1220
1221# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1222#if defined(MFC_OpenACC)
1223# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1224!$acc end parallel loop
1225# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1226#elif defined(MFC_OpenMP)
1227# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1228
1229# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1230!$omp end target teams loop
1231# 828 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1232#endif
1233# 829 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1234
1235 end subroutine s_amr_mig_pack_device
1236
1237 !> Fine-index window [flo, fhi] in level-1 old block ilo's stash of the coarse box [wlo, whi] (0:0 in collapsed dims).
1238 pure subroutine s_amr_mig_window(wlo, whi, ilo, lev, flo, fhi)
1239
1240 integer, intent(in) :: wlo(3), whi(3), ilo(3), lev
1241 integer, intent(out) :: flo(3), fhi(3)
1242
1243 flo = merge(amr_ref_ratio**lev*(wlo - ilo), 0, amr_dim)
1244 fhi = merge(amr_ref_ratio**lev*(whi - ilo + 1) - 1, 0, amr_dim)
1245
1246 end subroutine s_amr_mig_window
1247
1248 !> Free a migration pool (device-resident when amr_fw_dev).
1249 impure subroutine s_amr_mig_free(pool)
1250
1251 real(wp), allocatable, intent(inout) :: pool(:)
1252
1253 if (.not. allocated(pool)) return
1254 if (amr_fw_dev) then
1255
1256# 850 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1257#if defined(MFC_OpenACC)
1258# 850 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1259!$acc exit data delete(pool)
1260# 850 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1261#elif defined(MFC_OpenMP)
1262# 850 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1263!$omp target exit data map(release:pool)
1264# 850 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1265#endif
1266# 851 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1267 end if
1268 deallocate (pool)
1269
1270 end subroutine s_amr_mig_free
1271
1272 !> Device cons->stor stash copy of one owned old block's fine interior (the store is device-authoritative; no host staging).
1273 impure subroutine s_amr_stash_copy_device(loc, e1, e2, e3)
1274
1275 integer, intent(in) :: loc, e1, e2, e3
1276 integer :: ii, gk, gj, gi
1277
1278
1279# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1280
1281# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1282#if defined(MFC_OpenACC)
1283# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1284!$acc parallel loop collapse(4) gang vector default(present)
1285# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1286#elif defined(MFC_OpenMP)
1287# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1288
1289# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1290
1291# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1292
1293# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1294!$omp target teams loop defaultmap(firstprivate:scalar) bind(teams,parallel) collapse(4) defaultmap(tofrom:aggregate) defaultmap(tofrom:allocatable) defaultmap(tofrom:pointer)
1295# 862 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1296#endif
1297# 863 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1298 do ii = 1, sys_size
1299 do gk = 0, e3
1300 do gj = 0, e2
1301 do gi = 0, e1
1302 amr_stor_st(gi, gj, gk, ii, loc) = amr_cons_st(gi, gj, gk, ii, loc)
1303 end do
1304 end do
1305 end do
1306 end do
1307
1308# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1309#if defined(MFC_OpenACC)
1310# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1311!$acc end parallel loop
1312# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1313#elif defined(MFC_OpenMP)
1314# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1315
1316# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1317!$omp end target teams loop
1318# 872 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1319#endif
1320# 873 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1321
1322 end subroutine s_amr_stash_copy_device
1323
1324 !> Overwrite the new block's prolonged cons with the covering old block's stashed detail (old fine index = new + sh) over the
1325 !! old window [l, h], from store column vold or the received wire slice buf.
1326# 879 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1327 impure subroutine s_amr_overlap_copy_st(vloc, vm, vn, vp, sh, l1, l2, l3, h1, h2, h3, &
1328# 880 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1329 & vold)
1330
1331 integer, intent(in) :: vloc, vm, vn, vp, sh(3), l1, l2, l3, h1, h2, h3
1332
1333# 885 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1334 integer, intent(in) :: vold
1335# 889 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1336 integer :: i, fi, fj, fk, ofi, ofj, ofk, sh1, sh2, sh3, n1, n2, n3
1337 logical :: vd2, vd3
1338
1339 sh1 = sh(1); sh2 = sh(2); sh3 = sh(3)
1340 n1 = h1 - l1 + 1; n2 = h2 - l2 + 1; n3 = h3 - l3 + 1
1341 vd2 = n_glb > 0; vd3 = p_glb > 0
1342# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1343
1344# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1345
1346# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1347#if defined(MFC_OpenACC)
1348# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1349!$acc parallel loop collapse(4) gang vector default(present) private(ofi, ofj, ofk)
1350# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1351#elif defined(MFC_OpenMP)
1352# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1353
1354# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1355
1356# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1357
1358# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1359!$omp target teams loop defaultmap(firstprivate:scalar) bind(teams,parallel) collapse(4) defaultmap(tofrom:aggregate) defaultmap(tofrom:allocatable) defaultmap(tofrom:pointer) private(ofi, ofj, ofk)
1360# 896 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1361#endif
1362# 897 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1363# 900 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1364 do i = 1, sys_size
1365 do fk = 0, vp
1366 do fj = 0, vn
1367 do fi = 0, vm
1368 ofk = fk + sh3; ofj = fj + sh2; ofi = fi + sh1
1369 if (vd3 .and. (ofk < l3 .or. ofk > h3)) cycle
1370 if (vd2 .and. (ofj < l2 .or. ofj > h2)) cycle
1371 if (ofi < l1 .or. ofi > h1) cycle
1372# 909 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1373 amr_cons_st(fi, fj, fk, i, vloc) = amr_stor_st(ofi, ofj, ofk, i, vold)
1374# 914 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1375 end do
1376 end do
1377 end do
1378 end do
1379
1380# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1381#if defined(MFC_OpenACC)
1382# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1383!$acc end parallel loop
1384# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1385#elif defined(MFC_OpenMP)
1386# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1387
1388# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1389!$omp end target teams loop
1390# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1391#endif
1392# 919 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1393
1394 end subroutine s_amr_overlap_copy_st
1395# 921 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1396# 879 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1397 impure subroutine s_amr_overlap_copy_wire(vloc, vm, vn, vp, sh, l1, l2, l3, h1, h2, h3, &
1398# 880 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1399 & buf)
1400
1401 integer, intent(in) :: vloc, vm, vn, vp, sh(3), l1, l2, l3, h1, h2, h3
1402
1403# 887 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1404 real(wp), intent(in), contiguous :: buf(:)
1405# 889 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1406 integer :: i, fi, fj, fk, ofi, ofj, ofk, sh1, sh2, sh3, n1, n2, n3
1407 logical :: vd2, vd3
1408
1409 sh1 = sh(1); sh2 = sh(2); sh3 = sh(3)
1410 n1 = h1 - l1 + 1; n2 = h2 - l2 + 1; n3 = h3 - l3 + 1
1411 vd2 = n_glb > 0; vd3 = p_glb > 0
1412# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1413
1414# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1415
1416# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1417#if defined(MFC_OpenACC)
1418# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1419!$acc parallel loop collapse(4) gang vector default(present) private(ofi, ofj, ofk) copyin(buf)
1420# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1421#elif defined(MFC_OpenMP)
1422# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1423
1424# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1425
1426# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1427
1428# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1429!$omp target teams loop defaultmap(firstprivate:scalar) bind(teams,parallel) collapse(4) defaultmap(tofrom:aggregate) defaultmap(tofrom:allocatable) defaultmap(tofrom:pointer) private(ofi, ofj, ofk) &
1430# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1431!$omp& map(to:buf)
1432# 898 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1433#endif
1434# 899 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1435# 900 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1436 do i = 1, sys_size
1437 do fk = 0, vp
1438 do fj = 0, vn
1439 do fi = 0, vm
1440 ofk = fk + sh3; ofj = fj + sh2; ofi = fi + sh1
1441 if (vd3 .and. (ofk < l3 .or. ofk > h3)) cycle
1442 if (vd2 .and. (ofj < l2 .or. ofj > h2)) cycle
1443 if (ofi < l1 .or. ofi > h1) cycle
1444# 911 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1445 amr_cons_st(fi, fj, fk, i, &
1446 & vloc) = real(buf(1 + ofi - l1 + n1*(ofj - l2 + n2*(ofk - l3 + n3*(i - 1)))), stp)
1447# 914 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1448 end do
1449 end do
1450 end do
1451 end do
1452
1453# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1454#if defined(MFC_OpenACC)
1455# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1456!$acc end parallel loop
1457# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1458#elif defined(MFC_OpenMP)
1459# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1460
1461# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1462!$omp end target teams loop
1463# 918 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1464#endif
1465# 919 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1466
1467 end subroutine s_amr_overlap_copy_wire
1468# 921 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1469# 922 "/home/runner/work/MFC/MFC/src/simulation/m_amr_regrid.fpp"
1470
1471 !> Record the live slots' fine interiors on the device and the old block set (old_*; old_chi = replicated coarse hi corner).
1472 impure subroutine s_amr_stash_old_blocks(old_np, old_ilo, old_chi, old_ext, old_owner, old_level, old_owns)
1473
1474 integer, intent(out) :: old_np, old_ilo(:,:), old_chi(:,:), old_ext(:,:), old_owner(:), old_level(:)
1475 logical, intent(out) :: old_owns(:)
1476 integer :: k, loc_ks, old_ext1_k, old_ext2_k, old_ext3_k !< plain scalars for device routines (NVHPC -Minline)
1477
1478 old_np = amr_num_blocks
1479 do k = 1, old_np
1480 ! replicated global origin/extent (valid on every rank, unlike the owner-only isect)
1481 old_ilo(:,k) = amr_region_lo_all(:,k)
1482 old_chi(:,k) = amr_region_hi_all(:,k)
1483 ! fine extent = amr_ref_ratio**level*footprint - 1 (a level-2 block is 4x its footprint)
1484 old_ext(:,k) = merge(amr_ref_ratio**amr_block_level(k)*(old_chi(:,k) - old_ilo(:,k) + 1) - 1, 0, amr_dim)
1485 old_owner(k) = amr_block_owner(k)
1486 ! the overlap copy must match levels
1487 old_level(k) = amr_block_level(k)
1488 old_owns(k) = amr_owns_all(k)
1489 if (old_owns(k)) then
1490 ! device stash: the store is device-authoritative; s_amr_st_reserve preserves it across a mid-rebuild grow.
1491 ! amdflang: the kernel is its own routine because target regions nested in BLOCK drop from the device image (HSA
1492 ! invalid symbol)
1493 loc_ks = amr_loc_of(k)
1494 old_ext1_k = old_ext(1, k)
1495 old_ext2_k = old_ext(2, k)
1496 old_ext3_k = old_ext(3, k)
1497 call s_amr_stash_copy_device(loc_ks, old_ext1_k, old_ext2_k, old_ext3_k)
1498 end if
1499 end do
1500
1501 end subroutine s_amr_stash_old_blocks
1502
1503 !> Commit the new regions, levels and owners, after checking proper nesting.
1504 impure subroutine s_amr_commit_boxes(boxes, nboxes, box_level)
1505
1506 type(t_box), intent(in) :: boxes(:)
1507 integer, intent(in) :: nboxes, box_level(:)
1508 integer :: k, kk, npar
1509
1510 ! regions and owners first: the migration needs the new owners and s_set_amr_fine_geometry reads amr_block_owner
1511
1512 amr_num_blocks = nboxes
1513 do k = 1, nboxes
1514 amr_region_lo_all(:,k) = boxes(k)%lo; amr_region_hi_all(:,k) = boxes(k)%hi
1515 amr_block_level(k) = box_level(k)
1516 end do
1517 ! dirty the seam-pair and overlap-rank caches before the rebuild's gathers consume them
1518 amr_seam_pairs_dirty = .true.
1519 amr_mesh_epoch = amr_mesh_epoch + 1
1520 ! proper nesting: a level >= 2 block must overlap exactly one parent-level block (f_amr_parent_block takes the first)
1521 do k = 1, nboxes
1522 if (box_level(k) < 2) cycle
1523 npar = 0
1524 do kk = 1, nboxes
1525 if (box_level(kk) == box_level(k) - 1 .and. f_amr_boxes_overlap(boxes(k)%lo, boxes(k)%hi, boxes(kk)%lo, &
1526 & boxes(kk)%hi)) npar = npar + 1
1527 end do
1528 if (npar /= 1) call s_mpi_abort('amr multi-level: a level>=2 block overlaps more than one (or no) ' &
1529 & // 'parent-level block - a fine tile straddling a parent-tile seam is unsupported (gather/reflux ' &
1530 & // 'couple to a single parent); reduce max_grid_size or the refined feature extent')
1531 end do
1532 amr_num_levels = maxval(box_level(1:nboxes))
1533 call s_amr_assign_block_owners()
1534
1535 end subroutine s_amr_commit_boxes
1536
1537 !> Cross-rank migration as one wave: each old owner ships the bounding box of its overlaps to every other rank owning an
1538 !! overlapping new block of the same level. Both sides enumerate old blocks ascending, so the transfers line up; received
1539 !! windows stay in amr_mig_rq. No-op at np=1.
1540 impure subroutine s_amr_migrate_old_blocks(boxes, nboxes, box_level, old_np, old_ilo, old_chi, old_owner, old_level, old_owns)
1541
1542 type(t_box), intent(in) :: boxes(:)
1543 integer, intent(in) :: nboxes, box_level(:), old_np, old_ilo(:,:), old_chi(:,:), old_owner(:), old_level(:)
1544 logical, intent(in) :: old_owns(:)
1545 integer :: h_loc !< plain scalar for device routines (NVHPC -Minline)
1546 integer :: k2, kk, rr, cnt, ix, lo, hi, flo(3), fhi(3)
1547 integer, allocatable :: wlo(:,:), whi(:,:) !< per-rank overlap window of the current old block
1548
1549 allocate (amr_mig_win(7, old_np)); amr_mig_win = 0
1550 if (num_procs > 1) then
1551 allocate (wlo(3,0:num_procs - 1), whi(3,0:num_procs - 1))
1552 wlo = huge(0); whi = -huge(0)
1553 call s_amr_wave_open(amr_wave, amr_band_migrate)
1554 call s_amr_wave_reset(amr_wsend); call s_amr_wave_reset(amr_wrecv)
1555 do kk = 1, old_np
1556 do k2 = 1, nboxes
1557 rr = amr_block_owner(k2)
1558 if (box_level(k2) /= old_level(kk) .or. rr == old_owner(kk)) cycle
1559 if (.not. f_amr_boxes_overlap(boxes(k2)%lo, boxes(k2)%hi, old_ilo(:,kk), old_chi(:,kk))) cycle
1560 wlo(:,rr) = min(wlo(:,rr), max(boxes(k2)%lo, old_ilo(:,kk)))
1561 whi(:,rr) = max(whi(:,rr), min(boxes(k2)%hi, old_chi(:,kk)))
1562 end do
1563 do rr = 0, num_procs - 1
1564 if (whi(1, rr) < wlo(1, rr)) cycle
1565 call s_amr_mig_window(wlo(:,rr), whi(:,rr), old_ilo(:,kk), old_level(kk), flo, fhi)
1566 cnt = sys_size*product(fhi - flo + 1)
1567 if (rr == proc_rank) then
1568 call s_amr_wave_add(amr_wrecv, old_owner(kk), kk, wlo(:,rr), whi(:,rr), cnt)
1569 else if (old_owns(kk)) then
1570 call s_amr_wave_add(amr_wsend, rr, kk, wlo(:,rr), whi(:,rr), cnt)
1571 end if
1572 wlo(:,rr) = huge(0); whi(:,rr) = -huge(0)
1573 end do
1574 end do
1575 deallocate (wlo, whi)
1576 call s_amr_wave_close(amr_wsend, amr_mig_sq, amr_fw_dev)
1577 call s_amr_wave_close(amr_wrecv, amr_mig_rq, amr_fw_dev)
1578 call s_amr_wave_post(amr_wave, amr_wrecv, amr_mig_rq, amr_fw_dev)
1579 do ix = 1, amr_wsend%nx
1580 kk = amr_wsend%blk(ix)
1581 call s_amr_wave_slice(amr_wsend, ix, lo, hi)
1582 call s_amr_mig_window(amr_wsend%bl(:,ix), amr_wsend%bh(:,ix), old_ilo(:,kk), old_level(kk), flo, fhi)
1583 h_loc = amr_loc_of(kk)
1584 call s_amr_mig_pack_device(h_loc, flo(1), flo(2), flo(3), fhi(1), fhi(2), fhi(3), amr_mig_sq(lo:hi))
1585 end do
1586 call s_amr_wave_send(amr_wave, amr_wsend, amr_mig_sq, amr_fw_dev)
1587 call s_amr_wave_wait(amr_wave)
1589 do ix = 1, amr_wrecv%nx
1590 kk = amr_wrecv%blk(ix)
1591 call s_amr_wave_slice(amr_wrecv, ix, lo, hi)
1592 call s_amr_mig_window(amr_wrecv%bl(:,ix), amr_wrecv%bh(:,ix), old_ilo(:,kk), old_level(kk), flo, fhi)
1593 amr_mig_win(:,kk) = [lo, flo, fhi]
1594 end do
1595 end if
1596
1597 end subroutine s_amr_migrate_old_blocks
1598
1599 !> Regrid phase 5: stash the old block set, commit the new one, and migrate stashed old blocks point-to-point to the ranks
1600 !! owning overlapping new blocks.
1601 impure subroutine s_amr_regrid_stash_migrate(boxes, nboxes, box_level, old_np, old_ilo, old_ext, old_level, old_owns)
1602
1603 type(t_box), intent(in) :: boxes(:)
1604 integer, intent(in) :: nboxes, box_level(:)
1605 integer, intent(out) :: old_np, old_ilo(:,:), old_ext(:,:), old_level(:)
1606 logical, intent(out) :: old_owns(:)
1607 integer :: old_chi(3, amr_max_blocks), old_owner(amr_max_blocks)
1608
1609 call s_phase_tic(ph_rg_own)
1610 call s_amr_stash_old_blocks(old_np, old_ilo, old_chi, old_ext, old_owner, old_level, old_owns)
1611 call s_amr_commit_boxes(boxes, nboxes, box_level)
1612 call s_phase_toc(ph_rg_own)
1613 call s_phase_tic(ph_rg_mig)
1614 call s_amr_migrate_old_blocks(boxes, nboxes, box_level, old_np, old_ilo, old_chi, old_owner, old_level, old_owns)
1615 call s_phase_toc(ph_rg_mig)
1616
1617 end subroutine s_amr_regrid_stash_migrate
1618
1619 !> Visit order of the owned new boxes (level-major, vpos) and, per held old slot, the last visit that reads it (last_use).
1620 impure subroutine s_amr_plan_rebuild_visits(boxes, nboxes, old_np, old_ilo, old_ext, old_level, vpos, last_use, held, nh)
1621
1622 type(t_box), intent(in) :: boxes(:)
1623 integer, intent(in) :: nboxes, old_np, old_ilo(:,:), old_ext(:,:), old_level(:)
1624 integer, allocatable, intent(out) :: vpos(:), last_use(:), held(:)
1625 integer, intent(out) :: nh
1626 integer :: i, k, kk, lev, pos, ohi(3)
1627
1628 call s_amr_refresh_my_blocks()
1629 allocate (vpos(nboxes)); vpos = 0; pos = 0
1630 do lev = 1, maxval(amr_block_level(1:amr_num_blocks))
1631 do i = 1, amr_n_my
1632 k = amr_my_blk(i)
1633 if (amr_block_level(k) /= lev) cycle
1634 pos = pos + 1; vpos(k) = pos
1635 end do
1636 end do
1637 allocate (last_use(old_np), held(old_np)); last_use = 0
1638 nh = 0
1639 do kk = 1, old_np
1640 if (.not. amr_slot_live(kk)) cycle
1641 nh = nh + 1; held(nh) = kk
1642 ohi = old_ilo(:,kk) + merge((old_ext(:,kk) + 1)/amr_ref_ratio**old_level(kk) - 1, 0, amr_dim)
1643 do i = 1, amr_n_my
1644 k = amr_my_blk(i)
1645 if (f_amr_boxes_overlap(boxes(k)%lo, boxes(k)%hi, old_ilo(:,kk), ohi)) last_use(kk) = max(last_use(kk), vpos(k))
1646 end do
1647 end do
1648
1649 end subroutine s_amr_plan_rebuild_visits
1650
1651 !> Overwrite the freshly prolonged slot ks with the overlapping old blocks of its level: from the migration pool when another
1652 !! rank owned them, else from the local stash.
1653 impure subroutine s_amr_carry_old_into_slot(ks, boxes, old_np, old_ilo, old_ext, old_level)
1654
1655 integer, intent(in) :: ks, old_np, old_ilo(:,:), old_ext(:,:), old_level(:)
1656 type(t_box), intent(in) :: boxes(:)
1657 integer :: sh(3), kk, ohi(3), rl
1658 ! NVHPC -Minline: plain scalars for device routines
1659 integer :: loc_kks, loc_ks, m_ks, n_ks, old_ext1_kk, old_ext2_kk, old_ext3_kk, p_ks, w1, w2, w3, w4, w5, w6, lo, hi
1660
1661 loc_ks = amr_loc_of(ks)
1662 m_ks = amr_slots(ks)%m
1663 n_ks = amr_slots(ks)%n
1664 p_ks = amr_slots(ks)%p
1665 do kk = 1, old_np
1666 if (old_level(kk) /= amr_block_level(ks)) cycle
1667 rl = amr_ref_ratio**old_level(kk)
1668 ohi = old_ilo(:,kk) + merge((old_ext(:,kk) + 1)/rl - 1, 0, amr_dim)
1669 if (.not. f_amr_boxes_overlap(boxes(ks)%lo, boxes(ks)%hi, old_ilo(:,kk), ohi)) cycle
1670 ! a level-l slot's local fine index 0 is L0 cell lo at rl = ratio**l (for l >= 2 amr_isect_lo is in the parent's frame)
1671 sh = merge(rl*(boxes(ks)%lo - old_ilo(:,kk)), 0, amr_dim) ! old local fine index = new local fine index + sh
1672 if (amr_mig_win(1, kk) > 0) then
1673 ! owned elsewhere: window in the migration receive pool
1674 lo = amr_mig_win(1, kk); hi = lo + sys_size*product(amr_mig_win(5:7,kk) - amr_mig_win(2:4,kk) + 1) - 1
1675 w1 = amr_mig_win(2, kk); w2 = amr_mig_win(3, kk); w3 = amr_mig_win(4, kk)
1676 w4 = amr_mig_win(5, kk); w5 = amr_mig_win(6, kk); w6 = amr_mig_win(7, kk)
1677 call s_amr_overlap_copy_wire(loc_ks, m_ks, n_ks, p_ks, sh, w1, w2, w3, w4, w5, w6, amr_mig_rq(lo:hi))
1678 else if (amr_slot_live(kk)) then
1679 loc_kks = amr_loc_of(kk)
1680 old_ext1_kk = old_ext(1, kk)
1681 old_ext2_kk = old_ext(2, kk)
1682 old_ext3_kk = old_ext(3, kk)
1683 call s_amr_overlap_copy_st(loc_ks, m_ks, n_ks, p_ks, sh, 0, 0, 0, old_ext1_kk, old_ext2_kk, old_ext3_kk, loc_kks)
1684 end if
1685 end do
1686
1687 end subroutine s_amr_carry_old_into_slot
1688
1689 !> Regrid phase 6: build each new slot level by level (prolong from the coarse/parent patch, overwrite the overlap from covering
1690 !! stashed old blocks), then reconcile the slot pool, rebuild the fine IB state and re-check the seam topology. Old-only slots
1691 !! are freed after their last reader so device memory does not peak.
1692 impure subroutine s_amr_regrid_rebuild_slots(q_cons_base, boxes, nboxes, old_np, old_ilo, old_ext, old_level)
1693
1694 type(scalar_field), dimension(sys_size), intent(inout) :: q_cons_base
1695 type(t_box), intent(in) :: boxes(:)
1696 integer, intent(in) :: nboxes, old_np, old_ilo(:,:), old_ext(:,:), old_level(:)
1697 integer :: k, kk, i, hh, ks, kks, lev, nh, nlev
1698 integer, allocatable :: last_use(:), held(:), vpos(:)
1699
1700 call s_phase_tic(ph_rg_build)
1701 do k = 1, nboxes
1702 ks = k
1703 if (amr_block_owner(ks) == proc_rank) cycle
1704 amr_cur = ks
1705 call s_set_amr_fine_geometry(boxes(k)%lo, boxes(k)%hi)
1706 end do
1707
1708 ! nlev is replicated so every rank enters every wave
1709 call s_amr_plan_rebuild_visits(boxes, nboxes, old_np, old_ilo, old_ext, old_level, vpos, last_use, held, nh)
1710 nlev = maxval(amr_block_level(1:amr_num_blocks))
1711
1712 do lev = 1, nlev
1713 if (lev == 1) then
1714 call s_amr_l1_fill_exchange(q_cons_base, .true.)
1715 else
1716 call s_amr_parent_fill_exchange(lev, .true.)
1717 end if
1718 do i = 1, amr_n_my
1719 ks = amr_my_blk(i)
1720 k = ks
1721 if (amr_block_level(ks) /= lev) cycle
1722 amr_cur = ks
1723 ! free old-only slots no later box reads (no-op if dead; slots serving new owned boxes live on)
1724 do hh = 1, nh
1725 kk = held(hh)
1726 if (last_use(kk) >= vpos(k)) cycle
1727 kks = kk
1728 if (kks <= amr_num_blocks) then
1729 if (amr_block_owner(kks) == proc_rank) cycle
1730 end if
1731 call s_amr_free_slot(kks)
1732 end do
1733 call s_amr_alloc_slot(ks)
1734 call s_set_amr_fine_geometry(boxes(k)%lo, boxes(k)%hi)
1735 call s_amr_select_slot(ks)
1736 if (lev == 1) then
1737 call s_amr_l1_fill_consume(q_cons_base, ks, .true.)
1738 else
1739 call s_amr_parent_fill_consume(ks, .true.)
1740 end if
1741 ! prolong from the freshly built coarse/parent patch, then overwrite the overlap with old blocks of this level
1742 call s_interpolate_coarse_to_fine()
1743 call s_amr_carry_old_into_slot(ks, boxes, old_np, old_ilo, old_ext, old_level)
1744 end do
1745 call s_amr_fill_wave_done()
1746 end do
1747 deallocate (last_use, held, vpos, amr_mig_win)
1749 call s_phase_toc(ph_rg_build)
1750 call s_phase_tic(ph_rg_rec)
1751
1752 ! sets amr_xchg_coarse_ghosts if any block needs it
1753 call s_amr_reduce_xchg_flag()
1754 ! free the transient slots of old blocks this rank does not now own
1755 call s_amr_reconcile_slots()
1756 ! rebuild fine IB state for the new geometry
1757 if (ib) call s_amr_setup_ib()
1758 call s_amr_select_slot(1)
1760 call s_phase_toc(ph_rg_rec)
1761
1762 end subroutine s_amr_regrid_rebuild_slots
1763
1764end module m_amr_regrid
integer, intent(in) k
integer, intent(in) j
One-way acoustic source injection, Maeda and Colonius JCP (2017).
integer, dimension(:,:), allocatable, public acoustic_supp_hi
(1:3, 1:num_source)
integer, dimension(:,:), allocatable, public acoustic_supp_lo
Global (level-0 index space) bounding box of each source's spatial support, reduced over ranks....
Berger-Rigoutsos clustering of a sparse tag list into separated block boxes, identically on every ran...
impure subroutine, public s_amr_cluster(tags, ntag_in, boxes, nboxes, reduce)
Cluster a sparse tag list tags(1:3, 1:ntag_in) of level-0 cell coords into separated block boxes,...
Rank decomposition, box arithmetic and block ownership (SFC cut, owner lookups, owned-block lists).
impure subroutine, public s_amr_refresh_my_blocks()
Refresh the owned-block list if any owner write has happened since the last refresh....
impure subroutine, public s_amr_body_bbox(i, mrg, blo, bhi)
Margin-padded global coarse-index bounding box of immersed body i (supported analytic geometries only...
pure subroutine, public s_amr_tile_box(lo, hi, out, nt, cap, capped, tsz)
Split box [lo:hi] into an even grid of adjacent sub-boxes each <= amr_maxc_fit per dim (the max a ran...
pure subroutine, public s_amr_sort_by_key(keys, n, ord)
ord(1:n) = item indices in ascending key order. Stable iterative bottom-up merge sort,...
impure subroutine, public s_amr_assign_block_owners()
impure subroutine, public s_amr_expand_box_over_bodies(lo, hi)
Expand a candidate regrid box (global indices) to fully contain every immersed body it overlaps,...
Hierarchical child nesting for AMR regrid: each level's children are tagged by the density-gradient s...
pure subroutine, public s_amr_nest_window(box, mlo, mhi)
The nesting window of a parent: its box inset by amr_cpat_mar, so a child's ghost prolongation reads ...
impure subroutine, public s_amr_regrid_nest_children(boxes, nboxes, box_level)
Regrid phase 3b: nest child boxes inside each level-(l-1) box for l = 2..amr_max_level (parents first...
pure real(wp) function, public f_amr_tag_ind(rp, r0, rm)
Tag indicator from the tagged variable at i+1, i, i-1: central difference over 2*r_i,...
real(wp) function, public f_amr_tag_val_sf(q, ci, cj, ck)
Dynamic regrid for the AMR level set: density-gradient tagging, clustering (m_amr_cluster),...
impure subroutine s_amr_compute_lag_supp(pad_cells)
Collectively recompute the global bubble-cloud exclusion bbox, padded by pad_cells; lag_supp_on = fal...
impure subroutine s_amr_clip_box_clear(lo, hi)
Clip a box clear of the acoustic source and bubble-cloud supports.
impure subroutine s_amr_check_box_disjoint(boxes, nboxes, box_level)
Abort if two same-level boxes overlap (the overlap carry-forward and exchange plans assume disjointne...
pure subroutine s_amr_mig_window(wlo, whi, ilo, lev, flo, fhi)
Fine-index window [flo, fhi] in level-1 old block ilo's stash of the coarse box [wlo,...
impure subroutine s_amr_stash_old_blocks(old_np, old_ilo, old_chi, old_ext, old_owner, old_level, old_owns)
Record the live slots' fine interiors on the device and the old block set (old_*; old_chi = replicate...
impure subroutine s_amr_migrate_old_blocks(boxes, nboxes, box_level, old_np, old_ilo, old_chi, old_owner, old_level, old_owns)
Cross-rank migration as one wave: each old owner ships the bounding box of its overlaps to every othe...
impure subroutine s_amr_regrid_snap_boxes(boxes, nboxes, box_level)
Hysteresis (amr_snap > 0): a box within amr_snap cells per face of a live same-level block takes that...
integer, dimension(3) lag_supp_hi
impure subroutine s_amr_regrid_tag_cells(q_cons_base, tag_grid, sidx)
Regrid phase 1: density-gradient tag field, skipping the global boundary cells; tags over acoustic an...
integer, dimension(:,:), allocatable amr_mig_win
impure subroutine s_amr_regrid_rebuild_slots(q_cons_base, boxes, nboxes, old_np, old_ilo, old_ext, old_level)
Regrid phase 6: build each new slot level by level (prolong from the coarse/parent patch,...
pure subroutine s_amr_clip_box_from_supp(lo, hi, slo, shi)
Clip a box (global indices) clear of one support bbox along the axis/side keeping the most extent (ti...
pure logical function f_in_lag_support(gi, gj, gk)
True iff global level-0 cell (gi, gj, gk) lies inside the Lagrangian-cloud exclusion bbox.
real(wp), dimension(:), allocatable amr_mig_rq
impure subroutine s_amr_commit_boxes(boxes, nboxes, box_level)
Commit the new regions, levels and owners, after checking proper nesting.
impure subroutine s_amr_mig_free(pool)
Free a migration pool (device-resident when amr_fw_dev).
integer, dimension(3) lag_supp_lo
Padded global coarse-index bbox of the Lagrangian bubble cloud; blocks and regrid boxes stay clear of...
impure subroutine s_amr_overlap_copy_wire(vloc, vm, vn, vp, sh, l1, l2, l3, h1, h2, h3,
impure subroutine s_amr_clip_box_from_sources(lo, hi)
Clip a box (global indices) clear of every acoustic source support; may empty it (hi < lo).
impure subroutine s_amr_regrid_boxes_unchanged(boxes, nboxes, box_level, same)
True iff the box set and levels equal the live slots (a rebuild would reproduce them).
real(wp), dimension(:), allocatable amr_mig_sq
Migration send/receive pools (freed after the regrid); amr_mig_win(:,old block) = payload start in am...
impure subroutine s_amr_local_tags(tag_grid, sidx, tags, ntag)
This rank's tagged cells as global level-0 coordinates; rank interiors are disjoint,...
impure subroutine s_amr_regrid_stash_migrate(boxes, nboxes, box_level, old_np, old_ilo, old_ext, old_level, old_owns)
Regrid phase 5: stash the old block set, commit the new one, and migrate stashed old blocks point-to-...
pure logical function f_in_acoustic_support(gi, gj, gk)
True iff global level-0 cell (gi, gj, gk) lies inside any acoustic source support bbox.
impure subroutine s_amr_gather_body_groups(glo, ghi, clo, chi, ng, mrg, nbody)
One margin-padded box (glo:ghi) and body extent (clo:chi) per immersed body, identical on every rank.
impure subroutine s_amr_carry_old_into_slot(ks, boxes, old_np, old_ilo, old_ext, old_level)
Overwrite the freshly prolonged slot ks with the overlapping old blocks of its level: from the migrat...
impure subroutine s_amr_carve_tile_around_bodies(boxes, nboxes, out, nt, capt)
Regrid phase 3a (IB): tile the boxes around the immersed bodies. Each body (bbox + margin; close bodi...
impure subroutine s_amr_mig_pack_device(loc, l1, l2, l3, h1, h2, h3, buf)
Pack fine window [l, h] of an owned old block's stash (device store) into its wire slice buf; gi fast...
impure subroutine, public s_amr_check_seam_topology()
Abort on same-level seam topologies no halo reconciles: partial-transverse face adjacency or box inte...
impure subroutine s_amr_resolve_body_groups(glo, ghi, clo, chi, ng, gap)
Separate body groups that face each other across < gap empty cells or overlap, reject groups over the...
impure subroutine s_amr_plan_rebuild_visits(boxes, nboxes, old_np, old_ilo, old_ext, old_level, vpos, last_use, held, nh)
Visit order of the owned new boxes (level-major, vpos) and, per held old slot, the last visit that re...
impure subroutine s_amr_regrid_cluster_tags(tag_grid, sidx, boxes, nboxes)
Regrid phase 2: cluster this rank's tags (rank-invariant, via signature reductions) into separated bo...
impure subroutine, public s_amr_regrid(q_cons_base)
Regrid: tag by density gradient, cluster, shape, nest and rebuild the slots, carrying old data into t...
impure subroutine s_amr_check_box_caps(boxes, nboxes, box_level)
Abort if a box exceeds its level's slot cap, amr_maxc_fit/amr_ref_ratio**(lev-1) coarse cells (it wou...
impure subroutine s_amr_overlap_copy_st(vloc, vm, vn, vp, sh, l1, l2, l3, h1, h2, h3,
Overwrite the new block's prolonged cons with the covering old block's stashed detail (old fine index...
impure subroutine s_amr_regrid_dormant_seed(boxes, nboxes)
One coarse cell at the domain center, which shaping pads into the dormant block that replaces the hie...
impure subroutine s_amr_report_coverage(boxes, nboxes, box_level)
Print the share of the base domain covered by each level's boxes (rank 0, rank_time_wrt).
pure logical function f_amr_boxes_compatible(alo, ahi, blo, bhi)
True iff two same-level blocks can coexist: disjoint, and either >= 2 empty coarse cells apart or fac...
impure subroutine s_amr_check_boxes_agree(boxes, nboxes)
Abort if ranks hold different box lists (each rank sees only nearby IB bodies; a mismatch hangs the o...
impure subroutine s_amr_stash_copy_device(loc, e1, e2, e3)
Device cons->stor stash copy of one owned old block's fine interior (the store is device-authoritativ...
impure subroutine s_amr_regrid_shape_boxes(boxes, nboxes)
Regrid phase 3: pad and clamp each box, clip it clear of the acoustic/bubble supports,...
pure subroutine s_amr_merge_body_groups(glo, ghi, clo, chi, ng, i, j)
Fold body group j into group i (bbox and body-extent unions) and move the last group into slot j.
pure subroutine s_amr_box_carve(box, glo, ghi, gap, out, n)
Append to out(n+1:) the pieces of box that stay gap empty cells clear of [glo:ghi] and of each other,...
Module-scope AMR state (block pool, flat field store, wave buffers) and the leaf helpers every AMR mo...
integer amr_cpat_mar
coarse-cell stencil reach (f_amr_cpat_mar)
pure subroutine s_amr_phys_to_cells(pmin, pmax, pad_cells, blo, bhi)
Convert a physical-space bbox to a global coarse-index bbox padded by pad_cells (uniform spacing; the...
type(t_level), dimension(:), allocatable amr_slots
Fixed pool of refined-block slots; the working slot amr_cur selects which one per-block routines oper...
integer, dimension(:), allocatable amr_loc_of
Dense local index for live slots: amr_slots is sparse over 1:amr_max_blocks, the field store is dense...
logical, dimension(:), allocatable amr_slot_live
logical amr_fw_dev
Device-resident wire pools: with rdma_mpi MPI sends/receives them by device address,...
pure logical function f_amr_boxes_overlap(alo, ahi, blo, bhi)
Do two coarse-index boxes [alo:ahi] and [blo:bhi] overlap? Collapsed dims never disqualify.
integer, parameter amr_band_migrate
logical amr_seam_pairs_dirty
real(stp), dimension(:,:,:,:,:), allocatable amr_cons_st
Flat per-block field store (x, y, z, var, local slot) so one kernel can run over every live block; al...
real(stp), dimension(:,:,:,:,:), allocatable amr_stor_st
integer, dimension(:), allocatable amr_my_blk
Block indices this rank owns, ascending (the paired MPI_SENDRECVs depend on the order); built in s_am...
integer, dimension(3) amr_maxc_fit
Regrid box size cap per dim (fixed for the run, identical on all ranks; 1 in collapsed dims): a box o...
MPI point-to-point waves for AMR exchange: post all receives, pack, post all sends,...
Block-structured AMR: up to amr_max_blocks refined blocks (2:1 or 4:1 per amr_ref_ratio),...
Real fields of a bubble MPI slot, as (names, indices, count) in pack order. Used by both pack and unp...
impure subroutine s_lag_cloud_bbox_local(pmin, pmax)
Rank-local bounding box of Lagrangian bubble positions; pmin > pmax when the rank holds none.
Compile-time constant parameters: default values, tolerances, and physical constants.
integer, parameter mapcells
Number of cells around the bubble where the smoothening function will have effect.
Shared derived types for field data, patch geometry, bubble dynamics, and MPI I/O structures.
Global parameters for the computational domain, fluid properties, and simulation algorithm configurat...
integer, dimension(:,:), allocatable amr_region_lo_all
Per-slot mirrors (allocated 1:amr_max_blocks by m_amr): region box, rank intersection and ownership f...
integer proc_rank
Rank of the local processor.
integer(8) amr_mesh_epoch
Monotone mesh epoch, incremented wherever amr_seam_pairs_dirty is set and at the end of each slot rec...
integer, dimension(:,:), allocatable amr_region_hi_all
integer, dimension(:), allocatable amr_block_level
Refinement level of each active block (1..amr_max_level) and the deepest level populated....
integer amr_num_blocks
Number of active AMR fine-block slots (>= 1) and the working slot index selecting which one the per-b...
MPI communication layer: domain decomposition, halo exchange, reductions, and parallel I/O setup.
impure subroutine s_mpi_abort(prnt, code)
The subroutine terminates the MPI execution environment.
impure subroutine s_mpi_allreduce_min(var_loc, var_glb)
Reduce a local real value to its global minimum across all MPI ranks.
impure subroutine s_mpi_allreduce_max(var_loc, var_glb)
Reduce a local real value to its global maximum across all MPI ranks.
MPI halo exchange, domain decomposition, and buffer packing/unpacking for the simulation solver.
Per-phase wall-time budget of the AMR step, reported when rank_time_wrt is on. Brackets are disjoint ...
An index-space rectangle in global cell indices. In v1, one t_box = one rank's subdomain....