OGS
ConvergenceCriterionDeltaX.cpp
Go to the documentation of this file.
1
12
13#include "BaseLib/ConfigTree.h"
14#include "BaseLib/Logging.h"
16
17namespace NumLib
18{
20 std::optional<double>&& absolute_tolerance,
21 std::optional<double>&& relative_tolerance,
22 const MathLib::VecNormType norm_type)
23 : ConvergenceCriterion(norm_type),
24 _abstol(std::move(absolute_tolerance)),
25 _reltol(std::move(relative_tolerance))
26{
27 if ((!_abstol) && (!_reltol))
28 {
30 "At least one of absolute or relative tolerance has to be "
31 "specified.");
32 }
33}
34
36 GlobalVector const& x)
37{
38 auto error_dx = MathLib::LinAlg::norm(minus_delta_x, _norm_type);
39 auto norm_x = MathLib::LinAlg::norm(x, _norm_type);
40
41 INFO("Convergence criterion: |dx|={:.4e}, |x|={:.4e}, |dx|/|x|={:.4e}",
42 error_dx, norm_x,
43 (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
44 : (error_dx / norm_x)));
45
46 bool satisfied_abs = false;
47 bool satisfied_rel = false;
48
49 if (_abstol)
50 {
51 satisfied_abs = error_dx < *_abstol;
52 }
53 if (_reltol)
54 {
55 satisfied_rel = checkRelativeTolerance(*_reltol, error_dx, norm_x);
56 }
57
58 _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
59}
60
61std::unique_ptr<ConvergenceCriterionDeltaX> createConvergenceCriterionDeltaX(
62 const BaseLib::ConfigTree& config)
63{
65 config.checkConfigParameter("type", "DeltaX");
66
68 auto abstol = config.getConfigParameterOptional<double>("abstol");
70 auto reltol = config.getConfigParameterOptional<double>("reltol");
71 auto const norm_type_str =
73 config.getConfigParameter<std::string>("norm_type");
74 auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
75
76 if (norm_type == MathLib::VecNormType::INVALID)
77 {
78 OGS_FATAL("Unknown vector norm type `{:s}'.", norm_type_str);
79 }
80
81 return std::make_unique<ConvergenceCriterionDeltaX>(
82 std::move(abstol), std::move(reltol), norm_type);
83}
84
85} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
void checkConfigParameter(std::string const &param, std::string_view const value) const
Global vector based on Eigen vector.
Definition EigenVector.h:25
void checkDeltaX(const GlobalVector &minus_delta_x, GlobalVector const &x) override
ConvergenceCriterionDeltaX(std::optional< double > &&absolute_tolerance, std::optional< double > &&relative_tolerance, const MathLib::VecNormType norm_type)
const MathLib::VecNormType _norm_type
double norm(MatrixOrVector const &x, MathLib::VecNormType type)
Definition LinAlg.h:96
VecNormType convertStringToVecNormType(const std::string &str)
convert string to VecNormType
bool checkRelativeTolerance(const double reltol, const double numerator, const double denominator)
std::unique_ptr< ConvergenceCriterionDeltaX > createConvergenceCriterionDeltaX(const BaseLib::ConfigTree &config)