Actual source code: snesncg.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1:  #include <../src/snes/impls/ncg/snesncgimpl.h>
  2: const char *const SNESNCGTypes[] = {"FR","PRP","HS","DY","CD","SNESNCGType","SNES_NCG_",0};

  4: PetscErrorCode SNESReset_NCG(SNES snes)
  5: {
  7:   return(0);
  8: }

 10: #define SNESLINESEARCHNCGLINEAR "ncglinear"

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

 15:   Input Parameter:
 16: . snes - the SNES context

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

 25:   PetscFree(snes->data);
 26:   return(0);
 27: }

 29: /*
 30:    SNESSetUp_NCG - Sets up the internal data structures for the later use
 31:    of the SNESNCG nonlinear solver.

 33:    Input Parameters:
 34: +  snes - the SNES context
 35: -  x - the solution vector

 37:    Application Interface Routine: SNESSetUp()
 38:  */

 40: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch);

 42: PetscErrorCode SNESSetUp_NCG(SNES snes)
 43: {

 47:   SNESSetWorkVecs(snes,2);
 48:   if (snes->npcside== PC_RIGHT) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNESNCG only supports left preconditioning");
 49:   if (snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_UNPRECONDITIONED;
 50:   return(0);
 51: }
 52: /*
 53:   SNESSetFromOptions_NCG - Sets various parameters for the SNESNCG method.

 55:   Input Parameter:
 56: . snes - the SNES context

 58:   Application Interface Routine: SNESSetFromOptions()
 59: */
 60: static PetscErrorCode SNESSetFromOptions_NCG(PetscOptionItems *PetscOptionsObject,SNES snes)
 61: {
 62:   SNES_NCG       *ncg = (SNES_NCG*)snes->data;
 64:   PetscBool      debug = PETSC_FALSE;
 65:   SNESLineSearch linesearch;
 66:   SNESNCGType    ncgtype=ncg->type;

 69:   PetscOptionsHead(PetscOptionsObject,"SNES NCG options");
 70:   PetscOptionsBool("-snes_ncg_monitor","Monitor NCG iterations","SNES",ncg->monitor ? PETSC_TRUE : PETSC_FALSE, &debug, NULL);
 71:   if (debug) {
 72:     ncg->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
 73:   }
 74:   PetscOptionsEnum("-snes_ncg_type","NCG Beta type used","SNESNCGSetType",SNESNCGTypes,(PetscEnum)ncg->type,(PetscEnum*)&ncgtype,NULL);
 75:   SNESNCGSetType(snes, ncgtype);
 76:   PetscOptionsTail();
 77:   if (!snes->linesearch) {
 78:     SNESGetLineSearch(snes, &linesearch);
 79:     if (!snes->npc) {
 80:       SNESLineSearchSetType(linesearch, SNESLINESEARCHCP);
 81:     } else {
 82:       SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
 83:     }
 84:   }
 85:   SNESLineSearchRegister(SNESLINESEARCHNCGLINEAR, SNESLineSearchCreate_NCGLinear);
 86:   return(0);
 87: }

 89: /*
 90:   SNESView_NCG - Prints info from the SNESNCG data structure.

 92:   Input Parameters:
 93: + SNES - the SNES context
 94: - viewer - visualization context

 96:   Application Interface Routine: SNESView()
 97: */
 98: static PetscErrorCode SNESView_NCG(SNES snes, PetscViewer viewer)
 99: {
100:   SNES_NCG      *ncg = (SNES_NCG *) snes->data;
101:   PetscBool      iascii;

105:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
106:   if (iascii) {
107:     PetscViewerASCIIPrintf(viewer, "  type: %s\n", SNESNCGTypes[ncg->type]);
108:   }
109:   return(0);
110: }

112: PetscErrorCode SNESLineSearchApply_NCGLinear(SNESLineSearch linesearch)
113: {
114:   PetscScalar    alpha, ptAp;
115:   Vec            X, Y, F, W;
116:   SNES           snes;
118:   PetscReal      *fnorm, *xnorm, *ynorm;

121:   SNESLineSearchGetSNES(linesearch, &snes);
122:   X     = linesearch->vec_sol;
123:   W     = linesearch->vec_sol_new;
124:   F     = linesearch->vec_func;
125:   Y     = linesearch->vec_update;
126:   fnorm = &linesearch->fnorm;
127:   xnorm = &linesearch->xnorm;
128:   ynorm = &linesearch->ynorm;

130:   if (!snes->jacobian) {
131:     SNESSetUpMatrices(snes);
132:   }

134:   /*

136:    The exact step size for unpreconditioned linear CG is just:
137:    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy)
138:    */
139:   SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre);
140:   VecDot(F, F, &alpha);
141:   MatMult(snes->jacobian, Y, W);
142:   VecDot(Y, W, &ptAp);
143:   alpha = alpha / ptAp;
144:   VecAXPY(X,-alpha,Y);
145:   SNESComputeFunction(snes,X,F);

147:   VecNorm(F,NORM_2,fnorm);
148:   VecNorm(X,NORM_2,xnorm);
149:   VecNorm(Y,NORM_2,ynorm);
150:   return(0);
151: }

