ryujin 2.1.1 revision feb53359f0c9a08baf43c3dfe847d8a9f7d6893a
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 - 2023 by the ryujin authors
4//
5
6#pragma once
7
8#include <deal.II/base/function.h>
9
14
15namespace ryujin
16{
17#ifndef DOXYGEN
18 namespace
19 {
20 template <int dim, typename Number, typename Callable>
21 class ToFunction : public dealii::Function<dim, Number>
22 {
23 public:
24 ToFunction(const Callable &callable, const unsigned int k)
25 : dealii::Function<dim, Number>(1)
26 , callable_(callable)
27 , k_(k)
28 {
29 }
30
31 Number value(const dealii::Point<dim> &point,
32 unsigned int /*component*/) const override
33 {
34 return callable_(point)[k_];
35 }
36
37 private:
38 const Callable callable_;
39 const unsigned int k_;
40 };
41 } // namespace
42#endif
43
44
67 template <int dim, typename Number, typename Callable>
68 ToFunction<dim, Number, Callable> to_function(const Callable &callable,
69 const unsigned int k)
70 {
71 return {callable, k};
72 }
73
74
78 template <typename FT,
79 int problem_dim = FT::dimension,
80 typename TT = typename FT::value_type,
81 typename T = typename TT::value_type>
82 DEAL_II_ALWAYS_INLINE inline dealii::Tensor<1, problem_dim, T>
83 contract(const FT &flux_ij, const TT &c_ij)
84 {
85 dealii::Tensor<1, problem_dim, T> result;
86 for (unsigned int k = 0; k < problem_dim; ++k)
87 result[k] = flux_ij[k] * c_ij;
88 return result;
89 }
90
91
95 template <typename FT, int problem_dim = FT::dimension>
96 DEAL_II_ALWAYS_INLINE inline FT add(const FT &flux_left_ij,
97 const FT &flux_right_ij)
98 {
99 FT result;
100 for (unsigned int k = 0; k < problem_dim; ++k)
101 result[k] = flux_left_ij[k] + flux_right_ij[k];
102 return result;
103 }
104
105
106} // namespace ryujin
107
108
119#define AssertThrowSIMD(variable, condition, exception) \
120 if constexpr (std::is_same< \
121 typename std::remove_const<decltype(variable)>::type, \
122 double>::value || \
123 std::is_same< \
124 typename std::remove_const<decltype(variable)>::type, \
125 float>::value) { \
126 AssertThrow(condition(variable), exception); \
127 } else { \
128 for (unsigned int k = 0; k < decltype(variable)::size(); ++k) { \
129 AssertThrow(condition((variable)[k]), exception); \
130 } \
131 }
132
133#ifndef DOXYGEN
134namespace
135{
136 template <typename T>
137 class is_dereferenceable
138 {
139 template <typename C>
140 static auto test(...) -> std::false_type;
141
142 template <typename C>
143 static auto test(C *) -> decltype(*std::declval<C>(), std::true_type());
144
145 public:
146 using type = decltype(test<T>(nullptr));
147 static constexpr auto value = type::value;
148 };
149
150 template <typename T, typename>
151 auto dereference(T &t) -> decltype(dereference(*t)) &;
152
153 template <
154 typename T,
155 typename = typename std::enable_if<!is_dereferenceable<T>::value>::type>
156 auto dereference(T &t) -> T &
157 {
158 return t;
159 }
160
161 template <
162 typename T,
163 typename = typename std::enable_if<is_dereferenceable<T>::value>::type>
164 auto dereference(T &t) -> decltype(*t) &
165 {
166 return *t;
167 }
168} /* anonymous namespace */
169#endif
170
185#define ACCESSOR_READ_ONLY(member) \
186 inline auto &member() const \
187 { \
188 return dereference(member##_); \
189 }
190
191
197#define ACCESSOR(member) \
198 inline auto &member() \
199 { \
200 return dereference(member##_); \
201 }
202
203
210#define ACCESSOR_READ_ONLY_NO_DEREFERENCE(member) \
211 inline const auto &member() const \
212 { \
213 return member##_; \
214 }
215
216
222#define ASM_LABEL(label) asm("#" label);
223
ToFunction< dim, Number, Callable > to_function(const Callable &callable, const unsigned int k)
DEAL_II_ALWAYS_INLINE FT add(const FT &flux_left_ij, const FT &flux_right_ij)
DEAL_II_ALWAYS_INLINE dealii::Tensor< 1, problem_dim, T > contract(const FT &flux_ij, const TT &c_ij)