Loading...
Searching...
No Matches
residreductioncriterion.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_RESID_REDUCTION_CRITERION_HH
#define EWOMS_RESID_REDUCTION_CRITERION_HH
#include "convergencecriterion.hh"
#include <dune/istl/scalarproducts.hh>
namespace Opm {
namespace Linear {
template <class Vector>
class ResidReductionCriterion : public ConvergenceCriterion<Vector>
{
using Scalar = typename Vector::field_type;
public:
explicit ResidReductionCriterion(Dune::ScalarProduct<Vector>& scalarProduct,
Scalar tolerance = 1e-6)
: scalarProduct_(scalarProduct), tolerance_(tolerance)
{}
void setTolerance(Scalar tol)
{ tolerance_ = tol; }
Scalar tolerance() const
{ return tolerance_; }
void setInitial(const Vector&, const Vector& curResid) override
{
static constexpr Scalar eps = std::numeric_limits<Scalar>::min()*1e10;
// make sure that we don't allow an initial error of 0 to avoid
// divisions by zero
curDefect_ = scalarProduct_.norm(curResid);
lastDefect_ = curDefect_;
initialDefect_ = std::max(curDefect_, eps);
}
void update(const Vector&,
const Vector&,
const Vector& curResid) override
{
lastDefect_ = curDefect_;
curDefect_ = scalarProduct_.norm(curResid);
}
bool converged() const override
{ return accuracy() <= tolerance(); }
Scalar accuracy() const override
{ return curDefect_/initialDefect_; }
void printInitial(std::ostream& os = std::cout) const override
{
os << std::setw(20) << "iteration ";
os << std::setw(20) << "residual ";
os << std::setw(20) << "accuracy ";
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) << curDefect_ << " ";
os << std::setw(20) << accuracy() << " ";
os << std::setw(20) << (lastDefect_/std::max(eps, curDefect_)) << " ";
os << std::endl;
}
private:
Dune::ScalarProduct<Vector>& scalarProduct_;
Scalar tolerance_;
Scalar initialDefect_;
Scalar curDefect_;
Scalar lastDefect_;
};
}} // 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::ResidReductionCriterion::printInitial
void printInitial(std::ostream &os=std::cout) const override
Prints the initial information about the convergence behaviour.
Definition residreductioncriterion.hh:114
Opm::Linear::ResidReductionCriterion::accuracy
Scalar accuracy() const override
Returns the accuracy of the solution at the last update.
Definition residreductioncriterion.hh:108
Opm::Linear::ResidReductionCriterion::print
void print(Scalar iter, std::ostream &os=std::cout) const override
Prints the information about the convergence behaviour for the current iteration.
Definition residreductioncriterion.hh:126
Opm::Linear::ResidReductionCriterion::converged
bool converged() const override
Returns true if and only if the convergence criterion is met.
Definition residreductioncriterion.hh:102
Opm::Linear::ResidReductionCriterion::setInitial
void setInitial(const Vector &, const Vector &curResid) override
Set the initial solution of the linear system of equations.
Definition residreductioncriterion.hh:77
Opm::Linear::ResidReductionCriterion::setTolerance
void setTolerance(Scalar tol)
Set the maximum allowed weighted maximum of the reduction of the linear residual.
Definition residreductioncriterion.hh:65
Opm::Linear::ResidReductionCriterion::tolerance
Scalar tolerance() const
Return the maximum allowed weighted maximum of the reduction of the linear residual.
Definition residreductioncriterion.hh:71
Opm::Linear::ResidReductionCriterion::update
void update(const Vector &, const Vector &, const Vector &curResid) override
Definition residreductioncriterion.hh:91
convergencecriterion.hh
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43