// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_SOLVERFACTORY_HH #define DUNE_ISTL_SOLVERFACTORY_HH #include #include #include #include #include #include #include #include #include #include #include #include namespace Dune{ /** @addtogroup ISTL_Factory @{ */ // Preconditioner factory: template using PreconditionerSignature = std::shared_ptr>(const std::shared_ptr&, const ParameterTree&); template using PreconditionerFactory = Singleton>>; // Iterative solver factory template using SolverSignature = std::shared_ptr>(const std::shared_ptr&, const ParameterTree&); template using SolverFactory = Singleton>>; template struct OperatorTraits{ private: template using _matrix_type = typename O::matrix_type; template using _comm_type = typename O::communication_type; public: using domain_type = typename Operator::domain_type; using range_type = typename Operator::range_type; using operator_type = Operator; using solver_type = InverseOperator; using matrix_type = Std::detected_or_t; static constexpr bool isAssembled = !std::is_same::value; using comm_type = Std::detected_or_t; static constexpr bool isParallel = !std::is_same::value; static const std::shared_ptr> getAssembledOpOrThrow(std::shared_ptr> op){ std::shared_ptr> aop = std::dynamic_pointer_cast>(op); if(aop) return aop; DUNE_THROW(NoAssembledOperator, "Failed to cast to AssembledLinearOperator. Please pass in an AssembledLinearOperator."); } static const comm_type& getCommOrThrow(std::shared_ptr> op){ std::shared_ptr _op = std::dynamic_pointer_cast(op); if constexpr (isParallel){ return _op->getCommunication(); }else{ DUNE_THROW(NoAssembledOperator, "Could not obtain communication object from operator. Please pass in a parallel operator."); } } static std::shared_ptr> getScalarProduct(std::shared_ptr> op) { if constexpr (isParallel){ return createScalarProduct(getCommOrThrow(op), op->category()); }else{ return std::make_shared>(); } } }; // initSolverFactories differs in different compilation units, so we have it // in an anonymous namespace namespace { /** initializes the preconditioners and solvers in the factories with the corresponding Operator and Vector types. @tparam O the assembled linear operator type */ template int initSolverFactories(){ using OpInfo = OperatorTraits; auto& pfac=Dune::PreconditionerFactory::instance(); addRegistryToFactory(pfac, PreconditionerTag{}); auto& isfac=Dune::SolverFactory::instance(); return addRegistryToFactory(isfac, SolverTag{}); } } // end anonymous namespace /** \brief Instantiates an `InverseOperator` from an Operator and a configuration given as a ParameterTree. \param op Operator \param config `ParameterTree` with configuration \param prec Preconditioner Example ini File that can be passed in to construct a CGSolver with a SSOR preconditioner: \verbatim type = cgsolver verbose = 1 maxit = 1000 reduction = 1e-5 [preconditioner] type = ssor iterations = 1 relaxation = 1 \endverbatim \tparam Operator type of the operator, necessary to deduce the matrix type etc. */ template std::shared_ptr> getSolverFromFactory(std::shared_ptr op, const ParameterTree& config, std::shared_ptr> prec = nullptr) { if(prec){ PreconditionerFactory::instance().define("__passed at runtime__", [=](auto...){ return prec; }); ParameterTree config_tmp = config; config_tmp.sub("preconditioner")["type"] = std::string("__passed at runtime__"); return SolverFactory::instance(). create(config.get("type"),op, config_tmp); } return SolverFactory::instance(). create(config.get("type"),op, config); } class UnknownSolverCategory : public InvalidStateException{}; /** @brief Construct a Preconditioner for a given Operator */ template std::shared_ptr> getPreconditionerFromFactory(std::shared_ptr op, const ParameterTree& config){ using Domain = typename Operator::domain_type; using Range = typename Operator::range_type; std::string prec_type = config.get("type"); std::shared_ptr> prec = PreconditionerFactory::instance().create(prec_type, op, config); if constexpr (OperatorTraits::isParallel){ using Comm = typename OperatorTraits::comm_type; const Comm& comm = OperatorTraits::getCommOrThrow(op); if(op->category() == SolverCategory::overlapping && prec->category() == SolverCategory::sequential) return std::make_shared >(prec, comm); else if(op->category() == SolverCategory::nonoverlapping && prec->category() == SolverCategory::sequential) return std::make_shared> >(prec, comm); } return prec; } /** * @} */ } // end namespace Dune #endif