OEMSolver.h 8.33 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
  
    /** \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, 
99
100
	      Preconditioner<VectorType> *rPrecon = NULL,
	      bool reuseMatrix = false)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    {
      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();

117
      int iter = solveSystem(mv, x, b, reuseMatrix);
118
119
120
121
122
123
124
125
126
127

      if (leftPrecon) 
	leftPrecon->exit();

      if (rightPrecon) 
	rightPrecon->exit();

      exit();

      return iter;
128
    }
129
130
131
132
133
134
135
136
137
138

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

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

    /** \brief
     * Returns solvers \ref name.
     */
139
    inline const std::string& getName() { 
140
      return name; 
141
    }
142
143
144
145
146
147

    /** \brief
     * Returns \ref tolerance
     */
    inline double getTolerance() {
      return tolerance;
148
    }  
149
150
151
152
153
154

    /** \brief
     * Returns \ref max_iter
     */
    inline int getMaxIterations() {
      return max_iter;
155
    }
156
157
158
159
160
161

    /** \brief
     * Returns \ref residual
     */
    inline double getResidual() {
      return residual;
162
    }
163
164
165
166
167
168
169
170
171
172
173
174

    /** \} */ 

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

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

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

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

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

    /** \brief
     * Sets \ref info
     */
196
    inline void setInfo(int i) {
197
      info = i;
198
    }
199
200
201
  
    inline void setVectorCreator(CreatorInterface<VectorType> *creator) {
      vectorCreator = creator;
202
    }
203

204
    inline CreatorInterface<VectorType>* getVectorCreator() {
205
      return vectorCreator;
206
    }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

    /** \} */

  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,
225
226
227
			    VectorType *x, 
			    VectorType *b,
			    bool reuseMatrix) = 0;
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242


    /** \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.
     */
243
    int solve_info(const char *, int, double, double *);
244
245
246
247
248

  protected:
    /** \brief
     * solvers name.
     */
249
    std::string name;
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

    /** \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:
298
    virtual ~OEMSolverCreator() {}
299
300
301
302

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

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

#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