Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
AMRCell.hpp
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
10#pragma once
11
19#include "shambackends/math.hpp"
22
23namespace shamrock::amr {
24
25 template<class Tcoord, u32 dim>
27
28 public:
29 static constexpr u32 splts_count = 1U << dim;
30 Tcoord bmin, bmax;
31
32 [[nodiscard]] inline static auto get_split_coord(Tcoord bmin, Tcoord bmax) -> Tcoord {
33 return (bmax - bmin) / 2 + bmin;
34 }
35
36 inline static auto get_split(Tcoord bmin, Tcoord bmax)
37 -> std::array<AMRBlockCoord, splts_count> {
38
39 std::array<AMRBlockCoord, splts_count> ret;
40
41 Tcoord splts = get_split_coord(bmin, bmax);
42 std::array<Tcoord, 3> szs = {bmin, splts, bmax};
43
44 auto get_coord = [](u32 i) -> std::array<u32, dim> {
45 constexpr u32 NsideBlockPow = 1;
46 constexpr u32 Nside = 1U << NsideBlockPow;
47 constexpr u32 side_size = Nside;
48 constexpr u32 block_size = shambase::pow_constexpr<dim>(Nside);
49
50 if constexpr (dim == 3) {
51 const u32 tmp = i >> NsideBlockPow;
52 return {i % Nside, (tmp) % Nside, (tmp) >> NsideBlockPow};
53 }
54 };
55
56 for (u32 i = 0; i < splts_count; i++) {
57 auto [lx, ly, lz] = get_coord(i);
58
59 // is this the correct order for the refinement ???
60 ret[i].bmin = Tcoord{szs[lx].x(), szs[ly].y(), szs[lz].z()};
61 ret[i].bmax = Tcoord{szs[lx + 1].x(), szs[ly + 1].y(), szs[lz + 1].z()};
62 }
63
64 return ret;
65 }
66
67 inline auto split() { return AMRBlockCoord::get_split(bmin, bmax); }
68
69 inline static AMRBlockCoord get_merge(AMRBlockCoord c1, AMRBlockCoord c2) {
70 return AMRBlockCoord{sycl::min(c1.bmin, c2.bmin), sycl::max(c1.bmax, c2.bmax)};
71 }
72
73 inline static AMRBlockCoord get_merge(std::array<AMRBlockCoord, splts_count> others) {
74 return AMRBlockCoord::get_merge(
75 AMRBlockCoord::get_merge(
76 AMRBlockCoord::get_merge(others[0], others[1]),
77 AMRBlockCoord::get_merge(others[2], others[3])),
78 AMRBlockCoord::get_merge(
79 AMRBlockCoord::get_merge(others[4], others[5]),
80 AMRBlockCoord::get_merge(others[6], others[7])));
81 }
82
83 inline static bool are_mergeable(std::array<AMRBlockCoord, splts_count> others) {
84
85 AMRBlockCoord merged = AMRBlockCoord::get_merge(others);
86
87 std::array<AMRBlockCoord, splts_count> splitted = merged.split();
88
89 bool are_same = true;
90
91 static_assert(dim == 3, "only dim 3 is handled");
92
93 if constexpr (dim == 3) {
94 for (u32 i = 0; i < splts_count; i++) {
95 are_same = are_same && sham::equals(others[i].bmin, splitted[i].bmin)
96 && sham::equals(others[i].bmax, splitted[i].bmax);
97 }
98 }
99
100 return are_same;
101 }
102 };
103
104} // namespace shamrock::amr
utility to manipulate AMR blocks
std::uint32_t u32
32 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.