1#ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
2#define DUNE_FEM_PETSCDISCRETEFUNCTION_HH
8#include <dune/fem/function/blockvectorfunction.hh>
9#include <dune/fem/function/adaptivefunction.hh>
10#include <dune/fem/function/vectorfunction.hh>
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>
21#include <dune/fem/space/common/restrictprolonginterface.hh>
28 template<
class DiscreteFunctionSpace >
29 class PetscDiscreteFunction;
31 template<
class DofProxy,
class Allocator >
32 struct AssignVectorReference<
Dune::DynamicVector< DofProxy, Allocator > >
37 AssignVectorReference ( Vector &vector )
41 void operator() (
const std::size_t index, DofProxy value )
const
43 vector_[ index ].assign( value );
58 template<
typename DiscreteFunctionSpace >
60 :
public DefaultDiscreteFunctionTraits< DiscreteFunctionSpace, PetscVector< DiscreteFunctionSpace > >
62 typedef PetscVector< DiscreteFunctionSpace > DofVectorType;
65 typedef typename DofVectorType::DofBlockType DofBlockType;
66 typedef typename DofBlockType::DofProxy DofProxyType;
69 typedef StackAllocator< DofProxyType, LocalDofVectorStackType* > LocalDofVectorAllocatorType;
80 template <
class DiscreteFunctionSpace>
81 class PetscDiscreteFunction
82 :
public DiscreteFunctionDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >
84 typedef PetscDiscreteFunction< DiscreteFunctionSpace > ThisType;
85 typedef DiscreteFunctionDefault< ThisType > BaseType;
88 typedef typename BaseType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
89 typedef typename BaseType :: DofVectorType DofVectorType;
99 PetscDiscreteFunction(
const std::string& name,
100 const DiscreteFunctionSpaceType& space )
101 : BaseType( name, space ),
103 dofVector_( allocateDofStorage( space ) )
112 PetscDiscreteFunction(
const std::string& name,
113 const DiscreteFunctionSpaceType& space,
114 DofVectorType& dofVector )
115 : BaseType( name, space ),
117 dofVector_( dofVector )
121 PetscDiscreteFunction(
const ThisType& other )
122 : BaseType(
"copy of " + other.name(), other.space() ),
124 dofVector_( allocateDofStorage( other.space() ) )
130 PetscDiscreteFunction( ThisType&& other )
131 : BaseType( static_cast< BaseType && >( other ) ),
132 memObject_(
std::move( other.memObject_ ) ),
133 dofVector_( other.dofVector_ )
136 PetscDiscreteFunction () =
delete;
137 ThisType& operator= (
const ThisType& ) =
delete;
138 ThisType& operator= ( ThisType&& ) =
delete;
141 void enableDofCompression ()
144 memObject_->enableDofCompression();
149 void assign(
const AdaptiveDiscreteFunction< DiscreteFunctionSpaceType > &g )
152 dofVector().assignVector( g.dofVector() );
157 template <
class Block >
158 void assign(
const ISTLBlockVectorDiscreteFunction< DiscreteFunctionSpaceType, Block > &g )
161 dofVector().assignVector( g.dofVector() );
166 template <
class Vector >
167 void assign(
const VectorDiscreteFunction< DiscreteFunctionSpaceType, Vector > &g )
170 dofVector().assignVector( g.dofVector() );
174 template<
class AssembleOperation >
175 void beginAssemble ()
177 BaseType :: template beginAssemble< AssembleOperation > ();
178 dofVector().beginAssemble();
182 template<
class AssembleOperation >
183 void endAssemble (
const bool communicate =
true )
185 BaseType :: template endAssemble< AssembleOperation > ();
186 dofVector().endAssemble( communicate );
190 DofVectorType& dofVector() {
return dofVector_; }
192 const DofVectorType& dofVector()
const {
return dofVector_; }
195 const Vec* petscVec ()
const {
return dofVector().getVector(); }
198 Vec* petscVec () {
return dofVector().getVector(); }
201 typedef typename DiscreteFunctionSpaceType :: BlockMapperType BlockMapperType;
202 typedef PetscManagedDofStorage< DiscreteFunctionSpaceType, BlockMapperType > PetscManagedDofStorageType;
205 DofVectorType& allocateDofStorage (
const DiscreteFunctionSpaceType &space )
207 memObject_.reset(
new PetscManagedDofStorageType( space, space.blockMapper() ) );
209 return memObject_->getArray();
213 std::unique_ptr< PetscManagedDofStorageType > memObject_;
216 DofVectorType& dofVector_;
220 template <
class DiscreteFunctionSpace>
222 :
public RestrictProlongInterfaceDefault< RestrictProlongTraits< RestrictProlongDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >,
223 typename DiscreteFunctionSpace::DomainFieldType > >
225 typedef PetscDiscreteFunction< DiscreteFunctionSpace > DiscreteFunction;
226 typedef RestrictProlongDefault< DiscreteFunction > ThisType;
227 typedef RestrictProlongInterfaceDefault< RestrictProlongTraits< ThisType, typename DiscreteFunction::DomainFieldType > > BaseType;
229 typedef AdaptiveDiscreteFunction< DiscreteFunctionSpace > AdaptiveDiscreteFunctionType;
230 typedef RestrictProlongDefault < AdaptiveDiscreteFunctionType > AdaptiveRestrictProlongType;
235 typedef typename BaseType::DomainFieldType DomainFieldType;
241 typedef DefaultLocalRestrictProlong< DiscreteFunctionSpaceType > LocalRestrictProlongType;
244 : discreteFunction_( discreteFunction ),
245 adaptiveFunction_( discreteFunction_.name()+
"-adaptive", discreteFunction_.space() ),
246 rpOp_( adaptiveFunction_ ),
247 initialized_( false )
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 )
261 rpOp_.setFatherChildWeight( weight );
265 template<
class Entity >
266 void restrictLocal (
const Entity &father,
const Entity &son,
bool initialize )
const
268 assert( initialized_ );
269 rpOp_.restrictLocal( father, son, initialize );
273 template<
class Entity >
274 void prolongLocal (
const Entity &father,
const Entity &son,
bool initialize )
const
276 assert( initialized_ );
277 rpOp_.prolongLocal( father, son, initialize );
281 template<
class Communicator,
class Operation >
282 void addToList ( Communicator &comm,
const Operation& op)
284 rpOp_.addToList( comm, op );
288 template<
class Communicator >
291 rpOp_.addToList( comm );
295 template<
class Communicator >
298 rpOp_.removeFromList( comm );
302 template<
class LoadBalancer >
305 rpOp_.addToLoadBalancer( lb );
310 adaptiveFunction_.assign( discreteFunction_ );
312 initialized_ = true ;
318 assert( initialized_ );
320 discreteFunction_.assign( adaptiveFunction_ );
321 initialized_ = false ;
330 AdaptiveDiscreteFunctionType adaptiveFunction_;
331 AdaptiveRestrictProlongType rpOp_;
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