dune-fem  2.8-git
function/common/functor.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FUNCTION_COMMON_FUNCTOR_HH
2 #define DUNE_FEM_FUNCTION_COMMON_FUNCTOR_HH
3 
4 #include <cstddef>
5 
6 #include <utility>
7 
11 
12 namespace Dune
13 {
14 
15  namespace Fem
16  {
17 
18  // LeftAdd
19  // -------
20 
21  template< class Vector >
22  struct LeftAdd
23  {
24  LeftAdd ( const Vector &vector )
25  : vector_( vector )
26  {}
27 
28  template< class Value >
29  void operator() ( const std::size_t index, Value &&value ) const
30  {
31  value += vector_[ index ];
32  }
33  private:
34  const Vector &vector_;
35  };
36 
37 
38  // LeftAddScaled
39  // -------------
40 
41  template< class Vector, class Scalar >
43  {
44  LeftAddScaled ( const Vector &vector, const Scalar &s )
45  : vector_( vector ),
46  s_( s )
47  {}
48 
49  template< class Value >
50  void operator() ( const std::size_t index, Value &&value ) const
51  {
52  axpy( s_, vector_[ index ], std::forward< Value >( value ) );
53  }
54  private:
55  const Vector &vector_;
56  const Scalar &s_;
57  };
58 
59 
60  // LeftAssign
61  // ----------
62 
63  template< class Vector >
64  struct LeftAssign
65  {
66  LeftAssign ( const Vector &vector )
67  : vector_( vector )
68  {}
69 
70  template< class Value >
71  void operator() ( const std::size_t index, Value &&value ) const
72  {
73  value = vector_[ index ];
74  }
75  private:
76  const Vector &vector_;
77  };
78 
79 
80  // AssignReference
81  // ---------------
82 
83  template< class Vector >
85  {
86  AssignVectorReference ( Vector &vector )
87  : vector_( vector )
88  {}
89 
90  template< class Value >
91  void operator() ( const std::size_t index, Value &&value ) const
92  {
93  vector_.bind( index, std::forward< Value > ( value ) );
94  }
95 
96  protected:
97  Vector &vector_;
98  };
99 
100 
101  // DofBlockFunctor
102  // ---------------
103 
104  template< class DofVector, class Functor >
106  {
107  typedef typename DofVector::BlockIndices BlockIndices;
108  static constexpr std::size_t blockSize = Hybrid::size( BlockIndices() );
109 
110  DofBlockFunctor ( DofVector &dofVector, Functor functor )
111  : dofVector_( dofVector ), functor_( functor )
112  {}
113 
114  template < class GlobalKey >
115  void operator () ( std::size_t local, const GlobalKey& globalKey ) const
116  {
117  Hybrid::forEach( BlockIndices(), [ this, local, &globalKey ] ( auto &&i ) {
118  functor_( local*blockSize + i, dofVector_[ globalKey ][ i ] );
119  } );
120  }
121 
122  private:
123  DofVector &dofVector_;
124  Functor functor_;
125  };
126 
127 
128  // dofBlockFunctor
129  // ---------------
130 
131  template< class DofVector, class Functor >
132  static inline DofBlockFunctor< DofVector, Functor > dofBlockFunctor ( DofVector &dofVector, Functor functor )
133  {
134  return DofBlockFunctor< DofVector, Functor >( dofVector, std::move( functor ) );
135  }
136 
137  } // namespace Fem
138 
139 } // namespace Dune
140 
141 #endif // #ifndef DUNE_FEM_FUNCTION_COMMON_FUNCTOR_HH
Definition: bindguard.hh:11
static DofBlockFunctor< DofVector, Functor > dofBlockFunctor(DofVector &dofVector, Functor functor)
Definition: function/common/functor.hh:132
void axpy(const T &a, const T &x, T &y)
Definition: space/basisfunctionset/functor.hh:38
static void forEach(IndexRange< T, sz > range, F &&f)
Definition: hybrid.hh:129
Definition: function/common/functor.hh:23
LeftAdd(const Vector &vector)
Definition: function/common/functor.hh:24
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:29
Definition: function/common/functor.hh:43
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:50
LeftAddScaled(const Vector &vector, const Scalar &s)
Definition: function/common/functor.hh:44
Definition: function/common/functor.hh:65
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:71
LeftAssign(const Vector &vector)
Definition: function/common/functor.hh:66
Definition: function/common/functor.hh:85
AssignVectorReference(Vector &vector)
Definition: function/common/functor.hh:86
Vector & vector_
Definition: function/common/functor.hh:97
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:91
Definition: function/common/functor.hh:106
void operator()(std::size_t local, const GlobalKey &globalKey) const
Definition: function/common/functor.hh:115
DofVector::BlockIndices BlockIndices
Definition: function/common/functor.hh:107
DofBlockFunctor(DofVector &dofVector, Functor functor)
Definition: function/common/functor.hh:110
static constexpr std::size_t blockSize
Definition: function/common/functor.hh:108