Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
FindGhostInterfaces.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
17/*
18
19Test code for godbolt
20
21
22#include <iostream>
23#include <vector>
24
25namespace sycl{
26 template<class T>
27 struct vec{
28 T _x,_y,_z;
29
30 inline T & x(){
31 return _x;
32 }
33
34 inline T & y(){
35 return _y;
36 }
37 inline T & z(){
38 return _z;
39 }
40 };
41}
42
43
44using i32 = int;
45using i32_3 = sycl::vec<i32>;
46
47template<class T>
48struct ShiftInfo{
49 sycl::vec<T> shift;
50 sycl::vec<T> shift_speed;
51};
52
53template<class T>
54struct ShearPeriodicInfo{
55 i32_3 shear_base;
56 i32_3 shear_dir;
57 T shear_value;
58 T shear_speed;
59};
60
61template<class T>
62inline ShiftInfo<T> compute_shift_infos(
63 i32_3 ioff, ShearPeriodicInfo<T> shear, sycl::vec<T> bsize
64 ){
65
66 i32 dx = ioff.x()*shear.shear_base.x();
67 i32 dy = ioff.y()*shear.shear_base.y();
68 i32 dz = ioff.z()*shear.shear_base.z();
69
70 i32 d = dx + dy + dz;
71
72 sycl::vec<T> shift = {
73 (d*shear.shear_dir.x())*shear.shear_value + bsize.x()*ioff.x(),
74 (d*shear.shear_dir.y())*shear.shear_value + bsize.y()*ioff.y() ,
75 (d*shear.shear_dir.z())*shear.shear_value + bsize.z()*ioff.z()
76 };
77 sycl::vec<T> shift_speed = {
78 (d*shear.shear_dir.x())*shear.shear_speed,
79 (d*shear.shear_dir.y())*shear.shear_speed,
80 (d*shear.shear_dir.z())*shear.shear_speed
81 };
82
83 return {shift,shift_speed};
84}
85
86template<class T>
87inline void for_each_patch_shift(ShearPeriodicInfo<T> shearinfo, sycl::vec<T> bsize){
88
89 i32_3 loop_offset = {0,0,0};
90
91 std::vector<i32_3> list_possible;
92
93
94 i32 repetition_x = 1;
95 i32 repetition_y = 1;
96 i32 repetition_z = 1;
97
98
99
100 for (i32 xoff = -repetition_x; xoff <= repetition_x; xoff++) {
101 for (i32 yoff = -repetition_y; yoff <= repetition_y; yoff++) {
102 for (i32 zoff = -repetition_z; zoff <= repetition_z; zoff++) {
103
104
105 i32 dx = xoff*shearinfo.shear_base.x();
106 i32 dy = yoff*shearinfo.shear_base.y();
107 i32 dz = zoff*shearinfo.shear_base.z();
108
109 i32 d = dx + dy + dz;
110
111 i32 df = -int(d * shearinfo.shear_value);
112
113 i32_3 off_d = {
114 shearinfo.shear_dir.x()*df,
115 shearinfo.shear_dir.y()*df,
116 shearinfo.shear_dir.z()*df
117 };
118
119 list_possible.push_back({xoff+off_d.x(),yoff+off_d.y(),zoff+off_d.z()});
120 }
121 }
122 }
123
124 for(i32_3 off : list_possible){
125
126 auto shift = compute_shift_infos(off,shearinfo,bsize);
127
128 std::cout <<
129 off.x() << " " << off.y() << " " << off.z() << " | " <<
130 shift.shift.x() << " " << shift.shift.y() << " " << shift.shift.z() << " "<<std::endl;
131 }
132
133
134
135}
136
137
138int main(){
139
140 ShearPeriodicInfo<float> shear{
141 {1,0,0},
142 {0,0,1},
143 13.5,
144 1
145 };
146
147 for_each_patch_shift(shear, {1,1,1});
148
149}
150
151
152*/
153
154#include "shambase/memory.hpp"
156#include "shambackends/math.hpp"
159#include <functional>
160#include <utility>
161#include <vector>
162
163namespace {
164
165 template<class T>
166 struct ShiftInfo {
167 sycl::vec<T, 3> shift;
168 sycl::vec<T, 3> shift_speed;
169 };
170
171 template<class T>
172 struct ShearPeriodicInfo {
173 i32_3 shear_base;
174 i32_3 shear_dir;
175 T shear_value;
176 T shear_speed;
177 };
178
179 template<class T>
180 inline ShiftInfo<T> compute_shift_infos(
181 i32_3 ioff, ShearPeriodicInfo<T> shear, sycl::vec<T, 3> bsize) {
182
183 i32 dx = ioff.x() * shear.shear_base.x();
184 i32 dy = ioff.y() * shear.shear_base.y();
185 i32 dz = ioff.z() * shear.shear_base.z();
186
187 i32 d = dx + dy + dz;
188
189 sycl::vec<T, 3> shift
190 = {(d * shear.shear_dir.x()) * shear.shear_value + bsize.x() * ioff.x(),
191 (d * shear.shear_dir.y()) * shear.shear_value + bsize.y() * ioff.y(),
192 (d * shear.shear_dir.z()) * shear.shear_value + bsize.z() * ioff.z()};
193 sycl::vec<T, 3> shift_speed
194 = {(d * shear.shear_dir.x()) * shear.shear_speed,
195 (d * shear.shear_dir.y()) * shear.shear_speed,
196 (d * shear.shear_dir.z()) * shear.shear_speed};
197
198 return {shift, shift_speed};
199 }
200
201 template<class T>
202 inline void for_each_patch_shift(
203 ShearPeriodicInfo<T> shearinfo,
204 sycl::vec<T, 3> bsize,
205 const std::function<void(i32_3, ShiftInfo<T>)> &funct) {
206
207 std::vector<i32_3> list_possible;
208
209 // a bit of dirty fix doesn't hurt
210 // this should be done in a better way a some point
211 i32 repetition_x = 1 + sham::abs(shearinfo.shear_dir.x());
212 i32 repetition_y = 1 + sham::abs(shearinfo.shear_dir.y());
213 i32 repetition_z = 1 + sham::abs(shearinfo.shear_dir.z());
214
215 T sz = bsize.x() * shearinfo.shear_dir.x() + bsize.y() * shearinfo.shear_dir.y()
216 + bsize.z() * shearinfo.shear_dir.z();
217
218 for (i32 xoff = -repetition_x; xoff <= repetition_x; xoff++) {
219 for (i32 yoff = -repetition_y; yoff <= repetition_y; yoff++) {
220 for (i32 zoff = -repetition_z; zoff <= repetition_z; zoff++) {
221
222 i32 dx = xoff * shearinfo.shear_base.x();
223 i32 dy = yoff * shearinfo.shear_base.y();
224 i32 dz = zoff * shearinfo.shear_base.z();
225
226 i32 d = dx + dy + dz;
227
228 i32 df = -int(d * shearinfo.shear_value / sz);
229
230 i32_3 off_d
231 = {shearinfo.shear_dir.x() * df,
232 shearinfo.shear_dir.y() * df,
233 shearinfo.shear_dir.z() * df};
234
235 // on redhat based systems stl vector freaks out
236 // because iterator to back does *(end() - 1)
237 // the issue is that the compiler gets confused
238 // by the sycl::vec defining the - operator
239 // creating the ambiguity and ...
240 // ultimatly the compiler shitting itself
241 list_possible.resize(list_possible.size() + 1);
242 list_possible[list_possible.size() - 1]
243 = i32_3{xoff + off_d.x(), yoff + off_d.y(), zoff + off_d.z()};
244 }
245 }
246 }
247
248 for (i32_3 off : list_possible) {
249 funct(off, compute_shift_infos(off, shearinfo, bsize));
250 }
251 }
252
253} // namespace
254
255template<class Tvec>
258
259 using namespace shammath;
260
261 auto edges = get_edges();
262
263 SerialPatchTree<Tvec> &sptree = edges.patch_tree.get_patch_tree();
264 const shambase::DistributedData<Tscal> &int_range_max = edges.interact_radius.values;
265
266 std::vector<std::pair<u64, CoordRange<Tvec>>> senders;
267 edges.local_patch_boxes.values.for_each([&](u64 id, const CoordRange<Tvec> &box) {
268 senders.push_back({id, box});
269 });
270
271 shambase::DistributedDataShared<InterfaceBuildInfos> &interf_map = edges.interface_infos.values;
272 interf_map = {};
273
274 sycl::host_accessor acc_tf{edges.interact_radius_tree.get_buf(), sycl::read_only};
275 // sender translation
276 Tvec periodic_offset = Tvec{0, 0, 0};
277
278 sycl::host_accessor tree{shambase::get_check_ref(sptree.serial_tree_buf), sycl::read_only};
279 sycl::host_accessor lpid{shambase::get_check_ref(sptree.linked_patch_ids_buf), sycl::read_only};
280
281#pragma omp parallel for
282 for (u32 i = 0; i < senders.size(); i++) {
283 u64 sender_id = senders[i].first;
284 CoordRange<Tvec> sender_bsize = senders[i].second;
285
286 CoordRange<Tvec> sender_bsize_off = sender_bsize.add_offset(periodic_offset);
287
288 Tscal sender_volume = sender_bsize.get_volume();
289
290 using PtNode = typename SerialPatchTree<Tvec>::PtNode;
291
292 sptree.host_for_each_leafs_internal(
293 [&](u64 tree_id, PtNode n) {
294 Tscal receiv_h_max = acc_tf[tree_id];
295 CoordRange<Tvec> receiv_exp{n.box_min - receiv_h_max, n.box_max + receiv_h_max};
296
297 return receiv_exp.get_intersect(sender_bsize_off).is_not_empty();
298 },
299 [&](u64 id_found, PtNode n) {
300 if (id_found == sender_id) {
301 return;
302 }
303
304 CoordRange<Tvec> receiv_exp = CoordRange<Tvec>{n.box_min, n.box_max}.expand_all(
305 int_range_max.get(id_found));
306
307 CoordRange<Tvec> interf_volume
308 = sender_bsize.get_intersect(receiv_exp.add_offset(-periodic_offset));
309
310#pragma omp critical
311 interf_map.add_obj(
312 sender_id,
313 id_found,
314 {periodic_offset,
315 {0, 0, 0},
316 {0, 0, 0},
317 interf_volume,
318 interf_volume.get_volume() / sender_volume});
319 },
320 tree,
321 lpid);
322 }
323}
324
325template<class Tvec>
328
329 using namespace shammath;
330
331 auto edges = get_edges();
332
333 const shammath::AABB<Tvec> &sim_box = edges.sim_box.value;
334 Tvec bsize = sim_box.upper - sim_box.lower;
335
336 SerialPatchTree<Tvec> &sptree = edges.patch_tree.get_patch_tree();
337 const shambase::DistributedData<Tscal> &int_range_max = edges.interact_radius.values;
338
339 std::vector<std::pair<u64, CoordRange<Tvec>>> senders;
340 edges.local_patch_boxes.values.for_each([&](u64 id, const CoordRange<Tvec> &box) {
341 senders.push_back({id, box});
342 });
343
344 shambase::DistributedDataShared<InterfaceBuildInfos> &interf_map = edges.interface_infos.values;
345 interf_map = {};
346
347 i32 repetition_x = 1;
348 i32 repetition_y = 1;
349 i32 repetition_z = 1;
350
351 sycl::host_accessor acc_tf{edges.interact_radius_tree.get_buf(), sycl::read_only};
352
353 for (i32 xoff = -repetition_x; xoff <= repetition_x; xoff++) {
354 for (i32 yoff = -repetition_y; yoff <= repetition_y; yoff++) {
355 for (i32 zoff = -repetition_z; zoff <= repetition_z; zoff++) {
356
357 // sender translation
358 Tvec periodic_offset = Tvec{xoff * bsize.x(), yoff * bsize.y(), zoff * bsize.z()};
359
360 sycl::host_accessor tree{
361 shambase::get_check_ref(sptree.serial_tree_buf), sycl::read_only};
362 sycl::host_accessor lpid{
363 shambase::get_check_ref(sptree.linked_patch_ids_buf), sycl::read_only};
364
365#pragma omp parallel for
366 for (u32 i = 0; i < senders.size(); i++) {
367 u64 sender_id = senders[i].first;
368 CoordRange<Tvec> sender_bsize = senders[i].second;
369
370 CoordRange<Tvec> sender_bsize_off = sender_bsize.add_offset(periodic_offset);
371
372 Tscal sender_volume = sender_bsize.get_volume();
373
374 using PtNode = typename SerialPatchTree<Tvec>::PtNode;
375
376 sptree.host_for_each_leafs_internal(
377 [&](u64 tree_id, PtNode n) {
378 Tscal receiv_h_max = acc_tf[tree_id];
379 CoordRange<Tvec> receiv_exp{
380 n.box_min - receiv_h_max, n.box_max + receiv_h_max};
381
382 return receiv_exp.get_intersect(sender_bsize_off).is_not_empty();
383 },
384 [&](u64 id_found, PtNode n) {
385 if ((id_found == sender_id) && (xoff == 0) && (yoff == 0)
386 && (zoff == 0)) {
387 return;
388 }
389
390 CoordRange<Tvec> receiv_exp
391 = CoordRange<Tvec>{n.box_min, n.box_max}.expand_all(
392 int_range_max.get(id_found));
393
394 CoordRange<Tvec> interf_volume = sender_bsize.get_intersect(
395 receiv_exp.add_offset(-periodic_offset));
396
397#pragma omp critical
398 interf_map.add_obj(
399 sender_id,
400 id_found,
401 {periodic_offset,
402 {0, 0, 0},
403 {xoff, yoff, zoff},
404 interf_volume,
405 interf_volume.get_volume() / sender_volume});
406 },
407 tree,
408 lpid);
409 }
410 }
411 }
412 }
413}
414
415template<class Tvec>
419
420 using namespace shammath;
421
422 auto edges = get_edges();
423
424 const shammath::AABB<Tvec> &sim_box = edges.sim_box.value;
425 Tvec bsize = sim_box.upper - sim_box.lower;
426
427 ShearPeriodicInfo<Tscal> shear_info{shear_base, shear_dir, edges.shear_value.data, shear_speed};
428
429 SerialPatchTree<Tvec> &sptree = edges.patch_tree.get_patch_tree();
430 const shambase::DistributedData<Tscal> &int_range_max = edges.interact_radius.values;
431
432 std::vector<std::pair<u64, CoordRange<Tvec>>> senders;
433 edges.local_patch_boxes.values.for_each([&](u64 id, const CoordRange<Tvec> &box) {
434 senders.push_back({id, box});
435 });
436
437 shambase::DistributedDataShared<InterfaceBuildInfos> &interf_map = edges.interface_infos.values;
438 interf_map = {};
439
440 sycl::host_accessor acc_tf{edges.interact_radius_tree.get_buf(), sycl::read_only};
441
442 for_each_patch_shift<Tscal>(shear_info, bsize, [&](i32_3 ioff, ShiftInfo<Tscal> shift) {
443 i32 xoff = ioff.x();
444 i32 yoff = ioff.y();
445 i32 zoff = ioff.z();
446
447 Tvec offset = shift.shift;
448
449 sycl::host_accessor tree{shambase::get_check_ref(sptree.serial_tree_buf), sycl::read_only};
450 sycl::host_accessor lpid{
451 shambase::get_check_ref(sptree.linked_patch_ids_buf), sycl::read_only};
452
453#pragma omp parallel for
454 for (u32 i = 0; i < senders.size(); i++) {
455 u64 sender_id = senders[i].first;
456 CoordRange<Tvec> sender_bsize = senders[i].second;
457
458 CoordRange<Tvec> sender_bsize_off = sender_bsize.add_offset(offset);
459
460 Tscal sender_volume = sender_bsize.get_volume();
461
462 using PtNode = typename SerialPatchTree<Tvec>::PtNode;
463
464 sptree.host_for_each_leafs_internal(
465 [&](u64 tree_id, PtNode n) {
466 Tscal receiv_h_max = acc_tf[tree_id];
467 CoordRange<Tvec> receiv_exp{n.box_min - receiv_h_max, n.box_max + receiv_h_max};
468
469 return receiv_exp.get_intersect(sender_bsize_off).is_not_empty();
470 },
471 [&](u64 id_found, PtNode n) {
472 if ((id_found == sender_id) && (xoff == 0) && (yoff == 0) && (zoff == 0)) {
473 return;
474 }
475
476 CoordRange<Tvec> receiv_exp = CoordRange<Tvec>{n.box_min, n.box_max}.expand_all(
477 int_range_max.get(id_found));
478
479 CoordRange<Tvec> interf_volume
480 = sender_bsize.get_intersect(receiv_exp.add_offset(-offset));
481
482#pragma omp critical
483 interf_map.add_obj(
484 sender_id,
485 id_found,
486 {offset,
487 shift.shift_speed,
488 {xoff, yoff, zoff},
489 interf_volume,
490 interf_volume.get_volume() / sender_volume});
491 },
492 tree,
493 lpid);
494 }
495 });
496}
497
498template<class Tvec>
502
503template<class Tvec>
507
508template<class Tvec>
513
Solvergraph nodes finding the SPH ghost interfaces, one node per boundary condition.
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
std::int32_t i32
32 bit integer
Container for objects shared between two distributed data elements.
iterator add_obj(u64 left_id, u64 right_id, T &&obj)
Add an object associated with a patch pair.
Represents a collection of objects distributed across patches identified by a u64 id.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
T & get(u64 id)
Returns a reference to an object in the collection.
Ghost interface finder for free boundaries: only direct neighbouring patches.
virtual std::string _impl_get_tex() const
get the tex of the node
Ghost interface finder for periodic boundaries: neighbouring patches, including through the 26 period...
virtual std::string _impl_get_tex() const
get the tex of the node
Ghost interface finder for shearing periodic boundaries.
virtual std::string _impl_get_tex() const
get the tex of the node
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 math utility
Definition AABB.hpp:26
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
T lower
Lower bound of the AABB.
Definition AABB.hpp:104
T upper
Upper bound of the AABB.
Definition AABB.hpp:105