17#include "libmesh/system.h"
41 _fe_problem(*getCheckedPointerParam<
FEProblemBase *>(
"_fe_problem_base")),
42 _nl_abs_div_tol(getSharedExecutionerParam<Real>(
"nl_abs_div_tol")),
43 _nl_rel_div_tol(getSharedExecutionerParam<Real>(
"nl_div_tol")),
44 _div_threshold(
std::numeric_limits<Real>::max()),
45 _nl_forced_its(getSharedExecutionerParam<unsigned
int>(
"nl_forced_its")),
46 _nl_max_pingpong(getSharedExecutionerParam<unsigned
int>(
"n_max_nonlinear_pingpong")),
47 _nl_current_pingpong(0),
48 _nl_rel_step_tol(getSharedExecutionerParam<Real>(
"nl_rel_step_tol")),
49 _nl_max_funcs(getSharedExecutionerParam<unsigned
int>(
"nl_max_funcs")),
50 _nl_max_its(getSharedExecutionerParam<unsigned
int>(
"nl_max_its")),
51 _nl_abs_tol(getSharedExecutionerParam<Real>(
"nl_abs_tol")),
52 _nl_rel_tol(getSharedExecutionerParam<Real>(
"nl_rel_tol"))
75 mooseError(
"DefaultNonlinearConvergence can only be used with nonlinear solves.");
92 params.
set<
unsigned int>(
"nonlinear solver maximum iterations") =
_nl_max_its;
93 params.set<
unsigned int>(
"nonlinear solver maximum function evaluations") =
_nl_max_funcs;
94 params.set<Real>(
"nonlinear solver absolute residual tolerance") =
_nl_abs_tol;
95 params.set<Real>(
"nonlinear solver relative residual tolerance") =
_nl_rel_tol;
96 params.set<Real>(
"nonlinear solver divergence tolerance") =
97 getSharedExecutionerParam<Real>(
"nl_div_tol");
98 params.set<Real>(
"nonlinear solver absolute step tolerance") =
99 getSharedExecutionerParam<Real>(
"nl_abs_step_tol");
100 params.set<Real>(
"nonlinear solver relative step tolerance") =
_nl_rel_step_tol;
106 "_default_nonlinear_convergence_tolerance_writer") =
this;
121 std::ostringstream & oss)
125 oss <<
"Converged due to absolute residual " << fnorm <<
" < absolute tolerance (" << abs_tol
129 else if (n_iter && fnorm <= ref_norm * rel_tol)
131 oss <<
"Converged due to relative/normalized residual norm " << fnorm / ref_norm
132 <<
" < relative tolerance (" << rel_tol <<
")\n";
153 "_default_nonlinear_convergence_tolerance_writer") !=
this)
157 PetscReal abs_tol, rel_tol, rel_step_tol;
158 PetscInt max_its, max_funcs;
161 SNESGetTolerances(snes, &abs_tol, &rel_tol, &rel_step_tol, &max_its, &max_funcs));
163 std::ostringstream oss;
165 oss <<
" -snes_atol = " << abs_tol <<
" (nl_abs_tol = " <<
_nl_abs_tol <<
")\n";
167 oss <<
" -snes_rtol = " << rel_tol <<
" (nl_rel_tol = " <<
_nl_rel_tol <<
")\n";
169 oss <<
" -snes_stol = " << rel_step_tol <<
" (nl_rel_step_tol = " <<
_nl_rel_step_tol <<
")\n";
171 oss <<
" -snes_max_it = " << max_its <<
" (nl_max_its = " <<
_nl_max_its <<
")\n";
173 oss <<
" -snes_max_funcs = " << max_funcs <<
" (nl_max_funcs = " <<
_nl_max_funcs <<
")\n";
175 if (!oss.str().empty())
176 mooseWarning(
"The following PETSc SNES option(s) were set to a value different from what "
177 "this Convergence object enforces. Because this object performs its own "
178 "convergence check using its own cached tolerances, these PETSc-level settings "
209 LibmeshPetscCallA(
_fe_problem.
comm().
get(), SNESGetNumberFunctionEvals(snes, &nfuncs));
211#if !PETSC_VERSION_LESS_THAN(3, 8, 4)
212 PetscBool force_iteration = PETSC_FALSE;
213 LibmeshPetscCallA(
_fe_problem.
comm().
get(), SNESGetForceIteration(snes, &force_iteration));
229#if PETSC_VERSION_LESS_THAN(3, 25, 0)
230 PetscBool domainerror;
231 LibmeshPetscCallA(
_fe_problem.
comm().
get(), SNESGetFunctionDomainError(snes, &domainerror));
235 SNESConvergedReason reason;
237 if (reason == SNES_DIVERGED_FUNCTION_DOMAIN)
266 std::ostringstream oss;
268 oss <<
"Number of forced iterations not yet reached: " << n_iter <<
" < " <<
_nl_forced_its
270 else if (fnorm != fnorm)
272 oss <<
"Failed to converge, residual norm is NaN\n";
279 oss <<
"Exceeded maximum number of residual evaluations: " << nfuncs <<
" > " <<
_nl_max_funcs
286 oss <<
"Nonlinear solve was blowing up!\n";
291 oss <<
"Converged due to small update length: " << snorm <<
" < " <<
_nl_rel_step_tol <<
" * "
297 oss <<
"Diverged due to relative residual " << ref_residual <<
" > divergence tolerance "
303 oss <<
"Diverged due to residual " << fnorm <<
" > absolute divergence tolerance "
309 oss <<
"Diverged due to maximum nonlinear residual pingpong achieved" <<
'\n';
320 if (msg.length() > 0)
321#
if !PETSC_VERSION_LESS_THAN(3, 17, 0)
registerMooseObject("MooseApp", DefaultNonlinearConvergence)
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
void ErrorVector unsigned int
virtual void preLoop()
Method that gets called before each iteration loop.
PerfID _perfid_check_convergence
Performance ID for checkConvergence.
void verboseOutput(std::ostringstream &oss)
Outputs the stream to the console if verbose output is enabled.
MooseConvergenceStatus
Status returned by calls to checkConvergence.
virtual void checkIterationType(IterationType) const
Perform checks related to the iteration type.
Base class for default convergence criteria.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
static InputParameters validParams()
Default nonlinear convergence criteria for FEProblem.
const Real _nl_rel_div_tol
Nonlinear relative divergence tolerance.
virtual void preLoop() override
Method that gets called before each iteration loop.
unsigned int _nl_current_pingpong
Current number of nonlinear ping-pong iterations for the current solve.
const Real _nl_abs_div_tol
Nonlinear absolute divergence tolerance.
static InputParameters validParams()
void setNonlinearSystemParameters()
Sets the nonlinear system parameters, such as tolerances.
FEProblemBase & _fe_problem
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
unsigned int _nl_max_its
Nonlinear maximum nonlinear iterations (modifiable by setMaximumIterations)
bool _nl_checked_petsc_tolerance_overrides
Whether checkPetscToleranceOverrides() has already run for this simulation.
unsigned int _nl_forced_its
Number of iterations to force.
const Real _nl_rel_step_tol
Nonlinear relative step tolerance.
void setMaximumIterations(const unsigned int max_iter)
Sets the maximum nonlinear iterations.
Real _nl_rel_tol
Nonlinear relative tolerance (modifiable by setRelativeTolerance)
virtual NonlinearSystemBase & nonlinearSystem()
Nonlinear system whose convergence state should be checked.
virtual bool checkResidualConvergence(const unsigned int n_iter, const Real fnorm, const Real ref_norm, const Real rel_tol, const Real abs_tol, std::ostringstream &oss)
Check the absolute and relative convergence of the nonlinear solution.
DefaultNonlinearConvergence(const InputParameters ¶meters)
virtual void nonlinearConvergenceSetup()
Performs setup necessary for each call to checkConvergence.
const unsigned int _nl_max_funcs
Maximum number of residual (function) evaluations.
virtual void checkIterationType(IterationType it_type) const override
Perform checks related to the iteration type.
const Real _div_threshold
Divergence threshold value.
void setRelativeTolerance(const Real rel_tol)
Sets the relative nonlinear tolerance.
void setAbsoluteTolerance(const Real abs_tol)
Sets the absolute nonlinear tolerance.
void checkPetscToleranceOverrides()
Warns, once per simulation, if any PETSc SNES tolerance/iteration options (e.g.
const unsigned int _nl_max_pingpong
Maximum number of nonlinear ping-pong iterations for a solve.
virtual MooseConvergenceStatus checkConvergence(unsigned int n_iter) override
Returns convergence status.
Real _nl_abs_tol
Nonlinear absolute tolerance (modifiable by setAbsoluteTolerance)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
NonlinearSystemBase & currentNonlinearSystem()
static InputParameters feProblemDefaultConvergenceParams()
unsigned int multiAppLevel() const
The MultiApp Level.
const std::string & name() const
Get the name of the class.
Class for containing MooseEnum item information.
MooseApp & _app
The MOOSE application this is associated with.
Nonlinear system to be solved.
Real referenceResidual() const
The reference residual used in relative convergence check.
void setInitialResidual(Real r)
Record the initial residual (for later relative convergence check)
virtual libMesh::System & system() override
Get the reference to the libMesh system.
unsigned int _current_nl_its
const Parallel::Communicator & comm() const
T & set(const std::string &)
void indentMessage(const std::string &prefix, std::string &message, const char *color, bool indent_first_line, const std::string &post_prefix)