LinearSolver.h 7.02 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
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


/** \file LinearSolver.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"
#include "AMDiS_fwd.h"
#include "Initfile.h"
#include "solver/SolverMatrix.h"
#include "DOFVector.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "Mapper.h"
#include "Timer.h"

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include "parallel/ParallelMapper.h"
#endif

namespace AMDiS {
  
  /// Base-Class for Preconditioner types
  struct OEMPreconditioner
  {
    virtual void exit() {}
  };

  /// Base-Class for Runner / Worker types
  struct OEMRunner
  {
    virtual void exit() {}
    
    virtual OEMPreconditioner* getLeftPrecon()
    {
      return NULL;
    }    
    
    virtual OEMPreconditioner* getRightPrecon()
    {
      return NULL;
    }
  };
  
  /**
   * \ingroup Solver
   * 
   *\brief
   * Solver for linear equation systems.
   */
  class LinearSolver
  {
  public:

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

    /// The constructor reads needed parameters and sets solvers \ref name.
    LinearSolver(std::string str) 
      : name(str),
	tolerance(DBL_TOL),
	relative(0),
	max_iter(1000),
	info(0),
	residual(-1.0),
	print_cycle(100),
	iterations(-1),
	error(-1),
Praetorius, Simon's avatar
Praetorius, Simon committed
98
	breakTolNotReached(true),
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
	calculateResidual(false)
    {      
      Parameters::get(name + "->tolerance", tolerance);
      Parameters::get(name + "->relative tolerance", relative);
      Parameters::get(name + "->max iteration", max_iter);
      Parameters::get(name + "->print cycle", print_cycle);
      Parameters::get(name + "->info", info);
      Parameters::get(name + "->break if tolerance not reached", breakTolNotReached);
      Parameters::get(name + "->calculate residual", calculateResidual);
    }

    /// destructor
    virtual ~LinearSolver() 
    {}


    int solveSystem(const SolverMatrix<Matrix<DOFMatrix*> >& A,
		    SystemVector& x,
		    SystemVector& b,
		    bool createMatrixData,
		    bool storeMatrixData)
    { FUNCNAME("LinearSolver::solveSystem()");
      MSG("LinearSolver::solveSystem()\n");
    
      residual = -1.0;
      int error_code = solveLinearSystem(A, x, b, createMatrixData, storeMatrixData);
      
      // calculate and print resiual
      if (info > 0) {
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
	if (residual < 0 || calculateResidual) {
	  SystemVector r(b);
	  mv(*A.getOriginalMat(), x, r);
	  r -= b;
	  residual = norm(&r);
	}
#endif
	
	MSG("Residual norm: ||b-Ax|| = %e\n", residual);

Praetorius, Simon's avatar
Praetorius, Simon committed
139
	TEST_EXIT((isNumber(residual) && residual <= tolerance) || !breakTolNotReached)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	  ("Tolerance tol = %e could not be reached!\n Set tolerance by '->solver->tolerance:' \n", tolerance);
      }
      return error_code;
    }

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

    /// Returns solvers \ref name.
    inline std::string getName() const
    { 
      return name; 
    }

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

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

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

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

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

    /// Returns \ref print_cycle
    inline int getPrint_cycle() const
    {
      return print_cycle;
    }

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

    /// Returns \ref relative
    inline double getRelative() const
    {
      return relative;
    }

    
    virtual OEMPreconditioner* getLeftPrecon()
    {
      FUNCNAME("LinearSolver::getLeftPrecon()");
      ERROR_EXIT("no left preconditioner provided!\n");
      return NULL;
    }
    
    virtual OEMPreconditioner* getRightPrecon()
    {
      FUNCNAME("LinearSolver::getRightPrecon()");
      ERROR_EXIT("no right preconditioner provided!\n");
      return NULL;
    }
    
    virtual OEMRunner* getRunner()
    {
      FUNCNAME("LinearSolver::getRunner()");
      ERROR_EXIT("no runner provided!\n");
      return NULL;
    }

    /** \} */ 

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

    /// Sets \ref tolerance
    inline void setTolerance(double tol) 
    {
      tolerance = tol;
    }

    inline void setResidual(double r)
    {
      residual = r;
    }

    /// Sets \ref relative
    inline void setRelative(double rel) 
    {
      relative = rel;
    }

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

    inline void setIterations(int i)
    {
      iterations=i;
    }

    /// set the \ref error
    inline void setErrorCode(int code)
    {
      error=code;
    }
  
    /// Sets \ref info
    inline void setInfo(int i) 
    {
      info = i;
    }

    /** \} */
    
  protected:
    /// main methods that all solvers must implement
    virtual int solveLinearSystem(const SolverMatrix<Matrix<DOFMatrix*> >& A,
				  SystemVector& x,
				  SystemVector& b,
				  bool createMatrixData,
				  bool storeMatrixData) = 0;

  protected:
    /// solvers name.
    std::string name;

    /// Solver tolerance |r|. Set in LinearSolver's constructor. 
    double tolerance;

    /// Relative solver tolerance |r|/|r0|. Set in LinearSolver's constructor. 
    double relative;

    /// maximal number of iterations. Set in LinearSolver's constructor.
    int max_iter;

    /// info level during solving the system. Set in LinearSolver's constructor.
    int info;

    /// current residual.
    double residual;

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

    /// 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;
    
    // break if residual norm > prescribed tolerance
    bool breakTolNotReached;
    
    // calculate residual ||b-Ax|| directly
    bool calculateResidual;
  };
  
  
  typedef CreatorInterfaceName<LinearSolver> LinearSolverCreator;
}

#endif // AMDIS_OEM_SOLVER_H