petsc_nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PETSC_NONLINEAR_SOLVER_H
21 #define LIBMESH_PETSC_NONLINEAR_SOLVER_H
22 
23 #include "libmesh/libmesh_config.h"
24 
25 // Petsc include files.
26 #ifdef LIBMESH_HAVE_PETSC
27 
28 // Local includes
30 #include "libmesh/petsc_macro.h"
31 
32 // PETSc includes
33 # include <petscsnes.h>
34 
35 namespace libMesh
36 {
37 // Allow users access to these functions in case they want to reuse them. Note that users shouldn't
38 // need access to these most of the time as they are used internally by this object.
39 extern "C"
40 {
41  PetscErrorCode __libmesh_petsc_snes_monitor (SNES, PetscInt its, PetscReal fnorm, void *);
42  PetscErrorCode __libmesh_petsc_snes_residual (SNES, Vec x, Vec r, void * ctx);
43 #if PETSC_RELEASE_LESS_THAN(3,5,0)
44  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
45 #else
46  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat jac, Mat pc, void * ctx);
47 #endif
48 
49  PetscErrorCode __libmesh_petsc_snes_postcheck(
50 #if PETSC_VERSION_LESS_THAN(3,3,0)
51  SNES, Vec x, Vec y, Vec w, void * context, PetscBool * changed_y, PetscBool * changed_w
52 #else
53  SNESLineSearch, Vec x, Vec y, Vec w, PetscBool * changed_y, PetscBool * changed_w, void * context
54 #endif
55  );
56 }
57 
66 template <typename T>
68 {
69 public:
74 
78  explicit
79  PetscNonlinearSolver (sys_type & system);
80 
85 
89  virtual void clear () libmesh_override;
90 
95  virtual void init (const char * name = libmesh_nullptr) libmesh_override;
96 
100  SNES snes() { this->init(); return _snes; }
101 
106  virtual std::pair<unsigned int, Real>
107  solve (SparseMatrix<T> &, // System Jacobian Matrix
108  NumericVector<T> &, // Solution vector
109  NumericVector<T> &, // Residual vector
110  const double, // Stopping tolerance
111  const unsigned int) libmesh_override; // N. Iterations
112 
117  virtual void print_converged_reason() libmesh_override;
118 
124  SNESConvergedReason get_converged_reason();
125 
129  virtual int get_total_linear_iterations() libmesh_override;
130 
136  virtual unsigned get_current_nonlinear_iteration_number() const libmesh_override
138 
142  void set_residual_zero_out(bool state) { _zero_out_residual = state; }
143 
147  void set_jacobian_zero_out(bool state) { _zero_out_jacobian = state; }
148 
152  void use_default_monitor(bool state) { _default_monitor = state; }
153 
154 protected:
158  SNES _snes;
159 
167  SNESConvergedReason _reason;
168 
173 
178 
183 
188 
193 
194 #if !PETSC_VERSION_LESS_THAN(3,3,0)
196  void (*)(std::vector<NumericVector<Number> *> &, sys_type &),
197  MatNullSpace *);
198 #endif
199 private:
200  friend PetscErrorCode __libmesh_petsc_snes_residual (SNES snes, Vec x, Vec r, void * ctx);
201 #if PETSC_RELEASE_LESS_THAN(3,5,0)
202  friend PetscErrorCode __libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
203 #else
204  friend PetscErrorCode __libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat jac, Mat pc, void * ctx);
205 #endif
206 };
207 
208 
209 
210 } // namespace libMesh
211 
212 
213 #endif // #ifdef LIBMESH_HAVE_PETSC
214 #endif // LIBMESH_PETSC_NONLINEAR_SOLVER_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
virtual void clear() libmesh_override
PetscErrorCode __libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
PetscErrorCode __libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
const class libmesh_nullptr_t libmesh_nullptr
PetscErrorCode __libmesh_petsc_snes_postcheck(#if PETSC_VERSION_LESS_THAN(3, 3, 0) SNES, Vec x, Vec y, Vec w, void *context, PetscBool *changed_y, PetscBool *changed_w#else SNESLineSearch, Vec x, Vec y, Vec w, PetscBool *changed_y, PetscBool *changed_w, void *context#endif)
PetscErrorCode Vec Mat Mat pc
PetscErrorCode Vec x
PetscErrorCode __libmesh_petsc_snes_jacobian(#if PETSC_RELEASE_LESS_THAN(3, 5, 0) SNES snes, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx#else SNES snes, Vec x, Mat jac, Mat pc, void *ctx#endif)
Used for solving nonlinear implicit systems of equations.
PetscErrorCode Vec Mat Mat void * ctx
virtual void init(const char *name=libmesh_nullptr) libmesh_override
friend PetscErrorCode __libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
void build_mat_null_space(NonlinearImplicitSystem::ComputeVectorSubspace *computeSubspaceObject, void(*)(std::vector< NumericVector< Number > * > &, sys_type &), MatNullSpace *)
SNESConvergedReason get_converged_reason()
friend PetscErrorCode __libmesh_petsc_snes_jacobian(SNES snes, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
PetscTruth PetscBool
Definition: petsc_macro.h:64
const sys_type & system() const
virtual void print_converged_reason() libmesh_override
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int) libmesh_override
virtual unsigned get_current_nonlinear_iteration_number() const libmesh_override
virtual int get_total_linear_iterations() libmesh_override
NonlinearImplicitSystem sys_type