Debian Codegraph @

dune/istl/test/cgconditiontest.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
4 #include <dune/istl/preconditioners.hh>
5 #include <dune/istl/solvers.hh>
6
7 typedef Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1> > MAT;
8 typedef Dune::BlockVector<Dune::FieldVector<double,1> > VEC;
9
10 void condition_test (MAT& T) {
11
12 int N = T.N();
13
14 // CG run (noop preconditioner) with condition estimate
15
16 auto prec = std::make_shared<Dune::Richardson<VEC, VEC>> (1.0);
17
18 auto op = std::make_shared<Dune::MatrixAdapter<MAT, VEC, VEC>>(T);
19
20 VEC d (N,N);
21 VEC v (N,N);
22
23 v = 1.0;
24 d = 1.0;
25
26 int verbosity = 2;
27
28 Dune::InverseOperatorResult result;
29 auto solver = std::make_shared<Dune::CGSolver<VEC> >(*op,*prec,1E-6,1000,verbosity,true);
30 solver->apply(v,d,result);
31
32
33
34 #if HAVE_ARPACKPP
35 // Actual condition number using ARPACK
36 Dune::ArPackPlusPlus_Algorithms<MAT, VEC> arpack(T);
37
38 double eps = 0.0;
39 VEC eigv;
40 double min_eigv, max_eigv;
41 arpack.computeSymMinMagnitude (eps, eigv, min_eigv);
42 arpack.computeSymMaxMagnitude (eps, eigv, max_eigv);
43
44 std::cout << "Actual condition number from ARPACK: " << max_eigv / min_eigv << std::endl;
45 #endif
46 }
47
48
49
50 int main(int argc, char **argv)
51 {
52
53 // Simple stencil
54 {
55 int N = 142;
56
57 MAT T(N, N, MAT::row_wise);
58
59 for (auto row = T.createbegin(); row != T.createend(); ++row) {
60 if (row.index() > 0)
61 row.insert(row.index()-1);
62 row.insert(row.index());
63 if (row.index() < T.N() - 1)
64 row.insert(row.index()+1);
65 }
66 for (int row = 0; row < N; ++row) {
67 T[row][row] = 2.0;
68 if (row > 0)
69 T[row][row-1] = -1.0;
70 if (row < N-1)
71 T[row][row+1] = -1.0;
72 }
73
74 condition_test (T);
75 }
76
77
78 // Ill-conditioned stencil
79 {
80 int N = 142;
81
82 MAT T(N, N, MAT::row_wise);
83
84 for (auto row = T.createbegin(); row != T.createend(); ++row) {
85 if (row.index() > 0)
86 row.insert(row.index()-1);
87 row.insert(row.index());
88 if (row.index() < T.N() - 1)
89 row.insert(row.index()+1);
90 }
91 for (int row = 0; row < N; ++row) {
92 double factor = 0.8 + 0.2 * ((double)row / N);
93 T[row][row] = 2.0 * factor;
94 if (row > 0)
95 T[row][row-1] = -1.0 * factor;
96 if (row < N-1)
97 T[row][row+1] = -1.0 * factor;
98 }
99
100 condition_test (T);
101 }
102
103
104 // Very ill-conditioned stencil
105 {
106 int N = 40;
107
108 MAT T(N, N, MAT::row_wise);
109
110 for (auto row = T.createbegin(); row != T.createend(); ++row) {
111 for (int i = -10; i <= 10; i++) {
112 if (row.index()+i >= 0 && row.index()+i < T.N())
113 row.insert(row.index()+i);
114 }
115 }
116 for (int row = 0; row < N; ++row) {
117 double factor = 0.8 + 0.2 * ((double)row / N);
118 for (int i = -10; i <= 10; i++) {
119 if (row+i >= 0 && row+i < int(T.N()))
120 T[row][row+i] = -1.0 * factor;
121 }
122 T[row][row] = 20.0 * factor;
123 }
124
125 condition_test (T);
126 }
127
128 }