https://mooseframework.inl.gov
Residual.C
Go to the documentation of this file.
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 
20 {
22  params.addClassDescription("Report the non-linear residual.");
23  MooseEnum residual_types(
24  "FINAL INITIAL_BEFORE_PRESET INITIAL_AFTER_PRESET PRE_SMO INITIAL CURRENT COMPUTE", "FINAL");
25  params.addParam<MooseEnum>("residual_type", residual_types, "Type of residual to be reported.");
26  return params;
27 }
28 
30  : GeneralPostprocessor(parameters), _residual_type(getParam<MooseEnum>("residual_type"))
31 {
32  if (_residual_type == "INITIAL_BEFORE_PRESET")
33  mooseDeprecated("INITIAL_BEFORE_PRESET is deprecated, use PRE_SMO instead.");
34  if (_residual_type == "INITIAL_AFTER_PRESET")
35  mooseDeprecated("INITIAL_AFTER_PRESET is deprecated, use INITIAL instead.");
36 }
37 
38 Real
40 {
41  Real residual = 0.0;
42  if (_residual_type == "FINAL")
44  else if (_residual_type == "CURRENT")
45  {
46  const auto & snes = _fe_problem.getNonlinearSystemBase(_sys.number()).getSNES();
47 
48  PetscReal norm;
49  LibmeshPetscCall(SNESGetFunctionNorm(snes, &norm));
50 
51  residual = norm;
52  }
53  else if (_residual_type == "COMPUTE")
55  else
56  {
57  FEProblemBase * fe_problem = dynamic_cast<FEProblemBase *>(&_subproblem);
58  if (!fe_problem)
59  mooseError("Dynamic cast to FEProblemBase failed in Residual Postprocessor");
60 
61  const auto & nl_sys = fe_problem->getNonlinearSystemBase(_sys.number());
62 
63  if (_residual_type == "INITIAL_BEFORE_PRESET" || _residual_type == "PRE_SMO")
64  residual = nl_sys.preSMOResidual();
65  else if (_residual_type == "INITIAL_AFTER_PRESET" || _residual_type == "INITIAL")
66  residual = nl_sys.initialResidual();
67  else
68  mooseError("Invalid residual_type option in Residual Postprocessor: ", _residual_type);
69  }
70  return residual;
71 }
void mooseDeprecated(Args &&... args) const
static InputParameters validParams()
Definition: Residual.C:19
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Real preSMOResidual() const
The pre-SMO residual.
This class is here to combine the Postprocessor interface and the base class Postprocessor object alo...
registerMooseObject("MooseApp", Residual)
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
SubProblem & _subproblem
Reference to the Subproblem for this user object.
Definition: UserObject.h:208
virtual Real finalNonlinearResidual(const unsigned int nl_sys_num) const
Definition: SubProblem.C:754
virtual Real getValue() const override
This will return the final nonlinear residual.
Definition: Residual.C:39
static InputParameters validParams()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
SystemBase & _sys
Reference to the system object for this user object.
Definition: UserObject.h:215
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1159
auto norm(const T &a) -> decltype(std::abs(a))
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
FEProblemBase & _fe_problem
Reference to the FEProblemBase for this user object.
Definition: UserObject.h:211
Residual(const InputParameters &parameters)
Definition: Residual.C:29
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object...
MooseEnum _residual_type
Definition: Residual.h:30
Real computeResidualL2Norm(NonlinearSystemBase &sys)
Computes the residual of a nonlinear system using whatever is sitting in the current solution vector ...