dune-grid  2.2.0
grapegriddisplay.hh
Go to the documentation of this file.
00001 #ifndef DUNE_GRAPEGRIDDISPLAY_HH
00002 #define DUNE_GRAPEGRIDDISPLAY_HH
00003 
00004 //- system includes 
00005 #include <list>
00006 #include <set>
00007 #include <stack>
00008 
00009 //- Dune includes 
00010 #include <dune/common/stdstreams.hh>
00011 #include <dune/geometry/typeindex.hh>
00012 #include <dune/grid/common/grid.hh>
00013 
00014 #if HAVE_GRAPE
00015 //-local includes 
00016 #include "grape/grapeinclude.hh"
00017 #endif
00018 
00024 namespace Dune 
00025 {
00026 
00028 enum GrapePartitionIteratorType 
00029 {
00030   g_Interior_Partition       = Interior_Partition,
00031   g_InteriorBorder_Partition = InteriorBorder_Partition,
00032   g_Overlap_Partition        = Overlap_Partition,
00033   g_OverlapFront_Partition   = OverlapFront_Partition,
00034   g_All_Partition            = All_Partition,
00035   g_Ghost_Partition          = Ghost_Partition
00036 };
00037   
00040 enum GrapeIteratorType 
00041 {
00042   g_LeafIterator       = 0,
00043   g_LevelIterator      = 1,
00044   g_HierarchicIterator = 2,
00045   g_GridPart           = 3 
00046 };
00047   
00051 template<class GridType>
00052 class GrapeGridDisplay 
00053 {
00054   typedef GrapeGridDisplay < GridType > MyDisplayType;
00055   typedef  MyDisplayType ThisType;
00056   enum { dim = GridType::dimension };
00057   enum { dimworld = GridType::dimensionworld };
00058 
00059 public:
00060 #if HAVE_GRAPE
00061   // defined in griddisplay.hh 
00062   typedef typename GrapeInterface<dim,dimworld>::DUNE_ELEM  DUNE_ELEM;
00063   typedef typename GrapeInterface<dim,dimworld>::DUNE_DAT   DUNE_DAT;
00064   typedef typename GrapeInterface<dim,dimworld>::DUNE_FDATA DUNE_FDATA;
00065   typedef typename GrapeInterface<dim,dimworld>::F_DATA     F_DATA;
00066   typedef typename GrapeInterface<dim,dimworld>::STACKENTRY STACKENTRY;
00067   
00068   typedef typename std::stack < STACKENTRY * > StackEntryType; 
00069   typedef void setGridPartIterators_t(DUNE_DAT * , void * gridPart);
00070 #endif // #if HAVE_GRAPE
00071 
00072   typedef typename GridType::HierarchicIterator HierarchicIteratorType; 
00073 
00074   typedef typename GridType::Traits::LocalIdSet LocalIdSetType; 
00075   typedef typename GridType::Traits::LeafIndexSet LeafIndexSetType; 
00076     
00077 protected:
00078   typedef std::list<HierarchicIteratorType *> HierarchicIteratorList;
00079   typedef typename HierarchicIteratorList::iterator ListIteratorType;
00080 
00081 private:  
00083   GrapeGridDisplay(const GrapeGridDisplay &); 
00084 public:
00086   inline GrapeGridDisplay(const GridType &grid, const int myrank = -1);
00087 
00089   template <class GridPartType>
00090   inline GrapeGridDisplay(const GridPartType &gridPart, const int myrank = -1);
00091 
00092   template< class VT >
00093   inline GrapeGridDisplay ( const GridView< VT > &gridView, const int myrank = -1 );
00094 
00096   inline ~GrapeGridDisplay();
00097 
00100   inline void display();
00101 
00103   int myRank () const { return myRank_; }
00104 
00106   inline const GridType& getGrid() const ;
00107 
00108 #if HAVE_GRAPE
00109 
00110   inline void * getHmesh();
00111 
00113   DUNE_DAT & getDuneDat () { return dune_; } 
00114 
00116   inline void addMyMeshToTimeScene(void * timescene, double time, int proc);
00117  
00118   bool hasData () { return false; }
00119 
00120 protected:  
00121   // generate hmesh 
00122   inline void * setupHmesh();
00123 
00124   inline void deleteHmesh();
00125 
00126   typedef typename GridType::template Codim<0>::Entity EntityCodim0Type;
00127 
00128   // type of index method 
00129   typedef int EntityIndexFuncType(void * iset, const EntityCodim0Type & en);
00130   // type of vertex method 
00131   typedef int VertexIndexFuncType(void * iset, const EntityCodim0Type & en, int vx);
00132   
00133   // return element index from given index set
00134   template <class IndexSetType>
00135   static int getEntityIndex(void * iset, const EntityCodim0Type & en)
00136   {
00137     assert( iset );
00138     const IndexSetType * set = ((const IndexSetType *) iset);
00139     return (en.isLeaf()) ? set->index(en) : -1;
00140   }
00141   
00142   // return vertex index from given index set
00143   template< class IndexSet >
00144   static int getVertexIndex ( void *iset, const EntityCodim0Type &entity, int vx )
00145   {
00146     assert( iset != 0 );
00147     const IndexSet *indexSet = (const IndexSet *)iset;
00148     //return set->template subIndex< dim >( entity, vx );
00149     return indexSet->subIndex( entity, vx, dim );
00150   }
00151   
00152 public:
00153 //****************************************************************  
00154 //
00155 // --GrapeGridDisplay, Some Subroutines needed for display with GRAPE
00156 // 
00157 //****************************************************************  
00158   // update element from entity
00159   template <class IntersectionIteratorType>
00160   inline void checkNeighbors(IntersectionIteratorType&, 
00161             const IntersectionIteratorType&, DUNE_ELEM *) ;
00162   
00163   // update element from entity
00164   template <class Entity>
00165   inline void el_update_base (Entity& en , DUNE_ELEM *) ;
00166 
00167   // update element from entity
00168   template <class EntityPointerType>
00169   inline int el_update (EntityPointerType *, DUNE_ELEM *) ;
00170 
00171   // update element from entity
00172   template <class EntityPointerType, class GridPartType>
00173   inline int el_update (EntityPointerType *, DUNE_ELEM *, GridPartType& );
00174 
00175   template< class EntityPointer, class VT >
00176   int el_update ( EntityPointer *, DUNE_ELEM *, const GridView< VT > & );
00177 
00178   // update child element 
00179   template <class EntityPointerType>
00180   inline int child_update (EntityPointerType * , DUNE_ELEM *) ;
00181 
00182   template <class EntityPointerType>
00183   inline int child_n_update (EntityPointerType *, DUNE_ELEM *) ;
00184 
00185   // first and next macro element via LevelIterator level 0 
00186   template <PartitionIteratorType pitype> 
00187   inline int first_leaf (DUNE_ELEM * he) ;
00188   template <PartitionIteratorType pitype> 
00189   inline int next_leaf (DUNE_ELEM * he) ;
00190   
00191   // first and next macro element via LevelIterator level 0 
00192   template <class GridPartImp> 
00193   inline int first_item (DUNE_ELEM * he) ;
00194   template <class GridPartImp> 
00195   inline int next_item (DUNE_ELEM * he) ;
00196   
00197   // first and next macro element via LevelIterator level  
00198   template <PartitionIteratorType pitype> 
00199   inline int first_level (DUNE_ELEM * he, int level) ;
00200   
00201   template <PartitionIteratorType pitype> 
00202   inline int next_level (DUNE_ELEM * he) ;
00203 
00204   // methods to call for combined display 
00205   inline int firstMacro (DUNE_ELEM * elem) { return dune_.first_macro(elem); }
00206   inline int nextMacro  (DUNE_ELEM * elem) { return dune_.next_macro(elem);  }
00207   inline int firstChild (DUNE_ELEM * elem) { return (dune_.first_child) ? dune_.first_child(elem) : 0; }
00208   inline int nextChild  (DUNE_ELEM * elem) { return (dune_.next_child) ? dune_.next_child(elem) : 0; }
00209   
00210   // first and next child via HierarchicIterator with given maxlevel in Grape
00211   inline int first_child (DUNE_ELEM * he) ;
00212   inline int next_child (DUNE_ELEM * he) ;
00213 
00214 public:   
00215   // delete leaf iterators 
00216   template <PartitionIteratorType pitype> 
00217   inline void delete_leaf (DUNE_ELEM * he) ;
00218   // delete level iterators 
00219   template <PartitionIteratorType pitype> 
00220   inline void delete_level (DUNE_ELEM * he) ;
00221   // delete level and hierarchical iterators 
00222   template <PartitionIteratorType pitype> 
00223   inline void delete_hier (DUNE_ELEM * he) ;
00224 
00225   // delete iterators 
00226   template <class IteratorType> 
00227   inline void delete_iterators(DUNE_ELEM * he) ;
00228 public:  
00229   
00230   // fake function for copy iterator 
00231   inline static void * copy_iterator (const void * i) ;
00232 
00233   // local to world 
00234   inline void local2world (DUNE_ELEM * he, const double * c, double * w);
00235 
00236   // world to local 
00237   inline int world2local (DUNE_ELEM * he, const double * w, double * c);
00238  
00239   // check inside reference element 
00240   inline int checkWhetherInside (DUNE_ELEM * he, const double * w);
00241 
00242   //*********************************
00243   //  wrapper functions 
00244   //*********************************
00245   // local to world 
00246   inline static void ctow (DUNE_ELEM * he, const double * c, double * w);
00247 
00248   // world to local 
00249   inline static int wtoc (DUNE_ELEM * he, const double * w, double * c);
00250  
00251   // check inside reference element 
00252   inline static int check_inside (DUNE_ELEM * he, const double * w);
00253 
00254   // dito 
00255   template< class Entity >
00256   int checkInside ( const Entity &entity, const double *w );
00257  
00258   // dito 
00259   template< class Entity >
00260   int world_to_local ( const Entity &entity, const double *w, double *c );
00261   
00262   // dito
00263   template <class EntityType>
00264   inline void local_to_world(const EntityType &en, const double * c, double * w);  
00265 
00266   template <PartitionIteratorType pitype>
00267   inline void selectIterators(DUNE_DAT *, void *, setGridPartIterators_t *) const;
00268   
00269   inline void setIterationMethods(DUNE_DAT *, DUNE_FDATA * ) const;
00270   
00271   inline void changeIterationMethods(int iterType, int partType, DUNE_FDATA *);
00272 
00273   template <PartitionIteratorType pitype>
00274   struct IterationMethods 
00275   {
00276     // wrapper methods for first_child and next_child
00277     inline static int first_mac (DUNE_ELEM * he)
00278     {
00279       MyDisplayType & disp = *((MyDisplayType *) he->display);
00280       return disp.template first_level<pitype>(he,0);
00281     }
00282 
00283     // wrapper methods for first_child and next_child
00284     inline static int first_lev (DUNE_ELEM * he)
00285     {
00286       MyDisplayType & disp = *((MyDisplayType *) he->display);
00287       return disp.template first_level<pitype>(he,he->level_of_interest);
00288     }
00289 
00290     inline static int next_lev  (DUNE_ELEM * he)
00291     {
00292       MyDisplayType & disp = *((MyDisplayType *) he->display);
00293       return disp.template next_level<pitype>(he);
00294     }
00295   
00296     // wrapper methods for first_child and next_child
00297     inline static int fst_leaf (DUNE_ELEM * he)
00298     {
00299       MyDisplayType & disp = *((MyDisplayType *) he->display);
00300       return disp.template first_leaf<pitype>(he);
00301     }
00302     inline static int nxt_leaf (DUNE_ELEM * he)
00303     {
00304       MyDisplayType & disp = *((MyDisplayType *) he->display);
00305       return disp.template next_leaf<pitype>(he);
00306     }
00307   
00308     // wrapper methods for first_child and next_child
00309     inline static int fst_child (DUNE_ELEM * he)
00310     {
00311       MyDisplayType & disp = *((MyDisplayType *) he->display);
00312       return disp.first_child(he);
00313     }
00314     inline static int nxt_child (DUNE_ELEM * he)
00315     {
00316       MyDisplayType & disp = *((MyDisplayType *) he->display);
00317       return disp.next_child(he);
00318     }
00319 
00320     // wrapper methods for deleting iterators 
00321     inline static void del_leaf (DUNE_ELEM * he)
00322     {
00323       MyDisplayType & disp = *((MyDisplayType *) he->display);
00324       disp.template delete_leaf<pitype>(he);
00325     }
00326 
00327     // wrapper methods for deleting iterators 
00328     inline static void del_level (DUNE_ELEM * he)
00329     {
00330       MyDisplayType & disp = *((MyDisplayType *) he->display);
00331       disp.template delete_level<pitype>(he);
00332     }
00333 
00334     // wrapper methods for deleting iterators 
00335     inline static void del_hier (DUNE_ELEM * he)
00336     {
00337       MyDisplayType & disp = *((MyDisplayType *) he->display);
00338       disp.template delete_hier<pitype>(he);
00339     }
00340 
00341   };
00342 
00343 protected:
00344   template <class GridPartType>
00345   struct IterationMethodsGP
00346   {
00347     // wrapper methods for first_item and next_item
00348     inline static int fst_item (DUNE_ELEM * he)
00349     {
00350       assert( he->display );
00351       MyDisplayType & disp = *((MyDisplayType *) he->display);
00352       return disp.template first_item<GridPartType>(he);
00353     }
00354     inline static int nxt_item (DUNE_ELEM * he)
00355     {
00356       assert( he->display );
00357       MyDisplayType & disp = *((MyDisplayType *) he->display);
00358       return disp.template next_item<GridPartType>(he);
00359     }
00360     
00361     // delete iterators 
00362     inline static void del_iter (DUNE_ELEM * he)
00363     {
00364       assert( he->display );
00365       MyDisplayType & disp = *((MyDisplayType *) he->display);
00366       typedef typename GridPartType :: template Codim<0> :: IteratorType IteratorType;
00367       disp.template delete_iterators<IteratorType> (he);
00368     }
00369   };
00370 
00371   template <class GridPartImp> 
00372   struct SetIter
00373   {
00374     static void setGPIterator (DUNE_DAT * dune ,void * gridPart)
00375     {
00376       assert( gridPart );
00377       dune->gridPart = gridPart;
00378       dune->first_macro = &IterationMethodsGP<GridPartImp>::fst_item;
00379       dune->next_macro  = &IterationMethodsGP<GridPartImp>::nxt_item;
00380       dune->delete_iter = &IterationMethodsGP<GridPartImp>::del_iter;
00381 
00382       dune->first_child = 0;
00383       dune->next_child = 0;
00384     }
00385   };
00386 
00387   template< class ViewTraits >
00388   struct GridViewIterators
00389   {
00390     typedef Dune::GridView< ViewTraits > GridView;
00391     typedef typename GridView::template Codim< 0 >::Iterator Iterator;
00392 
00393     static int first_macro ( DUNE_ELEM *he )
00394     {
00395       assert( he->display != 0 );
00396       MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
00397 
00398       if( he->liter != 0 )
00399         display.template delete_iterators< Iterator >( he );
00400   
00401       assert( he->gridPart != 0 );
00402       const GridView &gridView = *static_cast< const GridView * >( he->gridPart );
00403 
00404       assert( he->liter   == 0 );
00405       assert( he->enditer == 0 );
00406       
00407       Iterator *it  = new Iterator( gridView.template begin< 0 >() );
00408       Iterator *end = new Iterator( gridView.template end  < 0 >() );
00409 
00410       he->liter   = it;
00411       he->enditer = end;
00412 
00413       if( *it == *end )
00414       {
00415         display.template delete_iterators< Iterator >( he );
00416         return 0;
00417       }
00418 
00419       return display.el_update( it, he, gridView );
00420     }
00421 
00422     static int next_macro ( DUNE_ELEM *he )
00423     {
00424       assert( he->display != 0 );
00425       MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
00426 
00427       assert( he->gridPart != 0 );
00428       const GridView &gridView = *static_cast< const GridView * >( he->gridPart );
00429 
00430       Iterator *it  = static_cast< Iterator * >( he->liter );
00431       Iterator *end = static_cast< Iterator * >( he->enditer );
00432       assert( (it != 0) && (end != 0) );
00433      
00434       ++(*it);
00435       if( *it == *end )
00436       {
00437         display.template delete_iterators< Iterator >( he );
00438         return 0;
00439       }
00440 
00441       return display.el_update( it, he, gridView );
00442     }
00443     
00444     static void delete_iter ( DUNE_ELEM *he )
00445     {
00446       assert( he->display );
00447       MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
00448       display.template delete_iterators< Iterator >( he );
00449     }
00450 
00451     static void set ( DUNE_DAT *dune, void *gridView )
00452     {
00453       assert( gridView );
00454       dune->gridPart = gridView;
00455       dune->first_macro = &first_macro;
00456       dune->next_macro = &next_macro;
00457       dune->delete_iter = &delete_iter;
00458 
00459       dune->first_child = 0;
00460       dune->next_child = 0;
00461     }
00462   };
00463 
00464   inline static void setIterationModus(DUNE_DAT * , DUNE_FDATA *);
00465 
00466 public:  
00467   // create STACKENTRY or get from stack 
00468   inline static void * getStackEntry(StackEntryType & stackEntry); 
00469  
00470   // get StackEntry Wrapper 
00471   inline static void * getStackEn(DUNE_DAT * dune);
00472 
00473   // free StackEntry Wrapper 
00474   inline static void freeStackEn(DUNE_DAT * dune, void * entry);
00475 
00476   inline static void deleteStackEntry(StackEntryType &);
00477 
00478   // push STACKENTRY to stack 
00479   inline static void freeStackEntry(StackEntryType & stackEntry, void * entry);
00480 
00481 protected:  
00483   DUNE_ELEM hel_;
00484   DUNE_DAT dune_;
00485   setGridPartIterators_t * setGridPartIter_;
00486 
00487   // pointer to index method 
00488   const EntityIndexFuncType * entityIndex;
00489   // pointer to vertex method   
00490   const VertexIndexFuncType * vertexIndex;
00491 
00492   StackEntryType stackEntry_;
00493 #endif // #if HAVE_GRAPE
00494 
00495   // the grid we want to display 
00496   const GridType &grid_;
00497   
00498   // true if we can use LevelIntersectionIterator 
00499   const bool hasLevelIntersections_;
00500 
00501   void * gridPart_;
00502 
00503   // leaf index set of the grid 
00504   void * indexSet_;
00505 
00506   // leaf index set of the grid 
00507   const LocalIdSetType & lid_; 
00508 
00509   // my process number 
00510   const int myRank_;
00511 
00512   // no better way than this canot export HMESH structure to here
00513   // pointer to hmesh 
00514   void * hmesh_;
00515 
00516   HierarchicIteratorList hierList_;
00517 }; // end class GrapeGridDisplay
00518 
00519 #if HAVE_GRAPE
00520 /**************************************************************************/
00521 //  element types, see dune/grid/common/grid.hh 
00522 // and also geldesc.hh for GR_ElementTypes 
00523 enum GRAPE_ElementType
00524     {  g_vertex         = GrapeInterface_three_three::gr_vertex
00525     ,  g_line           = GrapeInterface_three_three::gr_line
00526     ,  g_triangle       = GrapeInterface_three_three::gr_triangle
00527     ,  g_quadrilateral  = GrapeInterface_three_three::gr_quadrilateral
00528     ,  g_tetrahedron    = GrapeInterface_three_three::gr_tetrahedron
00529     ,  g_pyramid        = GrapeInterface_three_three::gr_pyramid
00530     ,  g_prism          = GrapeInterface_three_three::gr_prism 
00531     ,  g_hexahedron     = GrapeInterface_three_three::gr_hexahedron
00532     ,  g_iso_triangle   = GrapeInterface_three_three::gr_iso_triangle
00533     ,  g_iso_quadrilateral  = GrapeInterface_three_three::gr_iso_quadrilateral
00534     ,  g_unknown            = GrapeInterface_three_three::gr_unknown
00535     };
00536 
00538 static inline GRAPE_ElementType convertToGrapeType ( GeometryType duneType , int dim )
00539 {
00540   static std::vector< GRAPE_ElementType > convertedType; 
00541   if( convertedType.size() == 0 ) 
00542   {
00543     const size_t geomSize = GlobalGeometryTypeIndex :: size( 3 ) ;
00544     convertedType.resize( geomSize, g_unknown );
00545 
00546     GeometryType type ; 
00548     //  2D 
00550     type.makeVertex();
00551     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_vertex;
00552     type.makeLine();
00553     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_line;
00554     type.makeTriangle();
00555     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_triangle;
00556     type.makeQuadrilateral();
00557     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_quadrilateral;
00558 
00560     //  3D 
00562     type.makeTetrahedron();
00563     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_tetrahedron;
00564     type.makeHexahedron();
00565     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_hexahedron;
00566     type.makePyramid();
00567     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_pyramid;
00568     type.makePrism();
00569     convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_prism;
00570   }
00571 
00572   assert( GlobalGeometryTypeIndex :: index( duneType ) < convertedType.size() );
00573   assert( convertedType[ GlobalGeometryTypeIndex :: index( duneType ) ] != g_unknown );
00574   return convertedType[ GlobalGeometryTypeIndex :: index( duneType ) ];
00575 }
00576 
00577 // see geldesc.hh for definition of this mapping 
00578 // this is the same for all namespaces (two_two , and two_three, ...)
00579 static const int * const * vxMap = GrapeInterface_three_three::dune2GrapeVertex;
00580 static inline int mapDune2GrapeVertex( int geomType , int vx )
00581 {
00582   enum { usedTypes = GrapeInterface_three_three::numberOfUsedGrapeElementTypes };
00583   assert( geomType >= 0 );
00584   assert( geomType <  usedTypes ); // at the moment only defined from 2 to 7 
00585   return vxMap[geomType][vx];
00586 }
00587 
00588 // see geldesc.hh for definition of this mapping 
00589 // this is the same for all namespaces (two_two , and two_three, ...)
00590 static const int * const * faceMap = GrapeInterface_three_three::dune2GrapeFace;
00591 static inline int mapDune2GrapeFace( int geomType , int duneFace )
00592 {
00593   enum { usedTypes = GrapeInterface_three_three::numberOfUsedGrapeElementTypes };
00594   assert( geomType >= 0 );
00595   assert( geomType <  usedTypes ); // at the moment only defined from 2 to 7 
00596   return faceMap[geomType][ duneFace ];
00597 }
00598 #endif
00599 
00600 } // end namespace Dune
00601 
00602 #include "grape/grapegriddisplay.cc"
00603 
00604 // undefs all defines 
00605 #include "grape/grape_undefs.hh"
00606 #endif