Actual source code: tfqmr.c
1: /*$Id: tfqmr.c,v 1.61 2001/08/07 03:03:54 balay Exp $*/
3: /*
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
9: of inner products.
10: */
12: #include src/sles/ksp/kspimpl.h
16: static int KSPSetUp_TFQMR(KSP ksp)
17: {
21: if (ksp->pc_side == PC_SYMMETRIC){
22: SETERRQ(2,"no symmetric preconditioning for KSPTFQMR");
23: }
24: KSPDefaultGetWork(ksp,9);
25: return(0);
26: }
30: static int KSPSolve_TFQMR(KSP ksp,int *its)
31: {
32: int i,maxit,m, ierr;
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;
38: maxit = ksp->max_it;
39: X = ksp->vec_sol;
40: B = ksp->vec_rhs;
41: R = ksp->work[0];
42: RP = ksp->work[1];
43: V = ksp->work[2];
44: T = ksp->work[3];
45: Q = ksp->work[4];
46: P = ksp->work[5];
47: U = ksp->work[6];
48: D = ksp->work[7];
49: T1 = ksp->work[8];
50: AUQ = V;
52: /* Compute initial preconditioned residual */
53: KSPInitialResidual(ksp,X,V,T,R,B);
55: /* Test for nothing to do */
56: VecNorm(R,NORM_2,&dp);
57: PetscObjectTakeAccess(ksp);
58: ksp->rnorm = dp;
59: ksp->its = 0;
60: PetscObjectGrantAccess(ksp);
61: (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
62: if (ksp->reason) {*its = 0; return(0);}
63: KSPMonitor(ksp,0,dp);
65: /* Make the initial Rp == R */
66: VecCopy(R,RP);
68: /* Set the initial conditions */
69: etaold = 0.0;
70: psiold = 0.0;
71: tau = dp;
72: dpold = dp;
74: VecDot(R,RP,&rhoold); /* rhoold = (r,rp) */
75: VecCopy(R,U);
76: VecCopy(R,P);
77: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,T);
78: VecSet(&zero,D);
80: for (i=0; i<maxit; i++) {
81: PetscObjectTakeAccess(ksp);
82: ksp->its++;
83: PetscObjectGrantAccess(ksp);
84: VecDot(V,RP,&s); /* s <- (v,rp) */
85: a = rhoold / s; /* a <- rho / s */
86: tmp = -a; VecWAXPY(&tmp,V,U,Q); /* q <- u - a v */
87: VecWAXPY(&one,U,Q,T); /* t <- u + q */
88: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,T,AUQ,T1);
89: VecAXPY(&tmp,AUQ,R); /* r <- r - a K (u + q) */
90: VecNorm(R,NORM_2,&dp);
91: for (m=0; m<2; m++) {
92: if (!m) {
93: w = sqrt(dp*dpold);
94: } else {
95: w = dp;
96: }
97: psi = w / tau;
98: cm = 1.0 / sqrt(1.0 + psi * psi);
99: tau = tau * psi * cm;
100: eta = cm * cm * a;
101: cf = psiold * psiold * etaold / a;
102: if (!m) {
103: VecAYPX(&cf,U,D);
104: } else {
105: VecAYPX(&cf,Q,D);
106: }
107: VecAXPY(&eta,D,X);
109: dpest = sqrt(m + 1.0) * tau;
110: PetscObjectTakeAccess(ksp);
111: ksp->rnorm = dpest;
112: PetscObjectGrantAccess(ksp);
113: KSPLogResidualHistory(ksp,dpest);
114: KSPMonitor(ksp,i+1,dpest);
115: (*ksp->converged)(ksp,i+1,dpest,&ksp->reason,ksp->cnvP);
116: if (ksp->reason) break;
118: etaold = eta;
119: psiold = psi;
120: }
121: if (ksp->reason) break;
123: VecDot(R,RP,&rho); /* rho <- (r,rp) */
124: b = rho / rhoold; /* b <- rho / rhoold */
125: VecWAXPY(&b,Q,R,U); /* u <- r + b q */
126: VecAXPY(&b,P,Q);
127: VecWAXPY(&b,Q,U,P); /* p <- u + b(q + b p) */
128: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,Q); /* v <- K p */
130: rhoold = rho;
131: dpold = dp;
132: }
133: if (i == maxit) {
134: ksp->reason = KSP_DIVERGED_ITS;
135: }
137: KSPUnwindPreconditioner(ksp,X,T);
138: *its = ksp->its;
139: return(0);
140: }
142: EXTERN_C_BEGIN
145: int KSPCreate_TFQMR(KSP ksp)
146: {
148: ksp->data = (void*)0;
149: ksp->pc_side = PC_LEFT;
150: ksp->ops->setup = KSPSetUp_TFQMR;
151: ksp->ops->solve = KSPSolve_TFQMR;
152: ksp->ops->destroy = KSPDefaultDestroy;
153: ksp->ops->buildsolution = KSPDefaultBuildSolution;
154: ksp->ops->buildresidual = KSPDefaultBuildResidual;
155: ksp->ops->setfromoptions = 0;
156: ksp->ops->view = 0;
157: return(0);
158: }
159: EXTERN_C_END