/*
Copyright 2019, 2020 SINTEF Digital, Mathematics and Cybernetics.
Copyright 2020 Equinor.
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 .
*/
#ifndef OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
#define OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace Dune
{
/// Create a sequential solver.
template
FlexibleSolver::
FlexibleSolver(AbstractOperatorType& op,
const boost::property_tree::ptree& prm,
const std::function& weightsCalculator)
{
init(op, Dune::Amg::SequentialInformation(), prm, weightsCalculator);
}
/// Create a parallel solver (if Comm is e.g. OwnerOverlapCommunication).
template
template
FlexibleSolver::
FlexibleSolver(AbstractOperatorType& op,
const Comm& comm,
const boost::property_tree::ptree& prm,
const std::function& weightsCalculator)
{
init(op, comm, prm, weightsCalculator);
}
template
void
FlexibleSolver::
apply(VectorType& x, VectorType& rhs, Dune::InverseOperatorResult& res)
{
linsolver_->apply(x, rhs, res);
}
template
void
FlexibleSolver::
apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res)
{
linsolver_->apply(x, rhs, reduction, res);
}
/// Access the contained preconditioner.
template
auto
FlexibleSolver::
preconditioner() -> AbstractPrecondType&
{
return *preconditioner_;
}
template
Dune::SolverCategory::Category
FlexibleSolver::
category() const
{
return linearoperator_for_solver_->category();
}
// Machinery for making sequential or parallel operators/preconditioners/scalar products.
template
template
void
FlexibleSolver::
initOpPrecSp(AbstractOperatorType& op,
const boost::property_tree::ptree& prm,
const std::function weightsCalculator,
const Comm& comm)
{
// Parallel case.
using pt = const boost::property_tree::ptree;
using ParOperatorType = Dune::OverlappingSchwarzOperator;
linearoperator_for_solver_ = &op;
auto op_prec = std::make_shared(op.getmat(), comm);
auto child = prm.get_child_optional("preconditioner");
preconditioner_ = Opm::PreconditionerFactory::create(*op_prec,
child ? *child : pt(),
weightsCalculator,
comm);
scalarproduct_ = Dune::createScalarProduct(comm, op.category());
linearoperator_for_precond_ = op_prec;
}
template
void
FlexibleSolver::
initOpPrecSp(AbstractOperatorType& op,
const boost::property_tree::ptree& prm,
const std::function weightsCalculator,
const Dune::Amg::SequentialInformation&)
{
// Sequential case.
using pt = const boost::property_tree::ptree;
using SeqOperatorType = Dune::MatrixAdapter;
linearoperator_for_solver_ = &op;
auto op_prec = std::make_shared(op.getmat());
auto child = prm.get_child_optional("preconditioner");
preconditioner_ = Opm::PreconditionerFactory::create(*op_prec,
child ? *child : pt(),
weightsCalculator);
scalarproduct_ = std::make_shared>();
linearoperator_for_precond_ = op_prec;
}
template
void
FlexibleSolver::
initSolver(const boost::property_tree::ptree& prm, const bool is_iorank)
{
const double tol = prm.get("tol", 1e-2);
const int maxiter = prm.get("maxiter", 200);
const int verbosity = is_iorank ? prm.get("verbosity", 0) : 0;
const std::string solver_type = prm.get("solver", "bicgstab");
if (solver_type == "bicgstab") {
linsolver_.reset(new Dune::BiCGSTABSolver(*linearoperator_for_solver_,
*scalarproduct_,
*preconditioner_,
tol, // desired residual reduction factor
maxiter, // maximum number of iterations
verbosity));
} else if (solver_type == "loopsolver") {
linsolver_.reset(new Dune::LoopSolver(*linearoperator_for_solver_,
*scalarproduct_,
*preconditioner_,
tol, // desired residual reduction factor
maxiter, // maximum number of iterations
verbosity));
} else if (solver_type == "gmres") {
int restart = prm.get("restart", 15);
linsolver_.reset(new Dune::RestartedGMResSolver(*linearoperator_for_solver_,
*scalarproduct_,
*preconditioner_,
tol,
restart, // desired residual reduction factor
maxiter, // maximum number of iterations
verbosity));
#if HAVE_SUITESPARSE_UMFPACK
} else if (solver_type == "umfpack") {
bool dummy = false;
linsolver_.reset(new Dune::UMFPack(linearoperator_for_solver_->getmat(), verbosity, dummy));
#endif
} else {
OPM_THROW(std::invalid_argument, "Properties: Solver " << solver_type << " not known.");
}
}
// Main initialization routine.
// Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
template
template
void
FlexibleSolver::
init(AbstractOperatorType& op,
const Comm& comm,
const boost::property_tree::ptree& prm,
const std::function weightsCalculator)
{
initOpPrecSp(op, prm, weightsCalculator, comm);
initSolver(prm, comm.communicator().rank() == 0);
}
} // namespace Dune
// Macros to simplify explicit instantiation of FlexibleSolver for various block sizes.
template
using BV = Dune::BlockVector>;
template
using BM = Dune::BCRSMatrix>;
template
using OBM = Dune::BCRSMatrix>;
#if HAVE_MPI
using Comm = Dune::OwnerOverlapCopyCommunication;
// 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.
#define INSTANTIATE_FLEXIBLESOLVER(N) \
template class Dune::FlexibleSolver, BV>; \
template class Dune::FlexibleSolver, BV>; \
template Dune::FlexibleSolver, BV>::FlexibleSolver(AbstractOperatorType& op, \
const Comm& comm, \
const boost::property_tree::ptree& prm, \
const std::function()>& weightsCalculator); \
template Dune::FlexibleSolver, BV>::FlexibleSolver(AbstractOperatorType& op, \
const Comm& comm, \
const boost::property_tree::ptree& prm, \
const std::function()>& weightsCalculator);
#else // HAVE_MPI
#define INSTANTIATE_FLEXIBLESOLVER(N) \
template class Dune::FlexibleSolver, BV>; \
template class Dune::FlexibleSolver, BV>;
#endif // HAVE_MPI
#endif // OPM_FLEXIBLE_SOLVER_IMPL_HEADER_INCLUDED