www.mooseframework.org
SetupResidualDebugAction.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
11 
12 // MOOSE includes
13 #include "ActionWarehouse.h"
14 #include "Factory.h"
15 #include "FEProblem.h"
16 #include "MooseVariableFE.h"
17 #include "NonlinearSystemBase.h"
18 #include "Conversion.h"
19 
20 registerMooseAction("MooseApp", SetupResidualDebugAction, "setup_residual_debug");
21 
22 template <>
25 {
27  params.addParam<std::vector<NonlinearVariableName>>(
28  "show_var_residual", "Variables for which residuals will be sent to the output file");
29  return params;
30 }
31 
33  : Action(parameters),
34  _show_var_residual(getParam<std::vector<NonlinearVariableName>>("show_var_residual"))
35 {
36 }
37 
38 void
40 {
41  if (_problem.get() == NULL)
42  return;
43 
44  _problem->getNonlinearSystemBase().debuggingResiduals(true);
45 
46  // debug variable residuals
47  for (const auto & var_name : _show_var_residual)
48  {
49  // add aux-variable
50  MooseVariableFEBase & var = _problem->getVariable(
52  InputParameters params = _factory.getValidParams("DebugResidualAux");
53  const std::set<SubdomainID> & subdomains = var.activeSubdomains();
54 
55  std::stringstream aux_var_ss;
56  aux_var_ss << "residual_" << var.name();
57  std::string aux_var_name = aux_var_ss.str();
58 
59  if (subdomains.empty())
60  _problem->addAuxVariable(aux_var_name, FEType(FIRST, LAGRANGE));
61  else
62  {
63  _problem->addAuxVariable(aux_var_name, FEType(FIRST, LAGRANGE), &subdomains);
64  std::vector<SubdomainName> block_names;
65  block_names.reserve(subdomains.size());
66  for (const SubdomainID & id : subdomains)
67  block_names.push_back(Moose::stringify(id));
68  params.set<std::vector<SubdomainName>>("block") = block_names;
69  }
70 
71  // add aux-kernel
72  std::stringstream kern_ss;
73  kern_ss << "residual_" << var.name() << "_kernel";
74  std::string kern_name = kern_ss.str();
75 
76  params.set<AuxVariableName>("variable") = aux_var_name;
77  params.set<NonlinearVariableName>("debug_variable") = var.name();
78  params.set<ExecFlagEnum>("execute_on") = {EXEC_LINEAR, EXEC_TIMESTEP_END};
79  _problem->addAuxKernel("DebugResidualAux", kern_name, params);
80  }
81 }
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
InputParameters getValidParams(const std::string &name)
Get valid parameters for the object.
Definition: Factory.C:67
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Factory & _factory
The Factory associated with the MooseApp.
Definition: Action.h:186
const ExecFlagType EXEC_TIMESTEP_END
registerMooseAction("MooseApp", SetupResidualDebugAction, "setup_residual_debug")
Base class for actions.
Definition: Action.h:35
SetupResidualDebugAction(InputParameters parameters)
std::vector< NonlinearVariableName > _show_var_residual
virtual const std::set< SubdomainID > & activeSubdomains() const =0
The subdomains the variable is active on.
InputParameters validParams< SetupResidualDebugAction >()
subdomain_id_type SubdomainID
const ExecFlagType EXEC_LINEAR
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
virtual void act() override
Method to add objects to the simulation or perform other setup tasks.
const std::string & name() const
Get the variable name.
std::shared_ptr< FEProblemBase > & _problem
Convenience reference to a problem this action works on.
Definition: Action.h:216
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
InputParameters validParams< Action >()
Definition: Action.C:22