dune-grid  2.2.0
grapegriddisplay.hh
Go to the documentation of this file.
1 #ifndef DUNE_GRAPEGRIDDISPLAY_HH
2 #define DUNE_GRAPEGRIDDISPLAY_HH
3 
4 //- system includes
5 #include <list>
6 #include <set>
7 #include <stack>
8 
9 //- Dune includes
10 #include <dune/common/stdstreams.hh>
11 #include <dune/geometry/typeindex.hh>
12 #include <dune/grid/common/grid.hh>
13 
14 #if HAVE_GRAPE
15 //-local includes
16 #include "grape/grapeinclude.hh"
17 #endif
18 
24 namespace Dune
25 {
26 
29 {
36 };
37 
41 {
46 };
47 
51 template<class GridType>
53 {
55  typedef MyDisplayType ThisType;
56  enum { dim = GridType::dimension };
57  enum { dimworld = GridType::dimensionworld };
58 
59 public:
60 #if HAVE_GRAPE
61  // defined in griddisplay.hh
62  typedef typename GrapeInterface<dim,dimworld>::DUNE_ELEM DUNE_ELEM;
63  typedef typename GrapeInterface<dim,dimworld>::DUNE_DAT DUNE_DAT;
64  typedef typename GrapeInterface<dim,dimworld>::DUNE_FDATA DUNE_FDATA;
65  typedef typename GrapeInterface<dim,dimworld>::F_DATA F_DATA;
67 
68  typedef typename std::stack < STACKENTRY * > StackEntryType;
69  typedef void setGridPartIterators_t(DUNE_DAT * , void * gridPart);
70 #endif // #if HAVE_GRAPE
71 
72  typedef typename GridType::HierarchicIterator HierarchicIteratorType;
73 
74  typedef typename GridType::Traits::LocalIdSet LocalIdSetType;
75  typedef typename GridType::Traits::LeafIndexSet LeafIndexSetType;
76 
77 protected:
78  typedef std::list<HierarchicIteratorType *> HierarchicIteratorList;
79  typedef typename HierarchicIteratorList::iterator ListIteratorType;
80 
81 private:
84 public:
86  inline GrapeGridDisplay(const GridType &grid, const int myrank = -1);
87 
89  template <class GridPartType>
90  inline GrapeGridDisplay(const GridPartType &gridPart, const int myrank = -1);
91 
92  template< class VT >
93  inline GrapeGridDisplay ( const GridView< VT > &gridView, const int myrank = -1 );
94 
96  inline ~GrapeGridDisplay();
97 
100  inline void display();
101 
103  int myRank () const { return myRank_; }
104 
106  inline const GridType& getGrid() const ;
107 
108 #if HAVE_GRAPE
109 
110  inline void * getHmesh();
111 
113  DUNE_DAT & getDuneDat () { return dune_; }
114 
116  inline void addMyMeshToTimeScene(void * timescene, double time, int proc);
117 
118  bool hasData () { return false; }
119 
120 protected:
121  // generate hmesh
122  inline void * setupHmesh();
123 
124  inline void deleteHmesh();
125 
126  typedef typename GridType::template Codim<0>::Entity EntityCodim0Type;
127 
128  // type of index method
129  typedef int EntityIndexFuncType(void * iset, const EntityCodim0Type & en);
130  // type of vertex method
131  typedef int VertexIndexFuncType(void * iset, const EntityCodim0Type & en, int vx);
132 
133  // return element index from given index set
134  template <class IndexSetType>
135  static int getEntityIndex(void * iset, const EntityCodim0Type & en)
136  {
137  assert( iset );
138  const IndexSetType * set = ((const IndexSetType *) iset);
139  return (en.isLeaf()) ? set->index(en) : -1;
140  }
141 
142  // return vertex index from given index set
143  template< class IndexSet >
144  static int getVertexIndex ( void *iset, const EntityCodim0Type &entity, int vx )
145  {
146  assert( iset != 0 );
147  const IndexSet *indexSet = (const IndexSet *)iset;
148  //return set->template subIndex< dim >( entity, vx );
149  return indexSet->subIndex( entity, vx, dim );
150  }
151 
152 public:
153 //****************************************************************
154 //
155 // --GrapeGridDisplay, Some Subroutines needed for display with GRAPE
156 //
157 //****************************************************************
158  // update element from entity
159  template <class IntersectionIteratorType>
160  inline void checkNeighbors(IntersectionIteratorType&,
161  const IntersectionIteratorType&, DUNE_ELEM *) ;
162 
163  // update element from entity
164  template <class Entity>
165  inline void el_update_base (Entity& en , DUNE_ELEM *) ;
166 
167  // update element from entity
168  template <class EntityPointerType>
169  inline int el_update (EntityPointerType *, DUNE_ELEM *) ;
170 
171  // update element from entity
172  template <class EntityPointerType, class GridPartType>
173  inline int el_update (EntityPointerType *, DUNE_ELEM *, GridPartType& );
174 
175  template< class EntityPointer, class VT >
176  int el_update ( EntityPointer *, DUNE_ELEM *, const GridView< VT > & );
177 
178  // update child element
179  template <class EntityPointerType>
180  inline int child_update (EntityPointerType * , DUNE_ELEM *) ;
181 
182  template <class EntityPointerType>
183  inline int child_n_update (EntityPointerType *, DUNE_ELEM *) ;
184 
185  // first and next macro element via LevelIterator level 0
186  template <PartitionIteratorType pitype>
187  inline int first_leaf (DUNE_ELEM * he) ;
188  template <PartitionIteratorType pitype>
189  inline int next_leaf (DUNE_ELEM * he) ;
190 
191  // first and next macro element via LevelIterator level 0
192  template <class GridPartImp>
193  inline int first_item (DUNE_ELEM * he) ;
194  template <class GridPartImp>
195  inline int next_item (DUNE_ELEM * he) ;
196 
197  // first and next macro element via LevelIterator level
198  template <PartitionIteratorType pitype>
199  inline int first_level (DUNE_ELEM * he, int level) ;
200 
201  template <PartitionIteratorType pitype>
202  inline int next_level (DUNE_ELEM * he) ;
203 
204  // methods to call for combined display
205  inline int firstMacro (DUNE_ELEM * elem) { return dune_.first_macro(elem); }
206  inline int nextMacro (DUNE_ELEM * elem) { return dune_.next_macro(elem); }
207  inline int firstChild (DUNE_ELEM * elem) { return (dune_.first_child) ? dune_.first_child(elem) : 0; }
208  inline int nextChild (DUNE_ELEM * elem) { return (dune_.next_child) ? dune_.next_child(elem) : 0; }
209 
210  // first and next child via HierarchicIterator with given maxlevel in Grape
211  inline int first_child (DUNE_ELEM * he) ;
212  inline int next_child (DUNE_ELEM * he) ;
213 
214 public:
215  // delete leaf iterators
216  template <PartitionIteratorType pitype>
217  inline void delete_leaf (DUNE_ELEM * he) ;
218  // delete level iterators
219  template <PartitionIteratorType pitype>
220  inline void delete_level (DUNE_ELEM * he) ;
221  // delete level and hierarchical iterators
222  template <PartitionIteratorType pitype>
223  inline void delete_hier (DUNE_ELEM * he) ;
224 
225  // delete iterators
226  template <class IteratorType>
227  inline void delete_iterators(DUNE_ELEM * he) ;
228 public:
229 
230  // fake function for copy iterator
231  inline static void * copy_iterator (const void * i) ;
232 
233  // local to world
234  inline void local2world (DUNE_ELEM * he, const double * c, double * w);
235 
236  // world to local
237  inline int world2local (DUNE_ELEM * he, const double * w, double * c);
238 
239  // check inside reference element
240  inline int checkWhetherInside (DUNE_ELEM * he, const double * w);
241 
242  //*********************************
243  // wrapper functions
244  //*********************************
245  // local to world
246  inline static void ctow (DUNE_ELEM * he, const double * c, double * w);
247 
248  // world to local
249  inline static int wtoc (DUNE_ELEM * he, const double * w, double * c);
250 
251  // check inside reference element
252  inline static int check_inside (DUNE_ELEM * he, const double * w);
253 
254  // dito
255  template< class Entity >
256  int checkInside ( const Entity &entity, const double *w );
257 
258  // dito
259  template< class Entity >
260  int world_to_local ( const Entity &entity, const double *w, double *c );
261 
262  // dito
263  template <class EntityType>
264  inline void local_to_world(const EntityType &en, const double * c, double * w);
265 
266  template <PartitionIteratorType pitype>
267  inline void selectIterators(DUNE_DAT *, void *, setGridPartIterators_t *) const;
268 
269  inline void setIterationMethods(DUNE_DAT *, DUNE_FDATA * ) const;
270 
271  inline void changeIterationMethods(int iterType, int partType, DUNE_FDATA *);
272 
273  template <PartitionIteratorType pitype>
274  struct IterationMethods
275  {
276  // wrapper methods for first_child and next_child
277  inline static int first_mac (DUNE_ELEM * he)
278  {
279  MyDisplayType & disp = *((MyDisplayType *) he->display);
280  return disp.template first_level<pitype>(he,0);
281  }
282 
283  // wrapper methods for first_child and next_child
284  inline static int first_lev (DUNE_ELEM * he)
285  {
286  MyDisplayType & disp = *((MyDisplayType *) he->display);
287  return disp.template first_level<pitype>(he,he->level_of_interest);
288  }
289 
290  inline static int next_lev (DUNE_ELEM * he)
291  {
292  MyDisplayType & disp = *((MyDisplayType *) he->display);
293  return disp.template next_level<pitype>(he);
294  }
295 
296  // wrapper methods for first_child and next_child
297  inline static int fst_leaf (DUNE_ELEM * he)
298  {
299  MyDisplayType & disp = *((MyDisplayType *) he->display);
300  return disp.template first_leaf<pitype>(he);
301  }
302  inline static int nxt_leaf (DUNE_ELEM * he)
303  {
304  MyDisplayType & disp = *((MyDisplayType *) he->display);
305  return disp.template next_leaf<pitype>(he);
306  }
307 
308  // wrapper methods for first_child and next_child
309  inline static int fst_child (DUNE_ELEM * he)
310  {
311  MyDisplayType & disp = *((MyDisplayType *) he->display);
312  return disp.first_child(he);
313  }
314  inline static int nxt_child (DUNE_ELEM * he)
315  {
316  MyDisplayType & disp = *((MyDisplayType *) he->display);
317  return disp.next_child(he);
318  }
319 
320  // wrapper methods for deleting iterators
321  inline static void del_leaf (DUNE_ELEM * he)
322  {
323  MyDisplayType & disp = *((MyDisplayType *) he->display);
324  disp.template delete_leaf<pitype>(he);
325  }
326 
327  // wrapper methods for deleting iterators
328  inline static void del_level (DUNE_ELEM * he)
329  {
330  MyDisplayType & disp = *((MyDisplayType *) he->display);
331  disp.template delete_level<pitype>(he);
332  }
333 
334  // wrapper methods for deleting iterators
335  inline static void del_hier (DUNE_ELEM * he)
336  {
337  MyDisplayType & disp = *((MyDisplayType *) he->display);
338  disp.template delete_hier<pitype>(he);
339  }
340 
341  };
342 
343 protected:
344  template <class GridPartType>
345  struct IterationMethodsGP
346  {
347  // wrapper methods for first_item and next_item
348  inline static int fst_item (DUNE_ELEM * he)
349  {
350  assert( he->display );
351  MyDisplayType & disp = *((MyDisplayType *) he->display);
352  return disp.template first_item<GridPartType>(he);
353  }
354  inline static int nxt_item (DUNE_ELEM * he)
355  {
356  assert( he->display );
357  MyDisplayType & disp = *((MyDisplayType *) he->display);
358  return disp.template next_item<GridPartType>(he);
359  }
360 
361  // delete iterators
362  inline static void del_iter (DUNE_ELEM * he)
363  {
364  assert( he->display );
365  MyDisplayType & disp = *((MyDisplayType *) he->display);
366  typedef typename GridPartType :: template Codim<0> :: IteratorType IteratorType;
367  disp.template delete_iterators<IteratorType> (he);
368  }
369  };
370 
371  template <class GridPartImp>
372  struct SetIter
373  {
374  static void setGPIterator (DUNE_DAT * dune ,void * gridPart)
375  {
376  assert( gridPart );
377  dune->gridPart = gridPart;
378  dune->first_macro = &IterationMethodsGP<GridPartImp>::fst_item;
379  dune->next_macro = &IterationMethodsGP<GridPartImp>::nxt_item;
380  dune->delete_iter = &IterationMethodsGP<GridPartImp>::del_iter;
381 
382  dune->first_child = 0;
383  dune->next_child = 0;
384  }
385  };
386 
387  template< class ViewTraits >
388  struct GridViewIterators
389  {
390  typedef Dune::GridView< ViewTraits > GridView;
391  typedef typename GridView::template Codim< 0 >::Iterator Iterator;
392 
393  static int first_macro ( DUNE_ELEM *he )
394  {
395  assert( he->display != 0 );
396  MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
397 
398  if( he->liter != 0 )
399  display.template delete_iterators< Iterator >( he );
400 
401  assert( he->gridPart != 0 );
402  const GridView &gridView = *static_cast< const GridView * >( he->gridPart );
403 
404  assert( he->liter == 0 );
405  assert( he->enditer == 0 );
406 
407  Iterator *it = new Iterator( gridView.template begin< 0 >() );
408  Iterator *end = new Iterator( gridView.template end < 0 >() );
409 
410  he->liter = it;
411  he->enditer = end;
412 
413  if( *it == *end )
414  {
415  display.template delete_iterators< Iterator >( he );
416  return 0;
417  }
418 
419  return display.el_update( it, he, gridView );
420  }
421 
422  static int next_macro ( DUNE_ELEM *he )
423  {
424  assert( he->display != 0 );
425  MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
426 
427  assert( he->gridPart != 0 );
428  const GridView &gridView = *static_cast< const GridView * >( he->gridPart );
429 
430  Iterator *it = static_cast< Iterator * >( he->liter );
431  Iterator *end = static_cast< Iterator * >( he->enditer );
432  assert( (it != 0) && (end != 0) );
433 
434  ++(*it);
435  if( *it == *end )
436  {
437  display.template delete_iterators< Iterator >( he );
438  return 0;
439  }
440 
441  return display.el_update( it, he, gridView );
442  }
443 
444  static void delete_iter ( DUNE_ELEM *he )
445  {
446  assert( he->display );
447  MyDisplayType &display = *static_cast< MyDisplayType * >( he->display );
448  display.template delete_iterators< Iterator >( he );
449  }
450 
451  static void set ( DUNE_DAT *dune, void *gridView )
452  {
453  assert( gridView );
454  dune->gridPart = gridView;
455  dune->first_macro = &first_macro;
456  dune->next_macro = &next_macro;
457  dune->delete_iter = &delete_iter;
458 
459  dune->first_child = 0;
460  dune->next_child = 0;
461  }
462  };
463 
464  inline static void setIterationModus(DUNE_DAT * , DUNE_FDATA *);
465 
466 public:
467  // create STACKENTRY or get from stack
468  inline static void * getStackEntry(StackEntryType & stackEntry);
469 
470  // get StackEntry Wrapper
471  inline static void * getStackEn(DUNE_DAT * dune);
472 
473  // free StackEntry Wrapper
474  inline static void freeStackEn(DUNE_DAT * dune, void * entry);
475 
476  inline static void deleteStackEntry(StackEntryType &);
477 
478  // push STACKENTRY to stack
479  inline static void freeStackEntry(StackEntryType & stackEntry, void * entry);
480 
481 protected:
483  DUNE_ELEM hel_;
484  DUNE_DAT dune_;
485  setGridPartIterators_t * setGridPartIter_;
486 
487  // pointer to index method
488  const EntityIndexFuncType * entityIndex;
489  // pointer to vertex method
490  const VertexIndexFuncType * vertexIndex;
491 
492  StackEntryType stackEntry_;
493 #endif // #if HAVE_GRAPE
494 
495  // the grid we want to display
496  const GridType &grid_;
497 
498  // true if we can use LevelIntersectionIterator
500 
501  void * gridPart_;
502 
503  // leaf index set of the grid
504  void * indexSet_;
505 
506  // leaf index set of the grid
508 
509  // my process number
510  const int myRank_;
511 
512  // no better way than this canot export HMESH structure to here
513  // pointer to hmesh
514  void * hmesh_;
515 
517 }; // end class GrapeGridDisplay
518 
519 #if HAVE_GRAPE
520 /**************************************************************************/
521 // element types, see dune/grid/common/grid.hh
522 // and also geldesc.hh for GR_ElementTypes
523 enum GRAPE_ElementType
524  { g_vertex = GrapeInterface_three_three::gr_vertex
525  , g_line = GrapeInterface_three_three::gr_line
526  , g_triangle = GrapeInterface_three_three::gr_triangle
527  , g_quadrilateral = GrapeInterface_three_three::gr_quadrilateral
528  , g_tetrahedron = GrapeInterface_three_three::gr_tetrahedron
529  , g_pyramid = GrapeInterface_three_three::gr_pyramid
530  , g_prism = GrapeInterface_three_three::gr_prism
531  , g_hexahedron = GrapeInterface_three_three::gr_hexahedron
532  , g_iso_triangle = GrapeInterface_three_three::gr_iso_triangle
533  , g_iso_quadrilateral = GrapeInterface_three_three::gr_iso_quadrilateral
534  , g_unknown = GrapeInterface_three_three::gr_unknown
535  };
536 
538 static inline GRAPE_ElementType convertToGrapeType ( GeometryType duneType , int dim )
539 {
540  static std::vector< GRAPE_ElementType > convertedType;
541  if( convertedType.size() == 0 )
542  {
543  const size_t geomSize = GlobalGeometryTypeIndex :: size( 3 ) ;
544  convertedType.resize( geomSize, g_unknown );
545 
546  GeometryType type ;
548  // 2D
550  type.makeVertex();
551  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_vertex;
552  type.makeLine();
553  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_line;
554  type.makeTriangle();
555  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_triangle;
556  type.makeQuadrilateral();
557  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_quadrilateral;
558 
560  // 3D
562  type.makeTetrahedron();
563  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_tetrahedron;
564  type.makeHexahedron();
565  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_hexahedron;
566  type.makePyramid();
567  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_pyramid;
568  type.makePrism();
569  convertedType[ GlobalGeometryTypeIndex :: index( type ) ] = g_prism;
570  }
571 
572  assert( GlobalGeometryTypeIndex :: index( duneType ) < convertedType.size() );
573  assert( convertedType[ GlobalGeometryTypeIndex :: index( duneType ) ] != g_unknown );
574  return convertedType[ GlobalGeometryTypeIndex :: index( duneType ) ];
575 }
576 
577 // see geldesc.hh for definition of this mapping
578 // this is the same for all namespaces (two_two , and two_three, ...)
579 static const int * const * vxMap = GrapeInterface_three_three::dune2GrapeVertex;
580 static inline int mapDune2GrapeVertex( int geomType , int vx )
581 {
582  enum { usedTypes = GrapeInterface_three_three::numberOfUsedGrapeElementTypes };
583  assert( geomType >= 0 );
584  assert( geomType < usedTypes ); // at the moment only defined from 2 to 7
585  return vxMap[geomType][vx];
586 }
587 
588 // see geldesc.hh for definition of this mapping
589 // this is the same for all namespaces (two_two , and two_three, ...)
590 static const int * const * faceMap = GrapeInterface_three_three::dune2GrapeFace;
591 static inline int mapDune2GrapeFace( int geomType , int duneFace )
592 {
593  enum { usedTypes = GrapeInterface_three_three::numberOfUsedGrapeElementTypes };
594  assert( geomType >= 0 );
595  assert( geomType < usedTypes ); // at the moment only defined from 2 to 7
596  return faceMap[geomType][ duneFace ];
597 }
598 #endif
599 
600 } // end namespace Dune
601 
602 #include "grape/grapegriddisplay.cc"
603 
604 // undefs all defines
605 #include "grape/grape_undefs.hh"
606 #endif