Loading...
Searching...
No Matches
weightedresidreductioncriterion.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_WEIGHTED_RESIDUAL_REDUCTION_CRITERION_HH
#define EWOMS_WEIGHTED_RESIDUAL_REDUCTION_CRITERION_HH
#include "convergencecriterion.hh"
#include <iostream>
namespace Opm {
namespace Linear {
template <class Vector, class CollectiveCommunication>
class WeightedResidualReductionCriterion : public ConvergenceCriterion<Vector>
{
using Scalar = typename Vector::field_type;
using BlockType = typename Vector::block_type;
public:
WeightedResidualReductionCriterion(const CollectiveCommunication& comm)
: comm_(comm)
{}
WeightedResidualReductionCriterion(const CollectiveCommunication& comm,
const Vector& residWeights,
Scalar residualReductionTolerance,
Scalar fixPointTolerance,
Scalar absResidualTolerance = 0.0,
Scalar maxError = 0.0)
: comm_(comm),
residWeightVec_(residWeights),
fixPointTolerance_(fixPointTolerance),
residualReductionTolerance_(residualReductionTolerance),
absResidualTolerance_(absResidualTolerance),
maxError_(maxError)
{ }
void setResidualWeight(const Vector& residWeightVec)
{ residWeightVec_ = residWeightVec; }
Scalar residualWeight(size_t outerIdx, unsigned innerIdx) const
{
return (residWeightVec_.size() == 0)
? 1.0
: residWeightVec_[outerIdx][innerIdx];
}
void setResidualReductionTolerance(Scalar tol)
{ residualReductionTolerance_ = tol; }
Scalar residualReductionTolerance() const
{ return residualReductionTolerance_; }
void setResidualTolerance(Scalar tol)
{ absResidualTolerance_ = tol; }
Scalar absResidualTolerance() const
{ return absResidualTolerance_; }
Scalar residualAccuracy() const
{ return residualError_/std::max<Scalar>(1e-20, initialResidualError_); }
void setFixPointTolerance(Scalar tol)
{ fixPointTolerance_ = tol; }
Scalar fixPointTolerance() const
{ return fixPointTolerance_; }
Scalar fixPointAccuracy() const
{ return fixPointError_; }
void setInitial(const Vector& curSol, const Vector& curResid) override
{
lastResidualError_ = 1e100;
lastSolVec_ = curSol;
updateErrors_(curSol, curResid);
// the fix-point error is not applicable for the initial solution!
fixPointError_ = 1e100;
// make sure that we don't allow an initial error of 0 to avoid
// divisions by zero
residualError_ = std::max<Scalar>(residualError_, 1e-20);
initialResidualError_ = residualError_;
}
void update(const Vector& curSol,
const Vector&,
const Vector& curResid) override
{
lastResidualError_ = residualError_;
updateErrors_(curSol, curResid);
}
bool converged() const override
{
// we're converged if the solution is better than the tolerance
// fix-point and residual tolerance.
return
residualAccuracy() <= residualReductionTolerance() ||
residualError_ <= absResidualTolerance_;
}
bool failed() const override
{
return
(!converged() && fixPointError_ <= fixPointTolerance_)
|| residualError_ > maxError_;
}
Scalar accuracy() const override
{ return residualError_; }
void printInitial(std::ostream& os = std::cout) const override
{
os << std::setw(20) << " Iter ";
os << std::setw(20) << " Delta ";
os << std::setw(20) << " Residual ";
os << std::setw(20) << " ResidRed ";
os << std::setw(20) << " Rate ";
os << std::endl;
}
void print(Scalar iter, std::ostream& os = std::cout) const override
{
static constexpr Scalar eps = std::numeric_limits<Scalar>::min()*1e10;
os << std::setw(20) << iter << " ";
os << std::setw(20) << fixPointAccuracy() << " ";
os << std::setw(20) << residualError_ << " ";
os << std::setw(20) << 1.0/residualAccuracy() << " ";
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& curSol, const Vector& curResid)
{
residualError_ = 0.0;
fixPointError_ = 0.0;
for (size_t i = 0; i < curResid.size(); ++i) {
for (unsigned j = 0; j < BlockType::dimension; ++j) {
residualError_ =
std::max<Scalar>(residualError_,
residualWeight(i, j)*std::abs(curResid[i][j]));
fixPointError_ =
std::max<Scalar>(fixPointError_,
std::abs(curSol[i][j] - lastSolVec_[i][j])
/std::max<Scalar>(1.0, curSol[i][j]));
}
}
lastSolVec_ = curSol;
residualError_ = comm_.max(residualError_);
fixPointError_ = comm_.max(fixPointError_);
}
const CollectiveCommunication& comm_;
// the weights of the components of the residual
Vector residWeightVec_;
// solution vector of the last iteration
Vector lastSolVec_;
// the maximum of the weighted difference between the last two
// iterations
Scalar fixPointError_;
// the maximum allowed relative tolerance for difference of the
// solution of two iterations
Scalar fixPointTolerance_;
// the maximum of the absolute weighted residual of the last
// iteration
Scalar residualError_;
// the maximum of the absolute weighted difference of the last
// iteration
Scalar lastResidualError_;
// the maximum of the absolute weighted residual of the initial
// solution
Scalar initialResidualError_;
// the maximum allowed relative tolerance of the residual for the
// solution to be considered converged
Scalar residualReductionTolerance_;
// the maximum allowed absolute tolerance of the residual for the
// solution to be considered converged
Scalar absResidualTolerance_;
// The maximum error which is tolerated before we fail.
Scalar maxError_;
};
}} // end namespace Linear, Opm
#endif
Opm::Linear::ConvergenceCriterion
Base class for all convergence criteria which only defines an virtual API.
Definition convergencecriterion.hh:56
Opm::Linear::WeightedResidualReductionCriterion::residualReductionTolerance
Scalar residualReductionTolerance() const
Returns the tolerance of the residual reduction of the solution.
Definition weightedresidreductioncriterion.hh:123
Opm::Linear::WeightedResidualReductionCriterion::accuracy
Scalar accuracy() const override
Returns the accuracy of the solution at the last update.
Definition weightedresidreductioncriterion.hh:222
Opm::Linear::WeightedResidualReductionCriterion::converged
bool converged() const override
Returns true if and only if the convergence criterion is met.
Definition weightedresidreductioncriterion.hh:198
Opm::Linear::WeightedResidualReductionCriterion::residualAccuracy
Scalar residualAccuracy() const
Returns the reduction of the weighted maximum of the residual compared to the initial solution.
Definition weightedresidreductioncriterion.hh:142
Opm::Linear::WeightedResidualReductionCriterion::residualWeight
Scalar residualWeight(size_t outerIdx, unsigned innerIdx) const
Return the relative weight of a row of the residual.
Definition weightedresidreductioncriterion.hh:107
Opm::Linear::WeightedResidualReductionCriterion::setInitial
void setInitial(const Vector &curSol, const Vector &curResid) override
Set the initial solution of the linear system of equations.
Definition weightedresidreductioncriterion.hh:169
Opm::Linear::WeightedResidualReductionCriterion::fixPointTolerance
Scalar fixPointTolerance() const
Returns the maximum tolerated difference between two iterations to be met before a solution is consid...
Definition weightedresidreductioncriterion.hh:156
Opm::Linear::WeightedResidualReductionCriterion::failed
bool failed() const override
Returns true if the convergence criterion cannot be met anymore because the solver has broken down.
Definition weightedresidreductioncriterion.hh:210
Opm::Linear::WeightedResidualReductionCriterion::setResidualTolerance
void setResidualTolerance(Scalar tol)
Sets the maximum absolute tolerated residual.
Definition weightedresidreductioncriterion.hh:129
Opm::Linear::WeightedResidualReductionCriterion::setResidualWeight
void setResidualWeight(const Vector &residWeightVec)
Sets the relative weight of each row of the residual.
Definition weightedresidreductioncriterion.hh:98
Opm::Linear::WeightedResidualReductionCriterion::setFixPointTolerance
void setFixPointTolerance(Scalar tol)
Sets the fix-point tolerance.
Definition weightedresidreductioncriterion.hh:148
Opm::Linear::WeightedResidualReductionCriterion::update
void update(const Vector &curSol, const Vector &, const Vector &curResid) override
Definition weightedresidreductioncriterion.hh:187
Opm::Linear::WeightedResidualReductionCriterion::printInitial
void printInitial(std::ostream &os=std::cout) const override
Prints the initial information about the convergence behaviour.
Definition weightedresidreductioncriterion.hh:228
Opm::Linear::WeightedResidualReductionCriterion::fixPointAccuracy
Scalar fixPointAccuracy() const
Returns the weighted maximum of the difference between the last two iterative solutions.
Definition weightedresidreductioncriterion.hh:163
Opm::Linear::WeightedResidualReductionCriterion::print
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
Opm::Linear::WeightedResidualReductionCriterion::setResidualReductionTolerance
void setResidualReductionTolerance(Scalar tol)
Sets the residual reduction tolerance.
Definition weightedresidreductioncriterion.hh:117
Opm::Linear::WeightedResidualReductionCriterion::absResidualTolerance
Scalar absResidualTolerance() const
Returns the maximum absolute tolerated residual.
Definition weightedresidreductioncriterion.hh:135
convergencecriterion.hh
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43