https://mooseframework.inl.gov
DiscreteVariableResidualNorm.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 
11 #include "MooseVariableFieldBase.h"
12 #include "NonlinearSystemBase.h"
13 
14 #include "libmesh/dof_map.h"
15 
17 
20 {
22 
23  params.addRequiredParam<VariableName>("variable",
24  "The name of the variable to compute the residual for");
25  MooseEnum norm_type("l_1=0 l_2=1 l_inf=2");
26  norm_type.addDocumentation("l_1", "l-1 norm");
27  norm_type.addDocumentation("l_2", "l-2 norm");
28  norm_type.addDocumentation("l_inf", "l-infinity norm");
29  params.addRequiredParam<MooseEnum>("norm_type", norm_type, "Type of discrete norm to compute");
30  params.addParam<bool>(
31  "correct_mesh_bias",
32  false,
33  "If set to true, correct the mesh size bias associated with the selected norm. For l-1, "
34  "divide by N, the number of block-restricted DoFs for the variable. For l-2, divide by "
35  "sqrt(N). For l-infinity, no correction needs to be made.");
36 
37  params.addClassDescription("Computes a discrete norm for a block-restricted variable residual.");
38 
39  return params;
40 }
41 
43  : ElementPostprocessor(parameters),
44  _var(_fe_problem.getVariable(_tid,
45  getParam<VariableName>("variable"),
48  _norm_type(getParam<MooseEnum>("norm_type").getEnum<NormType>()),
49  _correct_mesh_bias(getParam<bool>("correct_mesh_bias")),
50  _nl_residual_vector(_fe_problem.getNonlinearSystemBase(_sys.number()).RHS())
51 {
52 }
53 
54 void
56 {
57  _norm = 0;
58  _local_dof_indices.clear();
60 }
61 
62 void
64 {
65  for (const auto dof_index : _var.dofIndices())
66  {
67  // Dof indices may not be owned by the same processor as the current element
68  if (_var.dofMap().local_index(dof_index))
69  _local_dof_indices.insert(dof_index);
70  else
71  {
72  // if a Dof is non-local add it to a map, to be communicated to the owner in finalize()
73  const auto dof_owner = _var.dofMap().dof_owner(dof_index);
74  _nonlocal_dof_indices_map[dof_owner].push_back(dof_index);
75  }
76  }
77 }
78 
79 void
81 {
82  const auto & pps = static_cast<const DiscreteVariableResidualNorm &>(y);
83  _local_dof_indices.insert(pps._local_dof_indices.begin(), pps._local_dof_indices.end());
84 }
85 
86 void
88 {
89  // communicate the non-local Dofs to their processors
90  auto receive_functor = [&](processor_id_type /*pid*/, const std::vector<dof_id_type> & indices)
91  { _local_dof_indices.insert(indices.begin(), indices.end()); };
92  Parallel::push_parallel_vector_data(_communicator, _nonlocal_dof_indices_map, receive_functor);
93 
94  // compute the total number of Dofs for the variable on the subdomain
95  auto n_dofs = _local_dof_indices.size();
96  gatherSum(n_dofs);
97 
98  Real bias = 1.0;
99  switch (_norm_type)
100  {
101  case NormType::l_1:
103  bias = n_dofs;
104  break;
105  case NormType::l_2:
107  bias = sqrt(n_dofs);
108  break;
109  case NormType::l_inf:
111  break;
112  default:
113  mooseError("Invalid norm type");
114  }
115 
116  if (_correct_mesh_bias)
117  _norm /= bias;
118 }
119 
122 {
123  return _norm;
124 }
VarFieldType
Definition: MooseTypes.h:722
virtual Real subset_l2_norm(const std::set< numeric_index_type > &indices) const
virtual PostprocessorValue getValue() const override
This will get called to actually grab the final value the postprocessor has calculated.
processor_id_type dof_owner(const dof_id_type dof) const
const libMesh::DofMap & dofMap() const
The DofMap associated with the system this variable is in.
static InputParameters validParams()
virtual void execute() override
Execute method.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NumericVector< Number > & _nl_residual_vector
Nonlinear residual vector.
const Parallel::Communicator & _communicator
virtual void finalize() override
This is called after execute() and after threadJoin()! This is probably where you want to do MPI comm...
virtual Real subset_linfty_norm(const std::set< numeric_index_type > &indices) const
virtual Real subset_l1_norm(const std::set< numeric_index_type > &indices) const
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
MooseVariableFieldBase & _var
The variable we compute the residual for.
uint8_t processor_id_type
virtual void threadJoin(const UserObject &y) override
Must override.
const bool _correct_mesh_bias
If true, correct mesh-size bias in norm.
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Definition: UserObject.h:126
std::map< processor_id_type, std::vector< dof_id_type > > _nonlocal_dof_indices_map
Non-local DoF indices map, indexed by the owning PID.
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:715
Real PostprocessorValue
various MOOSE typedefs
Definition: MooseTypes.h:202
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
registerMooseObject("MooseApp", DiscreteVariableResidualNorm)
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
std::set< dof_id_type > _local_dof_indices
Local DoF indices for the variable, block-restricted.
DiscreteVariableResidualNorm(const InputParameters &parameters)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
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...
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
Computes a discrete norm for a block-restricted variable residual.
Base class for user-specific data.
Definition: UserObject.h:40
const NormType _norm_type
Type of norm to compute.
Real _norm
The computed residual norm.
bool local_index(dof_id_type dof_index) const