AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
SolverPrecon.hpp
1#pragma once
2
3#include <memory>
4
5#include <dune/istl/solver.hh>
6
7#include <amdis/CreatorMap.hpp>
8#include <amdis/Initfile.hpp>
9#include <amdis/Output.hpp>
10#include <amdis/linearalgebra/LinearSolverInterface.hpp>
11#include <amdis/linearalgebra/mtl/PreconditionerInterface.hpp>
12
13namespace AMDiS
14{
20 template <class M, class X, class Y>
22 : public PreconditionerInterface<M,X,Y>
23 {
24 using Self = SolverPrecon;
26
27 public:
30 {
31 std::unique_ptr<Super> createWithString(std::string prefix) override
32 {
33 return std::make_unique<Self>(std::move(prefix));
34 }
35 };
36
37 public:
38 SolverPrecon(std::string const& prefix)
39 {
40 std::string solverName = "default";
41 Parameters::get(prefix + "->solver", solverName);
42
43 auto solverCreator = named(CreatorMap<LinearSolverInterface<M,X,Y>>::get(solverName, prefix + "->solver"));
44 solver_ = solverCreator->createWithString(prefix + "->solver");
45 }
46
48 void init(M const& matrix) override
49 {
50 matrix_ = &matrix;
51 solver_->init(matrix);
52 stat_.clear();
53 }
54
56 void finish() override
57 {
58 matrix_ = nullptr;
59 }
60
62 void solve(X const& x, Y& y) const override
63 {
64 test_exit_dbg(bool(solver_), "No solver initialized!");
65 test_exit_dbg(bool(matrix_), "No matrix initialized!");
66
67 y.checked_change_resource(x);
68 test_exit(size(y) == num_cols(*matrix_), "incompatible size");
69 solver_->apply(y, x, stat_);
70 }
71
73 void adjoint_solve(X const& x, Y& y) const override
74 {
75 error_exit("Not Implemented.");
76 }
77
78 private:
79 mutable Dune::InverseOperatorResult stat_;
80
81 std::shared_ptr<LinearSolverInterface<M,X,Y>> solver_;
82 M const* matrix_ = nullptr;
83 };
84
85} // namespace AMDiS
Interface for creators with name.
Definition: CreatorInterface.hpp:44
A CreatorMap is used to construct objects, which types depends on key words determined at run time....
Definition: CreatorMap.hpp:30
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
Interface for Preconditioner y = M*x.
Definition: PreconditionerInterface.hpp:10
Use a LinearSolver as Preconditioner.
Definition: SolverPrecon.hpp:23
void finish() override
Implementation of PreconditionerInterface::exit()
Definition: SolverPrecon.hpp:56
void init(M const &matrix) override
Implementation of PreconditionerBase::init()
Definition: SolverPrecon.hpp:48
void adjoint_solve(X const &x, Y &y) const override
Implementation of PreconditionerInterface::adjointSolve()
Definition: SolverPrecon.hpp:73
void solve(X const &x, Y &y) const override
Implementation of PreconditionerInterface::solve()
Definition: SolverPrecon.hpp:62
A creator to be used instead of the constructor.
Definition: SolverPrecon.hpp:30
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:31