Actual source code: symmlq.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

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

  4: typedef struct {
  5:   PetscReal haptol;
  6: } KSP_SYMMLQ;

  8: PetscErrorCode KSPSetUp_SYMMLQ(KSP ksp)
  9: {

 13:   KSPSetWorkVecs(ksp,9);
 14:   return(0);
 15: }

 17: PetscErrorCode  KSPSolve_SYMMLQ(KSP ksp)
 18: {
 20:   PetscInt       i;
 21:   PetscScalar    alpha,beta,ibeta,betaold,beta1,ceta = 0,ceta_oold = 0.0, ceta_old = 0.0,ceta_bar;
 22:   PetscScalar    c  = 1.0,cold=1.0,s=0.0,sold=0.0,coold,soold,rho0,rho1,rho2,rho3;
 23:   PetscScalar    dp = 0.0;
 24:   PetscReal      np,s_prod;
 25:   Vec            X,B,R,Z,U,V,W,UOLD,VOLD,Wbar;
 26:   Mat            Amat,Pmat;
 27:   KSP_SYMMLQ     *symmlq = (KSP_SYMMLQ*)ksp->data;
 28:   PetscBool      diagonalscale;

 31:   PCGetDiagonalScale(ksp->pc,&diagonalscale);
 32:   if (diagonalscale) SETERRQ1(PetscObjectComm((PetscObject)ksp),PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",((PetscObject)ksp)->type_name);

 34:   X    = ksp->vec_sol;
 35:   B    = ksp->vec_rhs;
 36:   R    = ksp->work[0];
 37:   Z    = ksp->work[1];
 38:   U    = ksp->work[2];
 39:   V    = ksp->work[3];
 40:   W    = ksp->work[4];
 41:   UOLD = ksp->work[5];
 42:   VOLD = ksp->work[6];
 43:   Wbar = ksp->work[7];

 45:   PCGetOperators(ksp->pc,&Amat,&Pmat);

 47:   ksp->its = 0;

 49:   VecSet(UOLD,0.0);           /* u_old <- zeros;  */
 50:   VecCopy(UOLD,VOLD);          /* v_old <- u_old;  */
 51:   VecCopy(UOLD,W);             /* w     <- u_old;  */
 52:   VecCopy(UOLD,Wbar);          /* w_bar <- u_old;  */
 53:   if (!ksp->guess_zero) {
 54:     KSP_MatMult(ksp,Amat,X,R); /*     r <- b - A*x */
 55:     VecAYPX(R,-1.0,B);
 56:   } else {
 57:     VecCopy(B,R);              /*     r <- b (x is 0) */
 58:   }

 60:   KSP_PCApply(ksp,R,Z); /* z  <- B*r       */
 61:   VecDot(R,Z,&dp);             /* dp = r'*z;      */
 62:   if (PetscAbsScalar(dp) < symmlq->haptol) {
 63:     PetscInfo2(ksp,"Detected happy breakdown %g tolerance %g\n",(double)PetscAbsScalar(dp),(double)symmlq->haptol);
 64:     ksp->rnorm  = 0.0;  /* what should we really put here? */
 65:     ksp->reason = KSP_CONVERGED_HAPPY_BREAKDOWN;  /* bugfix proposed by Lourens (lourens.vanzanen@shell.com) */
 66:     return(0);
 67:   }

 69: #if !defined(PETSC_USE_COMPLEX)
 70:   if (dp < 0.0) {
 71:     ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
 72:     return(0);
 73:   }
 74: #endif
 75:   dp     = PetscSqrtScalar(dp);
 76:   beta   = dp;                         /*  beta <- sqrt(r'*z)  */
 77:   beta1  = beta;
 78:   s_prod = PetscAbsScalar(beta1);

 80:   VecCopy(R,V); /* v <- r; */
 81:   VecCopy(Z,U); /* u <- z; */
 82:   ibeta = 1.0 / beta;
 83:   VecScale(V,ibeta);    /* v <- ibeta*v; */
 84:   VecScale(U,ibeta);    /* u <- ibeta*u; */
 85:   VecCopy(U,Wbar);       /* w_bar <- u;   */
 86:   VecNorm(Z,NORM_2,&np);     /*   np <- ||z||        */
 87:   KSPLogResidualHistory(ksp,np);
 88:   KSPMonitor(ksp,0,np);
 89:   ksp->rnorm = np;
 90:   (*ksp->converged)(ksp,0,np,&ksp->reason,ksp->cnvP); /* test for convergence */
 91:   if (ksp->reason) return(0);

 93:   i = 0; ceta = 0.;
 94:   do {
 95:     ksp->its = i+1;

 97:     /*    Update    */
 98:     if (ksp->its > 1) {
 99:       VecCopy(V,VOLD);  /* v_old <- v; */
100:       VecCopy(U,UOLD);  /* u_old <- u; */

102:       VecCopy(R,V);
103:       VecScale(V,1.0/beta); /* v <- ibeta*r; */
104:       VecCopy(Z,U);
105:       VecScale(U,1.0/beta); /* u <- ibeta*z; */

107:       VecCopy(Wbar,W);
108:       VecScale(W,c);
109:       VecAXPY(W,s,U);   /* w  <- c*w_bar + s*u;    (w_k) */
110:       VecScale(Wbar,-s);
111:       VecAXPY(Wbar,c,U); /* w_bar <- -s*w_bar + c*u; (w_bar_(k+1)) */
112:       VecAXPY(X,ceta,W); /* x <- x + ceta * w;       (xL_k)  */

114:       ceta_oold = ceta_old;
115:       ceta_old  = ceta;
116:     }

118:     /*   Lanczos  */
119:     KSP_MatMult(ksp,Amat,U,R);   /*  r     <- Amat*u; */
120:     VecDot(U,R,&alpha);          /*  alpha <- u'*r;   */
121:     KSP_PCApply(ksp,R,Z); /*      z <- B*r;    */

123:     VecAXPY(R,-alpha,V);   /*  r <- r - alpha* v;  */
124:     VecAXPY(Z,-alpha,U);   /*  z <- z - alpha* u;  */
125:     VecAXPY(R,-beta,VOLD); /*  r <- r - beta * v_old; */
126:     VecAXPY(Z,-beta,UOLD); /*  z <- z - beta * u_old; */
127:     betaold = beta;                                /* beta_k                  */
128:     VecDot(R,Z,&dp);       /* dp <- r'*z;             */
129:     if (PetscAbsScalar(dp) < symmlq->haptol) {
130:       PetscInfo2(ksp,"Detected happy breakdown %g tolerance %g\n",(double)PetscAbsScalar(dp),(double)symmlq->haptol);
131:       dp   = 0.0;
132:     }

134: #if !defined(PETSC_USE_COMPLEX)
135:     if (dp < 0.0) {
136:       ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
137:       break;
138:     }
139: #endif
140:     beta = PetscSqrtScalar(dp);                    /*  beta = sqrt(dp); */

142:     /*    QR factorization    */
143:     coold = cold; cold = c; soold = sold; sold = s;
144:     rho0  = cold * alpha - coold * sold * betaold;   /* gamma_bar */
145:     rho1  = PetscSqrtScalar(rho0*rho0 + beta*beta);  /* gamma     */
146:     rho2  = sold * alpha + coold * cold * betaold;   /* delta     */
147:     rho3  = soold * betaold;                         /* epsilon   */

149:     /* Givens rotation: [c -s; s c] (different from the Reference!) */
150:     c = rho0 / rho1; s = beta / rho1;

152:     if (ksp->its==1) ceta = beta1/rho1;
153:     else ceta = -(rho2*ceta_old + rho3*ceta_oold)/rho1;

155:     s_prod = s_prod*PetscAbsScalar(s);
156:     if (c == 0.0) np = s_prod*1.e16;
157:     else np = s_prod/PetscAbsScalar(c);       /* residual norm for xc_k (CGNORM) */

159:     ksp->rnorm = np;
160:     KSPLogResidualHistory(ksp,np);
161:     KSPMonitor(ksp,i+1,np);
162:     (*ksp->converged)(ksp,i+1,np,&ksp->reason,ksp->cnvP); /* test for convergence */
163:     if (ksp->reason) break;
164:     i++;
165:   } while (i<ksp->max_it);

167:   /* move to the CG point: xc_(k+1) */
168:   if (c == 0.0) ceta_bar = ceta*1.e15;
169:   else ceta_bar = ceta/c;

171:   VecAXPY(X,ceta_bar,Wbar); /* x <- x + ceta_bar*w_bar */

173:   if (i >= ksp->max_it) ksp->reason = KSP_DIVERGED_ITS;
174:   return(0);
175: }

177: /*MC
178:      KSPSYMMLQ -  This code implements the SYMMLQ method.

180:    Options Database Keys:
181: .   see KSPSolve()

183:    Level: beginner

185:    Notes:
186:     The operator and the preconditioner must be symmetric for this method. The
187:           preconditioner must be POSITIVE-DEFINITE.

189:           Supports only left preconditioning.

191:    Reference: Paige & Saunders, 1975.

193: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP
194: M*/
195: PETSC_EXTERN PetscErrorCode KSPCreate_SYMMLQ(KSP ksp)
196: {
197:   KSP_SYMMLQ     *symmlq;

201:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);

203:   PetscNewLog(ksp,&symmlq);
204:   symmlq->haptol = 1.e-18;
205:   ksp->data      = (void*)symmlq;

207:   /*
208:        Sets the functions that are associated with this data structure
209:        (in C++ this is the same as defining virtual functions)
210:   */
211:   ksp->ops->setup          = KSPSetUp_SYMMLQ;
212:   ksp->ops->solve          = KSPSolve_SYMMLQ;
213:   ksp->ops->destroy        = KSPDestroyDefault;
214:   ksp->ops->setfromoptions = 0;
215:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
216:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
217:   return(0);
218: }