LinearSolverInterface.hpp 5.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once

/** \file LinearSolverInterface.h */

/**
 * \defgroup Solver Solver module
 * @{ <img src="solver.png"> @}
 *
 * \brief
 * Contains all classes needed for solving linear and non linear equation
 * systems.
 */

namespace AMDiS
{
  /// Non-templated base-class for Preconditioner types
  struct PreconditionerInterface
  {
    virtual ~PreconditionerInterface() {}
  };

  /// Non-templates base-class for Runner / Worker types
  struct RunnerInterface
  {
    // virtual destructor
    virtual ~RunnerInterface() {}

    virtual PreconditionerInterface* getLeftPrecon()
    {
      return NULL;
    }

    virtual PreconditionerInterface* getRightPrecon()
    {
      return NULL;
    }
  };

  // A general solver class for istl solvers
  // ---------------------------------------------------------------------------

  template <class Matrix, class VectorX, class VectorB = VectorX>
  class LinearSolverInterface
  {    
    /// The constructor reads needed parameters and sets solvers \p prefix.
    /**
     * Reads parameters for a solver with name 'NAME':
     *   NAME->absolute tolerance   \ref aTol
     *   NAME->relative tolerance   \ref rTol
     *   NAME->max iteration        \ref maxTter
     *   NAME->print cycle          \ref printCycle
     *   NAME->info                 \ref info
     *   NAME->break if tolerance not reached \ref breakTolNotReached
    **/
    explicit LinearSolverInterface(std::string prefix);

    /// Destructor.
    virtual ~LinearSolverInterface() {}
    
    
    /// Public method to call in order to solve a linear system Ax = b.
    /**
     * The method redirects to the specific linear solver and prints statistics
     * and error estimations at the end.
     * 
     * The parameters correspond to
     *  \p A     A block-matrix that represents the system-matrix.
     *  \p x     A block-vector for the unknown components.
     *  \p b     A block-vector for the right-hand side of the linear system.
     *  \p createMatrixData  If true, the matrix will be initialized and the 
     *                       corresponding runner of the system receives the 
     *                       matrix in the init() method.
     *  \p storeMatrixData   If true, the exit() method of the runner will be 
     *                       called.
     **/
    void solve(Matrix const& A, VectorX& x, VectorB const& b,
               bool createMatrixData,
               bool storeMatrixData)
    {      
      solveImpl(A, x, b, createMatrixData, storeMatrixData);
    }

    /** \name getting methods
     * \{
     */

    /// Returns \ref aTol
    double getAbsTolerance() const
    {
      return aTol;
    }

    /// Returns \ref rTol
    double getRelTolerance() const
    {
      return rTol;
    }

    /// Returns \ref maxIter
    int getMaxIterations() const
    {
      return maxIter;
    }

    /// Returns number of iterations in last run of an iterative solver
    int getIterations() const
    {
      return iterations;
    }

    /// Returns error code in last run of an iterative solver
    int getErrorCode() const
    {
      return error;
    }

    /// Returns info
    int getInfo() const
    {
      return info;
    }

    /// Returns \ref printCycle
    int getPrintCycle() const
    {
      return printCycle;
    }

    /// Returns \ref residual
    double getResidual() const
    {
      return residual;
    }
    
    /// Returns the initfile \ref prefix
    std::string getPrefix() const
    {
      return prefix;
    }

    /** \} */


    /** \name setting methods
     * \{
     */

    /// Sets \ref aTol
    void setAbsTolerance(double tol)
    {
      aTol = tol;
    }

    /// Sets \ref rTol
    void setAbsTolerance(double tol)
    {
      rTol = tol;
    }

    /// Sets \ref maxIter
    void setMaxIterations(int i)
    {
      maxIter = i;
    }

    /// Sets \ref info
    void setInfo(int i)
    {
      info = i;
    }

    /// Sets \ref info
    void setError(int e)
    {
      error = e;
    }

    /** \} */

  private:
    /// main methods that all solvers must implement
    virtual void solveImpl(Matrix const& A, VectorX& x, VectorB const& b,
                           bool createMatrixData,
                           bool storeMatrixData) = 0;
    
  private:
    /// The initfile prefix to read parameters
    std::string prefix;
    
    /// The name of the solver
    std::string solverName = "cg";
    
    /// The name of the preconditioner (not used by every solver)
    std::string preconName = "no";
    
  private:
    /// The abolute tolerance
    double aTol = 1.e-6;
    
    /// The relative tolerance
    double rTol = 0.0;
    
    /// The maximal number of iterations
    size_t maxIter = 1000;
    
    /// Print solver residuals every \ref printCycles 's iteration
    size_t print_cycle = 100;
    
    /// Throw an error if tolerance could not be reached
    bool breakTolNotReached = true;
    
    /// The solver verbosity level
    int info = 0;
    
    // some parameters used internally  
    double absRresidual = -1.0;
    double relResidual = -1.0;
    int iterations = -1;
    int error = -1;
  };

} // end namespace AMDiS

#include "LinearSolverInterface.inc.hpp"