DUNE PDELab (git)

solverfactory.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_ISTL_SOLVERFACTORY_HH
7#define DUNE_ISTL_SOLVERFACTORY_HH
8
9#include <unordered_map>
10#include <functional>
11#include <memory>
12
15
16#include "solverregistry.hh"
17#include <dune/istl/solver.hh>
18#include <dune/istl/schwarz.hh>
19#include <dune/istl/novlpschwarz.hh>
20
21namespace Dune{
26 // Direct solver factory:
27 template<class M, class X, class Y>
28 using DirectSolverSignature = std::shared_ptr<InverseOperator<X,Y>>(const M&, const ParameterTree&);
29 template<class M, class X, class Y>
30 using DirectSolverFactory = Singleton<ParameterizedObjectFactory<DirectSolverSignature<M,X,Y>>>;
31
32 // Preconditioner factory:
33 template<class M, class X, class Y>
34 using PreconditionerSignature = std::shared_ptr<Preconditioner<X,Y>>(const std::shared_ptr<M>&, const ParameterTree&);
35 template<class M, class X, class Y>
36 using PreconditionerFactory = Singleton<ParameterizedObjectFactory<PreconditionerSignature<M,X,Y>>>;
37
38 // Iterative solver factory
39 template<class X, class Y>
40 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&);
41 template<class X, class Y>
42 using IterativeSolverFactory = Singleton<ParameterizedObjectFactory<IterativeSolverSignature<X,Y>>>;
43
44 // initSolverFactories differs in different compilation units, so we have it
45 // in an anonymous namespace
46 namespace {
47
53 template<class O>
54 int initSolverFactories(){
55 using M = typename O::matrix_type;
56 using X = typename O::range_type;
57 using Y = typename O::domain_type;
58 using TL = Dune::TypeList<M,X,Y>;
60 addRegistryToFactory<TL>(dsfac, DirectSolverTag{});
62 addRegistryToFactory<TL>(pfac, PreconditionerTag{});
63 using TLS = Dune::TypeList<X,Y>;
65 return addRegistryToFactory<TLS>(isfac, IterativeSolverTag{});
66 }
67 } // end anonymous namespace
68
69
70 template<class O, class Preconditioner>
71 std::shared_ptr<Preconditioner> wrapPreconditioner4Parallel(const std::shared_ptr<Preconditioner>& prec,
72 const O&)
73 {
74 return prec;
75 }
76
77 template<class M, class X, class Y, class C, class Preconditioner>
78 std::shared_ptr<Preconditioner>
79 wrapPreconditioner4Parallel(const std::shared_ptr<Preconditioner>& prec,
80 const std::shared_ptr<OverlappingSchwarzOperator<M,X,Y,C> >& op)
81 {
82 return std::make_shared<BlockPreconditioner<X,Y,C,Preconditioner> >(prec, op->getCommunication());
83 }
84
85 template<class M, class X, class Y, class C, class Preconditioner>
86 std::shared_ptr<Preconditioner>
87 wrapPreconditioner4Parallel(const std::shared_ptr<Preconditioner>& prec,
88 const std::shared_ptr<NonoverlappingSchwarzOperator<M,X,Y,C> >& op)
89 {
90 return std::make_shared<NonoverlappingBlockPreconditioner<C,Preconditioner> >(prec, op->getCommunication());
91 }
92
93 template<class M, class X, class Y>
94 std::shared_ptr<ScalarProduct<X>> createScalarProduct(const std::shared_ptr<MatrixAdapter<M,X,Y> >&)
95 {
96 return std::make_shared<SeqScalarProduct<X>>();
97 }
98 template<class M, class X, class Y, class C>
99 std::shared_ptr<ScalarProduct<X>> createScalarProduct(const std::shared_ptr<OverlappingSchwarzOperator<M,X,Y,C> >& op)
100 {
101 return createScalarProduct<X>(op->getCommunication(), op->category());
102 }
103
104 template<class M, class X, class Y, class C>
105 std::shared_ptr<ScalarProduct<X>> createScalarProduct(const std::shared_ptr<NonoverlappingSchwarzOperator<M,X,Y,C> >& op)
106 {
107 return createScalarProduct<X>(op->getCommunication(), op->category());
108 }
109
129 template<class Operator>
131 using Domain = typename Operator::domain_type;
132 using Range = typename Operator::range_type;
135
136 template<class O>
137 using _matrix_type = typename O::matrix_type;
139 static constexpr bool isAssembled = !std::is_same<matrix_type, int>::value;
140
141 static const matrix_type* getmat(std::shared_ptr<Operator> op){
142 std::shared_ptr<AssembledLinearOperator<matrix_type, Domain, Range>> aop
143 = std::dynamic_pointer_cast<AssembledLinearOperator<matrix_type, Domain, Range>>(op);
144 if(aop)
145 return &aop->getmat();
146 return nullptr;
147 }
148
149 public:
150
153 static std::shared_ptr<Solver> get(std::shared_ptr<Operator> op,
154 const ParameterTree& config,
155 std::shared_ptr<Preconditioner> prec = nullptr){
156 std::string type = config.get<std::string>("type");
157 std::shared_ptr<Solver> result;
158 const matrix_type* mat = getmat(op);
159 if(mat){
161 if(op->category()!=SolverCategory::sequential){
162 DUNE_THROW(NotImplemented, "The solver factory does not support parallel direct solvers!");
163 }
164 result = DirectSolverFactory<matrix_type, Domain, Range>::instance().create(type, *mat, config);
165 return result;
166 }
167 }
168 // if no direct solver is found it might be an iterative solver
170 DUNE_THROW(Dune::InvalidStateException, "Solver not found in the factory.");
171 }
172 if(!prec){
173 const ParameterTree& precConfig = config.sub("preconditioner");
174 std::string prec_type = precConfig.get<std::string>("type");
175 prec = PreconditionerFactory<Operator, Domain, Range>::instance().create(prec_type, op, precConfig);
176 if (prec->category() != op->category() && prec->category() == SolverCategory::sequential)
177 // try to wrap to a parallel preconditioner
178 prec = wrapPreconditioner4Parallel(prec, op);
179 }
180 std::shared_ptr<ScalarProduct<Domain>> sp = createScalarProduct(op);
181 result = IterativeSolverFactory<Domain, Range>::instance().create(type, op, sp, prec, config);
182 return result;
183 }
184
188 static std::shared_ptr<Preconditioner> getPreconditioner(std::shared_ptr<Operator> op,
189 const ParameterTree& config){
190 const matrix_type* mat = getmat(op);
191 if(mat){
192 std::string prec_type = config.get<std::string>("type");
193 return PreconditionerFactory<Operator, Domain, Range>::instance().create(prec_type, op, config);
194 }else{
195 DUNE_THROW(InvalidStateException, "Could not obtain matrix from operator. Please pass in an AssembledLinearOperator.");
196 }
197 }
198 };
199
210 template<class Operator>
211 std::shared_ptr<InverseOperator<typename Operator::domain_type,
212 typename Operator::range_type>> getSolverFromFactory(std::shared_ptr<Operator> op,
213 const ParameterTree& config,
214 std::shared_ptr<Preconditioner<typename Operator::domain_type,
215 typename Operator::range_type>> prec = nullptr){
216 return SolverFactory<Operator>::get(op, config, prec);
217 }
218
222} // end namespace Dune
223
224
225#endif
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Default exception for dummy implementations.
Definition: exceptions.hh:263
Hierarchical structure of string parameters.
Definition: parametertree.hh:37
std::string get(const std::string &key, const std::string &defaultValue) const
get value as string
Definition: parametertree.cc:181
ParameterTree & sub(const std::string &sub)
get substructure by name
Definition: parametertree.cc:99
Base class for matrix free definition of preconditioners.
Definition: preconditioner.hh:33
An adapter to turn a class into a singleton.
Definition: singleton.hh:56
static DUNE_EXPORT T & instance()
Get the instance of the singleton.
Definition: singleton.hh:70
Factory to assembly solvers configured by a ParameterTree.
Definition: solverfactory.hh:130
static std::shared_ptr< Solver > get(std::shared_ptr< Operator > op, const ParameterTree &config, std::shared_ptr< Preconditioner > prec=nullptr)
get a solver from the factory
Definition: solverfactory.hh:153
static std::shared_ptr< Preconditioner > getPreconditioner(std::shared_ptr< Operator > op, const ParameterTree &config)
Construct a Preconditioner for a given Operator.
Definition: solverfactory.hh:188
Define general, extensible interface for inverse operators.
typename detected_or< Default, Op, Args... >::type detected_or_t
Returns Op<Args...> if that is valid; otherwise returns the fallback type Default.
Definition: type_traits.hh:189
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
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:212
std::tuple< MetaType< T >... > TypeList
A simple type list.
Definition: typelist.hh:87
Dune namespace.
Definition: alignedallocator.hh:13
std::shared_ptr< ScalarProduct< X > > createScalarProduct(const Comm &comm, SolverCategory::Category category)
Definition: scalarproducts.hh:242
constexpr std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137
A hierarchical structure of string parameters.
Useful wrapper for creating singletons.
@ sequential
Category for sequential solvers.
Definition: solvercategory.hh:25
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)