Loading...
Searching...
No Matches
combinedcriterion.hh
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef EWOMS_COMBINED_CRITERION_HH
#define EWOMS_COMBINED_CRITERION_HH
#include "convergencecriterion.hh"
#include <iostream>
namespace Opm {
namespace Linear {
template <class Vector, class CollectiveCommunication>
class CombinedCriterion : public ConvergenceCriterion<Vector>
{
using Scalar = typename Vector::field_type;
using BlockType = typename Vector::block_type;
public:
explicit CombinedCriterion(const CollectiveCommunication& comm)
: comm_(comm)
{}
CombinedCriterion(const CollectiveCommunication& comm,
Scalar residualReductionTolerance,
Scalar absResidualTolerance = 0.0,
Scalar maxResidual = 0.0)
: comm_(comm),
residualReductionTolerance_(residualReductionTolerance),
absResidualTolerance_(absResidualTolerance),
maxResidual_(maxResidual)
{ }
void setResidualReductionTolerance(Scalar tol)
{ residualReductionTolerance_ = tol; }
Scalar residualReductionTolerance() const
{ return residualReductionTolerance_; }
Scalar residualReduction() const
{ return residualError_/std::max<Scalar>(1e-20, initialResidualError_); }
void setAbsResidualTolerance(Scalar tol)
{ absResidualTolerance_ = tol; }
Scalar absResidualTolerance() const
{ return absResidualTolerance_; }
Scalar absResidual() const
{ return residualError_; }
void setInitial(const Vector& curSol, const Vector& curResid) override
{
updateErrors_(curSol, curSol, curResid);
stagnates_ = false;
// to avoid divisions by zero, make sure that we don't use an initial error of 0
residualError_ = std::max<Scalar>(residualError_,
std::numeric_limits<Scalar>::min()*1e10);
initialResidualError_ = residualError_;
lastResidualError_ = residualError_;
}
void update(const Vector& curSol, const Vector& changeIndicator, const Vector& curResid) override
{ updateErrors_(curSol, changeIndicator, curResid); }
bool converged() const override
{
// we're converged if the solution is better than the tolerance
// fix-point and residual tolerance.
return
residualReduction() <= residualReductionTolerance() ||
absResidual() <= absResidualTolerance();
}
bool failed() const override
{ return !converged() && (stagnates_ || residualError_ > maxResidual_); }
Scalar accuracy() const override
{ return residualError_/initialResidualError_; }
void printInitial(std::ostream& os = std::cout) const override
{
os << std::setw(20) << "iteration ";
os << std::setw(20) << "residual ";
os << std::setw(20) << "reduction ";
os << std::setw(20) << "rate ";
os << std::endl;
}
void print(Scalar iter, std::ostream& os = std::cout) const override
{
const Scalar eps = std::numeric_limits<Scalar>::min()*1e10;
os << std::setw(20) << iter << " ";
os << std::setw(20) << absResidual() << " ";
os << std::setw(20) << accuracy() << " ";
os << std::setw(20) << lastResidualError_/std::max<Scalar>(residualError_, eps) << " ";
os << std::endl << std::flush;
}
private:
// update the weighted absolute residual
void updateErrors_(const Vector&, const Vector& changeIndicator, const Vector& curResid)
{
lastResidualError_ = residualError_;
residualError_ = 0.0;
stagnates_ = true;
for (size_t i = 0; i < curResid.size(); ++i) {
for (unsigned j = 0; j < BlockType::dimension; ++j) {
residualError_ =
std::max<Scalar>(residualError_,
std::abs(curResid[i][j]));
if (stagnates_ && changeIndicator[i][j] != 0.0)
// only stagnation means that we've failed!
stagnates_ = false;
}
}
residualError_ = comm_.max(residualError_);
// the linear solver only stagnates if all processes stagnate
stagnates_ = comm_.min(stagnates_);
}
const CollectiveCommunication& comm_;
// the infinity norm of the residual of the last iteration
Scalar lastResidualError_;
// the infinity norm of the residual of the current iteration
Scalar residualError_;
// the infinity norm of the residual of the initial solution
Scalar initialResidualError_;
// the minimum reduction of the residual norm where the solution is to be considered
// converged
Scalar residualReductionTolerance_;
// the maximum residual norm for the residual for the solution to be considered to be
// converged
Scalar absResidualTolerance_;
// The maximum error which is tolerated before we fail.
Scalar maxResidual_;
// does the linear solver seem to stagnate, i.e. were the last two solutions
// identical?
bool stagnates_;
};
}} // end namespace Linear, Opm
#endif
Opm::Linear::CombinedCriterion::accuracy
Scalar accuracy() const override
Returns the accuracy of the solution at the last update.
Definition combinedcriterion.hh:157
Opm::Linear::CombinedCriterion::setResidualReductionTolerance
void setResidualReductionTolerance(Scalar tol)
Sets the residual reduction tolerance.
Definition combinedcriterion.hh:78
Opm::Linear::CombinedCriterion::absResidualTolerance
Scalar absResidualTolerance() const
Returns the tolerated maximum of the the infinity norm of the absolute residual.
Definition combinedcriterion.hh:104
Opm::Linear::CombinedCriterion::residualReductionTolerance
Scalar residualReductionTolerance() const
Returns the tolerance of the residual reduction of the solution.
Definition combinedcriterion.hh:84
Opm::Linear::CombinedCriterion::print
void print(Scalar iter, std::ostream &os=std::cout) const override
Prints the information about the convergence behaviour for the current iteration.
Definition combinedcriterion.hh:175
Opm::Linear::CombinedCriterion::absResidual
Scalar absResidual() const
Returns the infinity norm of the absolute residual.
Definition combinedcriterion.hh:110
Opm::Linear::CombinedCriterion::printInitial
void printInitial(std::ostream &os=std::cout) const override
Prints the initial information about the convergence behaviour.
Definition combinedcriterion.hh:163
Opm::Linear::CombinedCriterion::failed
bool failed() const override
Returns true if the convergence criterion cannot be met anymore because the solver has broken down.
Definition combinedcriterion.hh:149
Opm::Linear::CombinedCriterion::residualReduction
Scalar residualReduction() const
Returns the reduction of the maximum of the residual compared to the initial solution.
Definition combinedcriterion.hh:91
Opm::Linear::CombinedCriterion::setInitial
void setInitial(const Vector &curSol, const Vector &curResid) override
Set the initial solution of the linear system of equations.
Definition combinedcriterion.hh:116
Opm::Linear::CombinedCriterion::update
void update(const Vector &curSol, const Vector &changeIndicator, const Vector &curResid) override
Update the internal members of the convergence criterion with the current solution.
Definition combinedcriterion.hh:131
Opm::Linear::CombinedCriterion::converged
bool converged() const override
Returns true if and only if the convergence criterion is met.
Definition combinedcriterion.hh:137
Opm::Linear::CombinedCriterion::setAbsResidualTolerance
void setAbsResidualTolerance(Scalar tol)
Sets the maximum absolute tolerated residual.
Definition combinedcriterion.hh:97
Opm::Linear::ConvergenceCriterion
Base class for all convergence criteria which only defines an virtual API.
Definition convergencecriterion.hh:56
convergencecriterion.hh
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43