LCOV - code coverage report
Current view: top level - src/postprocessors - Residual.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 30 34 88.2 %
Date: 2025-07-17 01:28:37 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       14625 : Residual::validParams()
      20             : {
      21       14625 :   InputParameters params = GeneralPostprocessor::validParams();
      22       14625 :   params.addClassDescription("Report the non-linear residual.");
      23             :   MooseEnum residual_types(
      24       14625 :       "FINAL INITIAL_BEFORE_PRESET INITIAL_AFTER_PRESET PRE_SMO INITIAL CURRENT COMPUTE", "FINAL");
      25       14625 :   params.addParam<MooseEnum>("residual_type", residual_types, "Type of residual to be reported.");
      26       29250 :   return params;
      27       14625 : }
      28             : 
      29         180 : Residual::Residual(const InputParameters & parameters)
      30         180 :   : GeneralPostprocessor(parameters), _residual_type(getParam<MooseEnum>("residual_type"))
      31             : {
      32         180 :   if (_residual_type == "INITIAL_BEFORE_PRESET")
      33           0 :     mooseDeprecated("INITIAL_BEFORE_PRESET is deprecated, use PRE_SMO instead.");
      34         180 :   if (_residual_type == "INITIAL_AFTER_PRESET")
      35           0 :     mooseDeprecated("INITIAL_AFTER_PRESET is deprecated, use INITIAL instead.");
      36         180 : }
      37             : 
      38             : Real
      39         616 : Residual::getValue() const
      40             : {
      41         616 :   Real residual = 0.0;
      42         616 :   if (_residual_type == "FINAL")
      43         242 :     residual = _subproblem.finalNonlinearResidual(_sys.number());
      44         374 :   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         341 :   else if (_residual_type == "COMPUTE")
      54          77 :     residual = _fe_problem.computeResidualL2Norm();
      55             :   else
      56             :   {
      57         264 :     FEProblemBase * fe_problem = dynamic_cast<FEProblemBase *>(&_subproblem);
      58         264 :     if (!fe_problem)
      59           0 :       mooseError("Dynamic cast to FEProblemBase failed in Residual Postprocessor");
      60             : 
      61         264 :     const auto & nl_sys = fe_problem->getNonlinearSystemBase(_sys.number());
      62             : 
      63         264 :     if (_residual_type == "INITIAL_BEFORE_PRESET" || _residual_type == "PRE_SMO")
      64          22 :       residual = nl_sys.preSMOResidual();
      65         242 :     else if (_residual_type == "INITIAL_AFTER_PRESET" || _residual_type == "INITIAL")
      66         242 :       residual = nl_sys.initialResidual();
      67             :     else
      68           0 :       mooseError("Invalid residual_type option in Residual Postprocessor: ", _residual_type);
      69             :   }
      70         616 :   return residual;
      71             : }

Generated by: LCOV version 1.14