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_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED
|
|
|
|
#define OPM_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED
|
|
|
|
|
2019-05-20 07:31:36 -05:00
|
|
|
#include <opm/simulators/linalg/PreconditionerWithUpdate.hpp>
|
2019-05-20 06:23:57 -05:00
|
|
|
#include <opm/simulators/linalg/PressureSolverPolicy.hpp>
|
|
|
|
#include <opm/simulators/linalg/PressureTransferPolicy.hpp>
|
|
|
|
#include <opm/simulators/linalg/getQuasiImpesWeights.hpp>
|
2019-05-20 07:31:36 -05:00
|
|
|
#include <opm/simulators/linalg/twolevelmethodcpr.hh>
|
2019-05-20 06:23:57 -05:00
|
|
|
|
2020-03-25 15:09:32 -05:00
|
|
|
#include <opm/common/ErrorMacros.hpp>
|
|
|
|
|
2019-05-20 06:23:57 -05:00
|
|
|
#include <dune/common/fmatrix.hh>
|
|
|
|
#include <dune/istl/bcrsmatrix.hh>
|
|
|
|
#include <dune/istl/paamg/amg.hh>
|
|
|
|
|
|
|
|
#include <fstream>
|
2019-05-20 07:31:36 -05:00
|
|
|
#include <type_traits>
|
2019-05-20 06:23:57 -05:00
|
|
|
|
|
|
|
|
2020-01-07 10:53:54 -06:00
|
|
|
namespace Opm
|
|
|
|
{
|
2019-05-29 09:21:34 -05:00
|
|
|
// Circular dependency between PreconditionerFactory [which can make an OwningTwoLevelPreconditioner]
|
|
|
|
// and OwningTwoLevelPreconditioner [which uses PreconditionerFactory to choose the fine-level smoother]
|
2019-05-20 06:23:57 -05:00
|
|
|
// must be broken, accomplished by forward-declaration here.
|
2019-05-29 09:21:34 -05:00
|
|
|
template <class Operator, class Comm = Dune::Amg::SequentialInformation>
|
|
|
|
class PreconditionerFactory;
|
2020-01-07 10:53:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Dune
|
|
|
|
{
|
2019-05-20 07:31:36 -05:00
|
|
|
|
2019-05-20 06:23:57 -05:00
|
|
|
|
|
|
|
// Must forward-declare FlexibleSolver as we want to use it as solver for the pressure system.
|
2021-01-04 08:18:23 -06:00
|
|
|
template <class Operator>
|
2019-05-20 06:23:57 -05:00
|
|
|
class FlexibleSolver;
|
|
|
|
|
2019-08-21 03:50:57 -05:00
|
|
|
template <typename T, typename A, int i>
|
|
|
|
std::ostream& operator<<(std::ostream& out,
|
|
|
|
const BlockVector< FieldVector< T, i >, A >& vector)
|
|
|
|
{
|
|
|
|
Dune::writeMatrixMarket(vector, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename A, int i>
|
|
|
|
std::istream& operator>>(std::istream& input,
|
|
|
|
BlockVector< FieldVector< T, i >, A >& vector)
|
|
|
|
{
|
|
|
|
Dune::readMatrixMarket(vector, input);
|
|
|
|
return input;
|
|
|
|
}
|
2019-05-29 09:21:34 -05:00
|
|
|
|
|
|
|
/// A version of the two-level preconditioner that is:
|
|
|
|
/// - Self-contained, because it owns its policy components.
|
|
|
|
/// - Flexible, because it uses the runtime-flexible solver
|
|
|
|
/// and preconditioner factory.
|
2019-05-20 07:31:36 -05:00
|
|
|
template <class OperatorType,
|
|
|
|
class VectorType,
|
|
|
|
bool transpose = false,
|
|
|
|
class Communication = Dune::Amg::SequentialInformation>
|
|
|
|
class OwningTwoLevelPreconditioner : public Dune::PreconditionerWithUpdate<VectorType, VectorType>
|
2019-05-20 06:23:57 -05:00
|
|
|
{
|
|
|
|
public:
|
2019-05-20 07:31:36 -05:00
|
|
|
using MatrixType = typename OperatorType::matrix_type;
|
2020-01-07 10:53:54 -06:00
|
|
|
using PrecFactory = Opm::PreconditionerFactory<OperatorType, Communication>;
|
2019-05-20 06:23:57 -05:00
|
|
|
|
2021-06-09 03:35:22 -05:00
|
|
|
OwningTwoLevelPreconditioner(const OperatorType& linearoperator, const Opm::PropertyTree& prm,
|
2021-06-08 14:51:34 -05:00
|
|
|
const std::function<VectorType()> weightsCalculator,
|
|
|
|
std::size_t pressureIndex)
|
2019-05-20 07:31:36 -05:00
|
|
|
: linear_operator_(linearoperator)
|
2020-03-25 15:05:01 -05:00
|
|
|
, finesmoother_(PrecFactory::create(linearoperator,
|
2021-06-09 03:35:22 -05:00
|
|
|
prm.get_child_optional("finesmoother") ?
|
2021-06-08 14:51:34 -05:00
|
|
|
prm.get_child("finesmoother") : Opm::PropertyTree(),
|
|
|
|
std::function<VectorType()>(), pressureIndex))
|
2019-05-20 07:31:36 -05:00
|
|
|
, comm_(nullptr)
|
2020-03-24 08:19:57 -05:00
|
|
|
, weightsCalculator_(weightsCalculator)
|
|
|
|
, weights_(weightsCalculator())
|
2021-06-08 14:51:34 -05:00
|
|
|
, levelTransferPolicy_(dummy_comm_, weights_, pressureIndex)
|
2021-06-09 03:35:22 -05:00
|
|
|
, coarseSolverPolicy_(prm.get_child_optional("coarsesolver")? prm.get_child("coarsesolver") : Opm::PropertyTree())
|
2019-05-20 06:23:57 -05:00
|
|
|
, twolevel_method_(linearoperator,
|
|
|
|
finesmoother_,
|
|
|
|
levelTransferPolicy_,
|
|
|
|
coarseSolverPolicy_,
|
2020-03-25 15:05:01 -05:00
|
|
|
prm.get<int>("pre_smooth", transpose? 1 : 0),
|
|
|
|
prm.get<int>("post_smooth", transpose? 0 : 1))
|
2019-05-20 07:31:36 -05:00
|
|
|
, prm_(prm)
|
|
|
|
{
|
2020-03-25 15:05:01 -05:00
|
|
|
if (prm.get<int>("verbosity", 0) > 10) {
|
2020-03-25 15:09:32 -05:00
|
|
|
std::string filename = prm.get<std::string>("weights_filename", "impes_weights.txt");
|
|
|
|
std::ofstream outfile(filename);
|
2019-05-20 07:31:36 -05:00
|
|
|
if (!outfile) {
|
2020-03-25 15:09:32 -05:00
|
|
|
OPM_THROW(std::ofstream::failure, "Could not write weights to file " << filename << ".");
|
2019-05-20 07:31:36 -05:00
|
|
|
}
|
|
|
|
Dune::writeMatrixMarket(weights_, outfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 03:35:22 -05:00
|
|
|
OwningTwoLevelPreconditioner(const OperatorType& linearoperator, const Opm::PropertyTree& prm,
|
2021-06-08 14:51:34 -05:00
|
|
|
const std::function<VectorType()> weightsCalculator,
|
|
|
|
std::size_t pressureIndex, const Communication& comm)
|
2019-05-20 07:31:36 -05:00
|
|
|
: linear_operator_(linearoperator)
|
2020-03-25 15:05:01 -05:00
|
|
|
, finesmoother_(PrecFactory::create(linearoperator,
|
2021-06-09 03:35:22 -05:00
|
|
|
prm.get_child_optional("finesmoother") ?
|
2021-06-08 14:51:34 -05:00
|
|
|
prm.get_child("finesmoother"): Opm::PropertyTree(),
|
|
|
|
std::function<VectorType()>(),
|
|
|
|
comm, pressureIndex))
|
2019-05-20 07:31:36 -05:00
|
|
|
, comm_(&comm)
|
2020-03-24 08:19:57 -05:00
|
|
|
, weightsCalculator_(weightsCalculator)
|
|
|
|
, weights_(weightsCalculator())
|
2021-06-08 14:51:34 -05:00
|
|
|
, levelTransferPolicy_(*comm_, weights_, pressureIndex)
|
2021-06-09 03:35:22 -05:00
|
|
|
, coarseSolverPolicy_(prm.get_child_optional("coarsesolver")? prm.get_child("coarsesolver") : Opm::PropertyTree())
|
2019-05-20 07:31:36 -05:00
|
|
|
, twolevel_method_(linearoperator,
|
|
|
|
finesmoother_,
|
|
|
|
levelTransferPolicy_,
|
|
|
|
coarseSolverPolicy_,
|
2020-03-25 15:05:01 -05:00
|
|
|
prm.get<int>("pre_smooth", transpose? 1 : 0),
|
|
|
|
prm.get<int>("post_smooth", transpose? 0 : 1))
|
2019-05-20 07:31:36 -05:00
|
|
|
, prm_(prm)
|
2019-05-20 06:23:57 -05:00
|
|
|
{
|
2020-03-25 15:09:32 -05:00
|
|
|
if (prm.get<int>("verbosity", 0) > 10 && comm.communicator().rank() == 0) {
|
|
|
|
auto filename = prm.get<std::string>("weights_filename", "impes_weights.txt");
|
|
|
|
std::ofstream outfile(filename);
|
2019-05-20 06:23:57 -05:00
|
|
|
if (!outfile) {
|
2020-03-25 15:09:32 -05:00
|
|
|
OPM_THROW(std::ofstream::failure, "Could not write weights to file " << filename << ".");
|
2019-05-20 06:23:57 -05:00
|
|
|
}
|
|
|
|
Dune::writeMatrixMarket(weights_, outfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void pre(VectorType& x, VectorType& b) override
|
|
|
|
{
|
|
|
|
twolevel_method_.pre(x, b);
|
|
|
|
}
|
2019-05-20 07:31:36 -05:00
|
|
|
|
2019-05-20 06:23:57 -05:00
|
|
|
virtual void apply(VectorType& v, const VectorType& d) override
|
|
|
|
{
|
|
|
|
twolevel_method_.apply(v, d);
|
|
|
|
}
|
2019-05-20 07:31:36 -05:00
|
|
|
|
2019-05-20 06:23:57 -05:00
|
|
|
virtual void post(VectorType& x) override
|
|
|
|
{
|
|
|
|
twolevel_method_.post(x);
|
|
|
|
}
|
2019-05-20 07:31:36 -05:00
|
|
|
|
2020-03-24 08:19:57 -05:00
|
|
|
virtual void update() override
|
2019-05-20 07:31:36 -05:00
|
|
|
{
|
2020-03-24 08:19:57 -05:00
|
|
|
weights_ = weightsCalculator_();
|
2019-06-06 03:36:17 -05:00
|
|
|
updateImpl(comm_);
|
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 linear_operator_.category();
|
2019-05-20 06:23:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using PressureMatrixType = Dune::BCRSMatrix<Dune::FieldMatrix<double, 1, 1>>;
|
|
|
|
using PressureVectorType = Dune::BlockVector<Dune::FieldVector<double, 1>>;
|
2019-05-20 07:31:36 -05:00
|
|
|
using SeqCoarseOperatorType = Dune::MatrixAdapter<PressureMatrixType, PressureVectorType, PressureVectorType>;
|
|
|
|
using ParCoarseOperatorType
|
|
|
|
= Dune::OverlappingSchwarzOperator<PressureMatrixType, PressureVectorType, PressureVectorType, Communication>;
|
|
|
|
using CoarseOperatorType = std::conditional_t<std::is_same<Communication, Dune::Amg::SequentialInformation>::value,
|
|
|
|
SeqCoarseOperatorType,
|
|
|
|
ParCoarseOperatorType>;
|
2019-05-20 06:23:57 -05:00
|
|
|
using LevelTransferPolicy = Opm::PressureTransferPolicy<OperatorType, CoarseOperatorType, Communication, transpose>;
|
2019-05-20 07:31:36 -05:00
|
|
|
using CoarseSolverPolicy = Dune::Amg::PressureSolverPolicy<CoarseOperatorType,
|
2021-01-04 08:18:23 -06:00
|
|
|
FlexibleSolver<CoarseOperatorType>,
|
2019-05-20 07:31:36 -05:00
|
|
|
LevelTransferPolicy>;
|
2019-05-20 06:23:57 -05:00
|
|
|
using TwoLevelMethod
|
2019-05-20 07:31:36 -05:00
|
|
|
= Dune::Amg::TwoLevelMethodCpr<OperatorType, CoarseSolverPolicy, Dune::Preconditioner<VectorType, VectorType>>;
|
|
|
|
|
2019-05-29 09:21:34 -05:00
|
|
|
// Handling parallel vs serial instantiation of preconditioner factory.
|
2019-05-20 07:31:36 -05:00
|
|
|
template <class Comm>
|
2019-06-06 03:36:17 -05:00
|
|
|
void updateImpl(const Comm*)
|
2019-05-20 07:31:36 -05:00
|
|
|
{
|
|
|
|
// Parallel case.
|
2020-03-25 15:05:01 -05:00
|
|
|
auto child = prm_.get_child_optional("finesmoother");
|
2021-06-09 03:35:22 -05:00
|
|
|
finesmoother_ = PrecFactory::create(linear_operator_, child ? *child : Opm::PropertyTree(), *comm_);
|
2019-05-20 07:31:36 -05:00
|
|
|
twolevel_method_.updatePreconditioner(finesmoother_, coarseSolverPolicy_);
|
|
|
|
}
|
|
|
|
|
2019-06-06 03:36:17 -05:00
|
|
|
void updateImpl(const Dune::Amg::SequentialInformation*)
|
2019-05-20 07:31:36 -05:00
|
|
|
{
|
|
|
|
// Serial case.
|
2020-03-25 15:05:01 -05:00
|
|
|
auto child = prm_.get_child_optional("finesmoother");
|
2021-06-09 03:35:22 -05:00
|
|
|
finesmoother_ = PrecFactory::create(linear_operator_, child ? *child : Opm::PropertyTree());
|
2019-05-20 07:31:36 -05:00
|
|
|
twolevel_method_.updatePreconditioner(finesmoother_, coarseSolverPolicy_);
|
|
|
|
}
|
2019-05-20 06:23:57 -05:00
|
|
|
|
2019-05-28 09:22:54 -05:00
|
|
|
const OperatorType& linear_operator_;
|
2019-05-20 06:23:57 -05:00
|
|
|
std::shared_ptr<Dune::Preconditioner<VectorType, VectorType>> finesmoother_;
|
2019-05-20 07:31:36 -05:00
|
|
|
const Communication* comm_;
|
2020-03-24 08:19:57 -05:00
|
|
|
std::function<VectorType()> weightsCalculator_;
|
2019-05-20 06:23:57 -05:00
|
|
|
VectorType weights_;
|
|
|
|
LevelTransferPolicy levelTransferPolicy_;
|
|
|
|
CoarseSolverPolicy coarseSolverPolicy_;
|
|
|
|
TwoLevelMethod twolevel_method_;
|
2021-06-09 03:35:22 -05:00
|
|
|
Opm::PropertyTree prm_;
|
2019-08-07 07:17:17 -05:00
|
|
|
Communication dummy_comm_;
|
2019-05-20 06:23:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Dune
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // OPM_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED
|