AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
ISTLSolver.hpp
1 #pragma once
2 
3 #include <dune/common/unused.hh>
4 #include <dune/istl/solver.hh>
5 
6 #include <amdis/CreatorMap.hpp>
7 #include <amdis/Initfile.hpp>
8 #include <amdis/linearalgebra/LinearSolverInterface.hpp>
9 #include <amdis/linearalgebra/SolverInfo.hpp>
10 #include <amdis/linearalgebra/istl/CreatorInterfaces.hpp>
11 #include <amdis/linearalgebra/istl/Traits.hpp>
12 
13 namespace AMDiS
14 {
16  template <class Mat, class Vec>
17  class ISTLSolver
18  : public LinearSolverInterface<Mat,Vec>
19  {
20  using Self = ISTLSolver;
23 
24  public:
26  struct Creator : CreatorInterfaceName<Super>
27  {
28  std::unique_ptr<Super> createWithString(std::string prefix) override
29  {
30  return std::make_unique<Self>(prefix);
31  }
32  };
33 
34  public:
35  explicit ISTLSolver(std::string const& prefix)
36  {
37  std::string solver = "default";
38  Parameters::get(prefix, solver);
39 
41  auto* creator = named(CreatorMap::getCreator(solver, prefix));
42  solverCreator_ = creator->createWithString(prefix);
43  assert(solverCreator_);
44  }
45 
46  protected:
48  void solveImpl(Mat const& A, Vec& x, Vec const& b, SolverInfo& solverInfo) override
49  {
50  auto solver_ = solverCreator_->create(A.matrix(), A.comm());
51 
52  // storing some statistics
53  Dune::InverseOperatorResult statistics;
54 
55  // solve the linear system
56  typename Vec::BaseVector _b = b.vector();
57  solver_->apply(x.vector(), _b, statistics);
58 
59  solverInfo.setRelResidual(statistics.reduction);
60  solverInfo.setError(statistics.converged ? 0 : 1);
61  }
62 
63  private:
64  std::shared_ptr<ISTLSolverCreatorInterface<Traits>> solverCreator_;
65  };
66 
67 } // end namespace AMDiS
void solveImpl(Mat const &A, Vec &x, Vec const &b, SolverInfo &solverInfo) override
Implements LinearSolverInterface::solveImpl()
Definition: ISTLSolver.hpp:48
A creator to be used instead of the constructor.
Definition: ISTLSolver.hpp:26
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: ISTLSolver.hpp:28
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 setRelResidual(double r)
Sets relResidual_.
Definition: SolverInfo.hpp:86
Implementation of RunnerInterface for ISTL solvers.
Definition: ISTLSolver.hpp:17
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
void setError(int e)
Sets error_.
Definition: SolverInfo.hpp:98
static CreatorInterface< BaseClass > * getCreator(std::string key, std::string initFileStr)
Creates a object of the type corresponding to key.
Definition: CreatorMap.hpp:44
Definition: SolverInfo.hpp:11
Definition: Traits.hpp:42