~ubuntu-branches/ubuntu/trusty/rheolef/trusty-proposed

« back to all changes in this revision

Viewing changes to nfem/lib/newton.h

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2010-06-12 09:08:59 UTC
  • Revision ID: james.westby@ubuntu.com-20100612090859-8gpm2gc7j3ab43et
Tags: upstream-5.89
ImportĀ upstreamĀ versionĀ 5.89

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ifndef _RHEO_NEWTON_H
 
2
# define _RHEO_NEWTON_H
 
3
///
 
4
/// This file is part of Rheolef.
 
5
///
 
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
7
///
 
8
/// Rheolef is free software; you can redistribute it and/or modify
 
9
/// it under the terms of the GNU General Public License as published by
 
10
/// the Free Software Foundation; either version 2 of the License, or
 
11
/// (at your option) any later version.
 
12
///
 
13
/// Rheolef is distributed in the hope that it will be useful,
 
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
/// GNU General Public License for more details.
 
17
///
 
18
/// You should have received a copy of the GNU General Public License
 
19
/// along with Rheolef; if not, write to the Free Software
 
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
/// 
 
22
/// =========================================================================
 
23
 
 
24
/*Class:newton
 
25
NAME: @code{newton} -- Newton nonlinear algorithm
 
26
@findex newton
 
27
@cindex nonlinear problem
 
28
@cindex Newton method
 
29
DESCRIPTION: 
 
30
  @noindent
 
31
  Nonlinear Newton algorithm for the resolution of the following problem:
 
32
  @example
 
33
       F(u) = 0
 
34
  @end example
 
35
  A simple call to the algorithm writes:
 
36
  @example
 
37
    my_problem P;
 
38
    field uh (Vh);
 
39
    newton (P, uh, tol, max_iter);
 
40
  @end example
 
41
  The @code{my_problem} class may contains methods for the evaluation
 
42
  of F (aka residue) and its derivative:
 
43
  @example
 
44
    class my_problem @{
 
45
    public:
 
46
      my_problem();
 
47
      field residue (const field& uh) const;
 
48
      void update_derivative (const field& uh) const;
 
49
      field derivative_solve (const field& mrh) const;
 
50
      Float norm (const field& uh) const;
 
51
      Float dual_norm (const field& Muh) const;
 
52
    @};
 
53
  @end example
 
54
  See the example @code{p-laplacian.h} in the user's documentation 
 
55
  for more.
 
56
AUTHOR: 
 
57
   | Pierre.Saramito@imag.fr
 
58
    LJK, 38041 Grenoble cedex 9, France
 
59
DATE:   14 oct 2009
 
60
METHODS: @newton
 
61
End:
 
62
*/
 
63
 
 
64
//<newton:
 
65
template <class Problem, class Field>
 
66
int newton (Problem P, Field& uh, Float& tol, size_t& max_iter, std::ostream *p_cerr = 0) {
 
67
    if (p_cerr) *p_cerr << "# Newton: n r" << std::endl;
 
68
    for (size_t n = 0; true; n++) {
 
69
      Field rh = P.residue(uh);
 
70
      Float r = P.dual_norm(rh);
 
71
      if (p_cerr) *p_cerr << n << " " << r << std::endl;
 
72
      if (r <= tol) { tol = r; max_iter = n; return 0; }
 
73
      if (n == max_iter) { tol = r; return 1; }
 
74
      P.update_derivative (uh);
 
75
      Field delta_uh = P.derivative_solve (-rh);
 
76
      uh += delta_uh;
 
77
    }
 
78
}
 
79
//>newton:
 
80
# endif // _RHEO_NEWTON_H