153: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch linesearch)
154: {
156:   linesearch->ops->apply          = SNESLineSearchApply_NCGLinear;
157:   linesearch->ops->destroy        = NULL;
158:   linesearch->ops->setfromoptions = NULL;
159:   linesearch->ops->reset          = NULL;
160:   linesearch->ops->view           = NULL;
161:   linesearch->ops->setup          = NULL;
162:   return(0);
163: }

165: /*

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

169:  */
170: PetscErrorCode SNESNCGComputeYtJtF_Private(SNES snes, Vec X, Vec F, Vec Y, Vec W, Vec G, PetscScalar * ytJtf)
171: {
173:   PetscScalar    ftf, ftg, fty, h;

176:   VecDot(F, F, &ftf);
177:   VecDot(F, Y, &fty);
178:   h      = 1e-5*fty / fty;
179:   VecCopy(X, W);
180:   VecAXPY(W, -h, Y);          /* this is arbitrary */
181:   SNESComputeFunction(snes, W, G);
182:   VecDot(G, F, &ftg);
183:   *ytJtf = (ftg - ftf) / h;
184:   return(0);
185: }

187: /*@
188:     SNESNCGSetType - Sets the conjugate update type for SNESNCG.

190:     Logically Collective on SNES

192:     Input Parameters:
193: +   snes - the iterative context
194: -   btype - update type

196:     Options Database:
197: .   -snes_ncg_type<prp,fr,hs,dy,cd>

199:     Level: intermediate

201:     SNESNCGSelectTypes:
202: +   SNES_NCG_FR - Fletcher-Reeves update
203: .   SNES_NCG_PRP - Polak-Ribiere-Polyak update
204: .   SNES_NCG_HS - Hestenes-Steifel update
205: .   SNES_NCG_DY - Dai-Yuan update
206: -   SNES_NCG_CD - Conjugate Descent update

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

211: .keywords: SNES, SNESNCG, selection, type, set
212: @*/
213: PetscErrorCode SNESNCGSetType(SNES snes, SNESNCGType btype)
214: {

219:   PetscTryMethod(snes,"SNESNCGSetType_C",(SNES,SNESNCGType),(snes,btype));
220:   return(0);
221: }

223: PetscErrorCode SNESNCGSetType_NCG(SNES snes, SNESNCGType btype)
224: {
225:   SNES_NCG *ncg = (SNES_NCG*)snes->data;

228:   ncg->type = btype;
229:   return(0);
230: }

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

235:   Input Parameters:
236: . snes - the SNES context

238:   Output Parameter:
239: . outits - number of iterations until termination

