AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
SolverPrecon.hpp
1 #pragma once
2 
3 #include <memory>
4 
5 #include <amdis/CreatorMap.hpp>
6 #include <amdis/Initfile.hpp>
7 #include <amdis/Output.hpp>
8 #include <amdis/linearalgebra/LinearSolverInterface.hpp>
9 #include <amdis/linearalgebra/mtl/PreconditionerInterface.hpp>
10 
11 namespace AMDiS
12 {
18  template <class Mat, class Vec>
20  : public PreconditionerInterface<Mat,Vec>
21  {
22  using M = typename Mat::BaseMatrix;
23  using X = typename Vec::BaseVector;
24  using Y = typename Vec::BaseVector;
25 
26  using Self = SolverPrecon;
28 
29  public:
31  struct Creator : CreatorInterfaceName<Super>
32  {
33  std::unique_ptr<Super> createWithString(std::string prefix) override
34  {
35  return std::make_unique<Self>(std::move(prefix));
36  }
37  };
38 
39  public:
40  SolverPrecon(std::string const& prefix)
41  : solverInfo_(prefix + "->solver")
42  {
43  std::string solverName = "default";
44  Parameters::get(prefix + "->solver", solverName);
45 
46  auto solverCreator = named(CreatorMap<LinearSolverInterface<Mat,Vec>>::getCreator(solverName, prefix + "->solver"));
47  solver_ = solverCreator->createWithString(prefix + "->solver");
48  }
49 
51  void init(M const& matrix) override
52  {
53  matrix_ = &matrix;
54  solver_->runner()->init(matrix);
55  }
56 
58  void exit() override
59  {
60  matrix_ = nullptr;
61  }
62 
64  void solve(X const& x, Y& y) const override
65  {
66  test_exit_dbg(bool(solver_), "No solver initialized!");
67  test_exit_dbg(bool(matrix_), "No matrix initialized!");
68 
69  y.checked_change_resource(x);
70  test_exit(size(y) == num_cols(*matrix_), "incompatible size");
71  solver_->runner()->solve(*matrix_, y, x, solverInfo_);
72  }
73 
75  void adjoint_solve(X const& x, Y& y) const override
76  {
77  error_exit("Not Implemented.");
78  test_exit_dbg(bool(solver_), "No solver initialized!");
79  test_exit_dbg(bool(matrix_), "No matrix initialized!");
80 
81  y.checked_change_resource(x);
82  test_exit(size(y) == num_rows(*matrix_), "incompatible size");
83  solver_->runner()->adjointSolve(*matrix_, y, x, solverInfo_);
84  }
85 
86  private:
87  mutable SolverInfo solverInfo_;
88 
89  std::shared_ptr<LinearSolverInterface<Mat,Vec>> solver_;
90  M const* matrix_ = nullptr;
91  };
92 
93 } // namespace AMDiS
A creator to be used instead of the constructor.
Definition: SolverPrecon.hpp:31
void solve(X const &x, Y &y) const override
Implementation of PreconditionerInterface::solve()
Definition: SolverPrecon.hpp:64
void error_exit(std::string const &str, Args &&... args)
print a message and exit
Definition: Output.hpp:142
void adjoint_solve(X const &x, Y &y) const override
Implementation of PreconditionerInterface::adjointSolve()
Definition: SolverPrecon.hpp:75
void test_exit_dbg(bool condition, Args &&... args)
call assert_msg, in debug mode only
Definition: Output.hpp:205
Use a LinearSolver as Preconditioner.
Definition: SolverPrecon.hpp:19
Interface for creators with name.
Definition: CreatorInterface.hpp:42
A CreatorMap is used to construct objects, which types depends on key words determined at run time...
Definition: CreatorMap.hpp:29
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
void init(M const &matrix) override
Implementation of PreconditionerBase::init()
Definition: SolverPrecon.hpp:51
void exit() override
Implementation of PreconditionerInterface::exit()
Definition: SolverPrecon.hpp:58
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
CreatorInterfaceName< BaseClass > * named(CreatorInterface< BaseClass > *ptr)
cast a ptr of CreatorInterface to CreatorInterfaceName
Definition: CreatorInterface.hpp:64
Abstract base class for linear solvers.
Definition: LinearSolverInterface.hpp:27
Definition: SolverInfo.hpp:11
Interface for Preconditioner y = M*x.
Definition: PreconditionerInterface.hpp:9
std::unique_ptr< Super > createWithString(std::string prefix) override
Must be implemented by sub classes of CreatorInterfaceName. Creates a new instance of the sub class o...
Definition: SolverPrecon.hpp:33
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