HermesCommon  2.0
newton_solver_nox.h
Go to the documentation of this file.
1 // This file is part of HermesCommon
2 //
3 // Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
4 // Email: hpfem-group@unr.edu, home page: http://hpfem.org/.
5 //
6 // Hermes2D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the License,
9 // or (at your option) any later version.
10 //
11 // Hermes2D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Hermes2D; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef __HERMES_COMMON_NEWTON_SOLVER_NOX_H_
23 #define __HERMES_COMMON_NEWTON_SOLVER_NOX_H_
24 
25 #include "linear_matrix_solver.h"
26 #include "nonlinear_solver.h"
27 #include "epetra.h"
28 #if(defined HAVE_NOX && defined HAVE_EPETRA && defined HAVE_TEUCHOS)
29 #include <NOX.H>
30 #ifdef _POSIX_C_SOURCE
31 # undef _POSIX_C_SOURCE // pyconfig.h included by NOX_Epetra defines it
32 #endif
33 #ifdef _XOPEN_SOURCE
34 # undef _XOPEN_SOURCE // pyconfig.h included by NOX_Epetra defines it
35 #endif
36 #include <NOX_Epetra.H>
37 #include "exceptions.h"
38 
39 namespace Hermes
40 {
41  namespace Solvers
42  {
45  template <typename Scalar>
46  class HERMES_API DiscreteProblemNOX :
47  public NOX::Epetra::Interface::Required,
48  public NOX::Epetra::Interface::Jacobian,
49  public NOX::Epetra::Interface::Preconditioner
50  {
51  public:
52  DiscreteProblemNOX(DiscreteProblemInterface<Scalar> * problem);
53 
55  void set_precond(Teuchos::RCP<Precond<Scalar> > &pc);
57  Teuchos::RCP<Precond<Scalar> > get_precond();
58 
59  // that is generated by the Interface class.
60  EpetraMatrix<Scalar> *get_jacobian();
61 
63  virtual bool computeF(const Epetra_Vector &x, Epetra_Vector &f, FillType flag = Residual);
64 
66  virtual bool computeJacobian(const Epetra_Vector &x, Epetra_Operator &op);
67 
70  virtual bool computePreconditioner(const Epetra_Vector &x, Epetra_Operator &m,
71  Teuchos::ParameterList *precParams = 0);
72 
73  private:
74  DiscreteProblemInterface<Scalar> * dp;
76  EpetraMatrix<Scalar> jacobian;
78  Teuchos::RCP<Precond<Scalar> > precond;
79  };
80 
84  template <typename Scalar>
85  class HERMES_API NewtonSolverNOX : public NonlinearSolver<Scalar>
86  {
87  private:
88  DiscreteProblemNOX<Scalar> ndp;
89  Teuchos::RCP<Teuchos::ParameterList> nl_pars;
90  public:
92  NewtonSolverNOX(DiscreteProblemInterface<Scalar> *problem);
93 
94  virtual ~NewtonSolverNOX();
95 
97  virtual void set_time(double time);
98  virtual void set_time_step(double time_step);
99 
100  virtual void solve(Scalar* coeff_vec);
101 
102  virtual int get_num_iters();
103  virtual double get_residual();
104  int get_num_lin_iters();
105  double get_achieved_tol();
106 
114  void set_output_flags(int flags);
115 
125  void set_ls_type(const char *type);
127  void set_ls_max_iters(int iters);
129  void set_ls_tolerance(double tolerance);
131  void set_ls_sizeof_krylov_subspace(int size);
133 
140  void set_norm_type(NOX::Abstract::Vector::NormType type);
144  void set_scale_type(NOX::StatusTest::NormF::ScaleType type);
146  void set_conv_iters(int iters);
148  void set_conv_abs_resid(double resid);
150  void set_conv_rel_resid(double resid);
152  void disable_abs_resid();
154  void disable_rel_resid();
156  void set_conv_update(double update);
160  void set_conv_wrms(double rtol, double atol);
162 
175  void set_precond_reuse(const char * pc_reuse);
179  void set_precond_max_age(int max_age);
182  virtual void set_precond(Precond<Scalar> &pc);
189  virtual void set_precond(const char *pc);
190 
191  protected:
192  int num_iters;
193  double residual;
194  int num_lin_iters;
195  double achieved_tol;
196 
197  // convergence params
198  struct conv_t
199  {
200  int max_iters;
201  double abs_resid;
202  double rel_resid;
203  NOX::Abstract::Vector::NormType norm_type;
204  NOX::StatusTest::NormF::ScaleType stype;
205  double update;
206  double wrms_rtol;
207  double wrms_atol;
208  } conv;
209 
210  struct conv_flag_t
211  {
212  unsigned absresid:1;
213  unsigned relresid:1;
214  unsigned wrms:1;
215  unsigned update:1;
216  } conv_flag;
217  };
218  }
219 }
220 #endif
221 #endif