dune-fem  2.6-git
common/dataprojection/default.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_HPDG_SPACE_COMMON_DATAPROJECTION_DEFAULT_HH
2 #define DUNE_FEM_HPDG_SPACE_COMMON_DATAPROJECTION_DEFAULT_HH
3 
4 #include <cassert>
5 #include <cstddef>
6 
7 #include <functional>
8 #include <vector>
9 
10 #include <dune/common/dynvector.hh>
11 
13 
14 #include "dataprojection.hh"
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
22  namespace hpDG
23  {
24 
25  // DefaultDataProjection
26  // ---------------------
27 
34  template< class DiscreteFunction >
36  : public DataProjection< typename DiscreteFunction::DiscreteFunctionSpaceType, DefaultDataProjection< DiscreteFunction > >
37  {
40 
41  public:
47  using EntityType = typename BaseType::EntityType;
48 
49  private:
50  using RangeFieldType = typename DiscreteFunction::RangeFieldType;
51  using LocalDofVectorType = Dune::DynamicVector< RangeFieldType >;
53 
54  static const std::size_t localBlockSize = DiscreteFunctionSpaceType::localBlockSize;
55 
56  public:
61  explicit DefaultDataProjection ( DiscreteFunction &discreteFunction )
62  : discreteFunction_( discreteFunction )
63  {
64  discreteFunction_.get().enableDofCompression();
65  }
66 
69 #ifndef DOXYGEN
70 
71  DefaultDataProjection ( const ThisType & ) = delete;
72 
73  DefaultDataProjection ( ThisType && ) = default;
74 
75  ThisType &operator= ( const ThisType & ) = delete;
76 
77  ThisType &operator= ( ThisType && ) = default;
78 
79 #endif // #ifndef DOXYGEN
80 
82  void operator() ( const EntityType &entity,
83  const BasisFunctionSetType &prior,
84  const BasisFunctionSetType &present,
85  const std::vector< std::size_t > &origin,
86  const std::vector< std::size_t > &destination )
87  {
88  LocalFunctionType localFunction( prior );
89  read( origin, localFunction.localDofVector() );
90 
91  assert( present.size() == space().basisFunctionSet( entity ).size() );
92  LocalDofVectorType localDofVector( present.size() );
93 
94  const auto interpolation = space().interpolation( entity );
95  interpolation( localFunction, localDofVector );
96 
97  write( destination, localDofVector );
98  }
99 
101  template <class TemporaryStorage>
102  void operator () ( TemporaryStorage& tmp )
103  {
104  auto& df = discreteFunction();
105 
106  // copy dofs to temporary storage, old order still exists
107  auto dfit = df.dbegin();
108  const auto endtmp = tmp.dend();
109  for( auto it = tmp.dbegin(); it != endtmp; ++it, ++dfit )
110  {
111  assert( dfit != df.dend() );
112  *it = *dfit;
113  }
114 
115  // interpolate to new space, this can be a
116  // Lagrange interpolation or a L2 projection, both locally
117  const auto end = df.space().end();
118  for( auto it = df.space().begin(); it != end; ++it )
119  {
120  const auto& entity = *it;
121  const auto tmpLF = tmp.localFunction( entity );
122  auto lf = df.localFunction( entity );
123 
124  // if size is the same we can just copy the dof values
125  if( tmpLF.size() == lf.size() )
126  {
127  lf.assign( tmpLF );
128  }
129  else
130  {
131  // otherwise a local interpolation is needed
132  df.space().interpolation( entity )( tmpLF, lf );
133  }
134  }
135  }
136 
138  template< class Communicator >
139  void addToList ( Communicator &comm )
140  {
141  comm.addToList( discreteFunction() );
142  }
143 
144  private:
145  template< class LocalDofVector >
146  void read ( const std::vector< std::size_t > &blocks, LocalDofVector &localDofVector ) const
147  {
148  assert( localDofVector.size() == localBlockSize*blocks.size() );
149  std::size_t index = 0;
150  for( auto i : blocks )
151  {
152  const auto block = discreteFunction().block( i );
153  for( std::size_t j = 0; j < localBlockSize; ++j )
154  localDofVector[ index++ ] = (*block)[ j ];
155  }
156  assert( index == localDofVector.size() );
157  }
158 
159  template< class LocalDofVector >
160  void write ( const std::vector< std::size_t > &blocks, const LocalDofVector &localDofVector )
161  {
162  assert( localDofVector.size() == localBlockSize*blocks.size() );
163  std::size_t index = 0;
164  for( auto i : blocks )
165  {
166  auto block = discreteFunction().block( i );
167  for( std::size_t j = 0; j < localBlockSize; ++j )
168  (*block)[ j ] = localDofVector[ index++ ];
169  }
170  assert( index == localDofVector.size() );
171  }
172 
173  DiscreteFunction &discreteFunction () { return discreteFunction_.get(); }
174 
175  const DiscreteFunction &discreteFunction () const { return discreteFunction_.get(); }
176 
177  const DiscreteFunctionSpaceType &space () const { return discreteFunction().space(); }
178 
179  std::reference_wrapper< DiscreteFunction > discreteFunction_;
180  };
181 
182  } // namespace hpDG
183 
184  // forward types to Fem namespace for convenience
185  using hpDG::DefaultDataProjection;
186 
187  } // namespace Fem
188 
189 } // namespace Dune
190 
191 #endif // #ifndef DUNE_FEM_HPDG_SPACE_COMMON_DATAPROJECTION_DEFAULT_HH
Definition: bindguard.hh:11
interface for local functions
Definition: localfunction.hh:44
const LocalDofVectorType & localDofVector() const
return const reference to local Dof Vector
Definition: localfunction.hh:396
Abstract definition of the local restriction and prolongation of discrete functions.
Definition: common/dataprojection/dataprojection.hh:29
typename BasisFunctionSetType::EntityType EntityType
entity type
Definition: common/dataprojection/dataprojection.hh:38
DiscreteFunctionSpace DiscreteFunctionSpaceType
discrete function space type
Definition: common/dataprojection/dataprojection.hh:34
typename DiscreteFunctionSpaceType::BasisFunctionSetType BasisFunctionSetType
basis function set type
Definition: common/dataprojection/dataprojection.hh:36
Local -projection for the restriction and prolongation of discrete functions.
Definition: common/dataprojection/default.hh:37
void addToList(Communicator &comm)
()
Definition: common/dataprojection/default.hh:139
typename BaseType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
discrete function space type
Definition: common/dataprojection/default.hh:43
typename BaseType::EntityType EntityType
entity type
Definition: common/dataprojection/default.hh:47
typename BaseType::BasisFunctionSetType BasisFunctionSetType
basis function set type
Definition: common/dataprojection/default.hh:45
void operator()(const EntityType &entity, const BasisFunctionSetType &prior, const BasisFunctionSetType &present, const std::vector< std::size_t > &origin, const std::vector< std::size_t > &destination)
Definition: common/dataprojection/default.hh:82
DefaultDataProjection(DiscreteFunction &discreteFunction)
Definition: common/dataprojection/default.hh:61