Actual source code: cgs.c

      1: #define PETSCKSP_DLL

      3: /*

      5:     Note that for the complex numbers version, the VecDot() arguments
      6:     within the code MUST remain in the order given for correct computation
      7:     of inner products.
      8: */
     9:  #include src/ksp/ksp/kspimpl.h

     13: static PetscErrorCode KSPSetUp_CGS(KSP ksp)
     14: {

     18:   if (ksp->pc_side == PC_SYMMETRIC) SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPCGS");
     19:   KSPDefaultGetWork(ksp,7);
     20:   return(0);
     21: }

     25: static PetscErrorCode  KSPSolve_CGS(KSP ksp)
     26: {
     28:   PetscInt       i;
     29:   PetscScalar    rho,rhoold,a,s,b,tmp,one = 1.0;
     30:   Vec            X,B,V,P,R,RP,T,Q,U,AUQ;
     31:   PetscReal      dp = 0.0;
     32:   PetscTruth     diagonalscale;

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

     38:   X       = ksp->vec_sol;
     39:   B       = ksp->vec_rhs;
     40:   R       = ksp->work[0];
     41:   RP      = ksp->work[1];
     42:   V       = ksp->work[2];
     43:   T       = ksp->work[3];
     44:   Q       = ksp->work[4];
     45:   P       = ksp->work[5];
     46:   U       = ksp->work[6];
     47:   AUQ     = V;

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

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

     66:   /* Make the initial Rp == R */
     67:   VecCopy(R,RP);
     68:   /*  added for Fidap */
     69:   /* Penalize Startup - Isaac Hasbani Trick for CGS
     70:      Since most initial conditions result in a mostly 0 residual,
     71:      we change all the 0 values in the vector RP to the maximum.
     72:   */
     73:   if (ksp->normtype == KSP_NATURAL_NORM) {
     74:      PetscReal   vr0max;
     75:      PetscScalar *tmp_RP=0;
     76:      PetscInt    numnp=0, *max_pos=0;
     77:      VecMax(RP, max_pos, &vr0max);
     78:      VecGetArray(RP, &tmp_RP);
     79:      VecGetLocalSize(RP, &numnp);
     80:      for (i=0; i<numnp; i++) {
     81:        if (tmp_RP[i] == 0.0) tmp_RP[i] = vr0max;
     82:      }
     83:      VecRestoreArray(RP, &tmp_RP);
     84:   }
     85:   /*  end of addition for Fidap */

     87:   /* Set the initial conditions */
     88:   VecDot(R,RP,&rhoold);        /* rhoold = (r,rp)      */
     89:   VecCopy(R,U);
     90:   VecCopy(R,P);
     91:   KSP_PCApplyBAorAB(ksp,P,V,T);

     93:   i = 0;
     94:   do {

     96:     VecDot(V,RP,&s);           /* s <- (v,rp)          */
     97:     a = rhoold / s;                                  /* a <- rho / s         */
     98:     tmp = -a;
     99:     VecWAXPY(Q,tmp,V,U);      /* q <- u - a v         */
    100:     VecWAXPY(T,one,U,Q);      /* t <- u + q           */
    101:     VecAXPY(X,a,T);           /* x <- x + a (u + q)   */
    102:     KSP_PCApplyBAorAB(ksp,T,AUQ,U);
    103:     VecAXPY(R,tmp,AUQ);       /* r <- r - a K (u + q) */
    104:     VecDot(R,RP,&rho);         /* rho <- (r,rp)        */
    105:     if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
    106:       VecNorm(R,NORM_2,&dp);
    107:     } else if (ksp->normtype == KSP_NATURAL_NORM) {
    108:       dp = PetscAbsScalar(rho);
    109:     }

    111:     PetscObjectTakeAccess(ksp);
    112:     ksp->its++;
    113:     ksp->rnorm = dp;
    114:     PetscObjectGrantAccess(ksp);
    115:     KSPLogResidualHistory(ksp,dp);
    116:     KSPMonitor(ksp,i+1,dp);
    117:     (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
    118:     if (ksp->reason) break;

    120:     b    = rho / rhoold;                             /* b <- rho / rhoold    */
    121:     VecWAXPY(U,b,Q,R);         /* u <- r + b q         */
    122:     VecAXPY(Q,b,P);
    123:     VecWAXPY(P,b,Q,U);         /* p <- u + b(q + b p)  */
    124:     KSP_PCApplyBAorAB(ksp,P,V,Q);      /* v <- K p    */
    125:     rhoold = rho;
    126:     i++;
    127:   } while (i<ksp->max_it);
    128:   if (i >= ksp->max_it) {
    129:     ksp->reason = KSP_DIVERGED_ITS;
    130:   }

    132:   KSPUnwindPreconditioner(ksp,X,T);
    133:   return(0);
    134: }

    136: /*MC
    137:      KSPCGS - This code implements the CGS (Conjugate Gradient Squared) method.

    139:    Options Database Keys:
    140: .   see KSPSolve()

    142:    Level: beginner

    144:    Notes: Reference: Sonneveld, 1989.

    146: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBCGS
    147: M*/
    151: PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate_CGS(KSP ksp)
    152: {
    154:   ksp->data                      = (void*)0;
    155:   ksp->pc_side                   = PC_LEFT;
    156:   ksp->ops->setup                = KSPSetUp_CGS;
    157:   ksp->ops->solve                = KSPSolve_CGS;
    158:   ksp->ops->destroy              = KSPDefaultDestroy;
    159:   ksp->ops->buildsolution        = KSPDefaultBuildSolution;
    160:   ksp->ops->buildresidual        = KSPDefaultBuildResidual;
    161:   ksp->ops->setfromoptions       = 0;
    162:   ksp->ops->view                 = 0;
    163:   return(0);
    164: }