dune-fem 2.8.0
Loading...
Searching...
No Matches
defaultblockvectors.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_SIMPLEBLOCKVECTOR_HH
2#define DUNE_FEM_SIMPLEBLOCKVECTOR_HH
3
4#include <algorithm>
5#include <cassert>
6#include <memory>
7
8#include <dune/common/densevector.hh>
9#include <dune/common/dynvector.hh>
10
16
17#if HAVE_DUNE_ISTL
18#include <dune/istl/bvector.hh>
19#endif
20
21namespace Dune {
22
23 namespace Fem {
24
25 // tag for block vectors
26 struct IsBlockVector {};
27
28
29
37 template< class Imp, class Field >
39 : public IsBlockVector
40 {
41 protected:
43
45
47 typedef Imp ThisType;
48 public:
50 typedef Field FieldType;
51
53 const ThisType& operator= ( const ThisType &other )
54 {
55 if( &asImp() != &other )
56 {
57 assign( other );
58 sequence_ = other.sequence_;
59 }
60 return asImp();
61 }
62
64 const ThisType &operator+= ( const ThisType &other )
65 {
66 assert( asImp().size() == other.size() );
67 const auto endit = asImp().end();
68 auto oit = other.begin();
69 for( auto it = asImp().begin(); it != endit; ++it, ++oit )
70 *it += *oit;
71
72 ++sequence_;
73 return asImp();
74 }
75
77 const ThisType &operator-= ( const ThisType &other )
78 {
79 assert( asImp().size() == other.size() );
80 const auto endit = asImp().end();
81 auto oit = other.begin();
82 for( auto it = asImp().begin(); it != endit; ++it, ++oit )
83 *it -= *oit;
84
85 ++sequence_;
86 return asImp();
87 }
88
90 FieldType operator* ( const ThisType &other ) const
91 {
92 assert( asImp().size() == other.size() );
93 FieldType sum( 0 );
94 const auto endit = asImp().end();
95 auto oit = other.asImp().begin();
96 for( auto it = asImp().begin(); it != endit; ++it, ++oit )
97 sum += (*it * *oit);
98
99 return sum;
100 }
101
107 const ThisType &operator*= ( const FieldType &scalar )
108 {
109 const auto endit = asImp().end();
110 for( auto it = asImp().begin(); it != endit; ++it )
111 *it *= scalar;
112
113 ++sequence_;
114 return asImp();
115 }
116
124 void axpy ( const FieldType &scalar, const ThisType &other )
125 {
126 assert( asImp().size() == other.size() );
127 const auto endit = asImp().end();
128 auto oit = other.begin();
129 for( auto it = asImp().begin(); it != endit; ++it, ++oit )
130 *it += scalar * (*oit);
131
132 ++sequence_;
133 }
134
136 void clear ()
137 {
138 std::fill( asImp().begin(), asImp().end(), FieldType( 0 ) );
139 ++sequence_;
140 }
141
143 std::size_t usedMemorySize() const
144 {
145 return asImp().numDofs() * sizeof( FieldType ) ;
146 }
147
149 void copyContent( const size_t newIndex, const size_t oldIndex )
150 {
151 asImp()[ newIndex ] = asImp()[ oldIndex ];
152 }
153
155 void memMoveBackward(const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
156 {
157 assert( newStartIdx >= oldStartIdx );
158 // get new end of block which is offSet + (length of block - 1)
159 size_t newIdx = newStartIdx + length - 1;
160 assert( newIdx < asImp().size() );
161 // copy all entries backwards
162 for(size_t oldIdx = oldStartIdx + length-1; oldIdx >= oldStartIdx; --oldIdx, --newIdx )
163 {
164 assert( oldIdx < asImp().size() );
165 // copy to new location
166 copyContent( newIdx, oldIdx );
167 }
168 }
169
171 void memMoveForward(const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
172 {
173 assert( newStartIdx <= oldStartIdx );
174 const size_t upperBound = oldStartIdx + length;
175 // get new off set that should be smaller then old one
176 size_t newIdx = newStartIdx;
177 for(size_t oldIdx = oldStartIdx; oldIdx<upperBound; ++oldIdx, ++newIdx )
178 {
179 // copy to new location
180 copyContent( newIdx, oldIdx );
181 }
182 }
183
185 void setMemoryFactor( const double memFactor ) {}
186
187 protected:
188 // Copy block vectors.
189 // Note: No '++sequence_' here, sequence_ is only changed in public methods
190 void assign ( const ThisType &other )
191 {
192 assert( asImp().size() == other.size() );
193 std::copy( other.begin(), other.end(), asImp().begin() );
194 }
195
196 ThisType& asImp() { return static_cast< ThisType& > (*this); }
197 const ThisType& asImp() const { return static_cast< const ThisType& > (*this); }
198
199 mutable CounterType sequence_; // for consistency checks...
200 };
201
202
203
211 template< class Container, int BlockSize >
213 : public BlockVectorInterface< SimpleBlockVector< Container, BlockSize>, typename Container::value_type >
214 {
215 typedef BlockVectorInterface< SimpleBlockVector< Container, BlockSize>, typename Container::value_type > BaseType;
217 typedef Container ArrayType;
218
220
221 public:
222 typedef ArrayType DofContainerType;
223
225 typedef typename ArrayType::value_type FieldType;
227 typedef typename ArrayType::iterator IteratorType;
229 typedef typename ArrayType::const_iterator ConstIteratorType;
231 typedef typename ArrayType::size_type SizeType;
232
237
242
245
247 enum { blockSize = BlockSize };
248
250
251 protected:
252 template <class Array>
254 {
255 static FieldType* data( Array& array ) { return array.data(); }
256 static const FieldType* data( const Array& array ) { return array.data(); }
257 };
258
259 template <class K>
260 struct ContainerAccess< Dune::DynamicVector< K > >
261 {
262 typedef Dune::DynamicVector< K > Array;
263 static FieldType* data( Array& array ) { return array.container().data(); }
264 static const FieldType* data( const Array& array ) { return array.container().data(); }
265 };
266
267 public:
269 explicit SimpleBlockVector ( ArrayType& array )
270 : array_( array )
271 {}
272
274 const ThisType& operator= ( const ThisType& other )
275 {
276 BaseType::operator=( other );
277 return *this;
278 }
279
281 ConstDofBlockType operator[] ( const unsigned int i ) const
282 {
283 assert( i < size() );
285 }
286
288 DofBlockType operator[] ( const unsigned int i )
289 {
290 assert( i < size() );
292 }
293
295 ConstDofBlockPtrType blockPtr( const unsigned int i ) const
296 {
297 return ConstDofBlockPtrType( this->operator[] ( i ) );
298 }
299
301 DofBlockPtrType blockPtr( const unsigned int i )
302 {
303 return DofBlockPtrType( this->operator[] ( i ) );
304 }
305
307 IteratorType begin() { return array().begin(); }
308
310 ConstIteratorType begin() const { return array().begin(); }
311
313 IteratorType end() { return array().end(); }
314
316 ConstIteratorType end() const { return array().end(); }
317
319 SizeType size () const { return array().size() / blockSize; }
320
322 SizeType numDofs() const { return array().size(); }
323
326
327 const ArrayType &array () const { return array_; }
328 ArrayType &array () { return array_; }
329
330 protected:
331 ArrayType& array_;
332 };
333
334
335
343 template< class Container, unsigned int BlockSize >
345 : public SimpleBlockVector< Container, BlockSize >
346 {
348 typedef SimpleBlockVector < Container, BlockSize > BaseType;
349
350 typedef Container ArrayType;
351 using BaseType :: array_;
353 public:
354
355 using BaseType :: array;
356 using BaseType :: blockSize ;
357 typedef typename BaseType :: SizeType SizeType;
358
361 : BaseType( *(new Container( size*blockSize ) ) )
362 {}
363
366 : BaseType( *(new Container( other.array().size() ) ) )
367 {
368 assign( other );
369 }
370
372 {
373 delete &array_;
374 }
375
377 void setMemoryFactor( const double memFactor )
378 {
379 doSetMemoryFactor( array_, memFactor );
380 }
381
390 void reserve ( const int size )
391 {
392 array().reserve( size*blockSize );
393 }
394
397 {
398 array().resize( size*blockSize );
399 ++sequence_;
400 }
401
402 private:
403 template <class T, class Allocator>
404 void doSetMemoryFactor( Dune::Fem::DynamicArray< T, Allocator >& array, const double memFactor )
405 {
406 array_.setMemoryFactor( memFactor );
407 }
408
409 template <class Array>
410 void doSetMemoryFactor( Array& , const double memFactor ) {}
411
412 };
413
414
415
423 template< class Field, unsigned int BlockSize >
424 class MutableBlockVector< DynamicArray< Field >, BlockSize >
425 : public SimpleBlockVector< StaticArray< Field >, BlockSize >
426 {
431
432 protected:
433 using BaseType :: array_;
435
436 std::unique_ptr< MutableContainer > container_;
437 public:
438 using BaseType :: blockSize ;
439 typedef typename BaseType :: SizeType SizeType;
440
443 : BaseType( allocateContainer( size*blockSize ) ),
444 container_( static_cast< MutableContainer* > (&array_) )
445 {}
446
449 : BaseType( allocateContainer( other.array().size() ) ),
450 container_( static_cast< MutableContainer* > (&array_) )
451 {
452 assign( other );
453 }
454
463 void reserve ( const int size )
464 {
465 assert( container_ );
466 container_->reserve( size*blockSize );
467 }
468
471 {
472 assert( container_ );
473 container_->resize( size*blockSize );
474 ++sequence_;
475 }
476
477 protected:
479 {
480 MutableContainer* container = new MutableContainer( size );
481 return *container;
482 }
483 };
484
485
486
491 template< class DofBlock >
493 : public BlockVectorInterface< ISTLBlockVector< DofBlock >, typename DofBlock :: value_type >
494 {
496#if HAVE_DUNE_ISTL
497 typedef BlockVector< DofBlock > ArrayType;
498#else
499 // fallback in case dune-istl is not present
500 typedef Dune::DynamicVector< DofBlock > ArrayType;
501#endif
502 typedef BlockVectorInterface< ISTLBlockVector< DofBlock >, typename DofBlock :: value_type > BaseType;
503
504
506
507 public:
508 ISTLBlockVector ( const ThisType& ) = default;
509
510 typedef ArrayType DofContainerType;
511
512 enum { blockSize = DofBlock :: dimension };
514
515 typedef typename DofBlock :: value_type FieldType;
516
517 protected:
518 template <class EmbeddedIterator, class V>
520 : public ForwardIteratorFacade< Iterator< EmbeddedIterator,V >, V >
521 {
522 public:
523 typedef V FieldType;
524 protected:
525 mutable EmbeddedIterator it_;
526#ifndef NDEBUG
527 EmbeddedIterator end_;
528#endif
530 public:
532 Iterator( const EmbeddedIterator& it
533#ifndef NDEBUG
534 , const EmbeddedIterator& end = EmbeddedIterator()
535#endif
536 )
537 : it_( it ),
538#ifndef NDEBUG
539 end_( end ),
540#endif
541 index_(0)
542 {}
543
546 {
547 assert( it_ != end_ );
548 assert( index_ < blockSize );
549 return (*it_)[ index_ ];
550 }
551
553 void increment ()
554 {
555 ++index_;
556 if( index_ >= blockSize )
557 {
558 index_ = 0;
559 ++it_;
560 }
561 }
562
564 bool equals ( const Iterator &other ) const
565 {
566 return (it_ == other.it_) && (index_ == other.index_);
567 }
568
569 }; // end DofIteratorBlockVectorDiscreteFunction
570
571 public:
574
575 typedef DofBlock DofBlockType;
576 typedef const DofBlock ConstDofBlockType;
577
580
581 typedef typename ArrayType::size_type SizeType;
583 typedef typename ArrayType::value_type value_type;
584
586 explicit ISTLBlockVector ( ArrayType* array )
587 : array_( array )
588 {}
589
590 ISTLBlockVector () = default;
591
593 const ThisType& operator= ( const ThisType& other )
594 {
595 if( this != &other )
596 {
597 array() = other.array();
598 }
599 return *this;
600 }
601
602 DofBlockPtrType blockPtr(const unsigned int i ) { return &array()[ i ]; }
603 ConstDofBlockPtrType blockPtr(const unsigned int i ) const { return &array()[ i ]; }
604
605 DofBlockType& operator[] (const unsigned int i ) { return array()[ i ]; }
606 ConstDofBlockType& operator[] (const unsigned int i ) const { return array()[ i ]; }
607
609#ifndef NDEBUG
610 , array().end()
611#endif
612 ); }
614 {
615 return ConstIteratorType( array().begin()
616#ifndef NDEBUG
617 , array().end()
618#endif
619 ); }
620
621 IteratorType end() { return IteratorType( array().end() ); }
623
624 SizeType size() const { return array().size(); }
625
627 SizeType numDofs() const { return array().size() * DofBlock::dimension; }
628
637 void reserve ( const int size )
638 {
639 array().reserve( size );
640 }
641
644 {
645 array().resize( size );
646 ++sequence_;
647 }
648
649 ArrayType& array() { assert( array_ ); return *array_; }
650 const ArrayType& array() const { assert( array_ ); return *array_; }
651
652 protected:
653 // ISTL BlockVector
654 ArrayType* array_;
655 };
656
657} // namespace Fem
658} // namespace Dune
659
660#endif // DUNE_FEM_REFERENCEBLOCKVECTOR_HH
Definition: bindguard.hh:11
Definition: hybrid.hh:86
Definition: defaultblockvectors.hh:26
Definition: defaultblockvectors.hh:40
CounterType sequence_
Definition: defaultblockvectors.hh:199
void memMoveBackward(const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
move memory blocks backwards
Definition: defaultblockvectors.hh:155
Field FieldType
Type of the field the dofs lie in.
Definition: defaultblockvectors.hh:50
Imp ThisType
Type of derived class (implementation)
Definition: defaultblockvectors.hh:47
DebugCounter< size_t > CounterType
Definition: defaultblockvectors.hh:42
std::size_t usedMemorySize() const
Definition: defaultblockvectors.hh:143
void axpy(const FieldType &scalar, const ThisType &other)
Add a scalar multiple of another block vector to this block vector.
Definition: defaultblockvectors.hh:124
const ThisType & operator*=(const FieldType &scalar)
Scale this block vector.
Definition: defaultblockvectors.hh:107
void copyContent(const size_t newIndex, const size_t oldIndex)
Definition: defaultblockvectors.hh:149
ThisType & asImp()
Definition: defaultblockvectors.hh:196
BlockVectorInterface()
Definition: defaultblockvectors.hh:44
const ThisType & operator-=(const ThisType &other)
Subtract another block vector from *this.
Definition: defaultblockvectors.hh:77
const ThisType & asImp() const
Definition: defaultblockvectors.hh:197
const ThisType & operator+=(const ThisType &other)
Add another block vector to *this.
Definition: defaultblockvectors.hh:64
void setMemoryFactor(const double memFactor)
set memory overestimate factor, here does nothing
Definition: defaultblockvectors.hh:185
void memMoveForward(const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
move memory blocks forward
Definition: defaultblockvectors.hh:171
FieldType operator*(const ThisType &other) const
Scalar product between *this and another block vector.
Definition: defaultblockvectors.hh:90
void assign(const ThisType &other)
Definition: defaultblockvectors.hh:190
void clear()
Clear this block vector, i.e. set each dof to 0.
Definition: defaultblockvectors.hh:136
const ThisType & operator=(const ThisType &other)
Copy assignment operator.
Definition: defaultblockvectors.hh:53
This is the reference implementation of a block vector as it is expected as the second template param...
Definition: defaultblockvectors.hh:214
ArrayType::size_type SizeType
Used for indexing the blocks, for example.
Definition: defaultblockvectors.hh:231
SubVector< DofContainerType, StaticOffsetSubMapper< BlockSize > > DofBlockType
Type of one (mutable) block.
Definition: defaultblockvectors.hh:239
Fem::Envelope< DofBlockType > DofBlockPtrType
Definition: defaultblockvectors.hh:243
SizeType numDofs() const
Number of dofs in the block vector.
Definition: defaultblockvectors.hh:322
SizeType size() const
Number of blocks.
Definition: defaultblockvectors.hh:319
ArrayType DofContainerType
Definition: defaultblockvectors.hh:222
ArrayType::const_iterator ConstIteratorType
Constant iterator to iterate over the dofs.
Definition: defaultblockvectors.hh:229
ConstIteratorType end() const
Const-iterator pointing to the last dof.
Definition: defaultblockvectors.hh:316
ArrayType::value_type FieldType
Type of the field the dofs lie in.
Definition: defaultblockvectors.hh:225
const ArrayType & array() const
Definition: defaultblockvectors.hh:327
Hybrid::IndexRange< int, blockSize > BlockIndices
Definition: defaultblockvectors.hh:249
ConstDofBlockPtrType blockPtr(const unsigned int i) const
Constant access for the i-th block.
Definition: defaultblockvectors.hh:295
ConstIteratorType begin() const
Const-iterator pointing to the first dof.
Definition: defaultblockvectors.hh:310
const ThisType & operator=(const ThisType &other)
Copy assignment operator.
Definition: defaultblockvectors.hh:274
ArrayType & array_
Definition: defaultblockvectors.hh:331
ArrayType::iterator IteratorType
Iterator to iterate over the dofs.
Definition: defaultblockvectors.hh:227
IteratorType end()
Iterator pointing to the last dof.
Definition: defaultblockvectors.hh:313
SimpleBlockVector(ArrayType &array)
Constructor.
Definition: defaultblockvectors.hh:269
DofBlockPtrType blockPtr(const unsigned int i)
Access the i-th block.
Definition: defaultblockvectors.hh:301
ArrayType & array()
Definition: defaultblockvectors.hh:328
SizeType size_type
Typedef to make this class STL-compatible.
Definition: defaultblockvectors.hh:236
@ blockSize
Definition: defaultblockvectors.hh:247
FieldType value_type
Typedef to make this class STL-compatible.
Definition: defaultblockvectors.hh:234
DofBlockType ConstDofBlockType
Type of one constant block.
Definition: defaultblockvectors.hh:241
IteratorType begin()
Iterator pointing to the first dof.
Definition: defaultblockvectors.hh:307
ConstDofBlockType operator[](const unsigned int i) const
Constant access the i-th block.
Definition: defaultblockvectors.hh:281
FieldType * data()
Definition: defaultblockvectors.hh:324
Fem::Envelope< ConstDofBlockType > ConstDofBlockPtrType
Definition: defaultblockvectors.hh:244
const FieldType * data() const
Definition: defaultblockvectors.hh:325
Definition: defaultblockvectors.hh:254
static const FieldType * data(const Array &array)
Definition: defaultblockvectors.hh:256
static FieldType * data(Array &array)
Definition: defaultblockvectors.hh:255
static const FieldType * data(const Array &array)
Definition: defaultblockvectors.hh:264
static FieldType * data(Array &array)
Definition: defaultblockvectors.hh:263
Dune::DynamicVector< K > Array
Definition: defaultblockvectors.hh:262
Definition: defaultblockvectors.hh:346
MutableBlockVector(const ThisType &other)
Copy constructor.
Definition: defaultblockvectors.hh:365
void reserve(const int size)
Reserve memory.
Definition: defaultblockvectors.hh:390
BaseType::SizeType SizeType
Definition: defaultblockvectors.hh:357
~MutableBlockVector()
Definition: defaultblockvectors.hh:371
MutableBlockVector(SizeType size)
Construct a block vector with 'size' blocks (not initialized)
Definition: defaultblockvectors.hh:360
void resize(SizeType size)
Resize the block vector.
Definition: defaultblockvectors.hh:396
void setMemoryFactor(const double memFactor)
set memory overestimate factor, here does nothing
Definition: defaultblockvectors.hh:377
MutableBlockVector(const ThisType &other)
Copy constructor.
Definition: defaultblockvectors.hh:448
void resize(SizeType size)
Resize the block vector.
Definition: defaultblockvectors.hh:470
std::unique_ptr< MutableContainer > container_
Definition: defaultblockvectors.hh:436
MutableBlockVector(SizeType size)
Construct a block vector with 'size' blocks (not initialized)
Definition: defaultblockvectors.hh:442
BaseType::SizeType SizeType
Definition: defaultblockvectors.hh:439
void reserve(const int size)
Reserve memory.
Definition: defaultblockvectors.hh:463
StaticContainer & allocateContainer(const SizeType size)
Definition: defaultblockvectors.hh:478
Definition: defaultblockvectors.hh:494
ArrayType & array()
Definition: defaultblockvectors.hh:649
const ThisType & operator=(const ThisType &other)
Copy assignment operator.
Definition: defaultblockvectors.hh:593
ConstIteratorType begin() const
Definition: defaultblockvectors.hh:613
void reserve(const int size)
Reserve memory.
Definition: defaultblockvectors.hh:637
DofBlock DofBlockType
Definition: defaultblockvectors.hh:575
IteratorType end()
Definition: defaultblockvectors.hh:621
DofBlock::value_type FieldType
Definition: defaultblockvectors.hh:515
SizeType numDofs() const
Number of dofs in the block vector.
Definition: defaultblockvectors.hh:627
ConstDofBlockType * ConstDofBlockPtrType
Definition: defaultblockvectors.hh:579
ConstIteratorType end() const
Definition: defaultblockvectors.hh:622
ArrayType DofContainerType
Definition: defaultblockvectors.hh:510
DofBlockType * DofBlockPtrType
Definition: defaultblockvectors.hh:578
SizeType size() const
Definition: defaultblockvectors.hh:624
ISTLBlockVector(const ThisType &)=default
DofBlockType & operator[](const unsigned int i)
Definition: defaultblockvectors.hh:605
ArrayType::value_type value_type
Typedef to make this class STL-compatible.
Definition: defaultblockvectors.hh:583
const ArrayType & array() const
Definition: defaultblockvectors.hh:650
ArrayType::size_type SizeType
Definition: defaultblockvectors.hh:581
Iterator< typename ArrayType::ConstIterator, const FieldType > ConstIteratorType
Definition: defaultblockvectors.hh:573
Hybrid::IndexRange< int, blockSize > BlockIndices
Definition: defaultblockvectors.hh:513
IteratorType begin()
Definition: defaultblockvectors.hh:608
DofBlockPtrType blockPtr(const unsigned int i)
Definition: defaultblockvectors.hh:602
Iterator< typename ArrayType::Iterator, FieldType > IteratorType
Definition: defaultblockvectors.hh:572
@ blockSize
Definition: defaultblockvectors.hh:512
ISTLBlockVector(ArrayType *array)
Constructor.
Definition: defaultblockvectors.hh:586
const DofBlock ConstDofBlockType
Definition: defaultblockvectors.hh:576
ArrayType * array_
Definition: defaultblockvectors.hh:654
ConstDofBlockPtrType blockPtr(const unsigned int i) const
Definition: defaultblockvectors.hh:603
void resize(SizeType size)
Resize the block vector.
Definition: defaultblockvectors.hh:643
Definition: defaultblockvectors.hh:521
void increment()
go to next dof
Definition: defaultblockvectors.hh:553
EmbeddedIterator end_
Definition: defaultblockvectors.hh:527
EmbeddedIterator it_
Definition: defaultblockvectors.hh:525
int index_
Definition: defaultblockvectors.hh:529
V FieldType
Definition: defaultblockvectors.hh:523
bool equals(const Iterator &other) const
compare
Definition: defaultblockvectors.hh:564
Iterator(const EmbeddedIterator &it, const EmbeddedIterator &end=EmbeddedIterator())
Default constructor.
Definition: defaultblockvectors.hh:532
FieldType & dereference() const
return dof
Definition: defaultblockvectors.hh:545
A counter only present if NDEBUG is not defined.
Definition: debug.hh:30
An implementation of DenseVector which uses a C-array of fixed size as storage.
Definition: dynamicarray.hh:148
An implementation of DenseVector which uses a C-array of dynamic size as storage.
Definition: dynamicarray.hh:244
Definition: envelope.hh:11
An implementation of DenseVector to extract a portion, not necessarly contiguos, of a vector.
Definition: subvector.hh:161
Index mapper with static size which simply adds an offset to the index.
Definition: subvector.hh:121