ryujin 2.1.1 revision ee5cbcbf2346c1299c942d0e1f13b46449973c18
Loading...
Searching...
No Matches
convenience_macros.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2020 - 2026 by the ryujin authors
4//
5
6#pragma once
7
8#include <compile_time_options.h>
9
10#include <deal.II/base/function.h>
11#include <deal.II/lac/vector.h>
12
13namespace ryujin
14{
15#ifndef DOXYGEN
16 namespace
17 {
18 template <int dim, typename Number, typename Callable>
19 class ToFunction : public dealii::Function<dim, Number>
20 {
21 public:
22 ToFunction(const Callable &callable, const unsigned int k)
23 : dealii::Function<dim, Number>(1)
24 , callable_(callable)
25 , k_(k)
26 {
27 }
28
29 Number value(const dealii::Point<dim> &point,
30 unsigned int /*component*/) const override
31 {
32 return callable_(point)[k_];
33 }
34
35 private:
36 const Callable callable_;
37 const unsigned int k_;
38 };
39
40
41 template <int dim, typename Number, typename Callable>
42 class ToVectorFunction : public dealii::Function<dim, Number>
43 {
44 public:
45 ToVectorFunction(const Callable &callable, const unsigned int components)
46 : dealii::Function<dim, Number>(components)
47 , callable_(callable)
48 {
49 }
50
51 Number value(const dealii::Point<dim> &point,
52 unsigned int component) const override
53 {
54 return callable_(point)[component];
55 }
56
57 void vector_value(const dealii::Point<dim> &point,
58 dealii::Vector<double> &values) const override
59 {
60 AssertDimension(values.size(), this->n_components);
61
62 const auto temp = callable_(point);
63 for (unsigned int k = 0; k < this->n_components; ++k)
64 values(k) = temp[k];
65 }
66
67 private:
68 const Callable callable_;
69 };
70 } // namespace
71#endif
72
78
79
103 template <int dim, typename Number, typename Callable>
104 ToFunction<dim, Number, Callable> to_function(const Callable &callable,
105 const unsigned int k)
106 {
107 return {callable, k};
108 }
109
110
132 template <int dim, typename Number, typename Callable>
133 ToVectorFunction<dim, Number, Callable>
134 to_vector_function(const Callable &callable, const unsigned int n_components)
135 {
136 return {callable, n_components};
137 }
138
139
143 template <typename FT,
144 int problem_dim = FT::dimension,
145 typename TT = typename FT::value_type,
146 typename T = typename TT::value_type>
147 DEAL_II_HOST_DEVICE_ALWAYS_INLINE dealii::Tensor<1, problem_dim, T>
148 contract(const FT &flux_ij, const TT &c_ij)
149 {
150 dealii::Tensor<1, problem_dim, T> result;
151 for (unsigned int k = 0; k < problem_dim; ++k)
152 result[k] = flux_ij[k] * c_ij;
153 return result;
154 }
155
156
160 template <typename FT, int problem_dim = FT::dimension>
161 DEAL_II_HOST_DEVICE_ALWAYS_INLINE FT add(const FT &flux_left_ij,
162 const FT &flux_right_ij)
163 {
164 FT result;
165 for (unsigned int k = 0; k < problem_dim; ++k)
166 result[k] = flux_left_ij[k] + flux_right_ij[k];
167 return result;
168 }
169
171} // namespace ryujin
172
173
174#ifndef DOXYGEN
175namespace
176{
177 template <typename T>
178 class is_dereferenceable
179 {
180 template <typename C>
181 static auto test(...) -> std::false_type;
182
183 template <typename C>
184 static auto test(C *) -> decltype(*std::declval<C>(), std::true_type());
185
186 public:
187 using type = decltype(test<T>(nullptr));
188 static constexpr auto value = type::value;
189 };
190
191 template <typename T, typename>
192 auto dereference(T &t) -> decltype(dereference(*t)) &;
193
194 template <typename T>
195 auto dereference(T &t) -> T &
196 requires(!is_dereferenceable<T>::value)
197 {
198 return t;
199 }
200
201 template <typename T>
202 auto dereference(T &t) -> decltype(*t) &
203 requires is_dereferenceable<T>::value
204 {
205 return *t;
206 }
207} /* anonymous namespace */
208#endif
209
214
229#define ACCESSOR_READ_ONLY(member) \
230 inline const auto &member() const \
231 { \
232 return dereference(member##_); \
233 }
234
235
241#define ACCESSOR(member) \
242 inline auto &member() \
243 { \
244 return dereference(member##_); \
245 }
246
247
254#define ACCESSOR_READ_ONLY_NO_DEREFERENCE(member) \
255 inline const auto &member() const \
256 { \
257 return member##_; \
258 }
259
260
272#define ACCESSOR_CONTAINER_READ_ONLY(container, member) \
273 inline const auto &member() const \
274 { \
275 return dereference(dereference(container).member); \
276 }
277
278
280
284
292#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || \
293 defined(__SYCL_DEVICE_ONLY__)
294#define RYUJIN_DEVICE_COMPILATION_PASS
295#endif
296
297
304#define RYUJIN_PRAGMA(x) _Pragma(#x)
305
306
321#define RYUJIN_LIKELY(x) (__builtin_expect(!!(x), 1))
322
323
338#define RYUJIN_UNLIKELY(x) (__builtin_expect(!!(x), 0))
339
340
346#define ASM_LABEL(label) asm("#" label);
347
ToVectorFunction< dim, Number, Callable > to_vector_function(const Callable &callable, const unsigned int n_components)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE dealii::Tensor< 1, problem_dim, T > contract(const FT &flux_ij, const TT &c_ij)
ToFunction< dim, Number, Callable > to_function(const Callable &callable, const unsigned int k)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)