dune-grid  2.2.1
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  ALU3DSPACE Gitter::Geometric::BuilderIF* builder =
168  dynamic_cast< ALU3DSPACE Gitter::Geometric::BuilderIF* >( &grid.container() );
169  if( ! builder )
170  DUNE_THROW(InvalidStateException,"dynamic_cast of ALUGrid builder failed");
171  return *builder;
172  }
173 
175 
177  };
178 
179 #if ALU3DGRID_PARALLEL
180  template< ALU3dGridElementType elType >
181  struct ALU3dGridCommunications< elType, MPI_Comm >
182  {
183  typedef ALU3dGridGlobalIdSet< elType, MPI_Comm > GlobalIdSet;
184  typedef ALUGridId< ALUMacroKey > GlobalId;
185 
186  typedef ALU3DSPACE GitterDunePll GitterImplType;
187 
188  typedef Dune::CollectiveCommunication< MPI_Comm > CollectiveCommunication;
189 
190  explicit ALU3dGridCommunications ( MPI_Comm comm )
191  : ccobj_( comm ), mpAccess_( comm )
192  {}
193 
194  int nlinks () const { return mpAccess_.nlinks(); }
195 
196  GitterImplType *createALUGrid ( const std::string &macroName, ALU3DSPACE ProjectVertex *projection )
197  {
198  return new GitterImplType( macroName.c_str(), mpAccess_, projection );
199  }
200 
201  static MPI_Comm defaultComm () { return MPI_COMM_WORLD; }
202 
203  static int getRank ( MPI_Comm comm )
204  {
205  int rank = 0;
206  MPI_Comm_rank( comm, &rank );
207  return rank;
208  }
209 
210  static typename ALU3DSPACE Gitter::Geometric::BuilderIF &getBuilder ( GitterImplType &grid )
211  {
212  ALU3DSPACE Gitter::Geometric::BuilderIF* builder =
213  dynamic_cast< ALU3DSPACE Gitter::Geometric::BuilderIF* >( &grid.containerPll() );
214  if( ! builder )
215  DUNE_THROW(InvalidStateException,"dynamic_cast of ALUGrid builder failed");
216  return *builder;
217  }
218 
219  static void duneNotifyMacroGridChanges ( GitterImplType &grid )
220  {
221  grid.duneNotifyMacroGridChanges();
222  }
223 
224  CollectiveCommunication ccobj_;
225  ALU3DSPACE MpAccessMPI mpAccess_;
226  };
227 #endif // #if ALU3DGRID_PARALLEL
228 
229 
230 
231  // ALU3dGridFamily
232  // ---------------
233 
234  template< ALU3dGridElementType elType, class Comm >
236  {
239 
240  static const int dim = 3;
241  static const int dimworld = 3;
242 
245 
247  typedef typename ALU3dGridCommunications< elType, Comm >::GlobalIdSet GlobalIdSetImp;
248 
250  typedef typename ALU3dGridCommunications< elType, Comm >::GlobalId GlobalIdType;
251 
253  typedef int LocalIdType;
254 
255  struct Traits
256  {
259 
262 
263  typedef typename GridFamily::GridImp Grid;
264 
267 
269 
272 
274 
276  typedef std::vector< const DuneBoundaryProjectionType * > DuneBoundaryProjectionVector;
277 
278  template< int cd >
279  struct Codim
280  {
281  // IMPORTANT: Codim<codim>::Geometry == Geometry<dim-codim,dimw>
286 
288 
289  // minimal information to generate entities
291 
294 
295  template< PartitionIteratorType pitype >
296  struct Partition
297  {
300  }; // struct Partition
301 
304  }; // struct Codim
305 
306  template< PartitionIteratorType pitype >
307  struct Partition
308  {
311  }; // struct Partition
312 
315 
318 
323 
324  typedef Dune::CollectiveCommunication< Comm > CollectiveCommunication;
325  }; // struct Traits
326 
329 
332 
333  }; // struct ALU3dGridFamily
334 
335 
336 
337  //**********************************************************************
338  //
339  // --ALU3dGrid
340  // --Grid
341  //
342  //**********************************************************************
343 
364  template< ALU3dGridElementType elType, class Comm >
365  class ALU3dGrid
366  : public GridDefaultImplementation< 3, 3, alu3d_ctype,
367  ALU3dGridFamily< elType, Comm > >,
368  public HasObjectStream,
369  public HasHierarchicIndexSet
370  {
373 
374  // for compatibility: MyType := ThisType
375  typedef ThisType MyType;
376 
377  // friend declarations
378  friend class ALU3dGridEntity< 0, 3, const ThisType>;
379  friend class ALU3dGridEntity< 1, 3, const ThisType>;
380  friend class ALU3dGridEntity< 2, 3, const ThisType>;
381  friend class ALU3dGridEntity< 3, 3, const ThisType>;
382 
384 
385  friend class ALU3dGridEntityPointerBase< 0, const ThisType >;
386  friend class ALU3dGridEntityPointerBase< 1, const ThisType >;
387  friend class ALU3dGridEntityPointerBase< 2, const ThisType >;
388  friend class ALU3dGridEntityPointerBase< 3, const ThisType >;
389 
390  friend class ALU3dGridEntityPointer< 0, const ThisType >;
391  friend class ALU3dGridEntityPointer< 1, const ThisType >;
392  friend class ALU3dGridEntityPointer< 2, const ThisType >;
393  friend class ALU3dGridEntityPointer< 3, const ThisType >;
394 
396  friend class ALU3dGridHierarchicIterator< const ThisType >;
397 
398  friend class ALU3dGridHierarchicIndexSet< elType, Comm >;
399  friend class ALU3dGridGlobalIdSet< elType, Comm >;
400  friend class ALU3dGridLocalIdSet< elType, Comm >;
401 
402  friend class Conversion< ThisType, HasObjectStream >;
403  friend class Conversion< const ThisType, HasObjectStream >;
404 
405  friend class Conversion< ThisType, HasHierarchicIndexSet >;
406  friend class Conversion< const ThisType, HasHierarchicIndexSet >;
407 
408  friend struct ALU3dGridCommHelper< elType, Comm >;
409 
410  // new intersection iterator is a wrapper which get itersectioniteratoimp as pointers
411  public:
418 
421  friend class LeafIntersectionIteratorWrapper < const ThisType > ;
423 
424  //**********************************************************
425  // The Interface Methods
426  //**********************************************************
427  public:
428  enum { refineStepsForHalf = 1 };
429 
430  static const ALU3dGridElementType elementType = elType;
431  typedef typename ALU3DSPACE GatherScatterType::ObjectStreamType ObjectStreamType;
434 
436  typedef typename GridFamily::Traits Traits;
437 
438  static const int dimension = BaseType::dimension;
440 
441  protected:
443  friend class ALULocalGeometryStorage< const ThisType, GeometryObject, 8 >;
444 
445  public:
448 
453 
455  typedef GenericReferenceElement< alu3d_ctype, dimension > ReferenceElementType;
456 
461 
464 
467 
468  public:
473 
475 
476  protected:
478  // type of ALUGrid boundary projection wrapper
480 
483 
486 
489 
491  typedef typename Traits::LocalIdSet LocalIdSet;
492 
495  typedef typename Traits::template Codim< 0 >::LeafIterator LeafIteratorType;
496  typedef typename Traits::template Codim< 0 >::LeafIterator LeafIterator;
497 
499 
500  typedef typename ALU3dImplTraits< elType, Comm >::GitterImplType GitterImplType;
501 
503  enum {
505  MAXL = 32 };
506 
508  enum {
511 
513  enum {
518 
519  public:
520  typedef Comm MPICommunicatorType;
521 
522  typedef ALU3dGridCommunications< elType, Comm > Communications;
523 
524  protected:
527 
530  ALU3dGrid ( const std::string &macroTriangFilename,
531  const MPICommunicatorType mpiComm,
532  const DuneBoundaryProjectionType *bndPrj,
533  const DuneBoundaryProjectionVector *bndVec,
534  const ALUGridRefinementType refinementType );
535 
536  public:
538  virtual ~ALU3dGrid();
539 
541  static inline std::string name ();
542 
546  int maxLevel() const;
547 
549  template<int cd, PartitionIteratorType pitype>
550  typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator
551  lbegin (int level) const;
552 
554  template<int cd, PartitionIteratorType pitype>
555  typename Traits::template Codim<cd>::template Partition<pitype>::LevelIterator
556  lend (int level) const;
557 
559  template<int cd>
560  typename Traits::template Codim<cd>::
562  lbegin (int level) const;
563 
565  template<int cd>
566  typename Traits::template Codim<cd>::
568  lend (int level) const;
569 
570  private:
572  template <int codim, PartitionIteratorType pitype>
574  leafbegin(int level) const;
575 
577  template <int codim, PartitionIteratorType pitype>
579  leafend(int level) const;
580 
582  template <int codim>
583  typename Traits::template Codim<codim>::LeafIterator
584  leafbegin(int level) const;
585 
587  template <int codim>
588  typename Traits::template Codim<codim>::LeafIterator
589  leafend(int level) const;
590 
592  LeafIteratorType leafbegin (int level) const;
593 
595  LeafIteratorType leafend (int level) const;
596 
598  LeafIteratorType leafbegin () const;
599 
601  LeafIteratorType leafend () const;
602 
603  public:
605  template <int codim, PartitionIteratorType pitype>
607  leafbegin() const;
608 
610  template <int codim, PartitionIteratorType pitype>
612  leafend() const;
613 
615  template <int codim>
616  typename Traits::template Codim<codim>::LeafIterator
617  leafbegin() const;
618 
620  template <int codim>
621  typename Traits::template Codim<codim>::LeafIterator
622  leafend() const;
623 
624  private:
626  template <int codim, PartitionIteratorType pitype>
628  createLeafIteratorBegin (int level) const;
629 
631  template <int codim, PartitionIteratorType pitype>
633  createLeafIteratorEnd(int level) const;
634 
635  public:
637  int size (int level, int cd) const;
638 
640  int size (int codim) const;
641 
643  int size (int level, GeometryType type) const;
644 
646  size_t numBoundarySegments() const;
647 
649  int size (GeometryType type) const;
650 
652  int global_size (int cd) const ;
653 
654  // (no interface method) number of grid entities in the entire grid for given codim
655  int hierSetSize (int cd) const;
656 
658  const GlobalIdSet &globalIdSet () const
659  {
660  if( !globalIdSet_ )
661  globalIdSet_ = new GlobalIdSetImp( *this );
662  return *globalIdSet_;
663  }
664 
666  const LocalIdSet & localIdSet () const { return localIdSet_; }
667 
669  const typename Traits :: LeafIndexSet & leafIndexSet () const;
670 
672  const typename Traits :: LevelIndexSet & levelIndexSet (int level) const;
673 
678  bool loadBalance ();
679 
706  template <class DataHandle>
707  bool loadBalance (DataHandle & data);
708 
709  template< class DataHandleImpl, class Data >
711  {
713  LBHandle lbHandle( *this, dataHandle );
714  return loadBalance( lbHandle );
715  }
716 
718  int ghostSize (int level, int codim) const;
719 
721  int overlapSize (int level, int codim) const { return 0; }
722 
724  int ghostSize (int codim) const;
725 
727  int overlapSize (int codim) const { return 0; }
728 
730  template<class DataHandleImp,class DataTypeImp>
732  InterfaceType iftype, CommunicationDirection dir, int level) const;
733 
737  template<class DataHandleImp,class DataTypeImp>
739  InterfaceType iftype, CommunicationDirection dir) const;
740 
741  private:
742  typedef ALU3DSPACE GatherScatter GatherScatterType;
743 
744  public:
746  const CollectiveCommunication &comm () const { return communications().ccobj_; }
747 
749  bool preAdapt ( );
750 
752  void postAdapt ( );
753 
755  bool adapt ();
756 
761  template< class GridImp, class DataHandle >
763 
765  void globalRefine ( int refCount );
766 
767  template< class GridImp, class DataHandle >
768  void globalRefine ( int refCount, AdaptDataHandleInterface< GridImp, DataHandle > &handle );
769 
770  //**********************************************************
771  // End of Interface Methods
772  //**********************************************************
775  template <GrapeIOFileFormatType ftype>
776  bool writeGrid( const std::string filename, alu3d_ctype time ) const ;
777 
778  bool writeGrid_Xdr( const std::string filename, alu3d_ctype time ) const ;
780  bool writeGrid_Ascii( const std::string filename, alu3d_ctype time, bool scientific = false ) const ;
781 
784  bool writeMacroGrid( const std::string path, const std::string filename ) const ;
785 
788  template <GrapeIOFileFormatType ftype>
789  bool readGrid( const std::string filename, alu3d_ctype & time );
790 
791  // (no interface method) get hierarchic index set of the grid
792  const HierarchicIndexSet & hierarchicIndexSet () const { return hIndexSet_; }
793 
794  // set max of given mxl and actual maxLevel
795  // for loadBalance
796  void setMaxLevel (int mxl);
797 
798  // no interface method, but has to be public
799  void updateStatus ();
800 
802  bool mark( int refCount , const typename Traits::template Codim<0>::Entity & e);
803 
805  int getMark( const typename Traits::template Codim<0>::Entity & e) const;
806 
807  public:
809  {
810  return Communications::defaultComm();
811  }
812 
814 
815  template< class IntersectionType >
816  static const typename BaseType
817  :: template ReturnImplementationType< IntersectionType >
818  :: ImplementationType &
819  getRealIntersection ( const IntersectionType &intersection )
820  {
821  return getRealImplementation( intersection );
822  }
823 
825  const std::vector<GeometryType>& geomTypes (int codim) const { return geomTypes_[codim]; }
826 
827  // return reference to org ALU3dGrid
828  // private method, but otherwise we have to friend class all possible
829  // types of LevelIterator ==> later
830  GitterImplType &myGrid () const;
831 
832  virtual GitterImplType *createALUGrid ( const std::string &macroName )
833  {
834  assert( communications_ );
835  return communications_->createALUGrid( macroName, vertexProjection() );
836  }
837 
839 
840  // return appropriate ALUGrid builder
841  virtual typename ALU3DSPACE Gitter::Geometric::BuilderIF &getBuilder () const
842  {
844  }
845 
846  // helper function for factory
848  {
850  }
851 
854 
855  template < class EntitySeed >
856  typename Traits :: template Codim< EntitySeed :: codimension > :: EntityPointer
857  entityPointer( const EntitySeed& seed ) const
858  {
859  enum { codim = EntitySeed :: codimension };
860  typedef typename Traits :: template Codim< codim > :: EntityPointer EntityPointer;
862  return ALUPointer( factory(), seed ) ;
863  }
864 
865  // number of links to other processors, for internal use only
866  int nlinks () const { return communications().nlinks(); }
867 
869  {
871  return leafVertexList_;
872  }
873 
874  int getLevelOfLeafVertex ( const typename ALU3dImplTraits< elType, Comm >::VertexType &vertex ) const
875  {
876  assert( leafVertexList_.up2Date() );
877  return leafVertexList_.getLevel(vertex);
878  }
879 
880  VertexListType & getVertexList(int level) const
881  {
882  assert( level >= 0 );
883  assert( level <= maxLevel() );
884  VertexListType & vxList = vertexList_[level];
885  if(!vxList.up2Date()) vxList.setupVxList(*this,level);
886  return vxList;
887  }
888 
890  {
891  assert( codim >= 1 );
892  assert( codim <= 3 );
893  return ghostLeafList_[codim-1];
894  }
895 
896  ALU3dGridItemListType & getGhostLevelList(int codim, int level) const
897  {
898  assert( codim >= 1 );
899  assert( codim <= 3 );
900 
901  assert( level >= 0 );
902  assert( level <= maxLevel() );
903  return ghostLevelList_[codim-1][level];
904  }
905 
906  ALU3dGridItemListType & getEdgeList(int level) const
907  {
908  assert( level >= 0 );
909  assert( level <= maxLevel() );
910  return levelEdgeList_[level];
911  }
912 
913  protected:
915  ALU3dGrid( const ThisType & );
916 
918  const ThisType &operator= ( const ThisType & );
919 
921  void calcExtras();
922 
924  void calcMaxLevel();
925 
927  void recalcGlobalSize();
928 
930  void checkMacroGridFile (const std::string filename);
931 
933  void checkMacroGrid ();
934 
936  const DuneBoundaryProjectionType* boundaryProjection(const int segmentIndex) const
937  {
938  if( bndPrj_ )
939  {
940  return bndPrj_;
941  }
942  else
943  {
944  // pointer can be zero (which is emulates the identity mapping then)
945  assert( bndVec_ );
946  assert( segmentIndex < (int) bndVec_->size() );
947  return (*bndVec_)[ segmentIndex ];
948  }
949  }
950 
952  {
953  assert( communications_ );
954  return *communications_;
955  }
956 
957  const GridObjectFactoryType& factory() const {
958 #ifdef USE_SMP_PARALLEL
959  assert( (int) factoryVec_.size() > GridObjectFactoryType :: threadNumber() );
960  return factoryVec_[ GridObjectFactoryType :: threadNumber() ];
961 #else
962  return factory_;
963 #endif
964  }
965 
966  bool conformingRefinement() const
967  {
968  return (refinementType_ == conforming) ;
969  }
970  protected:
972  //
973  // Internal variables
974  //
976 
977  // the real ALU grid
979 
980  // max level of grid
981  int maxlevel_;
982 
983  // count how much elements where marked
984  mutable int coarsenMarked_;
985  mutable int refineMarked_;
986 
987  // at the moment the number of different geom types is 1
988  enum { numberOfGeomTypes = 1 };
989  std::vector< std::vector<GeometryType> > geomTypes_;
990 
991  // our hierarchic index set
993 
994  // out global id set
996 
997  // out global id set
999 
1000  // the level index set ( default type )
1001  mutable std::vector < LevelIndexSetImp * > levelIndexVec_;
1002 
1003  // the leaf index set
1005 
1006  // the reference element
1008 
1010 
1013 
1015 
1017 
1018  // the type of our size cache
1021 
1022 #ifdef USE_SMP_PARALLEL
1023  std::vector< GridObjectFactoryType > factoryVec_;
1024 #else
1026 #endif
1027 
1028  // variable to ensure that postAdapt ist called after adapt
1030 
1031  // pointer to Dune boundary projection
1033 
1034  // pointer to Dune boundary projection
1036 
1037  // boundary projection for vertices
1039 
1040  // pointer to communications object
1042 
1043  // refinement type (nonconforming or conforming)
1045  }; // end class ALU3dGrid
1046 
1047 
1048  bool checkMacroGrid ( ALU3dGridElementType elType ,
1049  const std::string filename );
1050  const char* elType2Name( ALU3dGridElementType elType );
1051 
1052  namespace Capabilities
1053  {
1054 
1055  template< ALU3dGridElementType elType, class Comm, int cdim >
1056  struct hasEntity< Dune::ALU3dGrid< elType, Comm >, cdim >
1057  {
1058  static const bool v = true;
1059  };
1060 
1061  template< ALU3dGridElementType elType, class Comm >
1062  struct isParallel< ALU3dGrid< elType, Comm > >
1063  {
1064  static const bool v = true;
1065  };
1066 
1067  template< ALU3dGridElementType elType, class Comm >
1068  struct isLevelwiseConforming< ALU3dGrid< elType, Comm > >
1069  {
1070  static const bool v = true;
1071  };
1072 
1073  template< ALU3dGridElementType elType, class Comm >
1074  struct hasBackupRestoreFacilities< ALU3dGrid< elType, Comm > >
1075  {
1076  static const bool v = true;
1077  };
1078 
1079  } // end namespace Capabilities
1080 
1081 } // end namespace Dune
1082 
1083 #include "grid_inline.hh"
1084 #if COMPILE_ALUGRID_INLINE
1085  #include "grid_imp.cc"
1086 #endif
1087 #endif