fixpointcriterion.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  Copyright (C) 2009-2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef EWOMS_ISTL_FIXPOINT_CRITERION_HH
26 #define EWOMS_ISTL_FIXPOINT_CRITERION_HH
27 
28 #include "convergencecriterion.hh"
29 
30 namespace Ewoms {
51 template <class Vector, class CollectiveCommunication>
53 {
54  typedef typename Vector::field_type Scalar;
55  typedef typename Vector::block_type BlockType;
56 
57 public:
58  FixPointCriterion(const CollectiveCommunication &comm) : comm_(comm)
59  {}
60 
61  FixPointCriterion(const CollectiveCommunication &comm,
62  const Vector &weightVec, Scalar reduction)
63  : comm_(comm), weightVec_(weightVec), tolerance_(reduction)
64  {}
65 
82  void setWeight(const Vector &weightVec)
83  { weightVec_ = weightVec; }
84 
101  Scalar weight(int outerIdx, int innerIdx) const
102  { return (weightVec_.size() == 0) ? 1.0 : weightVec_[outerIdx][innerIdx]; }
103 
108  void setTolerance(Scalar tol)
109  { tolerance_ = tol; }
110 
115  Scalar tolerance() const
116  { return tolerance_; }
117 
121  void setInitial(const Vector &curSol, const Vector &curResid)
122  {
123  lastSol_ = curSol;
124  delta_ = 1000 * tolerance_;
125  }
126 
130  void update(const Vector &curSol, const Vector &curResid)
131  {
132  assert(curSol.size() == lastSol_.size());
133 
134  delta_ = 0.0;
135  for (size_t i = 0; i < curSol.size(); ++i) {
136  for (size_t j = 0; j < BlockType::dimension; ++j) {
137  delta_ = std::max<Scalar>(delta_, weight(i, j)
138  * std::abs(curSol[i][j]
139  - lastSol_[i][j]));
140  }
141  }
142 
143  delta_ = comm_.max(delta_);
144  lastSol_ = curSol;
145  }
146 
150  bool converged() const
151  { return delta_ < tolerance(); }
152 
156  Scalar accuracy() const
157  { return delta_; }
158 
159 private:
160  const CollectiveCommunication &comm_;
161 
162  Vector lastSol_; // solution of the last iteration
163  Vector weightVec_; // solution of the last iteration
164  Scalar delta_; // the maximum of the absolute weighted difference of the
165  // last two iterations
166  Scalar tolerance_; // the maximum allowed delta for the solution to be
167  // considered converged
168 };
169 
171 
172 } // end namespace Ewoms
173 
174 #endif
void setWeight(const Vector &weightVec)
Sets the relative weight of a primary variable.
Definition: fixpointcriterion.hh:82
FixPointCriterion(const CollectiveCommunication &comm)
Definition: fixpointcriterion.hh:58
Base class for all convergence criteria which only defines an virtual API.
Definition: convergencecriterion.hh:51
bool converged() const
Returns true if and only if the convergence criterion is met.
Definition: fixpointcriterion.hh:150
Definition: baseauxiliarymodule.hh:35
FixPointCriterion(const CollectiveCommunication &comm, const Vector &weightVec, Scalar reduction)
Definition: fixpointcriterion.hh:61
Base class for all convergence criteria which only defines an virtual API.
Scalar weight(int outerIdx, int innerIdx) const
Return the relative weight of a primary variable.
Definition: fixpointcriterion.hh:101
void update(const Vector &curSol, const Vector &curResid)
Update the internal members of the convergence criterion with the current solution.
Definition: fixpointcriterion.hh:130
Provides a convergence criterion for the linear solvers which looks at the weighted maximum of the di...
Definition: fixpointcriterion.hh:52
void setInitial(const Vector &curSol, const Vector &curResid)
Set the initial solution of the linear system of equations.
Definition: fixpointcriterion.hh:121
void setTolerance(Scalar tol)
Set the maximum allowed weighted maximum difference between two iterations.
Definition: fixpointcriterion.hh:108
Scalar tolerance() const
Return the maximum allowed weighted maximum difference between two iterations.
Definition: fixpointcriterion.hh:115
Scalar accuracy() const
Returns the accuracy of the solution at the last update.
Definition: fixpointcriterion.hh:156