AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Basic.hpp
1#pragma once
2
3#include <algorithm>
4#include <utility>
5
6#include <amdis/common/Index.hpp>
7#include <amdis/common/ConceptsBase.hpp>
8
9namespace AMDiS
10{
11 namespace Concepts
12 {
13 namespace Definition
14 {
16 {
17 template <class F, int... I>
18 auto require(F&& f, std::integer_sequence<int,I...>) -> decltype( order(f, I...) );
19 };
20 }
21
22 template <class F, int N>
23 constexpr bool HasFunctorOrder = models<Definition::HasFunctorOrder(F, std::make_integer_sequence<int,N>)>;
24
25 template <class F, int N>
26 using HasFunctorOrder_t = bool_t<HasFunctorOrder<F,N>>;
27 }
28
29 namespace Operation
30 {
36 template <class T, T value>
38 {
39 template <class... Ts>
40 constexpr T operator()(Ts const&... /*args*/) const
41 {
42 return value;
43 }
44 };
45
48
49 template <class T, T value, class... Int>
50 constexpr int order(StaticConstant<T,value> const&, Int... /*orders*/)
51 {
52 return 0;
53 }
54
55 template <class T, T value, std::size_t J>
56 constexpr auto partial(StaticConstant<T,value> const&, index_t<J>)
57 {
58 return Zero{};
59 }
60
61 // -------------------------------------------------------------------------
62
64 struct Id
65 {
66 template <class T>
67 constexpr T const& operator()(T const& x) const
68 {
69 return x;
70 }
71
72 friend constexpr int order(Id const&, int d)
73 {
74 return d;
75 }
76
77 friend auto partial(Id const&, index_t<0>)
78 {
79 return One{};
80 }
81 };
82
83 // -------------------------------------------------------------------------
84
86 template <class T>
87 struct Constant
88 {
89 constexpr Constant(T value)
90 : value_(value)
91 {}
92
93 template <class... Ts>
94 constexpr T const& operator()(Ts const&... /*args*/) const
95 {
96 return value_;
97 }
98
99 private:
100 T value_;
101 };
102
103 template <class T, class... Int>
104 constexpr int order(Constant<T> const&, Int... /*orders*/)
105 {
106 return 0;
107 }
108
109 template <class T, std::size_t J>
110 constexpr auto partial(Constant<T> const&, index_t<J>)
111 {
112 return Zero{};
113 }
114
115 // -------------------------------------------------------------------------
116
118 template <class T>
120 {
121 constexpr Reference(T& value)
122 : value_(&value)
123 {}
124
125 template <class... Ts>
126 constexpr T const& operator()(Ts const&... /*args*/) const
127 {
128 return *value_;
129 }
130
131 private:
132 T* value_;
133 };
134
135 template <class T, class... Int>
136 constexpr int order(Reference<T> const&, Int... /*orders*/)
137 {
138 return 0;
139 }
140
141 template <class T, std::size_t J>
142 constexpr auto partial(Reference<T> const&, index_t<J>)
143 {
144 return Zero{};
145 }
146
147 // -------------------------------------------------------------------------
148
149
150 template <class T0, class... Ts>
151 inline constexpr decltype(auto) get_element(index_t<0>, T0&& t0, Ts&&... /*ts*/)
152 {
153 return FWD(t0);
154 }
155
156 template <std::size_t J, class T0, class... Ts>
157 inline constexpr decltype(auto) get_element(index_t<J>, T0&& /*t0*/, Ts&&... ts)
158 {
159 static_assert(J <= sizeof...(Ts), "");
160 return get_element(index_t<J-1>{}, FWD(ts)...);
161 }
162
163 template <std::size_t I>
164 struct Arg
165 {
166 template <class... Ts>
167 constexpr auto&& operator()(Ts&&... args) const
168 {
169 return get_element(index_t<I>{}, FWD(args)...);
170 }
171 };
172
173 template <std::size_t I, class... Int>
174 constexpr int order(Arg<I> const&, Int... orders)
175 {
176 return get_element(index_t<I>{}, orders...);
177 }
178
179 template <std::size_t I, std::size_t J>
180 constexpr auto partial(Arg<I> const&, index_t<J>)
181 {
182 return StaticConstant<int,(I==J ? 1 : 0)>{};
183 }
184
185 // -------------------------------------------------------------------------
186
187 template <std::size_t I>
188 struct Get
189 {
190 template <class T, int N>
191 constexpr T const& operator()(Dune::FieldVector<T,N> const& vec) const
192 {
193 return vec[I];
194 }
195
196 friend constexpr int order(Get const&, int d)
197 {
198 return d;
199 }
200 };
201
202 struct Get_
203 {
204 explicit constexpr Get_(std::size_t i)
205 : i_(i)
206 {}
207
208 template <class T, int N>
209 constexpr T const& operator()(Dune::FieldVector<T,N> const& vec) const
210 {
211 return vec[i_];
212 }
213
214 friend constexpr int order(Get_ const&, int d)
215 {
216 return d;
217 }
218
219 std::size_t i_;
220 };
221
224 } // end namespace Operation
225} // end namespace AMDiS
Definition: Basic.hpp:165
Functor representing a constant value.
Definition: Basic.hpp:88
Definition: Basic.hpp:203
Definition: Basic.hpp:189
(Unary-)Functor representing the identity
Definition: Basic.hpp:65
Functor representing a constant value.
Definition: Basic.hpp:120
Functor representing a static constant value.
Definition: Basic.hpp:38