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