Dune Core Modules (2.5.2)

meshpointer.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ALBERTA_MESHPOINTER_HH
4#define DUNE_ALBERTA_MESHPOINTER_HH
5
11#include <limits>
12#include <string>
13
14#include <dune/grid/albertagrid/misc.hh>
17#include <dune/grid/albertagrid/projection.hh>
18
19#if HAVE_ALBERTA
20
21namespace Dune
22{
23
24 namespace Alberta
25 {
26
27 // External Forward Declarations
28 // -----------------------------
29
30 template< int dim >
31 class HierarchyDofNumbering;
32
33 // MeshPointer
34 // -----------
35
36 template< int dim >
37 class MeshPointer
38 {
39 typedef Alberta::ElementInfo< dim > ElementInfo;
40 typedef typename ElementInfo::MacroElement MacroElement;
41 typedef typename ElementInfo::FillFlags FillFlags;
42
43 class BoundaryProvider;
44
45 template< int dimWorld >
46 struct Library;
47
48 public:
49 class MacroIterator;
50
51 MeshPointer ()
52 : mesh_( 0 )
53 {}
54
55 explicit MeshPointer ( Mesh *mesh )
56 : mesh_( mesh )
57 {}
58
59 operator Mesh * () const
60 {
61 return mesh_;
62 }
63
64 operator bool () const
65 {
66 return (bool)mesh_;
67 }
68
69 MacroIterator begin () const
70 {
71 return MacroIterator( *this, false );
72 }
73
74 MacroIterator end () const
75 {
76 return MacroIterator( *this, true );
77 }
78
79 int numMacroElements () const;
80 int size ( int codim ) const;
81
82 // create a mesh from a macrodata structure
83 // params: macroData - macro data structure
84 // returns: number of boundary segments
85 unsigned int create ( const MacroData< dim > &macroData );
86
87 // create a mesh from a macrodata structure, adding projections
88 // params: macroData - macro data structure
89 // projectionFactory - factory for the projections
90 // returns: number of boundary segments
91 template< class Proj, class Impl >
92 unsigned int create ( const MacroData< dim > &macroData,
93 const ProjectionFactoryInterface< Proj, Impl > &projectionFactory );
94
95 // create a mesh from a file
96 // params: filename - file name of an Alberta macro triangulation
97 // binary - read binary?
98 // returns: number of boundary segments
99 unsigned int create ( const std::string &filename, bool binary = false );
100
101 // read back a mesh from a file
102 // params: filename - file name of an Alberta save file
103 // time - variable to receive the time stored in the file
104 // returns: number of boundary segments
105 //
106 // notes: - projections are not preserved
107 // - we assume that projections are added in the same order they
108 // inserted in when the grid was created (otherwise the boundary
109 // indices change)
110 unsigned int read ( const std::string &filename, Real &time );
111
112 bool write ( const std::string &filename, Real time ) const;
113
114 void release ();
115
116 template< class Functor >
117 void hierarchicTraverse ( Functor &functor,
118 typename FillFlags::Flags fillFlags = FillFlags::standard ) const;
119
120 template< class Functor >
121 void leafTraverse ( Functor &functor,
122 typename FillFlags::Flags fillFlags = FillFlags::standard ) const;
123
124 bool coarsen ( typename FillFlags::Flags fillFlags = FillFlags::nothing );
125
126 bool refine ( typename FillFlags::Flags fillFlags = FillFlags::nothing );
127
128 private:
129 static ALBERTA NODE_PROJECTION *
130 initNodeProjection ( Mesh *mesh, ALBERTA MACRO_EL *macroElement, int n );
131 template< class ProjectionProvider >
132 static ALBERTA NODE_PROJECTION *
133 initNodeProjection ( Mesh *mesh, ALBERTA MACRO_EL *macroElement, int n );
134
135 Mesh *mesh_;
136 };
137
138
139
140 // MeshPointer::Library
141 // --------------------
142
143 template< int dim >
144 template< int dimWorld >
145 struct MeshPointer< dim >::Library
146 {
147 typedef Alberta::MeshPointer< dim > MeshPointer;
148
149 static unsigned int boundaryCount;
150 static const void *projectionFactory;
151
152 static void
153 create ( MeshPointer &ptr, const MacroData< dim > &macroData,
154 ALBERTA NODE_PROJECTION *(*initNodeProjection)( Mesh *, ALBERTA MACRO_EL *, int ) );
155 static void release ( MeshPointer &ptr );
156 };
157
158
159
160 // MeshPointer::MacroIterator
161 // --------------------------
162
163 template< int dim >
164 class MeshPointer< dim >::MacroIterator
165 {
166 typedef MacroIterator This;
167
168 friend class MeshPointer< dim >;
169
170 public:
171 typedef Alberta::MeshPointer< dim > MeshPointer;
172 typedef Alberta::ElementInfo< dim > ElementInfo;
173
174 MacroIterator ()
175 : mesh_(),
176 index_( -1 )
177 {}
178
179 private:
180
181 explicit MacroIterator ( const MeshPointer &mesh, bool end = false )
182 : mesh_( mesh ),
183 index_( end ? mesh.numMacroElements() : 0 )
184 {}
185
186 public:
187 bool done () const
188 {
189 return (index_ >= mesh().numMacroElements());
190 }
191
192 bool equals ( const MacroIterator &other ) const
193 {
194 return (index_ == other.index_);
195 }
196
197 void increment ()
198 {
199 assert( !done() );
200 ++index_;
201 }
202
203 const MacroElement &macroElement () const
204 {
205 assert( !done() );
206 return static_cast< const MacroElement & >( mesh().mesh_->macro_els[ index_ ] );
207 }
208
209 const MeshPointer &mesh () const
210 {
211 return mesh_;
212 }
213
214 This &operator++ ()
215 {
216 increment();
217 return *this;
218 }
219
220 ElementInfo operator* () const
221 {
222 return elementInfo();
223 }
224
225 bool operator== ( const MacroIterator &other ) const
226 {
227 return equals( other );
228 }
229
230 bool operator!= ( const MacroIterator &other ) const
231 {
232 return !equals( other );
233 }
234
235 ElementInfo
236 elementInfo ( typename FillFlags::Flags fillFlags = FillFlags::standard ) const
237 {
238 if( done() )
239 return ElementInfo();
240 else
241 return ElementInfo( mesh(), macroElement(), fillFlags );
242 }
243
244 private:
245 MeshPointer mesh_;
246 int index_;
247 };
248
249
250
251 // Implementation of MeshPointer
252 // -----------------------------
253
254 template< int dim >
255 inline int MeshPointer< dim >::numMacroElements () const
256 {
257 return (mesh_ ? mesh_->n_macro_el : 0);
258 }
259
260
261 template<>
262 inline int MeshPointer< 1 >::size( int codim ) const
263 {
264 assert( (codim >= 0) && (codim <= 1) );
265 return (codim == 0 ? mesh_->n_elements : mesh_->n_vertices);
266 }
267
268 template<>
269 inline int MeshPointer< 2 >::size( int codim ) const
270 {
271 assert( (codim >= 0) && (codim <= 2) );
272 if( codim == 0 )
273 return mesh_->n_elements;
274 else if( codim == 2 )
275 return mesh_->n_vertices;
276 else
277 return mesh_->n_edges;
278 }
279
280 template<>
281 inline int MeshPointer< 3 >::size( int codim ) const
282 {
283 assert( (codim >= 0) && (codim <= 3) );
284 if( codim == 0 )
285 return mesh_->n_elements;
286 else if( codim == 3 )
287 return mesh_->n_vertices;
288 else if( codim == 1 )
289 return mesh_->n_faces;
290 else
291 return mesh_->n_edges;
292 }
293
294
295 template< int dim >
296 inline unsigned int MeshPointer< dim >
297 ::create ( const MacroData< dim > &macroData )
298 {
299 release();
300
301 Library< dimWorld >::boundaryCount = 0;
302 Library< dimWorld >::create( *this, macroData, &initNodeProjection );
303 return Library< dimWorld >::boundaryCount;
304 }
305
306
307 template< int dim >
308 template< class Proj, class Impl >
309 inline unsigned int MeshPointer< dim >
310 ::create ( const MacroData< dim > &macroData,
311 const ProjectionFactoryInterface< Proj, Impl > &projectionFactory )
312 {
313 typedef ProjectionFactoryInterface< Proj, Impl > ProjectionFactory;
314
315 release();
316
317 Library< dimWorld >::boundaryCount = 0;
318 Library< dimWorld >::projectionFactory = &projectionFactory;
319 Library< dimWorld >::create( *this, macroData, &initNodeProjection< ProjectionFactory > );
320 Library< dimWorld >::projectionFactory = 0;
321 return Library< dimWorld >::boundaryCount;
322 }
323
324
325
326
327 template< int dim >
328 inline unsigned int MeshPointer< dim >
329 ::create ( const std::string &filename, bool binary )
330 {
331 MacroData< dim > macroData;
332 macroData.read( filename, binary );
333 const unsigned int boundaryCount = create( macroData );
334 macroData.release();
335 return boundaryCount;
336 }
337
338
339 template< int dim >
340 inline unsigned int MeshPointer< dim >::read ( const std::string &filename, Real &time )
341 {
342 release();
343
344 Library< dimWorld >::boundaryCount = 0;
345 mesh_ = ALBERTA read_mesh_xdr( filename.c_str(), &time, NULL, NULL );
346 return Library< dimWorld >::boundaryCount;
347 }
348
349
350 template< int dim >
351 inline bool MeshPointer< dim >::write ( const std::string &filename, Real time ) const
352 {
353 int success = ALBERTA write_mesh_xdr( mesh_, filename.c_str(), time );
354 return (success == 0);
355 }
356
357
358 template< int dim >
359 inline void MeshPointer< dim >::release ()
360 {
361 Library< dimWorld >::release( *this );
362 }
363
364
365 template< int dim >
366 template< class Functor >
367 inline void MeshPointer< dim >
368 ::hierarchicTraverse ( Functor &functor,
369 typename FillFlags::Flags fillFlags ) const
370 {
371 const MacroIterator eit = end();
372 for( MacroIterator it = begin(); it != eit; ++it )
373 {
374 const ElementInfo info = it.elementInfo( fillFlags );
375 info.hierarchicTraverse( functor );
376 }
377 }
378
379
380 template< int dim >
381 template< class Functor >
382 inline void MeshPointer< dim >
383 ::leafTraverse ( Functor &functor,
384 typename FillFlags::Flags fillFlags ) const
385 {
386 const MacroIterator eit = end();
387 for( MacroIterator it = begin(); it != eit; ++it )
388 {
389 const ElementInfo info = it.elementInfo( fillFlags );
390 info.leafTraverse( functor );
391 }
392 }
393
394
395 template< int dim >
396 inline bool MeshPointer< dim >::coarsen ( typename FillFlags::Flags fillFlags )
397 {
398 const bool coarsened = (ALBERTA coarsen( mesh_, fillFlags ) == meshCoarsened);
399 if( coarsened )
400 ALBERTA dof_compress( mesh_ );
401 return coarsened;
402 }
403
404 template< int dim >
405 inline bool MeshPointer< dim >::refine ( typename FillFlags::Flags fillFlags )
406 {
407 return (ALBERTA refine( mesh_, fillFlags ) == meshRefined);
408 }
409
410
411 template< int dim >
412 inline ALBERTA NODE_PROJECTION *
413 MeshPointer< dim >::initNodeProjection ( Mesh *mesh, ALBERTA MACRO_EL *macroEl, int n )
414 {
415 const MacroElement &macroElement = static_cast< const MacroElement & >( *macroEl );
416 if( (n > 0) && macroElement.isBoundary( n-1 ) )
417 return new BasicNodeProjection( Library< dimWorld >::boundaryCount++ );
418 else
419 return 0;
420 }
421
422
423 template< int dim >
424 template< class ProjectionFactory >
425 inline ALBERTA NODE_PROJECTION *
426 MeshPointer< dim >::initNodeProjection ( Mesh *mesh, ALBERTA MACRO_EL *macroEl, int n )
427 {
428 typedef typename ProjectionFactory::Projection Projection;
429
430 const MacroElement &macroElement = static_cast< const MacroElement & >( *macroEl );
431
432 MeshPointer< dim > meshPointer( mesh );
433 ElementInfo elementInfo( meshPointer, macroElement, FillFlags::standard );
434 const ProjectionFactory &projectionFactory = *static_cast< const ProjectionFactory * >( Library< dimWorld >::projectionFactory );
435 if( (n > 0) && macroElement.isBoundary( n-1 ) )
436 {
437 const unsigned int boundaryIndex = Library< dimWorld >::boundaryCount++;
438 if( projectionFactory.hasProjection( elementInfo, n-1 ) )
439 {
440 Projection projection = projectionFactory.projection( elementInfo, n-1 );
441 return new NodeProjection< dim, Projection >( boundaryIndex, projection );
442 }
443 else
444 return new BasicNodeProjection( boundaryIndex );
445 }
446 else if( (dim < dimWorld) && (n == 0) )
447 {
448 const unsigned int boundaryIndex = std::numeric_limits< unsigned int >::max();
449 if( projectionFactory.hasProjection( elementInfo ) )
450 {
451 Projection projection = projectionFactory.projection( elementInfo );
452 return new NodeProjection< dim, Projection >( boundaryIndex, projection );
453 }
454 else
455 return 0;
456 }
457 else
458 return 0;
459 }
460
461 } // namespace Alberta
462
463} // namespace Dune
464
465#endif // #if HAVE_ALBERTA
466
467#endif // #ifndef DUNE_ALBERTA_MESHPOINTER_HH
provides a wrapper for ALBERTA's el_info structure
constexpr auto equals(T1 &&t1, T2 &&t2)
Equality comparison.
Definition: hybridutilities.hh:441
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:233
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:255
provides a wrapper for ALBERTA's macro_data structure
Dune namespace.
Definition: alignment.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)