Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SimBox.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
25#include <type_traits>
26#include <stdexcept>
27#include <tuple>
28
29namespace shamrock::patch {
30
36
37 static constexpr u32 dim = 3;
38
40
42
43 var_t bounding_box;
44 PatchCoord<> patch_coord_bounding_box;
45
46 public:
47 inline SimulationBoxInfo(
48 PatchDataLayerLayout &pdl, PatchCoord<dim> patch_coord_bounding_box)
49 : pdl(pdl), patch_coord_bounding_box(std::move(patch_coord_bounding_box)),
50 bounding_box(shammath::CoordRange<f32>{}) {
51
53 }
54
55 void set_patch_coord_bounding_box(PatchCoord<dim> new_patch_coord_box) {
56 patch_coord_bounding_box = new_patch_coord_box;
57 shamlog_debug_ln(
58 "SimBox",
59 "changed patch coord bounds :",
60 std::pair{
61 u64_3{
62 new_patch_coord_box.coord_min[0],
63 new_patch_coord_box.coord_min[1],
64 new_patch_coord_box.coord_min[2]},
65 u64_3{
66 new_patch_coord_box.coord_max[0],
67 new_patch_coord_box.coord_max[1],
68 new_patch_coord_box.coord_max[2]}});
69 }
70
77 template<class T>
78 [[nodiscard]] std::tuple<T, T> get_bounding_box() const;
79
86 template<class T>
87 inline T get_bounding_box_size() const {
88 auto [bmin, bmax] = get_bounding_box<T>();
89 return bmax - bmin;
90 }
91
98 template<class T>
100
112 template<class T>
114 std::pair<T, T> reduced = shamalgs::collective::allreduce_bounds(
115 std::pair<T, T>{new_box.lower, new_box.upper});
116 set_bounding_box<>(shammath::CoordRange<T>(reduced));
117 }
118
134 template<class T>
136
144 template<class T>
145 std::tuple<T, T> patch_coord_to_domain(const Patch &p) const;
146
147 // TODO implement box size reduction here
148
173 void reset_box_size();
174
177 template<class primtype>
178 void clean_box(primtype tol);
179
180 template<>
181 inline void clean_box<f32>(f32 tol) {
182
183 auto [bmin, bmax] = get_bounding_box<f32_3>();
184
185 f32_3 center = (bmin + bmax) / 2;
186 f32_3 cur_delt = bmax - bmin;
187 cur_delt /= 2;
188
189 cur_delt *= tol;
190
191 bmin = center - cur_delt;
192 bmax = center + cur_delt;
193
194 set_bounding_box<f32_3>({bmin, bmax});
195 }
196
197 template<>
198 inline void clean_box<f64>(f64 tol) {
199 auto [bmin, bmax] = get_bounding_box<f64_3>();
200
201 f64_3 center = (bmin + bmax) / 2;
202 f64_3 cur_delt = bmax - bmin;
203 cur_delt /= 2;
204
205 cur_delt *= tol;
206
207 bmin = center - cur_delt;
208 bmax = center + cur_delt;
209
210 set_bounding_box<f64_3>({bmin, bmax});
211 }
212
213 template<class primtype>
214 inline std::tuple<sycl::vec<primtype, 3>, sycl::vec<primtype, 3>> get_box(Patch &p) {
215 return patch_coord_to_domain<sycl::vec<primtype, 3>>(p);
216 }
217
218 template<class T>
219 inline PatchCoordTransform<T> get_transform() {
220 auto [bmin, bmax] = get_bounding_box<T>();
221 return PatchCoordTransform<T>{
222 patch_coord_bounding_box, shammath::CoordRange<T>{bmin, bmax}};
223 }
224
231 void to_json(nlohmann::json &j);
232
239 void from_json(const nlohmann::json &j);
240 };
241
243 // out of line implementation of the simbox
245
246 template<class T>
247 [[nodiscard]] inline std::tuple<T, T> SimulationBoxInfo::get_bounding_box() const {
248
249 if (!pdl.check_main_field_type<T>()) {
250
252 "the chosen type for the main field does not match the required template type\n"
253 "call : "
254 + std::string(__PRETTY_FUNCTION__));
255 }
256
257 const shammath::CoordRange<T> *pval
258 = std::get_if<shammath::CoordRange<T>>(&bounding_box.value);
259
260 if (!pval) {
261
263 "the type in SimulationBoxInfo does not match the one in the layout\n"
264 "call : "
265 + std::string(__PRETTY_FUNCTION__));
266 }
267
268 return {pval->lower, pval->upper};
269 }
270
271 template<class T>
273 new_box.check_throw_ranges();
274 if (pdl.check_main_field_type<T>()) {
275 bounding_box.value = new_box;
276 } else {
278 "The main field is not of the required type\n"
279 "call : "
280 + std::string(__PRETTY_FUNCTION__));
281 }
282 }
283
284 template<class T>
286
287 auto [bmin, bmax] = get_bounding_box<T>();
288
289 shammath::CoordRange<T> tmp{bmin, bmax};
290
291 if (tmp.is_err_mode()) {
293 "the box size is not set, please resize the box to the domain size");
294 }
295
296 return PatchCoordTransform<T>{patch_coord_bounding_box.get_patch_range(), tmp};
297 }
298
299 template<class T>
300 inline std::tuple<T, T> SimulationBoxInfo::patch_coord_to_domain(const Patch &p) const {
301
302 PatchCoordTransform<T> transform = get_patch_transform<T>();
303
304 auto [obj_min, obj_max] = transform.to_obj_coord(p);
305
306 return {obj_min, obj_max};
307 }
308
310
311 if (pdl.check_main_field_type<f32_3>()) {
313 } else if (pdl.check_main_field_type<f64_3>()) {
315 } else if (pdl.check_main_field_type<u32_3>()) {
317 } else if (pdl.check_main_field_type<u64_3>()) {
319 } else if (pdl.check_main_field_type<i64_3>()) {
321 } else {
323 "the chosen type for the main field is not handled");
324 }
325 }
326
327} // namespace shamrock::patch
Header file for the patch struct and related function.
double f64
Alias for double.
float f32
Alias for float.
std::uint32_t u32
32 bit unsigned integer
bool check_main_field_type()
check that main field (id=0)is of type T
Store the information related to the size of the simulation box to convert patch integer coordinates ...
Definition SimBox.hpp:35
void allreduce_set_bounding_box(shammath::CoordRange< T > new_box)
Set the stored bounding box after an all-reduce operation on the supplied bounds.
Definition SimBox.hpp:113
void reset_box_size()
Reset the bounding box of the simulation domain to the maximum extents of the main field.
Definition SimBox.hpp:309
std::tuple< T, T > get_bounding_box() const
Get the stored bounding box of the domain.
Definition SimBox.hpp:247
T get_bounding_box_size() const
Get the size of the stored bounding box of the domain.
Definition SimBox.hpp:87
void to_json(nlohmann::json &j)
Serializes a SimulationBoxInfo object to a JSON object.
Definition SimBox.cpp:31
std::tuple< T, T > patch_coord_to_domain(const Patch &p) const
get the patch coordinates on the domain
Definition SimBox.hpp:300
void from_json(const nlohmann::json &j)
Deserializes a JSON object into a SimulationBoxInfo object.
Definition SimBox.cpp:71
PatchCoordTransform< T > get_patch_transform() const
Get a PatchCoordTransform object that describes the conversion between patch coordinates and domain c...
Definition SimBox.hpp:285
void set_bounding_box(shammath::CoordRange< T > new_box)
Override the stored bounding box by the one given in new_box.
Definition SimBox.hpp:272
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
Patch object that contain generic patch information.
Definition Patch.hpp:33