https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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 params.addParam<bool>("include_scaling_factor",
37 false,
38 "If set to true, include the residual scaling factor in the norm; "
39 "otherwise, divide by the scaling factor");
40
41 params.addClassDescription("Computes a discrete norm for a block-restricted variable residual.");
42
43 return params;
44}
45
47 : ElementPostprocessor(parameters),
48 _var(_fe_problem.getVariable(_tid,
49 getParam<VariableName>("variable"),
50 Moose::VarKindType::VAR_SOLVER,
51 Moose::VarFieldType::VAR_FIELD_STANDARD)),
52 _norm_type(getParam<MooseEnum>("norm_type").getEnum<NormType>()),
53 _correct_mesh_bias(getParam<bool>("correct_mesh_bias")),
54 _include_scaling_factor(getParam<bool>("include_scaling_factor")),
55 _nl_residual_vector(_fe_problem.getNonlinearSystemBase(_sys.number()).RHS())
56{
57}
58
59void
66
67void
69{
70 for (const auto dof_index : _var.dofIndices())
71 {
72 // Dof indices may not be owned by the same processor as the current element
73 if (_var.dofMap().local_index(dof_index))
74 _local_dof_indices.insert(dof_index);
75 else
76 {
77 // if a Dof is non-local add it to a map, to be communicated to the owner in finalize()
78 const auto dof_owner = _var.dofMap().dof_owner(dof_index);
79 _nonlocal_dof_indices_map[dof_owner].push_back(dof_index);
80 }
81 }
82}
83
84void
86{
87 const auto & pps = static_cast<const DiscreteVariableResidualNorm &>(y);
88 _local_dof_indices.insert(pps._local_dof_indices.begin(), pps._local_dof_indices.end());
89}
90
91void
93{
94 // communicate the non-local Dofs to their processors
95 auto receive_functor = [&](processor_id_type /*pid*/, const std::vector<dof_id_type> & indices)
96 { _local_dof_indices.insert(indices.begin(), indices.end()); };
97 Parallel::push_parallel_vector_data(_communicator, _nonlocal_dof_indices_map, receive_functor);
98
99 // compute the total number of Dofs for the variable on the subdomain
100 auto n_dofs = _local_dof_indices.size();
101 gatherSum(n_dofs);
102
103 Real bias = 1.0;
104 switch (_norm_type)
105 {
106 case NormType::l_1:
108 bias = n_dofs;
109 break;
110 case NormType::l_2:
112 bias = sqrt(n_dofs);
113 break;
114 case NormType::l_inf:
116 break;
117 default:
118 mooseError("Invalid norm type");
119 }
120
122 _norm /= bias;
123
126}
127
130{
131 return _norm;
132}
registerMooseObject("MooseApp", DiscreteVariableResidualNorm)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Real PostprocessorValue
various MOOSE typedefs
Definition MooseTypes.h:230
Computes a discrete norm for a block-restricted variable residual.
DiscreteVariableResidualNorm(const InputParameters &parameters)
virtual void threadJoin(const UserObject &y) override
Must override.
const bool _correct_mesh_bias
If true, correct mesh-size bias in norm.
NumericVector< Number > & _nl_residual_vector
Nonlinear residual vector.
virtual PostprocessorValue getValue() const override
This will get called to actually grab the final value the postprocessor has calculated.
virtual void initialize() override
Called before execute() is ever called so that data can be cleared.
std::set< dof_id_type > _local_dof_indices
Local DoF indices for the variable, block-restricted.
const bool _include_scaling_factor
If false, divide by residual scaling factor.
std::map< processor_id_type, std::vector< dof_id_type > > _nonlocal_dof_indices_map
Non-local DoF indices map, indexed by the owning PID.
virtual void execute() override
Execute method.
MooseVariableFieldBase & _var
The variable we compute the residual for.
const NormType _norm_type
Type of norm to compute.
virtual void finalize() override
This is called after execute() and after threadJoin()! This is probably where you want to do MPI comm...
Real _norm
The computed residual norm.
static InputParameters validParams()
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
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.
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...
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.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
virtual const std::vector< dof_id_type > & dofIndices() const
Get local DoF indices.
const libMesh::DofMap & dofMap() const
The DofMap associated with the system this variable is in.
void scalingFactor(const std::vector< Real > &factor)
Set the scaling factor for this variable.
void gatherSum(T &value)
Gather the parallel sum of the variable passed in.
Base class for user-specific data.
Definition UserObject.h:20
processor_id_type dof_owner(const dof_id_type dof) const
bool local_index(dof_id_type dof_index) const
virtual Real subset_l1_norm(const std::set< numeric_index_type > &indices) const
virtual Real subset_l2_norm(const std::set< numeric_index_type > &indices) const
virtual Real subset_linfty_norm(const std::set< numeric_index_type > &indices) const
const Parallel::Communicator & _communicator
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...