dune-fem  2.8-git
petscdiscretefunction/petscdiscretefunction.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
2 #define DUNE_FEM_PETSCDISCRETEFUNCTION_HH
3 
4 #include <memory>
5 #include <string>
6 #include <utility>
7 
11 
12 #if HAVE_PETSC
13 
14 #include <dune/common/dynvector.hh>
20 
22 
23 namespace Dune
24 {
25  namespace Fem
26  {
27 
28  template< class DiscreteFunctionSpace >
29  class PetscDiscreteFunction;
30 
31  template< class DofProxy, class Allocator >
32  struct AssignVectorReference< Dune::DynamicVector< DofProxy, Allocator > >
33  {
34  // we need to overload this
35  typedef Dune::DynamicVector< DofProxy, Allocator > Vector;
36 
37  AssignVectorReference ( Vector &vector )
38  : vector_( vector )
39  {}
40 
41  void operator() ( const std::size_t index, DofProxy value ) const
42  {
43  vector_[ index ].assign( value );
44  }
45 
46  protected:
47  Vector &vector_;
48  };
49 
50 
51 
58  template< typename DiscreteFunctionSpace >
59  struct DiscreteFunctionTraits< PetscDiscreteFunction< DiscreteFunctionSpace > >
60  : public DefaultDiscreteFunctionTraits< DiscreteFunctionSpace, PetscVector< DiscreteFunctionSpace > >
61  {
62  typedef PetscVector< DiscreteFunctionSpace > DofVectorType;
63  typedef PetscDiscreteFunction< DiscreteFunctionSpace > DiscreteFunctionType;
64 
65  typedef typename DofVectorType::DofBlockType DofBlockType;
66  typedef typename DofBlockType::DofProxy DofProxyType;
67 
68  typedef ThreadSafeValue< UninitializedObjectStack > LocalDofVectorStackType;
69  typedef StackAllocator< DofProxyType, LocalDofVectorStackType* > LocalDofVectorAllocatorType;
70  typedef Dune::DynamicVector< DofProxyType, LocalDofVectorAllocatorType > LocalDofVectorType;
71 
72  typedef MutableLocalFunction< DiscreteFunctionType > LocalFunctionType;
73  };
74 
75 
76 
77  // PetscDiscreteFunction
78  // ---------------------
79 
80  template <class DiscreteFunctionSpace>
81  class PetscDiscreteFunction
82  : public DiscreteFunctionDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >
83  {
84  typedef PetscDiscreteFunction< DiscreteFunctionSpace > ThisType;
85  typedef DiscreteFunctionDefault< ThisType > BaseType;
86 
87  public:
88  typedef typename BaseType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
89  typedef typename BaseType :: DofVectorType DofVectorType;
90 
91  // generic assign method
92  using BaseType::assign;
93 
99  PetscDiscreteFunction( const std::string& name,
100  const DiscreteFunctionSpaceType& space )
101  : BaseType( name, space ),
102  memObject_(),
103  dofVector_( allocateDofStorage( space ) )
104  {}
105 
112  PetscDiscreteFunction( const std::string& name,
113  const DiscreteFunctionSpaceType& space,
114  DofVectorType& dofVector )
115  : BaseType( name, space ),
116  memObject_(),
117  dofVector_( dofVector )
118  {}
119 
121  PetscDiscreteFunction( const ThisType& other )
122  : BaseType( "copy of " + other.name(), other.space() ),
123  memObject_(),
124  dofVector_( allocateDofStorage( other.space() ) )
125  {
126  assign( other );
127  }
128 
130  PetscDiscreteFunction( ThisType&& other )
131  : BaseType( static_cast< BaseType && >( other ) ),
132  memObject_( std::move( other.memObject_ ) ),
133  dofVector_( other.dofVector_ )
134  {}
135 
136  PetscDiscreteFunction () = delete;
137  ThisType& operator= ( const ThisType& ) = delete;
138  ThisType& operator= ( ThisType&& ) = delete;
139 
141  void enableDofCompression ()
142  {
143  if( memObject_ )
144  memObject_->enableDofCompression();
145  }
146 
149  void assign( const AdaptiveDiscreteFunction< DiscreteFunctionSpaceType > &g )
150  {
151  // call more efficient assign on PetscVector
152  dofVector().assignVector( g.dofVector() );
153  }
154 
157  template < class Block >
158  void assign( const ISTLBlockVectorDiscreteFunction< DiscreteFunctionSpaceType, Block > &g )
159  {
160  // call more efficient assign on PetscVector
161  dofVector().assignVector( g.dofVector() );
162  }
163 
166  template < class Vector >
167  void assign( const VectorDiscreteFunction< DiscreteFunctionSpaceType, Vector > &g )
168  {
169  // call more efficient assign on PetscVector
170  dofVector().assignVector( g.dofVector() );
171  }
172 
174  template< class AssembleOperation >
175  void beginAssemble ()
176  {
177  BaseType :: template beginAssemble< AssembleOperation > ();
178  dofVector().beginAssemble();
179  }
180 
182  template< class AssembleOperation >
183  void endAssemble ()
184  {
185  BaseType :: template endAssemble< AssembleOperation > ();
186  dofVector().endAssemble();
187  }
188 
190  DofVectorType& dofVector() { return dofVector_; }
192  const DofVectorType& dofVector() const { return dofVector_; }
193 
195  const Vec* petscVec () const { return dofVector().getVector(); }
196 
198  Vec* petscVec () { return dofVector().getVector(); }
199 
200  protected:
201  typedef typename DiscreteFunctionSpaceType :: BlockMapperType BlockMapperType;
202  typedef PetscManagedDofStorage< DiscreteFunctionSpaceType, BlockMapperType > PetscManagedDofStorageType;
203 
204  // allocate managed dof storage
205  DofVectorType& allocateDofStorage ( const DiscreteFunctionSpaceType &space )
206  {
207  memObject_.reset( new PetscManagedDofStorageType( space, space.blockMapper() ) );
208 
209  return memObject_->getArray();
210  }
211 
212  // pointer to allocated DofVector
213  std::unique_ptr< PetscManagedDofStorageType > memObject_;
214 
215  // dof vector impl
216  DofVectorType& dofVector_;
217  };
218 
219 
220  template <class DiscreteFunctionSpace>
221  class RestrictProlongDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >
222  : public RestrictProlongInterfaceDefault< RestrictProlongTraits< RestrictProlongDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >,
223  typename DiscreteFunctionSpace::DomainFieldType > >
224  {
225  typedef PetscDiscreteFunction< DiscreteFunctionSpace > DiscreteFunction;
226  typedef RestrictProlongDefault< DiscreteFunction > ThisType;
227  typedef RestrictProlongInterfaceDefault< RestrictProlongTraits< ThisType, typename DiscreteFunction::DomainFieldType > > BaseType;
228 
229  typedef AdaptiveDiscreteFunction< DiscreteFunctionSpace > AdaptiveDiscreteFunctionType;
230  typedef RestrictProlongDefault < AdaptiveDiscreteFunctionType > AdaptiveRestrictProlongType;
231 
232  public:
233  typedef DiscreteFunction DiscreteFunctionType;
234 
236 
237  typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
238  typedef typename DiscreteFunctionType::LocalFunctionType LocalFunctionType;
239  typedef typename DiscreteFunctionType::GridPartType GridPartType;
240 
242 
243  explicit RestrictProlongDefault ( DiscreteFunctionType &discreteFunction )
244  : discreteFunction_( discreteFunction ),
245  adaptiveFunction_( discreteFunction_.name()+"-adaptive", discreteFunction_.space() ),
246  rpOp_( adaptiveFunction_ ),
247  initialized_( false )
248  {
249  }
250 
252  : discreteFunction_( const_cast< DiscreteFunction& > (other.discreteFunction_) ),
253  adaptiveFunction_( other.adaptiveFunction_.name()+"-copy", discreteFunction_.space() ),
254  rpOp_( adaptiveFunction_ ),
255  initialized_( false )
256  {
257  }
258 
259  void setFatherChildWeight ( const DomainFieldType &weight ) const
260  {
261  rpOp_.setFatherChildWeight( weight );
262  }
263 
265  template< class Entity >
266  void restrictLocal ( const Entity &father, const Entity &son, bool initialize ) const
267  {
268  assert( initialized_ );
269  rpOp_.restrictLocal( father, son, initialize );
270  }
271 
273  template< class Entity >
274  void prolongLocal ( const Entity &father, const Entity &son, bool initialize ) const
275  {
276  assert( initialized_ );
277  rpOp_.prolongLocal( father, son, initialize );
278  }
279 
281  template< class Communicator, class Operation >
282  void addToList ( Communicator &comm, const Operation& op)
283  {
284  rpOp_.addToList( comm, op );
285  }
286 
288  template< class Communicator >
289  void addToList ( Communicator &comm )
290  {
291  rpOp_.addToList( comm );
292  }
293 
295  template< class Communicator >
296  void removeFromList ( Communicator &comm )
297  {
298  rpOp_.removeFromList( comm );
299  }
300 
302  template< class LoadBalancer >
303  void addToLoadBalancer ( LoadBalancer& lb )
304  {
305  rpOp_.addToLoadBalancer( lb );
306  }
307 
308  void initialize ()
309  {
310  adaptiveFunction_.assign( discreteFunction_ );
311  rpOp_.initialize();
312  initialized_ = true ;
313  }
314 
315  void finalize ()
316  {
317  // only finalize if previously initialized
318  assert( initialized_ );
319  rpOp_.finalize();
320  discreteFunction_.assign( adaptiveFunction_ );
321  initialized_ = false ;
322  }
323 
324  protected:
325  using BaseType::calcWeight;
327 
328  protected:
330  AdaptiveDiscreteFunctionType adaptiveFunction_;
331  AdaptiveRestrictProlongType rpOp_;
332 
333  bool initialized_;
334  };
335 
336  } // namespace Fem
337 } // namespace Dune
338 
339 #endif // #if HAVE_PETSC
340 
341 #endif // #ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
Definition: bindguard.hh:11
AssignVectorReference(Vector &vector)
Definition: function/common/functor.hh:86
Vector & vector_
Definition: function/common/functor.hh:97
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:91
DomainFieldType calcWeight(const Entity &father, const Entity &son) const
calculates the weight, i.e. (volume son)/(volume father)
Definition: restrictprolonginterface.hh:138
bool entitiesAreCopies(const IndexSet &indexSet, const Entity &father, const Entity &son) const
return true if father and son have the same index
Definition: restrictprolonginterface.hh:175
BaseType::DomainFieldType DomainFieldType
Definition: restrictprolonginterface.hh:170
void setFatherChildWeight(const DomainFieldType &weight) const
explicit set volume ratio of son and father
Definition: restrictprolonginterface.hh:252
DefaultLocalRestrictProlong< DiscreteFunctionSpaceType > LocalRestrictProlongType
Definition: restrictprolonginterface.hh:219
void addToList(Communicator &comm, const Operation &op)
add discrete function to communicator with given unpack operation
Definition: restrictprolonginterface.hh:343
DiscreteFunctionType & discreteFunction_
Definition: restrictprolonginterface.hh:373
void removeFromList(Communicator &comm)
remove discrete function from communicator
Definition: restrictprolonginterface.hh:359
DiscreteFunction DiscreteFunctionType
Definition: restrictprolonginterface.hh:208
RestrictProlongDefault(const RestrictProlongDefault &other)
Definition: restrictprolonginterface.hh:221
BaseType::DomainFieldType DomainFieldType
Definition: restrictprolonginterface.hh:210
void prolongLocal(const Entity &father, const Entity &son, bool initialize) const
prolong data to children
Definition: restrictprolonginterface.hh:307
DiscreteFunctionType::GridPartType GridPartType
Definition: restrictprolonginterface.hh:217
void addToLoadBalancer(LoadBalancer &lb)
add discrete function to load balancer
Definition: restrictprolonginterface.hh:367
void restrictLocal(const Entity &father, const Entity &son, bool initialize) const
restrict data to father
Definition: restrictprolonginterface.hh:259
DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition: restrictprolonginterface.hh:212
discrete function space