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