Date: 2013-07-02T14:49:33+00:00

Actual source code: snesncg.c
petsc-3.4.2 2013-07-02

      1: #include <../src/snes/impls/ncg/snesncgimpl.h> /*I "petscsnes.h" I*/
      2: const char *const SNESNCGTypes[] = {"FR","PRP","HS","DY","CD","SNESNCGType","SNES_NCG_",0};

      6: PetscErrorCode SNESReset_NCG(SNES snes)
      7: {
      9:   return(0);
     10: }

     12: #define SNESLINESEARCHNCGLINEAR "linear"

     14: /*
     15:   SNESDestroy_NCG - Destroys the private SNES_NCG context that was created with SNESCreate_NCG().

     17:   Input Parameter:
     18: . snes - the SNES context

     20:   Application Interface Routine: SNESDestroy()
     21: */
     24: PetscErrorCode SNESDestroy_NCG(SNES snes)
     25: {

     29:   PetscFree(snes->data);
     30:   return(0);
     31: }

     33: /*
     34:    SNESSetUp_NCG - Sets up the internal data structures for the later use
     35:    of the SNESNCG nonlinear solver.

     37:    Input Parameters:
     38: +  snes - the SNES context
     39: -  x - the solution vector

     41:    Application Interface Routine: SNESSetUp()
     42:  */

     44: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch);

     48: PetscErrorCode SNESSetUp_NCG(SNES snes)
     49: {

     53:   SNESSetWorkVecs(snes,2);
     54:   SNESSetUpMatrices(snes);
     55:   SNESLineSearchRegister(SNESLINESEARCHNCGLINEAR, SNESLineSearchCreate_NCGLinear);
     56:   return(0);
     57: }
     58: /*
     59:   SNESSetFromOptions_NCG - Sets various parameters for the SNESNCG method.

     61:   Input Parameter:
     62: . snes - the SNES context

     64:   Application Interface Routine: SNESSetFromOptions()
     65: */
     68: static PetscErrorCode SNESSetFromOptions_NCG(SNES snes)
     69: {
     70:   SNES_NCG       *ncg = (SNES_NCG*)snes->data;
     72:   PetscBool      debug;
     73:   SNESLineSearch linesearch;
     74:   SNESNCGType    ncgtype=ncg->type;

     77:   PetscOptionsHead("SNES NCG options");
     78:   PetscOptionsBool("-snes_ncg_monitor","Monitor NCG iterations","SNES",ncg->monitor ? PETSC_TRUE : PETSC_FALSE, &debug, NULL);
     79:   PetscOptionsEnum("-snes_ncg_type","NCG Beta type used","SNESNCGSetType",SNESNCGTypes,(PetscEnum)ncg->type,(PetscEnum*)&ncgtype,NULL);
     80:   SNESNCGSetType(snes, ncgtype);
     81:   if (debug) {
     82:     ncg->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
     83:   }
     84:   PetscOptionsTail();
     85:   if (!snes->linesearch) {
     86:     SNESGetLineSearch(snes, &linesearch);
     87:     if (!snes->pc) {
     88:       SNESLineSearchSetType(linesearch, SNESLINESEARCHCP);
     89:     } else {
     90:       SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
     91:     }
     92:   }
     93:   return(0);
     94: }

     96: /*
     97:   SNESView_NCG - Prints info from the SNESNCG data structure.

     99:   Input Parameters:
    100: + SNES - the SNES context
    101: - viewer - visualization context

    103:   Application Interface Routine: SNESView()
    104: */
    107: static PetscErrorCode SNESView_NCG(SNES snes, PetscViewer viewer)
    108: {
    109:   PetscBool      iascii;

    113:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
    114:   if (iascii) {
    115:   }
    116:   return(0);
    117: }

    121: PetscErrorCode SNESLineSearchApply_NCGLinear(SNESLineSearch linesearch)
    122: {
    123:   PetscScalar    alpha, ptAp;
    124:   Vec            X, Y, F, W;
    125:   SNES           snes;
    127:   PetscReal      *fnorm, *xnorm, *ynorm;
    128:   MatStructure   flg = DIFFERENT_NONZERO_PATTERN;

    131:   SNESLineSearchGetSNES(linesearch, &snes);
    132:   X     = linesearch->vec_sol;
    133:   W     = linesearch->vec_sol_new;
    134:   F     = linesearch->vec_func;
    135:   Y     = linesearch->vec_update;
    136:   fnorm = &linesearch->fnorm;
    137:   xnorm = &linesearch->xnorm;
    138:   ynorm = &linesearch->ynorm;

    140:   /*

    142:    The exact step size for unpreconditioned linear CG is just:
    143:    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy)
    144:    */
    145:   SNESComputeJacobian(snes, X, &snes->jacobian, &snes->jacobian_pre, &flg);
    146:   VecDot(F, F, &alpha);
    147:   MatMult(snes->jacobian, Y, W);
    148:   VecDot(Y, W, &ptAp);
    149:   alpha = alpha / ptAp;
    150:   PetscPrintf(PetscObjectComm((PetscObject)snes), "alpha: %G\n", PetscRealPart(alpha));
    151:   VecAXPY(X, alpha, Y);
    152:   SNESComputeFunction(snes, X, F);

    154:   VecNorm(F, NORM_2, fnorm);
    155:   VecNorm(X, NORM_2, xnorm);
    156:   VecNorm(Y, NORM_2, ynorm);
    157:   return(0);
    158: }

    162: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch linesearch)
    163: {
    165:   linesearch->ops->apply          = SNESLineSearchApply_NCGLinear;
    166:   linesearch->ops->destroy        = NULL;
    167:   linesearch->ops->setfromoptions = NULL;
    168:   linesearch->ops->reset          = NULL;
    169:   linesearch->ops->view           = NULL;
    170:   linesearch->ops->setup          = NULL;
    171:   return(0);
    172: }

    176: /*

    178:  Assuming F = SNESComputeFunction(X) compute Y^tJ^tF using a simple secant approximation of the jacobian.

    180:  */
    181: PetscErrorCode SNESNCGComputeYtJtF_Private(SNES snes, Vec X, Vec F, Vec Y, Vec W, Vec G, PetscScalar * ytJtf)
    182: {
    184:   PetscScalar    ftf, ftg, fty, h;

    187:   VecDot(F, F, &ftf);
    188:   VecDot(F, Y, &fty);
    189:   h      = 1e-5*fty / fty;
    190:   VecCopy(X, W);
    191:   VecAXPY(W, -h, Y);          /* this is arbitrary */
    192:   SNESComputeFunction(snes, W, G);
    193:   VecDot(G, F, &ftg);
    194:   *ytJtf = (ftg - ftf) / h;
    195:   return(0);
    196: }

    200: /*@
    201:     SNESNCGSetType - Sets the conjugate update type for SNESNCG.

    203:     Logically Collective on SNES

    205:     Input Parameters:
    206: +   snes - the iterative context
    207: -   btype - update type

    209:     Options Database:
    210: .   -snes_ncg_type<prp,fr,hs,dy,cd>

    212:     Level: intermediate

    214:     SNESNCGSelectTypes:
    215: +   SNES_NCG_FR - Fletcher-Reeves update
    216: .   SNES_NCG_PRP - Polak-Ribiere-Polyak update
    217: .   SNES_NCG_HS - Hestenes-Steifel update
    218: .   SNES_NCG_DY - Dai-Yuan update
    219: -   SNES_NCG_CD - Conjugate Descent update

    221:    Notes:
    222:    PRP is the default, and the only one that tolerates generalized search directions.

    224: .keywords: SNES, SNESNCG, selection, type, set
    225: @*/
    226: PetscErrorCode SNESNCGSetType(SNES snes, SNESNCGType btype)
    227: {

    232:   PetscTryMethod(snes,"SNESNCGSetType_C",(SNES,SNESNCGType),(snes,btype));
    233:   return(0);
    234: }

    238: PetscErrorCode SNESNCGSetType_NCG(SNES snes, SNESNCGType btype)
    239: {
    240:   SNES_NCG *ncg = (SNES_NCG*)snes->data;

    243:   ncg->type = btype;
    244:   return(0);
    245: }

    247: /*
    248:   SNESSolve_NCG - Solves a nonlinear system with the Nonlinear Conjugate Gradient method.

    250:   Input Parameters:
    251: . snes - the SNES context

    253:   Output Parameter:
    254: . outits - number of iterations until termination

    256:   Application Interface Routine: SNESSolve()
    257: */
    260: PetscErrorCode SNESSolve_NCG(SNES snes)
    261: {
    262:   SNES_NCG            *ncg = (SNES_NCG*)snes->data;
    263:   Vec                 X, dX, lX, F, B, Fold;
    264:   PetscReal           fnorm, ynorm, xnorm, beta = 0.0;
    265:   PetscScalar         dXdotF, dXolddotFold, dXdotFold, lXdotF, lXdotFold;
    266:   PetscInt            maxits, i;
    267:   PetscErrorCode      ierr;
    268:   SNESConvergedReason reason;
    269:   PetscBool           lsSuccess = PETSC_TRUE;
    270:   SNESLineSearch      linesearch;

    273:   snes->reason = SNES_CONVERGED_ITERATING;

    275:   maxits = snes->max_its;            /* maximum number of iterations */
    276:   X      = snes->vec_sol;            /* X^n */
    277:   Fold   = snes->work[0];            /* The previous iterate of X */
    278:   dX     = snes->work[1];            /* the preconditioned direction */
    279:   lX     = snes->vec_sol_update;     /* the conjugate direction */
    280:   F      = snes->vec_func;           /* residual vector */
    281:   B      = snes->vec_rhs;            /* the right hand side */

    283:   SNESGetLineSearch(snes, &linesearch);

    285:   PetscObjectAMSTakeAccess((PetscObject)snes);
    286:   snes->iter = 0;
    287:   snes->norm = 0.;
    288:   PetscObjectAMSGrantAccess((PetscObject)snes);

    290:   /* compute the initial function and preconditioned update dX */
    291:   if (!snes->vec_func_init_set) {
    292:     SNESComputeFunction(snes,X,F);
    293:     if (snes->domainerror) {
    294:       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
    295:       return(0);
    296:     }
    297:   } else snes->vec_func_init_set = PETSC_FALSE;

    299:   if (!snes->norm_init_set) {
    300:     /* convergence test */
    301:     VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F||  */
    302:     if (PetscIsInfOrNanReal(fnorm)) {
    303:       snes->reason = SNES_DIVERGED_FNORM_NAN;
    304:       return(0);
    305:     }
    306:   } else {
    307:     fnorm               = snes->norm_init;
    308:     snes->norm_init_set = PETSC_FALSE;
    309:   }
    310:   PetscObjectAMSTakeAccess((PetscObject)snes);
    311:   snes->norm = fnorm;
    312:   PetscObjectAMSGrantAccess((PetscObject)snes);
    313:   SNESLogConvergenceHistory(snes,fnorm,0);
    314:   SNESMonitor(snes,0,fnorm);

    316:   /* set parameter for default relative tolerance convergence test */
    317:   snes->ttol = fnorm*snes->rtol;
    318:   /* test convergence */
    319:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
    320:   if (snes->reason) return(0);

    322:   /* Call general purpose update function */
    323:   if (snes->ops->update) {
    324:     (*snes->ops->update)(snes, snes->iter);
    325:   }

    327:   /* first update -- just use the (preconditioned) residual direction for the initial conjugate direction */

    329:   if (snes->pc && snes->pcside == PC_RIGHT) {
    330:     VecCopy(X, dX);
    331:     SNESSetInitialFunction(snes->pc, F);
    332:     SNESSetInitialFunctionNorm(snes->pc, fnorm);

    334:     PetscLogEventBegin(SNES_NPCSolve,snes->pc,dX,B,0);
    335:     SNESSolve(snes->pc, B, dX);
    336:     PetscLogEventEnd(SNES_NPCSolve,snes->pc,dX,B,0);

    338:     SNESGetConvergedReason(snes->pc,&reason);
    339:     if (reason < 0 && (reason != SNES_DIVERGED_MAX_IT)) {
    340:       snes->reason = SNES_DIVERGED_INNER;
    341:       return(0);
    342:     }
    343:     VecAYPX(dX,-1.0,X);
    344:   } else {
    345:     VecCopy(F, dX);
    346:   }
    347:   VecCopy(dX, lX);
    348:   VecDot(F, dX, &dXdotF);
    349:   /*
    350:   } else {
    351:     SNESNCGComputeYtJtF_Private(snes, X, F, dX, W, G, &dXdotF);
    352:   }
    353:    */
    354:   for (i = 1; i < maxits + 1; i++) {
    355:     lsSuccess = PETSC_TRUE;
    356:     /* some update types require the old update direction or conjugate direction */
    357:     if (ncg->type != SNES_NCG_FR) {
    358:       VecCopy(F, Fold);
    359:     }
    360:     SNESLineSearchApply(linesearch, X, F, &fnorm, lX);
    361:     SNESLineSearchGetSuccess(linesearch, &lsSuccess);
    362:     if (!lsSuccess) {
    363:       if (++snes->numFailures >= snes->maxFailures) {
    364:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
    365:         return(0);
    366:       }
    367:     }
    368:     if (snes->nfuncs >= snes->max_funcs) {
    369:       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
    370:       return(0);
    371:     }
    372:     if (snes->domainerror) {
    373:       snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
    374:       return(0);
    375:     }
    376:     SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm);
    377:     /* Monitor convergence */
    378:     PetscObjectAMSTakeAccess((PetscObject)snes);
    379:     snes->iter = i;
    380:     snes->norm = fnorm;
    381:     PetscObjectAMSGrantAccess((PetscObject)snes);
    382:     SNESLogConvergenceHistory(snes,snes->norm,0);
    383:     SNESMonitor(snes,snes->iter,snes->norm);

    385:     /* Test for convergence */
    386:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
    387:     if (snes->reason) return(0);

    389:     /* Call general purpose update function */
    390:     if (snes->ops->update) {
    391:       (*snes->ops->update)(snes, snes->iter);
    392:     }
    393:     if (snes->pc && snes->pcside == PC_RIGHT) {
    394:       VecCopy(X,dX);
    395:       SNESSetInitialFunction(snes->pc, F);
    396:       SNESSetInitialFunctionNorm(snes->pc, fnorm);
    397:       PetscLogEventBegin(SNES_NPCSolve,snes->pc,dX,B,0);
    398:       SNESSolve(snes->pc, B, dX);
    399:       PetscLogEventEnd(SNES_NPCSolve,snes->pc,dX,B,0);
    400:       SNESGetConvergedReason(snes->pc,&reason);
    401:       if (reason < 0 && (reason != SNES_DIVERGED_MAX_IT)) {
    402:         snes->reason = SNES_DIVERGED_INNER;
    403:         return(0);
    404:       }
    405:       VecAYPX(dX,-1.0,X);
    406:     } else {
    407:       VecCopy(F, dX);
    408:     }

    410:     /* compute the conjugate direction lX = dX + beta*lX with beta = ((dX, dX) / (dX_old, dX_old) (Fletcher-Reeves update)*/
    411:     switch (ncg->type) {
    412:     case SNES_NCG_FR: /* Fletcher-Reeves */
    413:       dXolddotFold = dXdotF;
    414:       VecDot(dX, dX, &dXdotF);
    415:       beta         = PetscRealPart(dXdotF / dXolddotFold);
    416:       break;
    417:     case SNES_NCG_PRP: /* Polak-Ribiere-Poylak */
    418:       dXolddotFold = dXdotF;
    419:       VecDotBegin(F, dX, &dXdotF);
    420:       VecDotBegin(Fold, dX, &dXdotFold);
    421:       VecDotEnd(F, dX, &dXdotF);
    422:       VecDotEnd(Fold, dX, &dXdotFold);
    423:       beta         = PetscRealPart(((dXdotF - dXdotFold) / dXolddotFold));
    424:       if (beta < 0.0) beta = 0.0; /* restart */
    425:       break;
    426:     case SNES_NCG_HS: /* Hestenes-Stiefel */
    427:       VecDotBegin(dX, F, &dXdotF);
    428:       VecDotBegin(dX, Fold, &dXdotFold);
    429:       VecDotBegin(lX, F, &lXdotF);
    430:       VecDotBegin(lX, Fold, &lXdotFold);
    431:       VecDotEnd(dX, F, &dXdotF);
    432:       VecDotEnd(dX, Fold, &dXdotFold);
    433:       VecDotEnd(lX, F, &lXdotF);
    434:       VecDotEnd(lX, Fold, &lXdotFold);
    435:       beta = PetscRealPart((dXdotF - dXdotFold) / (lXdotF - lXdotFold));
    436:       break;
    437:     case SNES_NCG_DY: /* Dai-Yuan */
    438:       VecDotBegin(dX, F, &dXdotF);
    439:       VecDotBegin(lX, F, &lXdotF);
    440:       VecDotBegin(lX, Fold, &lXdotFold);
    441:       VecDotEnd(dX, F, &dXdotF);
    442:       VecDotEnd(lX, F, &lXdotF);
    443:       VecDotEnd(lX, Fold, &lXdotFold);
    444:       beta = PetscRealPart(dXdotF / (lXdotFold - lXdotF));
    445:       break;
    446:     case SNES_NCG_CD: /* Conjugate Descent */
    447:       VecDotBegin(dX, F, &dXdotF);
    448:       VecDotBegin(lX, Fold, &lXdotFold);
    449:       VecDotEnd(dX, F, &dXdotF);
    450:       VecDotEnd(lX, Fold, &lXdotFold);
    451:       beta = PetscRealPart(dXdotF / lXdotFold);
    452:       break;
    453:     }
    454:     if (ncg->monitor) {
    455:       PetscViewerASCIIPrintf(ncg->monitor, "beta = %e\n", beta);
    456:     }
    457:     VecAYPX(lX, beta, dX);
    458:   }
    459:   PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
    460:   if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
    461:   return(0);
    462: }

    466: /*MC
    467:   SNESNCG - Nonlinear Conjugate-Gradient method for the solution of general nonlinear systems.

    469:   Level: beginner

    471:   Options Database:
    472: +   -snes_ncg_type <fr, prp, dy, hs, cd> - Choice of conjugate-gradient update parameter.
    473: .   -snes_linesearch_type <cp,l2,basic> - Line search type.
    474: -   -snes_ncg_monitor - Print relevant information about the ncg iteration.

    476: Notes: This solves the nonlinear system of equations F(x) = 0 using the nonlinear generalization of the conjugate
    477: gradient method.  This may be used with a nonlinear preconditioner used to pick the new search directions, but otherwise
    478: chooses the initial search direction as F(x) for the initial guess x.

    481: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESNQN
    482: M*/
    485: PETSC_EXTERN PetscErrorCode SNESCreate_NCG(SNES snes)
    486: {
    488:   SNES_NCG       * neP;

    491:   snes->ops->destroy        = SNESDestroy_NCG;
    492:   snes->ops->setup          = SNESSetUp_NCG;
    493:   snes->ops->setfromoptions = SNESSetFromOptions_NCG;
    494:   snes->ops->view           = SNESView_NCG;
    495:   snes->ops->solve          = SNESSolve_NCG;
    496:   snes->ops->reset          = SNESReset_NCG;

    498:   snes->usesksp = PETSC_FALSE;
    499:   snes->usespc  = PETSC_TRUE;

    501:   if (!snes->tolerancesset) {
    502:     snes->max_funcs = 30000;
    503:     snes->max_its   = 10000;
    504:     snes->stol      = 1e-20;
    505:   }

    507:   PetscNewLog(snes, SNES_NCG, &neP);
    508:   snes->data   = (void*) neP;
    509:   neP->monitor = NULL;
    510:   neP->type    = SNES_NCG_PRP;
    511:   PetscObjectComposeFunction((PetscObject)snes,"SNESNCGSetType_C", SNESNCGSetType_NCG);
    512:   return(0);
    513: }