https://mooseframework.inl.gov
IterationCountConvergence.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 "FixedPointSolve.h"
12 
14 
17 {
19 
20  params.addParam<unsigned int>("min_iterations", 0, "Minimum number of iterations");
21  params.addParam<unsigned int>("max_iterations", 50, "Maximum number of iterations");
22  params.addParam<bool>(
23  "converge_at_max_iterations", false, "Converge at 'max_iterations' instead of diverging");
24 
25  params.addClassDescription("Checks the iteration count.");
26 
27  return params;
28 }
29 
31  : Convergence(parameters),
32  _min_iterations(getParam<unsigned int>("min_iterations")),
33  _max_iterations(getParam<unsigned int>("max_iterations")),
34  _converge_at_max_iterations(getParam<bool>("converge_at_max_iterations"))
35 {
37  mooseError("'max_iterations' must be >= 'min_iterations'.");
38 }
39 
42 {
43  const auto status_inner = checkConvergenceInner(iter);
44 
45  std::ostringstream oss;
46  switch (status_inner)
47  {
49  if (iter >= _max_iterations)
50  {
52  {
53  oss << "Converged due to iterations (" << iter << ") >= max iterations ("
54  << _max_iterations << ") and 'converge_at_max_iterations' = 'true'.";
55  verboseOutput(oss);
57  }
58  else
59  {
60  oss << "Diverged due to iterations (" << iter << ") >= max iterations ("
61  << _max_iterations << ").";
62  verboseOutput(oss);
64  }
65  }
66  else
68  break;
69 
71  if (iter < _min_iterations)
72  {
73  oss << "Still iterating because iterations (" << iter << ") < min iterations ("
74  << _min_iterations << ").";
75  verboseOutput(oss);
77  }
78  else
80  break;
81 
84  break;
85 
86  default:
87  mooseError("Invalid convergence status");
88  }
89 }
90 
93 {
95 }
virtual MooseConvergenceStatus checkConvergenceInner(unsigned int iter)
Inner check of convergence.
virtual MooseConvergenceStatus checkConvergence(unsigned int iter) override final
Returns convergence status.
registerMooseObject("MooseApp", IterationCountConvergence)
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...
static InputParameters validParams()
Base class for convergence criteria.
Definition: Convergence.h:21
const unsigned int _min_iterations
Minimum number of iterations.
MooseConvergenceStatus
Status returned by calls to checkConvergence.
Definition: Convergence.h:33
static InputParameters validParams()
Definition: Convergence.C:16
IterationCountConvergence(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...
const bool _converge_at_max_iterations
Whether to converge at the maximum number of iterations.
const unsigned int _max_iterations
Maximum number of iterations.
void ErrorVector unsigned int
Checks convergence based on the iteration count.