https://mooseframework.inl.gov
MultiPostprocessorConvergence.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 
13 
16 {
18 
19  params.addClassDescription("Converges if multiple post-processors are all less than tolerances.");
20 
21  params.addRequiredParam<std::vector<PostprocessorName>>("postprocessors",
22  "Postprocessors to check");
23  params.addParam<std::vector<std::string>>(
24  "descriptions",
25  "Description of each Postprocessor. If not provided, the Postprocessor names are used as "
26  "their descriptions.");
27  params.addRequiredParam<std::vector<Real>>("tolerances", "Tolerance for each Postprocessor");
28 
29  return params;
30 }
31 
33  : IterationCountConvergence(parameters), _tolerances(getParam<std::vector<Real>>("tolerances"))
34 {
35  const auto & pp_names = getParam<std::vector<PostprocessorName>>("postprocessors");
36 
37  if (isParamValid("descriptions"))
38  _descriptions = getParam<std::vector<std::string>>("descriptions");
39  else
40  {
41  for (const auto & pp_name : pp_names)
42  _descriptions.push_back(pp_name);
43  }
44 
45  for (const auto & pp_name : pp_names)
46  _pp_values.push_back(&getPostprocessorValueByName(pp_name));
47 
48  if (_descriptions.size() != pp_names.size() || _tolerances.size() != pp_names.size())
49  mooseError(
50  "The parameters 'postprocessors', 'descriptions', and 'tolerances' must be the same size.");
51 }
52 
53 void
55 {
57 
59 }
60 
63 {
64  bool all_converged = true;
65  std::ostringstream oss;
66  oss << "\n";
67 
68  for (const auto i : index_range(_descriptions))
69  {
70  const Real abs_pp_value = std::abs(*_pp_values[i]);
71  if (abs_pp_value > _tolerances[i])
72  all_converged = false;
73 
74  std::string desc = _descriptions[i];
75  desc.resize(_max_desc_length + 1, ' '); // pad with spaces for alignment
76  oss << comparisonLine(desc, abs_pp_value, _tolerances[i]);
77  }
78 
79  verboseOutput(oss);
80 
81  if (all_converged)
83  else
85 }
86 
87 unsigned int
89 {
90  std::size_t max_desc_length = 0;
91  for (const auto & description : _descriptions)
92  max_desc_length = std::max(max_desc_length, description.length());
93 
94  return max_desc_length;
95 }
96 
97 std::string
98 MultiPostprocessorConvergence::comparisonLine(const std::string & description,
99  const Real err,
100  const Real tol) const
101 {
102  std::string color, compare_str;
103  if (std::abs(err) > tol)
104  {
105  color = COLOR_RED;
106  compare_str = ">";
107  }
108  else
109  {
110  color = COLOR_GREEN;
111  compare_str = "<";
112  }
113 
114  std::ostringstream oss;
115  oss << " " << description << ": " << color << std::scientific << std::setprecision(5)
116  << std::abs(err) << " " << compare_str << " " << tol << COLOR_DEFAULT << "\n";
117  return oss.str();
118 }
std::vector< const PostprocessorValue * > _pp_values
Postprocessor values.
OStreamProxy err
const std::vector< Real > & _tolerances
Tolerance for each Postprocessor.
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:50
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
Definition: Convergence.h:45
unsigned int _max_desc_length
Maximum description length.
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
void verboseOutput(std::ostringstream &oss)
Outputs the stream to the console if verbose output is enabled.
Definition: Convergence.C:51
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
unsigned int getMaxDescriptionLength() const
Returns the maximum description length.
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...
auto max(const L &left, const R &right)
MultiPostprocessorConvergence(const InputParameters &parameters)
static InputParameters validParams()
std::string comparisonLine(const std::string &description, const Real err, const Real tol) const
Generates a colored line for a tolerance comparison.
registerMooseObject("MooseApp", MultiPostprocessorConvergence)
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition: Convergence.h:33
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
Retrieve the value of the Postprocessor.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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:281
Converges if multiple post-processors are all less than tolerances.
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 MooseConvergenceStatus checkConvergenceInner(unsigned int iter) override
Inner check of convergence.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:209
std::vector< std::string > _descriptions
Description of each Postprocessor.
auto index_range(const T &sizable)
Checks convergence based on the iteration count.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...