OGS
ConvergenceCriterionPerComponentResidual.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
7#include "BaseLib/Logging.h"
11
12namespace NumLib
13{
16 std::vector<double>&& absolute_tolerances,
17 std::vector<double>&& relative_tolerances,
18 std::vector<double>&& damping_alpha,
19 bool damping_alpha_switch,
20 const MathLib::VecNormType norm_type)
22 _abstols(std::move(absolute_tolerances)),
23 _reltols(std::move(relative_tolerances)),
25 _damping_alpha(std::move(damping_alpha)),
26 _damping_alpha_switch(damping_alpha_switch)
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)
45 {
46 OGS_FATAL("D.o.f. table has not been set.");
47 }
48
49 for (unsigned global_component = 0; global_component < _abstols.size();
50 ++global_component)
51 {
52 // TODO short cut if tol <= 0.0
53 auto error_dx =
54 norm(minus_delta_x, global_component, _norm_type, *_dof_table);
55 auto norm_x = norm(x, global_component, _norm_type, *_dof_table);
56
57 INFO(
58 "Convergence criterion, component {:d}: |dx|={:.4e}, |x|={:.4e}, "
59 "|dx|/|x|={:.4e}",
60 global_component, error_dx, norm_x,
61 (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
62 : (error_dx / norm_x)));
63 }
64}
65
67 const GlobalVector& residual)
68{
69 if (!_dof_table)
70 {
71 OGS_FATAL("D.o.f. table has not been set.");
72 }
73
74 for (unsigned global_component = 0; global_component < _abstols.size();
75 ++global_component)
76 {
77 // TODO short cut if tol <= 0.0
78 auto norm_res =
79 norm(residual, global_component, _norm_type, *_dof_table);
80
82 {
83 INFO("Convergence criterion, component {:d}: |r0|={:.4e}",
84 global_component, norm_res);
85 _residual_norms_0[global_component] = norm_res;
86 }
87 else
88 {
89 auto const norm_res0 = _residual_norms_0[global_component];
90 INFO(
91 "Convergence criterion, component {:d}: |r|={:.4e}, "
92 "|r0|={:.4e}, |r|/|r0|={:.4e}",
93 global_component, norm_res, norm_res0,
94 (norm_res0 == 0. ? std::numeric_limits<double>::quiet_NaN()
95 : (norm_res / norm_res0)));
96 }
97
98 bool const satisfied_abs = norm_res < _abstols[global_component];
99 // Make sure that in the first iteration the relative residual tolerance
100 // is not satisfied.
101 bool const satisfied_rel =
103 checkRelativeTolerance(_reltols[global_component], norm_res,
104 _residual_norms_0[global_component]);
105 _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
106 }
107}
108
110 const GlobalVector& minus_delta_x, GlobalVector const& x,
111 double damping_orig)
112{
113 if ((!_dof_table) || (!_mesh))
114 {
115 OGS_FATAL("D.o.f. table or mesh have not been set.");
116 }
117
119 double damping_final = 1;
120 for (unsigned global_component = 0;
121 global_component < _damping_alpha.size();
122 ++global_component)
123 {
124 auto const& ms = _dof_table->getMeshSubset(global_component);
125 assert(ms.getMeshID() == _mesh->getID());
126 DBUG("Non-negative damping for component: {:d} alpha: {:g}",
127 global_component, _damping_alpha[global_component]);
128 for (auto const& l :
130 {
131 auto index = _dof_table->getGlobalIndex(l, global_component);
132 damping_final = std::min(
133 damping_final,
134 damping_orig / std::max(1.0, (minus_delta_x.get(index) *
135 _damping_alpha[global_component] /
136 x.get(index))));
137 }
138 }
139 DBUG("Final damping value due to non-negative damping: {:g}",
140 damping_final);
141 return damping_final;
142}
143
145 const LocalToGlobalIndexMap& dof_table, MeshLib::Mesh const& mesh)
146{
147 _dof_table = &dof_table;
148 _mesh = &mesh;
149
150 if (_dof_table->getNumberOfGlobalComponents() !=
151 static_cast<int>(_abstols.size()))
152 {
153 OGS_FATAL(
154 "The number of components in the DOF table ({:d}) and the number "
155 "of tolerances given ({:d}) do not match.",
156 _dof_table->getNumberOfGlobalComponents(),
157 static_cast<int>(_abstols.size()));
158 }
159}
160
161std::unique_ptr<ConvergenceCriterionPerComponentResidual>
163 const BaseLib::ConfigTree& config)
164{
166 config.checkConfigParameter("type", "PerComponentResidual");
167
168 auto abstols =
170 config.getConfigParameterOptional<std::vector<double>>("abstols");
171 auto reltols =
173 config.getConfigParameterOptional<std::vector<double>>("reltols");
174 auto damping_alpha =
176 config.getConfigParameterOptional<std::vector<double>>("damping_alpha");
177 auto const norm_type_str =
179 config.getConfigParameter<std::string>("norm_type");
180
181 bool damping_alpha_switch = true;
182 if ((!abstols) && (!reltols))
183 {
184 OGS_FATAL(
185 "At least one of absolute or relative tolerance has to be "
186 "specified.");
187 }
188 if (!abstols)
189 {
190 abstols = std::vector<double>(reltols->size());
191 }
192 else if (!reltols)
193 {
194 reltols = std::vector<double>(abstols->size());
195 }
196 if (!damping_alpha)
197 {
198 damping_alpha = std::vector<double>(abstols->size(), 0.0);
199 damping_alpha_switch = false;
200 }
201
202 auto const norm_type = MathLib::convertStringToVecNormType(norm_type_str);
203
204 if (norm_type == MathLib::VecNormType::INVALID)
205 {
206 OGS_FATAL("Unknown vector norm type `{:s}'.", norm_type_str);
207 }
208
209 return std::make_unique<ConvergenceCriterionPerComponentResidual>(
210 std::move(*abstols), std::move(*reltols), std::move(*damping_alpha),
211 damping_alpha_switch, norm_type);
212}
213
214} // namespace NumLib
#define OGS_FATAL(...)
Definition Error.h:19
MathLib::EigenVector GlobalVector
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
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
double get(IndexType rowId) const
get entry
Definition EigenVector.h:52
ConvergenceCriterionPerComponentResidual(std::vector< double > &&absolute_tolerances, std::vector< double > &&relative_tolerances, std::vector< double > &&damping_alpha, bool daming_alpha_switch, const MathLib::VecNormType norm_type)
double getDampingFactor(GlobalVector const &minus_delta_x, GlobalVector const &x, double damping_orig) override
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 checkResidual(const GlobalVector &residual) override
Check if the residual satisfies the convergence criterion.
void checkDeltaX(const GlobalVector &minus_delta_x, GlobalVector const &x) override
ConvergenceCriterionPerComponent(const MathLib::VecNormType norm_type)
const MathLib::VecNormType _norm_type
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:20
VecNormType convertStringToVecNormType(const std::string &str)
convert string to VecNormType
auto meshLocations(Mesh const &mesh, MeshItemType const item_type)
Definition Mesh.h:227
bool checkRelativeTolerance(const double reltol, const double numerator, const double denominator)
std::unique_ptr< ConvergenceCriterionPerComponentResidual > createConvergenceCriterionPerComponentResidual(const BaseLib::ConfigTree &config)
double norm(GlobalVector const &x, unsigned const global_component, MathLib::VecNormType norm_type, LocalToGlobalIndexMap const &dof_table)