DirectRunner.hpp 1.77 KB
Newer Older
1
2
3
4
5
#pragma once

#include <algorithm>
#include <string>

6
7
8
#include <amdis/linearalgebra/RunnerInterface.hpp>
#include <amdis/linearalgebra/SolverInfo.hpp>
#include <amdis/linearalgebra/eigen/SolverConfig.hpp>
9
10
11
12

namespace AMDiS
{
  /**
13
14
15
16
17
  * \ingroup Solver
  * \class AMDiS::DirectRunner
  * \brief \implements RunnerInterface for the (external) direct solvers
  */
  template <template <class> class Solver, class Matrix, class VectorX, class VectorB>
18
19
20
21
22
  class DirectRunner
      : public RunnerInterface<Matrix, VectorX, VectorB>
  {
  protected:
    using Super = RunnerInterface<Matrix, VectorX, VectorB>;
23
    using EigenSolver = Solver<Matrix>;
24
25
26

  public:
    /// Constructor.
Praetorius, Simon's avatar
Praetorius, Simon committed
27
    DirectRunner(std::string const& prefix)
28
29
      : solver_{}
    {
30
      SolverConfig<Solver>::init(prefix, solver_);
31
      Parameters::get(prefix + "->reuse pattern", reusePattern_);
32
33
34
    }

    /// Implementes \ref RunnerInterface::init()
35
    void init(Matrix const& A) override
36
    {
37
38
39
40
41
      if (!reusePattern_ || !initialized_) {
        solver_.analyzePattern(A);
        initialized_ = true;
      }
      solver_.factorize(A);
Praetorius, Simon's avatar
Praetorius, Simon committed
42
43
44

      test_exit(solver_.info() == Eigen::Success,
        "Error in solver.compute(matrix)");
45
46
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
47

48
    /// Implementes \ref RunnerInterface::exit()
49
    void exit() override {}
50
51

    /// Implementes \ref RunnerInterface::solve()
52
53
    int solve(Matrix const& A, VectorX& x, VectorB const& b,
              SolverInfo& solverInfo) override
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    {
      x = solver_.solve(b);

      auto r = VectorB(b);
      if (x.norm() != 0)
        r -= A * x;

      solverInfo.setAbsResidual(r.norm());
      solverInfo.setError(solver_.info());

      return solver_.info() == Eigen::Success ? 0 : 1;
    }

  private:
68
    EigenSolver solver_;
69
70
    bool reusePattern_ = false;
    bool initialized_ = false;
71
72
  };
}