DUNE PDELab (git)

localmatrix.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
5 #define DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
6 
8 
10 
11 namespace Dune {
12  namespace PDELab {
13 
20  template<typename C>
22  {
23 
24  public:
25 
27  typedef C Container;
28 
30 
33  typedef typename Container::BaseContainer BaseContainer;
34 
36  typedef typename C::value_type value_type;
37 
39  typedef typename C::weight_type weight_type;
40 
44 
48  {
50  }
51 
53  typedef typename C::size_type size_type;
54 
56 
61  {
62  return _weight;
63  }
64 
66 
71  {
72  _weight = weight;
73  }
74 
76  template<typename LFSU, typename LFSV>
77  void accumulate(const LFSV& lfsv, size_type i,
78  const LFSU& lfsu, size_type j,
79  value_type v)
80  {
81  _modified = true;
82  _container(lfsv,i,lfsu,j) += _weight * v;
83  }
84 
86 
90  template<typename LFSU, typename LFSV>
91  void rawAccumulate(const LFSV& lfsv, size_type i,
92  const LFSU& lfsu, size_type j,
93  value_type v)
94  {
95  _modified = true;
96  _container(lfsv,i,lfsu,j) += v;
97  }
98 
100  size_type nrows() const
101  {
102  return _container.nrows();
103  }
104 
106  size_type ncols() const
107  {
108  return _container.ncols();
109  }
110 
112  bool modified() const
113  {
114  return _modified;
115  }
116 
118 
123  {
124  _modified = false;
125  }
126 
129  {
130  _modified = true;
131  return _container;
132  }
133 
135  const Container& container() const
136  {
137  return _container;
138  }
139 
141 
145  {
146  _modified = true;
147  return _container.base();
148  }
149 
151 
154  const BaseContainer& base() const
155  {
156  return _container.base();
157  }
158 
161  : _container(container)
162  , _weight(weight)
163  , _modified(false)
164  {}
165 
166  private:
167  Container& _container;
168  weight_type _weight;
169  bool _modified;
170  };
171 
172 
174  template<typename T, typename W = T>
184  {
185  public:
186 
188 
191  typedef std::vector<T> BaseContainer;
192 
194  typedef typename BaseContainer::value_type value_type;
195 
197  typedef typename BaseContainer::size_type size_type;
198 
200  typedef typename BaseContainer::reference reference;
201 
203  typedef typename BaseContainer::const_reference const_reference;
204 
206 
210  typedef W weight_type;
211 
214 
215  struct iterator
216  : public Dune::BidirectionalIteratorFacade<iterator,value_type>
217  {
218 
219  iterator()
220  : _m(nullptr)
221  , _i(0)
222  , _j(0)
223  {}
224 
225  iterator(LocalMatrix& m, size_type i, size_type j)
226  : _m(&m)
227  , _i(i)
228  , _j(j)
229  {}
230 
231  bool equals(const iterator& other) const
232  {
233  return _m == other._m && _i == other._i && _j == other._j;
234  }
235 
236  value_type& dereference() const
237  {
238  return _m->getEntry(_i,_j);
239  }
240 
241  void increment()
242  {
243  if (_j < _m->ncols() - 1)
244  ++_j;
245  else
246  {
247  ++_i;
248  _j = 0;
249  }
250  }
251 
252  void decrement()
253  {
254  if (_j > 0)
255  --_j;
256  else
257  {
258  --_i;
259  _j = _m->ncols() - 1;
260  }
261  }
262 
263  size_type row() const
264  {
265  return _i;
266  }
267 
268  size_type col() const
269  {
270  return _j;
271  }
272 
273  LocalMatrix* _m;
274  size_type _i;
275  size_type _j;
276 
277  };
278 
279  iterator begin()
280  {
281  return iterator(*this,0,0);
282  }
283 
284  iterator end()
285  {
286  return ncols() > 0 ? iterator(*this, nrows(), 0) : begin();
287  }
288 
291 
294  : _container(r*c)
295  , _rows(r)
296  , _cols(c)
297  {}
298 
300  LocalMatrix (size_type r, size_type c, const T& t)
301  : _container(r*c,t)
302  , _rows(r)
303  , _cols(c)
304  {}
305 
308  {
309  _container.resize(r*c);
310  _rows = r;
311  _cols = c;
312  }
313 
315  LocalMatrix& operator=(const T& t)
316  {
317  std::fill(_container.begin(),_container.end(),t);
318  return *this;
319  }
320 
322  void assign (size_type r, size_type c, const T& t)
323  {
324  _container.assign(r*c,t);
325  _rows = r;
326  _cols = c;
327  }
328 
330 
336  template<typename LFSU, typename LFSV>
337  T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j)
338  {
339  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
340  }
341 
343 
349  template<typename LFSU, typename LFSV>
350  const T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j) const
351  {
352  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
353  }
354 
357  {
358  using namespace std::placeholders;
359  std::transform(
360  _container.begin(),
361  _container.end(),
362  _container.begin(),
363  std::bind(std::multiplies<T>(),x,_1)
364  );
365  return *this;
366  }
367 
369  size_type nrows () const
370  {
371  return _rows;
372  }
373 
375  size_type ncols () const
376  {
377  return _cols;
378  }
379 
381  template<class X, class R>
382  void umv (const X& x, R& y) const
383  {
384  for (size_type i=0; i<_rows; ++i)
385  {
386  for (size_type j=0; j<_cols; j++)
387  accessBaseContainer(y)[i] += getEntry(i,j) * accessBaseContainer(x)[j];
388  }
389  }
390 
392  template<class X, class R>
393  void usmv (const value_type& alpha, const X& x, R& y) const
394  {
395  for (size_type i=0; i<_rows; ++i)
396  {
397  for (size_type j=0; j<_cols; j++)
398  accessBaseContainer(y)[i] += alpha * getEntry(i,j) * accessBaseContainer(x)[j];
399  }
400  }
401 
404  {
405  return WeightedAccumulationView(*this,weight);
406  }
407 
409 
413  {
414  return _container;
415  }
416 
418 
421  const BaseContainer& base() const
422  {
423  return _container;
424  }
425 
427 
436  {
437  return _container[j*_rows + i];
438  }
439 
441 
450  {
451  return _container[j*_rows + i];
452  }
453 
454  private:
455 
456  std::vector<T> _container;
457  size_type _rows, _cols;
458  };
459 
460  template<class Stream, class T, class W>
461  Stream &operator<<(Stream &stream, const LocalMatrix<T,W> &m) {
462  for(int r = 0; r < m.nrows(); ++r) {
463  if(m.ncols() >= 1)
464  stream << m.getEntry(r, 0);
465  for(int c = 1; c < m.ncols(); ++c)
466  stream << "\t" << m.getEntry(r, c);
467  stream << "\n";
468  }
469  return stream;
470  }
471 
476  } // namespace PDELab
477 } // namespace Dune
478 
479 #endif // DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
Facade class for stl conformant bidirectional iterators.
Definition: iteratorfacades.hh:274
A dense matrix for storing data associated with the degrees of freedom of a pair of LocalFunctionSpac...
Definition: localmatrix.hh:184
const value_type & getEntry(size_type i, size_type j) const
Direct (unmapped) access to the (i,j)-th entry of the matrix (const version).
Definition: localmatrix.hh:449
LocalMatrix()
Default constructor.
Definition: localmatrix.hh:290
BaseContainer & base()
Returns the underlying storage container.
Definition: localmatrix.hh:412
W weight_type
The weight type of this container.
Definition: localmatrix.hh:210
const BaseContainer & base() const
Returns the underlying storage container (const version).
Definition: localmatrix.hh:421
LocalMatrix(size_type r, size_type c)
Construct a LocalMatrix with r rows and c columns.
Definition: localmatrix.hh:293
LocalMatrix(size_type r, size_type c, const T &t)
Construct a LocalMatrix with r rows and c columns and initialize its entries with t.
Definition: localmatrix.hh:300
size_type nrows() const
Returns the number of rows.
Definition: localmatrix.hh:369
WeightedMatrixAccumulationView< LocalMatrix > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions.
Definition: localmatrix.hh:213
T & operator()(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j)
Access the value associated with the i-th DOF of lfsv and the j-th DOF of lfsu.
Definition: localmatrix.hh:337
BaseContainer::reference reference
The reference type of this container.
Definition: localmatrix.hh:200
void resize(size_type r, size_type c)
Resize the matrix.
Definition: localmatrix.hh:307
BaseContainer::size_type size_type
The size type of this container.
Definition: localmatrix.hh:197
BaseContainer::value_type value_type
The value type of this container.
Definition: localmatrix.hh:194
value_type & getEntry(size_type i, size_type j)
Direct (unmapped) access to the (i,j)-th entry of the matrix.
Definition: localmatrix.hh:435
LocalMatrix & operator=(const T &t)
Assign t to all entries of the matrix.
Definition: localmatrix.hh:315
void assign(size_type r, size_type c, const T &t)
Resize the matrix and assign t to all entries.
Definition: localmatrix.hh:322
LocalMatrix & operator*=(const T &x)
Multiplies all entries of the matrix with x.
Definition: localmatrix.hh:356
void umv(const X &x, R &y) const
y += A x
Definition: localmatrix.hh:382
size_type ncols() const
Returns the number of columns.
Definition: localmatrix.hh:375
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: localmatrix.hh:191
void usmv(const value_type &alpha, const X &x, R &y) const
y += alpha A x
Definition: localmatrix.hh:393
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a weighted accumulate-only view of this matrix with the given weight.
Definition: localmatrix.hh:403
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: localmatrix.hh:203
An accumulate-only view on a local matrix that automatically takes into account an accumulation weigh...
Definition: localmatrix.hh:22
C Container
The type of the underlying container.
Definition: localmatrix.hh:27
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView with some weight in addition to this view's weight.
Definition: localmatrix.hh:47
C::value_type value_type
The value type of the entries.
Definition: localmatrix.hh:36
BaseContainer & base()
Returns the storage container of the underlying LocalMatrix.
Definition: localmatrix.hh:144
Container & container()
Returns the container (of type LocalMatrix) that this view is based on.
Definition: localmatrix.hh:128
size_type ncols() const
Returns the number of colums of the underlying container.
Definition: localmatrix.hh:106
WeightedMatrixAccumulationView WeightedAccumulationView
Export this type for uniform handling of the containers themselves and their views.
Definition: localmatrix.hh:43
const Container & container() const
Returns the container (of type LocalMatrix) that this view is based on (const version).
Definition: localmatrix.hh:135
void setWeight(weight_type weight)
Resets the weighting coefficient of the view.
Definition: localmatrix.hh:70
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localmatrix.hh:154
WeightedMatrixAccumulationView(Container &container, weight_type weight)
Constructor.
Definition: localmatrix.hh:160
void resetModified()
Resets the modification state of the view to not modified.
Definition: localmatrix.hh:122
size_type nrows() const
Returns the number of rows of the underlying container.
Definition: localmatrix.hh:100
void rawAccumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type v)
Adds v to the (i,j)-th entry of the underlying container without applying the current weight.
Definition: localmatrix.hh:91
bool modified() const
Returns whether this view has been written to.
Definition: localmatrix.hh:112
weight_type weight() const
Returns the weight associated with this view.
Definition: localmatrix.hh:60
C::size_type size_type
The size_type of the underlying container.
Definition: localmatrix.hh:53
Container::BaseContainer BaseContainer
The type of the storage container underlying the LocalVector.
Definition: localmatrix.hh:33
C::weight_type weight_type
The type of the weight applied when accumulating contributions.
Definition: localmatrix.hh:39
void accumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type v)
Applies the current weight to v and adds the result to the matrix entry associated with the i-th entr...
Definition: localmatrix.hh:77
constexpr index_constant< 1 > _1
Compile time index with value 1.
Definition: indices.hh:55
constexpr auto equals
Function object for performing equality comparison.
Definition: hybridutilities.hh:572
This file implements iterator facade classes for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)