/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/tuples.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/common/tuples.hh Source File
DiFfRG
tuples.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <cmath>
5#include <cstring>
6#include <iostream>
7#include <string_view>
8#include <tuple>
9#include <utility>
10#include <vector>
11
12// DiFfRG
14
15namespace DiFfRG
16{
20 template <typename T>
21 concept NamedTuple = requires(T t) {
22 typename T::tuple_type;
23 typename T::tuple_names;
24 { t.tuple } -> std::same_as<typename T::tuple_type>;
25 { t.names } -> std::same_as<typename T::tuple_names>;
26 { t.size } -> std::same_as<size_t>;
27 };
28
32 constexpr bool strings_equal(char const *a, char const *b) { return std::string_view(a) == b; }
33
34 template <FixedString... strs> struct StringSet {
35 static constexpr size_t size = sizeof...(strs);
36 static constexpr std::array<const char *, size> names{{strs...}};
37
38 // If two names are the same, the program should not compile
39 static_assert(
40 []<size_t... I>(std::index_sequence<I...>) {
41 for (size_t i : {I...})
42 for (size_t j : {I...})
43 if (i != j && strings_equal(names[i], names[j])) return false;
44 return true;
45 }(std::make_index_sequence<size>{}),
46 "Names of a StringSet must be unique!");
47 };
48
56 template <typename tuple_type, typename tuple_names> struct named_tuple {
57 static_assert(tuple_names::size == std::tuple_size_v<tuple_type>,
58 "Number of names must match number of elements in tuple");
59 tuple_type tuple;
60
61 constexpr operator tuple_type &() { return tuple; }
62
63 static constexpr size_t size = tuple_names::size;
64 static constexpr auto names = tuple_names::names;
65
66 // If two names are the same, the program should not compile
67 static_assert(
68 []<size_t... I>(std::index_sequence<I...>) {
69 for (size_t i : {I...})
70 for (size_t j : {I...})
71 if (i != j && strings_equal(names[i], names[j])) return false;
72 return true;
73 }(std::make_index_sequence<size>{}),
74 "Names of a named_tuple must be unique!");
75
76 named_tuple(tuple_type &&t) : tuple(t) {}
77 named_tuple(tuple_type &t) : tuple(t) {}
78
79 template <typename... T> static constexpr auto as(std::tuple<T...> &&tup)
80 {
81 return named_tuple<std::tuple<T...>, tuple_names>(tup);
82 }
83 template <typename... T> static constexpr auto as(std::tuple<T...> &tup)
84 {
85 return named_tuple<std::tuple<T...>, tuple_names>(tup);
86 }
87
88 static consteval size_t get_idx(const char *name)
89 {
90 size_t running_sum = 0;
91 for (size_t i = 0; i < names.size(); ++i) {
92 // this is a way to compare two strings at compile time https://stackoverflow.com/a/53762940
93 if (strings_equal(names[i], name)) return i;
94 running_sum += 1;
95 }
96 // produce a compile-time error if the name is not found in the list
97 return size != running_sum
98 ? 0
99 : throw std::invalid_argument("named_tuple::get_idx: Name \"" + std::string(name) +
100 "\" not found. Available names are: " + ((std::string("") + "; ") + ""));
101 }
102
103 template <size_t idx> auto &get() { return std::get<idx>(tuple); }
104 template <size_t idx> const auto &get() const { return std::get<idx>(tuple); }
105 };
106
110 template <FixedString name, typename tuple_type, typename strSet>
112 {
113 constexpr size_t idx = named_tuple<tuple_type, strSet>::get_idx(name);
114 return ob.template get<idx>();
115 }
116 template <FixedString name, typename tuple_type, typename strSet>
118 {
119 constexpr size_t idx = named_tuple<tuple_type, strSet>::get_idx(name);
120 return ob.template get<idx>();
121 }
122 template <FixedString name, typename tuple_type, typename strSet>
123 constexpr auto &get(const named_tuple<tuple_type, strSet> &ob)
124 {
125 return std::get<named_tuple<tuple_type, strSet>::get_idx(name)>(ob.tuple);
126 }
127
128 template <size_t idx, typename tuple_type, typename strSet> constexpr auto &get(named_tuple<tuple_type, strSet> &ob)
129 {
130 return ob.template get<idx>();
131 }
132 template <size_t idx, typename tuple_type, typename strSet> constexpr auto &get(named_tuple<tuple_type, strSet> &&ob)
133 {
134 return ob.template get<idx>();
135 }
136 template <size_t idx, typename tuple_type, typename strSet>
137 constexpr auto &get(const named_tuple<tuple_type, strSet> &ob)
138 {
139 return ob.template get<idx>();
140 }
141} // namespace DiFfRG
142
143// Intense Voodoo
144namespace DiFfRG
145{
146 // ----------------------------------------------------------------------
147 // AD helpers. The following are used to process the jacobians via
148 // automatic differentiation. Most of these methods are used in the
149 // file model/AD.hh
150 // ----------------------------------------------------------------------
151
155 template <typename NT, uint N, uint M = N> class SimpleMatrix
156 {
157 public:
159
163 NT &operator()(const uint n, const uint m) { return data[n * M + m]; }
164
168 const NT &operator()(const uint n, const uint m) const { return data[n * M + m]; }
169
173 void clear()
174 {
175 for (uint i = 0; i < N * M; ++i)
176 data[i] = 0.;
177 }
178
179 bool is_finite() const
180 {
181 if constexpr (std::is_floating_point_v<NT>) {
182 for (uint i = 0; i < N * M; ++i)
183 if (!std::isfinite(data[i])) return false;
184 }
185 return true;
186 }
187
191 void print() const
192 {
193 for (uint i = 0; i < N; ++i) {
194 for (uint j = 0; j < M; ++j)
195 std::cout << data[i * M + j] << " ";
196 std::cout << std::endl;
197 }
198 }
199
200 private:
201 std::array<NT, N * M> data;
202 };
203
204 template <uint n, typename NT, typename Vector> std::array<NT, n> vector_to_array(const Vector &v)
205 {
206 std::array<NT, n> x;
207 for (uint i = 0; i < n; ++i)
208 x[i] = v[i];
209 return x;
210 }
211
212 template <typename T, std::size_t... Indices>
213 auto vector_to_tuple_helper(const std::vector<T> &v, std::index_sequence<Indices...>)
214 {
215 return std::tie(v[Indices]...);
216 }
217 template <std::size_t N, typename T> auto vector_to_tuple(const std::vector<T> &v)
218 {
219 assert(v.size() >= N);
220 return vector_to_tuple_helper(v, std::make_index_sequence<N>());
221 }
222
223 template <typename Head, typename... Tail> constexpr auto tuple_tail(const std::tuple<Head, Tail...> &t)
224 {
225 return std::apply([](auto & /*head*/, auto &...tail) { return std::tie(tail...); }, t);
226 }
227 // also for named tuple
228 template <typename tuple_type, typename strSet> constexpr auto tuple_tail(const named_tuple<tuple_type, strSet> &t)
229 {
230 return tuple_tail(t.tuple);
231 }
232
233 template <int i, typename Head, typename... Tail> constexpr auto tuple_last(const std::tuple<Head, Tail...> &t)
234 {
235 if constexpr (sizeof...(Tail) == i)
236 return std::apply([](auto & /*head*/, auto &...tail) { return std::tie(tail...); }, t);
237 else
238 return std::apply([](auto & /*head*/, auto &...tail) { return tuple_last<i>(std::tie(tail...)); }, t);
239 }
240 // also for named tuple
241 template <int i, typename tuple_type, typename strSet>
243 {
244 return tuple_last<i>(t.tuple);
245 }
246
247 template <int i, typename Head, typename... Tail> constexpr auto tuple_first(const std::tuple<Head, Tail...> &t)
248 {
249 if constexpr (i == 0)
250 return std::tuple();
251 else if constexpr (i == 1)
252 return std::apply([](auto &head, auto &.../*tail*/) { return std::tie(head); }, t);
253 else
254 return std::apply(
255 [](auto &head, auto &...tail) {
256 return std::tuple_cat(std::tie(head), tuple_first<i - 1>(std::tie(tail...)));
257 },
258 t);
259 }
260 // also for named tuple
261 template <int i, typename tuple_type, typename strSet>
263 {
264 return tuple_first<i>(t.tuple);
265 }
266
267 // ----------------------------------------------------------------------
268 // Helper functions to get the local solution at a given q_index.
269 // This is specifically for the local solutions of the subsystems,
270 // i.e. only used by the LDG assembler.
271 // ----------------------------------------------------------------------
272
273 template <typename T, size_t N, size_t... IDXs>
274 auto _local_sol_tuple(const std::array<T, N> &a, std::index_sequence<IDXs...>, uint q_index)
275 {
276 return std::tie(a[IDXs][q_index]...);
277 }
278 template <typename T, size_t N> auto local_sol_q(const std::array<T, N> &a, uint q_index)
279 {
280 return _local_sol_tuple(a, std::make_index_sequence<N>{}, q_index);
281 }
282
283 template <typename T_inner, typename Model, size_t... IDXs> auto _jacobian_tuple(std::index_sequence<IDXs...>)
284 {
285 return std::tuple{SimpleMatrix<T_inner, Model::Components::count_fe_functions(0),
286 Model::Components::count_fe_functions(IDXs)>()...};
287 }
288 template <typename T_inner, typename Model> auto jacobian_tuple()
289 {
290 return _jacobian_tuple<T_inner, Model>(std::make_index_sequence<Model::Components::count_fe_subsystems()>{});
291 }
292
293 template <typename T_inner, typename Model, size_t... IDXs> auto _jacobian_2_tuple(std::index_sequence<IDXs...>)
294 {
295 return std::tuple{std::array<
296 SimpleMatrix<T_inner, Model::Components::count_fe_functions(0), Model::Components::count_fe_functions(IDXs)>,
297 2>()...};
298 }
299 template <typename T_inner, typename Model> auto jacobian_2_tuple()
300 {
301 return _jacobian_2_tuple<T_inner, Model>(std::make_index_sequence<Model::Components::count_fe_subsystems()>{});
302 }
303} // namespace DiFfRG
A simple NxM-matrix class, which is used for cell-wise Jacobians.
Definition tuples.hh:156
const NT & operator()(const uint n, const uint m) const
Access the matrix entry at (n,m).
Definition tuples.hh:168
NT & operator()(const uint n, const uint m)
Access the matrix entry at (n,m).
Definition tuples.hh:163
SimpleMatrix()
Definition tuples.hh:158
std::array< NT, N *M > data
Definition tuples.hh:201
void clear()
Set all entries to zero.
Definition tuples.hh:173
bool is_finite() const
Definition tuples.hh:179
void print() const
Print the matrix to the console.
Definition tuples.hh:191
Concept for a named tuple.
Definition tuples.hh:21
Definition complex_math.hh:10
constexpr auto tuple_tail(const std::tuple< Head, Tail... > &t)
Definition tuples.hh:223
auto _jacobian_tuple(std::index_sequence< IDXs... >)
Definition tuples.hh:283
constexpr auto tuple_first(const std::tuple< Head, Tail... > &t)
Definition tuples.hh:247
auto _jacobian_2_tuple(std::index_sequence< IDXs... >)
Definition tuples.hh:293
auto local_sol_q(const std::array< T, N > &a, uint q_index)
Definition tuples.hh:278
auto jacobian_tuple()
Definition tuples.hh:288
auto vector_to_tuple_helper(const std::vector< T > &v, std::index_sequence< Indices... >)
Definition tuples.hh:213
auto _local_sol_tuple(const std::array< T, N > &a, std::index_sequence< IDXs... >, uint q_index)
Definition tuples.hh:274
auto vector_to_tuple(const std::vector< T > &v)
Definition tuples.hh:217
consteval bool strings_equal(FixedString< N1 > s1, FixedString< N2 > s2)
Definition fixed_string.hh:49
auto jacobian_2_tuple()
Definition tuples.hh:299
constexpr auto & get(named_tuple< tuple_type, strSet > &ob)
get a reference to the element with the given name
Definition tuples.hh:111
std::array< NT, n > vector_to_array(const Vector &v)
Definition tuples.hh:204
unsigned int uint
Definition utils.hh:24
constexpr auto tuple_last(const std::tuple< Head, Tail... > &t)
Definition tuples.hh:233
A fixed size compile-time string.
Definition fixed_string.hh:12
Definition tuples.hh:34
static constexpr size_t size
Definition tuples.hh:35
static constexpr std::array< const char *, size > names
Definition tuples.hh:36
A class to store a tuple with elements that can be accessed by name. The names are stored as FixedStr...
Definition tuples.hh:56
static constexpr size_t size
Definition tuples.hh:63
static constexpr auto as(std::tuple< T... > &tup)
Definition tuples.hh:83
named_tuple(tuple_type &&t)
Definition tuples.hh:76
static constexpr auto names
Definition tuples.hh:64
const auto & get() const
Definition tuples.hh:104
static constexpr auto as(std::tuple< T... > &&tup)
Definition tuples.hh:79
auto & get()
Definition tuples.hh:103
tuple_type tuple
Definition tuples.hh:59
static consteval size_t get_idx(const char *name)
Definition tuples.hh:88
named_tuple(tuple_type &t)
Definition tuples.hh:77