OGS 6.1.0-1721-g6382411ad
ConvergenceCriterionDeltaX.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 
32 void ConvergenceCriterionDeltaX::checkDeltaX(const GlobalVector& minus_delta_x,
33  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  bool satisfied_abs = false;
44  bool satisfied_rel = false;
45 
46  if (_abstol) {
47  satisfied_abs = error_dx < *_abstol;
48  }
49  if (_reltol) {
50  satisfied_rel = checkRelativeTolerance(*_reltol, error_dx, norm_x);
51  }
52 
53  _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
54 }
55 
56 std::unique_ptr<ConvergenceCriterionDeltaX> createConvergenceCriterionDeltaX(
57  const BaseLib::ConfigTree& config)
58 {
60  config.checkConfigParameter("type", "DeltaX");
61 
63  auto abstol = config.getConfigParameterOptional<double>("abstol");
65  auto reltol = config.getConfigParameterOptional<double>("reltol");
66  auto const norm_type_str =
68  config.getConfigParameter<std::string>("norm_type");
69  auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
70 
71  if (norm_type == MathLib::VecNormType::INVALID)
72  OGS_FATAL("Unknown vector norm type `%s'.", norm_type_str.c_str());
73 
74  return std::make_unique<ConvergenceCriterionDeltaX>(
75  std::move(abstol), std::move(reltol), norm_type);
76 }
77 
78 } // NumLib
void checkDeltaX(const GlobalVector &minus_delta_x, GlobalVector const &x) override
VecNormType
Norm type. Not declared as class type in order to use the members as integers.
Definition: LinAlgEnums.h:20
const boost::optional< double > _reltol
T getConfigParameter(std::string const &param) const
const boost::optional< double > _abstol
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)
ConvergenceCriterionDeltaX(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
std::unique_ptr< ConvergenceCriterionDeltaX > createConvergenceCriterionDeltaX(const BaseLib::ConfigTree &config)
VecNormType convertStringToVecNormType(const std::string &str)
convert string to VecNormType
Definition: LinAlgEnums.cpp:28