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
17
19#include "nlohmann/json_fwd.hpp"
26#include <type_traits>
27#include <stdexcept>
28#include <tuple>
29
30namespace shamrock::patch {
31
36 class SimulationBoxInfo {
37
38 static constexpr u32 dim = 3;
39
41
43
44 var_t bounding_box;
45 PatchCoord<> patch_coord_bounding_box;
46
47 public:
48 inline SimulationBoxInfo(
49 PatchDataLayerLayout &pdl, PatchCoord<dim> patch_coord_bounding_box)
50 : pdl(pdl), patch_coord_bounding_box(std::move(patch_coord_bounding_box)),
51 bounding_box(shammath::CoordRange<f32>{}) {
52
54 }
55
56 void set_patch_coord_bounding_box(PatchCoord<dim> new_patch_coord_box) {
57 patch_coord_bounding_box = new_patch_coord_box;
58 shamlog_debug_ln(
59 "SimBox",
60 "changed patch coord bounds :",
61 std::pair{
62 u64_3{
63 new_patch_coord_box.coord_min[0],
64 new_patch_coord_box.coord_min[1],
65 new_patch_coord_box.coord_min[2]},
66 u64_3{
67 new_patch_coord_box.coord_max[0],
68 new_patch_coord_box.coord_max[1],
69 new_patch_coord_box.coord_max[2]}});
70 }
71
78 template<class T>
79 [[nodiscard]] std::tuple<T, T> get_bounding_box() const;
80
87 template<class T>
88 inline T get_bounding_box_size() const {
89 auto [bmin, bmax] = get_bounding_box<T>();
90 return bmax - bmin;
91 }
92
99 template<class T>
101
113 template<class T>
115 std::pair<T, T> reduced = shamalgs::collective::allreduce_bounds(
116 std::pair<T, T>{new_box.lower, new_box.upper});
118 }
119
135 template<class T>
137
145 template<class T>
146 std::tuple<T, T> patch_coord_to_domain(const Patch &p) const;
147
148 // TODO implement box size reduction here
149
174 void reset_box_size();
175
178 template<class primtype>
179 void clean_box(primtype tol);
180
181 template<>
182 inline void clean_box<f32>(f32 tol) {
183
184 auto [bmin, bmax] = get_bounding_box<f32_3>();
185
186 f32_3 center = (bmin + bmax) / 2;
187 f32_3 cur_delt = bmax - bmin;
188 cur_delt /= 2;
189
190 cur_delt *= tol;
191
192 bmin = center - cur_delt;
193 bmax = center + cur_delt;
194
195 set_bounding_box<f32_3>({bmin, bmax});
196 }
197
198 template<>
199 inline void clean_box<f64>(f64 tol) {
200 auto [bmin, bmax] = get_bounding_box<f64_3>();
201
202 f64_3 center = (bmin + bmax) / 2;
203 f64_3 cur_delt = bmax - bmin;
204 cur_delt /= 2;
205
206 cur_delt *= tol;
207
208 bmin = center - cur_delt;
209 bmax = center + cur_delt;
210
211 set_bounding_box<f64_3>({bmin, bmax});
212 }
213
214 template<class primtype>
215 inline std::tuple<sycl::vec<primtype, 3>, sycl::vec<primtype, 3>> get_box(Patch &p) {
217 }
218
219 template<class T>
220 inline PatchCoordTransform<T> get_transform() {
221 auto [bmin, bmax] = get_bounding_box<T>();
222 return PatchCoordTransform<T>{
223 patch_coord_bounding_box, shammath::CoordRange<T>{bmin, bmax}};
224 }
225
232 void to_json(nlohmann::json &j);
233
240 void from_json(const nlohmann::json &j);
241 };
242
244 // out of line implementation of the simbox
246
247 template<class T>
248 [[nodiscard]] inline std::tuple<T, T> SimulationBoxInfo::get_bounding_box() const {
249
250 if (!pdl.check_main_field_type<T>()) {
251
253 "the chosen type for the main field does not match the required template type\n"
254 "call : "
255 + std::string(__PRETTY_FUNCTION__));
256 }
257
258 const shammath::CoordRange<T> *pval
259 = std::get_if<shammath::CoordRange<T>>(&bounding_box.value);
260
261 if (!pval) {
262
264 "the type in SimulationBoxInfo does not match the one in the layout\n"
265 "call : "
266 + std::string(__PRETTY_FUNCTION__));
267 }
268
269 return {pval->lower, pval->upper};
270 }
271
272 template<class T>
274 new_box.check_throw_ranges();
275 if (pdl.check_main_field_type<T>()) {
276 bounding_box.value = new_box;
277 } else {
279 "The main field is not of the required type\n"
280 "call : "
281 + std::string(__PRETTY_FUNCTION__));
282 }
283 }
284
285 template<class T>
287
288 auto [bmin, bmax] = get_bounding_box<T>();
289
290 shammath::CoordRange<T> tmp{bmin, bmax};
291
292 if (tmp.is_err_mode()) {
294 "the box size is not set, please resize the box to the domain size");
295 }
296
297 return PatchCoordTransform<T>{patch_coord_bounding_box.get_patch_range(), tmp};
298 }
299
300 template<class T>
301 inline std::tuple<T, T> SimulationBoxInfo::patch_coord_to_domain(const Patch &p) const {
302
304
305 auto [obj_min, obj_max] = transform.to_obj_coord(p);
306
307 return {obj_min, obj_max};
308 }
309
311
312 if (pdl.check_main_field_type<f32_3>()) {
313 bounding_box.value = shammath::CoordRange<f32_3>::max_range();
314 } else if (pdl.check_main_field_type<f64_3>()) {
315 bounding_box.value = shammath::CoordRange<f64_3>::max_range();
316 } else if (pdl.check_main_field_type<u32_3>()) {
317 bounding_box.value = shammath::CoordRange<u32_3>::max_range();
318 } else if (pdl.check_main_field_type<u64_3>()) {
319 bounding_box.value = shammath::CoordRange<u64_3>::max_range();
320 } else if (pdl.check_main_field_type<i64_3>()) {
321 bounding_box.value = shammath::CoordRange<i64_3>::max_range();
322 } else {
324 "the chosen type for the main field is not handled");
325 }
326 }
327
328} // 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
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:114
void reset_box_size()
Reset the bounding box of the simulation domain to the maximum extents of the main field.
Definition SimBox.hpp:310
std::tuple< T, T > get_bounding_box() const
Get the stored bounding box of the domain.
Definition SimBox.hpp:248
T get_bounding_box_size() const
Get the size of the stored bounding box of the domain.
Definition SimBox.hpp:88
void to_json(nlohmann::json &j)
Serializes a SimulationBoxInfo object to a JSON object.
Definition SimBox.cpp:32
std::tuple< T, T > patch_coord_to_domain(const Patch &p) const
get the patch coordinates on the domain
Definition SimBox.hpp:301
void from_json(const nlohmann::json &j)
Deserializes a JSON object into a SimulationBoxInfo object.
Definition SimBox.cpp:72
PatchCoordTransform< T > get_patch_transform() const
Get a PatchCoordTransform object that describes the conversion between patch coordinates and domain c...
Definition SimBox.hpp:286
void set_bounding_box(shammath::CoordRange< T > new_box)
Override the stored bounding box by the one given in new_box.
Definition SimBox.hpp:273
This header file contains utility functions related to exception handling in the code.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
Patch object that contain generic patch information.
Definition Patch.hpp:33