1 #ifndef DUNE_FILTEREDGRID_DATAHANDLE_HH
2 #define DUNE_FILTEREDGRID_DATAHANDLE_HH
3 
4 //- C++ includes
5 #include <cassert>
6 
7 //- dune-common includes
8 #include <dune/common/typetraits.hh>
9 
10 //- dune-grid includes
11 #include <dune/grid/common/datahandleif.hh>
12 
13 
14 namespace Dune
15 {
16 
17   // FilteredGridDataHandle
18   // ----------------------
19 
20   template< class WrappedHandle, class Grid >
21   class FilteredGridDataHandle
22   : public CommDataHandleIF< FilteredGridDataHandle< WrappedHandle, Grid >, typename WrappedHandle::DataType >
23   {
24     typedef FilteredGridDataHandle< WrappedHandle, Grid > This;
25 
26     // type of traits
27     typedef typename std::remove_const< Grid >::type::Traits Traits;
28 
29     template< int codim >
30     struct Codim
31     {
32       // type of entity
33       typedef typename Traits::template Codim< codim >::Entity Entity;
34     };
35 
36   public:
37     // type of data to be communicated
38     typedef typename WrappedHandle::DataType DataType;
39 
40     typedef CommDataHandleIF< This, DataType > DataHandleIF;
41 
42   private:
43     // prohibit copying
44     FilteredGridDataHandle ( const This & );
45 
46   public:
FilteredGridDataHandle(WrappedHandle & wrappedHandle,const Grid & grid)47     FilteredGridDataHandle ( WrappedHandle &wrappedHandle, const Grid &grid )
48     : wrappedHandle_( wrappedHandle ),
49       grid_( grid )
50     {}
51 
contains(int dim,int codim) const52     bool contains ( int dim, int codim ) const
53     {
54       return wrappedHandle_.contains( dim, codim );
55     }
56 
fixedSize(int dim,int codim) const57     bool fixedSize ( int dim, int codim ) const
58     {
59       return wrappedHandle_.fixedSize( dim, codim );
60     }
61 
62     template< class HostEntity >
size(const HostEntity & hostEntity) const63     size_t size ( const HostEntity &hostEntity ) const
64     {
65       typedef typename Codim< HostEntity::codimension >::Entity Entity;
66       if( grid_.contains( hostEntity ) )
67       {
68         const Entity entity( typename Entity::Implementation( grid_.extraData(), hostEntity ) );
69         return wrappedHandle_.size( entity );
70       }
71       else
72         return 0;
73     }
74 
75     template< class MessageBuffer, class HostEntity >
gather(MessageBuffer & buffer,const HostEntity & hostEntity) const76     void gather ( MessageBuffer &buffer, const HostEntity &hostEntity ) const
77     {
78       typedef typename Codim< HostEntity::codimension >::Entity Entity;
79       if( grid_.contains( hostEntity ) )
80       {
81         const Entity entity( typename Entity::Implementation( grid_.extraData(), hostEntity ) );
82         wrappedHandle_.gather( buffer, entity );
83       }
84     }
85 
86     template< class MessageBuffer, class HostEntity >
scatter(MessageBuffer & buffer,const HostEntity & hostEntity,size_t size)87     void scatter ( MessageBuffer &buffer, const HostEntity &hostEntity, size_t size )
88     {
89       typedef typename Codim< HostEntity::codimension >::Entity Entity;
90       if( grid_.contains( hostEntity ) )
91       {
92         const Entity entity( typename Entity::Implementation( grid_.extraData(), hostEntity ) );
93         wrappedHandle_.scatter( buffer, entity, size );
94       }
95       else
96         assert( size == size_t( 0 ) );
97     }
98 
99   private:
100     WrappedHandle &wrappedHandle_;
101     const Grid &grid_;
102   };
103 
104 
105 
106   // FilteredGridWrappedDofManager
107   // -----------------------------
108 
109   template< class WrappedDofManager, class Grid >
110   class FilteredGridWrappedDofManager
111   {
112     typedef FilteredGridWrappedDofManager< WrappedDofManager, Grid > This;
113 
114     typedef typename std::remove_const< Grid >::type::Traits Traits;
115 
116     // type of element (i.e., entity of codimension 0)
117     typedef typename Traits::template Codim< 0 >::Entity Element;
118 
119     // type of host element (i.e., host entity of codimension 0)
120     typedef typename Traits::HostGrid::template Codim< 0 >::Entity HostElement;
121 
122   private:
123     // prohibit copy constructor
124     FilteredGridWrappedDofManager ( const This & );
125 
126   public:
FilteredGridWrappedDofManager(WrappedDofManager & wrappedDofManager,const Grid & grid)127     FilteredGridWrappedDofManager ( WrappedDofManager &wrappedDofManager, const Grid &grid )
128     : wrappedDofManager_( wrappedDofManager ),
129       grid_( grid )
130     {}
131 
132     template< class MessageBuffer >
inlineData(MessageBuffer & buffer,const HostElement & hostElement)133     void inlineData ( MessageBuffer &buffer, const HostElement &hostElement )
134     {
135       if( grid_.contains( hostElement ) )
136       {
137         const Element element( typename Element::Implementation( grid_.extraData(), hostElement ) );
138         wrappedDofManager_.inlineData( buffer, element );
139       }
140     }
141 
142     template< class MessageBuffer >
xtractData(MessageBuffer & buffer,const HostElement & hostElement,std::size_t newElements)143     void xtractData ( MessageBuffer &buffer, const HostElement &hostElement, std::size_t newElements )
144     {
145       if( grid_.contains( hostElement ) )
146       {
147         const Element element( typename Element::Implementation( grid_.extraData(), hostElement ) );
148         wrappedDofManager_.xtractData( buffer, element, newElements );
149       }
150     }
151 
compress()152     void compress () { wrappedDofManager_.compress(); }
153 
154   private:
155     WrappedDofManager &wrappedDofManager_;
156     const Grid &grid_;
157   };
158 
159 } // namespace Dune
160 
161 #endif // #ifndef DUNE_FILTEREDGRID_DATAHANDLE_HH
162