Actual source code: snesncg.c

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

  4: static PetscErrorCode SNESDestroy_NCG(SNES snes)
  5: {
  6:   PetscFunctionBegin;
  7:   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESNCGSetType_C", NULL));
  8:   PetscCall(PetscFree(snes->data));
  9:   PetscFunctionReturn(PETSC_SUCCESS);
 10: }

 12: static PetscErrorCode SNESSetUp_NCG(SNES snes)
 13: {
 14:   PetscFunctionBegin;
 15:   PetscCall(SNESSetWorkVecs(snes, 2));
 16:   PetscCheck(snes->npcside != PC_RIGHT, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONGSTATE, "SNESNCG only supports left preconditioning");
 17:   if (snes->functype == SNES_FUNCTION_DEFAULT) snes->functype = SNES_FUNCTION_UNPRECONDITIONED;
 18:   PetscFunctionReturn(PETSC_SUCCESS);
 19: }

 21: static PetscErrorCode SNESLineSearchApply_NCGLinear(SNESLineSearch linesearch)
 22: {
 23:   PetscScalar alpha, ptAp;
 24:   Vec         X, Y, F, W;
 25:   SNES        snes;
 26:   PetscReal  *fnorm, *xnorm, *ynorm;

 28:   PetscFunctionBegin;
 29:   PetscCall(SNESLineSearchGetSNES(linesearch, &snes));
 30:   X     = linesearch->vec_sol;
 31:   W     = linesearch->vec_sol_new;
 32:   F     = linesearch->vec_func;
 33:   Y     = linesearch->vec_update;
 34:   fnorm = &linesearch->fnorm;
 35:   xnorm = &linesearch->xnorm;
 36:   ynorm = &linesearch->ynorm;

 38:   if (!snes->jacobian) PetscCall(SNESSetUpMatrices(snes));

 40:   /*
 41:    The exact step size for unpreconditioned linear CG is just:
 42:    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy)
 43:    */
 44:   PetscCall(SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre));
 45:   SNESLineSearchCheckJacobianDomainError(snes, linesearch);

 47:   PetscCall(VecDot(F, F, &alpha));
 48:   PetscCall(MatMult(snes->jacobian, Y, W));
 49:   PetscCall(VecDot(Y, W, &ptAp));
 50:   alpha = alpha / ptAp;
 51:   PetscCall(VecAXPY(X, -alpha, Y));
 52:   PetscCall(SNESComputeFunction(snes, X, F));

 54:   PetscCall(VecNorm(F, NORM_2, fnorm));
 55:   SNESLineSearchCheckFunctionDomainError(snes, linesearch, *fnorm);
 56:   PetscCall(VecNorm(X, NORM_2, xnorm));
 57:   PetscCall(VecNorm(Y, NORM_2, ynorm));
 58:   PetscFunctionReturn(PETSC_SUCCESS);
 59: }

 61: /*MC
 62:    SNESLINESEARCHNCGLINEAR - Special line search only for the nonlinear CG solver `SNESNCG`

 64:    This line search uses the length "as if" the problem is linear (that is what is computed by the linear CG method) using the Jacobian of the function.
 65:    alpha = (r, r) / (p, Ap) = (f, f) / (y, Jy) where r (f) is the current residual (function value), p (y) is the current search direction.

 67:    Level: advanced

 69:    Notes:
 70:    This requires a Jacobian-vector product but does not require the solution of a linear system with the Jacobian

 72:    This is a "odd-ball" line search, we don't know if it is in the literature or used in practice by anyone.

 74: .seealso: [](ch_snes), `SNES`, `SNESNCG`, `SNESLineSearchCreate()`, `SNESLineSearchSetType()`
 75: M*/

 77: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NCGLinear(SNESLineSearch linesearch)
 78: {
 79:   PetscFunctionBegin;
 80:   linesearch->ops->apply = SNESLineSearchApply_NCGLinear;
 81:   linesearch->ops->reset = NULL;
 82:   linesearch->ops->setup = NULL;
 83:   PetscFunctionReturn(PETSC_SUCCESS);
 84: }

 86: static PetscErrorCode SNESSetFromOptions_NCG(SNES snes, PetscOptionItems PetscOptionsObject)
 87: {
 88:   SNES_NCG      *ncg     = (SNES_NCG *)snes->data;
 89:   PetscBool      debug   = PETSC_FALSE;
 90:   SNESNCGType    ncgtype = ncg->type;
 91:   SNESLineSearch linesearch;

 93:   PetscFunctionBegin;
 94:   PetscOptionsHeadBegin(PetscOptionsObject, "SNES NCG options");
 95:   PetscCall(PetscOptionsBool("-snes_ncg_monitor", "Monitor the beta values used in the NCG iterations", "SNES", ncg->monitor ? PETSC_TRUE : PETSC_FALSE, &debug, NULL));
 96:   if (debug) ncg->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
 97:   PetscCall(PetscOptionsEnum("-snes_ncg_type", "NCG Beta type used", "SNESNCGSetType", SNESNCGTypes, (PetscEnum)ncg->type, (PetscEnum *)&ncgtype, NULL));
 98:   PetscCall(SNESNCGSetType(snes, ncgtype));
 99:   PetscOptionsHeadEnd();
100:   if (!snes->linesearch) {
101:     PetscCall(SNESGetLineSearch(snes, &linesearch));
102:     if (!((PetscObject)linesearch)->type_name) {
103:       if (!snes->npc) {
104:         PetscCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHCP));
105:       } else {
106:         PetscCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHSECANT));
107:       }
108:     }
109:   }
110:   PetscFunctionReturn(PETSC_SUCCESS);
111: }

