Actual source code: snesncg.c

petsc-3.6.1 2015-07-22
Report Typos and Errors
  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 "ncglinear"

 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:   if (snes->pcside == PC_RIGHT) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNESNCG only supports left preconditioning");
 55:   if (snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_UNPRECONDITIONED;
 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(PetscOptions *PetscOptionsObject,SNES snes)
 69: {
 70:   SNES_NCG       *ncg = (SNES_NCG*)snes->data;
 72:   PetscBool      debug = PETSC_FALSE;
 73:   SNESLineSearch linesearch;
 74:   SNESNCGType    ncgtype=ncg->type;

 77:   PetscOptionsHead(PetscOptionsObject,"SNES NCG options");
 78:   PetscOptionsBool("-snes_ncg_monitor","Monitor NCG iterations","SNES",ncg->monitor ? PETSC_TRUE : PETSC_FALSE, &debug, NULL);
 79:   if (debug) {
 80:     ncg->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
 81:   }
 82:   PetscOptionsEnum("-snes_ncg_type","NCG Beta type used","SNESNCGSetType",SNESNCGTypes,(PetscEnum)ncg->type,(PetscEnum*)&ncgtype,NULL);
 83:   SNESNCGSetType(snes, ncgtype);
 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:   SNESLineSearchRegister(SNESLINESEARCHNCGLINEAR, SNESLineSearchCreate_NCGLinear);
 94:   return(0);
 95: }

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

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

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

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

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

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:   if (!snes->jacobian) {
141:     SNESSetUpMatrices(snes);
142:   }

144:   /*

146:    The exact step size for unpreconditioned linear CG is just:
147:    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy)
148:    */
149:   SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre);
150:   VecDot(F, F, &alpha);
151:   MatMult(snes->jacobian, Y, W);
152:   VecDot(Y, W, &ptAp);
153:   alpha = alpha / ptAp;
154:   VecAXPY(X,-alpha,Y);
155:   SNESComputeFunction(snes,X,F);

157:   VecNorm(F,NORM_2,fnorm);
158:   VecNorm(X,NORM_2,xnorm);
159:   VecNorm(Y,NORM_2,ynorm);
160:   return(0);
161: }

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

179: /*

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

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

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

203: /*@
204:     SNESNCGSetType - Sets the conjugate update type for SNESNCG.

206:     Logically Collective on SNES

208:     Input Parameters:
209: +   snes - the iterative context
210: -   btype - update type

212:     Options Database:
213: .   -snes_ncg_type<prp,fr,hs,dy,cd>

215:     Level: intermediate

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

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

227: .keywords: SNES, SNESNCG, selection, type, set
228: @*/
229: PetscErrorCode SNESNCGSetType(SNES snes, SNESNCGType btype)
230: {

235:   PetscTryMethod(snes,"SNESNCGSetType_C",(SNES,SNESNCGType),(snes,btype));
236:   return(0);
237: }

241: PetscErrorCode SNESNCGSetType_NCG(SNES snes, SNESNCGType btype)
242: {
243:   SNES_NCG *ncg = (SNES_NCG*)snes->data;

246:   ncg->type = btype;
247:   return(0);
248: }

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

253:   Input Parameters:
254: . snes - the SNES context

256:   Output Parameter:
257: . outits - number of iterations until termination

259:   Application Interface Routine: SNESSolve()
260: */
263: PetscErrorCode SNESSolve_NCG(SNES snes)
264: {
265:   SNES_NCG             *ncg = (SNES_NCG*)snes->data;
266:   Vec                  X,dX,lX,F,dXold;
267:   PetscReal            fnorm, ynorm, xnorm, beta = 0.0;
268:   PetscScalar          dXdotdX, dXolddotdXold, dXdotdXold, lXdotdX, lXdotdXold;
269:   PetscInt             maxits, i;
270:   PetscErrorCode       ierr;
271:   SNESLineSearchReason lsresult = SNES_LINESEARCH_SUCCEEDED;
272:   SNESLineSearch       linesearch;
273:   SNESConvergedReason  reason;


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

281:   PetscCitationsRegister(SNESCitation,&SNEScite);
282:   snes->reason = SNES_CONVERGED_ITERATING;

284:   maxits = snes->max_its;            /* maximum number of iterations */
285:   X      = snes->vec_sol;            /* X^n */
286:   dXold  = snes->work[0];            /* The previous iterate of X */
287:   dX     = snes->work[1];            /* the preconditioned direction */
288:   lX     = snes->vec_sol_update;     /* the conjugate direction */
289:   F      = snes->vec_func;           /* residual vector */

291:   SNESGetLineSearch(snes, &linesearch);

293:   PetscObjectSAWsTakeAccess((PetscObject)snes);
294:   snes->iter = 0;
295:   snes->norm = 0.;
296:   PetscObjectSAWsGrantAccess((PetscObject)snes);

298:   /* compute the initial function and preconditioned update dX */

300:   if (snes->pc && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
301:     SNESApplyNPC(snes,X,NULL,dX);
302:     SNESGetConvergedReason(snes->pc,&reason);
303:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
304:       snes->reason = SNES_DIVERGED_INNER;
305:       return(0);
306:     }
307:     VecCopy(dX,F);
308:     VecNorm(F,NORM_2,&fnorm);
309:   } else {
310:     if (!snes->vec_func_init_set) {
311:       SNESComputeFunction(snes,X,F);
312:     } else snes->vec_func_init_set = PETSC_FALSE;

314:     /* convergence test */
315:     VecNorm(F,NORM_2,&fnorm);
316:     SNESCheckFunctionNorm(snes,fnorm);
317:     VecCopy(F,dX);
318:   }
319:   if (snes->pc) {
320:     if (snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
321:       SNESApplyNPC(snes,X,F,dX);
322:       SNESGetConvergedReason(snes->pc,&reason);
323:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
324:         snes->reason = SNES_DIVERGED_INNER;
325:         return(0);
326:       }
327:     }
328:   }
329:   VecCopy(dX,lX);
330:   VecDot(dX, dX, &dXdotdX);


