Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
paving_function.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
20#include "shambackends/math.hpp"
21#include "shambackends/sycl.hpp"
22#include "shamcomm/logs.hpp"
23#include "shammath/AABB.hpp"
24
25namespace shammath {
26
27 template<typename Tvec, class paving_func>
28 inline AABB<Tvec> f_aabb(
29 const paving_func &paving, const AABB<Tvec> &aabb, int i, int j, int k) {
30
31 static_assert(!paving_func::can_deform_aabb, "The paving function cannot deform the AABB");
32
33 Tvec min = paving.f(aabb.lower, i, j, k);
34 Tvec max = paving.f(aabb.upper, i, j, k);
35
36 // if the min is greater than the max, swap them
37 for (size_t d = 0; d < shambase::VectorProperties<Tvec>::dimension; ++d) {
38 if (min[d] > max[d]) {
39 std::swap(min[d], max[d]);
40 }
41 }
42
43 return AABB<Tvec>{min, max};
44 }
45
46 template<typename Tvec, class paving_func>
47 inline AABB<Tvec> f_aabb_inv(
48 const paving_func &paving, const AABB<Tvec> &aabb, int i, int j, int k) {
49
50 static_assert(!paving_func::can_deform_aabb, "The paving function cannot deform the AABB");
51
52 Tvec min = paving.f_inv(aabb.lower, i, j, k);
53 Tvec max = paving.f_inv(aabb.upper, i, j, k);
54
55 // if the min is greater than the max, swap them
56 for (size_t d = 0; d < shambase::VectorProperties<Tvec>::dimension; ++d) {
57 if (min[d] > max[d]) {
58 std::swap(min[d], max[d]);
59 }
60 }
61
62 return AABB<Tvec>{min, max};
63 }
64
70 template<typename Tvec>
72
73 // only translation so f(AABB) is still an AABB
74 inline static constexpr bool can_deform_aabb = false;
75
76 Tvec box_size;
77
87 Tvec f(Tvec x, int i, int j, int k) const { return x + box_size * Tvec{i, j, k}; }
88
98 Tvec f_inv(Tvec x, int i, int j, int k) const { return x - box_size * Tvec{i, j, k}; }
99
100 inline AABB<Tvec> f_aabb(const AABB<Tvec> &aabb, int i, int j, int k) const {
101 return shammath::f_aabb(*this, aabb, i, j, k);
102 }
103
104 inline AABB<Tvec> f_aabb_inv(const AABB<Tvec> &aabb, int i, int j, int k) const {
105 return shammath::f_aabb_inv(*this, aabb, i, j, k);
106 }
107
108 inline std::vector<std::array<int, 3>> get_paving_index_intersecting(
109 AABB<Tvec> aabb) const {
110
111 Tvec lower = aabb.lower / box_size;
112 Tvec upper = aabb.upper / box_size;
113
114 lower[0] = std::floor(lower[0]);
115 lower[1] = std::floor(lower[1]);
116 lower[2] = std::floor(lower[2]);
117
118 i32_3 low_int = {
119 static_cast<i32>(lower[0]), static_cast<i32>(lower[1]), static_cast<i32>(lower[2])};
120
121 std::vector<std::array<int, 3>> indices;
122
123 for (int i = low_int[0]; i <= upper[0]; ++i) {
124 for (int j = low_int[1]; j <= upper[1]; ++j) {
125 for (int k = low_int[2]; k <= upper[2]; ++k) {
126 indices.push_back({i, j, k});
127 }
128 }
129 }
130
131 return indices;
132 }
133 };
134
141 template<typename Tvec>
143
144 // only translation so f(AABB) is still an AABB
145 inline static constexpr bool can_deform_aabb = false;
146
147 using Tscal = shambase::VecComponent<Tvec>;
148
153
158
166 bool is_y_periodic;
167 bool is_z_periodic;
168
178 Tvec f(Tvec x, int i, int j, int k) const {
179 Tvec off{
180 (is_x_periodic) ? 0 : (x[0] - box_center[0]) * (sham::m1pown<Tscal>(i) - 1),
181 (is_y_periodic) ? 0 : (x[1] - box_center[1]) * (sham::m1pown<Tscal>(j) - 1),
182 (is_z_periodic) ? 0 : (x[2] - box_center[2]) * (sham::m1pown<Tscal>(k) - 1)};
183 return x + box_size * Tvec{i, j, k} + off;
184 }
185
195 Tvec f_inv(Tvec x, int i, int j, int k) const {
196 Tvec tmp = x - box_size * Tvec{i, j, k};
197 Tvec off{
198 (is_x_periodic) ? 0 : (tmp[0] - box_center[0]) * (sham::m1pown<Tscal>(i) - 1),
199 (is_y_periodic) ? 0 : (tmp[1] - box_center[1]) * (sham::m1pown<Tscal>(j) - 1),
200 (is_z_periodic) ? 0 : (tmp[2] - box_center[2]) * (sham::m1pown<Tscal>(k) - 1)};
201 return tmp + off;
202 }
203
204 inline AABB<Tvec> f_aabb(const AABB<Tvec> &aabb, int i, int j, int k) const {
205 return shammath::f_aabb(*this, aabb, i, j, k);
206 }
207
208 inline AABB<Tvec> f_aabb_inv(const AABB<Tvec> &aabb, int i, int j, int k) const {
209 return shammath::f_aabb_inv(*this, aabb, i, j, k);
210 }
211
212 inline std::vector<std::array<int, 3>> get_paving_index_intersecting(
213 AABB<Tvec> aabb) const {
214
215 Tvec lower = aabb.lower / box_size;
216 Tvec upper = aabb.upper / box_size;
217
218 lower[0] = std::floor(lower[0]);
219 lower[1] = std::floor(lower[1]);
220 lower[2] = std::floor(lower[2]);
221
222 i32_3 low_int = {
223 static_cast<i32>(lower[0]), static_cast<i32>(lower[1]), static_cast<i32>(lower[2])};
224
225 std::vector<std::array<int, 3>> indices;
226
227 for (int i = low_int[0]; i <= upper[0]; ++i) {
228 for (int j = low_int[1]; j <= upper[1]; ++j) {
229 for (int k = low_int[2]; k <= upper[2]; ++k) {
230 indices.push_back({i, j, k});
231 }
232 }
233 }
234
235 return indices;
236 }
237 };
238
248 template<typename Tvec>
250
251 // only translation so f(AABB) is still an AABB
252 inline static constexpr bool can_deform_aabb = false;
253
254 using Tscal = shambase::VecComponent<Tvec>;
255
256 Tvec box_size;
258
265
266 Tscal shear_x;
267
277 Tvec f(Tvec x, int i, int j, int k) const {
278 Tvec off{
279 (is_x_periodic) ? 0 : (x[0] - box_center[0]) * (sham::m1pown<Tscal>(i) - 1),
280 (is_y_periodic) ? 0 : (x[1] - box_center[1]) * (sham::m1pown<Tscal>(j) - 1),
281 (is_z_periodic) ? 0 : (x[2] - box_center[2]) * (sham::m1pown<Tscal>(k) - 1)};
282 return x + box_size * Tvec{i, j, k} + off + shear_x * Tvec{j, 0, 0};
283 }
284
294 Tvec f_inv(Tvec x, int i, int j, int k) const {
295 Tvec tmp = x - box_size * Tvec{i, j, k} - shear_x * Tvec{j, 0, 0};
296 Tvec off{
297 (is_x_periodic) ? 0 : (tmp[0] - box_center[0]) * (sham::m1pown<Tscal>(i) - 1),
298 (is_y_periodic) ? 0 : (tmp[1] - box_center[1]) * (sham::m1pown<Tscal>(j) - 1),
299 (is_z_periodic) ? 0 : (tmp[2] - box_center[2]) * (sham::m1pown<Tscal>(k) - 1)};
300 return tmp + off;
301 }
302
303 inline AABB<Tvec> f_aabb(const AABB<Tvec> &aabb, int i, int j, int k) const {
304 return shammath::f_aabb(*this, aabb, i, j, k);
305 }
306
307 inline AABB<Tvec> f_aabb_inv(const AABB<Tvec> &aabb, int i, int j, int k) const {
308 return shammath::f_aabb_inv(*this, aabb, i, j, k);
309 }
310
311 inline std::vector<std::array<int, 3>> get_paving_index_intersecting(
312 AABB<Tvec> aabb) const {
313
314 Tvec lower = aabb.lower / box_size;
315 Tvec upper = aabb.upper / box_size;
316
317 lower[0] = std::floor(lower[0]);
318 lower[1] = std::floor(lower[1]);
319 lower[2] = std::floor(lower[2]);
320
321 lower[0] -= 1;
322 upper[0] += 1;
323
324 i32_3 low_int = {
325 static_cast<i32>(lower[0]), static_cast<i32>(lower[1]), static_cast<i32>(lower[2])};
326
327 std::vector<std::array<int, 3>> indices;
328
329 for (int i = low_int[0]; i <= upper[0]; ++i) {
330 for (int j = low_int[1]; j <= upper[1]; ++j) {
331 for (int k = low_int[2]; k <= upper[2]; ++k) {
332
333 Tscal shear_x_floor = std::floor(j * shear_x / box_size[0]);
334 i32 s_x = static_cast<i32>(shear_x_floor);
335
336 i32 i_s = i - s_x;
337
338 auto aabb_mapped = f_aabb(
339 AABB<Tvec>{box_center - box_size / 2, box_center + box_size / 2},
340 i_s,
341 j,
342 k);
343
344 if (aabb_mapped.get_intersect(aabb).is_volume_not_null()) {
345 indices.push_back({i_s, j, k});
346 }
347 }
348 }
349 }
350
351 return indices;
352 }
353 };
354
355} // namespace shammath
std::int32_t i32
32 bit integer
Define the fmt formatters for sycl::vec.
namespace for math utility
Definition AABB.hpp:26
Axis-Aligned bounding box.
Definition AABB.hpp:99
A structure for 3D paving functions with shearing along the x-axis and general boundary conditions.
Tscal shear_x
Shearing factor applied along the x-axis.
Tvec box_center
The center of the box in each dimension.
Tvec f_inv(Tvec x, int i, int j, int k) const
Applies the inverse of the paving function with shearing and boundary conditions.
Tvec f(Tvec x, int i, int j, int k) const
Applies the paving function with shearing and boundary conditions.
Tvec box_size
The size of the box in each dimension.
A structure for 3D paving functions with general boundary conditions (periodic or reflective per dire...
bool is_x_periodic
The boundary condition in each dimension.
Tvec box_size
The size of the box in each dimension.
Tvec f_inv(Tvec x, int i, int j, int k) const
Applies the inverse of the paving function.
Tvec f(Tvec x, int i, int j, int k) const
Applies the paving function with periodic or reflective boundary conditions.
Tvec box_center
The center of the box in each dimension.
A structure for 3D paving functions with periodic boundary conditions.
Tvec f_inv(Tvec x, int i, int j, int k) const
Applies the inverse of the paving function.
Tvec box_size
The size of the box in each dimension.
Tvec f(Tvec x, int i, int j, int k) const
Applies the paving function with periodic boundary conditions.