Date: 2022-03-04T17:39:05+00:00

Actual source code: lcd.c

      2: #include <../src/ksp/ksp/impls/lcd/lcdimpl.h>

      4: PetscErrorCode KSPSetUp_LCD(KSP ksp)
      5: {
      6:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;
      8:   PetscInt       restart = lcd->restart;

     11:   /* get work vectors needed by LCD */
     12:   KSPSetWorkVecs(ksp,2);

     14:   VecDuplicateVecs(ksp->work[0],restart+1,&lcd->P);
     15:   VecDuplicateVecs(ksp->work[0], restart + 1, &lcd->Q);
     16:   PetscLogObjectMemory((PetscObject)ksp,2*(restart+2)*sizeof(Vec));
     17:   return(0);
     18: }

     20: /*     KSPSolve_LCD - This routine actually applies the left conjugate
     21:     direction method

     23:    Input Parameter:
     24: .     ksp - the Krylov space object that was set to use LCD, by, for
     25:             example, KSPCreate(MPI_Comm,KSP *ksp); KSPSetType(ksp,KSPLCD);

     27:    Output Parameter:
     28: .     its - number of iterations used

     30: */
     31: PetscErrorCode  KSPSolve_LCD(KSP ksp)
     32: {
     34:   PetscInt       it,j,max_k;
     35:   PetscScalar    alfa, beta, num, den, mone;
     36:   PetscReal      rnorm = 0.0;
     37:   Vec            X,B,R,Z;
     38:   KSP_LCD        *lcd;
     39:   Mat            Amat,Pmat;
     40:   PetscBool      diagonalscale;

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

     46:   lcd   = (KSP_LCD*)ksp->data;
     47:   X     = ksp->vec_sol;
     48:   B     = ksp->vec_rhs;
     49:   R     = ksp->work[0];
     50:   Z     = ksp->work[1];
     51:   max_k = lcd->restart;
     52:   mone  = -1;

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

     56:   ksp->its = 0;
     57:   if (!ksp->guess_zero) {
     58:     KSP_MatMult(ksp,Amat,X,Z);             /*   z <- b - Ax       */
     59:     VecAYPX(Z,mone,B);
     60:   } else {
     61:     VecCopy(B,Z);                         /*     z <- b (x is 0) */
     62:   }

     64:   KSP_PCApply(ksp,Z,R);                   /*     r <- M^-1z         */
     65:   if (ksp->normtype != KSP_NORM_NONE) {
     66:     VecNorm(R,NORM_2,&rnorm);
     67:     KSPCheckNorm(ksp,rnorm);
     68:   }
     69:   KSPLogResidualHistory(ksp,rnorm);
     70:   KSPMonitor(ksp,0,rnorm);
     71:   ksp->rnorm = rnorm;

     73:   /* test for convergence */
     74:   (*ksp->converged)(ksp,0,rnorm,&ksp->reason,ksp->cnvP);
     75:   if (ksp->reason) return(0);

     77:   VecCopy(R,lcd->P[0]);

     79:   while (!ksp->reason && ksp->its < ksp->max_it) {
     80:     it   = 0;
     81:     KSP_MatMult(ksp,Amat,lcd->P[it],Z);
     82:     KSP_PCApply(ksp,Z,lcd->Q[it]);

     84:     while (!ksp->reason && it < max_k && ksp->its < ksp->max_it) {
     85:       ksp->its++;
     86:       VecDot(lcd->P[it],R,&num);
     87:       VecDot(lcd->P[it],lcd->Q[it], &den);
     88:       KSPCheckDot(ksp,den);
     89:       alfa = num/den;
     90:       VecAXPY(X,alfa,lcd->P[it]);
     91:       VecAXPY(R,-alfa,lcd->Q[it]);
     92:       if (ksp->normtype != KSP_NORM_NONE) {
     93:         VecNorm(R,NORM_2,&rnorm);
     94:         KSPCheckNorm(ksp,rnorm);
     95:       }

     97:       ksp->rnorm = rnorm;
     98:       KSPLogResidualHistory(ksp,rnorm);
     99:       KSPMonitor(ksp,ksp->its,rnorm);
    100:       (*ksp->converged)(ksp,ksp->its,rnorm,&ksp->reason,ksp->cnvP);

    102:       if (ksp->reason) break;

    104:       VecCopy(R,lcd->P[it+1]);
    105:       KSP_MatMult(ksp,Amat,lcd->P[it+1],Z);
    106:       KSP_PCApply(ksp,Z,lcd->Q[it+1]);

    108:       for (j = 0; j <= it; j++) {
    109:         VecDot(lcd->P[j],lcd->Q[it+1],&num);
    110:         KSPCheckDot(ksp,num);
    111:         VecDot(lcd->P[j],lcd->Q[j],&den);
    112:         beta = -num/den;
    113:         VecAXPY(lcd->P[it+1],beta,lcd->P[j]);
    114:         VecAXPY(lcd->Q[it+1],beta,lcd->Q[j]);
    115:       }
    116:       it++;
    117:     }
    118:     VecCopy(lcd->P[it],lcd->P[0]);
    119:   }
    120:   if (ksp->its >= ksp->max_it && !ksp->reason) ksp->reason = KSP_DIVERGED_ITS;
    121:   VecCopy(X,ksp->vec_sol);
    122:   return(0);
    123: }
    124: /*
    125:        KSPDestroy_LCD - Frees all memory space used by the Krylov method

    127: */
    128: PetscErrorCode KSPReset_LCD(KSP ksp)
    129: {
    130:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;

    134:   if (lcd->P) { VecDestroyVecs(lcd->restart+1,&lcd->P);}
    135:   if (lcd->Q) { VecDestroyVecs(lcd->restart+1,&lcd->Q);}
    136:   return(0);
    137: }

    139: PetscErrorCode KSPDestroy_LCD(KSP ksp)
    140: {

    144:   KSPReset_LCD(ksp);
    145:   PetscFree(ksp->data);
    146:   return(0);
    147: }

    149: /*
    150:      KSPView_LCD - Prints information about the current Krylov method being used

    152:       Currently this only prints information to a file (or stdout) about the
    153:       symmetry of the problem. If your Krylov method has special options or
    154:       flags that information should be printed here.

    156: */
    157: PetscErrorCode KSPView_LCD(KSP ksp,PetscViewer viewer)
    158: {

    160:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;
    162:   PetscBool      iascii;

    165:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    166:   if (iascii) {
    167:     PetscViewerASCIIPrintf(viewer,"  restart=%d\n",lcd->restart);
    168:     PetscViewerASCIIPrintf(viewer,"  happy breakdown tolerance %g\n",lcd->haptol);
    169:   }
    170:   return(0);
    171: }

    173: /*
    174:     KSPSetFromOptions_LCD - Checks the options database for options related to the
    175:                             LCD method.
    176: */
    177: PetscErrorCode KSPSetFromOptions_LCD(PetscOptionItems *PetscOptionsObject,KSP ksp)
    178: {
    180:   PetscBool      flg;
    181:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;

    184:   PetscOptionsHead(PetscOptionsObject,"KSP LCD options");
    185:   PetscOptionsInt("-ksp_lcd_restart","Number of vectors conjugate","KSPLCDSetRestart",lcd->restart,&lcd->restart,&flg);
    186:   if (flg && lcd->restart < 1) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Restart must be positive");
    187:   PetscOptionsReal("-ksp_lcd_haptol","Tolerance for exact convergence (happy ending)","KSPLCDSetHapTol",lcd->haptol,&lcd->haptol,&flg);
    188:   if (flg && lcd->haptol < 0.0) SETERRQ(PetscObjectComm((PetscObject)ksp),PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
    189:   return(0);
    190: }

    192: /*MC
    193:      KSPLCD -  Implements the LCD (left conjugate direction) method in PETSc.

    195:    Options Database Keys:
    196: +   -ksp_lcd_restart - number of vectors conjudate
    197: -   -ksp_lcd_haptol - tolerance for exact convergence (happing ending)

    199:    Level: beginner

    201:     Notes:
    202:     Support only for left preconditioning

    204:     References:
    205: +    1. - J.Y. Yuan, G.H.Golub, R.J. Plemmons, and W.A.G. Cecilio. Semiconjugate
    206:      direction methods for real positive definite system. BIT Numerical
    207:      Mathematics, 44(1),2004.
    208: .    2. - Y. Dai and J.Y. Yuan. Study on semiconjugate direction methods for
    209:      nonsymmetric systems. International Journal for Numerical Methods in
    210:      Engineering, 60, 2004.
    211: .    3. - L. Catabriga, A.L.G.A. Coutinho, and L.P.Franca. Evaluating the LCD
    212:      algorithm for solving linear systems of equations arising from implicit
    213:      SUPG formulation of compressible flows. International Journal for
    214:      Numerical Methods in Engineering, 60, 2004
    215: -    4. - L. Catabriga, A. M. P. Valli, B. Z. Melotti, L. M. Pessoa,
    216:      A. L. G. A. Coutinho, Performance of LCD iterative method in the finite
    217:      element and finite difference solution of convection diffusion
    218:      equations,  Communications in Numerical Methods in Engineering, (Early
    219:      View).

    221:   Contributed by: Lucia Catabriga <luciac@ices.utexas.edu>

    223: .seealso:  KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP,
    224:            KSPCGSetType(), KSPLCDSetRestart(), KSPLCDSetHapTol()

    226: M*/

    228: PETSC_EXTERN PetscErrorCode KSPCreate_LCD(KSP ksp)
    229: {
    231:   KSP_LCD        *lcd;

    234:   PetscNewLog(ksp,&lcd);
    235:   ksp->data    = (void*)lcd;
    236:   KSPSetSupportedNorm(ksp,KSP_NORM_NONE,PC_LEFT,1);
    237:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
    238:   lcd->restart = 30;
    239:   lcd->haptol  = 1.0e-30;

    241:   /*
    242:        Sets the functions that are associated with this data structure
    243:        (in C++ this is the same as defining virtual functions)
    244:   */
    245:   ksp->ops->setup          = KSPSetUp_LCD;
    246:   ksp->ops->solve          = KSPSolve_LCD;
    247:   ksp->ops->reset          = KSPReset_LCD;
    248:   ksp->ops->destroy        = KSPDestroy_LCD;
    249:   ksp->ops->view           = KSPView_LCD;
    250:   ksp->ops->setfromoptions = KSPSetFromOptions_LCD;
    251:   ksp->ops->buildsolution  = KSPBuildSolutionDefault;
    252:   ksp->ops->buildresidual  = KSPBuildResidualDefault;
    253:   return(0);
    254: }