dune-fem  2.6-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  maxNumDofs_( 0 )
85  {
86  AllGeomTypes< IndexSetType, typename GridPartType::GridType > types( indexSet );
87  for( GeometryType type : types.geomTypes( 0 ) )
88  maxNumDofs_ = std::max( maxNumDofs_, referenceElement( type ).size( codimension ) );
89 
90  // submit codimension request to index set to enable support
91  std::vector< int > codimensions( 1, int(Traits::codimension) );
92  indexSet_.requestCodimensions( codimensions );
93  }
94 
95  /* \name DofMapper interface methods
96  * \{
97  */
98 
99  SizeType size () const
100  {
101  return indexSet().size( codimension );
102  }
103 
104  static constexpr bool contains ( int codim ) noexcept
105  {
106  return codim == codimension;
107  }
108 
109  static constexpr bool fixedDataSize ( int codim ) noexcept
110  {
111  return true;
112  }
113 
114  template< class Function >
115  void mapEach ( const ElementType &element, Function function ) const
116  {
117  if( codimension == 0 )
118  function( 0, indexSet().index( element ) );
119  else
120  {
121  const SizeType numDofs = this->numDofs( element );
122  for( SizeType i = 0; i < numDofs; ++i )
123  function( i, indexSet().subIndex( element, i, codimension ) );
124  }
125  }
126 
127  template< class Entity, class Function >
128  void mapEachEntityDof ( const Entity &entity, Function function ) const
129  {
130  assert( Entity::codimension == codimension );
131  function( 0, indexSet().index( entity ) );
132  }
133 
134  int maxNumDofs () const { return maxNumDofs_; }
135 
136  SizeType numDofs ( const ElementType &element ) const
137  {
138  return element.subEntities( codimension );
139  }
140 
141  template< class Entity >
142  static constexpr SizeType numEntityDofs ( const Entity &entity ) noexcept
143  {
144  return contains( Entity::codimension ) ? 1 : 0;
145  }
146 
147  void update () {}
148 
149  /* \} */
150 
151  /* \name AdaptiveDofMapper interface methods
152  * \{
153  */
154 
155  /* Compatibility methods; users expect an AdaptiveDiscreteFunction to
156  * compile over spaces built on top of a LeafGridPart or LevelGridPart.
157  *
158  * The AdaptiveDiscreteFunction requires the block mapper (i.e. this
159  * type) to be adaptive. The CodimensionMapper however is truly
160  * adaptive if and only if the underlying index set is adaptive. We
161  * don't want to wrap the index set as 1) it hides the actual problem
162  * (don't use the AdaptiveDiscreteFunction with non-adaptive index
163  * sets), and 2) other dune-fem classes may make correct use of the
164  * index set's capabilities.
165  */
166 
167  static constexpr bool consecutive () noexcept { return false; }
168 
169  SizeType numBlocks () const
170  {
171  DUNE_THROW( NotImplemented, "Method numBlocks() called on non-adaptive block mapper" );
172  }
173 
174  SizeType numberOfHoles ( int ) const
175  {
176  DUNE_THROW( NotImplemented, "Method numberOfHoles() called on non-adaptive block mapper" );
177  }
178 
179  GlobalKeyType oldIndex ( int hole, int ) const
180  {
181  DUNE_THROW( NotImplemented, "Method oldIndex() called on non-adaptive block mapper" );
182  }
183 
184  GlobalKeyType newIndex ( int hole, int ) const
185  {
186  DUNE_THROW( NotImplemented, "Method newIndex() called on non-adaptive block mapper" );
187  }
188 
189  SizeType oldOffSet ( int ) const
190  {
191  DUNE_THROW( NotImplemented, "Method oldOffSet() called on non-adaptive block mapper" );
192  }
193 
194  SizeType offSet ( int ) const
195  {
196  DUNE_THROW( NotImplemented, "Method offSet() called on non-adaptive block mapper" );
197  }
198 
199  /* \} */
200 
201  protected:
202  const IndexSetType &indexSet () const { return indexSet_; }
203 
204  private:
205  static auto referenceElement ( GeometryType type )
206  -> decltype( ReferenceElements< typename GridPartType::ctype, GridPartType::dimension >::general( type ) )
207  {
208  return ReferenceElements< typename GridPartType::ctype, GridPartType::dimension >::general( type );
209  }
210 
211  const IndexSetType &indexSet_;
212  int maxNumDofs_;
213  };
214 
215 
216 
217  // AdaptiveDofMapper
218  // -----------------
219 
220  template< class Traits >
221  class AdaptiveDofMapper
222  : public DofMapper< Traits, Dune::Fem::AdaptiveDofMapper >
223  {
224  typedef DofMapper< Traits, Dune::Fem::AdaptiveDofMapper > BaseType;
225 
226  public:
227  static const int codimension = BaseType::codimension;
228 
229  typedef typename BaseType::SizeType SizeType;
230  typedef typename BaseType::GlobalKeyType GlobalKeyType;
231 
232  protected:
233  using BaseType::indexSet;
234 
235  public:
236  explicit AdaptiveDofMapper ( const typename BaseType::GridPartType &gridPart )
237  : BaseType( gridPart )
238  {}
239 
240  explicit AdaptiveDofMapper ( const typename BaseType::IndexSetType &indexSet )
241  : BaseType( indexSet )
242  {}
243 
244  static constexpr bool consecutive () noexcept { return true; }
245 
246  static constexpr SizeType numBlocks () noexcept
247  {
248  return 1;
249  }
250 
251  SizeType numberOfHoles ( int ) const
252  {
253  return indexSet().numberOfHoles( codimension );
254  }
255 
256  GlobalKeyType oldIndex ( int hole, int ) const
257  {
258  return indexSet().oldIndex( hole, codimension );
259  }
260 
261  GlobalKeyType newIndex ( int hole, int ) const
262  {
263  return indexSet().newIndex( hole, codimension );
264  }
265 
266  static constexpr SizeType oldOffSet ( int ) noexcept
267  {
268  return 0;
269  }
270 
271  static constexpr SizeType offSet ( int ) noexcept
272  {
273  return 0;
274  }
275  };
276 
277 
278 
279  // Implementation
280  // --------------
281 
282  template< class GridPart, int codim,
284  class Implementation
285  {
286  typedef __CodimensionMapper::Traits< GridPart, codim > Traits;
287 
288  public:
289  typedef typename std::conditional< adaptive,
290  AdaptiveDofMapper< Traits >,
291  DofMapper< Traits >
292  >::type Type;
293  };
294 
295  } // namespace __CodimensionMapper
296 
297 #endif // #ifndef DOXYGEN
298 
299 
300 
301  // CodimensionMapper
302  // -----------------
303 
313  template< class GridPart, int codim >
315  : public __CodimensionMapper::template Implementation< GridPart, codim >::Type
316  {
317  typedef typename __CodimensionMapper::template Implementation< GridPart, codim >::Type BaseType;
318 
319  public:
320  explicit CodimensionMapper ( const typename BaseType::GridPartType &gridPart )
321  : BaseType( gridPart )
322  {}
323 
324  explicit CodimensionMapper ( const typename BaseType::IndexSetType &indexSet )
325  : BaseType( indexSet )
326  {}
327  };
328 
329 
330 
331  // CodimensionMapperSingletonFactory
332  // ---------------------------------
333 
334  template< class GridPart, int codim >
336  {
337  public:
339 
340  struct Key
341  {
342  Key ( const GridPart &gridPart )
343  : gridPart_( gridPart )
344  {}
345 
346  bool operator== ( const Key &rhs )
347  {
348  return &gridPart_.indexSet() == &rhs.gridPart_.indexSet();
349  }
350 
351  bool operator!= ( const Key &rhs )
352  {
353  return !(*this == rhs);
354  }
355 
356  explicit operator const GridPart & () const
357  {
358  return gridPart_;
359  }
360 
361  private:
362  const GridPart &gridPart_;
363  };
364 
365  static Object *createObject ( const Key &key )
366  {
367  return new Object( static_cast< const GridPart & >( key ) );
368  }
369 
370  static void deleteObject ( Object *object )
371  {
372  delete object;
373  }
374  };
375 
376 
377  // Capabilities
378  // ------------
379 
380  namespace Capabilities
381  {
382  // isAdaptiveDofMapper
383  // -------------------
384 
385  template< class GridPart, int codim >
386  struct isAdaptiveDofMapper< CodimensionMapper< GridPart, codim > >
387  {
389  };
390 
391 
392  // isConsecutiveIndexSet
393  // ---------------------
394 
395  template< class GridPart, int codim >
396  struct isConsecutiveIndexSet< __CodimensionMapper::AdaptiveDofMapper< __CodimensionMapper::Traits< GridPart, codim > > >
397  {
398  static const bool v = true;
399  };
400 
401  } // namespace Capabilities
402 
403  } // namespace Fem
404 
405 } // namespace Dune
406 
407 #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:316
CodimensionMapper(const typename BaseType::IndexSetType &indexSet)
Definition: codimensionmapper.hh:324
CodimensionMapper(const typename BaseType::GridPartType &gridPart)
Definition: codimensionmapper.hh:320
Definition: codimensionmapper.hh:336
static Object * createObject(const Key &key)
Definition: codimensionmapper.hh:365
static void deleteObject(Object *object)
Definition: codimensionmapper.hh:370
CodimensionMapper< GridPart, codim > Object
Definition: codimensionmapper.hh:338
Definition: codimensionmapper.hh:341
bool operator==(const Key &rhs)
Definition: codimensionmapper.hh:346
Key(const GridPart &gridPart)
Definition: codimensionmapper.hh:342
bool operator!=(const Key &rhs)
Definition: codimensionmapper.hh:351
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