1 #ifndef DUNE_MULTISPGRID_GRIDVIEW_HH
2 #define DUNE_MULTISPGRID_GRIDVIEW_HH
3 
4 #include <cassert>
5 #include <vector>
6 
7 #include <dune/common/typetraits.hh>
8 
9 #include <dune/grid/common/gridview.hh>
10 
11 namespace Dune
12 {
13 
14   // Forward declarations
15   // --------------------
16 
17   template< PartitionIteratorType, class > class MultiSPLeafGridView;
18   template< PartitionIteratorType, class > class MultiSPLevelGridView;
19 
20 
21 
22   // MultiSPGridView
23   // ---------------
24 
25   /** \brief Base class for grid views
26    *
27    *  \ingroup GridView
28    */
29   template< class ViewTraits >
30   class MultiSPGridView
31   {
32     // this type
33     typedef MultiSPGridView< ViewTraits > This;
34 
35   public:
36     //! \brief grid type
37     typedef typename ViewTraits::Grid Grid;
38     //! \brief collective communication type
39     typedef typename ViewTraits::CollectiveCommunication CollectiveCommunication;
40 
41     //! \brief constructor
MultiSPGridView(const Grid & grid)42     explicit MultiSPGridView ( const Grid &grid )
43     : grid_( &grid )
44     {}
45 
46     //! \brief return const reference to grid
grid() const47     const Grid &grid () const
48     {
49       assert( grid_ );
50       return *grid_;
51     }
52 
53     //! \brief obtain collective communication object
comm() const54     const CollectiveCommunication &comm () const
55     {
56       return grid().comm();
57     }
58 
59   private:
60     const Grid *grid_;
61   };
62 
63 
64 
65   // MultiSPLeafGridViewTraits
66   // -------------------------
67 
68   template< PartitionIteratorType pitype, class GridImp >
69   class MultiSPLeafGridViewTraits
70   {
71     // grid traits
72     typedef typename std::remove_const< GridImp >::type::Traits Traits;
73 
74   public:
75     //! \brief grid view type
76     typedef MultiSPLeafGridView< pitype, GridImp > GridViewImp;
77 
78     //! \brief grid type
79     typedef typename std::remove_const< GridImp >::type Grid;
80     //! \brief host grid type
81     typedef typename Traits::HostGrid HostGrid;
82 
83     //! \brief index set
84     typedef typename Traits::LeafIndexSet IndexSet;
85     //! \brief intersection
86     typedef typename Traits::LeafIntersection Intersection;
87     //! \brief intersection iterator type
88     typedef typename Traits::LeafIntersectionIterator IntersectionIterator;
89 
90     //! \brief collective communication type
91     typedef typename Traits::CollectiveCommunication CollectiveCommunication;
92 
93     template< int codim >
94     struct Codim
95     {
96       //! \brief iterator type
97       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LeafIterator Iterator;
98 
99       //! \brief entity type
100       typedef typename Traits::template Codim< codim >::Entity Entity;
101       //! \brief entity pointer type
102       typedef typename Traits::template Codim< codim >::EntityPointer EntityPointer;
103 
104       //! \brief geometry type
105       typedef typename Traits::template Codim< codim >::Geometry Geometry;
106       //! \brief local geometry type
107       typedef typename Traits::template Codim< codim >::LocalGeometry LocalGeometry;
108 
109       template <PartitionIteratorType pit >
110       struct Partition
111       {
112         //! \brief iterator type
113         typedef typename Traits::template Codim< codim >::template Partition< pit >::LeafIterator Iterator;
114       };
115     };
116 
117     static const bool conforming = false;
118 
119     template< PartitionIteratorType pit >
120     struct Partition
121     {
122       //! \brief host grid view type
123       typedef typename HostGrid::template Partition< pit >::LeafGridView HostGridView;
124       //! \brief leaf connectivity
125       typedef typename Traits::template Partition< pit >::LeafConnectivity Connectivity;
126     };
127     //! \brief host grid view type
128     typedef typename Partition< pitype >::HostGridView HostGridView;
129     //! \brief leaf connectivity
130     typedef typename Partition< pitype >::Connectivity Connectivity;
131   };
132 
133 
134 
135 
136   // MultiSPLeafGridView
137   // -------------------
138 
139   /** \brief Leaf grid views
140    *
141    *  \ingroup GridView
142    */
143   template< PartitionIteratorType pitype, class Grid >
144   class MultiSPLeafGridView
145   : public MultiSPGridView< MultiSPLeafGridViewTraits< pitype, Grid > >
146   {
147     // this type
148     typedef MultiSPLeafGridView< pitype, Grid > This;
149     // vies traits
150     typedef MultiSPLeafGridViewTraits< pitype, Grid > Traits;
151     // base type
152     typedef MultiSPGridView< Traits > Base;
153 
154   public:
155     //! \brief host grid type
156     typedef typename Traits::HostGrid HostGrid;
157 
158     //! \brief index set
159     typedef typename Traits::IndexSet IndexSet;
160     //! \brief intersection
161     typedef typename Traits::Intersection Intersection;
162     //! \brief intersection iterator type
163     typedef typename Traits::IntersectionIterator IntersectionIterator;
164 
165     template< int codim >
166     struct Codim
167     : public Traits::template Codim< codim >
168     {};
169 
170     template< PartitionIteratorType pit >
171     struct Partition
172     : public Traits::template Partition< pit >
173     {};
174 
175     //! \brief host grid view
176     typedef typename Traits::HostGridView HostGridView;
177     //! \brief connectivity
178     typedef typename Traits::Connectivity Connectivity;
179 
180   public:
181     using Base::grid;
182 
183     //! \brief constructor
MultiSPLeafGridView(const Grid & grid)184     explicit MultiSPLeafGridView ( const Grid &grid )
185     : Base( grid )
186     {}
187 
188     //! \brief return index set
indexSet() const189     const IndexSet &indexSet () const
190     {
191       return grid().leafIndexSet();
192     }
193 
194     //! \brief return number of entities for given codimension in grid view
size(const int codim) const195     int size ( const int codim ) const
196     {
197       return grid().size( codim );
198     }
199 
200     //! \brief return number of entities for given geometry type in grid view
size(const GeometryType & type) const201     int size ( const GeometryType &type ) const
202     {
203       return grid().size( type );
204     }
205 
206     //! \brief return begin iterator for this view
207     template< int codim >
begin() const208     typename Codim< codim >::Iterator begin () const
209     {
210       return begin< codim, pitype >();
211     }
212 
213     //! \brief return begin iterator for this view
214     template< int codim, PartitionIteratorType pit >
begin() const215     typename Codim< codim >::template Partition< pit >::Iterator begin () const
216     {
217       return grid().template leafbegin< codim, pit >();
218     }
219 
220     //! \brief return end iterator for this view
221     template< int codim >
end() const222     typename Codim< codim >::Iterator end () const
223     {
224       return end< codim, pitype >();
225     }
226 
227     //! \brief return end iterator for this view
228     template< int codim, PartitionIteratorType pit >
end() const229     typename Codim< codim >::template Partition< pit >::Iterator end () const
230     {
231       return grid().template leafend< codim, pit >();
232     }
233 
234     //! \brief obtain begin intersection iterator with respect to this view
ibegin(const typename Traits::template Codim<0>::Entity & entity) const235     IntersectionIterator ibegin ( const typename Traits::template Codim< 0 >::Entity &entity ) const
236     {
237       if( !entity.isLeaf() )
238         return iend( entity );
239       typedef typename IntersectionIterator::Implementation IteratorImp;
240       return IteratorImp::begin( entity.impl() );
241     }
242 
243     //! \brief obtain end intersection iterator with respect to this view
iend(const typename Traits::template Codim<0>::Entity & entity) const244     IntersectionIterator iend ( const typename Traits::template Codim< 0 >::Entity &entity ) const
245     {
246       typedef typename IntersectionIterator::Implementation IteratorImp;
247       return IteratorImp::end( entity.impl() );
248     }
249 
250     //! \brief return size of the overlap region for a given codim on the grid view
overlapSize(int codim) const251     int overlapSize ( int codim ) const
252     {
253       return grid().overlapSize( codim );
254     }
255 
256     //! \brief return size of the ghost region for a given codim on the grid view
ghostSize(int codim) const257     int ghostSize ( int codim ) const
258     {
259       return grid().ghostSize( codim );
260     }
261 
262     //! \brief communicate data on this view
263     template< class DataHandleImp, class DataType >
communicate(CommDataHandleIF<DataHandleImp,DataType> & data,InterfaceType iftype,CommunicationDirection dir) const264     void communicate ( CommDataHandleIF< DataHandleImp, DataType > &data,
265                        InterfaceType iftype,
266                        CommunicationDirection dir ) const
267     {
268       return grid().communicate( data, iftype, dir );
269     }
270 
271     //! \brief return connectivity
272     template< PartitionType pit >
connectivity() const273     typename Partition< pit >::Connectivity connectivity () const
274     {
275       return grid().template leafConnectivity< pit >();
276     }
277 
278     //! \brief return connectivity
connectivity() const279     Connectivity connectivity () const
280     {
281       return connectivity< pitype >();
282     }
283 
284     //! \brief return host grid view
285     template< PartitionType pit >
hostGridView(const int node) const286     const typename Partition< pit >::HostGridView hostGridView ( const int node ) const
287     {
288       assert( connectivity< pit >().contains( node ) );
289       return connectivity< pit >().hostGrid( node );
290     }
291 
292     //! \brief return host grid view
hostGridView(const int node) const293     const HostGridView hostGridView ( const int node ) const
294     {
295       return hostGridView< pitype >( node );
296     }
297   };
298 
299 
300 
301   // MultiSPLevelGridViewTraits
302   // --------------------------
303 
304   template< PartitionIteratorType pitype, class GridImp >
305   class MultiSPLevelGridViewTraits
306   {
307     // grid traits
308     typedef typename std::remove_const< GridImp >::type::Traits Traits;
309 
310   public:
311     //! \brief grid view type
312     typedef MultiSPLevelGridView< pitype, GridImp > GridViewImp;
313 
314     // \brief grid type
315     typedef typename std::remove_const< GridImp >::type Grid;
316     //! \brief host grid type
317     typedef typename Traits::HostGrid HostGrid;
318 
319     //! \brief index set
320     typedef typename Traits::LevelIndexSet IndexSet;
321     //! \brief intersection
322     typedef typename Traits::LevelIntersection Intersection;
323     //! \brief intersection iterator type
324     typedef typename Traits::LevelIntersectionIterator IntersectionIterator;
325 
326     //! \brief collective communication type
327     typedef typename Traits::CollectiveCommunication CollectiveCommunication;
328 
329     template< int codim >
330     struct Codim
331     {
332       //! \brief iterator type
333       typedef typename Traits::template Codim< codim >::template Partition< pitype >::LevelIterator Iterator;
334 
335       //! \brief entity type
336       typedef typename Traits::template Codim< codim >::Entity Entity;
337       //! \brief entity pointer type
338       typedef typename Traits::template Codim< codim >::EntityPointer EntityPointer;
339 
340       //! \brief geometry type
341       typedef typename Traits::template Codim< codim >::Geometry Geometry;
342       //! \brief local geometry type
343       typedef typename Traits::template Codim< codim >::LocalGeometry LocalGeometry;
344 
345       template <PartitionIteratorType pit >
346       struct Partition
347       {
348         //! \brief iterator type
349         typedef typename Traits::template Codim< codim >::template Partition< pit >::LevelIterator Iterator;
350       };
351     };
352 
353     static const bool conforming = false;
354 
355     template< PartitionIteratorType pit >
356     struct Partition
357     {
358       //! \brief host grid view type
359       typedef typename HostGrid::template Partition< pit >::LevelGridView HostGridView;
360       //! \brief connectivity
361       typedef typename Traits::template Partition< pit >::LevelConnectivity Connectivity;
362     };
363     //! \brief host grid view type
364     typedef typename Partition< pitype >::HostGridView HostGridView;
365     //! \brief connectivity
366     typedef typename Partition< pitype >::Connectivity Connectivity;
367   };
368 
369 
370 
371   // MultiSPLevelGridView
372   // --------------------
373 
374   /** \brief Level grid views
375    *
376    *  \ingroup GridView
377    */
378   template< PartitionIteratorType pitype, class Grid >
379   class MultiSPLevelGridView
380   : public MultiSPGridView< MultiSPLevelGridViewTraits< pitype, Grid > >
381   {
382     // this type
383     typedef MultiSPLevelGridView< pitype, Grid > This;
384     // view traits
385     typedef MultiSPLevelGridViewTraits< pitype, Grid > Traits;
386     // base type
387     typedef MultiSPGridView< Traits > Base;
388 
389   public:
390     //! \brief host grid type
391     typedef typename Traits::HostGrid HostGrid;
392 
393     //! \brief index set
394     typedef typename Traits::IndexSet IndexSet;
395     //! \brief intersection
396     typedef typename Traits::Intersection Intersection;
397     //! \brief intersection iterator type
398     typedef typename Traits::IntersectionIterator IntersectionIterator;
399 
400     template< int codim >
401     struct Codim
402     : public Traits::template Codim< codim >
403     {};
404 
405     template< PartitionIteratorType pit >
406     struct Partition
407     : public Traits::template Partition< pit >
408     {};
409 
410     //! \brief host grid view
411     typedef typename Traits::HostGridView HostGridView;
412     //! \brief connectivity
413     typedef typename Traits::Connectivity Connectivity;
414 
415   public:
416     using Base::grid;
417 
418     //! \brief constructor
MultiSPLevelGridView(int level,const Grid & grid)419     MultiSPLevelGridView ( int level, const Grid &grid )
420     : Base( grid ),
421       level_( level )
422     {}
423 
424     //! \brief return index set
indexSet() const425     const IndexSet &indexSet () const
426     {
427       return grid().levelIndexSet( level_ );
428     }
429 
430     //! \brief return number of entities for given codimension in grid view
size(const int codim) const431     int size ( const int codim ) const
432     {
433       return grid().size( level_, codim );
434     }
435 
436     //! \brief return number of entities for given geometry type in grid view
size(const GeometryType & type) const437     int size ( const GeometryType &type ) const
438     {
439       return grid().size( level_, type );
440     }
441 
442     //! \brief return begin iterator for this view
443     template< int codim >
begin() const444     typename Codim< codim >::Iterator begin () const
445     {
446       return begin< codim, pitype >();
447     }
448 
449     //! \brief return begin iterator for this view
450     template< int codim, PartitionIteratorType pit >
begin() const451     typename Codim< codim >::template Partition< pit >::Iterator begin () const
452     {
453       return grid().template lbegin< codim, pit >( level_ );
454     }
455 
456     //! \brief return end iterator for this view
457     template< int codim >
end() const458     typename Codim< codim >::Iterator end () const
459     {
460       return end< codim, pitype >();
461     }
462 
463     //! \brief return end iterator for this view
464     template< int codim, PartitionIteratorType pit >
end() const465     typename Codim< codim >::template Partition< pit >::Iterator end () const
466     {
467       return grid().template lend< codim, pit >( level_ );
468     }
469 
470     //! \brief obtain begin intersection iterator with respect to this view
ibegin(const typename Traits::template Codim<0>::Entity & entity) const471     IntersectionIterator ibegin ( const typename Traits::template Codim< 0 >::Entity &entity ) const
472     {
473       typedef typename IntersectionIterator::Implementation IteratorImp;
474       return IteratorImp::begin( entity.impl() );
475     }
476 
477     //! \brief obtain end intersection iterator with respect to this view
iend(const typename Traits::template Codim<0>::Entity & entity) const478     IntersectionIterator iend ( const typename Traits::template Codim< 0 >::Entity &entity ) const
479     {
480       typedef typename IntersectionIterator::Implementation IteratorImp;
481       return IteratorImp::end( entity.impl() );
482     }
483 
484     //! \brief return size of the overlap region for a given codim on the grid view
overlapSize(int codim) const485     int overlapSize ( int codim ) const
486     {
487       return grid().overlapSize( codim );
488     }
489 
490     //! \brief return size of the ghost region for a given codim on the grid view
ghostSize(int codim) const491     int ghostSize ( int codim ) const
492     {
493       return grid().ghostSize( level_, codim );
494     }
495 
496     //! \brief communicate data on this view
497     template< class DataHandleImp, class DataType >
communicate(CommDataHandleIF<DataHandleImp,DataType> & data,InterfaceType iftype,CommunicationDirection dir) const498     void communicate ( CommDataHandleIF< DataHandleImp, DataType > &data,
499                        InterfaceType iftype,
500                        CommunicationDirection dir ) const
501     {
502       return grid().communicate( data, iftype, dir, level_ );
503     }
504 
505     //! \brief return connectivity
506     template< PartitionType pit >
connectivity() const507     typename Partition< pit >::Connectivity connectivity () const
508     {
509       return grid().template levelConnectivity< pit >( level_ );
510     }
511 
512     //! \brief return connectivity
connectivity() const513     Connectivity connectivity () const
514     {
515       return connectivity< pitype >();
516     }
517 
518     //! \brief return host grid view
519     template< PartitionType pit >
hostGridView(const int node) const520     const typename Partition< pit >::HostGridView hostGridView ( const int node ) const
521     {
522       assert( connectivity< pit >().contains( node ) );
523       return connectivity< pit >().hostGrid( node );
524     }
525 
526     //! \brief return host grid view
hostGridView(const int node) const527     const HostGridView hostGridView ( const int node ) const
528     {
529       return hostGridView< pitype >( node );
530     }
531 
532   private:
533     int level_;
534   };
535 
536 } // namespace Dune
537 
538 #endif // #ifndef DUNE_MULTISPGRID_GRIDVIEW_HH
539