Actual source code: lcd.c
petsc-3.13.6 2020-09-29
Report Typos and Errors

      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;
     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:   VecNorm(R,NORM_2,&rnorm);
     66:   KSPCheckNorm(ksp,rnorm);
     67:   KSPLogResidualHistory(ksp,rnorm);
     68:   KSPMonitor(ksp,0,rnorm);
     69:   ksp->rnorm = rnorm;

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

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

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

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

     93:       ksp->rnorm = rnorm;
     94:       KSPLogResidualHistory(ksp,rnorm);
     95:       KSPMonitor(ksp,ksp->its,rnorm);
     96:       (*ksp->converged)(ksp,ksp->its,rnorm,&ksp->reason,ksp->cnvP);

     98:       if (ksp->reason) break;

    100:       VecCopy(R,lcd->P[it+1]);
    101:       KSP_MatMult(ksp,Amat,lcd->P[it+1],Z);
    102:       KSP_PCApply(ksp,Z,lcd->Q[it+1]);

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

    123: */
    124: PetscErrorCode KSPReset_LCD(KSP ksp)
    125: {
    126:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;

    130:   if (lcd->P) { VecDestroyVecs(lcd->restart+1,&lcd->P);}
    131:   if (lcd->Q) { VecDestroyVecs(lcd->restart+1,&lcd->Q);}
    132:   return(0);
    133: }

    136: PetscErrorCode KSPDestroy_LCD(KSP ksp)
    137: {

    141:   KSPReset_LCD(ksp);
    142:   PetscFree(ksp->data);
    143:   return(0);
    144: }

    146: /*
    147:      KSPView_LCD - Prints information about the current Krylov method being used

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

    153: */
    154: PetscErrorCode KSPView_LCD(KSP ksp,PetscViewer viewer)
    155: {

    157:   KSP_LCD        *lcd = (KSP_LCD*)ksp->data;
    159:   PetscBool      iascii;

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

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

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

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

    192:    Options Database Keys:
    193: +   -ksp_lcd_restart - number of vectors conjudate
    194: -   -ksp_lcd_haptol - tolerance for exact convergence (happing ending)

    196:    Level: beginner

    198:     Notes:
    199:     Support only for left preconditioning

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

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

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

    224: M*/

    226: PETSC_EXTERN PetscErrorCode KSPCreate_LCD(KSP ksp)
    227: {
    229:   KSP_LCD        *lcd;

    232:   PetscNewLog(ksp,&lcd);
    233:   ksp->data    = (void*)lcd;
    234:   KSPSetSupportedNorm(ksp,KSP_NORM_PRECONDITIONED,PC_LEFT,3);
    235:   lcd->restart = 30;
    236:   lcd->haptol  = 1.0e-30;

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