dune-grid-glue  2.4.0
gridglue.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:
10 #ifndef DUNE_GRIDGLUE_GRIDGLUE_HH
11 #define DUNE_GRIDGLUE_GRIDGLUE_HH
12 
13 #include <dune/common/exceptions.hh>
14 #include <dune/common/iteratorfacades.hh>
15 #include <dune/common/shared_ptr.hh>
16 
19 
20 #if DUNE_VERSION_NEWER_REV(DUNE_COMMON,2,3,0)
21 #include <dune/common/parallel/mpitraits.hh>
22 #include <dune/common/parallel/mpicollectivecommunication.hh>
23 #else
24 #include <dune/common/mpitraits.hh>
25 #include <dune/common/mpicollectivecommunication.hh>
26 #endif
27 #include <dune/common/parallel/indexset.hh>
28 #include <dune/common/parallel/plocalindex.hh>
29 #include <dune/common/parallel/remoteindices.hh>
30 #include <dune/common/parallel/communicator.hh>
31 #include <dune/common/parallel/interface.hh>
32 
33 namespace Dune {
34 namespace GridGlue {
35 
38  Domain = 0,
39  Target = 1
40 };
41 
42 // forward declarations
43 template<typename P0, typename P1>
44 class GridGlue;
45 
46 template<typename P0, typename P1>
48 
49 template<typename P0, typename P1, int inside, int outside>
51 
52 template<typename P0, typename P1, int inside, int outside>
54 
55 template<typename P0, typename P1>
57 
58 template<typename P0, typename P1, int P>
59 struct GridGlueView;
60 
61 template<typename P0, typename P1>
62 struct GridGlueView<P0,P1,0>
63 {
64  typedef P0 Patch;
66  typedef typename Patch::GridView::template Codim<0>::Entity GridElement;
67  static const P0& patch(const GridGlue<P0,P1>& g)
68  {
69  return *g.patch0_;
70  }
71 };
72 
73 template<typename P0, typename P1>
74 struct GridGlueView<P0,P1,1>
75 {
76  typedef P1 Patch;
78  typedef typename Patch::GridView::template Codim<0>::Entity GridElement;
79  static const P1& patch(const GridGlue<P0,P1>& g)
80  {
81  return *g.patch1_;
82  }
83 };
84 
94 template<typename P0, typename P1>
95 class GridGlue
96 {
97 private:
98 
99  /* F R I E N D S */
100 
101  friend class IntersectionData<P0,P1>;
102  friend class Intersection<P0,P1,0,1>;
103  friend class Intersection<P0,P1,1,0>;
104  friend class IntersectionIterator<P0,P1,0,1>;
105  friend class IntersectionIterator<P0,P1,1,0>;
106  friend class IntersectionIndexSet<P0,P1>;
107  friend struct GridGlueView<P0,P1,0>;
108  friend struct GridGlueView<P0,P1,1>;
109 
110  /* P R I V A T E T Y P E S */
111 
113  typedef ::Dune::GridGlue::GlobalId GlobalId;
114 
116  typedef Dune::ParallelLocalIndex <Dune::PartitionType> LocalIndex;
117 
119  typedef Dune::ParallelIndexSet <GlobalId, LocalIndex> PIndexSet;
120 
121 public:
122 
123  /* P U B L I C T Y P E S A N D C O N S T A N T S */
124 
126  typedef typename P0::GridView Grid0View;
127 
129  typedef typename Grid0View::Grid Grid0;
130 
134  typedef Grid0 DomainGridType DUNE_DEPRECATED;
135 
137  typedef P0 Grid0Patch;
138 
140  enum {
142  grid0dim = Grid0Patch::dim,
143  domdim = Grid0Patch::dim,
145  grid0dimworld = Grid0Patch::dimworld,
146  domdimworld = Grid0Patch::dimworld
147  };
148 
150  typedef typename P1::GridView Grid1View;
151 
153  typedef typename Grid1View::Grid Grid1;
154 
158  typedef Grid1 TargetGridType DUNE_DEPRECATED;
159 
161  typedef P1 Grid1Patch;
162 
164  typedef unsigned int IndexType;
165 
167  enum {
169  tardim = Grid1Patch::dim,
170  grid1dim = Grid1Patch::dim,
172  tardimworld = Grid1Patch::dimworld,
173  grid1dimworld = Grid1Patch::dimworld
174  };
175 
176 
178  enum {
181  dimworld = ((int)Grid0Patch::dimworld > (int)Grid1Patch::dimworld) ? (int)Grid0Patch::dimworld : (int)Grid1Patch::dimworld
182 
183  };
184 
188  typedef typename Grid0View::ctype ctype;
189 
191  typedef Dune::FieldVector<ctype, dimworld> Coords;
192 
194  typedef typename Grid0View::Traits::template Codim<0>::Entity Grid0Element;
195 
199  typedef typename Grid0View::Traits::template Codim<0>::Entity DomainElement DUNE_DEPRECATED;
200 
202  typedef typename Grid0View::Traits::template Codim<0>::EntityPointer Grid0ElementPtr;
203 
207  typedef typename Grid0View::Traits::template Codim<0>::EntityPointer DomainElementPtr DUNE_DEPRECATED;
208 
210  typedef typename Grid0View::Traits::template Codim<Grid0::dimension>::Entity Grid0Vertex;
211 
215  typedef typename Grid0View::Traits::template Codim<Grid0::dimension>::Entity DomainVertex DUNE_DEPRECATED;
216 
218  typedef typename Grid0View::Traits::template Codim<Grid0::dimension>::EntityPointer Grid0VertexPtr;
219 
223  typedef typename Grid0View::Traits::template Codim<Grid0::dimension>::EntityPointer DomainVertexPtr DUNE_DEPRECATED;
224 
226  typedef typename Grid1View::Traits::template Codim<0>::Entity Grid1Element;
227 
231  typedef typename Grid1View::Traits::template Codim<0>::Entity TargetElement DUNE_DEPRECATED;
232 
234  typedef typename Grid1View::Traits::template Codim<0>::EntityPointer Grid1ElementPtr;
235 
239  typedef typename Grid1View::Traits::template Codim<0>::EntityPointer TargetElementPtr DUNE_DEPRECATED;
240 
242  typedef typename Grid1View::Traits::template Codim<Grid1::dimension>::Entity Grid1Vertex;
243 
247  typedef typename Grid1View::Traits::template Codim<Grid1::dimension>::Entity TargetVertex DUNE_DEPRECATED;
248 
250  typedef typename Grid1View::Traits::template Codim<Grid1::dimension>::EntityPointer Grid1VertexPtr;
251 
255  typedef typename Grid1View::Traits::template Codim<Grid1::dimension>::EntityPointer TargetVertexPtr DUNE_DEPRECATED;
256 
258  typedef Dune::GridGlue::Merger<ctype,
259  Grid0::dimension - Grid0Patch::codim,
260  Grid1::dimension - Grid1Patch::codim,
261  dimworld> Merger;
262 
265 
268 
275 
276 private:
277 
278  /* M E M B E R V A R I A B L E S */
279 
281  const std::shared_ptr<const Grid0Patch> patch0_;
282 
284  const std::shared_ptr<const Grid1Patch> patch1_;
285 
287  const std::shared_ptr<Merger> merger_;
288 
290  IndexType index__sz;
291 
292 #if HAVE_MPI
293  MPI_Comm mpicomm_;
295 
297  PIndexSet patch0_is_;
298 
300  PIndexSet patch1_is_;
301 
303  Dune::RemoteIndices<PIndexSet> remoteIndices_;
304 #endif // HAVE_MPI
305 
308 
310  mutable std::vector<IntersectionData> intersections_;
311 
312 protected:
313 
330  void mergePatches(const std::vector<Dune::FieldVector<ctype,dimworld> >& patch0coords,
331  const std::vector<unsigned int>& patch0entities,
332  const std::vector<Dune::GeometryType>& patch0types,
333  const int patch0rank,
334  const std::vector<Dune::FieldVector<ctype,dimworld> >& patch1coords,
335  const std::vector<unsigned int>& patch1entities,
336  const std::vector<Dune::GeometryType>& patch1types,
337  const int patch1rank);
338 
339 
340  template<typename Extractor>
341  void extractGrid (const Extractor & extractor,
342  std::vector<Dune::FieldVector<ctype, dimworld> > & coords,
343  std::vector<unsigned int> & faces,
344  std::vector<Dune::GeometryType>& geometryTypes) const;
345 
346 public:
347 
348  /* C O N S T R U C T O R S A N D D E S T R U C T O R S */
349 
360  GridGlue(const Grid0Patch& gp0, const Grid1Patch& gp1, Merger* merger);
361  GridGlue(const std::shared_ptr<const Grid0Patch> gp0, const std::shared_ptr<const Grid1Patch> gp1, const std::shared_ptr<Merger> merger);
362 
363  /* G E T T E R S */
364 
366  template<int P>
367  const typename GridGlueView<P0,P1,P>::Patch & patch() const
368  {
369  return GridGlueView<P0,P1,P>::patch(*this);
370  }
371 
376  template<int P>
378  {
379  return GridGlueView<P0,P1,P>::patch(*this).gridView();
380  }
381 
382 
383  /* F U N C T I O N A L I T Y */
384 
385  void build();
386 
387  /* I N T E R S E C T I O N S A N D I N T E R S E C T I O N I T E R A T O R S */
388 
395  template<int I>
397  {
398  return typename GridGlueView<P0,P1,I>::IntersectionIterator(this, 0);
399  }
400 
401 
408  template<int I>
410  {
411  return typename GridGlueView<P0,P1,I>::IntersectionIterator(this, index__sz);
412  }
413 
414 
426  template<class DataHandleImp, class DataTypeImp>
428  Dune::InterfaceType iftype, Dune::CommunicationDirection dir) const
429  {
431  typedef typename DataHandle::DataType DataType;
432 
433 #if HAVE_MPI
434 
435  if (mpicomm_ != MPI_COMM_SELF)
436  {
437  /*
438  * P A R A L L E L V E R S I O N
439  */
440  // setup communication interfaces
441  Dune::dinfo << "GridGlue: parallel communication" << std::endl;
442  typedef Dune::EnumItem <Dune::PartitionType, Dune::InteriorEntity> InteriorFlags;
443  typedef Dune::EnumItem <Dune::PartitionType, Dune::OverlapEntity> OverlapFlags;
444  typedef Dune::EnumRange <Dune::PartitionType, Dune::InteriorEntity, Dune::GhostEntity> AllFlags;
445  Dune::Interface interface;
446  assert(remoteIndices_.isSynced());
447  switch (iftype)
448  {
449  case Dune::InteriorBorder_InteriorBorder_Interface :
450  interface.build (remoteIndices_, InteriorFlags(), InteriorFlags() );
451  break;
452  case Dune::InteriorBorder_All_Interface :
453  if (dir == Dune::ForwardCommunication)
454  interface.build (remoteIndices_, InteriorFlags(), AllFlags() );
455  else
456  interface.build (remoteIndices_, AllFlags(), InteriorFlags() );
457  break;
458  case Dune::Overlap_OverlapFront_Interface :
459  interface.build (remoteIndices_, OverlapFlags(), OverlapFlags() );
460  break;
461  case Dune::Overlap_All_Interface :
462  if (dir == Dune::ForwardCommunication)
463  interface.build (remoteIndices_, OverlapFlags(), AllFlags() );
464  else
465  interface.build (remoteIndices_, AllFlags(), OverlapFlags() );
466  break;
467  case Dune::All_All_Interface :
468  interface.build (remoteIndices_, AllFlags(), AllFlags() );
469  break;
470  default :
471  DUNE_THROW(Dune::NotImplemented, "GridGlue::communicate for interface " << iftype << " not implemented");
472  }
473 
474  // setup communication info (class needed to tunnel all info to the operator)
476  CommInfo commInfo;
477  commInfo.dir = dir;
478  commInfo.gridglue = this;
479  commInfo.data = &data;
480 
481  // create communicator
482  Dune::BufferedCommunicator bComm ;
483  bComm.template build< CommInfo >(commInfo, commInfo, interface);
484 
485  // do communication
486  // choose communication direction.
487  if (dir == Dune::ForwardCommunication)
488  bComm.forward< Dune::GridGlue::ForwardOperator >(commInfo, commInfo);
489  else
490  bComm.backward< Dune::GridGlue::BackwardOperator >(commInfo, commInfo);
491  }
492  else
493 #endif // HAVE_MPI
494  {
495  /*
496  * S E Q U E N T I A L V E R S I O N
497  */
498  Dune::dinfo << "GridGlue: sequential fallback communication" << std::endl;
499 
500  // get comm buffer size
501  int ssz = size() * 10; // times data per intersection
502  int rsz = size() * 10;
503 
504  // allocate send/receive buffer
505  DataType* sendbuffer = new DataType[ssz];
506  DataType* receivebuffer = new DataType[rsz];
507 
508  // iterators
509  Grid0IntersectionIterator rit = ibegin<0>();
510  Grid0IntersectionIterator ritend = iend<0>();
511 
512  // gather
513  Dune::GridGlue::StreamingMessageBuffer<DataType> gatherbuffer(sendbuffer);
514  for (; rit != ritend; ++rit)
515  {
516  /*
517  we need to have to variants depending on the communication direction.
518  */
519  if (dir == Dune::ForwardCommunication)
520  {
521  /*
522  dir : Forward (grid0 -> grid1)
523  */
524  if (rit->self())
525  {
526  data.gather(gatherbuffer, rit->inside(), *rit);
527  }
528  }
529  else // (dir == Dune::BackwardCommunication)
530  {
531  /*
532  dir : Backward (grid1 -> grid0)
533  */
534  if (rit->neighbor())
535  {
536  data.gather(gatherbuffer, rit->outside(), rit->flip());
537  }
538  }
539  }
540 
541  assert(ssz == rsz);
542  for (int i=0; i<ssz; i++)
543  receivebuffer[i] = sendbuffer[i];
544 
545  // scatter
546  Dune::GridGlue::StreamingMessageBuffer<DataType> scatterbuffer(receivebuffer);
547  for (rit = ibegin<0>(); rit != ritend; ++rit)
548  {
549  /*
550  we need to have to variants depending on the communication direction.
551  */
552  if (dir == Dune::ForwardCommunication)
553  {
554  /*
555  dir : Forward (grid0 -> grid1)
556  */
557  if (rit->neighbor())
558  data.scatter(scatterbuffer, rit->outside(), rit->flip(),
559  data.size(*rit));
560  }
561  else // (dir == Dune::BackwardCommunication)
562  {
563  /*
564  dir : Backward (grid1 -> grid0)
565  */
566  if (rit->self())
567  data.scatter(scatterbuffer, rit->inside(), *rit,
568  data.size(*rit));
569  }
570  }
571 
572  // cleanup pointers
573  delete[] sendbuffer;
574  delete[] receivebuffer;
575  }
576  }
577 
578  /*
579  * @brief return an IndexSet mapping from Intersection to IndexType
580  */
581  IndexSet indexSet() const
582  {
583  return IndexSet(this);
584  }
585 
586 #if QUICKHACK_INDEX
587  // indexset size
588  size_t indexSet_size() const
589  {
590  return index__sz;
591  }
592 
593 #endif
594 
595  Intersection getIntersection(int i) const
596  {
597  return Intersection(this, & intersections_[i]);
598  }
599 
600  size_t size() const
601  {
602  return index__sz;
603  }
604 
605 };
606 
607 } // end namespace GridGlue
608 } // end namespace Dune
609 
610 #include "adapter/gridglue.cc"
611 
612 #include "adapter/intersection.hh"
615 
617 
618 #endif // DUNE_GRIDGLUE_GRIDGLUE_HH
::Dune::GridGlue::CommDataHandle< DataHandleImp, DataTypeImp > * data
Definition: gridgluecommunicate.hh:279
GridOrdering
Definition: gridglue.hh:37
Grid0View::Traits::template Codim< 0 >::Entity Grid0Element
The type of the Grid0 elements.
Definition: gridglue.hh:194
Dune::GridGlue::IntersectionIterator< P0, P1, 1, 0 > Grid1IntersectionIterator
Definition: gridglue.hh:274
P0::GridView Grid0View
GridView of grid 0 (aka domain grid)
Definition: gridglue.hh:126
Dune::GridGlue::IntersectionIterator< P0, P1, 1, 0 > IntersectionIterator
Definition: gridglue.hh:77
Dune::GridGlue::IntersectionIndexSet< P0, P1 > IndexSet
Type of remote intersection indexSet.
Definition: gridglue.hh:267
GridGlueView< P0, P1, I >::IntersectionIterator ibegin() const
gets an iterator over all remote intersections in the merged grid between grid0 and grid1 ...
Definition: gridglue.hh:396
const GridGlueView< P0, P1, P >::Patch & patch() const
Definition: gridglue.hh:367
void communicate(Dune::GridGlue::CommDataHandle< DataHandleImp, DataTypeImp > &data, Dune::InterfaceType iftype, Dune::CommunicationDirection dir) const
Communicate information on the MergedGrid of a GridGlue.
Definition: gridglue.hh:427
Grid1View::Traits::template Codim< Grid1::dimension >::EntityPointer Grid1VertexPtr
Pointer type to Grid1 vertices.
Definition: gridglue.hh:250
Definition: gridglue.hh:33
Dune::FieldVector< ctype, dimworld > Coords
The type used for coordinate vectors.
Definition: gridglue.hh:191
Intersection getIntersection(int i) const
Definition: gridglue.hh:595
Definition: gridgluecommunicate.hh:138
Grid0View::Grid Grid0
Grid 0 type.
Definition: gridglue.hh:129
Grid1View::Traits::template Codim< 0 >::Entity Grid1Element
The type of the Grid1 elements.
Definition: gridglue.hh:226
Definition: gridglue.hh:59
void scatter(MessageBufferImp &buff, const EntityType &e, const RISType &i, size_t n)
Definition: gridgluecommunicate.hh:115
const GridGlue * gridglue
Definition: gridgluecommunicate.hh:278
P0 Grid0Patch
Coupling patch of grid 0.
Definition: gridglue.hh:137
Grid0View::Traits::template Codim< 0 >::EntityPointer Grid0ElementPtr
Pointer type to Grid0 elements.
Definition: gridglue.hh:202
P1::GridView Grid1View
GridView of grid 1 (aka target grid)
Definition: gridglue.hh:150
void gather(MessageBufferImp &buff, const EntityType &e, const RISType &i) const
pack data from user to message buffer
Definition: gridgluecommunicate.hh:101
Model of the Intersection concept provided by GridGlue.
Describes the parallel communication interface class for Dune::GridGlue.
Definition: gridgluecommunicate.hh:22
const GridGlueView< P0, P1, P >::Patch::GridView & gridView() const
getter for the GridView of patch P
Definition: gridglue.hh:377
collects all GridGlue data requried for communication
Definition: gridgluecommunicate.hh:268
Grid0View::Traits::template Codim< Grid0::dimension >::EntityPointer DomainVertexPtr
Pointer type to Grid0 vertices.
Definition: gridglue.hh:223
static const P1 & patch(const GridGlue< P0, P1 > &g)
Definition: gridglue.hh:79
Grid0View::Traits::template Codim< Grid0::dimension >::Entity DomainVertex
The type of the Grid0 vertices.
Definition: gridglue.hh:215
Dune::GridGlue::Merger< ctype, Grid0::dimension-Grid0Patch::codim, Grid1::dimension-Grid1Patch::codim, dimworld > Merger
Instance of a Merger.
Definition: gridglue.hh:261
Abstract base for all classes that take extracted grids and build sets of intersections.
Definition: merger.hh:16
GridGlueView< P0, P1, I >::IntersectionIterator iend() const
gets the (general) end-iterator for grid glue iterations
Definition: gridglue.hh:409
Dune::GridGlue::IntersectionIterator< P0, P1, 0, 1 > IntersectionIterator
Definition: gridglue.hh:65
Dune::GridGlue::Intersection< P0, P1, 0, 1 > Intersection
Type of remote intersection objects.
Definition: gridglue.hh:264
The intersection of two entities of the two patches of a GridGlue.
Definition: gridglue.hh:50
Grid1View::Traits::template Codim< 0 >::EntityPointer TargetElementPtr
Pointer type to Grid1 elements.
Definition: gridglue.hh:239
Definition: gridglue.hh:38
Definition: gridglue.hh:39
Grid0View::ctype ctype
The type used for coordinates.
Definition: gridglue.hh:188
Patch::GridView::template Codim< 0 >::Entity GridElement
Definition: gridglue.hh:66
Grid1View::Traits::template Codim< Grid1::dimension >::EntityPointer TargetVertexPtr
Pointer type to Grid1 vertices.
Definition: gridglue.hh:255
Grid1 TargetGridType
Grid 1 type.
Definition: gridglue.hh:158
IndexSet indexSet() const
Definition: gridglue.hh:581
Grid0View::Traits::template Codim< 0 >::EntityPointer DomainElementPtr
Pointer type to Grid0 elements.
Definition: gridglue.hh:207
Patch::GridView::template Codim< 0 >::Entity GridElement
Definition: gridglue.hh:78
describes the features of a data handle for communication in parallel runs using the GridGlue::commun...
Definition: gridgluecommunicate.hh:73
Definition: gridglue.hh:56
size_t size(RISType &i) const
Definition: gridgluecommunicate.hh:89
Dune::CommunicationDirection dir
Definition: gridgluecommunicate.hh:285
Grid0View::Traits::template Codim< Grid0::dimension >::Entity Grid0Vertex
The type of the Grid0 vertices.
Definition: gridglue.hh:210
Grid1View::Grid Grid1
Grid 1 type.
Definition: gridglue.hh:153
Grid0View::Traits::template Codim< Grid0::dimension >::EntityPointer Grid0VertexPtr
Pointer type to Grid0 vertices.
Definition: gridglue.hh:218
P0 Patch
Definition: gridglue.hh:64
Grid1View::Traits::template Codim< Grid1::dimension >::Entity TargetVertex
The type of the Grid1 vertices.
Definition: gridglue.hh:247
Grid1View::Traits::template Codim< Grid1::dimension >::Entity Grid1Vertex
The type of the Grid1 vertices.
Definition: gridglue.hh:242
Implement iterators over GridGlue intersections.
Dune::GridGlue::IntersectionIterator< P0, P1, 0, 1 > Grid0IntersectionIterator
Type of the iterator that iterates over remove intersections.
Definition: gridglue.hh:272
Definition: gridglue.hh:53
forward gather scatter to user defined CommInfo class
Definition: gridgluecommunicate.hh:190
P1 Patch
Definition: gridglue.hh:76
Grid0View::Traits::template Codim< 0 >::Entity DomainElement
The type of the Grid0 elements.
Definition: gridglue.hh:199
static const P0 & patch(const GridGlue< P0, P1 > &g)
Definition: gridglue.hh:67
Grid0 DomainGridType
Grid 0 type.
Definition: gridglue.hh:134
Grid1View::Traits::template Codim< 0 >::Entity TargetElement
The type of the Grid1 elements.
Definition: gridglue.hh:231
storage class for Dune::GridGlue::Intersection related data
Definition: gridglue.hh:47
Grid1View::Traits::template Codim< 0 >::EntityPointer Grid1ElementPtr
Pointer type to Grid1 elements.
Definition: gridglue.hh:234
size_t size() const
Definition: gridglue.hh:600
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:47
unsigned int IndexType
Definition: gridglue.hh:164
sequential adapter to couple two grids at specified close together boundaries
Definition: gridglue.hh:44
P1 Grid1Patch
Coupling patch of grid 1.
Definition: gridglue.hh:161