AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
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 
14 namespace 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 
51  createPrecon(prefix);
52  }
53 
55  void init(M const& A) override
56  {
57  P_->init(A);
58  A_ = &A;
59  }
60 
62  void finish() override
63  {
64  P_->finish();
65  }
66 
68  void apply(X& x, Y const& b, Dune::InverseOperatorResult& stat) override
69  {
70  Dune::Timer t;
71 
72  // print information about the solution process
73  FloatType r0 = two_norm(b - (*A_)*x);
74  itl::cyclic_iteration<FloatType> iter(r0, maxIter_, rTol_, aTol_, printCycle_);
75  iter.set_quite(Environment::infoLevel() == 0);
76  iter.suppress_resume(Environment::infoLevel() == 0);
77 
78  [[maybe_unused]] int error = itlSolver_(*A_, x, b, *P_, iter);
79 
80  stat.iterations = iter.iterations();
81  stat.converged = iter.is_converged();
82  stat.reduction = iter.relresid();
83  stat.elapsed = t.elapsed();
84  }
85 
86  protected:
88  void createPrecon(std::string const& prefix)
89  {
90  // Creator for the left preconditioner
91  std::string preconName = "default";
92  Parameters::get(prefix + "->precon", preconName);
93 
94  auto creator
95  = named(CreatorMap<PreconBase>::get(preconName, prefix + "->precon"));
96  P_ = creator->createWithString(prefix + "->precon");
97  }
98 
99  private:
101  ITLSolver itlSolver_;
102 
104  std::string prefix_;
105 
107  std::shared_ptr<PreconBase> P_;
108 
110  M const* A_ = nullptr;
111 
113  FloatType aTol_ = 0;
114 
116  FloatType rTol_ = 1.e-6;
117 
119  std::size_t maxIter_ = 1000;
120 
122  std::size_t printCycle_ = 100;
123  };
124 
125 } // end namespace AMDiS
void apply(X &x, Y const &b, Dune::InverseOperatorResult &stat) override
Implementation of LinearSolverInterface::apply()
Definition: KrylovRunner.hpp:68
Interface for creators with name.
Definition: CreatorInterface.hpp:42
static int infoLevel()
Return the info level for messages in info()
Definition: Environment.cpp:63
A CreatorMap is used to construct objects, which types depends on key words determined at run time...
Definition: CreatorMap.hpp:29
Definition: AdaptBase.hpp:6
KrylovRunner(std::string const &prefix)
Constructor.
Definition: KrylovRunner.hpp:43
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
Definition: LinearSolverInterface.hpp:8
Wrapper class for different MTL4 itl-solvers. These solvers are parametrized by Matrix and Vector...
Definition: KrylovRunner.hpp:23
void finish() override
Implementation of LinearSolverInterface::finish()
Definition: KrylovRunner.hpp:62
void createPrecon(std::string const &prefix)
Create left/right preconditioners from parameters given in the init-file.
Definition: KrylovRunner.hpp:88
Interface for Preconditioner y = M*x.
Definition: PreconditionerInterface.hpp:9
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
Definition: KrylovRunner.hpp:32
void init(M const &A) override
Implementation of LinearSolverInterface::init()
Definition: KrylovRunner.hpp:55