1
/*$Id: tfqmr.c,v 1.61 2001/08/07 03:03:54 balay Exp $*/
4
This code implements the TFQMR (Transpose-free variant of Quasi-Minimal
5
Residual) method. Reference: Freund, 1993
7
Note that for the complex numbers version, the VecDot() arguments
8
within the code MUST remain in the order given for correct computation
12
#include "src/sles/ksp/kspimpl.h"
15
#define __FUNCT__ "KSPSetUp_TFQMR"
16
static int KSPSetUp_TFQMR(KSP ksp)
21
if (ksp->pc_side == PC_SYMMETRIC){
22
SETERRQ(2,"no symmetric preconditioning for KSPTFQMR");
24
ierr = KSPDefaultGetWork(ksp,9);CHKERRQ(ierr);
25
PetscFunctionReturn(0);
29
#define __FUNCT__ "KSPSolve_TFQMR"
30
static int KSPSolve_TFQMR(KSP ksp,int *its)
33
PetscScalar rho,rhoold,a,s,b,eta,etaold,psiold,cf,tmp,one = 1.0,zero = 0.0;
34
PetscReal dp,dpold,w,dpest,tau,psi,cm;
35
Vec X,B,V,P,R,RP,T,T1,Q,U,D,AUQ;
52
/* Compute initial preconditioned residual */
53
ierr = KSPInitialResidual(ksp,X,V,T,R,B);CHKERRQ(ierr);
55
/* Test for nothing to do */
56
ierr = VecNorm(R,NORM_2,&dp);CHKERRQ(ierr);
57
ierr = PetscObjectTakeAccess(ksp);CHKERRQ(ierr);
60
ierr = PetscObjectGrantAccess(ksp);CHKERRQ(ierr);
61
ierr = (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);CHKERRQ(ierr);
62
if (ksp->reason) {*its = 0; PetscFunctionReturn(0);}
65
/* Make the initial Rp == R */
66
ierr = VecCopy(R,RP);CHKERRQ(ierr);
68
/* Set the initial conditions */
74
ierr = VecDot(R,RP,&rhoold);CHKERRQ(ierr); /* rhoold = (r,rp) */
75
ierr = VecCopy(R,U);CHKERRQ(ierr);
76
ierr = VecCopy(R,P);CHKERRQ(ierr);
77
ierr = KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,T);CHKERRQ(ierr);
78
ierr = VecSet(&zero,D);CHKERRQ(ierr);
80
for (i=0; i<maxit; i++) {
81
ierr = PetscObjectTakeAccess(ksp);CHKERRQ(ierr);
83
ierr = PetscObjectGrantAccess(ksp);CHKERRQ(ierr);
84
ierr = VecDot(V,RP,&s);CHKERRQ(ierr); /* s <- (v,rp) */
85
a = rhoold / s; /* a <- rho / s */
86
tmp = -a; VecWAXPY(&tmp,V,U,Q);CHKERRQ(ierr); /* q <- u - a v */
87
ierr = VecWAXPY(&one,U,Q,T);CHKERRQ(ierr); /* t <- u + q */
88
ierr = KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,T,AUQ,T1);CHKERRQ(ierr);
89
ierr = VecAXPY(&tmp,AUQ,R);CHKERRQ(ierr); /* r <- r - a K (u + q) */
90
ierr = VecNorm(R,NORM_2,&dp);CHKERRQ(ierr);
98
cm = 1.0 / sqrt(1.0 + psi * psi);
101
cf = psiold * psiold * etaold / a;
103
ierr = VecAYPX(&cf,U,D);CHKERRQ(ierr);
105
ierr = VecAYPX(&cf,Q,D);CHKERRQ(ierr);
107
ierr = VecAXPY(&eta,D,X);CHKERRQ(ierr);
109
dpest = sqrt(m + 1.0) * tau;
110
ierr = PetscObjectTakeAccess(ksp);CHKERRQ(ierr);
112
ierr = PetscObjectGrantAccess(ksp);CHKERRQ(ierr);
113
KSPLogResidualHistory(ksp,dpest);
114
KSPMonitor(ksp,i+1,dpest);
115
ierr = (*ksp->converged)(ksp,i+1,dpest,&ksp->reason,ksp->cnvP);CHKERRQ(ierr);
116
if (ksp->reason) break;
121
if (ksp->reason) break;
123
ierr = VecDot(R,RP,&rho);CHKERRQ(ierr); /* rho <- (r,rp) */
124
b = rho / rhoold; /* b <- rho / rhoold */
125
ierr = VecWAXPY(&b,Q,R,U);CHKERRQ(ierr); /* u <- r + b q */
126
ierr = VecAXPY(&b,P,Q);CHKERRQ(ierr);
127
ierr = VecWAXPY(&b,Q,U,P);CHKERRQ(ierr); /* p <- u + b(q + b p) */
128
ierr = KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,Q);CHKERRQ(ierr); /* v <- K p */
135
ksp->reason = KSP_DIVERGED_ITS;
138
ierr = KSPUnwindPreconditioner(ksp,X,T);CHKERRQ(ierr);
140
PetscFunctionReturn(0);
145
#define __FUNCT__ "KSPCreate_TFQMR"
146
int KSPCreate_TFQMR(KSP ksp)
149
ksp->data = (void*)0;
150
ksp->pc_side = PC_LEFT;
151
ksp->ops->setup = KSPSetUp_TFQMR;
152
ksp->ops->solve = KSPSolve_TFQMR;
153
ksp->ops->destroy = KSPDefaultDestroy;
154
ksp->ops->buildsolution = KSPDefaultBuildSolution;
155
ksp->ops->buildresidual = KSPDefaultBuildResidual;
156
ksp->ops->setfromoptions = 0;
158
PetscFunctionReturn(0);