| 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 <iostream> |
| 6 |
|
| 7 |
#include <dune/common/fmatrix.hh> |
| 8 |
#include <dune/common/fvector.hh> |
| 9 |
#include <dune/common/indices.hh> |
| 10 |
#include <dune/common/timer.hh> |
| 11 |
#include <dune/common/test/testsuite.hh> |
| 12 |
#include <dune/istl/bvector.hh> |
| 13 |
#include <dune/istl/umfpack.hh> |
| 14 |
|
| 15 |
#include "laplacian.hh" |
| 16 |
|
| 17 |
using namespace Dune; |
| 18 |
|
| 19 |
template<typename Matrix, typename Vector, typename BitVector> |
| 20 |
TestSuite runUMFPack(std::size_t N) |
| 21 |
{ |
| 22 |
TestSuite t; |
| 23 |
|
| 24 |
Matrix mat; |
| 25 |
Vector b(N*N), x(N*N), b1(N/2), x1(N/2); |
| 26 |
|
| 27 |
// with additional diagonal regularization |
| 28 |
setupLaplacian(mat,N,100.0); |
| 29 |
|
| 30 |
b=1; |
| 31 |
b1=1; |
| 32 |
x=0; |
| 33 |
x1=0; |
| 34 |
|
| 35 |
Dune::Timer watch; |
| 36 |
|
| 37 |
watch.reset(); |
| 38 |
|
| 39 |
Dune::UMFPack<Matrix> solver(mat,1); |
| 40 |
|
| 41 |
Dune::InverseOperatorResult res; |
| 42 |
|
| 43 |
solver.apply(x, b, res); |
| 44 |
solver.free(); |
| 45 |
|
| 46 |
// test |
| 47 |
auto norm = b.two_norm(); |
| 48 |
mat.mmv(x,b); |
| 49 |
t.check( b.two_norm()/norm < 1e-11 ) << " Error in UMFPACK, relative residual is too large: " << b.two_norm()/norm; |
| 50 |
|
| 51 |
Dune::UMFPack<Matrix> solver1; |
| 52 |
|
| 53 |
std::set<typename Matrix::size_type> mrs; |
| 54 |
BitVector bitVector(N*N); |
| 55 |
bitVector = true; |
| 56 |
for(typename Matrix::size_type s=0; s < N/2; ++s) |
| 57 |
{ |
| 58 |
mrs.insert(s); |
| 59 |
bitVector[s] = false; |
| 60 |
} |
| 61 |
|
| 62 |
solver1.setMatrix(mat,bitVector); |
| 63 |
solver1.apply(x1,b1, res); |
| 64 |
|
| 65 |
// test |
| 66 |
x=0; |
| 67 |
b=0; |
| 68 |
for( typename Matrix::size_type i=0; i<N/2; i++ ) |
| 69 |
{ |
| 70 |
// set subindices |
| 71 |
x[i] = x1[i]; |
| 72 |
b[i] = b1[i]; |
| 73 |
} |
| 74 |
|
| 75 |
norm = b.two_norm(); |
| 76 |
mat.mmv(x,b); |
| 77 |
|
| 78 |
// truncate deactivated indices |
| 79 |
for( std::size_t i=N/2; i<N*N; i++ ) |
| 80 |
b[i] = 0; |
| 81 |
|
| 82 |
t.check( b.two_norm()/norm < 1e-11 ) << " Error in UMFPACK, relative residual is too large: " << b.two_norm()/norm; |
| 83 |
|
| 84 |
// compare with setSubMatrix |
| 85 |
solver1.setSubMatrix(mat,mrs); |
| 86 |
solver1.setVerbosity(true); |
| 87 |
|
| 88 |
auto x2 = x1; |
| 89 |
x2 = 0; |
| 90 |
solver1.apply(x2,b1, res); |
| 91 |
|
| 92 |
x2 -= x1; |
| 93 |
t.check( x2.two_norm()/x1.two_norm() < 1e-11 ) << " Error in UMFPACK, setSubMatrix yields different result as setMatrix with BitVector, relative diff: " << x2.two_norm()/x1.two_norm(); |
| 94 |
|
| 95 |
|
| 96 |
solver1.apply(reinterpret_cast<typename Matrix::field_type*>(&x1[0]), |
| 97 |
reinterpret_cast<typename Matrix::field_type*>(&b1[0])); |
| 98 |
|
| 99 |
Dune::UMFPack<Matrix> save_solver(mat,"umfpack_decomp",0); |
| 100 |
Dune::UMFPack<Matrix> load_solver(mat,"umfpack_decomp",0); |
| 101 |
|
| 102 |
return t; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
template<typename Matrix, typename Vector, typename BitVector> |
| 108 |
TestSuite runUMFPackMultitype2x2(std::size_t N) |
| 109 |
{ |
| 110 |
TestSuite t; |
| 111 |
|
| 112 |
using namespace Dune::Indices; |
| 113 |
|
| 114 |
Matrix mat; |
| 115 |
|
| 116 |
Vector b,x,b1,x1; |
| 117 |
|
| 118 |
b[_0].resize(N*N); |
| 119 |
b[_1].resize(N*N); |
| 120 |
|
| 121 |
x[_0].resize(N*N); |
| 122 |
x[_1].resize(N*N); |
| 123 |
|
| 124 |
b1[_0].resize(N/2); |
| 125 |
b1[_1].resize(N/2); |
| 126 |
x1[_0].resize(N/2); |
| 127 |
x1[_1].resize(N/2); |
| 128 |
|
| 129 |
|
| 130 |
// initialize all 4 matrix blocks |
| 131 |
setupLaplacian(mat[_0][_0],N,100.0); // regularize the diagonal block |
| 132 |
setupLaplacian(mat[_0][_1],N); |
| 133 |
setupLaplacian(mat[_1][_0],N); |
| 134 |
setupLaplacian(mat[_1][_1],N,200.0); // regularize the diagonal block |
| 135 |
|
| 136 |
b=1.0; |
| 137 |
b1=1.0; |
| 138 |
x=0.0; |
| 139 |
x1=0.0; |
| 140 |
|
| 141 |
Dune::Timer watch; |
| 142 |
|
| 143 |
watch.reset(); |
| 144 |
|
| 145 |
Dune::UMFPack<Matrix> solver(mat,1); |
| 146 |
|
| 147 |
Dune::InverseOperatorResult res; |
| 148 |
|
| 149 |
solver.apply(x, b, res); |
| 150 |
solver.free(); |
| 151 |
|
| 152 |
// test |
| 153 |
auto norm = b.two_norm(); |
| 154 |
mat.mmv(x,b); |
| 155 |
t.check( b.two_norm()/norm < 1e-11 ) << " Error in UMFPACK, relative residual is too large: " << b.two_norm()/norm; |
| 156 |
|
| 157 |
Dune::UMFPack<Matrix> solver1; |
| 158 |
|
| 159 |
BitVector bitVector; |
| 160 |
bitVector[_0].resize(N*N); |
| 161 |
bitVector[_1].resize(N*N); |
| 162 |
|
| 163 |
bitVector = true; |
| 164 |
for(std::size_t s=0; s < N/2; ++s) |
| 165 |
{ |
| 166 |
bitVector[_0][s] = false; |
| 167 |
bitVector[_1][s] = false; |
| 168 |
} |
| 169 |
|
| 170 |
solver1.setMatrix(mat,bitVector); |
| 171 |
solver1.apply(x1,b1, res); |
| 172 |
|
| 173 |
// test |
| 174 |
x=0; |
| 175 |
b=0; |
| 176 |
for( std::size_t i=0; i<N/2; i++ ) |
| 177 |
{ |
| 178 |
// set subindices |
| 179 |
x[_0][i] = x1[_0][i]; |
| 180 |
x[_1][i] = x1[_1][i]; |
| 181 |
b[_0][i] = b1[_0][i]; |
| 182 |
b[_1][i] = b1[_1][i]; |
| 183 |
} |
| 184 |
|
| 185 |
norm = b.two_norm(); |
| 186 |
mat.mmv(x,b); |
| 187 |
|
| 188 |
// truncate deactivated indices |
| 189 |
for( std::size_t i=N/2; i<N*N; i++ ) |
| 190 |
{ |
| 191 |
b[_0][i] = 0; |
| 192 |
b[_1][i] = 0; |
| 193 |
} |
| 194 |
|
| 195 |
t.check( b.two_norm()/norm < 1e-11 ) << " Error in UMFPACK, relative residual is too large: " << b.two_norm()/norm; |
| 196 |
|
| 197 |
return t; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
int main(int argc, char** argv) try |
| 202 |
{ |
| 203 |
#if HAVE_SUITESPARSE_UMFPACK |
| 204 |
|
| 205 |
TestSuite t; |
| 206 |
std::size_t N=100; |
| 207 |
|
| 208 |
if(argc>1) |
| 209 |
N = atoi(argv[1]); |
| 210 |
|
| 211 |
// ------------------------------------------------------------------------------ |
| 212 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<double>"<<std::endl; |
| 213 |
{ |
| 214 |
using Matrix = Dune::BCRSMatrix<double>; |
| 215 |
using Vector = Dune::BlockVector<double>; |
| 216 |
using BitVector = Dune::BlockVector<int>; |
| 217 |
t.subTest(runUMFPack<Matrix,Vector,BitVector>(N)); |
| 218 |
} |
| 219 |
|
| 220 |
// ------------------------------------------------------------------------------ |
| 221 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<FieldMatrix<double,1,1> >"<<std::endl; |
| 222 |
{ |
| 223 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> >; |
| 224 |
using Vector = Dune::BlockVector<Dune::FieldVector<double,1> >; |
| 225 |
using BitVector = Dune::BlockVector<Dune::FieldVector<int,1> >; |
| 226 |
t.subTest(runUMFPack<Matrix,Vector,BitVector>(N)); |
| 227 |
} |
| 228 |
|
| 229 |
// ------------------------------------------------------------------------------ |
| 230 |
std::cout<<"testing for N="<<N<<" BCRSMatrix<FieldMatrix<double,2,2> >"<<std::endl; |
| 231 |
{ |
| 232 |
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double,2,2> >; |
| 233 |
using Vector = Dune::BlockVector<Dune::FieldVector<double,2> >; |
| 234 |
using BitVector = Dune::BlockVector<Dune::FieldVector<int,2> >; |
| 235 |
t.subTest(runUMFPack<Matrix,Vector,BitVector>(N)); |
| 236 |
} |
| 237 |
|
| 238 |
// ------------------------------------------------------------------------------ |
| 239 |
std::cout<<"testing for N="<<N<<" MultiTypeBlockMatrix<BCRSMatrix<FieldMatrix<double,2,2>> ... >"<<std::endl; |
| 240 |
{ |
| 241 |
using MatrixBlock = Dune::BCRSMatrix<Dune::FieldMatrix<double,2,2> >; |
| 242 |
using BlockRow = Dune::MultiTypeBlockVector<MatrixBlock,MatrixBlock>; |
| 243 |
using Matrix = Dune::MultiTypeBlockMatrix<BlockRow,BlockRow>; |
| 244 |
|
| 245 |
using VectorBlock = Dune::BlockVector<Dune::FieldVector<double,2> >; |
| 246 |
using Vector = Dune::MultiTypeBlockVector<VectorBlock,VectorBlock>; |
| 247 |
|
| 248 |
using BitVectorBlock = Dune::BlockVector<Dune::FieldVector<int,2> >; |
| 249 |
using BitVector = Dune::MultiTypeBlockVector<BitVectorBlock,BitVectorBlock>; |
| 250 |
|
| 251 |
t.subTest(runUMFPackMultitype2x2<Matrix,Vector,BitVector>(N)); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
return t.exit(); |
| 257 |
#else // HAVE_SUITESPARSE_UMFPACK |
| 258 |
std::cerr << "You need SuiteSparse's UMFPack to run this test." << std::endl; |
| 259 |
return 77; |
| 260 |
#endif // HAVE_SUITESPARSE_UMFPACK |
| 261 |
} |
| 262 |
catch (std::exception &e) |
| 263 |
{ |
| 264 |
std::cout << "ERROR: " << e.what() << std::endl; |
| 265 |
return 1; |
| 266 |
} |