DUNE PDELab (git)

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#include <dune/functions/backends/istlvectorbackend.hh>
11
12#include <dune/pdelab/backend/interface.hh>
14#include <dune/pdelab/backend/common/uncachedvectorview.hh>
15#include <dune/pdelab/backend/common/aliasedvectorview.hh>
16#include <dune/pdelab/backend/istl/descriptors.hh>
17#include <dune/pdelab/backend/istl/vectorhelpers.hh>
18#include <dune/pdelab/backend/istl/vectoriterator.hh>
19#include <dune/pdelab/gridfunctionspace/gridfunctionspace.hh>
20#include <dune/pdelab/gridfunctionspace/lfsindexcache.hh>
21#include <dune/pdelab/gridfunctionspace/tags.hh>
22
23namespace Dune {
24 namespace PDELab {
25 namespace ISTL {
26
27 template<typename GFS, typename C>
28 class BlockVector
29 : public Backend::impl::Wrapper<C>
30 {
31
32 friend Backend::impl::Wrapper<C>;
33
34 public:
35 typedef typename C::field_type ElementType;
36 typedef ElementType E;
37 typedef C Container;
38 typedef GFS GridFunctionSpace;
39 typedef typename Container::field_type field_type;
40 typedef typename Container::block_type block_type;
41 typedef typename Container::size_type size_type;
42
43 using value_type = E;
44
45 typedef typename GFS::Ordering::Traits::ContainerIndex ContainerIndex;
46
47 typedef ISTL::vector_iterator<C> iterator;
48 typedef ISTL::vector_iterator<const C> const_iterator;
49
50
51 template<typename LFSCache>
52 using LocalView = UncachedVectorView<BlockVector,LFSCache>;
53
54 template<typename LFSCache>
55 using ConstLocalView = ConstUncachedVectorView<const BlockVector,LFSCache>;
56
57 template<typename LFSCache>
58 using AliasedLocalView = AliasedVectorView<BlockVector,LFSCache>;
59
60 template<typename LFSCache>
61 using ConstAliasedLocalView = ConstAliasedVectorView<const BlockVector,LFSCache>;
62
63 BlockVector(const BlockVector& rhs)
64 : _gfs(rhs._gfs)
65 , _container(std::make_shared<Container>(_gfs->ordering().blockCount()))
66 {
67 resize();
68 (*_container) = rhs.native();
69 }
70
71 BlockVector(BlockVector&& rhs)
72 : _gfs(rhs._gfs)
73 , _container(std::move(rhs._container))
74 {}
75
76 BlockVector (std::shared_ptr<const GFS> gfs, Backend::attached_container = Backend::attached_container())
77 : _gfs(gfs)
78 , _container(std::make_shared<Container>())
79 {
80 resize();
81 }
82
84 BlockVector(std::shared_ptr<const GFS> gfs, Backend::unattached_container)
85 : _gfs(gfs)
86 {}
87
93 BlockVector (std::shared_ptr<const GFS> gfs, Container& container)
94 : _gfs(gfs)
95 , _container(stackobject_to_shared_ptr(container))
96 {
97 resize();
98 }
99
100 BlockVector (std::shared_ptr<const GFS> gfs, const E& e)
101 : _gfs(gfs)
102 , _container(std::make_shared<Container>())
103 {
104 resize();
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
137 void resize()
138 {
139 assert(_gfs);
140 auto b = Functions::istlVectorBackend(*_container);
141 SizeProviderAdapter size_provider{_gfs->orderingStorage()};
142 static_assert(decltype(size_provider)::ContainerIndexOrder == MultiIndexOrder::Outer2Inner);
143 b.resize(size_provider);
144 }
145
146 void detach()
147 {
148 _container.reset();
149 }
150
151 template<typename LFSCache>
152 value_type* data(const LFSCache& lfs_cache)
153 {
154 return &((*this)[lfs_cache.containerIndex(0)]);
155 }
156
157 template<typename LFSCache>
158 const value_type* data(const LFSCache& lfs_cache) const
159 {
160 return &((*this)[lfs_cache.containerIndex(0)]);
161 }
162
163 void attach(std::shared_ptr<Container> container)
164 {
165 _container = container;
166 }
167
168 bool attached() const
169 {
170 return bool(_container);
171 }
172
173 const std::shared_ptr<Container>& storage() const
174 {
175 return _container;
176 }
177
178 size_type N() const
179 {
180 return _container->N();
181 }
182
183 BlockVector& operator= (const BlockVector& r)
184 {
185 if (this == &r)
186 return *this;
187 if (attached())
188 {
189 (*_container) = r.native();
190 }
191 else
192 {
193 _container = std::make_shared<Container>(r.native());
194 }
195 return *this;
196 }
197
198 BlockVector& operator= (const E& e)
199 {
200 (*_container)=e;
201 return *this;
202 }
203
204 BlockVector& operator*= (const E& e)
205 {
206 (*_container)*=e;
207 return *this;
208 }
209
210
211 BlockVector& operator+= (const E& e)
212 {
213 (*_container)+=e;
214 return *this;
215 }
216
217 BlockVector& operator+= (const BlockVector& e)
218 {
219 (*_container)+= e.native();
220 return *this;
221 }
222
223 BlockVector& operator-= (const BlockVector& e)
224 {
225 (*_container)-= e.native();
226 return *this;
227 }
228
229 block_type& block(std::size_t i)
230 {
231 return (*_container)[i];
232 }
233
234 const block_type& block(std::size_t i) const
235 {
236 return (*_container)[i];
237 }
238
239 E& operator[](const ContainerIndex& ci)
240 {
241 return ISTL::access_vector_element(ISTL::container_tag(*_container),*_container,ci,ci.size()-1);
242 }
243
244 const E& operator[](const ContainerIndex& ci) const
245 {
246 return ISTL::access_vector_element(ISTL::container_tag(*_container),*_container,ci,ci.size()-1);
247 }
248
249 typename Dune::template FieldTraits<E>::real_type two_norm2() const
250 {
251 return _container->two_norm2();
252 }
253
254 typename Dune::template FieldTraits<E>::real_type two_norm() const
255 {
256 return _container->two_norm();
257 }
258
259 typename Dune::template FieldTraits<E>::real_type one_norm() const
260 {
261 return _container->one_norm();
262 }
263
264 typename Dune::template FieldTraits<E>::real_type infinity_norm() const
265 {
266 return _container->infinity_norm();
267 }
268
269 E operator*(const BlockVector& y) const
270 {
271 return (*_container)*y.native();
272 }
273
274 E dot(const BlockVector& y) const
275 {
276 return _container->dot(y.native());
277 }
278
279 BlockVector& axpy(const E& a, const BlockVector& y)
280 {
281 _container->axpy(a, y.native());
282 return *this;
283 }
284
285 private:
286
287 // for debugging and AMG access
288 Container& native ()
289 {
290 return *_container;
291 }
292
293 const Container& native () const
294 {
295 return *_container;
296 }
297
298 public:
299
300 operator Container&()
301 {
302 return *_container;
303 }
304
305 operator const Container&() const
306 {
307 return *_container;
308 }
309
310 iterator begin()
311 {
312 return iterator(*_container,false);
313 }
314
315
316 const_iterator begin() const
317 {
318 return const_iterator(*_container,false);
319 }
320
321 iterator end()
322 {
323 return iterator(*_container,true);
324 }
325
326
327 const_iterator end() const
328 {
329 return const_iterator(*_container,true);
330 }
331
332 size_t flatsize() const
333 {
334 return _container->dim();
335 }
336
337 const GFS& gridFunctionSpace() const
338 {
339 return *_gfs;
340 }
341
342 std::shared_ptr<const GFS> gridFunctionSpaceStorage() const
343 {
344 return _gfs;
345 }
346
347 private:
348 std::shared_ptr<const GFS> _gfs;
349 std::shared_ptr<Container> _container;
350 };
351
352#ifndef DOXYGEN
353
354 // helper struct invoking the GFS tree -> ISTL vector reduction
355 template<typename GFS, typename E>
356 struct BlockVectorSelectorHelper
357 {
358
359 typedef typename TypeTree::AccumulateType<
360 GFS,
361 ISTL::vector_creation_policy<E>
362 >::type vector_descriptor;
363
364 typedef BlockVector<GFS,typename vector_descriptor::vector_type> Type;
365
366 };
367
368#endif // DOXYGEN
369
370 // can't have the closing of the namespace inside the #ifndef DOXYGEN block
371 } // namespace ISTL
372
373#ifndef DOXYGEN
374
375 namespace Backend {
376 namespace impl {
377
378 template<Dune::PDELab::ISTL::Blocking blocking, std::size_t block_size, typename GFS, typename E>
379 struct BackendVectorSelectorHelper<ISTL::VectorBackend<blocking,block_size>, GFS, E>
380 : public ISTL::BlockVectorSelectorHelper<GFS,E>
381 {};
382
383 } // namespace impl
384 } // namespace Backend
385
386#endif // DOXYGEN
387
388 } // namespace PDELab
389} // namespace Dune
390
391#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< IsNumber< A >::value &&!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:42
auto istlVectorBackend(Vector &v)
Return a vector backend wrapping non-const ISTL like containers.
Definition: istlvectorbackend.hh:346
SizeProviderAdapter(const std::shared_ptr< const Ordering > &ordering) -> SizeProviderAdapter< typename Ordering::Traits::SizeType, typename Ordering::Traits::ContainerIndex, Ordering::Traits::ContainerIndexOrder >
template deduction guide for orderings
@ Outer2Inner
indices are ordered from outer to inner container: {outer,...,inner}
Dune namespace.
Definition: alignedallocator.hh:13
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:72
STL namespace.
This file implements several utilities related to std::shared_ptr.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)