Go to the documentation of this file.
21#ifndef OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
22#define OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
24#include <opm/common/ErrorMacros.hpp>
25#include <opm/common/TimingMacros.hpp>
36#if HAVE_AVX2_EXTENSION
41#include <dune/common/fmatrix.hh>
42#include <dune/istl/bcrsmatrix.hh>
43#include <dune/istl/multitypeblockvector.hh>
44#include <dune/istl/solvers.hh>
45#include <dune/istl/umfpack.hh>
46#include <dune/istl/owneroverlapcopy.hh>
47#include <dune/istl/paamg/pinfo.hh>
53template < typename... Args>
61#include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
70 template < class Operator>
74 const std::function< VectorType()>& weightsCalculator,
75 std::size_t pressureIndex)
77 init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
82 template < class Operator>
88 const std::function< VectorType()>& weightsCalculator,
89 std::size_t pressureIndex)
91 init(op, comm, prm, weightsCalculator, pressureIndex);
94 template < class Operator>
101 if (direct_precond && direct_precond->needsRebuild()) {
102 recreateDirectSolver();
103 direct_precond->resetNeedsRebuild();
106 linsolver_->apply(x, rhs, res);
109 template < class Operator>
114 if (direct_solver_) {
116 if (direct_precond && direct_precond->needsRebuild()) {
117 recreateDirectSolver();
118 direct_precond->resetNeedsRebuild();
121 linsolver_->apply(x, rhs, reduction, res);
125 template < class Operator>
130 return *preconditioner_;
133 template < class Operator>
134 Dune::SolverCategory::Category
138 return linearoperator_for_solver_->category();
142 template < class Operator>
143 template < class Comm>
148 const std::function<VectorType()> weightsCalculator,
150 std::size_t pressureIndex)
153 linearoperator_for_solver_ = &op;
154 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
156 if (solver_type == "umfpack") {
157 preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
158 linearoperator_for_solver_->category());
166 scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
169 template < class Operator>
171 FlexibleSolver<Operator>::
172 initOpPrecSp(Operator& op,
174 const std::function<VectorType()> weightsCalculator,
175 const Dune::Amg::SequentialInformation&,
176 std::size_t pressureIndex)
179 linearoperator_for_solver_ = &op;
180 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
182 if (solver_type == "umfpack") {
183 preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
184 linearoperator_for_solver_->category());
191 scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
195 template < class Operator>
196 template < class Comm>
198 FlexibleSolver<Operator>::
201 const bool is_iorank = comm.communicator().rank() == 0;
202 const double tol = prm. get< double>( "tol", 1e-2);
203 const int maxiter = prm. get< int>( "maxiter", 200);
204 const int verbosity = is_iorank ? prm. get< int>( "verbosity", 0) : 0;
205 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
213 direct_solver_ = false;
214 if (solver_type == "bicgstab") {
215 linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
221#if HAVE_AVX2_EXTENSION
222 } else if (solver_type == "mixed-bicgstab") {
223 if constexpr (Opm::is_gpu_operator_v<Operator>) {
224 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for GPU operators");
225 } else if constexpr (Opm::detail::is_multi_type_block_vector_v<VectorType>) {
226 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for multi-type block vectors.");
227 } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
228 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for single precision.");
230 const std::string prec_type = prm. get<std::string>( "preconditioner.type", "error");
231 bool use_mixed_dilu= (prec_type== "legacy-mixed-dilu");
232 using MatrixType = decltype(linearoperator_for_solver_->getmat());
233 linsolver_ = std::make_shared<Dune::MixedSolver<VectorType,MatrixType>>(
234 linearoperator_for_solver_->getmat(),
241 } else if (solver_type == "mixed-precision") {
242 if constexpr (Opm::is_gpu_operator_v<Operator>) {
243 OPM_THROW(std::invalid_argument, "mixed-precision solver not supported for GPU operators");
244 } else if constexpr (Opm::detail::is_multi_type_block_vector_v<VectorType>) {
245 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for multi-type block vectors.");
246 } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
247 OPM_THROW(std::invalid_argument, "mixed-precision solver not supported for single precision.");
249 linsolver_ = std::make_shared<Dune::MixedBiCGSTABSolver<Comm,Operator,VectorType>>(linearoperator_for_solver_,
258 } else if (solver_type == "cg") {
259 linsolver_ = std::make_shared<Dune::CGSolver<VectorType>>(*linearoperator_for_solver_,
265 } else if (solver_type == "loopsolver") {
266 linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
272 } else if (solver_type == "gmres") {
273 int restart = prm. get< int>( "restart", 15);
274 linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
281 } else if (solver_type == "flexgmres") {
282 if constexpr (Opm::is_gpu_operator_v<Operator>) {
283 OPM_THROW(std::invalid_argument, "flexgmres solver not supported for GPU operators.");
285 int restart = prm. get< int>( "restart", 15);
286 linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
294 } else if (solver_type == "preconditioner2inverseoperator") {
295 if (!preconditioner_) {
296 OPM_THROW(std::invalid_argument,
297 "Properties: Solver preconditioner2inverseoperator requires a preconditioner.");
299 linsolver_ = std::make_shared<Dune::Preconditioner2InverseOperator<VectorType>>(preconditioner_);
301 if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
302#if HAVE_SUITESPARSE_UMFPACK
303 if (solver_type == "umfpack") {
304 if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
305 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
307 using MatrixType = std::remove_const_t<std::remove_reference_t< decltype(linearoperator_for_solver_->getmat())>>;
308 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
309 direct_solver_ = true;
313 } else if (solver_type == "gpubicgstab") {
315 *linearoperator_for_solver_,
327 OPM_THROW(std::invalid_argument,
328 "Properties: Solver " + solver_type + " not known.");
338 template < class Operator>
340 FlexibleSolver<Operator>::
341 recreateDirectSolver()
343#if HAVE_SUITESPARSE_UMFPACK
344 if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
345 if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
346 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
348 using MatrixType = std::remove_const_t<std::remove_reference_t< decltype(linearoperator_for_solver_->getmat())>>;
349 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
353 OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
360 template < class Operator>
361 template < class Comm>
367 const std::function< VectorType()> weightsCalculator,
368 std::size_t pressureIndex)
370 initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
371 initSolver(prm, comm);
380template< class Scalar, int N>
381using BV = Dune::BlockVector<Dune::FieldVector<Scalar, N>>;
382template< class Scalar, int N>
383using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<Scalar, N, N>>;
386template< class Scalar, int N>
388template< class Scalar, int N>
394using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
395template< class Scalar, int N>
397template< class Scalar, int N>
399template< class Scalar, int N>
406#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
407 template class Dune::FlexibleSolver<__VA_ARGS__>; \
408 template Dune::FlexibleSolver<__VA_ARGS__>:: \
409 FlexibleSolver(__VA_ARGS__& op, \
411 const Opm::PropertyTree& prm, \
412 const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
413 std::size_t pressureIndex);
415#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
416 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
417 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
418 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
419 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
420 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
424#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
425 template class Dune::FlexibleSolver<__VA_ARGS__>;
427#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
428 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
429 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
Dune::OverlappingSchwarzOperator< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N >, Comm > ParOpD Definition: FlexibleSolver_impl.hpp:400
Dune::BCRSMatrix< Opm::MatrixBlock< Scalar, N, N > > OBM Definition: FlexibleSolver_impl.hpp:383
Dune::BlockVector< Dune::FieldVector< Scalar, N > > BV Definition: FlexibleSolver_impl.hpp:381
Dune::OwnerOverlapCopyCommunication< int, int > Comm Definition: FlexibleSolver_impl.hpp:394
Dune::MatrixAdapter< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N > > SeqOpM Definition: FlexibleSolver_impl.hpp:387
Definition: PreconditionerWithUpdate.hpp:98
Definition: FlexibleSolver.hpp:45
Dune::SolverCategory::Category category() const override Definition: FlexibleSolver_impl.hpp:136
void apply(VectorType &x, VectorType &rhs, Dune::InverseOperatorResult &res) override Definition: FlexibleSolver_impl.hpp:97
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:72
AbstractPrecondType & preconditioner() Access the contained preconditioner. Definition: FlexibleSolver_impl.hpp:128
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:154
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: gpuistl/SolverAdapter.hpp:52
Definition: fvbaseprimaryvariables.hh:161
Definition: alignedallocator.hh:32
constexpr bool is_multi_type_block_vector_v Definition: FlexibleSolver_impl.hpp:56
Dune::InverseOperatorResult InverseOperatorResult Definition: GpuBridge.hpp:32
Definition: FlexibleSolver_impl.hpp:52
|