www.mooseframework.org
TopResidualDebugOutput.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "TopResidualDebugOutput.h"
17 #include "FEProblem.h"
18 #include "MooseApp.h"
19 #include "Material.h"
20 #include "Console.h"
21 #include "Action.h"
22 #include "MooseMesh.h"
23 #include "NonlinearSystemBase.h"
24 
25 #include "libmesh/transient_system.h"
26 #include "libmesh/fe_type.h"
27 
28 template <>
31 {
33 
34  // Create parameters for allowing debug outputter to be defined within the [Outputs] block
35  params.addParam<unsigned int>(
36  "num_residuals", 0, "The number of top residuals to print out (0 = no output)");
37 
38  // By default operate on both nonlinear and linear residuals
39  params.set<MultiMooseEnum>("execute_on") = "linear nonlinear timestep_end";
40  return params;
41 }
42 
44  : PetscOutput(parameters),
45  _num_residuals(getParam<unsigned int>("num_residuals")),
46  _sys(_problem_ptr->getNonlinearSystemBase().system())
47 {
48 }
49 
50 void
52 {
53  // Display the top residuals
54  if (_num_residuals > 0)
56 }
57 
58 void
59 TopResidualDebugOutput::printTopResiduals(const NumericVector<Number> & residual, unsigned int n)
60 {
61  // Need a reference to the libMesh mesh object
62  MeshBase & mesh = _problem_ptr->mesh().getMesh();
63 
64  std::vector<TopResidualDebugOutputTopResidualData> vec;
65  vec.resize(residual.local_size());
66 
67  unsigned int j = 0;
68  MeshBase::node_iterator it = mesh.local_nodes_begin();
69  const MeshBase::node_iterator end = mesh.local_nodes_end();
70  for (; it != end; ++it)
71  {
72  Node & node = *(*it);
73  dof_id_type nd = node.id();
74 
75  for (unsigned int var = 0; var < node.n_vars(_sys.number()); ++var)
76  if (node.n_dofs(_sys.number(), var) >
77  0) // this check filters scalar variables (which are clearly not a dof on every node)
78  {
79  dof_id_type dof_idx = node.dof_number(_sys.number(), var, 0);
80  vec[j] = TopResidualDebugOutputTopResidualData(var, nd, residual(dof_idx));
81  j++;
82  }
83  }
84 
85  // Loop over all scalar variables
86  std::vector<unsigned int> var_nums;
87  _sys.get_all_variable_numbers(var_nums);
88  const DofMap & dof_map = _sys.get_dof_map();
89  for (const auto & var_num : var_nums)
90  if (_sys.variable_type(var_num).family == SCALAR)
91  {
92  std::vector<dof_id_type> dof_indices;
93  dof_map.SCALAR_dof_indices(dof_indices, var_num);
94 
95  for (const auto & dof : dof_indices)
96  if (dof >= dof_map.first_dof() && dof < dof_map.end_dof())
97  {
98  vec[j] = TopResidualDebugOutputTopResidualData(var_num, 0, residual(dof), true);
99  j++;
100  }
101  }
102 
103  // Sort vec by residuals
104  std::sort(vec.begin(), vec.end(), sortTopResidualData);
105 
106  // Display the residuals
107  Moose::err << "[DBG][" << processor_id() << "] Max " << n << " residuals";
108  if (j < n)
109  {
110  n = j;
111  Moose::err << " (Only " << n << " available)";
112  }
113  Moose::err << std::endl;
114 
115  for (unsigned int i = 0; i < n; ++i)
116  {
117  Moose::err << "[DBG][" << processor_id() << "] " << std::setprecision(15) << vec[i]._residual
118  << " '" << _sys.variable_name(vec[i]._var).c_str() << "' ";
119  if (vec[i]._is_scalar)
120  Moose::err << "(SCALAR)\n";
121  else
122  Moose::err << "at node " << vec[i]._nd << '\n';
123  }
124 }
static bool sortTopResidualData(TopResidualDebugOutputTopResidualData i, TopResidualDebugOutputTopResidualData j)
Method for sorting the residuals data from TopResidualDebugOutputTopResidualData structs.
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.
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...
System & _sys
Reference to libMesh system.
nl system()
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:2355
DofMap & dof_map
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:171
A structure for storing data related to top residuals.
virtual NumericVector< Number > & RHS()=0
InputParameters validParams< PetscOutput >()
Definition: PetscOutput.C:25
TopResidualDebugOutput(const InputParameters &parameters)
Class constructor.
PetscInt n
virtual MooseMesh & mesh() override
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...
ExecFlagType
Execution flags - when is the object executed/evaluated.
Definition: MooseTypes.h:90
InputParameters validParams< TopResidualDebugOutput >()
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:30
unsigned int _num_residuals
Number of residuals to display.