opm-simulators
AbstractISTLSolver.hpp
1 /*
2  Copyright 2025 Equinor ASA
3 
4  This file is part of the Open Porous Media project (OPM).
5  OPM is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9  OPM is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13  You should have received a copy of the GNU General Public License
14  along with OPM. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
17 #ifndef OPM_ABSTRACTISTLSOLVER_HEADER_INCLUDED
18 #define OPM_ABSTRACTISTLSOLVER_HEADER_INCLUDED
19 
20 #include <opm/common/Exceptions.hpp>
21 #include <opm/simulators/linalg/FlowLinearSolverParameters.hpp>
22 #include <opm/simulators/linalg/PropertyTree.hpp>
23 
24 namespace Opm
25 {
26 
43 template <class SparseMatrixAdapter, class Vector>
45 {
46 public:
47 #if HAVE_MPI
48  using CommunicationType = Dune::OwnerOverlapCopyCommunication<int, int>;
49 #else
50  using CommunicationType = Dune::Communication<int>;
51 #endif
52 
53  using Matrix = typename SparseMatrixAdapter::IstlMatrix;
54 
55  virtual ~AbstractISTLSolver() = default;
56 
63  virtual void eraseMatrix() = 0;
64 
72  virtual void setActiveSolver(int num) = 0;
73 
79  virtual int numAvailableSolvers() const = 0;
80 
90  virtual void prepare(const Matrix& M, Vector& b) = 0;
91 
103  virtual void prepare(const SparseMatrixAdapter& M, Vector& b) = 0;
104 
114  virtual void setResidual(Vector& b) = 0;
115 
123  virtual void getResidual(Vector& b) const = 0;
124 
134  virtual void setMatrix(const SparseMatrixAdapter& M) = 0;
135 
150  virtual bool solve(Vector& x) = 0;
151 
161  virtual int iterations() const = 0;
162 
170  virtual const CommunicationType* comm() const = 0;
171 
179  virtual int getSolveCount() const = 0;
180 
181 protected:
182 
192  static bool checkConvergence(const Dune::InverseOperatorResult& result,
193  const FlowLinearSolverParameters& parameters)
194  {
195  if (!result.converged) {
196  if (result.reduction < parameters.relaxed_linear_solver_reduction_) {
197  std::stringstream ss;
198  ss << "Full linear solver tolerance not achieved. The reduction is:" << result.reduction << " after "
199  << result.iterations << " iterations ";
200  OpmLog::warning(ss.str());
201  return true;
202  }
203  }
204  // Check for failure of linear solver.
205  if (!parameters.ignoreConvergenceFailure_ && !result.converged) {
206  const std::string msg("Convergence failure for linear solver.");
207  OPM_THROW_NOLOG(NumericalProblem, msg);
208  }
209 
210  return result.converged;
211  }
212 };
213 } // namespace Opm
214 
215 #endif
virtual void prepare(const Matrix &M, Vector &b)=0
Prepare the solver with the given matrix and right-hand side vector.
static bool checkConvergence(const Dune::InverseOperatorResult &result, const FlowLinearSolverParameters &parameters)
Check the convergence of the linear solver.
Definition: AbstractISTLSolver.hpp:192
virtual int numAvailableSolvers() const =0
Get the number of available solvers.
virtual void setMatrix(const SparseMatrixAdapter &M)=0
Set the matrix for the solver.
virtual bool solve(Vector &x)=0
Solve the system of equations Ax = b.
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
virtual const CommunicationType * comm() const =0
Get the communication object used by the solver.
virtual void setResidual(Vector &b)=0
Set the residual vector.
virtual int iterations() const =0
Get the number of iterations used in the last solve.
virtual void setActiveSolver(int num)=0
Set the active solver by its index.
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition: FlowLinearSolverParameters.hpp:97
virtual int getSolveCount() const =0
Get the count of how many times the solver has been called.
Abstract interface for ISTL solvers.
Definition: AbstractISTLSolver.hpp:44
virtual void getResidual(Vector &b) const =0
Get the residual vector.
virtual void eraseMatrix()=0
Signals that the memory for the matrix internally in the solver could be erased.