Dune Core Modules (2.6.0)

colcompmatrix.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_ISTL_COLCOMPMATRIX_HH
4 #define DUNE_ISTL_COLCOMPMATRIX_HH
5 #include "bcrsmatrix.hh"
6 #include "bvector.hh"
7 #include <dune/common/fmatrix.hh>
8 #include <dune/common/fvector.hh>
10 #include <dune/common/unused.hh>
11 #include <limits>
12 
13 namespace Dune
14 {
20  template<class M>
22  {
23  public:
25  typedef M Matrix;
28 
33  MatrixRowSet(const Matrix& m)
34  : m_(m)
35  {}
36 
39  {
40  return m_.begin();
41  }
44  {
45  return m_.end();
46  }
47  private:
48  const Matrix& m_;
49  };
50 
58  template<class M, class S>
60  {
61  public:
63  typedef M Matrix;
65  typedef S RowIndexSet;
66 
72  MatrixRowSubset(const Matrix& m, const RowIndexSet& s)
73  : m_(m), s_(s)
74  {}
75 
76  const Matrix& matrix() const
77  {
78  return m_;
79  }
80 
81  const RowIndexSet& rowIndexSet() const
82  {
83  return s_;
84  }
85 
88  : public ForwardIteratorFacade<const_iterator, const typename Matrix::row_type>
89  {
90  public:
91  const_iterator(typename Matrix::const_iterator firstRow,
92  typename RowIndexSet::const_iterator pos)
93  : firstRow_(firstRow), pos_(pos)
94  {}
95 
96 
97  const typename Matrix::row_type& dereference() const
98  {
99  return *(firstRow_+ *pos_);
100  }
101  bool equals(const const_iterator& o) const
102  {
103  return pos_==o.pos_;
104  }
105  void increment()
106  {
107  ++pos_;
108  }
109  typename RowIndexSet::value_type index() const
110  {
111  return *pos_;
112  }
113 
114  private:
116  typename Matrix::const_iterator firstRow_;
118  typename RowIndexSet::const_iterator pos_;
119  };
120 
123  {
124  return const_iterator(m_.begin(), s_.begin());
125  }
128  {
129  return const_iterator(m_.begin(), s_.end());
130  }
131 
132  private:
134  const Matrix& m_;
136  const RowIndexSet& s_;
137  };
138 
143  template<class M>
145  {};
146 
152  template<class M>
154  {};
155 
156  template<class M, class X, class TM, class TD, class T1>
157  class SeqOverlappingSchwarz;
158 
159  template<class T, bool flag>
160  struct SeqOverlappingSchwarzAssemblerHelper;
161 
166  template<class B, class TA, int n, int m>
168  {
169  friend struct ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<B,n,m>,TA> >;
170 
171  public:
174  friend struct SeqOverlappingSchwarzAssemblerHelper<ColCompMatrix<Matrix>, true>;
175 
176  typedef typename Matrix::size_type size_type;
177 
182  explicit ColCompMatrix(const Matrix& mat);
183 
184  ColCompMatrix();
185 
187  virtual ~ColCompMatrix();
188 
193  size_type N() const
194  {
195  return N_;
196  }
197 
198  size_type nnz() const
199  {
200  return Nnz_/n/m;
201  }
202 
207  size_type M() const
208  {
209  return M_;
210  }
211 
212  B* getValues() const
213  {
214  return values;
215  }
216 
217  int* getRowIndex() const
218  {
219  return rowindex;
220  }
221 
222  int* getColStart() const
223  {
224  return colstart;
225  }
226 
227  ColCompMatrix& operator=(const Matrix& mat);
228  ColCompMatrix& operator=(const ColCompMatrix& mat);
229 
235  virtual void setMatrix(const Matrix& mat, const std::set<std::size_t>& mrs);
237  virtual void free();
238 
240  virtual void setMatrix(const Matrix& mat);
241 
242  public:
243  int N_, M_, Nnz_;
244  B* values;
245  int* rowindex;
246  int* colstart;
247  };
248 
249  template<class T, class A, int n, int m>
251  {
252  template<class I, class S, class D>
253  friend class OverlappingSchwarzInitializer;
254  public:
257  typedef typename Matrix::row_type::const_iterator CIter;
258  typedef typename Matrix::size_type size_type;
259 
261 
263 
264  virtual ~ColCompMatrixInitializer();
265 
266  template<typename Iter>
267  void addRowNnz(const Iter& row) const;
268 
269  template<typename Iter, typename Set>
270  void addRowNnz(const Iter& row, const Set& s) const;
271 
272  void allocate();
273 
274  template<typename Iter>
275  void countEntries(const Iter& row, const CIter& col) const;
276 
277  void countEntries(size_type colidx) const;
278 
279  void calcColstart() const;
280 
281  template<typename Iter>
282  void copyValue(const Iter& row, const CIter& col) const;
283 
284  void copyValue(const CIter& col, size_type rowindex, size_type colidx) const;
285 
286  virtual void createMatrix() const;
287 
288  protected:
289 
290  void allocateMatrixStorage() const;
291 
292  void allocateMarker();
293 
294  ColCompMatrix* mat;
295  size_type cols;
296  mutable size_type *marker;
297  };
298 
299  template<class T, class A, int n, int m>
300  ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::ColCompMatrixInitializer(ColCompMatrix& mat_)
301  : mat(&mat_), cols(mat_.M()), marker(0)
302  {
303  mat->Nnz_=0;
304  }
305 
306  template<class T, class A, int n, int m>
307  ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::ColCompMatrixInitializer()
308  : mat(0), cols(0), marker(0)
309  {}
310 
311  template<class T, class A, int n, int m>
312  ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::~ColCompMatrixInitializer()
313  {
314  if(marker)
315  delete[] marker;
316  }
317 
318  template<class T, class A, int n, int m>
319  template<typename Iter>
320  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::addRowNnz(const Iter& row) const
321  {
322  mat->Nnz_+=row->getsize();
323  }
324 
325  template<class T, class A, int n, int m>
326  template<typename Iter, typename Map>
327  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::addRowNnz(const Iter& row,
328  const Map& indices) const
329  {
330  typedef typename Iter::value_type::const_iterator RIter;
331  typedef typename Map::const_iterator MIter;
332  MIter siter =indices.begin();
333  for(RIter entry=row->begin(); entry!=row->end(); ++entry)
334  {
335  for(; siter!=indices.end() && *siter<entry.index(); ++siter) ;
336  if(siter==indices.end())
337  break;
338  if(*siter==entry.index())
339  // index is in subdomain
340  ++mat->Nnz_;
341  }
342  }
343 
344  template<class T, class A, int n, int m>
345  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::allocate()
346  {
347  allocateMatrixStorage();
348  allocateMarker();
349  }
350 
351  template<class T, class A, int n, int m>
352  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::allocateMatrixStorage() const
353  {
354  mat->Nnz_*=n*m;
355  // initialize data
356  mat->values=new T[mat->Nnz_];
357  mat->rowindex=new int[mat->Nnz_];
358  mat->colstart=new int[cols+1];
359  }
360 
361  template<class T, class A, int n, int m>
362  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::allocateMarker()
363  {
364  marker = new typename Matrix::size_type[cols];
365 
366  for(size_type i=0; i < cols; ++i)
367  marker[i]=0;
368  }
369 
370  template<class T, class A, int n, int m>
371  template<typename Iter>
372  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::countEntries(const Iter& row, const CIter& col) const
373  {
375  countEntries(col.index());
376  }
377 
378  template<class T, class A, int n, int m>
379  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::countEntries(size_type colindex) const
380  {
381  for(size_type i=0; i < m; ++i)
382  {
383  assert(colindex*m+i<cols);
384  marker[colindex*m+i]+=n;
385  }
386  }
387 
388  template<class T, class A, int n, int m>
389  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::calcColstart() const
390  {
391  mat->colstart[0]=0;
392  for(size_type i=0; i < cols; ++i) {
393  assert(i<cols);
394  mat->colstart[i+1]=mat->colstart[i]+marker[i];
395  marker[i]=mat->colstart[i];
396  }
397  }
398 
399  template<class T, class A, int n, int m>
400  template<typename Iter>
401  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::copyValue(const Iter& row, const CIter& col) const
402  {
403  copyValue(col, row.index(), col.index());
404  }
405 
406  template<class T, class A, int n, int m>
407  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::copyValue(const CIter& col, size_type rowindex, size_type colindex) const
408  {
409  for(size_type i=0; i<n; i++) {
410  for(size_type j=0; j<m; j++) {
411  assert(colindex*m+j<cols-1 || (int)marker[colindex*m+j]<mat->colstart[colindex*m+j+1]);
412  assert((int)marker[colindex*m+j]<mat->Nnz_);
413  mat->rowindex[marker[colindex*m+j]]=rowindex*n+i;
414  mat->values[marker[colindex*m+j]]=(*col)[i][j];
415  ++marker[colindex*m+j]; // index for next entry in column
416  }
417  }
418  }
419 
420  template<class T, class A, int n, int m>
421  void ColCompMatrixInitializer<BCRSMatrix<FieldMatrix<T,n,m>,A> >::createMatrix() const
422  {
423  delete[] marker;
424  marker=0;
425  }
426 
427  template<class F, class MRS>
428  void copyToColCompMatrix(F& initializer, const MRS& mrs)
429  {
430  typedef typename MRS::const_iterator Iter;
431  typedef typename std::iterator_traits<Iter>::value_type::const_iterator CIter;
432  for(Iter row=mrs.begin(); row!= mrs.end(); ++row)
433  initializer.addRowNnz(row);
434 
435  initializer.allocate();
436 
437  for(Iter row=mrs.begin(); row!= mrs.end(); ++row) {
438 
439  for(CIter col=row->begin(); col != row->end(); ++col)
440  initializer.countEntries(row, col);
441  }
442 
443  initializer.calcColstart();
444 
445  for(Iter row=mrs.begin(); row!= mrs.end(); ++row) {
446  for(CIter col=row->begin(); col != row->end(); ++col) {
447  initializer.copyValue(row, col);
448  }
449 
450  }
451  initializer.createMatrix();
452  }
453 
454  template<class F, class M,class S>
455  void copyToColCompMatrix(F& initializer, const MatrixRowSubset<M,S>& mrs)
456  {
457  typedef MatrixRowSubset<M,S> MRS;
458  typedef typename MRS::RowIndexSet SIS;
459  typedef typename SIS::const_iterator SIter;
460  typedef typename MRS::const_iterator Iter;
461  typedef typename std::iterator_traits<Iter>::value_type row_type;
462  typedef typename row_type::const_iterator CIter;
463 
464  // Calculate upper Bound for nonzeros
465  for(Iter row=mrs.begin(); row!= mrs.end(); ++row)
466  initializer.addRowNnz(row, mrs.rowIndexSet());
467 
468  initializer.allocate();
469 
470  typedef typename MRS::Matrix::size_type size_type;
471 
472  // A vector containing the corresponding indices in
473  // the to create submatrix.
474  // If an entry is the maximum of size_type then this index will not appear in
475  // the submatrix.
476  std::vector<size_type> subMatrixIndex(mrs.matrix().N(),
477  std::numeric_limits<size_type>::max());
478  size_type s=0;
479  for(SIter index = mrs.rowIndexSet().begin(); index!=mrs.rowIndexSet().end(); ++index)
480  subMatrixIndex[*index]=s++;
481 
482  for(Iter row=mrs.begin(); row!= mrs.end(); ++row)
483  for(CIter col=row->begin(); col != row->end(); ++col) {
484  if(subMatrixIndex[col.index()]!=std::numeric_limits<size_type>::max())
485  // This column is in our subset (use submatrix column index)
486  initializer.countEntries(subMatrixIndex[col.index()]);
487  }
488 
489  initializer.calcColstart();
490 
491  for(Iter row=mrs.begin(); row!= mrs.end(); ++row)
492  for(CIter col=row->begin(); col != row->end(); ++col) {
493  if(subMatrixIndex[col.index()]!=std::numeric_limits<size_type>::max())
494  // This value is in our submatrix -> copy (use submatrix indices
495  initializer.copyValue(col, subMatrixIndex[row.index()], subMatrixIndex[col.index()]);
496  }
497  initializer.createMatrix();
498  }
499 
500 #ifndef DOXYGEN
501 
502  template<class B, class TA, int n, int m>
503  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >::ColCompMatrix()
504  : N_(0), M_(0), Nnz_(0), values(0), rowindex(0), colstart(0)
505  {}
506 
507  template<class B, class TA, int n, int m>
508  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >
509  ::ColCompMatrix(const Matrix& mat)
510  : N_(n*mat.N()), M_(m*mat.M()), Nnz_(n*m*mat.nonzeroes())
511  {}
512 
513  template<class B, class TA, int n, int m>
514  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >&
515  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >::operator=(const Matrix& mat)
516  {
517  if(N_+M_+Nnz_!=0)
518  free();
519  setMatrix(mat);
520  return *this;
521  }
522 
523  template<class B, class TA, int n, int m>
524  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >&
525  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >::operator=(const ColCompMatrix& mat)
526  {
527  if(N_+M_+Nnz_!=0)
528  free();
529  N_=mat.N_;
530  M_=mat.M_;
531  Nnz_= mat.Nnz_;
532  if(M_>0) {
533  colstart=new int[M_+1];
534  for(int i=0; i<=M_; ++i)
535  colstart[i]=mat.colstart[i];
536  }
537 
538  if(Nnz_>0) {
539  values = new B[Nnz_];
540  rowindex = new int[Nnz_];
541 
542  for(int i=0; i<Nnz_; ++i)
543  values[i]=mat.values[i];
544 
545  for(int i=0; i<Nnz_; ++i)
546  rowindex[i]=mat.rowindex[i];
547  }
548  return *this;
549  }
550 
551  template<class B, class TA, int n, int m>
552  void ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >
553  ::setMatrix(const Matrix& mat)
554  {
555  N_=n*mat.N();
556  M_=m*mat.M();
557  ColCompMatrixInitializer<Matrix> initializer(*this);
558 
559  copyToColCompMatrix(initializer, MatrixRowSet<Matrix>(mat));
560  }
561 
562  template<class B, class TA, int n, int m>
563  void ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >
564  ::setMatrix(const Matrix& mat, const std::set<std::size_t>& mrs)
565  {
566  if(N_+M_+Nnz_!=0)
567  free();
568  N_=mrs.size()*n;
569  M_=mrs.size()*m;
570  ColCompMatrixInitializer<Matrix> initializer(*this);
571 
572  copyToColCompMatrix(initializer, MatrixRowSubset<Matrix,std::set<std::size_t> >(mat,mrs));
573  }
574 
575  template<class B, class TA, int n, int m>
576  ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >::~ColCompMatrix()
577  {
578  if(N_+M_+Nnz_!=0)
579  free();
580  }
581 
582  template<class B, class TA, int n, int m>
583  void ColCompMatrix<BCRSMatrix<FieldMatrix<B,n,m>,TA> >::free()
584  {
585  delete[] values;
586  delete[] rowindex;
587  delete[] colstart;
588  N_ = 0;
589  M_ = 0;
590  Nnz_ = 0;
591  }
592 
593 #endif // DOXYGEN
594 
595 }
596 #endif
Implementation of the BCRSMatrix class.
This file implements a vector space as a tensor product of a given vector space. The number of compon...
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:423
A::size_type size_type
The type for the index access and the size.
Definition: bcrsmatrix.hh:457
BCRSMatrix< FieldMatrix< B, n, m >, TA > Matrix
The type of the matrix to convert.
Definition: colcompmatrix.hh:173
ColCompMatrix(const Matrix &mat)
Constructor that initializes the data.
virtual void setMatrix(const Matrix &mat, const std::set< std::size_t > &mrs)
Initialize data from a given set of matrix rows and columns.
size_type N() const
Get the number of rows.
Definition: colcompmatrix.hh:193
virtual void setMatrix(const Matrix &mat)
Initialize data from given matrix.
size_type M() const
Get the number of columns.
Definition: colcompmatrix.hh:207
A dense n x m matrix.
Definition: fmatrix.hh:68
Base class for stl conformant forward iterators.
Definition: iteratorfacades.hh:144
ConstIterator class for sequential access.
Definition: matrix.hh:398
Provides access to an iterator over all matrix rows.
Definition: colcompmatrix.hh:22
Matrix::ConstRowIterator const_iterator
The matrix row iterator type.
Definition: colcompmatrix.hh:27
const_iterator begin() const
Get the row iterator at the first row.
Definition: colcompmatrix.hh:38
M Matrix
The type of the matrix.
Definition: colcompmatrix.hh:25
MatrixRowSet(const Matrix &m)
Construct an row set over all matrix rows.
Definition: colcompmatrix.hh:33
const_iterator end() const
Get the row iterator at the end of all rows.
Definition: colcompmatrix.hh:43
The matrix row iterator type.
Definition: colcompmatrix.hh:89
Provides access to an iterator over an arbitrary subset of matrix rows.
Definition: colcompmatrix.hh:60
S RowIndexSet
the type of the set of valid row indices.
Definition: colcompmatrix.hh:65
const_iterator begin() const
Get the row iterator at the first row.
Definition: colcompmatrix.hh:122
M Matrix
the type of the matrix class.
Definition: colcompmatrix.hh:63
MatrixRowSubset(const Matrix &m, const RowIndexSet &s)
Construct an row set over all matrix rows.
Definition: colcompmatrix.hh:72
const_iterator end() const
Get the row iterator at the end of all rows.
Definition: colcompmatrix.hh:127
A generic dynamic dense matrix.
Definition: matrix.hh:555
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:571
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:609
size_type M() const
Return the number of columns.
Definition: matrix.hh:695
MatrixImp::DenseMatrixBase< T, A >::window_type row_type
The type implementing a matrix row.
Definition: matrix.hh:568
size_type N() const
Return the number of rows.
Definition: matrix.hh:690
Initializer for SuperLU Matrices representing the subdomains.
Definition: overlappingschwarz.hh:43
Sequential overlapping Schwarz preconditioner.
Definition: overlappingschwarz.hh:742
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
Dune namespace.
Definition: alignedallocator.hh:10
Inititializer for the ColCompMatrix as needed by OverlappingSchwarz.
Definition: colcompmatrix.hh:154
Utility class for converting an ISTL Matrix into a column-compressed matrix.
Definition: colcompmatrix.hh:145
Traits for type conversions and type information.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 27, 22:29, 2024)