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>
32
33#include <dune/common/fmatrix.hh>
34#include <dune/istl/bcrsmatrix.hh>
35#include <dune/istl/solvers.hh>
36#include <dune/istl/umfpack.hh>
37#include <dune/istl/owneroverlapcopy.hh>
38#include <dune/istl/paamg/pinfo.hh>
39
40#if HAVE_CUDA
41#if USE_HIP
42#include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
43#else
45#endif
46#endif
47
48namespace Dune
49{
51 template <class Operator>
53 FlexibleSolver(Operator& op,
54 const Opm::PropertyTree& prm,
55 const std::function<VectorType()>& weightsCalculator,
56 std::size_t pressureIndex)
57 {
58 init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
59 pressureIndex);
60 }
61
63 template <class Operator>
64 template <class Comm>
66 FlexibleSolver(Operator& op,
67 const Comm& comm,
68 const Opm::PropertyTree& prm,
69 const std::function<VectorType()>& weightsCalculator,
70 std::size_t pressureIndex)
71 {
72 init(op, comm, prm, weightsCalculator, pressureIndex);
73 }
74
75 template <class Operator>
76 void
79 {
80 if (direct_solver_) {
81 recreateDirectSolver();
82 }
83 linsolver_->apply(x, rhs, res);
84 }
85
86 template <class Operator>
87 void
89 apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
90 {
91 if (direct_solver_) {
92 recreateDirectSolver();
93 }
94 linsolver_->apply(x, rhs, reduction, res);
95 }
96
98 template <class Operator>
99 auto
102 {
103 return *preconditioner_;
104 }
105
106 template <class Operator>
107 Dune::SolverCategory::Category
109 category() const
110 {
111 return linearoperator_for_solver_->category();
112 }
113
114 // Machinery for making sequential or parallel operators/preconditioners/scalar products.
115 template <class Operator>
116 template <class Comm>
117 void
119 initOpPrecSp(Operator& op,
120 const Opm::PropertyTree& prm,
121 const std::function<VectorType()> weightsCalculator,
122 const Comm& comm,
123 std::size_t pressureIndex)
124 {
125 // Parallel case.
126 linearoperator_for_solver_ = &op;
127 auto child = prm.get_child_optional("preconditioner");
129 child ? *child : Opm::PropertyTree(),
130 weightsCalculator,
131 comm,
132 pressureIndex);
133 scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
134 }
135
136 template <class Operator>
137 void
138 FlexibleSolver<Operator>::
139 initOpPrecSp(Operator& op,
140 const Opm::PropertyTree& prm,
141 const std::function<VectorType()> weightsCalculator,
142 const Dune::Amg::SequentialInformation&,
143 std::size_t pressureIndex)
144 {
145 // Sequential case.
146 linearoperator_for_solver_ = &op;
147 auto child = prm.get_child_optional("preconditioner");
149 child ? *child : Opm::PropertyTree(),
150 weightsCalculator,
151 pressureIndex);
152 scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
153 }
154
155 template <class Operator>
156 template <class Comm>
157 void
158 FlexibleSolver<Operator>::
159 initSolver(const Opm::PropertyTree& prm, const Comm& comm)
160 {
161 const bool is_iorank = comm.communicator().rank() == 0;
162 const double tol = prm.get<double>("tol", 1e-2);
163 const int maxiter = prm.get<int>("maxiter", 200);
164 const int verbosity = is_iorank ? prm.get<int>("verbosity", 0) : 0;
165 const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
166 if (solver_type == "bicgstab") {
167 linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
168 *scalarproduct_,
169 *preconditioner_,
170 tol, // desired residual reduction factor
171 maxiter, // maximum number of iterations
172 verbosity);
173 } else if (solver_type == "loopsolver") {
174 linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
175 *scalarproduct_,
176 *preconditioner_,
177 tol, // desired residual reduction factor
178 maxiter, // maximum number of iterations
179 verbosity);
180 } else if (solver_type == "gmres") {
181 int restart = prm.get<int>("restart", 15);
182 linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
183 *scalarproduct_,
184 *preconditioner_,
185 tol,// desired residual reduction factor
186 restart,
187 maxiter, // maximum number of iterations
188 verbosity);
189 } else if (solver_type == "flexgmres") {
190 int restart = prm.get<int>("restart", 15);
191 linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
192 *scalarproduct_,
193 *preconditioner_,
194 tol,// desired residual reduction factor
195 restart,
196 maxiter, // maximum number of iterations
197 verbosity);
198#if HAVE_SUITESPARSE_UMFPACK
199 } else if (solver_type == "umfpack") {
200 if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
201 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
202 } else {
203 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
204 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
205 direct_solver_ = true;
206 }
207#endif
208#if HAVE_CUDA
209 } else if (solver_type == "gpubicgstab") {
211 *linearoperator_for_solver_,
212 *scalarproduct_,
213 preconditioner_,
214 tol, // desired residual reduction factor
215 maxiter, // maximum number of iterations
216 verbosity,
217 comm));
218#endif
219 } else {
220 OPM_THROW(std::invalid_argument,
221 "Properties: Solver " + solver_type + " not known.");
222 }
223 }
224
225
226 // For now, the only direct solver we support is UMFPACK from SuiteSparse.
227 // When the matrix is updated (keeping sparsity pattern) it is possible to
228 // exploit separation of symbolic and numeric factorization, but we do not
229 // do so at this point. For complete generality, the solver abstract class
230 // Dune::InverseOperator<> should be extended with an update() function.
231 template <class Operator>
232 void
233 FlexibleSolver<Operator>::
234 recreateDirectSolver()
235 {
236#if HAVE_SUITESPARSE_UMFPACK
237 if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
238 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
239 } else {
240 using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
241 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
242 }
243#else
244 OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
245#endif
246 }
247
248
249 // Main initialization routine.
250 // Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
251 template <class Operator>
252 template <class Comm>
253 void
254 FlexibleSolver<Operator>::
255 init(Operator& op,
256 const Comm& comm,
257 const Opm::PropertyTree& prm,
258 const std::function<VectorType()> weightsCalculator,
259 std::size_t pressureIndex)
260 {
261 initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
262 initSolver(prm, comm);
263 }
264
265} // namespace Dune
266
267
268// Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
269
270// Vectors and matrices.
271template<class Scalar, int N>
272using BV = Dune::BlockVector<Dune::FieldVector<Scalar, N>>;
273template<class Scalar, int N>
274using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<Scalar, N, N>>;
275
276// Sequential operators.
277template<class Scalar, int N>
278using SeqOpM = Dune::MatrixAdapter<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>>;
279template<class Scalar, int N>
281
282#if HAVE_MPI
283
284// Parallel communicator and operators.
285using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
286template<class Scalar, int N>
288template<class Scalar, int N>
290template<class Scalar, int N>
291using ParOpD = Dune::OverlappingSchwarzOperator<OBM<Scalar,N>, BV<Scalar,N>, BV<Scalar,N>, Comm>;
292
293// Note: we must instantiate the constructor that is a template.
294// This is only needed in the parallel case, since otherwise the Comm type is
295// not a template argument but always SequentialInformation.
296
297#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
298 template class Dune::FlexibleSolver<__VA_ARGS__>; \
299 template Dune::FlexibleSolver<__VA_ARGS__>:: \
300 FlexibleSolver(__VA_ARGS__& op, \
301 const Comm& comm, \
302 const Opm::PropertyTree& prm, \
303 const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
304 std::size_t pressureIndex);
305
306#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
307 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
308 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
309 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
310 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
311 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
312
313#else // HAVE_MPI
314
315#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
316 template class Dune::FlexibleSolver<__VA_ARGS__>;
317
318#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
319 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
320 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
321
322#endif // HAVE_MPI
323
324#endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
Dune::OverlappingSchwarzOperator< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N >, Comm > ParOpD
Definition: FlexibleSolver_impl.hpp:291
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, N, N > > OBM
Definition: FlexibleSolver_impl.hpp:274
Dune::BlockVector< Dune::FieldVector< Scalar, N > > BV
Definition: FlexibleSolver_impl.hpp:272
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: FlexibleSolver_impl.hpp:285
Dune::MatrixAdapter< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N > > SeqOpM
Definition: FlexibleSolver_impl.hpp:278
Definition: FlexibleSolver.hpp:45
virtual Dune::SolverCategory::Category category() const override
Definition: FlexibleSolver_impl.hpp:109
virtual void apply(VectorType &x, VectorType &rhs, Dune::InverseOperatorResult &res) override
Definition: FlexibleSolver_impl.hpp:78
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:53
AbstractPrecondType & preconditioner()
Access the contained preconditioner.
Definition: FlexibleSolver_impl.hpp:101
Dune linear operator that assumes ghost rows are ordered after interior rows. Avoids some computation...
Definition: WellOperators.hpp:340
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:735
Definition: PropertyTree.hpp:37
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:237
Adapter to combine a matrix and another linear operator into a combined linear operator.
Definition: WellOperators.hpp:143
Wraps a CUDA solver to work with CPU data.
Definition: SolverAdapter.hpp:52
Definition: fvbaseprimaryvariables.hh:141
Dune::InverseOperatorResult InverseOperatorResult
Definition: BdaBridge.hpp:32