11 #ifndef KRYLOV_METHODS_HPP
12 #define KRYLOV_METHODS_HPP
20 template<
typename DMatrixT,
typename DVectorT>
21 int BiCGStab(DMatrixT &A, DVectorT &x, DVectorT &b,
double tolerance,
uint maxiter=500,
double maxnorm=1e100,
int info=0)
24 typename DVectorT::DNhbd &nhbd = x.nhbd();
25 typename DVectorT::DVertex xb_o(nhbd), r_o(nhbd), rnxt_o(nhbd), p_o(nhbd), v_o(nhbd);
26 typename DVectorT::DVertex s_o(nhbd), t_o(nhbd), vec_o(nhbd), r0_o(nhbd), tx_o(nhbd);
29 DVectorT xb(xb_o), r(r_o), rnxt(rnxt_o), p(p_o), v(v_o);
30 DVectorT s(s_o), t(t_o), vec(vec_o), r0(r0_o), tx(tx_o);
40 uint iteration = 0, best = 1;
41 double rnorm, minnorm = 0;
44 double alpha, alpha_u, alpha_d;
45 double beta, beta_u, beta_d;
46 double omega, omega_u, omega_d;
57 alpha = alpha_u / alpha_d;
72 omega = omega_u / omega_d;
87 if(omega == 0 or beta_d == 0)
90 beta = (alpha*beta_u) / (omega*beta_d);
105 if(iteration == 0 or rnorm <= minnorm) {
113 std::cout << iteration <<
" iterations: " <<
" norm of residual: " << rnorm << std::endl;
115 while(!
isNan(rnorm) and rnorm < maxnorm and rnorm/100.0 < minnorm and rnorm > tolerance and iteration <= maxiter);
121 std::cout<<
"BICGSTAB iterations:"<< iteration - 1 <<
" best iteration:" << best <<
" norm:" << minnorm << std::endl;
123 return iteration - 1;
127 template<
typename DMatrixT,
typename DVectorT>
128 int BiCG(DMatrixT &A, DMatrixT &AT, DVectorT &x, DVectorT &b,
double tolerance,
uint maxiter=500,
double maxnorm=1e100,
int info=0)
131 typename DVectorT::DNhbd &nhbd = x.nhbd();
132 typename DVectorT::DVertex r1_o(nhbd), r2_o(nhbd), p1_o(nhbd), p2_o(nhbd);
133 typename DVectorT::DVertex Ap1_o(nhbd), ATp2_o(nhbd), t_o(nhbd);
136 DVectorT r1(r1_o), r2(r2_o), p1(p1_o), p2(p2_o), Ap1(Ap1_o), ATp2(ATp2_o), t(t_o);
146 std::cout <<
"Initial residual below tolerance" << std::endl;
150 uint iteration = 1, best = 1;
158 double p2Ap1 = p2 * Ap1;
159 if(info > 0 and p2Ap1 == 0)
160 std::cout <<
"error" << std::endl;
162 double alpha = rr / p2Ap1;
173 double beta = rr/oldrr;
180 if(minnorm == 0 or rr <= minnorm) {
186 std::cout << iteration <<
" iterations: " <<
" norm of residual: " << rr << std::endl;
190 while(!
isNan(rr) and rr < maxnorm and rr/100.0 < minnorm and rr > tolerance and iteration <= maxiter);
193 std::cout<<
"BICG iterations:"<< iteration - 1 <<
" best iteration:" << best <<
" norm:" << minnorm << std::endl;
195 return iteration - 1;
199 template<
typename DMatrixT,
typename DVectorT>
200 int CG(DMatrixT &A, DVectorT &x, DVectorT &b,
double tolerance,
uint maxiter=500,
double maxnorm=1e100,
int info=0)
203 typename DVectorT::DNhbd &nhbd = x.nhbd();
204 typename DVectorT::DVertex r_o(nhbd), p_o(nhbd), ap_o(nhbd), t_o(nhbd);
207 DVectorT r(r_o), p(p_o), ap(ap_o);
216 uint iteration = 1, best = 1;
222 double alpha = p * ap;
229 double beta = rr/oldrr;
233 if(minnorm == 0 or rr <= minnorm) {
239 std::cout << iteration <<
" iterations: " <<
" norm of residual: " << rr << std::endl;
243 while(!
isNan(rr) and rr < maxnorm and rr/100.0 < minnorm and rr > tolerance and iteration <= maxiter);
246 std::cout<<
"CG iterations:"<< iteration - 1 <<
" best iteration:" << best <<
" norm:" << minnorm << std::endl;
248 return iteration - 1;
252 template<
typename DMatrixT,
typename DVectorT>
253 int CGPreCond(DMatrixT &A, DVectorT &x, DVectorT &b,
double tolerance,
uint maxiter=500,
double maxnorm=1e100,
int info=0)
256 typename DVectorT::DNhbd &nhbd = x.nhbd();
257 typename DVectorT::DVertex r_o(nhbd), p_o(nhbd), ap_o(nhbd), t_o(nhbd), z_o(nhbd);
258 typename DMatrixT::DVertex Mv_o(nhbd);
259 typename DMatrixT::DEdge Me_o(nhbd);
262 DVectorT r(r_o), p(p_o), ap(ap_o), z(z_o);
264 DMatrixT M(Mv_o, Me_o);
274 uint iteration = 1, best = 1;
279 double alpha = p * ap;
287 double beta = rz/oldrz;
291 if(minnorm == 0 or rz <= minnorm) {
297 std::cout << iteration <<
" iterations: " <<
" norm of residual: " << rz << std::endl;
301 while(!
isNan(rz) and rz > tolerance and iteration <= maxiter);
305 std::cout<<
"CG iterations:"<< iteration - 1 <<
" best iteration:" << best <<
" norm:" << minnorm << std::endl;
307 return iteration - 1;
312 template<
typename DMatrixT,
typename DVectorT>
313 int BiCGStabPreCond(DMatrixT &A, DVectorT &x, DVectorT &b,
double tolerance,
uint maxiter=500,
double maxnorm=1e100,
int info=0)
316 typename DVectorT::DNhbd &nhbd = x.nhbd();
317 typename DVectorT::DVertex xb_o(nhbd), r_o(nhbd), rnxt_o(nhbd), p_o(nhbd), v_o(nhbd), pHat_o(nhbd), sHat_o(nhbd);
318 typename DVectorT::DVertex s_o(nhbd), t_o(nhbd), vec_o(nhbd), r0_o(nhbd), tx_o(nhbd);
321 DVectorT xb(xb_o), r(r_o), rnxt(rnxt_o), p(p_o), v(v_o), pHat(pHat_o), sHat(sHat_o);;
322 DVectorT s(s_o), t(t_o), vec(vec_o), r0(r0_o), tx(tx_o);
325 typename DMatrixT::DVertex Mv_o(nhbd);
326 typename DMatrixT::DEdge Me_o(nhbd);
328 DMatrixT M(Mv_o, Me_o);
339 uint iteration = 0, best = 1;
340 double rnorm, minnorm = 0;
343 double alpha, alpha_u, alpha_d;
344 double beta, beta_u, beta_d;
345 double omega, omega_u, omega_d;
358 alpha = alpha_u / alpha_d;
374 omega = omega_u / omega_d;
389 if(omega == 0 or beta_d == 0)
392 beta = (alpha*beta_u) / (omega*beta_d);
407 if(iteration == 0 or rnorm <= minnorm) {
415 std::cout << iteration <<
" iterations: " <<
" norm of residual: " << rnorm << std::endl;
417 while(!
isNan(rnorm) and rnorm < maxnorm and rnorm > tolerance and iteration <= maxiter);
424 std::cout<<
"BICGSTAB iterations:"<< iteration - 1 <<
" best iteration:" << best <<
" norm:" << minnorm << std::endl;
426 return iteration - 1;