1#ifndef DUNE_FEM_STENCIL_HH
2#define DUNE_FEM_STENCIL_HH
9#include <unordered_map>
11#include <dune/grid/common/gridenums.hh>
12#include <dune/grid/common/rangegenerators.hh>
13#include <dune/fem/misc/functor.hh>
14#include <dune/fem/common/utility.hh>
33 template <
class DomainSpace,
class RangeSpace>
37 typedef typename DomainSpace::IteratorType DomainIteratorType;
38 typedef typename DomainSpace::BlockMapperType DomainBlockMapper;
41 typedef typename RangeSpace::IteratorType RangeIteratorType;
42 typedef typename RangeSpace::BlockMapperType RangeBlockMapper;
45 typedef typename DomainIteratorType::Entity DomainEntityType;
46 typedef typename RangeIteratorType::Entity RangeEntityType;
47 typedef typename DomainBlockMapper::GlobalKeyType DomainGlobalKeyType;
48 typedef typename RangeBlockMapper::GlobalKeyType RangeGlobalKeyType;
54 typedef typename std::vector< std::size_t > :: size_type
IndexType;
56 static const bool indexIsSimple = std::is_convertible< RangeGlobalKeyType, IndexType >::value;
58 typedef typename std::conditional< indexIsSimple,
59 std::unordered_map< RangeGlobalKeyType, LocalStencilType >,
60 std::map< RangeGlobalKeyType, LocalStencilType > > :: type GlobalStencilType;
69 Stencil(
const DomainSpace &dSpace,
const RangeSpace &rSpace)
70 : domainSpace_(dSpace), rangeSpace_(rSpace)
71 , domainBlockMapper_( dSpace.blockMapper() )
72 , rangeBlockMapper_( rSpace.blockMapper() )
77 const DomainSpace &domainSpace()
const
81 const RangeSpace &rangeSpace()
const
93 void fill (
const DomainEntityType &dEntity,
const RangeEntityType &rEntity,
94 bool fillGhost=
true )
const
96 if( (dEntity.partitionType() ==
GhostEntity) && !fillGhost )
99 rangeBlockMapper_.mapEach( rEntity, [
this, &dEntity ] (
int localRow,
auto globalRow ) {
100 domainBlockMapper_.mapEach( dEntity, RowFillFunctor( globalStencil_[ globalRow ] ) );
118 return globalStencil_;
126 for(
const auto& entry : globalStencil_ )
128 maxNZ = std::max( maxNZ,
static_cast<int>( entry.second.size() ) );
133 int rows ()
const {
return rangeBlockMapper_.size(); }
134 int cols ()
const {
return domainBlockMapper_.size(); }
139 globalStencil_.clear();
144 [[deprecated(
"Use Stencil::update instead()")]]
154 struct RowFillFunctor
160 void operator() (
const std::size_t,
const DomainGlobalKeyType &domainGlobal )
const
162 localStencil_.insert( domainGlobal );
170 const DomainSpace &domainSpace_;
171 const RangeSpace &rangeSpace_;
172 const DomainBlockMapper &domainBlockMapper_;
173 const RangeBlockMapper &rangeBlockMapper_;
175 mutable GlobalStencilType globalStencil_;
185 template <
class DomainSpace,
class RangeSpace>
190 typedef typename StencilType::DomainEntityType DomainEntityType;
191 typedef typename StencilType::RangeEntityType RangeEntityType;
192 typedef typename StencilType::DomainGlobalKeyType DomainGlobalKeyType;
193 typedef typename StencilType::RangeGlobalKeyType RangeGlobalKeyType;
195 typedef typename StencilType::GlobalStencilType GlobalStencilType;
204 int maxNonZerosEstimate()
const
208 const LocalStencilType &localStencil(
const DomainGlobalKeyType &key)
const
211 return localStencil_;
213 const GlobalStencilType &globalStencil()
const
220 GlobalStencilType stencil_;
221 LocalStencilType localStencil_;
232 template <
class DomainSpace,
class RangeSpace,
class LocalStencil>
238 typedef typename StencilType::DomainEntityType DomainEntityType;
239 typedef typename StencilType::RangeEntityType RangeEntityType;
240 typedef typename StencilType::DomainGlobalKeyType DomainGlobalKeyType;
241 typedef typename StencilType::RangeGlobalKeyType RangeGlobalKeyType;
243 static_assert( Std::is_pod< DomainGlobalKeyType > :: value,
"StencilWrapper only works for POD DomainGlobalKeyType");
245 typedef LocalStencil LocalStencilType;
246 typedef std::vector< LocalStencilType > GlobalStencilType;
250 typedef std::pair< DomainGlobalKeyType, const LocalStencilType& > ItemType;
252 DomainGlobalKeyType index_;
253 const GlobalStencilType& stencil_;
256 Iterator(
const DomainGlobalKeyType& init,
const GlobalStencilType& stencil )
257 : index_( init ), stencil_( stencil ) {}
259 Iterator& operator++ ()
265 ItemType operator*()
const
267 assert( index_ < stencil_.size() );
268 return ItemType( index_, stencil_[ index_ ] );
273 return index_ == other.index_;
283 : stencil_( stencil )
284 , maxNZ_( computeMaxNZ() )
288 int maxNonZerosEstimate()
const
293 const LocalStencilType &localStencil(
const DomainGlobalKeyType &key)
const
295 return stencil_[ key ];
298 const ThisType& globalStencil()
const
310 void fill (
const DomainEntityType &dEntity,
const RangeEntityType &rEntity,
311 bool fillGhost=
true )
315 Iterator begin()
const {
return Iterator(0, stencil_); }
316 Iterator end()
const {
return Iterator(stencil_.size(), stencil_); }
317 Iterator find(
const DomainGlobalKeyType &key)
const
319 assert( key < stencil_.size() );
320 return Iterator( key, stencil_);
324 int computeMaxNZ()
const
327 for(
const auto& row : stencil_ )
329 maxNZ = std::max( maxNZ,
int(row.size()) );
334 const GlobalStencilType& stencil_;
347 template <
class DomainSpace,
class RangeSpace,
class Partition = Dune::Partitions::InteriorBorder>
352 typedef Partition PartitionType;
353 typedef typename BaseType::DomainEntityType DomainEntityType;
354 typedef typename BaseType::RangeEntityType RangeEntityType;
355 typedef typename BaseType::DomainGlobalKeyType DomainGlobalKeyType;
356 typedef typename BaseType::RangeGlobalKeyType RangeGlobalKeyType;
358 typedef typename BaseType::GlobalStencilType GlobalStencilType;
370 const DomainSpace &dSpace = BaseType::domainSpace();
371 for (
const auto& entity : elements( dSpace.gridPart(), PartitionType{} ) )
385 template <
class DomainSpace,
class RangeSpace,
class Partition = Dune::Partitions::InteriorBorder>
390 typedef Partition PartitionType;
391 typedef typename BaseType::DomainEntityType DomainEntityType;
392 typedef typename BaseType::RangeEntityType RangeEntityType;
393 typedef typename BaseType::DomainGlobalKeyType DomainGlobalKeyType;
394 typedef typename BaseType::RangeGlobalKeyType RangeGlobalKeyType;
396 typedef typename BaseType::GlobalStencilType GlobalStencilType;
399 bool onlyNonContinuousNeighbors =
false)
401 onlyNonContinuousNeighbors_(onlyNonContinuousNeighbors)
410 const DomainSpace &dSpace = BaseType::domainSpace();
411 const RangeSpace &rSpace = BaseType::rangeSpace();
412 for (
const auto & entity: elements( dSpace.gridPart(), PartitionType{} ) )
415 for (
const auto & intersection: intersections(dSpace.gridPart(), entity) )
417 if ( onlyNonContinuousNeighbors_
418 && rSpace.continuous(intersection) && dSpace.continuous(intersection) )
420 if( intersection.neighbor() )
422 auto neighbor = intersection.outside();
429 bool onlyNonContinuousNeighbors_;
a watered down stencil providing only the upper bound for the non-zero entries per row.
Definition: stencil.hh:187
a simple wrapper class for sparsity patterns provide as vector< set< size_t > >
Definition: stencil.hh:234
void fill(const DomainEntityType &dEntity, const RangeEntityType &rEntity, bool fillGhost=true)
Create stencil entries for (dEntity,rEntity) pair.
Definition: stencil.hh:310
default implementation for a general operator stencil
Definition: stencil.hh:35
void update()
clear previously computed entries such that a re-compute happens when used again
Definition: stencil.hh:137
virtual void setupStencil() const =0
method to setup stencil depending on entity set defined in derived class
const GlobalStencilType & globalStencil() const
Return the full stencil.
Definition: stencil.hh:116
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
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
Default exception for dummy implementations.
Definition: exceptions.hh:263
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:238
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:260
Dune namespace.
Definition: alignedallocator.hh:13
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
Stencil contaning the entries (en,en) for all entities in the space. Defailt for an operator over a L...
Definition: stencil.hh:349
virtual void setupStencil() const override
method to setup stencil depending on entity set defined in derived class
Definition: stencil.hh:368