petsc_nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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 class ResidualContext;
38 
39 // Allow users access to these functions in case they want to reuse them. Users shouldn't
40 // need access to these most of the time as they are used internally by this object.
41 extern "C"
42 {
43  PetscErrorCode libmesh_petsc_snes_monitor (SNES, PetscInt its, PetscReal fnorm, void *);
44  PetscErrorCode libmesh_petsc_snes_residual (SNES, Vec x, Vec r, void * ctx);
45  PetscErrorCode libmesh_petsc_snes_fd_residual (SNES, Vec x, Vec r, void * ctx);
46  PetscErrorCode libmesh_petsc_snes_mffd_residual (SNES snes, Vec x, Vec r, void * ctx);
47  PetscErrorCode libmesh_petsc_snes_mffd_interface (void * ctx, Vec x, Vec r);
48 #if PETSC_RELEASE_LESS_THAN(3,5,0)
49  PetscErrorCode libmesh_petsc_snes_jacobian (SNES, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
50 #else
51  PetscErrorCode libmesh_petsc_snes_jacobian (SNES, Vec x, Mat jac, Mat pc, void * ctx);
52 #endif
53 
54  PetscErrorCode libmesh_petsc_snes_postcheck(
55 #if PETSC_VERSION_LESS_THAN(3,3,0)
56  SNES, Vec x, Vec y, Vec w, void * context, PetscBool * changed_y, PetscBool * changed_w
57 #else
58  SNESLineSearch, Vec x, Vec y, Vec w, PetscBool * changed_y, PetscBool * changed_w, void * context
59 #endif
60  );
61  PetscErrorCode libmesh_petsc_linesearch_shellfunc(SNESLineSearch linesearch, void * ctx);
62 
63 #ifdef LIBMESH_ENABLE_DEPRECATED
64  PetscErrorCode __libmesh_petsc_snes_monitor (SNES, PetscInt its, PetscReal fnorm, void *);
65  PetscErrorCode __libmesh_petsc_snes_residual (SNES, Vec x, Vec r, void * ctx);
66  PetscErrorCode __libmesh_petsc_snes_fd_residual (SNES, Vec x, Vec r, void * ctx);
67  PetscErrorCode __libmesh_petsc_snes_mffd_interface (void * ctx, Vec x, Vec r);
68 #if PETSC_RELEASE_LESS_THAN(3,5,0)
69  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
70 #else
71  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat jac, Mat pc, void * ctx);
72 #endif
73 
74  PetscErrorCode __libmesh_petsc_snes_postcheck(
75 #if PETSC_VERSION_LESS_THAN(3,3,0)
76  SNES, Vec x, Vec y, Vec w, void * context, PetscBool * changed_y, PetscBool * changed_w
77 #else
78  SNESLineSearch, Vec x, Vec y, Vec w, PetscBool * changed_y, PetscBool * changed_w, void * context
79 #endif
80  );
81 #endif
82 }
83 
92 template <typename T>
94 {
95 public:
100 
104  explicit
106 
111 
115  virtual void clear () override;
116 
121  virtual void init (const char * name = nullptr) override;
122 
126  SNES snes() { this->init(); return _snes; }
127 
132  virtual std::pair<unsigned int, Real>
133  solve (SparseMatrix<T> &, // System Jacobian Matrix
134  NumericVector<T> &, // Solution vector
135  NumericVector<T> &, // Residual vector
136  const double, // Stopping tolerance
137  const unsigned int) override; // N. Iterations
138 
143  virtual void print_converged_reason() override;
144 
152  SNESConvergedReason get_converged_reason();
153 
157  virtual int get_total_linear_iterations() override;
158 
164  virtual unsigned get_current_nonlinear_iteration_number() const override
166 
170  void set_residual_zero_out(bool state) { _zero_out_residual = state; }
171 
175  void set_jacobian_zero_out(bool state) { _zero_out_jacobian = state; }
176 
180  void use_default_monitor(bool state) { _default_monitor = state; }
181 
185  void set_snesmf_reuse_base(bool state) { _snesmf_reuse_base = state; }
186 
191  {
192  public:
194 
195  virtual void linesearch (SNESLineSearch linesearch) = 0;
196  };
197 
201  std::unique_ptr<ComputeLineSearchObject> linesearch_object;
202 
203 protected:
204 
208  SNES _snes;
209 
220  SNESConvergedReason _reason;
221 
226 
231 
236 
241 
246 
253 
254 #if !PETSC_VERSION_LESS_THAN(3,3,0)
256  void (*)(std::vector<NumericVector<Number> *> &, sys_type &),
257  MatNullSpace *);
258 #endif
259 private:
260  friend ResidualContext libmesh_petsc_snes_residual_helper (SNES snes, Vec x, void * ctx);
261  friend PetscErrorCode libmesh_petsc_snes_residual (SNES snes, Vec x, Vec r, void * ctx);
262  friend PetscErrorCode libmesh_petsc_snes_fd_residual (SNES snes, Vec x, Vec r, void * ctx);
263  friend PetscErrorCode libmesh_petsc_snes_mffd_residual (SNES snes, Vec x, Vec r, void * ctx);
264 #if PETSC_RELEASE_LESS_THAN(3,5,0)
265  friend PetscErrorCode libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
266 #else
267  friend PetscErrorCode libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat jac, Mat pc, void * ctx);
268 #endif
269 };
270 
271 
272 
273 } // namespace libMesh
274 
275 
276 #endif // #ifdef LIBMESH_HAVE_PETSC
277 #endif // LIBMESH_PETSC_NONLINEAR_SOLVER_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
friend PetscErrorCode libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
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 __libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
friend PetscErrorCode libmesh_petsc_snes_jacobian(SNES snes, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
PetscErrorCode __libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
virtual int get_total_linear_iterations() override
PetscErrorCode libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
PetscErrorCode __libmesh_petsc_snes_mffd_interface(void *ctx, Vec x, Vec r)
PetscErrorCode libmesh_petsc_snes_mffd_residual(SNES snes, Vec x, Vec r, void *ctx)
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)
const sys_type & system() const
PetscErrorCode libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
friend PetscErrorCode libmesh_petsc_snes_fd_residual(SNES snes, Vec x, Vec r, void *ctx)
friend PetscErrorCode libmesh_petsc_snes_mffd_residual(SNES snes, Vec x, Vec r, void *ctx)
friend ResidualContext libmesh_petsc_snes_residual_helper(SNES snes, Vec x, void *ctx)
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
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)
virtual unsigned get_current_nonlinear_iteration_number() const override
PetscErrorCode libmesh_petsc_snes_fd_residual(SNES snes, Vec x, Vec r, void *ctx)
PetscErrorCode libmesh_petsc_snes_mffd_interface(void *ctx, Vec x, Vec r)
virtual void linesearch(SNESLineSearch linesearch)=0
SNESConvergedReason get_converged_reason()
PetscTruth PetscBool
Definition: petsc_macro.h:67
PetscErrorCode libmesh_petsc_linesearch_shellfunc(SNESLineSearch linesearch, void *ctx)
std::unique_ptr< ComputeLineSearchObject > linesearch_object
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)
virtual void print_converged_reason() override
void build_mat_null_space(NonlinearImplicitSystem::ComputeVectorSubspace *computeSubspaceObject, void(*)(std::vector< NumericVector< Number > *> &, sys_type &), MatNullSpace *)
virtual void init(const char *name=nullptr) override
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int) override
NonlinearImplicitSystem sys_type
PetscErrorCode __libmesh_petsc_snes_fd_residual(SNES snes, Vec x, Vec r, void *ctx)