241:   Application Interface Routine: SNESSolve()
242: */
243: PetscErrorCode SNESSolve_NCG(SNES snes)
244: {
245:   SNES_NCG             *ncg = (SNES_NCG*)snes->data;
246:   Vec                  X,dX,lX,F,dXold;
247:   PetscReal            fnorm, ynorm, xnorm, beta = 0.0;
248:   PetscScalar          dXdotdX, dXolddotdXold, dXdotdXold, lXdotdX, lXdotdXold;
249:   PetscInt             maxits, i;
250:   PetscErrorCode       ierr;
251:   SNESLineSearchReason lsresult = SNES_LINESEARCH_SUCCEEDED;
252:   SNESLineSearch       linesearch;
253:   SNESConvergedReason  reason;

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

258:   PetscCitationsRegister(SNESCitation,&SNEScite);
259:   snes->reason = SNES_CONVERGED_ITERATING;

261:   maxits = snes->max_its;            /* maximum number of iterations */
262:   X      = snes->vec_sol;            /* X^n */
263:   dXold  = snes->work[0];            /* The previous iterate of X */
264:   dX     = snes->work[1];            /* the preconditioned direction */
265:   lX     = snes->vec_sol_update;     /* the conjugate direction */
266:   F      = snes->vec_func;           /* residual vector */

268:   SNESGetLineSearch(snes, &linesearch);

270:   PetscObjectSAWsTakeAccess((PetscObject)snes);
271:   snes->iter = 0;
272:   snes->norm = 0.;
273:   PetscObjectSAWsGrantAccess((PetscObject)snes);

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

277:   if (snes->npc && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
278:     SNESApplyNPC(snes,X,NULL,dX);
279:     SNESGetConvergedReason(snes->npc,&reason);
280:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
281:       snes->reason = SNES_DIVERGED_INNER;
282:       return(0);
283:     }
284:     VecCopy(dX,F);
285:     VecNorm(F,NORM_2,&fnorm);
286:   } else {
287:     if (!snes->vec_func_init_set) {
288:       SNESComputeFunction(snes,X,F);
289:     } else snes->vec_func_init_set = PETSC_FALSE;

291:     /* convergence test */
292:     VecNorm(F,NORM_2,&fnorm);
293:     SNESCheckFunctionNorm(snes,fnorm);
294:     VecCopy(F,dX);
295:   }
296:   if (snes->npc) {
297:     if (snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
298:       SNESApplyNPC(snes,X,F,dX);
299:       SNESGetConvergedReason(snes->npc,&reason);
300:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
301:         snes->reason = SNES_DIVERGED_INNER;
302:         return(0);
303:       }
304:     }
305:   }
306:   VecCopy(dX,lX);
307:   VecDot(dX, dX, &dXdotdX);


310:   PetscObjectSAWsTakeAccess((PetscObject)snes);
311:   snes->norm = fnorm;
312:   PetscObjectSAWsGrantAccess((PetscObject)snes);
313:   SNESLogConvergenceHistory(snes,fnorm,0);
314:   SNESMonitor(snes,0,fnorm);

316:   /* test convergence */
317:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
318:   if (snes->reason) return(0);

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

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

327:   for (i = 1; i < maxits + 1; i++) {
328:     /* some update types require the old update direction or conjugate direction */
329:     if (ncg->type != SNES_NCG_FR) {
330:       VecCopy(dX, dXold);
331:     }
332:     SNESLineSearchApply(linesearch,X,F,&fnorm,lX);
333:     SNESLineSearchGetReason(linesearch, &lsresult);
334:     SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm);
335:     if (lsresult) {
336:       if (++snes->numFailures >= snes->maxFailures) {
337:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
338:         return(0);
339:       }
340:     }
341:     if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
342:       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
343:       return(0);
344:     }
345:     /* Monitor convergence */
346:     PetscObjectSAWsTakeAccess((PetscObject)snes);
347:     snes->iter = i;
348:     snes->norm = fnorm;
349:     snes->xnorm = xnorm;
350:     snes->ynorm = ynorm;
351:     PetscObjectSAWsGrantAccess((PetscObject)snes);
352:     SNESLogConvergenceHistory(snes,snes->norm,0);
353:     SNESMonitor(snes,snes->iter,snes->norm);

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

359:     /* Call general purpose update function */
360:     if (snes->ops->update) {
361:       (*snes->ops->update)(snes, snes->iter);
362:     }
363:     if (snes->npc) {
364:       if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
365:         SNESApplyNPC(snes,X,NULL,dX);
366:         SNESGetConvergedReason(snes->npc,&reason);
367:         if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
368:           snes->reason = SNES_DIVERGED_INNER;
369:           return(0);
370:         }
371:         VecCopy(dX,F);
372:       } else {
373:         SNESApplyNPC(snes,X,F,dX);
374:         SNESGetConvergedReason(snes->npc,&reason);
375:         if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
376:           snes->reason = SNES_DIVERGED_INNER;
377:           return(0);
378:         }
379:       }
380:     } else {
381:       VecCopy(F,dX);
382:     }

