Actual source code: linesearchnleqerr.c

  1: #include <petsc/private/linesearchimpl.h>
  2: #include <petsc/private/snesimpl.h>

  4: typedef struct {
  5:   PetscReal norm_delta_x_prev; /* norm of previous update */
  6:   PetscReal norm_bar_delta_x_prev; /* norm of previous bar update */
  7:   PetscReal mu_curr; /* current local Lipschitz estimate */
  8:   PetscReal lambda_prev; /* previous step length: for some reason SNESLineSearchGetLambda returns 1 instead of the previous step length */
  9: } SNESLineSearch_NLEQERR;

 11: static PetscBool NLEQERR_cited = PETSC_FALSE;
 12: static const char NLEQERR_citation[] = "@book{deuflhard2011,\n"
 13:                                "  title = {Newton Methods for Nonlinear Problems},\n"
 14:                                "  author = {Peter Deuflhard},\n"
 15:                                "  volume = 35,\n"
 16:                                "  year = 2011,\n"
 17:                                "  isbn = {978-3-642-23898-7},\n"
 18:                                "  doi  = {10.1007/978-3-642-23899-4},\n"
 19:                                "  publisher = {Springer-Verlag},\n"
 20:                                "  address = {Berlin, Heidelberg}\n}\n";

 22: static PetscErrorCode SNESLineSearchReset_NLEQERR(SNESLineSearch linesearch)
 23: {
 24:   SNESLineSearch_NLEQERR *nleqerr = (SNESLineSearch_NLEQERR*)linesearch->data;

 26:   nleqerr->mu_curr               = 0.0;
 27:   nleqerr->norm_delta_x_prev     = -1.0;
 28:   nleqerr->norm_bar_delta_x_prev = -1.0;
 29:   return 0;
 30: }

 32: static PetscErrorCode  SNESLineSearchApply_NLEQERR(SNESLineSearch linesearch)
 33: {
 34:   PetscBool              changed_y,changed_w;
 35:   Vec                    X,F,Y,W,G;
 36:   SNES                   snes;
 37:   PetscReal              fnorm, xnorm, ynorm, gnorm, wnorm;
 38:   PetscReal              lambda, minlambda, stol;
 39:   PetscViewer            monitor;
 40:   PetscInt               max_its, count, snes_iteration;
 41:   PetscReal              theta, mudash, lambdadash;
 42:   SNESLineSearch_NLEQERR *nleqerr = (SNESLineSearch_NLEQERR*)linesearch->data;
 43:   KSPConvergedReason     kspreason;

 45:   PetscCitationsRegister(NLEQERR_citation, &NLEQERR_cited);

 47:   SNESLineSearchGetVecs(linesearch, &X, &F, &Y, &W, &G);
 48:   SNESLineSearchGetNorms(linesearch, &xnorm, &fnorm, &ynorm);
 49:   SNESLineSearchGetLambda(linesearch, &lambda);
 50:   SNESLineSearchGetSNES(linesearch, &snes);
 51:   SNESLineSearchGetDefaultMonitor(linesearch, &monitor);
 52:   SNESLineSearchGetTolerances(linesearch,&minlambda,NULL,NULL,NULL,NULL,&max_its);
 53:   SNESGetTolerances(snes,NULL,NULL,&stol,NULL,NULL);

 55:   /* reset the state of the Lipschitz estimates */
 56:   SNESGetIterationNumber(snes, &snes_iteration);
 57:   if (!snes_iteration) {
 58:     SNESLineSearchReset_NLEQERR(linesearch);
 59:   }

 61:   /* precheck */
 62:   SNESLineSearchPreCheck(linesearch,X,Y,&changed_y);
 63:   SNESLineSearchSetReason(linesearch, SNES_LINESEARCH_SUCCEEDED);

 65:   VecNormBegin(Y, NORM_2, &ynorm);
 66:   VecNormBegin(X, NORM_2, &xnorm);
 67:   VecNormEnd(Y, NORM_2, &ynorm);
 68:   VecNormEnd(X, NORM_2, &xnorm);

 70:   /* Note: Y is *minus* the Newton step. For whatever reason PETSc doesn't solve with the minus on  the RHS. */

 72:   if (ynorm == 0.0) {
 73:     if (monitor) {
 74:       PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
 75:       PetscViewerASCIIPrintf(monitor,"    Line search: Initial direction and size is 0\n");
 76:       PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
 77:     }
 78:     VecCopy(X,W);
 79:     VecCopy(F,G);
 80:     SNESLineSearchSetNorms(linesearch,xnorm,fnorm,ynorm);
 81:     SNESLineSearchSetReason(linesearch, SNES_LINESEARCH_FAILED_REDUCT);
 82:     return 0;
 83:   }

 85:   /* At this point, we've solved the Newton system for delta_x, and we assume that
 86:      its norm is greater than the solution tolerance (otherwise we wouldn't be in
 87:      here). So let's go ahead and estimate the Lipschitz constant.

 89:      W contains bar_delta_x_prev at this point. */

 91:   if (monitor) {
 92:     PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
 93:     PetscViewerASCIIPrintf(monitor,"    Line search: norm of Newton step: %14.12e\n", (double) ynorm);
 94:     PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
 95:   }

 97:   /* this needs information from a previous iteration, so can't do it on the first one */
 98:   if (nleqerr->norm_delta_x_prev > 0 && nleqerr->norm_bar_delta_x_prev > 0) {
 99:     VecWAXPY(G, +1.0, Y, W); /* bar_delta_x - delta_x; +1 because Y is -delta_x */
100:     VecNormBegin(G, NORM_2, &gnorm);
101:     VecNormEnd(G, NORM_2, &gnorm);

103:     nleqerr->mu_curr = nleqerr->lambda_prev * (nleqerr->norm_delta_x_prev * nleqerr->norm_bar_delta_x_prev) / (gnorm * ynorm);
104:     lambda = PetscMin(1.0, nleqerr->mu_curr);

106:     if (monitor) {
107:       PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
108:       PetscViewerASCIIPrintf(monitor,"    Line search: Lipschitz estimate: %14.12e; lambda: %14.12e\n", (double) nleqerr->mu_curr, (double) lambda);
109:       PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
110:     }
111:   } else {
112:     lambda = linesearch->damping;
113:   }

115:   /* The main while loop of the algorithm.
116:      At the end of this while loop, G should have the accepted new X in it. */

118:   count = 0;
119:   while (PETSC_TRUE) {
120:     if (monitor) {
121:       PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
122:       PetscViewerASCIIPrintf(monitor,"    Line search: entering iteration with lambda: %14.12e\n", lambda);
123:       PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
124:     }

126:     /* Check that we haven't performed too many iterations */
127:     count += 1;
128:     if (count >= max_its) {
129:       if (monitor) {
130:         PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
131:         PetscViewerASCIIPrintf(monitor,"    Line search: maximum iterations reached\n");
132:         PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
133:       }
134:       SNESLineSearchSetReason(linesearch, SNES_LINESEARCH_FAILED_REDUCT);
135:       return 0;
136:     }

138:     /* Now comes the Regularity Test. */
139:     if (lambda <= minlambda) {
140:       /* This isn't what is suggested by Deuflhard, but it works better in my experience */
141:       if (monitor) {
142:         PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
143:         PetscViewerASCIIPrintf(monitor,"    Line search: lambda has reached lambdamin, taking full Newton step\n");
144:         PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
145:       }
146:       lambda = 1.0;
147:       VecWAXPY(G, -lambda, Y, X);

149:       /* and clean up the state for next time */
150:       SNESLineSearchReset_NLEQERR(linesearch);
151:       /*
152:          The clang static analyzer detected a problem here; once the loop is broken the values
153:          nleqerr->norm_delta_x_prev     = ynorm;
154:          nleqerr->norm_bar_delta_x_prev = wnorm;
155:          are set, but wnorm has not even been computed.
156:          I don't know if this is the correct fix but by setting ynorm and wnorm to -1.0 at
157:          least the linesearch object is kept in the state set by the SNESLineSearchReset_NLEQERR() call above
158:       */
159:       ynorm = wnorm = -1.0;
160:       break;
161:     }

163:     /* Compute new trial iterate */
164:     VecWAXPY(W, -lambda, Y, X);
165:     SNESComputeFunction(snes, W, G);

167:     /* Solve linear system for bar_delta_x_curr: old Jacobian, new RHS. Note absence of minus sign, compared to Deuflhard, in keeping with PETSc convention */
168:     KSPSolve(snes->ksp, G, W);
169:     KSPGetConvergedReason(snes->ksp, &kspreason);
170:     if (kspreason < 0) {
171:       PetscInfo(snes,"Solution for \\bar{delta x}^{k+1} failed.");
172:     }

174:     /* W now contains -bar_delta_x_curr. */

176:     VecNorm(W, NORM_2, &wnorm);
177:     if (monitor) {
178:       PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
179:       PetscViewerASCIIPrintf(monitor,"    Line search: norm of simplified Newton update: %14.12e\n", (double) wnorm);
180:       PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
181:     }

183:     /* compute the monitoring quantities theta and mudash. */

185:     theta = wnorm / ynorm;

187:     VecWAXPY(G, -(1.0 - lambda), Y, W);
188:     VecNorm(G, NORM_2, &gnorm);

190:     mudash = (0.5 * ynorm * lambda * lambda) / gnorm;

192:     /* Check for termination of the linesearch */
193:     if (theta >= 1.0) {
194:       /* need to go around again with smaller lambda */
195:       if (monitor) {
196:         PetscViewerASCIIAddTab(monitor,((PetscObject)linesearch)->tablevel);
197:         PetscViewerASCIIPrintf(monitor,"    Line search: monotonicity check failed, ratio: %14.12e\n", (double) theta);
198:         PetscViewerASCIISubtractTab(monitor,((PetscObject)linesearch)->tablevel);
199:       }
200:       lambda = PetscMin(mudash, 0.5 * lambda);
201:       lambda = PetscMax(lambda, minlambda);
202:       /* continue through the loop, i.e. go back to regularity test */
203:     } else {
204:       /* linesearch terminated */
205:       lambdadash = PetscMin(1.0, mudash);

207:       if (lambdadash == 1.0 && lambda == 1.0 && wnorm <= stol) {
208:         /* store the updated state, X - Y - W, in G:
209:            I need to keep W for the next linesearch */
210:         VecCopy(X, G);
211:         VecAXPY(G, -1.0, Y);
212:         VecAXPY(G, -1.0, W);
213:         break;
214:       }

216:       /* Deuflhard suggests to add the following:
217:       else if (lambdadash >= 4.0 * lambda) {
218:         lambda = lambdadash;
219:       }
220:       to continue through the loop, i.e. go back to regularity test.
221:       I deliberately exclude this, as I have practical experience of this
222:       getting stuck in infinite loops (on e.g. an Allen--Cahn problem). */

224:       else {
225:         /* accept iterate without adding on, i.e. don't use bar_delta_x;
226:            again, I need to keep W for the next linesearch */
227:         VecWAXPY(G, -lambda, Y, X);
228:         break;
229:       }
230:     }
231:   }

233:   if (linesearch->ops->viproject) {
234:     (*linesearch->ops->viproject)(snes, G);
235:   }

237:   /* W currently contains -bar_delta_u. Scale it so that it contains bar_delta_u. */
238:   VecScale(W, -1.0);

240:   /* postcheck */
241:   SNESLineSearchPostCheck(linesearch,X,Y,G,&changed_y,&changed_w);
242:   if (changed_y || changed_w) {
243:     SNESLineSearchSetReason(linesearch, SNES_LINESEARCH_FAILED_USER);
244:     PetscInfo(snes,"Changing the search direction here doesn't make sense.\n");
245:     return 0;
246:   }

248:   /* copy the solution and information from this iteration over */
249:   nleqerr->norm_delta_x_prev     = ynorm;
250:   nleqerr->norm_bar_delta_x_prev = wnorm;
251:   nleqerr->lambda_prev           = lambda;

253:   VecCopy(G, X);
254:   SNESComputeFunction(snes, X, F);
255:   VecNorm(X, NORM_2, &xnorm);
256:   VecNorm(F, NORM_2, &fnorm);
257:   SNESLineSearchSetLambda(linesearch, lambda);
258:   SNESLineSearchSetNorms(linesearch, xnorm, fnorm, (ynorm < 0 ? PETSC_INFINITY : ynorm));
259:   return 0;
260: }

