OGS 6.2.0-97-g4a610c866
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  {
28  OGS_FATAL(
29  "At least one of absolute or relative tolerance has to be "
30  "specified.");
31  }
32 }
33 
34 void ConvergenceCriterionDeltaX::checkDeltaX(const GlobalVector& minus_delta_x,
35  GlobalVector const& x)
36 {
37  auto error_dx = MathLib::LinAlg::norm(minus_delta_x, _norm_type);
38  auto norm_x = MathLib::LinAlg::norm(x, _norm_type);
39 
40  INFO("Convergence criterion: |dx|=%.4e, |x|=%.4e, |dx|/|x|=%.4e", error_dx,
41  norm_x,
42  (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
43  : (error_dx / norm_x)));
44 
45  bool satisfied_abs = false;
46  bool satisfied_rel = false;
47 
48  if (_abstol) {
49  satisfied_abs = error_dx < *_abstol;
50  }
51  if (_reltol) {
52  satisfied_rel = checkRelativeTolerance(*_reltol, error_dx, norm_x);
53  }
54 
55  _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
56 }
57 
58 std::unique_ptr<ConvergenceCriterionDeltaX> createConvergenceCriterionDeltaX(
59  const BaseLib::ConfigTree& config)
60 {
62  config.checkConfigParameter("type", "DeltaX");
63 
65  auto abstol = config.getConfigParameterOptional<double>("abstol");
67  auto reltol = config.getConfigParameterOptional<double>("reltol");
68  auto const norm_type_str =
70  config.getConfigParameter<std::string>("norm_type");
71  auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
72 
73  if (norm_type == MathLib::VecNormType::INVALID)
74  {
75  OGS_FATAL("Unknown vector norm type `%s'.", norm_type_str.c_str());
76  }
77 
78  return std::make_unique<ConvergenceCriterionDeltaX>(
79  std::move(abstol), std::move(reltol), norm_type);
80 }
81 
82 } // namespace 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:63
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