15 #ifndef ALLIUM_LA_GMRES_IMPL_HPP
16 #define ALLIUM_LA_GMRES_IMPL_HPP
20 #include "local_vector.hpp"
34 void real_givens(N& c, N& s, N a, N b)
40 if (std::abs(b) > std::abs(a)) {
42 s = 1.0 / sqrt(1.0 + tau*tau);
46 c = 1.0 / sqrt(1.0 + tau*tau);
52 template <
typename N>
void givens(N& c, N& s, N a, N b)
54 using Real = real_part_t<N>;
56 Real c_alpha, s_alpha;
57 real_givens(c_alpha, s_alpha, std::real(a), std::imag(a));
58 auto r_a = c_alpha * std::real(a) - s_alpha * std::imag(a);
61 real_givens(c_beta, s_beta, std::real(b), std::imag(b));
62 auto r_b = c_beta * std::real(b) - s_beta * std::imag(b);
64 Real c_theta, s_theta;
65 real_givens(c_theta, s_theta, r_a, r_b);
68 s = s_theta * N(c_alpha*c_beta+s_alpha*s_beta,
69 c_alpha*s_beta-c_beta*s_alpha);
72 template <>
inline void givens(
float& c,
float& s,
float a,
float b)
73 {
return real_givens<float>(c, s, a, b); }
74 template <>
inline void givens(
double& c,
double& s,
double a,
double b)
75 {
return real_givens<double>(c, s, a, b); }
82 double inline conj(
double z) {
return z; }
84 float inline conj(
float z) {
return z; }
90 template <
typename N,
typename V>
91 void gmres_rotate(
size_t i1,
size_t i2, N c, N s, V& u)
95 Number new_entry1 = c * u[i1] - s * u[i2];
96 Number new_entry2 = conj<N>(s) * u[i1] + c * u[i2];
105 template <
typename N>
109 using Real = real_part_t<Number>;
110 using Vector = LocalVector<Number>;
112 HessenbergQr(Number first_rhs_entry);
114 void add_column(LocalVector<N> next_column, Number next_rhs_entry);
117 Real residual_norm()
const;
120 Vector solution()
const;
122 std::vector<LocalVector<N>> m_r_columns;
123 std::vector<Number> m_cos_list;
124 std::vector<Number> m_sin_list;
125 std::vector<Number> m_rhs;
129 template <
typename N>
130 HessenbergQr<N>::HessenbergQr(Number first_rhs_entry)
131 : m_rhs({first_rhs_entry})
134 template <
typename N>
135 void HessenbergQr<N>::add_column(LocalVector<N> next_column, Number next_rhs_entry)
137 size_t n_columns = m_r_columns.size();
139 assert(next_column.rows() == n_columns + 2);
142 for (
size_t i_rot = 0; i_rot < n_columns; ++i_rot) {
143 gmres_rotate<Number>(i_rot, i_rot+1, m_cos_list[i_rot], m_sin_list[i_rot], next_column);
152 Number r = next_column[n_columns];
153 Number h = next_column[n_columns+1];
156 givens(new_c, new_s, r, h);
158 m_cos_list.push_back(new_c);
159 m_sin_list.push_back(new_s);
162 gmres_rotate<Number>(n_columns, n_columns+1, new_c, new_s, next_column);
165 m_rhs.push_back(next_rhs_entry);
166 gmres_rotate<Number>(n_columns, n_columns+1, new_c, new_s, m_rhs);
169 m_r_columns.push_back(std::move(next_column));
172 template <
typename N>
173 typename HessenbergQr<N>::Vector HessenbergQr<N>::solution()
const
175 size_t n_columns = m_r_columns.size();
177 Vector result(n_columns);
180 for (
int i_row = n_columns-1; i_row >= 0; i_row--)
182 Number acc = m_rhs[i_row];
183 for (
size_t i_col = i_row+1; i_col < n_columns; ++i_col) {
184 acc -= m_r_columns[i_col][i_row] * result[i_col];
186 result[i_row] = acc / m_r_columns[i_row][i_row];
193 template <
typename N>
194 typename HessenbergQr<N>::Real HessenbergQr<N>::residual_norm()
const
196 assert(m_rhs.size() == m_r_columns.size()+1);
197 size_t n_columns = m_r_columns.size();
198 return abs(m_rhs[n_columns]);
201 template <
typename N>
202 void GmresSolverBase<N>::solve(VectorStorage<N>& x,
203 const VectorStorage<N>& rhs,
206 auto tmp1 = allocate_like(rhs);
208 m_max_krylov_size = 30;
210 if (initial_guess == InitialGuess::NOT_PROVIDED) {
215 auto residual = allocate_like(rhs);
216 residual->assign(rhs);
217 this->apply_matrix(*tmp1, x);
218 residual->add_scaled(-1.0, *tmp1);
219 auto residual_norm = residual->l2_norm();
221 Real abs_tol = tolerance() * rhs.l2_norm();
223 m_iteration_count = 0;
225 if (residual_norm <= abs_tol)
228 bool success = inner_solve(x, *residual, residual_norm, abs_tol);
233 residual->assign(rhs);
234 this->apply_matrix(*tmp1, x);
235 residual->add_scaled(-1.0, *tmp1);
236 residual_norm = residual->l2_norm();
240 template <
typename N>
241 bool GmresSolverBase<N>::inner_solve(VectorStorage<N>& x,
242 const VectorStorage<N>& residual,
243 real_part_t<N> residual_norm,
244 real_part_t<N> abs_tol)
247 using Real = real_part_t<Number>;
249 bool success =
false;
251 std::vector<std::unique_ptr<VectorStorage<N>>> krylov_base;
253 Real beta = residual_norm;
255 auto v0 = allocate_like(residual);
256 v0->assign(residual);
258 krylov_base.push_back(std::move(v0));
260 HessenbergQr<N> qr(beta);
262 for (
size_t i_iteration = 0;
263 i_iteration < m_max_krylov_size && !success;
268 auto v_hat = allocate_like(residual);
269 this->apply_matrix(*v_hat, *krylov_base.at(i_iteration));
272 LocalVector<N> hessenberg_column(i_iteration + 2);
277 for (
size_t i_base = 0; i_base <= i_iteration; ++i_base) {
278 hessenberg_column[i_base] = v_hat->dot(*krylov_base.at(i_base));
279 v_hat->add_scaled(-hessenberg_column[i_base], *krylov_base.at(i_base));
283 Real hessenberg_extra = v_hat->l2_norm();
284 hessenberg_column[i_iteration+1] = hessenberg_extra;
290 *v_hat *= (1.0 / hessenberg_extra);
291 krylov_base.push_back(std::move(v_hat));
294 qr.add_column(std::move(hessenberg_column), 0.0);
296 if (qr.residual_norm() <= abs_tol)
300 LocalVector<N> y = qr.solution();
303 assert(y.rows() == krylov_base.size() - 1);
304 for (
size_t i_base = 0; i_base < y.rows(); ++i_base) {
305 x.add_scaled(y[i_base], *krylov_base[i_base]);
InitialGuess
Behavior for the initial guess.
Definition: linear_solver.hpp:32