LCOV - code coverage report
Current view: top level - src/postprocessors - Residual.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 30 34 88.2 %
Date: 2026-05-29 20:35:17 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 "Residual.h"
      11             : 
      12             : #include "FEProblem.h"
      13             : #include "SubProblem.h"
      14             : #include "NonlinearSystem.h"
      15             : 
      16             : registerMooseObject("MooseApp", Residual);
      17             : 
      18             : InputParameters
      19        3431 : Residual::validParams()
      20             : {
      21        3431 :   InputParameters params = GeneralPostprocessor::validParams();
      22        6862 :   params.addClassDescription("Report the non-linear residual.");
      23             :   MooseEnum residual_types(
      24       13724 :       "FINAL INITIAL_BEFORE_PRESET INITIAL_AFTER_PRESET PRE_SMO INITIAL CURRENT COMPUTE", "FINAL");
      25       10293 :   params.addParam<MooseEnum>("residual_type", residual_types, "Type of residual to be reported.");
      26        6862 :   return params;
      27        3431 : }
      28             : 
      29         185 : Residual::Residual(const InputParameters & parameters)
      30         370 :   : GeneralPostprocessor(parameters), _residual_type(getParam<MooseEnum>("residual_type"))
      31             : {
      32         185 :   if (_residual_type == "INITIAL_BEFORE_PRESET")
      33           0 :     mooseDeprecated("INITIAL_BEFORE_PRESET is deprecated, use PRE_SMO instead.");
      34         185 :   if (_residual_type == "INITIAL_AFTER_PRESET")
      35           0 :     mooseDeprecated("INITIAL_AFTER_PRESET is deprecated, use INITIAL instead.");
      36         185 : }
      37             : 
      38             : Real
      39         611 : Residual::getValue() const
      40             : {
      41         611 :   Real residual = 0.0;
      42         611 :   if (_residual_type == "FINAL")
      43         242 :     residual = _subproblem.finalNonlinearResidual(_sys.number());
      44         369 :   else if (_residual_type == "CURRENT")
      45             :   {
      46          33 :     const auto & snes = _fe_problem.getNonlinearSystemBase(_sys.number()).getSNES();
      47             : 
      48             :     PetscReal norm;
      49          33 :     LibmeshPetscCall(SNESGetFunctionNorm(snes, &norm));
      50             : 
      51          33 :     residual = norm;
      52             :   }
      53         336 :   else if (_residual_type == "COMPUTE")
      54          77 :     residual = _fe_problem.computeResidualL2Norm();
      55             :   else
      56             :   {
      57         259 :     FEProblemBase * fe_problem = dynamic_cast<FEProblemBase *>(&_subproblem);
      58         259 :     if (!fe_problem)
      59           0 :       mooseError("Dynamic cast to FEProblemBase failed in Residual Postprocessor");
      60             : 
      61         259 :     const auto & nl_sys = fe_problem->getNonlinearSystemBase(_sys.number());
      62             : 
      63         259 :     if (_residual_type == "INITIAL_BEFORE_PRESET" || _residual_type == "PRE_SMO")
      64          23 :       residual = nl_sys.preSMOResidual();
      65         236 :     else if (_residual_type == "INITIAL_AFTER_PRESET" || _residual_type == "INITIAL")
      66         236 :       residual = nl_sys.initialResidual();
      67             :     else
      68           0 :       mooseError("Invalid residual_type option in Residual Postprocessor: ", _residual_type);
      69             :   }
      70         611 :   return residual;
      71             : }

Generated by: LCOV version 1.14