AMDiS  0.1 The Adaptive Multi-Dimensional Simulation Toolbox
IterativeRunner.hpp
1 #pragma once
2
3 #include <dune/istl/preconditioner.hh>
4
5 #include <amdis/linearalgebra/RunnerInterface.hpp>
6 #include <amdis/linearalgebra/SolverInfo.hpp>
7 #include <amdis/linearalgebra/eigen/PreconConfig.hpp>
8 #include <amdis/linearalgebra/eigen/SolverConfig.hpp>
9
10 namespace AMDiS
11 {
12  template <class Mat, class Vec, class IterativeSolver>
14  : public RunnerInterface<Mat,Vec>
15  {
16  using M = typename Mat::BaseMatrix;
17  using X = typename Vec::BaseVector;
18  using Y = typename Vec::BaseVector;
19
22
23  public:
24  IterativeRunner(std::string const& prefix)
25  : solver_{}
26  {
27  SolverCfg::init(prefix, solver_);
28  PreconCfg::init(prefix + "->precon", solver_.preconditioner());
29  Parameters::get(prefix + "->reuse pattern", reusePattern_);
30  }
31
33  void init(M const& A) override
34  {
35  if (!reusePattern_ || !initialized_) {
36  solver_.analyzePattern(A);
37  initialized_ = true;
38  }
39  solver_.factorize(A);
40
41  test_exit(solver_.info() == Eigen::Success,
42  "Error in solver.compute(matrix)");
43  }
44
46  void exit() override
47  {
48  initialized_ = false;
49  }
50
52  int solve([[maybe_unused]] M const& A, X& x, Y const& b, SolverInfo& solverInfo) override
53  {
54  x = solver_.solveWithGuess(b, x);
55
56  Y r = b;
57  if (x.norm() != 0)
58  r -= A * x;
59
60  solverInfo.setAbsResidual(r.norm());
61  solverInfo.setRelResidual(solver_.error());
62  solverInfo.setError(solver_.info());
63  msg("number of iteration: {}", solver_.iterations());
64
65  return solver_.info() == Eigen::Success ? 0 : int(solver_.info());
66  }
67
68  private:
69  IterativeSolver solver_;
70  bool reusePattern_ = false;
71  bool initialized_ = false;
72  };
73
74 } // end namespace AMDiS
Definition: PreconConfig.hpp:12
void setAbsResidual(double r)
Sets absResidual_.
Definition: SolverInfo.hpp:80
Contains all classes needed for solving linear and non linear equation systems.
void init(M const &A) override
Implements RunnerInterface::init()
Definition: IterativeRunner.hpp:33
void setRelResidual(double r)
Sets relResidual_.
Definition: SolverInfo.hpp:86
void msg(std::string const &str, Args &&... args)
print a message
Definition: Output.hpp:98
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
Definition: IterativeRunner.hpp:13
int solve([[maybe_unused]] M const &A, X &x, Y const &b, SolverInfo &solverInfo) override
Implements RunnerInterface::solve()
Definition: IterativeRunner.hpp:52
void setError(int e)
Sets error_.
Definition: SolverInfo.hpp:98
Definition: SolverInfo.hpp:11
Definition: SolverConfig.hpp:13
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
Interface for Runner / Worker types used in solver classes.
Definition: RunnerInterface.hpp:11
void exit() override
Implements RunnerInterface::exit()
Definition: IterativeRunner.hpp:46