Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AMRGridRefinementHandler.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
17
20#include "shambase/memory.hpp"
22#include "shamcomm/logs.hpp"
28#include <stdexcept>
29#include <variant>
30
31namespace {
32
44 template<class AMRBlock, class TgridVec>
45 inline u32 get_merged_cell_index(TgridVec sibling_min, TgridVec merged_min) {
46 return AMRBlock::get_index(
47 {static_cast<u32>(sibling_min.x() != merged_min.x()),
48 static_cast<u32>(sibling_min.y() != merged_min.y()),
49 static_cast<u32>(sibling_min.z() != merged_min.z())});
50 }
51
52} // namespace
53
54template<class Tvec, class TgridVec>
55template<class UserAcc, class... T>
60 T &&...args) {
61
62 using namespace shamrock::patch;
63
64 u64 tot_refine = 0;
65 u64 tot_derefine = 0;
66
67 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
68 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
69
70 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
71 u64 id_patch = cur_p.id_patch;
72
73 // create the refine and derefine flags buffers
74 u32 obj_cnt = pdat.get_obj_cnt();
75
76 sham::DeviceBuffer<u32> refine_flags(obj_cnt, dev_sched);
77 sham::DeviceBuffer<u32> derefine_flags(obj_cnt, dev_sched);
78
79 {
80 sham::EventList depends_list;
81
82 UserAcc uacc(depends_list, id_patch, cur_p, pdat, args...);
83
84 auto refine_acc = refine_flags.get_write_access(depends_list);
85 auto derefine_acc = derefine_flags.get_write_access(depends_list);
86
87 // fill in the flags
88 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
89 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
90 bool flag_refine = false;
91 bool flag_derefine = false;
92 uacc.refine_criterion(gid.get_linear_id(), uacc, flag_refine, flag_derefine);
93
94 // This is just a safe guard to avoid this nonsensicall case
95 if (flag_refine && flag_derefine) {
96 flag_derefine = false;
97 }
98
99 refine_acc[gid] = (flag_refine) ? 1 : 0;
100 derefine_acc[gid] = (flag_derefine) ? 1 : 0;
101 });
102 });
103
104 sham::EventList resulting_events;
105 resulting_events.add_event(e);
106
107 refine_flags.complete_event_state(resulting_events);
108 derefine_flags.complete_event_state(resulting_events);
109
110 uacc.finalize(resulting_events, id_patch, cur_p, pdat, args...);
111 }
112
113 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
114 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
115
116 // The kernel below reads the flags of the 8 blocks following each block while writing the
117 // flag of the current one, so the input flags are read from a copy (otherwise the result
118 // depends on the execution order of the work-items)
119 sham::DeviceBuffer<u32> derefine_flags_in(obj_cnt, dev_sched);
120 derefine_flags.copy_range(0, obj_cnt, derefine_flags_in);
121
122 sham::EventList depends_list;
123 auto acc_min = buf_cell_min.get_read_access(depends_list);
124 auto acc_max = buf_cell_max.get_read_access(depends_list);
125 auto acc_want_merge = derefine_flags_in.get_read_access(depends_list);
126 auto acc_merge_flag = derefine_flags.get_write_access(depends_list);
127
128 // keep only derefine flags on only if the eight cells want to merge and if they can
129 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
130 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
131 u32 id = gid.get_linear_id();
132
133 std::array<BlockCoord, split_count> blocks;
134 bool do_merge = true;
135
136 // This avoid the case where we are in the last block of the buffer to avoid the
137 // out-of-bound read
138 if (id + split_count <= obj_cnt) {
139 bool all_want_to_merge = true;
140
141 for (u32 lid = 0; lid < split_count; lid++) {
142 blocks[lid] = BlockCoord{acc_min[gid + lid], acc_max[gid + lid]};
143 all_want_to_merge = all_want_to_merge && acc_want_merge[gid + lid];
144 }
145
146 do_merge = all_want_to_merge && BlockCoord::are_mergeable(blocks);
147
148 } else {
149 do_merge = false;
150 }
151
152 acc_merge_flag[gid] = do_merge;
153 });
154 });
155
156 buf_cell_min.complete_event_state(e);
157 buf_cell_max.complete_event_state(e);
158 derefine_flags_in.complete_event_state(e);
159 derefine_flags.complete_event_state(e);
160
162 // refinement
164
165 // perform stream compactions on the refinement flags
166 auto buf_refine = shamalgs::numeric::stream_compact(dev_sched, refine_flags, obj_cnt);
167
168 shamlog_debug_ln(
169 "AMRGrid", "patch ", id_patch, "refine block count = ", buf_refine.get_size());
170
171 tot_refine += buf_refine.get_size();
172
173 // add the results to the map
174 dd_refine_list.add_obj(id_patch, std::move(buf_refine));
175
177 // derefinement
179
180 // perform stream compactions on the derefinement flags
181 auto buf_derefine = shamalgs::numeric::stream_compact(dev_sched, derefine_flags, obj_cnt);
182
183 shamlog_debug_ln(
184 "AMRGrid", "patch ", id_patch, "merge block count = ", buf_derefine.get_size());
185
186 tot_derefine += buf_derefine.get_size();
187
188 // add the results to the map
189 dd_derefine_list.add_obj(id_patch, std::move(buf_derefine));
190 });
191
192 logger::info_ln("AMRGrid", "on this process", tot_refine, "blocks were refined");
194 "AMRGrid", "on this process", tot_derefine * split_count, "blocks were derefined");
195}
196template<class Tvec, class TgridVec>
197template<class UserAcc>
200
201 using namespace shamrock::patch;
202
203 u64 sum_block_count = 0;
204
205 bool new_cell_were_added = false;
206
207 scheduler().for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
208 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
209
210 u32 old_obj_cnt = pdat.get_obj_cnt();
211
212 sham::DeviceBuffer<u32> &refine_flags = dd_refine_list.get(id_patch);
213
214 if (refine_flags.get_size() > 0) {
215
216 // alloc memory for the new blocks to be created
217 pdat.expand(refine_flags.get_size() * (split_count - 1));
218
219 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
220 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
221
222 sham::EventList depends_list;
223 auto block_bound_low = buf_cell_min.get_write_access(depends_list);
224 auto block_bound_high = buf_cell_max.get_write_access(depends_list);
225 UserAcc uacc(depends_list, pdat);
226 auto index_to_ref = refine_flags.get_read_access(depends_list);
227
228 // Refine the block (set the positions) and fill the corresponding fields
229 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
230 u32 start_index_push = old_obj_cnt;
231
232 constexpr u32 new_splits = split_count - 1;
233
234 cgh.parallel_for(sycl::range<1>(refine_flags.get_size()), [=](sycl::item<1> gid) {
235 u32 tid = gid.get_linear_id();
236
237 u32 idx_to_refine = index_to_ref[tid];
238
239 // gen splits coordinates
240 BlockCoord cur_block{
241 block_bound_low[idx_to_refine], block_bound_high[idx_to_refine]};
242
243 std::array<BlockCoord, split_count> block_coords
244 = BlockCoord::get_split(cur_block.bmin, cur_block.bmax);
245
246 // generate index for the refined blocks
247 std::array<u32, split_count> blocks_ids;
248 blocks_ids[0] = idx_to_refine;
249
250 // generate index for the new blocks (the current index is reused for the first
251 // new block, the others are pushed at the end of the patchdata)
252#pragma unroll
253 for (u32 pid = 0; pid < new_splits; pid++) {
254 blocks_ids[pid + 1] = start_index_push + tid * new_splits + pid;
255 }
256
257 // write coordinates
258
259#pragma unroll
260 for (u32 pid = 0; pid < split_count; pid++) {
261 block_bound_low[blocks_ids[pid]] = block_coords[pid].bmin;
262 block_bound_high[blocks_ids[pid]] = block_coords[pid].bmax;
263 }
264
265 // user lambda to fill the fields
266 uacc.apply_refine(idx_to_refine, cur_block, blocks_ids, block_coords, uacc);
267 });
268 });
269
270 sham::EventList resulting_events{e};
271
272 buf_cell_min.complete_event_state(resulting_events);
273 buf_cell_max.complete_event_state(resulting_events);
274
275 uacc.finalize(resulting_events, pdat);
276
277 refine_flags.complete_event_state(resulting_events);
278 }
279
280 sum_block_count += pdat.get_obj_cnt();
281 new_cell_were_added = new_cell_were_added || refine_flags.get_size() > 0;
282 });
283
284 logger::info_ln("AMRGrid", "process block count =", sum_block_count);
285
286 return new_cell_were_added;
287}
288
289template<class Tvec, class TgridVec>
290template<class UserAcc>
294
295 using namespace shamrock::patch;
296
297 bool cell_were_removed = false;
298
299 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
300 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
301
302 scheduler().for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
303 u32 old_obj_cnt = pdat.get_obj_cnt();
304
305 sham::DeviceBuffer<u32> &derefine_flags = dd_derefine_list.get(id_patch);
306
307 if (derefine_flags.get_size() > 0) {
308
309 // init flag table
310 sham::DeviceBuffer<u32> keep_block_flag(old_obj_cnt, dev_sched);
311 keep_block_flag.fill(1);
312
313 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
314 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
315
316 sham::EventList depends_list;
317 auto block_bound_low = buf_cell_min.get_write_access(depends_list);
318 auto block_bound_high = buf_cell_max.get_write_access(depends_list);
319 UserAcc uacc(depends_list, pdat);
320 auto index_to_deref = derefine_flags.get_read_access(depends_list);
321 auto flag_keep = keep_block_flag.get_write_access(depends_list);
322
323 // edit block content + make flag of blocks to keep
324 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
325 cgh.parallel_for(sycl::range<1>(derefine_flags.get_size()), [=](sycl::item<1> gid) {
326 u32 tid = gid.get_linear_id();
327
328 u32 idx_to_derefine = index_to_deref[gid];
329
330 // compute old block indexes
331 std::array<u32, split_count> old_indexes;
332#pragma unroll
333 for (u32 pid = 0; pid < split_count; pid++) {
334 old_indexes[pid] = idx_to_derefine + pid;
335 }
336
337 // load block coords
338 std::array<BlockCoord, split_count> block_coords;
339#pragma unroll
340 for (u32 pid = 0; pid < split_count; pid++) {
341 block_coords[pid] = BlockCoord{
342 block_bound_low[old_indexes[pid]], block_bound_high[old_indexes[pid]]};
343 }
344
345 // make new block coord
346 BlockCoord merged_block_coord = BlockCoord::get_merge(block_coords);
347
348 // write new coord
349 block_bound_low[idx_to_derefine] = merged_block_coord.bmin;
350 block_bound_high[idx_to_derefine] = merged_block_coord.bmax;
351
352// flag the old blocks for removal
353#pragma unroll
354 for (u32 pid = 1; pid < split_count; pid++) {
355 flag_keep[idx_to_derefine + pid] = 0;
356 }
357
358 // user lambda to fill the fields
359 uacc.apply_derefine(
360 old_indexes, block_coords, idx_to_derefine, merged_block_coord, uacc);
361 });
362 });
363
364 sham::EventList resulting_events{e};
365
366 buf_cell_min.complete_event_state(resulting_events);
367 buf_cell_max.complete_event_state(resulting_events);
368
369 uacc.finalize(resulting_events, pdat);
370
371 keep_block_flag.complete_event_state(resulting_events);
372 derefine_flags.complete_event_state(resulting_events);
373
374 // stream compact the flags
375 auto buf_keep
376 = shamalgs::numeric::stream_compact(dev_sched, keep_block_flag, old_obj_cnt);
377
378 shamlog_debug_ln(
379 "AMR Grid",
380 "patch",
381 id_patch,
382 "derefine block count ",
383 old_obj_cnt,
384 "->",
385 buf_keep.get_size());
386
387 if (buf_keep.get_size() == 0) {
388 throw std::runtime_error("buf keep must contain something at this point");
389 }
390
391 // remap pdat according to stream compact
392 pdat.index_remap_resize(buf_keep, buf_keep.get_size());
393
394 cell_were_removed = cell_were_removed || derefine_flags.get_size() > 0;
395 }
396 });
397
398 return cell_were_removed;
399}
400
401template<class Tvec, class TgridVec>
402template<class UserAccCrit, class UserAccSplit, class UserAccMerge>
405
406 // Ensure that the blocks are sorted before refinement
407 AMRSortBlocks block_sorter(context, solver_config, storage);
408 block_sorter.reorder_amr_blocks();
409
410 // get refine and derefine list
413
414 gen_refine_block_changes<UserAccCrit>(dd_refine_list, dd_derefine_list);
415
417 // Note that this only add new blocks at the end of the patchdata
418 internal_refine_grid<UserAccSplit>(std::move(dd_refine_list));
419
421 // Note that this will perform the merge then remove the old blocks
422 // This is ok to call straight after the refine without edditing the index list in derefine_list
423 // since no permutations were applied in internal_refine_grid and no cells can be both refined
424 // and derefined in the same pass
425 internal_derefine_grid<UserAccMerge>(std::move(dd_derefine_list));
426}
427
428template<class Tvec, class TgridVec>
431
432 class RefineCritBlock {
433 public:
434 const TgridVec *block_low_bound;
435 const TgridVec *block_high_bound;
436 const Tscal *block_density_field;
437
438 Tscal one_over_Nside = 1. / AMRBlock::Nside;
439
440 Tscal dxfact;
441 Tscal wanted_mass;
442
443 RefineCritBlock(
444 sham::EventList &depends_list,
445 u64 id_patch,
446 shamrock::patch::Patch p,
447 shamrock::patch::PatchDataLayer &pdat,
448 Tscal dxfact,
449 Tscal wanted_mass)
450 : dxfact(dxfact), wanted_mass(wanted_mass) {
451
452 block_low_bound = pdat.get_field<TgridVec>(0).get_buf().get_read_access(depends_list);
453 block_high_bound = pdat.get_field<TgridVec>(1).get_buf().get_read_access(depends_list);
454 block_density_field = pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
455 .get_buf()
456 .get_read_access(depends_list);
457 }
458
459 void finalize(
460 sham::EventList &resulting_events,
461 u64 id_patch,
462 shamrock::patch::Patch p,
463 shamrock::patch::PatchDataLayer &pdat,
464 Tscal dxfact,
465 Tscal wanted_mass) {
466
467 sham::DeviceBuffer<i64_3> &buf_cell_low_bound = pdat.get_field<i64_3>(0).get_buf();
468 sham::DeviceBuffer<i64_3> &buf_cell_high_bound = pdat.get_field<i64_3>(1).get_buf();
469
470 buf_cell_low_bound.complete_event_state(resulting_events);
471 buf_cell_high_bound.complete_event_state(resulting_events);
472 pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
473 .get_buf()
474 .complete_event_state(resulting_events);
475 }
476
477 void refine_criterion(
478 u32 block_id, RefineCritBlock acc, bool &should_refine, bool &should_derefine) const {
479
480 TgridVec low_bound = acc.block_low_bound[block_id];
481 TgridVec high_bound = acc.block_high_bound[block_id];
482
483 Tvec block_cell_size
484 = (high_bound - low_bound).template convert<Tscal>() * (dxfact * one_over_Nside);
485
486 Tscal sum_mass = 0;
487 for (u32 i = 0; i < AMRBlock::block_size; i++) {
488 sum_mass += acc.block_density_field[i + block_id * AMRBlock::block_size];
489 }
490 sum_mass *= block_cell_size.x() * block_cell_size.y() * block_cell_size.z();
491
492 if (sum_mass > wanted_mass * 8) {
493 should_refine = true;
494 should_derefine = false;
495 } else if (sum_mass < wanted_mass) {
496 should_refine = false;
497 should_derefine = true;
498 } else {
499 should_refine = false;
500 should_derefine = false;
501 }
502
503 should_refine = should_refine && (high_bound.x() - low_bound.x() > AMRBlock::Nside);
504 should_refine = should_refine && (high_bound.y() - low_bound.y() > AMRBlock::Nside);
505 should_refine = should_refine && (high_bound.z() - low_bound.z() > AMRBlock::Nside);
506 }
507 };
508
509 class RefineCellAccessor {
510 public:
511 f64 *rho;
512 f64_3 *rho_vel;
513 f64 *rhoE;
514
515 RefineCellAccessor(sham::EventList &depends_list, shamrock::patch::PatchDataLayer &pdat) {
516
517 rho = pdat.get_field<f64>(2).get_buf().get_write_access(depends_list);
518 rho_vel = pdat.get_field<f64_3>(3).get_buf().get_write_access(depends_list);
519 rhoE = pdat.get_field<f64>(4).get_buf().get_write_access(depends_list);
520 }
521
522 void finalize(sham::EventList &resulting_events, shamrock::patch::PatchDataLayer &pdat) {
523 pdat.get_field<f64>(2).get_buf().complete_event_state(resulting_events);
524 pdat.get_field<f64_3>(3).get_buf().complete_event_state(resulting_events);
525 pdat.get_field<f64>(4).get_buf().complete_event_state(resulting_events);
526 }
527
528 void apply_refine(
529 u32 cur_idx,
530 BlockCoord cur_coords,
531 std::array<u32, 8> new_blocks,
532 std::array<BlockCoord, 8> new_block_coords,
533 RefineCellAccessor acc) const {
534
535 auto get_coord_ref = [](u32 i) -> std::array<u32, dim> {
536 constexpr u32 NsideBlockPow = 1;
537 constexpr u32 Nside = 1U << NsideBlockPow;
538
539 if constexpr (dim == 3) {
540 const u32 tmp = i >> NsideBlockPow;
541 return {i % Nside, (tmp) % Nside, (tmp) >> NsideBlockPow};
542 }
543 };
544
545 auto get_index_block = [](std::array<u32, dim> coord) -> u32 {
546 constexpr u32 NsideBlockPow = 1;
547 constexpr u32 Nside = 1U << NsideBlockPow;
548
549 if constexpr (dim == 3) {
550 return coord[0] + Nside * coord[1] + Nside * Nside * coord[2];
551 }
552 };
553
554 auto get_gid_write = [&](std::array<u32, dim> &glid) -> u32 {
555 std::array<u32, dim> bid
556 = {glid[0] >> AMRBlock::NsideBlockPow,
557 glid[1] >> AMRBlock::NsideBlockPow,
558 glid[2] >> AMRBlock::NsideBlockPow};
559
560 // logger::raw_ln(glid,bid);
561 return new_blocks[get_index_block(bid)] * AMRBlock::block_size
562 + AMRBlock::get_index(
563 {glid[0] % AMRBlock::Nside,
564 glid[1] % AMRBlock::Nside,
565 glid[2] % AMRBlock::Nside});
566 };
567
568 std::array<f64, AMRBlock::block_size> old_rho_block;
569 std::array<f64_3, AMRBlock::block_size> old_rho_vel_block;
570 std::array<f64, AMRBlock::block_size> old_rhoE_block;
571
572 // save old block
573 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
574
575 auto [lx, ly, lz] = get_coord_ref(loc_id);
576 u32 old_cell_idx = cur_idx * AMRBlock::block_size + loc_id;
577 old_rho_block[loc_id] = acc.rho[old_cell_idx];
578 old_rho_vel_block[loc_id] = acc.rho_vel[old_cell_idx];
579 old_rhoE_block[loc_id] = acc.rhoE[old_cell_idx];
580 }
581
582 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
583
584 auto [lx, ly, lz] = get_coord_ref(loc_id);
585 u32 old_cell_idx = cur_idx * AMRBlock::block_size + loc_id;
586
587 Tscal rho_block = old_rho_block[loc_id];
588 Tvec rho_vel_block = old_rho_vel_block[loc_id];
589 Tscal rhoE_block = old_rhoE_block[loc_id];
590 for (u32 subdiv_lid = 0; subdiv_lid < 8; subdiv_lid++) {
591
592 auto [sx, sy, sz] = get_coord_ref(subdiv_lid);
593
594 std::array<u32, 3> glid = {lx * 2 + sx, ly * 2 + sy, lz * 2 + sz};
595
596 u32 new_cell_idx = get_gid_write(glid);
597 /*
598 if (1627 == cur_idx) {
599 logger::raw_ln(
600 cur_idx,
601 "set cell ",
602 new_cell_idx,
603 " from cell",
604 old_cell_idx,
605 "old",
606 rho_block,
607 rho_vel_block,
608 rhoE_block);
609 }
610 */
611 acc.rho[new_cell_idx] = rho_block;
612 acc.rho_vel[new_cell_idx] = rho_vel_block;
613 acc.rhoE[new_cell_idx] = rhoE_block;
614 }
615 }
616 }
617
618 void apply_derefine(
619 std::array<u32, 8> old_blocks,
620 std::array<BlockCoord, 8> old_coords,
621 u32 new_cell,
622 BlockCoord new_coord,
623
624 RefineCellAccessor acc) const {
625
626 std::array<f64, AMRBlock::block_size> rho_block;
627 std::array<f64_3, AMRBlock::block_size> rho_vel_block;
628 std::array<f64, AMRBlock::block_size> rhoE_block;
629
630 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
631 rho_block[cell_id] = {};
632 rho_vel_block[cell_id] = {};
633 rhoE_block[cell_id] = {};
634 }
635
636 for (u32 pid = 0; pid < 8; pid++) {
637 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
638 rho_block[cell_id] += acc.rho[old_blocks[pid] * AMRBlock::block_size + cell_id];
639 rho_vel_block[cell_id]
640 += acc.rho_vel[old_blocks[pid] * AMRBlock::block_size + cell_id];
641 rhoE_block[cell_id]
642 += acc.rhoE[old_blocks[pid] * AMRBlock::block_size + cell_id];
643 }
644 }
645
646 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
647 rho_block[cell_id] /= 8;
648 rho_vel_block[cell_id] /= 8;
649 rhoE_block[cell_id] /= 8;
650 }
651
652 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
653 u32 newcell_idx = new_cell * AMRBlock::block_size + cell_id;
654 acc.rho[newcell_idx] = rho_block[cell_id];
655 acc.rho_vel[newcell_idx] = rho_vel_block[cell_id];
656 acc.rhoE[newcell_idx] = rhoE_block[cell_id];
657 }
658 }
659 };
660
661 using AMRmode_None = typename AMRMode<Tvec, TgridVec>::None;
662 using AMRmode_DensityBased = typename AMRMode<Tvec, TgridVec>::DensityBased;
663
664 bool has_cell_order_changed = false;
665
666 if (AMRmode_None *cfg = std::get_if<AMRmode_None>(&solver_config.amr_mode.config)) {
667 // no refinment here turn around there is nothing to see
668 } else if (
669 AMRmode_DensityBased *cfg
670 = std::get_if<AMRmode_DensityBased>(&solver_config.amr_mode.config)) {
671 Tscal dxfact(solver_config.grid_coord_to_pos_fact);
672
673 // get refine and derefine list
676
677 gen_refine_block_changes_old<RefineCritBlock>(
678 refine_list, derefine_list, dxfact, cfg->crit_mass);
679
681 // Note that this only add new blocks at the end of the patchdata
682 bool change_refine = internal_refine_grid_old<RefineCellAccessor>(std::move(refine_list));
683
685 // Note that this will perform the merge then remove the old blocks
686 // This is ok to call straight after the refine without edditing the index list in
687 // derefine_list since no permutations were applied in internal_refine_grid and no cells can
688 // be both refined and derefined in the same pass
689 bool change_derefine
690 = internal_derefine_grid_old<RefineCellAccessor>(std::move(derefine_list));
691
692 has_cell_order_changed = has_cell_order_changed || (change_refine || change_derefine);
693 } else {
694 // SolverConfig::check_config rejects the other criteria when old AMR is enabled,
695 // this is a safety net for criteria added later without old AMR support
697 "this AMR criterion is not implemented in the old AMR (cfg.set_amr_mode_old(False))");
698 }
699
700 if (has_cell_order_changed) {
701 // Ensure that the blocks are sorted before refinement
702 AMRSortBlocks block_sorter(context, solver_config, storage);
703 block_sorter.reorder_amr_blocks();
704 }
705}
706
707template<class Tvec, class TgridVec>
708template<class UserAcc, class... T>
713 T &&...args) {
714
715 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
716 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
717
718 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
719 u64 id_patch = cur_p.id_patch;
720
721 // create the refine and derefine flags buffers
722 u32 obj_cnt = pdat.get_obj_cnt();
723
724 sham::DeviceBuffer<u32> refine_flags(obj_cnt, dev_sched);
725 sham::DeviceBuffer<u32> derefine_flags(obj_cnt, dev_sched);
726
727 {
728 sham::EventList depends_list;
729
730 UserAcc uacc(depends_list, storage, id_patch, cur_p, pdat, args...);
731
732 auto refine_acc = refine_flags.get_write_access(depends_list);
733 auto derefine_acc = derefine_flags.get_write_access(depends_list);
734
735 // fill in the flags
736 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
737 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
738 bool flag_refine = false;
739 bool flag_derefine = false;
740 uacc.refine_criterion_new(
741 gid.get_linear_id(), uacc, flag_refine, flag_derefine);
742
743 // This is just a safe guard to avoid this nonsensicall case
744 if (flag_refine && flag_derefine) {
745 flag_derefine = false;
746 }
747
748 refine_acc[gid] = (flag_refine) ? 1 : 0;
749 derefine_acc[gid] = (flag_derefine) ? 1 : 0;
750 });
751 });
752
753 sham::EventList resulting_events;
754 resulting_events.add_event(e);
755
756 refine_flags.complete_event_state(resulting_events);
757 derefine_flags.complete_event_state(resulting_events);
758
759 uacc.finalize_new(resulting_events, storage, id_patch, cur_p, pdat, args...);
760 }
761
762 dd_refine_flags.add_obj(id_patch, std::move(refine_flags));
763 dd_derefine_flags.add_obj(id_patch, std::move(derefine_flags));
764 });
765}
766
773template<class Tvec, class TgridVec>
777
778 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
779 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
780 u64 id_patch = cur_p.id_patch;
781 sham::DeviceBuffer<u32> &patch_refine_flags = dd_refine_flags.get(id_patch);
782 u32 obj_cnt = pdat.get_obj_cnt();
783
784 // blocks graph in each direction for the current patch
785 AMRGraph &block_graph_neighs_xp = shambase::get_check_ref(storage.block_graph_edge)
786 .get_refs_dir(Direction_::xp)
787 .get(id_patch);
788 AMRGraph &block_graph_neighs_xm = shambase::get_check_ref(storage.block_graph_edge)
789 .get_refs_dir(Direction_::xm)
790 .get(id_patch);
791 AMRGraph &block_graph_neighs_yp = shambase::get_check_ref(storage.block_graph_edge)
792 .get_refs_dir(Direction_::yp)
793 .get(id_patch);
794 AMRGraph &block_graph_neighs_ym = shambase::get_check_ref(storage.block_graph_edge)
795 .get_refs_dir(Direction_::ym)
796 .get(id_patch);
797 AMRGraph &block_graph_neighs_zp = shambase::get_check_ref(storage.block_graph_edge)
798 .get_refs_dir(Direction_::zp)
799 .get(id_patch);
800 AMRGraph &block_graph_neighs_zm = shambase::get_check_ref(storage.block_graph_edge)
801 .get_refs_dir(Direction_::zm)
802 .get(id_patch);
803 // get levels in the current patch
804 sham::DeviceBuffer<TgridUint> &buf_amr_block_levels
805 = shambase::get_check_ref(storage.amr_block_levels).get_buf(id_patch);
806
807 std::shared_ptr<sham::DeviceScheduler> dev_sched
808 = shamsys::instance::get_compute_scheduler_ptr();
809
810 sham::DeviceBuffer<u32> changed_buf(1, dev_sched);
811
812 for (u32 pass = 0; pass < 100; pass++) {
813
814 changed_buf.set_val_at_idx(0, 0);
815
816 sham::EventList depend_list;
817 AMRGraphLinkiterator block_graph_xp
818 = block_graph_neighs_xp.get_read_access(depend_list);
819 AMRGraphLinkiterator block_graph_xm
820 = block_graph_neighs_xm.get_read_access(depend_list);
821 AMRGraphLinkiterator block_graph_yp
822 = block_graph_neighs_yp.get_read_access(depend_list);
823 AMRGraphLinkiterator block_graph_ym
824 = block_graph_neighs_ym.get_read_access(depend_list);
825 AMRGraphLinkiterator block_graph_zp
826 = block_graph_neighs_zp.get_read_access(depend_list);
827 AMRGraphLinkiterator block_graph_zm
828 = block_graph_neighs_zm.get_read_access(depend_list);
829
830 auto acc_amr_levels = buf_amr_block_levels.get_read_access(depend_list);
831 auto acc_changed = changed_buf.get_write_access(depend_list);
832
833 auto acc_ref_flags = patch_refine_flags.get_write_access(depend_list);
834
835 auto e = q.submit(depend_list, [&](sycl::handler &cgh) {
836 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
837 u32 block_id = gid.get_linear_id();
838
839 u32 cur_ref_flag = acc_ref_flags[block_id];
840
841 if (!cur_ref_flag)
842 return;
843
844 auto cur_block_level = acc_amr_levels[block_id];
845
846 auto check_2To1_ref = [&](u32 nid) {
847 if (nid >= obj_cnt)
848 return;
849
850 // get refinement flag and amr level of the neighborh block
851 u32 neigh_ref_flag = acc_ref_flags[nid];
852 auto neigh_block_level = acc_amr_levels[nid];
853
854 auto cur_future = cur_block_level + (cur_ref_flag ? 1 : 0);
855
856 auto neigh_future = neigh_block_level + (neigh_ref_flag ? 1 : 0);
857
858 if (cur_ref_flag && (cur_future > neigh_future + 1)) {
859
860 if (!neigh_ref_flag) {
861 sycl::atomic_ref<
862 u32,
863 sycl::memory_order::relaxed,
864 sycl::memory_scope::system>
865 atomic_neigh_flag(acc_ref_flags[nid]);
866 atomic_neigh_flag.exchange(1);
867
868 sycl::atomic_ref<
869 u32,
870 sycl::memory_order::relaxed,
871 sycl::memory_scope::system>
872 atomic_changed(acc_changed[0]);
873 atomic_changed.exchange(1);
874 }
875 }
876 };
877
878 block_graph_xp.for_each_object_link(block_id, check_2To1_ref);
879 block_graph_xm.for_each_object_link(block_id, check_2To1_ref);
880 block_graph_yp.for_each_object_link(block_id, check_2To1_ref);
881 block_graph_ym.for_each_object_link(block_id, check_2To1_ref);
882 block_graph_zp.for_each_object_link(block_id, check_2To1_ref);
883 block_graph_zm.for_each_object_link(block_id, check_2To1_ref);
884 });
885 });
886 block_graph_neighs_xp.complete_event_state(e);
887 block_graph_neighs_xm.complete_event_state(e);
888 block_graph_neighs_yp.complete_event_state(e);
889 block_graph_neighs_ym.complete_event_state(e);
890 block_graph_neighs_zp.complete_event_state(e);
891 block_graph_neighs_zm.complete_event_state(e);
892 buf_amr_block_levels.complete_event_state(e);
893
894 changed_buf.complete_event_state(e);
895
896 patch_refine_flags.complete_event_state(e);
897
898 e.wait();
899
900 if (changed_buf.get_val_at_idx(0) == 0) {
901 logger::raw_ln("Refinement 2:1 balance converged in ", pass + 1, " sweeps");
902 break;
903 }
904 }
905
907 // refinement
909
910 // perform stream compactions on the refinement flags
911 auto dev_buf_ref
912 = shamalgs::numeric::stream_compact(dev_sched, patch_refine_flags, obj_cnt);
913
914 shamlog_debug_ln(
915 "AMRGrid", "patch ", id_patch, dev_buf_ref.get_size(), "marked for refinement + 2:1");
916 });
917}
918
926
927template<class Tvec, class TgridVec>
932 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
933
934 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
935 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
936 u64 id_patch = cur_p.id_patch;
937
938 sham::DeviceBuffer<u32> &patch_derefine_flag = dd_derefine_flags.get(id_patch);
939 sham::DeviceBuffer<u32> &patch_refine_flag = dd_refine_flags.get(id_patch);
940
941 u32 obj_cnt = pdat.get_obj_cnt();
942
943 auto dev_buf_deref_0
944 = shamalgs::numeric::stream_compact(dev_sched, patch_derefine_flag, obj_cnt);
945
947 " Count block's flag for derefinement [No geometry validity check and no 2:1 check] \t "
948 ": ",
949 dev_buf_deref_0.get_size(),
950 "\n");
951
954 // keep derefine flags on only if the eight cells want to merge and if they can
955 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
956 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
957
958 // The kernel below reads the flags of the 8 blocks following each block while writing the
959 // flag of the current one, so the input flags are read from a copy (otherwise the result
960 // depends on the execution order of the work-items)
961 sham::DeviceBuffer<u32> patch_derefine_flag_in(obj_cnt, dev_sched);
962 patch_derefine_flag.copy_range(0, obj_cnt, patch_derefine_flag_in);
963
964 sham::EventList depends_list;
965 auto acc_min = buf_cell_min.get_read_access(depends_list);
966 auto acc_max = buf_cell_max.get_read_access(depends_list);
967
968 auto acc_want_merge = patch_derefine_flag_in.get_read_access(depends_list);
969 auto acc_merge_flag = patch_derefine_flag.get_write_access(depends_list);
970 auto acc_refine_flag = patch_refine_flag.get_read_access(depends_list);
971
972 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
973 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
974 u32 id = gid.get_linear_id();
975
976 std::array<BlockCoord, split_count> blocks;
977 bool do_merge = true;
978
979 // This avoid the case where we are in the last block of the buffer to
980 // avoid the out-of-bound read
981 if (id + split_count <= obj_cnt) {
982 bool all_want_to_merge = true;
983
984 auto get_coord = [](u32 i) -> std::array<u32, dim> {
985 constexpr u32 NsideBlockPow = 1;
986 constexpr u32 Nside = 1U << NsideBlockPow;
987
988 if constexpr (dim == 3) {
989 const u32 tmp = i >> NsideBlockPow;
990 // This line is why derefinement never happens
991 // return {i % Nside, (tmp) % Nside, (tmp ) >> NsideBlockPow};
992 return {(tmp) >> NsideBlockPow, (tmp) % Nside, i % Nside};
993 }
994 };
995
996 auto get_split
997 = [=](BlockCoord target_block) -> std::array<BlockCoord, split_count> {
998 std::array<BlockCoord, split_count> ret;
999 auto bmin = target_block.bmin;
1000 auto bmax = target_block.bmax;
1001 auto split = bmin + (bmax - bmin) / 2;
1002 std::array<TgridVec, 3> szs = {bmin, split, bmax};
1003 for (u32 i = 0; i < split_count; i++) {
1004 auto [lx, ly, lz] = get_coord(i);
1005
1006 ret[i].bmin = TgridVec{szs[lx].x(), szs[ly].y(), szs[lz].z()};
1007 ret[i].bmax
1008 = TgridVec{szs[lx + 1].x(), szs[ly + 1].y(), szs[lz + 1].z()};
1009 }
1010
1011 return ret;
1012 };
1013
1014 for (u32 b_lid = 0; b_lid < split_count; b_lid++) {
1015 blocks[b_lid] = BlockCoord{acc_min[id + b_lid], acc_max[id + b_lid]};
1016 all_want_to_merge = all_want_to_merge && acc_want_merge[id + b_lid];
1017 }
1018
1019 BlockCoord merged = BlockCoord::get_merge(blocks);
1020 std::array<BlockCoord, split_count> splitted = get_split(merged);
1021 for (u32 lid = 0; lid < split_count; lid++) {
1022 do_merge = do_merge && sham::equals(blocks[lid].bmin, splitted[lid].bmin)
1023 && sham::equals(blocks[lid].bmax, splitted[lid].bmax);
1024 }
1025
1026 do_merge = do_merge && all_want_to_merge;
1027 if (acc_refine_flag[id] && do_merge) {
1028 do_merge = false;
1029 }
1030
1031 } else {
1032 do_merge = false;
1033 }
1034 acc_merge_flag[id] = do_merge;
1035 });
1036 });
1037 buf_cell_min.complete_event_state(e);
1038 buf_cell_max.complete_event_state(e);
1039 patch_derefine_flag_in.complete_event_state(e);
1040 patch_derefine_flag.complete_event_state(e);
1041 patch_refine_flag.complete_event_state(e);
1042
1043 auto buf_derefine_1
1044 = shamalgs::numeric::stream_compact(dev_sched, patch_derefine_flag, obj_cnt);
1046 " Count block's flag for derefinement [After geometry validity check and before 2:1 "
1047 "check] "
1048 "\t : ",
1049 buf_derefine_1.get_size(),
1050 "\n");
1051 });
1052}
1053
1061
1062template<class Tvec, class TgridVec>
1067
1068 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1069
1070 scheduler().for_each_patchdata_nonempty([&](Patch cur_p, PatchDataLayer &pdat) {
1071 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
1072 u64 id_patch = cur_p.id_patch;
1073
1074 sham::DeviceBuffer<u32> &patch_derefine_flag = dd_derefine_flags.get(id_patch);
1075 sham::DeviceBuffer<u32> &patch_refine_flag = dd_refine_flags.get(id_patch);
1076
1077 u32 obj_cnt = pdat.get_obj_cnt();
1078
1079 // blocks graph in each direction for the current patch
1080 AMRGraph &block_graph_neighs_xp = shambase::get_check_ref(storage.block_graph_edge)
1081 .get_refs_dir(Direction_::xp)
1082 .get(id_patch);
1083 AMRGraph &block_graph_neighs_xm = shambase::get_check_ref(storage.block_graph_edge)
1084 .get_refs_dir(Direction_::xm)
1085 .get(id_patch);
1086 AMRGraph &block_graph_neighs_yp = shambase::get_check_ref(storage.block_graph_edge)
1087 .get_refs_dir(Direction_::yp)
1088 .get(id_patch);
1089 AMRGraph &block_graph_neighs_ym = shambase::get_check_ref(storage.block_graph_edge)
1090 .get_refs_dir(Direction_::ym)
1091 .get(id_patch);
1092 AMRGraph &block_graph_neighs_zp = shambase::get_check_ref(storage.block_graph_edge)
1093 .get_refs_dir(Direction_::zp)
1094 .get(id_patch);
1095 AMRGraph &block_graph_neighs_zm = shambase::get_check_ref(storage.block_graph_edge)
1096 .get_refs_dir(Direction_::zm)
1097 .get(id_patch);
1098 // get the current buffer of block levels in the current patch
1099 sham::DeviceBuffer<TgridUint> &buf_amr_block_levels
1100 = shambase::get_check_ref(storage.amr_block_levels).get_buf(id_patch);
1101
1102 // ////////////////////////////////////////////////////////////////////////////////////
1103 // // // enforce 2:1 at parent level
1104 // ///////////////////////////////////////////////////////////////////////////////////
1105
1106 std::shared_ptr<sham::DeviceScheduler> dev_sched
1107 = shamsys::instance::get_compute_scheduler_ptr();
1108
1109 sham::DeviceBuffer<u32> changed_buf(1, dev_sched);
1110
1111 // copy old deref buffer to avoid race condition
1112 sham::DeviceBuffer<u32> patch_derefine_flag_old(obj_cnt, dev_sched);
1113 patch_derefine_flag.copy_range(0, obj_cnt, patch_derefine_flag_old);
1114
1115 //
1116 sham::DeviceBuffer<u32> patch_derefine_flag_new(obj_cnt, dev_sched);
1117
1118 for (int it = 0; it < 100; it++) {
1119 changed_buf.set_val_at_idx(0, 0);
1120
1121 sham::EventList depend_list;
1122
1123 AMRGraphLinkiterator block_graph_xp
1124 = block_graph_neighs_xp.get_read_access(depend_list);
1125
1126 AMRGraphLinkiterator block_graph_xm
1127 = block_graph_neighs_xm.get_read_access(depend_list);
1128 AMRGraphLinkiterator block_graph_yp
1129 = block_graph_neighs_yp.get_read_access(depend_list);
1130 AMRGraphLinkiterator block_graph_ym
1131 = block_graph_neighs_ym.get_read_access(depend_list);
1132 AMRGraphLinkiterator block_graph_zp
1133 = block_graph_neighs_zp.get_read_access(depend_list);
1134 AMRGraphLinkiterator block_graph_zm
1135 = block_graph_neighs_zm.get_read_access(depend_list);
1136
1137 auto acc_amr_levels = buf_amr_block_levels.get_read_access(depend_list);
1138 auto acc_ref_flag = patch_refine_flag.get_read_access(depend_list);
1139 auto acc_changed = changed_buf.get_write_access(depend_list);
1140
1141 auto acc_deref_old = patch_derefine_flag_old.get_read_access(depend_list);
1142 auto acc_deref_new = patch_derefine_flag_new.get_write_access(depend_list);
1143
1144 auto e_2to1 = q.submit(depend_list, [&](sycl::handler &cgh) {
1145 cgh.parallel_for(sycl::range<1>(obj_cnt), [=](sycl::item<1> gid) {
1146 auto lid = gid.get_linear_id();
1147
1148 auto old_flag = acc_deref_old[lid];
1149 auto new_flag = old_flag;
1150
1151 auto check_2To1_der = [&](u32 nid) {
1152 if (nid < obj_cnt)
1153
1154 {
1155
1156 auto neigh_future = acc_amr_levels[nid] + (acc_ref_flag[nid] ? 1 : 0)
1157 - (acc_deref_old[nid] ? 1 : 0);
1158
1159 auto my_future = acc_amr_levels[lid] - 1;
1160
1161 if (neigh_future > my_future + 1) {
1162 new_flag = 0;
1163 }
1164 }
1165 };
1166
1167 if (old_flag) {
1168
1169 for (u32 i = 0; i < AMRBlock::block_size; i++) {
1170 block_graph_xp.for_each_object_link((lid + i), check_2To1_der);
1171 block_graph_xm.for_each_object_link((lid + i), check_2To1_der);
1172 block_graph_yp.for_each_object_link((lid + i), check_2To1_der);
1173 block_graph_ym.for_each_object_link((lid + i), check_2To1_der);
1174 block_graph_zp.for_each_object_link((lid + i), check_2To1_der);
1175 block_graph_zm.for_each_object_link((lid + i), check_2To1_der);
1176 }
1177
1178 if (old_flag != new_flag) {
1179 sycl::atomic_ref<
1180 u32,
1181 sycl::memory_order::relaxed,
1182 sycl::memory_scope::system>
1183 atomic_changed(acc_changed[0]);
1184 atomic_changed.exchange(1);
1185 }
1186 }
1187
1188 acc_deref_new[lid] = new_flag;
1189 });
1190 });
1191 block_graph_neighs_xp.complete_event_state(e_2to1);
1192 block_graph_neighs_xm.complete_event_state(e_2to1);
1193 block_graph_neighs_yp.complete_event_state(e_2to1);
1194 block_graph_neighs_ym.complete_event_state(e_2to1);
1195 block_graph_neighs_zp.complete_event_state(e_2to1);
1196 block_graph_neighs_zm.complete_event_state(e_2to1);
1197 buf_amr_block_levels.complete_event_state(e_2to1);
1198 patch_refine_flag.complete_event_state(e_2to1);
1199 changed_buf.complete_event_state(e_2to1);
1200
1201 patch_derefine_flag_old.complete_event_state(e_2to1);
1202 patch_derefine_flag_new.complete_event_state(e_2to1);
1203 e_2to1.wait();
1204
1205 std::swap(patch_derefine_flag_old, patch_derefine_flag_new);
1206
1207 if (changed_buf.get_val_at_idx(0) == 0) {
1208 logger:
1210 "Derefinement 2:1 balance converge in \t ", it + 1, "\t sweeps \n\n");
1211 break;
1212 }
1213 }
1214 // copy back to ..
1215 patch_derefine_flag_old.copy_range(0, obj_cnt, patch_derefine_flag);
1216
1217 // ////////////////////////////////////////////////////////////////////////////////
1218 // // derefinement
1219 // ////////////////////////////////////////////////////////////////////////////////
1220 // perform stream compactions on the derefinement flags
1221 auto buf_derefine
1222 = shamalgs::numeric::stream_compact(dev_sched, patch_derefine_flag, obj_cnt);
1224 " Count block's flag for derefinement [After geometry validity check and after 2:1 "
1225 "check] \t : ",
1226 buf_derefine.get_size(),
1227 "\n");
1228
1229 shamlog_debug_ln(
1230 "AMRGrid", "patch ", id_patch, buf_derefine.get_size(), "marked for derefinement ");
1231 });
1232}
1233
1234template<class Tvec, class TgridVec>
1235template<class UserAcc>
1239 const AMRInterpMode amr_refine_interp_mode) {
1240
1241 u64 sum_block_count = 0;
1242
1243 bool new_cell_were_added = false;
1244 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
1245 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1246
1247 scheduler().for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
1248 u32 old_obj_cnt = pdat.get_obj_cnt();
1249 auto stream_compaction_result = shamalgs::numeric::stream_compact(
1250 dev_sched, dd_refine_flags.get(id_patch), old_obj_cnt);
1251
1252 if (stream_compaction_result.get_size() > 0) {
1253 // alloc memory for the new blocks to be created
1254
1256 "Will refine \t", stream_compaction_result.get_size(), " \t blocks \n\n");
1257
1258 pdat.expand(static_cast<u32>(stream_compaction_result.get_size()) * (split_count - 1));
1259 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
1260 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
1261 sham::EventList depends_list;
1262
1263 auto block_bound_low = buf_cell_min.get_write_access(depends_list);
1264 auto block_bound_high = buf_cell_max.get_write_access(depends_list);
1265 UserAcc uacc(depends_list, storage, id_patch, pdat, amr_refine_interp_mode);
1266 auto index_to_ref = stream_compaction_result.get_read_access(depends_list);
1267
1268 // Refine the block (set the positions) and fill the corresponding fields
1269 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1270 u32 start_index_push = old_obj_cnt;
1271
1272 constexpr u32 new_splits = split_count - 1;
1273
1274 cgh.parallel_for(
1275 sycl::range<1>(stream_compaction_result.get_size()), [=](sycl::item<1> gid) {
1276 u32 tid = gid.get_linear_id();
1277
1278 u32 idx_to_refine = index_to_ref[gid];
1279
1280 // gen splits coordinates
1281 BlockCoord cur_block{
1282 block_bound_low[idx_to_refine], block_bound_high[idx_to_refine]};
1283
1284 std::array<BlockCoord, split_count> block_coords
1285 = BlockCoord::get_split(cur_block.bmin, cur_block.bmax);
1286
1287 // generate index for the refined blocks
1288 std::array<u32, split_count> blocks_ids;
1289 blocks_ids[0] = idx_to_refine;
1290
1291 // generate index for the new blocks (the current index is reused for the first
1292 // new block, the others are pushed at the end of the patchdata)
1293#pragma unroll
1294 for (u32 pid = 0; pid < new_splits; pid++) {
1295 blocks_ids[pid + 1] = start_index_push + tid * new_splits + pid;
1296 }
1297
1298 // write coordinates
1299
1300#pragma unroll
1301 for (u32 pid = 0; pid < split_count; pid++) {
1302 block_bound_low[blocks_ids[pid]] = block_coords[pid].bmin;
1303 block_bound_high[blocks_ids[pid]] = block_coords[pid].bmax;
1304 }
1305
1306 // user lambda to fill the fields
1307 uacc.apply_refine_new(
1308 idx_to_refine, cur_block, blocks_ids, block_coords, uacc);
1309 });
1310 });
1311
1312 sham::EventList resulting_events{e};
1313
1314 buf_cell_min.complete_event_state(resulting_events);
1315 buf_cell_max.complete_event_state(resulting_events);
1316 stream_compaction_result.complete_event_state(e);
1317 uacc.finalize_new(resulting_events, storage, id_patch, pdat, amr_refine_interp_mode);
1318 }
1319
1320 shamlog_debug_ln("AMRGrid", "patch ", id_patch, "new block count = ", pdat.get_obj_cnt());
1321 sum_block_count += pdat.get_obj_cnt();
1322 new_cell_were_added = new_cell_were_added || (stream_compaction_result.get_size() > 0);
1323 });
1324
1325 logger::info_ln("AMRGrid", "process block count =", sum_block_count);
1326
1327 return new_cell_were_added;
1328}
1329
1330template<class Tvec, class TgridVec>
1331template<class UserAcc>
1335 const AMRInterpMode amr_refine_interp_mode) {
1336
1337 using namespace shamrock::patch;
1338
1339 bool cell_were_removed = false;
1340
1341 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
1342 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
1343
1344 scheduler().for_each_patch_data([&](u64 id_patch, Patch cur_p, PatchDataLayer &pdat) {
1345 u32 old_obj_cnt = pdat.get_obj_cnt();
1346 u32 old_obj_cnt_before_refinement = dd_derefine_flags.get(id_patch).get_size();
1347 auto stream_compact_results = shamalgs::numeric::stream_compact(
1348 dev_sched, dd_derefine_flags.get(id_patch), old_obj_cnt_before_refinement);
1349 if (stream_compact_results.get_size() > 0) {
1350 // init flag table
1351 sycl::buffer<u32> keep_block_flag
1352 = shamalgs::algorithm::gen_buffer_device(q.q, old_obj_cnt, [](u32 i) -> u32 {
1353 return 1;
1354 });
1355
1356 sham::DeviceBuffer<TgridVec> &buf_cell_min = pdat.get_field_buf_ref<TgridVec>(0);
1357 sham::DeviceBuffer<TgridVec> &buf_cell_max = pdat.get_field_buf_ref<TgridVec>(1);
1358 sham::EventList depends_list;
1359 auto block_bound_low = buf_cell_min.get_write_access(depends_list);
1360 auto block_bound_high = buf_cell_max.get_write_access(depends_list);
1361 UserAcc uacc(depends_list, storage, id_patch, pdat, amr_refine_interp_mode);
1362 auto index_to_deref = stream_compact_results.get_read_access(depends_list);
1363
1364 // edit block content + make flag of blocks to keep
1365 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
1366 sycl::accessor flag_keep{keep_block_flag, cgh, sycl::read_write};
1367 cgh.parallel_for(
1368 sycl::range<1>(stream_compact_results.get_size()), [=](sycl::item<1> gid) {
1369 u32 tid = gid.get_linear_id();
1370
1371 u32 idx_to_derefine = index_to_deref[gid];
1372
1373 // compute old block indexes
1374 std::array<u32, split_count> old_indexes;
1375#pragma unroll
1376 for (u32 pid = 0; pid < split_count; pid++) {
1377 old_indexes[pid] = idx_to_derefine + pid;
1378 }
1379
1380 // load block coords
1381 std::array<BlockCoord, split_count> block_coords;
1382#pragma unroll
1383 for (u32 pid = 0; pid < split_count; pid++) {
1384 block_coords[pid] = BlockCoord{
1385 block_bound_low[old_indexes[pid]],
1386 block_bound_high[old_indexes[pid]]};
1387 }
1388
1389 // make new block coord
1390 BlockCoord merged_block_coord = BlockCoord::get_merge(block_coords);
1391
1392 // write new coord
1393 block_bound_low[idx_to_derefine] = merged_block_coord.bmin;
1394 block_bound_high[idx_to_derefine] = merged_block_coord.bmax;
1395
1396// flag the old blocks for removal
1397#pragma unroll
1398 for (u32 pid = 1; pid < split_count; pid++) {
1399 flag_keep[idx_to_derefine + pid] = 0;
1400 }
1401
1402 // user lambda to fill the fields
1403
1404 uacc.apply_derefine_new(
1405 old_indexes, block_coords, idx_to_derefine, merged_block_coord, uacc);
1406 });
1407 });
1408
1409 sham::EventList resulting_events;
1410
1411 buf_cell_min.complete_event_state(resulting_events);
1412 buf_cell_max.complete_event_state(resulting_events);
1413 uacc.finalize_new(resulting_events, storage, id_patch, pdat, amr_refine_interp_mode);
1414
1415 stream_compact_results.complete_event_state(resulting_events);
1416
1417 // stream compact the flags (get new block ids map after merged)
1418 auto [opt_buf, len]
1419 = shamalgs::numeric::stream_compact(q.q, keep_block_flag, old_obj_cnt);
1420
1422 "AMR Grid",
1423 "patch",
1424 id_patch,
1425 "derefine block count = ",
1426 old_obj_cnt - len,
1427 "new block count = ",
1428 len);
1429
1430 if (!opt_buf) {
1431 throw std::runtime_error("opt buf must contain something at this point");
1432 }
1433
1434 // remap pdat according to stream compact (for each field in patchdataleyer resize
1435 // according to new block ids map)
1436 pdat.index_remap_resize(*opt_buf, len);
1437
1438 cell_were_removed = cell_were_removed || stream_compact_results.get_size() > 0;
1439 }
1440 });
1441
1442 return cell_were_removed;
1443}
1444
1445template<class Tvec, class TgridVec>
1446template<class UserAccCrit, class UserAccSplit, class UserAccMerge>
1448 internal_update_refinement_new(const AMRInterpMode amr_refine_interp_mode) {
1449
1450 // Ensure that the blocks are sorted before refinement
1451 AMRSortBlocks block_sorter(context, solver_config, storage);
1452 block_sorter.reorder_amr_blocks();
1453
1454 // get refine and derefine list
1457
1458 gen_refine_block_changes_new<UserAccCrit>(dd_refine_list, dd_derefine_list);
1459
1461 // Note that this only add new blocks at the end of the patchdata
1462 internal_refine_grid_new<UserAccSplit>(std::move(dd_refine_list), amr_refine_interp_mode);
1463
1465 // Note that this will perform the merge then remove the old blocks
1466 // This is ok to call straight after the refine without edditing the index list in derefine_list
1467 // since no permutations were applied in internal_refine_grid_new and no cells can be both
1468 // refined and derefined in the same pass
1469 internal_derefine_grid_new<UserAccMerge>(std::move(dd_derefine_list), amr_refine_interp_mode);
1470}
1471
1472template<class Tvec, class TgridVec>
1475
1476 class RefineCritBlock {
1477 public:
1478 const TgridVec *block_low_bound;
1479 const TgridVec *block_high_bound;
1480 const Tscal *block_density_field;
1481
1482 Tscal one_over_Nside = 1. / AMRBlock::Nside;
1483
1484 Tscal dxfact;
1485 Tscal wanted_mass;
1486
1487 RefineCritBlock(
1488 sham::EventList &depends_list,
1489 Storage &storage,
1490 u64 id_patch,
1493 Tscal dxfact,
1494 Tscal wanted_mass)
1495 : dxfact(dxfact), wanted_mass(wanted_mass) {
1496
1497 block_low_bound = pdat.get_field<TgridVec>(0).get_buf().get_read_access(depends_list);
1498 block_high_bound = pdat.get_field<TgridVec>(1).get_buf().get_read_access(depends_list);
1499 block_density_field = pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
1500 .get_buf()
1501 .get_read_access(depends_list);
1502 }
1503
1504 void finalize_new(
1505 sham::EventList &resulting_events,
1506 Storage &storage,
1507 u64 id_patch,
1510 Tscal dxfact,
1511 Tscal wanted_mass) {
1512
1513 pdat.get_field<TgridVec>(0).get_buf().complete_event_state(resulting_events);
1514 pdat.get_field<TgridVec>(1).get_buf().complete_event_state(resulting_events);
1515 pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
1516 .get_buf()
1517 .complete_event_state(resulting_events);
1518 }
1519
1520 void refine_criterion_new(
1521 u32 block_id, RefineCritBlock acc, bool &should_refine, bool &should_derefine) const {
1522
1523 TgridVec low_bound = acc.block_low_bound[block_id];
1524 TgridVec high_bound = acc.block_high_bound[block_id];
1525
1526 Tvec block_cell_size
1527 = (high_bound - low_bound).template convert<Tscal>() * (dxfact * one_over_Nside);
1528
1529 Tscal sum_mass = 0;
1530 for (u32 i = 0; i < AMRBlock::block_size; i++) {
1531 sum_mass += acc.block_density_field[i + block_id * AMRBlock::block_size];
1532 }
1533 sum_mass *= block_cell_size.x() * block_cell_size.y() * block_cell_size.z();
1534
1535 if (sum_mass > wanted_mass * 8) {
1536 should_refine = true;
1537 should_derefine = false;
1538 } else if (sum_mass < wanted_mass) {
1539 should_refine = false;
1540 should_derefine = true;
1541 } else {
1542 should_refine = false;
1543 should_derefine = false;
1544 }
1545
1546 should_refine = should_refine && (high_bound.x() - low_bound.x() > AMRBlock::Nside);
1547 should_refine = should_refine && (high_bound.y() - low_bound.y() > AMRBlock::Nside);
1548 should_refine = should_refine && (high_bound.z() - low_bound.z() > AMRBlock::Nside);
1549 }
1550 };
1551
1552 class RefineCellAccessor {
1553 public:
1554 f64 *rho;
1555 f64_3 *rho_vel;
1556 f64 *rhoE;
1557 u64 p_id;
1558 f64 *cell_sizes;
1559
1560 const f64 *rho_old_snap;
1561 const f64_3 *rho_vel_old_snap;
1562 const f64 *rhoE_old_snap;
1563
1564 // self-gravity potentials (warm starts of the Poisson solvers), nullptr without gravity
1565 f64 *phi = nullptr;
1566 f64 *phi_old = nullptr;
1567
1568 AMRInterpMode amr_ref_interp_mode;
1569
1570 // this will be needed for interpolation during refinement
1571 AMRGraphLinkiterator cell_graph_xp;
1572 AMRGraphLinkiterator cell_graph_xm;
1573 AMRGraphLinkiterator cell_graph_yp;
1574 AMRGraphLinkiterator cell_graph_ym;
1575 AMRGraphLinkiterator cell_graph_zp;
1576 AMRGraphLinkiterator cell_graph_zm;
1577
1578 RefineCellAccessor(
1579 sham::EventList &depends_list,
1580 Storage &storage,
1581 u64 &id_patch,
1583 AMRInterpMode _amr_ref_interp_mode)
1584 : cell_graph_xp(
1585 shambase::get_check_ref(storage.cell_graph_edge)
1586 .get_refs_dir(Direction::xp)
1587 .get(id_patch)
1588 .get()
1589 .get_read_access(depends_list)),
1590 cell_graph_xm(
1591 shambase::get_check_ref(storage.cell_graph_edge)
1592 .get_refs_dir(Direction::xm)
1593 .get(id_patch)
1594 .get()
1595 .get_read_access(depends_list)),
1596 cell_graph_yp(
1597 shambase::get_check_ref(storage.cell_graph_edge)
1598 .get_refs_dir(Direction::yp)
1599 .get(id_patch)
1600 .get()
1601 .get_read_access(depends_list)),
1602 cell_graph_ym(
1603 shambase::get_check_ref(storage.cell_graph_edge)
1604 .get_refs_dir(Direction::ym)
1605 .get(id_patch)
1606 .get()
1607 .get_read_access(depends_list)),
1608 cell_graph_zp(
1609 shambase::get_check_ref(storage.cell_graph_edge)
1610 .get_refs_dir(Direction::zp)
1611 .get(id_patch)
1612 .get()
1613 .get_read_access(depends_list)),
1614 cell_graph_zm(
1615 shambase::get_check_ref(storage.cell_graph_edge)
1616 .get_refs_dir(Direction::zm)
1617 .get(id_patch)
1618 .get()
1619 .get_read_access(depends_list)),
1620 amr_ref_interp_mode(_amr_ref_interp_mode)
1621
1622 {
1623 p_id = id_patch;
1624
1625 // old conservatives variables
1626 rho_old_snap = shambase::get_check_ref(storage.rho_snap)
1627 .get(id_patch)
1628 .get_buf()
1629 .get_read_access(depends_list);
1630 rhoE_old_snap = shambase::get_check_ref(storage.rhoe_snap)
1631 .get(id_patch)
1632 .get_buf()
1633 .get_read_access(depends_list);
1634 rho_vel_old_snap = shambase::get_check_ref(storage.rho_vel_snap)
1635 .get(id_patch)
1636 .get_buf()
1637 .get_read_access(depends_list);
1638
1639 // new conservative variables
1640 rho = pdat.get_field<f64>(2).get_buf().get_write_access(depends_list);
1641 rho_vel = pdat.get_field<f64_3>(3).get_buf().get_write_access(depends_list);
1642 rhoE = pdat.get_field<f64>(4).get_buf().get_write_access(depends_list);
1643 cell_sizes = shambase::get_check_ref(storage.block_cell_sizes)
1644 .get_buf(id_patch)
1645 .get_write_access(depends_list);
1646
1647 if (pdat.pdl().has_field_name("phi")) {
1648 phi = pdat.get_field<f64>(pdat.pdl().get_field_idx<f64>("phi"))
1649 .get_buf()
1650 .get_write_access(depends_list);
1651 phi_old = pdat.get_field<f64>(pdat.pdl().get_field_idx<f64>("phi_old"))
1652 .get_buf()
1653 .get_write_access(depends_list);
1654 }
1655 }
1656
1657 void finalize_new(
1658 sham::EventList &resulting_events,
1659 Storage &storage,
1660 u64 &id_patch,
1662 AMRInterpMode amr_ref_interp_mode) {
1663
1664 pdat.get_field<f64>(2).get_buf().complete_event_state(resulting_events);
1665 pdat.get_field<f64_3>(3).get_buf().complete_event_state(resulting_events);
1666 pdat.get_field<f64>(4).get_buf().complete_event_state(resulting_events);
1667
1668 if (pdat.pdl().has_field_name("phi")) {
1669 pdat.get_field<f64>(pdat.pdl().get_field_idx<f64>("phi"))
1670 .get_buf()
1671 .complete_event_state(resulting_events);
1672 pdat.get_field<f64>(pdat.pdl().get_field_idx<f64>("phi_old"))
1673 .get_buf()
1674 .complete_event_state(resulting_events);
1675 }
1676
1677 shambase::get_check_ref(storage.cell_graph_edge)
1678 .get_refs_dir(Direction_::xp)
1679 .get(id_patch)
1680 .get()
1681 .complete_event_state(resulting_events);
1682 shambase::get_check_ref(storage.cell_graph_edge)
1683 .get_refs_dir(Direction_::xm)
1684 .get(id_patch)
1685 .get()
1686 .complete_event_state(resulting_events);
1687 shambase::get_check_ref(storage.cell_graph_edge)
1688 .get_refs_dir(Direction_::yp)
1689 .get(id_patch)
1690 .get()
1691 .complete_event_state(resulting_events);
1692 shambase::get_check_ref(storage.cell_graph_edge)
1693 .get_refs_dir(Direction_::ym)
1694 .get(id_patch)
1695 .get()
1696 .complete_event_state(resulting_events);
1697 shambase::get_check_ref(storage.cell_graph_edge)
1698 .get_refs_dir(Direction_::zp)
1699 .get(id_patch)
1700 .get()
1701 .complete_event_state(resulting_events);
1702 shambase::get_check_ref(storage.cell_graph_edge)
1703 .get_refs_dir(Direction_::zm)
1704 .get(id_patch)
1705 .get()
1706 .complete_event_state(resulting_events);
1707
1708 shambase::get_check_ref(storage.block_cell_sizes)
1709 .get_buf(id_patch)
1710 .complete_event_state(resulting_events);
1711
1712 // old conservative variables
1713 shambase::get_check_ref(storage.rho_snap)
1714 .get(id_patch)
1715 .get_buf()
1716 .complete_event_state(resulting_events);
1717 shambase::get_check_ref(storage.rhoe_snap)
1718 .get(id_patch)
1719 .get_buf()
1720 .complete_event_state(resulting_events);
1721 shambase::get_check_ref(storage.rho_vel_snap)
1722 .get(id_patch)
1723 .get_buf()
1724 .complete_event_state(resulting_events);
1725 }
1726
1727 void apply_refine_new(
1728 u32 cur_idx,
1729 BlockCoord cur_coords,
1730 std::array<u32, 8> new_blocks,
1731 std::array<BlockCoord, 8> new_block_coords,
1732 RefineCellAccessor acc) const {
1733
1734 auto get_coord_ref = [](u32 i) -> std::array<u32, dim> {
1735 constexpr u32 NsideBlockPow = 1;
1736 constexpr u32 Nside = 1U << NsideBlockPow;
1737
1738 if constexpr (dim == 3) {
1739 const u32 tmp = i >> NsideBlockPow;
1740 return {i % Nside, (tmp) % Nside, (tmp) >> NsideBlockPow};
1741 }
1742 };
1743
1744 auto get_index_block = [](std::array<u32, dim> coord) -> u32 {
1745 constexpr u32 NsideBlockPow = 1;
1746 constexpr u32 Nside = 1U << NsideBlockPow;
1747
1748 if constexpr (dim == 3) {
1749 return coord[0] + Nside * coord[1] + Nside * Nside * coord[2];
1750 }
1751 };
1752
1753 auto get_gid_write = [&](std::array<u32, dim> &glid) -> u32 {
1754 // First, get the block id (it's the block to be refine) in wich the new cell glid
1755 // is located.
1756 std::array<u32, dim> bid
1757 = {glid[0] >> AMRBlock::NsideBlockPow,
1758 glid[1] >> AMRBlock::NsideBlockPow,
1759 glid[2] >> AMRBlock::NsideBlockPow};
1760
1761 // get the new global block id
1762 auto new_glob_id = new_blocks[get_index_block(bid)] * AMRBlock::block_size;
1763
1764 // then added to new_glob_id the local index (between 0 and 7) of the generated
1765 // cells to get. This give the global ids of the new generated cells.
1766 return new_glob_id
1767 + AMRBlock::get_index(
1768 {glid[0] % AMRBlock::Nside,
1769 glid[1] % AMRBlock::Nside,
1770 glid[2] % AMRBlock::Nside});
1771 };
1772
1773 // The potentials are only warm starts of the Poisson solvers (recomputed at each
1774 // step), the children get the value of their parent cell. They are read before any
1775 // write since the refined block is reused as one of the new blocks.
1776 std::array<f64, AMRBlock::block_size> parent_phi{};
1777 std::array<f64, AMRBlock::block_size> parent_phi_old{};
1778 if (acc.phi != nullptr) {
1779 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
1780 parent_phi[loc_id] = acc.phi[cur_idx * AMRBlock::block_size + loc_id];
1781 parent_phi_old[loc_id] = acc.phi_old[cur_idx * AMRBlock::block_size + loc_id];
1782 }
1783 }
1784
1785 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
1786
1787 auto [lx, ly, lz] = get_coord_ref(loc_id);
1788 u32 old_cell_idx = cur_idx * AMRBlock::block_size + loc_id;
1789
1790 // // cell size in the refined block
1791 Tscal delta_cell = cell_sizes[cur_idx];
1792 Tscal c_offset = delta_cell * 0.25;
1793 std::array<f64_3, AMRBlock::block_size> child_center_offsets;
1794 child_center_offsets[0] = {-c_offset, -c_offset, -c_offset}; /*(0,0,0) */
1795 child_center_offsets[1] = {c_offset, -c_offset, -c_offset}; /*(1,0,0)*/
1796 child_center_offsets[2] = {-c_offset, c_offset, -c_offset}; /* (0,1,0)*/
1797 child_center_offsets[3] = {c_offset, c_offset, -c_offset}; /*(1,1,0)*/
1798 child_center_offsets[4] = {-c_offset, -c_offset, c_offset}; /*(0,0,1)*/
1799 child_center_offsets[5] = {c_offset, -c_offset, c_offset}; /*(1,0,1)*/
1800 child_center_offsets[6] = {-c_offset, c_offset, c_offset}; /*(0,1,1)*/
1801 child_center_offsets[7] = {c_offset, c_offset, c_offset}; /*(1,1,1)*/
1802
1803 auto cons_var_slopes = get_3d_grad_cons<Tvec, Minmod>(
1804 cell_sizes,
1805 AMRBlock::block_size,
1806 old_cell_idx,
1807 acc.cell_graph_xp,
1808 acc.cell_graph_xm,
1809 acc.cell_graph_yp,
1810 acc.cell_graph_ym,
1811 acc.cell_graph_zp,
1812 acc.cell_graph_zm,
1813 [=](u32 id) {
1814 return acc.rho_old_snap[id];
1815 },
1816 [=](u32 id) {
1817 return acc.rho_vel_old_snap[id];
1818 },
1819 [=](u32 id) {
1820 return acc.rhoE_old_snap[id];
1821 });
1822
1823 std::array<f64, AMRBlock::block_size> _rho_block;
1824 std::array<f64_3, AMRBlock::block_size> _rho_vel_block;
1825 std::array<f64, AMRBlock::block_size> _rhoE_block;
1826
1827 int mul_second_order
1828 = (acc.amr_ref_interp_mode == AMRInterpMode::SECOND_ORDER) ? 1 : 0;
1829
1830 bool do_second_order = true;
1831
1832 for (u32 subdiv_lid = 0; subdiv_lid < 8; subdiv_lid++) {
1833 shammath::ConsState<Tvec> cons_var_interp
1834 = child_center_offsets[subdiv_lid][0] * cons_var_slopes[0]
1835 + child_center_offsets[subdiv_lid][1] * cons_var_slopes[1]
1836 + child_center_offsets[subdiv_lid][2] * cons_var_slopes[2];
1837
1838 _rho_block[subdiv_lid]
1839 = acc.rho_old_snap[old_cell_idx] + cons_var_interp.rho * mul_second_order;
1840 _rho_vel_block[subdiv_lid] = acc.rho_vel_old_snap[old_cell_idx]
1841 + cons_var_interp.rhovel * mul_second_order;
1842 _rhoE_block[subdiv_lid]
1843 = acc.rhoE_old_snap[old_cell_idx] + cons_var_interp.rhoe * mul_second_order;
1844
1845 const auto e_int
1846 = _rhoE_block[subdiv_lid]
1847 - 0.5 * sham::dot(_rho_vel_block[subdiv_lid], _rho_vel_block[subdiv_lid])
1848 / (_rho_block[subdiv_lid]);
1849 do_second_order
1850 = do_second_order && (_rho_block[subdiv_lid] > 0.0) && (e_int > 0.0);
1851 }
1852
1853 for (u32 subdiv_lid = 0; subdiv_lid < 8; subdiv_lid++) {
1854
1855 auto [sx, sy, sz] = get_coord_ref(subdiv_lid);
1856
1857 std::array<u32, 3> glid = {lx * 2 + sx, ly * 2 + sy, lz * 2 + sz};
1858
1859 u32 new_cell_idx = get_gid_write(glid);
1860
1861 acc.rho[new_cell_idx] = (do_second_order) ? _rho_block[subdiv_lid]
1862 : acc.rho_old_snap[old_cell_idx];
1863 acc.rho_vel[new_cell_idx] = (do_second_order)
1864 ? _rho_vel_block[subdiv_lid]
1865 : acc.rho_vel_old_snap[old_cell_idx];
1866 acc.rhoE[new_cell_idx] = (do_second_order) ? _rhoE_block[subdiv_lid]
1867 : acc.rhoE_old_snap[old_cell_idx];
1868
1869 if (acc.phi != nullptr) {
1870 acc.phi[new_cell_idx] = parent_phi[loc_id];
1871 acc.phi_old[new_cell_idx] = parent_phi_old[loc_id];
1872 }
1873 }
1874 }
1875 }
1876
1877 void apply_derefine_new(
1878 std::array<u32, 8> old_blocks,
1879 std::array<BlockCoord, 8> old_coords,
1880 u32 new_cell,
1881 BlockCoord new_coord,
1882
1883 RefineCellAccessor acc) const {
1884
1885 std::array<f64, AMRBlock::block_size> rho_block;
1886 std::array<f64_3, AMRBlock::block_size> rho_vel_block;
1887 std::array<f64, AMRBlock::block_size> rhoE_block;
1888
1889 // average of the children potentials (warm starts of the Poisson solvers)
1890 std::array<f64, AMRBlock::block_size> phi_block{};
1891 std::array<f64, AMRBlock::block_size> phi_old_block{};
1892
1893 // for each siblings block, perform restriction from its 8 children cells
1894 for (u32 pid = 0; pid < 8; pid++) {
1895 Tscal rho_pid = {};
1896 Tvec rho_vel_pid = {};
1897 Tscal rhoe_pid = {};
1898
1899 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
1900 rho_pid += acc.rho[old_blocks[pid] * AMRBlock::block_size + cell_id];
1901 rho_vel_pid += acc.rho_vel[old_blocks[pid] * AMRBlock::block_size + cell_id];
1902 rhoe_pid += acc.rhoE[old_blocks[pid] * AMRBlock::block_size + cell_id];
1903 }
1904 // the siblings are in memory order, the cells of the merged block are x fastest
1905 u32 cell_id = get_merged_cell_index<AMRBlock>(old_coords[pid].bmin, new_coord.bmin);
1906
1907 rho_block[cell_id] = rho_pid * (1. / 8.);
1908 rho_vel_block[cell_id] = rho_vel_pid * (1. / 8.);
1909 rhoE_block[cell_id] = rhoe_pid * (1. / 8.);
1910
1911 if (acc.phi != nullptr) {
1912 Tscal phi_pid = {};
1913 Tscal phi_old_pid = {};
1914 for (u32 child_id = 0; child_id < AMRBlock::block_size; child_id++) {
1915 phi_pid += acc.phi[old_blocks[pid] * AMRBlock::block_size + child_id];
1916 phi_old_pid
1917 += acc.phi_old[old_blocks[pid] * AMRBlock::block_size + child_id];
1918 }
1919 phi_block[cell_id] = phi_pid * (1. / 8.);
1920 phi_old_block[cell_id] = phi_old_pid * (1. / 8.);
1921 }
1922 }
1923
1924 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
1925 u32 newcell_idx = new_cell * AMRBlock::block_size + cell_id;
1926 acc.rho[newcell_idx] = rho_block[cell_id];
1927 acc.rho_vel[newcell_idx] = rho_vel_block[cell_id];
1928 acc.rhoE[newcell_idx] = rhoE_block[cell_id];
1929
1930 if (acc.phi != nullptr) {
1931 acc.phi[newcell_idx] = phi_block[cell_id];
1932 acc.phi_old[newcell_idx] = phi_old_block[cell_id];
1933 }
1934 }
1935 }
1936 };
1937
1941 class RefineCritPseudoGradientAccessor {
1942 public:
1943 Tscal one_over_Nside = 1. / AMRBlock::Nside;
1944 const TgridVec *block_low_bound;
1945 const TgridVec *block_high_bound;
1946 const Tscal *block_rho;
1947 const f64 *block_pressure;
1948 const f64_3 *block_velocity;
1949
1950 const Tscal *rho_cons;
1951 Tscal error_min;
1952 Tscal error_max;
1953 u32 nblock_per_patch;
1954
1955 AMRGraphLinkiterator cell_graph_xp;
1956 AMRGraphLinkiterator cell_graph_xm;
1957 AMRGraphLinkiterator cell_graph_yp;
1958 AMRGraphLinkiterator cell_graph_ym;
1959 AMRGraphLinkiterator cell_graph_zp;
1960 AMRGraphLinkiterator cell_graph_zm;
1961
1962 RefineCritPseudoGradientAccessor(
1963 sham::EventList &depends_list,
1964 Storage &storage,
1965 u64 id_patch,
1968 Tscal err_min,
1969 Tscal err_max)
1970 : error_min(err_min), error_max(err_max),
1971 cell_graph_xp(
1972 shambase::get_check_ref(storage.cell_graph_edge)
1973 .get_refs_dir(Direction_::xp)
1974 .get(id_patch)
1975 .get()
1976 .get_read_access(depends_list)),
1977 cell_graph_xm(
1978 shambase::get_check_ref(storage.cell_graph_edge)
1979 .get_refs_dir(Direction_::xm)
1980 .get(id_patch)
1981 .get()
1982 .get_read_access(depends_list)),
1983 cell_graph_yp(
1984 shambase::get_check_ref(storage.cell_graph_edge)
1985 .get_refs_dir(Direction_::yp)
1986 .get(id_patch)
1987 .get()
1988 .get_read_access(depends_list)),
1989 cell_graph_ym(
1990 shambase::get_check_ref(storage.cell_graph_edge)
1991 .get_refs_dir(Direction_::ym)
1992 .get(id_patch)
1993 .get()
1994 .get_read_access(depends_list)),
1995 cell_graph_zp(
1996 shambase::get_check_ref(storage.cell_graph_edge)
1997 .get_refs_dir(Direction_::zp)
1998 .get(id_patch)
1999 .get()
2000 .get_read_access(depends_list)),
2001 cell_graph_zm(
2002 shambase::get_check_ref(storage.cell_graph_edge)
2003 .get_refs_dir(Direction_::zm)
2004 .get(id_patch)
2005 .get()
2006 .get_read_access(depends_list))
2007
2008 {
2009 block_low_bound = pdat.get_field<TgridVec>(0).get_buf().get_read_access(depends_list);
2010 block_high_bound = pdat.get_field<TgridVec>(1).get_buf().get_read_access(depends_list);
2011 rho_cons = pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
2012 .get_buf()
2013 .get_read_access(depends_list);
2014
2015 nblock_per_patch = pdat.get_obj_cnt();
2016
2017 block_rho = shambase::get_check_ref(storage.rho_primitive)
2018 .get_buf(id_patch)
2019 .get_read_access(depends_list);
2020 block_pressure = shambase::get_check_ref(storage.press)
2021 .get_buf(id_patch)
2022 .get_read_access(depends_list);
2023 block_velocity = shambase::get_check_ref(storage.vel)
2024 .get_buf(id_patch)
2025 .get_read_access(depends_list);
2026 }
2027
2028 void finalize_new(
2029 sham::EventList &resulting_events,
2030 Storage &storage,
2031 u64 id_patch,
2034 Tscal err_min,
2035 Tscal err_max) {
2036
2037 pdat.get_field<i64_3>(0).get_buf().complete_event_state(resulting_events);
2038 pdat.get_field<i64_3>(1).get_buf().complete_event_state(resulting_events);
2039 pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
2040 .get_buf()
2041 .complete_event_state(resulting_events);
2042
2043 shambase::get_check_ref(storage.cell_graph_edge)
2044 .get_refs_dir(Direction_::xp)
2045 .get(id_patch)
2046 .get()
2047 .complete_event_state(resulting_events);
2048
2049 shambase::get_check_ref(storage.cell_graph_edge)
2050 .get_refs_dir(Direction_::xm)
2051 .get(id_patch)
2052 .get()
2053 .complete_event_state(resulting_events);
2054
2055 shambase::get_check_ref(storage.cell_graph_edge)
2056 .get_refs_dir(Direction_::yp)
2057 .get(id_patch)
2058 .get()
2059 .complete_event_state(resulting_events);
2060 shambase::get_check_ref(storage.cell_graph_edge)
2061 .get_refs_dir(Direction_::ym)
2062 .get(id_patch)
2063 .get()
2064 .complete_event_state(resulting_events);
2065 shambase::get_check_ref(storage.cell_graph_edge)
2066 .get_refs_dir(Direction_::zp)
2067 .get(id_patch)
2068 .get()
2069 .complete_event_state(resulting_events);
2070 shambase::get_check_ref(storage.cell_graph_edge)
2071 .get_refs_dir(Direction_::zm)
2072 .get(id_patch)
2073 .get()
2074 .complete_event_state(resulting_events);
2075
2076 shambase::get_check_ref(storage.rho_primitive)
2077 .get_buf(id_patch)
2078 .complete_event_state(resulting_events);
2079
2080 shambase::get_check_ref(storage.press)
2081 .get_buf(id_patch)
2082 .complete_event_state(resulting_events);
2083
2084 shambase::get_check_ref(storage.vel)
2085 .get_buf(id_patch)
2086 .complete_event_state(resulting_events);
2087 }
2088
2089 void refine_criterion_new(
2090 u32 block_id,
2091 RefineCritPseudoGradientAccessor acc,
2092 bool &should_refine,
2093 bool &should_derefine) const {
2094 TgridVec low_bound = acc.block_low_bound[block_id];
2095 TgridVec high_bound = acc.block_high_bound[block_id];
2096
2097 // Tscal block_rho_slope = shambase::VectorProperties<Tscal>::get_zero();
2098 // for (u32 i = 0; i < AMRBlock::block_size; i++) {
2099 // block_rho_slope = sham::details::g_sycl_max(
2100 // block_rho_slope,
2101 // modif_second_derivative<Tscal, Tvec>(
2102 // i + block_id * AMRBlock::block_size,
2103 // cell_graph_xp,
2104 // cell_graph_xm,
2105 // cell_graph_yp,
2106 // cell_graph_ym,
2107 // cell_graph_zp,
2108 // cell_graph_zm,
2109 // [=](u32 id) {
2110 // return acc.block_rho[id];
2111 // }));
2112 // }
2113
2114 Tscal block_rho_slope = shambase::VectorProperties<Tscal>::get_zero();
2115 // for (u32 i = 0; i < AMRBlock::block_size; i++) {
2116 // block_rho_slope = sham::details::g_sycl_max(
2117 // block_rho_slope,
2118 // // baryonic_normalized_slope_criterion<Tscal>
2119 // // get_pseudo_grad<Tscal, Tvec>
2120 // (
2121 // i + block_id * AMRBlock::block_size,
2122 // cell_graph_xp,
2123 // cell_graph_xm,
2124 // cell_graph_yp,
2125 // cell_graph_ym,
2126 // cell_graph_zp,
2127 // cell_graph_zm,
2128 // [=](u32 id) {
2129 // // return rho_cons[id];
2130 // return acc.block_rho[id];
2131 // }));
2132 // }
2133
2134 Tscal block_press_grad = shambase::VectorProperties<Tscal>::get_zero();
2135 for (u32 i = 0; i < AMRBlock::block_size; i++) {
2136 block_press_grad = sham::details::g_sycl_max(
2137 block_press_grad,
2138 get_pseudo_grad<Tscal, Tvec>(
2139 i + block_id * AMRBlock::block_size,
2140 cell_graph_xp,
2141 cell_graph_xm,
2142 cell_graph_yp,
2143 cell_graph_ym,
2144 cell_graph_zp,
2145 cell_graph_zm,
2146 [=, this](u32 id) {
2147 return block_pressure[id];
2148 }));
2149 }
2150
2151 Tscal error = sham::details::g_sycl_max(
2152 block_press_grad, sham::details::g_sycl_max(block_rho_slope, 0.0));
2153
2154 should_refine = false;
2155 should_derefine = false;
2156 if (error > error_max) {
2157 should_refine = true;
2158 } else if (error < (error_min * error_max)) {
2159 should_derefine = true;
2160 }
2161
2162 should_refine = should_refine && (high_bound.x() - low_bound.x() > AMRBlock::Nside);
2163 should_refine = should_refine && (high_bound.y() - low_bound.y() > AMRBlock::Nside);
2164 should_refine = should_refine && (high_bound.z() - low_bound.z() > AMRBlock::Nside);
2165 }
2166 };
2167
2171 class RefineCritShearAccessor {
2172 public:
2173 Tscal one_over_Nside = 1. / AMRBlock::Nside;
2174 const TgridVec *block_low_bound;
2175 const TgridVec *block_high_bound;
2176 const Tscal *block_rho;
2177 const f64 *block_pressure;
2178 const f64_3 *block_velocity;
2179
2180 Tscal threshold;
2181 Tscal gamma;
2182 Tscal dxfact;
2183
2184 AMRGraphLinkiterator cell_graph_xp;
2185 AMRGraphLinkiterator cell_graph_xm;
2186 AMRGraphLinkiterator cell_graph_yp;
2187 AMRGraphLinkiterator cell_graph_ym;
2188 AMRGraphLinkiterator cell_graph_zp;
2189 AMRGraphLinkiterator cell_graph_zm;
2190
2191 RefineCritShearAccessor(
2192 sham::EventList &depends_list,
2193 Storage &storage,
2194 u64 id_patch,
2197 Tscal threshold,
2198 Tscal gamma,
2199 Tscal dxfact)
2200 : threshold(threshold), gamma(gamma), dxfact(dxfact),
2201 cell_graph_xp(
2202 shambase::get_check_ref(storage.cell_graph_edge)
2203 .get_refs_dir(Direction_::xp)
2204 .get(id_patch)
2205 .get()
2206 .get_read_access(depends_list)),
2207 cell_graph_xm(
2208 shambase::get_check_ref(storage.cell_graph_edge)
2209 .get_refs_dir(Direction_::xm)
2210 .get(id_patch)
2211 .get()
2212 .get_read_access(depends_list)),
2213 cell_graph_yp(
2214 shambase::get_check_ref(storage.cell_graph_edge)
2215 .get_refs_dir(Direction_::yp)
2216 .get(id_patch)
2217 .get()
2218 .get_read_access(depends_list)),
2219 cell_graph_ym(
2220 shambase::get_check_ref(storage.cell_graph_edge)
2221 .get_refs_dir(Direction_::ym)
2222 .get(id_patch)
2223 .get()
2224 .get_read_access(depends_list)),
2225 cell_graph_zp(
2226 shambase::get_check_ref(storage.cell_graph_edge)
2227 .get_refs_dir(Direction_::zp)
2228 .get(id_patch)
2229 .get()
2230 .get_read_access(depends_list)),
2231 cell_graph_zm(
2232 shambase::get_check_ref(storage.cell_graph_edge)
2233 .get_refs_dir(Direction_::zm)
2234 .get(id_patch)
2235 .get()
2236 .get_read_access(depends_list))
2237
2238 {
2239 block_low_bound = pdat.get_field<TgridVec>(0).get_buf().get_read_access(depends_list);
2240 block_high_bound = pdat.get_field<TgridVec>(1).get_buf().get_read_access(depends_list);
2241
2242 block_rho = shambase::get_check_ref(storage.rho_primitive)
2243 .get_buf(id_patch)
2244 .get_read_access(depends_list);
2245 block_pressure = shambase::get_check_ref(storage.press)
2246 .get_buf(id_patch)
2247 .get_read_access(depends_list);
2248 block_velocity = shambase::get_check_ref(storage.vel)
2249 .get_buf(id_patch)
2250 .get_read_access(depends_list);
2251 }
2252
2253 void finalize_new(
2254 sham::EventList &resulting_events,
2255 Storage &storage,
2256 u64 id_patch,
2259 Tscal threshold,
2260 Tscal gamma,
2261 Tscal dxfact) {
2262
2263 pdat.get_field<i64_3>(0).get_buf().complete_event_state(resulting_events);
2264 pdat.get_field<i64_3>(1).get_buf().complete_event_state(resulting_events);
2265
2266 shambase::get_check_ref(storage.cell_graph_edge)
2267 .get_refs_dir(Direction_::xp)
2268 .get(id_patch)
2269 .get()
2270 .complete_event_state(resulting_events);
2271
2272 shambase::get_check_ref(storage.cell_graph_edge)
2273 .get_refs_dir(Direction_::xm)
2274 .get(id_patch)
2275 .get()
2276 .complete_event_state(resulting_events);
2277
2278 shambase::get_check_ref(storage.cell_graph_edge)
2279 .get_refs_dir(Direction_::yp)
2280 .get(id_patch)
2281 .get()
2282 .complete_event_state(resulting_events);
2283 shambase::get_check_ref(storage.cell_graph_edge)
2284 .get_refs_dir(Direction_::ym)
2285 .get(id_patch)
2286 .get()
2287 .complete_event_state(resulting_events);
2288 shambase::get_check_ref(storage.cell_graph_edge)
2289 .get_refs_dir(Direction_::zp)
2290 .get(id_patch)
2291 .get()
2292 .complete_event_state(resulting_events);
2293 shambase::get_check_ref(storage.cell_graph_edge)
2294 .get_refs_dir(Direction_::zm)
2295 .get(id_patch)
2296 .get()
2297 .complete_event_state(resulting_events);
2298
2299 shambase::get_check_ref(storage.rho_primitive)
2300 .get_buf(id_patch)
2301 .complete_event_state(resulting_events);
2302
2303 shambase::get_check_ref(storage.press)
2304 .get_buf(id_patch)
2305 .complete_event_state(resulting_events);
2306
2307 shambase::get_check_ref(storage.vel)
2308 .get_buf(id_patch)
2309 .complete_event_state(resulting_events);
2310 }
2311
2312 void refine_criterion_new(
2313 u32 block_id,
2314 RefineCritShearAccessor acc,
2315 bool &should_refine,
2316 bool &should_derefine) const {
2317 TgridVec low_bound = acc.block_low_bound[block_id];
2318 TgridVec high_bound = acc.block_high_bound[block_id];
2319
2320 Tvec block_cell_size
2321 = (high_bound - low_bound).template convert<Tscal>() * (dxfact * one_over_Nside);
2322
2323 Tscal block_normalized_shear = shambase::VectorProperties<Tscal>::get_zero();
2324 for (u32 i = 0; i < AMRBlock::block_size; i++) {
2325 auto cell_id = i + block_id * AMRBlock::block_size;
2326 auto cs = sycl::sqrt(gamma * acc.block_pressure[cell_id] / acc.block_rho[cell_id]);
2327 block_normalized_shear = sham::details::g_sycl_max(
2328 block_normalized_shear,
2329 normalized_shear<Tvec>(
2330 cell_id,
2331 cs,
2332 block_cell_size,
2333
2334 cell_graph_xp,
2335 cell_graph_xm,
2336 cell_graph_yp,
2337 cell_graph_ym,
2338 cell_graph_zp,
2339 cell_graph_zm,
2340 [=](u32 id) {
2341 return acc.block_velocity[id];
2342 }));
2343 }
2344 should_refine = false;
2345 should_derefine = false;
2346 if (block_normalized_shear > threshold * threshold) {
2347 should_refine = true;
2348 } else if (block_normalized_shear < 0.25 * threshold * threshold) {
2349 should_derefine = true;
2350 }
2351
2352 should_refine = should_refine && (high_bound.x() - low_bound.x() > AMRBlock::Nside);
2353 should_refine = should_refine && (high_bound.y() - low_bound.y() > AMRBlock::Nside);
2354 should_refine = should_refine && (high_bound.z() - low_bound.z() > AMRBlock::Nside);
2355 }
2356 };
2357
2366 class RefineCritJeansLengthAccessor {
2367 public:
2368 Tscal one_over_Nside = 1. / AMRBlock::Nside;
2369 const TgridVec *block_low_bound;
2370 const TgridVec *block_high_bound;
2371 const Tscal *rho;
2372 const Tvec *rhovel;
2373 const Tscal *rhoetot;
2374
2375 Tscal dxfact;
2376 Tscal N_J;
2377 Tscal G;
2378
2379 // sound speed: of the barotrope if use_barotrope, adiabatic otherwise
2380 bool use_barotrope;
2383
2384 RefineCritJeansLengthAccessor(
2385 sham::EventList &depends_list,
2386 Storage &storage,
2387 u64 id_patch,
2390 Tscal dxfact,
2391 Tscal N_J,
2392 Tscal G,
2393 bool use_barotrope,
2396 : dxfact(dxfact), N_J(N_J), G(G), use_barotrope(use_barotrope), barotrope(barotrope),
2397 adiabatic(adiabatic) {
2398 block_low_bound = pdat.get_field<TgridVec>(0).get_buf().get_read_access(depends_list);
2399 block_high_bound = pdat.get_field<TgridVec>(1).get_buf().get_read_access(depends_list);
2400 rho = pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
2401 .get_buf()
2402 .get_read_access(depends_list);
2403 rhovel = pdat.get_field<Tvec>(pdat.pdl().get_field_idx<Tvec>("rhovel"))
2404 .get_buf()
2405 .get_read_access(depends_list);
2406 rhoetot = pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rhoetot"))
2407 .get_buf()
2408 .get_read_access(depends_list);
2409 }
2410
2411 void finalize_new(
2412 sham::EventList &resulting_events,
2413 Storage &storage,
2414 u64 id_patch,
2417 Tscal dxfact,
2418 Tscal N_J,
2419 Tscal G,
2420 bool use_barotrope,
2423 pdat.get_field<TgridVec>(0).get_buf().complete_event_state(resulting_events);
2424 pdat.get_field<TgridVec>(1).get_buf().complete_event_state(resulting_events);
2425 pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rho"))
2426 .get_buf()
2427 .complete_event_state(resulting_events);
2428 pdat.get_field<Tvec>(pdat.pdl().get_field_idx<Tvec>("rhovel"))
2429 .get_buf()
2430 .complete_event_state(resulting_events);
2431 pdat.get_field<Tscal>(pdat.pdl().get_field_idx<Tscal>("rhoetot"))
2432 .get_buf()
2433 .complete_event_state(resulting_events);
2434 }
2435
2436 void refine_criterion_new(
2437 u32 block_id,
2438 RefineCritJeansLengthAccessor acc,
2439 bool &should_refine,
2440 bool &should_derefine) const {
2441 TgridVec low_bound = acc.block_low_bound[block_id];
2442 TgridVec high_bound = acc.block_high_bound[block_id];
2443
2444 // size from the int coords difference, so that it does not depend on the block position
2445 Tscal dx = Tscal(high_bound.x() - low_bound.x()) * (dxfact * one_over_Nside);
2446
2447 // max over the block of q = (N_J dx / lambda_J)^2 = (N_J dx)^2 G rho / (pi cs^2)
2448 Tscal q_max = 0;
2449 for (u32 i = 0; i < AMRBlock::block_size; i++) {
2450 const u32 cell_id = i + block_id * AMRBlock::block_size;
2451
2453 cons.rho = acc.rho[cell_id];
2454 cons.rhovel = acc.rhovel[cell_id];
2455 cons.rhoe = acc.rhoetot[cell_id];
2456
2457 const Tscal cs = (use_barotrope)
2458 ? barotrope.sound_speed(barotrope.cons_to_prim(cons))
2459 : adiabatic.sound_speed(adiabatic.cons_to_prim(cons));
2460
2461 const Tscal q = (N_J * dx) * (N_J * dx) * G * cons.rho
2462 / (shambase::constants::pi<Tscal> * cs * cs);
2463 q_max = sham::max(q_max, q);
2464 }
2465
2466 should_refine = q_max > 1;
2467 should_derefine = 4 * q_max < 0.5;
2468
2469 should_refine = should_refine && (high_bound.x() - low_bound.x() > AMRBlock::Nside);
2470 should_refine = should_refine && (high_bound.y() - low_bound.y() > AMRBlock::Nside);
2471 should_refine = should_refine && (high_bound.z() - low_bound.z() > AMRBlock::Nside);
2472 }
2473 };
2474
2478 class RefineCellAccessorAutogravity {
2479 public:
2480 f64 *rho;
2481 f64_3 *rho_vel;
2482 f64 *rhoE;
2483 f64 *phi_old;
2484 f64 *phi_new;
2485
2486 u64 p_id;
2487 // f64* cell_sizes;
2488
2489 // this will be needed for interpolation during refinement
2490 AMRGraphLinkiterator cell_graph_xp;
2491 AMRGraphLinkiterator cell_graph_xm;
2492 AMRGraphLinkiterator cell_graph_yp;
2493 AMRGraphLinkiterator cell_graph_ym;
2494 AMRGraphLinkiterator cell_graph_zp;
2495 AMRGraphLinkiterator cell_graph_zm;
2496
2497 RefineCellAccessorAutogravity(
2498 sham::EventList &depends_list,
2499 Storage &storage,
2500 u64 &id_patch,
2502 : cell_graph_xp(
2503 shambase::get_check_ref(storage.cell_graph_edge)
2504 .get_refs_dir(Direction::xp)
2505 .get(id_patch)
2506 .get()
2507 .get_read_access(depends_list)),
2508 cell_graph_xm(
2509 shambase::get_check_ref(storage.cell_graph_edge)
2510 .get_refs_dir(Direction::xm)
2511 .get(id_patch)
2512 .get()
2513 .get_read_access(depends_list)),
2514 cell_graph_yp(
2515 shambase::get_check_ref(storage.cell_graph_edge)
2516 .get_refs_dir(Direction::yp)
2517 .get(id_patch)
2518 .get()
2519 .get_read_access(depends_list)),
2520 cell_graph_ym(
2521 shambase::get_check_ref(storage.cell_graph_edge)
2522 .get_refs_dir(Direction::ym)
2523 .get(id_patch)
2524 .get()
2525 .get_read_access(depends_list)),
2526 cell_graph_zp(
2527 shambase::get_check_ref(storage.cell_graph_edge)
2528 .get_refs_dir(Direction::zp)
2529 .get(id_patch)
2530 .get()
2531 .get_read_access(depends_list)),
2532 cell_graph_zm(
2533 shambase::get_check_ref(storage.cell_graph_edge)
2534 .get_refs_dir(Direction::zm)
2535 .get(id_patch)
2536 .get()
2537 .get_read_access(depends_list))
2538
2539 {
2540 p_id = id_patch;
2541 rho = pdat.get_field<f64>(2).get_buf().get_write_access(depends_list);
2542 rho_vel = pdat.get_field<f64_3>(3).get_buf().get_write_access(depends_list);
2543 rhoE = pdat.get_field<f64>(4).get_buf().get_write_access(depends_list);
2544 phi_old = pdat.get_field<f64>(pdat.pdl().get_field_idx<Tscal>("phi_old"))
2545 .get_buf()
2546 .get_write_access(depends_list);
2547 phi_new = pdat.get_field<f64>(pdat.pdl().get_field_idx<Tscal>("phi"))
2548 .get_buf()
2549 .get_write_access(depends_list);
2550 }
2551
2552 void finalize_new(
2553 sham::EventList &resulting_events,
2554 Storage &storage,
2555 u64 &id_patch,
2557 pdat.get_field<f64>(2).get_buf().complete_event_state(resulting_events);
2558 pdat.get_field<f64_3>(3).get_buf().complete_event_state(resulting_events);
2559 pdat.get_field<f64>(4).get_buf().complete_event_state(resulting_events);
2560 pdat.get_field<f64>(pdat.pdl().get_field_idx<Tscal>("phi_old"))
2561 .get_buf()
2562 .complete_event_state(resulting_events);
2563 pdat.get_field<f64>(pdat.pdl().get_field_idx<Tscal>("phi"))
2564 .get_buf()
2565 .complete_event_state(resulting_events);
2566
2567 shambase::get_check_ref(storage.cell_graph_edge)
2568 .get_refs_dir(Direction_::xp)
2569 .get(id_patch)
2570 .get()
2571 .complete_event_state(resulting_events);
2572 shambase::get_check_ref(storage.cell_graph_edge)
2573 .get_refs_dir(Direction_::xm)
2574 .get(id_patch)
2575 .get()
2576 .complete_event_state(resulting_events);
2577 shambase::get_check_ref(storage.cell_graph_edge)
2578 .get_refs_dir(Direction_::yp)
2579 .get(id_patch)
2580 .get()
2581 .complete_event_state(resulting_events);
2582 shambase::get_check_ref(storage.cell_graph_edge)
2583 .get_refs_dir(Direction_::ym)
2584 .get(id_patch)
2585 .get()
2586 .complete_event_state(resulting_events);
2587 shambase::get_check_ref(storage.cell_graph_edge)
2588 .get_refs_dir(Direction_::zp)
2589 .get(id_patch)
2590 .get()
2591 .complete_event_state(resulting_events);
2592 shambase::get_check_ref(storage.cell_graph_edge)
2593 .get_refs_dir(Direction_::zm)
2594 .get(id_patch)
2595 .get()
2596 .complete_event_state(resulting_events);
2597 }
2598
2599 void apply_refine_new(
2600 u32 cur_idx,
2601 BlockCoord cur_coords,
2602 std::array<u32, 8> new_blocks,
2603 std::array<BlockCoord, 8> new_block_coords,
2604 RefineCellAccessorAutogravity acc) const {
2605
2606 auto get_coord_ref = [](u32 i) -> std::array<u32, dim> {
2607 constexpr u32 NsideBlockPow = 1;
2608 constexpr u32 Nside = 1U << NsideBlockPow;
2609
2610 if constexpr (dim == 3) {
2611 const u32 tmp = i >> NsideBlockPow;
2612 return {i % Nside, (tmp) % Nside, (tmp) >> NsideBlockPow};
2613 }
2614 };
2615
2616 auto get_index_block = [](std::array<u32, dim> coord) -> u32 {
2617 constexpr u32 NsideBlockPow = 1;
2618 constexpr u32 Nside = 1U << NsideBlockPow;
2619
2620 if constexpr (dim == 3) {
2621 return coord[0] + Nside * coord[1] + Nside * Nside * coord[2];
2622 }
2623 };
2624
2625 auto get_gid_write = [&](std::array<u32, dim> &glid) -> u32 {
2626 // First, get the block id (it's the block to be refine) in wich the new cell glid
2627 // is located.
2628 std::array<u32, dim> bid
2629 = {glid[0] >> AMRBlock::NsideBlockPow,
2630 glid[1] >> AMRBlock::NsideBlockPow,
2631 glid[2] >> AMRBlock::NsideBlockPow};
2632
2633 // get the new global block id
2634 auto new_glob_id = new_blocks[get_index_block(bid)] * AMRBlock::block_size;
2635
2636 // then added to new_glob_id the local index (between 0 and 7) of the generated
2637 // cells to get. This give the global ids of the new generated cells.
2638 return new_glob_id
2639 + AMRBlock::get_index(
2640 {glid[0] % AMRBlock::Nside,
2641 glid[1] % AMRBlock::Nside,
2642 glid[2] % AMRBlock::Nside});
2643 };
2644
2645 std::array<f64, AMRBlock::block_size> old_rho_block;
2646 std::array<f64_3, AMRBlock::block_size> old_rho_vel_block;
2647 std::array<f64, AMRBlock::block_size> old_rhoE_block;
2648 std::array<f64, AMRBlock::block_size> old_phi_old_block;
2649 std::array<f64, AMRBlock::block_size> old_phi_new_block;
2650
2651 // save old block
2652 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
2653
2654 auto [lx, ly, lz] = get_coord_ref(loc_id);
2655 u32 old_cell_idx = cur_idx * AMRBlock::block_size + loc_id;
2656 old_rho_block[loc_id] = acc.rho[old_cell_idx];
2657 old_rho_vel_block[loc_id] = acc.rho_vel[old_cell_idx];
2658 old_rhoE_block[loc_id] = acc.rhoE[old_cell_idx];
2659 old_phi_old_block[loc_id] = acc.phi_old[old_cell_idx];
2660 old_phi_new_block[loc_id] = acc.phi_new[old_cell_idx];
2661 }
2662
2663 for (u32 loc_id = 0; loc_id < AMRBlock::block_size; loc_id++) {
2664
2665 auto [lx, ly, lz] = get_coord_ref(loc_id);
2666 u32 old_cell_idx = cur_idx * AMRBlock::block_size + loc_id;
2667
2668 // // // cell size in the refined block
2669 // // Tscal delta_cell = cell_sizes[cur_idx];
2670 // // Tscal c_offset = delta_cell * 0.25;
2671 // // std::array<f64_3, AMRBlock::block_size> child_center_offsets;
2672 // // child_center_offsets[0] = {-c_offset, -c_offset, -c_offset}; /*(0,0,0) */
2673 // // child_center_offsets[1] = {c_offset, -c_offset, -c_offset}; /*(1,0,0)*/
2674 // // child_center_offsets[2] = {-c_offset, c_offset, -c_offset}; /* (0,1,0)*/
2675 // // child_center_offsets[3] = {c_offset, c_offset, -c_offset}; /*(1,1,0)*/
2676 // // child_center_offsets[4] = {-c_offset, -c_offset, c_offset}; /*(0,0,1)*/
2677 // // child_center_offsets[5] = {c_offset, -c_offset, c_offset}; /*(1,0,1)*/
2678 // // child_center_offsets[6] = {-c_offset, c_offset, c_offset}; /*(0,1,1)*/
2679 // // child_center_offsets[7] = {c_offset, c_offset, c_offset}; /*(1,1,1)*/
2680
2681 // auto cons_var_slopes = get_3d_grad_cons<Tvec, Minmod>(
2682 // old_cell_idx,
2683 // delta_cell,
2684 // cell_graph_xp,
2685 // cell_graph_xm,
2686 // cell_graph_yp,
2687 // cell_graph_ym,
2688 // cell_graph_zp,
2689 // cell_graph_zm,
2690 // [=](u32 id){
2691 // return acc.rho[id];
2692 // },
2693 // [=](u32 id){
2694 // return acc.rho_vel[id];
2695 // },
2696 // [=](u32 id){
2697 // return acc.rhoE[id];
2698 // });
2699
2700 Tscal rho_block = old_rho_block[loc_id];
2701 Tvec rho_vel_block = old_rho_vel_block[loc_id];
2702 Tscal rhoE_block = old_rhoE_block[loc_id];
2703 Tscal phi_old_block = old_phi_old_block[loc_id];
2704 Tscal phi_new_block = old_phi_new_block[loc_id];
2705
2706 for (u32 subdiv_lid = 0; subdiv_lid < 8; subdiv_lid++) {
2707
2708 auto [sx, sy, sz] = get_coord_ref(subdiv_lid);
2709
2710 std::array<u32, 3> glid = {lx * 2 + sx, ly * 2 + sy, lz * 2 + sz};
2711
2712 u32 new_cell_idx = get_gid_write(glid);
2713
2714 // shammath::ConsState<Tvec> cons_var_interp =
2715 // child_center_offsets[subdiv_lid][0] * cons_var_slopes[0] +
2716 // child_center_offsets[subdiv_lid][1] * cons_var_slopes[1] +
2717 // child_center_offsets[subdiv_lid][2] * cons_var_slopes[2];
2718
2719 // // acc.rho[new_cell_idx] = rho_block + cons_var_interp.rho ;
2720 // // acc.rho_vel[new_cell_idx] = rho_vel_block + cons_var_interp.rhovel;
2721 // // acc.rhoE[new_cell_idx] = rhoE_block + cons_var_interp.rhoe;
2722
2723 acc.rho[new_cell_idx] = rho_block;
2724 acc.rho_vel[new_cell_idx] = rho_vel_block;
2725 acc.rhoE[new_cell_idx] = rhoE_block;
2726 acc.phi_old[new_cell_idx] = phi_old_block;
2727 acc.phi_new[new_cell_idx] = phi_new_block;
2728 }
2729 }
2730 }
2731
2732 void apply_derefine_new(
2733 std::array<u32, 8> old_blocks,
2734 std::array<BlockCoord, 8> old_coords,
2735 u32 new_cell,
2736 BlockCoord new_coord,
2737
2738 RefineCellAccessorAutogravity acc) const {
2739
2740 std::array<f64, AMRBlock::block_size> rho_block;
2741 std::array<f64_3, AMRBlock::block_size> rho_vel_block;
2742 std::array<f64, AMRBlock::block_size> rhoE_block;
2743 std::array<f64, AMRBlock::block_size> phi_old_block;
2744 std::array<f64, AMRBlock::block_size> phi_new_block;
2745
2746 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
2747 phi_old_block[cell_id] = {};
2748 phi_new_block[cell_id] = {};
2749 }
2750
2751 // for each siblings block, perform restriction from its 8 children cells
2752 for (u32 pid = 0; pid < 8; pid++) {
2753 Tscal rho_pid = {};
2754 Tvec rho_vel_pid = {};
2755 Tscal rhoe_pid = {};
2756 Tscal phi_old_pid = {};
2757 Tscal phi_new_pid = {};
2758
2759 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
2760 rho_pid += acc.rho[old_blocks[pid] * AMRBlock::block_size + cell_id];
2761 rho_vel_pid += acc.rho_vel[old_blocks[pid] * AMRBlock::block_size + cell_id];
2762 rhoe_pid += acc.rhoE[old_blocks[pid] * AMRBlock::block_size + cell_id];
2763 phi_old_pid += acc.phi_old[old_blocks[pid] * AMRBlock::block_size + cell_id];
2764 phi_new_pid += acc.phi_new[old_blocks[pid] * AMRBlock::block_size + cell_id];
2765 }
2766 // the siblings are in memory order, the cells of the merged block are x fastest
2767 u32 cell_id = get_merged_cell_index<AMRBlock>(old_coords[pid].bmin, new_coord.bmin);
2768
2769 rho_block[cell_id] = rho_pid * (1. / 8.);
2770 rho_vel_block[cell_id] = rho_vel_pid * (1. / 8.);
2771 rhoE_block[cell_id] = rhoe_pid * (1. / 8.);
2772 // phi_old_block[cell_id] = phi_old_pid * (1. / 8.);
2773 // phi_new_block[cell_id] = phi_new_pid * (1. / 8.);
2774 }
2775
2776 for (u32 cell_id = 0; cell_id < AMRBlock::block_size; cell_id++) {
2777 u32 newcell_idx = new_cell * AMRBlock::block_size + cell_id;
2778 acc.rho[newcell_idx] = rho_block[cell_id];
2779 acc.rho_vel[newcell_idx] = rho_vel_block[cell_id];
2780 acc.rhoE[newcell_idx] = rhoE_block[cell_id];
2781
2782 // acc.phi_old[newcell_idx] = phi_old_block[cell_id];
2783 // acc.phi_new[newcell_idx] = phi_new_block[cell_id];
2784 }
2785 }
2786 };
2787
2788 using AMRmode_None = typename AMRMode<Tvec, TgridVec>::None;
2789 using AMRmode_DensityBased = typename AMRMode<Tvec, TgridVec>::DensityBased;
2790 using AMRmode_PseudoGradientBased = typename AMRMode<Tvec, TgridVec>::PseudoGradientBased;
2791 using AMRmode_JeansLengthBased = typename AMRMode<Tvec, TgridVec>::JeansLengthBased;
2792 using AMRmode_ShearBased = typename AMRMode<Tvec, TgridVec>::ShearBased;
2793
2794 bool has_cell_order_changed = false;
2795
2796 // get refine and derefine list
2799
2800 if (AMRmode_None *cfg = std::get_if<AMRmode_None>(&solver_config.amr_mode.config)) {
2801 // no refinment here turn around there is nothing to see
2802 } else {
2803 if (AMRmode_DensityBased *cfg
2804 = std::get_if<AMRmode_DensityBased>(&solver_config.amr_mode.config)) {
2805
2806 Tscal dxfact(solver_config.grid_coord_to_pos_fact);
2807 gen_refine_block_changes_new<RefineCritBlock>(
2808 refine_list, derefine_list, dxfact, cfg->crit_mass);
2809 }
2810
2811 else if (
2812 AMRmode_PseudoGradientBased *cfg
2813 = std::get_if<AMRmode_PseudoGradientBased>(&solver_config.amr_mode.config)) {
2814
2815 gen_refine_block_changes_new<RefineCritPseudoGradientAccessor>(
2816 refine_list, derefine_list, cfg->error_min, cfg->error_max);
2817 }
2818
2819 else if (
2820 AMRmode_ShearBased *cfg
2821 = std::get_if<AMRmode_ShearBased>(&solver_config.amr_mode.config)) {
2822 Tscal dxfact(solver_config.grid_coord_to_pos_fact);
2823 Tscal gamma(solver_config.get_eos_gamma());
2824
2825 gen_refine_block_changes_new<RefineCritShearAccessor>(
2826 refine_list, derefine_list, cfg->threshold, gamma, dxfact);
2827 }
2828
2829 else if (
2830 AMRmode_JeansLengthBased *cfg
2831 = std::get_if<AMRmode_JeansLengthBased>(&solver_config.amr_mode.config)) {
2832 Tscal dxfact(solver_config.grid_coord_to_pos_fact);
2833 Tscal G = solver_config.get_constant_G();
2834
2835 using CoolingInstantBarotropic = typename CoolingConfig<Tvec>::InstantBarotropic;
2836 const auto *baro_cfg
2837 = std::get_if<CoolingInstantBarotropic>(&solver_config.cooling_config.config);
2838
2839 // the barotrope is unused (only to fill the accessor) without the InstantBarotropic
2840 // cooling
2841 bool use_barotrope = baro_cfg != nullptr;
2843 = (use_barotrope) ? baro_cfg->get_barotrope()
2844 : shammath::FluidStateBarotropic<Tvec>(1, 1, 5. / 3.);
2845 shammath::FluidStateAdiabatic<Tvec> adiabatic{.m_gamma = solver_config.get_eos_gamma()};
2846
2847 gen_refine_block_changes_new<RefineCritJeansLengthAccessor>(
2848 refine_list,
2849 derefine_list,
2850 dxfact,
2851 Tscal(cfg->N_J),
2852 G,
2853 use_barotrope,
2854 barotrope,
2855 adiabatic);
2856 }
2857
2858 else {
2859 shambase::throw_unimplemented("this AMR mode is not implemented in the new AMR");
2860 }
2861
2863 enforce_two_to_one_refinement_new(refine_list);
2865 check_geometrical_validity(refine_list, derefine_list);
2867 enforce_two_to_one_derefinement_new(derefine_list, refine_list);
2869 // Note that this only add new blocks at the end of the patchdata
2870 const AMRInterpMode amr_ref_interp_mode = solver_config.amr_interp_mode;
2871 bool change_refine = internal_refine_grid_new<RefineCellAccessor>(
2872 std::move(refine_list), amr_ref_interp_mode);
2873
2875 // Note that this will perform the merge then remove the old blocks
2876 // This is ok to call straight after the refine without edditing the index list in
2877 // derefine_list since no permutations were applied in internal_refine_grid_new and no cells
2878 // can be both refined and derefined in the same pass
2879 bool change_derefine = internal_derefine_grid_new<RefineCellAccessor>(
2880 std::move(derefine_list), amr_ref_interp_mode);
2881
2882 has_cell_order_changed = has_cell_order_changed || (change_refine || change_derefine);
2883
2884 if (has_cell_order_changed) {
2885 // Ensure that the blocks are sorted before refinement
2886 AMRSortBlocks block_sorter(context, solver_config, storage);
2887 block_sorter.reorder_amr_blocks();
2888 }
2889 }
2890}
2891
double f64
Alias for double.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
void copy_range(size_t begin, size_t end, sham::DeviceBuffer< T, dest_target > &dest) const
Copy a range of elements from the buffer to another buffer.
size_t get_size() const
Gets the number of elements in the buffer.
const T * get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const
Get a read-only pointer to the buffer's data.
A SYCL queue associated with a device and a context.
sycl::queue q
The SYCL queue associated with this context.
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
void add_event(sycl::event e)
Add an event to the list of events.
Definition EventList.hpp:88
Represents a collection of objects distributed across patches identified by a u64 id.
FluidStateSpec implementation for a barotropic equation of state that is isothermal below a threshold...
bool has_field_name(const std::string &field_name) const
Check whether a field with the given name already exists.
PatchDataLayer container class, the layout is described in patchdata_layout.
u32 get_obj_cnt() const
get the number of objects (particles) stored in this layer
void index_remap_resize(sycl::buffer< u32 > &index_map, u32 len)
this function remaps the patchdatafield like so val[id] = val[index_map[id]] This function can be use...
Class holding the value of numerous constants generated from the following source.
main include file for the shamalgs algorithms
Slope mode enum + json serialization/deserialization.
This header file contains utility functions related to exception handling in the code.
alias namespace to simplify the use of log functions
Definition logs.hpp:318
sycl::buffer< typename std::invoke_result_t< Fct, u32 > > gen_buffer_device(sycl::queue &q, u32 len, Fct &&func)
generate a buffer from a lambda expression based on the indexes
Definition algorithm.hpp:67
std::tuple< std::optional< sycl::buffer< u32 > >, u32 > stream_compact(sycl::queue &q, sycl::buffer< u32 > &buf_flags, u32 len)
Stream compaction algorithm.
Definition numeric.cpp:65
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
std::vector< std::string_view > args
Executable argument list (mapped from argv).
Definition cmdopt.cpp:63
void raw_ln(Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:89
void info_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
FluidStateSpec implementation for an ideal (adiabatic) gas equation of state.
Refine the blocks where the Jeans length is resolved by less than N_J cells.
Instantaneous cooling onto a barotrope (RAMSES-like).
Patch object that contain generic patch information.
Definition Patch.hpp:33
u64 id_patch
unique key that identify the patch
Definition Patch.hpp:86