262: PetscErrorCode SNESLineSearchView_NLEQERR(SNESLineSearch linesearch, PetscViewer viewer)
263: {
264:   PetscBool               iascii;
265:   SNESLineSearch_NLEQERR *nleqerr;

267:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
268:   nleqerr   = (SNESLineSearch_NLEQERR*)linesearch->data;
269:   if (iascii) {
270:     PetscViewerASCIIPrintf(viewer, "  NLEQ-ERR affine-covariant linesearch");
271:     PetscViewerASCIIPrintf(viewer, "  current local Lipschitz estimate omega=%e\n", (double)nleqerr->mu_curr);
272:   }
273:   return 0;
274: }

276: static PetscErrorCode SNESLineSearchDestroy_NLEQERR(SNESLineSearch linesearch)
277: {
278:   PetscFree(linesearch->data);
279:   return 0;
280: }

282: /*MC
283:    SNESLINESEARCHNLEQERR - Error-oriented affine-covariant globalised Newton algorithm of Deuflhard (2011).

285:    This linesearch is intended for Newton-type methods which are affine covariant. Affine covariance
286:    means that Newton's method will give the same iterations for F(x) = 0 and AF(x) = 0 for a nonsingular
287:    matrix A. This is a fundamental property; the philosophy of this linesearch is that globalisations
288:    of Newton's method should carefully preserve it.

290:    For a discussion of the theory behind this algorithm, see

292:    @book{deuflhard2011,
293:      title={Newton Methods for Nonlinear Problems},
294:      author={Deuflhard, P.},
295:      volume={35},
296:      year={2011},
297:      publisher={Springer-Verlag},
298:      address={Berlin, Heidelberg}
299:    }

301:    Pseudocode is given on page 148.

303:    Options Database Keys:
304: +  -snes_linesearch_damping<1.0> - initial step length
305: -  -snes_linesearch_minlambda<1e-12> - minimum step length allowed

307:    Contributed by Patrick Farrell <patrick.farrell@maths.ox.ac.uk>

309:    Level: advanced

311: .seealso: SNESLineSearchCreate(), SNESLineSearchSetType()
312: M*/
313: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_NLEQERR(SNESLineSearch linesearch)
314: {
315:   SNESLineSearch_NLEQERR *nleqerr;

317:   linesearch->ops->apply          = SNESLineSearchApply_NLEQERR;
318:   linesearch->ops->destroy        = SNESLineSearchDestroy_NLEQERR;
319:   linesearch->ops->setfromoptions = NULL;
320:   linesearch->ops->reset          = SNESLineSearchReset_NLEQERR;
321:   linesearch->ops->view           = SNESLineSearchView_NLEQERR;
322:   linesearch->ops->setup          = NULL;

324:   PetscNewLog(linesearch,&nleqerr);

326:   linesearch->data    = (void*)nleqerr;
327:   linesearch->max_its = 40;
328:   SNESLineSearchReset_NLEQERR(linesearch);
329:   return 0;
330: }