Dune-Fufem 2.12-git
Loading...
Searching...
No Matches
tensors.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE-FUFEM Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUFEM_FORMS_TENSORS_HH
8#define DUNE_FUFEM_FORMS_TENSORS_HH
9
10#include <array>
11#include <cstddef>
12#include <type_traits>
13#include <utility>
14
19
22
23
24namespace Dune::Fufem::Forms::Impl::Tensor {
25
26
27
28 // Class to tag tensor implementations
29 template<class T>
30 struct IsTensor : public std::false_type
31 {};
32
33 // Class to tag tensor with lazy evaluation
34 template<class T>
35 struct IsLazyTensor : public std::false_type
36 {};
37
38
39
40 // This forwards operator()(i,...) to subsequent operator[](i) calls
41 template<class T, std::size_t k>
42 class TensorView
43 {
44 T& t_;
45
46 static decltype(auto) resolveIndices(auto&& t)
47 {
48 return t;
49 }
50
51 static decltype(auto) resolveIndices(auto&& t, const auto& i0, const auto&... i)
52 {
53 return resolveIndices(t[i0], i...);
54 }
55
56 template<class I, I... i>
57 static decltype(auto) resolveIndexSequence(auto&&t, std::integer_sequence<I, i...>)
58 {
59 return resolveIndices(t, i...);
60 }
61
62 public:
63
65
66 static constexpr Dune::index_constant<k> rank()
67 {
68 return {};
69 }
70
71 TensorView(T& t, Dune::index_constant<k>)
72 : t_(t)
73 {}
74
75 decltype(auto) operator()(const auto&... i) const
76 {
77 return resolveIndices(t_, i...);
78 }
79 };
80
81 template<class T, std::size_t k>
82 struct IsTensor<TensorView<T, k>> : public std::true_type
83 {};
84
85
86
87 // This wraps a T and makes it available using k calls to operator[]
88 template<class T, std::size_t k>
89 requires(k>0)
90 class LiftRank
91 {
92 using RawT = Dune::ResolveRef_t<T>;
93 T t_;
94 public:
95
96 using value_type = typename RawT::value_type;
97
98 static constexpr Dune::index_constant<RawT::rank()+k> rank()
99 {
100 return {};
101 }
102
103 LiftRank(T t, Dune::index_constant<k>)
104 : t_(t)
105 {}
106
107 template<class I0, class... I>
108 requires (k==1)
109 decltype(auto) operator()(const I0& i0, const I&... i) const
110 {
111 return Dune::resolveRef(t_)(i...);
112 }
113
114 template<class I0, class I1, class... I>
115 requires (k==2)
116 decltype(auto) operator()(const I0& i0, const I1& i1, const I&... i) const
117 {
118 return Dune::resolveRef(t_)(i...);
119 }
120 };
121
122 template<class T, std::size_t k>
123 struct IsTensor<LiftRank<T, k>> : public std::true_type
124 {};
125
126
127
128 // This wraps flips the first k indices between 0 and 1
129 template<class T, std::size_t k>
130 class FlipIndices
131 {
132 using RawT = Dune::ResolveRef_t<T>;
133 T t_;
134 public:
135
136 using value_type = typename RawT::value_type;
137
138 static constexpr Dune::index_constant<RawT::rank()> rank()
139 {
140 return {};
141 }
142
143 FlipIndices(T t, Dune::index_constant<k>)
144 : t_(t)
145 {}
146
147 template<class I0, class... I>
148 requires (k==1)
149 decltype(auto) operator()(const I0& i0, const I&... i) const
150 {
151 return Dune::resolveRef(t_)(not i0, i...);
152 }
153
154 template<class I0, class I1, class... I>
155 requires (k==2)
156 decltype(auto) operator()(const I0& i0, const I1& i1, const I&... i) const
157 {
158 return Dune::resolveRef(t_)(not i0, not i1, i...);
159 }
160 };
161
162 template<class T, std::size_t k>
163 struct IsTensor<FlipIndices<T, k>> : public std::true_type
164 {};
165
166
167
168 template<class Value>
169 class RankZeroTensor
170 {
171 public:
172
173 using value_type = Value;
174
175 static constexpr Dune::index_constant<0> rank()
176 {
177 return {};
178 }
179
180 RankZeroTensor(Value value)
181 : value_(std::move(value))
182 {}
183
184 template<class F>
185 friend void sparseForEach(const RankZeroTensor& tensor, F&& f)
186 {
187 f(tensor.value_);
188 }
189
190 template<class Outer>
191 friend auto compose(const Outer& outer, const RankZeroTensor& inner)
192 {
193 return Dune::Fufem::Forms::Impl::Tensor::RankZeroTensor(outer(inner()));
194 }
195
196 static constexpr std::size_t nnz()
197 {
198 return 1;
199 }
200
201 // Special interface for rank=0
202 const Value& operator()() const
203 {
204 return value_;
205 }
206
207 protected:
208 Value value_;
209 };
210
211 template<class Value>
212 struct IsTensor<RankZeroTensor<Value>> : public std::true_type
213 {};
214
215
216
217 template<std::size_t r, class Value, class ForEach>
218 class SparseTensor
219 {
220 public:
221
222 using value_type = std::decay_t<Value>;
223
224 static constexpr Dune::index_constant<r> rank()
225 {
226 return {};
227 }
228
229 SparseTensor(Dune::index_constant<r>, ForEach forEach, std::size_t nnz, Dune::MetaType<Value>)
230 : forEach_(std::move(forEach))
231 , nnz_(nnz)
232 {}
233
234 template<class F>
235 friend void sparseForEach(const SparseTensor& tensor, F&& f)
236 {
237 tensor.forEach_(f);
238 }
239
240 template<class Outer>
241 friend auto compose(const Outer& outer, const SparseTensor& inner)
242 {
244 return Dune::Fufem::Forms::Impl::Tensor::SparseTensor(
245 inner.rank(),
246 [outer, innerForEach=inner.forEach_](auto&& f) {
247 innerForEach([&](const auto& y_i, auto... i) {
248 f(outer(y_i), i...);
249 });
250 },
251 inner.nnz(),
253 );
254 }
255
256 std::size_t nnz() const
257 {
258 return nnz_;
259 }
260
261 protected:
262 ForEach forEach_;
263 std::size_t nnz_;
264 };
265
266 template<std::size_t r, class Value, class ForEach>
267 struct IsTensor<SparseTensor<r, Value, ForEach>> : public std::true_type
268 {};
269
270 template<std::size_t r, class Value, class ForEach>
271 struct IsLazyTensor<SparseTensor<r, Value, ForEach>> : public std::is_rvalue_reference<Value>
272 {};
273
274
275
276 template<class Product, class T0, class T1>
277 class SparseProductTensor
278 {
279 public:
280
282
283 static constexpr Dune::index_constant<T0::rank() + T1::rank()> rank()
284 {
285 return {};
286 }
287
288 SparseProductTensor(Product product, T0 t0, T1 t1)
289 : product_(std::move(product))
290 , t0_(std::move(t0))
291 , t1_(std::move(t1))
292 {}
293
294 template<class F>
295 friend void sparseForEach(const SparseProductTensor& tensor, F&& f)
296 {
297 using namespace Dune::Indices;
298 if constexpr(T0::rank() == 0)
299 sparseForEach(tensor.t1_, [&](const auto& x1_j, auto... j) {
300 f(tensor.product_(tensor.t0_(), x1_j), j...);
301 });
302 else if constexpr(T1::rank() == 0)
303 sparseForEach(tensor.t0_, [&](const auto& x0_i, auto... i) {
304 f(tensor.product_(x0_i, tensor.t1_()), i...);
305 });
306 else if constexpr((T0::rank() == 2) and (T1::rank() == 2))
307 sparseForEach(tensor.t0_, [&](const auto& x0_i, auto i0, auto i1) {
308 sparseForEach(tensor.t1_, [&](const auto& x1_j, auto j0, auto j1) {
309 f(tensor.product_(x0_i, x1_j), i0, j0, i1, j1);
310 });
311 });
312 else
313 static_assert(Dune::AlwaysFalse<F>::value, "Only tensors of rank 0,2, and 4 are supported.");
314 }
315
316 template<class Outer>
317 friend auto compose(const Outer& outer, const SparseProductTensor& inner)
318 {
319 using namespace Dune::Indices;
320 return Dune::Fufem::Forms::Impl::Tensor::SparseProductTensor(
321 [outer, innerProduct=inner.product()](const auto&... args) { return outer(innerProduct(args...)); },
322 inner.factor(_0),
323 inner.factor(_1)
324 );
325 }
326
327 std::size_t nnz() const
328 {
329 return t0_.nnz() * t1_.nnz();
330 }
331
332 // Special interface for product tensor
333 const Product& product() const
334 {
335 return product_;
336 }
337
338 template<std::size_t k>
339 const auto& factor(Dune::index_constant<k> = {}) const
340 {
341 if constexpr(k==0)
342 return t0_;
343 else if constexpr(k==1)
344 return t1_;
345 }
346
347 protected:
348 Product product_;
349 T0 t0_;
350 T1 t1_;
351 };
352
353 template<class Product, class T0, class T1>
354 struct IsTensor<SparseProductTensor<Product, T0, T1>> : public std::true_type
355 {};
356
357 template<class Product, class T0, class T1>
358 struct IsLazyTensor<SparseProductTensor<Product, T0, T1>> : public std::true_type
359 {};
360
361
362
363 template<class Product, class X, class Y>
364 requires (IsTensor<X>::value and IsTensor<Y>::value)
365 auto interleavedOuterProduct(Product product, const X& x, const Y& y)
366 {
367 using namespace Dune::Indices;
368 if constexpr((X::rank() == 0) and (Y::rank() == 0))
369 return RankZeroTensor(product(x(), y()));
370 else
371 return SparseProductTensor(product, x, y);
372 }
373
374
375
376 template<class K, std::size_t rank, class Value, class ForEach, class Y>
377 requires(
379 and IsTensor<Y>::value
380 and (rank == Y::rank())
381 )
382 void axpy(const K& alpha, const SparseTensor<rank, Value, ForEach>& x, Y& y)
383 {
384 sparseForEach(x, [&](const auto& xi, auto...i) {
385 y(i...) += Dune::Fufem::asNumber(alpha*xi);
386 });
387 }
388
389 template<class K, class Product, class T0, class T1, class Y>
390 requires(
392 and IsTensor<Y>::value
393 and (SparseProductTensor<Product, T0, T1>::rank() == Y::rank())
394 )
395 void axpy(const K& alpha, const SparseProductTensor<Product, T0, T1>& x, Y& y)
396 {
397 using namespace Dune::Indices;
399 // Since x.product() is a bilinear we can factor out
400 // the multiplication with alpha.
401 if constexpr(T0::rank() == 0)
402 sparseForEach(x.factor(_1), [&](const auto& x1_j, auto... j) {
403 y(j...) += Dune::Fufem::asNumber(x.product()(mult(alpha,x.factor(_0)()), x1_j));
404 });
405 else if constexpr(T1::rank() == 0)
406 sparseForEach(x.factor(_0), [&](const auto& x0_i, auto... i) {
407 y(i...) += Dune::Fufem::asNumber(x.product()(x0_i, mult(alpha,x.factor(_1)())));
408 });
409 else if constexpr((T0::rank() == 2) and (T1::rank() == 2))
410 sparseForEach(x.factor(_0), [&](const auto& x0_i, auto i0, auto i1) {
411 auto x0_i_alpha = mult(x0_i,alpha);
412 sparseForEach(x.factor(_1), [&](const auto& x1_j, auto j0, auto j1) {
413 y(i0, j0, i1, j1) += Dune::Fufem::asNumber(x.product()(x0_i_alpha, x1_j));
414 });
415 });
416 else
417 static_assert((T0::rank() == 2) and (T1::rank() == 2));
418 }
419
420} // namespace Dune::Fufem::Forms::Impl::Tensor
421
422
423
424#endif // DUNE_FUFEM_FORMS_TENSORS_HH
void axpy(const Ta &a, const type &y)
double alpha() const
auto operator()(T &&t) -> decltype(this->apply(t, std::index_sequence_for< Args... >{})) const
constexpr void forEach(Range &&range, F &&f)
constexpr T & resolveRef(T &gf) noexcept
virtual void operator()()=0
T forward(T... args)
auto product(const Op &op, const L &l, const R &r)
Generic exterior product of two multilinear operators.
Definition userfunctions.hh:401
auto compose(const OuterOp &outerOp, const InnerOp &innerOp)
Generic composition of a multilinear operators with a pointwise outer operator.
Definition userfunctions.hh:481
T move(T... args)
const T & asNumber(const T &t)
Forward given value as number.
Definition asnumber.hh:23
STL namespace.
Definition localoperators.hh:401