KASKADE 7 development version
Loading...
Searching...
No Matches
linearspace.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2023 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef LINEARSPACE_HH
14#define LINEARSPACE_HH
15
16#include <cmath>
17#include <type_traits>
18
19#include <boost/version.hpp>
20#include <boost/fusion/algorithm.hpp>
21#include <boost/fusion/sequence.hpp>
22
23#include "fem/functionspace.hh"
24
25#include "linalg/crsutil.hh"
26
27namespace Kaskade
28{
30 // forward declaration
31 template <class> class VariableSet;
32
34 {
35
36 template <class Space0, class Space2, int id> struct Copy;
37
38 template <class Space, int id, class VSDescriptions>
39 struct Copy<VariableSet<VSDescriptions>,Space,id>
40 {
41 static void apply(const VariableSet<VSDescriptions>& from, Space& to)
42 {
43 boost::fusion::at_c<id>(to.data) = boost::fusion::at_c<id>(from.data).coefficients();
44 Copy<VariableSet<VSDescriptions>,Space,id-1>::apply(from,to);
45 }
46 };
47
48 template <class Space, class VSDescriptions>
49 struct Copy<VariableSet<VSDescriptions>,Space,0>
50 {
51 static void apply(const VariableSet<VSDescriptions>& from, Space& to)
52 {
53 boost::fusion::at_c<0>(to.data) = boost::fusion::at_c<0>(from.data).coefficients();
54 }
55 };
56 }
57
58
75 template <class Scalar_, class Seq>
77 {
78 private:
80
81 // forward declarations (definitions at end of file)
82 struct Add;
83 struct Sub;
84 struct Assign;
85 struct ScalarMult;
86 struct Axpy;
87 struct ScalarProduct;
88 template <typename> struct ReadBlock;
89 template <typename> struct WriteBlock;
90
91 public:
93 typedef Scalar_ Scalar;
94 typedef Scalar field_type; // for compatibility with Dune conventions
95
97 typedef Seq Sequence;
98 typedef Seq Functions;
99
103 template <int m>
104 using Component = std::remove_reference_t<typename boost::fusion::result_of::at_c<Sequence,m>::type>;
105
106
107 // use with care: default constructed subvectors may not be properly initialized (e.g. size?)
109
116
121
123 template <class VSDescriptions>
125 {
126 *this = y;
127 }
128
135 template <class S>
136 explicit LinearProductSpace(S const& init): data(init)
137 {}
138
139 template <typename... Args>
140 explicit LinearProductSpace(boost::fusion::transform_view<Args...> const& data_) : data(data_)
141 {
142 applyUnaryOp(Assign(0));
143 }
144
148
154 size_t dim() const
155 {
156 return boost::fusion::accumulate(data,0,[](auto dim, auto const& v) { return dim+v.dim(); });
157 }
158
163
165 Self& operator=(Self const& y) { if (this!=&y) data = y.data; return *this; }
166
168 template <class VSDescriptions>
170 {
171 using namespace boost::fusion;
172 static_assert(result_of::size<Sequence>::type::value == VSDescriptions::noOfVariables,"Numbers of variables do not match.");
174 result_of::size<typename VSDescriptions::RepresentationData>::type::value-1>::apply(y,*this);
175 return *this;
176 } // need not check for self assignment here
177
179 template <class OtherSeq>
181 {
182 using namespace boost::fusion;
183 static_assert(result_of::size<Sequence>::type::value == result_of::size<OtherSeq>::type::value, "Numbers of variables do not match.");
184 data = y.data;
185 return *this;
186 } // need not check for self assignment here
187
189 template <class FunctionSpace, int m>
191 {
192 data = fse.coefficients();
193 return *this;
194 }
195
198 {
199 return applyUnaryOp(Assign(a));
200 }
201
203 template <class First, class Last>
204 Self& operator=(boost::fusion::iterator_range<First,Last> range)
205 {
206 static_assert(boost::fusion::result_of::size<Sequence>::type::value == boost::fusion::result_of::distance<First,Last>::type::value,
207 "Numbers of variables do not match.");
208 data = range;
209 return *this;
210 }
211
213 Self& operator*=(Scalar a) { return applyUnaryOp(ScalarMult(a)); }
214
218
220 template <class SequenceY>
222 {
223 return applyBinaryOp(Add(),y);
224 }
225
227 Self& operator-=(Self const& y)
228 {
229 return applyBinaryOp(Sub(),y);
230 }
231
233 Self& axpy(Scalar a, Self const& y)
234 {
235 return applyBinaryOp(Axpy(a),y);
236 }
237
244 template <class InIterator>
245 [[deprecated]] void read(InIterator i)
246 {
247 vectorFromSequence(*this,i);
248 }
249
254 template <class InIterator>
255 void read(int rbegin, int rend, InIterator i)
256 { boost::fusion::for_each(data,ReadBlock<InIterator>(rbegin,rend,i)); }
257
264 template <class OutIterator>
265 [[deprecated]] void write(OutIterator i) const
266 {
267 vectorToSequence(*this,i);
268 }
269
274 template <class DataOutIter>
275 void write(int rbegin, int rend, DataOutIter i) const
276 {
277 boost::fusion::for_each(data,WriteBlock<DataOutIter>(rbegin,rend,i));
278 }
279
284
293 field_type operator*(Self const& y) const
294 {
295 using namespace boost::fusion;
296 zip_view<vector<Sequence const&,Sequence const&> > zipped(vector<Sequence const&,Sequence const&>(data,y.data));
297 field_type s = 0;
298 for_each(zipped,[&](auto const& p) { s += at_c<0>(p) * at_c<1>(p); });
299 return s;
300 }
301
303 field_type dot(Self const& y) const
304 {
305 return *this * y;
306 }
307
311 double two_norm2() const
312 {
313 return (*this)*(*this);
314 }
315
319 double two_norm() const
320 {
321 return std::sqrt(two_norm2());
322 }
323
327
330
331 private:
332
333 // applies a binary operator to the elements of a pair
334 template <class BinaryOp>
335 struct PairOp
336 {
337 PairOp(BinaryOp const& op_): op(op_) {}
338
339 template <class Pair> void operator()(Pair p) const {
340 op(boost::fusion::at_c<0>(p),boost::fusion::at_c<1>(p));
341 }
342
343 private:
344 BinaryOp const& op;
345 };
346
347 template <class UnaryOp>
348 Self& applyUnaryOp(UnaryOp const& op) {
349 boost::fusion::for_each(data,op);
350 return *this;
351 }
352
353 template <class BinaryOp, class SpaceY>
354 Self& applyBinaryOp(BinaryOp const& op, SpaceY const& y) {
355 using namespace boost::fusion;
356 typedef typename SpaceY::Sequence SequenceY;
357 zip_view<vector<Sequence&,SequenceY const&> > zipped(vector<Sequence&,SequenceY const&>(data,y.data));
358 for_each(zipped,PairOp<BinaryOp>(op));
359 return *this;
360 }
361
362 struct Add { template <class T, class S> void operator()(T& a, S const& b) const { a += b; } };
363 struct Sub { template <class T> void operator()(T& a, T const& b) const { a -= b; } };
364 struct Assign {
365 Assign(Scalar s_): s(s_) {}
366 template <class T> void operator()(T& a) const { a = s; }
367 private: Scalar s;
368 };
369
370
371 struct ScalarMult {
372 ScalarMult(Scalar s_): s(s_) {}
373 template <class Element> void operator()(Element& e) const { e *= s; }
374 private: Scalar s;
375 };
376
377 struct Axpy
378 {
379 Axpy(Scalar s_): s(s_) {}
380 template <class T> void operator()(T& a, T const& b) const {
381
382 // Funny runtime error could only be avoided by this funny code.
383 // Probably a compiler bug.
384 // assert(a.size()==b.size());
385 std::cout << "";
386
387
388 a.axpy(s,b);
389 }
390
391 private: Scalar s;
392 };
393
394 template <class DataOutIter>
395 struct WriteBlock
396 {
397 WriteBlock(int& rbegin_, int& rend_, DataOutIter& out_): rbegin(rbegin_), rend(rend_), out(out_) {}
398 template <class VectorBlock> void operator()(VectorBlock const& v) const
399 {
400 if (rbegin<=0 && rend>0)
401 vectorToSequence(v,out);
402 --rbegin;
403 --rend;
404 }
405 private:
406 int& rbegin;
407 int& rend;
408 DataOutIter& out;
409
410 };
411
412 template <class DataInIter>
413 struct ReadBlock
414 {
415 ReadBlock(int& rbegin_, int& rend_, DataInIter& in_): rbegin(rbegin_), rend(rend_), in(in_) {}
416
417 template <class VectorBlock> void operator()(VectorBlock& v) const
418 {
419 if (rbegin<=0 && rend>0)
420 vectorFromSequence(v,in);
421 --rbegin;
422 --rend;
423 }
424 private:
425 int& rbegin;
426 int& rend;
427 DataInIter& in;
428
429 };
430
431 };
432
433 // ----------------------------------------------------------------------------------------------
434
457 template <int m, class Scalar, class Sequence>
459 {
460 return boost::fusion::at_c<m>(x.data);
461 }
462
463 template <int m, class Scalar, class Sequence>
465 {
466 return boost::fusion::at_c<m>(x.data);
467 }
468
475 template <int m, class LinearProductSpace>
476 using Component = typename LinearProductSpace::template Component<m>;
477
478 // ----------------------------------------------------------------------------------------------
479
480
487 template <class X0, class ...Xs>
488 auto cartesianProduct(X0 const& x0, Xs const&... xs)
489 {
490 using Sequence = boost::fusion::vector<X0,Xs...>;
492 }
493
494 // ----------------------------------------------------------------------------------------------
495
496
502 template <class Scalar, class Seq, class OutIter>
504 {
505 boost::fusion::for_each(v.data,[&](auto& x) { i = vectorToSequence(x,i); });
506 return i;
507 }
508
514 template <class Scalar, class Seq, class InIter>
516 {
517 boost::fusion::for_each(v.data,[&](auto& x) { i = vectorFromSequence(x,i); });
518 return i;
519 }
520
521} // end of namespace Kaskade
522
523
524#endif
A class for representing finite element functions.
StorageType const & coefficients() const
Provides read-only access to the coefficients of the finite element basis functions.
Self & axpy(Scalar a, Self const &y)
this <- this + a*y
std::remove_reference_t< typename boost::fusion::result_of::at_c< Sequence, m >::type > Component
The type of the m-th component.
LinearProductSpace(S const &init)
Constructor with explicit data.
void read(int rbegin, int rend, InIterator i)
Reads the coefficients of the subrange [rbegin,rend[ sequentially from an input iterator....
LinearProductSpace(LinearProductSpace< Scalar, Sequence > &&y)
Move constructor.
Self & operator+=(LinearProductSpace< Scalar, SequenceY > const &y)
In place addition.
Self & operator=(boost::fusion::iterator_range< First, Last > range)
Assignment from a (hopefully compatible) boost::fusion iterator range.
void read(InIterator i)
DEPRECATED use vectorFromSequence instead.
Self & operator=(VariableSet< VSDescriptions > const &y)
Assignment from a different (hopefully compatible) type.
LinearProductSpace(boost::fusion::transform_view< Args... > const &data_)
Self & operator*=(Scalar a)
Scaling.
size_t dim() const
Number of scalar degrees of freedom. This computes the total number of degrees of freedom,...
LinearProductSpace(LinearProductSpace< Scalar, Sequence > const &y)
Copy constructor.
Self & operator=(FunctionSpaceElement< FunctionSpace, m > const &fse)
Assignment from a (hopefully compatible) FunctionSpaceElement.
field_type operator*(Self const &y) const
Scalar product.
Self & operator=(LinearProductSpace< Scalar, OtherSeq > const &y)
Assignment from a different (hopefully compatible) type.
Scalar_ Scalar
scalar type
Self & operator=(Scalar a)
Assignment to constant scalar.
void write(int rbegin, int rend, DataOutIter i) const
Writes the coefficients of the subrange [rbegin,rend[ sequentially to an output iterator....
field_type dot(Self const &y) const
Scalar product.
Self & operator-=(Self const &y)
In place subtraction.
double two_norm2() const
squared euclidean norm
LinearProductSpace(const VariableSet< VSDescriptions > &y)
Copy from VariableSet.
void write(OutIterator i) const
DEPRECATED use vectorToSequence instead.
Seq Sequence
boost::fusion::vector of element vectors.
double two_norm() const
euclidean norm
Self & operator=(Self const &y)
Assignment from the same type.
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Definition variables.hh:337
FEFunctionSpace and FunctionSpaceElement and the like.
OutIter vectorToSequence(LinearProductSpace< Scalar, Seq > const &v, OutIter i)
writes the coefficients of a vector to a flat scalar sequence <Scalar,Seq>
auto const & component(LinearProductSpace< Scalar, Sequence > const &x)
Provides access to the m-th component of a product space.
InIter vectorFromSequence(LinearProductSpace< Scalar, Seq > &v, InIter i)
reads the coefficients of a vector from a flat scalar sequence <Scalar,Seq>
auto cartesianProduct(X0 const &x0, Xs const &... xs)
Creates a cartesian product of the given vectors, i.e. a LinearProductSpace object.
auto & component(LinearProductSpace< Scalar, Sequence > &x)
static void apply(const VariableSet< VSDescriptions > &from, Space &to)
static void apply(const VariableSet< VSDescriptions > &from, Space &to)