OGS 6.1.0-1721-g6382411ad
ConvergenceCriterionResidual.cpp
Go to the documentation of this file.
1 
11 #include <logog/include/logog.hpp>
12 
13 #include "BaseLib/ConfigTree.h"
14 #include "MathLib/LinAlg/LinAlg.h"
15 
16 namespace NumLib
17 {
19  boost::optional<double>&& absolute_tolerance,
20  boost::optional<double>&& relative_tolerance,
21  const MathLib::VecNormType norm_type)
22  : ConvergenceCriterion(norm_type),
23  _abstol(std::move(absolute_tolerance)),
24  _reltol(std::move(relative_tolerance))
25 {
26  if ((!_abstol) && (!_reltol))
27  OGS_FATAL(
28  "At least one of absolute or relative tolerance has to be "
29  "specified.");
30 }
31 
33  const GlobalVector& minus_delta_x, GlobalVector const& x)
34 {
35  auto error_dx = MathLib::LinAlg::norm(minus_delta_x, _norm_type);
36  auto norm_x = MathLib::LinAlg::norm(x, _norm_type);
37 
38  INFO("Convergence criterion: |dx|=%.4e, |x|=%.4e, |dx|/|x|=%.4e", error_dx,
39  norm_x,
40  (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
41  : (error_dx / norm_x)));
42 }
43 
44 void ConvergenceCriterionResidual::checkResidual(const GlobalVector& residual)
45 {
46  auto norm_res = MathLib::LinAlg::norm(residual, _norm_type);
47 
49  {
50  INFO("Convergence criterion: |r0|=%.4e", norm_res);
51  _residual_norm_0 = norm_res;
52  }
53  else
54  {
56  (_residual_norm_0 < std::numeric_limits<double>::epsilon())
57  ? norm_res
59  if (_residual_norm_0 < std::numeric_limits<double>::epsilon())
60  {
61  INFO("Convergence criterion: |r|=%.4e |r0|=%.4e", norm_res,
63  }
64  else
65  {
66  INFO("Convergence criterion: |r|=%.4e |r0|=%.4e |r|/|r0|=%.4e",
67  norm_res, _residual_norm_0,
68  (_residual_norm_0 == 0.
69  ? std::numeric_limits<double>::quiet_NaN()
70  : (norm_res / _residual_norm_0)));
71  }
72  }
73 
74  bool satisfied_abs = false;
75  bool satisfied_rel = false;
76 
77  if (_abstol)
78  {
79  satisfied_abs = norm_res < *_abstol;
80  }
82  {
83  satisfied_rel =
85  }
86 
87  _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
88 }
89 
90 std::unique_ptr<ConvergenceCriterionResidual>
92 {
94  config.checkConfigParameter("type", "Residual");
95 
97  auto abstol = config.getConfigParameterOptional<double>("abstol");
99  auto reltol = config.getConfigParameterOptional<double>("reltol");
100  auto const norm_type_str =
102  config.getConfigParameter<std::string>("norm_type");
103  auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
104 
105  if (norm_type == MathLib::VecNormType::INVALID)
106  OGS_FATAL("Unknown vector norm type `%s'.", norm_type_str.c_str());
107 
108  return std::make_unique<ConvergenceCriterionResidual>(
109  std::move(abstol), std::move(reltol), norm_type);
110 }
111 
112 } // NumLib
void checkDeltaX(const GlobalVector &minus_delta_x, GlobalVector const &x) override
std::unique_ptr< ConvergenceCriterionResidual > createConvergenceCriterionResidual(const BaseLib::ConfigTree &config)
VecNormType
Norm type. Not declared as class type in order to use the members as integers.
Definition: LinAlgEnums.h:20
T getConfigParameter(std::string const &param) const
void checkResidual(const GlobalVector &residual) override
Check if the residual satisfies the convergence criterion.
double norm(MatrixOrVector const &x, MathLib::VecNormType type)
Definition: LinAlg.h:87
void checkConfigParameter(std::string const &param, T const &value) const
bool checkRelativeTolerance(const double reltol, const double numerator, const double denominator)
ConvergenceCriterionResidual(boost::optional< double > &&absolute_tolerance, boost::optional< double > &&relative_tolerance, const MathLib::VecNormType norm_type)
boost::optional< T > getConfigParameterOptional(std::string const &param) const
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
const MathLib::VecNormType _norm_type
VecNormType convertStringToVecNormType(const std::string &str)
convert string to VecNormType
Definition: LinAlgEnums.cpp:28