dune-fem  2.8-git
codimensionmapper.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SPACE_MAPPER_CODIMENSIONMAPPER_HH
2 #define DUNE_FEM_SPACE_MAPPER_CODIMENSIONMAPPER_HH
3 
4 #include <cassert>
5 
6 #include <algorithm>
7 #include <type_traits>
8 
9 #include <dune/common/exceptions.hh>
10 
11 #include <dune/geometry/referenceelements.hh>
12 #include <dune/geometry/type.hh>
13 
17 
18 namespace Dune
19 {
20 
21  namespace Fem
22  {
23 
24  // Internal forward declaration
25  // ----------------------------
26 
27  template< class GridPart, int codim >
28  class CodimensionMapper;
29 
30 
31 
32 #ifndef DOXYGEN
33 
34  namespace __CodimensionMapper
35  {
36 
37  // Traits
38  // ------
39 
40  template< class GridPart, int codim >
41  struct Traits
42  {
43  typedef CodimensionMapper< GridPart, codim > DofMapperType;
44 
45  static const int codimension = codim;
46 
47  typedef GridPart GridPartType;
48  typedef typename GridPartType::IndexSetType IndexSetType;
49 
50  typedef typename GridPartType::template Codim< 0 >::EntityType ElementType;
51  typedef typename IndexSetType::IndexType SizeType;
52  typedef SizeType GlobalKeyType;
53  };
54 
55 
56 
57  // DofMapper
58  // ---------
59 
60  template< class T, template< class > class Base = Dune::Fem::DofMapper >
61  class DofMapper
62  : public Base< T >
63  {
64  typedef Base< T > BaseType;
65 
66  public:
67  typedef typename BaseType::Traits Traits;
68 
69  static const int codimension = Traits::codimension;
70 
71  typedef typename Traits::GridPartType GridPartType;
72  typedef typename Traits::IndexSetType IndexSetType;
73 
74  typedef typename BaseType::ElementType ElementType;
75  typedef typename BaseType::SizeType SizeType;
76  typedef typename Traits::GlobalKeyType GlobalKeyType;
77 
78  explicit DofMapper ( const GridPartType &gridPart )
79  : DofMapper( gridPart.indexSet() )
80  {}
81 
82  explicit DofMapper ( const IndexSetType &indexSet )
83  : indexSet_( indexSet ),
84  extension_( 0 ),
85  maxNumDofs_( 0 )
86  {
87  AllGeomTypes< IndexSetType, typename GridPartType::GridType > types( indexSet );
88  for( GeometryType type : types.geomTypes( 0 ) )
89  maxNumDofs_ = std::max( maxNumDofs_, referenceElement( type ).size( codimension ) );
90 
91  // submit codimension request to index set to enable support
92  std::vector< int > codimensions( 1, int(Traits::codimension) );
93  indexSet_.requestCodimensions( codimensions );
94  }
95 
96  /* \name DofMapper interface methods
97  * \{
98  */
99 
100  SizeType size () const
101  {
102  return indexSet().size( codimension ) + extension_;
103  }
104 
105  static constexpr bool contains ( int codim ) noexcept
106  {
107  return codim == codimension;
108  }
109 
110  static constexpr bool fixedDataSize ( int codim ) noexcept
111  {
112  return true;
113  }
114 
115  template< class Function >
116  void mapEach ( const ElementType &element, Function function ) const
117  {
118  if( codimension == 0 )
119  function( 0, indexSet().index( element ) );
120  else
121  {
122  const SizeType numDofs = this->numDofs( element );
123  for( SizeType i = 0; i < numDofs; ++i )
124  function( i, indexSet().subIndex( element, i, codimension ) );
125  }
126  }
127 
128  template< class Entity, class Function >
129  void mapEachEntityDof ( const Entity &entity, Function function ) const
130  {
131  assert( Entity::codimension == codimension );
132  function( 0, indexSet().index( entity ) );
133  }
134 
135  int maxNumDofs () const { return maxNumDofs_; }
136 
137  SizeType numDofs ( const ElementType &element ) const
138  {
139  return element.subEntities( codimension );
140  }
141 
142  template< class Entity >
143  static constexpr SizeType numEntityDofs ( const Entity &entity ) noexcept
144  {
145  return contains( Entity::codimension ) ? 1 : 0;
146  }
147 
148  void update () {}
149 
150  void extendSize( const SizeType extension )
151  {
152  extension_ = extension;
153  }
154 
155  void onSubEntity ( const ElementType &element, int i, int c, std::vector< bool > &indices ) const
156  {
157  indices.resize( numDofs(element) );
158  std::fill(indices.begin(),indices.end(),false);
159  indices[i] = true;
160  }
161 
162  /* \} */
163 
164  /* \name AdaptiveDofMapper interface methods
165  * \{
166  */
167 
168  /* Compatibility methods; users expect an AdaptiveDiscreteFunction to
169  * compile over spaces built on top of a LeafGridPart or LevelGridPart.
170  *
171  * The AdaptiveDiscreteFunction requires the block mapper (i.e. this
172  * type) to be adaptive. The CodimensionMapper however is truly
173  * adaptive if and only if the underlying index set is adaptive. We
174  * don't want to wrap the index set as 1) it hides the actual problem
175  * (don't use the AdaptiveDiscreteFunction with non-adaptive index
176  * sets), and 2) other dune-fem classes may make correct use of the
177  * index set's capabilities.
178  */
179 
180  static constexpr bool consecutive () noexcept { return false; }
181 
182  SizeType numBlocks () const
183  {
184  DUNE_THROW( NotImplemented, "Method numBlocks() called on non-adaptive block mapper" );
185  }
186 
187  SizeType numberOfHoles ( int ) const
188  {
189  DUNE_THROW( NotImplemented, "Method numberOfHoles() called on non-adaptive block mapper" );
190  }
191 
192  GlobalKeyType oldIndex ( int hole, int ) const
193  {
194  DUNE_THROW( NotImplemented, "Method oldIndex() called on non-adaptive block mapper" );
195  }
196 
197  GlobalKeyType newIndex ( int hole, int ) const
198  {
199  DUNE_THROW( NotImplemented, "Method newIndex() called on non-adaptive block mapper" );
200  }
201 
202  SizeType oldOffSet ( int ) const
203  {
204  DUNE_THROW( NotImplemented, "Method oldOffSet() called on non-adaptive block mapper" );
205  }
206 
207  SizeType offSet ( int ) const
208  {
209  DUNE_THROW( NotImplemented, "Method offSet() called on non-adaptive block mapper" );
210  }
211 
212  /* \} */
213 
214  protected:
215  const IndexSetType &indexSet () const { return indexSet_; }
216 
217  private:
218  static auto referenceElement ( GeometryType type )
219  -> decltype( ReferenceElements< typename GridPartType::ctype, GridPartType::dimension >::general( type ) )
220  {
221  return ReferenceElements< typename GridPartType::ctype, GridPartType::dimension >::general( type );
222  }
223 
224  const IndexSetType &indexSet_;
225  SizeType extension_;
226  int maxNumDofs_;
227  };
228 
229 
230 
231  // AdaptiveDofMapper
232  // -----------------
233 
234  template< class Traits >
235  class AdaptiveDofMapper
236  : public DofMapper< Traits, Dune::Fem::AdaptiveDofMapper >
237  {
238  typedef DofMapper< Traits, Dune::Fem::AdaptiveDofMapper > BaseType;
239 
240  public:
241  static const int codimension = BaseType::codimension;
242 
243  typedef typename BaseType::SizeType SizeType;
244  typedef typename BaseType::GlobalKeyType GlobalKeyType;
245 
246  protected:
247  using BaseType::indexSet;
248 
249  public:
250  explicit AdaptiveDofMapper ( const typename BaseType::GridPartType &gridPart )
251  : BaseType( gridPart )
252  {}
253 
254  explicit AdaptiveDofMapper ( const typename BaseType::IndexSetType &indexSet )
255  : BaseType( indexSet )
256  {}
257 
258  static constexpr bool consecutive () noexcept { return true; }
259 
260  static constexpr SizeType numBlocks () noexcept
261  {
262  return 1;
263  }
264 
265  SizeType numberOfHoles ( int ) const
266  {
267  return indexSet().numberOfHoles( codimension );
268  }
269 
270  GlobalKeyType oldIndex ( int hole, int ) const
271  {
272  return indexSet().oldIndex( hole, codimension );
273  }
274 
275  GlobalKeyType newIndex ( int hole, int ) const
276  {
277  return indexSet().newIndex( hole, codimension );
278  }
279 
280  static constexpr SizeType oldOffSet ( int ) noexcept
281  {
282  return 0;
283  }
284 
285  static constexpr SizeType offSet ( int ) noexcept
286  {
287  return 0;
288  }
289  };
290 
291 
292 
293  // Implementation
294  // --------------
295 
296  template< class GridPart, int codim,
298  class Implementation
299  {
300  typedef __CodimensionMapper::Traits< GridPart, codim > Traits;
301 
302  public:
303  typedef typename std::conditional< adaptive,
304  AdaptiveDofMapper< Traits >,
305  DofMapper< Traits >
306  >::type Type;
307  };
308 
309  } // namespace __CodimensionMapper
310 
311 #endif // #ifndef DOXYGEN
312 
313 
314 
315  // CodimensionMapper
316  // -----------------
317 
327  template< class GridPart, int codim >
329  : public __CodimensionMapper::template Implementation< GridPart, codim >::Type
330  {
331  typedef typename __CodimensionMapper::template Implementation< GridPart, codim >::Type BaseType;
332 
333  public:
334  explicit CodimensionMapper ( const typename BaseType::GridPartType &gridPart )
335  : BaseType( gridPart )
336  {}
337 
338  explicit CodimensionMapper ( const typename BaseType::IndexSetType *indexSet )
339  : BaseType( *indexSet )
340  {}
341 
342  explicit CodimensionMapper ( const typename BaseType::IndexSetType &indexSet )
343  : BaseType( indexSet )
344  {}
345  };
346 
347 
348  // Capabilities
349  // ------------
350 
351  namespace Capabilities
352  {
353  // isAdaptiveDofMapper
354  // -------------------
355 
356  template< class GridPart, int codim >
357  struct isAdaptiveDofMapper< CodimensionMapper< GridPart, codim > >
358  {
360  };
361 
362 
363  // isConsecutiveIndexSet
364  // ---------------------
365 
366  template< class GridPart, int codim >
367  struct isConsecutiveIndexSet< __CodimensionMapper::AdaptiveDofMapper< __CodimensionMapper::Traits< GridPart, codim > > >
368  {
369  static const bool v = true;
370  };
371 
372  } // namespace Capabilities
373 
374  } // namespace Fem
375 
376 } // namespace Dune
377 
378 #endif // #ifndef DUNE_FEM_SPACE_MAPPER_CODIMENSIONMAPPER_HH
Definition: bindguard.hh:11
typename Impl::Index< Range >::Type IndexType
Definition: hybrid.hh:69
static constexpr T max(T a)
Definition: utility.hh:77
specialize with true if index set implements the interface for consecutive index sets
Definition: common/indexset.hh:42
static const bool v
Definition: common/indexset.hh:49
specialize with true if index set implements the interface for adaptive index sets
Definition: common/indexset.hh:64
static const bool v
Definition: common/indexset.hh:71
Definition: space/mapper/capabilities.hh:22
static const bool v
Definition: space/mapper/capabilities.hh:23
mapper allocating one DoF per subentity of a given codimension
Definition: codimensionmapper.hh:330
CodimensionMapper(const typename BaseType::IndexSetType &indexSet)
Definition: codimensionmapper.hh:342
CodimensionMapper(const typename BaseType::IndexSetType *indexSet)
Definition: codimensionmapper.hh:338
CodimensionMapper(const typename BaseType::GridPartType &gridPart)
Definition: codimensionmapper.hh:334
Interface for calculating the size of a function space for a grid on a specified level....
Definition: mapper/dofmapper.hh:43
Extended interface for adaptive DoF mappers.
Definition: mapper/dofmapper.hh:219