Actual source code: tfqmr.c

  1: /*$Id: tfqmr.c,v 1.60 2001/03/23 23:23:44 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

 14: static int KSPSetUp_TFQMR(KSP ksp)
 15: {

 19:   if (ksp->pc_side == PC_SYMMETRIC){
 20:     SETERRQ(2,"no symmetric preconditioning for KSPTFQMR");
 21:   }
 22:   KSPDefaultGetWork(ksp,10);
 23:   return(0);
 24: }

 26: static int  KSPSolve_TFQMR(KSP ksp,int *its)
 27: {
 28:   int       i,maxit,m, ierr;
 29:   Scalar    rho,rhoold,a,s,b,eta,etaold,psiold,cf,tmp,one = 1.0,zero = 0.0;
 30:   PetscReal dp,dpold,w,dpest,tau,psi,cm;
 31:   Vec       X,B,V,P,R,RP,T,T1,Q,U,D,BINVF,AUQ;

 34:   maxit    = ksp->max_it;
 35:   X        = ksp->vec_sol;
 36:   B        = ksp->vec_rhs;
 37:   R        = ksp->work[0];
 38:   RP       = ksp->work[1];
 39:   V        = ksp->work[2];
 40:   T        = ksp->work[3];
 41:   Q        = ksp->work[4];
 42:   P        = ksp->work[5];
 43:   BINVF    = ksp->work[6];
 44:   U        = ksp->work[7];
 45:   D        = ksp->work[8];
 46:   T1       = ksp->work[9];
 47:   AUQ      = V;

 49:   /* Compute initial preconditioned residual */
 50:   KSPInitialResidual(ksp,X,V,T,R,BINVF,B);

 52:   /* Test for nothing to do */
 53:   VecNorm(R,NORM_2,&dp);
 54:   PetscObjectTakeAccess(ksp);
 55:   ksp->rnorm  = dp;
 56:   ksp->its    = 0;
 57:   PetscObjectGrantAccess(ksp);
 58:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 59:   if (ksp->reason) {*its = 0; return(0);}
 60:   KSPMonitor(ksp,0,dp);

 62:   /* Make the initial Rp == R */
 63:   VecCopy(R,RP);

 65:   /* Set the initial conditions */
 66:   etaold = 0.0;
 67:   psiold = 0.0;
 68:   tau    = dp;
 69:   dpold  = dp;

 71:   VecDot(R,RP,&rhoold);       /* rhoold = (r,rp)     */
 72:   VecCopy(R,U);
 73:   VecCopy(R,P);
 74:   KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,T);
 75:   VecSet(&zero,D);

 77:   for (i=0; i<maxit; i++) {
 78:     PetscObjectTakeAccess(ksp);
 79:     ksp->its++;
 80:     PetscObjectGrantAccess(ksp);
 81:     VecDot(V,RP,&s);          /* s <- (v,rp)          */
 82:     a = rhoold / s;                                 /* a <- rho / s         */
 83:     tmp = -a; VecWAXPY(&tmp,V,U,Q);  /* q <- u - a v         */
 84:     VecWAXPY(&one,U,Q,T);     /* t <- u + q           */
 85:     KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,T,AUQ,T1);
 86:     VecAXPY(&tmp,AUQ,R);      /* r <- r - a K (u + q) */
 87:     VecNorm(R,NORM_2,&dp);
 88:     for (m=0; m<2; m++) {
 89:       if (!m) {
 90:         w = sqrt(dp*dpold);
 91:       } else {
 92:         w = dp;
 93:       }
 94:       psi = w / tau;
 95:       cm  = 1.0 / sqrt(1.0 + psi * psi);
 96:       tau = tau * psi * cm;
 97:       eta = cm * cm * a;
 98:       cf  = psiold * psiold * etaold / a;
 99:       if (!m) {
100:         VecAYPX(&cf,U,D);
101:       } else {
102:         VecAYPX(&cf,Q,D);
103:       }
104:       VecAXPY(&eta,D,X);

106:       dpest = sqrt(m + 1.0) * tau;
107:       PetscObjectTakeAccess(ksp);
108:       ksp->rnorm                                    = dpest;
109:       PetscObjectGrantAccess(ksp);
110:       KSPLogResidualHistory(ksp,dpest);
111:       KSPMonitor(ksp,i+1,dpest);
112:       (*ksp->converged)(ksp,i+1,dpest,&ksp->reason,ksp->cnvP);
113:       if (ksp->reason) break;

115:       etaold = eta;
116:       psiold = psi;
117:     }
118:     if (ksp->reason) break;

120:     VecDot(R,RP,&rho);        /* rho <- (r,rp)       */
121:     b = rho / rhoold;                               /* b <- rho / rhoold   */
122:     VecWAXPY(&b,Q,R,U);       /* u <- r + b q        */
123:     VecAXPY(&b,P,Q);
124:     VecWAXPY(&b,Q,U,P);       /* p <- u + b(q + b p) */
125:     KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,Q); /* v <- K p  */

127:     rhoold = rho;
128:     dpold  = dp;
129:   }
130:   if (i == maxit) {
131:     i--;
132:     ksp->reason = KSP_DIVERGED_ITS;
133:   }

135:   KSPUnwindPreconditioner(ksp,X,T);
136:   *its = i + 1;
137:   return(0);
138: }

140: EXTERN_C_BEGIN
141: int KSPCreate_TFQMR(KSP ksp)
142: {
144:   ksp->data                      = (void*)0;
145:   ksp->pc_side                   = PC_LEFT;
146:   ksp->calc_res                  = PETSC_TRUE;
147:   ksp->ops->setup                = KSPSetUp_TFQMR;
148:   ksp->ops->solve                = KSPSolve_TFQMR;
149:   ksp->ops->destroy              = KSPDefaultDestroy;
150:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
151:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;
152:   ksp->ops->setfromoptions       = 0;
153:   ksp->ops->view                 = 0;
154:   return(0);
155: }
156: EXTERN_C_END