Criteria.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 
8 #include "Options/Options.hpp"
9 #include "Utilities/TMPL.hpp"
10 
11 /// \cond
12 namespace PUP {
13 class er;
14 } // namespace PUP
15 /// \endcond
16 
17 namespace Convergence {
18 
19 /*!
20  * \brief Criteria that determine an iterative algorithm has converged
21  *
22  * \details Most criteria are based on a residual magnitude
23  * \f$r_k\f$ after completion of an iteration \f$k\f$ (see, for instance, the
24  * \ref LinearSolverGroup documentation, `LinearSolver::Tags::Residual` and
25  * `LinearSolver::Tags::Magnitude`).
26  *
27  * The following criteria are implemented, ordered from highest to lowest
28  * priority:
29  *
30  * - AbsoluteResidual: Matches if the residual has reached this magnitude.
31  * - RelativeResidual: Matches if the residual has decreased by this factor,
32  * relative to the start of the first iteration.
33  * - MaxIterations: Matches if the number of iterations exceeds this limit.
34  */
35 struct Criteria {
36  static constexpr OptionString help =
37  "The algorithm terminates when any of these criteria is matched.";
38 
39  struct MaxIterations {
40  using type = size_t;
41  static constexpr OptionString help = {
42  "The number of iterations exceeds this limit."};
43  static type lower_bound() noexcept { return 0; }
44  };
45 
47  using type = double;
48  static constexpr OptionString help = {
49  "The residual has reached this magnitude."};
50  static type lower_bound() noexcept { return 0.; }
51  };
52 
54  using type = double;
55  static constexpr OptionString help = {
56  "The residual has decreased by this factor."};
57  static type lower_bound() noexcept { return 0.; }
58  static type upper_bound() noexcept { return 1.; }
59  };
60 
61  using options = tmpl::list<MaxIterations, AbsoluteResidual, RelativeResidual>;
62 
63  Criteria() = default;
64  Criteria(size_t max_iterations_in, double absolute_residual_in,
65  double relative_residual_in) noexcept;
66 
67  void pup(PUP::er& p) noexcept; // NOLINT
68 
69  size_t max_iterations{};
70  double absolute_residual{};
71  double relative_residual{};
72 };
73 
74 bool operator==(const Criteria& lhs, const Criteria& rhs) noexcept;
75 bool operator!=(const Criteria& lhs, const Criteria& rhs) noexcept;
76 
77 } // namespace Convergence
Definition: Strahlkorper.hpp:14
Defines classes and functions for making classes creatable from input files.
Definition: Criteria.hpp:46
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:27
Definition: Criteria.hpp:53
Criteria that determine an iterative algorithm has converged.
Definition: Criteria.hpp:35
Definition: Criteria.cpp:8
Wraps the template metaprogramming library used (brigand)
Definition: Criteria.hpp:39