LCOV - code coverage report
Current view: top level - src/convergence - DefaultNonlinearConvergence.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 104 116 89.7 %
Date: 2026-05-29 20:35:17 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : // MOOSE includes
      11             : #include "DefaultNonlinearConvergence.h"
      12             : #include "FEProblemBase.h"
      13             : #include "PetscSupport.h"
      14             : #include "NonlinearSystemBase.h"
      15             : #include "ConvergenceIterationTypes.h"
      16             : 
      17             : #include "libmesh/equation_systems.h"
      18             : 
      19             : // PETSc includes
      20             : #include <petsc.h>
      21             : #include <petscmat.h>
      22             : #include <petscsnes.h>
      23             : 
      24             : registerMooseObject("MooseApp", DefaultNonlinearConvergence);
      25             : 
      26             : InputParameters
      27      130926 : DefaultNonlinearConvergence::validParams()
      28             : {
      29      130926 :   InputParameters params = DefaultConvergenceBase::validParams();
      30      130926 :   params += FEProblemSolve::feProblemDefaultConvergenceParams();
      31             : 
      32      392778 :   params.addPrivateParam<bool>("added_as_default", false);
      33             : 
      34      130926 :   params.addClassDescription("Default convergence criteria for FEProblem.");
      35             : 
      36      130926 :   return params;
      37           0 : }
      38             : 
      39       65278 : DefaultNonlinearConvergence::DefaultNonlinearConvergence(const InputParameters & parameters)
      40             :   : DefaultConvergenceBase(parameters),
      41      195834 :     _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
      42      130556 :     _nl_abs_div_tol(getSharedExecutionerParam<Real>("nl_abs_div_tol")),
      43      130556 :     _nl_rel_div_tol(getSharedExecutionerParam<Real>("nl_div_tol")),
      44       65278 :     _div_threshold(std::numeric_limits<Real>::max()),
      45      130556 :     _nl_forced_its(getSharedExecutionerParam<unsigned int>("nl_forced_its")),
      46      130556 :     _nl_max_pingpong(getSharedExecutionerParam<unsigned int>("n_max_nonlinear_pingpong")),
      47       65278 :     _nl_current_pingpong(0)
      48             : {
      49       65278 :   EquationSystems & es = _fe_problem.es();
      50             : 
      51       65278 :   es.parameters.set<unsigned int>("nonlinear solver maximum iterations") =
      52      195834 :       getSharedExecutionerParam<unsigned int>("nl_max_its");
      53       65278 :   es.parameters.set<unsigned int>("nonlinear solver maximum function evaluations") =
      54      195834 :       getSharedExecutionerParam<unsigned int>("nl_max_funcs");
      55       65278 :   es.parameters.set<Real>("nonlinear solver absolute residual tolerance") =
      56      195834 :       getSharedExecutionerParam<Real>("nl_abs_tol");
      57       65278 :   es.parameters.set<Real>("nonlinear solver relative residual tolerance") =
      58      195834 :       getSharedExecutionerParam<Real>("nl_rel_tol");
      59       65278 :   es.parameters.set<Real>("nonlinear solver divergence tolerance") =
      60      195834 :       getSharedExecutionerParam<Real>("nl_div_tol");
      61       65278 :   es.parameters.set<Real>("nonlinear solver absolute step tolerance") =
      62      195834 :       getSharedExecutionerParam<Real>("nl_abs_step_tol");
      63       65278 :   es.parameters.set<Real>("nonlinear solver relative step tolerance") =
      64      195834 :       getSharedExecutionerParam<Real>("nl_rel_step_tol");
      65       65278 : }
      66             : 
      67             : void
      68       55790 : DefaultNonlinearConvergence::checkIterationType(IterationType it_type) const
      69             : {
      70       55790 :   DefaultConvergenceBase::checkIterationType(it_type);
      71             : 
      72       55790 :   if (it_type != ConvergenceIterationTypes::NONLINEAR)
      73           3 :     mooseError("DefaultNonlinearConvergence can only be used with nonlinear solves.");
      74       55787 : }
      75             : 
      76             : bool
      77      334094 : DefaultNonlinearConvergence::checkRelativeConvergence(const unsigned int /*it*/,
      78             :                                                       const Real fnorm,
      79             :                                                       const Real ref_norm,
      80             :                                                       const Real rel_tol,
      81             :                                                       const Real /*abs_tol*/,
      82             :                                                       std::ostringstream & oss)
      83             : {
      84      334094 :   if (fnorm <= ref_norm * rel_tol)
      85             :   {
      86      138918 :     oss << "Converged due to relative/normalized residual norm " << fnorm / ref_norm
      87      138918 :         << " < relative tolerance (" << rel_tol << ")\n";
      88      138918 :     return true;
      89             :   }
      90             :   else
      91      195176 :     return false;
      92             : }
      93             : 
      94             : Convergence::MooseConvergenceStatus
      95      747059 : DefaultNonlinearConvergence::checkConvergence(unsigned int iter)
      96             : {
      97      747059 :   TIME_SECTION(_perfid_check_convergence);
      98             : 
      99      747059 :   NonlinearSystemBase & system = _fe_problem.currentNonlinearSystem();
     100      747059 :   MooseConvergenceStatus status = MooseConvergenceStatus::ITERATING;
     101             : 
     102             :   // Needed by ResidualReferenceConvergence
     103      747059 :   nonlinearConvergenceSetup();
     104             : 
     105      747059 :   SNES snes = system.getSNES();
     106             : 
     107             :   // ||u||
     108             :   PetscReal xnorm;
     109      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetSolutionNorm(snes, &xnorm));
     110             : 
     111             :   // ||r||
     112             :   PetscReal fnorm;
     113      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetFunctionNorm(snes, &fnorm));
     114             : 
     115             :   // ||du||
     116             :   PetscReal snorm;
     117      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetUpdateNorm(snes, &snorm));
     118             : 
     119             :   // Get current number of function evaluations done by SNES
     120             :   PetscInt nfuncs;
     121      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetNumberFunctionEvals(snes, &nfuncs));
     122             : 
     123             :   // Get tolerances from SNES
     124             :   PetscReal abs_tol, rel_tol, rel_step_tol;
     125             :   PetscInt max_its, max_funcs;
     126      747059 :   LibmeshPetscCallA(
     127             :       _fe_problem.comm().get(),
     128             :       SNESGetTolerances(snes, &abs_tol, &rel_tol, &rel_step_tol, &max_its, &max_funcs));
     129             : 
     130             : #if !PETSC_VERSION_LESS_THAN(3, 8, 4)
     131      747059 :   PetscBool force_iteration = PETSC_FALSE;
     132      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetForceIteration(snes, &force_iteration));
     133             : 
     134             :   // if PETSc says to force iteration, then force at least one iteration
     135      747059 :   if (force_iteration && !(_nl_forced_its))
     136         716 :     _nl_forced_its = 1;
     137             : 
     138             :   // if specified here to force iteration, but PETSc doesn't know, tell it
     139      747059 :   if (!force_iteration && (_nl_forced_its))
     140             :   {
     141          96 :     LibmeshPetscCallA(_fe_problem.comm().get(), SNESSetForceIteration(snes, PETSC_TRUE));
     142             :   }
     143             : #endif
     144             : 
     145             :   // See if SNESSetFunctionDomainError() has been called.
     146             :   // Note: SNESSetFunctionDomainError() and SNESGetFunctionDomainError() were added
     147             :   // in different releases of PETSc. The latter was removed in PETSc 3.25.0.
     148             : #if PETSC_VERSION_LESS_THAN(3, 25, 0)
     149             :   PetscBool domainerror;
     150             :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetFunctionDomainError(snes, &domainerror));
     151             :   if (domainerror)
     152             :     status = MooseConvergenceStatus::DIVERGED;
     153             : #else
     154             :   SNESConvergedReason reason;
     155      747059 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetConvergedReason(snes, &reason));
     156      747059 :   if (reason == SNES_DIVERGED_FUNCTION_DOMAIN)
     157           0 :     status = MooseConvergenceStatus::DIVERGED;
     158             : #endif
     159             : 
     160             :   Real fnorm_old;
     161             :   // This is the first residual before any iterations have been done, but after
     162             :   // solution-modifying objects (if any) have been imposed on the solution vector.
     163             :   // We save it, and use it to detect convergence if system.usePreSMOResidual() == false.
     164      747059 :   if (iter == 0)
     165             :   {
     166      285112 :     system.setInitialResidual(fnorm);
     167      285112 :     fnorm_old = fnorm;
     168      285112 :     _nl_current_pingpong = 0;
     169             :   }
     170             :   else
     171      461947 :     fnorm_old = system._last_nl_rnorm;
     172             : 
     173             :   // Check for nonlinear residual ping-pong.
     174             :   // Ping-pong will always start from a residual increase
     175      747059 :   if ((_nl_current_pingpong % 2 == 1 && !(fnorm > fnorm_old)) ||
     176      742412 :       (_nl_current_pingpong % 2 == 0 && fnorm > fnorm_old))
     177        9608 :     _nl_current_pingpong += 1;
     178             :   else
     179      737451 :     _nl_current_pingpong = 0;
     180             : 
     181      747059 :   std::ostringstream oss;
     182      747059 :   if (fnorm != fnorm)
     183             :   {
     184           0 :     oss << "Failed to converge, residual norm is NaN\n";
     185           0 :     status = MooseConvergenceStatus::DIVERGED;
     186             :   }
     187      747059 :   else if ((iter >= _nl_forced_its) && fnorm < abs_tol)
     188             :   {
     189      143781 :     oss << "Converged due to residual norm " << fnorm << " < " << abs_tol << '\n';
     190      143781 :     status = MooseConvergenceStatus::CONVERGED;
     191             :   }
     192      603278 :   else if (nfuncs >= max_funcs)
     193             :   {
     194           0 :     oss << "Exceeded maximum number of residual evaluations: " << nfuncs << " > " << max_funcs
     195           0 :         << '\n';
     196           0 :     status = MooseConvergenceStatus::DIVERGED;
     197             :   }
     198      603278 :   else if ((iter >= _nl_forced_its) && iter && fnorm > system._last_nl_rnorm &&
     199        5155 :            fnorm >= _div_threshold)
     200             :   {
     201           0 :     oss << "Nonlinear solve was blowing up!\n";
     202           0 :     status = MooseConvergenceStatus::DIVERGED;
     203             :   }
     204      747059 :   if ((iter >= _nl_forced_its) && iter && status == MooseConvergenceStatus::ITERATING)
     205             :   {
     206      349572 :     const auto ref_residual = system.referenceResidual();
     207      349572 :     if (checkRelativeConvergence(iter, fnorm, ref_residual, rel_tol, abs_tol, oss))
     208      140710 :       status = MooseConvergenceStatus::CONVERGED;
     209      208862 :     else if (snorm < rel_step_tol * xnorm)
     210             :     {
     211           0 :       oss << "Converged due to small update length: " << snorm << " < " << rel_step_tol << " * "
     212           0 :           << xnorm << '\n';
     213           0 :       status = MooseConvergenceStatus::CONVERGED;
     214             :     }
     215      208862 :     else if (_nl_rel_div_tol > 0 && fnorm > ref_residual * _nl_rel_div_tol)
     216             :     {
     217           2 :       oss << "Diverged due to relative residual " << ref_residual << " > divergence tolerance "
     218           2 :           << _nl_rel_div_tol << " * relative residual " << ref_residual << '\n';
     219           2 :       status = MooseConvergenceStatus::DIVERGED;
     220             :     }
     221      208860 :     else if (_nl_abs_div_tol > 0 && fnorm > _nl_abs_div_tol)
     222             :     {
     223           9 :       oss << "Diverged due to residual " << fnorm << " > absolute divergence tolerance "
     224           9 :           << _nl_abs_div_tol << '\n';
     225           9 :       status = MooseConvergenceStatus::DIVERGED;
     226             :     }
     227      208851 :     else if (_nl_current_pingpong > _nl_max_pingpong)
     228             :     {
     229           9 :       oss << "Diverged due to maximum nonlinear residual pingpong achieved" << '\n';
     230           9 :       status = MooseConvergenceStatus::DIVERGED;
     231             :     }
     232             :   }
     233             : 
     234      747059 :   system._last_nl_rnorm = fnorm;
     235      747059 :   system._current_nl_its = static_cast<unsigned int>(iter);
     236             : 
     237      747059 :   std::string msg;
     238      747059 :   msg = oss.str();
     239      747059 :   if (_app.multiAppLevel() > 0)
     240      383880 :     MooseUtils::indentMessage(_app.name(), msg);
     241      747059 :   if (msg.length() > 0)
     242             : #if !PETSC_VERSION_LESS_THAN(3, 17, 0)
     243      284511 :     LibmeshPetscCallA(_fe_problem.comm().get(), PetscInfo(snes, "%s", msg.c_str()));
     244             : #else
     245             :     LibmeshPetscCallA(_fe_problem.comm().get(), PetscInfo(snes, msg.c_str()));
     246             : #endif
     247             : 
     248      747059 :   verboseOutput(oss);
     249             : 
     250      747059 :   return status;
     251      747059 : }

Generated by: LCOV version 1.14