dune-grid  2.2.0
alu2dgridfactory.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_ALU2DGRID_FACTORY_HH
5 #define DUNE_ALU2DGRID_FACTORY_HH
6 
7 #ifdef ENABLE_ALUGRID
8 
9 #include <dune/common/array.hh>
10 #include <dune/common/mpihelper.hh>
11 
12 #include <dune/geometry/referenceelements.hh>
13 
15 
18 
19 namespace Dune
20 {
21 
23  template< class GridImp >
25  : public GridFactoryInterface< GridImp >
26  {
27  public:
28  typedef GridImp Grid;
29 
30  static const int dimension = Grid::dimension;
32 
35 
36  template< int codim >
37  struct Codim
38  {
39  typedef typename Grid::template Codim< codim >::Entity Entity;
40  };
41 
42  private:
44 
47 
48  typedef typename Grid::ctype ctype;
49 
50  static const ALU2DSPACE ElementType elementType = Grid::elementType;
51  static const unsigned int numFaceCorners = 2;
52 
53  typedef FieldVector< ctype, dimensionworld > VertexType;
54  typedef std::vector< unsigned int > ElementType;
55  typedef array< unsigned int, numFaceCorners > FaceType;
56 
58 
59  public:
61 
66 
67  private:
68  struct FaceLess;
69 
70  typedef std::vector< VertexType > VertexVector;
71  typedef std::vector< ElementType > ElementVector;
72  typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
73 
74  typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
75  typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
76 
77  typedef std::pair< unsigned int, int > SubEntity;
78  typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
79  typedef std::vector< Transformation > FaceTransformationVector;
80  typedef std::map< FaceType, unsigned int, FaceLess > PeriodicNeighborMap;
81 
82  // copy vertex numbers and store smalled #dimension ones
83  void copyAndSort(const std::vector<unsigned int>& vertices, FaceType& faceId) const
84  {
85  std::vector<unsigned int> tmp( vertices );
86  std::sort( tmp.begin(), tmp.end() );
87 
88  // copy only the first dimension vertices (enough for key)
89  for( size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
90  }
91 
92  public:
94  explicit ALU2dGridFactory ( bool removeGeneratedFile = true );
95 
97  explicit ALU2dGridFactory ( const std::string &filename );
98 
100  virtual ~ALU2dGridFactory ();
101 
106  virtual void insertVertex ( const VertexType &pos );
107 
116  virtual void
117  insertElement ( const GeometryType &geometry,
118  const std::vector< unsigned int > &vertices );
119 
130  virtual void
131  insertBoundary ( const GeometryType &geometry,
132  const std::vector< unsigned int > &faceVertices,
133  const int id );
134 
141  virtual void insertBoundary ( const int element, const int face, const int id );
142 
151  virtual void
153  const std::vector< unsigned int > &vertices,
154  const DuneBoundaryProjectionType *projection );
155 
160  virtual void
161  insertBoundarySegment ( const std::vector< unsigned int >& vertices ) ;
162 
168  virtual void
169  insertBoundarySegment ( const std::vector< unsigned int >& vertices,
170  const shared_ptr<BoundarySegment<2,dimensionworld> >& boundarySegment ) ;
171 
176  virtual void insertBoundaryProjection ( const DuneBoundaryProjectionType& bndProjection );
177 
187  void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
188 
189  virtual unsigned int
190  insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
191  {
192  return Grid::getRealImplementation( entity ).getIndex();
193  }
194 
195  virtual unsigned int
196  insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
197  {
198  return Grid::getRealImplementation( entity ).getIndex();
199  }
200 
201  virtual unsigned int
202  insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
203  {
204  return intersection.boundarySegmentIndex();
205  }
206 
207  virtual bool
208  wasInserted ( const typename Grid::LeafIntersection &intersection ) const
209  {
210  return intersection.boundary() &&
211  ( insertionIndex(intersection) < numFacesInserted_ );
212  }
213 
218  Grid *createGrid ();
219 
220  Grid *createGrid ( const bool addMissingBoundaries, const std::string dgfName = "" );
221 
222  Grid *createGrid ( const bool addMissingBoundaries, bool temporary, const std::string dgfName = "" );
223 
224  void setTolerance ( const ctype &epsilon ) { epsilon_ = epsilon; }
225 
226  protected:
228  virtual Grid* createGridObj( const bool temporary,
229  const std::string& filename,
230  std::istream& inFile,
231  BoundaryProjectionVector* bndProjections )
232  {
233  return ( temporary ) ?
234  new Grid( filename, inFile, globalProjection_, bndProjections, grdVerbose_ ) :
235  new Grid( filename, globalProjection_ , bndProjections, grdVerbose_ );
236  }
237 
242  void setVerbosity( const bool verbose ) { grdVerbose_ = verbose; }
243 
244  private:
245  static void generateFace ( const ElementType &element, const int f, FaceType &face );
246  void correctElementOrientation ();
247  typename FaceMap::const_iterator findPeriodicNeighbor( const FaceMap &faceMap, const FaceType &key ) const;
248  void reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id );
249  void recreateBoundaryIds ( const int defaultId = 1 );
250 
251  VertexVector vertices_;
252  ElementVector elements_;
253  BoundaryIdVector boundaryIds_;
254  const DuneBoundaryProjectionType* globalProjection_ ;
255  BoundaryProjectionMap boundaryProjections_;
256  unsigned int numFacesInserted_;
257  bool grdVerbose_;
258  FaceTransformationVector faceTransformations_;
259  PeriodicNeighborMap periodicNeighborMap_;
260  ctype epsilon_;
261  };
262 
263 
264  template< class GridImp >
265  struct ALU2dGridFactory< GridImp >::FaceLess
266  : public std::binary_function< FaceType, FaceType, bool >
267  {
268  bool operator() ( const FaceType &a, const FaceType &b ) const
269  {
270  for( unsigned int i = 0; i < numFaceCorners; ++i )
271  {
272  if( a[ i ] != b[ i ] )
273  return (a[ i ] < b[ i ]);
274  }
275  return false;
276  }
277  };
278 
279 
283  template<int dimw>
284  class GridFactory< ALUConformGrid<2,dimw> >
285  : public ALU2dGridFactory<ALUConformGrid<2, dimw> >
286  {
287  public:
289 
290  protected:
293 
294  public:
296  explicit GridFactory ( )
297  : BaseType( )
298  {}
299 
301  GridFactory ( const std::string &filename )
302  : BaseType( filename )
303  {}
304 
306  GridFactory ( const bool verbose )
307  : BaseType( )
308  {
309  this->setVerbosity( verbose );
310  }
311  };
315  template<int dimw>
316  class GridFactory< ALUSimplexGrid<2,dimw> >
317  : public ALU2dGridFactory<ALUSimplexGrid<2, dimw> >
318  {
319  public:
321 
322  protected:
325 
326  public:
328  explicit GridFactory ( )
329  : BaseType( )
330  {}
331 
333  GridFactory ( const std::string &filename )
334  : BaseType( filename )
335  {}
336 
341  GridFactory ( const bool verbose )
342  : BaseType( )
343  {
344  this->setVerbosity( verbose );
345  }
346  };
347 
351  template<int dimw>
352  class GridFactory< ALUCubeGrid<2,dimw> >
353  : public ALU2dGridFactory<ALUCubeGrid<2,dimw> >
354  {
355  public:
357 
358  protected:
361 
362  public:
364  explicit GridFactory ( )
365  : BaseType( )
366  {}
367 
369  GridFactory ( const std::string &filename )
370  : BaseType( filename )
371  {}
372 
377  GridFactory ( const bool verbose )
378  : BaseType( )
379  {
380  this->setVerbosity( verbose );
381  }
382  };
383 
388  template<int dimw, ALUGridElementType eltype, ALUGridRefinementType refinementtype, class Comm>
389  class GridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
390  : public ALU2dGridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
391  {
392  public:
393  typedef ALUGrid<2, dimw, eltype, refinementtype, Comm > Grid;
394 
395  protected:
398 
399  public:
401  explicit GridFactory ( )
402  : BaseType( )
403  {}
404 
406  GridFactory ( const std::string &filename )
407  : BaseType( filename )
408  {}
409 
414  GridFactory ( const bool verbose )
415  : BaseType( )
416  {
417  this->setVerbosity( verbose );
418  }
419  };
420 
421 
422 
423  // Inline Implementations
424  // ----------------------
425 
426  template< class GridImp >
427  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( bool removeGeneratedFile )
428  : globalProjection_ ( 0 ),
429  numFacesInserted_ ( 0 ),
430  grdVerbose_( true ),
431  epsilon_( 1e-8 )
432  {}
433 
434 
435  template< class GridImp >
436  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( const std::string &filename )
437  : globalProjection_ ( 0 ),
438  numFacesInserted_ ( 0 ),
439  grdVerbose_( true ),
440  epsilon_( 1e-8 )
441  {}
442 
443 
444  template< class GridImp >
446  {}
447 
448 
449  template< class GridImp >
451  {
452  return createGrid( true, true, "" );
453  }
454 
455 
456  template< class GridImp >
457  inline GridImp *ALU2dGridFactory< GridImp >
458  ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
459  {
460  return createGrid( addMissingBoundaries, true, dgfName );
461  }
462 
463 }
464 
465 #endif // #ifdef ENABLE_ALUGRID
466 
467 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH