LCOV - code coverage report
Current view: top level - src/convergence - DefaultNonlinearConvergence.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31706 (f8ed4a) with base bb0a08 Lines: 104 116 89.7 %
Date: 2025-11-03 17:23:24 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      162172 : DefaultNonlinearConvergence::validParams()
      28             : {
      29      162172 :   InputParameters params = DefaultConvergenceBase::validParams();
      30      162172 :   params += FEProblemSolve::feProblemDefaultConvergenceParams();
      31             : 
      32      486516 :   params.addPrivateParam<bool>("added_as_default", false);
      33             : 
      34      162172 :   params.addClassDescription("Default convergence criteria for FEProblem.");
      35             : 
      36      162172 :   return params;
      37           0 : }
      38             : 
      39       68802 : DefaultNonlinearConvergence::DefaultNonlinearConvergence(const InputParameters & parameters)
      40             :   : DefaultConvergenceBase(parameters),
      41      206406 :     _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
      42      137604 :     _nl_abs_div_tol(getSharedExecutionerParam<Real>("nl_abs_div_tol")),
      43      137604 :     _nl_rel_div_tol(getSharedExecutionerParam<Real>("nl_div_tol")),
      44       68802 :     _div_threshold(std::numeric_limits<Real>::max()),
      45      137604 :     _nl_forced_its(getSharedExecutionerParam<unsigned int>("nl_forced_its")),
      46      137604 :     _nl_max_pingpong(getSharedExecutionerParam<unsigned int>("n_max_nonlinear_pingpong")),
      47       68802 :     _nl_current_pingpong(0)
      48             : {
      49       68802 :   EquationSystems & es = _fe_problem.es();
      50             : 
      51       68802 :   es.parameters.set<unsigned int>("nonlinear solver maximum iterations") =
      52      206406 :       getSharedExecutionerParam<unsigned int>("nl_max_its");
      53       68802 :   es.parameters.set<unsigned int>("nonlinear solver maximum function evaluations") =
      54      206406 :       getSharedExecutionerParam<unsigned int>("nl_max_funcs");
      55       68802 :   es.parameters.set<Real>("nonlinear solver absolute residual tolerance") =
      56      206406 :       getSharedExecutionerParam<Real>("nl_abs_tol");
      57       68802 :   es.parameters.set<Real>("nonlinear solver relative residual tolerance") =
      58      206406 :       getSharedExecutionerParam<Real>("nl_rel_tol");
      59       68802 :   es.parameters.set<Real>("nonlinear solver divergence tolerance") =
      60      206406 :       getSharedExecutionerParam<Real>("nl_div_tol");
      61       68802 :   es.parameters.set<Real>("nonlinear solver absolute step tolerance") =
      62      206406 :       getSharedExecutionerParam<Real>("nl_abs_step_tol");
      63       68802 :   es.parameters.set<Real>("nonlinear solver relative step tolerance") =
      64      206406 :       getSharedExecutionerParam<Real>("nl_rel_step_tol");
      65       68802 : }
      66             : 
      67             : void
      68       58979 : DefaultNonlinearConvergence::checkIterationType(IterationType it_type) const
      69             : {
      70       58979 :   DefaultConvergenceBase::checkIterationType(it_type);
      71             : 
      72       58979 :   if (it_type != ConvergenceIterationTypes::NONLINEAR)
      73           4 :     mooseError("DefaultNonlinearConvergence can only be used with nonlinear solves.");
      74       58975 : }
      75             : 
      76             : bool
      77      374617 : 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      374617 :   if (fnorm <= ref_norm * rel_tol)
      85             :   {
      86      156331 :     oss << "Converged due to relative/normalized residual norm " << fnorm / ref_norm
      87      156331 :         << " < relative tolerance (" << rel_tol << ")\n";
      88      156331 :     return true;
      89             :   }
      90             :   else
      91      218286 :     return false;
      92             : }
      93             : 
      94             : Convergence::MooseConvergenceStatus
      95      827899 : DefaultNonlinearConvergence::checkConvergence(unsigned int iter)
      96             : {
      97      827899 :   TIME_SECTION(_perfid_check_convergence);
      98             : 
      99      827899 :   NonlinearSystemBase & system = _fe_problem.currentNonlinearSystem();
     100      827899 :   MooseConvergenceStatus status = MooseConvergenceStatus::ITERATING;
     101             : 
     102             :   // Needed by ResidualReferenceConvergence
     103      827899 :   nonlinearConvergenceSetup();
     104             : 
     105      827899 :   SNES snes = system.getSNES();
     106             : 
     107             :   // ||u||
     108             :   PetscReal xnorm;
     109      827899 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetSolutionNorm(snes, &xnorm));
     110             : 
     111             :   // ||r||
     112             :   PetscReal fnorm;
     113      827899 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetFunctionNorm(snes, &fnorm));
     114             : 
     115             :   // ||du||
     116             :   PetscReal snorm;
     117      827899 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetUpdateNorm(snes, &snorm));
     118             : 
     119             :   // Get current number of function evaluations done by SNES
     120             :   PetscInt nfuncs;
     121      827899 :   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      827899 :   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      827899 :   PetscBool force_iteration = PETSC_FALSE;
     132      827899 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetForceIteration(snes, &force_iteration));
     133             : 
     134             :   // if PETSc says to force iteration, then force at least one iteration
     135      827899 :   if (force_iteration && !(_nl_forced_its))
     136         706 :     _nl_forced_its = 1;
     137             : 
     138             :   // if specified here to force iteration, but PETSc doesn't know, tell it
     139      827899 :   if (!force_iteration && (_nl_forced_its))
     140             :   {
     141         116 :     LibmeshPetscCallA(_fe_problem.comm().get(), SNESSetForceIteration(snes, PETSC_TRUE));
     142             :   }
     143             : #endif
     144             : 
     145             :   // See if SNESSetFunctionDomainError() has been called.  Note:
     146             :   // SNESSetFunctionDomainError() and SNESGetFunctionDomainError()
     147             :   // were added in different releases of PETSc.
     148             :   PetscBool domainerror;
     149      827899 :   LibmeshPetscCallA(_fe_problem.comm().get(), SNESGetFunctionDomainError(snes, &domainerror));
     150      827899 :   if (domainerror)
     151           0 :     status = MooseConvergenceStatus::DIVERGED;
     152             : 
     153             :   Real fnorm_old;
     154             :   // This is the first residual before any iterations have been done, but after
     155             :   // solution-modifying objects (if any) have been imposed on the solution vector.
     156             :   // We save it, and use it to detect convergence if system.usePreSMOResidual() == false.
     157      827899 :   if (iter == 0)
     158             :   {
     159      313938 :     system.setInitialResidual(fnorm);
     160      313938 :     fnorm_old = fnorm;
     161      313938 :     _nl_current_pingpong = 0;
     162             :   }
     163             :   else
     164      513961 :     fnorm_old = system._last_nl_rnorm;
     165             : 
     166             :   // Check for nonlinear residual ping-pong.
     167             :   // Ping-pong will always start from a residual increase
     168      827899 :   if ((_nl_current_pingpong % 2 == 1 && !(fnorm > fnorm_old)) ||
     169      823092 :       (_nl_current_pingpong % 2 == 0 && fnorm > fnorm_old))
     170       10240 :     _nl_current_pingpong += 1;
     171             :   else
     172      817659 :     _nl_current_pingpong = 0;
     173             : 
     174      827899 :   std::ostringstream oss;
     175      827899 :   if (fnorm != fnorm)
     176             :   {
     177           0 :     oss << "Failed to converge, residual norm is NaN\n";
     178           0 :     status = MooseConvergenceStatus::DIVERGED;
     179             :   }
     180      827899 :   else if ((iter >= _nl_forced_its) && fnorm < abs_tol)
     181             :   {
     182      153157 :     oss << "Converged due to residual norm " << fnorm << " < " << abs_tol << '\n';
     183      153157 :     status = MooseConvergenceStatus::CONVERGED;
     184             :   }
     185      674742 :   else if (nfuncs >= max_funcs)
     186             :   {
     187           0 :     oss << "Exceeded maximum number of residual evaluations: " << nfuncs << " > " << max_funcs
     188           0 :         << '\n';
     189           0 :     status = MooseConvergenceStatus::DIVERGED;
     190             :   }
     191      674742 :   else if ((iter >= _nl_forced_its) && iter && fnorm > system._last_nl_rnorm &&
     192        5899 :            fnorm >= _div_threshold)
     193             :   {
     194           0 :     oss << "Nonlinear solve was blowing up!\n";
     195           0 :     status = MooseConvergenceStatus::DIVERGED;
     196             :   }
     197      827899 :   if ((iter >= _nl_forced_its) && iter && status == MooseConvergenceStatus::ITERATING)
     198             :   {
     199      394800 :     const auto ref_residual = system.referenceResidual();
     200      394800 :     if (checkRelativeConvergence(iter, fnorm, ref_residual, rel_tol, abs_tol, oss))
     201      158287 :       status = MooseConvergenceStatus::CONVERGED;
     202      236513 :     else if (snorm < rel_step_tol * xnorm)
     203             :     {
     204           0 :       oss << "Converged due to small update length: " << snorm << " < " << rel_step_tol << " * "
     205           0 :           << xnorm << '\n';
     206           0 :       status = MooseConvergenceStatus::CONVERGED;
     207             :     }
     208      236513 :     else if (_nl_rel_div_tol > 0 && fnorm > ref_residual * _nl_rel_div_tol)
     209             :     {
     210           6 :       oss << "Diverged due to relative residual " << ref_residual << " > divergence tolerance "
     211           6 :           << _nl_rel_div_tol << " * relative residual " << ref_residual << '\n';
     212           6 :       status = MooseConvergenceStatus::DIVERGED;
     213             :     }
     214      236507 :     else if (_nl_abs_div_tol > 0 && fnorm > _nl_abs_div_tol)
     215             :     {
     216          10 :       oss << "Diverged due to residual " << fnorm << " > absolute divergence tolerance "
     217          10 :           << _nl_abs_div_tol << '\n';
     218          10 :       status = MooseConvergenceStatus::DIVERGED;
     219             :     }
     220      236497 :     else if (_nl_current_pingpong > _nl_max_pingpong)
     221             :     {
     222           6 :       oss << "Diverged due to maximum nonlinear residual pingpong achieved" << '\n';
     223           6 :       status = MooseConvergenceStatus::DIVERGED;
     224             :     }
     225             :   }
     226             : 
     227      827899 :   system._last_nl_rnorm = fnorm;
     228      827899 :   system._current_nl_its = static_cast<unsigned int>(iter);
     229             : 
     230      827899 :   std::string msg;
     231      827899 :   msg = oss.str();
     232      827899 :   if (_app.multiAppLevel() > 0)
     233      414932 :     MooseUtils::indentMessage(_app.name(), msg);
     234      827899 :   if (msg.length() > 0)
     235             : #if !PETSC_VERSION_LESS_THAN(3, 17, 0)
     236      311466 :     LibmeshPetscCallA(_fe_problem.comm().get(), PetscInfo(snes, "%s", msg.c_str()));
     237             : #else
     238             :     LibmeshPetscCallA(_fe_problem.comm().get(), PetscInfo(snes, msg.c_str()));
     239             : #endif
     240             : 
     241      827899 :   verboseOutput(oss);
     242             : 
     243      827899 :   return status;
     244      827899 : }

Generated by: LCOV version 1.14