dune-fem  2.4.1-rc
petscdiscretefunction/petscdiscretefunction.hh
Go to the documentation of this file.
1 // vim: set expandtab ts=2 sw=2 sts=2:
2 #ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
3 #define DUNE_FEM_PETSCDISCRETEFUNCTION_HH
4 
5 #include <string>
6 #include <algorithm>
7 #include <map>
8 #include <vector>
9 #include <iterator>
10 #include <utility>
11 
12 
13 #if HAVE_PETSC
14 
19 
20 
21 #include <dune/common/fvector.hh>
22 #include <dune/common/dynvector.hh>
23 
28 
29 #include <dune/common/shared_ptr.hh>
30 
31 namespace Dune
32 {
33 
34  namespace Fem
35  {
36 
37 
38  /* ==============================
39  * forward declarations
40  */
41  template< class DiscreteFunctionSpace > class PetscDiscreteFunction;
42 
43  // AssignVectorReference
44  // ---------------------
45 
46  template< class DofProxy, class Allocator >
47  struct AssignVectorReference< Dune::DynamicVector< DofProxy, Allocator > >
48  {
49  // we need to overload this
50  typedef Dune::DynamicVector< DofProxy, Allocator > Vector;
51 
52  AssignVectorReference ( Vector &vector )
53  : vector_( vector )
54  {}
55 
56  void operator() ( const std::size_t index, DofProxy value )
57  {
58  vector_[ index ].assign( value );
59  }
60 
61  protected:
62  Vector &vector_;
63  };
64 
65  // Internal Forward Declaration
66  //-----------------------------
67 
68  template <class DiscreteFunctionSpace>
69  class PetscDiscreteFunction;
70 
77  template< typename DiscreteFunctionSpace >
78  struct DiscreteFunctionTraits< PetscDiscreteFunction< DiscreteFunctionSpace > >
79  : public DefaultDiscreteFunctionTraits< DiscreteFunctionSpace, PetscVector< DiscreteFunctionSpace > >
80  {
81  typedef PetscVector< DiscreteFunctionSpace > DofVectorType;
82  typedef PetscDiscreteFunction< DiscreteFunctionSpace > DiscreteFunctionType;
83 
84  typedef typename DofVectorType::DofBlockType DofBlockType;
85  typedef typename DofBlockType::DofProxy DofProxyType;
86 
87  typedef ThreadSafeValue< UninitializedObjectStack > LocalDofVectorStackType;
88  typedef StackAllocator< DofProxyType, LocalDofVectorStackType* > LocalDofVectorAllocatorType;
89  typedef Dune::DynamicVector< DofProxyType, LocalDofVectorAllocatorType > LocalDofVectorType;
90 
91  typedef MutableLocalFunction< DiscreteFunctionType > LocalFunctionType;
92  };
93 
94  /* ========================================
95  * class PetscDiscreteFunction
96  * =======================================
97  */
98 
99  /* @addtogroup PetscDiscreFunction
100  * \brief Discrete Function implementation based on the PETsc vector.
101  *
102  */
103 
104  // PetscDiscreteFunction
105  //----------------------
106 
107  template <class DiscreteFunctionSpace>
108  class PetscDiscreteFunction
109  : public DiscreteFunctionDefault< PetscDiscreteFunction< DiscreteFunctionSpace > >
110  {
111  typedef PetscDiscreteFunction< DiscreteFunctionSpace > ThisType;
112  typedef DiscreteFunctionDefault< ThisType > BaseType;
113 
114  public:
115  typedef typename BaseType :: DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
116  typedef typename BaseType :: DofVectorType DofVectorType;
117 
118  using BaseType::assign;
119 
120  PetscDiscreteFunction( const std::string &name,
121  const DiscreteFunctionSpaceType &space )
122  : BaseType( name, space ),
123  memObject_(),
124  dofVector_( allocateDofStorage( space ) )
125  {
126  }
127 
128  PetscDiscreteFunction( const std::string &name,
129  const DiscreteFunctionSpaceType &space,
130  DofVectorType& dofVector )
131  : BaseType( name, space ),
132  memObject_(),
133  dofVector_( dofVector )
134  {
135  }
136 
137  PetscDiscreteFunction( const PetscDiscreteFunction& other )
138  : BaseType( "copy of " + other.name(), other.space() ),
139  memObject_(),
140  dofVector_( allocateDofStorage( other.space() ) )
141  {
142  assign( other );
143  }
144 
147  void enableDofCompression ()
148  {
149  if( memObject_ )
150  memObject_->enableDofCompression();
151  }
152 
154  void communicate ()
155  {
156  dofVector().communicateNow();
157  }
158 
160  DofVectorType& dofVector() { return dofVector_; }
162  const DofVectorType& dofVector() const { return dofVector_; }
163 
165  const Vec* petscVec () const { return dofVector().getVector(); }
166 
168  Vec* petscVec () { return dofVector().getVector(); }
169 
170  protected:
171  typedef typename DiscreteFunctionSpaceType :: BlockMapperType BlockMapperType;
172  typedef PetscManagedDofStorage< DiscreteFunctionSpaceType, BlockMapperType > PetscManagedDofStorageType;
173 
174  // allocate managed dof storage
175  DofVectorType& allocateDofStorage ( const DiscreteFunctionSpaceType &space )
176  {
177  std::string name("deprecated");
178 
179  memObject_.reset( new PetscManagedDofStorageType( space, space.blockMapper(), name ) );
180 
181  return memObject_->getArray();
182  }
183 
184  // pointer to allocated DofVector
185  std::unique_ptr< PetscManagedDofStorageType > memObject_;
186 
187  // dof vector impl
188  DofVectorType& dofVector_;
189  };
190 
191  } // namespace Fem
192 
193 } // namespace Dune
194 
195 #endif // #if HAVE_PETSC
196 
197 #endif // #ifndef DUNE_FEM_PETSCDISCRETEFUNCTION_HH
Vector & vector_
Definition: function/common/functor.hh:94
Definition: coordinate.hh:4
discrete function space
AssignVectorReference(Vector &vector)
Definition: function/common/functor.hh:83
void operator()(const std::size_t index, Value &&value) const
Definition: function/common/functor.hh:88