113: static PetscErrorCode SNESView_NCG(SNES snes, PetscViewer viewer)
114: {
115:   SNES_NCG *ncg = (SNES_NCG *)snes->data;
116:   PetscBool isascii;

118:   PetscFunctionBegin;
119:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
120:   if (isascii) PetscCall(PetscViewerASCIIPrintf(viewer, "  type: %s\n", SNESNCGTypes[ncg->type]));
121:   PetscFunctionReturn(PETSC_SUCCESS);
122: }

124: /*@
125:   SNESNCGSetType - Sets the conjugate update type for nonlinear CG `SNESNCG`.

127:   Logically Collective

129:   Input Parameters:
130: + snes  - the iterative context
131: - btype - update type, see `SNESNCGType`

133:   Options Database Key:
134: . -snes_ncg_type (prp|fr|hs|dy|cd) - strategy for selecting algorithm for computing beta

136:   Level: intermediate

138:   Notes:
139:   `SNES_NCG_PRP` is the default, and the only one that tolerates generalized search directions.

141:   It is not clear what "generalized search directions" means, does it mean use with a nonlinear preconditioner,
142:   that is using -npc_snes_type <type>, `SNESSetNPC()`, or `SNESGetNPC()`?

144: .seealso: [](ch_snes), `SNES`, `SNESNCG`, `SNESNCGType`, `SNES_NCG_FR`, `SNES_NCG_PRP`, `SNES_NCG_HS`, `SNES_NCG_DY`, `SNES_NCG_CD`
145: @*/
146: PetscErrorCode SNESNCGSetType(SNES snes, SNESNCGType btype)
147: {
148:   PetscFunctionBegin;
150:   PetscTryMethod(snes, "SNESNCGSetType_C", (SNES, SNESNCGType), (snes, btype));
151:   PetscFunctionReturn(PETSC_SUCCESS);
152: }

154: static PetscErrorCode SNESNCGSetType_NCG(SNES snes, SNESNCGType btype)
155: {
156:   SNES_NCG *ncg = (SNES_NCG *)snes->data;

158:   PetscFunctionBegin;
159:   ncg->type = btype;
160:   PetscFunctionReturn(PETSC_SUCCESS);
161: }

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

166:   Input Parameter:
167: . snes - the `SNES` context

169:   Output Parameter:
170: . outits - number of iterations until termination

