libMesh
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
29 #include "libmesh/nonlinear_solver.h"
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. 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 
126  SNESConvergedReason get_converged_reason();
127 
131  virtual int get_total_linear_iterations() libmesh_override;
132 
138  virtual unsigned get_current_nonlinear_iteration_number() const libmesh_override
140 
144  void set_residual_zero_out(bool state) { _zero_out_residual = state; }
145 
149  void set_jacobian_zero_out(bool state) { _zero_out_jacobian = state; }
150 
154  void use_default_monitor(bool state) { _default_monitor = state; }
155 
156 protected:
160  SNES _snes;
161 
172  SNESConvergedReason _reason;
173 
178 
183 
188 
193 
198 
199 #if !PETSC_VERSION_LESS_THAN(3,3,0)
201  void (*)(std::vector<NumericVector<Number> *> &, sys_type &),
202  MatNullSpace *);
203 #endif
204 private:
205  friend PetscErrorCode __libmesh_petsc_snes_residual (SNES snes, Vec x, Vec r, void * ctx);
206 #if PETSC_RELEASE_LESS_THAN(3,5,0)
207  friend PetscErrorCode __libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
208 #else
209  friend PetscErrorCode __libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat jac, Mat pc, void * ctx);
210 #endif
211 };
212 
213 
214 
215 } // namespace libMesh
216 
217 
218 #endif // #ifdef LIBMESH_HAVE_PETSC
219 #endif // LIBMESH_PETSC_NONLINEAR_SOLVER_H
unsigned _current_nonlinear_iteration_number
Stores the current nonlinear iteration number.
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
virtual void clear() libmesh_override
Release all memory and clear data structures.
PetscNonlinearSolver(sys_type &system)
Constructor.
void use_default_monitor(bool state)
Set to true to use the libMesh&#39;s default monitor, set to false to use your own.
PetscErrorCode __libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
const class libmesh_nullptr_t libmesh_nullptr
Numeric vector.
Definition: dof_map.h:66
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
PetscErrorCode __libmesh_petsc_snes_residual(SNES, Vec x, Vec r, void *ctx)
SNES _snes
Nonlinear solver context.
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)
bool _zero_out_residual
true to zero out residual before going into application level call-back, otherwise false ...
The libMesh namespace provides an interface to certain functionality in the library.
PetscErrorCode Vec Mat Mat pc
This class provides an interface to PETSc iterative solvers that is compatible with the libMesh Nonli...
Generic sparse matrix.
Definition: dof_map.h:65
PetscErrorCode Vec x
bool _zero_out_jacobian
true to zero out jacobian before going into application level call-back, otherwise false ...
PetscInt _n_linear_iterations
Stores the total number of linear iterations from the last solve.
This class provides a specific system class.
PetscErrorCode Vec Mat Mat void * ctx
virtual void init(const char *name=libmesh_nullptr) libmesh_override
Initialize data structures if not done so already.
PetscErrorCode __libmesh_petsc_snes_jacobian(SNES, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
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 *)
void set_residual_zero_out(bool state)
Set if the residual should be zeroed out in the callback.
SNESConvergedReason _reason
Store the reason for SNES convergence/divergence for use even after the _snes has been cleared...
void set_jacobian_zero_out(bool state)
Set if the jacobian should be zeroed out in the callback.
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
Prints a useful message about why the latest nonlinear solve con(di)verged.
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int) libmesh_override
Call the Petsc solver.
bool _default_monitor
true if we want the default monitor to be set, false for no monitor (i.e.
virtual unsigned get_current_nonlinear_iteration_number() const libmesh_override
virtual int get_total_linear_iterations() libmesh_override
Get the total number of linear iterations done in the last solve.
NonlinearImplicitSystem sys_type
The type of system.
Callable abstract base class to be used as a callback to provide the solver with a basis for the syst...