// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 1999 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_solver_selector_h #define dealii_solver_selector_h #include #include #include #include #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN /** * @addtogroup Solvers * @{ */ /** * Selects a solver by changing a parameter. * * By calling the @p solve function of this @p SolverSelector, it selects the * @p solve function of that @p Solver that was specified in the constructor * of this class. * *

Usage

The simplest use of this class is the following: * @code * // generate a @p SolverSelector that calls the @p SolverCG * SolverControl control; * SolverSelector > solver_selector ("cg", control); * * // generate e.g. a @p PreconditionRelaxation * PreconditionRelaxation, Vector > * preconditioning( * A, &SparseMatrix::template precondition_SSOR,0.8); * * // call the @p solve function with this preconditioning as last argument * solver_selector.solve(A,x,b,preconditioning); * @endcode * But the full usefulness of the @p SolverSelector class is not clear until * the presentation of the following example that assumes the user using the * @p ParameterHandler class and having declared a "solver" entry, e.g. with * @code * Parameter_Handler prm; * prm.declare_entry ("solver", "none", * Patterns::Selection( * SolverSelector<>::get_solver_names())); * ... * @endcode * Assuming that in the users parameter file there exists the line * @code * set solver = cg * @endcode * then the constructor call in the above example can be written as * @code * SolverSelector, Vector > * solver_selector(prm.get("solver"), control); * @endcode * * * If at some time there exists a new solver "xyz" then the user does not need * to change their program. Only in the implementation of the @p SolverSelector * the calling of this solver has to be added and each user with program lines * quoted above only needs to 'set solver = xyz' in their parameter file to get * access to that new solver. */ template > DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) class SolverSelector : public EnableObserverPointer { public: /** * An alias for the underlying vector type */ using vector_type = VectorType; /** * Constructor, filling in default values */ SolverSelector() = default; /** * Constructor, selecting the solver @p name * and the SolverControl object @p control already. */ SolverSelector(const std::string &name, SolverControl &control); /** * Destructor */ virtual ~SolverSelector() override = default; /** * Solver procedure. Calls the @p solve function of the @p solver whose @p * SolverName was specified in the constructor. */ template DEAL_II_CXX20_REQUIRES( (concepts::is_linear_operator_on && concepts::is_linear_operator_on)) void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precond) const; /** * Select a new solver. Note that all solver names used in this class are * all lower case. */ void select(const std::string &name); /** * Set a new SolverControl. This needs to be set before solving. */ void set_control(SolverControl &ctrl); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverRichardson::AdditionalData &data); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverCG::AdditionalData &data); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverMinRes::AdditionalData &data); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverBicgstab::AdditionalData &data); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverGMRES::AdditionalData &data); /** * Set the additional data. For more information see the @p Solver class. */ void set_data(const typename SolverFGMRES::AdditionalData &data); /** * Get the names of all implemented solvers. The list of possible * options includes: *
    *
  • "richardson"
  • *
  • "cg"
  • *
  • "bicgstab"
  • *
  • "gmres"
  • *
  • "fgmres"
  • *
  • "minres"
  • *
*/ static std::string get_solver_names(); /** * Exception. */ DeclException1(ExcSolverDoesNotExist, std::string, << "Solver " << arg1 << " does not exist. Use one of " << std::endl << get_solver_names()); protected: /** * Stores the @p SolverControl that is needed in the constructor of each @p * Solver class. This can be changed with @p set_control(). */ ObserverPointer> control; /** * Stores the name of the solver. */ std::string solver_name; private: /** * Stores the additional data. */ typename SolverRichardson::AdditionalData richardson_data; /** * Stores the additional data. */ typename SolverCG::AdditionalData cg_data; /** * Stores the additional data. */ typename SolverMinRes::AdditionalData minres_data; /** * Stores the additional data. */ typename SolverBicgstab::AdditionalData bicgstab_data; /** * Stores the additional data. */ typename SolverGMRES::AdditionalData gmres_data; /** * Stores the additional data. */ typename SolverFGMRES::AdditionalData fgmres_data; }; /** @} */ /* --------------------- Inline and template functions ------------------- */ template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) SolverSelector::SolverSelector(const std::string &name, SolverControl &solver_control) : solver_name(name) , control(&solver_control) {} template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::select(const std::string &name) { solver_name = name; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) template DEAL_II_CXX20_REQUIRES( (concepts::is_linear_operator_on && concepts::is_linear_operator_on)) void SolverSelector::solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precond) const { if (solver_name == "richardson") { SolverRichardson solver(*control, richardson_data); solver.solve(A, x, b, precond); } else if (solver_name == "cg") { SolverCG solver(*control, cg_data); solver.solve(A, x, b, precond); } else if (solver_name == "minres") { SolverMinRes solver(*control, minres_data); solver.solve(A, x, b, precond); } else if (solver_name == "bicgstab") { SolverBicgstab solver(*control, bicgstab_data); solver.solve(A, x, b, precond); } else if (solver_name == "gmres") { SolverGMRES solver(*control, gmres_data); solver.solve(A, x, b, precond); } else if (solver_name == "fgmres") { SolverFGMRES solver(*control, fgmres_data); solver.solve(A, x, b, precond); } else Assert(false, ExcSolverDoesNotExist(solver_name)); } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_control(SolverControl &ctrl) { control = &ctrl; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) std::string SolverSelector::get_solver_names() { return "richardson|cg|bicgstab|gmres|fgmres|minres"; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverGMRES::AdditionalData &data) { gmres_data = data; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverFGMRES::AdditionalData &data) { fgmres_data = data; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverRichardson::AdditionalData &data) { richardson_data = data; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverCG::AdditionalData &data) { cg_data = data; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverMinRes::AdditionalData &data) { minres_data = data; } template DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector) void SolverSelector::set_data( const typename SolverBicgstab::AdditionalData &data) { bicgstab_data = data; } DEAL_II_NAMESPACE_CLOSE #endif