libMesh
petsc_nonlinear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 #ifdef I
34 # define LIBMESH_SAW_I
35 #endif
36 #include <petscsnes.h>
37 #ifndef LIBMESH_SAW_I
38 # undef I // Avoid complex.h contamination
39 #endif
40 
41 namespace libMesh
42 {
43 class ResidualContext;
44 
45 // Allow users access to these functions in case they want to reuse them. Users shouldn't
46 // need access to these most of the time as they are used internally by this object.
47 extern "C"
48 {
49  PetscErrorCode libmesh_petsc_snes_monitor (SNES, PetscInt its, PetscReal fnorm, void *);
50  PetscErrorCode libmesh_petsc_snes_residual (SNES, Vec x, Vec r, void * ctx);
51  PetscErrorCode libmesh_petsc_snes_fd_residual (SNES, Vec x, Vec r, void * ctx);
52  PetscErrorCode libmesh_petsc_snes_mffd_residual (SNES snes, Vec x, Vec r, void * ctx);
53  PetscErrorCode libmesh_petsc_snes_mffd_interface (void * ctx, Vec x, Vec r);
54 #if PETSC_RELEASE_LESS_THAN(3,5,0)
55  PetscErrorCode libmesh_petsc_snes_jacobian (SNES, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
56 #else
57  PetscErrorCode libmesh_petsc_snes_jacobian (SNES, Vec x, Mat jac, Mat pc, void * ctx);
58 #endif
59 
60  PetscErrorCode libmesh_petsc_snes_postcheck(
61 #if PETSC_VERSION_LESS_THAN(3,3,0)
62  SNES, Vec x, Vec y, Vec w, void * context, PetscBool * changed_y, PetscBool * changed_w
63 #else
64  SNESLineSearch, Vec x, Vec y, Vec w, PetscBool * changed_y, PetscBool * changed_w, void * context
65 #endif
66  );
67  PetscErrorCode libmesh_petsc_linesearch_shellfunc(SNESLineSearch linesearch, void * ctx);
68 
69 #ifdef LIBMESH_ENABLE_DEPRECATED
70  PetscErrorCode __libmesh_petsc_snes_monitor (SNES, PetscInt its, PetscReal fnorm, void *);
71  PetscErrorCode __libmesh_petsc_snes_residual (SNES, Vec x, Vec r, void * ctx);
72  PetscErrorCode __libmesh_petsc_snes_fd_residual (SNES, Vec x, Vec r, void * ctx);
73  PetscErrorCode __libmesh_petsc_snes_mffd_interface (void * ctx, Vec x, Vec r);
74 #if PETSC_RELEASE_LESS_THAN(3,5,0)
75  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
76 #else
77  PetscErrorCode __libmesh_petsc_snes_jacobian (SNES, Vec x, Mat jac, Mat pc, void * ctx);
78 #endif
79 
80  PetscErrorCode __libmesh_petsc_snes_postcheck(
81 #if PETSC_VERSION_LESS_THAN(3,3,0)
82  SNES, Vec x, Vec y, Vec w, void * context, PetscBool * changed_y, PetscBool * changed_w
83 #else
84  SNESLineSearch, Vec x, Vec y, Vec w, PetscBool * changed_y, PetscBool * changed_w, void * context
85 #endif
86  );
87 #endif
88 }
89 
98 template <typename T>
100 {
101 public:
106 
110  explicit
112 
117 
121  virtual void clear () override;
122 
127  virtual void init (const char * name = nullptr) override;
128 
132  SNES snes() { this->init(); return _snes; }
133 
138  virtual std::pair<unsigned int, Real>
139  solve (SparseMatrix<T> &, // System Jacobian Matrix
140  NumericVector<T> &, // Solution vector
141  NumericVector<T> &, // Residual vector
142  const double, // Stopping tolerance
143  const unsigned int) override; // N. Iterations
144 
149  virtual void print_converged_reason() override;
150 
158  SNESConvergedReason get_converged_reason();
159 
163  virtual int get_total_linear_iterations() override;
164 
170  virtual unsigned get_current_nonlinear_iteration_number() const override
172 
176  void set_residual_zero_out(bool state) { _zero_out_residual = state; }
177 
181  void set_jacobian_zero_out(bool state) { _zero_out_jacobian = state; }
182 
186  void use_default_monitor(bool state) { _default_monitor = state; }
187 
191  void set_snesmf_reuse_base(bool state) { _snesmf_reuse_base = state; }
192 
197  {
198  public:
200 
201  virtual void linesearch (SNESLineSearch linesearch) = 0;
202  };
203 
207  std::unique_ptr<ComputeLineSearchObject> linesearch_object;
208 
209 protected:
210 
214  SNES _snes;
215 
226  SNESConvergedReason _reason;
227 
232 
237 
242 
247 
252 
259 
260 #if !PETSC_VERSION_LESS_THAN(3,3,0)
262  void (*)(std::vector<NumericVector<Number> *> &, sys_type &),
263  MatNullSpace *);
264 #endif
265 private:
266  friend ResidualContext libmesh_petsc_snes_residual_helper (SNES snes, Vec x, void * ctx);
267  friend PetscErrorCode libmesh_petsc_snes_residual (SNES snes, Vec x, Vec r, void * ctx);
268  friend PetscErrorCode libmesh_petsc_snes_fd_residual (SNES snes, Vec x, Vec r, void * ctx);
269  friend PetscErrorCode libmesh_petsc_snes_mffd_residual (SNES snes, Vec x, Vec r, void * ctx);
270 #if PETSC_RELEASE_LESS_THAN(3,5,0)
271  friend PetscErrorCode libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat * jac, Mat * pc, MatStructure * msflag, void * ctx);
272 #else
273  friend PetscErrorCode libmesh_petsc_snes_jacobian (SNES snes, Vec x, Mat jac, Mat pc, void * ctx);
274 #endif
275 };
276 
277 
278 
279 } // namespace libMesh
280 
281 
282 #endif // #ifdef LIBMESH_HAVE_PETSC
283 #endif // LIBMESH_PETSC_NONLINEAR_SOLVER_H
libMesh::PetscNonlinearSolver::ComputeLineSearchObject
Abstract base class to be used to implement a custom line-search algorithm.
Definition: petsc_nonlinear_solver.h:196
libMesh::PetscNonlinearSolver::clear
virtual void clear() override
Release all memory and clear data structures.
Definition: petsc_nonlinear_solver.C:567
libMesh::PetscNonlinearSolver::sys_type
NonlinearImplicitSystem sys_type
The type of system.
Definition: petsc_nonlinear_solver.h:105
libMesh::PetscNonlinearSolver
This class provides an interface to PETSc iterative solvers that is compatible with the libMesh Nonli...
Definition: petsc_nonlinear_solver.h:99
libMesh::libmesh_petsc_snes_residual
PetscErrorCode libmesh_petsc_snes_residual(SNES, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:151
libMesh::PetscNonlinearSolver::print_converged_reason
virtual void print_converged_reason() override
Prints a useful message about why the latest nonlinear solve con(di)verged.
Definition: petsc_nonlinear_solver.C:971
libMesh::ResidualContext
Definition: petsc_nonlinear_solver.C:49
libMesh::NonlinearSolver
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
Definition: nonlinear_solver.h:60
libMesh::__libmesh_petsc_snes_residual
PetscErrorCode __libmesh_petsc_snes_residual(SNES, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:196
libMesh::PetscNonlinearSolver::_zero_out_jacobian
bool _zero_out_jacobian
true to zero out jacobian before going into application level call-back, otherwise false
Definition: petsc_nonlinear_solver.h:246
libMesh::PetscNonlinearSolver::libmesh_petsc_snes_residual
friend PetscErrorCode libmesh_petsc_snes_residual(SNES snes, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:151
libMesh::PetscNonlinearSolver::libmesh_petsc_snes_fd_residual
friend PetscErrorCode libmesh_petsc_snes_fd_residual(SNES snes, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:206
libMesh::PetscNonlinearSolver::solve
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int) override
Call the Petsc solver.
Definition: petsc_nonlinear_solver.C:764
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::PetscNonlinearSolver::_snesmf_reuse_base
bool _snesmf_reuse_base
True, If we want the base vector to be used for differencing even if the function provided to SNESSet...
Definition: petsc_nonlinear_solver.h:258
libMesh::libmesh_petsc_snes_fd_residual
PetscErrorCode libmesh_petsc_snes_fd_residual(SNES, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:206
libMesh::PetscNonlinearSolver::_current_nonlinear_iteration_number
unsigned _current_nonlinear_iteration_number
Stores the current nonlinear iteration number.
Definition: petsc_nonlinear_solver.h:236
libMesh::PetscNonlinearSolver::PetscNonlinearSolver
PetscNonlinearSolver(sys_type &system)
Constructor.
Definition: petsc_nonlinear_solver.C:543
libMesh::__libmesh_petsc_snes_monitor
PetscErrorCode __libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
Definition: petsc_nonlinear_solver.C:140
libMesh::NonlinearImplicitSystem
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
Definition: nonlinear_implicit_system.h:54
libMesh::PetscNonlinearSolver::linesearch_object
std::unique_ptr< ComputeLineSearchObject > linesearch_object
A callable object that can be used to specify a custom line-search.
Definition: petsc_nonlinear_solver.h:207
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::libmesh_petsc_snes_mffd_residual
PetscErrorCode libmesh_petsc_snes_mffd_residual(SNES snes, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:247
PetscBool
PetscTruth PetscBool
Definition: petsc_macro.h:73
libMesh::PetscNonlinearSolver::_reason
SNESConvergedReason _reason
Store the reason for SNES convergence/divergence for use even after the _snes has been cleared.
Definition: petsc_nonlinear_solver.h:226
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::PetscNonlinearSolver::set_residual_zero_out
void set_residual_zero_out(bool state)
Set if the residual should be zeroed out in the callback.
Definition: petsc_nonlinear_solver.h:176
libMesh::PetscNonlinearSolver::snes
SNES snes()
Definition: petsc_nonlinear_solver.h:132
libMesh::__libmesh_petsc_snes_mffd_interface
PetscErrorCode __libmesh_petsc_snes_mffd_interface(void *ctx, Vec x, Vec r)
Definition: petsc_nonlinear_solver.C:291
libMesh::PetscNonlinearSolver::set_snesmf_reuse_base
void set_snesmf_reuse_base(bool state)
Set to true to let PETSc reuse the base vector.
Definition: petsc_nonlinear_solver.h:191
libMesh::libmesh_petsc_snes_jacobian
PetscErrorCode libmesh_petsc_snes_jacobian(SNES, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
libMesh::PetscNonlinearSolver::init
virtual void init(const char *name=nullptr) override
Initialize data structures if not done so already.
Definition: petsc_nonlinear_solver.C:588
libMesh::libmesh_petsc_snes_monitor
PetscErrorCode libmesh_petsc_snes_monitor(SNES, PetscInt its, PetscReal fnorm, void *)
Definition: petsc_nonlinear_solver.C:123
libMesh::PetscNonlinearSolver::libmesh_petsc_snes_mffd_residual
friend PetscErrorCode libmesh_petsc_snes_mffd_residual(SNES snes, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:247
libMesh::libmesh_petsc_linesearch_shellfunc
PetscErrorCode libmesh_petsc_linesearch_shellfunc(SNESLineSearch linesearch, void *ctx)
Definition: petsc_nonlinear_solver.C:400
libMesh::NonlinearImplicitSystem::ComputeVectorSubspace
Callable abstract base class to be used as a callback to provide the solver with a basis for the syst...
Definition: nonlinear_implicit_system.h:147
libMesh::__libmesh_petsc_snes_postcheck
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)
Definition: petsc_nonlinear_solver.C:518
libMesh::PetscNonlinearSolver::ComputeLineSearchObject::~ComputeLineSearchObject
virtual ~ComputeLineSearchObject()
Definition: petsc_nonlinear_solver.h:199
libMesh::PetscNonlinearSolver::get_converged_reason
SNESConvergedReason get_converged_reason()
Definition: petsc_nonlinear_solver.C:981
libMesh::PetscNonlinearSolver::set_jacobian_zero_out
void set_jacobian_zero_out(bool state)
Set if the jacobian should be zeroed out in the callback.
Definition: petsc_nonlinear_solver.h:181
libMesh::PetscNonlinearSolver::~PetscNonlinearSolver
~PetscNonlinearSolver()
Destructor.
Definition: petsc_nonlinear_solver.C:559
libMesh::__libmesh_petsc_snes_fd_residual
PetscErrorCode __libmesh_petsc_snes_fd_residual(SNES, Vec x, Vec r, void *ctx)
Definition: petsc_nonlinear_solver.C:236
libMesh::PetscNonlinearSolver::_n_linear_iterations
PetscInt _n_linear_iterations
Stores the total number of linear iterations from the last solve.
Definition: petsc_nonlinear_solver.h:231
libMesh::__libmesh_petsc_snes_jacobian
PetscErrorCode __libmesh_petsc_snes_jacobian(SNES, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
libMesh::PetscNonlinearSolver::get_current_nonlinear_iteration_number
virtual unsigned get_current_nonlinear_iteration_number() const override
Definition: petsc_nonlinear_solver.h:170
libMesh::ctx
void * ctx
Definition: petsc_dm_wrapper.C:71
libMesh::PetscNonlinearSolver::_zero_out_residual
bool _zero_out_residual
true to zero out residual before going into application level call-back, otherwise false
Definition: petsc_nonlinear_solver.h:241
libMesh::PetscNonlinearSolver::get_total_linear_iterations
virtual int get_total_linear_iterations() override
Get the total number of linear iterations done in the last solve.
Definition: petsc_nonlinear_solver.C:995
libMesh::PetscNonlinearSolver::_snes
SNES _snes
Nonlinear solver context.
Definition: petsc_nonlinear_solver.h:214
libMesh::libmesh_petsc_snes_mffd_interface
PetscErrorCode libmesh_petsc_snes_mffd_interface(void *ctx, Vec x, Vec r)
Definition: petsc_nonlinear_solver.C:280
libMesh::PetscNonlinearSolver::libmesh_petsc_snes_residual_helper
friend ResidualContext libmesh_petsc_snes_residual_helper(SNES snes, Vec x, void *ctx)
Definition: petsc_nonlinear_solver.C:65
libMesh::PetscNonlinearSolver::libmesh_petsc_snes_jacobian
friend PetscErrorCode libmesh_petsc_snes_jacobian(SNES snes, Vec x, Mat *jac, Mat *pc, MatStructure *msflag, void *ctx)
libMesh::PetscNonlinearSolver::ComputeLineSearchObject::linesearch
virtual void linesearch(SNESLineSearch linesearch)=0
libMesh::libmesh_petsc_snes_postcheck
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)
Definition: petsc_nonlinear_solver.C:441
libMesh::NonlinearSolver::system
const sys_type & system() const
Definition: nonlinear_solver.h:279
libMesh::PetscNonlinearSolver::_default_monitor
bool _default_monitor
true if we want the default monitor to be set, false for no monitor (i.e.
Definition: petsc_nonlinear_solver.h:251
libMesh::PetscNonlinearSolver::build_mat_null_space
void build_mat_null_space(NonlinearImplicitSystem::ComputeVectorSubspace *computeSubspaceObject, void(*)(std::vector< NumericVector< Number > * > &, sys_type &), MatNullSpace *)
Definition: petsc_nonlinear_solver.C:690
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
libMesh::PetscNonlinearSolver::use_default_monitor
void use_default_monitor(bool state)
Set to true to use the libMesh's default monitor, set to false to use your own.
Definition: petsc_nonlinear_solver.h:186