https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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())
50 "The parameters 'postprocessors', 'descriptions', and 'tolerances' must be the same size.");
51}
52
53void
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
87unsigned 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
97std::string
98MultiPostprocessorConvergence::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}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
registerMooseObject("MooseApp", MultiPostprocessorConvergence)
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
void verboseOutput(std::ostringstream &oss)
Outputs the stream to the console if verbose output is enabled.
Definition Convergence.C:51
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition Convergence.h:34
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.
Checks convergence based on the iteration count.
static InputParameters validParams()
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
Converges if multiple post-processors are all less than tolerances.
unsigned int getMaxDescriptionLength() const
Returns the maximum description length.
std::string comparisonLine(const std::string &description, const Real err, const Real tol) const
Generates a colored line for a tolerance comparison.
unsigned int _max_desc_length
Maximum description length.
MultiPostprocessorConvergence(const InputParameters &parameters)
const std::vector< Real > & _tolerances
Tolerance for each Postprocessor.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job.
virtual MooseConvergenceStatus checkConvergenceInner(unsigned int n_iter) override
Inner check of convergence.
std::vector< const PostprocessorValue * > _pp_values
Postprocessor values.
std::vector< std::string > _descriptions
Description of each Postprocessor.
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
Retrieve the value of the Postprocessor.