Allium  0.1.0
iterative_solver.hpp
1 // Copyright 2021 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_ITERATIVE_SOLVER_HPP
16 #define ALLIUM_LA_ITERATIVE_SOLVER_HPP
17 
18 #include <allium/util/numeric.hpp>
19 #include "vector_storage.hpp"
20 #include "linear_solver.hpp"
21 #include "linear_operator.hpp"
22 
23 namespace allium {
24 
28  template <typename N>
30  public:
31  using Number = N;
32 
33  IterativeSolverBase(real_part_t<N> tol = 1e-8) : m_tol(tol) {}
34  virtual ~IterativeSolverBase() {};
35 
36  real_part_t<N> tolerance() { return m_tol; }
37  void tolerance(real_part_t<N> t) { m_tol = t; }
38  protected:
39  virtual void apply_matrix(VectorStorage<Number>& out, const VectorStorage<Number>& in) = 0;
40 
41  private:
42  real_part_t<N> m_tol;
43  };
44 
48  template <typename Base, typename V>
50  public Base,
51  public LinearSolver<V>
52  {
53  public:
54  using Base::Base;
55  using Vector = V;
56  using Number = typename Vector::Number;
57  using Matrix = LinearOperator<V>;
58 
59  void setup(std::shared_ptr<Matrix> mat) override {
60  m_mat = mat;
61  }
62 
63  void solve(V& solution,
64  const V& rhs,
65  InitialGuess initial_guess = InitialGuess::NOT_PROVIDED) override {
66  Base::solve(solution, rhs, initial_guess);
67  }
68 
69  private:
70  std::shared_ptr<Matrix> m_mat;
71 
72  void apply_matrix(VectorStorage<Number>& out, const VectorStorage<Number>& in) override
73  {
74  ALLIUM_NO_NONNULL_WARNING
75  allium_assert(dynamic_cast<const V*>(&in) != nullptr);
76  allium_assert(dynamic_cast<V*>(&out) != nullptr);
77  ALLIUM_RESTORE_WARNING
78 
79  m_mat->apply(static_cast<V&>(out),
80  static_cast<const V&>(in));
81  }
82  };
83 
84 };
85 
86 #endif
Base class for iterative solvers.
Definition: iterative_solver.hpp:29
Mixin for iterative solvers, which implements the apply_matrix method.
Definition: iterative_solver.hpp:52
Linear operator for the use in iterative linear solvers.
Definition: linear_operator.hpp:29
Linear solver interface.
Definition: linear_solver.hpp:42
Abstract base class for any vector storage.
Definition: vector_storage.hpp:41
InitialGuess
Behavior for the initial guess.
Definition: linear_solver.hpp:32
@ NOT_PROVIDED
Initialize with zero.