DUNE-FEM (unstable)

petscdiscretefunction.hh
1#ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
2#define DUNE_FEM_PETSCDISCRETEFUNCTION_HH
3
4#include <memory>
5#include <string>
6#include <utility>
7
8#include <dune/fem/function/blockvectorfunction.hh>
9#include <dune/fem/function/adaptivefunction.hh>
10#include <dune/fem/function/vectorfunction.hh>
11
12#if HAVE_PETSC
13
15#include <dune/fem/common/stackallocator.hh>
16#include <dune/fem/function/common/discretefunction.hh>
17#include <dune/fem/function/common/functor.hh>
18#include <dune/fem/function/localfunction/mutable.hh>
19#include <dune/fem/misc/petsc/petscvector.hh>
20
21#include <dune/fem/space/common/restrictprolonginterface.hh>
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
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 ( const bool communicate = true )
184 {
185 BaseType :: template endAssemble< AssembleOperation > ();
186 dofVector().endAssemble( communicate );
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
235 typedef typename BaseType::DomainFieldType DomainFieldType;
236
237 typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
238 typedef typename DiscreteFunctionType::LocalFunctionType LocalFunctionType;
239 typedef typename DiscreteFunctionType::GridPartType GridPartType;
240
241 typedef DefaultLocalRestrictProlong< DiscreteFunctionSpaceType > LocalRestrictProlongType;
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
251 RestrictProlongDefault ( const RestrictProlongDefault& other )
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:
327
328 protected:
329 DiscreteFunctionType& discreteFunction_;
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
discrete function space
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
BaseType::GridPartType GridPartType
type of the underlying grid part
Definition: discretefunction.hh:609
void setFatherChildWeight(const DomainFieldType &weight) const
explicit set volume ratio of son and father
Definition: restrictprolonginterface.hh:256
void addToList(Communicator &comm, const Operation &op)
add discrete function to communicator with given unpack operation
Definition: restrictprolonginterface.hh:347
void removeFromList(Communicator &comm)
remove discrete function from communicator
Definition: restrictprolonginterface.hh:363
void prolongLocal(const Entity &father, const Entity &son, bool initialize) const
prolong data to children
Definition: restrictprolonginterface.hh:311
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
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
DomainFieldType calcWeight(const Entity &father, const Entity &son) const
calculates the weight, i.e. (volume son)/(volume father)
Definition: restrictprolonginterface.hh:139
forward declaration
Definition: discretefunction.hh:51
TupleDiscreteFunctionSpace< typename DiscreteFunctions::DiscreteFunctionSpaceType ... > DiscreteFunctionSpaceType
type for the discrete function space this function lives in
Definition: discretefunction.hh:69
This file implements a dense vector with a dynamic size.
Dune namespace.
Definition: alignedallocator.hh:13
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)