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));
75 delta_u, delta_u.l2_norm(),
77 res, res.l2_norm(), its);
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 case SNES_DIVERGED_FNORM_NAN:
263 case SNES_DIVERGED_INNER:
264 case SNES_DIVERGED_LINEAR_SOLVE:
265 case SNES_DIVERGED_LOCAL_MIN:
267 case SNES_DIVERGED_MAX_IT:
269 case SNES_DIVERGED_LINE_SEARCH:
276 case SNES_CONVERGED_ITERATING:
288 LOG_SCOPE(
"solve()",
"PetscDiffSolver");
290 #if !PETSC_VERSION_LESS_THAN(3,7,3) 291 #if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL) 303 *(cast_ptr<PetscVector<Number> *>(
_system.
rhs));
305 LibmeshPetscCall(SNESSetFunction (
_snes, r.
vec(),
308 LibmeshPetscCall(SNESSetJacobian (
_snes, jac.
mat(), jac.
mat(),
311 LibmeshPetscCall(SNESSetFromOptions(
_snes));
313 LibmeshPetscCall(SNESSolve (
_snes, LIBMESH_PETSC_NULLPTR, x.vec()));
315 #ifdef LIBMESH_ENABLE_CONSTRAINTS 320 SNESConvergedReason reason;
321 LibmeshPetscCall(SNESGetConvergedReason(
_snes, &reason));
323 PetscInt l_its, nl_its;
324 LibmeshPetscCall(SNESGetLinearSolveIterations(
_snes, &l_its));
327 LibmeshPetscCall(SNESGetIterationNumber(
_snes, &nl_its));
335 LibmeshPetscCall(SNESCreate(this->
comm().
get(),
_snes.
get()));
338 this, LIBMESH_PETSC_NULLPTR));
341 LibmeshPetscCall(SNESSetOptionsPrefix(
_snes, (
_system.
name()+
"_").c_str()));
346 #if !PETSC_VERSION_LESS_THAN(3,7,3) 347 #if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL) 357 LibmeshPetscCall(SNESSetFromOptions(
_snes));
360 LibmeshPetscCall(SNESGetKSP(
_snes, &my_ksp));
363 LibmeshPetscCall(KSPGetPC(my_ksp, &my_pc));
371 #endif // LIBMESH_HAVE_PETSC
bool exact_constraint_enforcement()
void petsc_auto_fieldsplit(PC my_pc, const System &sys)
This class provides a nice interface to PETSc's Vec object.
virtual void init() override
The initialization function.
void clear()
The clear function.
void setup_petsc_data()
Common helper function to setup PETSc data structures.
virtual ~PetscDiffSolver()
Destructor.
bool _exact_constraint_enforcement
Whether we should enforce exact constraints globally during a solve.
DiffSolver::SolveResult convert_solve_result(SNESConvergedReason r)
NumericVector< Number > * rhs
The system matrix.
const Parallel::Communicator & comm() const
virtual void reinit()
The reinitialization function.
virtual unsigned int solve() override
This method performs a solve.
The libMesh namespace provides an interface to certain functionality in the library.
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
const MeshBase & get_mesh() const
PetscDiffSolver(sys_type &system)
Constructor.
unsigned int _inner_iterations
The number of inner iterations used by the last solve.
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
virtual void init()
The initialization function.
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
The DiffSolver reached the maximum allowed number of nonlinear iterations before satisfying any conve...
This class defines a solver which uses a PETSc SNES context to handle a DifferentiableSystem.
The DiffSolver achieved the desired relative step size tolerance.
virtual void assembly(bool, bool, bool=false, bool=false)
Assembles a residual in rhs and/or a jacobian in matrix, as requested.
sys_type & _system
A reference to the system we are solving.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
The DiffSolver achieved the desired relative residual tolerance.
const sys_type & system() const
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false...
virtual void update()
Update the local values to reflect the solution on neighboring processors.
The solver converged but no particular reason is specified.
PetscDMWrapper _dm_wrapper
Wrapper object for interacting with PetscDM.
WrappedPetsc< SNES > _snes
Nonlinear solver context.
The DiffSolver achieved the desired absolute residual tolerance.
SolveResult
Enumeration return type for the solve() function.
unsigned int _outer_iterations
The number of outer iterations used by the last solve.
void init_and_attach_petscdm(System &system, SNES snes)
SparseMatrix< Number > * matrix
The system matrix.
PetscErrorCode __libmesh_petsc_diff_solver_jacobian(SNES, Vec x, Mat libmesh_dbg_var(j), Mat libmesh_dbg_var(pc), void *ctx)
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
The DiffSolver failed to find a descent direction by backtracking (See newton_solver.C)
bool is_children_on_boundary_side() const
bool on_command_line(std::string arg)
const std::string & name() const
The DiffSolver diverged but no particular reason is specified.
A default or invalid solve result.
PetscFunctionReturn(LIBMESH_PETSC_SUCCESS)
PetscErrorCode __libmesh_petsc_diff_solver_residual(SNES, Vec x, Vec r, void *ctx)
void clear()
Destroys and clears all build DM-related data.
const DofMap & get_dof_map() const
virtual void reinit() override
The reinitialization function.
void destroy()
Must be specialized to call the appropriate XXXDestroy() routine in order for a WrappedPetsc<T> objec...
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
std::unique_ptr< LinearSolutionMonitor > linear_solution_monitor
Pointer to functor which is called right after each linear solve.
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. ...
PetscErrorCode __libmesh_petsc_diff_solver_monitor(SNES snes, PetscInt its, PetscReal fnorm, void *ctx)
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...