22 CgSolverBase<N>::~CgSolverBase()
26 void CgSolverBase<N>::solve(VectorStorage<N>& solution,
27 const VectorStorage<N>& rhs,
30 auto residual = allocate_like(rhs);
31 auto new_residual = allocate_like(rhs);
32 auto x = allocate_like(rhs);
33 auto tmp1 = allocate_like(rhs);
35 switch (initial_guess) {
46 residual->assign(rhs);
47 residual->add_scaled(-1, *tmp1);
49 Real residual_norm_sq = std::real(residual->dot(*residual));
50 auto p = allocate_like(rhs);
53 Real abs_tol = rhs.l2_norm() * m_tol;
55 m_iteration_count = 0;
56 if (sqrt(residual_norm_sq) > abs_tol)
58 auto Ap = allocate_like(rhs);
64 Real alpha = std::real(residual->dot(*residual)) / std::real(p->dot(*Ap));
67 x->add_scaled(alpha, *p);
70 new_residual->assign(*residual);
71 new_residual->add_scaled(-alpha, *Ap);
73 Real new_residual_norm_sq
74 = std::real(new_residual->dot(*new_residual));
76 if (sqrt(new_residual_norm_sq) <= abs_tol)
break;
78 Real beta = new_residual_norm_sq / residual_norm_sq;
84 residual->assign(*new_residual);
85 residual_norm_sq = new_residual_norm_sq;
InitialGuess
Behavior for the initial guess.
Definition: linear_solver.hpp:32
@ NOT_PROVIDED
Initialize with zero.
@ PROVIDED
Use the value from the solution vector.