19#include "libmesh/diff_system.h"
20#include "libmesh/dof_map.h"
21#include "libmesh/libmesh_logging.h"
22#include "libmesh/petsc_diff_solver.h"
23#include "libmesh/petsc_matrix_base.h"
24#include "libmesh/petsc_vector.h"
25#include "libmesh/petsc_auto_fieldsplit.h"
26#include "libmesh/boundary_info.h"
28#ifdef LIBMESH_HAVE_PETSC
56 <<
", |residual|_2 = " << fnorm << std::endl;
60 LibmeshPetscCall2(solver.
comm(), SNESGetSolutionUpdate(snes, &petsc_delta_u));
65 LibmeshPetscCall2(solver.
comm(), SNESGetSolution(snes, &petsc_u));
70 LibmeshPetscCall2(solver.
comm(), SNESGetFunction(snes, &petsc_res,
nullptr,
nullptr));
101 *cast_ptr<PetscVector<Number> *>(sys.
solution.get());
103 *cast_ptr<PetscVector<Number> *>(sys.
rhs);
110 X_input.swap(X_system);
111 R_input.swap(R_system);
125 X_input.swap(X_system);
126 R_input.swap(R_system);
136 Mat libmesh_dbg_var(j),
137 Mat libmesh_dbg_var(pc),
155 *cast_ptr<PetscVector<Number> *>(sys.
solution.get());
159 *cast_ptr<PetscMatrixBase<Number> *>(sys.
matrix);
166 X_input.swap(X_system);
180 X_input.swap(X_system);
197 LOG_SCOPE(
"init()",
"PetscDiffSolver");
212 LOG_SCOPE(
"clear()",
"PetscDiffSolver");
217#if !PETSC_VERSION_LESS_THAN(3,7,3)
218#if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
228 LOG_SCOPE(
"reinit()",
"PetscDiffSolver");
246 case SNES_CONVERGED_FNORM_ABS:
248 case SNES_CONVERGED_FNORM_RELATIVE:
250 case SNES_CONVERGED_SNORM_RELATIVE:
252 case SNES_CONVERGED_ITS:
256#if PETSC_VERSION_LESS_THAN(3,12,0)
257 case SNES_CONVERGED_TR_DELTA:
260 case SNES_DIVERGED_FUNCTION_DOMAIN:
261 case SNES_DIVERGED_FUNCTION_COUNT:
262#if PETSC_VERSION_LESS_THAN(3,25,0)
263 case SNES_DIVERGED_FNORM_NAN:
265 case SNES_DIVERGED_FUNCTION_NANORINF:
267#if !PETSC_VERSION_LESS_THAN(3,12,0)
268 case SNES_DIVERGED_TR_DELTA:
270 case SNES_DIVERGED_INNER:
271 case SNES_DIVERGED_LINEAR_SOLVE:
272 case SNES_DIVERGED_LOCAL_MIN:
274 case SNES_DIVERGED_MAX_IT:
276 case SNES_DIVERGED_LINE_SEARCH:
283 case SNES_CONVERGED_ITERATING:
295 LOG_SCOPE(
"solve()",
"PetscDiffSolver");
297#if !PETSC_VERSION_LESS_THAN(3,7,3)
298#if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
310 *(cast_ptr<PetscVector<Number> *>(
_system.
rhs));
312 LibmeshPetscCall(SNESSetFunction (
_snes, r.
vec(),
315 LibmeshPetscCall(SNESSetJacobian (
_snes, jac.
mat(), jac.
mat(),
318 LibmeshPetscCall(SNESSetFromOptions(
_snes));
320 LibmeshPetscCall(SNESSolve (
_snes, LIBMESH_PETSC_NULLPTR, x.
vec()));
322#ifdef LIBMESH_ENABLE_CONSTRAINTS
327 SNESConvergedReason reason;
328 LibmeshPetscCall(SNESGetConvergedReason(
_snes, &reason));
330 PetscInt l_its, nl_its;
331 LibmeshPetscCall(SNESGetLinearSolveIterations(
_snes, &l_its));
334 LibmeshPetscCall(SNESGetIterationNumber(
_snes, &nl_its));
342 LibmeshPetscCall(SNESCreate(this->
comm().get(),
_snes.
get()));
345 this, LIBMESH_PETSC_NULLPTR));
348 LibmeshPetscCall(SNESSetOptionsPrefix(
_snes, (
_system.
name()+
"_").c_str()));
353#if !PETSC_VERSION_LESS_THAN(3,7,3)
354#if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
364 LibmeshPetscCall(SNESSetFromOptions(
_snes));
367 LibmeshPetscCall(SNESGetKSP(
_snes, &my_ksp));
370 LibmeshPetscCall(KSPGetPC(my_ksp, &my_pc));
bool is_children_on_boundary_side() const
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
unsigned int _inner_iterations
The number of inner iterations used by the last solve.
std::unique_ptr< LinearSolutionMonitor > linear_solution_monitor
Pointer to functor which is called right after each linear solve.
bool exact_constraint_enforcement()
const sys_type & system() const
virtual void init()
The initialization function.
sys_type & _system
A reference to the system we are solving.
SolveResult
Enumeration return type for the solve() function.
@ DIVERGED_BACKTRACKING_FAILURE
The DiffSolver failed to find a descent direction by backtracking (See newton_solver....
@ DIVERGED_MAX_NONLINEAR_ITERATIONS
The DiffSolver reached the maximum allowed number of nonlinear iterations before satisfying any conve...
@ CONVERGED_RELATIVE_STEP
The DiffSolver achieved the desired relative step size tolerance.
@ CONVERGED_ABSOLUTE_RESIDUAL
The DiffSolver achieved the desired absolute residual tolerance.
@ DIVERGED_NO_REASON
The DiffSolver diverged but no particular reason is specified.
@ CONVERGED_NO_REASON
The solver converged but no particular reason is specified.
@ INVALID_SOLVE_RESULT
A default or invalid solve result.
@ CONVERGED_RELATIVE_RESIDUAL
The DiffSolver achieved the desired relative residual tolerance.
unsigned int _outer_iterations
The number of outer iterations used by the last solve.
virtual void reinit()
The reinitialization function.
bool _exact_constraint_enforcement
Whether we should enforce exact constraints globally during a solve.
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false.
void enforce_constraints_exactly(const System &system, NumericVector< Number > *v=nullptr, bool homogeneous=false) const
Constrains the numeric vector v, which represents a solution defined on the mesh.
NumericVector< Number > * rhs
The system matrix.
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...
SparseMatrix< Number > * matrix
The system matrix.
virtual void assembly(bool, bool, bool=false, bool=false)
Assembles a residual in rhs and/or a jacobian in matrix, as requested.
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
const Parallel::Communicator & comm() const
void init_and_attach_petscdm(System &system, SNES snes)
void clear()
Destroys and clears all build DM-related data.
This class defines a solver which uses a PETSc SNES context to handle a DifferentiableSystem.
virtual unsigned int solve() override
This method performs a solve.
void clear()
The clear function.
virtual void init() override
The initialization function.
void setup_petsc_data()
Common helper function to setup PETSc data structures.
PetscDMWrapper _dm_wrapper
Wrapper object for interacting with PetscDM.
PetscDiffSolver(sys_type &system)
Constructor.
virtual void reinit() override
The reinitialization function.
virtual ~PetscDiffSolver()
Destructor.
WrappedPetsc< SNES > _snes
Nonlinear solver context.
This class provides a nice interface to the PETSc C-based data structures for parallel,...
virtual void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
This class provides a nice interface to PETSc's Vec object.
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual Real l2_norm() const override
const std::string & name() const
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
virtual void update()
Update the local values to reflect the solution on neighboring processors.
const DofMap & get_dof_map() const
const MeshBase & get_mesh() const
The libMesh namespace provides an interface to certain functionality in the library.
PetscFunctionReturn(LIBMESH_PETSC_SUCCESS)
PetscErrorCode __libmesh_petsc_diff_solver_monitor(SNES snes, PetscInt its, PetscReal fnorm, void *ctx)
DiffSolver::SolveResult convert_solve_result(SNESConvergedReason r)
PetscErrorCode __libmesh_petsc_diff_solver_jacobian(SNES, Vec x, Mat libmesh_dbg_var(j), Mat libmesh_dbg_var(pc), void *ctx)
void petsc_auto_fieldsplit(PC my_pc, const System &sys)
bool on_command_line(std::string arg)
PetscErrorCode __libmesh_petsc_diff_solver_residual(SNES, Vec x, Vec r, void *ctx)
void destroy()
Must be specialized to call the appropriate XXXDestroy() routine in order for a WrappedPetsc<T> objec...