172:   Application Interface Routine: SNESSolve()
173: */
174: static PetscErrorCode SNESSolve_NCG(SNES snes)
175: {
176:   SNES_NCG           *ncg = (SNES_NCG *)snes->data;
177:   Vec                 X, dX, lX, F, dXold;
178:   PetscReal           fnorm, ynorm, xnorm, beta = 0.0;
179:   PetscScalar         dXdotdX, dXolddotdXold, dXdotdXold, lXdotdX, lXdotdXold;
180:   PetscInt            maxits;
181:   SNESLineSearch      linesearch;
182:   SNESConvergedReason reason;

184:   PetscFunctionBegin;
185:   PetscCheck(!snes->xl && !snes->xu && !snes->ops->computevariablebounds, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

187:   PetscCall(PetscCitationsRegister(SNESCitation, &SNEScite));
188:   snes->reason = SNES_CONVERGED_ITERATING;

190:   maxits = snes->max_its;        /* maximum number of iterations */
191:   X      = snes->vec_sol;        /* X^n */
192:   dXold  = snes->work[0];        /* The previous iterate of X */
193:   dX     = snes->work[1];        /* the preconditioned direction */
194:   lX     = snes->vec_sol_update; /* the conjugate direction */
195:   F      = snes->vec_func;       /* residual vector */

197:   PetscCall(SNESGetLineSearch(snes, &linesearch));

199:   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
200:   snes->iter = 0;
201:   snes->norm = 0.;
202:   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));

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

206:   if (snes->npc && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
207:     PetscCall(SNESApplyNPC(snes, X, NULL, dX));
208:     PetscCall(SNESGetConvergedReason(snes->npc, &reason));
209:     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
210:       snes->reason = SNES_DIVERGED_INNER;
211:       PetscFunctionReturn(PETSC_SUCCESS);
212:     }
213:     PetscCall(VecCopy(dX, F));
214:     PetscCall(VecNorm(F, NORM_2, &fnorm));
215:   } else {
216:     if (!snes->vec_func_init_set) PetscCall(SNESComputeFunction(snes, X, F));
217:     else snes->vec_func_init_set = PETSC_FALSE;

219:     /* convergence test */
220:     PetscCall(VecNorm(F, NORM_2, &fnorm));
221:     SNESCheckFunctionDomainError(snes, fnorm);
222:     PetscCall(VecCopy(F, dX));
223:   }
224:   if (snes->npc) {
225:     if (snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
226:       PetscCall(SNESApplyNPC(snes, X, F, dX));
227:       PetscCall(SNESGetConvergedReason(snes->npc, &reason));
228:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
229:         snes->reason = SNES_DIVERGED_INNER;
230:         PetscFunctionReturn(PETSC_SUCCESS);
231:       }
232:     }
233:   }
234:   PetscCall(VecCopy(dX, lX));
235:   PetscCall(VecDot(dX, dX, &dXdotdX));

237:   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
238:   snes->norm = fnorm;
239:   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
240:   PetscCall(SNESLogConvergenceHistory(snes, fnorm, 0));

242:   /* test convergence */
243:   PetscCall(SNESConverged(snes, 0, 0.0, 0.0, fnorm));
244:   PetscCall(SNESMonitor(snes, 0, fnorm));
245:   if (snes->reason) PetscFunctionReturn(PETSC_SUCCESS);

247:   /* Call general purpose update function */
248:   PetscTryTypeMethod(snes, update, snes->iter);

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

252:   for (PetscInt i = 1; i < maxits + 1; i++) {
253:     /* some update types require the old update direction or conjugate direction */
254:     if (ncg->type != SNES_NCG_FR) PetscCall(VecCopy(dX, dXold));
255:     PetscCall(SNESLineSearchApply(linesearch, X, F, &fnorm, lX));
256:     if (snes->reason) PetscFunctionReturn(PETSC_SUCCESS);
257:     PetscCall(SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm));
258:     SNESCheckLineSearchFailure(snes, snes->linesearch);
259:     if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
260:       snes->reason = SNES_DIVERGED_FUNCTION_COUNT;
261:       PetscFunctionReturn(PETSC_SUCCESS);
262:     }
263:     /* Monitor convergence */
264:     PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
265:     snes->iter  = i;
266:     snes->norm  = fnorm;
267:     snes->xnorm = xnorm;
268:     snes->ynorm = ynorm;
269:     PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
270:     PetscCall(SNESLogConvergenceHistory(snes, snes->norm, 0));

