OGS 6.2.1-97-g73d1aeda3
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  {
30  OGS_FATAL(
31  "The number of absolute and relative tolerances given must be the "
32  "same.");
33  }
34 
35  if (_abstols.empty())
36  {
37  OGS_FATAL("The given tolerances vector is empty.");
38  }
39 }
40 
42  const GlobalVector& minus_delta_x, GlobalVector const& x)
43 {
44  if ((!_dof_table) || (!_mesh))
45  {
46  OGS_FATAL("D.o.f. table or mesh have not been set.");
47  }
48 
49  bool satisfied_abs = true;
50  bool satisfied_rel = true;
51 
52  for (unsigned global_component = 0; global_component < _abstols.size();
53  ++global_component)
54  {
55  // TODO short cut if tol <= 0.0
56  auto error_dx = norm(minus_delta_x, global_component, _norm_type,
57  *_dof_table, *_mesh);
58  auto norm_x =
59  norm(x, global_component, _norm_type, *_dof_table, *_mesh);
60 
61  INFO(
62  "Convergence criterion, component %u: |dx|=%.4e, |x|=%.4e, "
63  "|dx|/|x|=%.4e",
64  global_component, error_dx, norm_x,
65  (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
66  : (error_dx / norm_x)));
67 
68  satisfied_abs = satisfied_abs && error_dx < _abstols[global_component];
69  satisfied_rel =
70  satisfied_rel && checkRelativeTolerance(_reltols[global_component],
71  error_dx, norm_x);
72  }
73 
74  _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
75 }
76 
78  const LocalToGlobalIndexMap& dof_table, MeshLib::Mesh const& mesh)
79 {
80  _dof_table = &dof_table;
81  _mesh = &mesh;
82 
84  static_cast<int>(_abstols.size()))
85  {
86  OGS_FATAL(
87  "The number of components in the DOF table and the number of "
88  "tolerances given do not match.");
89  }
90 }
91 
92 std::unique_ptr<ConvergenceCriterionPerComponentDeltaX>
94 {
96  config.checkConfigParameter("type", "PerComponentDeltaX");
97 
98  auto abstols =
100  config.getConfigParameterOptional<std::vector<double>>("abstols");
101  auto reltols =
103  config.getConfigParameterOptional<std::vector<double>>("reltols");
104  auto const norm_type_str =
106  config.getConfigParameter<std::string>("norm_type");
107 
108  if ((!abstols) && (!reltols))
109  {
110  OGS_FATAL(
111  "At least one of absolute or relative tolerance has to be "
112  "specified.");
113  }
114  if (!abstols) {
115  abstols = std::vector<double>(reltols->size());
116  } else if (!reltols) {
117  reltols = std::vector<double>(abstols->size());
118  }
119 
120  auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
121 
122  if (norm_type == MathLib::VecNormType::INVALID)
123  {
124  OGS_FATAL("Unknown vector norm type `%s'.", norm_type_str.c_str());
125  }
126 
127  return std::make_unique<ConvergenceCriterionPerComponentDeltaX>(
128  std::move(*abstols), std::move(*reltols), norm_type);
129 }
130 
131 } // namespace 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:63
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