Debian Codegraph @

dune/istl/test/matrixfreesolverfactorytest.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 #include "config.h"
6
7 #include <dune/common/parametertree.hh>
8
9 #include <dune/istl/bvector.hh>
10 #include <dune/istl/operators.hh>
11 #include <dune/istl/solverfactory.hh>
12
13 // include direct solvers to see whether it compiles (should throw an exception if used)
14 #include <dune/istl/cholmod.hh>
15 #include <dune/istl/umfpack.hh>
16 #include <dune/istl/superlu.hh>
17 #include <dune/istl/spqr.hh>
18 #include <dune/istl/paamg/amg.hh>
19
20
21 using namespace Dune;
22
23 template<class V>
24 class MatrixFreeLaplacian2D
25 : public LinearOperator<V, V>{
26
27 public:
28 using domain_type = V;
29 using range_type = V;
30 using field_type = typename domain_type::field_type;
31
32 MatrixFreeLaplacian2D() = default;
33
34 void apply(const domain_type& x, range_type& y) const override{
35 assert(x.N() == y.N());
36 y[0] = 2*x[0] - x[1];
37 for(size_t i=1; i<y.N()-1; ++i){
38 y[i] = -x[i-1] + 2*x[i] -x[i+1];
39 }
40 y[y.N()-1] = -x[y.N()-2] + 2*x[y.N()-1];
41 }
42
43 void applyscaleadd(field_type alpha, const domain_type& x, range_type& y) const override{
44 assert(x.N() == y.N());
45 y[0] += alpha*(2*x[0] - x[1]);
46 for(size_t i=1; i<y.N()-1; ++i){
47 y[i] += alpha*(-x[i-1] + 2*x[i] -x[i+1]);
48 }
49 y[y.N()-1] += alpha*(-x[y.N()-2] + 2*x[y.N()-1]);
50 }
51
52 SolverCategory::Category category() const override{
53 return SolverCategory::sequential;
54 }
55 };
56
57 template<class V, int k>
58 class MatrixFreeBlockJacobi
59 : public Preconditioner<V,V>{
60 public:
61 using domain_type = V;
62 using range_type = V;
63 using field_type = typename V::field_type;
64
65 MatrixFreeBlockJacobi()
66 {
67 dia_block_inv = 0.;
68 for(size_t i=0;i<k-1; ++i){
69 dia_block_inv[i][i] = 2.;
70 dia_block_inv[i+1][i] = -1.;
71 dia_block_inv[i][i+1] = -1.;
72 }
73 dia_block_inv[k-1][k-1] = 2.;
74 dia_block_inv.invert();
75 }
76
77 void pre(domain_type& x, range_type& y) override {}
78 void post(domain_type& x) override {}
79
80 void apply(domain_type& x, const range_type& y) override{
81 assert(x.N() == y.N());
82 size_t n = y.N();
83 for(size_t i=0;i<n;++i){
84 for(size_t j=0; j<k;++j)
85 x[i] += dia_block_inv[i%k][j]*y[k*(i/k)+j];
86 }
87 }
88
89 SolverCategory::Category category() const override{
90 return SolverCategory::sequential;
91 }
92
93 protected:
94 FieldMatrix<field_type, k, k> dia_block_inv;
95 };
96
97 using V = BlockVector<float>;
98
99 int main(int argc, char** argv){
100 ParameterTree config;
101 config["type"] = "minressolver";
102 config["verbose"] = "2";
103 config["maxit"] = "10000";
104 config["reduction"] = "1e-8";
105 config.report();
106 initSolverFactories<MatrixFreeLaplacian2D<V>>();
107 std::shared_ptr<MatrixFreeLaplacian2D<V>> op = std::make_shared<MatrixFreeLaplacian2D<V>>();
108
109 using Prec = MatrixFreeBlockJacobi<V, 10>;
110 std::shared_ptr<Prec> prec = std::make_shared<Prec>();
111
112 auto solver = getSolverFromFactory(op, config, prec);
113 V b(1000);
114 V x = b;
115 x = 1.;
116 b = 0.;
117 InverseOperatorResult res;
118 solver->apply(x,b,res);
119 return 0;
120 }