dune-pdelab  2.6.0-rc1
istl/vector.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_PDELAB_BACKEND_ISTL_VECTOR_HH
4 #define DUNE_PDELAB_BACKEND_ISTL_VECTOR_HH
5 
6 #include <dune/common/fvector.hh>
7 #include <dune/istl/bvector.hh>
8 #include <dune/typetree/typetree.hh>
9 
20 
21 namespace Dune {
22  namespace PDELab {
23  namespace ISTL {
24 
25  template<typename GFS, typename C>
27  : public Backend::impl::Wrapper<C>
28  {
29 
30  friend Backend::impl::Wrapper<C>;
31 
32  public:
33  typedef typename C::field_type ElementType;
34  typedef ElementType E;
35  typedef C Container;
36  typedef GFS GridFunctionSpace;
37  typedef typename Container::field_type field_type;
38  typedef typename Container::block_type block_type;
39  typedef typename Container::size_type size_type;
40 
41  using value_type = E;
42 
43  typedef typename GFS::Ordering::Traits::ContainerIndex ContainerIndex;
44 
47 
48 
49  template<typename LFSCache>
51 
52  template<typename LFSCache>
54 
55  template<typename LFSCache>
57 
58  template<typename LFSCache>
60 
62  : _gfs(rhs._gfs)
63  , _container(std::make_shared<Container>(_gfs.ordering().blockCount()))
64  {
65  ISTL::dispatch_vector_allocation(_gfs.ordering(),*_container,typename GFS::Ordering::ContainerAllocationTag());
66  (*_container) = rhs.native();
67  }
68 
70  : _gfs(rhs._gfs)
71  , _container(std::move(rhs._container))
72  {}
73 
75  : _gfs(gfs)
76  , _container(std::make_shared<Container>(gfs.ordering().blockCount()))
77  {
78  ISTL::dispatch_vector_allocation(gfs.ordering(),*_container,typename GFS::Ordering::ContainerAllocationTag());
79  }
80 
83  : _gfs(gfs)
84  {}
85 
91  BlockVector (const GFS& gfs, Container& container)
92  : _gfs(gfs)
93  , _container(stackobject_to_shared_ptr(container))
94  {
95  _container->resize(gfs.ordering().blockCount());
96  ISTL::dispatch_vector_allocation(gfs.ordering(),*_container,typename GFS::Ordering::ContainerAllocationTag());
97  }
98 
99  BlockVector (const GFS& gfs, const E& e)
100  : _gfs(gfs)
101  , _container(std::make_shared<Container>(gfs.ordering().blockCount()))
102  {
103  ISTL::dispatch_vector_allocation(gfs.ordering(),*_container,typename GFS::Ordering::ContainerAllocationTag());
104  (*_container)=e;
105  }
106 
107  void detach()
108  {
109  _container.reset();
110  }
111 
112  template<typename LFSCache>
113  value_type* data(const LFSCache& lfs_cache)
114  {
115  return &((*this)[lfs_cache.containerIndex(0)]);
116  }
117 
118  template<typename LFSCache>
119  const value_type* data(const LFSCache& lfs_cache) const
120  {
121  return &((*this)[lfs_cache.containerIndex(0)]);
122  }
123 
124  void attach(std::shared_ptr<Container> container)
125  {
126  _container = container;
127  }
128 
129  bool attached() const
130  {
131  return bool(_container);
132  }
133 
134  const std::shared_ptr<Container>& storage() const
135  {
136  return _container;
137  }
138 
139  size_type N() const
140  {
141  return _container->N();
142  }
143 
145  {
146  if (this == &r)
147  return *this;
148  if (attached())
149  {
150  (*_container) = r.native();
151  }
152  else
153  {
154  _container = std::make_shared<Container>(r.native());
155  }
156  return *this;
157  }
158 
160  {
161  (*_container)=e;
162  return *this;
163  }
164 
166  {
167  (*_container)*=e;
168  return *this;
169  }
170 
171 
173  {
174  (*_container)+=e;
175  return *this;
176  }
177 
179  {
180  (*_container)+= e.native();
181  return *this;
182  }
183 
185  {
186  (*_container)-= e.native();
187  return *this;
188  }
189 
190  block_type& block(std::size_t i)
191  {
192  return (*_container)[i];
193  }
194 
195  const block_type& block(std::size_t i) const
196  {
197  return (*_container)[i];
198  }
199 
201  {
202  return ISTL::access_vector_element(ISTL::container_tag(*_container),*_container,ci,ci.size()-1);
203  }
204 
205  const E& operator[](const ContainerIndex& ci) const
206  {
207  return ISTL::access_vector_element(ISTL::container_tag(*_container),*_container,ci,ci.size()-1);
208  }
209 
210  typename Dune::template FieldTraits<E>::real_type two_norm2() const
211  {
212  return _container->two_norm2();
213  }
214 
215  typename Dune::template FieldTraits<E>::real_type two_norm() const
216  {
217  return _container->two_norm();
218  }
219 
220  typename Dune::template FieldTraits<E>::real_type one_norm() const
221  {
222  return _container->one_norm();
223  }
224 
225  typename Dune::template FieldTraits<E>::real_type infinity_norm() const
226  {
227  return _container->infinity_norm();
228  }
229 
230  E operator*(const BlockVector& y) const
231  {
232  return (*_container)*y.native();
233  }
234 
235  E dot(const BlockVector& y) const
236  {
237  return _container->dot(y.native());
238  }
239 
240  BlockVector& axpy(const E& a, const BlockVector& y)
241  {
242  _container->axpy(a, y.native());
243  return *this;
244  }
245 
246  private:
247 
248  // for debugging and AMG access
249  Container& native ()
250  {
251  return *_container;
252  }
253 
254  const Container& native () const
255  {
256  return *_container;
257  }
258 
259  public:
260 
261  operator Container&()
262  {
263  return *_container;
264  }
265 
266  operator const Container&() const
267  {
268  return *_container;
269  }
270 
272  {
273  return iterator(*_container,false);
274  }
275 
276 
278  {
279  return const_iterator(*_container,false);
280  }
281 
283  {
284  return iterator(*_container,true);
285  }
286 
287 
289  {
290  return const_iterator(*_container,true);
291  }
292 
293  size_t flatsize() const
294  {
295  return _container->dim();
296  }
297 
298  const GFS& gridFunctionSpace() const
299  {
300  return _gfs;
301  }
302 
303  private:
304  const GFS& _gfs;
305  std::shared_ptr<Container> _container;
306  };
307 
308 #ifndef DOXYGEN
309 
310  // helper struct invoking the GFS tree -> ISTL vector reduction
311  template<typename GFS, typename E>
312  struct BlockVectorSelectorHelper
313  {
314 
315  typedef typename TypeTree::AccumulateType<
316  GFS,
317  ISTL::vector_creation_policy<E>
318  >::type vector_descriptor;
319 
320  typedef BlockVector<GFS,typename vector_descriptor::vector_type> Type;
321 
322  };
323 
324 #endif // DOXYGEN
325 
326  // can't have the closing of the namespace inside the #ifndef DOXYGEN block
327  } // namespace ISTL
328 
329 #ifndef DOXYGEN
330 
331  namespace Backend {
332  namespace impl {
333 
334  template<Dune::PDELab::ISTL::Blocking blocking, std::size_t block_size, typename GFS, typename E>
335  struct BackendVectorSelectorHelper<ISTL::VectorBackend<blocking,block_size>, GFS, E>
336  : public ISTL::BlockVectorSelectorHelper<GFS,E>
337  {};
338 
339  } // namespace impl
340  } // namespace Backend
341 
342 #endif // DOXYGEN
343 
344  } // namespace PDELab
345 } // namespace Dune
346 
347 #endif // DUNE_PDELAB_BACKEND_ISTL_VECTOR_HH
BlockVector(const GFS &gfs, Container &container)
Constructs an BlockVector for an explicitly given vector object.
Definition: istl/vector.hh:91
Dune::template FieldTraits< E >::real_type two_norm() const
Definition: istl/vector.hh:215
Container::size_type size_type
Definition: istl/vector.hh:39
const_iterator begin() const
Definition: istl/vector.hh:277
bool attached() const
Definition: istl/vector.hh:129
ISTL::vector_iterator< const C > const_iterator
Definition: istl/vector.hh:46
E operator *(const BlockVector &y) const
Definition: istl/vector.hh:230
size_t flatsize() const
Definition: istl/vector.hh:293
BlockVector & axpy(const E &a, const BlockVector &y)
Definition: istl/vector.hh:240
BlockVector(const GFS &gfs, Backend::attached_container=Backend::attached_container())
Definition: istl/vector.hh:74
Various tags for influencing backend behavior.
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
const Entity & e
Definition: localfunctionspace.hh:120
iterator begin()
Definition: istl/vector.hh:271
BlockVector & operator=(const BlockVector &r)
Definition: istl/vector.hh:144
Definition: aliasedvectorview.hh:14
E & operator[](const ContainerIndex &ci)
Definition: istl/vector.hh:200
BlockVector(const BlockVector &rhs)
Definition: istl/vector.hh:61
value_type * data(const LFSCache &lfs_cache)
Definition: istl/vector.hh:113
Dune::template FieldTraits< E >::real_type infinity_norm() const
Definition: istl/vector.hh:225
Container::field_type field_type
Definition: istl/vector.hh:37
BlockVector & operator-=(const BlockVector &e)
Definition: istl/vector.hh:184
E value_type
Definition: istl/vector.hh:41
BlockVector(BlockVector &&rhs)
Definition: istl/vector.hh:69
BlockVector & operator+=(const E &e)
Definition: istl/vector.hh:172
Definition: istl/vector.hh:26
void attach(std::shared_ptr< Container > container)
Definition: istl/vector.hh:124
const_iterator end() const
Definition: istl/vector.hh:288
const block_type & block(std::size_t i) const
Definition: istl/vector.hh:195
Container::block_type block_type
Definition: istl/vector.hh:38
Tag for requesting a vector or matrix container without a pre-attached underlying object.
Definition: backend/common/tags.hh:23
void detach()
Definition: istl/vector.hh:107
block_type & block(std::size_t i)
Definition: istl/vector.hh:190
C::field_type ElementType
Definition: istl/vector.hh:33
Definition: uncachedvectorview.hh:134
BlockVector & operator *=(const E &e)
Definition: istl/vector.hh:165
ElementType E
Definition: istl/vector.hh:34
ISTL::vector_iterator< C > iterator
Definition: istl/vector.hh:45
Definition: aliasedvectorview.hh:112
BlockVector(const GFS &gfs, const E &e)
Definition: istl/vector.hh:99
GFS GridFunctionSpace
Definition: istl/vector.hh:36
const std::shared_ptr< Container > & storage() const
Definition: istl/vector.hh:134
const GFS & gridFunctionSpace() const
Definition: istl/vector.hh:298
C Container
Definition: istl/vector.hh:35
Definition: uncachedvectorview.hh:14
Dune::template FieldTraits< E >::real_type two_norm2() const
Definition: istl/vector.hh:210
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/common/tags.hh:27
const value_type * data(const LFSCache &lfs_cache) const
Definition: istl/vector.hh:119
size_type N() const
Definition: istl/vector.hh:139
const E & operator[](const ContainerIndex &ci) const
Definition: istl/vector.hh:205
BlockVector(const GFS &gfs, Backend::unattached_container)
Creates an BlockVector without allocating an underlying ISTL vector.
Definition: istl/vector.hh:82
GFS::Ordering::Traits::ContainerIndex ContainerIndex
Definition: istl/vector.hh:43
E dot(const BlockVector &y) const
Definition: istl/vector.hh:235
Dune::template FieldTraits< E >::real_type one_norm() const
Definition: istl/vector.hh:220
iterator end()
Definition: istl/vector.hh:282
Definition: vectoriterator.hh:111
tags::container< T >::type container_tag(const T &)
Gets instance of container tag associated with T.
Definition: backend/istl/tags.hh:246