xref: /petsc/src/tao/unconstrained/impls/ntl/ntl.c (revision 6aeb920f53dc9577f1df4ff00bd447ebf24efec0)
1 #include <../src/tao/matrix/lmvmmat.h>
2 #include <../src/tao/unconstrained/impls/ntl/ntl.h>
3 
4 #include <petscksp.h>
5 #include <petscpc.h>
6 #include <petsc-private/kspimpl.h>
7 #include <petsc-private/pcimpl.h>
8 
9 #define NTL_KSP_NASH    0
10 #define NTL_KSP_STCG    1
11 #define NTL_KSP_GLTR    2
12 #define NTL_KSP_TYPES   3
13 
14 #define NTL_PC_NONE     0
15 #define NTL_PC_AHESS    1
16 #define NTL_PC_BFGS     2
17 #define NTL_PC_PETSC    3
18 #define NTL_PC_TYPES    4
19 
20 #define BFGS_SCALE_AHESS        0
21 #define BFGS_SCALE_BFGS         1
22 #define BFGS_SCALE_TYPES        2
23 
24 #define NTL_INIT_CONSTANT         0
25 #define NTL_INIT_DIRECTION        1
26 #define NTL_INIT_INTERPOLATION    2
27 #define NTL_INIT_TYPES            3
28 
29 #define NTL_UPDATE_REDUCTION      0
30 #define NTL_UPDATE_INTERPOLATION  1
31 #define NTL_UPDATE_TYPES          2
32 
33 static const char *NTL_KSP[64] = {"nash", "stcg", "gltr"};
34 
35 static const char *NTL_PC[64] = {"none", "ahess", "bfgs", "petsc"};
36 
37 static const char *BFGS_SCALE[64] = {"ahess", "bfgs"};
38 
39 static const char *NTL_INIT[64] = {"constant", "direction", "interpolation"};
40 
41 static const char *NTL_UPDATE[64] = {"reduction", "interpolation"};
42 
43 /* Routine for BFGS preconditioner */
44 
45 #undef __FUNCT__
46 #define __FUNCT__ "MatLMVMSolveShell"
47 static PetscErrorCode MatLMVMSolveShell(PC pc, Vec b, Vec x)
48 {
49   PetscErrorCode ierr;
50   Mat            M;
51 
52   PetscFunctionBegin;
53   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
54   PetscValidHeaderSpecific(b,VEC_CLASSID,2);
55   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
56   ierr = PCShellGetContext(pc,(void**)&M);CHKERRQ(ierr);
57   ierr = MatLMVMSolve(M, b, x);CHKERRQ(ierr);
58   PetscFunctionReturn(0);
59 }
60 
61 /* Implements Newton's Method with a trust-region, line-search approach for
62    solving unconstrained minimization problems.  A More'-Thuente line search
63    is used to guarantee that the bfgs preconditioner remains positive
64    definite. */
65 
66 #define NTL_NEWTON              0
67 #define NTL_BFGS                1
68 #define NTL_SCALED_GRADIENT     2
69 #define NTL_GRADIENT            3
70 
71 #undef __FUNCT__
72 #define __FUNCT__ "TaoSolve_NTL"
73 static PetscErrorCode TaoSolve_NTL(Tao tao)
74 {
75   TAO_NTL                      *tl = (TAO_NTL *)tao->data;
76   PC                           pc;
77   KSPConvergedReason           ksp_reason;
78   TaoConvergedReason           reason;
79   TaoLineSearchConvergedReason ls_reason;
80 
81   PetscReal                    fmin, ftrial, prered, actred, kappa, sigma;
82   PetscReal                    tau, tau_1, tau_2, tau_max, tau_min, max_radius;
83   PetscReal                    f, fold, gdx, gnorm;
84   PetscReal                    step = 1.0;
85 
86   PetscReal                    delta;
87   PetscReal                    norm_d = 0.0;
88   PetscErrorCode               ierr;
89   PetscInt                     stepType;
90   PetscInt                     iter = 0,its;
91 
92   PetscInt                     bfgsUpdates = 0;
93   PetscInt                     needH;
94 
95   PetscInt                     i_max = 5;
96   PetscInt                     j_max = 1;
97   PetscInt                     i, j, n, N;
98 
99   PetscInt                     tr_reject;
100 
101   PetscFunctionBegin;
102   if (tao->XL || tao->XU || tao->ops->computebounds) {
103     ierr = PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by ntl algorithm\n");CHKERRQ(ierr);
104   }
105 
106   /* Initialize trust-region radius */
107   tao->trust = tao->trust0;
108 
109   /* Modify the radius if it is too large or small */
110   tao->trust = PetscMax(tao->trust, tl->min_radius);
111   tao->trust = PetscMin(tao->trust, tl->max_radius);
112 
113   if (NTL_PC_BFGS == tl->pc_type && !tl->M) {
114     ierr = VecGetLocalSize(tao->solution,&n);CHKERRQ(ierr);
115     ierr = VecGetSize(tao->solution,&N);CHKERRQ(ierr);
116     ierr = MatCreateLMVM(((PetscObject)tao)->comm,n,N,&tl->M);CHKERRQ(ierr);
117     ierr = MatLMVMAllocateVectors(tl->M,tao->solution);CHKERRQ(ierr);
118   }
119 
120   /* Check convergence criteria */
121   ierr = TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);CHKERRQ(ierr);
122   ierr = VecNorm(tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr);
123   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
124   needH = 1;
125 
126   ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr);
127   if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0);
128 
129   /* Create vectors for the limited memory preconditioner */
130   if ((NTL_PC_BFGS == tl->pc_type) && (BFGS_SCALE_BFGS != tl->bfgs_scale_type)) {
131     if (!tl->Diag) {
132       ierr = VecDuplicate(tao->solution, &tl->Diag);CHKERRQ(ierr);
133     }
134   }
135 
136   /* Modify the linear solver to a conjugate gradient method */
137   switch(tl->ksp_type) {
138   case NTL_KSP_NASH:
139     ierr = KSPSetType(tao->ksp, KSPNASH);CHKERRQ(ierr);
140     if (tao->ksp->ops->setfromoptions) {
141       (*tao->ksp->ops->setfromoptions)(tao->ksp);
142     }
143     break;
144 
145   case NTL_KSP_STCG:
146     ierr = KSPSetType(tao->ksp, KSPSTCG);CHKERRQ(ierr);
147     if (tao->ksp->ops->setfromoptions) {
148       (*tao->ksp->ops->setfromoptions)(tao->ksp);
149     }
150     break;
151 
152   default:
153     ierr = KSPSetType(tao->ksp, KSPGLTR);CHKERRQ(ierr);
154     if (tao->ksp->ops->setfromoptions) {
155       (*tao->ksp->ops->setfromoptions)(tao->ksp);
156     }
157     break;
158   }
159 
160   /* Modify the preconditioner to use the bfgs approximation */
161   ierr = KSPGetPC(tao->ksp, &pc);CHKERRQ(ierr);
162   switch(tl->pc_type) {
163   case NTL_PC_NONE:
164     ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr);
165     if (pc->ops->setfromoptions) {
166       (*pc->ops->setfromoptions)(pc);
167     }
168     break;
169 
170   case NTL_PC_AHESS:
171     ierr = PCSetType(pc, PCJACOBI);CHKERRQ(ierr);
172     if (pc->ops->setfromoptions) {
173       (*pc->ops->setfromoptions)(pc);
174     }
175     ierr = PCJacobiSetUseAbs(pc);CHKERRQ(ierr);
176     break;
177 
178   case NTL_PC_BFGS:
179     ierr = PCSetType(pc, PCSHELL);CHKERRQ(ierr);
180     if (pc->ops->setfromoptions) {
181       (*pc->ops->setfromoptions)(pc);
182     }
183     ierr = PCShellSetName(pc, "bfgs");CHKERRQ(ierr);
184     ierr = PCShellSetContext(pc, tl->M);CHKERRQ(ierr);
185     ierr = PCShellSetApply(pc, MatLMVMSolveShell);CHKERRQ(ierr);
186     break;
187 
188   default:
189     /* Use the pc method set by pc_type */
190     break;
191   }
192 
193   /* Initialize trust-region radius.  The initialization is only performed
194      when we are using Steihaug-Toint or the Generalized Lanczos method. */
195   switch(tl->init_type) {
196   case NTL_INIT_CONSTANT:
197     /* Use the initial radius specified */
198     break;
199 
200   case NTL_INIT_INTERPOLATION:
201     /* Use the initial radius specified */
202     max_radius = 0.0;
203 
204     for (j = 0; j < j_max; ++j) {
205       fmin = f;
206       sigma = 0.0;
207 
208       if (needH) {
209         ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr);
210         needH = 0;
211       }
212 
213       for (i = 0; i < i_max; ++i) {
214         ierr = VecCopy(tao->solution, tl->W);CHKERRQ(ierr);
215         ierr = VecAXPY(tl->W, -tao->trust/gnorm, tao->gradient);CHKERRQ(ierr);
216 
217         ierr = TaoComputeObjective(tao, tl->W, &ftrial);CHKERRQ(ierr);
218         if (PetscIsInfOrNanReal(ftrial)) {
219           tau = tl->gamma1_i;
220         } else {
221           if (ftrial < fmin) {
222             fmin = ftrial;
223             sigma = -tao->trust / gnorm;
224           }
225 
226           ierr = MatMult(tao->hessian, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
227           ierr = VecDot(tao->gradient, tao->stepdirection, &prered);CHKERRQ(ierr);
228 
229           prered = tao->trust * (gnorm - 0.5 * tao->trust * prered / (gnorm * gnorm));
230           actred = f - ftrial;
231           if ((PetscAbsScalar(actred) <= tl->epsilon) && (PetscAbsScalar(prered) <= tl->epsilon)) {
232             kappa = 1.0;
233           } else {
234             kappa = actred / prered;
235           }
236 
237           tau_1 = tl->theta_i * gnorm * tao->trust / (tl->theta_i * gnorm * tao->trust + (1.0 - tl->theta_i) * prered - actred);
238           tau_2 = tl->theta_i * gnorm * tao->trust / (tl->theta_i * gnorm * tao->trust - (1.0 + tl->theta_i) * prered + actred);
239           tau_min = PetscMin(tau_1, tau_2);
240           tau_max = PetscMax(tau_1, tau_2);
241 
242           if (PetscAbsScalar(kappa - 1.0) <= tl->mu1_i) {
243             /* Great agreement */
244             max_radius = PetscMax(max_radius, tao->trust);
245 
246             if (tau_max < 1.0) {
247               tau = tl->gamma3_i;
248             } else if (tau_max > tl->gamma4_i) {
249               tau = tl->gamma4_i;
250             } else if (tau_1 >= 1.0 && tau_1 <= tl->gamma4_i && tau_2 < 1.0) {
251               tau = tau_1;
252             } else if (tau_2 >= 1.0 && tau_2 <= tl->gamma4_i && tau_1 < 1.0) {
253               tau = tau_2;
254             } else {
255               tau = tau_max;
256             }
257           } else if (PetscAbsScalar(kappa - 1.0) <= tl->mu2_i) {
258             /* Good agreement */
259             max_radius = PetscMax(max_radius, tao->trust);
260 
261             if (tau_max < tl->gamma2_i) {
262               tau = tl->gamma2_i;
263             } else if (tau_max > tl->gamma3_i) {
264               tau = tl->gamma3_i;
265             } else {
266               tau = tau_max;
267             }
268           } else {
269             /* Not good agreement */
270             if (tau_min > 1.0) {
271               tau = tl->gamma2_i;
272             } else if (tau_max < tl->gamma1_i) {
273               tau = tl->gamma1_i;
274             } else if ((tau_min < tl->gamma1_i) && (tau_max >= 1.0)) {
275               tau = tl->gamma1_i;
276             } else if ((tau_1 >= tl->gamma1_i) && (tau_1 < 1.0) &&  ((tau_2 < tl->gamma1_i) || (tau_2 >= 1.0))) {
277               tau = tau_1;
278             } else if ((tau_2 >= tl->gamma1_i) && (tau_2 < 1.0) &&  ((tau_1 < tl->gamma1_i) || (tau_2 >= 1.0))) {
279               tau = tau_2;
280             } else {
281               tau = tau_max;
282             }
283           }
284         }
285         tao->trust = tau * tao->trust;
286       }
287 
288       if (fmin < f) {
289         f = fmin;
290         ierr = VecAXPY(tao->solution, sigma, tao->gradient);CHKERRQ(ierr);
291         ierr = TaoComputeGradient(tao, tao->solution, tao->gradient);CHKERRQ(ierr);
292 
293         ierr = VecNorm(tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr);
294         if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
295         needH = 1;
296 
297         ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, 1.0, &reason);CHKERRQ(ierr);
298         if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0);
299       }
300     }
301     tao->trust = PetscMax(tao->trust, max_radius);
302 
303     /* Modify the radius if it is too large or small */
304     tao->trust = PetscMax(tao->trust, tl->min_radius);
305     tao->trust = PetscMin(tao->trust, tl->max_radius);
306     break;
307 
308   default:
309     /* Norm of the first direction will initialize radius */
310     tao->trust = 0.0;
311     break;
312   }
313 
314   /* Set initial scaling for the BFGS preconditioner
315      This step is done after computing the initial trust-region radius
316      since the function value may have decreased */
317   if (NTL_PC_BFGS == tl->pc_type) {
318     if (f != 0.0) {
319       delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
320     } else {
321       delta = 2.0 / (gnorm*gnorm);
322     }
323     ierr = MatLMVMSetDelta(tl->M, delta);CHKERRQ(ierr);
324   }
325 
326   /* Set counter for gradient/reset steps */
327   tl->ntrust = 0;
328   tl->newt = 0;
329   tl->bfgs = 0;
330   tl->sgrad = 0;
331   tl->grad = 0;
332 
333   /* Have not converged; continue with Newton method */
334   while (reason == TAO_CONTINUE_ITERATING) {
335     ++iter;
336     tao->ksp_its=0;
337     /* Compute the Hessian */
338     if (needH) {
339       ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr);
340       needH = 0;
341     }
342 
343     if (NTL_PC_BFGS == tl->pc_type) {
344       if (BFGS_SCALE_AHESS == tl->bfgs_scale_type) {
345         /* Obtain diagonal for the bfgs preconditioner */
346         ierr = MatGetDiagonal(tao->hessian, tl->Diag);CHKERRQ(ierr);
347         ierr = VecAbs(tl->Diag);CHKERRQ(ierr);
348         ierr = VecReciprocal(tl->Diag);CHKERRQ(ierr);
349         ierr = MatLMVMSetScale(tl->M, tl->Diag);CHKERRQ(ierr);
350       }
351 
352       /* Update the limited memory preconditioner */
353       ierr = MatLMVMUpdate(tl->M,tao->solution, tao->gradient);CHKERRQ(ierr);
354       ++bfgsUpdates;
355     }
356     ierr = KSPSetOperators(tao->ksp, tao->hessian, tao->hessian_pre);CHKERRQ(ierr);
357     /* Solve the Newton system of equations */
358     if (NTL_KSP_NASH == tl->ksp_type) {
359       ierr = KSPNASHSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
360       ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
361       ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
362       tao->ksp_its+=its;
363       tao->ksp_tot_its+=its;
364       ierr = KSPNASHGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
365     } else if (NTL_KSP_STCG == tl->ksp_type) {
366       ierr = KSPSTCGSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
367       ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
368       ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
369       tao->ksp_its+=its;
370       tao->ksp_tot_its+=its;
371       ierr = KSPSTCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
372     } else { /* NTL_KSP_GLTR */
373       ierr = KSPGLTRSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
374       ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
375       ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
376       tao->ksp_its+=its;
377       ierr = KSPGLTRGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
378     }
379 
380     if (0.0 == tao->trust) {
381       /* Radius was uninitialized; use the norm of the direction */
382       if (norm_d > 0.0) {
383         tao->trust = norm_d;
384 
385         /* Modify the radius if it is too large or small */
386         tao->trust = PetscMax(tao->trust, tl->min_radius);
387         tao->trust = PetscMin(tao->trust, tl->max_radius);
388       } else {
389         /* The direction was bad; set radius to default value and re-solve
390            the trust-region subproblem to get a direction */
391         tao->trust = tao->trust0;
392 
393         /* Modify the radius if it is too large or small */
394         tao->trust = PetscMax(tao->trust, tl->min_radius);
395         tao->trust = PetscMin(tao->trust, tl->max_radius);
396 
397         if (NTL_KSP_NASH == tl->ksp_type) {
398           ierr = KSPNASHSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
399           ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
400           ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
401           tao->ksp_its+=its;
402           ierr = KSPNASHGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
403         } else if (NTL_KSP_STCG == tl->ksp_type) {
404           ierr = KSPSTCGSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
405           ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
406           ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
407           tao->ksp_its+=its;
408           ierr = KSPSTCGGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
409         } else { /* NTL_KSP_GLTR */
410           ierr = KSPGLTRSetRadius(tao->ksp,tl->max_radius);CHKERRQ(ierr);
411           ierr = KSPSolve(tao->ksp, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
412           ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr);
413           tao->ksp_its+=its;
414           ierr = KSPGLTRGetNormD(tao->ksp, &norm_d);CHKERRQ(ierr);
415         }
416 
417 
418         if (norm_d == 0.0) SETERRQ(PETSC_COMM_SELF,1, "Initial direction zero");
419       }
420     }
421 
422     ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr);
423     ierr = KSPGetConvergedReason(tao->ksp, &ksp_reason);CHKERRQ(ierr);
424     if ((KSP_DIVERGED_INDEFINITE_PC == ksp_reason) && (NTL_PC_BFGS == tl->pc_type) && (bfgsUpdates > 1)) {
425       /* Preconditioner is numerically indefinite; reset the
426          approximate if using BFGS preconditioning. */
427 
428       if (f != 0.0) {
429         delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
430       } else {
431         delta = 2.0 / (gnorm*gnorm);
432       }
433       ierr = MatLMVMSetDelta(tl->M, delta);CHKERRQ(ierr);
434       ierr = MatLMVMReset(tl->M);CHKERRQ(ierr);
435       ierr = MatLMVMUpdate(tl->M, tao->solution, tao->gradient);CHKERRQ(ierr);
436       bfgsUpdates = 1;
437     }
438 
439     /* Check trust-region reduction conditions */
440     tr_reject = 0;
441     if (NTL_UPDATE_REDUCTION == tl->update_type) {
442       /* Get predicted reduction */
443       if (NTL_KSP_NASH == tl->ksp_type) {
444         ierr = KSPNASHGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
445       } else if (NTL_KSP_STCG == tl->ksp_type) {
446         ierr = KSPSTCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
447       } else { /* gltr */
448         ierr = KSPGLTRGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
449       }
450 
451       if (prered >= 0.0) {
452         /* The predicted reduction has the wrong sign.  This cannot
453            happen in infinite precision arithmetic.  Step should
454            be rejected! */
455         tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
456         tr_reject = 1;
457       } else {
458         /* Compute trial step and function value */
459         ierr = VecCopy(tao->solution, tl->W);CHKERRQ(ierr);
460         ierr = VecAXPY(tl->W, 1.0, tao->stepdirection);CHKERRQ(ierr);
461         ierr = TaoComputeObjective(tao, tl->W, &ftrial);CHKERRQ(ierr);
462 
463         if (PetscIsInfOrNanReal(ftrial)) {
464           tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
465           tr_reject = 1;
466         } else {
467           /* Compute and actual reduction */
468           actred = f - ftrial;
469           prered = -prered;
470           if ((PetscAbsScalar(actred) <= tl->epsilon) &&
471               (PetscAbsScalar(prered) <= tl->epsilon)) {
472             kappa = 1.0;
473           } else {
474             kappa = actred / prered;
475           }
476 
477           /* Accept of reject the step and update radius */
478           if (kappa < tl->eta1) {
479             /* Reject the step */
480             tao->trust = tl->alpha1 * PetscMin(tao->trust, norm_d);
481             tr_reject = 1;
482           } else {
483             /* Accept the step */
484             if (kappa < tl->eta2) {
485               /* Marginal bad step */
486               tao->trust = tl->alpha2 * PetscMin(tao->trust, norm_d);
487             } else if (kappa < tl->eta3) {
488               /* Reasonable step */
489               tao->trust = tl->alpha3 * tao->trust;
490             } else if (kappa < tl->eta4) {
491               /* Good step */
492               tao->trust = PetscMax(tl->alpha4 * norm_d, tao->trust);
493             } else {
494               /* Very good step */
495               tao->trust = PetscMax(tl->alpha5 * norm_d, tao->trust);
496             }
497           }
498         }
499       }
500     } else {
501       /* Get predicted reduction */
502       if (NTL_KSP_NASH == tl->ksp_type) {
503         ierr = KSPNASHGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
504       } else if (NTL_KSP_STCG == tl->ksp_type) {
505         ierr = KSPSTCGGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
506       } else { /* gltr */
507         ierr = KSPGLTRGetObjFcn(tao->ksp,&prered);CHKERRQ(ierr);
508       }
509 
510       if (prered >= 0.0) {
511         /* The predicted reduction has the wrong sign.  This cannot
512            happen in infinite precision arithmetic.  Step should
513            be rejected! */
514         tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
515         tr_reject = 1;
516       } else {
517         ierr = VecCopy(tao->solution, tl->W);CHKERRQ(ierr);
518         ierr = VecAXPY(tl->W, 1.0, tao->stepdirection);CHKERRQ(ierr);
519         ierr = TaoComputeObjective(tao, tl->W, &ftrial);CHKERRQ(ierr);
520         if (PetscIsInfOrNanReal(ftrial)) {
521           tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
522           tr_reject = 1;
523         } else {
524           ierr = VecDot(tao->gradient, tao->stepdirection, &gdx);CHKERRQ(ierr);
525 
526           actred = f - ftrial;
527           prered = -prered;
528           if ((PetscAbsScalar(actred) <= tl->epsilon) &&
529               (PetscAbsScalar(prered) <= tl->epsilon)) {
530             kappa = 1.0;
531           } else {
532             kappa = actred / prered;
533           }
534 
535           tau_1 = tl->theta * gdx / (tl->theta * gdx - (1.0 - tl->theta) * prered + actred);
536           tau_2 = tl->theta * gdx / (tl->theta * gdx + (1.0 + tl->theta) * prered - actred);
537           tau_min = PetscMin(tau_1, tau_2);
538           tau_max = PetscMax(tau_1, tau_2);
539 
540           if (kappa >= 1.0 - tl->mu1) {
541             /* Great agreement; accept step and update radius */
542             if (tau_max < 1.0) {
543               tao->trust = PetscMax(tao->trust, tl->gamma3 * norm_d);
544             } else if (tau_max > tl->gamma4) {
545               tao->trust = PetscMax(tao->trust, tl->gamma4 * norm_d);
546             } else {
547               tao->trust = PetscMax(tao->trust, tau_max * norm_d);
548             }
549           } else if (kappa >= 1.0 - tl->mu2) {
550             /* Good agreement */
551 
552             if (tau_max < tl->gamma2) {
553               tao->trust = tl->gamma2 * PetscMin(tao->trust, norm_d);
554             } else if (tau_max > tl->gamma3) {
555               tao->trust = PetscMax(tao->trust, tl->gamma3 * norm_d);
556             } else if (tau_max < 1.0) {
557               tao->trust = tau_max * PetscMin(tao->trust, norm_d);
558             } else {
559               tao->trust = PetscMax(tao->trust, tau_max * norm_d);
560             }
561           } else {
562             /* Not good agreement */
563             if (tau_min > 1.0) {
564               tao->trust = tl->gamma2 * PetscMin(tao->trust, norm_d);
565             } else if (tau_max < tl->gamma1) {
566               tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
567             } else if ((tau_min < tl->gamma1) && (tau_max >= 1.0)) {
568               tao->trust = tl->gamma1 * PetscMin(tao->trust, norm_d);
569             } else if ((tau_1 >= tl->gamma1) && (tau_1 < 1.0) && ((tau_2 < tl->gamma1) || (tau_2 >= 1.0))) {
570               tao->trust = tau_1 * PetscMin(tao->trust, norm_d);
571             } else if ((tau_2 >= tl->gamma1) && (tau_2 < 1.0) && ((tau_1 < tl->gamma1) || (tau_2 >= 1.0))) {
572               tao->trust = tau_2 * PetscMin(tao->trust, norm_d);
573             } else {
574               tao->trust = tau_max * PetscMin(tao->trust, norm_d);
575             }
576             tr_reject = 1;
577           }
578         }
579       }
580     }
581 
582     if (tr_reject) {
583       /* The trust-region constraints rejected the step.  Apply a linesearch.
584          Check for descent direction. */
585       ierr = VecDot(tao->stepdirection, tao->gradient, &gdx);CHKERRQ(ierr);
586       if ((gdx >= 0.0) || PetscIsInfOrNanReal(gdx)) {
587         /* Newton step is not descent or direction produced Inf or NaN */
588 
589         if (NTL_PC_BFGS != tl->pc_type) {
590           /* We don't have the bfgs matrix around and updated
591              Must use gradient direction in this case */
592           ierr = VecCopy(tao->gradient, tao->stepdirection);CHKERRQ(ierr);
593           ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr);
594           ++tl->grad;
595           stepType = NTL_GRADIENT;
596         } else {
597           /* Attempt to use the BFGS direction */
598           ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
599           ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr);
600 
601           /* Check for success (descent direction) */
602           ierr = VecDot(tao->stepdirection, tao->gradient, &gdx);CHKERRQ(ierr);
603           if ((gdx >= 0) || PetscIsInfOrNanReal(gdx)) {
604             /* BFGS direction is not descent or direction produced not a number
605                We can assert bfgsUpdates > 1 in this case because
606                the first solve produces the scaled gradient direction,
607                which is guaranteed to be descent */
608 
609             /* Use steepest descent direction (scaled) */
610             if (f != 0.0) {
611               delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
612             } else {
613               delta = 2.0 / (gnorm*gnorm);
614             }
615             ierr = MatLMVMSetDelta(tl->M, delta);CHKERRQ(ierr);
616             ierr = MatLMVMReset(tl->M);CHKERRQ(ierr);
617             ierr = MatLMVMUpdate(tl->M, tao->solution, tao->gradient);CHKERRQ(ierr);
618             ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
619             ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr);
620 
621             bfgsUpdates = 1;
622             ++tl->sgrad;
623             stepType = NTL_SCALED_GRADIENT;
624           } else {
625             if (1 == bfgsUpdates) {
626               /* The first BFGS direction is always the scaled gradient */
627               ++tl->sgrad;
628               stepType = NTL_SCALED_GRADIENT;
629             } else {
630               ++tl->bfgs;
631               stepType = NTL_BFGS;
632             }
633           }
634         }
635       } else {
636         /* Computed Newton step is descent */
637         ++tl->newt;
638         stepType = NTL_NEWTON;
639       }
640 
641       /* Perform the linesearch */
642       fold = f;
643       ierr = VecCopy(tao->solution, tl->Xold);CHKERRQ(ierr);
644       ierr = VecCopy(tao->gradient, tl->Gold);CHKERRQ(ierr);
645 
646       step = 1.0;
647       ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, tao->stepdirection, &step, &ls_reason);CHKERRQ(ierr);
648       ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr);
649 
650       while (ls_reason != TAOLINESEARCH_SUCCESS && ls_reason != TAOLINESEARCH_SUCCESS_USER && stepType != NTL_GRADIENT) {      /* Linesearch failed */
651         /* Linesearch failed */
652         f = fold;
653         ierr = VecCopy(tl->Xold, tao->solution);CHKERRQ(ierr);
654         ierr = VecCopy(tl->Gold, tao->gradient);CHKERRQ(ierr);
655 
656         switch(stepType) {
657         case NTL_NEWTON:
658           /* Failed to obtain acceptable iterate with Newton step */
659 
660           if (NTL_PC_BFGS != tl->pc_type) {
661             /* We don't have the bfgs matrix around and being updated
662                Must use gradient direction in this case */
663             ierr = VecCopy(tao->gradient, tao->stepdirection);CHKERRQ(ierr);
664             ++tl->grad;
665             stepType = NTL_GRADIENT;
666           } else {
667             /* Attempt to use the BFGS direction */
668             ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
669 
670 
671             /* Check for success (descent direction) */
672             ierr = VecDot(tao->stepdirection, tao->gradient, &gdx);CHKERRQ(ierr);
673             if ((gdx <= 0) || PetscIsInfOrNanReal(gdx)) {
674               /* BFGS direction is not descent or direction produced
675                  not a number.  We can assert bfgsUpdates > 1 in this case
676                  Use steepest descent direction (scaled) */
677 
678               if (f != 0.0) {
679                 delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
680               } else {
681                 delta = 2.0 / (gnorm*gnorm);
682               }
683               ierr = MatLMVMSetDelta(tl->M, delta);CHKERRQ(ierr);
684               ierr = MatLMVMReset(tl->M);CHKERRQ(ierr);
685               ierr = MatLMVMUpdate(tl->M, tao->solution, tao->gradient);CHKERRQ(ierr);
686               ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
687 
688               bfgsUpdates = 1;
689               ++tl->sgrad;
690               stepType = NTL_SCALED_GRADIENT;
691             } else {
692               if (1 == bfgsUpdates) {
693                 /* The first BFGS direction is always the scaled gradient */
694                 ++tl->sgrad;
695                 stepType = NTL_SCALED_GRADIENT;
696               } else {
697                 ++tl->bfgs;
698                 stepType = NTL_BFGS;
699               }
700             }
701           }
702           break;
703 
704         case NTL_BFGS:
705           /* Can only enter if pc_type == NTL_PC_BFGS
706              Failed to obtain acceptable iterate with BFGS step
707              Attempt to use the scaled gradient direction */
708 
709           if (f != 0.0) {
710             delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
711           } else {
712             delta = 2.0 / (gnorm*gnorm);
713           }
714           ierr = MatLMVMSetDelta(tl->M, delta);CHKERRQ(ierr);
715           ierr = MatLMVMReset(tl->M);CHKERRQ(ierr);
716           ierr = MatLMVMUpdate(tl->M, tao->solution, tao->gradient);CHKERRQ(ierr);
717           ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
718 
719           bfgsUpdates = 1;
720           ++tl->sgrad;
721           stepType = NTL_SCALED_GRADIENT;
722           break;
723 
724         case NTL_SCALED_GRADIENT:
725           /* Can only enter if pc_type == NTL_PC_BFGS
726              The scaled gradient step did not produce a new iterate;
727              attemp to use the gradient direction.
728              Need to make sure we are not using a different diagonal scaling */
729           ierr = MatLMVMSetScale(tl->M, tl->Diag);CHKERRQ(ierr);
730           ierr = MatLMVMSetDelta(tl->M, 1.0);CHKERRQ(ierr);
731           ierr = MatLMVMReset(tl->M);CHKERRQ(ierr);
732           ierr = MatLMVMUpdate(tl->M, tao->solution, tao->gradient);CHKERRQ(ierr);
733           ierr = MatLMVMSolve(tl->M, tao->gradient, tao->stepdirection);CHKERRQ(ierr);
734 
735           bfgsUpdates = 1;
736           ++tl->grad;
737           stepType = NTL_GRADIENT;
738           break;
739         }
740         ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr);
741 
742         /* This may be incorrect; linesearch has values for stepmax and stepmin
743            that should be reset. */
744         step = 1.0;
745         ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, tao->stepdirection, &step, &ls_reason);CHKERRQ(ierr);
746         ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr);
747       }
748 
749       if (ls_reason != TAOLINESEARCH_SUCCESS && ls_reason != TAOLINESEARCH_SUCCESS_USER) {
750         /* Failed to find an improving point */
751         f = fold;
752         ierr = VecCopy(tl->Xold, tao->solution);CHKERRQ(ierr);
753         ierr = VecCopy(tl->Gold, tao->gradient);CHKERRQ(ierr);
754         tao->trust = 0.0;
755         step = 0.0;
756         reason = TAO_DIVERGED_LS_FAILURE;
757         tao->reason = TAO_DIVERGED_LS_FAILURE;
758         break;
759       } else if (stepType == NTL_NEWTON) {
760         if (step < tl->nu1) {
761           /* Very bad step taken; reduce radius */
762           tao->trust = tl->omega1 * PetscMin(norm_d, tao->trust);
763         } else if (step < tl->nu2) {
764           /* Reasonably bad step taken; reduce radius */
765           tao->trust = tl->omega2 * PetscMin(norm_d, tao->trust);
766         } else if (step < tl->nu3) {
767           /* Reasonable step was taken; leave radius alone */
768           if (tl->omega3 < 1.0) {
769             tao->trust = tl->omega3 * PetscMin(norm_d, tao->trust);
770           } else if (tl->omega3 > 1.0) {
771             tao->trust = PetscMax(tl->omega3 * norm_d, tao->trust);
772           }
773         } else if (step < tl->nu4) {
774           /* Full step taken; increase the radius */
775           tao->trust = PetscMax(tl->omega4 * norm_d, tao->trust);
776         } else {
777           /* More than full step taken; increase the radius */
778           tao->trust = PetscMax(tl->omega5 * norm_d, tao->trust);
779         }
780       } else {
781         /* Newton step was not good; reduce the radius */
782         tao->trust = tl->omega1 * PetscMin(norm_d, tao->trust);
783       }
784     } else {
785       /* Trust-region step is accepted */
786       ierr = VecCopy(tl->W, tao->solution);CHKERRQ(ierr);
787       f = ftrial;
788       ierr = TaoComputeGradient(tao, tao->solution, tao->gradient);CHKERRQ(ierr);
789       ++tl->ntrust;
790     }
791 
792     /* The radius may have been increased; modify if it is too large */
793     tao->trust = PetscMin(tao->trust, tl->max_radius);
794 
795     /* Check for converged */
796     ierr = VecNorm(tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr);
797     if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1,"User provided compute function generated Not-a-Number");
798     needH = 1;
799 
800     ierr = TaoMonitor(tao, iter, f, gnorm, 0.0, tao->trust, &reason);CHKERRQ(ierr);
801   }
802   PetscFunctionReturn(0);
803 }
804 
805 /* ---------------------------------------------------------- */
806 #undef __FUNCT__
807 #define __FUNCT__ "TaoSetUp_NTL"
808 static PetscErrorCode TaoSetUp_NTL(Tao tao)
809 {
810   TAO_NTL        *tl = (TAO_NTL *)tao->data;
811   PetscErrorCode ierr;
812 
813   PetscFunctionBegin;
814   if (!tao->gradient) {ierr = VecDuplicate(tao->solution, &tao->gradient);CHKERRQ(ierr); }
815   if (!tao->stepdirection) {ierr = VecDuplicate(tao->solution, &tao->stepdirection);CHKERRQ(ierr);}
816   if (!tl->W) { ierr = VecDuplicate(tao->solution, &tl->W);CHKERRQ(ierr);}
817   if (!tl->Xold) { ierr = VecDuplicate(tao->solution, &tl->Xold);CHKERRQ(ierr);}
818   if (!tl->Gold) { ierr = VecDuplicate(tao->solution, &tl->Gold);CHKERRQ(ierr);}
819   tl->Diag = 0;
820   tl->M = 0;
821   PetscFunctionReturn(0);
822 }
823 
824 /*------------------------------------------------------------*/
825 #undef __FUNCT__
826 #define __FUNCT__ "TaoDestroy_NTL"
827 static PetscErrorCode TaoDestroy_NTL(Tao tao)
828 {
829   TAO_NTL        *tl = (TAO_NTL *)tao->data;
830   PetscErrorCode ierr;
831 
832   PetscFunctionBegin;
833   if (tao->setupcalled) {
834     ierr = VecDestroy(&tl->W);CHKERRQ(ierr);
835     ierr = VecDestroy(&tl->Xold);CHKERRQ(ierr);
836     ierr = VecDestroy(&tl->Gold);CHKERRQ(ierr);
837   }
838   ierr = VecDestroy(&tl->Diag);CHKERRQ(ierr);
839   ierr = MatDestroy(&tl->M);CHKERRQ(ierr);
840   ierr = PetscFree(tao->data);CHKERRQ(ierr);
841   PetscFunctionReturn(0);
842 }
843 
844 /*------------------------------------------------------------*/
845 #undef __FUNCT__
846 #define __FUNCT__ "TaoSetFromOptions_NTL"
847 static PetscErrorCode TaoSetFromOptions_NTL(Tao tao)
848 {
849   TAO_NTL        *tl = (TAO_NTL *)tao->data;
850   PetscErrorCode ierr;
851 
852   PetscFunctionBegin;
853   ierr = PetscOptionsHead("Newton trust region with line search method for unconstrained optimization");CHKERRQ(ierr);
854   ierr = PetscOptionsEList("-tao_ntl_ksp_type", "ksp type", "", NTL_KSP, NTL_KSP_TYPES, NTL_KSP[tl->ksp_type], &tl->ksp_type, 0);CHKERRQ(ierr);
855   ierr = PetscOptionsEList("-tao_ntl_pc_type", "pc type", "", NTL_PC, NTL_PC_TYPES, NTL_PC[tl->pc_type], &tl->pc_type, 0);CHKERRQ(ierr);
856   ierr = PetscOptionsEList("-tao_ntl_bfgs_scale_type", "bfgs scale type", "", BFGS_SCALE, BFGS_SCALE_TYPES, BFGS_SCALE[tl->bfgs_scale_type], &tl->bfgs_scale_type, 0);CHKERRQ(ierr);
857   ierr = PetscOptionsEList("-tao_ntl_init_type", "radius initialization type", "", NTL_INIT, NTL_INIT_TYPES, NTL_INIT[tl->init_type], &tl->init_type, 0);CHKERRQ(ierr);
858   ierr = PetscOptionsEList("-tao_ntl_update_type", "radius update type", "", NTL_UPDATE, NTL_UPDATE_TYPES, NTL_UPDATE[tl->update_type], &tl->update_type, 0);CHKERRQ(ierr);
859   ierr = PetscOptionsReal("-tao_ntl_eta1", "poor steplength; reduce radius", "", tl->eta1, &tl->eta1, 0);CHKERRQ(ierr);
860   ierr = PetscOptionsReal("-tao_ntl_eta2", "reasonable steplength; leave radius alone", "", tl->eta2, &tl->eta2, 0);CHKERRQ(ierr);
861   ierr = PetscOptionsReal("-tao_ntl_eta3", "good steplength; increase radius", "", tl->eta3, &tl->eta3, 0);CHKERRQ(ierr);
862   ierr = PetscOptionsReal("-tao_ntl_eta4", "excellent steplength; greatly increase radius", "", tl->eta4, &tl->eta4, 0);CHKERRQ(ierr);
863   ierr = PetscOptionsReal("-tao_ntl_alpha1", "", "", tl->alpha1, &tl->alpha1, 0);CHKERRQ(ierr);
864   ierr = PetscOptionsReal("-tao_ntl_alpha2", "", "", tl->alpha2, &tl->alpha2, 0);CHKERRQ(ierr);
865   ierr = PetscOptionsReal("-tao_ntl_alpha3", "", "", tl->alpha3, &tl->alpha3, 0);CHKERRQ(ierr);
866   ierr = PetscOptionsReal("-tao_ntl_alpha4", "", "", tl->alpha4, &tl->alpha4, 0);CHKERRQ(ierr);
867   ierr = PetscOptionsReal("-tao_ntl_alpha5", "", "", tl->alpha5, &tl->alpha5, 0);CHKERRQ(ierr);
868   ierr = PetscOptionsReal("-tao_ntl_nu1", "poor steplength; reduce radius", "", tl->nu1, &tl->nu1, 0);CHKERRQ(ierr);
869   ierr = PetscOptionsReal("-tao_ntl_nu2", "reasonable steplength; leave radius alone", "", tl->nu2, &tl->nu2, 0);CHKERRQ(ierr);
870   ierr = PetscOptionsReal("-tao_ntl_nu3", "good steplength; increase radius", "", tl->nu3, &tl->nu3, 0);CHKERRQ(ierr);
871   ierr = PetscOptionsReal("-tao_ntl_nu4", "excellent steplength; greatly increase radius", "", tl->nu4, &tl->nu4, 0);CHKERRQ(ierr);
872   ierr = PetscOptionsReal("-tao_ntl_omega1", "", "", tl->omega1, &tl->omega1, 0);CHKERRQ(ierr);
873   ierr = PetscOptionsReal("-tao_ntl_omega2", "", "", tl->omega2, &tl->omega2, 0);CHKERRQ(ierr);
874   ierr = PetscOptionsReal("-tao_ntl_omega3", "", "", tl->omega3, &tl->omega3, 0);CHKERRQ(ierr);
875   ierr = PetscOptionsReal("-tao_ntl_omega4", "", "", tl->omega4, &tl->omega4, 0);CHKERRQ(ierr);
876   ierr = PetscOptionsReal("-tao_ntl_omega5", "", "", tl->omega5, &tl->omega5, 0);CHKERRQ(ierr);
877   ierr = PetscOptionsReal("-tao_ntl_mu1_i", "", "", tl->mu1_i, &tl->mu1_i, 0);CHKERRQ(ierr);
878   ierr = PetscOptionsReal("-tao_ntl_mu2_i", "", "", tl->mu2_i, &tl->mu2_i, 0);CHKERRQ(ierr);
879   ierr = PetscOptionsReal("-tao_ntl_gamma1_i", "", "", tl->gamma1_i, &tl->gamma1_i, 0);CHKERRQ(ierr);
880   ierr = PetscOptionsReal("-tao_ntl_gamma2_i", "", "", tl->gamma2_i, &tl->gamma2_i, 0);CHKERRQ(ierr);
881   ierr = PetscOptionsReal("-tao_ntl_gamma3_i", "", "", tl->gamma3_i, &tl->gamma3_i, 0);CHKERRQ(ierr);
882   ierr = PetscOptionsReal("-tao_ntl_gamma4_i", "", "", tl->gamma4_i, &tl->gamma4_i, 0);CHKERRQ(ierr);
883   ierr = PetscOptionsReal("-tao_ntl_theta_i", "", "", tl->theta_i, &tl->theta_i, 0);CHKERRQ(ierr);
884   ierr = PetscOptionsReal("-tao_ntl_mu1", "", "", tl->mu1, &tl->mu1, 0);CHKERRQ(ierr);
885   ierr = PetscOptionsReal("-tao_ntl_mu2", "", "", tl->mu2, &tl->mu2, 0);CHKERRQ(ierr);
886   ierr = PetscOptionsReal("-tao_ntl_gamma1", "", "", tl->gamma1, &tl->gamma1, 0);CHKERRQ(ierr);
887   ierr = PetscOptionsReal("-tao_ntl_gamma2", "", "", tl->gamma2, &tl->gamma2, 0);CHKERRQ(ierr);
888   ierr = PetscOptionsReal("-tao_ntl_gamma3", "", "", tl->gamma3, &tl->gamma3, 0);CHKERRQ(ierr);
889   ierr = PetscOptionsReal("-tao_ntl_gamma4", "", "", tl->gamma4, &tl->gamma4, 0);CHKERRQ(ierr);
890   ierr = PetscOptionsReal("-tao_ntl_theta", "", "", tl->theta, &tl->theta, 0);CHKERRQ(ierr);
891   ierr = PetscOptionsReal("-tao_ntl_min_radius", "lower bound on initial radius", "", tl->min_radius, &tl->min_radius, 0);CHKERRQ(ierr);
892   ierr = PetscOptionsReal("-tao_ntl_max_radius", "upper bound on radius", "", tl->max_radius, &tl->max_radius, 0);CHKERRQ(ierr);
893   ierr = PetscOptionsReal("-tao_ntl_epsilon", "tolerance used when computing actual and predicted reduction", "", tl->epsilon, &tl->epsilon, 0);CHKERRQ(ierr);
894   ierr = PetscOptionsTail();CHKERRQ(ierr);
895   ierr = TaoLineSearchSetFromOptions(tao->linesearch);CHKERRQ(ierr);
896   ierr = KSPSetFromOptions(tao->ksp);CHKERRQ(ierr);
897   PetscFunctionReturn(0);
898 }
899 
900 /*------------------------------------------------------------*/
901 #undef __FUNCT__
902 #define __FUNCT__ "TaoView_NTL"
903 static PetscErrorCode TaoView_NTL(Tao tao, PetscViewer viewer)
904 {
905   TAO_NTL        *tl = (TAO_NTL *)tao->data;
906   PetscInt       nrejects;
907   PetscBool      isascii;
908   PetscErrorCode ierr;
909 
910   PetscFunctionBegin;
911   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
912   if (isascii) {
913     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
914     if (NTL_PC_BFGS == tl->pc_type && tl->M) {
915       ierr = MatLMVMGetRejects(tl->M, &nrejects);CHKERRQ(ierr);
916       ierr = PetscViewerASCIIPrintf(viewer, "Rejected matrix updates: %D\n", nrejects);CHKERRQ(ierr);
917     }
918     ierr = PetscViewerASCIIPrintf(viewer, "Trust-region steps: %D\n", tl->ntrust);CHKERRQ(ierr);
919     ierr = PetscViewerASCIIPrintf(viewer, "Newton search steps: %D\n", tl->newt);CHKERRQ(ierr);
920     ierr = PetscViewerASCIIPrintf(viewer, "BFGS search steps: %D\n", tl->bfgs);CHKERRQ(ierr);
921     ierr = PetscViewerASCIIPrintf(viewer, "Scaled gradient search steps: %D\n", tl->sgrad);CHKERRQ(ierr);
922     ierr = PetscViewerASCIIPrintf(viewer, "Gradient search steps: %D\n", tl->grad);CHKERRQ(ierr);
923     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
924   }
925   PetscFunctionReturn(0);
926 }
927 
928 /* ---------------------------------------------------------- */
929 /*MC
930   TAONTR - Newton's method with trust region and linesearch
931   for unconstrained minimization.
932   At each iteration, the Newton trust region method solves the system for d
933   and performs a line search in the d direction:
934 
935             min_d  .5 dT Hk d + gkT d,  s.t.   ||d|| < Delta_k
936 
937   Options Database Keys:
938 + -tao_ntl_ksp_type - "nash","stcg","gltr"
939 . -tao_ntl_pc_type - "none","ahess","bfgs","petsc"
940 . -tao_ntl_bfgs_scale_type - type of scaling with bfgs pc, "ahess" or "bfgs"
941 . -tao_ntl_init_type - "constant","direction","interpolation"
942 . -tao_ntl_update_type - "reduction","interpolation"
943 . -tao_ntl_min_radius - lower bound on trust region radius
944 . -tao_ntl_max_radius - upper bound on trust region radius
945 . -tao_ntl_epsilon - tolerance for accepting actual / predicted reduction
946 . -tao_ntl_mu1_i - mu1 interpolation init factor
947 . -tao_ntl_mu2_i - mu2 interpolation init factor
948 . -tao_ntl_gamma1_i - gamma1 interpolation init factor
949 . -tao_ntl_gamma2_i - gamma2 interpolation init factor
950 . -tao_ntl_gamma3_i - gamma3 interpolation init factor
951 . -tao_ntl_gamma4_i - gamma4 interpolation init factor
952 . -tao_ntl_theta_i - thetha1 interpolation init factor
953 . -tao_ntl_eta1 - eta1 reduction update factor
954 . -tao_ntl_eta2 - eta2 reduction update factor
955 . -tao_ntl_eta3 - eta3 reduction update factor
956 . -tao_ntl_eta4 - eta4 reduction update factor
957 . -tao_ntl_alpha1 - alpha1 reduction update factor
958 . -tao_ntl_alpha2 - alpha2 reduction update factor
959 . -tao_ntl_alpha3 - alpha3 reduction update factor
960 . -tao_ntl_alpha4 - alpha4 reduction update factor
961 . -tao_ntl_alpha4 - alpha4 reduction update factor
962 . -tao_ntl_mu1 - mu1 interpolation update
963 . -tao_ntl_mu2 - mu2 interpolation update
964 . -tao_ntl_gamma1 - gamma1 interpolcation update
965 . -tao_ntl_gamma2 - gamma2 interpolcation update
966 . -tao_ntl_gamma3 - gamma3 interpolcation update
967 . -tao_ntl_gamma4 - gamma4 interpolation update
968 - -tao_ntl_theta - theta1 interpolation update
969 
970   Level: beginner
971 M*/
972 
973 EXTERN_C_BEGIN
974 #undef __FUNCT__
975 #define __FUNCT__ "TaoCreate_NTL"
976 PetscErrorCode TaoCreate_NTL(Tao tao)
977 {
978   TAO_NTL        *tl;
979   PetscErrorCode ierr;
980   const char     *morethuente_type = TAOLINESEARCHMT;
981 
982   PetscFunctionBegin;
983   ierr = PetscNewLog(tao,&tl);CHKERRQ(ierr);
984   tao->ops->setup = TaoSetUp_NTL;
985   tao->ops->solve = TaoSolve_NTL;
986   tao->ops->view = TaoView_NTL;
987   tao->ops->setfromoptions = TaoSetFromOptions_NTL;
988   tao->ops->destroy = TaoDestroy_NTL;
989 
990   tao->max_it = 50;
991 #if defined(PETSC_USE_REAL_SINGLE)
992   tao->fatol = 1e-5;
993   tao->frtol = 1e-5;
994 #else
995   tao->fatol = 1e-10;
996   tao->frtol = 1e-10;
997 #endif
998   tao->data = (void*)tl;
999 
1000   tao->trust0 = 100.0;
1001 
1002 
1003   /* Default values for trust-region radius update based on steplength */
1004   tl->nu1 = 0.25;
1005   tl->nu2 = 0.50;
1006   tl->nu3 = 1.00;
1007   tl->nu4 = 1.25;
1008 
1009   tl->omega1 = 0.25;
1010   tl->omega2 = 0.50;
1011   tl->omega3 = 1.00;
1012   tl->omega4 = 2.00;
1013   tl->omega5 = 4.00;
1014 
1015   /* Default values for trust-region radius update based on reduction */
1016   tl->eta1 = 1.0e-4;
1017   tl->eta2 = 0.25;
1018   tl->eta3 = 0.50;
1019   tl->eta4 = 0.90;
1020 
1021   tl->alpha1 = 0.25;
1022   tl->alpha2 = 0.50;
1023   tl->alpha3 = 1.00;
1024   tl->alpha4 = 2.00;
1025   tl->alpha5 = 4.00;
1026 
1027   /* Default values for trust-region radius update based on interpolation */
1028   tl->mu1 = 0.10;
1029   tl->mu2 = 0.50;
1030 
1031   tl->gamma1 = 0.25;
1032   tl->gamma2 = 0.50;
1033   tl->gamma3 = 2.00;
1034   tl->gamma4 = 4.00;
1035 
1036   tl->theta = 0.05;
1037 
1038   /* Default values for trust region initialization based on interpolation */
1039   tl->mu1_i = 0.35;
1040   tl->mu2_i = 0.50;
1041 
1042   tl->gamma1_i = 0.0625;
1043   tl->gamma2_i = 0.5;
1044   tl->gamma3_i = 2.0;
1045   tl->gamma4_i = 5.0;
1046 
1047   tl->theta_i = 0.25;
1048 
1049   /* Remaining parameters */
1050   tl->min_radius = 1.0e-10;
1051   tl->max_radius = 1.0e10;
1052   tl->epsilon = 1.0e-6;
1053 
1054   tl->ksp_type        = NTL_KSP_STCG;
1055   tl->pc_type         = NTL_PC_BFGS;
1056   tl->bfgs_scale_type = BFGS_SCALE_AHESS;
1057   tl->init_type       = NTL_INIT_INTERPOLATION;
1058   tl->update_type     = NTL_UPDATE_REDUCTION;
1059 
1060   ierr = TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);CHKERRQ(ierr);
1061   ierr = TaoLineSearchSetType(tao->linesearch, morethuente_type);CHKERRQ(ierr);
1062   ierr = TaoLineSearchUseTaoRoutines(tao->linesearch, tao);CHKERRQ(ierr);
1063   ierr = KSPCreate(((PetscObject)tao)->comm, &tao->ksp);CHKERRQ(ierr);
1064   PetscFunctionReturn(0);
1065 }
1066 EXTERN_C_END
1067 
1068 
1069 
1070