OGS
NumLib::ConvergenceCriterionPerComponentResidual Class Reference

Detailed Description

Convergence criterion applying absolute or relative tolerances individually to each component of the whole residual vector.

A check of the solution increment is not done. If both an absolute and a relative tolerances are specified, at least one of them has to be satisfied.

Definition at line 21 of file ConvergenceCriterionPerComponentResidual.h.

#include <ConvergenceCriterionPerComponentResidual.h>

Inheritance diagram for NumLib::ConvergenceCriterionPerComponentResidual:
[legend]
Collaboration diagram for NumLib::ConvergenceCriterionPerComponentResidual:
[legend]

Public Member Functions

 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)
bool hasDeltaXCheck () const override
bool hasResidualCheck () const override
bool hasNonNegativeDamping () const override
void checkDeltaX (const GlobalVector &minus_delta_x, GlobalVector const &x) override
void checkResidual (const GlobalVector &residual) override
 Check if the residual satisfies the convergence criterion.
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.
Public Member Functions inherited from NumLib::ConvergenceCriterionPerComponent
 ConvergenceCriterionPerComponent (const MathLib::VecNormType norm_type)
Public Member Functions inherited from NumLib::ConvergenceCriterion
 ConvergenceCriterion (const MathLib::VecNormType norm_type)
virtual void preFirstIteration ()
virtual void setNoFirstIteration ()
virtual void reset ()
virtual bool isSatisfied () const
 Tell if the convergence criterion is satisfied.
MathLib::VecNormType getVectorNormType () const
virtual ~ConvergenceCriterion ()=default

Private Attributes

const std::vector< double > _abstols
const std::vector< double > _reltols
LocalToGlobalIndexMap const * _dof_table = nullptr
MeshLib::Mesh const * _mesh = nullptr
std::vector< double > _residual_norms_0
const std::vector< double > _damping_alpha
bool _damping_alpha_switch

Additional Inherited Members

Protected Attributes inherited from NumLib::ConvergenceCriterion
bool _satisfied = true
bool _is_first_iteration = true
const MathLib::VecNormType _norm_type

Constructor & Destructor Documentation

◆ ConvergenceCriterionPerComponentResidual()

NumLib::ConvergenceCriterionPerComponentResidual::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 )

Definition at line 14 of file ConvergenceCriterionPerComponentResidual.cpp.

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 {
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}
#define OGS_FATAL(...)
Definition Error.h:19
ConvergenceCriterionPerComponent(const MathLib::VecNormType norm_type)

References NumLib::ConvergenceCriterionPerComponent::ConvergenceCriterionPerComponent(), _abstols, _damping_alpha, _damping_alpha_switch, _reltols, and _residual_norms_0.

Member Function Documentation

◆ checkDeltaX()

void NumLib::ConvergenceCriterionPerComponentResidual::checkDeltaX ( const GlobalVector & minus_delta_x,
GlobalVector const & x )
overridevirtual

The function will only do diagnostic output and no actual check of the solution increment is made

Implements NumLib::ConvergenceCriterion.

Definition at line 41 of file ConvergenceCriterionPerComponentResidual.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
const MathLib::VecNormType _norm_type
double norm(GlobalVector const &x, unsigned const global_component, MathLib::VecNormType norm_type, LocalToGlobalIndexMap const &dof_table)

References _abstols, _dof_table, NumLib::ConvergenceCriterion::_norm_type, INFO(), NumLib::norm(), and OGS_FATAL.

◆ checkResidual()

void NumLib::ConvergenceCriterionPerComponentResidual::checkResidual ( const GlobalVector & residual)
overridevirtual

Check if the residual satisfies the convergence criterion.

Implements NumLib::ConvergenceCriterion.

Definition at line 66 of file ConvergenceCriterionPerComponentResidual.cpp.

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}
bool checkRelativeTolerance(const double reltol, const double numerator, const double denominator)

