LCOV - code coverage report
Current view: top level - src/postprocessors - Residual.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 30 34 88.2 %
Date: 2025-08-08 20:01:16 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       14651 : Residual::validParams()
      20             : {
      21       14651 :   InputParameters params = GeneralPostprocessor::validParams();
      22       14651 :   params.addClassDescription("Report the non-linear residual.");
      23             :   MooseEnum residual_types(
      24       14651 :       "FINAL INITIAL_BEFORE_PRESET INITIAL_AFTER_PRESET PRE_SMO INITIAL CURRENT COMPUTE", "FINAL");
      25       14651 :   params.addParam<MooseEnum>("residual_type", residual_types, "Type of residual to be reported.");
      26       29302 :   return params;
      27       14651 : }
      28             : 
      29         193 : Residual::Residual(const InputParameters & parameters)
      30         193 :   : GeneralPostprocessor(parameters), _residual_type(getParam<MooseEnum>("residual_type"))
      31             : {
      32         193 :   if (_residual_type == "INITIAL_BEFORE_PRESET")
      33           0 :     mooseDeprecated("INITIAL_BEFORE_PRESET is deprecated, use PRE_SMO instead.");
      34         193 :   if (_residual_type == "INITIAL_AFTER_PRESET")
      35           0 :     mooseDeprecated("INITIAL_AFTER_PRESET is deprecated, use INITIAL instead.");
      36         193 : }
      37             : 
      38             : Real
      39         661 : Residual::getValue() const
      40             : {
      41         661 :   Real residual = 0.0;
      42         661 :   if (_residual_type == "FINAL")
      43         258 :     residual = _subproblem.finalNonlinearResidual(_sys.number());
      44         403 :   else if (_residual_type == "CURRENT")
      45             :   {
      46          36 :     const auto & snes = _fe_problem.getNonlinearSystemBase(_sys.number()).getSNES();
      47             : 
      48             :     PetscReal norm;
      49          36 :     LibmeshPetscCall(SNESGetFunctionNorm(snes, &norm));
      50             : 
      51          36 :     residual = norm;
      52             :   }
      53         367 :   else if (_residual_type == "COMPUTE")
      54          84 :     residual = _fe_problem.computeResidualL2Norm();
      55             :   else
      56             :   {
      57         283 :     FEProblemBase * fe_problem = dynamic_cast<FEProblemBase *>(&_subproblem);
      58         283 :     if (!fe_problem)
      59           0 :       mooseError("Dynamic cast to FEProblemBase failed in Residual Postprocessor");
      60             : 
      61         283 :     const auto & nl_sys = fe_problem->getNonlinearSystemBase(_sys.number());
      62             : 
      63         283 :     if (_residual_type == "INITIAL_BEFORE_PRESET" || _residual_type == "PRE_SMO")
      64          25 :       residual = nl_sys.preSMOResidual();
      65         258 :     else if (_residual_type == "INITIAL_AFTER_PRESET" || _residual_type == "INITIAL")
      66         258 :       residual = nl_sys.initialResidual();
      67             :     else
      68           0 :       mooseError("Invalid residual_type option in Residual Postprocessor: ", _residual_type);
      69             :   }
      70         661 :   return residual;
      71             : }

Generated by: LCOV version 1.14