LCOV - code coverage report
Current view: top level - src/convergence - PostprocessorConvergence.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31706 (f8ed4a) with base bb0a08 Lines: 54 55 98.2 %
Date: 2025-11-03 17:23:24 Functions: 3 3 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             : #include "PostprocessorConvergence.h"
      11             : 
      12             : registerMooseObject("MooseApp", PostprocessorConvergence);
      13             : 
      14             : InputParameters
      15       14957 : PostprocessorConvergence::validParams()
      16             : {
      17       14957 :   InputParameters params = IterationCountConvergence::validParams();
      18             : 
      19       29914 :   params.addClassDescription("Compares the absolute value of a post-processor to a tolerance.");
      20             : 
      21       59828 :   params.addRequiredParam<PostprocessorName>("postprocessor",
      22             :                                              "Post-processor to use for convergence criteria");
      23       59828 :   params.addRequiredParam<Real>("tolerance", "Absolute tolerance to use for convergence criteria");
      24       44871 :   params.addParam<unsigned int>(
      25             :       "max_diverging_iterations",
      26       29914 :       std::numeric_limits<unsigned int>::max(),
      27             :       "Number of consecutive iterations of the post-processor value either increasing or not "
      28             :       "reducing fast enough, at which to consider the solve diverged");
      29       29914 :   params.addParam<Real>(
      30             :       "diverging_iteration_rel_reduction",
      31       29914 :       0.0,
      32             :       "The relative reduction with respect to the previous iteration, for which the iteration "
      33             :       "counts as a diverging iteration with respect to 'max_diverging_iterations': an iteration is "
      34             :       "diverging if (|pp_old| - |pp_new|)/|pp_old| < rel_reduction. The default value of 0 "
      35             :       "corresponds to checking that the iteration is actually converging, whereas values < 1 can "
      36             :       "be used to additionally check that the iteration is converging at an acceptable rate.");
      37             : 
      38       14957 :   return params;
      39           0 : }
      40             : 
      41          47 : PostprocessorConvergence::PostprocessorConvergence(const InputParameters & parameters)
      42             :   : IterationCountConvergence(parameters),
      43          47 :     _postprocessor(getPostprocessorValue("postprocessor")),
      44          94 :     _tol(getParam<Real>("tolerance")),
      45          94 :     _max_diverging_iterations(getParam<unsigned int>("max_diverging_iterations")),
      46          94 :     _div_rel_reduction(getParam<Real>("diverging_iteration_rel_reduction")),
      47          47 :     _diverging_iterations(0),
      48          94 :     _pp_value_old(std::numeric_limits<Real>::max())
      49             : {
      50          47 : }
      51             : 
      52             : Convergence::MooseConvergenceStatus
      53         389 : PostprocessorConvergence::checkConvergenceInner(unsigned int iter)
      54             : {
      55         389 :   const auto pp_value = std::abs(_postprocessor);
      56             : 
      57             :   // Initialize diverging iteration data
      58         389 :   if (iter == 0)
      59             :   {
      60          24 :     _diverging_iterations = 0;
      61          24 :     _pp_value_old = std::numeric_limits<Real>::max();
      62             :   }
      63             : 
      64             :   // Check for diverging iteration
      65         389 :   const Real rel_reduction = (_pp_value_old - pp_value) / _pp_value_old;
      66         389 :   if (rel_reduction < _div_rel_reduction)
      67             :   {
      68          24 :     _diverging_iterations++;
      69             : 
      70          24 :     std::ostringstream oss;
      71          24 :     oss << COLOR_YELLOW << "Diverging iteration: (" << _pp_value_old << "-" << pp_value << ")/"
      72          24 :         << _pp_value_old << " = " << rel_reduction << " < " << _div_rel_reduction << "."
      73          24 :         << COLOR_DEFAULT;
      74          24 :     verboseOutput(oss);
      75          24 :   }
      76             :   else
      77         365 :     _diverging_iterations = 0;
      78         389 :   _pp_value_old = pp_value;
      79             : 
      80         389 :   if (pp_value <= _tol)
      81             :   {
      82          37 :     std::ostringstream oss;
      83          37 :     oss << COLOR_GREEN << "Converged due to |post-processor| (" << pp_value << ") <= tolerance ("
      84          37 :         << _tol << ")." << COLOR_DEFAULT;
      85          37 :     verboseOutput(oss);
      86          37 :     return MooseConvergenceStatus::CONVERGED;
      87          37 :   }
      88         352 :   else if (_diverging_iterations > _max_diverging_iterations)
      89             :   {
      90           4 :     std::ostringstream oss;
      91           4 :     oss << COLOR_RED << "Diverged due to exceeding maximum number of diverging iterations ("
      92           4 :         << _max_diverging_iterations << ")." << COLOR_DEFAULT;
      93           4 :     verboseOutput(oss);
      94           4 :     return MooseConvergenceStatus::DIVERGED;
      95           4 :   }
      96             :   else
      97             :   {
      98         348 :     std::ostringstream oss;
      99         348 :     oss << "Still iterating due to |post-processor| (" << pp_value << ") > tolerance (" << _tol
     100         348 :         << ").";
     101         348 :     verboseOutput(oss);
     102         348 :     return MooseConvergenceStatus::ITERATING;
     103         348 :   }
     104             : }

Generated by: LCOV version 1.14