4 #ifndef DUNE_ISTL_SOLVERFACTORY_HH
5 #define DUNE_ISTL_SOLVERFACTORY_HH
7 #include <unordered_map>
11 #include <dune/common/parametertree.hh>
12 #include <dune/common/singleton.hh>
17 #define DUNE_REGISTER_DIRECT_SOLVER(name, ...) \
18 DUNE_REGISTRY_PUT(DirectSolverTag, name, __VA_ARGS__)
20 #define DUNE_REGISTER_PRECONDITIONER(name, ...) \
21 DUNE_REGISTRY_PUT(PreconditionerTag, name, __VA_ARGS__)
23 #define DUNE_REGISTER_ITERATIVE_SOLVER(name, ...) \
24 DUNE_REGISTRY_PUT(IterativeSolverTag, name, __VA_ARGS__)
32 struct DirectSolverTag {};
33 struct PreconditionerTag {};
34 struct IterativeSolverTag {};
37 template<
template<
class,
class,
class,
int>
class Preconditioner,
int blockLevel=1>
39 return [](
auto typeList,
const auto& matrix,
const Dune::ParameterTree& config)
41 using Matrix =
typename Dune::TypeListElement<0, decltype(typeList)>::type;
42 using Domain =
typename Dune::TypeListElement<1, decltype(typeList)>::type;
43 using Range =
typename Dune::TypeListElement<2, decltype(typeList)>::type;
44 std::shared_ptr<Dune::Preconditioner<Domain, Range>> preconditioner
45 = std::make_shared<Preconditioner<Matrix, Domain, Range, blockLevel>>(matrix, config);
46 return preconditioner;
50 template<
template<
class,
class,
class>
class Preconditioner>
52 return [](
auto typeList,
const auto& matrix,
const Dune::ParameterTree& config)
54 using Matrix =
typename Dune::TypeListElement<0, decltype(typeList)>::type;
55 using Domain =
typename Dune::TypeListElement<1, decltype(typeList)>::type;
56 using Range =
typename Dune::TypeListElement<2, decltype(typeList)>::type;
57 std::shared_ptr<Dune::Preconditioner<Domain, Range>> preconditioner
58 = std::make_shared<Preconditioner<Matrix, Domain, Range>>(matrix, config);
59 return preconditioner;
63 template<
template<
class...>
class Solver>
65 return [](
auto typeList,
66 const auto& linearOperator,
67 const auto& scalarProduct,
68 const auto& preconditioner,
69 const Dune::ParameterTree& config)
71 using Domain =
typename Dune::TypeListElement<0, decltype(typeList)>::type;
72 using Range =
typename Dune::TypeListElement<1, decltype(typeList)>::type;
73 std::shared_ptr<Dune::InverseOperator<Domain, Range>> solver
74 = std::make_shared<Solver<Domain>>(linearOperator, scalarProduct, preconditioner, config);
80 template<
class M,
class X,
class Y>
82 template<
class M,
class X,
class Y>
86 template<
class M,
class X,
class Y>
88 template<
class M,
class X,
class Y>
92 template<
class X,
class Y>
93 using IterativeSolverSignature = std::shared_ptr<InverseOperator<X,Y>>(
const std::shared_ptr<LinearOperator<X,Y>>&,
const std::shared_ptr<ScalarProduct<X>>&,
const std::shared_ptr<Preconditioner<X,Y>>,
const ParameterTree&);
94 template<
class X,
class Y>
108 template<
class M,
class X,
class Y>
109 int initSolverFactories(){
110 using TL = Dune::TypeList<M,X,Y>;
112 addRegistryToFactory<TL>(dsfac, DirectSolverTag{});
114 addRegistryToFactory<TL>(pfac, PreconditionerTag{});
115 using TLS = Dune::TypeList<X,Y>;
117 return addRegistryToFactory<TLS>(isfac, IterativeSolverTag{});
145 template<
class Operator>
147 using Domain =
typename Operator::domain_type;
148 using Range =
typename Operator::range_type;
153 using _matrix_type =
typename O::matrix_type;
154 using matrix_type = Std::detected_or_t<int, _matrix_type, Operator>;
155 static constexpr
bool isAssembled = !std::is_same<matrix_type, int>::value;
157 static const matrix_type* getmat(std::shared_ptr<Operator> op){
158 std::shared_ptr<AssembledLinearOperator<matrix_type, Domain, Range>> aop
159 = std::dynamic_pointer_cast<AssembledLinearOperator<matrix_type, Domain, Range>>(op);
161 return &aop->getmat();
168 static std::shared_ptr<Solver>
get(std::shared_ptr<Operator> op,
169 const ParameterTree& config,
170 std::shared_ptr<Preconditioner> prec =
nullptr){
171 std::string type = config.get<std::string>(
"type");
172 std::shared_ptr<Solver> result;
173 const matrix_type*
mat = getmat(op);
182 DUNE_THROW(Dune::InvalidStateException,
"Solver not found in the factory.");
185 const ParameterTree& precConfig = config.sub(
"preconditioner");
186 std::string prec_type = precConfig.get<std::string>(
"type");
190 DUNE_THROW(NotImplemented,
"The solver factory is currently only implemented for sequential solvers!");
192 std::shared_ptr<ScalarProduct<Domain>> sp = std::make_shared<SeqScalarProduct<Domain>>();
201 const ParameterTree& config){
202 const matrix_type*
mat = getmat(op);
204 std::string prec_type = config.get<std::string>(
"type");
207 DUNE_THROW(InvalidStateException,
"Could not obtain matrix from operator. Please pass in an AssembledLinearOperator.");
222 template<
class Operator>
223 std::shared_ptr<InverseOperator<
typename Operator::domain_type,
225 const ParameterTree& config,
227 typename Operator::range_type>> prec =
nullptr){
Define general, extensible interface for inverse operators.
Matrix & mat
Definition: matrixmatrix.hh:345
std::shared_ptr< Preconditioner< X, Y > >(const M &, const ParameterTree &) PreconditionerSignature
Definition: solverfactory.hh:87
std::shared_ptr< InverseOperator< typename Operator::domain_type, typename Operator::range_type > > getSolverFromFactory(std::shared_ptr< Operator > op, const ParameterTree &config, std::shared_ptr< Preconditioner< typename Operator::domain_type, typename Operator::range_type >> prec=nullptr)
Instantiates an InverseOperator from an Operator and a configuration given as a ParameterTree.
Definition: solverfactory.hh:224
auto defaultIterativeSolverCreator()
Definition: solverfactory.hh:64
Singleton< ParameterizedObjectFactory< PreconditionerSignature< M, X, Y > >> PreconditionerFactory
Definition: solverfactory.hh:89
auto defaultPreconditionerBlockLevelCreator()
Definition: solverfactory.hh:38
Singleton< ParameterizedObjectFactory< DirectSolverSignature< M, X, Y > >> DirectSolverFactory
Definition: solverfactory.hh:83
std::shared_ptr< InverseOperator< X, Y > >(const std::shared_ptr< LinearOperator< X, Y > > &, const std::shared_ptr< ScalarProduct< X > > &, const std::shared_ptr< Preconditioner< X, Y > >, const ParameterTree &) IterativeSolverSignature
Definition: solverfactory.hh:93
std::shared_ptr< InverseOperator< X, Y > >(const M &, const ParameterTree &) DirectSolverSignature
Definition: solverfactory.hh:81
auto defaultPreconditionerCreator()
Definition: solverfactory.hh:51
Singleton< ParameterizedObjectFactory< IterativeSolverSignature< X, Y > >> IterativeSolverFactory
Definition: solverfactory.hh:95
Definition: allocator.hh:7
A generic dynamic dense matrix.
Definition: matrix.hh:558
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:30
Abstract base class for all solvers.
Definition: solver.hh:97
@ sequential
Category for sequential solvers.
Definition: solvercategory.hh:23
Definition: solverfactory.hh:124
Factory to assembly solvers configured by a ParameterTree.
Definition: solverfactory.hh:146
static std::shared_ptr< Solver > get(std::shared_ptr< Operator > op, const ParameterTree &config, std::shared_ptr< Preconditioner > prec=nullptr)
Definition: solverfactory.hh:168
static std::shared_ptr< Preconditioner > getPreconditioner(std::shared_ptr< Operator > op, const ParameterTree &config)
Definition: solverfactory.hh:200