dune-grid  2.2.0
alugrid/3d/grid.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALU3DGRIDGRID_HH
2 #define DUNE_ALU3DGRIDGRID_HH
3 
4 //- System includes
5 #include <vector>
6 
7 //- Dune includes
11 #include <dune/common/bigunsignedint.hh>
12 #include <dune/common/deprecated.hh>
13 #include <dune/common/static_assert.hh>
14 
15 #include <dune/geometry/referenceelements.hh>
16 
17 #include <dune/grid/common/grid.hh>
23 
24 // bnd projection stuff
28 
29 //- Local includes
30 #include "alu3dinclude.hh"
31 #include "topology.hh"
32 #include "indexsets.hh"
33 #include "datahandle.hh"
34 
36 
37 #include <dune/common/mpihelper.hh>
38 
39 #if ALU3DGRID_PARALLEL
40 #include <dune/common/mpicollectivecommunication.hh>
41 #else
42 #include <dune/common/collectivecommunication.hh>
43 #endif
44 
45 namespace Dune
46 {
47 
48  // Forward declarations
49  template<int cd, int dim, class GridImp>
50  class ALU3dGridEntity;
51  template<int cd, PartitionIteratorType pitype, class GridImp >
52  class ALU3dGridLevelIterator;
53  template<int cd, class GridImp >
54  class ALU3dGridEntityPointerBase;
55  template<int cd, class GridImp >
56  class ALU3dGridEntitySeed;
57  template<int cd, class GridImp >
58  class ALU3dGridEntityPointer;
59  template<int mydim, int coorddim, class GridImp>
60  class ALU3dGridGeometry;
61  template<class GridImp>
62  class ALU3dGridHierarchicIterator;
63  template<class GridImp>
64  class ALU3dGridIntersectionIterator;
65  template<class GridImp>
66  class ALU3dGridLevelIntersectionIterator;
67  template<int codim, PartitionIteratorType pitype, class GridImp>
68  class ALU3dGridLeafIterator;
69  template <int mydim, int coorddim, class GridImp>
70  class ALU3dGridMakeableEntity;
71  template <class GridImp>
72  class ALU3dGridFaceGeometryInfo;
73  template< ALU3dGridElementType, class >
74  class ALU3dGridGlobalIdSet;
75  template< ALU3dGridElementType, class >
76  class ALU3dGridLocalIdSet;
77  template< ALU3dGridElementType, class >
78  class ALU3dGridHierarchicIndexSet;
79  template <class EntityImp>
80  class ALUMemoryProvider;
81  template< class >
82  class ALU3dGridFactory;
83  template <class GridImp, class GeometryImp, int nChild>
84  class ALULocalGeometryStorage;
85  template< ALU3dGridElementType elType, class Comm >
86  struct ALU3dGridCommHelper;
87 
88 
89 
90  // Internal Forward Declarations
91  // -----------------------------
92 
93 #if ALU3DGRID_PARALLEL
94  template< ALU3dGridElementType elType, class Comm = MPI_Comm >
95  class ALU3dGrid;
96 #else // #if ALU3DGRID_PARALLEL
97  template< ALU3dGridElementType elType, class Comm = No_Comm >
98  class ALU3dGrid;
99 #endif // #else // #if ALU3DGRID_PARALLEL
100 
101  template < class Comm >
102  struct ALUGridBaseGrid< 3, 3, cube, Comm >
103  {
105  };
106 
107  template < class Comm>
108  struct ALUGridBaseGrid< 3, 3, simplex, Comm >
109  {
111  };
112 
113  namespace DefaultIndexSetHelper
114  {
115 
116  template< ALU3dGridElementType elType, class Comm, class Index >
117  struct ContainsIndex< ALU3dGrid< elType, Comm >, Index >
118  {
120 
121  static bool
123  const size_t index )
124  {
125  return (container.getData( index ).index() >= 0);
126  }
127  };
128 
129  } // namespace DefaultIndexSetHelper
130 
131 
132 
133  // ALU3dGridCommunications
134  // -----------------------
135 
136  template< ALU3dGridElementType elType, class Comm >
137  struct ALU3dGridCommunications;
138 
139  template< ALU3dGridElementType elType >
140  struct ALU3dGridCommunications< elType, No_Comm >
141  {
143  typedef int GlobalId;
144 
145  typedef ALU3DSPACE GitterDuneImpl GitterImplType;
146 
147  typedef Dune::CollectiveCommunication< No_Comm > CollectiveCommunication;
148 
149  explicit ALU3dGridCommunications ( No_Comm comm ) {}
150 
151  int nlinks () const { return 0; }
152 
153  GitterImplType *createALUGrid ( const std::string &macroName, ALU3DSPACE ProjectVertex *projection )
154  {
155  if( macroName.empty() )
156  return new GitterImplType();
157  else
158  return new GitterImplType ( macroName.c_str(), projection );
159  }
160 
161  static No_Comm defaultComm () { return No_Comm(); }
162 
163  static int getRank ( No_Comm comm ) { return 0; }
164 
165  static typename ALU3DSPACE Gitter::Geometric::BuilderIF &getBuilder ( GitterImplType &grid )
166  {
167  return dynamic_cast< ALU3DSPACE Gitter::Geometric::BuilderIF & >( grid.container() );
168  }
169 
171 
173  };
174 
175 #if ALU3DGRID_PARALLEL
176  template< ALU3dGridElementType elType >
177  struct ALU3dGridCommunications< elType, MPI_Comm >
178  {
179  typedef ALU3dGridGlobalIdSet< elType, MPI_Comm > GlobalIdSet;
180  typedef ALUGridId< ALUMacroKey > GlobalId;
181 
182  typedef ALU3DSPACE GitterDunePll GitterImplType;
183 
184  typedef Dune::CollectiveCommunication< MPI_Comm > CollectiveCommunication;
185 
186  explicit ALU3dGridCommunications ( MPI_Comm comm )
187  : ccobj_( comm ), mpAccess_( comm )
188  {}
189 
190  int nlinks () const { return mpAccess_.nlinks(); }
191 
192  GitterImplType *createALUGrid ( const std::string &macroName, ALU3DSPACE ProjectVertex *projection )
193  {
194  return new GitterImplType( macroName.c_str(), mpAccess_, projection );
195  }
196 
197  static MPI_Comm defaultComm () { return MPI_COMM_WORLD; }
198 
199  static int getRank ( MPI_Comm comm )
200  {
201  int rank = 0;
202  MPI_Comm_rank( comm, &rank );
203  return rank;
204  }
205 
206  static typename ALU3DSPACE Gitter::Geometric::BuilderIF &getBuilder ( GitterImplType &grid )
207  {
208  return dynamic_cast< ALU3DSPACE Gitter::Geometric::BuilderIF & >( grid.containerPll() );
209  }
210 
211  static void duneNotifyMacroGridChanges ( GitterImplType &grid )
212  {
213  grid.duneNotifyMacroGridChanges();
214  }
215 
216  CollectiveCommunication ccobj_;
217  ALU3DSPACE MpAccessMPI mpAccess_;
218  };
219 #endif // #if ALU3DGRID_PARALLEL
220 
221 
222 
223  // ALU3dGridFamily
224  // ---------------
225 
226  template< ALU3dGridElementType elType, class Comm >
228  {
231 
232  static const int dim = 3;
233  static const int dimworld = 3;
234 
237 
239  typedef typename ALU3dGridCommunications< elType, Comm >::GlobalIdSet GlobalIdSetImp;
240 
242  typedef typename ALU3dGridCommunications< elType, Comm >::GlobalId GlobalIdType;
243 
245  typedef int LocalIdType;
246 
247  struct Traits
248  {
251 
254 
255  typedef typename GridFamily::GridImp Grid;
256 
259 
261 
264 
266 
268  typedef std::vector< const DuneBoundaryProjectionType * > DuneBoundaryProjectionVector;
269 
270  template< int cd >
271  struct Codim
272  {
273  // IMPORTANT: Codim<codim>::Geometry == Geometry<dim-codim,dimw>
278 
280 
281  // minimal information to generate entities
283 
286 
287  template< PartitionIteratorType pitype >
288  struct Partition
289  {
292  }; // struct Partition
293 
296  }; // struct Codim
297 
298  template< PartitionIteratorType pitype >
299  struct Partition
300  {
303  }; // struct Partition
304 
307 
310 
315 
316  typedef Dune::CollectiveCommunication< Comm > CollectiveCommunication;
317  }; // struct Traits
318 
321 
324 
325  }; // struct ALU3dGridFamily
326 
327 
328 
329  //**********************************************************************
330  //
331  // --ALU3dGrid
332  // --Grid
333  //
334  //**********************************************************************
335 
356  template< ALU3dGridElementType elType, class Comm >
357  class ALU3dGrid
358  : public GridDefaultImplementation< 3, 3, alu3d_ctype,
359  ALU3dGridFamily< elType, Comm > >,
360  public HasObjectStream,
361  public HasHierarchicIndexSet
362  {
365 
366  // for compatibility: MyType := ThisType
367  typedef ThisType MyType;
368 
369  // friend declarations
370  friend class ALU3dGridEntity< 0, 3, const ThisType>;
371  friend class ALU3dGridEntity< 1, 3, const ThisType>;
372  friend class ALU3dGridEntity< 2, 3, const ThisType>;
373  friend class ALU3dGridEntity< 3, 3, const ThisType>;
374 
376 
377  friend class ALU3dGridEntityPointerBase< 0, const ThisType >;
378  friend class ALU3dGridEntityPointerBase< 1, const ThisType >;
379  friend class ALU3dGridEntityPointerBase< 2, const ThisType >;
380  friend class ALU3dGridEntityPointerBase< 3, const ThisType >;
381 
382  friend class ALU3dGridEntityPointer< 0, const ThisType >;
383  friend class ALU3dGridEntityPointer< 1, const ThisType >;
384  friend class ALU3dGridEntityPointer< 2, const ThisType >;
385  friend class ALU3dGridEntityPointer< 3, const ThisType >;
386 
388  friend class ALU3dGridHierarchicIterator< const ThisType >;
389 
390  friend class ALU3dGridHierarchicIndexSet< elType, Comm >;
391  friend class ALU3dGridGlobalIdSet< elType, Comm >;
392  friend class ALU3dGridLocalIdSet< elType, Comm >;
393 
394  friend class Conversion< ThisType, HasObjectStream >;
395  friend class Conversion< const ThisType, HasObjectStream >;
396 
397  friend class Conversion< ThisType, HasHierarchicIndexSet >;
398  friend class Conversion< const ThisType, HasHierarchicIndexSet >;
399 
400  friend class ALU3dGridCommHelper< elType, Comm >;
401 
402  // new intersection iterator is a wrapper which get itersectioniteratoimp as pointers
403  public:
410 
413  friend class LeafIntersectionIteratorWrapper < const ThisType > ;
415 
416  //**********************************************************
417  // The Interface Methods
418  //**********************************************************
419  public:
420  enum { refineStepsForHalf = 1 };
421 
422  static const ALU3dGridElementType elementType = elType;
423  typedef typename ALU3DSPACE GatherScatterType::ObjectStreamType ObjectStreamType;
426 
428  typedef typename GridFamily::Traits Traits;
429 
430  static const int dimension = BaseType::dimension;
432 
433  protected:
435  friend class ALULocalGeometryStorage< const ThisType, GeometryObject, 8 >;
436 
437  public:
440 
445 
447  typedef GenericReferenceElement< alu3d_ctype, dimension > ReferenceElementType;
448 
453 
456 
459 
460  public:
465 
467 
468  protected:
470  // type of ALUGrid boundary projection wrapper
472 
475 
478 
481 
483  typedef typename Traits::LocalIdSet LocalIdSet;
484 
487  typedef typename Traits::template Codim< 0 >::LeafIterator LeafIteratorType;
488  typedef typename Traits::template Codim< 0 >::LeafIterator LeafIterator;
489 
491 
492  typedef typename ALU3dImplTraits< elType, Comm >::GitterImplType GitterImplType;
493 
495  enum {
497  MAXL = 32 };
498 
500  enum {
503 
505  enum {
510 
511  public:
512  typedef Comm MPICommunicatorType;
513 
514  typedef ALU3dGridCommunications< elType, Comm > Communications;
515 
516  protected:
519 
522  ALU3dGrid ( const std::string &macroTriangFilename,
523  const MPICommunicatorType mpiComm,
524  const DuneBoundaryProjectionType *bndPrj,
525  const DuneBoundaryProjectionVector *bndVec,
526  const ALUGridRefinementType refinementType );
527 
528  public:
530  virtual ~ALU3dGrid();
531 
533  static inline std::string name ();
534 
538  int maxLevel() const;
539 
541  template<int cd, PartitionIteratorType pitype>
542  typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator
543  lbegin (int level) const;
544 
546  template<int cd, PartitionIteratorType pitype>
547  typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator
548  lend (int level) const;
549 
551  template<int cd>
552  typename Traits::template Codim<cd>::
554  lbegin (int level) const;
555 
557  template<int cd>
558  typename Traits::template Codim<cd>::
560  lend (int level) const;
561 
562  private:
564  template <int codim, PartitionIteratorType pitype>
566  leafbegin(int level) const;
567 
569  template <int codim, PartitionIteratorType pitype>
571  leafend(int level) const;
572 
574  template <int codim>
575  typename Traits::template Codim<codim>::LeafIterator
576  leafbegin(int level) const;
577 
579  template <int codim>
580  typename Traits::template Codim<codim>::LeafIterator
581  leafend(int level) const;
582 
584  LeafIteratorType leafbegin (int level) const;
585 
587  LeafIteratorType leafend (int level) const;
588 
590  LeafIteratorType leafbegin () const;
591 
593  LeafIteratorType leafend () const;
594 
595  public:
597  template <int codim, PartitionIteratorType pitype>
599  leafbegin() const;
600 
602  template <int codim, PartitionIteratorType pitype>
604  leafend() const;
605 
607  template <int codim>
608  typename Traits::template Codim<codim>::LeafIterator
609  leafbegin() const;
610 
612  template <int codim>
613  typename Traits::template Codim<codim>::LeafIterator
614  leafend() const;
615 
616  private:
618  template <int codim, PartitionIteratorType pitype>
620  createLeafIteratorBegin (int level) const;
621 
623  template <int codim, PartitionIteratorType pitype>
625  createLeafIteratorEnd(int level) const;
626 
627  public:
629  int size (int level, int cd) const;
630 
632  int size (int codim) const;
633 
635  int size (int level, GeometryType type) const;
636 
638  size_t numBoundarySegments() const;
639 
641  int size (GeometryType type) const;
642 
644  int global_size (int cd) const ;
645 
646  // (no interface method) number of grid entities in the entire grid for given codim
647  int hierSetSize (int cd) const;
648 
650  const GlobalIdSet &globalIdSet () const
651  {
652  if( !globalIdSet_ )
653  globalIdSet_ = new GlobalIdSetImp( *this );
654  return *globalIdSet_;
655  }
656 
658  const LocalIdSet & localIdSet () const { return localIdSet_; }
659 
661  const typename Traits :: LeafIndexSet & leafIndexSet () const;
662 
664  const typename Traits :: LevelIndexSet & levelIndexSet (int level) const;
665 
670  bool loadBalance ();
671 
698  template <class DataHandle>
699  bool loadBalance (DataHandle & data);
700 
701  template< class DataHandleImpl, class Data >
703  {
705  LBHandle lbHandle( *this, dataHandle );
706  return loadBalance( lbHandle );
707  }
708 
710  int ghostSize (int level, int codim) const;
711 
713  int overlapSize (int level, int codim) const { return 0; }
714 
716  int ghostSize (int codim) const;
717 
719  int overlapSize (int codim) const { return 0; }
720 
722  template<class DataHandleImp,class DataTypeImp>
724  InterfaceType iftype, CommunicationDirection dir, int level) const;
725 
729  template<class DataHandleImp,class DataTypeImp>
731  InterfaceType iftype, CommunicationDirection dir) const;
732 
733  private:
734  typedef ALU3DSPACE GatherScatter GatherScatterType;
735 
736  public:
738  const CollectiveCommunication &comm () const { return communications().ccobj_; }
739 
741  bool preAdapt ( );
742 
744  void postAdapt ( );
745 
747  bool adapt ();
748 
753  template< class GridImp, class DataHandle >
755 
757  void globalRefine ( int refCount );
758 
759  template< class GridImp, class DataHandle >
760  void globalRefine ( int refCount, AdaptDataHandleInterface< GridImp, DataHandle > &handle );
761 
762  //**********************************************************
763  // End of Interface Methods
764  //**********************************************************
767  template <GrapeIOFileFormatType ftype>
768  bool writeGrid( const std::string filename, alu3d_ctype time ) const ;
769 
770  bool writeGrid_Xdr( const std::string filename, alu3d_ctype time ) const ;
772  bool writeGrid_Ascii( const std::string filename, alu3d_ctype time, bool scientific = false ) const ;
773 
776  bool writeMacroGrid( const std::string path, const std::string filename ) const ;
777 
780  template <GrapeIOFileFormatType ftype>
781  bool readGrid( const std::string filename, alu3d_ctype & time );
782 
783  // (no interface method) get hierarchic index set of the grid
784  const HierarchicIndexSet & hierarchicIndexSet () const { return hIndexSet_; }
785 
786  // set max of given mxl and actual maxLevel
787  // for loadBalance
788  void setMaxLevel (int mxl);
789 
790  // no interface method, but has to be public
791  void updateStatus ();
792 
794  bool mark( int refCount , const typename Traits::template Codim<0>::Entity & e);
795 
797  int getMark( const typename Traits::template Codim<0>::Entity & e) const;
798 
799  public:
801  {
802  return Communications::defaultComm();
803  }
804 
806 
807  template< class IntersectionType >
808  static const typename BaseType
809  :: template ReturnImplementationType< IntersectionType >
810  :: ImplementationType &
811  getRealIntersection ( const IntersectionType &intersection )
812  {
813  return getRealImplementation( intersection );
814  }
815 
817  const std::vector<GeometryType>& geomTypes (int codim) const { return geomTypes_[codim]; }
818 
819  // return reference to org ALU3dGrid
820  // private method, but otherwise we have to friend class all possible
821  // types of LevelIterator ==> later
822  GitterImplType &myGrid () const;
823 
824  virtual GitterImplType *createALUGrid ( const std::string &macroName )
825  {
826  assert( communications_ );
827  return communications_->createALUGrid( macroName, vertexProjection() );
828  }
829 
831 
832  // return appropriate ALUGrid builder
833  virtual typename ALU3DSPACE Gitter::Geometric::BuilderIF &getBuilder () const
834  {
836  }
837 
838  // helper function for factory
840  {
842  }
843 
846 
847  template < class EntitySeed >
848  typename Traits :: template Codim< EntitySeed :: codimension > :: EntityPointer
849  entityPointer( const EntitySeed& seed ) const
850  {
851  enum { codim = EntitySeed :: codimension };
852  typedef typename Traits :: template Codim< codim > :: EntityPointer EntityPointer;
854  return ALUPointer( factory(), seed ) ;
855  }
856 
857  // number of links to other processors, for internal use only
858  int nlinks () const { return communications().nlinks(); }
859 
861  {
863  return leafVertexList_;
864  }
865 
866  int getLevelOfLeafVertex ( const typename ALU3dImplTraits< elType, Comm >::VertexType &vertex ) const
867  {
868  assert( leafVertexList_.up2Date() );
869  return leafVertexList_.getLevel(vertex);
870  }
871 
872  VertexListType & getVertexList(int level) const
873  {
874  assert( level >= 0 );
875  assert( level <= maxLevel() );
876  VertexListType & vxList = vertexList_[level];
877  if(!vxList.up2Date()) vxList.setupVxList(*this,level);
878  return vxList;
879  }
880 
882  {
883  assert( codim >= 1 );
884  assert( codim <= 3 );
885  return ghostLeafList_[codim-1];
886  }
887 
888  ALU3dGridItemListType & getGhostLevelList(int codim, int level) const
889  {
890  assert( codim >= 1 );
891  assert( codim <= 3 );
892 
893  assert( level >= 0 );
894  assert( level <= maxLevel() );
895  return ghostLevelList_[codim-1][level];
896  }
897 
898  ALU3dGridItemListType & getEdgeList(int level) const
899  {
900  assert( level >= 0 );
901  assert( level <= maxLevel() );
902  return levelEdgeList_[level];
903  }
904 
905  protected:
907  ALU3dGrid( const ThisType & );
908 
910  const ThisType &operator= ( const ThisType & );
911 
913  void calcExtras();
914 
916  void calcMaxLevel();
917 
919  void recalcGlobalSize();
920 
922  void checkMacroGridFile (const std::string filename);
923 
925  void checkMacroGrid ();
926 
928  const DuneBoundaryProjectionType* boundaryProjection(const int segmentIndex) const
929  {
930  if( bndPrj_ )
931  {
932  return bndPrj_;
933  }
934  else
935  {
936  // pointer can be zero (which is emulates the identity mapping then)
937  assert( bndVec_ );
938  assert( segmentIndex < (int) bndVec_->size() );
939  return (*bndVec_)[ segmentIndex ];
940  }
941  }
942 
944  {
945  assert( communications_ );
946  return *communications_;
947  }
948 
949  const GridObjectFactoryType& factory() const {
950 #ifdef USE_SMP_PARALLEL
951  assert( (int) factoryVec_.size() > GridObjectFactoryType :: threadNumber() );
952  return factoryVec_[ GridObjectFactoryType :: threadNumber() ];
953 #else
954  return factory_;
955 #endif
956  }
957 
958  bool conformingRefinement() const
959  {
960  return (refinementType_ == conforming) ;
961  }
962  protected:
964  //
965  // Internal variables
966  //
968 
969  // the real ALU grid
971 
972  // max level of grid
973  int maxlevel_;
974 
975  // count how much elements where marked
976  mutable int coarsenMarked_;
977  mutable int refineMarked_;
978 
979  // at the moment the number of different geom types is 1
980  enum { numberOfGeomTypes = 1 };
981  std::vector< std::vector<GeometryType> > geomTypes_;
982 
983  // our hierarchic index set
985 
986  // out global id set
988 
989  // out global id set
991 
992  // the level index set ( default type )
993  mutable std::vector < LevelIndexSetImp * > levelIndexVec_;
994 
995  // the leaf index set
997 
998  // the reference element
1000 
1002 
1005 
1007 
1009 
1010  // the type of our size cache
1013 
1014 #ifdef USE_SMP_PARALLEL
1015  std::vector< GridObjectFactoryType > factoryVec_;
1016 #else
1018 #endif
1019 
1020  // variable to ensure that postAdapt ist called after adapt
1022 
1023  // pointer to Dune boundary projection
1025 
1026  // pointer to Dune boundary projection
1028 
1029  // boundary projection for vertices
1031 
1032  // pointer to communications object
1034 
1035  // refinement type (nonconforming or conforming)
1037  }; // end class ALU3dGrid
1038 
1039 
1040  bool checkMacroGrid ( ALU3dGridElementType elType ,
1041  const std::string filename );
1042  const char* elType2Name( ALU3dGridElementType elType );
1043 
1044  namespace Capabilities
1045  {
1046 
1047  template< ALU3dGridElementType elType, class Comm, int cdim >
1048  struct hasEntity< Dune::ALU3dGrid< elType, Comm >, cdim >
1049  {
1050  static const bool v = true;
1051  };
1052 
1053  template< ALU3dGridElementType elType, class Comm >
1054  struct isParallel< ALU3dGrid< elType, Comm > >
1055  {
1056  static const bool v = true;
1057  };
1058 
1059  template< ALU3dGridElementType elType, class Comm >
1060  struct isLevelwiseConforming< ALU3dGrid< elType, Comm > >
1061  {
1062  static const bool v = true;
1063  };
1064 
1065  template< ALU3dGridElementType elType, class Comm >
1066  struct hasBackupRestoreFacilities< ALU3dGrid< elType, Comm > >
1067  {
1068  static const bool v = true;
1069  };
1070 
1071  } // end namespace Capabilities
1072 
1073 } // end namespace Dune
1074 
1075 #include "grid_inline.hh"
1076 #if COMPILE_ALUGRID_INLINE
1077  #include "grid_imp.cc"
1078 #endif
1079 #endif