DirectRunner.hpp 1.7 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
13
14
15
16
17
18
19
20
21
22
23
24
25

namespace AMDiS
{
  /**
   * \ingroup Solver
   * \class AMDiS::DirectRunner
   * \brief \implements RunnerInterface for the (external) direct solvers
   */
  template <class LU, class Matrix, class VectorX, class VectorB>
  class DirectRunner
      : public RunnerInterface<Matrix, VectorX, VectorB>
  {
  protected:
    using Super = RunnerInterface<Matrix, VectorX, VectorB>;

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

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
46

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

    /// Implementes \ref RunnerInterface::solve()
51
52
    int solve(Matrix const& A, VectorX& x, VectorB const& b,
              SolverInfo& solverInfo) override
53
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:
    LU solver_;
68
69
    bool reusePattern_ = false;
    bool initialized_ = false;
70
71
  };
}