| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#include <dune/istl/bcrsmatrix.hh> |
| 6 |
#include <dune/common/fmatrix.hh> |
| 7 |
#include <dune/common/exceptions.hh> |
| 8 |
#include <dune/common/test/testsuite.hh> |
| 9 |
#include <dune/istl/io.hh> |
| 10 |
|
| 11 |
template<class M> |
| 12 |
std::size_t computeNNZ(M&& matrix) |
| 13 |
{ |
| 14 |
std::size_t nnz = 0; |
| 15 |
for (auto&& row : matrix) |
| 16 |
for ([[maybe_unused]] auto&& entry : row) |
| 17 |
++nnz; |
| 18 |
return nnz; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
template<class M> |
| 23 |
struct Builder |
| 24 |
{ |
| 25 |
void randomBuild(int m, int n) |
| 26 |
{ |
| 27 |
DUNE_THROW(Dune::NotImplemented, "No specialization"); |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
template<class B, class A> |
| 33 |
struct Builder<Dune::BCRSMatrix<B,A> > |
| 34 |
{ |
| 35 |
Dune::TestSuite randomBuild(int rows, int cols) |
| 36 |
{ |
| 37 |
Dune::TestSuite testSuite("BCRSMatrix::random"); |
| 38 |
|
| 39 |
int maxNZCols = 15; // maximal number of nonzeros per row |
| 40 |
{ |
| 41 |
Dune::BCRSMatrix<B,A> matrix( rows, cols, Dune::BCRSMatrix<B,A>::random ); |
| 42 |
for(int i=0; i<rows; ++i) |
| 43 |
matrix.setrowsize(i,maxNZCols); |
| 44 |
matrix.endrowsizes(); |
| 45 |
|
| 46 |
// During setup, or before, if there is no other way. |
| 47 |
for(int i=0; i<rows; ++i) { |
| 48 |
if(i<cols) |
| 49 |
matrix.addindex(i,i); |
| 50 |
if(i-1>=0) |
| 51 |
matrix.addindex(i,i-1); |
| 52 |
if(i+1<cols) |
| 53 |
matrix.addindex(i,i+1); |
| 54 |
} |
| 55 |
|
| 56 |
matrix.endindices(); |
| 57 |
matrix=1; |
| 58 |
|
| 59 |
Dune::printmatrix(std::cout, matrix, "random", "row"); |
| 60 |
Dune::BCRSMatrix<B,A> matrix1(matrix); |
| 61 |
Dune::printmatrix(std::cout, matrix, "random", "row"); |
| 62 |
|
| 63 |
auto nnz = computeNNZ(matrix); |
| 64 |
testSuite.check(nnz == matrix.nonzeroes(), "nnz after building") |
| 65 |
<< "BCRSMatrix::nonzeroes() returns " << matrix.nonzeroes() << " while there are " << nnz << " stored entries."; |
| 66 |
} |
| 67 |
/*{ |
| 68 |
|
| 69 |
Dune::BCRSMatrix<B,A> matrix( rows, cols, rows*maxNZCols, Dune::BCRSMatrix<B,A>::random ); |
| 70 |
for(int i=0; i<rows; ++i) |
| 71 |
matrix.setrowsize(i,maxNZCols); |
| 72 |
matrix.endrowsizes(); |
| 73 |
|
| 74 |
for(int i=0; i<rows; ++i){ |
| 75 |
if(i<cols) |
| 76 |
matrix.addindex(i,i); |
| 77 |
if(i-1>=0) |
| 78 |
matrix.addindex(i,i-1); |
| 79 |
if(i+1<cols) |
| 80 |
matrix.addindex(i,i+1); |
| 81 |
} |
| 82 |
matrix.endrowsizes(); |
| 83 |
|
| 84 |
Dune::printmatrix(std::cout, matrix, "random", "row"); |
| 85 |
}*/ |
| 86 |
return testSuite; |
| 87 |
} |
| 88 |
|
| 89 |
void rowWiseBuild(Dune::TestSuite& testSuite, Dune::BCRSMatrix<B,A>& matrix, int /* rows */, int cols) |
| 90 |
{ |
| 91 |
for(typename Dune::BCRSMatrix<B,A>::CreateIterator ci=matrix.createbegin(), cend=matrix.createend(); |
| 92 |
ci!=cend; ++ci) |
| 93 |
{ |
| 94 |
int i=ci.index(); |
| 95 |
if(i<cols) |
| 96 |
ci.insert(i); |
| 97 |
if(i-1>=0 && i-1<cols) |
| 98 |
ci.insert(i-1); |
| 99 |
if(i+1<cols) |
| 100 |
ci.insert(i+1); |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
auto nnz = computeNNZ(matrix); |
| 105 |
testSuite.check(nnz == matrix.nonzeroes(), "nnz after building") |
| 106 |
<< "BCRSMatrix::nonzeroes() returns " << matrix.nonzeroes() << " while there are " << nnz << " stored entries."; |
| 107 |
} |
| 108 |
|
| 109 |
Dune::printmatrix(std::cout, matrix, "row_wise", "row"); |
| 110 |
// test copy ctor |
| 111 |
Dune::BCRSMatrix<B,A> matrix1(matrix); |
| 112 |
Dune::printmatrix(std::cout, matrix1, "row_wise", "row"); |
| 113 |
|
| 114 |
{ |
| 115 |
auto nnz = computeNNZ(matrix1); |
| 116 |
testSuite.check(nnz == matrix1.nonzeroes(), "nnz after constructed from") |
| 117 |
<< "BCRSMatrix::nonzeroes() returns " << matrix1.nonzeroes() << " while there are " << nnz << " stored entries."; |
| 118 |
} |
| 119 |
|
| 120 |
// test copy assignment |
| 121 |
Dune::BCRSMatrix<B,A> matrix2; |
| 122 |
matrix2 = matrix; |
| 123 |
Dune::printmatrix(std::cout, matrix2, "row_wise", "row"); |
| 124 |
|
| 125 |
{ |
| 126 |
auto nnz = computeNNZ(matrix2); |
| 127 |
testSuite.check(nnz == matrix2.nonzeroes(), "nnz after assigned from") |
| 128 |
<< "BCRSMatrix::nonzeroes() returns " << matrix2.nonzeroes() << " while there are " << nnz << " stored entries."; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
Dune::TestSuite rowWiseBuild(int rows, int cols) |
| 133 |
{ |
| 134 |
Dune::TestSuite testSuite("BCRSMatrix::row_wise without nnz"); |
| 135 |
Dune::BCRSMatrix<B,A> matrix( rows, cols, Dune::BCRSMatrix<B,A>::row_wise ); |
| 136 |
rowWiseBuild(testSuite, matrix, rows, cols); |
| 137 |
return testSuite; |
| 138 |
} |
| 139 |
|
| 140 |
Dune::TestSuite rowWiseBuild(int rows, int cols, int nnz) |
| 141 |
{ |
| 142 |
Dune::TestSuite testSuite("BCRSMatrix::row_wise with nnz"); |
| 143 |
Dune::BCRSMatrix<B,A> matrix( rows, cols, nnz, Dune::BCRSMatrix<B,A>::row_wise ); |
| 144 |
rowWiseBuild(testSuite, matrix, rows, cols); |
| 145 |
return testSuite; |
| 146 |
} |
| 147 |
|
| 148 |
}; |
| 149 |
|
| 150 |
// This code used to trigger a valgrind 'uninitialized memory' warning; see FS 1041 |
| 151 |
void testDoubleSetSize() |
| 152 |
{ |
| 153 |
Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > foo; |
| 154 |
foo.setSize(5,5); |
| 155 |
foo.setSize(5,5); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
int main() |
| 160 |
{ |
| 161 |
Dune::TestSuite testSuite; |
| 162 |
|
| 163 |
Builder<Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > > builder; |
| 164 |
|
| 165 |
testSuite.subTest(builder.randomBuild(5,4)); |
| 166 |
testSuite.subTest(builder.rowWiseBuild(5,4,13)); |
| 167 |
testSuite.subTest(builder.rowWiseBuild(5,4)); |
| 168 |
|
| 169 |
testDoubleSetSize(); |
| 170 |
|
| 171 |
return testSuite.exit(); |
| 172 |
} |