NonLinSolver.h 4.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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file NonLinSolver.h */

#ifndef AMDIS_NONLINSOLVER_H
#define AMDIS_NONLINSOLVER_H

#include <string>

#include "Global.h"
#include "CreatorInterface.h"
#include "MatrixVector.h"
#include "DOFMatrix.h"
#include "OEMSolver.h"
33
#include "ProblemStat.h"
34

35
36
37
38
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include "parallel/PetscProblemStat.h"
#endif

39
40
41
42
43
44
namespace AMDiS {

  /** 
   * \ingroup Solver
   *
   * \brief
45
   * Pure virtual base class for specific non linear solvers.
46
47
48
49
50
   */
  class NonLinSolver
  {
  public:
    /** \brief
51
52
53
     * Constructor
     *
     * \param name     Name of this solver
54
     */
55
56
    NonLinSolver(const std::string &name, OEMSolver *solver)
      : name(name),
57
	linSolver(solver),
58
	tolerance(1.e-8),
59
60
	maxIter(50),
	info(8),
61
	initialResidual(0.0),
62
63
64
65
66
67
68
69
70
	residual(0.0),
	usedNorm(NO_NORM)
    {
      Parameters::get(name + "->tolerance", tolerance);
      Parameters::get(name + "->max iteration", maxIter);
      Parameters::get(name + "->info", info);
      Parameters::get(name + "->norm", usedNorm);  
    }

71
    /// Destructor
72
73
74
75
76
77
    virtual ~NonLinSolver() {}

    /** \brief
     * solves the non linear system. Uses sub class methods
     */
    inline int solve(SolverMatrix<Matrix<DOFMatrix*> >& mat,
78
79
80
		     SystemVector &solution, SystemVector &rhs, 
		     AdaptInfo *adaptInfo,
		     ProblemStat *prob) 
81
82
    {
      init();
83
84
      solution.set(0.0);
      int result = nlsolve(mat, solution, rhs, adaptInfo, prob);
85
      exit();
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
      return result;
    }

    inline void setTolerance(double tol)
    {
      tolerance = tol;
    }

    inline double getTolerance()
    {
      return tolerance;
    }

    inline OEMSolver* getLinearSolver()
    { 
      return linSolver; 
    }

  protected:
106
    /// Allocates needed memory. Must be overriden in sub classes.
107
108
    virtual void init() = 0;

109
    /// Solves the non linear system. Must be overriden in sub classes.
110
    virtual int nlsolve(SolverMatrix<Matrix<DOFMatrix*> >& matVec,
111
112
113
			SystemVector& x, SystemVector& rhs, 
			AdaptInfo *adaptInfo,
			ProblemStat *prob) = 0;
114

115
    /// Frees needed memory. Must be overriden in sub classes.
116
117
118
    virtual void exit() = 0;
  
    virtual int solveLinearSystem(SolverMatrix<Matrix<DOFMatrix*> >& mat,
119
				  SystemVector &x, SystemVector &b)
120
121
122
    {
      FUNCNAME("NonLinSolver::solveLinearSystem()");
      TEST_EXIT(linSolver)("no solver\n");
123
      return linSolver->solveSystem(mat, x, b);
124
125
126
    }

  protected:
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    /// Name of the solver.
    std::string name;

    /// Linear solver object.
    OEMSolver *linSolver;

    /// Solver tolerance.
    double tolerance;

    /// Maximal number of iterations.
    int maxIter;

    /// Info level.
    int info;

    /// Initial residual.
    double initialResidual;

    /// Current residual.
    double residual; 

    /// Used norm for convergent test.
    Norm usedNorm;
150
151
152
  };


153
  /// Interface for creators of concrete NonLinSolvers. 
154
155
156
157
158
  class NonLinSolverCreator : public CreatorInterface<NonLinSolver>
  { 
  public:
    virtual ~NonLinSolverCreator() {}

159
    void setName(std::string n) 
160
    { 
161
      name = n; 
162
163
164
165
166
167
168
169
170
    }

    void setLinearSolver(OEMSolver *solver) 
    { 
      linearSolver = solver; 
    }

  protected:
    std::string name;
171

172
173
174
175
176
    OEMSolver *linearSolver;
  };

}

177
178
#include "Newton.h"

179
180
#endif // AMDIS_NONLINSOLVER_H