DUNE PDELab (git)

localvector.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_GRIDFUNCTIONSPACE_LOCALVECTOR_HH
4#define DUNE_PDELAB_GRIDFUNCTIONSPACE_LOCALVECTOR_HH
5
6#include <vector>
7#include <algorithm>
8#include <functional>
9#include <numeric>
11
17namespace Dune {
18 namespace PDELab {
19
26 template<typename C>
28 {
29 public:
30
32 typedef C Container;
33
35 typedef typename Container::BaseContainer BaseContainer;
36
38 typedef typename Container::value_type value_type;
39
41 typedef typename Container::weight_type weight_type;
42
46
50 {
52 }
53
55 typedef typename Container::size_type size_type;
56
58
63 {
64 return _weight;
65 }
66
68
73 {
74 _weight = weight;
75 }
76
78 template<typename LFS>
79 void accumulate(const LFS& lfs, size_type n, value_type v)
80 {
81 _modified = true;
82 _container(lfs,n) += _weight * v;
83 }
84
86
90 template<typename LFS>
91 void rawAccumulate(const LFS& lfs, size_type n, value_type v)
92 {
93 _modified = true;
94 _container(lfs,n) += v;
95 }
96
99 : _container(container)
100 , _weight(weight)
101 , _modified(false)
102 {}
103
106 {
107 return _container.size();
108 }
109
111 bool modified() const
112 {
113 return _modified;
114 }
115
117
122 {
123 _modified = false;
124 }
125
128 {
129 _modified = true;
130 return _container;
131 }
132
134 const Container& container() const
135 {
136 return _container;
137 }
138
141 {
142 _modified = true;
143 return _container.base();
144 }
145
147 const BaseContainer& base() const
148 {
149 return _container.base();
150 }
151
153 auto data()
154 {
155 return _container.data();
156 }
157
159 auto data() const
160 {
161 return _container.data();
162 }
163
164 private:
165 C& _container;
166 weight_type _weight;
167 bool _modified;
168 };
169
170
172
182 template<typename T, typename LFSFlavorTag = AnySpaceTag, typename W = T>
184 {
185 public:
186
188 typedef std::vector<T> BaseContainer;
189
191 typedef typename BaseContainer::value_type value_type;
192 typedef value_type field_type;
193
195 typedef typename BaseContainer::size_type size_type;
196
198 typedef typename BaseContainer::reference reference;
199
201 typedef typename BaseContainer::const_reference const_reference;
202
204
208 typedef W weight_type;
209
212
215 {
216 return WeightedAccumulationView(*this,weight);
217 }
218
220
225 template<typename LFS>
226 reference operator()(const LFS& lfs, size_type i)
227 {
228 return _container[lfs.localIndex(i)];
229 }
230
232
237 template<typename LFS>
238 const_reference operator()(const LFS& lfs, size_type i) const
239 {
240 return _container[lfs.localIndex(i)];
241 }
242
244 auto data()
245 {
246 return _container.data();
247 }
248
250 auto data() const
251 {
252 return _container.data();
253 }
254
256 LocalVector& axpy(const value_type alpha, const LocalVector& other)
257 {
258 std::transform(_container.begin(),_container.end(),
259 other._container.begin(),
260 _container.begin(),
261 [=](const value_type &a,
262 const value_type &b) { return a + alpha*b;});
263 return *this;
264 }
265
268 {
269 std::fill(_container.begin(),_container.end(),v);
270 return *this;
271 }
272
275 {
276 std::transform(_container.begin(),_container.end(),
277 other._container.begin(),
278 _container.begin(),
279 std::plus<value_type>());
280 return *this;
281 }
282
285 {
286 using namespace std::placeholders;
287 std::transform(
288 _container.begin(),
289 _container.end(),
290 _container.begin(),
291 std::bind(std::multiplies<value_type>(),v,_1)
292 );
293 return *this;
294 }
295
297 value_type dot(const LocalVector& other) const
298 {
299 value_type dot_product=0.0;
300 dot_product = std::inner_product(_container.begin(),_container.end(),
301 other._container.begin(),
302 dot_product);
303 return dot_product;
304 }
305
308 {
309 using std::sqrt;
310 value_type nrm=0.0;
311 nrm = std::inner_product(_container.begin(),_container.end(),
312 _container.begin(),
313 nrm);
314 return sqrt(nrm);
315 }
316
319 {
320 return _container.size();
321 }
322
325 {
326 _container.resize(size);
327 }
328
330 void assign(size_type size, const T& value)
331 {
332 _container.assign(size,value);
333 }
334
337 {
338 return _container;
339 }
340
342 const BaseContainer& base() const
343 {
344 return _container;
345 }
346
349 {}
350
353 : _container(n)
354 {}
355
358 : _container(n,v)
359 {}
360
361 private:
362
363 BaseContainer _container;
364
365 };
366
367
368 template<typename C>
369 C& accessBaseContainer(C& c)
370 {
371 return c;
372 }
373
374 template<typename T, typename Tag, typename W>
375 typename LocalVector<T,Tag,W>::BaseContainer& accessBaseContainer(LocalVector<T,Tag,W>& c)
376 {
377 return c.base();
378 }
379
380 template<typename C>
381 typename WeightedVectorAccumulationView<C>::BaseContainer& accessBaseContainer
382 (WeightedVectorAccumulationView<C>& c)
383 {
384 return c.base();
385 }
386
387 template<typename C>
388 const C& accessBaseContainer(const C& c)
389 {
390 return c;
391 }
392
393 template<typename T, typename Tag, typename W>
394 const typename LocalVector<T,Tag,W>::BaseContainer& accessBaseContainer(const LocalVector<T,Tag,W>& c)
395 {
396 return c.base();
397 }
398
399 template<typename C>
400 const typename WeightedVectorAccumulationView<C>::BaseContainer& accessBaseContainer
401 (const WeightedVectorAccumulationView<C>& c)
402 {
403 return c.base();
404 }
405
410 } // end namespace PDELab
411} // end namespace Dune
412
413#endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_LOCALVECTOR_HH
A container for storing data associated with the degrees of freedom of a LocalFunctionSpace.
Definition: localvector.hh:184
value_type two_norm() const
Return Euclidean norm of vector.
Definition: localvector.hh:307
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView of this container with the given weight.
Definition: localvector.hh:214
BaseContainer::reference reference
The reference type of this container.
Definition: localvector.hh:198
auto data() const
Access underlying container, const version.
Definition: localvector.hh:250
value_type dot(const LocalVector &other) const
Dot product with other vector.
Definition: localvector.hh:297
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: localvector.hh:201
size_type size() const
The size of the container.
Definition: localvector.hh:318
BaseContainer::size_type size_type
The size type of this container.
Definition: localvector.hh:195
void assign(size_type size, const T &value)
Resize the container to size and assign the passed value to all entries.
Definition: localvector.hh:330
LocalVector()
Default constructor.
Definition: localvector.hh:348
WeightedVectorAccumulationView< LocalVector > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions.
Definition: localvector.hh:211
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: localvector.hh:188
W weight_type
The weight type of this container.
Definition: localvector.hh:208
const BaseContainer & base() const
Returns the underlying, std::vector-like storage container (const version).
Definition: localvector.hh:342
LocalVector & axpy(const value_type alpha, const LocalVector &other)
Calculate axpy operation this -> this += alpha*other.
Definition: localvector.hh:256
const_reference operator()(const LFS &lfs, size_type i) const
Access the value in this container associated with the i-th degree of freedom of the LocalFunctionSpa...
Definition: localvector.hh:238
LocalVector & operator+=(const LocalVector &other)
Adds two vectors.
Definition: localvector.hh:274
void resize(size_type size)
Resize the container.
Definition: localvector.hh:324
LocalVector & operator*=(const value_type &v)
Multiplies all entries by v.
Definition: localvector.hh:284
auto data()
Access underlying container.
Definition: localvector.hh:244
BaseContainer::value_type value_type
The value type of this container.
Definition: localvector.hh:191
LocalVector(size_type n)
Construct a LocalVector with size n.
Definition: localvector.hh:352
LocalVector(size_type n, const value_type &v)
Construct a LocalVector with size n and initialize all entries with v.
Definition: localvector.hh:357
reference operator()(const LFS &lfs, size_type i)
Access the value in this container associated with the i-th degree of freedom of the LocalFunctionSpa...
Definition: localvector.hh:226
BaseContainer & base()
Returns the underlying, std::vector-like storage container.
Definition: localvector.hh:336
LocalVector & operator=(const value_type &v)
Assigns v to all entries.
Definition: localvector.hh:267
An accumulate-only view on a local vector that automatically takes into account an accumulation weigh...
Definition: localvector.hh:28
auto data() const
Access underlying container, const version.
Definition: localvector.hh:159
Container & container()
Returns the container (of type LocalVector) that this view is based on.
Definition: localvector.hh:127
Container::size_type size_type
The size_type of the underlying container.
Definition: localvector.hh:55
C Container
The type of the underlying LocalVector.
Definition: localvector.hh:32
void accumulate(const LFS &lfs, size_type n, value_type v)
Applies the current weight to v and adds the result to the n-th degree of freedom of the lfs.
Definition: localvector.hh:79
Container::value_type value_type
The value type of the entries.
Definition: localvector.hh:38
BaseContainer & base()
Returns the storage container of the underlying LocalVector.
Definition: localvector.hh:140
void rawAccumulate(const LFS &lfs, size_type n, value_type v)
Adds v to the n-th degree of freedom of the lfs without applying the current weight.
Definition: localvector.hh:91
const Container & container() const
Returns the container (of type LocalVector) that this view is based on (const version).
Definition: localvector.hh:134
weight_type weight() const
Returns the weight associated with this view.
Definition: localvector.hh:62
Container::weight_type weight_type
The type of the weight applied when accumulating contributions.
Definition: localvector.hh:41
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localvector.hh:147
Container::BaseContainer BaseContainer
The type of the storage container underlying the LocalVector.
Definition: localvector.hh:35
void resetModified()
Resets the modification state of the view to not modified.
Definition: localvector.hh:121
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView with some weight in addition to this view's weight.
Definition: localvector.hh:49
WeightedVectorAccumulationView(C &container, weight_type weight)
Constructor.
Definition: localvector.hh:98
WeightedVectorAccumulationView WeightedAccumulationView
Export this type for uniform handling of the containers themselves and their views.
Definition: localvector.hh:45
size_type size() const
Returns the size of the underlying container.
Definition: localvector.hh:105
auto data()
Access underlying container.
Definition: localvector.hh:153
bool modified() const
Returns whether this view has been written to.
Definition: localvector.hh:111
void setWeight(weight_type weight)
Resets the weighting coefficient of the view.
Definition: localvector.hh:72
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:55
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)