Go to the documentation of this file.
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.h"
24 #include "libmesh/petsc_vector.h"
25 #include "libmesh/petsc_auto_fieldsplit.h"
27 #ifdef LIBMESH_HAVE_PETSC
49 *(static_cast<PetscDiffSolver *> (
ctx));
53 <<
", |residual|_2 = " << fnorm << std::endl;
58 ierr = SNESGetSolutionUpdate(snes, &petsc_delta_u);
59 CHKERRABORT(solver.
comm().get(),
ierr);
64 ierr = SNESGetSolution(snes, &petsc_u);
65 CHKERRABORT(solver.
comm().get(),
ierr);
70 ierr = SNESGetFunction(snes, &petsc_res,
nullptr,
nullptr);
71 CHKERRABORT(solver.
comm().get(),
ierr);
76 delta_u, delta_u.l2_norm(),
78 res, res.l2_norm(), its);
93 *(static_cast<PetscDiffSolver*> (
ctx));
100 *cast_ptr<PetscVector<Number> *>(sys.
solution.get());
102 *cast_ptr<PetscVector<Number> *>(sys.
rhs);
109 X_input.
swap(X_system);
110 R_input.swap(R_system);
123 X_input.swap(X_system);
124 R_input.swap(R_system);
134 #
if PETSC_RELEASE_LESS_THAN(3,5,0)
135 Mat * libmesh_dbg_var(j),
137 MatStructure * msflag,
139 Mat libmesh_dbg_var(j),
150 *(static_cast<PetscDiffSolver*> (
ctx));
157 *cast_ptr<PetscVector<Number> *>(sys.
solution.get());
160 #if PETSC_RELEASE_LESS_THAN(3,5,0)
166 *cast_ptr<PetscMatrix<Number> *>(sys.
matrix);
172 X_input.
swap(X_system);
173 J_input.swap(J_system);
186 X_input.swap(X_system);
187 J_input.swap(J_system);
189 #if PETSC_RELEASE_LESS_THAN(3,5,0)
190 *msflag = SAME_NONZERO_PATTERN;
207 LOG_SCOPE(
"init()",
"PetscDiffSolver");
225 LOG_SCOPE(
"clear()",
"PetscDiffSolver");
228 LIBMESH_CHKERR(
ierr);
230 #if !PETSC_VERSION_LESS_THAN(3,7,3)
231 #if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
241 LOG_SCOPE(
"reinit()",
"PetscDiffSolver");
259 case SNES_CONVERGED_FNORM_ABS:
261 case SNES_CONVERGED_FNORM_RELATIVE:
263 #if PETSC_VERSION_LESS_THAN(3,2,1)
264 case SNES_CONVERGED_PNORM_RELATIVE:
266 case SNES_CONVERGED_SNORM_RELATIVE:
269 case SNES_CONVERGED_ITS:
273 #if PETSC_RELEASE_LESS_THAN(3,12,0)
274 case SNES_CONVERGED_TR_DELTA:
277 case SNES_DIVERGED_FUNCTION_DOMAIN:
278 case SNES_DIVERGED_FUNCTION_COUNT:
279 case SNES_DIVERGED_FNORM_NAN:
280 #if !PETSC_VERSION_LESS_THAN(3,3,0)
281 case SNES_DIVERGED_INNER:
283 case SNES_DIVERGED_LINEAR_SOLVE:
284 case SNES_DIVERGED_LOCAL_MIN:
286 case SNES_DIVERGED_MAX_IT:
288 #if PETSC_VERSION_LESS_THAN(3,2,0)
289 case SNES_DIVERGED_LS_FAILURE:
291 case SNES_DIVERGED_LINE_SEARCH:
299 case SNES_CONVERGED_ITERATING:
311 LOG_SCOPE(
"solve()",
"PetscDiffSolver");
318 *(cast_ptr<PetscVector<Number> *>(
_system.
rhs));
324 LIBMESH_CHKERR(
ierr);
328 LIBMESH_CHKERR(
ierr);
331 LIBMESH_CHKERR(
ierr);
334 LIBMESH_CHKERR(
ierr);
336 #ifdef LIBMESH_ENABLE_CONSTRAINTS
340 SNESConvergedReason reason;
341 SNESGetConvergedReason(
_snes, &reason);
343 PetscInt l_its, nl_its;
344 ierr = SNESGetLinearSolveIterations(
_snes,&l_its);
345 LIBMESH_CHKERR(
ierr);
348 ierr = SNESGetIterationNumber(
_snes,&nl_its);
349 LIBMESH_CHKERR(
ierr);
360 LIBMESH_CHKERR(
ierr);
364 LIBMESH_CHKERR(
ierr);
369 LIBMESH_CHKERR(
ierr);
375 #if !PETSC_VERSION_LESS_THAN(3,7,3)
376 #if defined(LIBMESH_ENABLE_AMR) && defined(LIBMESH_HAVE_METAPHYSICL)
387 LIBMESH_CHKERR(
ierr);
391 LIBMESH_CHKERR(
ierr);
394 ierr = KSPGetPC(my_ksp, &my_pc);
395 LIBMESH_CHKERR(
ierr);
403 #endif // LIBMESH_HAVE_PETSC
virtual void reinit()
The reinitialization function.
void clear()
The clear function.
PetscErrorCode __libmesh_petsc_diff_solver_residual(SNES, Vec x, Vec r, void *ctx)
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...
NumericVector< Number > * rhs
The system matrix.
The DiffSolver achieved the desired relative step size tolerance.
virtual ~PetscDiffSolver()
Destructor.
PetscDMWrapper _dm_wrapper
Wrapper object for interacting with PetscDM.
The libMesh namespace provides an interface to certain functionality in the library.
const Parallel::Communicator & comm() const
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
The DiffSolver failed to find a descent direction by backtracking (See newton_solver....
The DiffSolver achieved the desired relative residual tolerance.
const sys_type & system() const
unsigned int _inner_iterations
The number of inner iterations used by the last solve.
virtual void init()
The initialization function.
PetscErrorCode __libmesh_petsc_diff_solver_jacobian(SNES, Vec x, #if PETSC_RELEASE_LESS_THAN(3, 5, 0) Mat *libmesh_dbg_var(j), Mat *pc, MatStructure *msflag, #else Mat libmesh_dbg_var(j), Mat pc, #endif void *ctx)
DiffSolver::SolveResult convert_solve_result(SNESConvergedReason r)
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
void setup_petsc_data()
Common helper function to setup PETSc data structures.
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false.
virtual void assembly(bool, bool, bool=false, bool=false)
Assembles a residual in rhs and/or a jacobian in matrix, as requested.
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.
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
SparseMatrix< Number > * matrix
The system matrix.
void swap(PetscMatrix< T > &)
Swaps the internal data pointers of two PetscMatrices, no actual values are swapped.
This class provides a nice interface to PETSc's Vec object.
The solver converged but no particular reason is specified.
The DiffSolver achieved the desired absolute residual tolerance.
The DiffSolver diverged but no particular reason is specified.
void init_and_attach_petscdm(System &system, SNES &snes)
The DiffSolver reached the maximum allowed number of nonlinear iterations before satisfying any conve...
void clear()
Destroys and clears all build DM-related data.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
std::unique_ptr< LinearSolutionMonitor > linear_solution_monitor
Pointer to functor which is called right after each linear solve.
const Elem & get(const ElemType type_in)
SolveResult
Enumeration return type for the solve() function.
unsigned int _outer_iterations
The number of outer iterations used by the last solve.
This class provides a nice interface to the PETSc C-based data structures for parallel,...
const std::string & name() const
virtual void reinit() override
The reinitialization function.
virtual unsigned int solve() override
This method performs a solve.
PetscDiffSolver(sys_type &system)
Constructor.
bool on_command_line(std::string arg)
PetscErrorCode __libmesh_petsc_diff_solver_monitor(SNES snes, PetscInt its, PetscReal fnorm, void *ctx)
const DofMap & get_dof_map() const
This class defines a solver which uses a PETSc SNES context to handle a DifferentiableSystem.
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
void petsc_auto_fieldsplit(PC my_pc, const System &sys)
virtual void init() override
The initialization function.
virtual void update()
Update the local values to reflect the solution on neighboring processors.
sys_type & _system
A reference to the system we are solving.
SNES _snes
Nonlinear solver context.
A default or invalid solve result.