FlexibleSolver_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2019, 2020 SINTEF Digital, Mathematics and Cybernetics.
3 Copyright 2020 Equinor.
4
5 This file is part of the Open Porous Media project (OPM).
6
7 OPM is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 OPM is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with OPM. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
22#define OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
23
24#include <opm/common/ErrorMacros.hpp>
25#include <opm/common/TimingMacros.hpp>
34
35#include <dune/common/fmatrix.hh>
36#include <dune/istl/bcrsmatrix.hh>
37#include <dune/istl/solvers.hh>
38#include <dune/istl/umfpack.hh>
39#include <dune/istl/owneroverlapcopy.hh>
40#include <dune/istl/paamg/pinfo.hh>
41#include <type_traits>
42
43#if HAVE_CUDA
44#if USE_HIP
45#include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
46#else
48#endif
49#endif
50
51namespace Dune
52{
54 template <class Operator>
56 FlexibleSolver(Operator& op,
57 const Opm::PropertyTree& prm,
58 const std::function<VectorType()>& weightsCalculator,
59 std::size_t pressureIndex)
60 {
61 init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
62 pressureIndex);
63 }
64
66 template <class Operator>
67 template <class Comm>
69 FlexibleSolver(Operator& op,
70 const Comm& comm,
71 const Opm::PropertyTree& prm,
72 const std::function<VectorType()>& weightsCalculator,
73 std::size_t pressureIndex)
74 {
75 init(op, comm, prm, weightsCalculator, pressureIndex);
76 }
77
78 template <class Operator>
79 void
82 {
83 if (direct_solver_) {
84 recreateDirectSolver();
85 }
86 linsolver_->apply(x, rhs, res);
87 }
88
89 template <class Operator>
90 void
92 apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
93 {
94 if (direct_solver_) {
95 recreateDirectSolver();
96 }
97 linsolver_->apply(x, rhs, reduction, res);
98 }
99
101 template <class Operator>
102 auto
105 {
106 return *preconditioner_;
107 }
108
109 template <class Operator>
110 Dune::SolverCategory::Category
112 category() const
113 {
114 return linearoperator_for_solver_->category();
115 }
116
117 // Machinery for making sequential or parallel operators/preconditioners/scalar products.
118 template <class Operator>
119 template <class Comm>
120 void
122 initOpPrecSp(Operator& op,
123 const Opm::PropertyTree& prm,
124 const std::function<VectorType()> weightsCalculator,
125 const Comm& comm,
126 std::size_t pressureIndex)
127 {
128 // Parallel case.
129 linearoperator_for_solver_ = &op;
130 auto child = prm.get_child_optional("preconditioner");
132 child ? *child : Opm::PropertyTree(),
133 weightsCalculator,
134 comm,
135 pressureIndex);
136 scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
137 }
138
139 template <class Operator>
140 void
141 FlexibleSolver<Operator>::
142 initOpPrecSp(Operator& op,
143 const Opm::PropertyTree& prm,
144 const std::function<VectorType()> weightsCalculator,
145 const Dune::Amg::SequentialInformation&,
146 std::size_t pressureIndex)
147 {
148 // Sequential case.
149 linearoperator_for_solver_ = &op;
150 auto child = prm.get_child_optional("preconditioner");
152 child ? *child : Opm::PropertyTree(),
153 weightsCalculator,
154 pressureIndex);
155 scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
156 }
157
158
159 template <class Operator>
160 template <class Comm>
161 void
162 FlexibleSolver<Operator>::
163 initSolver(const Opm::PropertyTree& prm, const Comm& comm)
164 {
165 const bool is_iorank = comm.communicator().rank() == 0;
166 const double tol = prm.get<double>("tol", 1e-2);
167 const int maxiter = prm.get<int>("maxiter", 200);
168 const int verbosity = is_iorank ? prm.get<int>("verbosity", 0) : 0;
169 const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
170
171
172 // make sure it is nullptr at the start (used for error checking in the end).
173 // while the linSolver_ is initalized as a nullptr, we want to make sure it is reset here,
174 // simply because we will check if it is at the end of this function and need to keep this invariant
175 // (that it is nullptr at the start of this function).
176 linsolver_.reset();
177 if (solver_type == "bicgstab") {
178 linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
179 *scalarproduct_,
180 *preconditioner_,
181 tol, // desired residual reduction factor
182 maxiter, // maximum number of iterations
183 verbosity);
184 } else if (solver_type == "loopsolver") {
185 linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
186 *scalarproduct_,
187 *preconditioner_,
188 tol, // desired residual reduction factor
189 maxiter, // maximum number of iterations
190 verbosity);
191 } else if (solver_type == "gmres") {
192 int restart = prm.get<int>("restart", 15);
193 linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
194 *scalarproduct_,
195 *preconditioner_,
196 tol,// desired residual reduction factor
197 restart,
198 maxiter, // maximum number of iterations
199 verbosity);
200
201 } else {
202 if constexpr (!Opm::is_gpu_operator_v<Operator>) {
203 if (solver_type == "flexgmres") {
204 int restart = prm.get<int>("restart", 15);
205 linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
206 *scalarproduct_,
207 *preconditioner_,
208 tol,// desired residual reduction factor
209 restart,
210 maxiter, // maximum number of iterations
211 verbosity);
212#if HAVE_SUITESPARSE_UMFPACK
213 } else if (solver_type == "umfpack") {
214 if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
215 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
216 } else {
217 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
218 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
219 direct_solver_ = true;
220 }
221#endif
222#if HAVE_CUDA
223 } else if (solver_type == "gpubicgstab") {
225 *linearoperator_for_solver_,
226 *scalarproduct_,
227 preconditioner_,
228 tol, // desired residual reduction factor
229 maxiter, // maximum number of iterations
230 verbosity,
231 comm));
232 #endif
233 }
234 }
235 }
236 if (!linsolver_) {
237 OPM_THROW(std::invalid_argument,
238 "Properties: Solver " + solver_type + " not known.");
239 }
240 }
241
242
243 // For now, the only direct solver we support is UMFPACK from SuiteSparse.
244 // When the matrix is updated (keeping sparsity pattern) it is possible to
245 // exploit separation of symbolic and numeric factorization, but we do not
246 // do so at this point. For complete generality, the solver abstract class
247 // Dune::InverseOperator<> should be extended with an update() function.
248 template <class Operator>
249 void
250 FlexibleSolver<Operator>::
251 recreateDirectSolver()
252 {
253#if HAVE_SUITESPARSE_UMFPACK
254 if constexpr (!Opm::is_gpu_operator_v<Operator>) {
255 if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
256 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
257 } else {
258 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
259 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
260 }
261 }
262#else
263 OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
264#endif
265 }
266
267
268 // Main initialization routine.
269 // Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
270 template <class Operator>
271 template <class Comm>
272 void
273 FlexibleSolver<Operator>::
274 init(Operator& op,
275 const Comm& comm,
276 const Opm::PropertyTree& prm,
277 const std::function<VectorType()> weightsCalculator,
278 std::size_t pressureIndex)
279 {
280 initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
281 initSolver(prm, comm);
282 }
283
284} // namespace Dune
285
286
287// Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
288
289// Vectors and matrices.
290template<class Scalar, int N>
291using BV = Dune::BlockVector<Dune::FieldVector<Scalar, N>>;
292template<class Scalar, int N>
293using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<Scalar, N, N>>;
294
295// Sequential operators.
296template<class Scalar, int N>
297using SeqOpM = Dune::MatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>>;
298template<class Scalar, int N>
300
301#if HAVE_MPI
302
303// Parallel communicator and operators.
304using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
305template<class Scalar, int N>
307template<class Scalar, int N>
309template<class Scalar, int N>
310using ParOpD = Dune::OverlappingSchwarzOperator<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, Comm>;
311
312// Note: we must instantiate the constructor that is a template.
313// This is only needed in the parallel case, since otherwise the Comm type is
314// not a template argument but always SequentialInformation.
315
316#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
317 template class Dune::FlexibleSolver<__VA_ARGS__>; \
318 template Dune::FlexibleSolver<__VA_ARGS__>:: \
319 FlexibleSolver(__VA_ARGS__& op, \
320 const Comm& comm, \
321 const Opm::PropertyTree& prm, \
322 const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
323 std::size_t pressureIndex);
324
325#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
326 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
327 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
328 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
329 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
330 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
331
332#else // HAVE_MPI
333
334#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
335 template class Dune::FlexibleSolver<__VA_ARGS__>;
336
337#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
338 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
339 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
340
341#endif // HAVE_MPI
342
343#endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
Dune::OverlappingSchwarzOperator< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N >, Comm > ParOpD
Definition: FlexibleSolver_impl.hpp:310
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, N, N > > OBM
Definition: FlexibleSolver_impl.hpp:293
Dune::BlockVector< Dune::FieldVector< Scalar, N > > BV
Definition: FlexibleSolver_impl.hpp:291
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: FlexibleSolver_impl.hpp:304
Dune::MatrixAdapter< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N > > SeqOpM
Definition: FlexibleSolver_impl.hpp:297
Definition: FlexibleSolver.hpp:45
virtual Dune::SolverCategory::Category category() const override
Definition: FlexibleSolver_impl.hpp:112
virtual void apply(VectorType &x, VectorType &rhs, Dune::InverseOperatorResult &res) override
Definition: FlexibleSolver_impl.hpp:81
typename Operator::domain_type VectorType
Definition: FlexibleSolver.hpp:47
FlexibleSolver(Operator &op, const Opm::PropertyTree &prm, const std::function< VectorType()> &weightsCalculator, std::size_t pressureIndex)
Create a sequential solver.
Definition: FlexibleSolver_impl.hpp:56
AbstractPrecondType & preconditioner()
Access the contained preconditioner.
Definition: FlexibleSolver_impl.hpp:104
Dune linear operator that assumes ghost rows are ordered after interior rows. Avoids some computation...
Definition: WellOperators.hpp:402
static PrecPtr create(const Operator &op, const PropertyTree &prm, const std::function< Vector()> &weightsCalculator={}, std::size_t pressureIndex=std::numeric_limits< std::size_t >::max())
Definition: PreconditionerFactory_impl.hpp:150
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:39
T get(const std::string &key) const
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:299
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:225
Wraps a CUDA solver to work with CPU data.
Definition: SolverAdapter.hpp:57
Definition: fvbaseprimaryvariables.hh:141
Dune::InverseOperatorResult InverseOperatorResult
Definition: GpuBridge.hpp:32