dune-fem 2.12-git
Loading...
Searching...
No Matches
tuplediscretefunction/dofvector.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_FUNCTION_BLOCKVECTORS_TUPLE_HH
2#define DUNE_FEM_FUNCTION_BLOCKVECTORS_TUPLE_HH
3
4#include <limits>
5#include <utility>
6#include <tuple>
7
9
14
15
16namespace Dune
17{
18
19 namespace Fem
20 {
21
22
23 // TupleDofVector
24 // --------------
25
26 template< class ... DofVectors >
28 : public IsBlockVector,
29 public std::tuple< DofVectors& ... >
30 {
31 typedef TupleDofVector< DofVectors ... > ThisType;
32 typedef std::tuple< DofVectors & ... > BaseType;
33
34 static_assert( sizeof ... ( DofVectors ) > 0, "TupleDofVector needs at least one DofVector." );
35
36 typedef std::tuple< DofVectors ... > DofVectorTuple;
37
38 typedef decltype ( std::index_sequence_for< DofVectors ... >() ) Sequence;
39
40 public:
41 static_assert( Std::are_all_same< typename DofVectors::FieldType ... >::value, "All blocks need to have the same FieldType." );
42 typedef typename std::tuple_element< 0, DofVectorTuple >::type::FieldType FieldType;
43
44 struct Iterator;
45 struct ConstIterator;
46
49
53
54 static const int blockSize = 1;
55
57 {
59 explicit DofBlockRef(FieldType *p = nullptr) : ptr_(p) {}
60 FieldType &operator[]( std::size_t i ) const { return ptr_[ i ]; }
61 operator FieldType &() const { return *ptr_; }
62 FieldType *operator->() const { return ptr_; }
63 };
64
66 {
68 explicit ConstDofBlockRef(const FieldType *p = nullptr) : ptr_(p) {}
69 const FieldType &operator[]( std::size_t i ) const { return ptr_[ i ]; }
70 operator const FieldType &() const { return *ptr_; }
71 const FieldType *operator->() const { return ptr_; }
72 };
73
76
79
80 TupleDofVector ( DofVectors & ... dofVectors )
81 : BaseType( std::forward_as_tuple( dofVectors ... ) )
82 {}
83
84 // constructor
85 TupleDofVector ( BaseType data ) : BaseType( data ) {}
86 TupleDofVector ( const ThisType & ) = default;
87 TupleDofVector ( ThisType && ) = default;
88
89 const ThisType &operator= ( const ThisType &other )
90 {
91 assign( other, Sequence() );
92 return *this;
93 }
94
95 const ThisType &operator+= ( const ThisType &other )
96 {
97 axpy( 1.0, other, Sequence() );
98 return *this;
99 }
100
101 const ThisType &operator-= ( const ThisType &other )
102 {
103 axpy( -1.0, other, Sequence() );
104 return *this;
105 }
106
107 private:
108 template <int i>
109 struct Acc
110 {
111 static void apply(const ThisType &a, const ThisType& b, FieldType& result )
112 {
113 result += (a.template subDofVector< i >() * b.template subDofVector<i>());
114 }
115 };
116
117 public:
118 FieldType operator* ( const ThisType &other ) const
119 {
120 FieldType result( 0 );
121 static const int length = std::tuple_size< BaseType >::value;
123 return result;
124 }
125
126 const ThisType &operator*= ( const FieldType &scalar )
127 {
128 scale( scalar, Sequence() );
129 return *this;
130 }
131
132 const ThisType &operator/= ( const FieldType &scalar )
133 {
134 scale( 1.0 / scalar, Sequence() );
135 return *this;
136 }
137
138 void axpy ( const FieldType &scalar, const ThisType &other )
139 {
140 axpy( scalar, other, Sequence() );
141 }
142
143 void clear () { clear( Sequence() ); }
144
145 SizeType size () const { return size( Sequence() ); }
146
147 // ----------------------------------------------------------------------------
148
149 IteratorType begin () { return IteratorType( *this, 0 ); }
150 ConstIteratorType begin () const { return ConstIteratorType( *this, 0 ); }
151
152 IteratorType end () { return IteratorType( *this, size() ); }
153 ConstIteratorType end () const { return ConstIteratorType( *this, size() ); }
154
163
165 {
166 return DofBlockPtrType( this->operator[]( index ) );
167 }
169 {
170 return ConstDofBlockPtrType( this->operator[]( index ) );
171 }
172
173 // ----------------------------------------------------------------------------
174
176 void resize ( SizeType size ) {}
177
178 constexpr std::size_t blocks () const { return sizeof ... ( DofVectors ); }
179
180 template< int i >
181 const typename std::tuple_element< i, DofVectorTuple >::type &
182 subDofVector() const { return std::get< i >( *this ); }
183
184 template< int i >
185 typename std::tuple_element< i, DofVectorTuple >::type &
187 return std::get< i >( *this );
188 }
189
190 protected:
191 template< std::size_t ... i >
193 {
194 std::ignore = std::make_tuple( (std::get< i >( *this ) *= scale, i ) ... );
195 }
196
197 template< std::size_t ... i >
199 {
200 std::ignore = std::make_tuple( ( std::get< i >( *this ).axpy( a, std::get< i >( other ) ), i ) ... );
201 }
202
203 template< std::size_t ... i >
205 {
206 std::ignore = std::make_tuple( ( std::get< i >( *this ) = std::get< i >( other ), i ) ... );
207 }
208
209 template< std::size_t ... i >
211 {
212 return Std::sum( std::get< i >( *this ).size() *
213 std::tuple_element< i, DofVectorTuple >::type::blockSize ... );
214 }
215
216 template< std::size_t ... I >
218 {
219 std::ignore = std::make_tuple( ( std::get< I >( *this ).clear(), I ) ... );
220 }
221
222 template< std::size_t i >
224 {
225 const std::size_t thisBlockSize = std::tuple_element< i, DofVectorTuple >::type::blockSize;
226 std::size_t offset = std::get< i >( *this ).size() * thisBlockSize;
227 if( index < offset )
228 return DofBlockType( &std::get< i >( *this )[ index / thisBlockSize ][ index % thisBlockSize ] );
229 else
231 }
232
234 {
235 DUNE_THROW( RangeError, "Index out of range" );
236 }
237
238 template< std::size_t i >
240 {
241 const std::size_t thisBlockSize = std::tuple_element< i, DofVectorTuple >::type::blockSize;
242 std::size_t offset = std::get< i >( *this ).size() * thisBlockSize;
243 if( index < offset )
244 return ConstDofBlockType( &std::get< i >( *this )[ index / thisBlockSize ][ index % thisBlockSize ] );
245 else
247 }
248
250 {
251 DUNE_THROW( RangeError, "Index out of range" );
252 }
253
254 };
255
256
257 template< class ... DofVectors >
258 struct TupleDofVector< DofVectors ... >::
260 {
262 : container_( container ), iterator_( it ) {}
263
264 Iterator( const Iterator & ) = default;
265 Iterator &operator= ( const Iterator & ) = default;
266
267 FieldType &operator* () { return container_[ iterator_ ]; }
268 FieldType *operator-> () { return container_[ iterator_ ]; }
269
270 Iterator &operator++ () { iterator_++; return *this; }
272 {
273 ThisType copy( *this );
274 iterator_++;
275 return copy;
276 }
277
278 bool operator== ( const Iterator &other ) const { return iterator_ == other.iterator_; }
279 bool operator!= ( const Iterator &other ) const { return iterator_ != other.iterator_; }
280
281 protected:
282 TupleDofVector< DofVectors ... > &container_;
284 };
285
286 template< class ... DofVectors >
287 struct TupleDofVector< DofVectors ... >::
289 {
291 : container_( container ), iterator_( it ) {}
292
293 ConstIterator( const ConstIterator & ) = default;
294
295 const FieldType &operator* () const { return container_[ iterator_ ]; }
296 FieldType *operator-> () const { return container_[ iterator_ ]; }
297
298 ConstIterator &operator++ () { iterator_++; return *this; }
300 {
301 ThisType copy( *this );
302 iterator_++;
303 return copy;
304 }
305
306 bool operator== ( const ConstIterator &other ) const { return iterator_ == other.iterator_; }
307 bool operator!= ( const ConstIterator &other ) const { return iterator_ != other.iterator_; }
308
309 protected:
310 const TupleDofVector< DofVectors ... > &container_;
312 };
313
314 } // namespace Fem
315
316} // namespace Dune
317
318#endif // #ifndef DUNE_FEM_FUNCTION_BLOCKVECTORS_TUPLE_HH
Indent & operator++()
T * operator->() const
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
static constexpr std::decay_t< T > sum(T a)
Definition utility.hh:33
bool operator==(const DiscreteFunctionInterface< ImplX > &x, const DiscreteFunctionInterface< ImplY > &y)
Definition common/discretefunction.hh:1053
bool operator!=(const DiscreteFunctionInterface< ImplX > &x, const DiscreteFunctionInterface< ImplY > &y)
Definition common/discretefunction.hh:1059
STL namespace.
static DUNE_PRIVATE void apply(Args &&... args)
Definition forloop.hh:23
Definition utility.hh:147
Definition defaultblockvectors.hh:27
Definition tuplediscretefunction/dofvector.hh:30
constexpr std::size_t blocks() const
Definition tuplediscretefunction/dofvector.hh:178
const ThisType & operator=(const ThisType &other)
Definition tuplediscretefunction/dofvector.hh:89
void reserve(SizeType size)
Definition tuplediscretefunction/dofvector.hh:175
void clear()
Definition tuplediscretefunction/dofvector.hh:143
const ThisType & operator/=(const FieldType &scalar)
Definition tuplediscretefunction/dofvector.hh:132
TupleDofVector(BaseType data)
Definition tuplediscretefunction/dofvector.hh:85
ConstDofBlockType blockAccess(std::size_t index, std::integral_constant< std::size_t, sizeof ...(DofVectors) >) const
Definition tuplediscretefunction/dofvector.hh:249
Iterator IteratorType
Definition tuplediscretefunction/dofvector.hh:47
FieldType value_type
Definition tuplediscretefunction/dofvector.hh:51
const ThisType & operator-=(const ThisType &other)
Definition tuplediscretefunction/dofvector.hh:101
DofBlockRef DofBlockType
Definition tuplediscretefunction/dofvector.hh:74
SizeType size_type
Definition tuplediscretefunction/dofvector.hh:52
ConstDofBlockType blockAccess(std::size_t index, std::integral_constant< std::size_t, i >) const
Definition tuplediscretefunction/dofvector.hh:239
DofBlockType blockAccess(std::size_t index, std::integral_constant< std::size_t, sizeof ...(DofVectors) >)
Definition tuplediscretefunction/dofvector.hh:233
FieldType operator*(const ThisType &other) const
Definition tuplediscretefunction/dofvector.hh:118
Fem::Envelope< ConstDofBlockType > ConstDofBlockPtrType
Definition tuplediscretefunction/dofvector.hh:78
const ThisType & operator+=(const ThisType &other)
Definition tuplediscretefunction/dofvector.hh:95
ConstIterator ConstIteratorType
Definition tuplediscretefunction/dofvector.hh:48
DofBlockPtrType blockPtr(std::size_t index)
Definition tuplediscretefunction/dofvector.hh:164
static const int blockSize
Definition tuplediscretefunction/dofvector.hh:54
void scale(FieldType scale, std::index_sequence< i ... >)
Definition tuplediscretefunction/dofvector.hh:192
ConstIteratorType begin() const
Definition tuplediscretefunction/dofvector.hh:150
IteratorType end()
Definition tuplediscretefunction/dofvector.hh:152
void assign(const ThisType &other, std::index_sequence< i ... >)
Definition tuplediscretefunction/dofvector.hh:204
DofBlockType operator[](std::size_t index)
Definition tuplediscretefunction/dofvector.hh:155
Fem::Envelope< DofBlockType > DofBlockPtrType
Definition tuplediscretefunction/dofvector.hh:77
std::tuple_element< 0, DofVectorTuple >::type::FieldType FieldType
Definition tuplediscretefunction/dofvector.hh:42
SizeType size(std::index_sequence< i ... >) const
Definition tuplediscretefunction/dofvector.hh:210
std::tuple_element< i, DofVectorTuple >::type & subDofVector()
Definition tuplediscretefunction/dofvector.hh:186
SizeType size() const
Definition tuplediscretefunction/dofvector.hh:145
ConstIteratorType end() const
Definition tuplediscretefunction/dofvector.hh:153
TupleDofVector(DofVectors &... dofVectors)
Definition tuplediscretefunction/dofvector.hh:80
std::size_t SizeType
Definition tuplediscretefunction/dofvector.hh:50
TupleDofVector(const ThisType &)=default
const std::tuple_element< i, DofVectorTuple >::type & subDofVector() const
Definition tuplediscretefunction/dofvector.hh:182
ConstDofBlockPtrType blockPtr(std::size_t index) const
Definition tuplediscretefunction/dofvector.hh:168
void resize(SizeType size)
Definition tuplediscretefunction/dofvector.hh:176
void axpy(const FieldType &scalar, const ThisType &other)
Definition tuplediscretefunction/dofvector.hh:138
IteratorType begin()
Definition tuplediscretefunction/dofvector.hh:149
const ThisType & operator*=(const FieldType &scalar)
Definition tuplediscretefunction/dofvector.hh:126
TupleDofVector(ThisType &&)=default
void axpy(FieldType a, const ThisType &other, std::index_sequence< i ... >)
Definition tuplediscretefunction/dofvector.hh:198
DofBlockType blockAccess(std::size_t index, std::integral_constant< std::size_t, i >)
Definition tuplediscretefunction/dofvector.hh:223
void clear(std::index_sequence< I ... >)
Definition tuplediscretefunction/dofvector.hh:217
ConstDofBlockRef ConstDofBlockType
Definition tuplediscretefunction/dofvector.hh:75
Definition tuplediscretefunction/dofvector.hh:57
FieldType * ptr_
Definition tuplediscretefunction/dofvector.hh:58
DofBlockRef(FieldType *p=nullptr)
Definition tuplediscretefunction/dofvector.hh:59
FieldType & operator[](std::size_t i) const
Definition tuplediscretefunction/dofvector.hh:60
FieldType * operator->() const
Definition tuplediscretefunction/dofvector.hh:62
Definition tuplediscretefunction/dofvector.hh:66
ConstDofBlockRef(const FieldType *p=nullptr)
Definition tuplediscretefunction/dofvector.hh:68
const FieldType * operator->() const
Definition tuplediscretefunction/dofvector.hh:71
const FieldType & operator[](std::size_t i) const
Definition tuplediscretefunction/dofvector.hh:69
const FieldType * ptr_
Definition tuplediscretefunction/dofvector.hh:67
Definition tuplediscretefunction/dofvector.hh:260
std::size_t iterator_
Definition tuplediscretefunction/dofvector.hh:283
TupleDofVector< DofVectors ... > & container_
Definition tuplediscretefunction/dofvector.hh:282
Iterator(const Iterator &)=default
Iterator(TupleDofVector< DofVectors ... > &container, std::size_t it=0)
Definition tuplediscretefunction/dofvector.hh:261
Definition tuplediscretefunction/dofvector.hh:289
ConstIterator(const ConstIterator &)=default
std::size_t iterator_
Definition tuplediscretefunction/dofvector.hh:311
const TupleDofVector< DofVectors ... > & container_
Definition tuplediscretefunction/dofvector.hh:310
ConstIterator(const TupleDofVector< DofVectors ... > &container, std::size_t it=0)
Definition tuplediscretefunction/dofvector.hh:290
Definition envelope.hh:11
T forward(T... args)
T make_tuple(T... args)