https://mooseframework.inl.gov
TopResidualDebugOutput.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 // 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 
27 {
29  params.addClassDescription("Debug output object for displaying the top contributing residuals.");
30 
31  // Create parameters for allowing debug outputter to be defined within the [Outputs] block
32  params.addParam<unsigned int>(
33  "num_residuals", 0, "The number of top residuals to print out (0 = no output)");
34 
35  // By default operate on both nonlinear and linear residuals
36  params.set<ExecFlagEnum>("execute_on", true) = {EXEC_LINEAR, EXEC_NONLINEAR, EXEC_TIMESTEP_END};
37  params.addParam<NonlinearSystemName>(
38  "nl_sys", "nl0", "The nonlinear system that we should output information for.");
39  return params;
40 }
41 
43  : PetscOutput(parameters),
44  _num_residuals(getParam<unsigned int>("num_residuals")),
45  _nl(_problem_ptr->getNonlinearSystemBase(
46  _problem_ptr->nlSysNum(getParam<NonlinearSystemName>("nl_sys")))),
47  _sys(_nl.system())
48 {
49 }
50 
51 void
53 {
54  // Display the top residuals
55  if (_num_residuals > 0)
57 }
58 
59 void
61 {
63 
64  std::vector<TopResidualDebugOutputTopResidualData> vec;
65  vec.resize(residual.local_size());
66 
67  unsigned int j = 0;
68 
69  // Loop over all nodal variables
70  for (const auto & node : as_range(mesh.localNodesBegin(), mesh.localNodesEnd()))
71  {
72  dof_id_type nd = node->id();
73 
74  for (unsigned int var = 0; var < node->n_vars(_sys.number()); ++var)
75  // check that variable exists on node
76  if (node->n_dofs(_sys.number(), var) > 0)
77  {
78  const auto & subdomain_ids = mesh.getNodeBlockIds(*node);
79  dof_id_type dof_idx = node->dof_number(_sys.number(), var, 0);
81  var, subdomain_ids, nd, *node, residual(dof_idx), false, true);
82  j++;
83  }
84  }
85 
86  // Loop over all elemental variables
87  for (const auto & elem : as_range(mesh.activeLocalElementsBegin(), mesh.activeLocalElementsEnd()))
88  {
89  dof_id_type elem_id = elem->id();
90  const SubdomainID subdomain_id = elem->subdomain_id();
91 
92  for (unsigned int var = 0; var < elem->n_vars(_sys.number()); ++var)
93  // check that variable exists on element
94  if (elem->n_dofs(_sys.number(), var) > 0)
95  {
96  dof_id_type dof_idx = elem->dof_number(_sys.number(), var, 0);
98  var, {subdomain_id}, elem_id, elem->vertex_average(), residual(dof_idx), false, false);
99  j++;
100  }
101  }
102 
103  // Loop over all scalar variables
104  std::vector<unsigned int> var_nums;
105  _sys.get_all_variable_numbers(var_nums);
106  const DofMap & dof_map = _sys.get_dof_map();
107  for (const auto & var_num : var_nums)
108  if (_sys.variable_type(var_num).family == SCALAR)
109  {
110  std::vector<dof_id_type> dof_indices;
111  dof_map.SCALAR_dof_indices(dof_indices, var_num);
112 
113  for (const auto & dof : dof_indices)
114  if (dof >= dof_map.first_dof() && dof < dof_map.end_dof())
115  {
116  vec[j] =
117  TopResidualDebugOutputTopResidualData(var_num, {}, 0, Point(), residual(dof), true);
118  j++;
119  }
120  }
121 
122  // Sort vec by residuals
123  std::sort(vec.begin(), vec.end(), sortTopResidualData);
124 
125  // Display the residuals
126  Moose::err << "[DBG][" << processor_id() << "] Max " << n << " residuals";
127  if (j < n)
128  {
129  n = j;
130  Moose::err << " (Only " << n << " available)";
131  }
132  Moose::err << std::endl;
133 
134  for (unsigned int i = 0; i < n; ++i)
135  {
136  Moose::err << "[DBG][" << processor_id() << "] " << std::setprecision(15) << vec[i]._residual
137  << " '" << _sys.variable_name(vec[i]._var).c_str() << "' ";
138  if (vec[i]._is_scalar)
139  Moose::err << "(SCALAR)\n";
140  else
141  {
142  // Create subdomain list string for node
143  const unsigned int n_subdomains = vec[i]._subdomain_ids.size();
144  std::vector<SubdomainName> subdomain_names(n_subdomains);
145  unsigned int i_block = 0;
146  for (const auto & subdomain_id : vec[i]._subdomain_ids)
147  {
148  subdomain_names[i_block] = mesh.getSubdomainName(subdomain_id);
149  i_block++;
150  }
151  const std::string subdomains_string = Moose::stringify(subdomain_names, ", ", "'", true);
152 
153  const std::string elem_or_node_string = vec[i]._is_nodal ? "node" : "element";
154 
155  Moose::err << "in subdomain(s) " << subdomains_string << " at " << elem_or_node_string << " "
156  << vec[i]._id << ": " << vec[i]._point << '\n';
157  }
158  }
159 
160  Moose::err << std::flush;
161 }
static bool sortTopResidualData(TopResidualDebugOutputTopResidualData i, TopResidualDebugOutputTopResidualData j)
Method for sorting the residuals data from TopResidualDebugOutputTopResidualData structs.
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
SCALAR
static InputParameters validParams()
void printTopResiduals(const NumericVector< Number > &residual, unsigned int n)
Prints the n top residuals for the variables in the system.
registerMooseObject("MooseApp", TopResidualDebugOutput)
T & set(const std::string &name, bool quiet_mode=false)
Returns a writable reference to the named parameters.
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void get_all_variable_numbers(std::vector< unsigned int > &all_variable_numbers) const
A class for producing various debug related outputs.
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:34
void SCALAR_dof_indices(std::vector< dof_id_type > &di, const unsigned int vn, const bool old_dofs=false) const
virtual void output() override
Perform the debugging output.
unsigned int number() const
static InputParameters validParams()
Definition: PetscOutput.C:125
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:185
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
const std::string & variable_name(const unsigned int i) const
A structure for storing data related to top residuals.
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:29
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:31
virtual NumericVector< Number > & RHS()=0
TopResidualDebugOutput(const InputParameters &parameters)
Class constructor.
const FEType & variable_type(const unsigned int i) const
virtual numeric_index_type local_size() const =0
libMesh::System & _sys
Reference to libMesh system.
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 optional parameter and a documentation string to the InputParameters object...
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:41
processor_id_type processor_id() const
const DofMap & get_dof_map() const
void ErrorVector unsigned int
unsigned int _num_residuals
Number of residuals to display.
uint8_t dof_id_type
NonlinearSystemBase & _nl
Reference to MOOSE&#39;s nonlinear system.