Actual source code: minres.c

  1: #define PETSCKSP_DLL

 3:  #include src/ksp/ksp/kspimpl.h

  5: typedef struct {
  6:   PetscReal haptol;
  7: } KSP_MINRES;

 11: PetscErrorCode KSPSetUp_MINRES(KSP ksp)
 12: {

 16:   if (ksp->pc_side == PC_RIGHT) {
 17:     SETERRQ(PETSC_ERR_SUP,"No right preconditioning for KSPMINRES");
 18:   } else if (ksp->pc_side == PC_SYMMETRIC) {
 19:     SETERRQ(PETSC_ERR_SUP,"No symmetric preconditioning for KSPMINRES");
 20:   }
 21:   KSPDefaultGetWork(ksp,9);
 22:   return(0);
 23: }


 28: PetscErrorCode  KSPSolve_MINRES(KSP ksp)
 29: {
 31:   PetscInt       i;
 32:   PetscScalar    alpha,malpha,beta,mbeta,ibeta,betaold,eta,c=1.0,ceta,cold=1.0,coold,s=0.0,sold=0.0,soold;
 33:   PetscScalar    rho0,rho1,irho1,rho2,mrho2,rho3,mrho3,mone = -1.0,zero = 0.0,dp = 0.0;
 34:   PetscReal      np;
 35:   Vec            X,B,R,Z,U,V,W,UOLD,VOLD,WOLD,WOOLD;
 36:   Mat            Amat,Pmat;
 37:   MatStructure   pflag;
 38:   KSP_MINRES     *minres = (KSP_MINRES*)ksp->data;
 39:   PetscTruth     diagonalscale;

 42:   PCDiagonalScale(ksp->pc,&diagonalscale);
 43:   if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",ksp->type_name);

 45:   X       = ksp->vec_sol;
 46:   B       = ksp->vec_rhs;
 47:   R       = ksp->work[0];
 48:   Z       = ksp->work[1];
 49:   U       = ksp->work[2];
 50:   V       = ksp->work[3];
 51:   W       = ksp->work[4];
 52:   UOLD    = ksp->work[5];
 53:   VOLD    = ksp->work[6];
 54:   WOLD    = ksp->work[7];
 55:   WOOLD   = ksp->work[8];

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

 59:   ksp->its = 0;

 61:   VecSet(UOLD,zero);          /*     u_old  <-   0   */
 62:   VecCopy(UOLD,VOLD);         /*     v_old  <-   0   */
 63:   VecCopy(UOLD,W);            /*     w      <-   0   */
 64:   VecCopy(UOLD,WOLD);         /*     w_old  <-   0   */

 66:   if (!ksp->guess_zero) {
 67:     KSP_MatMult(ksp,Amat,X,R); /*     r <- b - A*x    */
 68:     VecAYPX(R,mone,B);
 69:   } else {
 70:     VecCopy(B,R);              /*     r <- b (x is 0) */
 71:   }

 73:   KSP_PCApply(ksp,R,Z); /*     z  <- B*r       */

 75:   VecDot(R,Z,&dp);
 76:   if (PetscAbsScalar(dp) < minres->haptol) {
 77:     PetscLogInfo((ksp,"KSPSolve_MINRES:Detected happy breakdown %g tolerance %g\n",PetscAbsScalar(dp),minres->haptol));
 78:     dp = PetscAbsScalar(dp); /* tiny number, can't use 0.0, cause divided by below */
 79:     if (dp == 0.0) {
 80:       ksp->reason = KSP_CONVERGED_ATOL;
 81:       return(0);
 82:     }
 83:   }

 85: #if !defined(PETSC_USE_COMPLEX)
 86:   if (dp < 0.0) {
 87:     ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
 88:     return(0);
 89:   }
 90: #endif
 91:   dp   = PetscSqrtScalar(dp);
 92:   beta = dp;                                        /*  beta <- sqrt(r'*z  */
 93:   eta  = beta;

 95:   VecCopy(R,V);
 96:   VecCopy(Z,U);
 97:   ibeta = 1.0 / beta;
 98:   VecScale(V,ibeta);         /*    v <- r / beta     */
 99:   VecScale(U,ibeta);         /*    u <- z / beta     */

101:   VecNorm(Z,NORM_2,&np);      /*   np <- ||z||        */

103:   KSPLogResidualHistory(ksp,np);
104:   KSPMonitor(ksp,0,np);            /* call any registered monitor routines */
105:   ksp->rnorm = np;
106:   (*ksp->converged)(ksp,0,np,&ksp->reason,ksp->cnvP);  /* test for convergence */
107:   if (ksp->reason) return(0);

109:   i = 0;
110:   do {
111:      ksp->its = i+1;

113: /*   Lanczos  */

115:      KSP_MatMult(ksp,Amat,U,R);   /*      r <- A*u   */
116:      VecDot(U,R,&alpha);          /*  alpha <- r'*u  */
117:      KSP_PCApply(ksp,R,Z); /*      z <- B*r   */

119:      malpha = - alpha;
120:      VecAXPY(R,malpha,V);     /*  r <- r - alpha v     */
121:      VecAXPY(Z,malpha,U);     /*  z <- z - alpha u     */
122:      mbeta = - beta;
123:      VecAXPY(R,mbeta,VOLD);   /*  r <- r - beta v_old  */
124:      VecAXPY(Z,mbeta,UOLD);   /*  z <- z - beta u_old  */

126:      betaold = beta;

128:      VecDot(R,Z,&dp);
129:      if (PetscAbsScalar(dp) < minres->haptol) {
130:        PetscLogInfo((ksp,"KSPSolve_MINRES:Detected happy breakdown %g tolerance %g\n",PetscAbsScalar(dp),minres->haptol));
131:        dp = PetscAbsScalar(dp); /* tiny number, can we use 0.0? */
132:      }

134: #if !defined(PETSC_USE_COMPLEX)
135:      if (dp < 0.0) {
136:        ksp->reason = KSP_DIVERGED_INDEFINITE_PC;
137:        break;
138:      }

140: #endif
141:      beta = PetscSqrtScalar(dp);                               /*  beta <- sqrt(r'*z)   */

143: /*    QR factorisation    */

145:      coold = cold; cold = c; soold = sold; sold = s;

147:      rho0 = cold * alpha - coold * sold * betaold;
148:      rho1 = PetscSqrtScalar(rho0*rho0 + beta*beta);
149:      rho2 = sold * alpha + coold * cold * betaold;
150:      rho3 = soold * betaold;

152: /*     Givens rotation    */

154:      c = rho0 / rho1;
155:      s = beta / rho1;

157: /*    Update    */

159:      VecCopy(WOLD,WOOLD);     /*  w_oold <- w_old      */
160:      VecCopy(W,WOLD);         /*  w_old  <- w          */
161: 
162:      VecCopy(U,W);            /*  w      <- u          */
163:      mrho2 = - rho2;
164:      VecAXPY(W,mrho2,WOLD);  /*  w <- w - rho2 w_old  */
165:      mrho3 = - rho3;
166:      VecAXPY(W,mrho3,WOOLD); /*  w <- w - rho3 w_oold */
167:      irho1 = 1.0 / rho1;
168:      VecScale(W,irho1);      /*  w <- w / rho1        */

170:      ceta = c * eta;
171:      VecAXPY(X,ceta,W);      /*  x <- x + c eta w     */
172:      eta = - s * eta;

174:      VecCopy(V,VOLD);
175:      VecCopy(U,UOLD);
176:      VecCopy(R,V);
177:      VecCopy(Z,U);
178:      ibeta = 1.0 / beta;
179:      VecScale(V,ibeta);      /*  v <- r / beta       */
180:      VecScale(U,ibeta);      /*  u <- z / beta       */
181: 
182:      np = ksp->rnorm * PetscAbsScalar(s);

184:      ksp->rnorm = np;
185:      KSPLogResidualHistory(ksp,np);
186:      KSPMonitor(ksp,i+1,np);
187:      (*ksp->converged)(ksp,i+1,np,&ksp->reason,ksp->cnvP); /* test for convergence */
188:      if (ksp->reason) break;
189:      i++;
190:   } while (i<ksp->max_it);
191:   if (i >= ksp->max_it) {
192:     ksp->reason = KSP_DIVERGED_ITS;
193:   }
194:   return(0);
195: }

197: /*MC
198:      KSPMINRES - This code implements the MINRES (Minimum Residual) method. 

200:    Options Database Keys:
201: .   see KSPSolve()

203:    Level: beginner

205:    Contributed by: Robert Scheichl: maprs@maths.bath.ac.uk

207:    Notes: The operator and the preconditioner must be positive definite for this method
208:           Reference: Paige & Saunders, 1975.

210: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPCG
211: M*/
215: PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate_MINRES(KSP ksp)
216: {
217:   KSP_MINRES     *minres;


222:   ksp->pc_side   = PC_LEFT;
223:   PetscNew(KSP_MINRES,&minres);
224:   minres->haptol = 1.e-18;
225:   ksp->data      = (void*)minres;

227:   /*
228:        Sets the functions that are associated with this data structure 
229:        (in C++ this is the same as defining virtual functions)
230:   */
231:   ksp->ops->setup                = KSPSetUp_MINRES;
232:   ksp->ops->solve                = KSPSolve_MINRES;
233:   ksp->ops->destroy              = KSPDefaultDestroy;
234:   ksp->ops->setfromoptions       = 0;
235:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
236:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;
237:   return(0);
238: }