OEMSolver.h 8.31 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file OEMSolver.h */

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

#ifndef AMDIS_OEM_SOLVER_H
#define AMDIS_OEM_SOLVER_H

// ============================================================================
// ===== includes =============================================================
// ============================================================================

#include "Global.h"

namespace AMDiS {

  // ============================================================================
  // ===== forward declarations =================================================
  // ============================================================================

  template<typename VectorType> class Preconditioner;
  template<typename VectorType> class CreatorInterface;
  template<typename VectorType> class MatVecMultiplier;

  // ============================================================================
  // ===== definitions ==========================================================
  // ============================================================================

  /** \brief prints information before solving the system */
#define START_INFO() this->start_info(funcName)

  /** \brief prints information after solving the system */
#define BREAK_INFO(st,it,res,ores)		\
  this->break_info(funcName,st,it,res,ores)

  /** \brief prints information while solving the system */
#define SOLVE_INFO(it,res,ores)			\
  this->solve_info(funcName,it,res,ores)


  // ============================================================================
  // ===== class OEMSolver ======================================================
  // ============================================================================

  /**
   * \ingroup Solver
   * 
   *\brief
   * Solver for linear equation systems.
   * Virtual base class for OEM solvers. 
   */
  template<typename VectorType>
  class OEMSolver
  {
  public:
    /** \brief
     * The constructor reads needed parameters and sets solvers \ref name.
     */
84
    OEMSolver(std::string name);
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
  
    /** \brief
     * Virtual destructor because OEMSolver is pure virtual. 
     */
    virtual ~OEMSolver();

    void initParameters();

    /** \brief
     * Solves the linear system.
     */
    int solve(MatVecMultiplier<VectorType> *mv, 
	      VectorType *x, VectorType *b,
	      Preconditioner<VectorType> *lPrecon = NULL, 
	      Preconditioner<VectorType> *rPrecon = NULL)
    {
      FUNCNAME("OEMSolver::solve()");
      TEST_EXIT(mv)("no matVec\n");
      TEST_EXIT(vectorCreator)("no vectorCreator\n");

      leftPrecon = lPrecon;
      rightPrecon = rPrecon;

      init();

      if (leftPrecon) 
	leftPrecon->init();
      
      if (rightPrecon) 
	rightPrecon->init();

      int iter = solveSystem(mv, x, b);

      if (leftPrecon) 
	leftPrecon->exit();

      if (rightPrecon) 
	rightPrecon->exit();

      exit();

      return iter;
    };

    // ===== getting-methods ======================================================

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

    /** \brief
     * Returns solvers \ref name.
     */
138
    inline const std::string& getName() { 
139
140
      return name; 
    };
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

    /** \brief
     * Returns \ref tolerance
     */
    inline double getTolerance() {
      return tolerance;
    };  

    /** \brief
     * Returns \ref max_iter
     */
    inline int getMaxIterations() {
      return max_iter;
    };

    /** \brief
     * Returns \ref residual
     */
    inline double getResidual() {
      return residual;
    };

    /** \} */ 

    // ===== setting-methods ======================================================

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

    /** \brief
     * Sets \ref tolerance
     */
174
    inline void setTolerance(double tol) {
175
176
177
178
179
180
      tolerance = tol;
    };

    /** \brief
     * Sets \ref relative
     */
181
    inline void setRelative(bool rel) {
182
183
184
185
186
187
      relative = rel;
    };

    /** \brief
     * Sets \ref max_iter
     */
188
    inline void setMaxIterations(int i) {
189
190
191
192
193
194
      max_iter = i;
    };

    /** \brief
     * Sets \ref info
     */
195
    inline void setInfo(int i) {
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
      info = i;
    };
  
    inline void setVectorCreator(CreatorInterface<VectorType> *creator) {
      vectorCreator = creator;
    };

    inline CreatorInterface<VectorType>* getVectorCreator()
    {
      return vectorCreator;
    };

    /** \} */

  protected:
    /** \brief
     * To be overloaded by concrete solvers.
     */
    virtual void init() = 0;

    /** \brief
     * To be overloaded by concrete solvers.
     */
    virtual void exit() = 0;

    /** \brief
     * To be overloaded by concrete solvers.
     */
    virtual int solveSystem(MatVecMultiplier<VectorType> *mv,
			    VectorType                   *x, 
			    VectorType                   *b) = 0;


    /** \brief
     * Prints start information. Uses \ref START_INFO.
     */
    void start_info(const char *);

    /** \brief
     * Prints break information. Uses \ref BREAK_INFO.
     */
    void break_info(const char *, const char *, int, double, double *);

    /** \brief
     * Prints solve information. Uses \ref SOLVE_INFO.
     */
    int  solve_info(const char *, int, double, double *);

  protected:
    /** \brief
     * solvers name.
     */
248
    std::string name;
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

    /** \brief
     * solver tolerance. Set in OEMSolver's constructor. 
     */
    double tolerance;

    /** \brief
     * solver relative. Set in OEMSolver's constructor. 
     */
    bool relative;

    /** \brief
     * maximal number of iterations. Set in OEMSolver's constructor.
     */
    int max_iter;

    /** \brief
     * info level during solving the system. Set in OEMSolver's constructor.
     */
    int info;

    /** \brief
     * current residual.
     */
    double residual;

    Preconditioner<VectorType> *leftPrecon;

    Preconditioner<VectorType> *rightPrecon;

    CreatorInterface<VectorType> *vectorCreator;

  };

  // ============================================================================
  // ===== class OEMSolverCreator ===============================================
  // ============================================================================

  /** 
   * \ingroup Solver
   *
   * \brief
   * Interface for creators of concrete OEMSolvers. 
   */
  template<typename T>
  class OEMSolverCreator : public CreatorInterface<OEMSolver<T> >
  { 
  public:
    virtual ~OEMSolverCreator() {};

    /** \brief
     * Sets \ref problem
     */
302
    void setName(std::string name_) { 
303
304
      name = name_; 
    };
305
306
307
308
309
310

  protected:
    /** \brief
     * Pointer to the problem the solver will belong to. Needed as parameter
     * when constructing an OEMSolver
     */
311
    std::string name;
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  };
}

#include "OEMSolver.hh"

#include "Preconditioner.h"
#include "BiCGSolver.h"
#include "BiCGStab_M.h"
#include "CGSolver.h"
#include "GMResSolver.h"
#include "ODirSolver.h"
#include "OResSolver.h"

#endif // AMDIS_OEM_SOLVER_H