dune-fem 2.12-git
Loading...
Searching...
No Matches
stencil.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_STENCIL_HH
2#define DUNE_FEM_STENCIL_HH
3
4#include <algorithm>
5#include <map>
6#include <numeric>
7#include <set>
8#include <type_traits>
9#include <unordered_map>
10
15
16namespace Dune
17{
18 namespace Fem
19 {
33 template <class DomainSpace, class RangeSpace>
34 class Stencil
35 {
36 // Domain = Row
37 typedef typename DomainSpace::IteratorType DomainIteratorType;
38 typedef typename DomainSpace::BlockMapperType DomainBlockMapper;
39
40 // Range = Column
41 typedef typename RangeSpace::IteratorType RangeIteratorType;
42 typedef typename RangeSpace::BlockMapperType RangeBlockMapper;
43
44 public:
45 typedef typename DomainIteratorType::Entity DomainEntityType;
46 typedef typename RangeIteratorType::Entity RangeEntityType;
47 typedef typename DomainBlockMapper::GlobalKeyType DomainGlobalKeyType;
48 typedef typename RangeBlockMapper::GlobalKeyType RangeGlobalKeyType;
49
52
54 typedef typename std::vector< std::size_t > :: size_type IndexType;
55
57 //static const bool indexIsPOD = false ;//std::is_convertible< RangeGlobalKeyType, IndexType >::value;
58 typedef typename std::conditional< indexIsSimple,
61
62 public:
69 Stencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
70 : domainSpace_(dSpace), rangeSpace_(rSpace)
71 , domainBlockMapper_( dSpace.blockMapper() )
72 , rangeBlockMapper_( rSpace.blockMapper() )
73 {
74 }
75 virtual ~Stencil() {}
76
77 const DomainSpace &domainSpace() const
78 {
79 return domainSpace_;
80 }
81 const RangeSpace &rangeSpace() const
82 {
83 return rangeSpace_;
84 }
85
93 void fill ( const DomainEntityType &dEntity, const RangeEntityType &rEntity,
94 bool fillGhost=true ) const
95 {
96 if( (dEntity.partitionType() == GhostEntity) && !fillGhost )
97 return;
98
99 rangeBlockMapper_.mapEach( rEntity, [ this, &dEntity ] ( int localRow, auto globalRow ) {
100 domainBlockMapper_.mapEach( dEntity, RowFillFunctor( globalStencil_[ globalRow ] ) );
101 } );
102 }
103
110 {
111 return globalStencil()[ key ];
112 }
113
117 {
118 return globalStencil_;
119 }
120
124 {
125 int maxNZ = 0;
126 for( const auto& entry : globalStencil_ )
127 {
128 maxNZ = std::max( maxNZ, static_cast<int>( entry.second.size() ) );
129 }
130 return maxNZ;
131 }
132
133 int rows () const { return rangeBlockMapper_.size(); }
134 int cols () const { return domainBlockMapper_.size(); }
135
137 void update()
138 {
139 globalStencil_.clear();
140 // compute stencil based on overloaded implementation
141 setupStencil();
142 }
143
144 [[deprecated("Use Stencil::update instead()")]]
145 void setup() {
146 update();
147 }
148
149 protected:
151 virtual void setupStencil() const = 0;
152
153 private:
154 struct RowFillFunctor
155 {
156 explicit RowFillFunctor ( LocalStencilType &localStencil )
157 : localStencil_( localStencil )
158 {}
159
160 void operator() ( const std::size_t, const DomainGlobalKeyType &domainGlobal ) const
161 {
162 localStencil_.insert( domainGlobal );
163 }
164
165 private:
166 LocalStencilType &localStencil_;
167 };
168
169 protected:
170 const DomainSpace &domainSpace_;
171 const RangeSpace &rangeSpace_;
172 const DomainBlockMapper &domainBlockMapper_;
173 const RangeBlockMapper &rangeBlockMapper_;
174
176 };
177
185 template <class DomainSpace, class RangeSpace>
187 {
189 public:
196
197 SimpleStencil(int maxNZ)
198 : maxNZ_(maxNZ)
199 {}
201 {
202 maxNZ_ = 1;
203 }
205 {
206 return maxNZ_;
207 }
209 {
210 DUNE_THROW( Dune::NotImplemented, "SimpleStencil: exact stencil information is unavailable." );
211 return localStencil_;
212 }
214 {
215 DUNE_THROW( Dune::NotImplemented, "SimpleStencil: global stencil is unavailable." );
216 return stencil_;
217 }
218 protected:
222 };
223
224
232 template <class DomainSpace, class RangeSpace, class LocalStencil>
234 {
237 public:
242
243 static_assert( Std::is_pod< DomainGlobalKeyType > :: value, "StencilWrapper only works for POD DomainGlobalKeyType");
244
245 typedef LocalStencil LocalStencilType;
247
248 struct Iterator
249 {
251
254
255
257 : index_( init ), stencil_( stencil ) {}
258
260 {
261 ++ index_ ;
262 return *this;
263 }
264
266 {
267 assert( index_ < stencil_.size() );
268 return ItemType( index_, stencil_[ index_ ] );
269 }
270
271 bool operator == ( const Iterator& other ) const
272 {
273 return index_ == other.index_;
274 }
275
276 bool operator != ( const Iterator& other ) const
277 {
278 return !this->operator==( other );
279 }
280 };
281
283 : stencil_( stencil )
284 , maxNZ_( computeMaxNZ() )
285 {
286 }
287
289 {
290 return maxNZ_;
291 }
292
294 {
295 return stencil_[ key ];
296 }
297
298 const ThisType& globalStencil() const
299 {
300 return *this;
301 }
302
310 void fill ( const DomainEntityType &dEntity, const RangeEntityType &rEntity,
311 bool fillGhost=true )
312 {
313 }
314
315 Iterator begin() const { return Iterator(0, stencil_); }
316 Iterator end() const { return Iterator(stencil_.size(), stencil_); }
318 {
319 assert( key < stencil_.size() );
320 return Iterator( key, stencil_);
321 }
322
323 protected:
324 int computeMaxNZ() const
325 {
326 int maxNZ = 0;
327 for( const auto& row : stencil_ )
328 {
329 maxNZ = std::max( maxNZ, int(row.size()) );
330 }
331 return maxNZ;
332 }
333
336 };
337
338
347 template <class DomainSpace, class RangeSpace, class Partition = Dune::Partitions::InteriorBorder>
348 struct DiagonalStencil : public Stencil<DomainSpace,RangeSpace>
349 {
351 public:
352 typedef Partition PartitionType;
359
360 DiagonalStencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
361 : BaseType( dSpace, rSpace )
362 {
363 setupStencil();
364 }
365 virtual ~DiagonalStencil() {}
366
367 protected:
368 virtual void setupStencil () const override
369 {
370 const DomainSpace &dSpace = BaseType::domainSpace();
371 for (const auto& entity : elements( dSpace.gridPart(), PartitionType{} ) )
372 BaseType::fill(entity,entity);
373 }
374 };
375
385 template <class DomainSpace, class RangeSpace, class Partition = Dune::Partitions::InteriorBorder>
386 struct DiagonalAndNeighborStencil : public Stencil<DomainSpace,RangeSpace>
387 {
389 public:
390 typedef Partition PartitionType;
397
398 DiagonalAndNeighborStencil(const DomainSpace &dSpace, const RangeSpace &rSpace,
399 bool onlyNonContinuousNeighbors = false)
400 : BaseType( dSpace, rSpace ),
401 onlyNonContinuousNeighbors_(onlyNonContinuousNeighbors)
402 {
403 setupStencil();
404 }
406
407 protected:
408 virtual void setupStencil() const override
409 {
410 const DomainSpace &dSpace = BaseType::domainSpace();
411 const RangeSpace &rSpace = BaseType::rangeSpace();
412 for (const auto & entity: elements( dSpace.gridPart(), PartitionType{} ) )
413 {
414 BaseType::fill(entity,entity);
415 for (const auto & intersection: intersections(dSpace.gridPart(), entity) )
416 {
418 && rSpace.continuous(intersection) && dSpace.continuous(intersection) )
419 continue;
420 if( intersection.neighbor() )
421 {
422 auto neighbor = intersection.outside();
423 BaseType::fill(neighbor,entity);
424 }
425 }
426 }
427 }
428
430 };
431
432 } // namespace Fem
433
434} // namespace Dune
435
436#endif // #if defined DUNE_FEM_STENCIL_HH
437
void init(const Matrix *matrix)
virtual void operator()()=0
#define DUNE_THROW(E,...)
Definition utility.hh:162
default implementation for a general operator stencil
Definition stencil.hh:35
const DomainSpace & domainSpace_
Definition stencil.hh:170
static const bool indexIsSimple
Definition stencil.hh:56
const RangeBlockMapper & rangeBlockMapper_
Definition stencil.hh:173
GlobalStencilType globalStencil_
Definition stencil.hh:175
int rows() const
Definition stencil.hh:133
RangeIteratorType::Entity RangeEntityType
Definition stencil.hh:46
void update()
clear previously computed entries such that a re-compute happens when used again
Definition stencil.hh:137
DomainBlockMapper::GlobalKeyType DomainGlobalKeyType
Definition stencil.hh:47
const RangeSpace & rangeSpace_
Definition stencil.hh:171
void setup()
Definition stencil.hh:145
const RangeSpace & rangeSpace() const
Definition stencil.hh:81
virtual void setupStencil() const =0
method to setup stencil depending on entity set defined in derived class
RangeBlockMapper::GlobalKeyType RangeGlobalKeyType
Definition stencil.hh:48
const DomainSpace & domainSpace() const
Definition stencil.hh:77
const GlobalStencilType & globalStencil() const
Return the full stencil.
Definition stencil.hh:116
DomainIteratorType::Entity DomainEntityType
Definition stencil.hh:45
std::set< DomainGlobalKeyType > LocalStencilType
type for storing the stencil of one row
Definition stencil.hh:51
void fill(const DomainEntityType &dEntity, const RangeEntityType &rEntity, bool fillGhost=true) const
Create stencil entries for (dEntity,rEntity) pair.
Definition stencil.hh:93
std::vector< std::size_t >::size_type IndexType
type of std::vector for indexing
Definition stencil.hh:54
std::conditional< indexIsSimple, std::unordered_map< RangeGlobalKeyType, LocalStencilType >, std::map< RangeGlobalKeyType, LocalStencilType > >::type GlobalStencilType
Definition stencil.hh:60
virtual ~Stencil()
Definition stencil.hh:75
int maxNonZerosEstimate() const
Return an upper bound for the maximum number of non-zero entries in all rows.
Definition stencil.hh:123
Stencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
Constructor.
Definition stencil.hh:69
const LocalStencilType & localStencil(const RangeGlobalKeyType &key) const
Return stencil for a given row of the matrix.
Definition stencil.hh:109
const DomainBlockMapper & domainBlockMapper_
Definition stencil.hh:172
int cols() const
Definition stencil.hh:134
a watered down stencil providing only the upper bound for the non-zero entries per row.
Definition stencil.hh:187
StencilType::RangeGlobalKeyType RangeGlobalKeyType
Definition stencil.hh:193
int maxNZ_
Definition stencil.hh:219
StencilType::RangeEntityType RangeEntityType
Definition stencil.hh:191
LocalStencilType localStencil_
Definition stencil.hh:221
const LocalStencilType & localStencil(const DomainGlobalKeyType &key) const
Definition stencil.hh:208
GlobalStencilType stencil_
Definition stencil.hh:220
SimpleStencil()
Definition stencil.hh:200
int maxNonZerosEstimate() const
Definition stencil.hh:204
SimpleStencil(int maxNZ)
Definition stencil.hh:197
const GlobalStencilType & globalStencil() const
Definition stencil.hh:213
StencilType::DomainEntityType DomainEntityType
Definition stencil.hh:190
StencilType::GlobalStencilType GlobalStencilType
Definition stencil.hh:195
StencilType::LocalStencilType LocalStencilType
Definition stencil.hh:194
StencilType::DomainGlobalKeyType DomainGlobalKeyType
Definition stencil.hh:192
a simple wrapper class for sparsity patterns provide as vector< set< size_t > >
Definition stencil.hh:234
int computeMaxNZ() const
Definition stencil.hh:324
void fill(const DomainEntityType &dEntity, const RangeEntityType &rEntity, bool fillGhost=true)
Create stencil entries for (dEntity,rEntity) pair.
Definition stencil.hh:310
int maxNZ_
Definition stencil.hh:335
StencilType::DomainEntityType DomainEntityType
Definition stencil.hh:238
std::vector< LocalStencilType > GlobalStencilType
Definition stencil.hh:246
const LocalStencilType & localStencil(const DomainGlobalKeyType &key) const
Definition stencil.hh:293
const ThisType & globalStencil() const
Definition stencil.hh:298
StencilType::RangeGlobalKeyType RangeGlobalKeyType
Definition stencil.hh:241
StencilType::DomainGlobalKeyType DomainGlobalKeyType
Definition stencil.hh:240
LocalStencil LocalStencilType
Definition stencil.hh:245
Iterator find(const DomainGlobalKeyType &key) const
Definition stencil.hh:317
StencilWrapper(const GlobalStencilType &stencil)
Definition stencil.hh:282
StencilType::RangeEntityType RangeEntityType
Definition stencil.hh:239
Iterator begin() const
Definition stencil.hh:315
int maxNonZerosEstimate() const
Definition stencil.hh:288
const GlobalStencilType & stencil_
Definition stencil.hh:334
Iterator end() const
Definition stencil.hh:316
Definition stencil.hh:249
bool operator==(const Iterator &other) const
Definition stencil.hh:271
const GlobalStencilType & stencil_
Definition stencil.hh:253
bool operator!=(const Iterator &other) const
Definition stencil.hh:276
Iterator(const DomainGlobalKeyType &init, const GlobalStencilType &stencil)
Definition stencil.hh:256
ItemType operator*() const
Definition stencil.hh:265
std::pair< DomainGlobalKeyType, const LocalStencilType & > ItemType
Definition stencil.hh:250
DomainGlobalKeyType index_
Definition stencil.hh:252
Iterator & operator++()
Definition stencil.hh:259
Stencil contaning the entries (en,en) for all entities in the space. Defailt for an operator over a L...
Definition stencil.hh:349
BaseType::DomainGlobalKeyType DomainGlobalKeyType
Definition stencil.hh:355
BaseType::GlobalStencilType GlobalStencilType
Definition stencil.hh:358
BaseType::RangeEntityType RangeEntityType
Definition stencil.hh:354
BaseType::DomainEntityType DomainEntityType
Definition stencil.hh:353
DiagonalStencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
Definition stencil.hh:360
virtual void setupStencil() const override
method to setup stencil depending on entity set defined in derived class
Definition stencil.hh:368
Partition PartitionType
Definition stencil.hh:352
BaseType::LocalStencilType LocalStencilType
Definition stencil.hh:357
BaseType::RangeGlobalKeyType RangeGlobalKeyType
Definition stencil.hh:356
virtual ~DiagonalStencil()
Definition stencil.hh:365
Stencil< DomainSpace, RangeSpace > BaseType
Definition stencil.hh:350
Stencil contaning the entries (en,en) and (en,nb) for all entities en in the space and neighbors nb o...
Definition stencil.hh:387
virtual void setupStencil() const override
method to setup stencil depending on entity set defined in derived class
Definition stencil.hh:408
virtual ~DiagonalAndNeighborStencil()
Definition stencil.hh:405
bool onlyNonContinuousNeighbors_
Definition stencil.hh:429
BaseType::RangeEntityType RangeEntityType
Definition stencil.hh:392
BaseType::DomainGlobalKeyType DomainGlobalKeyType
Definition stencil.hh:393
DiagonalAndNeighborStencil(const DomainSpace &dSpace, const RangeSpace &rSpace, bool onlyNonContinuousNeighbors=false)
Definition stencil.hh:398
Stencil< DomainSpace, RangeSpace > BaseType
Definition stencil.hh:388
BaseType::LocalStencilType LocalStencilType
Definition stencil.hh:395
BaseType::GlobalStencilType GlobalStencilType
Definition stencil.hh:396
Partition PartitionType
Definition stencil.hh:390
BaseType::DomainEntityType DomainEntityType
Definition stencil.hh:391
BaseType::RangeGlobalKeyType RangeGlobalKeyType
Definition stencil.hh:394
T max(T... args)
T size(T... args)