Allium  0.1.0
cg.hpp
1 // Copyright 2020 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_CG_HPP
16 #define ALLIUM_LA_CG_HPP
17 
18 #include <allium/util/extern.hpp>
19 #include "vector_storage.hpp"
20 #include "sparse_matrix.hpp"
21 #include "linear_solver.hpp"
22 #include <allium/util/warnings.hpp>
23 
24 namespace allium {
25 
29  template <typename N>
30  class CgSolverBase {
31  public:
32  using Number = N;
33  using Real = real_part_t<N>;
34 
35  virtual ~CgSolverBase();
36 
37  CgSolverBase(Real tolerance = 1e-8)
38  {
39  m_tol = tolerance;
40  }
41 
42  void solve(VectorStorage<N>& solution,
43  const VectorStorage<N>& rhs,
45 
46  int iteration_count() { return m_iteration_count; }
47  protected:
48  Real m_tol;
49  int m_iteration_count;
50 
51  virtual void matvec(VectorStorage<N>& out, const VectorStorage<N>& in) = 0;
52  };
53 
58  template <typename V>
59  class CgSolver
60  : public CgSolverBase<typename V::Number>,
61  public LinearSolver<V> {
62  public:
63  using Matrix = LinearOperator<V>;
64  using typename LinearSolver<V>::Vector;
65  using Number = typename Vector::Number;
66  using Real = typename Vector::Real;
68 
70 
71  void setup(std::shared_ptr<Matrix> mat) override {
72  m_mat = mat;
73  }
74 
75  void solve(Vector& solution,
76  const Vector& rhs,
77  InitialGuess initial_guess = InitialGuess::NOT_PROVIDED) override {
78  CgSolverBase<Number>::solve(solution, rhs, initial_guess);
79  }
80 
81  private:
82  std::shared_ptr<Matrix> m_mat;
83 
84  void matvec(VectorStorage<Number>& out, const VectorStorage<Number>& in) override {
85  ALLIUM_NO_NONNULL_WARNING
86  allium_assert(dynamic_cast<const V*>(&in) != nullptr);
87  allium_assert(dynamic_cast<V*>(&out) != nullptr);
88  ALLIUM_RESTORE_WARNING
89  allium_assert(static_cast<bool>(m_mat), "operator provided");
90 
91  m_mat->apply(static_cast<V&>(out),
92  static_cast<const V&>(in));
93  }
94  };
95 
103  template <typename V, typename M>
104  void cg(V& solution, std::shared_ptr<M> mat, const V& rhs, real_part_t<typename V::Number> tol = 1e-8)
105  {
106  CgSolver<typename M::Vector> solver(tol);
107  solver.setup(mat);
108  solver.solve(solution, rhs);
109  }
110 
111  #define ALLIUM_CG_DECL(extern, N) \
112  extern template class CgSolverBase<N>;
113  ALLIUM_EXTERN_N(ALLIUM_CG_DECL)
114 }
115 
116 #endif
Base for the conjugate gradient method.
Definition: cg.hpp:30
The conjugate gradient method, solves .
Definition: cg.hpp:61
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
void cg(V &solution, std::shared_ptr< M > mat, const V &rhs, real_part_t< typename V::Number > tol=1e-8)
The conjugate gradient method.
Definition: cg.hpp:104
InitialGuess
Behavior for the initial guess.
Definition: linear_solver.hpp:32
@ NOT_PROVIDED
Initialize with zero.