Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchDataLayerLayout.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
18
21#include "nlohmann/json_fwd.hpp"
23#include <sstream>
24#include <variant>
25#include <vector>
26
27namespace shamrock::patch {
28
35 template<class T>
37 public:
39 using field_T = T;
40
42 std::string name;
43
46
53 inline FieldDescriptor() : name(""), nvar(1) {};
54
61 inline FieldDescriptor(std::string name, u32 nvar) : nvar(nvar), name(name) {}
62 };
63
65
66 template<class T>
67 using FieldDescriptor = shamrock::patch::FieldDescriptor<T>;
68
69 // using var_t = var_t_template<FieldDescriptor>;
71
72 std::vector<var_t> fields;
73
74 public:
82 template<class T>
83 void add_field(
84 const std::string &field_name, u32 nvar, SourceLocation loc = SourceLocation{});
85
92 [[nodiscard]] bool has_field_name(const std::string &field_name) const;
93
101 template<class T>
102 FieldDescriptor<T> get_field(const std::string &field_name);
103
111 template<class T>
112 FieldDescriptor<T> get_field(u32 idx);
113
121 template<class T>
122 u32 get_field_idx(const std::string &field_name) const;
123
132 template<class T>
133 u32 get_field_idx(const std::string &field_name, u32 nvar) const;
134
143 template<class T>
144 bool check_field_type(u32 idx);
145
153 template<class T>
154 inline bool check_main_field_type() {
155 return check_field_type<T>(0);
156 }
157
163 [[nodiscard]] inline const var_t &get_main_field_any() const { return fields[0]; }
164
170 std::string get_description_str() const;
171
177 std::vector<std::string> get_field_names();
178
185 template<class Functor>
186 inline void for_each_field_any(Functor &&func) const {
187 for (auto &f : fields) {
188 f.visit([&](auto &arg) {
189 func(arg);
190 });
191 }
192 }
193
201 inline void add_field_t(std::string fname, u32 nvar, std::string type) {
202 if (type == "f32") {
203 add_field<f32>(fname, nvar);
204 } else if (type == "f32_2") {
205 add_field<f32_2>(fname, nvar);
206 } else if (type == "f32_3") {
207 add_field<f32_3>(fname, nvar);
208 } else if (type == "f32_4") {
209 add_field<f32_4>(fname, nvar);
210 } else if (type == "f32_8") {
211 add_field<f32_8>(fname, nvar);
212 } else if (type == "f32_16") {
213 add_field<f32_16>(fname, nvar);
214 } else if (type == "f64") {
215 add_field<f64>(fname, nvar);
216 } else if (type == "f64_2") {
217 add_field<f64_2>(fname, nvar);
218 } else if (type == "f64_3") {
219 add_field<f64_3>(fname, nvar);
220 } else if (type == "f64_4") {
221 add_field<f64_4>(fname, nvar);
222 } else if (type == "f64_8") {
223 add_field<f64_8>(fname, nvar);
224 } else if (type == "f64_16") {
225 add_field<f64_16>(fname, nvar);
226 } else if (type == "u32") {
227 add_field<u32>(fname, nvar);
228 } else if (type == "u64") {
229 add_field<u64>(fname, nvar);
230 } else if (type == "u32_3") {
231 add_field<u32_3>(fname, nvar);
232 } else if (type == "u64_3") {
233 add_field<u64_3>(fname, nvar);
234 } else if (type == "i64_3") {
235 add_field<i64_3>(fname, nvar);
236 } else {
238 "the select type is not recognized");
239 }
240 }
241
254 friend bool operator==(const PatchDataLayerLayout &lhs, const PatchDataLayerLayout &rhs);
255 };
256
266 void to_json(nlohmann::json &j, const PatchDataLayerLayout &p);
267
277 void from_json(const nlohmann::json &j, PatchDataLayerLayout &p);
278
291 bool operator==(const PatchDataLayerLayout &lhs, const PatchDataLayerLayout &rhs);
292
294 // out of line implementation of the PatchDataLayerLayout
296
297 template<class T>
298 inline PatchDataLayerLayout::FieldDescriptor<T> PatchDataLayerLayout::get_field(
299 const std::string &field_name) {
300
301 for (var_t &fvar : fields) {
302 if (FieldDescriptor<T> *pval = std::get_if<FieldDescriptor<T>>(&fvar.value)) {
303 if (pval->name == field_name) {
304 return *pval;
305 }
306 }
307 }
308
310 "the requested field does not exists\n current table : " + get_description_str());
311 }
312
313 template<class T>
314 inline PatchDataLayerLayout::FieldDescriptor<T> PatchDataLayerLayout::get_field(u32 idx) {
315
316 if (FieldDescriptor<T> *pval = std::get_if<FieldDescriptor<T>>(&fields[idx].value)) {
317 return *pval;
318 }
319
321 "the required type does no match at index " + std::to_string(idx)
322 + "\n current table : " + get_description_str());
323 }
324
325 template<class T>
327 var_t &tmp = fields[idx];
328
329 FieldDescriptor<T> *pval = std::get_if<FieldDescriptor<T>>(&tmp.value);
330
331 if (pval) {
332 return true;
333 } else {
334 return false;
335 }
336 }
337
338} // namespace shamrock::patch
Field variant object to instanciate a variant on the patch types.
void to_json(nlohmann::json &j, const PatchSchedulerConfig &p)
Converts a PatchSchedulerConfig object to a JSON object.
void from_json(const nlohmann::json &j, PatchSchedulerConfig &p)
Deserializes a PatchSchedulerConfig object from a JSON object.
Source location utility.
std::uint32_t u32
32 bit unsigned integer
Structure describing a field in a patch data layout.
T field_T
The type of the field mirrored from the template type.
std::string name
The name of the field.
FieldDescriptor(std::string name, u32 nvar)
Constructor with a given name and number of variables.
u32 nvar
The number of variables of the field per object.
u32 get_field_idx(const std::string &field_name) const
Get the field id if matching name & type.
friend bool operator==(const PatchDataLayerLayout &lhs, const PatchDataLayerLayout &rhs)
Overloaded equality operator for PatchDataLayerLayout class.
void add_field(const std::string &field_name, u32 nvar, SourceLocation loc=SourceLocation{})
add a field of type T to the layout
const var_t & get_main_field_any() const
Get the main field description as a variant object.
bool check_field_type(u32 idx)
check that field of id @idx is of type T
bool check_main_field_type()
check that main field (id=0)is of type T
FieldDescriptor< T > get_field(const std::string &field_name)
Get the field description id if matching name & type.
std::vector< std::string > get_field_names()
Get the list of field names.
bool has_field_name(const std::string &field_name) const
Check whether a field with the given name already exists.
void for_each_field_any(Functor &&func) const
for each visit of each field
void add_field_t(std::string fname, u32 nvar, std::string type)
Add a field with type specified as a string.
std::string get_description_str() const
Get the description of the layout.
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.
provide information about the source location