2020-06-10 13:54:16 -05:00
|
|
|
/*
|
|
|
|
Copyright 2019, 2020 SINTEF Digital, Mathematics and Cybernetics.
|
2020-06-17 14:07:13 -05:00
|
|
|
Copyright 2020 Equinor.
|
2020-06-10 13:54:16 -05:00
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-17 14:07:13 -05:00
|
|
|
#ifndef OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
|
|
|
|
#define OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
|
2020-06-10 13:54:16 -05:00
|
|
|
|
2022-08-17 01:23:01 -05:00
|
|
|
#include <opm/common/ErrorMacros.hpp>
|
|
|
|
|
2021-09-13 05:25:49 -05:00
|
|
|
#include <opm/simulators/linalg/matrixblock.hh>
|
|
|
|
#include <opm/simulators/linalg/ilufirstelement.hh>
|
2020-06-10 13:54:16 -05:00
|
|
|
#include <opm/simulators/linalg/FlexibleSolver.hpp>
|
2020-06-17 06:03:57 -05:00
|
|
|
#include <opm/simulators/linalg/PreconditionerFactory.hpp>
|
2022-08-17 01:23:01 -05:00
|
|
|
#include <opm/simulators/linalg/PropertyTree.hpp>
|
|
|
|
#include <opm/simulators/linalg/WellOperators.hpp>
|
2020-06-10 13:54:16 -05:00
|
|
|
|
|
|
|
#include <dune/common/fmatrix.hh>
|
|
|
|
#include <dune/istl/bcrsmatrix.hh>
|
|
|
|
#include <dune/istl/solvers.hh>
|
|
|
|
#include <dune/istl/umfpack.hh>
|
|
|
|
#include <dune/istl/owneroverlapcopy.hh>
|
|
|
|
#include <dune/istl/paamg/pinfo.hh>
|
|
|
|
|
|
|
|
namespace Dune
|
|
|
|
{
|
|
|
|
/// Create a sequential solver.
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
|
|
|
FlexibleSolver<Operator>::
|
|
|
|
FlexibleSolver(Operator& op,
|
2021-06-09 03:35:22 -05:00
|
|
|
const Opm::PropertyTree& prm,
|
2021-06-08 14:51:34 -05:00
|
|
|
const std::function<VectorType()>& weightsCalculator,
|
|
|
|
std::size_t pressureIndex)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
2021-06-08 14:51:34 -05:00
|
|
|
init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator,
|
|
|
|
pressureIndex);
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a parallel solver (if Comm is e.g. OwnerOverlapCommunication).
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
template <class Comm>
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
|
|
|
FlexibleSolver(Operator& op,
|
2020-06-17 02:31:19 -05:00
|
|
|
const Comm& comm,
|
2021-06-09 03:35:22 -05:00
|
|
|
const Opm::PropertyTree& prm,
|
2021-06-08 14:51:34 -05:00
|
|
|
const std::function<VectorType()>& weightsCalculator,
|
|
|
|
std::size_t pressureIndex)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
2021-06-08 14:51:34 -05:00
|
|
|
init(op, comm, prm, weightsCalculator, pressureIndex);
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
2020-06-10 13:54:16 -05:00
|
|
|
apply(VectorType& x, VectorType& rhs, Dune::InverseOperatorResult& res)
|
|
|
|
{
|
|
|
|
linsolver_->apply(x, rhs, res);
|
|
|
|
}
|
|
|
|
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
2020-06-10 13:54:16 -05:00
|
|
|
apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
|
|
|
|
{
|
|
|
|
linsolver_->apply(x, rhs, reduction, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the contained preconditioner.
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
auto
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
2020-06-10 13:54:16 -05:00
|
|
|
preconditioner() -> AbstractPrecondType&
|
|
|
|
{
|
|
|
|
return *preconditioner_;
|
|
|
|
}
|
|
|
|
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
Dune::SolverCategory::Category
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
2020-06-10 13:54:16 -05:00
|
|
|
category() const
|
|
|
|
{
|
2020-06-19 09:45:34 -05:00
|
|
|
return linearoperator_for_solver_->category();
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Machinery for making sequential or parallel operators/preconditioners/scalar products.
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
template <class Comm>
|
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
|
|
|
initOpPrecSp(Operator& op,
|
2021-06-09 03:35:22 -05:00
|
|
|
const Opm::PropertyTree& prm,
|
2020-06-19 09:45:34 -05:00
|
|
|
const std::function<VectorType()> weightsCalculator,
|
2021-06-08 14:51:34 -05:00
|
|
|
const Comm& comm,
|
|
|
|
std::size_t pressureIndex)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
|
|
|
// Parallel case.
|
2020-06-19 09:45:34 -05:00
|
|
|
linearoperator_for_solver_ = &op;
|
2020-06-10 13:54:16 -05:00
|
|
|
auto child = prm.get_child_optional("preconditioner");
|
2021-01-04 08:18:23 -06:00
|
|
|
preconditioner_ = Opm::PreconditionerFactory<Operator, Comm>::create(op,
|
|
|
|
child ? *child : Opm::PropertyTree(),
|
|
|
|
weightsCalculator,
|
|
|
|
comm,
|
|
|
|
pressureIndex);
|
2020-06-19 09:45:34 -05:00
|
|
|
scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, op.category());
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
|
|
|
initOpPrecSp(Operator& op,
|
2021-06-09 03:35:22 -05:00
|
|
|
const Opm::PropertyTree& prm,
|
2020-06-19 09:45:34 -05:00
|
|
|
const std::function<VectorType()> weightsCalculator,
|
2021-06-08 14:51:34 -05:00
|
|
|
const Dune::Amg::SequentialInformation&,
|
|
|
|
std::size_t pressureIndex)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
|
|
|
// Sequential case.
|
2020-06-19 09:45:34 -05:00
|
|
|
linearoperator_for_solver_ = &op;
|
2020-06-10 13:54:16 -05:00
|
|
|
auto child = prm.get_child_optional("preconditioner");
|
2022-08-17 01:23:01 -05:00
|
|
|
preconditioner_ = Opm::PreconditionerFactory<Operator,Dune::Amg::SequentialInformation>::create(op,
|
2021-01-04 08:18:23 -06:00
|
|
|
child ? *child : Opm::PropertyTree(),
|
|
|
|
weightsCalculator,
|
|
|
|
pressureIndex);
|
2020-06-10 13:54:16 -05:00
|
|
|
scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
|
|
|
|
}
|
|
|
|
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
2021-06-09 03:35:22 -05:00
|
|
|
initSolver(const Opm::PropertyTree& prm, const bool is_iorank)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
|
|
|
const double tol = prm.get<double>("tol", 1e-2);
|
|
|
|
const int maxiter = prm.get<int>("maxiter", 200);
|
2020-06-19 09:45:34 -05:00
|
|
|
const int verbosity = is_iorank ? prm.get<int>("verbosity", 0) : 0;
|
2020-06-10 13:54:16 -05:00
|
|
|
const std::string solver_type = prm.get<std::string>("solver", "bicgstab");
|
|
|
|
if (solver_type == "bicgstab") {
|
2023-03-29 02:50:09 -05:00
|
|
|
linsolver_ = std::make_shared<Dune::BiCGSTABSolver<VectorType>>(*linearoperator_for_solver_,
|
|
|
|
*scalarproduct_,
|
|
|
|
*preconditioner_,
|
|
|
|
tol, // desired residual reduction factor
|
|
|
|
maxiter, // maximum number of iterations
|
|
|
|
verbosity);
|
2020-06-10 13:54:16 -05:00
|
|
|
} else if (solver_type == "loopsolver") {
|
2023-03-29 02:50:09 -05:00
|
|
|
linsolver_ = std::make_shared<Dune::LoopSolver<VectorType>>(*linearoperator_for_solver_,
|
2020-06-10 13:54:16 -05:00
|
|
|
*scalarproduct_,
|
|
|
|
*preconditioner_,
|
2023-03-29 02:50:09 -05:00
|
|
|
tol, // desired residual reduction factor
|
2020-06-10 13:54:16 -05:00
|
|
|
maxiter, // maximum number of iterations
|
2023-03-29 02:50:09 -05:00
|
|
|
verbosity);
|
|
|
|
} else if (solver_type == "gmres") {
|
|
|
|
int restart = prm.get<int>("restart", 15);
|
|
|
|
linsolver_ = std::make_shared<Dune::RestartedGMResSolver<VectorType>>(*linearoperator_for_solver_,
|
|
|
|
*scalarproduct_,
|
|
|
|
*preconditioner_,
|
2023-03-31 04:03:57 -05:00
|
|
|
tol,// desired residual reduction factor
|
|
|
|
restart,
|
2023-03-29 02:50:09 -05:00
|
|
|
maxiter, // maximum number of iterations
|
|
|
|
verbosity);
|
2023-03-31 04:01:49 -05:00
|
|
|
} else if (solver_type == "flexgmres") {
|
|
|
|
int restart = prm.get<int>("restart", 15);
|
|
|
|
linsolver_ = std::make_shared<Dune::RestartedFlexibleGMResSolver<VectorType>>(*linearoperator_for_solver_,
|
|
|
|
*scalarproduct_,
|
|
|
|
*preconditioner_,
|
2023-03-31 04:03:57 -05:00
|
|
|
tol,// desired residual reduction factor
|
|
|
|
restart,
|
2023-03-31 04:01:49 -05:00
|
|
|
maxiter, // maximum number of iterations
|
2023-04-03 07:15:16 -05:00
|
|
|
verbosity);
|
2020-06-10 13:54:16 -05:00
|
|
|
#if HAVE_SUITESPARSE_UMFPACK
|
|
|
|
} else if (solver_type == "umfpack") {
|
|
|
|
bool dummy = false;
|
2021-01-04 08:18:23 -06:00
|
|
|
using MatrixType = std::remove_const_t<std::remove_reference_t<decltype(linearoperator_for_solver_->getmat())>>;
|
2023-03-29 02:50:09 -05:00
|
|
|
linsolver_ = std::make_shared<Dune::UMFPack<MatrixType>>(linearoperator_for_solver_->getmat(), verbosity, dummy);
|
2020-06-10 13:54:16 -05:00
|
|
|
#endif
|
|
|
|
} else {
|
2023-01-02 08:17:49 -06:00
|
|
|
OPM_THROW(std::invalid_argument,
|
|
|
|
"Properties: Solver " + solver_type + " not known.");
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Main initialization routine.
|
|
|
|
// Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2020-06-10 13:54:16 -05:00
|
|
|
template <class Comm>
|
|
|
|
void
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<Operator>::
|
|
|
|
init(Operator& op,
|
2020-06-17 02:31:19 -05:00
|
|
|
const Comm& comm,
|
2021-06-09 03:35:22 -05:00
|
|
|
const Opm::PropertyTree& prm,
|
2021-06-08 14:51:34 -05:00
|
|
|
const std::function<VectorType()> weightsCalculator,
|
|
|
|
std::size_t pressureIndex)
|
2020-06-10 13:54:16 -05:00
|
|
|
{
|
2021-06-08 14:51:34 -05:00
|
|
|
initOpPrecSp(op, prm, weightsCalculator, comm, pressureIndex);
|
2020-06-19 09:45:34 -05:00
|
|
|
initSolver(prm, comm.communicator().rank() == 0);
|
2020-06-10 13:54:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dune
|
2020-06-17 14:07:13 -05:00
|
|
|
|
2020-06-19 09:37:43 -05:00
|
|
|
|
|
|
|
// Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
|
2022-05-19 04:35:11 -05:00
|
|
|
|
|
|
|
// Vectors and matrices.
|
2020-06-19 09:37:43 -05:00
|
|
|
template <int N>
|
|
|
|
using BV = Dune::BlockVector<Dune::FieldVector<double, N>>;
|
|
|
|
template <int N>
|
|
|
|
using OBM = Dune::BCRSMatrix<Opm::MatrixBlock<double, N, N>>;
|
|
|
|
|
2022-05-19 04:35:11 -05:00
|
|
|
// Sequential operators.
|
|
|
|
template <int N>
|
|
|
|
using SeqOpM = Dune::MatrixAdapter<OBM<N>, BV<N>, BV<N>>;
|
|
|
|
template <int N>
|
|
|
|
using SeqOpW = Opm::WellModelMatrixAdapter<OBM<N>, BV<N>, BV<N>, false>;
|
|
|
|
|
2020-06-19 09:37:43 -05:00
|
|
|
#if HAVE_MPI
|
2020-06-19 09:45:34 -05:00
|
|
|
|
2022-05-19 04:35:11 -05:00
|
|
|
// Parallel communicator and operators.
|
2020-06-19 09:37:43 -05:00
|
|
|
using Comm = Dune::OwnerOverlapCopyCommunication<int, int>;
|
2022-05-19 04:35:11 -05:00
|
|
|
template <int N>
|
|
|
|
using ParOpM = Dune::OverlappingSchwarzOperator<OBM<N>, BV<N>, BV<N>, Comm>;
|
|
|
|
template <int N>
|
|
|
|
using ParOpW = Opm::WellModelGhostLastMatrixAdapter<OBM<N>, BV<N>, BV<N>, true>;
|
2020-06-19 09:45:34 -05:00
|
|
|
|
|
|
|
// Note: we must instantiate the constructor that is a template.
|
|
|
|
// This is only needed in the parallel case, since otherwise the Comm type is
|
|
|
|
// not a template argument but always SequentialInformation.
|
|
|
|
|
2022-05-19 04:35:11 -05:00
|
|
|
#define INSTANTIATE_FLEXIBLESOLVER_OP(Operator) \
|
|
|
|
template class Dune::FlexibleSolver<Operator>; \
|
|
|
|
template Dune::FlexibleSolver<Operator>::FlexibleSolver(Operator& op, \
|
|
|
|
const Comm& comm, \
|
|
|
|
const Opm::PropertyTree& prm, \
|
|
|
|
const std::function<typename Operator::domain_type()>& weightsCalculator, \
|
|
|
|
std::size_t pressureIndex);
|
|
|
|
#define INSTANTIATE_FLEXIBLESOLVER(N) \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<N>); \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<N>); \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(ParOpM<N>); \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(ParOpW<N>);
|
2020-06-19 09:45:34 -05:00
|
|
|
|
|
|
|
#else // HAVE_MPI
|
2020-06-19 09:37:43 -05:00
|
|
|
|
2022-05-19 04:35:11 -05:00
|
|
|
#define INSTANTIATE_FLEXIBLESOLVER_OP(Operator) \
|
|
|
|
template class Dune::FlexibleSolver<Operator>;
|
|
|
|
|
|
|
|
#define INSTANTIATE_FLEXIBLESOLVER(N) \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpM<N>); \
|
|
|
|
INSTANTIATE_FLEXIBLESOLVER_OP(SeqOpW<N>);
|
2020-06-19 09:37:43 -05:00
|
|
|
|
2020-06-19 09:45:34 -05:00
|
|
|
#endif // HAVE_MPI
|
2020-06-19 09:37:43 -05:00
|
|
|
|
2022-05-19 04:35:11 -05:00
|
|
|
|
2020-06-19 09:37:43 -05:00
|
|
|
|
2020-06-17 14:07:13 -05:00
|
|
|
#endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
|