OGS 6.1.0-1721-g6382411ad
ConvergenceCriterionPerComponentDeltaX.cpp
Go to the documentation of this file.
1 
11 #include <logog/include/logog.hpp>
12 #include <limits>
13 
14 #include "BaseLib/ConfigTree.h"
17 
18 namespace NumLib
19 {
21  std::vector<double>&& absolute_tolerances,
22  std::vector<double>&& relative_tolerances,
23  const MathLib::VecNormType norm_type)
25  _abstols(std::move(absolute_tolerances)),
26  _reltols(std::move(relative_tolerances))
27 {
28  if (_abstols.size() != _reltols.size())
29  OGS_FATAL(
30  "The number of absolute and relative tolerances given must be the "
31  "same.");
32 
33  if (_abstols.empty())
34  OGS_FATAL("The given tolerances vector is empty.");
35 }
36 
38  const GlobalVector& minus_delta_x, GlobalVector const& x)
39 {
40  if ((!_dof_table) || (!_mesh))
41  OGS_FATAL("D.o.f. table or mesh have not been set.");
42 
43  bool satisfied_abs = true;
44  bool satisfied_rel = true;
45 
46  for (unsigned global_component = 0; global_component < _abstols.size();
47  ++global_component)
48  {
49  // TODO short cut if tol <= 0.0
50  auto error_dx = norm(minus_delta_x, global_component, _norm_type,
51  *_dof_table, *_mesh);
52  auto norm_x =
53  norm(x, global_component, _norm_type, *_dof_table, *_mesh);
54 
55  INFO(
56  "Convergence criterion, component %u: |dx|=%.4e, |x|=%.4e, "
57  "|dx|/|x|=%.4e",
58  error_dx, global_component, norm_x,
59  (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
60  : (error_dx / norm_x)));
61 
62  satisfied_abs = satisfied_abs && error_dx < _abstols[global_component];
63  satisfied_rel =
64  satisfied_rel && checkRelativeTolerance(_reltols[global_component],
65  error_dx, norm_x);
66  }
67 
68  _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
69 }
70 
72  const LocalToGlobalIndexMap& dof_table, MeshLib::Mesh const& mesh)
73 {
74  _dof_table = &dof_table;
75  _mesh = &mesh;
76 
78  static_cast<int>(_abstols.size()))
79  OGS_FATAL(
80  "The number of components in the DOF table and the number of "
81  "tolerances given do not match.");
82 }
83 
84 std::unique_ptr<ConvergenceCriterionPerComponentDeltaX>
86 {
88  config.checkConfigParameter("type", "PerComponentDeltaX");
89 
90  auto abstols =
92  config.getConfigParameterOptional<std::vector<double>>("abstols");
93  auto reltols =
95  config.getConfigParameterOptional<std::vector<double>>("reltols");
96  auto const norm_type_str =
98  config.getConfigParameter<std::string>("norm_type");
99 
100  if ((!abstols) && (!reltols))
101  OGS_FATAL(
102  "At least one of absolute or relative tolerance has to be "
103  "specified.");
104  if (!abstols) {
105  abstols = std::vector<double>(reltols->size());
106  } else if (!reltols) {
107  reltols = std::vector<double>(abstols->size());
108  }
109 
110  auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
111 
112  if (norm_type == MathLib::VecNormType::INVALID)
113  OGS_FATAL("Unknown vector norm type `%s'.", norm_type_str.c_str());
114 
115  return std::make_unique<ConvergenceCriterionPerComponentDeltaX>(
116  std::move(*abstols), std::move(*reltols), norm_type);
117 }
118 
119 } // NumLib
VecNormType
Norm type. Not declared as class type in order to use the members as integers.
Definition: LinAlgEnums.h:20
void checkDeltaX(const GlobalVector &minus_delta_x, GlobalVector const &x) override
T getConfigParameter(std::string const &param) const
void setDOFTable(const LocalToGlobalIndexMap &dof_table, MeshLib::Mesh const &mesh) override
Sets the d.o.f. table used to extract data for a specific component.
void checkConfigParameter(std::string const &param, T const &value) const
bool checkRelativeTolerance(const double reltol, const double numerator, const double denominator)
ConvergenceCriterionPerComponentDeltaX(std::vector< double > &&absolute_tolerances, std::vector< double > &&relative_tolerances, const MathLib::VecNormType norm_type)
std::unique_ptr< ConvergenceCriterionPerComponentDeltaX > createConvergenceCriterionPerComponentDeltaX(const BaseLib::ConfigTree &config)
boost::optional< T > getConfigParameterOptional(std::string const &param) const
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
const MathLib::VecNormType _norm_type
double norm(GlobalVector const &x, unsigned const global_component, MathLib::VecNormType norm_type, LocalToGlobalIndexMap const &dof_table, MeshLib::Mesh const &mesh)
VecNormType convertStringToVecNormType(const std::string &str)
convert string to VecNormType
Definition: LinAlgEnums.cpp:28