/* 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 . */ #ifndef OPM_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED #define OPM_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED #include #include #include #include #include #include #include #include #include #include #include namespace Opm { // Circular dependency between PreconditionerFactory [which can make an OwningTwoLevelPreconditioner] // and OwningTwoLevelPreconditioner [which uses PreconditionerFactory to choose the fine-level smoother] // must be broken, accomplished by forward-declaration here. template class PreconditionerFactory; } namespace Dune { // Must forward-declare FlexibleSolver as we want to use it as solver for the pressure system. template class FlexibleSolver; /// 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. template class OwningTwoLevelPreconditioner : public Dune::PreconditionerWithUpdate { public: using pt = boost::property_tree::ptree; using MatrixType = typename OperatorType::matrix_type; using PrecFactory = Opm::PreconditionerFactory; OwningTwoLevelPreconditioner(const OperatorType& linearoperator, const pt& prm) : linear_operator_(linearoperator) , finesmoother_(PrecFactory::create(linearoperator, prm.get_child("finesmoother"))) , comm_(nullptr) , weights_(Opm::Amg::getQuasiImpesWeights( linearoperator.getmat(), prm.get("pressure_var_index"), transpose)) , levelTransferPolicy_(dummy_comm_, weights_, prm.get("pressure_var_index")) , coarseSolverPolicy_(prm.get_child("coarsesolver")) , twolevel_method_(linearoperator, finesmoother_, levelTransferPolicy_, coarseSolverPolicy_, transpose ? 1 : 0, transpose ? 0 : 1) , prm_(prm) { if (prm.get("verbosity") > 10) { std::ofstream outfile(prm.get("weights_filename")); if (!outfile) { throw std::runtime_error("Could not write weights"); } Dune::writeMatrixMarket(weights_, outfile); } } OwningTwoLevelPreconditioner(const OperatorType& linearoperator, const pt& prm, const Communication& comm) : linear_operator_(linearoperator) , finesmoother_(PrecFactory::create(linearoperator, prm.get_child("finesmoother"), comm)) , comm_(&comm) , weights_(Opm::Amg::getQuasiImpesWeights( linearoperator.getmat(), prm.get("pressure_var_index"), transpose)) , levelTransferPolicy_(*comm_, weights_, prm.get("pressure_var_index")) , coarseSolverPolicy_(prm.get_child("coarsesolver")) , twolevel_method_(linearoperator, finesmoother_, levelTransferPolicy_, coarseSolverPolicy_, transpose ? 1 : 0, transpose ? 0 : 1) , prm_(prm) { if (prm.get("verbosity") > 10) { std::ofstream outfile(prm.get("weights_filename")); if (!outfile) { throw std::runtime_error("Could not write weights"); } Dune::writeMatrixMarket(weights_, outfile); } } virtual void pre(VectorType& x, VectorType& b) override { twolevel_method_.pre(x, b); } virtual void apply(VectorType& v, const VectorType& d) override { twolevel_method_.apply(v, d); } virtual void post(VectorType& x) override { twolevel_method_.post(x); } virtual void update() override { Opm::Amg::getQuasiImpesWeights( linear_operator_.getmat(), prm_.get("pressure_var_index"), transpose, weights_); updateImpl(comm_); } virtual Dune::SolverCategory::Category category() const override { return linear_operator_.category(); } private: using PressureMatrixType = Dune::BCRSMatrix>; using PressureVectorType = Dune::BlockVector>; using SeqCoarseOperatorType = Dune::MatrixAdapter; using ParCoarseOperatorType = Dune::OverlappingSchwarzOperator; using CoarseOperatorType = std::conditional_t::value, SeqCoarseOperatorType, ParCoarseOperatorType>; using LevelTransferPolicy = Opm::PressureTransferPolicy; using CoarseSolverPolicy = Dune::Amg::PressureSolverPolicy, LevelTransferPolicy>; using TwoLevelMethod = Dune::Amg::TwoLevelMethodCpr>; // Handling parallel vs serial instantiation of preconditioner factory. template void updateImpl(const Comm*) { // Parallel case. finesmoother_ = PrecFactory::create(linear_operator_, prm_.get_child("finesmoother"), *comm_); twolevel_method_.updatePreconditioner(finesmoother_, coarseSolverPolicy_); } void updateImpl(const Dune::Amg::SequentialInformation*) { // Serial case. finesmoother_ = PrecFactory::create(linear_operator_, prm_.get_child("finesmoother")); twolevel_method_.updatePreconditioner(finesmoother_, coarseSolverPolicy_); } const OperatorType& linear_operator_; std::shared_ptr> finesmoother_; const Communication* comm_; VectorType weights_; LevelTransferPolicy levelTransferPolicy_; CoarseSolverPolicy coarseSolverPolicy_; TwoLevelMethod twolevel_method_; boost::property_tree::ptree prm_; Communication dummy_comm_; }; } // namespace Dune #endif // OPM_OWNINGTWOLEVELPRECONDITIONER_HEADER_INCLUDED