DirectRunner.hpp 1.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#include <algorithm>
#include <string>

#include <amdis/linear_algebra/RunnerInterface.hpp>
#include <amdis/linear_algebra/SolverInfo.hpp>
#include <amdis/linear_algebra/eigen/SolverConfig.hpp>

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.
    DirectRunner(std::string prefix)
      : solver_{}
    {
      SolverConfig<LU>::init(prefix, solver_);
    }

    /// Implementes \ref RunnerInterface::init()
    virtual void init(Matrix const& A) override
    {
      solver_.compute(A);
Praetorius, Simon's avatar
Praetorius, Simon committed
36
37
38

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

Praetorius, Simon's avatar
Praetorius, Simon committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    /// Implementes \ref RunnerInterface::exit()
    virtual void exit() override {}

    /// Implementes \ref RunnerInterface::solve()
    virtual int solve(Matrix const& A, VectorX& x, VectorB const& b,
                      SolverInfo& solverInfo) override
    {
      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_;
  };
}