OEMSolver.h 8.5 KB
Newer Older
1
2
3
4
5
6
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// ==                                                                        ==
// ============================================================================

/** \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

#include "Global.h"
35
36
37
38
39
40
41
#include "AMDiS_fwd.h"
#include "Parameters.h"
#include "SolverMatrix.h"
#include "DOFVector.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "ITL_Preconditioner.h"
42
43
44
45
46
47
48
49
50
51
52
53

namespace AMDiS {

  /**
   * \ingroup Solver
   * 
   *\brief
   * Solver for linear equation systems.
   */
  class OEMSolver
  {
  public:
54
55
56
57

    typedef DOFMatrix::base_matrix_type   matrix_type;
    typedef DOFMatrix::value_type         value_type;

58
    /// The constructor reads needed parameters and sets solvers \ref name.
59
60
61
62
63
64
65
66
    OEMSolver(std::string str) 
      : name(str),
	tolerance(0),
	relative(0),
	max_iter(1000),
	info(0),
	residual(0),
	print_cycle(100),
67
68
	iterations(-1),
	error(-1),
69
	multipleRhs(false),
70
71
72
	leftPrecon(NULL),
	rightPrecon(NULL)
    {}
73

74
    ///
75
    virtual ~OEMSolver() 
Thomas Witkowski's avatar
Thomas Witkowski committed
76
    {}
77

78
79
80
    void initParameters()
    {
      FUNCNAME("OEMSolver::initParameters()");
81
      
82
83
84
85
86
87
      GET_PARAMETER(0, name + "->tolerance", "%f", &tolerance);
      GET_PARAMETER(0, name + "->relative tolerance", "%f", &relative);
      GET_PARAMETER(0, name + "->max iteration", "%d", &max_iter);
      GET_PARAMETER(0, name + "->print cycle", "%d", &print_cycle);
      GET_PARAMETER(0, name + "->info", "%d", &info);
    }
88

89
90
91
92
93
94
95
    /** Set left Preconditioner
     *
     *  It is assumed that the solver owns the preconditioner from this call.
     *  That means that the preconditioner is deleted by the solver when not needed.
     */
    void setLeftPrecon(ITL_BasePreconditioner* p) 
    {
Naumann, Andreas's avatar
Naumann, Andreas committed
96
97
      if (leftPrecon)
        delete leftPrecon;
Thomas Witkowski's avatar
Thomas Witkowski committed
98
      leftPrecon = p;
99
    }
100

101
102
103
104
105
106
107
    /** Set right Preconditioner
     *
     *  It is assumed that the solver owns the preconditioner from this call.
     *  That means that the preconditioner is deleted by the solver when not needed.
     */
    void setRightPrecon(ITL_BasePreconditioner* p) 
    {
Naumann, Andreas's avatar
Naumann, Andreas committed
108
109
      if (rightPrecon)
        delete rightPrecon;
Thomas Witkowski's avatar
Thomas Witkowski committed
110
      rightPrecon = p;
111
    }
112

113
    /// Linear System to be solved in the derived class
Thomas Witkowski's avatar
Thomas Witkowski committed
114
115
116
    virtual int solveSystem(const DOFMatrix::base_matrix_type& A,
			    mtl::dense_vector<value_type>& x,
			    const mtl::dense_vector<value_type>& b) = 0;
117
    
118

119
120
    /// Solve a linear system for a scalar problem.
    int solveSystem(const SolverMatrix<DOFMatrix>& A,
Thomas Witkowski's avatar
Thomas Witkowski committed
121
122
		    DOFVector<double>& x, 
		    DOFVector<double>& b) 
123
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
124
      mtl::dense_vector<value_type> xx(x.getUsedSize()), bb(b.getUsedSize());
Thomas Witkowski's avatar
Thomas Witkowski committed
125
126
127
128

      // Copy rhs vector
      int counter = 0;
      DOFVector<double>::Iterator it_b(&b, USED_DOFS);
129
      DOFVector<double>::Iterator it_x(&x, USED_DOFS);
130

131
132
133
134
135
      for (it_b.reset(), it_x.reset(); !it_b.end(); ++it_b, ++it_x) {	
	bb[counter] = *it_b;
	xx[counter] = *it_x;
	counter++;
      }
136

Thomas Witkowski's avatar
Thomas Witkowski committed
137
138
139
140
141
142
143
144
      int r = solveSystem(A.getMatrix(), xx, bb);

      // Copy solution vector to DOFVector
      counter = 0;
      for (it_x.reset(); !it_x.end(); ++it_x)
	*it_x = xx[counter++];

      return r;
145
    }
146

147
    /// Solve a linear system for a vectorial problem.
148
149
150
    virtual int solveSystem(const SolverMatrix<Matrix<DOFMatrix*> >& A,
			    SystemVector& x, 
			    SystemVector& b) 
