dune-grid-glue  2.5.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/deprecated.hh>
14 #include <dune/common/exceptions.hh>
15 #include <dune/common/iteratorfacades.hh>
16 #include <dune/common/promotiontraits.hh>
17 #include <dune/common/shared_ptr.hh>
18 #include <dune/common/stdstreams.hh>
19 
22 
23 #include <dune/common/parallel/mpitraits.hh>
24 #include <dune/common/parallel/mpicollectivecommunication.hh>
25 #include <dune/common/parallel/indexset.hh>
26 #include <dune/common/parallel/plocalindex.hh>
27 #include <dune/common/parallel/remoteindices.hh>
28 #include <dune/common/parallel/communicator.hh>
29 #include <dune/common/parallel/interface.hh>
30 
31 namespace Dune {
32 namespace GridGlue {
33 
36  Domain = 0,
37  Target = 1
38 };
39 
40 // forward declarations
41 template<typename P0, typename P1>
42 class GridGlue;
43 
44 template<typename P0, typename P1>
46 
47 template<typename P0, typename P1, int inside, int outside>
49 
50 template<typename P0, typename P1, int inside, int outside>
52 
53 template<typename P0, typename P1>
55 
56 template<typename P0, typename P1, int P>
57 struct GridGlueView;
58 
59 template<typename P0, typename P1>
60 struct GridGlueView<P0,P1,0>
61 {
62  typedef P0 Patch;
64  typedef typename Patch::GridView::template Codim<0>::Entity GridElement;
65  static const P0& patch(const GridGlue<P0,P1>& g)
66  {
67  return *g.patch0_;
68  }
69 };
70 
71 template<typename P0, typename P1>
72 struct GridGlueView<P0,P1,1>
73 {
74  typedef P1 Patch;
76  typedef typename Patch::GridView::template Codim<0>::Entity GridElement;
77  static const P1& patch(const GridGlue<P0,P1>& g)
78  {
79  return *g.patch1_;
80  }
81 };
82 
92 template<typename P0, typename P1>
93 class GridGlue
94 {
95 private:
96 
97  /* F R I E N D S */
98 
99  friend class IntersectionData<P0,P1>;
100  friend class Intersection<P0,P1,0,1>;
101  friend class Intersection<P0,P1,1,0>;
102  friend class IntersectionIterator<P0,P1,0,1>;
103  friend class IntersectionIterator<P0,P1,1,0>;
104  friend class IntersectionIndexSet<P0,P1>;
105  friend struct GridGlueView<P0,P1,0>;
106  friend struct GridGlueView<P0,P1,1>;
107 
108  /* P R I V A T E T Y P E S */
109 
111  typedef ::Dune::GridGlue::GlobalId GlobalId;
112 
114  typedef Dune::ParallelLocalIndex <Dune::PartitionType> LocalIndex;
115 
117  typedef Dune::ParallelIndexSet <GlobalId, LocalIndex> PIndexSet;
118 
119 public:
120 
121  /* P U B L I C T Y P E S A N D C O N S T A N T S */
122 
124  typedef typename P0::GridView Grid0View;
125 
127  typedef typename Grid0View::Grid Grid0;
128 
130  typedef P0 Grid0Patch;
131 
133  enum {
135  grid0dim = Grid0Patch::dim,
136  domdim = Grid0Patch::dim,
138  grid0dimworld = Grid0Patch::dimworld,
139  domdimworld = Grid0Patch::dimworld
140  };
141 
143  typedef typename P1::GridView Grid1View;
144 
146  typedef typename Grid1View::Grid Grid1;
147 
149  typedef P1 Grid1Patch;
150 
152  typedef unsigned int IndexType;
153 
155  enum {
157  tardim = Grid1Patch::dim,
158  grid1dim = Grid1Patch::dim,
160  tardimworld = Grid1Patch::dimworld,
161  grid1dimworld = Grid1Patch::dimworld
162  };
163 
164 
166  enum {
169  dimworld = ((int)Grid0Patch::dimworld > (int)Grid1Patch::dimworld) ? (int)Grid0Patch::dimworld : (int)Grid1Patch::dimworld
170 
171  };
172 
175  typedef typename PromotionTraits<typename Grid0View::ctype,
176  typename Grid1View::ctype>::PromotedType ctype;
177 
179  typedef Dune::FieldVector<ctype, dimworld> Coords;
180 
182  typedef typename Grid0View::Traits::template Codim<0>::Entity Grid0Element;
183 
185  typedef typename Grid0View::Traits::template Codim<Grid0::dimension>::Entity Grid0Vertex;
186 
188  typedef typename Grid1View::Traits::template Codim<0>::Entity Grid1Element;
189 
191  typedef typename Grid1View::Traits::template Codim<Grid1::dimension>::Entity Grid1Vertex;
192 
194  typedef Dune::GridGlue::Merger<ctype,
195  Grid0::dimension - Grid0Patch::codim,
196  Grid1::dimension - Grid1Patch::codim,
197  dimworld> Merger;
198 
201 
204 
211 
212 private:
213 
214  /* M E M B E R V A R I A B L E S */
215 
217  const std::shared_ptr<const Grid0Patch> patch0_;
218 
220  const std::shared_ptr<const Grid1Patch> patch1_;
221 
223  const std::shared_ptr<Merger> merger_;
224 
226  IndexType index__sz;
227 
228 #if HAVE_MPI
229  MPI_Comm mpicomm_;
231 
233  PIndexSet patch0_is_;
234 
236  PIndexSet patch1_is_;
237 
239  Dune::RemoteIndices<PIndexSet> remoteIndices_;
240 #endif // HAVE_MPI
241 
244 
246  mutable std::vector<IntersectionData> intersections_;
247 
248 protected:
249 
266  void mergePatches(const std::vector<Dune::FieldVector<ctype,dimworld> >& patch0coords,
267  const std::vector<unsigned int>& patch0entities,
268  const std::vector<Dune::GeometryType>& patch0types,
269  const int patch0rank,
270  const std::vector<Dune::FieldVector<ctype,dimworld> >& patch1coords,
271  const std::vector<unsigned int>& patch1entities,
272  const std::vector<Dune::GeometryType>& patch1types,
273  const int patch1rank);
274 
275 
276  template<typename Extractor>
277  void extractGrid (const Extractor & extractor,
278  std::vector<Dune::FieldVector<ctype, dimworld> > & coords,
279  std::vector<unsigned int> & faces,
280  std::vector<Dune::GeometryType>& geometryTypes) const;
281 
282 public:
283 
284  /* 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 */
285 
296  DUNE_DEPRECATED_MSG("Please use a std::shared_ptr for patches and merger")
297  GridGlue(const Grid0Patch& gp0, const Grid1Patch& gp1, Merger* merger);
298 
309  GridGlue(const std::shared_ptr<const Grid0Patch> gp0, const std::shared_ptr<const Grid1Patch> gp1, const std::shared_ptr<Merger> merger);
310 
311  /* G E T T E R S */
312 
314  template<int P>
315  const typename GridGlueView<P0,P1,P>::Patch & patch() const
316  {
317  return GridGlueView<P0,P1,P>::patch(*this);
318  }
319 
324  template<int P>
326  {
327  return GridGlueView<P0,P1,P>::patch(*this).gridView();
328  }
329 
330 
331  /* F U N C T I O N A L I T Y */
332 
333  void build();
334 
335  /* 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 */
336 
343  template<int I>
345  {
346  return typename GridGlueView<P0,P1,I>::IntersectionIterator(this, 0);
347  }
348 
349 
356  template<int I>
358  {
359  return typename GridGlueView<P0,P1,I>::IntersectionIterator(this, index__sz);
360  }
361 
362 
374  template<class DataHandleImp, class DataTypeImp>
376  Dune::InterfaceType iftype, Dune::CommunicationDirection dir) const
377  {
379  typedef typename DataHandle::DataType DataType;
380 
381 #if HAVE_MPI
382 
383  if (mpicomm_ != MPI_COMM_SELF)
384  {
385  /*
386  * P A R A L L E L V E R S I O N
387  */
388  // setup communication interfaces
389  Dune::dinfo << "GridGlue: parallel communication" << std::endl;
390  typedef Dune::EnumItem <Dune::PartitionType, Dune::InteriorEntity> InteriorFlags;
391  typedef Dune::EnumItem <Dune::PartitionType, Dune::OverlapEntity> OverlapFlags;
392  typedef Dune::EnumRange <Dune::PartitionType, Dune::InteriorEntity, Dune::GhostEntity> AllFlags;
393  Dune::Interface interface;
394  assert(remoteIndices_.isSynced());
395  switch (iftype)
396  {
397  case Dune::InteriorBorder_InteriorBorder_Interface :
398  interface.build (remoteIndices_, InteriorFlags(), InteriorFlags() );
399  break;
400  case Dune::InteriorBorder_All_Interface :
401  if (dir == Dune::ForwardCommunication)
402  interface.build (remoteIndices_, InteriorFlags(), AllFlags() );
403  else
404  interface.build (remoteIndices_, AllFlags(), InteriorFlags() );
405  break;
406  case Dune::Overlap_OverlapFront_Interface :
407  interface.build (remoteIndices_, OverlapFlags(), OverlapFlags() );
408  break;
409  case Dune::Overlap_All_Interface :
410  if (dir == Dune::ForwardCommunication)
411  interface.build (remoteIndices_, OverlapFlags(), AllFlags() );
412  else
413  interface.build (remoteIndices_, AllFlags(), OverlapFlags() );
414  break;
415  case Dune::All_All_Interface :
416  interface.build (remoteIndices_, AllFlags(), AllFlags() );
417  break;
418  default :
419  DUNE_THROW(Dune::NotImplemented, "GridGlue::communicate for interface " << iftype << " not implemented");
420  }
421 
422  // setup communication info (class needed to tunnel all info to the operator)
424  CommInfo commInfo;
425  commInfo.dir = dir;
426  commInfo.gridglue = this;
427  commInfo.data = &data;
428 
429  // create communicator
430  Dune::BufferedCommunicator bComm ;
431  bComm.template build< CommInfo >(commInfo, commInfo, interface);
432 
433  // do communication
434  // choose communication direction.
435  if (dir == Dune::ForwardCommunication)
436  bComm.forward< Dune::GridGlue::ForwardOperator >(commInfo, commInfo);
437  else
438  bComm.backward< Dune::GridGlue::BackwardOperator >(commInfo, commInfo);
439  }
440  else
441 #endif // HAVE_MPI
442  {
443  /*
444  * S E Q U E N T I A L V E R S I O N
445  */
446  Dune::dinfo << "GridGlue: sequential fallback communication" << std::endl;
447 
448  // get comm buffer size
449  int ssz = size() * 10; // times data per intersection
450  int rsz = size() * 10;
451 
452  // allocate send/receive buffer
453  DataType* sendbuffer = new DataType[ssz];
454  DataType* receivebuffer = new DataType[rsz];
455 
456  // iterators
457  Grid0IntersectionIterator rit = ibegin<0>();
458  Grid0IntersectionIterator ritend = iend<0>();
459 
460  // gather
461  Dune::GridGlue::StreamingMessageBuffer<DataType> gatherbuffer(sendbuffer);
462  for (; rit != ritend; ++rit)
463  {
464  /*
465  we need to have to variants depending on the communication direction.
466  */
467  if (dir == Dune::ForwardCommunication)
468  {
469  /*
470  dir : Forward (grid0 -> grid1)
471  */
472  if (rit->self())
473  {
474  data.gather(gatherbuffer, rit->inside(), *rit);
475  }
476  }
477  else // (dir == Dune::BackwardCommunication)
478  {
479  /*
480  dir : Backward (grid1 -> grid0)
481  */
482  if (rit->neighbor())
483  {
484  data.gather(gatherbuffer, rit->outside(), rit->flip());
485  }
486  }
487  }
488 
489  assert(ssz == rsz);
490  for (int i=0; i<ssz; i++)
491  receivebuffer[i] = sendbuffer[i];
492 
493  // scatter
494  Dune::GridGlue::StreamingMessageBuffer<DataType> scatterbuffer(receivebuffer);
495  for (rit = ibegin<0>(); rit != ritend; ++rit)
496  {
497  /*
498  we need to have to variants depending on the communication direction.
499  */
500  if (dir == Dune::ForwardCommunication)
501  {
502  /*
503  dir : Forward (grid0 -> grid1)
504  */
505  if (rit->neighbor())
506  data.scatter(scatterbuffer, rit->outside(), rit->flip(),
507  data.size(*rit));
508  }
509  else // (dir == Dune::BackwardCommunication)
510  {
511  /*
512  dir : Backward (grid1 -> grid0)
513  */
514  if (rit->self())
515  data.scatter(scatterbuffer, rit->inside(), *rit,
516  data.size(*rit));
517  }
518  }
519 
520  // cleanup pointers
521  delete[] sendbuffer;
522  delete[] receivebuffer;
523  }
524  }
525 
526  /*
527  * @brief return an IndexSet mapping from Intersection to IndexType
528  */
529  IndexSet indexSet() const
530  {
531  return IndexSet(this);
532  }
533 
534 #if QUICKHACK_INDEX
535  // indexset size
536  size_t indexSet_size() const
537  {
538  return index__sz;
539  }
540 
541 #endif
542 
543  Intersection getIntersection(int i) const
544  {
545  return Intersection(this, & intersections_[i]);
546  }
547 
548  size_t size() const
549  {
550  return index__sz;
551  }
552 
553 };
554 
555 } // end namespace GridGlue
556 } // end namespace Dune
557 
558 #include "adapter/gridglue.cc"
559 
560 #include "adapter/intersection.hh"
563 
565 
566 #endif // DUNE_GRIDGLUE_GRIDGLUE_HH
unsigned int IndexType
Definition: gridglue.hh:152
Patch::GridView::template Codim< 0 >::Entity GridElement
Definition: gridglue.hh:76
const GridGlue * gridglue
Definition: gridgluecommunicate.hh:279
Dune::GridGlue::IntersectionIterator< P0, P1, 0, 1 > IntersectionIterator
Definition: gridglue.hh:63
Abstract base for all classes that take extracted grids and build sets of intersections.
Definition: merger.hh:16
Provides codimension-independent methods for grid extraction.
Definition: extractor.hh:43
forward gather scatter to user defined CommInfo class
Definition: gridgluecommunicate.hh:191
Dune::GridGlue::Intersection< P0, P1, 0, 1 > Intersection
Type of remote intersection objects.
Definition: gridglue.hh:200
Definition: gridgluecommunicate.hh:23
Definition: gridglue.hh:31
Grid1View::Grid Grid1
Grid 1 type.
Definition: gridglue.hh:146
IndexSet indexSet() const
Definition: gridglue.hh:529
storage class for Dune::GridGlue::Intersection related data
Definition: gridglue.hh:45
Grid0View::Traits::template Codim< Grid0::dimension >::Entity Grid0Vertex
The type of the Grid0 vertices.
Definition: gridglue.hh:185
void scatter(MessageBufferImp &buff, const EntityType &e, const RISType &i, size_t n)
Definition: gridgluecommunicate.hh:116
Implement iterators over GridGlue intersections.
P0::GridView Grid0View
GridView of grid 0 (aka domain grid)
Definition: gridglue.hh:124
Dune::GridGlue::IntersectionIterator< P0, P1, 1, 0 > IntersectionIterator
Definition: gridglue.hh:75
Intersection getIntersection(int i) const
Definition: gridglue.hh:543
Dune::FieldVector< ctype, dimworld > Coords
The type used for coordinate vectors.
Definition: gridglue.hh:179
Dune::GridGlue::IntersectionIndexSet< P0, P1 > IndexSet
Type of remote intersection indexSet.
Definition: gridglue.hh:203
Dune::GridGlue::IntersectionIterator< P0, P1, 1, 0 > Grid1IntersectionIterator
Definition: gridglue.hh:210
P1 Grid1Patch
Coupling patch of grid 1.
Definition: gridglue.hh:149
P0 Grid0Patch
Coupling patch of grid 0.
Definition: gridglue.hh:130
Definition: gridglue.hh:51
sequential adapter to couple two grids at specified close together boundaries
Definition: gridglue.hh:42
size_t size() const
Definition: gridglue.hh:548
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:375
Grid0View::Grid Grid0
Grid 0 type.
Definition: gridglue.hh:127
Dune::GridGlue::Merger< ctype, Grid0::dimension - Grid0Patch::codim, Grid1::dimension - Grid1Patch::codim, dimworld > Merger
Instance of a Merger.
Definition: gridglue.hh:197
Definition: gridglue.hh:36
Dune::GridGlue::IntersectionIterator< P0, P1, 0, 1 > Grid0IntersectionIterator
Type of the iterator that iterates over remove intersections.
Definition: gridglue.hh:208
const GridGlueView< P0, P1, P >::Patch::GridView & gridView() const
getter for the GridView of patch P
Definition: gridglue.hh:325
Definition: gridglue.hh:57
GridGlueView< P0, P1, I >::IntersectionIterator iend() const
gets the (general) end-iterator for grid glue iterations
Definition: gridglue.hh:357
P1 Patch
Definition: gridglue.hh:74
The intersection of two entities of the two patches of a GridGlue.
Definition: gridglue.hh:48
void gather(MessageBufferImp &buff, const EntityType &e, const RISType &i) const
pack data from user to message buffer
Definition: gridgluecommunicate.hh:102
Dune::CommunicationDirection dir
Definition: gridgluecommunicate.hh:286
STL namespace.
GridOrdering
Definition: gridglue.hh:35
Model of the Intersection concept provided by GridGlue.
Grid1View::Traits::template Codim< 0 >::Entity Grid1Element
The type of the Grid1 elements.
Definition: gridglue.hh:188
Grid1View::Traits::template Codim< Grid1::dimension >::Entity Grid1Vertex
The type of the Grid1 vertices.
Definition: gridglue.hh:191
Definition: gridglue.hh:37
size_t size(RISType &i) const
Definition: gridgluecommunicate.hh:90
::Dune::GridGlue::CommDataHandle< DataHandleImp, DataTypeImp > * data
Definition: gridgluecommunicate.hh:280
Describes the parallel communication interface class for Dune::GridGlue.
P1::GridView Grid1View
GridView of grid 1 (aka target grid)
Definition: gridglue.hh:143
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:344
collects all GridGlue data requried for communication
Definition: gridgluecommunicate.hh:269
PromotionTraits< typename Grid0View::ctype, typename Grid1View::ctype >::PromotedType ctype
The type used for coordinates.
Definition: gridglue.hh:176
static const P0 & patch(const GridGlue< P0, P1 > &g)
Definition: gridglue.hh:65
Patch::GridView::template Codim< 0 >::Entity GridElement
Definition: gridglue.hh:64
P0 Patch
Definition: gridglue.hh:62
Grid0View::Traits::template Codim< 0 >::Entity Grid0Element
The type of the Grid0 elements.
Definition: gridglue.hh:182
static const P1 & patch(const GridGlue< P0, P1 > &g)
Definition: gridglue.hh:77
describes the features of a data handle for communication in parallel runs using the GridGlue::commun...
Definition: gridgluecommunicate.hh:74
Definition: gridglue.hh:54