LinearSolver.hpp 2.14 KB
Newer Older
1
#pragma once
2

3
4
#include <string>

5
6
#include <dune/common/timer.hh>

7
// AMDiS includes
8
9
#include <amdis/CreatorInterface.hpp>
#include <amdis/Output.hpp>
10
#include <amdis/linearalgebra/LinearSolverInterface.hpp>
11

12
13
namespace AMDiS
{
14
15
16
  /** \ingroup Solver
   *
   * \brief
17
   * Wrapper class for various solvers. These solvers
18
19
   * are parametrized by MatrixType and VectorType. The different
   * algorithms, like krylov subspace methods or other external
20
   * solvers where the backend provides an interface, can be assigned
21
22
   * by different Runner objects.
   **/
23
  template <class Matrix, class VectorX, class VectorB, class Runner>
24
  class LinearSolver
25
      : public LinearSolverInterface<Matrix, VectorX, VectorB>
26
  {
27
    using Self = LinearSolver;
28
    using Super = LinearSolverInterface<Matrix, VectorX, VectorB>;
29
    using RunnerBase = typename Super::RunnerBase;
30

31
32
33
34
  public:
    /// A creator to be used instead of the constructor.
    struct Creator : CreatorInterfaceName<Super>
    {
35
      std::unique_ptr<Super> createWithString(std::string prefix) override
36
      {
37
        return std::make_unique<Self>(prefix);
38
39
      }
    };
40

41
  public:
42
    /// Constructor
43
    explicit LinearSolver(std::string prefix)
44
      : runner_(std::make_shared<Runner>(prefix))
45
    {}
46

47
48
    /// Implements \ref LinearSolverInterface::runner()
    virtual std::shared_ptr<RunnerBase> runner() override
49
    {
50
      return runner_;
51
    }
52
53
54

  private:
    /// Implements \ref LinearSolverInterface::solveSystemImpl()
55
56
    void solveImpl(Matrix const& A, VectorX& x, VectorB const& b,
                   SolverInfo& solverInfo) override
57
    {
58
      Dune::Timer t;
59
      if (solverInfo.doCreateMatrixData()) {
60
        // init matrix or wrap block-matrix or ...
61
        runner_->init(A);
62
      }
63

Praetorius, Simon's avatar
Praetorius, Simon committed
64
      if (solverInfo.info() > 0)
65
        msg("fill matrix needed {} seconds", t.elapsed());
66

67
      int error = runner_->solve(A, x, b, solverInfo);
68
      solverInfo.setError(error);
69

70
      if (!solverInfo.doStoreMatrixData())
71
        runner_->exit();
72
    }
73

74
  private:
75
    /// redirect the implementation to a runner. Implements a \ref RunnerInterface
76
    std::shared_ptr<Runner> runner_;
77
78
79
  };

} // end namespace AMDiS