Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchDataLayerLayout.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
18
19namespace shamrock::patch {
21 std::stringstream ss;
22
23 if (fields.empty()) {
24 ss << "empty table\n";
25 } else {
26
27 u32 index = 0;
28 for (const var_t &v : fields) {
29 v.visit([&](auto &field) {
30 using f_t = typename std::remove_reference<decltype(field)>::type;
31 using base_t = typename f_t::field_T;
32
33 ss << index << " : " << field.name << " : nvar=" << field.nvar << " type : ";
34
35 if (std::is_same<base_t, f32>::value) {
36 ss << "f32 ";
37 } else if (std::is_same<base_t, f32_2>::value) {
38 ss << "f32_2 ";
39 } else if (std::is_same<base_t, f32_3>::value) {
40 ss << "f32_3 ";
41 } else if (std::is_same<base_t, f32_4>::value) {
42 ss << "f32_4 ";
43 } else if (std::is_same<base_t, f32_8>::value) {
44 ss << "f32_8 ";
45 } else if (std::is_same<base_t, f32_16>::value) {
46 ss << "f32_16";
47 } else if (std::is_same<base_t, f64>::value) {
48 ss << "f64 ";
49 } else if (std::is_same<base_t, f64_2>::value) {
50 ss << "f64_2 ";
51 } else if (std::is_same<base_t, f64_3>::value) {
52 ss << "f64_3 ";
53 } else if (std::is_same<base_t, f64_4>::value) {
54 ss << "f64_4 ";
55 } else if (std::is_same<base_t, f64_8>::value) {
56 ss << "f64_8 ";
57 } else if (std::is_same<base_t, f64_16>::value) {
58 ss << "f64_16";
59 } else if (std::is_same<base_t, u32>::value) {
60 ss << "u32 ";
61 } else if (std::is_same<base_t, u64>::value) {
62 ss << "u64 ";
63 } else if (std::is_same<base_t, u32_3>::value) {
64 ss << "u32_3 ";
65 } else if (std::is_same<base_t, u64_3>::value) {
66 ss << "u64_3 ";
67 } else if (std::is_same<base_t, i64_3>::value) {
68 ss << "i64_3 ";
69 } else {
70 ss << "unknown";
71 }
72
73 ss << "\n";
74
75 index++;
76 });
77 }
78 }
79
80 return ss.str();
81 }
82
83 std::vector<std::string> PatchDataLayerLayout::get_field_names() {
84 std::vector<std::string> ret;
85
86 for (var_t &v : fields) {
87 v.visit([&](auto &field) {
88 ret.push_back(field.name);
89 });
90 }
91
92 return ret;
93 }
94
95 void to_json(nlohmann::json &j, const PatchDataLayerLayout &p) {
96
97 using json = nlohmann::json;
98
99 std::vector<json> entries;
100
101 p.for_each_field_any([&](auto &field) {
102 using f_t = typename std::remove_reference<decltype(field)>::type;
103 using base_t = typename f_t::field_T;
104
105 auto get_tname = []() {
106 if (std::is_same<base_t, f32>::value) {
107 return "f32";
108 } else if (std::is_same<base_t, f32_2>::value) {
109 return "f32_2";
110 } else if (std::is_same<base_t, f32_3>::value) {
111 return "f32_3";
112 } else if (std::is_same<base_t, f32_4>::value) {
113 return "f32_4";
114 } else if (std::is_same<base_t, f32_8>::value) {
115 return "f32_8";
116 } else if (std::is_same<base_t, f32_16>::value) {
117 return "f32_16";
118 } else if (std::is_same<base_t, f64>::value) {
119 return "f64";
120 } else if (std::is_same<base_t, f64_2>::value) {
121 return "f64_2";
122 } else if (std::is_same<base_t, f64_3>::value) {
123 return "f64_3";
124 } else if (std::is_same<base_t, f64_4>::value) {
125 return "f64_4";
126 } else if (std::is_same<base_t, f64_8>::value) {
127 return "f64_8";
128 } else if (std::is_same<base_t, f64_16>::value) {
129 return "f64_16";
130 } else if (std::is_same<base_t, u32>::value) {
131 return "u32";
132 } else if (std::is_same<base_t, u64>::value) {
133 return "u64";
134 } else if (std::is_same<base_t, u32_3>::value) {
135 return "u32_3";
136 } else if (std::is_same<base_t, u64_3>::value) {
137 return "u64_3";
138 } else if (std::is_same<base_t, i64_3>::value) {
139 return "i64_3";
140 } else {
142 return "";
143 }
144 };
145
146 entries.push_back(
147 json{
148 {"type", get_tname()},
149 {"nvar", field.nvar},
150 {"field_name", field.name},
151 });
152 });
153
154 j = entries;
155 }
156
157 void from_json(const nlohmann::json &j, PatchDataLayerLayout &p) {
158 for (auto &entry : j) {
159 p.add_field_t(entry["field_name"], entry["nvar"].get<u32>(), entry["type"]);
160 }
161 }
162
163 bool operator==(const PatchDataLayerLayout &lhs, const PatchDataLayerLayout &rhs) {
164
165 bool ret = true;
166 ret = ret && (lhs.fields.size() == rhs.fields.size());
167
168 for (u32 i = 0; i < lhs.fields.size(); i++) {
169 const PatchDataLayerLayout::var_t &var_lhs = lhs.fields[i];
170 const PatchDataLayerLayout::var_t &var_rhs = rhs.fields[i];
171
172 std::visit(
173 [&](auto &flhs, auto &frhs) {
174 using t1 = typename std::remove_reference<decltype(flhs)>::type;
175 using t2 = typename std::remove_reference<decltype(frhs)>::type;
176
177 ret = ret && std::is_same_v<t1, t2>;
178 ret = ret && (flhs.nvar == frhs.nvar);
179 ret = ret && (flhs.name == frhs.name);
180 },
181 var_lhs.value,
182 var_rhs.value);
183 }
184
185 return ret;
186 }
187
188} // namespace shamrock::patch
std::uint32_t u32
32 bit unsigned integer
std::vector< std::string > get_field_names()
Get the list of field names.
std::string get_description_str() const
Get the description of the layout.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.