ryujin 2.1.1 revision 0db9b0a79dccad7f3238d57174dbcd4e2d7543a5
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
12namespace ryujin
13{
14#ifndef DOXYGEN
15 namespace
16 {
17 template <int dim, typename Number, typename Callable>
18 class ToFunction : public dealii::Function<dim, Number>
19 {
20 public:
21 ToFunction(const Callable &callable, const unsigned int k)
22 : dealii::Function<dim, Number>(1)
23 , callable_(callable)
24 , k_(k)
25 {
26 }
27
28 Number value(const dealii::Point<dim> &point,
29 unsigned int /*component*/) const override
30 {
31 return callable_(point)[k_];
32 }
33
34 private:
35 const Callable callable_;
36 const unsigned int k_;
37 };
38 } // namespace
39#endif
40
46
47
71 template <int dim, typename Number, typename Callable>
72 ToFunction<dim, Number, Callable> to_function(const Callable &callable,
73 const unsigned int k)
74 {
75 return {callable, k};
76 }
77
78
82 template <typename FT,
83 int problem_dim = FT::dimension,
84 typename TT = typename FT::value_type,
85 typename T = typename TT::value_type>
86 DEAL_II_HOST_DEVICE_ALWAYS_INLINE dealii::Tensor<1, problem_dim, T>
87 contract(const FT &flux_ij, const TT &c_ij)
88 {
89 dealii::Tensor<1, problem_dim, T> result;
90 for (unsigned int k = 0; k < problem_dim; ++k)
91 result[k] = flux_ij[k] * c_ij;
92 return result;
93 }
94
95
99 template <typename FT, int problem_dim = FT::dimension>
100 DEAL_II_HOST_DEVICE_ALWAYS_INLINE FT add(const FT &flux_left_ij,
101 const FT &flux_right_ij)
102 {
103 FT result;
104 for (unsigned int k = 0; k < problem_dim; ++k)
105 result[k] = flux_left_ij[k] + flux_right_ij[k];
106 return result;
107 }
108
110} // namespace ryujin
111
112
113#ifndef DOXYGEN
114namespace
115{
116 template <typename T>
117 class is_dereferenceable
118 {
119 template <typename C>
120 static auto test(...) -> std::false_type;
121
122 template <typename C>
123 static auto test(C *) -> decltype(*std::declval<C>(), std::true_type());
124
125 public:
126 using type = decltype(test<T>(nullptr));
127 static constexpr auto value = type::value;
128 };
129
130 template <typename T, typename>
131 auto dereference(T &t) -> decltype(dereference(*t)) &;
132
133 template <typename T>
134 auto dereference(T &t) -> T &
135 requires(!is_dereferenceable<T>::value)
136 {
137 return t;
138 }
139
140 template <typename T>
141 auto dereference(T &t) -> decltype(*t) &
142 requires is_dereferenceable<T>::value
143 {
144 return *t;
145 }
146} /* anonymous namespace */
147#endif
148
153
168#define ACCESSOR_READ_ONLY(member) \
169 inline const auto &member() const \
170 { \
171 return dereference(member##_); \
172 }
173
174
180#define ACCESSOR(member) \
181 inline auto &member() \
182 { \
183 return dereference(member##_); \
184 }
185
186
193#define ACCESSOR_READ_ONLY_NO_DEREFERENCE(member) \
194 inline const auto &member() const \
195 { \
196 return member##_; \
197 }
198
199
211#define ACCESSOR_CONTAINER_READ_ONLY(container, member) \
212 inline const auto &member() const \
213 { \
214 return dereference(dereference(container).member); \
215 }
216
217
219
223
231#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || \
232 defined(__SYCL_DEVICE_ONLY__)
233#define RYUJIN_DEVICE_COMPILATION_PASS
234#endif
235
236
243#define RYUJIN_PRAGMA(x) _Pragma(#x)
244
245
260#define RYUJIN_UNLIKELY(x) (__builtin_expect(!!(x), 0))
261
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)