Debian Codegraph @

dune/istl/test/complexrhstest.cc

raw
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 * \author: Matthias Wohlmuth
7 * \file
8 * \brief Test different solvers and preconditioners for
9 * \f$A*x = b\f$ with \f$A\f$ being a \f$N^2 \times N^2\f$
10 * Laplacian and \f$b\f$ a complex valued rhs.
11 */
12
13 #include <complex>
14
15 #include <dune/istl/bvector.hh>
16 #include <dune/istl/operators.hh>
17 #include <dune/istl/solvers.hh>
18 #include <dune/istl/preconditioners.hh>
19 #include "laplacian.hh"
20
21 #if HAVE_SUPERLU
22 #include <dune/istl/superlu.hh>
23 static_assert(SUPERLU_NTYPE == 3,
24 "If SuperLU is selected for complex rhs test, then SUPERLU_NTYPE must be set to 3 (std::complex<double>)!");
25 #endif
26
27 typedef std::complex<double> FIELD_TYPE;
28
29 /**
30 * \brief Test different solvers and preconditioners for
31 * \f$A*x = b\f$ with \f$A\f$ being a \f$N^2 \times N^2\f$
32 * Laplacian and \f$b\f$ a complex valued rhs.
33 *
34 * The rhs and reference solutions were computed using the following matlab code:
35 * \code
36 N=3;
37 A = full(gallery('poisson',N)); % create poisson matrix
38
39 % find a solution consisting of complex integers
40 indVec = (0:(N*N-1))',
41 iVec = complex(0,1).^indVec + indVec,
42 x0 = iVec .* indVec,
43
44 % compute corresponding rhs
45 b = A * x0,
46
47 % solve system using different solvers
48 xcg = pcg(A,b),
49 x = A \ b,
50 xgmres = gmres(A,b)
51 * \endcode
52 */
53 template<class Operator, class Vector>
54 class SolverTest
55 {
56 public:
57 SolverTest(Operator & op, Vector & rhs, Vector & x0, double maxError = 1e-10)
58 : m_op(op),
59 m_x(rhs),
60 m_x0(x0),
61 m_b(rhs),
62 m_rhs(rhs),
63 m_maxError(maxError),
64 m_numTests(0),
65 m_numFailures(0)
66 {
67 std::cout << "SolverTest uses rhs: " << std::endl
68 << m_rhs << std::endl
69 << "and expects the solultion: " << m_x0 << std::endl << std::endl;
70 }
71
72 template<class Solver>
73 bool operator() (Solver & solver)
74 {
75 m_b = m_rhs;
76 m_x = 0;
77 solver.apply(m_x, m_b, m_res);
78 std::cout << "Defect reduction is " << m_res.reduction << std::endl;
79 std::cout << "Computed solution is: " << std::endl;
80 std::cout << m_x << std::endl;
81 m_b = m_x0;
82 m_b -= m_x;
83 const double errorNorm = m_b.two_norm();
84 std::cout << "Error = " << errorNorm << std::endl;
85 ++m_numTests;
86 if(errorNorm > m_maxError)
87 {
88 std::cout << "SolverTest did not converge!" << std::endl;
89 ++m_numFailures;
90 return false;
91 }
92 return true;
93 }
94
95 int getNumTests() const
96 {
97 return m_numTests;
98 }
99
100 int getNumFailures() const
101 {
102 return m_numFailures;
103 }
104
105 private:
106 const Operator & m_op;
107 Vector m_x, m_x0, m_b;
108 const Vector m_rhs;
109 double m_maxError;
110 int m_numTests, m_numFailures;
111 Dune::InverseOperatorResult m_res;
112 };
113
114 int main(int argc, char** argv)
115 {
116 const int BS = 1;
117 std::size_t N = 3;
118
119 const int maxIter = int(N*N*N*N);
120 const double reduction = 1e-16;
121
122 if (argc > 1)
123 N = atoi(argv[1]);
124 std::cout << "testing for N=" << N << " BS=" << BS << std::endl;
125
126 typedef Dune::FieldMatrix<FIELD_TYPE,BS,BS> MatrixBlock;
127 typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat;
128 typedef Dune::FieldVector<FIELD_TYPE,BS> VectorBlock;
129 typedef Dune::BlockVector<VectorBlock> Vector;
130 typedef Dune::MatrixAdapter<BCRSMat,Vector,Vector> Operator;
131
132 BCRSMat mat;
133 Operator fop(mat);
134 Vector b(N*N), b0(N*N), x0(N*N), error(N*N);
135
136 setupLaplacian(mat,N);
137
138 typedef Vector::Iterator VectorIterator;
139
140 FIELD_TYPE count(0);
141
142 const FIELD_TYPE I(0.,1.); // complex case
143
144 for (VectorIterator it = x0.begin(); it != x0.end(); ++it)
145 {
146 *it = (count + std::pow(I,count))*count;
147 count = count + 1.;
148 }
149 std::cout << "x0 = " << x0 << std::endl;
150
151 mat.mv(x0,b);
152
153 std::cout << "b = " << b << std::endl;
154 b0 = b;
155
156 Dune::Timer watch;
157
158 watch.reset();
159
160 // create Dummy Preconditioner
161 typedef Dune::Richardson<Vector,Vector> DummyPreconditioner;
162
163 typedef Dune::SeqJac<BCRSMat,Vector,Vector> JacobiPreconditioner;
164 typedef Dune::SeqGS<BCRSMat,Vector,Vector> GaussSeidelPreconditioner;
165 typedef Dune::SeqSOR<BCRSMat,Vector,Vector> SORPreconditioner;
166 typedef Dune::SeqSSOR<BCRSMat,Vector,Vector> SSORPreconditioner;
167
168 const double maxError(1e-10);
169
170 SolverTest<Operator,Vector> solverTest(fop,b0,x0,maxError);
171
172 DummyPreconditioner dummyPrec(1.);
173
174 const typename Dune::FieldTraits<FIELD_TYPE>::real_type relaxFactor(1.);
175 JacobiPreconditioner jacobiPrec1(mat,1,relaxFactor);
176 JacobiPreconditioner jacobiPrec2(mat,maxIter,relaxFactor);
177
178 GaussSeidelPreconditioner gsPrec1(mat,1,relaxFactor);
179 GaussSeidelPreconditioner gsPrec2(mat,maxIter,relaxFactor);
180
181 SORPreconditioner sorPrec1(mat,1,relaxFactor);
182 SORPreconditioner sorPrec2(mat,maxIter,relaxFactor);
183 SSORPreconditioner ssorPrec1(mat,1,relaxFactor);
184 SSORPreconditioner ssorPrec2(mat,maxIter,relaxFactor);
185
186 #if HAVE_SUPERLU
187 Dune::SuperLU<BCRSMat> solverSuperLU(mat, true);
188 std::cout << "SuperLU converged: "<< solverTest(solverSuperLU) << std::endl << std::endl;
189 #else
190 std::cout << "SuperLU skipped because not found." << std::endl << std::endl;
191 #endif
192
193 typedef Dune::GradientSolver<Vector> GradientSolver;
194 GradientSolver solverGradient(fop,dummyPrec, reduction, maxIter, 1);
195 std::cout << "GradientSolver with identity preconditioner converged: " << solverTest(solverGradient) << std::endl << std::endl;
196
197 typedef Dune::CGSolver<Vector> CG;
198 CG solverCG(fop,dummyPrec, reduction, maxIter, 1);
199 std::cout << "CG with identity preconditioner converged: " << solverTest(solverCG) << std::endl << std::endl;
200
201 typedef Dune::BiCGSTABSolver<Vector> BiCG;
202 BiCG solverBiCG(fop,dummyPrec, reduction, maxIter, 1);
203 std::cout << "BiCGStab with identity preconditioner converged: " << solverTest(solverBiCG) << std::endl << std::endl;
204
205 typedef Dune::LoopSolver<Vector> JacobiSolver;
206 JacobiSolver solverJacobi1(fop,jacobiPrec1,reduction,maxIter,1);
207 std::cout << "LoopSolver with a single Jacobi iteration as preconditioner converged: " << solverTest(solverJacobi1) << std::endl << std::endl;
208
209 typedef Dune::LoopSolver<Vector> JacobiSolver;
210 JacobiSolver solverJacobi2(fop,jacobiPrec2,reduction,maxIter,1);
211 std::cout << "LoopSolver with multiple Jacobi iteration as preconditioner converged: " << solverTest(solverJacobi2) << std::endl << std::endl;
212
213 typedef Dune::LoopSolver<Vector> GaussSeidelSolver;
214 GaussSeidelSolver solverGaussSeidel1(fop,gsPrec1,reduction,maxIter,1);
215 std::cout << "LoopSolver with a single GaussSeidel iteration as preconditioner converged: " << solverTest(solverGaussSeidel1) << std::endl << std::endl;
216
217 typedef Dune::LoopSolver<Vector> GaussSeidelSolver;
218 GaussSeidelSolver solverGaussSeidel2(fop,gsPrec2,reduction,maxIter,1);
219 std::cout << "LoopSolver with multiple GaussSeidel iterations as preconditioner converged: " << solverTest(solverGaussSeidel2) << std::endl << std::endl;
220
221 typedef Dune::LoopSolver<Vector> SORSolver;
222 SORSolver solverSOR1(fop,sorPrec1,reduction,maxIter,1);
223 std::cout << "LoopSolver with a single SOR iteration as preconditioner converged: " << solverTest(solverSOR1) << std::endl << std::endl;
224
225 typedef Dune::LoopSolver<Vector> SORSolver;
226 SORSolver solverSOR2(fop,sorPrec2,reduction,maxIter,1);
227 std::cout << "LoopSolver with multiple SOR iterations as preconditioner converged: " << solverTest(solverSOR2) << std::endl << std::endl;
228
229 typedef Dune::LoopSolver<Vector> SSORSolver;
230 SSORSolver solverSSOR1(fop,ssorPrec1,reduction,maxIter,1);
231 std::cout << "LoopSolver with a single SSOR iteration as preconditioner converged: " << solverTest(solverSOR2) << std::endl << std::endl;
232
233 typedef Dune::LoopSolver<Vector> SSORSolver;
234 SSORSolver solverSSOR2(fop,ssorPrec2,reduction,maxIter,1);
235 std::cout << "LoopSolver with multiple SSOR iterations as preconditioner converged: " << solverTest(solverSSOR2) << std::endl << std::endl;
236
237 typedef Dune::MINRESSolver<Vector> MINRES;
238 MINRES solverMINRES(fop,dummyPrec, reduction, maxIter, 1);
239 std::cout << "MINRES with identity preconditioner converged: " << solverTest(solverMINRES) << std::endl << std::endl;
240
241 typedef Dune::RestartedGMResSolver<Vector> GMRES;
242 GMRES solverGMRES(fop,dummyPrec, reduction, maxIter, maxIter*maxIter, 1);
243 std::cout << "GMRES with identity preconditioner converged: " << solverTest(solverGMRES) << std::endl << std::endl;
244
245 const int testCount = solverTest.getNumTests();
246 const int errorCount = solverTest.getNumFailures();
247 std::cout << "Tested " << testCount << " different solvers or preconditioners " << " for a laplacian with complex rhs. " << testCount - errorCount << " out of " << testCount << " solvers converged! " << std::endl << std::endl;
248
249 return errorCount;
250 }