dune-fem  2.4.1-rc
defaultcommhandler.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_DEFAULTDATAHANDLE_HH
2 #define DUNE_FEM_DEFAULTDATAHANDLE_HH
3 
4 #include <cassert>
5 
6 //- Dune includes
7 #include <dune/grid/common/datahandleif.hh>
9 
10 namespace Dune
11 {
12 
13  namespace Fem
14  {
15 
22  template< class DiscreteFunction, class Operation = DFCommunicationOperation::Add >
24  : public CommDataHandleIF
25  < DefaultCommunicationHandler< DiscreteFunction, Operation >,
26  typename DiscreteFunction::DofType >
27  {
29  typedef CommDataHandleIF< ThisType, typename DiscreteFunction::DofType > BaseType;
30 
31  public:
32  typedef typename BaseType::DataType DataType;
33 
34  typedef DiscreteFunction DiscreteFunctionType;
35 
36  typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
37 
38  protected:
39  typedef typename DiscreteFunctionSpaceType::BlockMapperType BlockMapperType;
40 
41  typedef typename DiscreteFunctionType::DofBlockPtrType DofBlockPtrType;
42 
43  static const unsigned int blockSize = DiscreteFunctionSpaceType::localBlockSize;
44 
45  public:
46  DefaultCommunicationHandler( DiscreteFunctionType &function )
47  : function_( &function ),
48  blockMapper_( function.space().blockMapper() )
49  {}
50 
52  : function_( other.function_ ),
53  blockMapper_( other.blockMapper_ )
54  {}
55 
56  private:
59 
60  template < class Buffer >
61  struct GatherFunctor
62  {
63  Buffer& buffer_;
64  DiscreteFunctionType *const function_;
65 
66  GatherFunctor( Buffer& buffer, DiscreteFunctionType* function )
67  : buffer_( buffer ),
68  function_( function )
69  {
70  }
71 
72  template <class GlobalKey>
73  void operator () ( const size_t local, const GlobalKey& globalKey )
74  {
75  DofBlockPtrType blockPtr = function_->block( globalKey );
76  for( unsigned int j = 0; j < blockSize; ++j )
77  {
78  buffer_.write( (*blockPtr)[ j ] );
79  }
80  }
81  };
82 
83  template < class Buffer >
84  struct ScatterFunctor
85  {
86  Buffer& buffer_;
87  DiscreteFunctionType *const function_;
88 
89  ScatterFunctor( Buffer& buffer, DiscreteFunctionType* function )
90  : buffer_( buffer ),
91  function_( function )
92  {
93  }
94 
95  template <class GlobalKey>
96  void operator () ( const size_t local, const GlobalKey& globalKey )
97  {
98  DofBlockPtrType blockPtr = function_->block( globalKey );
99  for( unsigned int j = 0; j < blockSize; ++j )
100  {
101  DataType value;
102  buffer_.read( value );
103 
104  Operation :: apply( value, (*blockPtr)[ j ] );
105  }
106  }
107  };
108  public:
109  bool contains ( int dim, int codim ) const
110  {
111  return blockMapper_.contains( codim );
112  }
113 
114  bool fixedsize ( int dim, int codim) const
115  {
116  return blockMapper_.fixedDataSize( codim );
117  }
118 
120  template< class MessageBuffer, class Entity >
121  void gather ( MessageBuffer &buffer, const Entity &entity ) const
122  {
123  GatherFunctor< MessageBuffer > gatherDofs ( buffer, function_ );
124  blockMapper_.mapEachEntityDof( entity, gatherDofs );
125  }
126 
128  template< class MessageBuffer, class Entity >
129  void scatter ( MessageBuffer &buffer, const Entity &entity, size_t n )
130  {
131  assert( n == blockSize * blockMapper_.numEntityDofs( entity ) );
132  ScatterFunctor< MessageBuffer > scatterDofs ( buffer, function_ );
133 
134  blockMapper_.mapEachEntityDof( entity, scatterDofs );
135  }
136 
138  template< class Entity >
139  size_t size ( const Entity &entity ) const
140  {
141  return blockSize * blockMapper_.numEntityDofs( entity );
142  }
143 
144  protected:
145  DiscreteFunctionType *const function_;
146  const BlockMapperType &blockMapper_;
147  };
148 
149  } // namespace Fem
150 
151 } // namespace Dune
152 
153 #endif // #ifndef DUNE_FEM_DEFAULTDATAHANDLE_HH
void scatter(MessageBuffer &buffer, const Entity &entity, size_t n)
read buffer and apply operation
Definition: defaultcommhandler.hh:129
Default communication handler for discrete functions.
Definition: defaultcommhandler.hh:23
DiscreteFunction DiscreteFunctionType
Definition: defaultcommhandler.hh:34
DefaultCommunicationHandler(const DefaultCommunicationHandler &other)
Definition: defaultcommhandler.hh:51
static const unsigned int blockSize
Definition: defaultcommhandler.hh:43
DiscreteFunctionSpaceType::BlockMapperType BlockMapperType
Definition: defaultcommhandler.hh:39
const BlockMapperType & blockMapper_
Definition: defaultcommhandler.hh:146
bool contains(int dim, int codim) const
Definition: defaultcommhandler.hh:109
size_t size(const Entity &entity) const
return local dof size to be communicated
Definition: defaultcommhandler.hh:139
DiscreteFunctionType::DofBlockPtrType DofBlockPtrType
Definition: defaultcommhandler.hh:41
void gather(MessageBuffer &buffer, const Entity &entity) const
read buffer and apply operation
Definition: defaultcommhandler.hh:121
DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition: defaultcommhandler.hh:36
Definition: coordinate.hh:4
DefaultCommunicationHandler(DiscreteFunctionType &function)
Definition: defaultcommhandler.hh:46
bool fixedsize(int dim, int codim) const
Definition: defaultcommhandler.hh:114
BaseType::DataType DataType
Definition: defaultcommhandler.hh:32
DiscreteFunctionType *const function_
Definition: defaultcommhandler.hh:145