dune-grid  2.3.0
alu2dgridfactory.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 
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/parallel/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  typedef GridFactoryInterface< GridImp > Base ;
29  public:
30  using Base :: insertElement ;
31 
32  typedef GridImp Grid;
33 
34  static const int dimension = Grid::dimension;
35  static const int dimensionworld = Grid::dimensionworld;
36 
38  typedef DuneBoundaryProjection< dimensionworld > DuneBoundaryProjectionType;
39 
40  template< int codim >
41  struct Codim
42  {
43  typedef typename Grid::template Codim< codim >::Entity Entity;
44  };
45 
46  private:
47  typedef Dune::BoundarySegmentWrapper<2, dimensionworld> BoundarySegmentWrapperType;
48 
49  typedef ALU2dGridFactory< Grid > ThisType;
50  typedef GridFactoryInterface< Grid > BaseType;
51 
52  typedef typename Grid::ctype ctype;
53 
54  static const ALU2DSPACE ElementType elementType = Grid::elementType;
55  static const unsigned int numFaceCorners = 2;
56 
57  typedef FieldVector< ctype, dimensionworld > VertexType;
58  typedef std::vector< unsigned int > ElementType;
59  typedef array< unsigned int, numFaceCorners > FaceType;
60 
61  static const int periodicBndId = ALU2dImplTraits< dimensionworld, elementType >::HBndElType::general_periodic;
62 
63  public:
64  typedef ALUGridTransformation< ctype, dimensionworld > Transformation;
65 
67  typedef typename Transformation::WorldVector WorldVector;
69  typedef typename Transformation::WorldMatrix WorldMatrix;
70 
71  private:
72  struct FaceLess;
73 
74  typedef std::vector< VertexType > VertexVector;
75  typedef std::vector< ElementType > ElementVector;
76  typedef std::vector< std::pair< FaceType, int > > BoundaryIdVector;
77 
78  typedef std::map< FaceType, const DuneBoundaryProjectionType* > BoundaryProjectionMap;
79  typedef std::vector< const DuneBoundaryProjectionType* > BoundaryProjectionVector;
80 
81  typedef std::pair< unsigned int, int > SubEntity;
82  typedef std::map< FaceType, SubEntity, FaceLess > FaceMap;
83  typedef std::vector< Transformation > FaceTransformationVector;
84  typedef std::map< FaceType, unsigned int, FaceLess > PeriodicNeighborMap;
85 
86  // copy vertex numbers and store smalled #dimension ones
87  void copyAndSort(const std::vector<unsigned int>& vertices, FaceType& faceId) const
88  {
89  std::vector<unsigned int> tmp( vertices );
90  std::sort( tmp.begin(), tmp.end() );
91 
92  // copy only the first dimension vertices (enough for key)
93  for( size_t i = 0; i < faceId.size(); ++i ) faceId[ i ] = tmp[ i ];
94  }
95 
96  public:
98  explicit ALU2dGridFactory ( bool removeGeneratedFile = true );
99 
101  explicit ALU2dGridFactory ( const std::string &filename );
102 
104  virtual ~ALU2dGridFactory ();
105 
110  virtual void insertVertex ( const VertexType &pos );
111 
120  virtual void
121  insertElement ( const GeometryType &geometry,
122  const std::vector< unsigned int > &vertices );
123 
134  virtual void
135  insertBoundary ( const GeometryType &geometry,
136  const std::vector< unsigned int > &faceVertices,
137  const int id );
138 
145  virtual void insertBoundary ( const int element, const int face, const int id );
146 
155  virtual void
156  insertBoundaryProjection ( const GeometryType &type,
157  const std::vector< unsigned int > &vertices,
158  const DuneBoundaryProjectionType *projection );
159 
164  virtual void
165  insertBoundarySegment ( const std::vector< unsigned int >& vertices ) ;
166 
172  virtual void
173  insertBoundarySegment ( const std::vector< unsigned int >& vertices,
174  const shared_ptr<BoundarySegment<2,dimensionworld> >& boundarySegment ) ;
175 
180  virtual void insertBoundaryProjection ( const DuneBoundaryProjectionType& bndProjection );
181 
191  void insertFaceTransformation ( const WorldMatrix &matrix, const WorldVector &shift );
192 
193  virtual unsigned int
194  insertionIndex ( const typename Codim< 0 >::Entity &entity ) const
195  {
196  return Grid::getRealImplementation( entity ).getIndex();
197  }
198 
199  virtual unsigned int
200  insertionIndex ( const typename Codim< dimension >::Entity &entity ) const
201  {
202  return Grid::getRealImplementation( entity ).getIndex();
203  }
204 
205  virtual unsigned int
206  insertionIndex ( const typename Grid::LeafIntersection &intersection ) const
207  {
208  return intersection.boundarySegmentIndex();
209  }
210 
211  virtual bool
212  wasInserted ( const typename Grid::LeafIntersection &intersection ) const
213  {
214  return intersection.boundary() &&
215  ( insertionIndex(intersection) < numFacesInserted_ );
216  }
217 
222  Grid *createGrid ();
223 
224  Grid *createGrid ( const bool addMissingBoundaries, const std::string dgfName = "" );
225 
226  Grid *createGrid ( const bool addMissingBoundaries, bool temporary, const std::string dgfName = "" );
227 
228  void setTolerance ( const ctype &epsilon ) { epsilon_ = epsilon; }
229 
230  protected:
232  virtual Grid* createGridObj( const bool temporary,
233  const std::string& filename,
234  std::istream& inFile,
235  BoundaryProjectionVector* bndProjections )
236  {
237  return ( temporary ) ?
238  new Grid( filename, inFile, globalProjection_, bndProjections, grdVerbose_ ) :
239  new Grid( filename, globalProjection_ , bndProjections, grdVerbose_ );
240  }
241 
242  public:
247  void setVerbosity( const bool verbose ) { grdVerbose_ = verbose; }
248 
249  private:
250  static void generateFace ( const ElementType &element, const int f, FaceType &face );
251  void correctElementOrientation ();
252  typename FaceMap::const_iterator findPeriodicNeighbor( const FaceMap &faceMap, const FaceType &key ) const;
253  void reinsertBoundary ( const FaceMap &faceMap, const typename FaceMap::const_iterator &pos, const int id );
254  void recreateBoundaryIds ( const int defaultId = 1 );
255 
256  VertexVector vertices_;
257  ElementVector elements_;
258  BoundaryIdVector boundaryIds_;
259  const DuneBoundaryProjectionType* globalProjection_ ;
260  BoundaryProjectionMap boundaryProjections_;
261  unsigned int numFacesInserted_;
262  bool grdVerbose_;
263  FaceTransformationVector faceTransformations_;
264  PeriodicNeighborMap periodicNeighborMap_;
265  ctype epsilon_;
266  };
267 
268 
269  template< class GridImp >
270  struct ALU2dGridFactory< GridImp >::FaceLess
271  : public std::binary_function< FaceType, FaceType, bool >
272  {
273  bool operator() ( const FaceType &a, const FaceType &b ) const
274  {
275  for( unsigned int i = 0; i < numFaceCorners; ++i )
276  {
277  if( a[ i ] != b[ i ] )
278  return (a[ i ] < b[ i ]);
279  }
280  return false;
281  }
282  };
283 
284 
288  template<int dimw>
289  class GridFactory< ALUConformGrid<2,dimw> >
290  : public ALU2dGridFactory<ALUConformGrid<2, dimw> >
291  {
292  public:
293  typedef ALUConformGrid< 2, dimw > Grid;
294 
295  protected:
296  typedef GridFactory ThisType;
297  typedef ALU2dGridFactory< Grid > BaseType;
298 
299  public:
301  explicit GridFactory ( )
302  : BaseType( )
303  {}
304 
306  GridFactory ( const std::string &filename )
307  : BaseType( filename )
308  {}
309 
311  GridFactory ( const bool verbose )
312  : BaseType( )
313  {
314  this->setVerbosity( verbose );
315  }
316  };
320  template<int dimw>
321  class GridFactory< ALUSimplexGrid<2,dimw> >
322  : public ALU2dGridFactory<ALUSimplexGrid<2, dimw> >
323  {
324  public:
325  typedef ALUSimplexGrid< 2, dimw > Grid;
326 
327  protected:
328  typedef GridFactory ThisType;
329  typedef ALU2dGridFactory< Grid > BaseType;
330 
331  public:
333  explicit GridFactory ( )
334  : BaseType( )
335  {}
336 
338  GridFactory ( const std::string &filename )
339  : BaseType( filename )
340  {}
341 
346  GridFactory ( const bool verbose )
347  : BaseType( )
348  {
349  this->setVerbosity( verbose );
350  }
351  };
352 
356  template<int dimw>
357  class GridFactory< ALUCubeGrid<2,dimw> >
358  : public ALU2dGridFactory<ALUCubeGrid<2,dimw> >
359  {
360  public:
361  typedef ALUCubeGrid< 2, dimw > Grid;
362 
363  protected:
364  typedef GridFactory ThisType;
365  typedef ALU2dGridFactory< Grid > BaseType;
366 
367  public:
369  explicit GridFactory ( )
370  : BaseType( )
371  {}
372 
374  GridFactory ( const std::string &filename )
375  : BaseType( filename )
376  {}
377 
382  GridFactory ( const bool verbose )
383  : BaseType( )
384  {
385  this->setVerbosity( verbose );
386  }
387  };
388 
393  template<int dimw, ALUGridElementType eltype, ALUGridRefinementType refinementtype, class Comm>
394  class GridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
395  : public ALU2dGridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
396  {
397  public:
398  typedef ALUGrid<2, dimw, eltype, refinementtype, Comm > Grid;
399 
400  protected:
401  typedef GridFactory ThisType;
402  typedef ALU2dGridFactory< Grid > BaseType;
403 
404  public:
406  explicit GridFactory ( )
407  : BaseType( )
408  {}
409 
411  GridFactory ( const std::string &filename )
412  : BaseType( filename )
413  {}
414 
419  GridFactory ( const bool verbose )
420  : BaseType( )
421  {
422  this->setVerbosity( verbose );
423  }
424  };
425 
426 
427 
428  // Inline Implementations
429  // ----------------------
430 
431  template< class GridImp >
432  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( bool removeGeneratedFile )
433  : globalProjection_ ( 0 ),
434  numFacesInserted_ ( 0 ),
435  grdVerbose_( true ),
436  epsilon_( 1e-8 )
437  {}
438 
439 
440  template< class GridImp >
441  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( const std::string &filename )
442  : globalProjection_ ( 0 ),
443  numFacesInserted_ ( 0 ),
444  grdVerbose_( true ),
445  epsilon_( 1e-8 )
446  {}
447 
448 
449  template< class GridImp >
450  inline ALU2dGridFactory< GridImp >::~ALU2dGridFactory ()
451  {}
452 
453 
454  template< class GridImp >
455  inline GridImp *ALU2dGridFactory< GridImp >::createGrid ()
456  {
457  return createGrid( true, true, "" );
458  }
459 
460 
461  template< class GridImp >
462  inline GridImp *ALU2dGridFactory< GridImp >
463  ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
464  {
465  return createGrid( addMissingBoundaries, true, dgfName );
466  }
467 
468 }
469 
470 #endif // #if HAVE_ALUGRID
471 
472 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH
ElementType
Definition: alu2dinclude.hh:55
#define ALU2DSPACE
Definition: alu2dinclude.hh:34
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:178
Provide a generic factory class for unstructured grids.
GridFamily::Traits::LeafIntersection LeafIntersection
A type that is a model of Dune::Intersection, an intersections of two codimension 1 of two codimensio...
Definition: common/grid.hh:484
The dimension of the world the grid lives in.
Definition: common/grid.hh:406
The dimension of the grid.
Definition: common/grid.hh:400
GridFactory()
Default constructor.
Definition: common/gridfactory.hh:274
ct ctype
Define type used for coordinates in grid module.
Definition: common/grid.hh:546
Definition: boundaryprojection.hh:65