151
    {
152
153
      int ns = x.getSize(),  // Number of systems.
	size = x.getUsedSize(); // Size of all DOFVectors
154
155
156
157

      // Copy vectors
      mtl::dense_vector<value_type>   xx(size), bb(size);
      for (int i = 0, counter = 0; i < ns; i++) {
158
159
160
161
162
163
164
165
	DOFVector<double>::Iterator it_b(b.getDOFVector(i), USED_DOFS);	  
	DOFVector<double>::Iterator it_x(x.getDOFVector(i), USED_DOFS);
	
	for (it_b.reset(), it_x.reset(); !it_b.end(); ++it_b, ++it_x) {	
	  bb[counter] = *it_b;
	  xx[counter] = *it_x;
	  counter++;
	}
166
      }
167
      
168
      // Solver on DOFVector for single system
169
      int r = solveSystem(A.getMatrix(), xx, bb);
170
      
171
      for (int i = 0, counter = 0; i < ns; i++) {
172
173
174
	DOFVector<double>::Iterator it(x.getDOFVector(i), USED_DOFS);	  
	for (it.reset(); !it.end(); ++it)	
	  *it = xx[counter++];	
175
      }
176
      
177
      return r;
178
    }
179
180
181
182
183

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

184
    /// Returns solvers \ref name.
185
    inline std::string getName() const
186
    { 
187
      return name; 
188
    }
189

190
    /// Returns \ref tolerance
191
    inline double getTolerance() const
192
    {
193
      return tolerance;
194
    }  
195

196
    /// Returns \ref max_iter
197
    inline int getMaxIterations() const
198
    {
199
      return max_iter;
200
    }
201

202
    /// Returns \ref residual
203
    inline double getResidual() const
204
    {
205
      return residual;
206
    }
207
208
209
210
211
212
213

    /** \} */ 

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

214
    /// Sets \ref tolerance
215
216
    inline void setTolerance(double tol) 
    {
217
      tolerance = tol;
218
    }
219

220
    /// Sets \ref relative
Thomas Witkowski's avatar
Thomas Witkowski committed
221
    inline void setRelative(double rel) 
222
    {
223
      relative = rel;
224
    }
225

226
    /// Sets \ref max_iter
227
228
    inline void setMaxIterations(int i) 
    {
229
      max_iter = i;
230
    }
231

232
233
234
    /// Returns number of iterations in last run of an iterative solver
    inline int getIterations() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
235
      return iterations;
236
237
238
239
240
    }

    /// Returns error code in last run of an iterative solver
    inline int getErrorCode() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
241
      return error;
242
243
    }

244
    /// Sets \ref info
245
246
    inline void setInfo(int i) 
    {
247
      info = i;
248
    }
249

250
251
252
253
254
    void setMultipleRhs(bool b)
    {
      multipleRhs = b;
    }

255
256
257
    /** \} */

  protected:
258
    /// solvers name.
259
    std::string name;
260

261
    /// Solver tolerance |r|. Set in OEMSolver's constructor. 
262
263
    double tolerance;

264
    /// Relative solver tolerance |r|/|r0|. Set in OEMSolver's constructor. 
265
    double relative;
266

267
    /// maximal number of iterations. Set in OEMSolver's constructor.
268
269
    int max_iter;

270
    /// info level during solving the system. Set in OEMSolver's constructor.
271
272
    int info;

273
    /// current residual.
274
275
    double residual;

276
277
    /// Print cycle, after how many iterations the residuum norm is logged.
    int print_cycle;
278

279
280
281
282
283
284
    /// How many iterations were performed in last solver (not set by UmfPack)
    int iterations;

    /// Error code  in last solver (not set by UmfPack)
    int error;

285
286
287
288
289
290
291
    /** \brief
     * If true, the solver has to solve multiple right hand sides with the same
     * matrix. Some solvers, e.g. direct once, may take advanteges from this knowledge,
     * as they can do the factorization of the matrix only once.
     */
    bool multipleRhs;

292
    /// Left preconditioner (ignored by some iterative solvers and by UmfPack)
293
    ITL_BasePreconditioner *leftPrecon;
294

295
    /// Right preconditioner (ignored by some iterative solvers and by UmfPack)
296
    ITL_BasePreconditioner *rightPrecon;
297
298
299
300
301
302
303
304
  };

  /** 
   * \ingroup Solver
   *
   * \brief
   * Interface for creators of concrete OEMSolvers. 
   */
305
  class OEMSolverCreator : public CreatorInterface<OEMSolver>
306
307
  { 
  public:
308
    virtual ~OEMSolverCreator() {}
309

310
    /// Sets \ref problem
311
312
    void setName(std::string str) 
    { 
313
      name = str; 
314
    }
315
316
317
318
319
320

  protected:
    /** \brief
     * Pointer to the problem the solver will belong to. Needed as parameter
     * when constructing an OEMSolver
     */
321
    std::string name;
322
323
324
  };
}

325
#include "ITL_Solver.h"
326
327

#endif // AMDIS_OEM_SOLVER_H