272:     /* Test for convergence */
273:     PetscCall(SNESConverged(snes, snes->iter, xnorm, ynorm, fnorm));
274:     PetscCall(SNESMonitor(snes, snes->iter, snes->norm));
275:     if (snes->reason) PetscFunctionReturn(PETSC_SUCCESS);

277:     /* Call general purpose update function */
278:     PetscTryTypeMethod(snes, update, snes->iter);
279:     if (snes->npc) {
280:       if (snes->functype == SNES_FUNCTION_PRECONDITIONED) {
281:         PetscCall(SNESApplyNPC(snes, X, NULL, dX));
282:         PetscCall(SNESGetConvergedReason(snes->npc, &reason));
283:         if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
284:           snes->reason = SNES_DIVERGED_INNER;
285:           PetscFunctionReturn(PETSC_SUCCESS);
286:         }
287:         PetscCall(VecCopy(dX, F));
288:       } else {
289:         PetscCall(SNESApplyNPC(snes, X, F, dX));
290:         PetscCall(SNESGetConvergedReason(snes->npc, &reason));
291:         if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
292:           snes->reason = SNES_DIVERGED_INNER;
293:           PetscFunctionReturn(PETSC_SUCCESS);
294:         }
295:       }
296:     } else {
297:       PetscCall(VecCopy(F, dX));
298:     }

300:     /* compute the conjugate direction lX = dX + beta*lX with beta = ((dX, dX) / (dX_old, dX_old) (Fletcher-Reeves update)*/
301:     switch (ncg->type) {
302:     case SNES_NCG_FR: /* Fletcher-Reeves */
303:       dXolddotdXold = dXdotdX;
304:       PetscCall(VecDot(dX, dX, &dXdotdX));
305:       beta = PetscRealPart(dXdotdX / dXolddotdXold);
306:       break;
307:     case SNES_NCG_PRP: /* Polak-Ribiere-Poylak */
308:       dXolddotdXold = dXdotdX;
309:       PetscCall(VecDotBegin(dX, dX, &dXdotdX));
310:       PetscCall(VecDotBegin(dXold, dX, &dXdotdXold));
311:       PetscCall(VecDotEnd(dX, dX, &dXdotdX));
312:       PetscCall(VecDotEnd(dXold, dX, &dXdotdXold));
313:       beta = PetscRealPart((dXdotdX - dXdotdXold) / dXolddotdXold);
314:       if (beta < 0.0) beta = 0.0; /* restart */
315:       break;
316:     case SNES_NCG_HS: /* Hestenes-Stiefel */
317:       PetscCall(VecDotBegin(dX, dX, &dXdotdX));
318:       PetscCall(VecDotBegin(dX, dXold, &dXdotdXold));
319:       PetscCall(VecDotBegin(lX, dX, &lXdotdX));
320:       PetscCall(VecDotBegin(lX, dXold, &lXdotdXold));
321:       PetscCall(VecDotEnd(dX, dX, &dXdotdX));
322:       PetscCall(VecDotEnd(dX, dXold, &dXdotdXold));
323:       PetscCall(VecDotEnd(lX, dX, &lXdotdX));
324:       PetscCall(VecDotEnd(lX, dXold, &lXdotdXold));
325:       beta = PetscRealPart((dXdotdX - dXdotdXold) / (lXdotdX - lXdotdXold));
326:       break;
327:     case SNES_NCG_DY: /* Dai-Yuan */
328:       PetscCall(VecDotBegin(dX, dX, &dXdotdX));
329:       PetscCall(VecDotBegin(lX, dX, &lXdotdX));
330:       PetscCall(VecDotBegin(lX, dXold, &lXdotdXold));
331:       PetscCall(VecDotEnd(dX, dX, &dXdotdX));
332:       PetscCall(VecDotEnd(lX, dX, &lXdotdX));
333:       PetscCall(VecDotEnd(lX, dXold, &lXdotdXold));
334:       beta = PetscRealPart(dXdotdX / (lXdotdXold - lXdotdX));
335:       break;
336:     case SNES_NCG_CD: /* Conjugate Descent */
337:       PetscCall(VecDotBegin(dX, dX, &dXdotdX));
338:       PetscCall(VecDotBegin(lX, dXold, &lXdotdXold));
339:       PetscCall(VecDotEnd(dX, dX, &dXdotdX));
340:       PetscCall(VecDotEnd(lX, dXold, &lXdotdXold));
341:       beta = PetscRealPart(dXdotdX / lXdotdXold);
342:       break;
343:     }
344:     if (ncg->monitor) PetscCall(PetscViewerASCIIPrintf(ncg->monitor, "beta = %e\n", (double)beta));
345:     PetscCall(VecAYPX(lX, beta, dX));
346:   }
347:   PetscFunctionReturn(PETSC_SUCCESS);
348: }

