/usr/include/dune/istl/matrixutils.hh is in libdune-istl-dev 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=8 sw=2 sts=2:
#ifndef DUNE_MATRIX_UTILS_HH
#define DUNE_MATRIX_UTILS_HH
#include<set>
#include<vector>
#include<limits>
#include<dune/common/typetraits.hh>
#include<dune/common/static_assert.hh>
#include<dune/common/fmatrix.hh>
#include<dune/istl/bcrsmatrix.hh>
#include"istlexception.hh"
namespace Dune
{
#ifndef DOYXGEN
template<typename B, typename A>
class BCRSMatrix;
template<typename K, int n, int m>
class FieldMatrix;
template<class T, class A>
class Matrix;
#endif
/**
* @addtogroup ISTL_SPMV
* @{
*/
/**
* @file
* @brief Some handy generic functions for ISTL matrices.
* @author Markus Blatt
*/
namespace
{
template<int i>
struct NonZeroCounter
{
template<class M>
static typename M::size_type count(const M& matrix)
{
typedef typename M::ConstRowIterator RowIterator;
RowIterator endRow = matrix.end();
typename M::size_type nonZeros = 0;
for(RowIterator row = matrix.begin(); row != endRow; ++row){
typedef typename M::ConstColIterator Entry;
Entry endEntry = row->end();
for(Entry entry = row->begin(); entry != endEntry; ++entry){
nonZeros += NonZeroCounter<i-1>::count(*entry);
}
}
return nonZeros;
}
};
template<>
struct NonZeroCounter<1>
{
template<class M>
static typename M::size_type count(const M& matrix)
{
return matrix.N()*matrix.M();
}
};
}
/**
* @brief Check whether the a matrix has diagonal values
* on blocklevel recursion levels.
*/
template<class Matrix, std::size_t blocklevel, std::size_t l=blocklevel>
struct CheckIfDiagonalPresent
{
/**
* @brief Check whether the a matrix has diagonal values
* on blocklevel recursion levels.
*/
static void check(const Matrix& mat)
{
#ifdef DUNE_ISTL_WITH_CHECKING
typedef typename Matrix::ConstRowIterator Row;
typedef typename Matrix::ConstColIterator Entry;
for(Row row = mat.begin(); row!=mat.end(); ++row){
Entry diagonal = row->find(row.index());
if(diagonal==row->end())
DUNE_THROW(ISTLError, "Missing diagonal value in row "<<row.index()
<<" at block recursion level "<<l-blocklevel);
else
CheckIfDiagonalPresent<typename Matrix::block_type,blocklevel-1,l>::check(*diagonal);
}
#endif
}
};
template<class Matrix, std::size_t l>
struct CheckIfDiagonalPresent<Matrix,0,l>
{
static void check(const Matrix& mat)
{
typedef typename Matrix::ConstRowIterator Row;
for(Row row = mat.begin(); row!=mat.end(); ++row){
if(row->find(row.index())==row->end())
DUNE_THROW(ISTLError, "Missing diagonal value in row "<<row.index()
<<" at block recursion level "<<l);
}
}
};
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9>
class MultiTypeBlockMatrix;
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9, std::size_t blocklevel, std::size_t l>
struct CheckIfDiagonalPresent<MultiTypeBlockMatrix<T1,T2,T3,T4,T5,T6,T7,T8,T9>,
blocklevel,l>
{
typedef MultiTypeBlockMatrix<T1,T2,T3,T4,T5,T6,T7,T8,T9> Matrix;
/**
* @brief Check whether the a matrix has diagonal values
* on blocklevel recursion levels.
*/
static void check(const Matrix& mat)
{
#ifdef DUNE_ISTL_WITH_CHECKING
// TODO Implement check
#endif
}
};
/**
* @brief Get the number of nonzero fields in the matrix.
*
* This is not the number of nonzero blocks, but the number of non
* zero scalar entries (on blocklevel 1) if the matrix is viewed as
* a flat matrix.
*
* For FieldMatrix this is simply the number of columns times the
* number of rows, for a BCRSMatrix<FieldMatrix<K,n,m>> this is the
* number of nonzero blocks time n*m.
*/
template<class M>
inline int countNonZeros(const M& matrix)
{
return NonZeroCounter<M::blocklevel>::count(matrix);
}
/*
template<class M>
struct ProcessOnFieldsOfMatrix
*/
/** @} */
namespace
{
struct CompPair{
template<class G,class M>
bool operator()(const std::pair<G,M>& p1, const std::pair<G,M>& p2)
{
return p1.first<p2.first;
}
};
}
template<class M, class C>
void printGlobalSparseMatrix(const M& mat, C& ooc, std::ostream& os)
{
typedef typename C::ParallelIndexSet::const_iterator IIter;
typedef typename C::OwnerSet OwnerSet;
typedef typename C::ParallelIndexSet::GlobalIndex GlobalIndex;
GlobalIndex gmax=0;
for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end();
idx!=eidx; ++idx)
gmax=std::max(gmax,idx->global());
gmax=ooc.communicator().max(gmax);
ooc.buildGlobalLookup();
for(IIter idx=ooc.indexSet().begin(), eidx=ooc.indexSet().end();
idx!=eidx; ++idx){
if(OwnerSet::contains(idx->local().attribute()))
{
typedef typename M::block_type Block;
std::set<std::pair<GlobalIndex,Block>,CompPair> entries;
// sort rows
typedef typename M::ConstColIterator CIter;
for(CIter c=mat[idx->local()].begin(), cend=mat[idx->local()].end();
c!=cend; ++c){
const typename C::ParallelIndexSet::IndexPair* pair
=ooc.globalLookup().pair(c.index());
assert(pair);
entries.insert(std::make_pair(pair->global(), *c));
}
//wait until its the rows turn.
GlobalIndex rowidx = idx->global();
GlobalIndex cur=std::numeric_limits<GlobalIndex>::max();
while(cur!=rowidx)
cur=ooc.communicator().min(rowidx);
// print rows
typedef typename std::set<std::pair<GlobalIndex,Block>,CompPair>::iterator SIter;
for(SIter s=entries.begin(), send=entries.end(); s!=send; ++s)
os<<idx->global()<<" "<<s->first<<" "<<s->second<<std::endl;
}
}
ooc.freeGlobalLookup();
// Wait until everybody is finished
GlobalIndex cur=std::numeric_limits<GlobalIndex>::max();
while(cur!=ooc.communicator().min(cur));
}
template<typename M>
struct MatrixDimension
{
};
template<typename B, typename TA>
struct MatrixDimension<BCRSMatrix<B,TA> >
{
typedef BCRSMatrix<B,TA> Matrix;
typedef typename Matrix::block_type block_type;
typedef typename Matrix::size_type size_type;
static size_type rowdim (const Matrix& A, size_type i)
{
const B* row = A.r[i].getptr();
if(row)
return MatrixDimension<block_type>::rowdim(*row);
else
return 0;
}
static size_type coldim (const Matrix& A, size_type c)
{
// find an entry in column c
if (A.nnz>0)
{
for (size_type k=0; k<A.nnz; k++) {
if (A.j.get()[k]==c) {
return MatrixDimension<block_type>::coldim(A.a[k]);
}
}
}
else
{
for (size_type i=0; i<A.N(); i++)
{
size_type* j = A.r[i].getindexptr();
B* a = A.r[i].getptr();
for (size_type k=0; k<A.r[i].getsize(); k++)
if (j[k]==c) {
return MatrixDimension<block_type>::coldim(a[k]);
}
}
}
// not found
return 0;
}
static size_type rowdim (const Matrix& A){
size_type nn=0;
for (size_type i=0; i<A.N(); i++)
nn += rowdim(A,i);
return nn;
}
static size_type coldim (const Matrix& A){
typedef typename Matrix::ConstRowIterator ConstRowIterator;
typedef typename Matrix::ConstColIterator ConstColIterator;
// The following code has a complexity of nnz, and
// typically a very small constant.
//
std::vector<size_type> coldims(A.M(),
std::numeric_limits<size_type>::max());
for (ConstRowIterator row=A.begin(); row!=A.end(); ++row)
for (ConstColIterator col=row->begin(); col!=row->end(); ++col)
// only compute blocksizes we don't already have
if (coldims[col.index()]==std::numeric_limits<size_type>::max())
coldims[col.index()] = MatrixDimension<block_type>::coldim(*col);
size_type sum = 0;
for (typename std::vector<size_type>::iterator it=coldims.begin();
it!=coldims.end(); ++it)
// skip rows for which no coldim could be determined
if ((*it)>=0)
sum += *it;
return sum;
}
};
template<typename B, int n, int m, typename TA>
struct MatrixDimension<BCRSMatrix<FieldMatrix<B,n,m> ,TA> >
{
typedef BCRSMatrix<FieldMatrix<B,n,m> ,TA> Matrix;
typedef typename Matrix::size_type size_type;
static size_type rowdim (const Matrix& A, size_type i)
{
return n;
}
static size_type coldim (const Matrix& A, size_type c)
{
return m;
}
static size_type rowdim (const Matrix& A) {
return A.N()*n;
}
static size_type coldim (const Matrix& A) {
return A.M()*m;
}
};
template<typename K, int n, int m>
struct MatrixDimension<FieldMatrix<K,n,m> >
{
typedef FieldMatrix<K,n,m> Matrix;
typedef typename Matrix::size_type size_type;
static size_type rowdim(const Matrix& A, size_type r)
{
return 1;
}
static size_type coldim(const Matrix& A, size_type r)
{
return 1;
}
static size_type rowdim(const Matrix& A)
{
return n;
}
static size_type coldim(const Matrix& A)
{
return m;
}
};
template<typename K, int n, int m, typename TA>
struct MatrixDimension<Matrix<FieldMatrix<K,n,m>, TA> >
{
typedef Matrix<FieldMatrix<K,n,m>, TA> ThisMatrix;
typedef typename ThisMatrix::size_type size_type;
static size_type rowdim(const ThisMatrix& A, size_type r)
{
return n;
}
static size_type coldim(const ThisMatrix& A, size_type r)
{
return m;
}
static size_type rowdim(const ThisMatrix& A)
{
return A.N()*n;
}
static size_type coldim(const ThisMatrix& A)
{
return A.M()*m;
}
};
/**
* @brief Test whether a type is an ISTL Matrix
*/
template<typename T>
struct IsMatrix
{
enum{
/**
* @brief True if T is an ISTL matrix
*/
value = false
};
};
template<typename T>
struct IsMatrix<DenseMatrix<T> >
{
enum{
/**
* @brief True if T is an ISTL matrix
*/
value = true
};
};
template<typename T, typename A>
struct IsMatrix<BCRSMatrix<T,A> >
{
enum{
/**
* @brief True if T is an ISTL matrix
*/
value = true
};
};
}
#endif
|