dune-fem  2.4.1-rc
stencil.hh
Go to the documentation of this file.
1 // vim: set expandtab ts=2 sw=2 sts=2:
2 #ifndef DUNE_FEM_STENCIL_HH
3 #define DUNE_FEM_STENCIL_HH
4 
5 #include <iostream>
6 #include <set>
7 #include <map>
8 
9 #include <dune/grid/common/gridenums.hh>
11 #include <dune/fem/misc/functor.hh>
12 
13 namespace Dune
14 {
15  namespace Fem
16  {
30  template <class DomainSpace, class RangeSpace>
31  class Stencil
32  {
33  // Domain = Row
34  typedef typename DomainSpace::IteratorType DomainIteratorType;
35  typedef typename DomainSpace::BlockMapperType DomainBlockMapper;
36 
37  // Range = Column
38  typedef typename RangeSpace::IteratorType RangeIteratorType;
39  typedef typename RangeSpace::BlockMapperType RangeBlockMapper;
40 
41  public:
42  typedef typename DomainIteratorType::Entity DomainEntityType;
43  typedef typename RangeIteratorType::Entity RangeEntityType;
44  typedef typename DomainBlockMapper::GlobalKeyType DomainGlobalKeyType;
45  typedef typename RangeBlockMapper::GlobalKeyType RangeGlobalKeyType;
46 
48  typedef std::set<RangeGlobalKeyType> LocalStencilType;
50  typedef std::map<DomainGlobalKeyType,LocalStencilType> GlobalStencilType;
51 
52  public:
59  Stencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
60  : domainBlockMapper_( dSpace.blockMapper() )
61  , rangeBlockMapper_( rSpace.blockMapper() )
62  {
63  }
64 
72  void fill ( const DomainEntityType &dEntity, const RangeEntityType &rEntity,
73  bool fillGhost=true )
74  {
76 
77  bool doFill = (dEntity.partitionType()!=GhostEntity) || fillGhost;
78  rangeBlockMapper_.mapEach(rEntity,
79  MFunctor( domainBlockMapper_, dEntity, FillFunctor(globalStencil_,doFill) ) );
80  }
81 
87  const LocalStencilType &localStencil(const DomainGlobalKeyType &key) const
88  {
89  return globalStencil_[key];
90  }
93  const GlobalStencilType &globalStencil() const
94  {
95  return globalStencil_;
96  }
99  int maxNonZerosEstimate() const
100  {
101  int ret = 0;
102  typedef typename GlobalStencilType::const_iterator StencilIteratorType;
103  const GlobalStencilType &glStencil = globalStencil();
104  StencilIteratorType end = glStencil.end();
105  for ( StencilIteratorType it = glStencil.begin(); it != end; ++it)
106  ret = std::max(ret,(int)it->second.size());
107  return ret;
108  }
109 
110  private:
111 
112  struct FillFunctor
113  {
114  typedef DomainGlobalKeyType GlobalKey;
115  FillFunctor(GlobalStencilType &stencil,bool fill)
116  : stencil_(stencil),
117  localStencil_(0),
118  fill_(fill)
119  {}
120  void set(const std::size_t, const DomainGlobalKeyType &domainGlobal)
121  {
122  localStencil_ = &(stencil_[ domainGlobal ]);
123  }
124  void operator() ( const std::size_t, const RangeGlobalKeyType &rangeGlobal)
125  {
126  if (fill_)
127  localStencil_->insert( rangeGlobal );
128  }
129  private:
130  GlobalStencilType &stencil_;
131  LocalStencilType *localStencil_;
132  bool fill_;
133  };
134  const DomainBlockMapper &domainBlockMapper_;
135  const RangeBlockMapper &rangeBlockMapper_;
136  GlobalStencilType globalStencil_;
137  };
138 
146  template <class DomainSpace, class RangeSpace>
148  {
150  public:
157 
158  SimpleStencil(int maxNZ)
159  : maxNZ_(maxNZ)
160  {}
162  {
163  maxNZ_ = 1;
164  }
166  {
167  return maxNZ_;
168  }
169  const LocalStencilType &localStencil(const DomainGlobalKeyType &key) const
170  {
171  DUNE_THROW( Dune::NotImplemented, "SimpleStencil: exact stencil information is unavailable." );
172  return localStencil_;
173  }
174  const GlobalStencilType &globalStencil() const
175  {
176  DUNE_THROW( Dune::NotImplemented, "SimpleStencil: global stencil is unavailable." );
177  return stencil_;
178  }
179  private:
180  int maxNZ_;
181  GlobalStencilType stencil_;
182  LocalStencilType localStencil_;
183  };
184 
193  template <class DomainSpace, class RangeSpace>
194  struct DiagonalStencil : public Stencil<DomainSpace,RangeSpace>
195  {
197  public:
204 
205  DiagonalStencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
206  : BaseType( dSpace, rSpace )
207  {
208  for (const auto& entity : dSpace)
209  BaseType::fill(entity,entity);
210  }
211  };
212 
222  template <class DomainSpace, class RangeSpace>
223  struct DiagonalAndNeighborStencil : public Stencil<DomainSpace,RangeSpace>
224  {
226  public:
233 
234  DiagonalAndNeighborStencil(const DomainSpace &dSpace, const RangeSpace &rSpace,
235  bool onlyNonContinuousNeighbors = false)
236  : BaseType( dSpace, rSpace )
237  {
238  for (const auto & entity: dSpace)
239  {
240  BaseType::fill(entity,entity);
241  typedef typename DomainSpace::GridPartType GridPart;
242  typedef typename GridPart :: IntersectionIteratorType IntersectionIteratorType;
243  typedef typename IntersectionIteratorType :: Intersection IntersectionType;
244 
245  const IntersectionIteratorType endit = dSpace.gridPart().iend( entity );
246  for( IntersectionIteratorType it = dSpace.gridPart().ibegin( entity );
247  it != endit; ++it )
248  {
249  const IntersectionType& intersection = *it;
250  if ( onlyNonContinuousNeighbors
251  && rSpace.continuous(intersection) && dSpace.continuous(intersection) )
252  continue;
253  if( intersection.neighbor() )
254  {
255  DomainEntityType neighbor = make_entity( intersection.outside() );
256  BaseType::fill(neighbor,entity);
257  }
258  }
259  }
260  }
261  };
262 
263  } // namespace Fem
264 
265 } // namespace Dune
266 
267 #endif // #if defined DUNE_FEM_STENCIL_HH
268 
a watered down stencil providing only the upper bound for the non-zero entries per row...
Definition: stencil.hh:147
BaseType::LocalStencilType LocalStencilType
Definition: stencil.hh:231
BaseType::LocalStencilType LocalStencilType
Definition: stencil.hh:202
BaseType::GlobalStencilType GlobalStencilType
Definition: stencil.hh:203
int maxNonZerosEstimate() const
Definition: stencil.hh:165
const LocalStencilType & localStencil(const DomainGlobalKeyType &key) const
Return stencil for a given row of the matrix.
Definition: stencil.hh:87
BaseType::DomainGlobalKeyType DomainGlobalKeyType
Definition: stencil.hh:229
BaseType::DomainGlobalKeyType DomainGlobalKeyType
Definition: stencil.hh:200
BaseType::DomainEntityType DomainEntityType
Definition: stencil.hh:198
default implementation for a general operator stencil
Definition: stencil.hh:31
Stencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
Constructor.
Definition: stencil.hh:59
static constexpr T max(T a)
Definition: utility.hh:65
Definition: misc/functor.hh:92
Stencil contaning the entries (en,en) and (en,nb) for all entities en in the space and neighbors nb o...
Definition: stencil.hh:223
BaseType::RangeEntityType RangeEntityType
Definition: stencil.hh:228
RangeBlockMapper::GlobalKeyType RangeGlobalKeyType
Definition: stencil.hh:45
DomainBlockMapper::GlobalKeyType DomainGlobalKeyType
Definition: stencil.hh:44
DomainIteratorType::Entity DomainEntityType
Definition: stencil.hh:42
DiagonalStencil(const DomainSpace &dSpace, const RangeSpace &rSpace)
Definition: stencil.hh:205
BaseType::RangeGlobalKeyType RangeGlobalKeyType
Definition: stencil.hh:201
std::set< RangeGlobalKeyType > LocalStencilType
type for storing the stencil of one row
Definition: stencil.hh:48
int maxNonZerosEstimate() const
Return an upper bound for the maximum number of non-zero entries in all row.
Definition: stencil.hh:99
StencilType::DomainGlobalKeyType DomainGlobalKeyType
Definition: stencil.hh:153
StencilType::DomainEntityType DomainEntityType
Definition: stencil.hh:151
const LocalStencilType & localStencil(const DomainGlobalKeyType &key) const
Definition: stencil.hh:169
Dune::EntityPointer< Grid, Implementation >::Entity make_entity(const Dune::EntityPointer< Grid, Implementation > &entityPointer)
Definition: compatibility.hh:23
Stencil< DomainSpace, RangeSpace > BaseType
Definition: stencil.hh:225
StencilType::GlobalStencilType GlobalStencilType
Definition: stencil.hh:156
Stencil< DomainSpace, RangeSpace > BaseType
Definition: stencil.hh:196
BaseType::GlobalStencilType GlobalStencilType
Definition: stencil.hh:232
Definition: coordinate.hh:4
SimpleStencil(int maxNZ)
Definition: stencil.hh:158
const GlobalStencilType & globalStencil() const
Return the full stencil.
Definition: stencil.hh:93
void fill(const DomainEntityType &dEntity, const RangeEntityType &rEntity, bool fillGhost=true)
Create stencil entries for (dEntity,rEntity) pair.
Definition: stencil.hh:72
StencilType::RangeEntityType RangeEntityType
Definition: stencil.hh:152
const GlobalStencilType & globalStencil() const
Definition: stencil.hh:174
Stencil contaning the entries (en,en) for all entities in the space. Defailt for an operator over a L...
Definition: stencil.hh:194
SimpleStencil()
Definition: stencil.hh:161
std::map< DomainGlobalKeyType, LocalStencilType > GlobalStencilType
type for storing the full stencil
Definition: stencil.hh:50
StencilType::LocalStencilType LocalStencilType
Definition: stencil.hh:155
BaseType::DomainEntityType DomainEntityType
Definition: stencil.hh:227
DiagonalAndNeighborStencil(const DomainSpace &dSpace, const RangeSpace &rSpace, bool onlyNonContinuousNeighbors=false)
Definition: stencil.hh:234
StencilType::RangeGlobalKeyType RangeGlobalKeyType
Definition: stencil.hh:154
BaseType::RangeEntityType RangeEntityType
Definition: stencil.hh:199
BaseType::RangeGlobalKeyType RangeGlobalKeyType
Definition: stencil.hh:230
RangeIteratorType::Entity RangeEntityType
Definition: stencil.hh:43