AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
KrylovRunner.hpp
1#pragma once
2
3#include <string>
4
5// MTL4 headers
6#include <boost/numeric/itl/itl.hpp>
7#include <boost/numeric/mtl/mtl.hpp>
8
9// AMDiS headers
10#include <amdis/Environment.hpp>
11#include <amdis/linearalgebra/LinearSolverInterface.hpp>
12#include <amdis/linearalgebra/mtl/Preconditioners.hpp>
13
14namespace AMDiS
15{
22 template <class M, class X, class Y, class ITLSolver>
24 : public LinearSolverInterface<M,X,Y>
25 {
26 using Self = KrylovRunner;
27
29 using FloatType = typename M::value_type;
30
31 public:
32 struct Creator final : CreatorInterfaceName<LinearSolverInterface<M,X,Y>>
33 {
34 std::unique_ptr<LinearSolverInterface<M,X,Y>>
35 createWithString(std::string prefix) final
36 {
37 return std::make_unique<Self>(prefix);
38 }
39 };
40
41 public:
43 explicit KrylovRunner(std::string const& prefix)
44 : itlSolver_(prefix)
45 {
46 Parameters::get(prefix + "->absolute tolerance", aTol_);
47 Parameters::get(prefix + "->relative tolerance", rTol_);
48 Parameters::get(prefix + "->max iteration", maxIter_);
49 Parameters::get(prefix + "->print cycle", printCycle_);
50 Parameters::get(prefix + "->info", info_);
51
52 createPrecon(prefix);
53 }
54
56 void init(M const& A) override
57 {
58 P_->init(A);
59 A_ = &A;
60 }
61
63 void finish() override
64 {
65 P_->finish();
66 }
67
69 void apply(X& x, Y const& b, Dune::InverseOperatorResult& stat) override
70 {
71 Dune::Timer t;
72
73 // print information about the solution process
74 FloatType r0 = two_norm(b - (*A_)*x);
75 itl::cyclic_iteration<FloatType> iter(r0, maxIter_, rTol_, aTol_, printCycle_);
76 iter.set_quite(info_ == 0);
77 iter.suppress_resume(info_ == 0);
78
79 [[maybe_unused]] int error = itlSolver_(*A_, x, b, *P_, iter);
80
81 stat.iterations = iter.iterations();
82 stat.converged = iter.is_converged();
83 stat.reduction = iter.relresid();
84 stat.elapsed = t.elapsed();
85 }
86
87 protected:
89 void createPrecon(std::string const& prefix)
90 {
91 // Creator for the left preconditioner
92 std::string preconName = "default";
93 Parameters::get(prefix + "->precon", preconName);
94
95 auto creator
96 = named(CreatorMap<PreconBase>::get(preconName, prefix + "->precon"));
97 P_ = creator->createWithString(prefix + "->precon");
98 }
99
100 private:
102 ITLSolver itlSolver_;
103
105 std::string prefix_;
106
108 std::shared_ptr<PreconBase> P_;
109
111 M const* A_ = nullptr;
112
114 FloatType aTol_ = 0;
115
117 FloatType rTol_ = 1.e-6;
118
120 std::size_t maxIter_ = 1000;
121
123 std::size_t printCycle_ = 100;
124
126 int info_ = Environment::infoLevel();
127 };
128
129} // end namespace AMDiS
Interface for creators with name.
Definition CreatorInterface.hpp:44
A CreatorMap is used to construct objects, which types depends on key words determined at run time....
Definition CreatorMap.hpp:30
static int infoLevel()
Return the info level for messages in info()
Definition Environment.cpp:63
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition Initfile.hpp:31
Wrapper class for different MTL4 itl-solvers. These solvers are parametrized by Matrix and Vector.
Definition KrylovRunner.hpp:25
KrylovRunner(std::string const &prefix)
Constructor.
Definition KrylovRunner.hpp:43
void init(M const &A) override
Implementation of LinearSolverInterface::init()
Definition KrylovRunner.hpp:56
void apply(X &x, Y const &b, Dune::InverseOperatorResult &stat) override
Implementation of LinearSolverInterface::apply()
Definition KrylovRunner.hpp:69
void finish() override
Implementation of LinearSolverInterface::finish()
Definition KrylovRunner.hpp:63
void createPrecon(std::string const &prefix)
Create left/right preconditioners from parameters given in the init-file.
Definition KrylovRunner.hpp:89
Definition LinearSolverInterface.hpp:9
Interface for Preconditioner y = M*x.
Definition PreconditionerInterface.hpp:10
Definition KrylovRunner.hpp:33
std::unique_ptr< LinearSolverInterface< M, X, Y > > createWithString(std::string prefix) final
Must be implemented by sub classes of CreatorInterfaceName. Creates a new instance of the sub class o...
Definition KrylovRunner.hpp:35