dune-grid  2.2.1
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 #if HAVE_ALUGRID
8 
9 #include <dune/common/array.hh>
10 #include <dune/common/mpihelper.hh>
11 
12 #include <dune/geometry/referenceelements.hh>
13 
15 
19 
20 namespace Dune
21 {
22 
24  template< class GridImp >
25  class ALU2dGridFactory
26  : public GridFactoryInterface< GridImp >
27  {
28  public:
29  typedef GridImp Grid;
30 
31  static const int dimension = Grid::dimension;
32  static const int dimensionworld = Grid::dimensionworld;
33 
35  typedef DuneBoundaryProjection< dimensionworld > DuneBoundaryProjectionType;
36 
37  template< int codim >
38  struct Codim
39  {
40  typedef typename Grid::template Codim< codim >::Entity Entity;
41  };
42 
43  private:
44  typedef Dune::BoundarySegmentWrapper<2, dimensionworld> BoundarySegmentWrapperType;
45 
46  typedef ALU2dGridFactory< Grid > ThisType;
47  typedef GridFactoryInterface< Grid > BaseType;
48 
49  typedef typename Grid::ctype ctype;
50 
51  static const ALU2DSPACE ElementType elementType = Grid::elementType;
52  static const unsigned int numFaceCorners = 2;
53 
54  typedef FieldVector< ctype, dimensionworld > VertexType;
55  typedef std::vector< unsigned int > ElementType;
56  typedef array< unsigned int, numFaceCorners > FaceType;
57 
58  static const int periodicBndId = ALU2dImplTraits< dimensionworld, elementType >::HBndElType::general_periodic;
59 
60  public:
61  typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
62 
64  typedef typename Transformation::WorldVector WorldVector;
66  typedef typename Transformation::WorldMatrix WorldMatrix;
67 
68  private:
69  struct FaceLess;
70 
71  typedef std::vector< VertexType > VertexVector;
72  typedef std::vector< ElementType > ElementVector;
73  typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
74 
75  typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
76  typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
77 
78  typedef std::pair< unsigned int, int > SubEntity;
79  typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
80  typedef std::vector< Transformation > FaceTransformationVector;
81  typedef std::map< FaceType, unsigned int, FaceLess > PeriodicNeighborMap;
82 
83  // copy vertex numbers and store smalled #dimension ones
84  void copyAndSort(const std::vector<unsigned int>& vertices, FaceType& faceId) const
85  {
86  std::vector<unsigned int> tmp( vertices );
87  std::sort( tmp.begin(), tmp.end() );
88 
89  // copy only the first dimension vertices (enough for key)
90  for( size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
91  }
92 
93  public:
95  explicit ALU2dGridFactory ( bool removeGeneratedFile = true );
96 
98  explicit ALU2dGridFactory ( const std::string &filename );
99 
101  virtual ~ALU2dGridFactory ();
102 
107  virtual void insertVertex ( const VertexType &pos );
108 
117  virtual void
118  insertElement ( const GeometryType &geometry,
119  const std::vector< unsigned int > &vertices );
120 
131  virtual void
132  insertBoundary ( const GeometryType &geometry,
133  const std::vector< unsigned int > &faceVertices,
134  const int id );
135 
142  virtual void insertBoundary ( const int element, const int face, const int id );
143 
152  virtual void
153  insertBoundaryProjection ( const GeometryType &type,
154  const std::vector< unsigned int > &vertices,
155  const DuneBoundaryProjectionType *projection );
156 
161  virtual void
162  insertBoundarySegment ( const std::vector< unsigned int >& vertices ) ;
163 
169  virtual void
170  insertBoundarySegment ( const std::vector< unsigned int >& vertices,
171  const shared_ptr<BoundarySegment<2,dimensionworld> >& boundarySegment ) ;
172 
177  virtual void insertBoundaryProjection ( const DuneBoundaryProjectionType& bndProjection );
178 
188  void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
189 
190  virtual unsigned int
191  insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
192  {
193  return Grid::getRealImplementation( entity ).getIndex();
194  }
195 
196  virtual unsigned int
197  insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
198  {
199  return Grid::getRealImplementation( entity ).getIndex();
200  }
201 
202  virtual unsigned int
203  insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
204  {
205  return intersection.boundarySegmentIndex();
206  }
207 
208  virtual bool
209  wasInserted ( const typename Grid::LeafIntersection &intersection ) const
210  {
211  return intersection.boundary() &&
212  ( insertionIndex(intersection) < numFacesInserted_ );
213  }
214 
219  Grid *createGrid ();
220 
221  Grid *createGrid ( const bool addMissingBoundaries, const std::string dgfName = "" );
222 
223  Grid *createGrid ( const bool addMissingBoundaries, bool temporary, const std::string dgfName = "" );
224 
225  void setTolerance ( const ctype &epsilon ) { epsilon_ = epsilon; }
226 
227  protected:
229  virtual Grid* createGridObj( const bool temporary,
230  const std::string& filename,
231  std::istream& inFile,
232  BoundaryProjectionVector* bndProjections )
233  {
234  return ( temporary ) ?
235  new Grid( filename, inFile, globalProjection_, bndProjections, grdVerbose_ ) :
236  new Grid( filename, globalProjection_ , bndProjections, grdVerbose_ );
237  }
238 
239  public:
244  void setVerbosity( const bool verbose ) { grdVerbose_ = verbose; }
245 
246  private:
247  static void generateFace ( const ElementType &element, const int f, FaceType &face );
248  void correctElementOrientation ();
249  typename FaceMap::const_iterator findPeriodicNeighbor( const FaceMap &faceMap, const FaceType &key ) const;
250  void reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id );
251  void recreateBoundaryIds ( const int defaultId = 1 );
252 
253  VertexVector vertices_;
254  ElementVector elements_;
255  BoundaryIdVector boundaryIds_;
256  const DuneBoundaryProjectionType* globalProjection_ ;
257  BoundaryProjectionMap boundaryProjections_;
258  unsigned int numFacesInserted_;
259  bool grdVerbose_;
260  FaceTransformationVector faceTransformations_;
261  PeriodicNeighborMap periodicNeighborMap_;
262  ctype epsilon_;
263  };
264 
265 
266  template< class GridImp >
267  struct ALU2dGridFactory< GridImp >::FaceLess
268  : public std::binary_function< FaceType, FaceType, bool >
269  {
270  bool operator() ( const FaceType &a, const FaceType &b ) const
271  {
272  for( unsigned int i = 0; i < numFaceCorners; ++i )
273  {
274  if( a[ i ] != b[ i ] )
275  return (a[ i ] < b[ i ]);
276  }
277  return false;
278  }
279  };
280 
281 
285  template<int dimw>
286  class GridFactory< ALUConformGrid<2,dimw> >
287  : public ALU2dGridFactory<ALUConformGrid<2, dimw> >
288  {
289  public:
290  typedef ALUConformGrid< 2, dimw > Grid;
291 
292  protected:
293  typedef GridFactory ThisType;
294  typedef ALU2dGridFactory< Grid > BaseType;
295 
296  public:
298  explicit GridFactory ( )
299  : BaseType( )
300  {}
301 
303  GridFactory ( const std::string &filename )
304  : BaseType( filename )
305  {}
306 
308  GridFactory ( const bool verbose )
309  : BaseType( )
310  {
311  this->setVerbosity( verbose );
312  }
313  };
317  template<int dimw>
318  class GridFactory< ALUSimplexGrid<2,dimw> >
319  : public ALU2dGridFactory<ALUSimplexGrid<2, dimw> >
320  {
321  public:
322  typedef ALUSimplexGrid< 2, dimw > Grid;
323 
324  protected:
325  typedef GridFactory ThisType;
326  typedef ALU2dGridFactory< Grid > BaseType;
327 
328  public:
330  explicit GridFactory ( )
331  : BaseType( )
332  {}
333 
335  GridFactory ( const std::string &filename )
336  : BaseType( filename )
337  {}
338 
343  GridFactory ( const bool verbose )
344  : BaseType( )
345  {
346  this->setVerbosity( verbose );
347  }
348  };
349 
353  template<int dimw>
354  class GridFactory< ALUCubeGrid<2,dimw> >
355  : public ALU2dGridFactory<ALUCubeGrid<2,dimw> >
356  {
357  public:
358  typedef ALUCubeGrid< 2, dimw > Grid;
359 
360  protected:
361  typedef GridFactory ThisType;
362  typedef ALU2dGridFactory< Grid > BaseType;
363 
364  public:
366  explicit GridFactory ( )
367  : BaseType( )
368  {}
369 
371  GridFactory ( const std::string &filename )
372  : BaseType( filename )
373  {}
374 
379  GridFactory ( const bool verbose )
380  : BaseType( )
381  {
382  this->setVerbosity( verbose );
383  }
384  };
385 
390  template<int dimw, ALUGridElementType eltype, ALUGridRefinementType refinementtype, class Comm>
391  class GridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
392  : public ALU2dGridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
393  {
394  public:
395  typedef ALUGrid<2, dimw, eltype, refinementtype, Comm > Grid;
396 
397  protected:
398  typedef GridFactory ThisType;
399  typedef ALU2dGridFactory< Grid > BaseType;
400 
401  public:
403  explicit GridFactory ( )
404  : BaseType( )
405  {}
406 
408  GridFactory ( const std::string &filename )
409  : BaseType( filename )
410  {}
411 
416  GridFactory ( const bool verbose )
417  : BaseType( )
418  {
419  this->setVerbosity( verbose );
420  }
421  };
422 
423 
424 
425  // Inline Implementations
426  // ----------------------
427 
428  template< class GridImp >
429  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( bool removeGeneratedFile )
430  : globalProjection_ ( 0 ),
431  numFacesInserted_ ( 0 ),
432  grdVerbose_( true ),
433  epsilon_( 1e-8 )
434  {}
435 
436 
437  template< class GridImp >
438  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( const std::string &filename )
439  : globalProjection_ ( 0 ),
440  numFacesInserted_ ( 0 ),
441  grdVerbose_( true ),
442  epsilon_( 1e-8 )
443  {}
444 
445 
446  template< class GridImp >
447  inline ALU2dGridFactory< GridImp >::~ALU2dGridFactory ()
448  {}
449 
450 
451  template< class GridImp >
452  inline GridImp *ALU2dGridFactory< GridImp >::createGrid ()
453  {
454  return createGrid( true, true, "" );
455  }
456 
457 
458  template< class GridImp >
459  inline GridImp *ALU2dGridFactory< GridImp >
460  ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
461  {
462  return createGrid( addMissingBoundaries, true, dgfName );
463  }
464 
465 }
466 
467 #endif // #if HAVE_ALUGRID
468 
469 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH