opm-simulators
weightedresidreductioncriterion.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef EWOMS_WEIGHTED_RESIDUAL_REDUCTION_CRITERION_HH
28 #define EWOMS_WEIGHTED_RESIDUAL_REDUCTION_CRITERION_HH
29 
30 #include "convergencecriterion.hh"
31 
32 #include <iostream>
33 
34 namespace Opm {
35 namespace Linear {
36 
57 template <class Vector, class CollectiveCommunication>
59 {
60  using Scalar = typename Vector::field_type;
61  using BlockType = typename Vector::block_type;
62 
63 public:
64  WeightedResidualReductionCriterion(const CollectiveCommunication& comm)
65  : comm_(comm)
66  {}
67 
68  WeightedResidualReductionCriterion(const CollectiveCommunication& comm,
69  const Vector& residWeights,
71  Scalar fixPointTolerance,
72  Scalar absResidualTolerance = 0.0,
73  Scalar maxError = 0.0)
74  : comm_(comm),
75  residWeightVec_(residWeights),
76  fixPointTolerance_(fixPointTolerance),
77  residualReductionTolerance_(residualReductionTolerance),
78  absResidualTolerance_(absResidualTolerance),
79  maxError_(maxError)
80  { }
81 
98  void setResidualWeight(const Vector& residWeightVec)
99  { residWeightVec_ = residWeightVec; }
100 
107  Scalar residualWeight(size_t outerIdx, unsigned innerIdx) const
108  {
109  return (residWeightVec_.size() == 0)
110  ? 1.0
111  : residWeightVec_[outerIdx][innerIdx];
112  }
113 
118  { residualReductionTolerance_ = tol; }
119 
124  { return residualReductionTolerance_; }
125 
129  void setResidualTolerance(Scalar tol)
130  { absResidualTolerance_ = tol; }
131 
135  Scalar absResidualTolerance() const
136  { return absResidualTolerance_; }
137 
142  Scalar residualAccuracy() const
143  { return residualError_/std::max<Scalar>(1e-20, initialResidualError_); }
144 
148  void setFixPointTolerance(Scalar tol)
149  { fixPointTolerance_ = tol; }
150 
156  Scalar fixPointTolerance() const
157  { return fixPointTolerance_; }
158 
163  Scalar fixPointAccuracy() const
164  { return fixPointError_; }
165 
169  void setInitial(const Vector& curSol, const Vector& curResid) override
170  {
171  lastResidualError_ = 1e100;
172 
173  lastSolVec_ = curSol;
174  updateErrors_(curSol, curResid);
175  // the fix-point error is not applicable for the initial solution!
176  fixPointError_ = 1e100;
177 
178  // make sure that we don't allow an initial error of 0 to avoid
179  // divisions by zero
180  residualError_ = std::max<Scalar>(residualError_, 1e-20);
181  initialResidualError_ = residualError_;
182  }
183 
187  void update(const Vector& curSol,
188  const Vector&,
189  const Vector& curResid) override
190  {
191  lastResidualError_ = residualError_;
192  updateErrors_(curSol, curResid);
193  }
194 
198  bool converged() const override
199  {
200  // we're converged if the solution is better than the tolerance
201  // fix-point and residual tolerance.
202  return
204  residualError_ <= absResidualTolerance_;
205  }
206 
210  bool failed() const override
211  {
212  return
213  (!converged() && fixPointError_ <= fixPointTolerance_)
214  || residualError_ > maxError_;
215  }
216 
222  Scalar accuracy() const override
223  { return residualError_; }
224 
228  void printInitial(std::ostream& os = std::cout) const override
229  {
230  os << std::setw(20) << " Iter ";
231  os << std::setw(20) << " Delta ";
232  os << std::setw(20) << " Residual ";
233  os << std::setw(20) << " ResidRed ";
234  os << std::setw(20) << " Rate ";
235  os << std::endl;
236  }
237 
241  void print(Scalar iter, std::ostream& os = std::cout) const override
242  {
243  static constexpr Scalar eps = std::numeric_limits<Scalar>::min()*1e10;
244 
245  os << std::setw(20) << iter << " ";
246  os << std::setw(20) << fixPointAccuracy() << " ";
247  os << std::setw(20) << residualError_ << " ";
248  os << std::setw(20) << 1.0/residualAccuracy() << " ";
249  os << std::setw(20) << lastResidualError_ / std::max<Scalar>(residualError_, eps) << " ";
250  os << std::endl << std::flush;
251  }
252 
253 private:
254  // update the weighted absolute residual
255  void updateErrors_(const Vector& curSol, const Vector& curResid)
256  {
257  residualError_ = 0.0;
258  fixPointError_ = 0.0;
259  for (size_t i = 0; i < curResid.size(); ++i) {
260  for (unsigned j = 0; j < BlockType::dimension; ++j) {
261  residualError_ =
262  std::max<Scalar>(residualError_,
263  residualWeight(i, j)*std::abs(curResid[i][j]));
264  fixPointError_ =
265  std::max<Scalar>(fixPointError_,
266  std::abs(curSol[i][j] - lastSolVec_[i][j])
267  /std::max<Scalar>(1.0, curSol[i][j]));
268  }
269  }
270  lastSolVec_ = curSol;
271 
272  residualError_ = comm_.max(residualError_);
273  fixPointError_ = comm_.max(fixPointError_);
274  }
275 
276  const CollectiveCommunication& comm_;
277 
278  // the weights of the components of the residual
279  Vector residWeightVec_;
280 
281  // solution vector of the last iteration
282  Vector lastSolVec_;
283 
284  // the maximum of the weighted difference between the last two
285  // iterations
286  Scalar fixPointError_;
287 
288  // the maximum allowed relative tolerance for difference of the
289  // solution of two iterations
290  Scalar fixPointTolerance_;
291 
292  // the maximum of the absolute weighted residual of the last
293  // iteration
294  Scalar residualError_;
295 
296  // the maximum of the absolute weighted difference of the last
297  // iteration
298  Scalar lastResidualError_;
299 
300  // the maximum of the absolute weighted residual of the initial
301  // solution
302  Scalar initialResidualError_;
303 
304  // the maximum allowed relative tolerance of the residual for the
305  // solution to be considered converged
306  Scalar residualReductionTolerance_;
307 
308  // the maximum allowed absolute tolerance of the residual for the
309  // solution to be considered converged
310  Scalar absResidualTolerance_;
311 
312  // The maximum error which is tolerated before we fail.
313  Scalar maxError_;
314 };
315 
317 
318 }} // end namespace Linear, Opm
319 
320 #endif
void setResidualWeight(const Vector &residWeightVec)
Sets the relative weight of each row of the residual.
Definition: weightedresidreductioncriterion.hh:98
Base class for all convergence criteria which only defines an virtual API.
Definition: convergencecriterion.hh:55
Scalar absResidualTolerance() const
Returns the maximum absolute tolerated residual.
Definition: weightedresidreductioncriterion.hh:135
void setFixPointTolerance(Scalar tol)
Sets the fix-point tolerance.
Definition: weightedresidreductioncriterion.hh:148
void update(const Vector &curSol, const Vector &, const Vector &curResid) override
Definition: weightedresidreductioncriterion.hh:187
Scalar residualWeight(size_t outerIdx, unsigned innerIdx) const
Return the relative weight of a row of the residual.
Definition: weightedresidreductioncriterion.hh:107
Scalar residualAccuracy() const
Returns the reduction of the weighted maximum of the residual compared to the initial solution...
Definition: weightedresidreductioncriterion.hh:142
void setResidualTolerance(Scalar tol)
Sets the maximum absolute tolerated residual.
Definition: weightedresidreductioncriterion.hh:129
Scalar fixPointTolerance() const
Returns the maximum tolerated difference between two iterations to be met before a solution is consid...
Definition: weightedresidreductioncriterion.hh:156
Scalar residualReductionTolerance() const
Returns the tolerance of the residual reduction of the solution.
Definition: weightedresidreductioncriterion.hh:123
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
bool failed() const override
Returns true if the convergence criterion cannot be met anymore because the solver has broken down...
Definition: weightedresidreductioncriterion.hh:210
void print(Scalar iter, std::ostream &os=std::cout) const override
Prints the information about the convergence behaviour for the current iteration. ...
Definition: weightedresidreductioncriterion.hh:241
void setResidualReductionTolerance(Scalar tol)
Sets the residual reduction tolerance.
Definition: weightedresidreductioncriterion.hh:117
Scalar accuracy() const override
Returns the accuracy of the solution at the last update.
Definition: weightedresidreductioncriterion.hh:222
bool converged() const override
Returns true if and only if the convergence criterion is met.
Definition: weightedresidreductioncriterion.hh:198
void printInitial(std::ostream &os=std::cout) const override
Prints the initial information about the convergence behaviour.
Definition: weightedresidreductioncriterion.hh:228
void setInitial(const Vector &curSol, const Vector &curResid) override
Set the initial solution of the linear system of equations.
Definition: weightedresidreductioncriterion.hh:169
Scalar fixPointAccuracy() const
Returns the weighted maximum of the difference between the last two iterative solutions.
Definition: weightedresidreductioncriterion.hh:163
Convergence criterion which looks at the weighted absolute value of the residual. ...
Definition: weightedresidreductioncriterion.hh:58