2019-05-20 06:23:57 -05:00
|
|
|
/*
|
|
|
|
Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef OPM_FLEXIBLE_SOLVER_HEADER_INCLUDED
|
|
|
|
#define OPM_FLEXIBLE_SOLVER_HEADER_INCLUDED
|
|
|
|
|
2019-05-29 09:21:34 -05:00
|
|
|
#include <opm/simulators/linalg/PreconditionerFactory.hpp>
|
2019-05-20 06:23:57 -05:00
|
|
|
|
|
|
|
#include <dune/common/fmatrix.hh>
|
|
|
|
#include <dune/istl/bcrsmatrix.hh>
|
|
|
|
#include <dune/istl/solvers.hh>
|
|
|
|
#include <dune/istl/umfpack.hh>
|
|
|
|
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
|
|
|
namespace Dune
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2019-06-11 02:14:40 -05:00
|
|
|
/// A solver class that encapsulates all needed objects for a linear solver
|
|
|
|
/// (operator, scalar product, iterative solver and preconditioner) and sets
|
|
|
|
/// them up based on runtime parameters, using the PreconditionerFactory for
|
|
|
|
/// setting up preconditioners.
|
2019-05-20 06:23:57 -05:00
|
|
|
template <class MatrixTypeT, class VectorTypeT>
|
2019-06-11 02:14:40 -05:00
|
|
|
class FlexibleSolver : public Dune::InverseOperator<VectorTypeT, VectorTypeT>
|
2019-05-20 06:23:57 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
using MatrixType = MatrixTypeT;
|
|
|
|
using VectorType = VectorTypeT;
|
|
|
|
|
2019-05-20 07:31:36 -05:00
|
|
|
/// Create a sequential solver.
|
2019-05-20 06:23:57 -05:00
|
|
|
FlexibleSolver(const boost::property_tree::ptree& prm, const MatrixType& matrix)
|
|
|
|
{
|
2019-05-20 07:31:36 -05:00
|
|
|
init(prm, matrix, Dune::Amg::SequentialInformation());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a parallel solver (if Comm is e.g. OwnerOverlapCommunication).
|
|
|
|
template <class Comm>
|
|
|
|
FlexibleSolver(const boost::property_tree::ptree& prm, const MatrixType& matrix, const Comm& comm)
|
|
|
|
{
|
|
|
|
init(prm, matrix, comm);
|
2019-05-20 06:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(VectorType& x, VectorType& rhs, Dune::InverseOperatorResult& res) override
|
|
|
|
{
|
|
|
|
linsolver_->apply(x, rhs, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void apply(VectorType& x, VectorType& rhs, double reduction, Dune::InverseOperatorResult& res) override
|
|
|
|
{
|
|
|
|
linsolver_->apply(x, rhs, reduction, res);
|
|
|
|
}
|
|
|
|
|
2019-06-11 02:14:40 -05:00
|
|
|
/// Type of the contained preconditioner.
|
|
|
|
using AbstractPrecondType = Dune::PreconditionerWithUpdate<VectorType, VectorType>;
|
|
|
|
|
|
|
|
/// Access the contained preconditioner.
|
|
|
|
AbstractPrecondType& preconditioner()
|
2019-05-20 07:31:36 -05:00
|
|
|
{
|
2019-06-11 02:14:40 -05:00
|
|
|
return *preconditioner_;
|
2019-05-20 07:31:36 -05:00
|
|
|
}
|
|
|
|
|
2019-05-20 06:23:57 -05:00
|
|
|
virtual Dune::SolverCategory::Category category() const override
|
|
|
|
{
|
2019-05-20 07:31:36 -05:00
|
|
|
return linearoperator_->category();
|
2019-05-20 06:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-20 07:31:36 -05:00
|
|
|
using AbstractOperatorType = Dune::AssembledLinearOperator<MatrixType, VectorType, VectorType>;
|
|
|
|
using AbstractScalarProductType = Dune::ScalarProduct<VectorType>;
|
|
|
|
using AbstractSolverType = Dune::InverseOperator<VectorType, VectorType>;
|
|
|
|
|
|
|
|
// Machinery for making sequential or parallel operators/preconditioners/scalar products.
|
|
|
|
template <class Comm>
|
|
|
|
void initOpPrecSp(const MatrixType& matrix, const boost::property_tree::ptree& prm, const Comm& comm)
|
|
|
|
{
|
|
|
|
// Parallel case.
|
|
|
|
using ParOperatorType = Dune::OverlappingSchwarzOperator<MatrixType, VectorType, VectorType, Comm>;
|
|
|
|
auto linop = std::make_shared<ParOperatorType>(matrix, comm);
|
|
|
|
linearoperator_ = linop;
|
2019-05-29 09:21:34 -05:00
|
|
|
preconditioner_
|
2020-01-07 10:53:54 -06:00
|
|
|
= Opm::PreconditionerFactory<ParOperatorType, Comm>::create(*linop, prm.get_child("preconditioner"), comm);
|
2019-05-20 07:31:36 -05:00
|
|
|
scalarproduct_ = Dune::createScalarProduct<VectorType, Comm>(comm, linearoperator_->category());
|
|
|
|
}
|
2019-06-06 03:36:17 -05:00
|
|
|
|
|
|
|
void initOpPrecSp(const MatrixType& matrix, const boost::property_tree::ptree& prm, const Dune::Amg::SequentialInformation&)
|
2019-05-20 07:31:36 -05:00
|
|
|
{
|
|
|
|
// Sequential case.
|
|
|
|
using SeqOperatorType = Dune::MatrixAdapter<MatrixType, VectorType, VectorType>;
|
|
|
|
auto linop = std::make_shared<SeqOperatorType>(matrix);
|
|
|
|
linearoperator_ = linop;
|
2020-01-07 10:53:54 -06:00
|
|
|
preconditioner_ = Opm::PreconditionerFactory<SeqOperatorType>::create(*linop, prm.get_child("preconditioner"));
|
2019-05-20 07:31:36 -05:00
|
|
|
scalarproduct_ = std::make_shared<Dune::SeqScalarProduct<VectorType>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initSolver(const boost::property_tree::ptree& prm)
|
2019-05-20 06:23:57 -05:00
|
|
|
{
|
|
|
|
const double tol = prm.get<double>("tol");
|
|
|
|
const int maxiter = prm.get<int>("maxiter");
|
2019-05-20 07:31:36 -05:00
|
|
|
const int verbosity = prm.get<int>("verbosity");
|
|
|
|
const std::string solver_type = prm.get<std::string>("solver");
|
2019-05-20 06:23:57 -05:00
|
|
|
if (solver_type == "bicgstab") {
|
|
|
|
linsolver_.reset(new Dune::BiCGSTABSolver<VectorType>(*linearoperator_,
|
2019-05-20 07:31:36 -05:00
|
|
|
*scalarproduct_,
|
2019-05-20 06:23:57 -05:00
|
|
|
*preconditioner_,
|
|
|
|
tol, // desired residual reduction factor
|
|
|
|
maxiter, // maximum number of iterations
|
|
|
|
verbosity));
|
|
|
|
} else if (solver_type == "loopsolver") {
|
|
|
|
linsolver_.reset(new Dune::LoopSolver<VectorType>(*linearoperator_,
|
2019-05-20 07:31:36 -05:00
|
|
|
*scalarproduct_,
|
2019-05-20 06:23:57 -05:00
|
|
|
*preconditioner_,
|
|
|
|
tol, // desired residual reduction factor
|
|
|
|
maxiter, // maximum number of iterations
|
|
|
|
verbosity));
|
|
|
|
} else if (solver_type == "gmres") {
|
|
|
|
int restart = prm.get<int>("restart");
|
|
|
|
linsolver_.reset(new Dune::RestartedGMResSolver<VectorType>(*linearoperator_,
|
2019-05-20 07:31:36 -05:00
|
|
|
*scalarproduct_,
|
2019-05-20 06:23:57 -05:00
|
|
|
*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<MatrixType>(linearoperator_->getmat(), verbosity, dummy));
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
std::string msg("Solver not known ");
|
|
|
|
msg += solver_type;
|
|
|
|
throw std::runtime_error(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:31:36 -05:00
|
|
|
|
|
|
|
// Main initialization routine.
|
|
|
|
// Call with Comm == Dune::Amg::SequentialInformation to get a serial solver.
|
|
|
|
template <class Comm>
|
|
|
|
void init(const boost::property_tree::ptree& prm, const MatrixType& matrix, const Comm& comm)
|
|
|
|
{
|
|
|
|
initOpPrecSp(matrix, prm, comm);
|
|
|
|
initSolver(prm);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<AbstractOperatorType> linearoperator_;
|
|
|
|
std::shared_ptr<AbstractPrecondType> preconditioner_;
|
|
|
|
std::shared_ptr<AbstractScalarProductType> scalarproduct_;
|
|
|
|
std::shared_ptr<AbstractSolverType> linsolver_;
|
2019-05-20 06:23:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Dune
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // OPM_FLEXIBLE_SOLVER_HEADER_INCLUDED
|