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
16
19#include "shammath/AABB.hpp"
24
26
27 template<class Tvec, class TgridVec, class Tmorton>
28 class FindBlockNeigh<Tvec, TgridVec, Tmorton>::AMRBlockFinder {
29 public:
31
32 sycl::accessor<TgridVec, 1, sycl::access::mode::read, sycl::target::device> acc_block_min;
33 sycl::accessor<TgridVec, 1, sycl::access::mode::read, sycl::target::device> acc_block_max;
34
35 TgridVec dir_offset;
36
37 AMRBlockFinder(
38 sycl::handler &cgh,
39 const RTree &tree,
40 sycl::buffer<TgridVec> &buf_block_min,
41 sycl::buffer<TgridVec> &buf_block_max,
42 TgridVec dir_offset)
43 : block_looper(tree, cgh), acc_block_min{buf_block_min, cgh, sycl::read_only},
44 acc_block_max{buf_block_max, cgh, sycl::read_only},
45 dir_offset(std::move(dir_offset)) {}
46
47 template<class IndexFunctor>
48 void for_each_other_index(u32 id_a, IndexFunctor &&fct) const {
49
50 // current block AABB
51 shammath::AABB<TgridVec> block_aabb{acc_block_min[id_a], acc_block_max[id_a]};
52
53 // The wanted AABB (the block we look for)
54 shammath::AABB<TgridVec> check_aabb{
55 block_aabb.lower + dir_offset, block_aabb.upper + dir_offset};
56
57 block_looper.rtree_for(
58 [&](u32 node_id, TgridVec bmin, TgridVec bmax) -> bool {
59 return shammath::AABB<TgridVec>{bmin, bmax}
60 .get_intersect(check_aabb)
62 },
63 [&](u32 id_b) {
64 bool interact
65 = shammath::AABB<TgridVec>{acc_block_min[id_b], acc_block_max[id_b]}
66 .get_intersect(check_aabb)
68 && id_b != id_a;
69
70 if (interact) {
71 fct(id_b);
72 }
73 });
74 }
75 };
76
77 template<class Tvec, class TgridVec, class Tmorton>
80
81 auto edges = get_edges();
82
83 edges.spans_block_min.check_sizes(edges.sizes.indexes);
84 edges.spans_block_max.check_sizes(edges.sizes.indexes);
85
87
88 edges.trees.trees.for_each([&](u64 id, const RTree &tree) {
89 u32 leaf_count = tree.tree_reduced_morton_codes.tree_leaf_count;
90 u32 internal_cell_count = tree.tree_struct.internal_cell_count;
91 u32 tot_count = leaf_count + internal_cell_count;
92
93 OrientedAMRGraph result;
94
95 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
96
97 sycl::buffer<TgridVec> &tree_bmin
98 = shambase::get_check_ref(tree.tree_cell_ranges.buf_pos_min_cell_flt);
99 sycl::buffer<TgridVec> &tree_bmax
100 = shambase::get_check_ref(tree.tree_cell_ranges.buf_pos_max_cell_flt);
101
102 PatchDataField<TgridVec> &block_min = edges.spans_block_min.get_refs().get(id);
103 PatchDataField<TgridVec> &block_max = edges.spans_block_max.get_refs().get(id);
104
105 sycl::buffer<TgridVec> buf_block_min_sycl = block_min.get_buf().copy_to_sycl_buffer();
106 sycl::buffer<TgridVec> buf_block_max_sycl = block_max.get_buf().copy_to_sycl_buffer();
107
108 for (u32 dir = 0; dir < 6; dir++) {
109
110 TgridVec dir_offset = result.offset_check[dir];
111
113 shamsys::instance::get_compute_scheduler_ptr(),
114 edges.sizes.indexes.get(id),
115 tree,
116 buf_block_min_sycl,
117 buf_block_max_sycl,
118 dir_offset);
119
120 shamlog_debug_ln(
121 "AMR Block Graph", "Patch", id, "direction", dir, "link cnt", rslt.link_count);
122
123 std::unique_ptr<AMRGraph> tmp_graph = std::make_unique<AMRGraph>(std::move(rslt));
124
125 result.graph_links[dir] = std::move(tmp_graph);
126 }
127
128 graph.add_obj(id, std::move(result));
129 });
130
131 edges.block_neigh_graph.graph = std::move(graph);
132
133 // possible unittest
134 /*
135 one patch with :
136 sz = 1 << 4
137 base = 4
138 model.make_base_grid((0,0,0),(sz,sz,sz),(base*multx,base*multy,base*multz))
139
140 make a grid of 4^3 blocks, which when merge with interface make 6^3 blocks.
141 In each direction one slab will have no links, hence the number of links should always be
142 6^3 - 6^2 = 180 which we get here on all directions
143 */
144 }
145
146 template<class Tvec, class TgridVec, class Tmorton>
148
149 std::string sizes = get_ro_edge_base(0).get_tex_symbol();
150 std::string block_min = get_ro_edge_base(1).get_tex_symbol();
151 std::string block_max = get_ro_edge_base(2).get_tex_symbol();
152 std::string trees = get_ro_edge_base(3).get_tex_symbol();
153 std::string block_neigh_graph = get_rw_edge_base(0).get_tex_symbol();
154
155 std::string tex = R"tex(
156 Find neighbour blocks
157
158 \begin{align}
159 {block_neigh_graph} = \text{FindBlockNeigh}({sizes}, {block_min}, {block_max}, {trees})
160 \end{align}
161 )tex";
162
163 shambase::replace_all(tex, "{sizes}", sizes);
164 shambase::replace_all(tex, "{block_min}", block_min);
165 shambase::replace_all(tex, "{block_max}", block_max);
166 shambase::replace_all(tex, "{trees}", trees);
167 shambase::replace_all(tex, "{block_neigh_graph}", block_neigh_graph);
168
169 return tex;
170 }
171
172} // namespace shammodels::basegodunov::modules
173
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
A SYCL queue associated with a device and a context.
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
IEdge & get_rw_edge_base(int slot)
Get a reference to a read write edge and cast it to the type IEdge.
Definition INode.hpp:147
const IEdge & get_ro_edge_base(int slot)
Get a reference to a read only edge.
Definition INode.hpp:138
shammodels::basegodunov::modules::NeighGraph compute_neigh_graph_deprecated(const sham::DeviceScheduler_ptr &dev_sched, u32 graph_nodes, Args &&...args)
Create a neighbour graph using a class that will list the ids of the found neighbourgh NeighFindKerne...
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:106
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
namespace for the basegodunov model modules
This file contains the definition for the stacktrace related functionality.
#define __shamrock_stack_entry()
Macro to create a stack entry.
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
T upper
Upper bound of the AABB.
Definition AABB.hpp:105
AABB get_intersect(AABB other) const noexcept
Compute the intersection of two AABB.
Definition AABB.hpp:234