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>
38#if HAVE_AVX2_EXTENSION
43#include <dune/common/fmatrix.hh>
44#include <dune/istl/bcrsmatrix.hh>
45#include <dune/istl/multitypeblockvector.hh>
46#include <dune/istl/solvers.hh>
47#include <dune/istl/umfpack.hh>
48#include <dune/istl/owneroverlapcopy.hh>
49#include <dune/istl/paamg/pinfo.hh>
55template < typename... Args>
63#include <opm/simulators/linalg/gpuistl_hip/SolverAdapter.hpp>
72 template < class Operator>
76 const std::function< VectorType()>& weightsCalculator,
77 std::size_t pressureIndex)
79 init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
84 template < class Operator>
90 const std::function< VectorType()>& weightsCalculator,
91 std::size_t pressureIndex)
93 init(op, comm, prm, weightsCalculator, pressureIndex);
96 template < class Operator>
101 if (direct_solver_) {
103 if (direct_precond && direct_precond->needsRebuild()) {
104 recreateDirectSolver();
105 direct_precond->resetNeedsRebuild();
108 linsolver_->apply(x, rhs, res);
111 template < class Operator>
116 if (direct_solver_) {
118 if (direct_precond && direct_precond->needsRebuild()) {
119 recreateDirectSolver();
120 direct_precond->resetNeedsRebuild();
123 linsolver_->apply(x, rhs, reduction, res);
127 template < class Operator>
132 return *preconditioner_;
135 template < class Operator>
136 Dune::SolverCategory::Category
140 return linearoperator_for_solver_->category();
144 template < class Operator>
145 template < class Comm>
150 const std::function<VectorType()> weightsCalculator,
152 std::size_t pressureIndex)
155 linearoperator_for_solver_ = &op;
156 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
158 if (solver_type == "umfpack") {
159 if (comm.communicator().size() > 1) {
160 OPM_THROW(std::invalid_argument,
161 "UMFPACK is only supported for sequential linear solves. "
162 "Please use an iterative solver for distributed matrices.");
164 preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
165 linearoperator_for_solver_->category());
173 scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
176 template < class Operator>
178 FlexibleSolver<Operator>::
179 initOpPrecSp(Operator& op,
181 const std::function<VectorType()> weightsCalculator,
182 const Dune::Amg::SequentialInformation&,
183 std::size_t pressureIndex)
186 linearoperator_for_solver_ = &op;
187 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
189 if (solver_type == "umfpack") {
190 preconditioner_ = std::make_shared<Dune::DirectSolverUpdatePreconditioner<VectorType, VectorType>>(
191 linearoperator_for_solver_->category());
198 scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
202 template < class Operator>
203 template < class Comm>
205 FlexibleSolver<Operator>::
208 const bool is_iorank = comm.communicator().rank() == 0;
209 const double tol = prm. get< double>( "tol", 1e-2);
210 const int maxiter = prm. get< int>( "maxiter", 200);
211 const int verbosity = is_iorank ? prm. get< int>( "verbosity", 0) : 0;
212 const std::string solver_type = prm. get<std::string>( "solver", "bicgstab");
220 direct_solver_ = false;
221 if (solver_type == "bicgstab") {
222 linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
228#if HAVE_AVX2_EXTENSION
229 } else if (solver_type == "mixed-legacy") {
230 if constexpr (Opm::is_gpu_operator_v<Operator>) {
231 OPM_THROW(std::invalid_argument, "legacy mixed-bicgstab solver not supported for GPU operators");
232 } else if constexpr (Opm::detail::is_multi_type_block_vector_v<VectorType>) {
233 OPM_THROW(std::invalid_argument, "legacy mixed-bicgstab solver not supported for multi-type block vectors.");
234 } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
235 OPM_THROW(std::invalid_argument, "legacy mixed-bicgstab solver not supported for single precision.");
237 const std::string prec_type = prm. get<std::string>( "preconditioner.type", "error");
238 bool use_mixed_dilu= (prec_type== "legacy-mixed-dilu");
239 using MatrixType = decltype(linearoperator_for_solver_->getmat());
240 linsolver_ = std::make_shared<Dune::MixedSolver<VectorType,MatrixType>>(
241 linearoperator_for_solver_->getmat(),
248 } else if (solver_type == "mixed-bicgstab") {
249 if constexpr (Opm::is_gpu_operator_v<Operator>) {
250 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for GPU operators");
251 } else if constexpr (Opm::detail::is_multi_type_block_vector_v<VectorType>) {
252 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for multi-type block vectors.");
253 } else if constexpr (std::is_same_v<typename VectorType::field_type, float>){
254 OPM_THROW(std::invalid_argument, "mixed-bicgstab solver not supported for single precision.");
256 linsolver_ = std::make_shared<Dune::MixedBiCGSTABSolver<Comm,Operator,VectorType>>(linearoperator_for_solver_,
265 } else if (solver_type == "cg") {
266 linsolver_ = std::make_shared<Dune::CGSolver<VectorType>>(*linearoperator_for_solver_,
272 } else if (solver_type == "loopsolver") {
273 linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
279 } else if (solver_type == "gmres") {
280 int restart = prm. get< int>( "restart", 15);
281 linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
288 } else if (solver_type == "flexgmres") {
289 if constexpr (Opm::is_gpu_operator_v<Operator>) {
290 OPM_THROW(std::invalid_argument, "flexgmres solver not supported for GPU operators.");
292 int restart = prm. get< int>( "restart", 15);
293 linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
301 } else if (solver_type == "preconditioner2inverseoperator") {
302 if (!preconditioner_) {
303 OPM_THROW(std::invalid_argument,
304 "Properties: Solver preconditioner2inverseoperator requires a preconditioner.");
306 linsolver_ = std::make_shared<Dune::Preconditioner2InverseOperator<VectorType>>(preconditioner_);
308 if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
309#if HAVE_SUITESPARSE_UMFPACK
310 if (solver_type == "umfpack") {
311 if constexpr (std::is_same_v<typename VectorType::field_type,float>) {
312 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
314 using MatrixType = std::remove_const_t<std::remove_reference_t< decltype(linearoperator_for_solver_->getmat())>>;
315 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, false);
316 direct_solver_ = true;
320 } else if (solver_type == "gpubicgstab") {
321 if constexpr (VectorType::block_type::dimension
324 Dune::BiCGSTABSolver,
326 *linearoperator_for_solver_,
339 OPM_THROW(std::invalid_argument,
340 "Properties: Solver " + solver_type + " not known.");
350 template < class Operator>
352 FlexibleSolver<Operator>::
353 recreateDirectSolver()
355#if HAVE_SUITESPARSE_UMFPACK
356 if constexpr (!Opm::is_gpu_operator_v<Operator> && !Opm::detail::is_multi_type_block_vector_v<VectorType>) {
357 if constexpr (std::is_same_v<typename VectorType::field_type, float>) {
358 OPM_THROW(std::invalid_argument, "UMFPack cannot be used with floats");
360 using MatrixType = std::remove_const_t<std::remove_reference_t< decltype(linearoperator_for_solver_->getmat())>>;
361 linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), 0, false);
365 OPM_THROW(std::logic_error, "Direct solver specified, but the FlexibleSolver class was not compiled with SuiteSparse support.");
372 template < class Operator>
373 template < class Comm>
379 const std::function< VectorType()> weightsCalculator,
380 std::size_t pressureIndex)
382 initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
383 initSolver(prm, comm);
392template< class Scalar, int N>
394template< class Scalar, int N>
398template< class Scalar, int N>
400template< class Scalar, int N>
406using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
407template< class Scalar, int N>
409template< class Scalar, int N>
411template< class Scalar, int N>
418#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
419 template class Dune::FlexibleSolver<__VA_ARGS__>; \
420 template Dune::FlexibleSolver<__VA_ARGS__>:: \
421 FlexibleSolver(__VA_ARGS__& op, \
423 const Opm::PropertyTree& prm, \
424 const std::function<typename __VA_ARGS__::domain_type()>& weightsCalculator, \
425 std::size_t pressureIndex);
427#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
428 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
429 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>); \
430 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<T,N>); \
431 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<T,N>); \
432 INSTANTIATE_FLEXIBLESOLVER_OP(ParOpD<T,N>);
436#define INSTANTIATE_FLEXIBLESOLVER_OP(...) \
437 template class Dune::FlexibleSolver<__VA_ARGS__>;
439#define INSTANTIATE_FLEXIBLESOLVER(T,N) \
440 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<T,N>); \
441 INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<T,N>);
Dune::OverlappingSchwarzOperator< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N >, Comm > ParOpD Definition: FlexibleSolver_impl.hpp:412
Opm::BlockVector< Dune::FieldVector< Scalar, N > > BV Definition: FlexibleSolver_impl.hpp:393
Dune::OwnerOverlapCopyCommunication< int, int > Comm Definition: FlexibleSolver_impl.hpp:406
Dune::MatrixAdapter< OBM< Scalar, N >, BV< Scalar, N >, BV< Scalar, N > > SeqOpM Definition: FlexibleSolver_impl.hpp:399
Opm::BCRSMatrix< Opm::MatrixBlock< Scalar, N, N > > OBM Definition: FlexibleSolver_impl.hpp:395
Definition: PreconditionerWithUpdate.hpp:98
Definition: FlexibleSolver.hpp:45
Dune::SolverCategory::Category category() const override Definition: FlexibleSolver_impl.hpp:138
void apply(VectorType &x, VectorType &rhs, Dune::InverseOperatorResult &res) override Definition: FlexibleSolver_impl.hpp:99
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:74
AbstractPrecondType & preconditioner() Access the contained preconditioner. Definition: FlexibleSolver_impl.hpp:130
Dune linear operator that assumes ghost rows are ordered after interior rows. Avoids some computation... Definition: WellOperators.hpp:412
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:155
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:306
Adapter to combine a matrix and another linear operator into a combined linear operator. Definition: WellOperators.hpp:229
Wraps a CUDA solver to work with CPU data. Definition: gpuistl/SolverAdapter.hpp:51
Definition: fvbaseprimaryvariables.hh:161
Definition: flashcompositionstep.hh:36
constexpr bool is_multi_type_block_vector_v Definition: FlexibleSolver_impl.hpp:58
Dune::InverseOperatorResult InverseOperatorResult Definition: GpuBridge.hpp:32
Dune::BCRSMatrix< B, A > BCRSMatrix Definition: BCRSMatrix.hpp:35
constexpr int max_gpu_instantiated_block_size Definition: is_gpu_operator.hpp:87
Dune::BlockVector< B, A > BlockVector Definition: BlockVector.hpp:35
Definition: FlexibleSolver_impl.hpp:54
|