dune-fem 2.12-git
Loading...
Searching...
No Matches
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
20
22
23namespace 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
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;
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
100 PetscDiscreteFunction( const std::string& name,
101 const DiscreteFunctionSpaceType& space,
102 const int backend = ::Dune::Petsc::Backend::defaultBackend )
103 : BaseType( name, space ),
104 memObject_(),
105 dofVector_( allocateDofStorage( space, backend ) )
106 {}
107
114 PetscDiscreteFunction( const std::string& name,
115 const DiscreteFunctionSpaceType& space,
116 DofVectorType& dofVector )
117 : BaseType( name, space ),
118 memObject_(),
119 dofVector_( dofVector )
120 {}
121
123 PetscDiscreteFunction( const ThisType& other )
124 : BaseType( "copy of " + other.name(), other.space() ),
125 memObject_(),
126 dofVector_( allocateDofStorage( other.space(), other.dofVector().backend() ) )
127 {
128 assign( other );
129 }
130
132 PetscDiscreteFunction( ThisType&& other )
133 : BaseType( static_cast< BaseType && >( other ) ),
134 memObject_( std::move( other.memObject_ ) ),
135 dofVector_( other.dofVector_ )
136 {}
137
138 PetscDiscreteFunction () = delete;
139 ThisType& operator= ( const ThisType& ) = delete;
140 ThisType& operator= ( ThisType&& ) = delete;
141
143 void enableDofCompression ()
144 {
145 if( memObject_ )
146 memObject_->enableDofCompression();
147 }
148
151 void assign( const AdaptiveDiscreteFunction< DiscreteFunctionSpaceType > &g )
152 {
153 // call more efficient assign on PetscVector
154 dofVector().assignVector( g.dofVector() );
155 }
156
159 template < class Block >
160 void assign( const ISTLBlockVectorDiscreteFunction< DiscreteFunctionSpaceType, Block > &g )
161 {
162 // call more efficient assign on PetscVector
163 dofVector().assignVector( g.dofVector() );
164 }
165
168 template < class Vector >
169 void assign( const VectorDiscreteFunction< DiscreteFunctionSpaceType, Vector > &g )
170 {
171 // call more efficient assign on PetscVector
172 dofVector().assignVector( g.dofVector() );
173 }
174
176 template< class AssembleOperation >
177 void beginAssemble ()
178 {
179 BaseType :: template beginAssemble< AssembleOperation > ();
180 dofVector().beginAssemble();
181 }
182
184 template< class AssembleOperation >
185 void endAssemble ( const bool communicate = true )
186 {
187 BaseType :: template endAssemble< AssembleOperation > ();
188 dofVector().endAssemble( communicate );
189 }
190
192 DofVectorType& dofVector() { return dofVector_; }
194 const DofVectorType& dofVector() const { return dofVector_; }
195
197 const Vec* petscVec () const { return dofVector().getVector(); }
198
200 Vec* petscVec () { return dofVector().getVector(); }
201
202 protected:
203 typedef typename DiscreteFunctionSpaceType :: BlockMapperType BlockMapperType;
204 typedef PetscManagedDofStorage< DiscreteFunctionSpaceType, BlockMapperType > PetscManagedDofStorageType;
205
206 // allocate managed dof storage
207 DofVectorType& allocateDofStorage ( const DiscreteFunctionSpaceType &space, const int backend )
208 {
209 memObject_.reset( new PetscManagedDofStorageType( space, space.blockMapper(), backend ) );
210
211 return memObject_->getArray();
212 }
213
214 // pointer to allocated DofVector
216
217 // dof vector impl
218 DofVectorType& dofVector_;
219 };
220
221
222 template <class DiscreteFunctionSpace>
223 class RestrictProlongDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >
224 : public RestrictProlongInterfaceDefault< RestrictProlongTraits< RestrictProlongDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >,
225 typename DiscreteFunctionSpace::DomainFieldType > >
226 {
227 typedef PetscDiscreteFunction< DiscreteFunctionSpace > DiscreteFunction;
228 typedef RestrictProlongDefault< DiscreteFunction > ThisType;
229 typedef RestrictProlongInterfaceDefault< RestrictProlongTraits< ThisType, typename DiscreteFunction::DomainFieldType > > BaseType;
230
231 typedef AdaptiveDiscreteFunction< DiscreteFunctionSpace > AdaptiveDiscreteFunctionType;
232 typedef RestrictProlongDefault < AdaptiveDiscreteFunctionType > AdaptiveRestrictProlongType;
233
234 public:
235 typedef DiscreteFunction DiscreteFunctionType;
236
238
239 typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
240 typedef typename DiscreteFunctionType::LocalFunctionType LocalFunctionType;
241 typedef typename DiscreteFunctionType::GridPartType GridPartType;
242
244
245 explicit RestrictProlongDefault ( DiscreteFunctionType &discreteFunction )
246 : discreteFunction_( discreteFunction ),
247 adaptiveFunction_( discreteFunction_.name()+"-adaptive", discreteFunction_.space() ),
248 rpOp_( adaptiveFunction_ ),
249 initialized_( false )
250 {
251 }
252
253 RestrictProlongDefault ( const RestrictProlongDefault& other )
254 : discreteFunction_( const_cast< DiscreteFunction& > (other.discreteFunction_) ),
255 adaptiveFunction_( other.adaptiveFunction_.name()+"-copy", discreteFunction_.space() ),
256 rpOp_( adaptiveFunction_ ),
257 initialized_( false )
258 {
259 }
260
261 void setFatherChildWeight ( const DomainFieldType &weight ) const
262 {
263 rpOp_.setFatherChildWeight( weight );
264 }
265
267 template< class Entity >
268 void restrictLocal ( const Entity &father, const Entity &son, bool initialize ) const
269 {
270 assert( initialized_ );
271 rpOp_.restrictLocal( father, son, initialize );
272 }
273
275 template< class Entity >
276 void prolongLocal ( const Entity &father, const Entity &son, bool initialize ) const
277 {
278 assert( initialized_ );
279 rpOp_.prolongLocal( father, son, initialize );
280 }
281
283 template< class Communicator, class Operation >
284 void addToList ( Communicator &comm, const Operation& op)
285 {
286 rpOp_.addToList( comm, op );
287 }
288
290 template< class Communicator >
291 void addToList ( Communicator &comm )
292 {
293 rpOp_.addToList( comm );
294 }
295
297 template< class Communicator >
298 void removeFromList ( Communicator &comm )
299 {
300 rpOp_.removeFromList( comm );
301 }
302
304 template< class LoadBalancer >
305 void addToLoadBalancer ( LoadBalancer& lb )
306 {
307 rpOp_.addToLoadBalancer( lb );
308 }
309
310 void initialize ()
311 {
312 adaptiveFunction_.assign( discreteFunction_ );
313 rpOp_.initialize();
314 initialized_ = true ;
315 }
316
317 void finalize ()
318 {
319 // only finalize if previously initialized
320 assert( initialized_ );
321 rpOp_.finalize();
322 discreteFunction_.assign( adaptiveFunction_ );
323 initialized_ = false ;
324 }
325
326 protected:
329
330 protected:
332 AdaptiveDiscreteFunctionType adaptiveFunction_;
333 AdaptiveRestrictProlongType rpOp_;
334
335 bool initialized_;
336 };
337
338 } // namespace Fem
339} // namespace Dune
340
341#endif // #if HAVE_PETSC
342
343#endif // #ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
const char * name()
SLList< T, A > & operator=(const SLList< T, A > &other)
std::ptrdiff_t index() const
virtual void operator()()=0
STL namespace.
GridImp::template Codim< cd >::Entity Entity
AssignVectorReference(Vector &vector)
Definition function/common/functor.hh:86
Vector & vector_
Definition function/common/functor.hh:97
DomainFieldType calcWeight(const Entity &father, const Entity &son) const
calculates the weight, i.e. (volume son)/(volume father)
Definition restrictprolonginterface.hh:139
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:176
BaseType::DomainFieldType DomainFieldType
Definition restrictprolonginterface.hh:171
void setFatherChildWeight(const DomainFieldType &weight) const
explicit set volume ratio of son and father
Definition restrictprolonginterface.hh:256
DefaultLocalRestrictProlong< DiscreteFunctionSpaceType > LocalRestrictProlongType
Definition restrictprolonginterface.hh:223
void addToList(Communicator &comm, const Operation &op)
add discrete function to communicator with given unpack operation
Definition restrictprolonginterface.hh:347
DiscreteFunctionType & discreteFunction_
Definition restrictprolonginterface.hh:377
void removeFromList(Communicator &comm)
remove discrete function from communicator
Definition restrictprolonginterface.hh:363
DiscreteFunction DiscreteFunctionType
Definition restrictprolonginterface.hh:212
RestrictProlongDefault(const RestrictProlongDefault &other)
Definition restrictprolonginterface.hh:225
BaseType::DomainFieldType DomainFieldType
Definition restrictprolonginterface.hh:214
void prolongLocal(const Entity &father, const Entity &son, bool initialize) const
prolong data to children
Definition restrictprolonginterface.hh:311
DiscreteFunctionType::GridPartType GridPartType
Definition restrictprolonginterface.hh:221
void addToLoadBalancer(LoadBalancer &lb)
add discrete function to load balancer
Definition restrictprolonginterface.hh:371
void restrictLocal(const Entity &father, const Entity &son, bool initialize) const
restrict data to father
Definition restrictprolonginterface.hh:263
DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
Definition restrictprolonginterface.hh:216
discrete function space
T move(T... args)