AMDiS  0.3
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/linearalgebra/RunnerInterface.hpp>
11 #include <amdis/linearalgebra/SolverInfo.hpp>
12 
13 #include <amdis/linearalgebra/mtl/ITL_Preconditioner.hpp>
14 
15 namespace AMDiS
16 {
23  template <class Mat, class Vec, class ITLSolver>
25  : public RunnerInterface<Mat,Vec>
26  {
27  using M = typename Mat::BaseMatrix;
28  using X = typename Vec::BaseVector;
29  using Y = typename Vec::BaseVector;
30 
32  using FloatType = typename Mat::value_type;
33 
34  public:
36  explicit KrylovRunner(std::string const& prefix)
37  : itlSolver_(prefix)
38  , prefix_(prefix)
39  {
40  Parameters::get(prefix_ + "->absolute tolerance", aTol_);
41  Parameters::get(prefix_ + "->relative tolerance", rTol_);
42  Parameters::get(prefix_ + "->max iteration", maxIter_);
43  Parameters::get(prefix_ + "->print cycle", printCycle_);
44  }
45 
47  void init(M const& A) override
48  {
49  createPrecon(prefix_);
50  P_->init(A);
51  }
52 
54  void exit() override
55  {
56  P_->exit();
57  }
58 
60  int solve(M const& A, X& x, Y const& b, SolverInfo& solverInfo) override
61  {
62  test_exit(bool(P_), "There is no preconditioner. Call init() before solve().");
63 
64  // print information about the solution process
65  itl::cyclic_iteration<FloatType> iter(two_norm(b - A*x), maxIter_, rTol_, aTol_, printCycle_);
66  iter.set_quite(solverInfo.info() == 0);
67  iter.suppress_resume(solverInfo.info() == 0);
68 
69  int error = itlSolver_(A, x, b, *P_, iter);
70  solverInfo.setAbsResidual(iter.resid());
71  solverInfo.setRelResidual(iter.relresid());
72 
73  return error;
74  }
75 
77  int adjointSolve(M const& A, X& x, Y const& b, SolverInfo& solverInfo) override
78  {
79  test_exit(bool(P_), "There is no preconditioner. Call init() before adjointSolve().");
80 
81  auto At = trans(A);
82 
83  // print information about the solution process
84  itl::cyclic_iteration<FloatType> iter(two_norm(b - At*x), maxIter_, rTol_, aTol_, printCycle_);
85  iter.set_quite(solverInfo.info() == 0);
86  iter.suppress_resume(solverInfo.info() == 0);
87 
88  int error = itlSolver_(At, x, b, *P_, iter);
89  solverInfo.setAbsResidual(iter.resid());
90  solverInfo.setRelResidual(iter.relresid());
91 
92  return error;
93  }
94 
95 
96  protected:
98  void createPrecon(std::string const& prefix)
99  {
100  // Creator for the left preconditioner
101  std::string preconName = "default";
102  Parameters::get(prefix + "->precon", preconName);
103 
104  auto creator
105  = named(CreatorMap<PreconBase>::getCreator(preconName, prefix + "->precon"));
106  P_ = creator->createWithString(prefix + "->precon");
107  }
108 
109  private:
111  ITLSolver itlSolver_;
112 
114  std::string prefix_;
115 
117  std::shared_ptr<PreconBase> P_;
118 
120  FloatType aTol_ = 0;
121 
123  FloatType rTol_ = 1.e-6;
124 
126  std::size_t maxIter_ = 1000;
127 
129  std::size_t printCycle_ = 100;
130  };
131 
132 } // end namespace AMDiS
int solve(M const &A, X &x, Y const &b, SolverInfo &solverInfo) override
Implementation of RunnerInterface::solve()
Definition: KrylovRunner.hpp:60
void setAbsResidual(double r)
Sets absResidual_.
Definition: SolverInfo.hpp:80
A CreatorMap is used to construct objects, which types depends on key words determined at run time...
Definition: CreatorMap.hpp:29
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
KrylovRunner(std::string const &prefix)
Constructor.
Definition: KrylovRunner.hpp:36
void exit() override
Implementation of RunnerInterface::exit()
Definition: KrylovRunner.hpp:54
void setRelResidual(double r)
Sets relResidual_.
Definition: SolverInfo.hpp:86
int info() const
Returns info.
Definition: SolverInfo.hpp:38
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
CreatorInterfaceName< BaseClass > * named(CreatorInterface< BaseClass > *ptr)
cast a ptr of CreatorInterface to CreatorInterfaceName
Definition: CreatorInterface.hpp:64
Wrapper class for different MTL4 itl-solvers. These solvers are parametrized by Matrix and Vector...
Definition: KrylovRunner.hpp:24
int adjointSolve(M const &A, X &x, Y const &b, SolverInfo &solverInfo) override
Implementation of RunnerInterface::solve()
Definition: KrylovRunner.hpp:77
void createPrecon(std::string const &prefix)
Create left/right preconditioners from parameters given in the init-file.
Definition: KrylovRunner.hpp:98
Definition: SolverInfo.hpp:11
Interface for Preconditioner y = M*x.
Definition: PreconditionerInterface.hpp:9
void test_exit(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message and exit
Definition: Output.hpp:163
void init(M const &A) override
Implementation of RunnerInterface::initImpl()
Definition: KrylovRunner.hpp:47
Interface for Runner / Worker types used in solver classes.
Definition: RunnerInterface.hpp:11