dune-spgrid 2.7
gridlevel.hh
Go to the documentation of this file.
1#ifndef DUNE_SPGRID_GRIDLEVEL_HH
2#define DUNE_SPGRID_GRIDLEVEL_HH
3
4#include <cassert>
5#include <vector>
6#include <type_traits>
7
17
18namespace Dune
19{
20
21 // External Forward Declarations
22 // -----------------------------
23
24 template< int mydim, int cdim, class Grid >
25 class SPLocalGeometry;
26
27
28
29 // SPGridLevel
30 // -----------
31
32 template< class Grid >
34 : public SPGeometricGridLevel< typename std::remove_const< Grid >::type::ctype, std::remove_const< Grid >::type::dimension >
35 {
37 typedef SPGeometricGridLevel< typename std::remove_const< Grid >::type::ctype, std::remove_const< Grid >::type::dimension > Base;
38
39 public:
41
43 typedef typename Base::ctype ctype;
44
45 static const int dimension = Base::dimension;
46 static const unsigned int numDirections = Base::numDirections;
48
49 typedef typename std::remove_const< Grid >::type::Traits Traits;
50
51 typedef typename Traits::Domain Domain;
52 typedef typename Traits::Refinement Refinement;
53 typedef typename Traits::RefinementPolicy RefinementPolicy;
54
57
61
62 typedef typename Decomposition::Mesh Mesh;
63
65
66 typedef typename Linkage::Interface CommInterface;
67
68 typedef typename Traits::template Codim< 0 >::LocalGeometry LocalGeometry;
69
70 private:
72
73 public:
74 SPGridLevel ( const Grid &grid, const Decomposition &decomposition );
75 SPGridLevel ( const GridLevel &father, const RefinementPolicy &policy );
76 SPGridLevel ( const This &other );
77
78 ~SPGridLevel ();
79
81
82 const Grid &grid () const { return *grid_; }
83 int level () const { return level_; }
84 const Domain &domain () const { return domain_; }
85 const Refinement &refinement () const { return refinement_; }
86
87 const Mesh &globalMesh () const;
88 const Mesh &localMesh () const;
89
90 template< PartitionIteratorType pitype >
91 const PartitionList &partition () const;
92
93 const PartitionList &boundaryPartition ( int face ) const;
94
95 template< int codim >
96 PartitionType
97 partitionType ( const MultiIndex &id, const unsigned int partitionNumber ) const;
98
99 const CommInterface &commInterface ( const InterfaceType iftype ) const;
100
101 MultiIndex macroId ( const MultiIndex &id ) const;
102
103 size_t boundaryIndex ( const MultiIndex &id,
104 const unsigned int partitionNumber,
105 const int face ) const;
106
107 LocalGeometry geometryInFather ( const MultiIndex &id ) const;
108
109 int size () const;
110
111 private:
112 void buildLocalGeometry ();
113 void buildBoundaryPartitions ();
114
115 static MultiIndex coarseMacroFactor ();
116 static GlobalVector meshWidth ( const Domain &domain, const Mesh &mesh );
117 static MultiIndex refineWidth ( const MultiIndex &width, const Refinement &refinement );
118
119 MultiIndex overlap () const;
120
121 const Grid *grid_;
122 int level_;
123
124 const Refinement refinement_;
125 MultiIndex macroFactor_;
126 Domain domain_;
127 std::vector< Mesh > decomposition_;
128 Mesh localMesh_;
129 PartitionPool partitionPool_;
130 Linkage linkage_;
131
132 LocalGeometryImpl **geometryInFather_;
133
134 PartitionList boundaryPartition_[ numFaces+1 ];
135 };
136
137
138
139 // Implementation of SPGridLevel
140 // -----------------------------
141
142 template< class Grid >
144 ::SPGridLevel ( const Grid &grid, const Decomposition &decomposition )
145 : Base( grid.refCubes_, meshWidth( grid.domain(), decomposition.mesh() ) ),
146 grid_( &grid ),
147 level_( 0 ),
148 refinement_(),
149 macroFactor_( coarseMacroFactor() ),
150 domain_( grid.domain() ),
151 decomposition_( decomposition.subMeshes() ),
152 localMesh_( decomposition_[ grid.comm().rank() ] ),
153 partitionPool_( localMesh_, decomposition.mesh(), overlap(), domain_.topology() ),
154 linkage_( grid.comm().rank(), partitionPool_, decomposition_ )
155 {
156 buildLocalGeometry();
157 buildBoundaryPartitions();
158 }
159
160
161 template< class Grid >
162 inline SPGridLevel< Grid >::SPGridLevel ( const GridLevel &father, const RefinementPolicy &policy )
163 : Base( father.grid().refCubes_, meshWidth( father.domain(), father.globalMesh().refine( Refinement( father.refinement(), policy ) ) ) ),
164 grid_( father.grid_ ),
165 level_( father.level() + 1 ),
166 refinement_( father.refinement(), policy ),
167 macroFactor_( refineWidth( father.macroFactor_, refinement_ ) ),
168 domain_( father.domain() ),
169 decomposition_( transform( father.decomposition_, [ this ]( const Mesh &mesh ) { return mesh.refine( refinement_ ); } ) ),
170 localMesh_( father.localMesh().refine( refinement_ ) ),
171 partitionPool_( localMesh_, father.globalMesh().refine( refinement_ ), overlap(), domain_.topology() ),
172 linkage_( father.grid().comm().rank(), partitionPool_, decomposition_ )
173 {
174 buildLocalGeometry();
175 buildBoundaryPartitions();
176 }
177
178
179 template< class Grid >
181 : Base( other ),
182 grid_( other.grid_ ),
183 refinement_( other.refinement_ ),
184 macroFactor_( other.macroFactor_ ),
185 domain_( other.domain_ ),
186 decomposition_( other.decomposition_ ),
187 localMesh_( other.localMesh_ ),
188 partitionPool_( other.partitionPool_ ),
189 linkage_( other.linkage_ )
190 {
191 buildLocalGeometry();
192 buildBoundaryPartitions();
193 }
194
195
196 template< class Grid >
198 {
199 if( geometryInFather_ )
200 {
201 unsigned int numChildren = refinement().numChildren();
202 for( unsigned int index = 0; index < numChildren; ++index )
203 delete geometryInFather_[ index ];
204 delete geometryInFather_;
205 }
206 }
207
208 template< class Grid >
209 inline const typename SPGridLevel< Grid >::Mesh &
211 {
212 return partitionPool_.globalMesh();
213 }
214
215
216 template< class Grid >
217 inline const typename SPGridLevel< Grid >::Mesh &
219 {
220 return localMesh_;
221 }
222
223
224 template< class Grid >
225 template< PartitionIteratorType pitype >
226 inline const typename SPGridLevel< Grid >::PartitionList &
228 {
229 return partitionPool_.template get< pitype >();
230 }
231
232
233 template< class Grid >
234 inline const typename SPGridLevel< Grid >::PartitionList &
236 {
237 assert( (face >= 0) && (face <= numFaces) );
238 return boundaryPartition_[ face ];
239 }
240
241
242 template< class Grid >
243 template< int codim >
244 inline PartitionType SPGridLevel< Grid >
245 ::partitionType ( const MultiIndex &id, const unsigned int partitionNumber ) const
246 {
247 return partitionPool_.template partitionType< codim >( id, partitionNumber );
248 }
249
250
251 template< class Grid >
252 inline const typename SPGridLevel< Grid >::CommInterface &
253 SPGridLevel< Grid >::commInterface ( const InterfaceType iftype ) const
254 {
255 return linkage_.interface( iftype );
256 }
257
258
259 template< class Grid >
260 inline typename SPGridLevel< Grid >::MultiIndex
262 {
263 MultiIndex macroId;
264 for( int i = 0; i < dimension; ++i )
265 macroId[ i ] = (((id[ i ] >> 1) / macroFactor_[ i ]) << 1) | (id[ i ] & 1);
266 return macroId;
267 }
268
269
270 template< class Grid >
271 inline size_t SPGridLevel< Grid >
273 const unsigned int partitionNumber,
274 const int face ) const
275 {
276 // note: boundaryIndex ignores the last bit of macroId,
277 // hence we can use this fast computation
278 MultiIndex macroId;
279 for( int i = 0; i < dimension; ++i )
280 macroId[ i ] = id[ i ] / macroFactor_[ i ];
281 return grid().boundaryIndex( macroId, partitionNumber, face );
282 }
283
284
285 template< class Grid >
288 {
289 assert( (level() > 0) && (geometryInFather_ != 0) );
290 return LocalGeometry( *(geometryInFather_[ refinement().childIndex( id ) ]) );
291 }
292
293
294 template< class Grid >
295 inline int SPGridLevel< Grid >::size () const
296 {
297 return globalMesh().volume();
298 }
299
300
301 template< class Grid >
303 {
304 geometryInFather_ = 0;
305 if( level() > 0 )
306 {
307 const unsigned int numChildren = refinement().numChildren();
308 geometryInFather_ = new LocalGeometryImpl *[ numChildren ];
309 const GlobalVector hInFather = refinement().template hInFather< ctype >();
311 const typename Base::template Codim< 0 >::GeometryCache cacheInFather( hInFather, *dirIt );
312 for( unsigned int index = 0; index < numChildren; ++index )
313 {
314 const GlobalVector origin = refinement().template originInFather< ctype >( index );
315 geometryInFather_[ index ] = new LocalGeometryImpl( cacheInFather, origin );
316 }
317 }
318 }
319
320
321 template< class Grid >
322 inline void SPGridLevel< Grid >::buildBoundaryPartitions ()
323 {
324 const Mesh &globalMesh = This::globalMesh();
325
326 for( int face = 0; face < numFaces; ++face )
327 {
328 const int i = face >> 1;
329
330 // iterate over all partitions
331 const PartitionList &plist = partition< All_Partition >();
332 const typename PartitionList::Iterator end = plist.end();
333 for( typename PartitionList::Iterator it = plist.begin(); it != end; ++it )
334 {
335 // get partition
336 typedef typename PartitionList::Partition Partition;
337 const Partition &partition = *it;
338
339 // get partition bounds
340 MultiIndex bound[ 2 ];
341 bound[ 0 ] = partition.begin();
342 bound[ 1 ] = partition.end();
343
344 // shrink partition bounds to face bounds
345 int bnd = (face & 1)*bound[ 1 ][ i ] + (1 - (face & 1))*bound[ 0 ][ i ];
346 bound[ 0 ][ i ] = bnd;
347 bound[ 1 ][ i ] = bnd;
348
349 // insert partition iff it is part of the global boundary (see also intersection.hh)
350 if( bnd == 2*globalMesh.bound( face & 1 )[ i ] )
351 boundaryPartition_[ face ] += Partition( bound[ 0 ], bound[ 1 ], partition.number() );
352 }
353 }
354 }
355
356
357 template< class Grid >
358 inline typename SPGridLevel< Grid >::MultiIndex
359 SPGridLevel< Grid >::coarseMacroFactor ()
360 {
361 MultiIndex macroFactor;
362 for( int i = 0; i < dimension; ++i )
363 macroFactor[ i ] = 1;
364 return macroFactor;
365 }
366
367
368 template< class Grid >
370 SPGridLevel< Grid >::meshWidth ( const Domain &domain, const Mesh &mesh )
371 {
372 GlobalVector h = domain.cube().width();
373 const MultiIndex meshWidth = mesh.width();
374 for( int i = 0; i < dimension; ++i )
375 h[ i ] /= ctype( meshWidth[ i ] );
376 return h;
377 }
378
379
380 template< class Grid >
381 inline typename SPGridLevel< Grid >::MultiIndex
382 SPGridLevel< Grid >::refineWidth ( const MultiIndex &id, const Refinement &refinement )
383 {
384 MultiIndex result;
385 for( int i = 0; i < dimension; ++i )
386 result[ i ] = id[ i ] * refinement.factor( i );
387 return result;
388 }
389
390
391 template< class Grid >
392 inline typename SPGridLevel< Grid >::MultiIndex
393 SPGridLevel< Grid >::overlap () const
394 {
395 MultiIndex overlap;
396 for( int i = 0; i < dimension; ++i )
397 overlap[ i ] = macroFactor_[ i ] * grid().overlap()[ i ];
398 return overlap;
399 }
400
401} // namespace Dune
402
403#endif // #ifndef DUNE_SPGRID_GRIDLEVEL_HH
description of computational domain
miscellaneous helper functions
Definition: iostream.hh:7
std::vector< decltype(std::declval< Op >()(std::declval< T >())) > transform(const std::vector< T > &in, Op op)
copy a vector, performing an operation on each element
Definition: misc.hh:24
Definition: decomposition.hh:17
Definition: direction.hh:157
Definition: geometricgridlevel.hh:24
const ReferenceCube & referenceCube() const
Definition: geometricgridlevel.hh:53
static const unsigned int numDirections
Definition: geometricgridlevel.hh:37
ReferenceCube::ctype ctype
Definition: geometricgridlevel.hh:32
static const int dimension
Definition: geometricgridlevel.hh:33
Definition: geometry.hh:161
Definition: gridlevel.hh:35
Traits::Domain Domain
Definition: gridlevel.hh:51
Base::ctype ctype
Definition: gridlevel.hh:43
const Mesh & globalMesh() const
Definition: gridlevel.hh:210
Traits::Refinement Refinement
Definition: gridlevel.hh:52
const Refinement & refinement() const
Definition: gridlevel.hh:85
PartitionType partitionType(const MultiIndex &id, const unsigned int partitionNumber) const
Definition: gridlevel.hh:245
Decomposition::Mesh Mesh
Definition: gridlevel.hh:62
static const int dimension
Definition: gridlevel.hh:45
const Mesh & localMesh() const
Definition: gridlevel.hh:218
SPGridLevel< Grid > GridLevel
Definition: gridlevel.hh:40
Traits::template Codim< 0 >::LocalGeometry LocalGeometry
Definition: gridlevel.hh:68
static const unsigned int numDirections
Definition: gridlevel.hh:46
static const int numFaces
Definition: gridlevel.hh:47
std::remove_const< Grid >::type::Traits Traits
Definition: gridlevel.hh:49
Base::ReferenceCube ReferenceCube
Definition: gridlevel.hh:42
Linkage::Interface CommInterface
Definition: gridlevel.hh:66
int level() const
Definition: gridlevel.hh:83
const PartitionList & boundaryPartition(int face) const
Definition: gridlevel.hh:235
ReferenceCube::GlobalVector GlobalVector
Definition: gridlevel.hh:55
SPDecomposition< dimension > Decomposition
Definition: gridlevel.hh:58
MultiIndex macroId(const MultiIndex &id) const
Definition: gridlevel.hh:261
int size() const
Definition: gridlevel.hh:295
const PartitionList & partition() const
Definition: gridlevel.hh:227
Traits::RefinementPolicy RefinementPolicy
Definition: gridlevel.hh:53
SPGridLevel(const Grid &grid, const Decomposition &decomposition)
Definition: gridlevel.hh:144
size_t boundaryIndex(const MultiIndex &id, const unsigned int partitionNumber, const int face) const
Definition: gridlevel.hh:272
const CommInterface & commInterface(const InterfaceType iftype) const
Definition: gridlevel.hh:253
SPPartitionPool< dimension > PartitionPool
Definition: gridlevel.hh:59
const Grid & grid() const
Definition: gridlevel.hh:82
const Domain & domain() const
Definition: gridlevel.hh:84
~SPGridLevel()
Definition: gridlevel.hh:197
ReferenceCube::MultiIndex MultiIndex
Definition: gridlevel.hh:56
PartitionPool::PartitionList PartitionList
Definition: gridlevel.hh:64
SPLinkage< dimension > Linkage
Definition: gridlevel.hh:60
LocalGeometry geometryInFather(const MultiIndex &id) const
Definition: gridlevel.hh:287
Definition: linkage.hh:54
Definition: referencecube.hh:43
static const int numFaces
Definition: referencecube.hh:55
FieldVector< ctype, dimension > GlobalVector
Definition: referencecube.hh:51