384:     /* compute the conjugate direction lX = dX + beta*lX with beta = ((dX, dX) / (dX_old, dX_old) (Fletcher-Reeves update)*/
385:     switch (ncg->type) {
386:     case SNES_NCG_FR: /* Fletcher-Reeves */
387:       dXolddotdXold= dXdotdX;
388:       VecDot(dX, dX, &dXdotdX);
389:       beta         = PetscRealPart(dXdotdX / dXolddotdXold);
390:       break;
391:     case SNES_NCG_PRP: /* Polak-Ribiere-Poylak */
392:       dXolddotdXold= dXdotdX;
393:       VecDotBegin(dX, dX, &dXdotdXold);
394:       VecDotBegin(dXold, dX, &dXdotdXold);
395:       VecDotEnd(dX, dX, &dXdotdX);
396:       VecDotEnd(dXold, dX, &dXdotdXold);
397:       beta         = PetscRealPart(((dXdotdX - dXdotdXold) / dXolddotdXold));
398:       if (beta < 0.0) beta = 0.0; /* restart */
399:       break;
400:     case SNES_NCG_HS: /* Hestenes-Stiefel */
401:       VecDotBegin(dX, dX, &dXdotdX);
402:       VecDotBegin(dX, dXold, &dXdotdXold);
403:       VecDotBegin(lX, dX, &lXdotdX);
404:       VecDotBegin(lX, dXold, &lXdotdXold);
405:       VecDotEnd(dX, dX, &dXdotdX);
406:       VecDotEnd(dX, dXold, &dXdotdXold);
407:       VecDotEnd(lX, dX, &lXdotdX);
408:       VecDotEnd(lX, dXold, &lXdotdXold);
409:       beta = PetscRealPart((dXdotdX - dXdotdXold) / (lXdotdX - lXdotdXold));
410:       break;
411:     case SNES_NCG_DY: /* Dai-Yuan */
412:       VecDotBegin(dX, dX, &dXdotdX);
413:       VecDotBegin(lX, dX, &lXdotdX);
414:       VecDotBegin(lX, dXold, &lXdotdXold);
415:       VecDotEnd(dX, dX, &dXdotdX);
416:       VecDotEnd(lX, dX, &lXdotdX);
417:       VecDotEnd(lX, dXold, &lXdotdXold);
418:       beta = PetscRealPart(dXdotdX / (lXdotdXold - lXdotdX));
419:       break;
420:     case SNES_NCG_CD: /* Conjugate Descent */
421:       VecDotBegin(dX, dX, &dXdotdX);
422:       VecDotBegin(lX, dXold, &lXdotdXold);
423:       VecDotEnd(dX, dX, &dXdotdX);
424:       VecDotEnd(lX, dXold, &lXdotdXold);
425:       beta = PetscRealPart(dXdotdX / lXdotdXold);
426:       break;
427:     }
428:     if (ncg->monitor) {
429:       PetscViewerASCIIPrintf(ncg->monitor, "beta = %e\n", (double)beta);
430:     }
431:     VecAYPX(lX, beta, dX);
432:   }
433:   PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", maxits);
434:   if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
435:   return(0);
436: }



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

443:   Level: beginner

445:   Options Database:
446: +   -snes_ncg_type <fr, prp, dy, hs, cd> - Choice of conjugate-gradient update parameter, default is prp.
447: .   -snes_linesearch_type <cp,l2,basic> - Line search type.
448: -   -snes_ncg_monitor - Print relevant information about the ncg iteration.

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

455:           Only supports left non-linear preconditioning.

457:    References:
458: .  1. -  Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu,"Composing Scalable Nonlinear Algebraic Solvers",
459:    SIAM Review, 57(4), 2015


462: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR, SNESNGMRES, SNESQN
463: M*/
464: PETSC_EXTERN PetscErrorCode SNESCreate_NCG(SNES snes)
465: {
467:   SNES_NCG       * neP;

470:   snes->ops->destroy        = SNESDestroy_NCG;
471:   snes->ops->setup          = SNESSetUp_NCG;
472:   snes->ops->setfromoptions = SNESSetFromOptions_NCG;
473:   snes->ops->view           = SNESView_NCG;
474:   snes->ops->solve          = SNESSolve_NCG;
475:   snes->ops->reset          = SNESReset_NCG;

477:   snes->usesksp = PETSC_FALSE;
478:   snes->usesnpc = PETSC_TRUE;
479:   snes->npcside = PC_LEFT;

481:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

483:   if (!snes->tolerancesset) {
484:     snes->max_funcs = 30000;
485:     snes->max_its   = 10000;
486:     snes->stol      = 1e-20;
487:   }

489:   PetscNewLog(snes,&neP);
490:   snes->data   = (void*) neP;
491:   neP->monitor = NULL;
492:   neP->type    = SNES_NCG_PRP;
493:   PetscObjectComposeFunction((PetscObject)snes,"SNESNCGSetType_C", SNESNCGSetType_NCG);
494:   return(0);
495: }