AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ISTLPreconCreator.hpp
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include <dune/istl/preconditioners.hh>
7#include <dune/istl/novlpschwarz.hh>
8#include <dune/istl/schwarz.hh>
9
10#include <amdis/CreatorInterface.hpp>
11#include <amdis/CreatorMap.hpp>
12#include <amdis/linearalgebra/istl/PreconWrapper.hpp>
13#include <amdis/linearalgebra/istl/Traits.hpp>
14
15namespace AMDiS
16{
17 namespace tag
18 {
19 struct bjacobi {};
20
21 // forward declaration
22 template <class Traits>
23 struct solver;
24
25 template <class Traits>
26 struct preconditioner {};
27 }
28
29
31
38 template <class Traits>
40 : public CreatorInterface<tag::preconditioner<Traits>>
41 {
42 using X = typename Traits::X;
43 using Y = typename Traits::Y;
44
45 std::unique_ptr<tag::preconditioner<Traits>> create() final { return {}; };
46
47 public:
49 virtual void init(std::string const& prefix)
50 {
51 Parameters::get(prefix + "->relaxation", w_);
52 Parameters::get(prefix + "->iterations", iter_);
53 }
54
55 virtual std::unique_ptr<Dune::Preconditioner<X,Y>>
56 createPrecon(typename Traits::M const& A, typename Traits::Comm const& comm) const = 0;
57
58 protected:
59 double w_ = 1.0;
60 int iter_ = 1;
61 };
62
63
65
69 template <class Precon, class Traits>
71 : public ISTLPreconCreatorBase<Traits>
72 {
73 std::unique_ptr<typename Traits::Prec>
74 createPrecon(typename Traits::M const& mat, typename Traits::Comm const& /*comm*/) const override
75 {
76 return std::make_unique<Precon>(mat, this->iter_, this->w_);
77 }
78 };
79
80
82 template <class X, class Y, class Traits>
83 struct ISTLPreconCreator<Dune::Richardson<X, Y>, Traits>
84 : public ISTLPreconCreatorBase<Traits>
85 {
86 std::unique_ptr<typename Traits::Prec>
87 createPrecon(typename Traits::M const& /*mat*/, typename Traits::Comm const& /*comm*/) const override
88 {
89 using Precon = Dune::Richardson<X, Y>;
90 return std::make_unique<Precon>(this->w_);
91 }
92 };
93
94
96 template <class M, class X, class Y, class Traits>
97 struct ISTLPreconCreator<Dune::SeqILDL<M, X, Y>, Traits>
98 : public ISTLPreconCreatorBase<Traits>
99 {
100 std::unique_ptr<typename Traits::Prec>
101 createPrecon(typename Traits::M const& mat, typename Traits::Comm const& /*comm*/) const override
102 {
103 using Precon = Dune::SeqILDL<M, X, Y>;
104 return std::make_unique<Precon>(mat, this->w_);
105 }
106 };
107
108
110
114 template <class M, class X, class Y, class Comm, class Traits>
115 struct ISTLPreconCreator<Dune::ParSSOR<M, X, Y, Comm>, Traits>
116 : public ISTLPreconCreatorBase<Traits>
117 {
118 std::unique_ptr<typename Traits::Prec>
119 createPrecon(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
120 {
121 test_exit(Dune::SolverCategory::category(comm) == Dune::SolverCategory::overlapping,
122 "Dune::ParSSOR preconditioner can be used with overlapping domain decomposition.");
123
124 using Precon = Dune::ParSSOR<M,X,Y,Comm>;
125 return std::make_unique<Precon>(mat, this->iter_, this->w_, comm.impl());
126 }
127 };
128
129
130 // forward declaration
131 template <class Traits>
132 class ISTLSolverCreatorBase;
133
134
136
144 template <class Traits>
145 struct ISTLPreconCreator<tag::solver<Traits>, Traits>
146 : public ISTLPreconCreatorBase<Traits>
147 {
149
150 void init(std::string const& prefix) override
151 {
152 Super::init(prefix);
153
154 std::string solver = "default";
155 Parameters::get(prefix + "->solver", solver);
156
158 auto* creator = CreatorMap::get(solver, prefix + "->solver");
159 solverCreator_ = dynamic_cast<ISTLSolverCreatorBase<Traits>*>(creator);
160 assert(solverCreator_ != nullptr);
161
162 solverCreator_->init(prefix + "->solver");
163 }
164
165 std::unique_ptr<typename Traits::Prec>
166 createPrecon(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
167 {
168 using InverseOp = Dune::InverseOperator<typename Traits::X, typename Traits::Y>;
169 using Precon = Dune::InverseOperator2Preconditioner<InverseOp>;
170 using Wrapper = PreconWrapper<Precon, InverseOp>;
171
172 assert(solverCreator_ != nullptr);
173 return std::make_unique<Wrapper>(solverCreator_->createSolver(mat, comm));
174 }
175
176 private:
177 ISTLSolverCreatorBase<Traits>* solverCreator_ = nullptr;
178 };
179
180
182
193 template <class Traits>
194 struct ISTLPreconCreator<tag::bjacobi, Traits>
195 : public ISTLPreconCreatorBase<Traits>
196 {
199
200 void init(std::string const& prefix) override
201 {
202 Super::init(prefix);
203
204 std::string subPrecon = "default";
205 Parameters::get(prefix + "->sub precon", subPrecon);
206
208 auto* creator = CreatorMap::get(subPrecon, prefix + "->sub precon");
209 subPreconCreator_ = dynamic_cast<ISTLPreconCreatorBase<SeqTraits>*>(creator);
210 assert(subPreconCreator_ != nullptr);
211 subPreconCreator_->init(prefix + "->sub precon");
212 }
213
214 std::unique_ptr<typename Traits::Prec>
215 createPrecon(typename Traits::M const& mat, typename Traits::Comm const& comm) const override
216 {
217 assert(subPreconCreator_ != nullptr);
218 return Traits::ParPrecCreator::create(Dune::SolverCategory::category(comm),
219 subPreconCreator_->createPrecon(mat, comm.sequential()),
220 comm);
221 }
222
223 private:
224 ISTLPreconCreatorBase<SeqTraits>* subPreconCreator_ = nullptr;
225 };
226
227} // 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
Base class for precon creators,.
Definition: ISTLPreconCreator.hpp:41
virtual void init(std::string const &prefix)
Prepare the preconditioner for the creation.
Definition: ISTLPreconCreator.hpp:49
Base class for solver creators,.
Definition: ISTLSolverCreator.hpp:56
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
Definition: PreconWrapper.hpp:22
void init(std::string const &prefix) override
Prepare the preconditioner for the creation.
Definition: ISTLPreconCreator.hpp:200
void init(std::string const &prefix) override
Prepare the preconditioner for the creation.
Definition: ISTLPreconCreator.hpp:150
Default precon creator.
Definition: ISTLPreconCreator.hpp:72
Definition: Traits.hpp:68
Definition: ISTLPreconCreator.hpp:19
Definition: ISTLPreconCreator.hpp:26
Definition: ISTLSolverCreator.hpp:39