preCICE v3.3.0
Loading...
Searching...
No Matches
ResidualSumPreconditioner.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <cmath>
6#include "utils/IntraComm.hpp"
7#include "utils/assertion.hpp"
8
10
12 int maxNonConstTimeWindows,
13 bool preconditionerUpdateOnThreshold)
14 : Preconditioner(maxNonConstTimeWindows),
15 _preconditionerUpdateOnThreshold(preconditionerUpdateOnThreshold)
16{
17}
18
27
28void ResidualSumPreconditioner::_update_(bool timeWindowComplete,
29 const Eigen::VectorXd &oldValues,
30 const Eigen::VectorXd &res)
31{
32 if (timeWindowComplete) {
33 _firstTimeWindow = false;
34 std::fill(_residualSum.begin(), _residualSum.end(), 0.0);
35 return;
36 }
37
38 std::vector<double> norms(_subVectorSizes.size(), 0.0);
39
40 double sum = 0.0;
41
42 for (size_t k = 0; k < _subVectorSizes.size(); k++) {
43 Eigen::VectorXd part = res.segment(_subVectorOffsets[k], _subVectorSizes[k]);
44 norms[k] = utils::IntraComm::dot(part, part);
45 sum += norms[k];
46 norms[k] = std::sqrt(norms[k]);
47 }
48 sum = std::sqrt(sum);
50 math::equals(sum, 0.0),
51 "All residual sub-vectors in the residual-sum preconditioner are numerically zero ( sum = {}). "
52 "This indicates that the data values exchanged between two successive iterations did not change. "
53 "The simulation may be unstable, e.g. produces NAN values. Please check the data values exchanged "
54 "between the solvers is not identical between iterations. The preconditioner scaling factors were "
55 "not updated in this iteration and the scaling factors determined in the previous iteration were used.",
56 sum);
57
58 for (size_t k = 0; k < _subVectorSizes.size(); k++) {
60 _residualSum[k] += norms[k] / sum;
61
64 "A sub-vector in the residual-sum preconditioner became numerically zero ( sub-vector = {}). "
65 "If this occurred in the second iteration and the initial-relaxation factor is equal to 1.0, "
66 "check if the coupling data values of one solver is zero in the first iteration. "
67 "The preconditioner scaling factors were not updated for this iteration and the scaling factors "
68 "determined in the previous iteration were used.",
69 _residualSum[k]);
70 }
71
72 // Determine if weights needs to be reset
73 // if _preconditionerUpdateOnThreshold is true, the weights are reset only if the ratio of the new scaling weight to the previous residual sum has changed significantly
75 if (!resetWeights) {
76 for (size_t k = 0; k < _subVectorSizes.size(); k++) {
77 double resSum = _residualSum[k];
78 if (math::equals(resSum, 0.0)) {
79 continue; // These will be ignored when resetting the weights
80 }
81 // Check if the ratio of the new scaling weight to the previous residual sum
82 // has changed significantly, either exceeding the threshold of 10.0
83 // or dropping below its inverse.
84 double factor = _previousResidualSum[k] / resSum;
85 if ((factor > 10.0) || (factor < 0.1)) {
86 resetWeights = true;
87 PRECICE_DEBUG("Significant scaling weight change is detected. The pre-scaling weights will be reset.");
88 break;
89 }
90 }
91 }
92
93 if (!resetWeights) {
94 return;
95 }
96
97 // Reset the weights for non-zero residual sums
98 for (size_t k = 0; k < _subVectorSizes.size(); k++) {
99 double resSum = _residualSum[k];
100 if (not math::equals(resSum, 0.0)) {
101 auto offset = _subVectorOffsets[k];
102 for (size_t i = 0; i < _subVectorSizes[k]; i++) {
103 _weights[i + offset] = 1 / resSum;
104 _invWeights[i + offset] = resSum;
105 }
106 PRECICE_DEBUG("preconditioner scaling factor[{}] = {}", k, 1 / resSum);
107 }
108 _previousResidualSum[k] = resSum;
109 }
110 _requireNewQR = true;
111}
112
113} // namespace precice::acceleration::impl
#define PRECICE_WARN_IF(condition,...)
Definition LogMacros.hpp:18
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:61
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
std::vector< size_t > _subVectorSizes
Sizes of each sub-vector, i.e. each coupling data.
virtual void initialize(std::vector< size_t > &svs)
initialize the preconditioner
std::vector< double > _invWeights
Inverse weights (for efficiency reasons)
std::vector< double > _weights
Weights used to scale the matrix V and the residual.
std::vector< size_t > _subVectorOffsets
Offsets of each sub-vector in concatenated data, i.e. each coupling data.
bool _requireNewQR
True if a QR decomposition from scratch is necessary.
ResidualSumPreconditioner(int maxNonConstTimeWindows, bool preconditionerUpdateOnThreshold)
void initialize(std::vector< size_t > &svs) override
initialize the preconditioner
void _update_(bool timeWindowComplete, const Eigen::VectorXd &oldValues, const Eigen::VectorXd &res) override
Update the scaling after every FSI iteration.
static double dot(const Eigen::VectorXd &vec1, const Eigen::VectorXd &vec2)
T fill(T... args)
constexpr bool equals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares two Eigen::MatrixBase for equality up to tolerance.
constexpr double NUMERICAL_ZERO_DIFFERENCE
T sqrt(T... args)