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 28 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 21 of file ConvergenceCriterionPerComponentResidual.cpp.

29 _abstols(std::move(absolute_tolerances)),
30 _reltols(std::move(relative_tolerances)),
32 _damping_alpha(std::move(damping_alpha)),
33 _damping_alpha_switch(damping_alpha_switch)
34{
35 if (_abstols.size() != _reltols.size())
36 {
38 "The number of absolute and relative tolerances given must be the "
39 "same.");
40 }
41
42 if (_abstols.empty())
43 {
44 OGS_FATAL("The given tolerances vector is empty.");
45 }
46}
#define OGS_FATAL(...)
Definition Error.h:26
ConvergenceCriterionPerComponent(const MathLib::VecNormType norm_type)

References _abstols, and _reltols.

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 48 of file ConvergenceCriterionPerComponentResidual.cpp.

50{
51 if (!_dof_table)
52 {
53 OGS_FATAL("D.o.f. table has not been set.");
54 }
55
56 for (unsigned global_component = 0; global_component < _abstols.size();
57 ++global_component)
58 {
59 // TODO short cut if tol <= 0.0
60 auto error_dx =
61 norm(minus_delta_x, global_component, _norm_type, *_dof_table);
62 auto norm_x = norm(x, global_component, _norm_type, *_dof_table);
63
64 INFO(
65 "Convergence criterion, component {:d}: |dx|={:.4e}, |x|={:.4e}, "
66 "|dx|/|x|={:.4e}",
67 global_component, error_dx, norm_x,
68 (norm_x == 0. ? std::numeric_limits<double>::quiet_NaN()
69 : (error_dx / norm_x)));
70 }
71}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
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 73 of file ConvergenceCriterionPerComponentResidual.cpp.

75{
76 if (!_dof_table)
77 {
78 OGS_FATAL("D.o.f. table has not been set.");
79 }
80
81 for (unsigned global_component = 0; global_component < _abstols.size();
82 ++global_component)
83 {
84 // TODO short cut if tol <= 0.0
85 auto norm_res =
86 norm(residual, global_component, _norm_type, *_dof_table);
87
89 {
90 INFO("Convergence criterion, component {:d}: |r0|={:.4e}",
91 global_component, norm_res);
92 _residual_norms_0[global_component] = norm_res;
93 }
94 else
95 {
96 auto const norm_res0 = _residual_norms_0[global_component];
97 INFO(
98 "Convergence criterion, component {:d}: |r|={:.4e}, "
99 "|r0|={:.4e}, |r|/|r0|={:.4e}",
100 global_component, norm_res, norm_res0,
101 (norm_res0 == 0. ? std::numeric_limits<double>::quiet_NaN()
102 : (norm_res / norm_res0)));
103 }
104
105 bool const satisfied_abs = norm_res < _abstols[global_component];
106 // Make sure that in the first iteration the relative residual tolerance
107 // is not satisfied.
108 bool const satisfied_rel =
110 checkRelativeTolerance(_reltols[global_component], norm_res,
111 _residual_norms_0[global_component]);
112 _satisfied = _satisfied && (satisfied_abs || satisfied_rel);
113 }
114}
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 116 of file ConvergenceCriterionPerComponentResidual.cpp.

119{
120 if ((!_dof_table) || (!_mesh))
121 {
122 OGS_FATAL("D.o.f. table or mesh have not been set.");
123 }
124
126 double damping_final = 1;
127 for (unsigned global_component = 0;
128 global_component < _damping_alpha.size();
129 ++global_component)
130 {
131 auto const& ms = _dof_table->getMeshSubset(global_component);
132 assert(ms.getMeshID() == _mesh->getID());
133 DBUG("Non-negative damping for component: {:d} alpha: {:g}",
134 global_component, _damping_alpha[global_component]);
135 for (auto const& l :
137 {
138 auto index = _dof_table->getGlobalIndex(l, global_component);
139 damping_final = std::min(
140 damping_final,
141 damping_orig / std::max(1.0, (minus_delta_x.get(index) *
142 _damping_alpha[global_component] /
143 x.get(index))));
144 }
145 }
146 DBUG("Final damping value due to non-negative damping: {:g}",
147 damping_final);
148 return damping_final;
149}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
std::size_t getID() const
Get id of the mesh.
Definition Mesh.h:123
GlobalIndexType getGlobalIndex(MeshLib::Location const &l, int const variable_id, int const component_id) const
MeshLib::MeshSubset const & getMeshSubset(int const variable_id, int const component_id) const
void setLocalAccessibleVector(PETScVector const &x)
Definition LinAlg.cpp:27
auto meshLocations(Mesh const &mesh, MeshItemType const item_type)
Definition Mesh.h:238

References _damping_alpha, _dof_table, _mesh, DBUG(), MathLib::EigenVector::get(), NumLib::LocalToGlobalIndexMap::getGlobalIndex(), MeshLib::Mesh::getID(), NumLib::LocalToGlobalIndexMap::getMeshSubset(), 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 39 of file ConvergenceCriterionPerComponentResidual.h.

39{ return true; }

◆ hasNonNegativeDamping()

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

Implements NumLib::ConvergenceCriterion.

Definition at line 41 of file ConvergenceCriterionPerComponentResidual.h.

42 {
44 }

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 40 of file ConvergenceCriterionPerComponentResidual.h.

40{ 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 151 of file ConvergenceCriterionPerComponentResidual.cpp.

153{
154 _dof_table = &dof_table;
155 _mesh = &mesh;
156
158 static_cast<int>(_abstols.size()))
159 {
160 OGS_FATAL(
161 "The number of components in the DOF table and the number of "
162 "tolerances given do not match.");
163 }
164}

References _abstols, _dof_table, _mesh, NumLib::LocalToGlobalIndexMap::getNumberOfGlobalComponents(), 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

Definition at line 64 of file ConvergenceCriterionPerComponentResidual.h.

Referenced by getDampingFactor().

◆ _damping_alpha_switch

bool NumLib::ConvergenceCriterionPerComponentResidual::_damping_alpha_switch
private

Definition at line 65 of file ConvergenceCriterionPerComponentResidual.h.

Referenced by hasNonNegativeDamping().

◆ _dof_table

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

◆ _mesh

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

Definition at line 62 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

Definition at line 63 of file ConvergenceCriterionPerComponentResidual.h.

Referenced by checkResidual().


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