27 #ifndef EWOMS_BICG_STAB_SOLVER_HH 28 #define EWOMS_BICG_STAB_SOLVER_HH 37 #include <opm/common/Exceptions.hpp> 52 template <
class LinearOperator,
class Vector,
class Preconditioner>
56 using Scalar =
typename LinearOperator::field_type;
61 Dune::ScalarProduct<Vector>& scalarProduct)
62 : preconditioner_(preconditioner)
63 , convergenceCriterion_(convergenceCriterion)
64 , scalarProduct_(scalarProduct)
69 maxIterations_ = 1000;
77 { maxIterations_ = value; }
84 {
return maxIterations_; }
97 { verbosity_ = value; }
103 {
return verbosity_; }
123 const Scalar breakdownEps = std::numeric_limits<Scalar>::min() * Scalar(1e10);
131 report_.timer().
start();
145 preconditioner_.pre(x, r);
152 for (
unsigned i = 0; i < x.size(); ++i) {
153 const auto& u = x[i];
155 throw std::logic_error(
"The preconditioner is assumed not to modify the initial solution!");
161 report_.setConverged(
true);
162 return report_.converged();
165 if (verbosity_ > 0) {
166 std::cout <<
"-------- BiCGStabSolver --------" << std::endl;
174 const Vector& r0hat = *b_;
193 unsigned n = x.size();
195 for (; report_.iterations() < maxIterations_; report_.increment()) {
197 Scalar rho_i = scalarProduct_.dot(r0hat, r);
200 if (std::abs(rho) <= breakdownEps || std::abs(omega) <= breakdownEps)
201 throw NumericalProblem(
"Breakdown of the BiCGStab solver (division by zero)");
202 Scalar beta = (rho_i/rho)*(alpha/omega);
211 for (
unsigned i = 0; i < n; ++i) {
225 preconditioner_.apply(y, p);
231 Scalar denom = scalarProduct_.dot(r0hat, v);
232 if (std::abs(denom) <= breakdownEps)
233 throw NumericalProblem(
"Breakdown of the BiCGStab solver (division by zero)");
235 if (std::abs(alpha) <= breakdownEps)
236 throw NumericalProblem(
"Breakdown of the BiCGStab solver (stagnation detected)");
240 for (
unsigned i = 0; i < n; ++i) {
253 convergenceCriterion_.
update(h, y, s);
255 if (verbosity_ > 0) {
256 convergenceCriterion_.
print(report_.iterations() + 0.5);
257 std::cout <<
"-------- /BiCGStabSolver --------" << std::endl;
261 preconditioner_.post(x);
262 report_.setConverged(
true);
263 return report_.converged();
265 else if (convergenceCriterion_.
failed()) {
266 if (verbosity_ > 0) {
267 convergenceCriterion_.
print(report_.iterations() + 0.5);
268 std::cout <<
"-------- /BiCGStabSolver --------" << std::endl;
271 report_.setConverged(
false);
272 return report_.converged();
276 convergenceCriterion_.
print(report_.iterations() + 0.5);
280 preconditioner_.apply(z, s);
287 denom = scalarProduct_.dot(t, t);
288 if (std::abs(denom) <= breakdownEps)
289 throw NumericalProblem(
"Breakdown of the BiCGStab solver (division by zero)");
290 omega = scalarProduct_.dot(t, s)/denom;
291 if (std::abs(omega) <= breakdownEps)
292 throw NumericalProblem(
"Breakdown of the BiCGStab solver (stagnation detected)");
299 convergenceCriterion_.
update(x, z, r);
301 if (verbosity_ > 0) {
302 convergenceCriterion_.
print(1.0 + report_.iterations());
303 std::cout <<
"-------- /BiCGStabSolver --------" << std::endl;
306 preconditioner_.post(x);
307 report_.setConverged(
true);
308 return report_.converged();
310 else if (convergenceCriterion_.
failed()) {
311 if (verbosity_ > 0) {
312 convergenceCriterion_.
print(1.0 + report_.iterations());
313 std::cout <<
"-------- /BiCGStabSolver --------" << std::endl;
316 report_.setConverged(
false);
317 return report_.converged();
321 convergenceCriterion_.
print(1.0 + report_.iterations());
328 report_.setConverged(
false);
329 return report_.converged();
334 convergenceCriterion_ = &crit;
337 const SolverReport& report()
const 341 const LinearOperator* A_;
344 Preconditioner& preconditioner_;
345 ConvergenceCriterion& convergenceCriterion_;
346 Dune::ScalarProduct<Vector>& scalarProduct_;
347 SolverReport report_;
349 unsigned maxIterations_;
Base class for all convergence criteria which only defines an virtual API.
Definition: convergencecriterion.hh:55
unsigned maxIterations() const
Return the maximum number of iterations before we give up without achieving convergence.
Definition: bicgstabsolver.hh:83
void start()
Start counting the time resources used by the simulation.
Definition: timer.cpp:46
void setVerbosity(unsigned value)
Set the verbosity level of the linear solver.
Definition: bicgstabsolver.hh:96
unsigned verbosity() const
Return the verbosity level of the linear solver.
Definition: bicgstabsolver.hh:102
void setRhs(const Vector *b)
Set the right hand side "b" of the linear system.
Definition: bicgstabsolver.hh:114
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
virtual bool converged() const =0
Returns true if and only if the convergence criterion is met.
Provides a convergence criterion which looks at the reduction of the two-norm of the residual for the...
virtual void print(Scalar, std::ostream &=std::cout) const
Prints the information about the convergence behaviour for the current iteration. ...
Definition: convergencecriterion.hh:146
Collects summary information about the execution of the linear solver.
bool apply(Vector &x)
Run the stabilized BiCG solver and store the result into the "x" vector.
Definition: bicgstabsolver.hh:120
void setMaxIterations(unsigned value)
Set the maximum number of iterations before we give up without achieving convergence.
Definition: bicgstabsolver.hh:76
virtual bool failed() const
Returns true if the convergence criterion cannot be met anymore because the solver has broken down...
Definition: convergencecriterion.hh:115
Implements a preconditioned stabilized BiCG linear solver.
Definition: bicgstabsolver.hh:53
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Definition: timerguard.hh:41
void setLinearOperator(const LinearOperator *A)
Set the matrix "A" of the linear system.
Definition: bicgstabsolver.hh:108
Provides an encapsulation to measure the system time.
virtual void update(const Vector &curSol, const Vector &changeIndicator, const Vector &curResid)=0
Update the internal members of the convergence criterion with the current solution.
A simple class which makes sure that a timer gets stopped if an exception is thrown.
virtual void printInitial(std::ostream &=std::cout) const
Prints the initial information about the convergence behaviour.
Definition: convergencecriterion.hh:135
virtual void setInitial(const Vector &curSol, const Vector &curResid)=0
Set the initial solution of the linear system of equations.