3#ifndef DUNE_ISTL_MATRIXUTILS_HH
4#define DUNE_ISTL_MATRIXUTILS_HH
15#include "istlexception.hh"
21 template<
typename B,
typename A>
24 template<
typename K,
int n,
int m>
27 template<
class T,
class A>
47 static typename M::size_type count(
const M& matrix)
49 typedef typename M::ConstRowIterator RowIterator;
51 RowIterator endRow = matrix.end();
52 typename M::size_type nonZeros = 0;
54 for(RowIterator row = matrix.begin(); row != endRow; ++row) {
55 typedef typename M::ConstColIterator Entry;
56 Entry endEntry = row->end();
57 for(Entry entry = row->begin(); entry != endEntry; ++entry) {
58 nonZeros += NonZeroCounter<i-1>::count(*entry);
66 struct NonZeroCounter<1>
69 static typename M::size_type count(
const M& matrix)
71 return matrix.N()*matrix.M();
81 template<
class Matrix, std::
size_t blocklevel, std::
size_t l=blocklevel>
91#ifdef DUNE_ISTL_WITH_CHECKING
94 for(Row row = mat.begin(); row!=mat.end(); ++row) {
95 Entry diagonal = row->find(row.index());
96 if(diagonal==row->end())
98 <<
" at block recursion level "<<l-blocklevel);
106 template<
class Matrix, std::
size_t l>
107 struct CheckIfDiagonalPresent<Matrix,0,l>
109 static void check(
const Matrix& mat)
112 for(Row row = mat.begin(); row!=mat.end(); ++row) {
113 if(row->find(row.index())==row->end())
114 DUNE_THROW(ISTLError,
"Missing diagonal value in row "<<row.index()
115 <<
" at block recursion level "<<l);
120 template<
typename FirstRow,
typename... Args>
121 class MultiTypeBlockMatrix;
123 template<std::size_t blocklevel, std::size_t l,
typename T1,
typename... Args>
124 struct CheckIfDiagonalPresent<MultiTypeBlockMatrix<T1,Args...>,
127 typedef MultiTypeBlockMatrix<T1,Args...> Matrix;
133 static void check(
const Matrix& )
135#ifdef DUNE_ISTL_WITH_CHECKING
155 return NonZeroCounter<M::blocklevel>::count(matrix);
166 template<
class G,
class M>
167 bool operator()(
const std::pair<G,M>& p1,
const std::pair<G,M>& p2)
169 return p1.first<p2.first;
174 template<
class M,
class C>
175 void printGlobalSparseMatrix(
const M& mat, C& ooc, std::ostream& os)
177 typedef typename C::ParallelIndexSet::const_iterator IIter;
178 typedef typename C::OwnerSet OwnerSet;
179 typedef typename C::ParallelIndexSet::GlobalIndex GlobalIndex;
183 for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end();
185 gmax=std::max(gmax,idx->global());
187 gmax=ooc.communicator().max(gmax);
188 ooc.buildGlobalLookup();
190 for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end();
192 if(OwnerSet::contains(idx->local().attribute()))
194 typedef typename M::block_type Block;
196 std::set<std::pair<GlobalIndex,Block>,CompPair> entries;
199 typedef typename M::ConstColIterator CIter;
200 for(CIter c=mat[idx->local()].begin(), cend=mat[idx->local()].end();
202 const typename C::ParallelIndexSet::IndexPair* pair
203 =ooc.globalLookup().pair(c.index());
205 entries.insert(std::make_pair(pair->global(), *c));
209 GlobalIndex rowidx = idx->global();
210 GlobalIndex cur=std::numeric_limits<GlobalIndex>::max();
212 cur=ooc.communicator().min(rowidx);
215 typedef typename std::set<std::pair<GlobalIndex,Block>,CompPair>::iterator SIter;
216 for(SIter s=entries.begin(), send=entries.end(); s!=send; ++s)
217 os<<idx->global()<<
" "<<s->first<<
" "<<s->second<<std::endl;
223 ooc.freeGlobalLookup();
225 GlobalIndex cur=std::numeric_limits<GlobalIndex>::max();
226 while(cur!=ooc.communicator().min(cur)) ;
230 struct MatrixDimension
234 template<
typename B,
typename TA>
235 struct MatrixDimension<BCRSMatrix<B,TA> >
237 typedef BCRSMatrix<B,TA> Matrix;
241 static size_type rowdim (
const Matrix& A, size_type i)
243 const B* row = A.r[i].getptr();
245 return MatrixDimension<block_type>::rowdim(*row);
250 static size_type coldim (
const Matrix& A, size_type c)
255 for (size_type k=0; k<A.nnz_; k++) {
256 if (A.j_.get()[k] == c) {
257 return MatrixDimension<block_type>::coldim(A.a[k]);
263 for (size_type i=0; i<A.N(); i++)
265 size_type* j = A.r[i].getindexptr();
266 B* a = A.r[i].getptr();
267 for (size_type k=0; k<A.r[i].getsize(); k++)
269 return MatrixDimension<block_type>::coldim(a[k]);
278 static size_type rowdim (
const Matrix& A){
280 for (size_type i=0; i<A.N(); i++)
285 static size_type coldim (
const Matrix& A){
292 std::vector<size_type> coldims(A.M(),
293 std::numeric_limits<size_type>::max());
295 for (ConstRowIterator row=A.begin(); row!=A.end(); ++row)
296 for (ConstColIterator col=row->begin(); col!=row->end(); ++col)
298 if (coldims[col.index()]==std::numeric_limits<size_type>::max())
299 coldims[col.index()] = MatrixDimension<block_type>::coldim(*col);
302 for (
typename std::vector<size_type>::iterator it=coldims.begin();
303 it!=coldims.end(); ++it)
313 template<
typename B,
int n,
int m,
typename TA>
314 struct MatrixDimension<BCRSMatrix<FieldMatrix<B,n,m> ,TA> >
316 typedef BCRSMatrix<FieldMatrix<B,n,m> ,TA> Matrix;
319 static size_type rowdim (
const Matrix& , size_type )
324 static size_type coldim (
const Matrix& , size_type )
329 static size_type rowdim (
const Matrix& A) {
333 static size_type coldim (
const Matrix& A) {
338 template<
typename K,
int n,
int m>
339 struct MatrixDimension<FieldMatrix<K,n,m> >
341 typedef FieldMatrix<K,n,m> Matrix;
344 static size_type rowdim(
const Matrix& , size_type )
349 static size_type coldim(
const Matrix& , size_type )
354 static size_type rowdim(
const Matrix& )
359 static size_type coldim(
const Matrix& )
366 struct MatrixDimension<
Dune::DynamicMatrix<T> >
369 typedef typename MatrixType::size_type size_type;
371 static size_type rowdim(
const MatrixType& , size_type )
376 static size_type coldim(
const MatrixType& , size_type )
381 static size_type rowdim(
const MatrixType& A)
386 static size_type coldim(
const MatrixType& A)
392 template<
typename K,
int n,
int m,
typename TA>
393 struct MatrixDimension<Matrix<FieldMatrix<K,n,m>, TA> >
395 typedef Matrix<FieldMatrix<K,n,m>, TA> ThisMatrix;
396 typedef typename ThisMatrix::size_type size_type;
398 static size_type rowdim(
const ThisMatrix& , size_type )
403 static size_type coldim(
const ThisMatrix& , size_type )
408 static size_type rowdim(
const ThisMatrix& A)
413 static size_type coldim(
const ThisMatrix& A)
419 template<
typename K,
int n>
420 struct MatrixDimension<DiagonalMatrix<K,n> >
422 typedef DiagonalMatrix<K,n> Matrix;
425 static size_type rowdim(
const Matrix& , size_type )
430 static size_type coldim(
const Matrix& , size_type )
435 static size_type rowdim(
const Matrix& )
440 static size_type coldim(
const Matrix& )
446 template<
typename K,
int n>
447 struct MatrixDimension<ScaledIdentityMatrix<K,n> >
449 typedef ScaledIdentityMatrix<K,n> Matrix;
452 static size_type rowdim(
const Matrix& , size_type )
457 static size_type coldim(
const Matrix& , size_type )
462 static size_type rowdim(
const Matrix& )
467 static size_type coldim(
const Matrix& )
488 struct IsMatrix<DenseMatrix<T> >
499 template<
typename T,
typename A>
500 struct IsMatrix<BCRSMatrix<T,A> >
511 struct PointerCompare
513 bool operator()(
const T* l,
const T* r)
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:59
derive error class from the base class in common
Definition: istlexception.hh:16
ConstIterator class for sequential access.
Definition: matrix.hh:398
A generic dynamic dense matrix.
Definition: matrix.hh:555
A::size_type size_type
Type for indices and sizes.
Definition: matrix.hh:571
MatrixImp::DenseMatrixBase< T, A >::ConstIterator ConstRowIterator
Const iterator over the matrix rows.
Definition: matrix.hh:580
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:583
T block_type
Export the type representing the components.
Definition: matrix.hh:562
This file implements a quadratic diagonal matrix of fixed size.
This file implements a dense matrix with dynamic numbers of rows and columns.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
int countNonZeros(const M &matrix)
Get the number of nonzero fields in the matrix.
Definition: matrixutils.hh:153
Dune namespace.
Definition: alignment.hh:11
This file implements a quadratic matrix of fixed size which is a multiple of the identity.
Check whether the a matrix has diagonal values on blocklevel recursion levels.
Definition: matrixutils.hh:83
static void check(const Matrix &mat)
Check whether the a matrix has diagonal values on blocklevel recursion levels.
Definition: matrixutils.hh:88
Test whether a type is an ISTL Matrix.
Definition: matrixutils.hh:478
@ value
True if T is an ISTL matrix.
Definition: matrixutils.hh:483
Traits for type conversions and type information.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:18