Actual source code: snesngmres.c

      1: #include <../src/snes/impls/ngmres/snesngmres.h>
      2: #include <petscblaslapack.h>
      3: #include <petscdm.h>

      5: const char *const SNESNGMRESRestartTypes[] = {"NONE","PERIODIC","DIFFERENCE","SNESNGMRESRestartType","SNES_NGMRES_RESTART_",NULL};
      6: const char *const SNESNGMRESSelectTypes[] = {"NONE","DIFFERENCE","LINESEARCH","SNESNGMRESSelectType","SNES_NGMRES_SELECT_",NULL};

      8: PetscErrorCode SNESReset_NGMRES(SNES snes)
      9: {
     10:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;

     14:   VecDestroyVecs(ngmres->msize,&ngmres->Fdot);
     15:   VecDestroyVecs(ngmres->msize,&ngmres->Xdot);
     16:   SNESLineSearchDestroy(&ngmres->additive_linesearch);
     17:   return(0);
     18: }

     20: PetscErrorCode SNESDestroy_NGMRES(SNES snes)
     21: {
     23:   SNES_NGMRES    *ngmres = (SNES_NGMRES*)snes->data;

     26:   SNESReset_NGMRES(snes);
     27:   PetscFree4(ngmres->h,ngmres->beta,ngmres->xi,ngmres->q);
     28:   PetscFree3(ngmres->xnorms,ngmres->fnorms,ngmres->s);
     29: #if defined(PETSC_USE_COMPLEX)
     30:   PetscFree(ngmres->rwork);
     31: #endif
     32:   PetscFree(ngmres->work);
     33:   PetscFree(snes->data);
     34:   return(0);
     35: }

     37: PetscErrorCode SNESSetUp_NGMRES(SNES snes)
     38: {
     39:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
     40:   const char     *optionsprefix;
     41:   PetscInt       msize,hsize;
     43:   DM             dm;

     46:   if (snes->npc && snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
     47:     SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESNGMRES does not support left preconditioning with unpreconditioned function");
     48:   }
     49:   if (snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_PRECONDITIONED;
     50:   SNESSetWorkVecs(snes,5);

     52:   if (!snes->vec_sol) {
     53:     SNESGetDM(snes,&dm);
     54:     DMCreateGlobalVector(dm,&snes->vec_sol);
     55:   }

     57:   if (!ngmres->Xdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Xdot);}
     58:   if (!ngmres->Fdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Fdot);}
     59:   if (!ngmres->setup_called) {
     60:     msize = ngmres->msize;          /* restart size */
     61:     hsize = msize * msize;

     63:     /* explicit least squares minimization solve */
     64:     PetscCalloc4(hsize,&ngmres->h, msize,&ngmres->beta, msize,&ngmres->xi, hsize,&ngmres->q);
     65:     PetscMalloc3(msize,&ngmres->xnorms,msize,&ngmres->fnorms,msize,&ngmres->s);
     66:     ngmres->nrhs  = 1;
     67:     ngmres->lda   = msize;
     68:     ngmres->ldb   = msize;
     69:     ngmres->lwork = 12*msize;
     70: #if defined(PETSC_USE_COMPLEX)
     71:     PetscMalloc1(ngmres->lwork,&ngmres->rwork);
     72: #endif
     73:     PetscMalloc1(ngmres->lwork,&ngmres->work);
     74:   }

     76:   /* linesearch setup */
     77:   SNESGetOptionsPrefix(snes,&optionsprefix);

     79:   if (ngmres->select_type == SNES_NGMRES_SELECT_LINESEARCH) {
     80:     SNESLineSearchCreate(PetscObjectComm((PetscObject)snes),&ngmres->additive_linesearch);
     81:     SNESLineSearchSetSNES(ngmres->additive_linesearch,snes);
     82:     if (!((PetscObject)ngmres->additive_linesearch)->type_name) {
     83:       SNESLineSearchSetType(ngmres->additive_linesearch,SNESLINESEARCHL2);
     84:     }
     85:     SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,"additive_");
     86:     SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,optionsprefix);
     87:     SNESLineSearchSetFromOptions(ngmres->additive_linesearch);
     88:   }

     90:   ngmres->setup_called = PETSC_TRUE;
     91:   return(0);
     92: }

     94: PetscErrorCode SNESSetFromOptions_NGMRES(PetscOptionItems *PetscOptionsObject,SNES snes)
     95: {
     96:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
     98:   PetscBool      debug = PETSC_FALSE;

    101:   PetscOptionsHead(PetscOptionsObject,"SNES NGMRES options");
    102:   PetscOptionsEnum("-snes_ngmres_select_type","Select type","SNESNGMRESSetSelectType",SNESNGMRESSelectTypes,
    103:                           (PetscEnum)ngmres->select_type,(PetscEnum*)&ngmres->select_type,NULL);
    104:   PetscOptionsEnum("-snes_ngmres_restart_type","Restart type","SNESNGMRESSetRestartType",SNESNGMRESRestartTypes,
    105:                           (PetscEnum)ngmres->restart_type,(PetscEnum*)&ngmres->restart_type,NULL);
    106:   PetscOptionsBool("-snes_ngmres_candidate", "Use candidate storage",              "SNES",ngmres->candidate,&ngmres->candidate,NULL);
    107:   PetscOptionsBool("-snes_ngmres_approxfunc","Linearly approximate the function", "SNES",ngmres->approxfunc,&ngmres->approxfunc,NULL);
    108:   PetscOptionsInt("-snes_ngmres_m",          "Number of directions",               "SNES",ngmres->msize,&ngmres->msize,NULL);
    109:   PetscOptionsInt("-snes_ngmres_restart",    "Iterations before forced restart",   "SNES",ngmres->restart_periodic,&ngmres->restart_periodic,NULL);
    110:   PetscOptionsInt("-snes_ngmres_restart_it", "Tolerance iterations before restart","SNES",ngmres->restart_it,&ngmres->restart_it,NULL);
    111:   PetscOptionsBool("-snes_ngmres_monitor",   "Monitor actions of NGMRES",          "SNES",ngmres->monitor ? PETSC_TRUE : PETSC_FALSE,&debug,NULL);
    112:   if (debug) {
    113:     ngmres->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
    114:   }
    115:   PetscOptionsReal("-snes_ngmres_gammaA",    "Residual selection constant",   "SNES",ngmres->gammaA,&ngmres->gammaA,NULL);
    116:   PetscOptionsReal("-snes_ngmres_gammaC",    "Residual restart constant",     "SNES",ngmres->gammaC,&ngmres->gammaC,NULL);
    117:   PetscOptionsReal("-snes_ngmres_epsilonB",  "Difference selection constant", "SNES",ngmres->epsilonB,&ngmres->epsilonB,NULL);
    118:   PetscOptionsReal("-snes_ngmres_deltaB",    "Difference residual selection constant", "SNES",ngmres->deltaB,&ngmres->deltaB,NULL);
    119:   PetscOptionsBool("-snes_ngmres_single_reduction", "Aggregate reductions",  "SNES",ngmres->singlereduction,&ngmres->singlereduction,NULL);
    120:   PetscOptionsBool("-snes_ngmres_restart_fm_rise", "Restart on F_M residual rise",  "SNESNGMRESSetRestartFmRise",ngmres->restart_fm_rise,&ngmres->restart_fm_rise,NULL);
    121:   PetscOptionsTail();
    122:   if ((ngmres->gammaA > ngmres->gammaC) && (ngmres->gammaC > 2.)) ngmres->gammaC = ngmres->gammaA;
    123:   return(0);
    124: }

    126: PetscErrorCode SNESView_NGMRES(SNES snes,PetscViewer viewer)
    127: {
    128:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
    129:   PetscBool      iascii;

    133:   PetscObjectTypeCompare((PetscObject) viewer,PETSCVIEWERASCII,&iascii);
    134:   if (iascii) {
    135:     PetscViewerASCIIPrintf(viewer,"  Number of stored past updates: %d\n", ngmres->msize);
    136:     PetscViewerASCIIPrintf(viewer,"  Residual selection: gammaA=%1.0e, gammaC=%1.0e\n",ngmres->gammaA,ngmres->gammaC);
    137:     PetscViewerASCIIPrintf(viewer,"  Difference restart: epsilonB=%1.0e, deltaB=%1.0e\n",ngmres->epsilonB,ngmres->deltaB);
    138:     PetscViewerASCIIPrintf(viewer,"  Restart on F_M residual increase: %s\n",ngmres->restart_fm_rise?"TRUE":"FALSE");
    139:   }
    140:   return(0);
    141: }

    143: PetscErrorCode SNESSolve_NGMRES(SNES snes)
    144: {
    145:   SNES_NGMRES          *ngmres = (SNES_NGMRES*) snes->data;
    146:   /* present solution, residual, and preconditioned residual */
    147:   Vec                  X,F,B,D,Y;

    149:   /* candidate linear combination answers */
    150:   Vec                  XA,FA,XM,FM;

    152:   /* coefficients and RHS to the minimization problem */
    153:   PetscReal            fnorm,fMnorm,fAnorm;
    154:   PetscReal            xnorm,xMnorm,xAnorm;
    155:   PetscReal            ynorm,yMnorm,yAnorm;
    156:   PetscInt             k,k_restart,l,ivec,restart_count = 0;

    158:   /* solution selection data */
    159:   PetscBool            selectRestart;
    160:   /*
    161:       These two variables are initialized to prevent compilers/analyzers from producing false warnings about these variables being passed
    162:       to SNESNGMRESSelect_Private() without being set when SNES_NGMRES_RESTART_DIFFERENCE, the values are not used in the subroutines in that case
    163:       so the code is correct as written.
    164:   */
    165:   PetscReal            dnorm = 0.0,dminnorm = 0.0;
    166:   PetscReal            fminnorm;

    168:   SNESConvergedReason  reason;
    169:   SNESLineSearchReason lssucceed;
    170:   PetscErrorCode       ierr;

    173:   if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

    175:   PetscCitationsRegister(SNESCitation,&SNEScite);
    176:   /* variable initialization */
    177:   snes->reason = SNES_CONVERGED_ITERATING;
    178:   X            = snes->vec_sol;
    179:   F            = snes->vec_func;
    180:   B            = snes->vec_rhs;
    181:   XA           = snes->vec_sol_update;
    182:   FA           = snes->work[0];
    183:   D            = snes->work[1];

    185:   /* work for the line search */
    186:   Y  = snes->work[2];
    187:   XM = snes->work[3];
    188:   FM = snes->work[4];

    190:   PetscObjectSAWsTakeAccess((PetscObject)snes);
    191:   snes->iter = 0;
    192:   snes->norm = 0.;
    193:   PetscObjectSAWsGrantAccess((PetscObject)snes);

    195:   /* initialization */

    197:   if (snes->npc && snes->npcside== PC_LEFT) {
    198:     SNESApplyNPC(snes,X,NULL,F);
    199:     SNESGetConvergedReason(snes->npc,&reason);
    200:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
    201:       snes->reason = SNES_DIVERGED_INNER;
    202:       return(0);
    203:     }
    204:     VecNorm(F,NORM_2,&fnorm);
    205:   } else {
    206:     if (!snes->vec_func_init_set) {
    207:       SNESComputeFunction(snes,X,F);
    208:     } else snes->vec_func_init_set = PETSC_FALSE;

    210:     VecNorm(F,NORM_2,&fnorm);
    211:     SNESCheckFunctionNorm(snes,fnorm);
    212:   }
    213:   fminnorm = fnorm;

    215:   PetscObjectSAWsTakeAccess((PetscObject)snes);
    216:   snes->norm = fnorm;
    217:   PetscObjectSAWsGrantAccess((PetscObject)snes);
    218:   SNESLogConvergenceHistory(snes,fnorm,0);
    219:   SNESMonitor(snes,0,fnorm);
    220:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
    221:   if (snes->reason) return(0);
    222:   SNESNGMRESUpdateSubspace_Private(snes,0,0,F,fnorm,X);

    224:   k_restart = 1;
    225:   l         = 1;
    226:   ivec      = 0;
    227:   for (k=1; k < snes->max_its+1; k++) {
    228:     /* Computation of x^M */
    229:     if (snes->npc && snes->npcside== PC_RIGHT) {
    230:       VecCopy(X,XM);
    231:       SNESSetInitialFunction(snes->npc,F);

    233:       PetscLogEventBegin(SNES_NPCSolve,snes->npc,XM,B,0);
    234:       SNESSolve(snes->npc,B,XM);
    235:       PetscLogEventEnd(SNES_NPCSolve,snes->npc,XM,B,0);

    237:       SNESGetConvergedReason(snes->npc,&reason);
    238:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
    239:         snes->reason = SNES_DIVERGED_INNER;
    240:         return(0);
    241:       }
    242:       SNESGetNPCFunction(snes,FM,&fMnorm);
    243:     } else {
    244:       /* no preconditioner -- just take gradient descent with line search */
    245:       VecCopy(F,Y);
    246:       VecCopy(F,FM);
    247:       VecCopy(X,XM);

    249:       fMnorm = fnorm;

    251:       SNESLineSearchApply(snes->linesearch,XM,FM,&fMnorm,Y);
    252:       SNESLineSearchGetReason(snes->linesearch,&lssucceed);
    253:       if (lssucceed) {
    254:         if (++snes->numFailures >= snes->maxFailures) {
    255:           snes->reason = SNES_DIVERGED_LINE_SEARCH;
    256:           return(0);
    257:         }
    258:       }
    259:     }

    261:     SNESNGMRESFormCombinedSolution_Private(snes,ivec,l,XM,FM,fMnorm,X,XA,FA);
    262:     /* r = F(x) */
    263:     if (fminnorm > fMnorm) fminnorm = fMnorm;  /* the minimum norm is now of F^M */

    265:     /* differences for selection and restart */
    266:     if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE || ngmres->select_type == SNES_NGMRES_SELECT_DIFFERENCE) {
    267:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,&dnorm,&dminnorm,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
    268:     } else {
    269:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
    270:     }
    271:     SNESCheckFunctionNorm(snes,fnorm);

    273:     /* combination (additive) or selection (multiplicative) of the N-GMRES solution */
    274:     SNESNGMRESSelect_Private(snes,k_restart,XM,FM,xMnorm,fMnorm,yMnorm,XA,FA,xAnorm,fAnorm,yAnorm,dnorm,fminnorm,dminnorm,X,F,Y,&xnorm,&fnorm,&ynorm);
    275:     selectRestart = PETSC_FALSE;

    277:     if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE) {
    278:       SNESNGMRESSelectRestart_Private(snes,l,fMnorm,fAnorm,dnorm,fminnorm,dminnorm,&selectRestart);

    280:       /* if the restart conditions persist for more than restart_it iterations, restart. */
    281:       if (selectRestart) restart_count++;
    282:       else restart_count = 0;
    283:     } else if (ngmres->restart_type == SNES_NGMRES_RESTART_PERIODIC) {
    284:       if (k_restart > ngmres->restart_periodic) {
    285:         if (ngmres->monitor) PetscViewerASCIIPrintf(ngmres->monitor,"periodic restart after %D iterations\n",k_restart);
    286:         restart_count = ngmres->restart_it;
    287:       }
    288:     }

    290:     ivec = k_restart % ngmres->msize; /* replace the last used part of the subspace */

    292:     /* restart after restart conditions have persisted for a fixed number of iterations */
    293:     if (restart_count >= ngmres->restart_it) {
    294:       if (ngmres->monitor) {
    295:         PetscViewerASCIIPrintf(ngmres->monitor,"Restarted at iteration %d\n",k_restart);
    296:       }
    297:       restart_count = 0;
    298:       k_restart     = 1;
    299:       l             = 1;
    300:       ivec          = 0;
    301:       /* q_{00} = nu */
    302:       SNESNGMRESUpdateSubspace_Private(snes,0,0,FM,fMnorm,XM);
    303:     } else {
    304:       /* select the current size of the subspace */
    305:       if (l < ngmres->msize) l++;
    306:       k_restart++;
    307:       /* place the current entry in the list of previous entries */
    308:       if (ngmres->candidate) {
    309:         if (fminnorm > fMnorm) fminnorm = fMnorm;
    310:         SNESNGMRESUpdateSubspace_Private(snes,ivec,l,FM,fMnorm,XM);
    311:       } else {
    312:         if (fminnorm > fnorm) fminnorm = fnorm;
    313:         SNESNGMRESUpdateSubspace_Private(snes,ivec,l,F,fnorm,X);
    314:       }
    315:     }

    317:     PetscObjectSAWsTakeAccess((PetscObject)snes);
    318:     snes->iter = k;
    319:     snes->norm = fnorm;
    320:     PetscObjectSAWsGrantAccess((PetscObject)snes);
    321:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
    322:     SNESMonitor(snes,snes->iter,snes->norm);
    323:     (*snes->ops->converged)(snes,snes->iter,0,0,fnorm,&snes->reason,snes->cnvP);
    324:     if (snes->reason) return(0);
    325:   }
    326:   snes->reason = SNES_DIVERGED_MAX_IT;
    327:   return(0);
    328: }

    330: /*@
    331:  SNESNGMRESSetRestartFmRise - Increase the restart count if the step x_M increases the residual F_M

    333:   Input Parameters:
    334:   +  snes - the SNES context.
    335:   -  flg  - boolean value deciding whether to use the option or not

    337:   Options Database:
    338:   + -snes_ngmres_restart_fm_rise - Increase the restart count if the step x_M increases the residual F_M

    340:   Level: intermediate

    342:   Notes:
    343:   If the proposed step x_M increases the residual F_M, it might be trying to get out of a stagnation area.
    344:   To help the solver do that, reset the Krylov subspace whenever F_M increases.

    346:   This option must be used with SNES_NGMRES_RESTART_DIFFERENCE

    348:   The default is FALSE.
    349:   .seealso: SNES_NGMRES_RESTART_DIFFERENCE
    350:   @*/
    351: PetscErrorCode SNESNGMRESSetRestartFmRise(SNES snes,PetscBool flg)
    352: {
    353:     PetscErrorCode (*f)(SNES,PetscBool);

    357:     PetscObjectQueryFunction((PetscObject)snes,"SNESNGMRESSetRestartFmRise_C",&f);
    358:     if (f) {(f)(snes,flg);}
    359:     return(0);
    360: }

    362: PetscErrorCode SNESNGMRESSetRestartFmRise_NGMRES(SNES snes,PetscBool flg)
    363: {
    364:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    367:   ngmres->restart_fm_rise = flg;
    368:   return(0);
    369: }

    371: PetscErrorCode SNESNGMRESGetRestartFmRise(SNES snes,PetscBool *flg)
    372: {
    373:     PetscErrorCode (*f)(SNES,PetscBool*);

    377:     PetscObjectQueryFunction((PetscObject)snes,"SNESNGMRESGetRestartFmRise_C",&f);
    378:     if (f) {(f)(snes,flg);}
    379:     return(0);
    380: }

    382: PetscErrorCode SNESNGMRESGetRestartFmRise_NGMRES(SNES snes,PetscBool *flg)
    383: {
    384:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    387:   *flg = ngmres->restart_fm_rise;
    388:   return(0);
    389: }

    392: /*@
    393:     SNESNGMRESSetRestartType - Sets the restart type for SNESNGMRES.

    395:     Logically Collective on SNES

    397:     Input Parameters:
    398: +   snes - the iterative context
    399: -   rtype - restart type

    401:     Options Database:
    402: +   -snes_ngmres_restart_type<difference,periodic,none> - set the restart type
    403: -   -snes_ngmres_restart[30] - sets the number of iterations before restart for periodic

    405:     Level: intermediate

    407:     SNESNGMRESRestartTypes:
    408: +   SNES_NGMRES_RESTART_NONE - never restart
    409: .   SNES_NGMRES_RESTART_DIFFERENCE - restart based upon difference criteria
    410: -   SNES_NGMRES_RESTART_PERIODIC - restart after a fixed number of iterations

    412:     Notes:
    413:     The default line search used is the L2 line search and it requires two additional function evaluations.

    415: @*/
    416: PetscErrorCode SNESNGMRESSetRestartType(SNES snes,SNESNGMRESRestartType rtype)
    417: {

    422:   PetscTryMethod(snes,"SNESNGMRESSetRestartType_C",(SNES,SNESNGMRESRestartType),(snes,rtype));
    423:   return(0);
    424: }

    426: /*@
    427:     SNESNGMRESSetSelectType - Sets the selection type for SNESNGMRES.  This determines how the candidate solution and
    428:     combined solution are used to create the next iterate.

    430:     Logically Collective on SNES

    432:     Input Parameters:
    433: +   snes - the iterative context
    434: -   stype - selection type

    436:     Options Database:
    437: .   -snes_ngmres_select_type<difference,none,linesearch>

    439:     Level: intermediate

    441:     SNESNGMRESSelectTypes:
    442: +   SNES_NGMRES_SELECT_NONE - choose the combined solution all the time
    443: .   SNES_NGMRES_SELECT_DIFFERENCE - choose based upon the selection criteria
    444: -   SNES_NGMRES_SELECT_LINESEARCH - choose based upon line search combination

    446:     Notes:
    447:     The default line search used is the L2 line search and it requires two additional function evaluations.

    449: @*/
    450: PetscErrorCode SNESNGMRESSetSelectType(SNES snes,SNESNGMRESSelectType stype)
    451: {

    456:   PetscTryMethod(snes,"SNESNGMRESSetSelectType_C",(SNES,SNESNGMRESSelectType),(snes,stype));
    457:   return(0);
    458: }

    460: PetscErrorCode SNESNGMRESSetSelectType_NGMRES(SNES snes,SNESNGMRESSelectType stype)
    461: {
    462:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    465:   ngmres->select_type = stype;
    466:   return(0);
    467: }

    469: PetscErrorCode SNESNGMRESSetRestartType_NGMRES(SNES snes,SNESNGMRESRestartType rtype)
    470: {
    471:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    474:   ngmres->restart_type = rtype;
    475:   return(0);
    476: }

    478: /*MC
    479:   SNESNGMRES - The Nonlinear Generalized Minimum Residual method.

    481:    Level: beginner

    483:    Options Database:
    484: +  -snes_ngmres_select_type<difference,none,linesearch> - choose the select between candidate and combined solution
    485: .  -snes_ngmres_restart_type<difference,none,periodic> - choose the restart conditions
    486: .  -snes_ngmres_candidate        - Use NGMRES variant which combines candidate solutions instead of actual solutions
    487: .  -snes_ngmres_m                - Number of stored previous solutions and residuals
    488: .  -snes_ngmres_restart_it       - Number of iterations the restart conditions hold before restart
    489: .  -snes_ngmres_gammaA           - Residual tolerance for solution select between the candidate and combination
    490: .  -snes_ngmres_gammaC           - Residual tolerance for restart
    491: .  -snes_ngmres_epsilonB         - Difference tolerance between subsequent solutions triggering restart
    492: .  -snes_ngmres_deltaB           - Difference tolerance between residuals triggering restart
    493: .  -snes_ngmres_restart_fm_rise  - Restart on residual rise from x_M step
    494: .  -snes_ngmres_monitor          - Prints relevant information about the ngmres iteration
    495: .  -snes_linesearch_type <basic,l2,cp> - Line search type used for the default smoother
    496: -  -additive_snes_linesearch_type - linesearch type used to select between the candidate and combined solution with additive select type

    498:    Notes:

    500:    The N-GMRES method combines m previous solutions into a minimum-residual solution by solving a small linearized
    501:    optimization problem at each iteration.

    503:    Very similar to the SNESANDERSON algorithm.

    505:    References:
    506: +  1. - C. W. Oosterlee and T. Washio, "Krylov Subspace Acceleration of Nonlinear Multigrid with Application to Recirculating Flows",
    507:    SIAM Journal on Scientific Computing, 21(5), 2000.
    508: -  2. - Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu, "Composing Scalable Nonlinear Algebraic Solvers",
    509:    SIAM Review, 57(4), 2015

    512: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
    513: M*/

    515: PETSC_EXTERN PetscErrorCode SNESCreate_NGMRES(SNES snes)
    516: {
    517:   SNES_NGMRES    *ngmres;
    519:   SNESLineSearch linesearch;

    522:   snes->ops->destroy        = SNESDestroy_NGMRES;
    523:   snes->ops->setup          = SNESSetUp_NGMRES;
    524:   snes->ops->setfromoptions = SNESSetFromOptions_NGMRES;
    525:   snes->ops->view           = SNESView_NGMRES;
    526:   snes->ops->solve          = SNESSolve_NGMRES;
    527:   snes->ops->reset          = SNESReset_NGMRES;

    529:   snes->usesnpc  = PETSC_TRUE;
    530:   snes->usesksp  = PETSC_FALSE;
    531:   snes->npcside  = PC_RIGHT;

    533:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

    535:   PetscNewLog(snes,&ngmres);
    536:   snes->data    = (void*) ngmres;
    537:   ngmres->msize = 30;

    539:   if (!snes->tolerancesset) {
    540:     snes->max_funcs = 30000;
    541:     snes->max_its   = 10000;
    542:   }

    544:   ngmres->candidate = PETSC_FALSE;

    546:   SNESGetLineSearch(snes,&linesearch);
    547:   if (!((PetscObject)linesearch)->type_name) {
    548:     SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
    549:   }

    551:   ngmres->additive_linesearch = NULL;
    552:   ngmres->approxfunc          = PETSC_FALSE;
    553:   ngmres->restart_it          = 2;
    554:   ngmres->restart_periodic    = 30;
    555:   ngmres->gammaA              = 2.0;
    556:   ngmres->gammaC              = 2.0;
    557:   ngmres->deltaB              = 0.9;
    558:   ngmres->epsilonB            = 0.1;
    559:   ngmres->restart_fm_rise     = PETSC_FALSE;

    561:   ngmres->restart_type = SNES_NGMRES_RESTART_DIFFERENCE;
    562:   ngmres->select_type  = SNES_NGMRES_SELECT_DIFFERENCE;

    564:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetSelectType_C",SNESNGMRESSetSelectType_NGMRES);
    565:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetRestartType_C",SNESNGMRESSetRestartType_NGMRES);
    566:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetRestartFmRise_C",SNESNGMRESSetRestartFmRise_NGMRES);
    567:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESGetRestartFmRise_C",SNESNGMRESGetRestartFmRise_NGMRES);
    568:   return(0);
    569: }