333:   PetscObjectSAWsTakeAccess((PetscObject)snes);
334:   snes->norm = fnorm;
335:   PetscObjectSAWsGrantAccess((PetscObject)snes);
336:   SNESLogConvergenceHistory(snes,fnorm,0);
337:   SNESMonitor(snes,0,fnorm);

339:   /* test convergence */
340:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
341:   if (snes->reason) return(0);

343:   /* Call general purpose update function */
344:   if (snes->ops->update) {
345:     (*snes->ops->update)(snes, snes->iter);
346:   }

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

350:   for (i = 1; i < maxits + 1; i++) {
351:     /* some update types require the old update direction or conjugate direction */
352:     if (ncg->type != SNES_NCG_FR) {
353:       VecCopy(dX, dXold);
354:     }
355:     SNESLineSearchApply(linesearch,X,F,&fnorm,lX);
356:     SNESLineSearchGetReason(linesearch, &lsresult);
357:     SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm);
358:     if (lsresult) {
359:       if (++snes->numFailures >= snes->maxFailures) {
360:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
361:         return(0);
362:       }
363:     }
364:     if (snes->nfuncs >= snes->max_funcs) {
365:       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
366:       return(0);
367:     }
368:     /* Monitor convergence */
369:     PetscObjectSAWsTakeAccess((PetscObject)snes);
370:     snes->iter = i;
371:     snes->norm = fnorm;
372:     PetscObjectSAWsGrantAccess((PetscObject)snes);
373:     SNESLogConvergenceHistory(snes,snes->norm,0);
374:     SNESMonitor(snes,snes->iter,snes->norm);

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

380:     /* Call general purpose update function */
381:     if (snes->ops->update) {
382:       (*snes->ops->update)(snes, snes->iter);
383:     }
384:     if (snes->pc) {
385:       if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
386:         SNESApplyNPC(snes,X,NULL,dX);
387:         SNESGetConvergedReason(snes->pc,&reason);
388:         if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
389:           snes->reason = SNES_DIVERGED_INNER;
390:           return(0);
391:         }
392:         VecCopy(dX,F);
393:       } else {
394:         SNESApplyNPC(snes,X,F,dX);
395:         SNESGetConvergedReason(snes->pc,&reason);
396:         if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
397:           snes->reason = SNES_DIVERGED_INNER;
398:           return(0);
399:         }
400:       }
401:     } else {
402:       VecCopy(F,dX);
403:     }

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



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

464:   Level: beginner

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

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


476: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESNQN
477: M*/
480: PETSC_EXTERN PetscErrorCode SNESCreate_NCG(SNES snes)
481: {
483:   SNES_NCG       * neP;

486:   snes->ops->destroy        = SNESDestroy_NCG;
487:   snes->ops->setup          = SNESSetUp_NCG;
488:   snes->ops->setfromoptions = SNESSetFromOptions_NCG;
489:   snes->ops->view           = SNESView_NCG;
490:   snes->ops->solve          = SNESSolve_NCG;
491:   snes->ops->reset          = SNESReset_NCG;

493:   snes->usesksp = PETSC_FALSE;
494:   snes->usespc  = PETSC_TRUE;
495:   snes->pcside  = PC_LEFT;

497:   if (!snes->tolerancesset) {
498:     snes->max_funcs = 30000;
499:     snes->max_its   = 10000;
500:     snes->stol      = 1e-20;
501:   }

503:   PetscNewLog(snes,&neP);
504:   snes->data   = (void*) neP;
505:   neP->monitor = NULL;
506:   neP->type    = SNES_NCG_PRP;
507:   PetscObjectComposeFunction((PetscObject)snes,"SNESNCGSetType_C", SNESNCGSetType_NCG);
508:   return(0);
509: }