AbstractISTLSolver.hpp
Go to the documentation of this file.
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>
23
24namespace Opm
25{
26
43template <class SparseMatrixAdapter, class Vector>
45{
46public:
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
181protected:
182
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
Abstract interface for ISTL solvers.
Definition: AbstractISTLSolver.hpp:45
virtual bool solve(Vector &x)=0
Solve the system of equations Ax = b.
virtual void getResidual(Vector &b) const =0
Get the residual vector.
virtual void prepare(const Matrix &M, Vector &b)=0
Prepare the solver with the given matrix and right-hand side vector.
virtual void setActiveSolver(int num)=0
Set the active solver by its index.
virtual int iterations() const =0
Get the number of iterations used in the last solve.
virtual void setMatrix(const SparseMatrixAdapter &M)=0
Set the matrix for the solver.
virtual void eraseMatrix()=0
Signals that the memory for the matrix internally in the solver could be erased.
virtual int numAvailableSolvers() const =0
Get the number of available solvers.
virtual int getSolveCount() const =0
Get the count of how many times the solver has been called.
Dune::OwnerOverlapCopyCommunication< int, int > CommunicationType
Definition: AbstractISTLSolver.hpp:48
typename SparseMatrixAdapter::IstlMatrix Matrix
Definition: AbstractISTLSolver.hpp:53
virtual void prepare(const SparseMatrixAdapter &M, Vector &b)=0
Prepare the solver with the given sparse matrix and right-hand side vector.
virtual ~AbstractISTLSolver()=default
virtual void setResidual(Vector &b)=0
Set the residual vector.
virtual const CommunicationType * comm() const =0
Get the communication object used by the solver.
static bool checkConvergence(const Dune::InverseOperatorResult &result, const FlowLinearSolverParameters &parameters)
Check the convergence of the linear solver.
Definition: AbstractISTLSolver.hpp:192
Definition: blackoilbioeffectsmodules.hh:45
Dune::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32
This class carries all parameters for the NewtonIterationBlackoilInterleaved class.
Definition: FlowLinearSolverParameters.hpp:98
bool ignoreConvergenceFailure_
Definition: FlowLinearSolverParameters.hpp:110
double relaxed_linear_solver_reduction_
Definition: FlowLinearSolverParameters.hpp:100