dune-grid  2.4.1-rc2
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 
289  template<int dimw, ALUGridElementType eltype, ALUGridRefinementType refinementtype, class Comm>
290  class GridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
291  : public ALU2dGridFactory< ALUGrid<2, dimw, eltype, refinementtype, Comm > >
292  {
293  public:
294  typedef ALUGrid<2, dimw, eltype, refinementtype, Comm > Grid;
295 
296  protected:
297  typedef GridFactory ThisType;
298  typedef ALU2dGridFactory< Grid > BaseType;
299 
300  public:
302  explicit GridFactory ( )
303  : BaseType( )
304  {}
305 
307  GridFactory ( const std::string &filename )
308  : BaseType( filename )
309  {}
310 
315  GridFactory ( const bool verbose )
316  : BaseType( )
317  {
318  this->setVerbosity( verbose );
319  }
320  };
321 
322 
323 
324  // Inline Implementations
325  // ----------------------
326 
327  template< class GridImp >
328  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( bool removeGeneratedFile )
329  : globalProjection_ ( 0 ),
330  numFacesInserted_ ( 0 ),
331  grdVerbose_( true ),
332  epsilon_( 1e-8 )
333  {}
334 
335 
336  template< class GridImp >
337  inline ALU2dGridFactory< GridImp >::ALU2dGridFactory ( const std::string &filename )
338  : globalProjection_ ( 0 ),
339  numFacesInserted_ ( 0 ),
340  grdVerbose_( true ),
341  epsilon_( 1e-8 )
342  {}
343 
344 
345  template< class GridImp >
346  inline ALU2dGridFactory< GridImp >::~ALU2dGridFactory ()
347  {}
348 
349 
350  template< class GridImp >
351  inline GridImp *ALU2dGridFactory< GridImp >::createGrid ()
352  {
353  return createGrid( true, true, "" );
354  }
355 
356 
357  template< class GridImp >
358  inline GridImp *ALU2dGridFactory< GridImp >
359  ::createGrid ( const bool addMissingBoundaries, const std::string dgfName )
360  {
361  return createGrid( addMissingBoundaries, true, dgfName );
362  }
363 
364 }
365 
366 #endif // #if HAVE_ALUGRID
367 
368 #endif // #ifndef DUNE_ALU2DGRID_FACTORY_HH
#define ALU2DSPACE
Definition: alu2dinclude.hh:34
ElementType
Definition: alu2dinclude.hh:55
GeometryType
Type representing VTK&#39;s entity geometry types.
Definition: common.hh:178
The dimension of the world the grid lives in.
Definition: common/grid.hh:408
Definition: boundaryprojection.hh:65
Provide a generic factory class for unstructured grids.
ct ctype
Define type used for coordinates in grid module.
Definition: common/grid.hh:548
Include standard header files.
Definition: agrid.hh:59
The dimension of the grid.
Definition: common/grid.hh:402
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:486