Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
FindBlockNeigh.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
19#include "shammath/AABB.hpp"
23
25
26 template<class Tvec, class TgridVec, class Tmorton>
27 class FindBlockNeigh<Tvec, TgridVec, Tmorton>::AMRBlockFinder {
28 public:
30
31 sycl::accessor<TgridVec, 1, sycl::access::mode::read, sycl::target::device> acc_block_min;
32 sycl::accessor<TgridVec, 1, sycl::access::mode::read, sycl::target::device> acc_block_max;
33
34 TgridVec dir_offset;
35
37 sycl::handler &cgh,
38 const RTree &tree,
39 sycl::buffer<TgridVec> &buf_block_min,
40 sycl::buffer<TgridVec> &buf_block_max,
41 TgridVec dir_offset)
42 : block_looper(tree, cgh), acc_block_min{buf_block_min, cgh, sycl::read_only},
43 acc_block_max{buf_block_max, cgh, sycl::read_only},
44 dir_offset(std::move(dir_offset)) {}
45
46 template<class IndexFunctor>
47 void for_each_other_index(u32 id_a, IndexFunctor &&fct) const {
48
49 // current block AABB
50 shammath::AABB<TgridVec> block_aabb{acc_block_min[id_a], acc_block_max[id_a]};
51
52 // The wanted AABB (the block we look for)
53 shammath::AABB<TgridVec> check_aabb{
54 block_aabb.lower + dir_offset, block_aabb.upper + dir_offset};
55
56 block_looper.rtree_for(
57 [&](u32 node_id, TgridVec bmin, TgridVec bmax) -> bool {
58 return shammath::AABB<TgridVec>{bmin, bmax}
59 .get_intersect(check_aabb)
60 .is_volume_not_null();
61 },
62 [&](u32 id_b) {
63 bool interact
64 = shammath::AABB<TgridVec>{acc_block_min[id_b], acc_block_max[id_b]}
65 .get_intersect(check_aabb)
67 && id_b != id_a;
68
69 if (interact) {
70 fct(id_b);
71 }
72 });
73 }
74 };
75
76 template<class Tvec, class TgridVec, class Tmorton>
78 auto edges = get_edges();
79
80 edges.spans_block_min.check_sizes(edges.sizes.indexes);
81 edges.spans_block_max.check_sizes(edges.sizes.indexes);
82
84
85 edges.trees.trees.for_each([&](u64 id, const RTree &tree) {
86 u32 leaf_count = tree.tree_reduced_morton_codes.tree_leaf_count;
87 u32 internal_cell_count = tree.tree_struct.internal_cell_count;
88 u32 tot_count = leaf_count + internal_cell_count;
89
90 OrientedAMRGraph result;
91
92 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
93
94 sycl::buffer<TgridVec> &tree_bmin
95 = shambase::get_check_ref(tree.tree_cell_ranges.buf_pos_min_cell_flt);
96 sycl::buffer<TgridVec> &tree_bmax
97 = shambase::get_check_ref(tree.tree_cell_ranges.buf_pos_max_cell_flt);
98
99 PatchDataField<TgridVec> &block_min = edges.spans_block_min.get_refs().get(id);
100 PatchDataField<TgridVec> &block_max = edges.spans_block_max.get_refs().get(id);
101
102 sycl::buffer<TgridVec> buf_block_min_sycl = block_min.get_buf().copy_to_sycl_buffer();
103 sycl::buffer<TgridVec> buf_block_max_sycl = block_max.get_buf().copy_to_sycl_buffer();
104
105 for (u32 dir = 0; dir < 6; dir++) {
106
107 TgridVec dir_offset = result.offset_check[dir];
108
109 AMRGraph rslt = details::compute_neigh_graph_deprecated<AMRBlockFinder>(
110 shamsys::instance::get_compute_scheduler_ptr(),
111 edges.sizes.indexes.get(id),
112 tree,
113 buf_block_min_sycl,
114 buf_block_max_sycl,
115 dir_offset);
116
117 shamlog_debug_ln(
118 "AMR Block Graph", "Patch", id, "direction", dir, "link cnt", rslt.link_count);
119
120 std::unique_ptr<AMRGraph> tmp_graph = std::make_unique<AMRGraph>(std::move(rslt));
121
122 result.graph_links[dir] = std::move(tmp_graph);
123 }
124
125 graph.add_obj(id, std::move(result));
126 });
127
128 edges.block_neigh_graph.graph = std::move(graph);
129
130 // possible unittest
131 /*
132 one patch with :
133 sz = 1 << 4
134 base = 4
135 model.make_base_grid((0,0,0),(sz,sz,sz),(base*multx,base*multy,base*multz))
136
137 make a grid of 4^3 blocks, which when merge with interface make 6^3 blocks.
138 In each direction one slab will have no links, hence the number of links should always be
139 6^3 - 6^2 = 180 which we get here on all directions
140 */
141 }
142
143 template<class Tvec, class TgridVec, class Tmorton>
145
146 std::string sizes = get_ro_edge_base(0).get_tex_symbol();
147 std::string block_min = get_ro_edge_base(1).get_tex_symbol();
148 std::string block_max = get_ro_edge_base(2).get_tex_symbol();
149 std::string trees = get_ro_edge_base(3).get_tex_symbol();
150 std::string block_neigh_graph = get_rw_edge_base(0).get_tex_symbol();
151
152 std::string tex = R"tex(
153 Find neighbour blocks
154
155 \begin{align}
156 {block_neigh_graph} = \text{FindBlockNeigh}({sizes}, {block_min}, {block_max}, {trees})
157 \end{align}
158 )tex";
159
160 shambase::replace_all(tex, "{sizes}", sizes);
161 shambase::replace_all(tex, "{block_min}", block_min);
162 shambase::replace_all(tex, "{block_max}", block_max);
163 shambase::replace_all(tex, "{trees}", trees);
164 shambase::replace_all(tex, "{block_neigh_graph}", block_neigh_graph);
165
166 return tex;
167 }
168
169} // namespace shammodels::basegodunov::modules
170
Field variant object to instanciate a variant on the patch types.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
The radix tree.
Definition RadixTree.hpp:50
A SYCL queue associated with a device and a context.
DeviceQueue & get_queue(u32 id=0)
Get a reference to a DeviceQueue.
Represents a collection of objects distributed across patches identified by a u64 id.
iterator add_obj(u64 id, T &&obj)
Adds a new object to the collection.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
virtual std::string _impl_get_tex() const
get the tex of the node
void replace_all(std::string &inout, std::string_view what, std::string_view with)
replace all occurence of a search string with another
Definition string.hpp:183
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:110
namespace for the basegodunov model modules
Axis-Aligned bounding box.
Definition AABB.hpp:99
bool is_volume_not_null() const noexcept
Checks if the AABB has a non-zero volume.
Definition AABB.hpp:280
T lower
Lower bound of the AABB.
Definition AABB.hpp:104
AABB get_intersect(AABB other) const noexcept
Compute the intersection of two AABB.
Definition AABB.hpp:234