| 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 |
/** |
| 6 |
* \file |
| 7 |
* \brief Test the MultiTypeBlockMatrix data structure |
| 8 |
*/ |
| 9 |
#include <iostream> |
| 10 |
|
| 11 |
#include <dune/common/exceptions.hh> |
| 12 |
#include <dune/common/fvector.hh> |
| 13 |
#include <dune/common/fmatrix.hh> |
| 14 |
#include <dune/common/indices.hh> |
| 15 |
|
| 16 |
#include <dune/istl/matrix.hh> |
| 17 |
#include <dune/istl/bvector.hh> |
| 18 |
#include <dune/istl/io.hh> |
| 19 |
#include <dune/istl/operators.hh> |
| 20 |
#include <dune/istl/preconditioners.hh> |
| 21 |
#include <dune/istl/solvers.hh> |
| 22 |
#include <dune/istl/multitypeblockvector.hh> |
| 23 |
#include <dune/istl/multitypeblockmatrix.hh> |
| 24 |
#include <dune/istl/test/matrixtest.hh> |
| 25 |
|
| 26 |
using namespace Dune; |
| 27 |
|
| 28 |
// Import the static constants _0, _1, etc |
| 29 |
using namespace Indices; |
| 30 |
|
| 31 |
// Test whether we can use std::tuple_element |
| 32 |
template<typename... Args> |
| 33 |
void testTupleElement(const MultiTypeBlockMatrix<Args...>& multiMatrix) |
| 34 |
{ |
| 35 |
// Do std::tuple_element and operator[] return the same type? |
| 36 |
using TupleElementType = typename std::tuple_element<0, MultiTypeBlockMatrix<Args...> >::type; |
| 37 |
using BracketType = decltype(multiMatrix[_0]); |
| 38 |
|
| 39 |
// As the return type of const operator[], BracketType will always |
| 40 |
// be a const reference. We cannot simply strip the const and the &, |
| 41 |
// because entries of a MultiTypeBlockMatrix can be references themselves. |
| 42 |
// Therefore, always add const& to the result of std::tuple_element as well. |
| 43 |
constexpr bool sameType = std::is_same_v<const TupleElementType&,BracketType>; |
| 44 |
static_assert(sameType, "std::tuple_element does not provide the type of the 0th MultiTypeBlockMatrix row!"); |
| 45 |
} |
| 46 |
|
| 47 |
void testInterfaceMethods() |
| 48 |
{ |
| 49 |
{ |
| 50 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 |
// First, we test a MultiTypeBlockMatrix consisting of an array of 2x2 dense matrices. |
| 52 |
// The upper left dense matrix has dense 3x3 blocks, the lower right matrix has 1x1 blocks, |
| 53 |
// the off-set diagonal matrix block sizes are set accordingly. |
| 54 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 |
|
| 56 |
// set up the test matrix |
| 57 |
typedef MultiTypeBlockVector<Matrix<FieldMatrix<double,3,3> >, Matrix<FieldMatrix<double,3,1> > > RowType0; |
| 58 |
typedef MultiTypeBlockVector<Matrix<FieldMatrix<double,1,3> >, Matrix<FieldMatrix<double,1,1> > > RowType1; |
| 59 |
|
| 60 |
MultiTypeBlockMatrix<RowType0,RowType1> multiMatrix; |
| 61 |
|
| 62 |
multiMatrix[_0][_0].setSize(3,3); |
| 63 |
multiMatrix[_0][_1].setSize(3,2); |
| 64 |
multiMatrix[_1][_0].setSize(2,3); |
| 65 |
multiMatrix[_1][_1].setSize(2,2); |
| 66 |
|
| 67 |
// Init with scalar values |
| 68 |
multiMatrix[_0][_0] = 4200; |
| 69 |
multiMatrix[_0][_1] = 4201; |
| 70 |
multiMatrix[_1][_0] = 4210; |
| 71 |
multiMatrix[_1][_1] = 4211; |
| 72 |
|
| 73 |
// Set up a test vector |
| 74 |
MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<FieldVector<double,1> > > domainVector; |
| 75 |
|
| 76 |
domainVector[_0] = {{1,0,0}, |
| 77 |
{0,1,0}, |
| 78 |
{0,0,1}}; |
| 79 |
|
| 80 |
domainVector[_1] = {3.14, 42}; |
| 81 |
|
| 82 |
auto rangeVector = domainVector; // Range == Domain, because the matrix nesting pattern is symmetric |
| 83 |
|
| 84 |
// Test vector space operations |
| 85 |
testVectorSpaceOperations(multiMatrix); |
| 86 |
|
| 87 |
// Test matrix norms |
| 88 |
testNorms(multiMatrix); |
| 89 |
|
| 90 |
// Test whether matrix class has the required constructors |
| 91 |
testMatrixConstructibility<decltype(multiMatrix)>(); |
| 92 |
|
| 93 |
// Test matrix-vector products |
| 94 |
testMatrixVectorProducts(multiMatrix, domainVector, rangeVector); |
| 95 |
|
| 96 |
// Test whether std::tuple_element works |
| 97 |
testTupleElement(multiMatrix); |
| 98 |
} |
| 99 |
|
| 100 |
{ |
| 101 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 102 |
// We again test a MultiTypeBlockMatrix consisting of an array of 2x2 sparse matrices. |
| 103 |
// The upper left matrix has dense 3x3 blocks, the lower right matrix has scalar entries |
| 104 |
// the off-set diagonal matrix block sizes are set accordingly. |
| 105 |
// Unlike in the previous test, the scalar entries in the lower-right block are actually |
| 106 |
// scalars, not FieldMatrices of size 1x1. |
| 107 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 108 |
|
| 109 |
// set up the test matrix |
| 110 |
typedef MultiTypeBlockVector<Matrix<FieldMatrix<double,3,3> >, Matrix<FieldMatrix<double,3,1> > > RowType0; |
| 111 |
typedef MultiTypeBlockVector<Matrix<FieldMatrix<double,1,3> >, Matrix<double> > RowType1; |
| 112 |
|
| 113 |
MultiTypeBlockMatrix<RowType0,RowType1> multiMatrix; |
| 114 |
|
| 115 |
multiMatrix[_0][_0].setSize(3,3); |
| 116 |
multiMatrix[_0][_1].setSize(3,2); |
| 117 |
multiMatrix[_1][_0].setSize(2,3); |
| 118 |
multiMatrix[_1][_1].setSize(2,2); |
| 119 |
|
| 120 |
// Init with scalar values |
| 121 |
multiMatrix[_0][_0] = 4200; |
| 122 |
multiMatrix[_0][_1] = 4201; |
| 123 |
multiMatrix[_1][_0] = 4210; |
| 124 |
multiMatrix[_1][_1] = 4211; |
| 125 |
|
| 126 |
// Set up a test vector |
| 127 |
MultiTypeBlockVector<BlockVector<FieldVector<double,3> >, BlockVector<double> > domainVector; |
| 128 |
|
| 129 |
domainVector[_0] = {{1,0,0}, |
| 130 |
{0,1,0}, |
| 131 |
{0,0,1}}; |
| 132 |
|
| 133 |
domainVector[_1] = {3.14, 42}; |
| 134 |
|
| 135 |
auto rangeVector = domainVector; // Range == Domain, because the matrix nesting pattern is symmetric |
| 136 |
|
| 137 |
// Test vector space operations |
| 138 |
testVectorSpaceOperations(multiMatrix); |
| 139 |
|
| 140 |
// Test whether matrix class has the required constructors |
| 141 |
testMatrixConstructibility<decltype(multiMatrix)>(); |
| 142 |
|
| 143 |
// Test matrix-vector products |
| 144 |
testMatrixVectorProducts(multiMatrix, domainVector, rangeVector); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
int main(int argc, char** argv) try |
| 149 |
{ |
| 150 |
// Run the standard tests for the dune-istl matrix interface |
| 151 |
testInterfaceMethods(); |
| 152 |
|
| 153 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 154 |
// Next test: Set up a linear system with a matrix consisting of 2x2 sparse scalar matrices. |
| 155 |
// Solve the linear system with a simple iterative scheme. |
| 156 |
// \todo What's the point if all four matrices have the same type? |
| 157 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 158 |
std::cout << "Jacobi Solver Test on MultiTypeBlockMatrix<BCRSMatrix>"; |
| 159 |
|
| 160 |
typedef Dune::FieldMatrix<double,1,1> LittleBlock; //matrix block type |
| 161 |
typedef Dune::BCRSMatrix<LittleBlock> BCRSMat; //matrix type |
| 162 |
|
| 163 |
const int X1=3; //index bounds of all four matrices |
| 164 |
const int X2=2; |
| 165 |
const int Y1=3; |
| 166 |
const int Y2=2; |
| 167 |
BCRSMat A11 = BCRSMat(X1,Y1,X1*Y1,BCRSMat::random); //A11 is 3x3 |
| 168 |
BCRSMat A12 = BCRSMat(X1,Y2,X1*Y2,BCRSMat::random); //A12 is 2x3 |
| 169 |
BCRSMat A21 = BCRSMat(X2,Y1,X2*Y1,BCRSMat::random); //A11 is 3x2 |
| 170 |
BCRSMat A22 = BCRSMat(X2,Y2,X2*Y2,BCRSMat::random); //A12 is 2x2 |
| 171 |
|
| 172 |
typedef Dune::MultiTypeBlockVector<Dune::BlockVector<Dune::FieldVector<double,1> >,Dune::BlockVector<Dune::FieldVector<double,1> > > TestVector; |
| 173 |
TestVector x, b; |
| 174 |
|
| 175 |
x[_0].resize(Y1); |
| 176 |
x[_1].resize(Y2); |
| 177 |
b[_0].resize(X1); |
| 178 |
b[_1].resize(X2); |
| 179 |
|
| 180 |
x = 1; b = 1; |
| 181 |
|
| 182 |
//set row sizes |
| 183 |
for (int i=0; i<Y1; i++) |
| 184 |
{ |
| 185 |
A11.setrowsize(i,X1); |
| 186 |
A12.setrowsize(i,X2); |
| 187 |
} |
| 188 |
A11.endrowsizes(); |
| 189 |
A12.endrowsizes(); |
| 190 |
|
| 191 |
for (int i=0; i<Y2; i++) |
| 192 |
{ |
| 193 |
A21.setrowsize(i,X1); |
| 194 |
A22.setrowsize(i,X2); |
| 195 |
} |
| 196 |
A21.endrowsizes(); |
| 197 |
A22.endrowsizes(); |
| 198 |
|
| 199 |
//set indices |
| 200 |
for (int i=0; i<X1+X2; i++) |
| 201 |
{ |
| 202 |
for (int j=0; j<Y1+Y2; j++) |
| 203 |
{ |
| 204 |
if (i<X1 && j<Y1) |
| 205 |
A11.addindex(i,j); |
| 206 |
if (i<X1 && j>=Y1) |
| 207 |
A12.addindex(i,j-Y1); |
| 208 |
if (i>=X1 && j<Y1) |
| 209 |
A21.addindex(i-X1,j); |
| 210 |
if (i>=X1 && j>=Y1) |
| 211 |
A22.addindex(i-X1,j-Y1); |
| 212 |
} |
| 213 |
} |
| 214 |
A11.endindices(); |
| 215 |
A12.endindices(); |
| 216 |
A21.endindices(); |
| 217 |
A22.endindices(); |
| 218 |
|
| 219 |
A11 = 0; |
| 220 |
A12 = 0; |
| 221 |
A21 = 0; |
| 222 |
A22 = 0; |
| 223 |
|
| 224 |
//fill in values (row-wise) in A11 and A22 |
| 225 |
for (int i=0; i<Y1; i++) |
| 226 |
{ |
| 227 |
if (i>0) |
| 228 |
A11[i][i-1]=-1; |
| 229 |
A11[i][i]=2; //diag |
| 230 |
if (i<Y1-1) |
| 231 |
A11[i][i+1]=-1; |
| 232 |
if (i<Y2) |
| 233 |
{ //also in A22 |
| 234 |
if (i>0) |
| 235 |
A22[i][i-1]=-1; |
| 236 |
A22[i][i]=2; |
| 237 |
if (i<Y2-1) |
| 238 |
A22[i][i+1]=-1; |
| 239 |
} |
| 240 |
} |
| 241 |
A12[2][0] = -1; A21[0][2] = -1; //fill A12 & A21 |
| 242 |
|
| 243 |
typedef Dune::MultiTypeBlockVector<BCRSMat,BCRSMat> BCRS_Row; |
| 244 |
typedef Dune::MultiTypeBlockMatrix<BCRS_Row,BCRS_Row> CM_BCRS; |
| 245 |
CM_BCRS A; |
| 246 |
A[_0][_0] = A11; |
| 247 |
A[_0][_1] = A12; |
| 248 |
A[_1][_0] = A21; |
| 249 |
A[_1][_1] = A22; |
| 250 |
|
| 251 |
x = 1; |
| 252 |
b = 1; |
| 253 |
|
| 254 |
// Set up a variety of solvers, just to make sure they compile |
| 255 |
MatrixAdapter<CM_BCRS,TestVector,TestVector> op(A); // make linear operator from A |
| 256 |
SeqSOR<CM_BCRS,TestVector,TestVector,2> sor(A,1,1.9520932); // SOR preconditioner |
| 257 |
SeqSSOR<CM_BCRS,TestVector,TestVector,2> ssor(A,1,1.0); // SSOR preconditioner |
| 258 |
|
| 259 |
// Solve system using a Gauss-Seidel method |
| 260 |
SeqGS<CM_BCRS,TestVector,TestVector,2> gs(A,1,1); // GS preconditioner |
| 261 |
LoopSolver<TestVector> loop(op,gs,1E-4,18000,2); // an inverse operator |
| 262 |
InverseOperatorResult r; |
| 263 |
|
| 264 |
loop.apply(x,b,r); |
| 265 |
|
| 266 |
// Solve system using a CG method with a Jacobi preconditioner |
| 267 |
SeqJac<CM_BCRS,TestVector,TestVector,2> jac(A,1,1); // Jacobi preconditioner |
| 268 |
CGSolver<TestVector> cgSolver(op,jac,1E-4,18000,2); // an inverse operator |
| 269 |
|
| 270 |
cgSolver.apply(x,b,r); |
| 271 |
|
| 272 |
// Solve system using a GMRes solver without preconditioner at all |
| 273 |
// Fancy (but only) way to not have a preconditioner at all |
| 274 |
Richardson<TestVector,TestVector> richardson(1.0); |
| 275 |
|
| 276 |
// Preconditioned conjugate-gradient solver |
| 277 |
RestartedGMResSolver<TestVector> gmres(op, |
| 278 |
richardson, |
| 279 |
1e-4, // desired residual reduction factor |
| 280 |
5, // number of iterations between restarts |
| 281 |
100, // maximum number of iterations |
| 282 |
2); // verbosity of the solver |
| 283 |
|
| 284 |
gmres.apply(x, b, r); |
| 285 |
|
| 286 |
printvector(std::cout,x[_0],"solution x1","entry",11,9,1); |
| 287 |
printvector(std::cout,x[_1],"solution x2","entry",11,9,1); |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
catch (Dune::Exception& e) |
| 292 |
{ |
| 293 |
std::cerr << "DUNE reported an exception: " << e << std::endl; |
| 294 |
return 1; |
| 295 |
} |
| 296 |
catch (std::exception& e) |
| 297 |
{ |
| 298 |
std::cerr << "C++ reported an exception: " << e.what() << std::endl; |
| 299 |
return 2; |
| 300 |
} catch (...) |
| 301 |
{ |
| 302 |
std::cerr << "Unknown exception encountered!" << std::endl; |
| 303 |
return 3; |
| 304 |
} |