References _abstols, _dof_table, NumLib::ConvergenceCriterion::_is_first_iteration, NumLib::ConvergenceCriterion::_norm_type, _reltols, _residual_norms_0, NumLib::ConvergenceCriterion::_satisfied, NumLib::checkRelativeTolerance(), INFO(), NumLib::norm(), and OGS_FATAL.

◆ getDampingFactor()

double NumLib::ConvergenceCriterionPerComponentResidual::getDampingFactor ( GlobalVector const & minus_delta_x,
GlobalVector const & x,
double damping_orig )
overridevirtual

Implements NumLib::ConvergenceCriterion.

Definition at line 109 of file ConvergenceCriterionPerComponentResidual.cpp.

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}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:20
auto meshLocations(Mesh const &mesh, MeshItemType const item_type)
Definition Mesh.h:227

References _damping_alpha, _dof_table, _mesh, DBUG(), MathLib::EigenVector::get(), MeshLib::views::meshLocations(), MeshLib::Node, OGS_FATAL, and MathLib::LinAlg::setLocalAccessibleVector().

◆ hasDeltaXCheck()

bool NumLib::ConvergenceCriterionPerComponentResidual::hasDeltaXCheck ( ) const
inlineoverridevirtual

Tells if the change of the solution between iterations is checked.

Remarks
This method allows to save some computations if no such check will be done.

Implements NumLib::ConvergenceCriterion.

Definition at line 32 of file ConvergenceCriterionPerComponentResidual.h.

32{ return true; }

◆ hasNonNegativeDamping()

bool NumLib::ConvergenceCriterionPerComponentResidual::hasNonNegativeDamping ( ) const
inlineoverridevirtual

Implements NumLib::ConvergenceCriterion.

Definition at line 34 of file ConvergenceCriterionPerComponentResidual.h.

35 {
37 }

References _damping_alpha_switch.

◆ hasResidualCheck()

bool NumLib::ConvergenceCriterionPerComponentResidual::hasResidualCheck ( ) const
inlineoverridevirtual

Tells if the residual is checked.

Remarks
This method allows to save some computations if no such check will be done.

Implements NumLib::ConvergenceCriterion.

Definition at line 33 of file ConvergenceCriterionPerComponentResidual.h.

33{ return true; }

◆ setDOFTable()

void NumLib::ConvergenceCriterionPerComponentResidual::setDOFTable ( const LocalToGlobalIndexMap & dof_table,
MeshLib::Mesh const & mesh )
overridevirtual

Sets the d.o.f. table used to extract data for a specific component.

Implements NumLib::ConvergenceCriterionPerComponent.

Definition at line 144 of file ConvergenceCriterionPerComponentResidual.cpp.

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}

References _abstols, _dof_table, _mesh, and OGS_FATAL.

Member Data Documentation

◆ _abstols

const std::vector<double> NumLib::ConvergenceCriterionPerComponentResidual::_abstols
private

◆ _damping_alpha

const std::vector<double> NumLib::ConvergenceCriterionPerComponentResidual::_damping_alpha
private

◆ _damping_alpha_switch

bool NumLib::ConvergenceCriterionPerComponentResidual::_damping_alpha_switch
private

◆ _dof_table

LocalToGlobalIndexMap const* NumLib::ConvergenceCriterionPerComponentResidual::_dof_table = nullptr
private

◆ _mesh

MeshLib::Mesh const* NumLib::ConvergenceCriterionPerComponentResidual::_mesh = nullptr
private

Definition at line 55 of file ConvergenceCriterionPerComponentResidual.h.

Referenced by getDampingFactor(), and setDOFTable().

◆ _reltols

const std::vector<double> NumLib::ConvergenceCriterionPerComponentResidual::_reltols
private

◆ _residual_norms_0

std::vector<double> NumLib::ConvergenceCriterionPerComponentResidual::_residual_norms_0
private

The documentation for this class was generated from the following files: