www.mooseframework.org
TopResidualDebugOutput.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 
10 // MOOSE includes
11 #include "TopResidualDebugOutput.h"
12 #include "FEProblem.h"
13 #include "MooseApp.h"
14 #include "Material.h"
15 #include "Console.h"
16 #include "Action.h"
17 #include "MooseMesh.h"
18 #include "NonlinearSystemBase.h"
19 
20 #include "libmesh/transient_system.h"
21 #include "libmesh/fe_type.h"
22 
24 
25 template <>
28 {
30  params.addClassDescription("Debug output object for displaying the top contributing residuals.");
31 
32  // Create parameters for allowing debug outputter to be defined within the [Outputs] block
33  params.addParam<unsigned int>(
34  "num_residuals", 0, "The number of top residuals to print out (0 = no output)");
35 
36  // By default operate on both nonlinear and linear residuals
37  params.set<ExecFlagEnum>("execute_on", true) = {EXEC_LINEAR, EXEC_NONLINEAR, EXEC_TIMESTEP_END};
38  return params;
39 }
40 
42  : PetscOutput(parameters),
43  _num_residuals(getParam<unsigned int>("num_residuals")),
44  _sys(_problem_ptr->getNonlinearSystemBase().system())
45 {
46 }
47 
48 void
50 {
51  // Display the top residuals
52  if (_num_residuals > 0)
54 }
55 
56 void
57 TopResidualDebugOutput::printTopResiduals(const NumericVector<Number> & residual, unsigned int n)
58 {
59  MooseMesh & mesh = _problem_ptr->mesh();
60 
61  std::vector<TopResidualDebugOutputTopResidualData> vec;
62  vec.resize(residual.local_size());
63 
64  unsigned int j = 0;
65 
66  // Loop over all nodal variables
67  for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
68  {
69  dof_id_type nd = node->id();
70 
71  for (unsigned int var = 0; var < node->n_vars(_sys.number()); ++var)
72  // check that variable exists on node
73  if (node->n_dofs(_sys.number(), var) > 0)
74  {
75  const auto & subdomain_ids = mesh.getNodeBlockIds(*node);
76  dof_id_type dof_idx = node->dof_number(_sys.number(), var, 0);
78  var, subdomain_ids, nd, *node, residual(dof_idx), false, true);
79  j++;
80  }
81  }
82 
83  // Loop over all elemental variables
84  for (const auto & elem : as_range(mesh.activeLocalElementsBegin(), mesh.activeLocalElementsEnd()))
85  {
86  dof_id_type elem_id = elem->id();
87  const SubdomainID subdomain_id = elem->subdomain_id();
88 
89  for (unsigned int var = 0; var < elem->n_vars(_sys.number()); ++var)
90  // check that variable exists on element
91  if (elem->n_dofs(_sys.number(), var) > 0)
92  {
93  dof_id_type dof_idx = elem->dof_number(_sys.number(), var, 0);
95  var, {subdomain_id}, elem_id, elem->centroid(), residual(dof_idx), false, false);
96  j++;
97  }
98  }
99 
100  // Loop over all scalar variables
101  std::vector<unsigned int> var_nums;
102  _sys.get_all_variable_numbers(var_nums);
103  const DofMap & dof_map = _sys.get_dof_map();
104  for (const auto & var_num : var_nums)
105  if (_sys.variable_type(var_num).family == SCALAR)
106  {
107  std::vector<dof_id_type> dof_indices;
108  dof_map.SCALAR_dof_indices(dof_indices, var_num);
109 
110  for (const auto & dof : dof_indices)
111  if (dof >= dof_map.first_dof() && dof < dof_map.end_dof())
112  {
113  vec[j] =
114  TopResidualDebugOutputTopResidualData(var_num, {}, 0, Point(), residual(dof), true);
115  j++;
116  }
117  }
118 
119  // Sort vec by residuals
120  std::sort(vec.begin(), vec.end(), sortTopResidualData);
121 
122  // Display the residuals
123  Moose::err << "[DBG][" << processor_id() << "] Max " << n << " residuals";
124  if (j < n)
125  {
126  n = j;
127  Moose::err << " (Only " << n << " available)";
128  }
129  Moose::err << std::endl;
130 
131  for (unsigned int i = 0; i < n; ++i)
132  {
133  Moose::err << "[DBG][" << processor_id() << "] " << std::setprecision(15) << vec[i]._residual
134  << " '" << _sys.variable_name(vec[i]._var).c_str() << "' ";
135  if (vec[i]._is_scalar)
136  Moose::err << "(SCALAR)\n";
137  else
138  {
139  // Create subdomain list string for node
140  const unsigned int n_subdomains = vec[i]._subdomain_ids.size();
141  std::vector<SubdomainName> subdomain_names(n_subdomains);
142  unsigned int i_block = 0;
143  for (const auto & subdomain_id : vec[i]._subdomain_ids)
144  {
145  subdomain_names[i_block] = mesh.getSubdomainName(subdomain_id);
146  i_block++;
147  }
148  const std::string subdomains_string = Moose::stringify(subdomain_names, ", ", "'", true);
149 
150  const std::string elem_or_node_string = vec[i]._is_nodal ? "node" : "element";
151 
152  Moose::err << "in subdomain(s) " << subdomains_string << " at " << elem_or_node_string << " "
153  << vec[i]._id << ": " << vec[i]._point << '\n';
154  }
155  }
156 }
static bool sortTopResidualData(TopResidualDebugOutputTopResidualData i, TopResidualDebugOutputTopResidualData j)
Method for sorting the residuals data from TopResidualDebugOutputTopResidualData structs.
MeshBase::const_element_iterator activeLocalElementsBegin()
Calls active_local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2217
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:24
NonlinearSystemBase & getNonlinearSystemBase()
void printTopResiduals(const NumericVector< Number > &residual, unsigned int n)
Prints the n top residuals for the variables in the system.
virtual void output(const ExecFlagType &type) override
Perform the debugging output.
registerMooseObject("MooseApp", TopResidualDebugOutput)
MeshBase::const_node_iterator localNodesBegin()
Calls local_nodes_begin/end() on the underlying libMesh mesh object.
Definition: MooseMesh.C:2205
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...
A class for producing various debug related outputs.
const std::set< SubdomainID > & getNodeBlockIds(const Node &node) const
Return list of blocks to which the given node belongs.
Definition: MooseMesh.C:847
const ExecFlagType EXEC_TIMESTEP_END
System & _sys
Reference to libMesh system.
MeshBase::const_node_iterator localNodesEnd()
Definition: MooseMesh.C:2211
nl system()
DofMap & dof_map
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
A structure for storing data related to top residuals.
subdomain_id_type SubdomainID
const ExecFlagType EXEC_LINEAR
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
const ExecFlagType EXEC_NONLINEAR
virtual NumericVector< Number > & RHS()=0
InputParameters validParams< PetscOutput >()
Definition: PetscOutput.C:20
TopResidualDebugOutput(const InputParameters &parameters)
Class constructor.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
PetscInt n
virtual MooseMesh & mesh() override
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 option parameter and a documentation string to the InputParameters object...
InputParameters validParams< TopResidualDebugOutput >()
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:24
const MeshBase::const_element_iterator activeLocalElementsEnd()
Definition: MooseMesh.C:2223
unsigned int _num_residuals
Number of residuals to display.
const std::string & getSubdomainName(SubdomainID subdomain_id)
Return the name of a block given an id.
Definition: MooseMesh.C:1124