Dune Core Modules (2.9.0)

gridlevel.hh
1#ifndef DUNE_SPGRID_GRIDLEVEL_HH
2#define DUNE_SPGRID_GRIDLEVEL_HH
3
4#include <cassert>
5#include <vector>
6#include <type_traits>
7
8#include <dune/grid/spgrid/direction.hh>
9#include <dune/grid/spgrid/geometricgridlevel.hh>
11#include <dune/grid/spgrid/refinement.hh>
13#include <dune/grid/spgrid/mesh.hh>
14#include <dune/grid/spgrid/partitionpool.hh>
15#include <dune/grid/spgrid/linkage.hh>
16#include <dune/grid/spgrid/decomposition.hh>
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 >
33 class SPGridLevel
34 : public SPGeometricGridLevel< typename std::remove_const< Grid >::type::ctype, std::remove_const< Grid >::type::dimension >
35 {
36 typedef SPGridLevel< Grid > This;
37 typedef SPGeometricGridLevel< typename std::remove_const< Grid >::type::ctype, std::remove_const< Grid >::type::dimension > Base;
38
39 public:
40 typedef SPGridLevel< Grid > GridLevel;
41
42 typedef typename Base::ReferenceCube ReferenceCube;
43 typedef typename Base::ctype ctype;
44
45 static const int dimension = Base::dimension;
46 static const unsigned int numDirections = Base::numDirections;
47 static const int numFaces = ReferenceCube::numFaces;
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
55 typedef typename ReferenceCube::GlobalVector GlobalVector;
56 typedef typename ReferenceCube::MultiIndex MultiIndex;
57
58 typedef SPDecomposition< dimension > Decomposition;
59 typedef SPPartitionPool< dimension > PartitionPool;
60 typedef SPLinkage< dimension > Linkage;
61
62 typedef typename Decomposition::Mesh Mesh;
63
64 typedef typename PartitionPool::PartitionList PartitionList;
65
66 typedef typename Linkage::Interface CommInterface;
67
68 typedef typename Traits::template Codim< 0 >::LocalGeometry LocalGeometry;
69
70 private:
71 typedef SPLocalGeometry< dimension, dimension, const Grid > LocalGeometryImpl;
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
80 using Base::referenceCube;
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 >
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 >
143 inline SPGridLevel< 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 >
180 inline SPGridLevel< Grid >::SPGridLevel ( const This &other )
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 >
197 inline SPGridLevel< Grid >::~SPGridLevel ()
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 &
210 SPGridLevel< Grid >::globalMesh () const
211 {
212 return partitionPool_.globalMesh();
213 }
214
215
216 template< class Grid >
217 inline const typename SPGridLevel< Grid >::Mesh &
218 SPGridLevel< Grid >::localMesh () const
219 {
220 return localMesh_;
221 }
222
223
224 template< class Grid >
225 template< PartitionIteratorType pitype >
226 inline const typename SPGridLevel< Grid >::PartitionList &
227 SPGridLevel< Grid >::partition () const
228 {
229 return partitionPool_.template get< pitype >();
230 }
231
232
233 template< class Grid >
234 inline const typename SPGridLevel< Grid >::PartitionList &
235 SPGridLevel< Grid >::boundaryPartition ( int face ) const
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
261 SPGridLevel< Grid >::macroId ( const MultiIndex &id ) const
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 >
272 ::boundaryIndex ( const MultiIndex &id,
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 >
286 inline typename SPGridLevel< Grid >::LocalGeometry
287 SPGridLevel< Grid >::geometryInFather ( const MultiIndex &id ) const
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 >
302 inline void SPGridLevel< Grid >::buildLocalGeometry ()
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 >();
310 SPDirectionIterator< dimension, 0 > dirIt;
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 >
369 inline typename SPGridLevel< Grid >::GlobalVector
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
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:30
InterfaceType
Parameter to be used for the communication functions.
Definition: gridenums.hh:86
Dune namespace.
Definition: alignedallocator.hh:13
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)