350: /*MC
351:   SNESNCG - Nonlinear Conjugate-Gradient method for the solution of nonlinear systems {cite}`bruneknepleysmithtu15`.

353:   Level: beginner

355:   Options Database Keys:
356: +   -snes_ncg_type (fr|prp|dy|hs|cd)                                            - Choice of conjugate-gradient update parameter, default is `prp`.
357: .   -snes_linesearch_type (none|bt|secant|cp|nleqerr|bisection|shell|ncglinear) - Line search type, see `SNESLineSearchType`
358: -   -snes_ncg_monitor (true|false)                                              - Print the beta values nonlinear Conjugate-Gradient used in the  iteration, .

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

365:    Only supports left non-linear preconditioning.

367:    Default line search is `SNESLINESEARCHCP`, unless a nonlinear preconditioner is used with `-npc_snes_type` <type>, `SNESSetNPC()`, or `SNESGetNPC()` then
368:    `SNESLINESEARCHSECANT` is used. Also supports the special-purpose line search `SNESLINESEARCHNCGLINEAR`

370: .seealso: [](ch_snes), `SNES`, `SNESNCG`, `SNESCreate()`, `SNESType`, `SNESSetType()`, `SNESNEWTONLS`, `SNESNEWTONTR`, `SNESNGMRES`, `SNESQN`, `SNESLINESEARCHNCGLINEAR`, `SNESNCGSetType()`, `SNESLineSearchSetType()`
371: M*/
372: PETSC_EXTERN PetscErrorCode SNESCreate_NCG(SNES snes)
373: {
374:   SNES_NCG *neP;

376:   PetscFunctionBegin;
377:   snes->ops->destroy        = SNESDestroy_NCG;
378:   snes->ops->setup          = SNESSetUp_NCG;
379:   snes->ops->setfromoptions = SNESSetFromOptions_NCG;
380:   snes->ops->view           = SNESView_NCG;
381:   snes->ops->solve          = SNESSolve_NCG;

383:   snes->usesksp = PETSC_FALSE;
384:   snes->usesnpc = PETSC_TRUE;
385:   snes->npcside = PC_LEFT;

387:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

389:   PetscObjectParameterSetDefault(snes, max_funcs, 30000);
390:   PetscObjectParameterSetDefault(snes, max_its, 10000);
391:   PetscObjectParameterSetDefault(snes, stol, 1e-20);

393:   PetscCall(PetscNew(&neP));
394:   snes->data = (void *)neP;
395:   neP->type  = SNES_NCG_PRP;
396:   PetscCall(PetscObjectComposeFunction((PetscObject)snes, "SNESNCGSetType_C", SNESNCGSetType_NCG));
397:   PetscFunctionReturn(PETSC_SUCCESS);
398: }