Actual source code: tfqmr.c

petsc-3.11.3 2019-06-26
Report Typos and Errors

  2:  #include <petsc/private/kspimpl.h>

  4: static PetscErrorCode KSPSetUp_TFQMR(KSP ksp)
  5: {

  9:   if (ksp->pc_side == PC_SYMMETRIC) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"no symmetric preconditioning for KSPTFQMR");
 10:   KSPSetWorkVecs(ksp,9);
 11:   return(0);
 12: }

 14: static PetscErrorCode  KSPSolve_TFQMR(KSP ksp)
 15: {
 17:   PetscInt       i,m;
 18:   PetscScalar    rho,rhoold,a,s,b,eta,etaold,psiold,cf;
 19:   PetscReal      dp,dpold,w,dpest,tau,psi,cm;
 20:   Vec            X,B,V,P,R,RP,T,T1,Q,U,D,AUQ;

 23:   X   = ksp->vec_sol;
 24:   B   = ksp->vec_rhs;
 25:   R   = ksp->work[0];
 26:   RP  = ksp->work[1];
 27:   V   = ksp->work[2];
 28:   T   = ksp->work[3];
 29:   Q   = ksp->work[4];
 30:   P   = ksp->work[5];
 31:   U   = ksp->work[6];
 32:   D   = ksp->work[7];
 33:   T1  = ksp->work[8];
 34:   AUQ = V;

 36:   /* Compute initial preconditioned residual */
 37:   KSPInitialResidual(ksp,X,V,T,R,B);

 39:   /* Test for nothing to do */
 40:   VecNorm(R,NORM_2,&dp);
 41:   KSPCheckNorm(ksp,dp);
 42:   PetscObjectSAWsTakeAccess((PetscObject)ksp);
 43:   ksp->rnorm = dp;
 44:   ksp->its   = 0;
 45:   PetscObjectSAWsGrantAccess((PetscObject)ksp);
 46:   KSPMonitor(ksp,0,dp);
 47:   (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
 48:   if (ksp->reason) return(0);

 50:   /* Make the initial Rp == R */
 51:   VecCopy(R,RP);

 53:   /* Set the initial conditions */
 54:   etaold = 0.0;
 55:   psiold = 0.0;
 56:   tau    = dp;
 57:   dpold  = dp;

 59:   VecDot(R,RP,&rhoold);       /* rhoold = (r,rp)     */
 60:   VecCopy(R,U);
 61:   VecCopy(R,P);
 62:   KSP_PCApplyBAorAB(ksp,P,V,T);
 63:   VecSet(D,0.0);

 65:   i=0;
 66:   do {
 67:     PetscObjectSAWsTakeAccess((PetscObject)ksp);
 68:     ksp->its++;
 69:     PetscObjectSAWsGrantAccess((PetscObject)ksp);
 70:     VecDot(V,RP,&s);          /* s <- (v,rp)          */
 71:     KSPCheckDot(ksp,s);
 72:     a    = rhoold / s;                              /* a <- rho / s         */
 73:     VecWAXPY(Q,-a,V,U);  /* q <- u - a v         */
 74:     VecWAXPY(T,1.0,U,Q);     /* t <- u + q           */
 75:     KSP_PCApplyBAorAB(ksp,T,AUQ,T1);
 76:     VecAXPY(R,-a,AUQ);      /* r <- r - a K (u + q) */
 77:     VecNorm(R,NORM_2,&dp);
 78:     KSPCheckNorm(ksp,dp);
 79:     for (m=0; m<2; m++) {
 80:       if (!m) w = PetscSqrtReal(dp*dpold);
 81:       else w = dp;
 82:       psi = w / tau;
 83:       cm  = 1.0 / PetscSqrtReal(1.0 + psi * psi);
 84:       tau = tau * psi * cm;
 85:       eta = cm * cm * a;
 86:       cf  = psiold * psiold * etaold / a;
 87:       if (!m) {
 88:         VecAYPX(D,cf,U);
 89:       } else {
 90:         VecAYPX(D,cf,Q);
 91:       }
 92:       VecAXPY(X,eta,D);

 94:       dpest      = PetscSqrtReal(m + 1.0) * tau;
 95:       PetscObjectSAWsTakeAccess((PetscObject)ksp);
 96:       ksp->rnorm = dpest;
 97:       PetscObjectSAWsGrantAccess((PetscObject)ksp);
 98:       KSPLogResidualHistory(ksp,dpest);
 99:       KSPMonitor(ksp,i+1,dpest);
100:       (*ksp->converged)(ksp,i+1,dpest,&ksp->reason,ksp->cnvP);
101:       if (ksp->reason) break;

103:       etaold = eta;
104:       psiold = psi;
105:     }
106:     if (ksp->reason) break;

108:     VecDot(R,RP,&rho);        /* rho <- (r,rp)       */
109:     b    = rho / rhoold;                            /* b <- rho / rhoold   */
110:     VecWAXPY(U,b,Q,R);       /* u <- r + b q        */
111:     VecAXPY(Q,b,P);
112:     VecWAXPY(P,b,Q,U);       /* p <- u + b(q + b p) */
113:     KSP_PCApplyBAorAB(ksp,P,V,Q); /* v <- K p  */

115:     rhoold = rho;
116:     dpold  = dp;

118:     i++;
119:   } while (i<ksp->max_it);
120:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;

122:   KSPUnwindPreconditioner(ksp,X,T);
123:   return(0);
124: }

126: /*MC
127:      KSPTFQMR - A transpose free QMR (quasi minimal residual),

129:    Options Database Keys:
130: .   see KSPSolve()

132:    Level: beginner

134:    Notes:
135:     Supports left and right preconditioning, but not symmetric

137:           The "residual norm" computed in this algorithm is actually just an upper bound on the actual residual norm.
138:           That is for left preconditioning it is a bound on the preconditioned residual and for right preconditioning
139:           it is a bound on the true residual.

141:    References:
142: .   1. -  Freund, 1993

144: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPTCQMR
145: M*/
146: PETSC_EXTERN PetscErrorCode KSPCreate_TFQMR(KSP ksp)
147: {

151:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
152:   KSPSetSupportedNorm(ksp,KSP_NORM_UNPRECONDITIONED,PC_RIGHT,2);

154:   ksp->data                = (void*)0;
155:   ksp->ops->setup          = KSPSetUp_TFQMR;
156:   ksp->ops->solve          = KSPSolve_TFQMR;
157:   ksp->ops->destroy        = KSPDestroyDefault;
158:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
159:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
160:   ksp->ops->setfromoptions = 0;
161:   ksp->ops->view           = 0;
162:   return(0);
163: }