Note
Go to the end to download the full example code.
CI test: every sibling group merges in a uniform RAMSES grid (new AMR)#
All the blocks of a uniform grid are below the density based merge criterion
(cfg.set_amr_mode_density_based), so one AMR pass of the new AMR
(cfg.set_amr_mode_old(False)) must merge every group of 8 siblings: a base
grid of base^3 blocks becomes base^3 / 8 blocks twice as large.
This is a regression test for a race in the kernel validating the derefinement
flags, which read the flags of the blocks id .. id+7 while writing the
result for the block id into the same buffer. Depending on the execution
order of the work-items, whole sibling groups were refused (with several
OpenMP threads on the AdaptiveCpp OpenMP backend, most of them were).
17 import shamrock
18
19 if not shamrock.sys.is_initialized():
20 shamrock.change_loglevel(1)
21 shamrock.sys.init("0:0")
22
23 # the new AMR is experimental
24 shamrock.enable_experimental_features()
25
26 # int coords size of a base block
27 sz = 2
28
29 # number of base blocks per side, chosen so that the sibling groups are not aligned on the
30 # chunks of the work-items split between threads
31 bases = [6, 10]
32 n_repeat = 3
33
34
35 def run_one_amr_pass(base):
36 ctx = shamrock.Context()
37 ctx.pdata_layout_new()
38
39 model = shamrock.get_Model_Ramses(context=ctx, vector_type="f64_3", grid_repr="i64_3")
40
41 cfg = model.gen_default_config()
42 cfg.set_scale_factor(1.0 / (sz * base))
43 cfg.set_eos_gamma(5.0 / 3.0)
44 cfg.set_amr_mode_old(False)
45 # the mass of a block is at most 1 in the unit box, so every block wants to merge
46 cfg.set_amr_mode_density_based(crit_mass=10.0)
47 model.set_solver_config(cfg)
48
49 # a single patch holding all the blocks
50 model.init_scheduler(int(1e7), 1)
51 model.make_base_grid((0, 0, 0), (sz, sz, sz), (base, base, base))
52
53 model.set_field_value_lambda_f64("rho", lambda rmin, rmax: 1.0)
54 model.set_field_value_lambda_f64("rhoetot", lambda rmin, rmax: 1.0)
55 model.set_field_value_lambda_f64_3("rhovel", lambda rmin, rmax: (0.0, 0.0, 0.0))
56
57 # the AMR pass is done at the end of a step with dt > 0
58 model.evolve_once_override_time(0.0, 1e-6)
59
60 data = ctx.collect_data()
61 return data["cell_min"], data["cell_max"]
62
63
64 errors = []
65 for base in bases:
66 expected_count = base**3 // 8
67 for i in range(n_repeat):
68 cell_min, cell_max = run_one_amr_pass(base)
69 count = len(cell_min)
70 refused = (count - expected_count) // 7
71 print(f"base={base} run={i}: {count} blocks (expected {expected_count}), {refused} refused")
72
73 if count != expected_count:
74 errors.append(f"base={base} run={i}: {refused} sibling groups were not merged")
75 continue
76
77 for bmin, bmax in zip(cell_min, cell_max):
78 if any(hi - lo != 2 * sz for lo, hi in zip(bmin, bmax)):
79 errors.append(f"base={base} run={i}: unexpected block {bmin} {bmax}")
80 break
81
82 if errors:
83 raise RuntimeError("derefinement of a uniform grid failed:\n" + "\n".join(errors))
Estimated memory usage: 0 MB