Actual source code: ntrdc.c
1: #include <../src/snes/impls/ntrdc/ntrdcimpl.h>
3: typedef struct {
4: SNES snes;
5: /* Information on the regular SNES convergence test; which may have been user provided
6: Copied from tr.c (maybe able to disposed, but this is a private function) - Heeho
7: Same with SNESTR_KSPConverged_Private, SNESTR_KSPConverged_Destroy, and SNESTR_Converged_Private
8: */
10: PetscErrorCode (*convtest)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *);
11: PetscErrorCode (*convdestroy)(void *);
12: void *convctx;
13: } SNES_TRDC_KSPConverged_Ctx;
15: static PetscErrorCode SNESTRDC_KSPConverged_Private(KSP ksp, PetscInt n, PetscReal rnorm, KSPConvergedReason *reason, void *cctx)
16: {
17: SNES_TRDC_KSPConverged_Ctx *ctx = (SNES_TRDC_KSPConverged_Ctx *)cctx;
18: SNES snes = ctx->snes;
19: SNES_NEWTONTRDC *neP = (SNES_NEWTONTRDC *)snes->data;
20: Vec x;
21: PetscReal nrm;
23: PetscFunctionBegin;
24: PetscCall((*ctx->convtest)(ksp, n, rnorm, reason, ctx->convctx));
25: if (*reason) PetscCall(PetscInfo(snes, "Default or user provided convergence test KSP iterations=%" PetscInt_FMT ", rnorm=%g\n", n, (double)rnorm));
26: /* Determine norm of solution */
27: PetscCall(KSPBuildSolution(ksp, NULL, &x));
28: PetscCall(VecNorm(x, NORM_2, &nrm));
29: if (nrm >= neP->delta) {
30: PetscCall(PetscInfo(snes, "Ending linear iteration early, delta=%g, length=%g\n", (double)neP->delta, (double)nrm));
31: *reason = KSP_CONVERGED_STEP_LENGTH;
32: }
33: PetscFunctionReturn(PETSC_SUCCESS);
34: }
36: static PetscErrorCode SNESTRDC_KSPConverged_Destroy(void *cctx)
37: {
38: SNES_TRDC_KSPConverged_Ctx *ctx = (SNES_TRDC_KSPConverged_Ctx *)cctx;
40: PetscFunctionBegin;
41: PetscCall((*ctx->convdestroy)(ctx->convctx));
42: PetscCall(PetscFree(ctx));
43: PetscFunctionReturn(PETSC_SUCCESS);
44: }
46: /*
47: SNESTRDC_Converged_Private -test convergence JUST for
48: the trust region tolerance.
50: */
51: static PetscErrorCode SNESTRDC_Converged_Private(SNES snes, PetscInt it, PetscReal xnorm, PetscReal pnorm, PetscReal fnorm, SNESConvergedReason *reason, void *dummy)
52: {
53: SNES_NEWTONTRDC *neP = (SNES_NEWTONTRDC *)snes->data;
55: PetscFunctionBegin;
56: *reason = SNES_CONVERGED_ITERATING;
57: if (neP->delta < xnorm * snes->deltatol) {
58: PetscCall(PetscInfo(snes, "Diverged due to too small a trust region %g<%g*%g\n", (double)neP->delta, (double)xnorm, (double)snes->deltatol));
59: *reason = SNES_DIVERGED_TR_DELTA;
60: } else if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
61: PetscCall(PetscInfo(snes, "Exceeded maximum number of function evaluations: %" PetscInt_FMT "\n", snes->max_funcs));
62: *reason = SNES_DIVERGED_FUNCTION_COUNT;
63: }
64: PetscFunctionReturn(PETSC_SUCCESS);
65: }
67: /*@
68: SNESNewtonTRDCGetRhoFlag - Get whether the current solution update is within the trust-region.
70: Logically Collective
72: Input Parameter:
73: . snes - the nonlinear solver object
75: Output Parameter:
76: . rho_flag - `PETSC_FALSE` or `PETSC_TRUE`
78: Level: developer
80: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCSetPreCheck()`,
81: `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`
82: @*/
83: PetscErrorCode SNESNewtonTRDCGetRhoFlag(SNES snes, PetscBool *rho_flag)
84: {
85: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
87: PetscFunctionBegin;
89: PetscAssertPointer(rho_flag, 2);
90: *rho_flag = tr->rho_satisfied;
91: PetscFunctionReturn(PETSC_SUCCESS);
92: }
94: /*@C
95: SNESNewtonTRDCSetPreCheck - Sets a user function that is called before the search step has been determined.
96: Allows the user a chance to change or override the trust region decision.
98: Logically Collective
100: Input Parameters:
101: + snes - the nonlinear solver object
102: . func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPreCheck()`
103: - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
105: Level: intermediate
107: Note:
108: This function is called BEFORE the function evaluation within the `SNESNEWTONTRDC` solver.
110: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`,
111: `SNESNewtonTRDCGetRhoFlag()`
112: @*/
113: PetscErrorCode SNESNewtonTRDCSetPreCheck(SNES snes, PetscErrorCode (*func)(SNES, Vec, Vec, PetscBool *, void *), void *ctx)
114: {
115: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
117: PetscFunctionBegin;
119: if (func) tr->precheck = func;
120: if (ctx) tr->precheckctx = ctx;
121: PetscFunctionReturn(PETSC_SUCCESS);
122: }
124: /*@C
125: SNESNewtonTRDCGetPreCheck - Gets the pre-check function optionally set with `SNESNewtonTRDCSetPreCheck()`
127: Not Collective
129: Input Parameter:
130: . snes - the nonlinear solver context
132: Output Parameters:
133: + func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPreCheck()`
134: - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
136: Level: intermediate
138: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCPreCheck()`
139: @*/
140: PetscErrorCode SNESNewtonTRDCGetPreCheck(SNES snes, PetscErrorCode (**func)(SNES, Vec, Vec, PetscBool *, void *), void **ctx)
141: {
142: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
144: PetscFunctionBegin;
146: if (func) *func = tr->precheck;
147: if (ctx) *ctx = tr->precheckctx;
148: PetscFunctionReturn(PETSC_SUCCESS);
149: }
151: /*@C
152: SNESNewtonTRDCSetPostCheck - Sets a user function that is called after the search step has been determined but before the next
153: function evaluation. Allows the user a chance to change or override the decision of the line search routine
155: Logically Collective
157: Input Parameters:
158: + snes - the nonlinear solver object
159: . func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPostCheck()`
160: - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
162: Level: intermediate
164: Note:
165: This function is called BEFORE the function evaluation within the `SNESNEWTONTRDC` solver while the function set in
166: `SNESLineSearchSetPostCheck()` is called AFTER the function evaluation.
168: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCPostCheck()`, `SNESNewtonTRDCGetPostCheck()`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCGetPreCheck()`
169: @*/
170: PetscErrorCode SNESNewtonTRDCSetPostCheck(SNES snes, PetscErrorCode (*func)(SNES, Vec, Vec, Vec, PetscBool *, PetscBool *, void *), void *ctx)
171: {
172: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
174: PetscFunctionBegin;
176: if (func) tr->postcheck = func;
177: if (ctx) tr->postcheckctx = ctx;
178: PetscFunctionReturn(PETSC_SUCCESS);
179: }
181: /*@C
182: SNESNewtonTRDCGetPostCheck - Gets the post-check function optionally set with `SNESNewtonTRDCSetPostCheck()`
184: Not Collective
186: Input Parameter:
187: . snes - the nonlinear solver context
189: Output Parameters:
190: + func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPostCheck()`
191: - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
193: Level: intermediate
195: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCPostCheck()`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCGetPreCheck()`
196: @*/
197: PetscErrorCode SNESNewtonTRDCGetPostCheck(SNES snes, PetscErrorCode (**func)(SNES, Vec, Vec, Vec, PetscBool *, PetscBool *, void *), void **ctx)
198: {
199: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
201: PetscFunctionBegin;
203: if (func) *func = tr->postcheck;
204: if (ctx) *ctx = tr->postcheckctx;
205: PetscFunctionReturn(PETSC_SUCCESS);
206: }
208: // PetscClangLinter pragma disable: -fdoc-internal-linkage
209: /*@C
210: SNESNewtonTRDCPreCheck - Called before the step has been determined in `SNESNEWTONTRDC`
212: Logically Collective
214: Input Parameters:
215: + snes - the solver
216: . X - The last solution
217: - Y - The step direction
219: Output Parameter:
220: . changed_Y - Indicator that the step direction `Y` has been changed.
222: Level: developer
224: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCPostCheck()`
225: @*/
226: static PetscErrorCode SNESNewtonTRDCPreCheck(SNES snes, Vec X, Vec Y, PetscBool *changed_Y)
227: {
228: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
230: PetscFunctionBegin;
231: *changed_Y = PETSC_FALSE;
232: if (tr->precheck) {
233: PetscCall((*tr->precheck)(snes, X, Y, changed_Y, tr->precheckctx));
235: }
236: PetscFunctionReturn(PETSC_SUCCESS);
237: }
239: // PetscClangLinter pragma disable: -fdoc-internal-linkage
240: /*@C
241: SNESNewtonTRDCPostCheck - Called after the step has been determined in `SNESNEWTONTRDC` but before the function evaluation at that step
243: Logically Collective
245: Input Parameters:
246: + snes - the solver
247: . X - The last solution
248: . Y - The full step direction
249: - W - The updated solution, W = X - Y
251: Output Parameters:
252: + changed_Y - indicator if step has been changed
253: - changed_W - Indicator if the new candidate solution `W` has been changed.
255: Level: developer
257: Note:
258: If `Y` is changed then `W` is recomputed as `X` - `Y`
260: .seealso: [](ch_snes), `SNES`, `SNESNEWTONTRDC`, `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`, `SNESNewtonTRDCPreCheck()
261: @*/
262: static PetscErrorCode SNESNewtonTRDCPostCheck(SNES snes, Vec X, Vec Y, Vec W, PetscBool *changed_Y, PetscBool *changed_W)
263: {
264: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
266: PetscFunctionBegin;
267: *changed_Y = PETSC_FALSE;
268: *changed_W = PETSC_FALSE;
269: if (tr->postcheck) {
270: PetscCall((*tr->postcheck)(snes, X, Y, W, changed_Y, changed_W, tr->postcheckctx));
273: }
274: PetscFunctionReturn(PETSC_SUCCESS);
275: }
277: /*
278: SNESSolve_NEWTONTRDC - Implements Newton's Method with trust-region subproblem and adds dogleg Cauchy
279: (Steepest Descent direction) step and direction if the trust region is not satisfied for solving system of
280: nonlinear equations
282: */
283: static PetscErrorCode SNESSolve_NEWTONTRDC(SNES snes)
284: {
285: SNES_NEWTONTRDC *neP = (SNES_NEWTONTRDC *)snes->data;
286: Vec X, F, Y, G, W, GradF, YNtmp;
287: Vec YCtmp;
288: Mat jac;
289: PetscInt maxits, i, j, lits, inner_count, bs;
290: PetscReal rho, fnorm, gnorm, xnorm = 0, delta, ynorm, temp_xnorm, temp_ynorm; /* TRDC inner iteration */
291: PetscReal inorms[99]; /* need to make it dynamic eventually, fixed max block size of 99 for now */
292: PetscReal deltaM, ynnorm, f0, mp, gTy, g, yTHy; /* rho calculation */
293: PetscReal auk, gfnorm, ycnorm, c0, c1, c2, tau, tau_pos, tau_neg, gTBg; /* Cauchy Point */
294: KSP ksp;
295: SNESConvergedReason reason = SNES_CONVERGED_ITERATING;
296: PetscBool breakout = PETSC_FALSE;
297: SNES_TRDC_KSPConverged_Ctx *ctx;
298: PetscErrorCode (*convtest)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *), (*convdestroy)(void *);
299: void *convctx;
301: PetscFunctionBegin;
302: maxits = snes->max_its; /* maximum number of iterations */
303: X = snes->vec_sol; /* solution vector */
304: F = snes->vec_func; /* residual vector */
305: Y = snes->work[0]; /* update vector */
306: G = snes->work[1]; /* updated residual */
307: W = snes->work[2]; /* temporary vector */
308: GradF = snes->work[3]; /* grad f = J^T F */
309: YNtmp = snes->work[4]; /* Newton solution */
310: YCtmp = snes->work[5]; /* Cauchy solution */
312: 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);
314: PetscCall(VecGetBlockSize(YNtmp, &bs));
316: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
317: snes->iter = 0;
318: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
320: /* Set the linear stopping criteria to use the More' trick. From tr.c */
321: PetscCall(SNESGetKSP(snes, &ksp));
322: PetscCall(KSPGetConvergenceTest(ksp, &convtest, &convctx, &convdestroy));
323: if (convtest != SNESTRDC_KSPConverged_Private) {
324: PetscCall(PetscNew(&ctx));
325: ctx->snes = snes;
326: PetscCall(KSPGetAndClearConvergenceTest(ksp, &ctx->convtest, &ctx->convctx, &ctx->convdestroy));
327: PetscCall(KSPSetConvergenceTest(ksp, SNESTRDC_KSPConverged_Private, ctx, SNESTRDC_KSPConverged_Destroy));
328: PetscCall(PetscInfo(snes, "Using Krylov convergence test SNESTRDC_KSPConverged_Private\n"));
329: }
331: if (!snes->vec_func_init_set) {
332: PetscCall(SNESComputeFunction(snes, X, F)); /* F(X) */
333: } else snes->vec_func_init_set = PETSC_FALSE;
335: PetscCall(VecNorm(F, NORM_2, &fnorm)); /* fnorm <- || F || */
336: SNESCheckFunctionNorm(snes, fnorm);
337: PetscCall(VecNorm(X, NORM_2, &xnorm)); /* xnorm <- || X || */
339: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
340: snes->norm = fnorm;
341: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
342: delta = xnorm ? neP->delta0 * xnorm : neP->delta0; /* initial trust region size scaled by xnorm */
343: deltaM = xnorm ? neP->deltaM * xnorm : neP->deltaM; /* maximum trust region size scaled by xnorm */
344: neP->delta = delta;
345: PetscCall(SNESLogConvergenceHistory(snes, fnorm, 0));
346: PetscCall(SNESMonitor(snes, 0, fnorm));
348: neP->rho_satisfied = PETSC_FALSE;
350: /* test convergence */
351: PetscUseTypeMethod(snes, converged, snes->iter, 0.0, 0.0, fnorm, &snes->reason, snes->cnvP);
352: if (snes->reason) PetscFunctionReturn(PETSC_SUCCESS);
354: for (i = 0; i < maxits; i++) {
355: PetscBool changed_y;
356: PetscBool changed_w;
358: /* dogleg method */
359: PetscCall(SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre));
360: SNESCheckJacobianDomainerror(snes);
361: PetscCall(KSPSetOperators(snes->ksp, snes->jacobian, snes->jacobian));
362: PetscCall(KSPSolve(snes->ksp, F, YNtmp)); /* Quasi Newton Solution */
363: SNESCheckKSPSolve(snes); /* this is necessary but old tr.c did not have it*/
364: PetscCall(KSPGetIterationNumber(snes->ksp, &lits));
365: PetscCall(SNESGetJacobian(snes, &jac, NULL, NULL, NULL));
367: /* rescale Jacobian, Newton solution update, and re-calculate delta for multiphase (multivariable)
368: for inner iteration and Cauchy direction calculation
369: */
370: if (bs > 1 && neP->auto_scale_multiphase) {
371: PetscCall(VecStrideNormAll(YNtmp, NORM_INFINITY, inorms));
372: for (j = 0; j < bs; j++) {
373: if (neP->auto_scale_max > 1.0) {
374: if (inorms[j] < 1.0 / neP->auto_scale_max) inorms[j] = 1.0 / neP->auto_scale_max;
375: }
376: PetscCall(VecStrideSet(W, j, inorms[j]));
377: PetscCall(VecStrideScale(YNtmp, j, 1.0 / inorms[j]));
378: PetscCall(VecStrideScale(X, j, 1.0 / inorms[j]));
379: }
380: PetscCall(VecNorm(X, NORM_2, &xnorm));
381: if (i == 0) {
382: delta = neP->delta0 * xnorm;
383: } else {
384: delta = neP->delta * xnorm;
385: }
386: deltaM = neP->deltaM * xnorm;
387: PetscCall(MatDiagonalScale(jac, NULL, W));
388: }
390: /* calculating GradF of minimization function */
391: PetscCall(MatMultTranspose(jac, F, GradF)); /* grad f = J^T F */
392: PetscCall(VecNorm(YNtmp, NORM_2, &ynnorm)); /* ynnorm <- || Y_newton || */
394: inner_count = 0;
395: neP->rho_satisfied = PETSC_FALSE;
396: while (1) {
397: if (ynnorm <= delta) { /* see if the Newton solution is within the trust region */
398: PetscCall(VecCopy(YNtmp, Y));
399: } else if (neP->use_cauchy) { /* use Cauchy direction if enabled */
400: PetscCall(MatMult(jac, GradF, W));
401: PetscCall(VecDotRealPart(W, W, &gTBg)); /* completes GradF^T J^T J GradF */
402: PetscCall(VecNorm(GradF, NORM_2, &gfnorm)); /* grad f norm <- || grad f || */
403: if (gTBg <= 0.0) {
404: auk = PETSC_MAX_REAL;
405: } else {
406: auk = PetscSqr(gfnorm) / gTBg;
407: }
408: auk = PetscMin(delta / gfnorm, auk);
409: PetscCall(VecCopy(GradF, YCtmp)); /* this could be improved */
410: PetscCall(VecScale(YCtmp, auk)); /* YCtmp, Cauchy solution*/
411: PetscCall(VecNorm(YCtmp, NORM_2, &ycnorm)); /* ycnorm <- || Y_cauchy || */
412: if (ycnorm >= delta) { /* see if the Cauchy solution meets the criteria */
413: PetscCall(VecCopy(YCtmp, Y));
414: PetscCall(PetscInfo(snes, "DL evaluated. delta: %8.4e, ynnorm: %8.4e, ycnorm: %8.4e\n", (double)delta, (double)ynnorm, (double)ycnorm));
415: } else { /* take ratio, tau, of Cauchy and Newton direction and step */
416: PetscCall(VecAXPY(YNtmp, -1.0, YCtmp)); /* YCtmp = A, YNtmp = B */
417: PetscCall(VecNorm(YNtmp, NORM_2, &c0)); /* this could be improved */
418: c0 = PetscSqr(c0);
419: PetscCall(VecDotRealPart(YCtmp, YNtmp, &c1));
420: c1 = 2.0 * c1;
421: PetscCall(VecNorm(YCtmp, NORM_2, &c2)); /* this could be improved */
422: c2 = PetscSqr(c2) - PetscSqr(delta);
423: tau_pos = (c1 + PetscSqrtReal(PetscSqr(c1) - 4. * c0 * c2)) / (2. * c0); /* quadratic formula */
424: tau_neg = (c1 - PetscSqrtReal(PetscSqr(c1) - 4. * c0 * c2)) / (2. * c0);
425: tau = PetscMax(tau_pos, tau_neg); /* can tau_neg > tau_pos? I don't think so, but just in case. */
426: PetscCall(PetscInfo(snes, "DL evaluated. tau: %8.4e, ynnorm: %8.4e, ycnorm: %8.4e\n", (double)tau, (double)ynnorm, (double)ycnorm));
427: PetscCall(VecWAXPY(W, tau, YNtmp, YCtmp));
428: PetscCall(VecAXPY(W, -tau, YCtmp));
429: PetscCall(VecCopy(W, Y)); /* this could be improved */
430: }
431: } else {
432: /* if Cauchy is disabled, only use Newton direction */
433: auk = delta / ynnorm;
434: PetscCall(VecScale(YNtmp, auk));
435: PetscCall(VecCopy(YNtmp, Y)); /* this could be improved (many VecCopy, VecNorm)*/
436: }
438: PetscCall(VecNorm(Y, NORM_2, &ynorm)); /* compute the final ynorm */
439: f0 = 0.5 * PetscSqr(fnorm); /* minimizing function f(X) */
440: PetscCall(MatMult(jac, Y, W));
441: PetscCall(VecDotRealPart(W, W, &yTHy)); /* completes GradY^T J^T J GradY */
442: PetscCall(VecDotRealPart(GradF, Y, &gTy));
443: mp = f0 - gTy + 0.5 * yTHy; /* quadratic model to satisfy, -gTy because our update is X-Y*/
445: /* scale back solution update */
446: if (bs > 1 && neP->auto_scale_multiphase) {
447: for (j = 0; j < bs; j++) {
448: PetscCall(VecStrideScale(Y, j, inorms[j]));
449: if (inner_count == 0) {
450: /* TRDC inner algorithm does not need scaled X after calculating delta in the outer iteration */
451: /* need to scale back X to match Y and provide proper update to the external code */
452: PetscCall(VecStrideScale(X, j, inorms[j]));
453: }
454: }
455: if (inner_count == 0) PetscCall(VecNorm(X, NORM_2, &temp_xnorm)); /* only in the first iteration */
456: PetscCall(VecNorm(Y, NORM_2, &temp_ynorm));
457: } else {
458: temp_xnorm = xnorm;
459: temp_ynorm = ynorm;
460: }
461: inner_count++;
463: /* Evaluate the solution to meet the improvement ratio criteria */
464: PetscCall(SNESNewtonTRDCPreCheck(snes, X, Y, &changed_y));
465: PetscCall(VecWAXPY(W, -1.0, Y, X));
466: PetscCall(SNESNewtonTRDCPostCheck(snes, X, Y, W, &changed_y, &changed_w));
467: if (changed_y) PetscCall(VecWAXPY(W, -1.0, Y, X));
468: PetscCall(VecCopy(Y, snes->vec_sol_update));
469: PetscCall(SNESComputeFunction(snes, W, G)); /* F(X-Y) = G */
470: PetscCall(VecNorm(G, NORM_2, &gnorm)); /* gnorm <- || g || */
471: SNESCheckFunctionNorm(snes, gnorm);
472: g = 0.5 * PetscSqr(gnorm); /* minimizing function g(W) */
473: if (f0 == mp) rho = 0.0;
474: else rho = (f0 - g) / (f0 - mp); /* actual improvement over predicted improvement */
476: if (rho < neP->eta2) {
477: delta *= neP->t1; /* shrink the region */
478: } else if (rho > neP->eta3) {
479: delta = PetscMin(neP->t2 * delta, deltaM); /* expand the region, but not greater than deltaM */
480: }
482: neP->delta = delta;
483: if (rho >= neP->eta1) {
484: /* unscale delta and xnorm before going to the next outer iteration */
485: if (bs > 1 && neP->auto_scale_multiphase) {
486: neP->delta = delta / xnorm;
487: xnorm = temp_xnorm;
488: ynorm = temp_ynorm;
489: }
490: neP->rho_satisfied = PETSC_TRUE;
491: break; /* the improvement ratio is satisfactory */
492: }
493: PetscCall(PetscInfo(snes, "Trying again in smaller region\n"));
495: /* check to see if progress is hopeless */
496: neP->itflag = PETSC_FALSE;
497: /* both delta, ynorm, and xnorm are either scaled or unscaled */
498: PetscCall(SNESTRDC_Converged_Private(snes, snes->iter, xnorm, ynorm, fnorm, &reason, snes->cnvP));
499: /* if multiphase state changes, break out inner iteration */
500: if (reason == SNES_BREAKOUT_INNER_ITER) {
501: if (bs > 1 && neP->auto_scale_multiphase) {
502: /* unscale delta and xnorm before going to the next outer iteration */
503: neP->delta = delta / xnorm;
504: xnorm = temp_xnorm;
505: ynorm = temp_ynorm;
506: }
507: reason = SNES_CONVERGED_ITERATING;
508: break;
509: }
510: if (reason == SNES_CONVERGED_SNORM_RELATIVE) reason = SNES_DIVERGED_INNER;
511: if (reason) {
512: if (reason < 0) {
513: /* We're not progressing, so return with the current iterate */
514: PetscCall(SNESMonitor(snes, i + 1, fnorm));
515: breakout = PETSC_TRUE;
516: break;
517: } else if (reason > 0) {
518: /* We're converged, so return with the current iterate and update solution */
519: PetscCall(SNESMonitor(snes, i + 1, fnorm));
520: breakout = PETSC_FALSE;
521: break;
522: }
523: }
524: snes->numFailures++;
525: }
526: if (!breakout) {
527: /* Update function and solution vectors */
528: fnorm = gnorm;
529: PetscCall(VecCopy(G, F));
530: PetscCall(VecCopy(W, X));
531: /* Monitor convergence */
532: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
533: snes->iter = i + 1;
534: snes->norm = fnorm;
535: snes->xnorm = xnorm;
536: snes->ynorm = ynorm;
537: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
538: PetscCall(SNESLogConvergenceHistory(snes, snes->norm, lits));
539: PetscCall(SNESMonitor(snes, snes->iter, snes->norm));
540: /* Test for convergence, xnorm = || X || */
541: neP->itflag = PETSC_TRUE;
542: if (snes->ops->converged != SNESConvergedSkip) PetscCall(VecNorm(X, NORM_2, &xnorm));
543: PetscUseTypeMethod(snes, converged, snes->iter, xnorm, ynorm, fnorm, &reason, snes->cnvP);
544: if (reason) break;
545: } else break;
546: }
548: /* PetscCall(PetscFree(inorms)); */
549: if (i == maxits) {
550: PetscCall(PetscInfo(snes, "Maximum number of iterations has been reached: %" PetscInt_FMT "\n", maxits));
551: if (!reason) reason = SNES_DIVERGED_MAX_IT;
552: }
553: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
554: snes->reason = reason;
555: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
556: if (convtest != SNESTRDC_KSPConverged_Private) {
557: PetscCall(KSPGetAndClearConvergenceTest(ksp, &ctx->convtest, &ctx->convctx, &ctx->convdestroy));
558: PetscCall(PetscFree(ctx));
559: PetscCall(KSPSetConvergenceTest(ksp, convtest, convctx, convdestroy));
560: }
561: PetscFunctionReturn(PETSC_SUCCESS);
562: }
564: static PetscErrorCode SNESSetUp_NEWTONTRDC(SNES snes)
565: {
566: PetscFunctionBegin;
567: PetscCall(SNESSetWorkVecs(snes, 6));
568: PetscCall(SNESSetUpMatrices(snes));
569: PetscFunctionReturn(PETSC_SUCCESS);
570: }
572: static PetscErrorCode SNESReset_NEWTONTRDC(SNES snes)
573: {
574: PetscFunctionBegin;
575: PetscFunctionReturn(PETSC_SUCCESS);
576: }
578: static PetscErrorCode SNESDestroy_NEWTONTRDC(SNES snes)
579: {
580: PetscFunctionBegin;
581: PetscCall(SNESReset_NEWTONTRDC(snes));
582: PetscCall(PetscFree(snes->data));
583: PetscFunctionReturn(PETSC_SUCCESS);
584: }
586: static PetscErrorCode SNESSetFromOptions_NEWTONTRDC(SNES snes, PetscOptionItems *PetscOptionsObject)
587: {
588: SNES_NEWTONTRDC *ctx = (SNES_NEWTONTRDC *)snes->data;
590: PetscFunctionBegin;
591: PetscOptionsHeadBegin(PetscOptionsObject, "SNES trust region options for nonlinear equations");
592: PetscCall(PetscOptionsReal("-snes_trdc_tol", "Trust region tolerance", "SNESSetTrustRegionTolerance", snes->deltatol, &snes->deltatol, NULL));
593: PetscCall(PetscOptionsReal("-snes_trdc_eta1", "eta1", "None", ctx->eta1, &ctx->eta1, NULL));
594: PetscCall(PetscOptionsReal("-snes_trdc_eta2", "eta2", "None", ctx->eta2, &ctx->eta2, NULL));
595: PetscCall(PetscOptionsReal("-snes_trdc_eta3", "eta3", "None", ctx->eta3, &ctx->eta3, NULL));
596: PetscCall(PetscOptionsReal("-snes_trdc_t1", "t1", "None", ctx->t1, &ctx->t1, NULL));
597: PetscCall(PetscOptionsReal("-snes_trdc_t2", "t2", "None", ctx->t2, &ctx->t2, NULL));
598: PetscCall(PetscOptionsReal("-snes_trdc_deltaM", "deltaM", "None", ctx->deltaM, &ctx->deltaM, NULL));
599: PetscCall(PetscOptionsReal("-snes_trdc_delta0", "delta0", "None", ctx->delta0, &ctx->delta0, NULL));
600: PetscCall(PetscOptionsReal("-snes_trdc_auto_scale_max", "auto_scale_max", "None", ctx->auto_scale_max, &ctx->auto_scale_max, NULL));
601: PetscCall(PetscOptionsBool("-snes_trdc_use_cauchy", "use_cauchy", "use Cauchy step and direction", ctx->use_cauchy, &ctx->use_cauchy, NULL));
602: PetscCall(PetscOptionsBool("-snes_trdc_auto_scale_multiphase", "auto_scale_multiphase", "Auto scaling for proper cauchy direction", ctx->auto_scale_multiphase, &ctx->auto_scale_multiphase, NULL));
603: PetscOptionsHeadEnd();
604: PetscFunctionReturn(PETSC_SUCCESS);
605: }
607: static PetscErrorCode SNESView_NEWTONTRDC(SNES snes, PetscViewer viewer)
608: {
609: SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
610: PetscBool iascii;
612: PetscFunctionBegin;
613: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
614: if (iascii) {
615: PetscCall(PetscViewerASCIIPrintf(viewer, " Trust region tolerance %g (-snes_trtol)\n", (double)snes->deltatol));
616: PetscCall(PetscViewerASCIIPrintf(viewer, " eta1=%g, eta2=%g, eta3=%g\n", (double)tr->eta1, (double)tr->eta2, (double)tr->eta3));
617: PetscCall(PetscViewerASCIIPrintf(viewer, " delta0=%g, t1=%g, t2=%g, deltaM=%g\n", (double)tr->delta0, (double)tr->t1, (double)tr->t2, (double)tr->deltaM));
618: }
619: PetscFunctionReturn(PETSC_SUCCESS);
620: }
622: /*MC
623: SNESNEWTONTRDC - Newton based nonlinear solver that uses trust-region dogleg method with Cauchy direction
625: Options Database Keys:
626: + -snes_trdc_tol <tol> - trust region tolerance
627: . -snes_trdc_eta1 <eta1> - trust region parameter 0.0 <= eta1 <= eta2, rho >= eta1 breaks out of the inner iteration (default: eta1=0.001)
628: . -snes_trdc_eta2 <eta2> - trust region parameter 0.0 <= eta1 <= eta2, rho <= eta2 shrinks the trust region (default: eta2=0.25)
629: . -snes_trdc_eta3 <eta3> - trust region parameter eta3 > eta2, rho >= eta3 expands the trust region (default: eta3=0.75)
630: . -snes_trdc_t1 <t1> - trust region parameter, shrinking factor of trust region (default: 0.25)
631: . -snes_trdc_t2 <t2> - trust region parameter, expanding factor of trust region (default: 2.0)
632: . -snes_trdc_deltaM <deltaM> - trust region parameter, max size of trust region, $deltaM*norm2(x)$ (default: 0.5)
633: . -snes_trdc_delta0 <delta0> - trust region parameter, initial size of trust region, $delta0*norm2(x)$ (default: 0.1)
634: . -snes_trdc_auto_scale_max <auto_scale_max> - used with auto_scale_multiphase, caps the maximum auto-scaling factor
635: . -snes_trdc_use_cauchy <use_cauchy> - True uses dogleg Cauchy (Steepest Descent direction) step & direction in the trust region algorithm
636: - -snes_trdc_auto_scale_multiphase <auto_scale_multiphase> - True turns on auto-scaling for multivariable block matrix for Cauchy and trust region
638: Level: intermediate
640: Note:
641: See {cite}`park2021linear`
643: .seealso: [](ch_snes), `SNESCreate()`, `SNES`, `SNESSetType()`, `SNESNEWTONLS`, `SNESSetTrustRegionTolerance()`,
644: `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`,
645: `SNESNewtonTRDCGetRhoFlag()`, `SNESNewtonTRDCSetPreCheck()`
646: M*/
647: PETSC_EXTERN PetscErrorCode SNESCreate_NEWTONTRDC(SNES snes)
648: {
649: SNES_NEWTONTRDC *neP;
651: PetscFunctionBegin;
652: snes->ops->setup = SNESSetUp_NEWTONTRDC;
653: snes->ops->solve = SNESSolve_NEWTONTRDC;
654: snes->ops->destroy = SNESDestroy_NEWTONTRDC;
655: snes->ops->setfromoptions = SNESSetFromOptions_NEWTONTRDC;
656: snes->ops->view = SNESView_NEWTONTRDC;
657: snes->ops->reset = SNESReset_NEWTONTRDC;
659: snes->usesksp = PETSC_TRUE;
660: snes->usesnpc = PETSC_FALSE;
662: snes->alwayscomputesfinalresidual = PETSC_TRUE;
664: PetscCall(PetscNew(&neP));
665: snes->data = (void *)neP;
666: neP->delta = 0.0;
667: neP->delta0 = 0.1;
668: neP->eta1 = 0.001;
669: neP->eta2 = 0.25;
670: neP->eta3 = 0.75;
671: neP->t1 = 0.25;
672: neP->t2 = 2.0;
673: neP->deltaM = 0.5;
674: neP->sigma = 0.0001;
675: neP->itflag = PETSC_FALSE;
676: neP->rnorm0 = 0.0;
677: neP->ttol = 0.0;
678: neP->use_cauchy = PETSC_TRUE;
679: neP->auto_scale_multiphase = PETSC_FALSE;
680: neP->auto_scale_max = -1.0;
681: neP->rho_satisfied = PETSC_FALSE;
682: snes->deltatol = 1.e-12;
684: /* for multiphase (multivariable) scaling */
685: /* may be used for dynamic allocation of inorms, but it fails snes_tutorials-ex3_13
686: on test forced DIVERGED_JACOBIAN_DOMAIN test. I will use static array for now.
687: PetscCall(VecGetBlockSize(snes->work[0],&neP->bs));
688: PetscCall(PetscCalloc1(neP->bs,&neP->inorms));
689: */
690: PetscFunctionReturn(PETSC_SUCCESS);
691: }