AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
ISTLSolverCreator.hpp
1#pragma once
2
3#include <memory>
4
5#include <dune/common/version.hh>
6
7#include <dune/common/ftraits.hh>
8#include <dune/istl/solvers.hh>
9#include <dune/istl/solvertype.hh>
10
11#if HAVE_SUITESPARSE_UMFPACK
12#include <dune/istl/umfpack.hh>
13#endif
14#if HAVE_SUITESPARSE_LDL
15#include <dune/istl/ldl.hh>
16#endif
17#if HAVE_SUITESPARSE_SPQR
18#include <dune/istl/spqr.hh>
19#endif
20#if HAVE_SUPERLU
21#include <dune/istl/superlu.hh>
22#endif
23
24#include <amdis/CreatorMap.hpp>
25#include <amdis/Environment.hpp>
26#include <amdis/Initfile.hpp>
27#include <amdis/Output.hpp>
28
29#include <amdis/linearalgebra/istl/ISTLPreconCreator.hpp>
30#include <amdis/linearalgebra/istl/SolverWrapper.hpp>
31#include <amdis/linearalgebra/istl/Traits.hpp>
32
33namespace AMDiS
34{
35 namespace tag
36 {
37 template <class Traits>
38 struct solver {};
39
40 // forward declaration
41 template <class Traits>
42 struct preconditioner;
43 }
44
46
52 template <class Traits>
54 : public CreatorInterface<tag::solver<Traits>>
55 {
56 using X = typename Traits::X;
57 using Y = typename Traits::Y;
58
59 std::unique_ptr<tag::solver<Traits>> create() final { return {}; };
60
61 public:
63 virtual void init(std::string const& prefix)
64 {
65 if (Environment::mpiRank() == 0)
66 Parameters::get(prefix + "->info", info_);
67 }
68
69 virtual std::unique_ptr<Dune::InverseOperator<X,Y>>
70 createSolver(typename Traits::M const& A, typename Traits::Comm const& comm) const = 0;
71
72 protected:
73 int info_ = 0;
74 };
75
76
78
86 template <class Traits>
88 : public ISTLSolverCreatorBase<Traits>
89 {
91
92 using real_type = typename Dune::FieldTraits<typename Traits::M::field_type>::real_type;
93
94 public:
95 void init(std::string const& prefix) override
96 {
97 Super::init(prefix);
98
99 maxIter_ = Parameters::get<int>(prefix + "->maxit").value_or(
100 Parameters::get<int>(prefix + "->max iteration").value_or(maxIter_));
101 rTol_ = Parameters::get<real_type>(prefix + "->reduction").value_or(
102 Parameters::get<real_type>(prefix + "->relative tolerance").value_or(rTol_));
103
104 std::string precon = "default";
105 Parameters::get(prefix + "->precon", precon);
106
108 auto* creator = CreatorMap::get(precon, prefix + "->precon");
109 preconCreator_ = dynamic_cast<ISTLPreconCreatorBase<Traits>*>(creator);
110 assert(preconCreator_ != nullptr);
111 preconCreator_->init(prefix + "->precon");
112 }
113
114 protected:
115 template <class Solver, class... Args>
116 auto create_impl(typename Traits::M const& mat, typename Traits::Comm const& comm, Args&&... args) const
117 {
118 auto cat = Dune::SolverCategory::category(comm);
119 auto sp = Traits::ScalProdCreator::create(cat, comm);
120 auto linOp = Traits::LinOpCreator::create(cat, mat, comm);
121
122 assert(preconCreator_ != nullptr);
123 auto precon = preconCreator_->createPrecon(mat, comm);
124 return std::make_unique<IterativeSolverWrapper<Solver>>(
125 std::move(linOp), std::move(sp), std::move(precon), FWD(args)...);
126 }
127
128 protected:
129 int maxIter_ = 500;
130 real_type rTol_ = 1.e-6;
131 ISTLPreconCreatorBase<Traits>* preconCreator_ = nullptr;
132 };
133
134
136
140 template <class Solver, class Traits>
141 class IterativeSolverCreator
142 : public ISTLIterativeSolverCreatorBase<Traits>
143 {
144 using Super = ISTLIterativeSolverCreatorBase<Traits>;
145 using Interface = typename Traits::Solver;
146
147 public:
148 std::unique_ptr<Interface>
149 createSolver(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
150 {
151 return this->template create_impl<Solver>(mat, comm, this->rTol_, this->maxIter_, this->info_);
152 }
153 };
154
155
157
164 template <class Solver, class Traits>
167 {
169 using Interface = typename Traits::Solver;
170
171 public:
172 void init(std::string const& prefix) override
173 {
174 Super::init(prefix);
175 Parameters::get(prefix + "->restart", restart_);
176 }
177
178 std::unique_ptr<Interface>
179 createSolver(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
180 {
181 return this->template create_impl<Solver>(mat, comm, this->rTol_, restart_, this->maxIter_, this->info_);
182 }
183
184 private:
185 int restart_ = 30;
186 };
187
188 template <class X, class Y, class Traits>
189 struct IterativeSolverCreator<Dune::RestartedGMResSolver<X,Y>, Traits>
190 : public GMResSolverCreator<Dune::RestartedGMResSolver<X,Y>, Traits>
191 {};
192
193 template <class X, class Y, class Traits>
194 struct IterativeSolverCreator<Dune::RestartedFlexibleGMResSolver<X,Y>, Traits>
195 : public GMResSolverCreator<Dune::RestartedFlexibleGMResSolver<X,Y>, Traits>
196 {};
197
198
200
207 template <class Solver, class Traits>
209 : public ISTLIterativeSolverCreatorBase<Traits>
210 {
212 using Interface = typename Traits::Solver;
213
214 public:
215 void init(std::string const& prefix) override
216 {
217 Super::init(prefix);
218 Parameters::get(prefix + "->restart", restart_);
219 }
220
221 std::unique_ptr<Interface>
222 createSolver(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
223 {
224 return this->template create_impl<Solver>(mat, comm, this->rTol_, this->maxIter_, this->info_, restart_);
225 }
226
227 private:
228 int restart_ = 30;
229 };
230
231 template <class X, class Traits>
232 struct IterativeSolverCreator<Dune::GeneralizedPCGSolver<X>, Traits>
233 : public PCGSolverCreator<Dune::GeneralizedPCGSolver<X>, Traits>
234 {};
235
236 template <class X, class Traits>
237 struct IterativeSolverCreator<Dune::RestartedFCGSolver<X>, Traits>
238 : public PCGSolverCreator<Dune::RestartedFCGSolver<X>, Traits>
239 {};
240
241 template <class X, class Traits>
242 struct IterativeSolverCreator<Dune::CompleteFCGSolver<X>, Traits>
243 : public PCGSolverCreator<Dune::CompleteFCGSolver<X>, Traits>
244 {};
245
246
248
258 template <class Solver, class Traits>
260 : public ISTLSolverCreatorBase<Traits>
261 {
263
264 void init(std::string const& prefix) override
265 {
266 Super::init(prefix);
267 Parameters::get(prefix + "->reuse vector", reuseVector_);
268 }
269
270 std::unique_ptr<typename Traits::Solver>
271 createSolver(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
272 {
273 test_exit(Dune::SolverCategory::category(comm) == Dune::SolverCategory::sequential,
274 "Direct solver can be used as sequential solver only.");
275 return std::make_unique<Solver>(mat, this->info_, reuseVector_);
276 }
277
278 protected:
279 bool reuseVector_ = true;
280 };
281
282
283 template <class Solver, class Traits>
284 using ISTLSolverCreator = std::conditional_t<Dune::IsDirectSolver<Solver>::value,
285 DirectSolverCreator<Solver,Traits>,
286 IterativeSolverCreator<Solver,Traits>>;
287
288} // end namespace AMDiS
Interface for the implementation of the factory method pattern. The creation of an object of a sub cl...
Definition CreatorInterface.hpp:24
A CreatorMap is used to construct objects, which types depends on key words determined at run time....
Definition CreatorMap.hpp:30
static CreatorInterface< BaseClass > * get(std::string key, std::string initFileStr)
Creates a object of the type corresponding to key.
Definition CreatorMap.hpp:44
static int mpiRank()
Return the MPI_Rank of the current processor.
Definition Environment.hpp:71
Base solver creator for iterative solvers.
Definition ISTLSolverCreator.hpp:89
void init(std::string const &prefix) override
Prepare the solver for the creation.
Definition ISTLSolverCreator.hpp:95
Base class for precon creators,.
Definition ISTLPreconCreator.hpp:41
Base class for solver creators,.
Definition ISTLSolverCreator.hpp:55
virtual void init(std::string const &prefix)
Prepare the solver for the creation.
Definition ISTLSolverCreator.hpp:63
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition Initfile.hpp:31
Default creator for direct solvers.
Definition ISTLSolverCreator.hpp:261
void init(std::string const &prefix) override
Prepare the solver for the creation.
Definition ISTLSolverCreator.hpp:264
Solver creator for iterative GMRes-like solvers.
Definition ISTLSolverCreator.hpp:167
void init(std::string const &prefix) override
Prepare the solver for the creation.
Definition ISTLSolverCreator.hpp:172
Solver creator for iterative CG-like solvers.
Definition ISTLSolverCreator.hpp:210
void init(std::string const &prefix) override
Prepare the solver for the creation.
Definition ISTLSolverCreator.hpp:215
Definition ISTLPreconCreator.hpp:26
Definition ISTLSolverCreator.hpp:38