LCOV - code coverage report
Current view: top level - src/convergence - MultiPostprocessorConvergence.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: fa5e60 Lines: 56 58 96.6 %
Date: 2026-06-24 08:03:36 Functions: 6 6 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 "MultiPostprocessorConvergence.h"
      11             : 
      12             : registerMooseObject("MooseApp", MultiPostprocessorConvergence);
      13             : 
      14             : InputParameters
      15        3111 : MultiPostprocessorConvergence::validParams()
      16             : {
      17        3111 :   InputParameters params = IterationCountConvergence::validParams();
      18             : 
      19        6222 :   params.addClassDescription("Converges if multiple post-processors are all less than tolerances.");
      20             : 
      21       12444 :   params.addRequiredParam<std::vector<PostprocessorName>>("postprocessors",
      22             :                                                           "Postprocessors to check");
      23       12444 :   params.addParam<std::vector<std::string>>(
      24             :       "descriptions",
      25             :       "Description of each Postprocessor. If not provided, the Postprocessor names are used as "
      26             :       "their descriptions.");
      27        9333 :   params.addRequiredParam<std::vector<Real>>("tolerances", "Tolerance for each Postprocessor");
      28             : 
      29        3111 :   return params;
      30           0 : }
      31             : 
      32          26 : MultiPostprocessorConvergence::MultiPostprocessorConvergence(const InputParameters & parameters)
      33          52 :   : IterationCountConvergence(parameters), _tolerances(getParam<std::vector<Real>>("tolerances"))
      34             : {
      35          52 :   const auto & pp_names = getParam<std::vector<PostprocessorName>>("postprocessors");
      36             : 
      37          78 :   if (isParamValid("descriptions"))
      38          39 :     _descriptions = getParam<std::vector<std::string>>("descriptions");
      39             :   else
      40             :   {
      41          39 :     for (const auto & pp_name : pp_names)
      42          26 :       _descriptions.push_back(pp_name);
      43             :   }
      44             : 
      45          78 :   for (const auto & pp_name : pp_names)
      46          52 :     _pp_values.push_back(&getPostprocessorValueByName(pp_name));
      47             : 
      48          26 :   if (_descriptions.size() != pp_names.size() || _tolerances.size() != pp_names.size())
      49           0 :     mooseError(
      50             :         "The parameters 'postprocessors', 'descriptions', and 'tolerances' must be the same size.");
      51          26 : }
      52             : 
      53             : void
      54          26 : MultiPostprocessorConvergence::initialSetup()
      55             : {
      56          26 :   IterationCountConvergence::initialSetup();
      57             : 
      58          26 :   _max_desc_length = getMaxDescriptionLength();
      59          26 : }
      60             : 
      61             : Convergence::MooseConvergenceStatus
      62          68 : MultiPostprocessorConvergence::checkConvergenceInner(unsigned int /*iter*/)
      63             : {
      64          68 :   bool all_converged = true;
      65          68 :   std::ostringstream oss;
      66          68 :   oss << "\n";
      67             : 
      68         204 :   for (const auto i : index_range(_descriptions))
      69             :   {
      70         136 :     const Real abs_pp_value = std::abs(*_pp_values[i]);
      71         136 :     if (abs_pp_value > _tolerances[i])
      72          68 :       all_converged = false;
      73             : 
      74         136 :     std::string desc = _descriptions[i];
      75         136 :     desc.resize(_max_desc_length + 1, ' '); // pad with spaces for alignment
      76         136 :     oss << comparisonLine(desc, abs_pp_value, _tolerances[i]);
      77         136 :   }
      78             : 
      79          68 :   verboseOutput(oss);
      80             : 
      81          68 :   if (all_converged)
      82          22 :     return Convergence::MooseConvergenceStatus::CONVERGED;
      83             :   else
      84          46 :     return Convergence::MooseConvergenceStatus::ITERATING;
      85          68 : }
      86             : 
      87             : unsigned int
      88          26 : MultiPostprocessorConvergence::getMaxDescriptionLength() const
      89             : {
      90          26 :   std::size_t max_desc_length = 0;
      91          78 :   for (const auto & description : _descriptions)
      92          52 :     max_desc_length = std::max(max_desc_length, description.length());
      93             : 
      94          26 :   return max_desc_length;
      95             : }
      96             : 
      97             : std::string
      98         136 : MultiPostprocessorConvergence::comparisonLine(const std::string & description,
      99             :                                               const Real err,
     100             :                                               const Real tol) const
     101             : {
     102         136 :   std::string color, compare_str;
     103         136 :   if (std::abs(err) > tol)
     104             :   {
     105          68 :     color = COLOR_RED;
     106          68 :     compare_str = ">";
     107             :   }
     108             :   else
     109             :   {
     110          68 :     color = COLOR_GREEN;
     111          68 :     compare_str = "<";
     112             :   }
     113             : 
     114         136 :   std::ostringstream oss;
     115         136 :   oss << "  " << description << ": " << color << std::scientific << std::setprecision(5)
     116         136 :       << std::abs(err) << " " << compare_str << " " << tol << COLOR_DEFAULT << "\n";
     117         272 :   return oss.str();
     118         136 : }

Generated by: LCOV version 1.14