Debian Codegraph @

dune/istl/test/solveraborttest.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 #include <iostream>
7 #include <limits>
8 #include <string>
9
10 #include <dune/common/fmatrix.hh>
11 #include <dune/common/fvector.hh>
12
13 #include <dune/istl/operators.hh>
14 #include <dune/istl/preconditioners.hh>
15 #include <dune/istl/solver.hh>
16 #include <dune/istl/solvers.hh>
17
18 template<class Solver, class Vector>
19 void checkSolverAbort(int &status, const std::string &name,
20 Solver &solver, Vector &x, Vector &b)
21 {
22 try {
23 Dune::InverseOperatorResult res;
24 solver.apply(x, b, res);
25 std::cout << "Error: " << name << "::apply() did not abort, which is "
26 << "unexpected.\n"
27 << "converged = " << std::boolalpha << res.converged << "\n"
28 << "iterations = " << res.iterations << "\n"
29 << "reduction = " << res.reduction << "\n"
30 << "conv_rate = " << res.conv_rate << "\n"
31 << "elapsed = " << res.elapsed << "\n"
32 << "solution = {" << x << "}" << std::endl;
33 status = 1; // FAIL
34 }
35 catch(const Dune::SolverAbort &e)
36 {
37 std::cout << name << "::apply() aborted, as expected" << std::endl
38 << "Abort message was: " << e << std::endl;
39 if(status == 77) status = 0; // PASS
40 }
41 catch(const std::exception &e)
42 {
43 std::cout << "Error: " << name << "::apply() aborted with an exception "
44 << "not derived from Dune::SolverAbort, which is unexpected.\n"
45 << "e.what(): " << e.what() << std::endl;
46 status = 1; // FAIL
47 }
48 catch(...)
49 {
50 std::cout << "Error: " << name << "::apply() aborted with an exception "
51 << "not derived from Dune::SolverAbort, which is unexpected.\n"
52 << "In addition, the exception is not derived from "
53 << "std::exception, so there is no further information, sorry."
54 << std::endl;
55 status = 1; // FAIL
56 }
57 }
58
59 int main()
60 {
61
62 int status = 77;
63
64 // How verbose the solvers should be. Use 2 (maximum verbosity) by default,
65 // this will include all information in the logs, and for the casual user of
66 // the unit tests ctest will hide the output anyway.
67 int verbose = 2;
68
69 { // CGSolver
70 std::cout << "Checking CGSolver with an unsolvable system...\n"
71 << "Expecting SolverAbort with a NaN defect" << std::endl;
72
73 using Matrix = Dune::FieldMatrix<double, 2, 2>;
74 using Vector = Dune::FieldVector<double, 2>;
75
76 Matrix matrix = { { 1, 1 },
77 { 1, 1 } };
78 Vector b = { 1, 2 };
79 Vector x = { 0, 0 };
80
81 Dune::MatrixAdapter<Matrix, Vector, Vector> op(matrix);
82 Dune::Richardson<Vector, Vector> richardson;
83 Dune::CGSolver<Vector> solver(op, richardson, 1e-10, 5000, verbose);
84
85 checkSolverAbort(status, "CGSolver", solver, x, b);
86 }
87
88 { // BiCGSTABSolver
89 std::cout << "Checking BiCGSTABSolver with an unsolvable system...\n"
90 << "Expecting abs(h) < EPSILON" << std::endl;
91
92 using Matrix = Dune::FieldMatrix<double, 2, 2>;
93 using Vector = Dune::FieldVector<double, 2>;
94
95 Matrix matrix = { { 1, 1 },
96 { 1, 1 } };
97 Vector b = { 1, 2 };
98 Vector x = { 0, 0 };
99
100 Dune::MatrixAdapter<Matrix, Vector, Vector> op(matrix);
101 Dune::Richardson<Vector, Vector> richardson;
102 Dune::BiCGSTABSolver<Vector> solver(op, richardson, 1e-10, 5000, verbose);
103
104 checkSolverAbort(status, "BiCGSTABSolver", solver, x, b);
105 }
106
107 // TODO:
108 // - trigger "breakdown in BiCGSTAB - rho"
109 // - trigger "breakdown in BiCGSTAB - omega"
110 // - trigger "breakdown in GMRes"
111
112 return status;
113 }