Newton.h 4.15 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
98
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ============================================================================
// ==                                                                        ==
// == 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 Newton.h */

#ifndef AMDIS_NEWTON_H
#define AMDIS_NEWTON_H

#include "CreatorInterface.h"
#include "NonLinSolver.h"
#include "OEMSolver.h"

namespace AMDiS {

  /**
   * \ingroup Solver
   * 
   * \Brief
   * Implements the newton method for solving a non linear system. Sub class of
   * NonLinSolver.
   */
  class Newton : public NonLinSolver
  {
  public:
    /// Creator class used in the NonLinSolverMap.
    class Creator : public NonLinSolverCreator
    {
    public:
      virtual ~Creator() {}

      /** \brief
       * Returns a new Newton object.
       */
      NonLinSolver* create() 
      { 
	return new Newton(this->name, this->linearSolver, this->nonLinUpdater); 
      }
    };

    /** \brief
     * Calls constructor of base class NonLinSolver
     */
    Newton(const std::string& name_, 
	   OEMSolver *linSolver_,
	   NonLinUpdater *updater)
      : NonLinSolver(name_, linSolver_, updater),
	b(NULL)
    {}

  private:
    /** \brief
     * realisation of NonLinSolver::init
     */
    void init()
    {}

    /** \brief
     * realisation of NonLinSolver::nlsolve
     */
    int nlsolve(SolverMatrix<Matrix<DOFMatrix*> >& mat,
		SystemVector& x, SystemVector& rhs)
    {
      //DOFVector<T> *d = problem->getRHS();
      //DOFVector<T> *x = problem->getSolution();;

      b = new SystemVector(x);
      *b = rhs;

      //   // copy operators from fh to b
      //   std::vector<Operator*>::iterator op;
      //   std::vector<double*>::iterator fac;
      //   for(op = d->getOperatorsBegin(), 
      // 	fac = d->getOperatorFactorBegin();
      //       op != d->getOperatorsEnd();
      //       ++op, ++fac) 
      //   {
      //     b->addOperator(*op, *fac);
      //   }

      double err = 0.0, err_old = -1.0;
      int    iter, n;

      INFO(this->info,2)("iter. |     this->residual |     red. |    n |\n");

      for (iter = 1; iter <= this->maxIter; iter++) {
	/*--- Assemble DF(x) and F(x) ----------------------------------------------*/
	this->nonLinUpdater->update(/*x,*/true, b);
	/*--- Initial guess is zero ------------------------------------------------*/
	rhs.set(0.0);
	/*--- solve linear system --------------------------------------------------*/
	n = solveLinearSystem(mat, *b, rhs);
	/*--- x = x - d ------------------------------------------------------------*/
	x -= rhs;

	if (this->usedNorm == NO_NORM || this->usedNorm == L2_NORM)
	  err = L2Norm(&rhs);  // sollte hier nicht b genommen werden (s. NewtonS) ?
	else
	  err = H1Norm(&rhs);  // sollte hier nicht b genommen werden (s. NewtonS) ?
    

	if (iter == 1)  this->initial_residual = err;

	if (err_old <= 0) {
	  INFO(this->info,2)("%5d | %12.5e | -------- | %4d |\n", iter, err, n);
	} else {
	  INFO(this->info,2)("%5d | %12.5e | %8.2e | %4d |\n", 
			     iter, err, err/err_old, n);
	}

	if ((this->residual = err) < this->tolerance) {
	  INFO(this->info,4)("finished successfully\n");
	  return iter;
	}
	err_old = err;
      }

      if (this->info < 2)
	INFO(this->info,1)("iter. %d, residual: %12.5e\n", iter, err);
      INFO(this->info,1)("tolerance %e not reached\n", this->tolerance);

      this->residual = err;

      return iter;
    }

    /** \brief
     * realisation of NonLinSolver::exit
     */
    void exit()
    {
      if (b != NULL)
	delete b;
    }

  private:
    /** \brief
     * internal used data
     */
    SystemVector *b;
  };

}

#endif // AMDIS_NEWTON_H