xref: /petsc/src/snes/impls/ntrdc/ntrdc.c (revision 27f49a208b01d2e827ab9db411a2d16003fe9262)
1 
2 #include <../src/snes/impls/ntrdc/ntrdcimpl.h> /*I   "petscsnes.h"   I*/
3 
4 typedef struct {
5   SNES snes;
6   /*  Information on the regular SNES convergence test; which may have been user provided
7       Copied from tr.c (maybe able to disposed, but this is a private function) - Heeho
8       Same with SNESTR_KSPConverged_Private, SNESTR_KSPConverged_Destroy, and SNESTR_Converged_Private
9  */
10 
11   PetscErrorCode (*convtest)(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *);
12   PetscErrorCode (*convdestroy)(void *);
13   void *convctx;
14 } SNES_TRDC_KSPConverged_Ctx;
15 
16 static PetscErrorCode SNESTRDC_KSPConverged_Private(KSP ksp, PetscInt n, PetscReal rnorm, KSPConvergedReason *reason, void *cctx)
17 {
18   SNES_TRDC_KSPConverged_Ctx *ctx  = (SNES_TRDC_KSPConverged_Ctx *)cctx;
19   SNES                        snes = ctx->snes;
20   SNES_NEWTONTRDC            *neP  = (SNES_NEWTONTRDC *)snes->data;
21   Vec                         x;
22   PetscReal                   nrm;
23 
24   PetscFunctionBegin;
25   PetscCall((*ctx->convtest)(ksp, n, rnorm, reason, ctx->convctx));
26   if (*reason) PetscCall(PetscInfo(snes, "Default or user provided convergence test KSP iterations=%" PetscInt_FMT ", rnorm=%g\n", n, (double)rnorm));
27   /* Determine norm of solution */
28   PetscCall(KSPBuildSolution(ksp, NULL, &x));
29   PetscCall(VecNorm(x, NORM_2, &nrm));
30   if (nrm >= neP->delta) {
31     PetscCall(PetscInfo(snes, "Ending linear iteration early, delta=%g, length=%g\n", (double)neP->delta, (double)nrm));
32     *reason = KSP_CONVERGED_STEP_LENGTH;
33   }
34   PetscFunctionReturn(PETSC_SUCCESS);
35 }
36 
37 static PetscErrorCode SNESTRDC_KSPConverged_Destroy(void *cctx)
38 {
39   SNES_TRDC_KSPConverged_Ctx *ctx = (SNES_TRDC_KSPConverged_Ctx *)cctx;
40 
41   PetscFunctionBegin;
42   PetscCall((*ctx->convdestroy)(ctx->convctx));
43   PetscCall(PetscFree(ctx));
44 
45   PetscFunctionReturn(PETSC_SUCCESS);
46 }
47 
48 /*
49    SNESTRDC_Converged_Private -test convergence JUST for
50    the trust region tolerance.
51 
52 */
53 static PetscErrorCode SNESTRDC_Converged_Private(SNES snes, PetscInt it, PetscReal xnorm, PetscReal pnorm, PetscReal fnorm, SNESConvergedReason *reason, void *dummy)
54 {
55   SNES_NEWTONTRDC *neP = (SNES_NEWTONTRDC *)snes->data;
56 
57   PetscFunctionBegin;
58   *reason = SNES_CONVERGED_ITERATING;
59   if (neP->delta < xnorm * snes->deltatol) {
60     PetscCall(PetscInfo(snes, "Diverged due to too small a trust region %g<%g*%g\n", (double)neP->delta, (double)xnorm, (double)snes->deltatol));
61     *reason = SNES_DIVERGED_TR_DELTA;
62   } else if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
63     PetscCall(PetscInfo(snes, "Exceeded maximum number of function evaluations: %" PetscInt_FMT "\n", snes->max_funcs));
64     *reason = SNES_DIVERGED_FUNCTION_COUNT;
65   }
66   PetscFunctionReturn(PETSC_SUCCESS);
67 }
68 
69 /*@
70   SNESNewtonTRDCGetRhoFlag - Get whether the current solution update is within the trust-region.
71 
72   Logically Collective
73 
74   Input Parameter:
75 . snes - the nonlinear solver object
76 
77   Output Parameter:
78 . rho_flag: `PETSC_TRUE` if the solution update is in the trust-region; otherwise, `PETSC_FALSE`
79 
80   Level: developer
81 
82 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, , `SNESNewtonTRDCSetPreCheck()`,
83           `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`
84 @*/
85 PetscErrorCode SNESNewtonTRDCGetRhoFlag(SNES snes, PetscBool *rho_flag)
86 {
87   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
88 
89   PetscFunctionBegin;
90   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
91   PetscValidBoolPointer(rho_flag, 2);
92   *rho_flag = tr->rho_satisfied;
93   PetscFunctionReturn(PETSC_SUCCESS);
94 }
95 
96 /*@C
97    SNESNewtonTRDCSetPreCheck - Sets a user function that is called before the search step has been determined.
98        Allows the user a chance to change or override the trust region decision.
99 
100    Logically Collective
101 
102    Input Parameters:
103 +  snes - the nonlinear solver object
104 .  func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPreCheck()`
105 -  ctx  - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
106 
107    Level: intermediate
108 
109    Note:
110    This function is called BEFORE the function evaluation within the `SNESNEWTONTRDC` solver.
111 
112 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`,
113           `SNESNewtonTRDCGetRhoFlag()`
114 @*/
115 PetscErrorCode SNESNewtonTRDCSetPreCheck(SNES snes, PetscErrorCode (*func)(SNES, Vec, Vec, PetscBool *, void *), void *ctx)
116 {
117   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
118 
119   PetscFunctionBegin;
120   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
121   if (func) tr->precheck = func;
122   if (ctx) tr->precheckctx = ctx;
123   PetscFunctionReturn(PETSC_SUCCESS);
124 }
125 
126 /*@C
127    SNESNewtonTRDCGetPreCheck - Gets the pre-check function
128 
129    Not Collective
130 
131    Input Parameter:
132 .  snes - the nonlinear solver context
133 
134    Output Parameters:
135 +  func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPreCheck()`
136 -  ctx  - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
137 
138    Level: intermediate
139 
140 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCPreCheck()`
141 @*/
142 PetscErrorCode SNESNewtonTRDCGetPreCheck(SNES snes, PetscErrorCode (**func)(SNES, Vec, Vec, PetscBool *, void *), void **ctx)
143 {
144   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
145 
146   PetscFunctionBegin;
147   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
148   if (func) *func = tr->precheck;
149   if (ctx) *ctx = tr->precheckctx;
150   PetscFunctionReturn(PETSC_SUCCESS);
151 }
152 
153 /*@C
154    SNESNewtonTRDCSetPostCheck - Sets a user function that is called after the search step has been determined but before the next
155        function evaluation. Allows the user a chance to change or override the decision of the line search routine
156 
157    Logically Collective
158 
159    Input Parameters:
160 +  snes - the nonlinear solver object
161 .  func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPostCheck()`
162 -  ctx  - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
163 
164    Level: intermediate
165 
166    Note:
167    This function is called BEFORE the function evaluation within the `SNESNEWTONTRDC` solver while the function set in
168    `SNESLineSearchSetPostCheck()` is called AFTER the function evaluation.
169 
170 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCPostCheck()`, `SNESNewtonTRDCGetPostCheck()`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCGetPreCheck()`
171 @*/
172 PetscErrorCode SNESNewtonTRDCSetPostCheck(SNES snes, PetscErrorCode (*func)(SNES, Vec, Vec, Vec, PetscBool *, PetscBool *, void *), void *ctx)
173 {
174   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
175 
176   PetscFunctionBegin;
177   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
178   if (func) tr->postcheck = func;
179   if (ctx) tr->postcheckctx = ctx;
180   PetscFunctionReturn(PETSC_SUCCESS);
181 }
182 
183 /*@C
184    SNESNewtonTRDCGetPostCheck - Gets the post-check function
185 
186    Not Collective
187 
188    Input Parameter:
189 .  snes - the nonlinear solver context
190 
191    Output Parameters:
192 +  func - [optional] function evaluation routine, for the calling sequence see `SNESNewtonTRDCPostCheck()`
193 -  ctx  - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`)
194 
195    Level: intermediate
196 
197 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCPostCheck()`
198 @*/
199 PetscErrorCode SNESNewtonTRDCGetPostCheck(SNES snes, PetscErrorCode (**func)(SNES, Vec, Vec, Vec, PetscBool *, PetscBool *, void *), void **ctx)
200 {
201   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
202 
203   PetscFunctionBegin;
204   PetscValidHeaderSpecific(snes, SNES_CLASSID, 1);
205   if (func) *func = tr->postcheck;
206   if (ctx) *ctx = tr->postcheckctx;
207   PetscFunctionReturn(PETSC_SUCCESS);
208 }
209 
210 /*@C
211    SNESNewtonTRDCPreCheck - Called before the step has been determined in `SNESNEWTONTRDC`
212 
213    Logically Collective
214 
215    Input Parameters:
216 +  snes - the solver
217 .  X - The last solution
218 -  Y - The step direction
219 
220    Output Parameters:
221 .  changed_Y - Indicator that the step direction `Y` has been changed.
222 
223    Level: developer
224 
225 .seealso: `SNESNEWTONTRDC`, `SNESNewtonTRDCSetPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCPostCheck()`
226 @*/
227 static PetscErrorCode SNESNewtonTRDCPreCheck(SNES snes, Vec X, Vec Y, PetscBool *changed_Y)
228 {
229   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
230 
231   PetscFunctionBegin;
232   *changed_Y = PETSC_FALSE;
233   if (tr->precheck) {
234     PetscCall((*tr->precheck)(snes, X, Y, changed_Y, tr->precheckctx));
235     PetscValidLogicalCollectiveBool(snes, *changed_Y, 4);
236   }
237   PetscFunctionReturn(PETSC_SUCCESS);
238 }
239 
240 /*@C
241    SNESNewtonTRDCPostCheck - Called after the step has been determined in `SNESNEWTONTRDC` but before the function evaluation at that step
242 
243    Logically Collective
244 
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
250 
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.
254 
255    Note:
256      If `Y` is changed then `W` is recomputed as `X` - `Y`
257 
258    Level: developer
259 
260 .seealso: `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;
265 
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));
271     PetscValidLogicalCollectiveBool(snes, *changed_Y, 5);
272     PetscValidLogicalCollectiveBool(snes, *changed_W, 6);
273   }
274   PetscFunctionReturn(PETSC_SUCCESS);
275 }
276 
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
281 
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;
300 
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 */
311 
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);
313 
314   PetscCall(VecGetBlockSize(YNtmp, &bs));
315 
316   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes));
317   snes->iter = 0;
318   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes));
319 
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   }
330 
331   if (!snes->vec_func_init_set) {
332     PetscCall(SNESComputeFunction(snes, X, F)); /* F(X) */
333   } else snes->vec_func_init_set = PETSC_FALSE;
334 
335   PetscCall(VecNorm(F, NORM_2, &fnorm)); /* fnorm <- || F || */
336   SNESCheckFunctionNorm(snes, fnorm);
337   PetscCall(VecNorm(X, NORM_2, &xnorm)); /* xnorm <- || X || */
338 
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));
347 
348   neP->rho_satisfied = PETSC_FALSE;
349 
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);
353 
354   for (i = 0; i < maxits; i++) {
355     PetscBool changed_y;
356     PetscBool changed_w;
357 
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));
366 
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     }
389 
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 || */
393 
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       }
437 
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*/
444 
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++;
462 
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 */
475 
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       }
481 
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"));
494 
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   }
547 
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 }
563 
564 static PetscErrorCode SNESSetUp_NEWTONTRDC(SNES snes)
565 {
566   PetscFunctionBegin;
567   PetscCall(SNESSetWorkVecs(snes, 6));
568   PetscCall(SNESSetUpMatrices(snes));
569   PetscFunctionReturn(PETSC_SUCCESS);
570 }
571 
572 PetscErrorCode SNESReset_NEWTONTRDC(SNES snes)
573 {
574   PetscFunctionBegin;
575   PetscFunctionReturn(PETSC_SUCCESS);
576 }
577 
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 }
585 
586 static PetscErrorCode SNESSetFromOptions_NEWTONTRDC(SNES snes, PetscOptionItems *PetscOptionsObject)
587 {
588   SNES_NEWTONTRDC *ctx = (SNES_NEWTONTRDC *)snes->data;
589 
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 }
606 
607 static PetscErrorCode SNESView_NEWTONTRDC(SNES snes, PetscViewer viewer)
608 {
609   SNES_NEWTONTRDC *tr = (SNES_NEWTONTRDC *)snes->data;
610   PetscBool        iascii;
611 
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 }
621 
622 /*MC
623       SNESNEWTONTRDC - Newton based nonlinear solver that uses trust-region dogleg method with Cauchy direction
624 
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
637 
638    Level: intermediate
639 
640     Reference:
641 .   - * "Linear and Nonlinear Solvers for Simulating Multiphase Flow
642     within Large-Scale Engineered Subsurface Systems" by Heeho D. Park, Glenn E. Hammond,
643     Albert J. Valocchi, Tara LaForce.
644 
645 .seealso: `SNESCreate()`, `SNES`, `SNESSetType()`, `SNESNEWTONLS`, `SNESSetTrustRegionTolerance()`,
646           `SNESNewtonTRDCPreCheck()`, `SNESNewtonTRDCGetPreCheck()`, `SNESNewtonTRDCSetPostCheck()`, `SNESNewtonTRDCGetPostCheck()`,
647           `SNESNewtonTRDCGetRhoFlag()`, `SNESNewtonTRDCSetPreCheck()`
648 M*/
649 PETSC_EXTERN PetscErrorCode SNESCreate_NEWTONTRDC(SNES snes)
650 {
651   SNES_NEWTONTRDC *neP;
652 
653   PetscFunctionBegin;
654   snes->ops->setup          = SNESSetUp_NEWTONTRDC;
655   snes->ops->solve          = SNESSolve_NEWTONTRDC;
656   snes->ops->destroy        = SNESDestroy_NEWTONTRDC;
657   snes->ops->setfromoptions = SNESSetFromOptions_NEWTONTRDC;
658   snes->ops->view           = SNESView_NEWTONTRDC;
659   snes->ops->reset          = SNESReset_NEWTONTRDC;
660 
661   snes->usesksp = PETSC_TRUE;
662   snes->usesnpc = PETSC_FALSE;
663 
664   snes->alwayscomputesfinalresidual = PETSC_TRUE;
665 
666   PetscCall(PetscNew(&neP));
667   snes->data                 = (void *)neP;
668   neP->delta                 = 0.0;
669   neP->delta0                = 0.1;
670   neP->eta1                  = 0.001;
671   neP->eta2                  = 0.25;
672   neP->eta3                  = 0.75;
673   neP->t1                    = 0.25;
674   neP->t2                    = 2.0;
675   neP->deltaM                = 0.5;
676   neP->sigma                 = 0.0001;
677   neP->itflag                = PETSC_FALSE;
678   neP->rnorm0                = 0.0;
679   neP->ttol                  = 0.0;
680   neP->use_cauchy            = PETSC_TRUE;
681   neP->auto_scale_multiphase = PETSC_FALSE;
682   neP->auto_scale_max        = -1.0;
683   neP->rho_satisfied         = PETSC_FALSE;
684   snes->deltatol             = 1.e-12;
685 
686   /* for multiphase (multivariable) scaling */
687   /* may be used for dynamic allocation of inorms, but it fails snes_tutorials-ex3_13
688      on test forced DIVERGED_JACOBIAN_DOMAIN test. I will use static array for now.
689   PetscCall(VecGetBlockSize(snes->work[0],&neP->bs));
690   PetscCall(PetscCalloc1(neP->bs,&neP->inorms));
691   */
692 
693   PetscFunctionReturn(PETSC_SUCCESS);
694 }
695