parfvdatahandle.hh 1.71 KB
Newer Older
Peter Bastian's avatar
Peter Bastian committed
1 2
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
3 4 5 6 7
#ifndef __DUNE_GRID_HOWTO_PARFVDATAHANDLE_HH__
#define __DUNE_GRID_HOWTO_PARFVDATAHANDLE_HH__

#include <dune/grid/common/datahandleif.hh>

Peter Bastian's avatar
Peter Bastian committed
8 9
// A DataHandle class to exchange entries of a vector
template<class M, class V> // mapper type and vector type
10 11 12 13
class VectorExchange
  : public Dune::CommDataHandleIF<VectorExchange<M,V>,
        typename V::value_type>
{
Peter Bastian's avatar
Peter Bastian committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
public:
  //! export type of data for message buffer
  typedef typename V::value_type DataType;

  //! returns true if data for this codim should be communicated
  bool contains (int dim, int codim) const
  {
    return (codim==0);
  }

  //! returns true if size per entity of given dim and codim is a constant
  bool fixedsize (int dim, int codim) const
  {
    return true;
  }

  /*! how many objects of type DataType have to be sent for a given entity

     Note: Only the sender side needs to know this size.
   */
  template<class EntityType>
  size_t size (EntityType& e) const
  {
    return 1;
  }

  //! pack data from user to message buffer
  template<class MessageBuffer, class EntityType>
  void gather (MessageBuffer& buff, const EntityType& e) const
  {
44
    buff.write(c[mapper.index(e)]);
Peter Bastian's avatar
Peter Bastian committed
45 46 47 48 49 50 51 52 53 54 55
  }

  /*! unpack data from message buffer to user

     n is the number of objects sent by the sender
   */
  template<class MessageBuffer, class EntityType>
  void scatter (MessageBuffer& buff, const EntityType& e, size_t n)
  {
    DataType x;
    buff.read(x);
56
    c[mapper.index(e)]=x;
Peter Bastian's avatar
Peter Bastian committed
57 58 59 60 61 62 63 64 65 66 67
  }

  //! constructor
  VectorExchange (const M& mapper_, V& c_)
    : mapper(mapper_), c(c_)
  {}

private:
  const M& mapper;
  V& c;
};
68 69

#endif // __DUNE_GRID_HOWTO_PARFVDATAHANDLE_HH__