LinearSolverInterface.h 7.72 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * 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.
18
 *
19
20
21
22
23
24
25
26
 ******************************************************************************/


/** \file LinearSolverInterface.h */

/**
 * \defgroup Solver Solver module
 * @{ <img src="solver.png"> @}
27
 *
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 * \brief
 * Contains all classes needed for solving linear and non linear equation
 * systems.
 */

#ifndef AMDIS_LINEAR_SOLVER_H
#define AMDIS_LINEAR_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 {
51

52
53
54
55
56
57
58
59
60
61
  /// Non-templated base-class for Preconditioner types
  struct PreconditionerInterface
  {
    virtual void exit() {}
  };

  /// Non-templates base-class for Runner / Worker types
  struct RunnerInterface
  {
    virtual void exit() {}
62

63
64
65
    virtual PreconditionerInterface* getLeftPrecon()
    {
      return NULL;
66
67
    }

68
69
70
71
72
    virtual PreconditionerInterface* getRightPrecon()
    {
      return NULL;
    }
  };
73

74
75
  /**
   * \ingroup Solver
76
   *
77
78
79
80
81
82
83
   *\brief
   * Solver for linear equation systems.
   */
  class LinearSolverInterface
  {
  public:
    /// The constructor reads needed parameters and sets solvers \ref name.
84
    LinearSolverInterface(std::string name_)
85
86
87
88
89
90
91
92
93
94
95
      : name(name_),
	tolerance(DBL_TOL),
	relative(0),
	max_iter(1000),
	info(0),
	residual(-1.0),
	rel_residual(-1.0),
	print_cycle(100),
	iterations(-1),
	error(-1),
	breakTolNotReached(true)
96
    {
97
98
99
100
101
102
103
104
105
      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);
    }

    /// destructor
106
    virtual ~LinearSolverInterface()
107
108
109
110
111
112
113
114
115
116
    {}


    int solveSystem(const SolverMatrix<Matrix<DOFMatrix*> >& A,
		    SystemVector& x,
		    SystemVector& b,
		    bool createMatrixData,
		    bool storeMatrixData)
    { FUNCNAME("LinearSolverInterface::solveSystem()");
      MSG("LinearSolverInterface::solveSystem()\n");
117

118
119
120
      residual = -1.0;
      rel_residual = -1.0;
      int error_code = solveLinearSystem(A, x, b, createMatrixData, storeMatrixData);
121

122
123
124
125
126
127
128
      // calculate and print resiual
      if (info > 0) {
	if (residual >= 0.0 && rel_residual >= 0.0) {
	  MSG("Residual norm: ||b-Ax|| = %e, ||b-Ax||/||b|| = %e\n", residual, rel_residual);
	} else if (residual >= 0.0) {
	  MSG("Residual norm: ||b-Ax|| = %e\n", residual);
	}
129

130
#ifndef NDEBUG
131
132
133
	if (getIterations() > 0) {
	  MSG("Nr. of iterations needed = %d\n", getIterations());
	}
134

135
136
137
	if (error_code != 0) {
	  MSG("ERROR-Code = %d\n", error_code);
	}
138

139
140
141
142
	if (!isNumber(residual) || !isNumber(rel_residual)) {
	  MSG("Residual or relative residual is NaN/Inf!\n");
	}
#endif
143
144

	// test for absolute tolerance
145
146
147
	TEST_EXIT((isNumber(residual) && (residual < 0.0  || tolerance < 1.e-30 || residual <= tolerance))
		  || !breakTolNotReached)
	  ("Tolerance tol = %e could not be reached!\n Set tolerance by '->solver->tolerance:' \n", tolerance);
148

149
	// test for relative tolerance
150
151
152
	TEST_EXIT((isNumber(rel_residual) && (rel_residual < 0.0  || relative < 1.e-30 || rel_residual <= relative))
		  || (residual < 1.e-30) || !breakTolNotReached)
	  ("Relative tolerance rtol = %e could not be reached!\n Set tolerance by '->solver->relative tolerance:' \n", relative);
153
154
155
156
157
      }
      return error_code;
    }

    /** \name getting methods
158
     * \{
159
160
161
162
     */

    /// Returns solvers \ref name.
    inline std::string getName() const
163
164
    {
      return name;
165
166
167
168
169
170
    }

    /// Returns \ref tolerance
    inline double getTolerance() const
    {
      return tolerance;
171
    }
172
173
174
175
176
177
178
179

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

    /// Returns number of iterations in last run of an iterative solver
180
    inline int getIterations()
181
182
183
184
185
    {
      return iterations;
    }

    /// Returns error code in last run of an iterative solver
186
    inline int getErrorCode()
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
    {
      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;
    }

215

216
217
218
219
220
221
    virtual PreconditionerInterface* getLeftPrecon()
    {
      FUNCNAME("LinearSolverInterface::getLeftPrecon()");
      ERROR("no left preconditioner provided!\n");
      return NULL;
    }
222

223
224
225
226
227
228
    virtual PreconditionerInterface* getRightPrecon()
    {
      FUNCNAME("LinearSolverInterface::getRightPrecon()");
      ERROR("no right preconditioner provided!\n");
      return NULL;
    }
229

230
231
232
233
234
235
236
    virtual RunnerInterface* getRunner()
    {
      FUNCNAME("LinearSolverInterface::getRunner()");
      ERROR("no runner provided!\n");
      return NULL;
    }

237
    /** \} */
238
239

    /** \name setting methods
240
     * \{
241
242
243
     */

    /// Sets \ref tolerance
244
    inline void setTolerance(double tol)
245
246
247
248
249
250
251
252
253
254
    {
      tolerance = tol;
    }

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

    /// Sets \ref relative
255
    inline void setRelative(double rel)
256
257
258
259
260
261
262
263
264
265
    {
      relative = rel;
    }

    inline void setRelativeResidual(double r)
    {
      rel_residual = r;
    }

    /// Sets \ref max_iter
266
    inline void setMaxIterations(int i)
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    {
      max_iter = i;
    }

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

    /// set the \ref error
    inline void setErrorCode(int code)
    {
      error=code;
    }
281

282
    /// Sets \ref info
283
    inline void setInfo(int i)
284
285
286
287
288
    {
      info = i;
    }

    /** \} */
289

290
291
292
293
294
295
296
297
298
299
300
301
  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;

302
    /// Solver tolerance |r|. Set in LinearSolverInterface's constructor.
303
304
    double tolerance;

305
    /// Relative solver tolerance |r|/|r0|. Set in LinearSolverInterface's constructor.
306
307
308
309
310
311
312
313
314
315
    double relative;

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

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

    /// current absolute residual norm.
    double residual;
316

317
318
319
320
321
322
323
324
325
326
327
    /// current relative residual norm.
    double rel_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;
328

329
330
331
    /// break if residual norm > prescribed tolerance
    bool breakTolNotReached;
  };
332
333


334
335
336
337
  typedef CreatorInterfaceName<LinearSolverInterface> LinearSolverCreator;
}

#endif // AMDIS_LINEAR_SOLVER_H