| 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 \brief Test the preconditioners in the file `preconditioners.hh` |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <dune/common/fvector.hh> |
| 10 |
#include <dune/common/fmatrix.hh> |
| 11 |
|
| 12 |
#include <dune/istl/bcrsmatrix.hh> |
| 13 |
#include <dune/istl/operators.hh> |
| 14 |
#include <dune/istl/preconditioners.hh> |
| 15 |
#include <dune/istl/solvers.hh> |
| 16 |
#include <dune/istl/test/laplacian.hh> |
| 17 |
|
| 18 |
namespace Dune |
| 19 |
{ |
| 20 |
using Mat1 = BCRSMatrix<FieldMatrix<double,1,1>>; |
| 21 |
using Mat2 = BCRSMatrix<FieldMatrix<std::complex<double>,1,1>>; |
| 22 |
using Vec1 = BlockVector<FieldVector<double,1>>; |
| 23 |
using Vec2 = BlockVector<FieldVector<std::complex<double>,1>>; |
| 24 |
|
| 25 |
// explicit template instantiation of all preconditioners |
| 26 |
template class SeqJac<Mat1, Vec1, Vec1>; |
| 27 |
template class SeqSOR<Mat1, Vec1, Vec1>; |
| 28 |
template class SeqSSOR<Mat1, Vec1, Vec1>; |
| 29 |
template class Richardson<Vec1, Vec1>; |
| 30 |
template class SeqDILU<Mat1, Vec1, Vec1>; |
| 31 |
template class SeqILU<Mat1, Vec1, Vec1>; |
| 32 |
template class SeqILDL<Mat1, Vec1, Vec1>; |
| 33 |
|
| 34 |
template class SeqJac<Mat2, Vec2, Vec2>; |
| 35 |
template class SeqSOR<Mat2, Vec2, Vec2>; |
| 36 |
template class SeqSSOR<Mat2, Vec2, Vec2>; |
| 37 |
template class Richardson<Vec2, Vec2>; |
| 38 |
template class SeqDILU<Mat2, Vec2, Vec2>; |
| 39 |
template class SeqILU<Mat2, Vec2, Vec2>; |
| 40 |
template class SeqILDL<Mat2, Vec2, Vec2>; |
| 41 |
|
| 42 |
} // end namespace Dune |
| 43 |
|
| 44 |
|
| 45 |
using namespace Dune; |
| 46 |
|
| 47 |
template <class Matrix, class Vector> |
| 48 |
void setupProblem(Matrix& matrix, Vector& b) |
| 49 |
{ |
| 50 |
int n=100; |
| 51 |
|
| 52 |
setupLaplacian(matrix,n); |
| 53 |
|
| 54 |
b.resize(n*n); |
| 55 |
Vector x(n*n); |
| 56 |
|
| 57 |
// Construct right-hand side such that '1' is the solution |
| 58 |
b=0; |
| 59 |
x=1; |
| 60 |
matrix.mv(x, b); |
| 61 |
} |
| 62 |
|
| 63 |
template <class Matrix, class Vector, class Preconditioner> |
| 64 |
void testPreconditioner(const Matrix& matrix, const Vector& b, Vector& x, Preconditioner& prec) |
| 65 |
{ |
| 66 |
using Operator = MatrixAdapter<Matrix,Vector,Vector>; |
| 67 |
|
| 68 |
Operator linearOperator(matrix); |
| 69 |
InverseOperatorResult result; |
| 70 |
|
| 71 |
Dune::LoopSolver<Vector> solver(linearOperator, prec, 1e-8,15,2); |
| 72 |
auto residual = b; |
| 73 |
solver.apply(x,residual,result); |
| 74 |
} |
| 75 |
|
| 76 |
template <class Matrix, class Vector> |
| 77 |
void testAllPreconditioners(const Matrix& matrix, const Vector& b) |
| 78 |
{ |
| 79 |
Vector x = b; // Set the correct size |
| 80 |
x = 0; |
| 81 |
SeqSSOR<Matrix,Vector,Vector> seqSSOR(matrix, 1,1.0); |
| 82 |
testPreconditioner(matrix, b, x, seqSSOR); |
| 83 |
|
| 84 |
x = 0; |
| 85 |
SeqSOR<Matrix,Vector,Vector> seqSOR(matrix, 1,1.0); |
| 86 |
testPreconditioner(matrix, b, x, seqSOR); |
| 87 |
|
| 88 |
x = 0; |
| 89 |
SeqGS<Matrix,Vector,Vector> seqGS(matrix, 1,1.0); |
| 90 |
testPreconditioner(matrix, b, x, seqGS); |
| 91 |
|
| 92 |
x = 0; |
| 93 |
SeqJac<Matrix,Vector,Vector> seqJac(matrix, 1,1.0); |
| 94 |
testPreconditioner(matrix, b, x, seqJac); |
| 95 |
|
| 96 |
x = 0; |
| 97 |
SeqDILU<Matrix,Vector,Vector> seqDILU(matrix, 1.1); |
| 98 |
testPreconditioner(matrix, b, x, seqDILU); |
| 99 |
|
| 100 |
x = 0; |
| 101 |
SeqILU<Matrix,Vector,Vector> seqILU(matrix, 3, 1.2, true); |
| 102 |
testPreconditioner(matrix, b, x, seqILU); |
| 103 |
|
| 104 |
x = 0; |
| 105 |
Richardson<Vector,Vector> richardson(1.5); |
| 106 |
testPreconditioner(matrix, b, x, richardson); |
| 107 |
|
| 108 |
x = 0; |
| 109 |
SeqILDL<Matrix,Vector,Vector> seqILDL(matrix, 1.2); |
| 110 |
testPreconditioner(matrix, b, x, seqILDL); |
| 111 |
} |
| 112 |
|
| 113 |
int main() try |
| 114 |
{ |
| 115 |
{ |
| 116 |
using Matrix = BCRSMatrix<double>; |
| 117 |
using Vector = BlockVector<double>; |
| 118 |
Matrix matrix; |
| 119 |
Vector b,x; |
| 120 |
setupProblem(matrix, b); |
| 121 |
|
| 122 |
testAllPreconditioners(matrix, b); |
| 123 |
} |
| 124 |
|
| 125 |
{ |
| 126 |
using Matrix = BCRSMatrix<FieldMatrix<double,1,1> >; |
| 127 |
using Vector = BlockVector<FieldVector<double,1> >; |
| 128 |
Matrix matrix; |
| 129 |
Vector b,x; |
| 130 |
setupProblem(matrix, b); |
| 131 |
|
| 132 |
testAllPreconditioners(matrix, b); |
| 133 |
} |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
catch (std::exception& e) { |
| 138 |
std::cerr << e.what() << std::endl; |
| 139 |
return 1; |
| 140 |
} |