This section describes the SUNNonlinSol interface to the PETSc SNES nonlinear
solver(s).To enable the SUNonlinSol_PetscSNES module, SUNDIALS must be
configured to use PETSc. Instructions on how to do thus are given in Chapter
Building with PETSc. To access the
SUNNonlinSol_PetscSNES module, include the header file
sunnonlinsol/sunnonlinsol_petscsnes.h
. The library to link to is
libsundials_sunnonlinsolpetsc.lib
where .lib
is typically .so
for
shared libaries and .a
for static libraries. Users of the
SUNNonlinearSolver_PetscSNES
should also see the section
The NVECTOR_PETSC Module which discusses the NVECTOR interface to the PETSc Vec
API.
The SUNNonlinearSolver_PetscSNES
implementation allows users to utilize a
PETSc SNES nonlinear solver to solve the nonlinear systems that arise in the
SUNDIALS integrators. Since SNES uses the KSP linear solver interface underneath
it, the SUNNonlinearSolver_PetscSNES
implementation does not interface with
SUNDIALS linear solvers. Instead, users should set nonlinear solver options,
linear solver options, and preconditioner options through the PETSc SNES, KSP,
and PC APIs.
Important usage notes for the ``SUNNonlinearSolver_PetscSNES`` implementation are provided below:
SUNNonlinearSolver_PetscSNES
implementation handles calling
SNESSetFunction
at construction. The actual residual function \(F(y)\)
is set by the SUNDIALS integrator when the SUNNonlinearSolver_PetscSNES
object is attached to it. Therefore, a user should not call SNESSetFunction
on a SNES
object that is being used with SUNNonlinearSolver_PetscSNES
.
For these reasons, it is recommended, although not always necessary, that the
user calls SUNNonlinSol_PetscSNES
with the new SNES
object immediately
after calling SNESCreate
.SUNNonlinSolGetNumIters
reports
the cumulative number of iterations across the lifetime of the
SUNNonlinearSolver
object.SUNNonlinSolGetNumConvFails
will reflect
the number of recoverable convergence failures as determined by SUNDIALS, and
may differ from the count returned by SNESGetNonlinearStepFailures
.SUNNonlinearSolver_PetscSNES
module is not currently compatible with
the CVODES or IDAS staggered or simultaneous sensitivity strategies.The SUNNonlinearSolver_PetscSNES
module provides the following constructor
for creating a SUNNonlinearSolver
object.
SUNNonlinSol_PetscSNES
(N_Vector y, SNES snes)¶The function SUNNonlinSol_PetscSNES
creates a SUNNonlinearSolver
object that wraps a PETSc SNES
object for use with SUNDIALS. This
will call SNESSetFunction
on the provided SNES
object.
SNES
objectN_Vector
object of type NVECTOR_PETSC that is used as a template
for the residual vectorNULL
.This function calls ``SNESSetFunction`` and will overwrite whatever function was previously set. Users should not call ``SNESSetFunction`` on the ``SNES`` object provided to the constructor.
The SUNNonlinSol_PetscSNES
module implements all of the functions defined in
sections SUNNonlinearSolver core functions through SUNNonlinearSolver get functions except for
SUNNonlinSolSetup
, SUNNonlinSolSetLSetupFn
, SUNNonlinSolSetLSolveFn
,
SUNNonlinSolSetConvTestFn
, and SUNNonlinSolSetMaxIters
.
The SUNNonlinSol_PetscSNES
functions have the same names as those defined by
the generic SUNNonlinearSolver
API with _PetscSNES
appended to the
function name. Unless using the SUNNonlinSol_PetscSNES
module as a
standalone nonlinear solver the generic functions defined in sections
SUNNonlinearSolver core functions through SUNNonlinearSolver get functions should be called in
favor of the SUNNonlinSol_PetscSNES
specific implementations.
The SUNNonlinSol_PetscSNES
module also defines the following additional
user-callable functions.
SUNNonlinSolGetSNES_PetscSNES
(SUNNonlinearSolver NLS, SNES* snes)¶The function SUNNonlinSolGetSNES_PetscSNES
gets the SNES
object that
was wrapped.
SUNNonlinearSolver
objectSNES
object that will be set upon returnReturn value: The return value (of type int
) should be zero
for a successful call, and a negative value for a failure.
SUNNonlinSolGetPetscError_PetscSNES
(SUNNonlinearSolver NLS, PestcErrorCode* error)¶The function SUNNonlinSolGetPetscError_PetscSNES
gets the last error code
returned by the last internal call to a PETSc API function.
SUNNonlinearSolver
objectReturn value: The return value (of type int
) should be zero
for a successful call, and a negative value for a failure.
SUNNonlinSolGetSysFn_PetscSNES
(SUNNonlinearSolver NLS, SUNNonlinSolSysFn* SysFn)¶The function SUNNonlinSolGetSysFn_PetscSNES
returns the residual
function that defines the nonlinear system.
SUNNonlinearSolver
objectReturn value: The return value (of type int
) should be zero
for a successful call, and a negative value for a failure.
The content field of the SUNNonlinSol_Newton module is the following structure.
struct _SUNNonlinearSolverContent_PetscSNES {
int sysfn_last_err;
PetscErrorCode petsc_last_err;
long int nconvfails;
long int nni;
void *imem;
SNES snes;
Vec r;
N_Vector y, f;
SUNNonlinSolSysFn Sys;
};
These entries of the content field contain the following information:
sysfn_last_err
– last error returned by the system defining function,petsc_last_err
– last error returned by PETScnconvfails
– number of nonlinear converge failures (recoverable or not),nni
– number of nonlinear iterations,imem
– SUNDIALS integrator memory,snes
– PETSc SNES
object,r
– the nonlinear residual,y
– wrapper for PETSc vectors used in the system function,f
– wrapper for PETSc vectors used in the system function,Sys
– nonlinear system definining function.