DUNE PDELab (2.7)

vector.hh
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
9#include <dune/typetree/typetree.hh>
10
11#include <dune/pdelab/backend/interface.hh>
13#include <dune/pdelab/backend/common/uncachedvectorview.hh>
14#include <dune/pdelab/backend/common/aliasedvectorview.hh>
15#include <dune/pdelab/backend/istl/descriptors.hh>
16#include <dune/pdelab/backend/istl/vectorhelpers.hh>
17#include <dune/pdelab/backend/istl/vectoriterator.hh>
18#include <dune/pdelab/gridfunctionspace/gridfunctionspace.hh>
19#include <dune/pdelab/gridfunctionspace/lfsindexcache.hh>
20#include <dune/pdelab/gridfunctionspace/tags.hh>
21
22namespace Dune {
23 namespace PDELab {
24 namespace ISTL {
25
26 template<typename GFS, typename C>
27 class BlockVector
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
46 typedef ISTL::vector_iterator<C> iterator;
47 typedef ISTL::vector_iterator<const C> const_iterator;
48
49
50 template<typename LFSCache>
51 using LocalView = UncachedVectorView<BlockVector,LFSCache>;
52
53 template<typename LFSCache>
54 using ConstLocalView = ConstUncachedVectorView<const BlockVector,LFSCache>;
55
56 template<typename LFSCache>
57 using AliasedLocalView = AliasedVectorView<BlockVector,LFSCache>;
58
59 template<typename LFSCache>
60 using ConstAliasedLocalView = ConstAliasedVectorView<const BlockVector,LFSCache>;
61
62 BlockVector(const BlockVector& rhs)
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
70 BlockVector(BlockVector&& rhs)
71 : _gfs(rhs._gfs)
72 , _container(std::move(rhs._container))
73 {}
74
75 BlockVector (std::shared_ptr<const GFS> gfs, Backend::attached_container = Backend::attached_container())
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
108 BlockVector (const GFS& gfs, Backend::attached_container tag = Backend::attached_container())
109 : BlockVector(Dune::stackobject_to_shared_ptr(gfs), tag)
110 {}
111
113 BlockVector(const GFS& gfs, Backend::unattached_container tag)
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
167 BlockVector& operator= (const BlockVector& r)
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
182 BlockVector& operator= (const E& e)
183 {
184 (*_container)=e;
185 return *this;
186 }
187
188 BlockVector& operator*= (const E& e)
189 {
190 (*_container)*=e;
191 return *this;
192 }
193
194
195 BlockVector& operator+= (const E& e)
196 {
197 (*_container)+=e;
198 return *this;
199 }
200
201 BlockVector& operator+= (const BlockVector& e)
202 {
203 (*_container)+= e.native();
204 return *this;
205 }
206
207 BlockVector& operator-= (const BlockVector& e)
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
223 E& operator[](const ContainerIndex& ci)
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
294 iterator begin()
295 {
296 return iterator(*_container,false);
297 }
298
299
300 const_iterator begin() const
301 {
302 return const_iterator(*_container,false);
303 }
304
305 iterator end()
306 {
307 return iterator(*_container,true);
308 }
309
310
311 const_iterator end() const
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
Various tags for influencing backend behavior.
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Implements a vector constructed from a given type representing a field and a compile-time given size.
auto dot(const A &a, const B &b) -> typename std::enable_if<!IsVector< A >::value &&!std::is_same< typename FieldTraits< A >::field_type, typename FieldTraits< A >::real_type > ::value, decltype(conj(a) *b)>::type
computes the dot product for fundamental data types according to Petsc's VectDot function: dot(a,...
Definition: dotproduct.hh:40
Dune namespace.
Definition: alignedallocator.hh:14
shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:75
STL namespace.
This file implements the class shared_ptr (a reference counting pointer), for those systems that don'...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)