dune-pdelab  2.7-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 
17 namespace 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 
105  size_type size() const
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  const 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;
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  const 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  value_type nrm=0.0;
310  nrm = std::inner_product(_container.begin(),_container.end(),
311  _container.begin(),
312  nrm);
313  return sqrt(nrm);
314  }
315 
317  size_type size() const
318  {
319  return _container.size();
320  }
321 
324  {
325  _container.resize(size);
326  }
327 
329  void assign(size_type size, const T& value)
330  {
331  _container.assign(size,value);
332  }
333 
336  {
337  return _container;
338  }
339 
341  const BaseContainer& base() const
342  {
343  return _container;
344  }
345 
348  {}
349 
351  explicit LocalVector(size_type n)
352  : _container(n)
353  {}
354 
357  : _container(n,v)
358  {}
359 
360  private:
361 
362  BaseContainer _container;
363 
364  };
365 
366 
367  template<typename C>
369  {
370  return c;
371  }
372 
373  template<typename T, typename Tag, typename W>
375  {
376  return c.base();
377  }
378 
379  template<typename C>
382  {
383  return c.base();
384  }
385 
386  template<typename C>
387  const C& accessBaseContainer(const C& c)
388  {
389  return c;
390  }
391 
392  template<typename T, typename Tag, typename W>
394  {
395  return c.base();
396  }
397 
398  template<typename C>
401  {
402  return c.base();
403  }
404 
409  } // end namespace PDELab
410 } // end namespace Dune
411 
412 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_LOCALVECTOR_HH
C & accessBaseContainer(C &c)
Definition: localvector.hh:368
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
An accumulate-only view on a local vector that automatically takes into account an accumulation weigh...
Definition: localvector.hh:28
const Container & container() const
Returns the container (of type LocalVector) that this view is based on (const version).
Definition: localvector.hh:134
BaseContainer & base()
Returns the storage container of the underlying LocalVector.
Definition: localvector.hh:140
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
const auto data() const
Access underlying container, const version.
Definition: localvector.hh:159
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
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
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
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localvector.hh:147
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
Container & container()
Returns the container (of type LocalVector) that this view is based on.
Definition: localvector.hh:127
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
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
const auto data() const
Access underlying container, const version.
Definition: localvector.hh:250
BaseContainer::reference reference
The reference type of this container.
Definition: localvector.hh:198
value_type dot(const LocalVector &other) const
Dot product with other vector.
Definition: localvector.hh:297
LocalVector & operator=(const value_type &v)
Assigns v to all entries.
Definition: localvector.hh:267
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:317
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:329
const BaseContainer & base() const
Returns the underlying, std::vector-like storage container (const version).
Definition: localvector.hh:341
LocalVector()
Default constructor.
Definition: localvector.hh:347
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_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 value_type &v)
Multiplies all entries by v.
Definition: localvector.hh:284
LocalVector & operator+=(const LocalVector &other)
Adds two vectors.
Definition: localvector.hh:274
void resize(size_type size)
Resize the container.
Definition: localvector.hh:323
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:351
LocalVector(size_type n, const value_type &v)
Construct a LocalVector with size n and initialize all entries with v.
Definition: localvector.hh:356
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
LocalVector & axpy(const value_type alpha, const LocalVector &other)
Calculate axpy operation this -> this += alpha*other.
Definition: localvector.hh:256
value_type field_type
Definition: localvector.hh:192
BaseContainer & base()
Returns the underlying, std::vector-like storage container.
Definition: localvector.hh:335
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139