Date: 2016-07-24T17:09:15+00:00

Actual source code: snesngmres.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

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

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

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

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

     24: PetscErrorCode SNESDestroy_NGMRES(SNES snes)
     25: {
     27:   SNES_NGMRES    *ngmres = (SNES_NGMRES*)snes->data;

     30:   SNESReset_NGMRES(snes);
     31:   PetscFree5(ngmres->h,ngmres->beta,ngmres->xi,ngmres->fnorms,ngmres->q);
     32:   PetscFree(ngmres->s);
     33:   PetscFree(ngmres->xnorms);
     34: #if PETSC_USE_COMPLEX
     35:   PetscFree(ngmres->rwork);
     36: #endif
     37:   PetscFree(ngmres->work);
     38:   PetscFree(snes->data);
     39:   return(0);
     40: }

     44: PetscErrorCode SNESSetUp_NGMRES(SNES snes)
     45: {
     46:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
     47:   const char     *optionsprefix;
     48:   PetscInt       msize,hsize;
     50:   DM             dm;

     53:   if (snes->pc && snes->pcside == PC_LEFT && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
     54:     SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESNGMRES does not support left preconditioning with unpreconditioned function");
     55:   }
     56:   if (snes->pcside == PC_LEFT && snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_PRECONDITIONED;
     57:   SNESSetWorkVecs(snes,5);

     59:   if (!snes->vec_sol) {
     60:     SNESGetDM(snes,&dm);
     61:     DMCreateGlobalVector(dm,&snes->vec_sol);
     62:   }

     64:   if (!ngmres->Xdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Xdot);}
     65:   if (!ngmres->Fdot) {VecDuplicateVecs(snes->vec_sol,ngmres->msize,&ngmres->Fdot);}
     66:   if (!ngmres->setup_called) {
     67:     msize = ngmres->msize;          /* restart size */
     68:     hsize = msize * msize;

     70:     /* explicit least squares minimization solve */
     71:     PetscMalloc5(hsize,&ngmres->h, msize,&ngmres->beta, msize,&ngmres->xi, msize,&ngmres->fnorms, hsize,&ngmres->q);
     72:     PetscMalloc1(msize,&ngmres->xnorms);
     73:     ngmres->nrhs  = 1;
     74:     ngmres->lda   = msize;
     75:     ngmres->ldb   = msize;
     76:     PetscMalloc1(msize,&ngmres->s);
     77:     PetscMemzero(ngmres->h,   hsize*sizeof(PetscScalar));
     78:     PetscMemzero(ngmres->q,   hsize*sizeof(PetscScalar));
     79:     PetscMemzero(ngmres->xi,  msize*sizeof(PetscScalar));
     80:     PetscMemzero(ngmres->beta,msize*sizeof(PetscScalar));
     81:     ngmres->lwork = 12*msize;
     82: #if PETSC_USE_COMPLEX
     83:     PetscMalloc1(ngmres->lwork,&ngmres->rwork);
     84: #endif
     85:     PetscMalloc1(ngmres->lwork,&ngmres->work);
     86:   }

     88:   /* linesearch setup */
     89:   SNESGetOptionsPrefix(snes,&optionsprefix);

     91:   if (ngmres->select_type == SNES_NGMRES_SELECT_LINESEARCH) {
     92:     SNESLineSearchCreate(PetscObjectComm((PetscObject)snes),&ngmres->additive_linesearch);
     93:     SNESLineSearchSetSNES(ngmres->additive_linesearch,snes);
     94:     SNESLineSearchSetType(ngmres->additive_linesearch,SNESLINESEARCHL2);
     95:     SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,"additive_");
     96:     SNESLineSearchAppendOptionsPrefix(ngmres->additive_linesearch,optionsprefix);
     97:     SNESLineSearchSetFromOptions(ngmres->additive_linesearch);
     98:   }

    100:   ngmres->setup_called = PETSC_TRUE;
    101:   return(0);
    102: }

    106: PetscErrorCode SNESSetFromOptions_NGMRES(PetscOptionItems *PetscOptionsObject,SNES snes)
    107: {
    108:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
    110:   PetscBool      debug = PETSC_FALSE;
    111:   SNESLineSearch linesearch;

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

    137:   /* set the default type of the line search if the user hasn't already. */
    138:   if (!snes->linesearch) {
    139:     SNESGetLineSearch(snes,&linesearch);
    140:     SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
    141:   }
    142:   return(0);
    143: }

    147: PetscErrorCode SNESView_NGMRES(SNES snes,PetscViewer viewer)
    148: {
    149:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
    150:   PetscBool      iascii;

    154:   PetscObjectTypeCompare((PetscObject) viewer,PETSCVIEWERASCII,&iascii);
    155:   if (iascii) {
    156:     PetscViewerASCIIPrintf(viewer,"  Number of stored past updates: %d\n", ngmres->msize);
    157:     PetscViewerASCIIPrintf(viewer,"  Residual selection: gammaA=%1.0e, gammaC=%1.0e\n",ngmres->gammaA,ngmres->gammaC);
    158:     PetscViewerASCIIPrintf(viewer,"  Difference restart: epsilonB=%1.0e, deltaB=%1.0e\n",ngmres->epsilonB,ngmres->deltaB);
    159:     PetscViewerASCIIPrintf(viewer,"  Restart on F_M residual increase: %s\n",ngmres->restart_fm_rise?"TRUE":"FALSE");
    160:   }
    161:   return(0);
    162: }

    166: PetscErrorCode SNESSolve_NGMRES(SNES snes)
    167: {
    168:   SNES_NGMRES          *ngmres = (SNES_NGMRES*) snes->data;
    169:   /* present solution, residual, and preconditioned residual */
    170:   Vec                  X,F,B,D,Y;

    172:   /* candidate linear combination answers */
    173:   Vec                  XA,FA,XM,FM;

    175:   /* coefficients and RHS to the minimization problem */
    176:   PetscReal            fnorm,fMnorm,fAnorm;
    177:   PetscReal            xnorm,xMnorm,xAnorm;
    178:   PetscReal            ynorm,yMnorm,yAnorm;
    179:   PetscInt             k,k_restart,l,ivec,restart_count = 0;

    181:   /* solution selection data */
    182:   PetscBool            selectRestart;
    183:   /*
    184:       These two variables are initialized to prevent compilers/analyzers from producing false warnings about these variables being passed
    185:       to SNESNGMRESSelect_Private() without being set when SNES_NGMRES_RESTART_DIFFERENCE, the values are not used in the subroutines in that case
    186:       so the code is correct as written.
    187:   */
    188:   PetscReal            dnorm = 0.0,dminnorm = 0.0;
    189:   PetscReal            fminnorm;

    191:   SNESConvergedReason  reason;
    192:   SNESLineSearchReason lssucceed;
    193:   PetscErrorCode       ierr;

    196:   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);

    198:   PetscCitationsRegister(SNESCitation,&SNEScite);
    199:   /* variable initialization */
    200:   snes->reason = SNES_CONVERGED_ITERATING;
    201:   X            = snes->vec_sol;
    202:   F            = snes->vec_func;
    203:   B            = snes->vec_rhs;
    204:   XA           = snes->vec_sol_update;
    205:   FA           = snes->work[0];
    206:   D            = snes->work[1];

    208:   /* work for the line search */
    209:   Y  = snes->work[2];
    210:   XM = snes->work[3];
    211:   FM = snes->work[4];

    213:   PetscObjectSAWsTakeAccess((PetscObject)snes);
    214:   snes->iter = 0;
    215:   snes->norm = 0.;
    216:   PetscObjectSAWsGrantAccess((PetscObject)snes);

    218:   /* initialization */

    220:   if (snes->pc && snes->pcside == PC_LEFT) {
    221:     SNESApplyNPC(snes,X,NULL,F);
    222:     SNESGetConvergedReason(snes->pc,&reason);
    223:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
    224:       snes->reason = SNES_DIVERGED_INNER;
    225:       return(0);
    226:     }
    227:     VecNorm(F,NORM_2,&fnorm);
    228:   } else {
    229:     if (!snes->vec_func_init_set) {
    230:       SNESComputeFunction(snes,X,F);
    231:     } else snes->vec_func_init_set = PETSC_FALSE;

    233:     VecNorm(F,NORM_2,&fnorm);
    234:     SNESCheckFunctionNorm(snes,fnorm);
    235:   }
    236:   fminnorm = fnorm;

    238:   PetscObjectSAWsTakeAccess((PetscObject)snes);
    239:   snes->norm = fnorm;
    240:   PetscObjectSAWsGrantAccess((PetscObject)snes);
    241:   SNESLogConvergenceHistory(snes,fnorm,0);
    242:   SNESMonitor(snes,0,fnorm);
    243:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
    244:   if (snes->reason) return(0);
    245:   SNESNGMRESUpdateSubspace_Private(snes,0,0,F,fnorm,X);

    247:   k_restart = 1;
    248:   l         = 1;
    249:   ivec      = 0;
    250:   for (k=1; k < snes->max_its+1; k++) {
    251:     /* Computation of x^M */
    252:     if (snes->pc && snes->pcside == PC_RIGHT) {
    253:       VecCopy(X,XM);
    254:       SNESSetInitialFunction(snes->pc,F);

    256:       PetscLogEventBegin(SNES_NPCSolve,snes->pc,XM,B,0);
    257:       SNESSolve(snes->pc,B,XM);
    258:       PetscLogEventEnd(SNES_NPCSolve,snes->pc,XM,B,0);

    260:       SNESGetConvergedReason(snes->pc,&reason);
    261:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
    262:         snes->reason = SNES_DIVERGED_INNER;
    263:         return(0);
    264:       }
    265:       SNESGetNPCFunction(snes,FM,&fMnorm);
    266:     } else {
    267:       /* no preconditioner -- just take gradient descent with line search */
    268:       VecCopy(F,Y);
    269:       VecCopy(F,FM);
    270:       VecCopy(X,XM);

    272:       fMnorm = fnorm;

    274:       SNESLineSearchApply(snes->linesearch,XM,FM,&fMnorm,Y);
    275:       SNESLineSearchGetReason(snes->linesearch,&lssucceed);
    276:       if (lssucceed) {
    277:         if (++snes->numFailures >= snes->maxFailures) {
    278:           snes->reason = SNES_DIVERGED_LINE_SEARCH;
    279:           return(0);
    280:         }
    281:       }
    282:     }

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

    288:     /* differences for selection and restart */
    289:     if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE || ngmres->select_type == SNES_NGMRES_SELECT_DIFFERENCE) {
    290:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,&dnorm,&dminnorm,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
    291:     } else {
    292:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,&xMnorm,NULL,&yMnorm,&xAnorm,&fAnorm,&yAnorm);
    293:     }
    294:     SNESCheckFunctionNorm(snes,fnorm);

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

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

    303:       /* if the restart conditions persist for more than restart_it iterations, restart. */
    304:       if (selectRestart) restart_count++;
    305:       else restart_count = 0;
    306:     } else if (ngmres->restart_type == SNES_NGMRES_RESTART_PERIODIC) {
    307:       if (k_restart > ngmres->restart_periodic) {
    308:         if (ngmres->monitor) PetscViewerASCIIPrintf(ngmres->monitor,"periodic restart after %D iterations\n",k_restart);
    309:         restart_count = ngmres->restart_it;
    310:       }
    311:     }

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

    315:     /* restart after restart conditions have persisted for a fixed number of iterations */
    316:     if (restart_count >= ngmres->restart_it) {
    317:       if (ngmres->monitor) {
    318:         PetscViewerASCIIPrintf(ngmres->monitor,"Restarted at iteration %d\n",k_restart);
    319:       }
    320:       restart_count = 0;
    321:       k_restart     = 1;
    322:       l             = 1;
    323:       ivec          = 0;
    324:       /* q_{00} = nu */
    325:       SNESNGMRESUpdateSubspace_Private(snes,0,0,FM,fMnorm,XM);
    326:     } else {
    327:       /* select the current size of the subspace */
    328:       if (l < ngmres->msize) l++;
    329:       k_restart++;
    330:       /* place the current entry in the list of previous entries */
    331:       if (ngmres->candidate) {
    332:         if (fminnorm > fMnorm) fminnorm = fMnorm;
    333:         SNESNGMRESUpdateSubspace_Private(snes,ivec,l,FM,fMnorm,XM);
    334:       } else {
    335:         if (fminnorm > fnorm) fminnorm = fnorm;
    336:         SNESNGMRESUpdateSubspace_Private(snes,ivec,l,F,fnorm,X);
    337:       }
    338:     }

    340:     PetscObjectSAWsTakeAccess((PetscObject)snes);
    341:     snes->iter = k;
    342:     snes->norm = fnorm;
    343:     PetscObjectSAWsGrantAccess((PetscObject)snes);
    344:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
    345:     SNESMonitor(snes,snes->iter,snes->norm);
    346:     (*snes->ops->converged)(snes,snes->iter,0,0,fnorm,&snes->reason,snes->cnvP);
    347:     if (snes->reason) return(0);
    348:   }
    349:   snes->reason = SNES_DIVERGED_MAX_IT;
    350:   return(0);
    351: }

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

    358:   Input Parameters:
    359:   +  snes - the SNES context.
    360:   -  flg  - boolean value deciding whether to use the option or not

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

    365:   Level: intermediate

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

    371:   This option must be used with SNES_NGMRES_RESTART_DIFFERENCE

    373:   The default is FALSE.
    374:   .seealso: SNES_NGMRES_RESTART_DIFFERENCE
    375:   @*/
    376: PetscErrorCode SNESNGMRESSetRestartFmRise(SNES snes,PetscBool flg)
    377: {
    378:     PetscErrorCode (*f)(SNES,PetscBool);

    382:     PetscObjectQueryFunction((PetscObject)snes,"SNESNGMRESSetRestartFmRise_C",&f);
    383:     if (f) {(f)(snes,flg);}
    384:     return(0);
    385: }

    389: PetscErrorCode SNESNGMRESSetRestartFmRise_NGMRES(SNES snes,PetscBool flg)
    390: {
    391:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    394:   ngmres->restart_fm_rise = flg;
    395:   return(0);
    396: }

    400: PetscErrorCode SNESNGMRESGetRestartFmRise(SNES snes,PetscBool *flg)
    401: {
    402:     PetscErrorCode (*f)(SNES,PetscBool*);

    406:     PetscObjectQueryFunction((PetscObject)snes,"SNESNGMRESGetRestartFmRise_C",&f);
    407:     if (f) {(f)(snes,flg);}
    408:     return(0);
    409: }

    413: PetscErrorCode SNESNGMRESGetRestartFmRise_NGMRES(SNES snes,PetscBool *flg)
    414: {
    415:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    418:   *flg = ngmres->restart_fm_rise;
    419:   return(0);
    420: }

    425: /*@
    426:     SNESNGMRESSetRestartType - Sets the restart type for SNESNGMRES.

    428:     Logically Collective on SNES

    430:     Input Parameters:
    431: +   snes - the iterative context
    432: -   rtype - restart type

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

    438:     Level: intermediate

    440:     SNESNGMRESRestartTypes:
    441: +   SNES_NGMRES_RESTART_NONE - never restart
    442: .   SNES_NGMRES_RESTART_DIFFERENCE - restart based upon difference criteria
    443: -   SNES_NGMRES_RESTART_PERIODIC - restart after a fixed number of iterations

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

    448: .keywords: SNES, SNESNGMRES, restart, type, set SNESLineSearch
    449: @*/
    450: PetscErrorCode SNESNGMRESSetRestartType(SNES snes,SNESNGMRESRestartType rtype)
    451: {

    456:   PetscTryMethod(snes,"SNESNGMRESSetRestartType_C",(SNES,SNESNGMRESRestartType),(snes,rtype));
    457:   return(0);
    458: }

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

    466:     Logically Collective on SNES

    468:     Input Parameters:
    469: +   snes - the iterative context
    470: -   stype - selection type

    472:     Options Database:
    473: .   -snes_ngmres_select_type<difference,none,linesearch>

    475:     Level: intermediate

    477:     SNESNGMRESSelectTypes:
    478: +   SNES_NGMRES_SELECT_NONE - choose the combined solution all the time
    479: .   SNES_NGMRES_SELECT_DIFFERENCE - choose based upon the selection criteria
    480: -   SNES_NGMRES_SELECT_LINESEARCH - choose based upon line search combination

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

    485: .keywords: SNES, SNESNGMRES, selection, type, set SNESLineSearch
    486: @*/
    487: PetscErrorCode SNESNGMRESSetSelectType(SNES snes,SNESNGMRESSelectType stype)
    488: {

    493:   PetscTryMethod(snes,"SNESNGMRESSetSelectType_C",(SNES,SNESNGMRESSelectType),(snes,stype));
    494:   return(0);
    495: }

    499: PetscErrorCode SNESNGMRESSetSelectType_NGMRES(SNES snes,SNESNGMRESSelectType stype)
    500: {
    501:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    504:   ngmres->select_type = stype;
    505:   return(0);
    506: }

    510: PetscErrorCode SNESNGMRESSetRestartType_NGMRES(SNES snes,SNESNGMRESRestartType rtype)
    511: {
    512:   SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;

    515:   ngmres->restart_type = rtype;
    516:   return(0);
    517: }

    519: /*MC
    520:   SNESNGMRES - The Nonlinear Generalized Minimum Residual method.

    522:    Level: beginner

    524:    Options Database:
    525: +  -snes_ngmres_select_type<difference,none,linesearch> - choose the select between candidate and combined solution
    526: .  -snes_ngmres_restart_type<difference,none,periodic> - choose the restart conditions
    527: .  -snes_ngmres_candidate        - Use NGMRES variant which combines candidate solutions instead of actual solutions
    528: .  -snes_ngmres_m                - Number of stored previous solutions and residuals
    529: .  -snes_ngmres_restart_it       - Number of iterations the restart conditions hold before restart
    530: .  -snes_ngmres_gammaA           - Residual tolerance for solution select between the candidate and combination
    531: .  -snes_ngmres_gammaC           - Residual tolerance for restart
    532: .  -snes_ngmres_epsilonB         - Difference tolerance between subsequent solutions triggering restart
    533: .  -snes_ngmres_deltaB           - Difference tolerance between residuals triggering restart
    534: .  -snes_ngmres_restart_fm_rise  - Restart on residual rise from x_M step
    535: .  -snes_ngmres_monitor          - Prints relevant information about the ngmres iteration
    536: .  -snes_linesearch_type <basic,l2,cp> - Line search type used for the default smoother
    537: -  -additive_snes_linesearch_type - linesearch type used to select between the candidate and combined solution with additive select type

    539:    Notes:

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

    544:    Very similar to the SNESANDERSON algorithm.

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

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

    558: PETSC_EXTERN PetscErrorCode SNESCreate_NGMRES(SNES snes)
    559: {
    560:   SNES_NGMRES    *ngmres;

    564:   snes->ops->destroy        = SNESDestroy_NGMRES;
    565:   snes->ops->setup          = SNESSetUp_NGMRES;
    566:   snes->ops->setfromoptions = SNESSetFromOptions_NGMRES;
    567:   snes->ops->view           = SNESView_NGMRES;
    568:   snes->ops->solve          = SNESSolve_NGMRES;
    569:   snes->ops->reset          = SNESReset_NGMRES;

    571:   snes->usespc   = PETSC_TRUE;
    572:   snes->usesksp  = PETSC_FALSE;
    573:   snes->pcside   = PC_RIGHT;

    575:   PetscNewLog(snes,&ngmres);
    576:   snes->data    = (void*) ngmres;
    577:   ngmres->msize = 30;

    579:   if (!snes->tolerancesset) {
    580:     snes->max_funcs = 30000;
    581:     snes->max_its   = 10000;
    582:   }

    584:   ngmres->candidate = PETSC_FALSE;

    586:   ngmres->additive_linesearch = NULL;
    587:   ngmres->approxfunc          = PETSC_FALSE;
    588:   ngmres->restart_it          = 2;
    589:   ngmres->restart_periodic    = 30;
    590:   ngmres->gammaA              = 2.0;
    591:   ngmres->gammaC              = 2.0;
    592:   ngmres->deltaB              = 0.9;
    593:   ngmres->epsilonB            = 0.1;
    594:   ngmres->restart_fm_rise     = PETSC_FALSE;

    596:   ngmres->restart_type = SNES_NGMRES_RESTART_DIFFERENCE;
    597:   ngmres->select_type  = SNES_NGMRES_SELECT_DIFFERENCE;

    599:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetSelectType_C",SNESNGMRESSetSelectType_NGMRES);
    600:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetRestartType_C",SNESNGMRESSetRestartType_NGMRES);
    601:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESSetRestartFmRise_C",SNESNGMRESSetRestartFmRise_NGMRES);
    602:   PetscObjectComposeFunction((PetscObject)snes,"SNESNGMRESGetRestartFmRise_C",SNESNGMRESGetRestartFmRise_NGMRES);
    603:   return(0);
    604: }