Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
SimBox.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
15
18#include <nlohmann/json.hpp>
19
22template<class... Ts>
23struct overloaded : Ts... {
24 using Ts::operator()...;
25};
26
27template<class... Ts>
28overloaded(Ts...) -> overloaded<Ts...>;
29
30namespace shamrock::patch {
31
32 void SimulationBoxInfo::to_json(nlohmann::json &j) {
33 auto &pcoord = patch_coord_bounding_box;
34 auto &bounding_box_var = bounding_box;
35
36 j["patchcoormin"] = pcoord.coord_min;
37 j["patchcoormax"] = pcoord.coord_max;
38
39 std::visit(
41 [&](auto arg) {
43 },
45 j["coordtype"] = "f32_3";
46 j["box_min"] = sham::sycl_vec_to_array(arg.lower);
47 j["box_max"] = sham::sycl_vec_to_array(arg.upper);
48 },
50 j["coordtype"] = "f64_3";
51 j["box_min"] = sham::sycl_vec_to_array(arg.lower);
52 j["box_max"] = sham::sycl_vec_to_array(arg.upper);
53 },
55 j["coordtype"] = "u32_3";
56 j["box_min"] = sham::sycl_vec_to_array(arg.lower);
57 j["box_max"] = sham::sycl_vec_to_array(arg.upper);
58 },
60 j["coordtype"] = "u64_3";
61 j["box_min"] = sham::sycl_vec_to_array(arg.lower);
62 j["box_max"] = sham::sycl_vec_to_array(arg.upper);
63 },
65 j["coordtype"] = "i64_3";
66 j["box_min"] = sham::sycl_vec_to_array(arg.lower);
67 j["box_max"] = sham::sycl_vec_to_array(arg.upper);
68 }},
69 bounding_box_var.value);
70 }
71
72 void SimulationBoxInfo::from_json(const nlohmann::json &j) {
73 PatchCoord<3> pcoord;
74 j.at("patchcoormin").get_to(pcoord.coord_min);
75 j.at("patchcoormax").get_to(pcoord.coord_max);
76 patch_coord_bounding_box = pcoord;
77
78 // logger::raw_ln(
79 // pdl.check_main_field_type<f64_3>(),
80 // j.at("coordtype").get<std::string>(),
81 // j.at("coordtype").get<std::string>() == "f64_3");
82 if (pdl.check_main_field_type<f32_3>()
83 && (j.at("coordtype").get<std::string>() == "f32_3")) {
84 bounding_box.value = shammath::CoordRange<f32_3>{
85 sham::array_to_sycl_vec(j.at("box_min").get<std::array<f32, 3>>()),
86 sham::array_to_sycl_vec(j.at("box_max").get<std::array<f32, 3>>()),
87 };
88 } else if (
89 pdl.check_main_field_type<f64_3>()
90 && (j.at("coordtype").get<std::string>() == "f64_3")) {
91 bounding_box.value = shammath::CoordRange<f64_3>{
92 sham::array_to_sycl_vec(j.at("box_min").get<std::array<f64, 3>>()),
93 sham::array_to_sycl_vec(j.at("box_max").get<std::array<f64, 3>>()),
94 };
95 } else if (
96 pdl.check_main_field_type<u32_3>()
97 && (j.at("coordtype").get<std::string>() == "u32_3")) {
98 bounding_box.value = shammath::CoordRange<u32_3>{
99 sham::array_to_sycl_vec(j.at("box_min").get<std::array<u32, 3>>()),
100 sham::array_to_sycl_vec(j.at("box_max").get<std::array<u32, 3>>()),
101 };
102 } else if (
103 pdl.check_main_field_type<u64_3>()
104 && (j.at("coordtype").get<std::string>() == "u64_3")) {
105 bounding_box.value = shammath::CoordRange<u64_3>{
106 sham::array_to_sycl_vec(j.at("box_min").get<std::array<u64, 3>>()),
107 sham::array_to_sycl_vec(j.at("box_max").get<std::array<u64, 3>>()),
108 };
109 } else if (
110 pdl.check_main_field_type<i64_3>()
111 && (j.at("coordtype").get<std::string>() == "i64_3")) {
112 bounding_box.value = shammath::CoordRange<i64_3>{
113 sham::array_to_sycl_vec(j.at("box_min").get<std::array<i64, 3>>()),
114 sham::array_to_sycl_vec(j.at("box_max").get<std::array<i64, 3>>()),
115 };
116 } else {
117 throw shambase::make_except_with_loc<std::runtime_error>("unable to parse json type");
118 }
119 }
120
121} // namespace shamrock::patch
overloaded(Ts...) -> overloaded< Ts... >
explicit deduction guide (not needed as of C++20)
void to_json(nlohmann::json &j)
Serializes a SimulationBoxInfo object to a JSON object.
Definition SimBox.cpp:32
void from_json(const nlohmann::json &j)
Deserializes a JSON object into a SimulationBoxInfo object.
Definition SimBox.cpp:72
std::array< T, n > sycl_vec_to_array(sycl::vec< T, n > v)
Converts a SYCL vector into a C++ standard library array.
sycl::vec< T, n > array_to_sycl_vec(std::array< T, n > v)
Converts a C++ standard library array into a SYCL vector.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
Contains functions for converting between SYCL vector types and C++ standard library array types.