xref: /petsc/src/tao/interface/taosolver.c (revision 025e950061133d42a92c3d0733d309581cf1e1ed)
1 #define TAO_DLL
2 
3 #include <petsc-private/taoimpl.h> /*I "petsctao.h" I*/
4 
5 PetscBool TaoRegisterAllCalled = PETSC_FALSE;
6 PetscFunctionList TaoList = NULL;
7 
8 PetscClassId TAO_CLASSID;
9 PetscLogEvent Tao_Solve, Tao_ObjectiveEval, Tao_GradientEval, Tao_ObjGradientEval, Tao_HessianEval, Tao_ConstraintsEval, Tao_JacobianEval;
10 
11 const char *TaoSubSetTypes[] = {  "subvec","mask","matrixfree","TaoSubSetType","TAO_SUBSET_",0};
12 
13 #undef __FUNCT__
14 #define __FUNCT__ "TaoCreate"
15 /*@
16   TaoCreate - Creates a TAO solver
17 
18   Collective on MPI_Comm
19 
20   Input Parameter:
21 . comm - MPI communicator
22 
23   Output Parameter:
24 . newtao - the new Tao context
25 
26   Available methods include:
27 +    nls - Newton's method with line search for unconstrained minimization
28 .    ntr - Newton's method with trust region for unconstrained minimization
29 .    ntl - Newton's method with trust region, line search for unconstrained minimization
30 .    lmvm - Limited memory variable metric method for unconstrained minimization
31 .    cg - Nonlinear conjugate gradient method for unconstrained minimization
32 .    nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
33 .    tron - Newton Trust Region method for bound constrained minimization
34 .    gpcg - Newton Trust Region method for quadratic bound constrained minimization
35 .    blmvm - Limited memory variable metric method for bound constrained minimization
36 .    lcl - Linearly constrained Lagrangian method for pde-constrained minimization
37 -    pounders - Model-based algorithm for nonlinear least squares
38 
39    Options Database Keys:
40 .   -tao_type - select which method TAO should use
41 
42    Level: beginner
43 
44 .seealso: TaoSolve(), TaoDestroy()
45 @*/
46 PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao)
47 {
48   PetscErrorCode ierr;
49   Tao            tao;
50 
51   PetscFunctionBegin;
52   PetscValidPointer(newtao,2);
53   *newtao = NULL;
54 
55   ierr = TaoInitializePackage();CHKERRQ(ierr);
56   ierr = TaoLineSearchInitializePackage();CHKERRQ(ierr);
57 
58   ierr = PetscHeaderCreate(tao,_p_Tao, struct _TaoOps, TAO_CLASSID,"Tao",0,0,comm,TaoDestroy,TaoView);CHKERRQ(ierr);
59   tao->ops->computeobjective=0;
60   tao->ops->computeobjectiveandgradient=0;
61   tao->ops->computegradient=0;
62   tao->ops->computehessian=0;
63   tao->ops->computeseparableobjective=0;
64   tao->ops->computeconstraints=0;
65   tao->ops->computejacobian=0;
66   tao->ops->computejacobianequality=0;
67   tao->ops->computejacobianinequality=0;
68   tao->ops->computeequalityconstraints=0;
69   tao->ops->computeinequalityconstraints=0;
70   tao->ops->convergencetest=TaoDefaultConvergenceTest;
71   tao->ops->convergencedestroy=0;
72   tao->ops->computedual=0;
73   tao->ops->setup=0;
74   tao->ops->solve=0;
75   tao->ops->view=0;
76   tao->ops->setfromoptions=0;
77   tao->ops->destroy=0;
78 
79   tao->solution=NULL;
80   tao->gradient=NULL;
81   tao->sep_objective = NULL;
82   tao->constraints=NULL;
83   tao->constraints_equality=NULL;
84   tao->constraints_inequality=NULL;
85   tao->stepdirection=NULL;
86   tao->XL = NULL;
87   tao->XU = NULL;
88   tao->IL = NULL;
89   tao->IU = NULL;
90   tao->DI = NULL;
91   tao->DE = NULL;
92   tao->hessian = NULL;
93   tao->hessian_pre = NULL;
94   tao->jacobian = NULL;
95   tao->jacobian_pre = NULL;
96   tao->jacobian_state = NULL;
97   tao->jacobian_state_pre = NULL;
98   tao->jacobian_state_inv = NULL;
99   tao->jacobian_design = NULL;
100   tao->jacobian_design_pre = NULL;
101   tao->jacobian_equality = NULL;
102   tao->jacobian_equality_pre = NULL;
103   tao->jacobian_inequality = NULL;
104   tao->jacobian_inequality_pre = NULL;
105   tao->state_is = NULL;
106   tao->design_is = NULL;
107 
108   tao->max_it     = 10000;
109   tao->max_funcs   = 10000;
110 #if defined(PETSC_USE_REAL_SINGLE)
111   tao->fatol       = 1e-5;
112   tao->frtol       = 1e-5;
113   tao->gatol       = 1e-5;
114   tao->grtol       = 1e-5;
115 #else
116   tao->fatol       = 1e-8;
117   tao->frtol       = 1e-8;
118   tao->gatol       = 1e-8;
119   tao->grtol       = 1e-8;
120 #endif
121   tao->gttol       = 0.0;
122   tao->catol       = 0.0;
123   tao->crtol       = 0.0;
124   tao->xtol        = 0.0;
125   tao->steptol     = 0.0;
126   tao->trust0      = PETSC_INFINITY;
127   tao->fmin        = PETSC_NINFINITY;
128   tao->hist_reset = PETSC_TRUE;
129   tao->hist_max = 0;
130   tao->hist_len = 0;
131   tao->hist_obj = NULL;
132   tao->hist_resid = NULL;
133   tao->hist_cnorm = NULL;
134 
135   tao->numbermonitors=0;
136   tao->viewsolution=PETSC_FALSE;
137   tao->viewhessian=PETSC_FALSE;
138   tao->viewgradient=PETSC_FALSE;
139   tao->viewjacobian=PETSC_FALSE;
140   tao->viewconstraints = PETSC_FALSE;
141 
142   ierr = TaoResetStatistics(tao);CHKERRQ(ierr);
143   *newtao = tao;
144   PetscFunctionReturn(0);
145 }
146 
147 #undef __FUNCT__
148 #define __FUNCT__ "TaoSolve"
149 /*@
150   TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
151 
152   Collective on Tao
153 
154   Input Parameters:
155 . tao - the Tao context
156 
157   Notes:
158   The user must set up the Tao with calls to TaoSetInitialVector(),
159   TaoSetObjectiveRoutine(),
160   TaoSetGradientRoutine(), and (if using 2nd order method) TaoSetHessianRoutine().
161 
162   Level: beginner
163 
164 .seealso: TaoCreate(), TaoSetObjectiveRoutine(), TaoSetGradientRoutine(), TaoSetHessianRoutine()
165  @*/
166 PetscErrorCode TaoSolve(Tao tao)
167 {
168   PetscErrorCode   ierr;
169   static PetscBool set = PETSC_FALSE;
170 
171   PetscFunctionBegin;
172   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
173   ierr = PetscCitationsRegister("@TechReport{tao-user-ref,\n"
174                                 "title   = {Toolkit for Advanced Optimization (TAO) Users Manual},\n"
175                                 "author  = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n"
176                                 "Institution = {Argonne National Laboratory},\n"
177                                 "Year   = 2014,\n"
178                                 "Number = {ANL/MCS-TM-322 - Revision 3.5},\n"
179                                 "url    = {http://www.mcs.anl.gov/tao}\n}\n",&set);
180 
181   ierr = TaoSetUp(tao);CHKERRQ(ierr);
182   ierr = TaoResetStatistics(tao);CHKERRQ(ierr);
183   if (tao->linesearch) {
184     ierr = TaoLineSearchReset(tao->linesearch);CHKERRQ(ierr);
185   }
186 
187   ierr = PetscLogEventBegin(Tao_Solve,tao,0,0,0);CHKERRQ(ierr);
188   if (tao->ops->solve){ ierr = (*tao->ops->solve)(tao);CHKERRQ(ierr); }
189   ierr = PetscLogEventEnd(Tao_Solve,tao,0,0,0);CHKERRQ(ierr);
190 
191   ierr = TaoViewFromOptions(tao,NULL,"-tao_view");CHKERRQ(ierr);
192 
193   if (tao->printreason) {
194     if (tao->reason > 0) {
195       ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve converged due to %s\n",TaoConvergedReasons[tao->reason]);CHKERRQ(ierr);
196     } else {
197       ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve did not converge due to %s\n",TaoConvergedReasons[tao->reason]);CHKERRQ(ierr);
198     }
199   }
200   PetscFunctionReturn(0);
201 }
202 
203 #undef __FUNCT__
204 #define __FUNCT__ "TaoSetUp"
205 /*@
206   TaoSetUp - Sets up the internal data structures for the later use
207   of a Tao solver
208 
209   Collective on tao
210 
211   Input Parameters:
212 . tao - the TAO context
213 
214   Notes:
215   The user will not need to explicitly call TaoSetUp(), as it will
216   automatically be called in TaoSolve().  However, if the user
217   desires to call it explicitly, it should come after TaoCreate()
218   and any TaoSetSomething() routines, but before TaoSolve().
219 
220   Level: advanced
221 
222 .seealso: TaoCreate(), TaoSolve()
223 @*/
224 PetscErrorCode TaoSetUp(Tao tao)
225 {
226   PetscErrorCode ierr;
227 
228   PetscFunctionBegin;
229   PetscValidHeaderSpecific(tao, TAO_CLASSID,1);
230   if (tao->setupcalled) PetscFunctionReturn(0);
231 
232   if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetInitialVector");
233   if (tao->ops->setup) {
234     ierr = (*tao->ops->setup)(tao);CHKERRQ(ierr);
235   }
236   tao->setupcalled = PETSC_TRUE;
237   PetscFunctionReturn(0);
238 }
239 
240 #undef __FUNCT__
241 #define __FUNCT__ "TaoDestroy"
242 /*@
243   TaoDestroy - Destroys the TAO context that was created with
244   TaoCreate()
245 
246   Collective on Tao
247 
248   Input Parameter:
249 . tao - the Tao context
250 
251   Level: beginner
252 
253 .seealso: TaoCreate(), TaoSolve()
254 @*/
255 PetscErrorCode TaoDestroy(Tao *tao)
256 {
257   PetscErrorCode ierr;
258 
259   PetscFunctionBegin;
260   if (!*tao) PetscFunctionReturn(0);
261   PetscValidHeaderSpecific(*tao,TAO_CLASSID,1);
262   if (--((PetscObject)*tao)->refct > 0) {*tao=0;PetscFunctionReturn(0);}
263 
264   if ((*tao)->ops->destroy) {
265     ierr = (*((*tao))->ops->destroy)(*tao);CHKERRQ(ierr);
266   }
267   ierr = KSPDestroy(&(*tao)->ksp);CHKERRQ(ierr);
268   ierr = TaoLineSearchDestroy(&(*tao)->linesearch);CHKERRQ(ierr);
269 
270   if ((*tao)->ops->convergencedestroy) {
271     ierr = (*(*tao)->ops->convergencedestroy)((*tao)->cnvP);CHKERRQ(ierr);
272     if ((*tao)->jacobian_state_inv) {
273       ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr);
274     }
275   }
276   ierr = VecDestroy(&(*tao)->solution);CHKERRQ(ierr);
277   ierr = VecDestroy(&(*tao)->gradient);CHKERRQ(ierr);
278 
279   ierr = VecDestroy(&(*tao)->XL);CHKERRQ(ierr);
280   ierr = VecDestroy(&(*tao)->XU);CHKERRQ(ierr);
281   ierr = VecDestroy(&(*tao)->IL);CHKERRQ(ierr);
282   ierr = VecDestroy(&(*tao)->IU);CHKERRQ(ierr);
283   ierr = VecDestroy(&(*tao)->DE);CHKERRQ(ierr);
284   ierr = VecDestroy(&(*tao)->DI);CHKERRQ(ierr);
285   ierr = VecDestroy(&(*tao)->constraints_equality);CHKERRQ(ierr);
286   ierr = VecDestroy(&(*tao)->constraints_inequality);CHKERRQ(ierr);
287   ierr = VecDestroy(&(*tao)->stepdirection);CHKERRQ(ierr);
288   ierr = MatDestroy(&(*tao)->hessian_pre);CHKERRQ(ierr);
289   ierr = MatDestroy(&(*tao)->hessian);CHKERRQ(ierr);
290   ierr = MatDestroy(&(*tao)->jacobian_pre);CHKERRQ(ierr);
291   ierr = MatDestroy(&(*tao)->jacobian);CHKERRQ(ierr);
292   ierr = MatDestroy(&(*tao)->jacobian_state_pre);CHKERRQ(ierr);
293   ierr = MatDestroy(&(*tao)->jacobian_state);CHKERRQ(ierr);
294   ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr);
295   ierr = MatDestroy(&(*tao)->jacobian_design);CHKERRQ(ierr);
296   ierr = MatDestroy(&(*tao)->jacobian_equality);CHKERRQ(ierr);
297   ierr = MatDestroy(&(*tao)->jacobian_equality_pre);CHKERRQ(ierr);
298   ierr = MatDestroy(&(*tao)->jacobian_inequality);CHKERRQ(ierr);
299   ierr = MatDestroy(&(*tao)->jacobian_inequality_pre);CHKERRQ(ierr);
300   ierr = ISDestroy(&(*tao)->state_is);CHKERRQ(ierr);
301   ierr = ISDestroy(&(*tao)->design_is);CHKERRQ(ierr);
302   ierr = TaoCancelMonitors(*tao);CHKERRQ(ierr);
303   ierr = PetscHeaderDestroy(tao);CHKERRQ(ierr);
304   PetscFunctionReturn(0);
305 }
306 
307 #undef __FUNCT__
308 #define __FUNCT__ "TaoSetFromOptions"
309 /*@
310   TaoSetFromOptions - Sets various Tao parameters from user
311   options.
312 
313   Collective on Tao
314 
315   Input Paremeter:
316 . tao - the Tao solver context
317 
318   options Database Keys:
319 + -tao_type <type> - The algorithm that TAO uses (lmvm, nls, etc.)
320 . -tao_fatol <fatol> - absolute error tolerance in function value
321 . -tao_frtol <frtol> - relative error tolerance in function value
322 . -tao_gatol <gatol> - absolute error tolerance for ||gradient||
323 . -tao_grtol <grtol> - relative error tolerance for ||gradient||
324 . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient
325 . -tao_max_it <max> - sets maximum number of iterations
326 . -tao_max_funcs <max> - sets maximum number of function evaluations
327 . -tao_fmin <fmin> - stop if function value reaches fmin
328 . -tao_steptol <tol> - stop if trust region radius less than <tol>
329 . -tao_trust0 <t> - initial trust region radius
330 . -tao_monitor - prints function value and residual at each iteration
331 . -tao_smonitor - same as tao_monitor, but truncates very small values
332 . -tao_cmonitor - prints function value, residual, and constraint norm at each iteration
333 . -tao_view_solution - prints solution vector at each iteration
334 . -tao_view_separableobjective - prints separable objective vector at each iteration
335 . -tao_view_step - prints step direction vector at each iteration
336 . -tao_view_gradient - prints gradient vector at each iteration
337 . -tao_draw_solution - graphically view solution vector at each iteration
338 . -tao_draw_step - graphically view step vector at each iteration
339 . -tao_draw_gradient - graphically view gradient at each iteration
340 . -tao_fd_gradient - use gradient computed with finite differences
341 . -tao_cancelmonitors - cancels all monitors (except those set with command line)
342 . -tao_view - prints information about the Tao after solving
343 - -tao_converged_reason - prints the reason TAO stopped iterating
344 
345   Notes:
346   To see all options, run your program with the -help option or consult the
347   user's manual. Should be called after TaoCreate() but before TaoSolve()
348 
349   Level: beginner
350 @*/
351 PetscErrorCode TaoSetFromOptions(Tao tao)
352 {
353   PetscErrorCode ierr;
354   const TaoType  default_type = TAOLMVM;
355   const char     *prefix;
356   char           type[256], monfilename[PETSC_MAX_PATH_LEN];
357   PetscViewer    monviewer;
358   PetscBool      flg;
359   MPI_Comm       comm;
360 
361   PetscFunctionBegin;
362   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
363   ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
364   ierr = TaoGetOptionsPrefix(tao,&prefix);
365   /* So no warnings are given about unused options */
366   ierr = PetscOptionsHasName(prefix,"-tao_ls_type",&flg);
367 
368   ierr = PetscObjectOptionsBegin((PetscObject)tao);CHKERRQ(ierr);
369   {
370     if (!TaoRegisterAllCalled) {
371       ierr = TaoRegisterAll();CHKERRQ(ierr);
372     }
373     if (((PetscObject)tao)->type_name) {
374       default_type = ((PetscObject)tao)->type_name;
375     }
376     /* Check for type from options */
377     ierr = PetscOptionsFList("-tao_type","Tao Solver type","TaoSetType",TaoList,default_type,type,256,&flg);CHKERRQ(ierr);
378     if (flg) {
379       ierr = TaoSetType(tao,type);CHKERRQ(ierr);
380     } else if (!((PetscObject)tao)->type_name) {
381       ierr = TaoSetType(tao,default_type);
382     }
383 
384     ierr = PetscOptionsReal("-tao_fatol","Stop if solution within","TaoSetTolerances",tao->fatol,&tao->fatol,&flg);CHKERRQ(ierr);
385     ierr = PetscOptionsReal("-tao_frtol","Stop if relative solution within","TaoSetTolerances",tao->frtol,&tao->frtol,&flg);CHKERRQ(ierr);
386     ierr = PetscOptionsReal("-tao_catol","Stop if constraints violations within","TaoSetConstraintTolerances",tao->catol,&tao->catol,&flg);CHKERRQ(ierr);
387     ierr = PetscOptionsReal("-tao_crtol","Stop if relative contraint violations within","TaoSetConstraintTolerances",tao->crtol,&tao->crtol,&flg);CHKERRQ(ierr);
388     ierr = PetscOptionsReal("-tao_gatol","Stop if norm of gradient less than","TaoSetTolerances",tao->gatol,&tao->gatol,&flg);CHKERRQ(ierr);
389     ierr = PetscOptionsReal("-tao_grtol","Stop if norm of gradient divided by the function value is less than","TaoSetTolerances",tao->grtol,&tao->grtol,&flg);CHKERRQ(ierr);
390     ierr = PetscOptionsReal("-tao_gttol","Stop if the norm of the gradient is less than the norm of the initial gradient times tol","TaoSetTolerances",tao->gttol,&tao->gttol,&flg);CHKERRQ(ierr);
391     ierr = PetscOptionsInt("-tao_max_it","Stop if iteration number exceeds","TaoSetMaximumIterations",tao->max_it,&tao->max_it,&flg);CHKERRQ(ierr);
392     ierr = PetscOptionsInt("-tao_max_funcs","Stop if number of function evaluations exceeds","TaoSetMaximumFunctionEvaluations",tao->max_funcs,&tao->max_funcs,&flg);CHKERRQ(ierr);
393     ierr = PetscOptionsReal("-tao_fmin","Stop if function less than","TaoSetFunctionLowerBound",tao->fmin,&tao->fmin,&flg);CHKERRQ(ierr);
394     ierr = PetscOptionsReal("-tao_steptol","Stop if step size or trust region radius less than","",tao->steptol,&tao->steptol,&flg);CHKERRQ(ierr);
395     ierr = PetscOptionsReal("-tao_trust0","Initial trust region radius","TaoSetTrustRegionRadius",tao->trust0,&tao->trust0,&flg);CHKERRQ(ierr);
396 
397     ierr = PetscOptionsString("-tao_view_solution","view solution vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
398     if (flg) {
399       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
400       ierr = TaoSetMonitor(tao,TaoSolutionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
401     }
402 
403     ierr = PetscOptionsBool("-tao_converged_reason","Print reason for TAO converged","TaoSolve",flg,&flg,NULL);CHKERRQ(ierr);
404     if (flg) {
405       tao->printreason = PETSC_TRUE;
406     }
407     ierr = PetscOptionsString("-tao_view_gradient","view gradient vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
408     if (flg) {
409       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
410       ierr = TaoSetMonitor(tao,TaoGradientMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
411     }
412 
413     ierr = PetscOptionsString("-tao_view_stepdirection","view step direction vector after each iteration","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
414     if (flg) {
415       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
416       ierr = TaoSetMonitor(tao,TaoStepDirectionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
417     }
418 
419     ierr = PetscOptionsString("-tao_view_separableobjective","view separable objective vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
420     if (flg) {
421       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
422       ierr = TaoSetMonitor(tao,TaoSeparableObjectiveMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
423     }
424 
425     ierr = PetscOptionsString("-tao_monitor","Use the default convergence monitor","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
426     if (flg) {
427       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
428       ierr = TaoSetMonitor(tao,TaoDefaultMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
429     }
430 
431     ierr = PetscOptionsString("-tao_smonitor","Use the short convergence monitor","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
432     if (flg) {
433       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
434       ierr = TaoSetMonitor(tao,TaoDefaultSMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
435     }
436 
437     ierr = PetscOptionsString("-tao_cmonitor","Use the default convergence monitor with constraint norm","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
438     if (flg) {
439       ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr);
440       ierr = TaoSetMonitor(tao,TaoDefaultCMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
441     }
442 
443 
444     ierr = PetscOptionsBool("-tao_cancelmonitors","cancel all monitors and call any registered destroy routines","TaoCancelMonitors",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
445     if (flg) {ierr = TaoCancelMonitors(tao);CHKERRQ(ierr);}
446 
447     ierr = PetscOptionsBool("-tao_draw_solution","Plot solution vector at each iteration","TaoSetMonitor",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
448     if (flg) {
449       ierr = TaoSetMonitor(tao,TaoDrawSolutionMonitor,NULL,NULL);CHKERRQ(ierr);
450     }
451 
452     ierr = PetscOptionsBool("-tao_draw_step","plots step direction at each iteration","TaoSetMonitor",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
453     if (flg) {
454       ierr = TaoSetMonitor(tao,TaoDrawStepMonitor,NULL,NULL);CHKERRQ(ierr);
455     }
456 
457     ierr = PetscOptionsBool("-tao_draw_gradient","plots gradient at each iteration","TaoSetMonitor",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
458     if (flg) {
459       ierr = TaoSetMonitor(tao,TaoDrawGradientMonitor,NULL,NULL);CHKERRQ(ierr);
460     }
461     ierr = PetscOptionsBool("-tao_fd_gradient","compute gradient using finite differences","TaoDefaultComputeGradient",PETSC_FALSE,&flg,NULL);CHKERRQ(ierr);
462     if (flg) {
463       ierr = TaoSetGradientRoutine(tao,TaoDefaultComputeGradient,NULL);CHKERRQ(ierr);
464     }
465     ierr = PetscOptionsEnum("-tao_subset_type","subset type", "", TaoSubSetTypes,(PetscEnum)tao->subset_type, (PetscEnum*)&tao->subset_type, 0);CHKERRQ(ierr);
466 
467     if (tao->ops->setfromoptions) {
468       ierr = (*tao->ops->setfromoptions)(tao);CHKERRQ(ierr);
469     }
470   }
471   ierr = PetscOptionsEnd();CHKERRQ(ierr);
472   PetscFunctionReturn(0);
473 }
474 
475 #undef __FUNCT__
476 #define __FUNCT__ "TaoView"
477 /*@C
478   TaoView - Prints information about the Tao
479 
480   Collective on Tao
481 
482   InputParameters:
483 + tao - the Tao context
484 - viewer - visualization context
485 
486   Options Database Key:
487 . -tao_view - Calls TaoView() at the end of TaoSolve()
488 
489   Notes:
490   The available visualization contexts include
491 +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
492 -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
493          output where only the first processor opens
494          the file.  All other processors send their
495          data to the first processor to print.
496 
497   Level: beginner
498 
499 .seealso: PetscViewerASCIIOpen()
500 @*/
501 PetscErrorCode TaoView(Tao tao, PetscViewer viewer)
502 {
503   PetscErrorCode      ierr;
504   PetscBool           isascii,isstring;
505   const TaoType type;
506 
507   PetscFunctionBegin;
508   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
509   if (!viewer) {
510     ierr = PetscViewerASCIIGetStdout(((PetscObject)tao)->comm,&viewer);CHKERRQ(ierr);
511   }
512   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
513   PetscCheckSameComm(tao,1,viewer,2);
514 
515   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
516   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
517   if (isascii) {
518     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)tao,viewer);CHKERRQ(ierr);
519     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
520 
521     if (tao->ops->view) {
522       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
523       ierr = (*tao->ops->view)(tao,viewer);CHKERRQ(ierr);
524       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
525     }
526     if (tao->linesearch) {
527       ierr = PetscObjectPrintClassNamePrefixType((PetscObject)(tao->linesearch),viewer);CHKERRQ(ierr);
528     }
529     if (tao->ksp) {
530       ierr = PetscObjectPrintClassNamePrefixType((PetscObject)(tao->ksp),viewer);CHKERRQ(ierr);
531       ierr = PetscViewerASCIIPrintf(viewer,"total KSP iterations: %D\n",tao->ksp_its);CHKERRQ(ierr);
532     }
533     if (tao->XL || tao->XU) {
534       ierr = PetscViewerASCIIPrintf(viewer,"Active Set subset type: %s\n",TaoSubSetTypes[tao->subset_type]);
535     }
536 
537     ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: fatol=%g,",(double)tao->fatol);CHKERRQ(ierr);
538     ierr=PetscViewerASCIIPrintf(viewer," frtol=%g\n",(double)tao->frtol);CHKERRQ(ierr);
539 
540     ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: gatol=%g,",(double)tao->gatol);CHKERRQ(ierr);
541     ierr=PetscViewerASCIIPrintf(viewer," steptol=%g,",(double)tao->steptol);CHKERRQ(ierr);
542     ierr=PetscViewerASCIIPrintf(viewer," gttol=%g\n",(double)tao->gttol);CHKERRQ(ierr);
543 
544     ierr = PetscViewerASCIIPrintf(viewer,"Residual in Function/Gradient:=%g\n",(double)tao->residual);CHKERRQ(ierr);
545 
546     if (tao->cnorm>0 || tao->catol>0 || tao->crtol>0){
547       ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances:");CHKERRQ(ierr);
548       ierr=PetscViewerASCIIPrintf(viewer," catol=%g,",(double)tao->catol);CHKERRQ(ierr);
549       ierr=PetscViewerASCIIPrintf(viewer," crtol=%g\n",(double)tao->crtol);CHKERRQ(ierr);
550       ierr = PetscViewerASCIIPrintf(viewer,"Residual in Constraints:=%g\n",(double)tao->cnorm);CHKERRQ(ierr);
551     }
552 
553     if (tao->trust < tao->steptol){
554       ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: steptol=%g\n",(double)tao->steptol);CHKERRQ(ierr);
555       ierr=PetscViewerASCIIPrintf(viewer,"Final trust region radius:=%g\n",(double)tao->trust);CHKERRQ(ierr);
556     }
557 
558     if (tao->fmin>-1.e25){
559       ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: function minimum=%g\n",(double)tao->fmin);CHKERRQ(ierr);
560     }
561     ierr = PetscViewerASCIIPrintf(viewer,"Objective value=%g\n",(double)tao->fc);CHKERRQ(ierr);
562 
563     ierr = PetscViewerASCIIPrintf(viewer,"total number of iterations=%D,          ",tao->niter);CHKERRQ(ierr);
564     ierr = PetscViewerASCIIPrintf(viewer,"              (max: %D)\n",tao->max_it);CHKERRQ(ierr);
565 
566     if (tao->nfuncs>0){
567       ierr = PetscViewerASCIIPrintf(viewer,"total number of function evaluations=%D,",tao->nfuncs);CHKERRQ(ierr);
568       ierr = PetscViewerASCIIPrintf(viewer,"                max: %D\n",tao->max_funcs);CHKERRQ(ierr);
569     }
570     if (tao->ngrads>0){
571       ierr = PetscViewerASCIIPrintf(viewer,"total number of gradient evaluations=%D,",tao->ngrads);CHKERRQ(ierr);
572       ierr = PetscViewerASCIIPrintf(viewer,"                max: %D\n",tao->max_funcs);CHKERRQ(ierr);
573     }
574     if (tao->nfuncgrads>0){
575       ierr = PetscViewerASCIIPrintf(viewer,"total number of function/gradient evaluations=%D,",tao->nfuncgrads);CHKERRQ(ierr);
576       ierr = PetscViewerASCIIPrintf(viewer,"    (max: %D)\n",tao->max_funcs);CHKERRQ(ierr);
577     }
578     if (tao->nhess>0){
579       ierr = PetscViewerASCIIPrintf(viewer,"total number of Hessian evaluations=%D\n",tao->nhess);CHKERRQ(ierr);
580     }
581     /*  if (tao->linear_its>0){
582      ierr = PetscViewerASCIIPrintf(viewer,"  total Krylov method iterations=%D\n",tao->linear_its);CHKERRQ(ierr);
583      }*/
584     if (tao->nconstraints>0){
585       ierr = PetscViewerASCIIPrintf(viewer,"total number of constraint function evaluations=%D\n",tao->nconstraints);CHKERRQ(ierr);
586     }
587     if (tao->njac>0){
588       ierr = PetscViewerASCIIPrintf(viewer,"total number of Jacobian evaluations=%D\n",tao->njac);CHKERRQ(ierr);
589     }
590 
591     if (tao->reason>0){
592       ierr = PetscViewerASCIIPrintf(viewer,    "Solution converged: ");CHKERRQ(ierr);
593       switch (tao->reason) {
594       case TAO_CONVERGED_FATOL:
595         ierr = PetscViewerASCIIPrintf(viewer,"estimated f(x)-f(X*) <= fatol\n");CHKERRQ(ierr);
596         break;
597       case TAO_CONVERGED_FRTOL:
598         ierr = PetscViewerASCIIPrintf(viewer,"estimated |f(x)-f(X*)|/|f(X*)| <= frtol\n");CHKERRQ(ierr);
599         break;
600       case TAO_CONVERGED_GATOL:
601         ierr = PetscViewerASCIIPrintf(viewer," ||g(X)|| <= gatol\n");CHKERRQ(ierr);
602         break;
603       case TAO_CONVERGED_GRTOL:
604         ierr = PetscViewerASCIIPrintf(viewer," ||g(X)||/|f(X)| <= grtol\n");CHKERRQ(ierr);
605         break;
606       case TAO_CONVERGED_GTTOL:
607         ierr = PetscViewerASCIIPrintf(viewer," ||g(X)||/||g(X0)|| <= gttol\n");CHKERRQ(ierr);
608         break;
609       case TAO_CONVERGED_STEPTOL:
610         ierr = PetscViewerASCIIPrintf(viewer," Steptol -- step size small\n");CHKERRQ(ierr);
611         break;
612       case TAO_CONVERGED_MINF:
613         ierr = PetscViewerASCIIPrintf(viewer," Minf --  f < fmin\n");CHKERRQ(ierr);
614         break;
615       case TAO_CONVERGED_USER:
616         ierr = PetscViewerASCIIPrintf(viewer," User Terminated\n");CHKERRQ(ierr);
617         break;
618       default:
619         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
620         break;
621       }
622 
623     } else {
624       ierr = PetscViewerASCIIPrintf(viewer,"Solver terminated: %D",tao->reason);CHKERRQ(ierr);
625       switch (tao->reason) {
626       case TAO_DIVERGED_MAXITS:
627         ierr = PetscViewerASCIIPrintf(viewer," Maximum Iterations\n");CHKERRQ(ierr);
628         break;
629       case TAO_DIVERGED_NAN:
630         ierr = PetscViewerASCIIPrintf(viewer," NAN or Inf encountered\n");CHKERRQ(ierr);
631         break;
632       case TAO_DIVERGED_MAXFCN:
633         ierr = PetscViewerASCIIPrintf(viewer," Maximum Function Evaluations\n");CHKERRQ(ierr);
634         break;
635       case TAO_DIVERGED_LS_FAILURE:
636         ierr = PetscViewerASCIIPrintf(viewer," Line Search Failure\n");CHKERRQ(ierr);
637         break;
638       case TAO_DIVERGED_TR_REDUCTION:
639         ierr = PetscViewerASCIIPrintf(viewer," Trust Region too small\n");CHKERRQ(ierr);
640         break;
641       case TAO_DIVERGED_USER:
642         ierr = PetscViewerASCIIPrintf(viewer," User Terminated\n");CHKERRQ(ierr);
643         break;
644       default:
645         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
646         break;
647       }
648     }
649     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
650   } else if (isstring) {
651     ierr = TaoGetType(tao,&type);CHKERRQ(ierr);
652     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
653   }
654   PetscFunctionReturn(0);
655 }
656 
657 #undef __FUNCT__
658 #define __FUNCT__ "TaoSetTolerances"
659 /*@
660   TaoSetTolerances - Sets parameters used in TAO convergence tests
661 
662   Logically collective on Tao
663 
664   Input Parameters:
665 + tao - the Tao context
666 . fatol - absolute convergence tolerance
667 . frtol - relative convergence tolerance
668 . gatol - stop if norm of gradient is less than this
669 . grtol - stop if relative norm of gradient is less than this
670 - gttol - stop if norm of gradient is reduced by this factor
671 
672   Options Database Keys:
673 + -tao_fatol <fatol> - Sets fatol
674 . -tao_frtol <frtol> - Sets frtol
675 . -tao_gatol <gatol> - Sets gatol
676 . -tao_grtol <grtol> - Sets grtol
677 - -tao_gttol <gttol> - Sets gttol
678 
679   Stopping Criteria:
680 $ f(X) - f(X*) (estimated)            <= fatol
681 $ |f(X) - f(X*)| (estimated) / |f(X)| <= frtol
682 $ ||g(X)||                            <= gatol
683 $ ||g(X)|| / |f(X)|                   <= grtol
684 $ ||g(X)|| / ||g(X0)||                <= gttol
685 
686   Notes:
687   Use PETSC_DEFAULT to leave one or more tolerances unchanged.
688 
689   Level: beginner
690 
691 .seealso: TaoGetTolerances()
692 
693 @*/
694 PetscErrorCode TaoSetTolerances(Tao tao, PetscReal fatol, PetscReal frtol, PetscReal gatol, PetscReal grtol, PetscReal gttol)
695 {
696   PetscErrorCode ierr;
697 
698   PetscFunctionBegin;
699   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
700 
701   if (fatol != PETSC_DEFAULT) {
702     if (fatol<0) {
703       ierr = PetscInfo(tao,"Tried to set negative fatol -- ignored.");CHKERRQ(ierr);
704     } else {
705       tao->fatol = PetscMax(0,fatol);
706     }
707   }
708   if (frtol != PETSC_DEFAULT) {
709     if (frtol<0) {
710       ierr = PetscInfo(tao,"Tried to set negative frtol -- ignored.");CHKERRQ(ierr);
711     } else {
712       tao->frtol = PetscMax(0,frtol);
713     }
714   }
715 
716   if (gatol != PETSC_DEFAULT) {
717     if (gatol<0) {
718       ierr = PetscInfo(tao,"Tried to set negative gatol -- ignored.");CHKERRQ(ierr);
719     } else {
720       tao->gatol = PetscMax(0,gatol);
721     }
722   }
723 
724   if (grtol != PETSC_DEFAULT) {
725     if (grtol<0) {
726       ierr = PetscInfo(tao,"Tried to set negative grtol -- ignored.");CHKERRQ(ierr);
727     } else {
728       tao->grtol = PetscMax(0,grtol);
729     }
730   }
731 
732   if (gttol != PETSC_DEFAULT) {
733     if (gttol<0) {
734       ierr = PetscInfo(tao,"Tried to set negative gttol -- ignored.");CHKERRQ(ierr);
735     } else {
736       tao->gttol = PetscMax(0,gttol);
737     }
738   }
739   PetscFunctionReturn(0);
740 }
741 
742 #undef __FUNCT__
743 #define __FUNCT__ "TaoSetConstraintTolerances"
744 /*@
745   TaoSetConstraintTolerances - Sets constraint tolerance parameters used in TAO  convergence tests
746 
747   Logically collective on Tao
748 
749   Input Parameters:
750 + tao - the Tao context
751 . catol - absolute constraint tolerance, constraint norm must be less than catol for used for fatol, gatol convergence criteria
752 - crtol - relative contraint tolerance, constraint norm must be less than crtol for used for fatol, gatol, gttol convergence criteria
753 
754   Options Database Keys:
755 + -tao_catol <catol> - Sets catol
756 - -tao_crtol <crtol> - Sets crtol
757 
758   Level: intermediate
759 
760 .seealso: TaoGetTolerances(), TaoGetConstraintTolerances(), TaoSetTolerances()
761 
762 @*/
763 PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol)
764 {
765   PetscErrorCode ierr;
766 
767   PetscFunctionBegin;
768   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
769 
770   if (catol != PETSC_DEFAULT) {
771     if (catol<0) {
772       ierr = PetscInfo(tao,"Tried to set negative catol -- ignored.");CHKERRQ(ierr);
773     } else {
774       tao->catol = PetscMax(0,catol);
775     }
776   }
777 
778   if (crtol != PETSC_DEFAULT) {
779     if (crtol<0) {
780       ierr = PetscInfo(tao,"Tried to set negative crtol -- ignored.");CHKERRQ(ierr);
781     } else {
782       tao->crtol = PetscMax(0,crtol);
783     }
784   }
785   PetscFunctionReturn(0);
786 }
787 
788 #undef __FUNCT__
789 #define __FUNCT__ "TaoGetConstraintTolerances"
790 /*@
791   TaoGetConstraintTolerances - Gets constraint tolerance parameters used in TAO  convergence tests
792 
793   Not ollective
794 
795   Input Parameter:
796 . tao - the Tao context
797 
798   Output Parameter:
799 + catol - absolute constraint tolerance, constraint norm must be less than catol for used for fatol, gatol convergence criteria
800 - crtol - relative contraint tolerance, constraint norm must be less than crtol for used for fatol, gatol, gttol convergence criteria
801 
802   Level: intermediate
803 
804 .seealso: TaoGetTolerances(), TaoSetTolerances(), TaoSetConstraintTolerances()
805 
806 @*/
807 PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol)
808 {
809   PetscFunctionBegin;
810   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
811   if (catol) *catol = tao->catol;
812   if (crtol) *crtol = tao->crtol;
813   PetscFunctionReturn(0);
814 }
815 
816 #undef __FUNCT__
817 #define __FUNCT__ "TaoSetFunctionLowerBound"
818 /*@
819    TaoSetFunctionLowerBound - Sets a bound on the solution objective value.
820    When an approximate solution with an objective value below this number
821    has been found, the solver will terminate.
822 
823    Logically Collective on Tao
824 
825    Input Parameters:
826 +  tao - the Tao solver context
827 -  fmin - the tolerance
828 
829    Options Database Keys:
830 .    -tao_fmin <fmin> - sets the minimum function value
831 
832    Level: intermediate
833 
834 .seealso: TaoSetTolerances()
835 @*/
836 PetscErrorCode TaoSetFunctionLowerBound(Tao tao,PetscReal fmin)
837 {
838   PetscFunctionBegin;
839   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
840   tao->fmin = fmin;
841   PetscFunctionReturn(0);
842 }
843 
844 #undef __FUNCT__
845 #define __FUNCT__ "TaoGetFunctionLowerBound"
846 /*@
847    TaoGetFunctionLowerBound - Sets a bound on the solution objective value.
848    When an approximate solution with an objective value below this number
849    has been found, the solver will terminate.
850 
851    Not collective on Tao
852 
853    Input Parameters:
854 .  tao - the Tao solver context
855 
856    OutputParameters:
857 .  fmin - the minimum function value
858 
859    Level: intermediate
860 
861 .seealso: TaoSetFunctionLowerBound()
862 @*/
863 PetscErrorCode TaoGetFunctionLowerBound(Tao tao,PetscReal *fmin)
864 {
865   PetscFunctionBegin;
866   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
867   *fmin = tao->fmin;
868   PetscFunctionReturn(0);
869 }
870 
871 #undef __FUNCT__
872 #define __FUNCT__ "TaoSetMaximumFunctionEvaluations"
873 /*@
874    TaoSetMaximumFunctionEvaluations - Sets a maximum number of
875    function evaluations.
876 
877    Logically Collective on Tao
878 
879    Input Parameters:
880 +  tao - the Tao solver context
881 -  nfcn - the maximum number of function evaluations (>=0)
882 
883    Options Database Keys:
884 .    -tao_max_funcs <nfcn> - sets the maximum number of function evaluations
885 
886    Level: intermediate
887 
888 .seealso: TaoSetTolerances(), TaoSetMaximumIterations()
889 @*/
890 
891 PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao,PetscInt nfcn)
892 {
893   PetscFunctionBegin;
894   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
895   tao->max_funcs = PetscMax(0,nfcn);
896   PetscFunctionReturn(0);
897 }
898 
899 #undef __FUNCT__
900 #define __FUNCT__ "TaoGetMaximumFunctionEvaluations"
901 /*@
902    TaoGetMaximumFunctionEvaluations - Sets a maximum number of
903    function evaluations.
904 
905    Not Collective
906 
907    Input Parameters:
908 .  tao - the Tao solver context
909 
910    Output Parameters:
911 .  nfcn - the maximum number of function evaluations
912 
913    Level: intermediate
914 
915 .seealso: TaoSetMaximumFunctionEvaluations(), TaoGetMaximumIterations()
916 @*/
917 
918 PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao,PetscInt *nfcn)
919 {
920   PetscFunctionBegin;
921   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
922   *nfcn = tao->max_funcs;
923   PetscFunctionReturn(0);
924 }
925 
926 #undef __FUNCT__
927 #define __FUNCT__ "TaoSetMaximumIterations"
928 /*@
929    TaoSetMaximumIterations - Sets a maximum number of iterates.
930 
931    Logically Collective on Tao
932 
933    Input Parameters:
934 +  tao - the Tao solver context
935 -  maxits - the maximum number of iterates (>=0)
936 
937    Options Database Keys:
938 .    -tao_max_it <its> - sets the maximum number of iterations
939 
940    Level: intermediate
941 
942 .seealso: TaoSetTolerances(), TaoSetMaximumFunctionEvaluations()
943 @*/
944 PetscErrorCode TaoSetMaximumIterations(Tao tao,PetscInt maxits)
945 {
946   PetscFunctionBegin;
947   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
948   tao->max_it = PetscMax(0,maxits);
949   PetscFunctionReturn(0);
950 }
951 
952 #undef __FUNCT__
953 #define __FUNCT__ "TaoGetMaximumIterations"
954 /*@
955    TaoGetMaximumIterations - Sets a maximum number of iterates.
956 
957    Not Collective
958 
959    Input Parameters:
960 .  tao - the Tao solver context
961 
962    Output Parameters:
963 .  maxits - the maximum number of iterates
964 
965    Level: intermediate
966 
967 .seealso: TaoSetMaximumIterations(), TaoGetMaximumFunctionEvaluations()
968 @*/
969 PetscErrorCode TaoGetMaximumIterations(Tao tao,PetscInt *maxits)
970 {
971   PetscFunctionBegin;
972   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
973   *maxits = tao->max_it;
974   PetscFunctionReturn(0);
975 }
976 
977 #undef __FUNCT__
978 #define __FUNCT__ "TaoSetInitialTrustRegionRadius"
979 /*@
980    TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
981 
982    Logically collective on Tao
983 
984    Input Parameter:
985 +  tao - a TAO optimization solver
986 -  radius - the trust region radius
987 
988    Level: intermediate
989 
990    Options Database Key:
991 .  -tao_trust0 <t0> - sets initial trust region radius
992 
993 .seealso: TaoGetTrustRegionRadius(), TaoSetTrustRegionTolerance()
994 @*/
995 PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
996 {
997   PetscFunctionBegin;
998   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
999   tao->trust0 = PetscMax(0.0,radius);
1000   PetscFunctionReturn(0);
1001 }
1002 
1003 #undef __FUNCT__
1004 #define __FUNCT__ "TaoGetInitialTrustRegionRadius"
1005 /*@
1006    TaoGetInitialTrustRegionRadius - Sets the initial trust region radius.
1007 
1008    Not Collective
1009 
1010    Input Parameter:
1011 .  tao - a TAO optimization solver
1012 
1013    Output Parameter:
1014 .  radius - the trust region radius
1015 
1016    Level: intermediate
1017 
1018 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetCurrentTrustRegionRadius()
1019 @*/
1020 PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1021 {
1022   PetscFunctionBegin;
1023   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1024   *radius = tao->trust0;
1025   PetscFunctionReturn(0);
1026 }
1027 
1028 #undef __FUNCT__
1029 #define __FUNCT__ "TaoGetCurrentTrustRegionRadius"
1030 /*@
1031    TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1032 
1033    Not Collective
1034 
1035    Input Parameter:
1036 .  tao - a TAO optimization solver
1037 
1038    Output Parameter:
1039 .  radius - the trust region radius
1040 
1041    Level: intermediate
1042 
1043 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetInitialTrustRegionRadius()
1044 @*/
1045 PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1046 {
1047   PetscFunctionBegin;
1048   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1049   *radius = tao->trust;
1050   PetscFunctionReturn(0);
1051 }
1052 
1053 #undef __FUNCT__
1054 #define __FUNCT__ "TaoGetTolerances"
1055 /*@
1056   TaoGetTolerances - gets the current values of tolerances
1057 
1058   Not Collective
1059 
1060   Input Parameters:
1061 . tao - the Tao context
1062 
1063   Output Parameters:
1064 + fatol - absolute convergence tolerance
1065 . frtol - relative convergence tolerance
1066 . gatol - stop if norm of gradient is less than this
1067 . grtol - stop if relative norm of gradient is less than this
1068 - gttol - stop if norm of gradient is reduced by a this factor
1069 
1070   Note: NULL can be used as an argument if not all tolerances values are needed
1071 
1072 .seealso TaoSetTolerances()
1073 
1074   Level: intermediate
1075 @*/
1076 PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *fatol, PetscReal *frtol, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1077 {
1078   PetscFunctionBegin;
1079   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1080   if (fatol) *fatol=tao->fatol;
1081   if (frtol) *frtol=tao->frtol;
1082   if (gatol) *gatol=tao->gatol;
1083   if (grtol) *grtol=tao->grtol;
1084   if (gttol) *gttol=tao->gttol;
1085   PetscFunctionReturn(0);
1086 }
1087 
1088 #undef __FUNCT__
1089 #define __FUNCT__ "TaoGetKSP"
1090 /*@
1091   TaoGetKSP - Gets the linear solver used by the optimization solver.
1092   Application writers should use TaoGetKSP if they need direct access
1093   to the PETSc KSP object.
1094 
1095   Not Collective
1096 
1097    Input Parameters:
1098 .  tao - the TAO solver
1099 
1100    Output Parameters:
1101 .  ksp - the KSP linear solver used in the optimization solver
1102 
1103    Level: intermediate
1104 
1105 @*/
1106 PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
1107 {
1108   PetscFunctionBegin;
1109   *ksp = tao->ksp;
1110   PetscFunctionReturn(0);
1111 }
1112 
1113 #undef __FUNCT__
1114 #define __FUNCT__ "TaoGetLinearSolveIterations"
1115 /*@
1116    TaoGetLinearSolveIterations - Gets the total number of linear iterations
1117    used by the TAO solver
1118 
1119    Not Collective
1120 
1121    Input Parameter:
1122 .  tao - TAO context
1123 
1124    Output Parameter:
1125 .  lits - number of linear iterations
1126 
1127    Notes:
1128    This counter is reset to zero for each successive call to TaoSolve()
1129 
1130    Level: intermediate
1131 
1132 .keywords: TAO
1133 
1134 .seealso:  TaoGetKSP()
1135 @*/
1136 PetscErrorCode  TaoGetLinearSolveIterations(Tao tao,PetscInt *lits)
1137 {
1138   PetscFunctionBegin;
1139   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1140   PetscValidIntPointer(lits,2);
1141   *lits = tao->ksp_its;
1142   PetscFunctionReturn(0);
1143 }
1144 
1145 #undef __FUNCT__
1146 #define __FUNCT__ "TaoGetLineSearch"
1147 /*@
1148   TaoGetLineSearch - Gets the line search used by the optimization solver.
1149   Application writers should use TaoGetLineSearch if they need direct access
1150   to the TaoLineSearch object.
1151 
1152   Not Collective
1153 
1154    Input Parameters:
1155 .  tao - the TAO solver
1156 
1157    Output Parameters:
1158 .  ls - the line search used in the optimization solver
1159 
1160    Level: intermediate
1161 
1162 @*/
1163 PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
1164 {
1165   PetscFunctionBegin;
1166   *ls = tao->linesearch;
1167   PetscFunctionReturn(0);
1168 }
1169 
1170 #undef __FUNCT__
1171 #define __FUNCT__ "TaoAddLineSearchCounts"
1172 /*@
1173   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1174   in the line search to the running total.
1175 
1176    Input Parameters:
1177 +  tao - the TAO solver
1178 -  ls - the line search used in the optimization solver
1179 
1180    Level: developer
1181 
1182 .seealso: TaoLineSearchApply()
1183 @*/
1184 PetscErrorCode TaoAddLineSearchCounts(Tao tao)
1185 {
1186   PetscErrorCode ierr;
1187   PetscBool      flg;
1188   PetscInt       nfeval,ngeval,nfgeval;
1189 
1190   PetscFunctionBegin;
1191   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1192   if (tao->linesearch) {
1193     ierr = TaoLineSearchIsUsingTaoRoutines(tao->linesearch,&flg);
1194     if (flg == PETSC_FALSE) {
1195       ierr = TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch,&nfeval,&ngeval,&nfgeval);CHKERRQ(ierr);
1196       tao->nfuncs+=nfeval;
1197       tao->ngrads+=ngeval;
1198       tao->nfuncgrads+=nfgeval;
1199     }
1200   }
1201   PetscFunctionReturn(0);
1202 }
1203 
1204 #undef __FUNCT__
1205 #define __FUNCT__ "TaoGetSolutionVector"
1206 /*@
1207   TaoGetSolutionVector - Returns the vector with the current TAO solution
1208 
1209   Not Collective
1210 
1211   Input Parameter:
1212 . tao - the Tao context
1213 
1214   Output Parameter:
1215 . X - the current solution
1216 
1217   Level: intermediate
1218 
1219   Note:  The returned vector will be the same object that was passed into TaoSetInitialVector()
1220 @*/
1221 PetscErrorCode TaoGetSolutionVector(Tao tao, Vec *X)
1222 {
1223   PetscFunctionBegin;
1224   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1225   *X = tao->solution;
1226   PetscFunctionReturn(0);
1227 }
1228 
1229 #undef __FUNCT__
1230 #define __FUNCT__ "TaoGetGradientVector"
1231 /*@
1232   TaoGetGradientVector - Returns the vector with the current TAO gradient
1233 
1234   Not Collective
1235 
1236   Input Parameter:
1237 . tao - the Tao context
1238 
1239   Output Parameter:
1240 . G - the current solution
1241 
1242   Level: intermediate
1243 @*/
1244 PetscErrorCode TaoGetGradientVector(Tao tao, Vec *G)
1245 {
1246   PetscFunctionBegin;
1247   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1248   *G = tao->gradient;
1249   PetscFunctionReturn(0);
1250 }
1251 
1252 #undef __FUNCT__
1253 #define __FUNCT__ "TaoResetStatistics"
1254 /*@
1255    TaoResetStatistics - Initialize the statistics used by TAO for all of the solvers.
1256    These statistics include the iteration number, residual norms, and convergence status.
1257    This routine gets called before solving each optimization problem.
1258 
1259    Collective on Tao
1260 
1261    Input Parameters:
1262 .  solver - the Tao context
1263 
1264    Level: developer
1265 
1266 .seealso: TaoCreate(), TaoSolve()
1267 @*/
1268 PetscErrorCode TaoResetStatistics(Tao tao)
1269 {
1270   PetscFunctionBegin;
1271   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1272   tao->niter        = 0;
1273   tao->nfuncs       = 0;
1274   tao->nfuncgrads   = 0;
1275   tao->ngrads       = 0;
1276   tao->nhess        = 0;
1277   tao->njac         = 0;
1278   tao->nconstraints = 0;
1279   tao->ksp_its      = 0;
1280   tao->reason       = TAO_CONTINUE_ITERATING;
1281   tao->residual     = 0.0;
1282   tao->cnorm        = 0.0;
1283   tao->step         = 0.0;
1284   tao->lsflag       = PETSC_FALSE;
1285   if (tao->hist_reset) tao->hist_len=0;
1286   PetscFunctionReturn(0);
1287 }
1288 
1289 #undef __FUNCT__
1290 #define __FUNCT__ "TaoSetConvergenceTest"
1291 /*@C
1292   TaoSetConvergenceTest - Sets the function that is to be used to test
1293   for convergence o fthe iterative minimization solution.  The new convergence
1294   testing routine will replace TAO's default convergence test.
1295 
1296   Logically Collective on Tao
1297 
1298   Input Parameters:
1299 + tao - the Tao object
1300 . conv - the routine to test for convergence
1301 - ctx - [optional] context for private data for the convergence routine
1302         (may be NULL)
1303 
1304   Calling sequence of conv:
1305 $   PetscErrorCode conv(Tao tao, void *ctx)
1306 
1307 + tao - the Tao object
1308 - ctx - [optional] convergence context
1309 
1310   Note: The new convergence testing routine should call TaoSetConvergedReason().
1311 
1312   Level: advanced
1313 
1314 .seealso: TaoSetConvergedReason(), TaoGetSolutionStatus(), TaoGetTolerances(), TaoSetMonitor
1315 
1316 @*/
1317 PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao,void*), void *ctx)
1318 {
1319   PetscFunctionBegin;
1320   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1321   (tao)->ops->convergencetest = conv;
1322   (tao)->cnvP = ctx;
1323   PetscFunctionReturn(0);
1324 }
1325 
1326 #undef __FUNCT__
1327 #define __FUNCT__ "TaoSetMonitor"
1328 /*@C
1329    TaoSetMonitor - Sets an ADDITIONAL function that is to be used at every
1330    iteration of the solver to display the iteration's
1331    progress.
1332 
1333    Logically Collective on Tao
1334 
1335    Input Parameters:
1336 +  tao - the Tao solver context
1337 .  mymonitor - monitoring routine
1338 -  mctx - [optional] user-defined context for private data for the
1339           monitor routine (may be NULL)
1340 
1341    Calling sequence of mymonitor:
1342 $     int mymonitor(Tao tao,void *mctx)
1343 
1344 +    tao - the Tao solver context
1345 -    mctx - [optional] monitoring context
1346 
1347 
1348    Options Database Keys:
1349 +    -tao_monitor        - sets TaoDefaultMonitor()
1350 .    -tao_smonitor       - sets short monitor
1351 .    -tao_cmonitor       - same as smonitor plus constraint norm
1352 .    -tao_view_solution   - view solution at each iteration
1353 .    -tao_view_gradient   - view gradient at each iteration
1354 .    -tao_view_separableobjective - view separable objective function at each iteration
1355 -    -tao_cancelmonitors - cancels all monitors that have been hardwired into a code by calls to TaoSetMonitor(), but does not cancel those set via the options database.
1356 
1357 
1358    Notes:
1359    Several different monitoring routines may be set by calling
1360    TaoSetMonitor() multiple times; all will be called in the
1361    order in which they were set.
1362 
1363    Fortran Notes: Only one monitor function may be set
1364 
1365    Level: intermediate
1366 
1367 .seealso: TaoDefaultMonitor(), TaoCancelMonitors(),  TaoSetDestroyRoutine()
1368 @*/
1369 PetscErrorCode TaoSetMonitor(Tao tao, PetscErrorCode (*func)(Tao, void*), void *ctx,PetscErrorCode (*dest)(void**))
1370 {
1371   PetscFunctionBegin;
1372   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1373   if (tao->numbermonitors >= MAXTAOMONITORS) SETERRQ1(PETSC_COMM_SELF,1,"Cannot attach another monitor -- max=",MAXTAOMONITORS);
1374   tao->monitor[tao->numbermonitors] = func;
1375   tao->monitorcontext[tao->numbermonitors] = ctx;
1376   tao->monitordestroy[tao->numbermonitors] = dest;
1377   ++tao->numbermonitors;
1378   PetscFunctionReturn(0);
1379 }
1380 
1381 #undef __FUNCT__
1382 #define __FUNCT__ "TaoCancelMonitors"
1383 /*@
1384    TaoCancelMonitors - Clears all the monitor functions for a Tao object.
1385 
1386    Logically Collective on Tao
1387 
1388    Input Parameters:
1389 .  tao - the Tao solver context
1390 
1391    Options Database:
1392 .  -tao_cancelmonitors - cancels all monitors that have been hardwired
1393     into a code by calls to TaoSetMonitor(), but does not cancel those
1394     set via the options database
1395 
1396    Notes:
1397    There is no way to clear one specific monitor from a Tao object.
1398 
1399    Level: advanced
1400 
1401 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1402 @*/
1403 PetscErrorCode TaoCancelMonitors(Tao tao)
1404 {
1405   PetscInt       i;
1406   PetscErrorCode ierr;
1407 
1408   PetscFunctionBegin;
1409   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1410   for (i=0;i<tao->numbermonitors;i++) {
1411     if (tao->monitordestroy[i]) {
1412       ierr = (*tao->monitordestroy[i])(&tao->monitorcontext[i]);CHKERRQ(ierr);
1413     }
1414   }
1415   tao->numbermonitors=0;
1416   PetscFunctionReturn(0);
1417 }
1418 
1419 #undef __FUNCT__
1420 #define __FUNCT__ "TaoDefaultMonitor"
1421 /*@C
1422    TaoDefaultMonitor - Default routine for monitoring progress of the
1423    Tao solvers (default).  This monitor prints the function value and gradient
1424    norm at each iteration.  It can be turned on from the command line using the
1425    -tao_monitor option
1426 
1427    Collective on Tao
1428 
1429    Input Parameters:
1430 +  tao - the Tao context
1431 -  ctx - PetscViewer context or NULL
1432 
1433    Options Database Keys:
1434 .  -tao_monitor
1435 
1436    Level: advanced
1437 
1438 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1439 @*/
1440 PetscErrorCode TaoDefaultMonitor(Tao tao, void *ctx)
1441 {
1442   PetscErrorCode ierr;
1443   PetscInt       its;
1444   PetscReal      fct,gnorm;
1445   PetscViewer    viewer;
1446 
1447   PetscFunctionBegin;
1448   if (ctx) {
1449     viewer = (PetscViewer)ctx;
1450   } else {
1451     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1452   }
1453   its=tao->niter;
1454   fct=tao->fc;
1455   gnorm=tao->residual;
1456   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1457   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1458   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1459   PetscFunctionReturn(0);
1460 }
1461 
1462 #undef __FUNCT__
1463 #define __FUNCT__ "TaoDefaultSMonitor"
1464 /*@C
1465    TaoDefaultSMonitor - Default routine for monitoring progress of the
1466    solver. Same as TaoDefaultMonitor() except
1467    it prints fewer digits of the residual as the residual gets smaller.
1468    This is because the later digits are meaningless and are often
1469    different on different machines; by using this routine different
1470    machines will usually generate the same output. It can be turned on
1471    by using the -tao_smonitor option
1472 
1473    Collective on Tao
1474 
1475    Input Parameters:
1476 +  tao - the Tao context
1477 -  ctx - PetscViewer context or NULL
1478 
1479    Options Database Keys:
1480 .  -tao_smonitor
1481 
1482    Level: advanced
1483 
1484 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1485 @*/
1486 PetscErrorCode TaoDefaultSMonitor(Tao tao, void *ctx)
1487 {
1488   PetscErrorCode ierr;
1489   PetscInt       its;
1490   PetscReal      fct,gnorm;
1491   PetscViewer    viewer;
1492 
1493   PetscFunctionBegin;
1494   if (ctx) {
1495     viewer = (PetscViewer)ctx;
1496   } else {
1497     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1498   }
1499   its=tao->niter;
1500   fct=tao->fc;
1501   gnorm=tao->residual;
1502   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1503   ierr=PetscViewerASCIIPrintf(viewer," Function value %g,",(double)fct);CHKERRQ(ierr);
1504   if (gnorm > 1.e-6) {
1505     ierr=PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1506   } else if (gnorm > 1.e-11) {
1507     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-6 \n");CHKERRQ(ierr);
1508   } else {
1509     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-11 \n");CHKERRQ(ierr);
1510   }
1511   PetscFunctionReturn(0);
1512 }
1513 
1514 #undef __FUNCT__
1515 #define __FUNCT__ "TaoDefaultCMonitor"
1516 /*@C
1517    TaoDefaultCMonitor - same as TaoDefaultMonitor() except
1518    it prints the norm of the constraints function. It can be turned on
1519    from the command line using the -tao_cmonitor option
1520 
1521    Collective on Tao
1522 
1523    Input Parameters:
1524 +  tao - the Tao context
1525 -  ctx - PetscViewer context or NULL
1526 
1527    Options Database Keys:
1528 .  -tao_cmonitor
1529 
1530    Level: advanced
1531 
1532 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1533 @*/
1534 PetscErrorCode TaoDefaultCMonitor(Tao tao, void *ctx)
1535 {
1536   PetscErrorCode ierr;
1537   PetscInt       its;
1538   PetscReal      fct,gnorm;
1539   PetscViewer    viewer;
1540 
1541   PetscFunctionBegin;
1542   if (ctx) {
1543     viewer = (PetscViewer)ctx;
1544   } else {
1545     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1546   }
1547   its=tao->niter;
1548   fct=tao->fc;
1549   gnorm=tao->residual;
1550   ierr=PetscViewerASCIIPrintf(viewer,"iter = %D,",its);CHKERRQ(ierr);
1551   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1552   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g ",(double)gnorm);CHKERRQ(ierr);
1553   ierr = PetscViewerASCIIPrintf(viewer,"  Constraint: %g \n",(double)tao->cnorm);CHKERRQ(ierr);
1554   PetscFunctionReturn(0);
1555 }
1556 
1557 #undef __FUNCT__
1558 #define __FUNCT__ "TaoSolutionMonitor"
1559 /*@C
1560    TaoSolutionMonitor - Views the solution at each iteration
1561    It can be turned on from the command line using the
1562    -tao_view_solution option
1563 
1564    Collective on Tao
1565 
1566    Input Parameters:
1567 +  tao - the Tao context
1568 -  ctx - PetscViewer context or NULL
1569 
1570    Options Database Keys:
1571 .  -tao_view_solution
1572 
1573    Level: advanced
1574 
1575 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1576 @*/
1577 PetscErrorCode TaoSolutionMonitor(Tao tao, void *ctx)
1578 {
1579   PetscErrorCode ierr;
1580   PetscViewer viewer;
1581 
1582   PetscFunctionBegin;
1583   if (ctx) {
1584     viewer = (PetscViewer)ctx;
1585   } else {
1586     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1587   }
1588   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1589   PetscFunctionReturn(0);
1590 }
1591 
1592 #undef __FUNCT__
1593 #define __FUNCT__ "TaoGradientMonitor"
1594 /*@C
1595    TaoGradientMonitor - Views the gradient at each iteration
1596    It can be turned on from the command line using the
1597    -tao_view_gradient option
1598 
1599    Collective on Tao
1600 
1601    Input Parameters:
1602 +  tao - the Tao context
1603 -  ctx - PetscViewer context or NULL
1604 
1605    Options Database Keys:
1606 .  -tao_view_gradient
1607 
1608    Level: advanced
1609 
1610 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1611 @*/
1612 PetscErrorCode TaoGradientMonitor(Tao tao, void *ctx)
1613 {
1614   PetscErrorCode ierr;
1615   PetscViewer viewer;
1616 
1617   PetscFunctionBegin;
1618   if (ctx) {
1619     viewer = (PetscViewer)ctx;
1620   } else {
1621     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1622   }
1623   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1624   PetscFunctionReturn(0);
1625 }
1626 
1627 #undef __FUNCT__
1628 #define __FUNCT__ "TaoStepDirectionMonitor"
1629 /*@C
1630    TaoStepDirectionMonitor - Views the gradient at each iteration
1631    It can be turned on from the command line using the
1632    -tao_view_gradient option
1633 
1634    Collective on Tao
1635 
1636    Input Parameters:
1637 +  tao - the Tao context
1638 -  ctx - PetscViewer context or NULL
1639 
1640    Options Database Keys:
1641 .  -tao_view_gradient
1642 
1643    Level: advanced
1644 
1645 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1646 @*/
1647 PetscErrorCode TaoStepDirectionMonitor(Tao tao, void *ctx)
1648 {
1649   PetscErrorCode ierr;
1650   PetscViewer viewer;
1651   PetscFunctionBegin;
1652   if (ctx) {
1653     viewer = (PetscViewer)ctx;
1654   } else {
1655     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1656   }
1657   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1658   PetscFunctionReturn(0);
1659 }
1660 
1661 #undef __FUNCT__
1662 #define __FUNCT__ "TaoDrawSolutionMonitor"
1663 /*@C
1664    TaoDrawSolutionMonitor - Plots the solution at each iteration
1665    It can be turned on from the command line using the
1666    -tao_draw_solution option
1667 
1668    Collective on Tao
1669 
1670    Input Parameters:
1671 +  tao - the Tao context
1672 -  ctx - PetscViewer context or NULL
1673 
1674    Options Database Keys:
1675 .  -tao_draw_solution
1676 
1677    Level: advanced
1678 
1679 .seealso: TaoSolutionMonitor(), TaoSetMonitor(), TaoDrawGradientMonitor
1680 @*/
1681 PetscErrorCode TaoDrawSolutionMonitor(Tao tao, void *ctx)
1682 {
1683   PetscErrorCode ierr;
1684   PetscViewer    viewer = (PetscViewer) ctx;
1685   MPI_Comm       comm;
1686 
1687   PetscFunctionBegin;
1688   if (!viewer) {
1689     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1690     viewer = PETSC_VIEWER_DRAW_(comm);
1691   }
1692   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1693   PetscFunctionReturn(0);
1694 }
1695 
1696 #undef __FUNCT__
1697 #define __FUNCT__ "TaoDrawGradientMonitor"
1698 /*@C
1699    TaoDrawGradientMonitor - Plots the gradient at each iteration
1700    It can be turned on from the command line using the
1701    -tao_draw_gradient option
1702 
1703    Collective on Tao
1704 
1705    Input Parameters:
1706 +  tao - the Tao context
1707 -  ctx - PetscViewer context or NULL
1708 
1709    Options Database Keys:
1710 .  -tao_draw_gradient
1711 
1712    Level: advanced
1713 
1714 .seealso: TaoGradientMonitor(), TaoSetMonitor(), TaoDrawSolutionMonitor
1715 @*/
1716 PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx)
1717 {
1718   PetscErrorCode ierr;
1719   PetscViewer    viewer = (PetscViewer)ctx;
1720   MPI_Comm       comm;
1721 
1722   PetscFunctionBegin;
1723   if (!viewer) {
1724     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1725     viewer = PETSC_VIEWER_DRAW_(comm);
1726   }
1727   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1728   PetscFunctionReturn(0);
1729 }
1730 
1731 #undef __FUNCT__
1732 #define __FUNCT__ "TaoDrawStepMonitor"
1733 /*@C
1734    TaoDrawStepMonitor - Plots the step direction at each iteration
1735    It can be turned on from the command line using the
1736    -tao_draw_step option
1737 
1738    Collective on Tao
1739 
1740    Input Parameters:
1741 +  tao - the Tao context
1742 -  ctx - PetscViewer context or NULL
1743 
1744    Options Database Keys:
1745 .  -tao_draw_step
1746 
1747    Level: advanced
1748 
1749 .seealso: TaoSetMonitor(), TaoDrawSolutionMonitor
1750 @*/
1751 PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx)
1752 {
1753   PetscErrorCode ierr;
1754   PetscViewer    viewer = (PetscViewer)(ctx);
1755   MPI_Comm       comm;
1756 
1757   PetscFunctionBegin;
1758   if (!viewer) {
1759     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1760     viewer = PETSC_VIEWER_DRAW_(comm);
1761   }
1762   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1763   PetscFunctionReturn(0);
1764 }
1765 
1766 #undef __FUNCT__
1767 #define __FUNCT__ "TaoSeparableObjectiveMonitor"
1768 /*@C
1769    TaoSeparableObjectiveMonitor - Views the separable objective function at each iteration
1770    It can be turned on from the command line using the
1771    -tao_view_separableobjective option
1772 
1773    Collective on Tao
1774 
1775    Input Parameters:
1776 +  tao - the Tao context
1777 -  ctx - PetscViewer context or NULL
1778 
1779    Options Database Keys:
1780 .  -tao_view_separableobjective
1781 
1782    Level: advanced
1783 
1784 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1785 @*/
1786 PetscErrorCode TaoSeparableObjectiveMonitor(Tao tao, void *ctx)
1787 {
1788   PetscErrorCode ierr;
1789   PetscViewer    viewer;
1790 
1791   PetscFunctionBegin;
1792   if (ctx) {
1793     viewer = (PetscViewer)ctx;
1794   } else {
1795     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1796   }
1797   ierr = VecView(tao->sep_objective,viewer);CHKERRQ(ierr);
1798   PetscFunctionReturn(0);
1799 }
1800 
1801 #undef __FUNCT__
1802 #define __FUNCT__ "TaoDefaultConvergenceTest"
1803 /*@C
1804    TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1805    or terminate.
1806 
1807    Collective on Tao
1808 
1809    Input Parameters:
1810 +  tao - the Tao context
1811 -  dummy - unused dummy context
1812 
1813    Output Parameter:
1814 .  reason - for terminating
1815 
1816    Notes:
1817    This routine checks the residual in the optimality conditions, the
1818    relative residual in the optimity conditions, the number of function
1819    evaluations, and the function value to test convergence.  Some
1820    solvers may use different convergence routines.
1821 
1822    Level: developer
1823 
1824 .seealso: TaoSetTolerances(),TaoGetConvergedReason(),TaoSetConvergedReason()
1825 @*/
1826 
1827 PetscErrorCode TaoDefaultConvergenceTest(Tao tao,void *dummy)
1828 {
1829   PetscInt           niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads);
1830   PetscInt           max_funcs=tao->max_funcs;
1831   PetscReal          gnorm=tao->residual, gnorm0=tao->gnorm0;
1832   PetscReal          f=tao->fc, steptol=tao->steptol,trradius=tao->step;
1833   PetscReal          gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol;
1834   PetscReal          fatol=tao->fatol,frtol=tao->frtol,catol=tao->catol,crtol=tao->crtol;
1835   PetscReal          fmin=tao->fmin, cnorm=tao->cnorm, cnorm0=tao->cnorm0;
1836   PetscReal          gnorm2;
1837   TaoConvergedReason reason=tao->reason;
1838   PetscErrorCode     ierr;
1839 
1840   PetscFunctionBegin;
1841   PetscValidHeaderSpecific(tao, TAO_CLASSID,1);
1842   if (reason != TAO_CONTINUE_ITERATING) {
1843     PetscFunctionReturn(0);
1844   }
1845   gnorm2=gnorm*gnorm;
1846 
1847   if (PetscIsInfOrNanReal(f)) {
1848     ierr = PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n");CHKERRQ(ierr);
1849     reason = TAO_DIVERGED_NAN;
1850   } else if (f <= fmin && cnorm <=catol) {
1851     ierr = PetscInfo2(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin);CHKERRQ(ierr);
1852     reason = TAO_CONVERGED_MINF;
1853   } else if (gnorm2 <= fatol && cnorm <=catol) {
1854     ierr = PetscInfo2(tao,"Converged due to estimated f(X) - f(X*) = %g < %g\n",(double)gnorm2,(double)fatol);CHKERRQ(ierr);
1855     reason = TAO_CONVERGED_FATOL;
1856   } else if (f != 0 && gnorm2 / PetscAbsReal(f)<= frtol && cnorm/PetscMax(cnorm0,1.0) <= crtol) {
1857     ierr = PetscInfo2(tao,"Converged due to estimated |f(X)-f(X*)|/f(X) = %g < %g\n",(double)(gnorm2/PetscAbsReal(f)),(double)frtol);CHKERRQ(ierr);
1858     reason = TAO_CONVERGED_FRTOL;
1859   } else if (gnorm<= gatol && cnorm <=catol) {
1860     ierr = PetscInfo2(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol);CHKERRQ(ierr);
1861     reason = TAO_CONVERGED_GATOL;
1862   } else if ( f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) {
1863     ierr = PetscInfo2(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol);CHKERRQ(ierr);
1864     reason = TAO_CONVERGED_GRTOL;
1865   } else if (gnorm0 != 0 && gnorm/gnorm0 <= gttol && cnorm <= crtol) {
1866     ierr = PetscInfo2(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol);CHKERRQ(ierr);
1867     reason = TAO_CONVERGED_GTTOL;
1868   } else if (nfuncs > max_funcs){
1869     ierr = PetscInfo2(tao,"Exceeded maximum number of function evaluations: %D > %D\n", nfuncs,max_funcs);CHKERRQ(ierr);
1870     reason = TAO_DIVERGED_MAXFCN;
1871   } else if ( tao->lsflag != 0 ){
1872     ierr = PetscInfo(tao,"Tao Line Search failure.\n");CHKERRQ(ierr);
1873     reason = TAO_DIVERGED_LS_FAILURE;
1874   } else if (trradius < steptol && niter > 0){
1875     ierr = PetscInfo2(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol);CHKERRQ(ierr);
1876     reason = TAO_CONVERGED_STEPTOL;
1877   } else if (niter > tao->max_it) {
1878     ierr = PetscInfo2(tao,"Exceeded maximum number of iterations: %D > %D\n",niter,tao->max_it);CHKERRQ(ierr);
1879     reason = TAO_DIVERGED_MAXITS;
1880   } else {
1881     reason = TAO_CONTINUE_ITERATING;
1882   }
1883   tao->reason = reason;
1884   PetscFunctionReturn(0);
1885 }
1886 
1887 #undef __FUNCT__
1888 #define __FUNCT__ "TaoSetOptionsPrefix"
1889 /*@C
1890    TaoSetOptionsPrefix - Sets the prefix used for searching for all
1891    TAO options in the database.
1892 
1893 
1894    Logically Collective on Tao
1895 
1896    Input Parameters:
1897 +  tao - the Tao context
1898 -  prefix - the prefix string to prepend to all TAO option requests
1899 
1900    Notes:
1901    A hyphen (-) must NOT be given at the beginning of the prefix name.
1902    The first character of all runtime options is AUTOMATICALLY the hyphen.
1903 
1904    For example, to distinguish between the runtime options for two
1905    different TAO solvers, one could call
1906 .vb
1907       TaoSetOptionsPrefix(tao1,"sys1_")
1908       TaoSetOptionsPrefix(tao2,"sys2_")
1909 .ve
1910 
1911    This would enable use of different options for each system, such as
1912 .vb
1913       -sys1_tao_method blmvm -sys1_tao_gtol 1.e-3
1914       -sys2_tao_method lmvm  -sys2_tao_gtol 1.e-4
1915 .ve
1916 
1917 
1918    Level: advanced
1919 
1920 .seealso: TaoAppendOptionsPrefix(), TaoGetOptionsPrefix()
1921 @*/
1922 
1923 PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
1924 {
1925   PetscErrorCode ierr;
1926 
1927   PetscFunctionBegin;
1928   ierr = PetscObjectSetOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1929   if (tao->linesearch) {
1930     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1931   }
1932   if (tao->ksp) {
1933     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1934   }
1935   PetscFunctionReturn(0);
1936 }
1937 
1938 #undef __FUNCT__
1939 #define __FUNCT__ "TaoAppendOptionsPrefix"
1940 /*@C
1941    TaoAppendOptionsPrefix - Appends to the prefix used for searching for all
1942    TAO options in the database.
1943 
1944 
1945    Logically Collective on Tao
1946 
1947    Input Parameters:
1948 +  tao - the Tao solver context
1949 -  prefix - the prefix string to prepend to all TAO option requests
1950 
1951    Notes:
1952    A hyphen (-) must NOT be given at the beginning of the prefix name.
1953    The first character of all runtime options is AUTOMATICALLY the hyphen.
1954 
1955 
1956    Level: advanced
1957 
1958 .seealso: TaoSetOptionsPrefix(), TaoGetOptionsPrefix()
1959 @*/
1960 PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
1961 {
1962   PetscErrorCode ierr;
1963 
1964   PetscFunctionBegin;
1965   ierr = PetscObjectAppendOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1966   if (tao->linesearch) {
1967     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1968   }
1969   if (tao->ksp) {
1970     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1971   }
1972   PetscFunctionReturn(0);
1973 }
1974 
1975 #undef __FUNCT__
1976 #define __FUNCT__ "TaoGetOptionsPrefix"
1977 /*@C
1978   TaoGetOptionsPrefix - Gets the prefix used for searching for all
1979   TAO options in the database
1980 
1981   Not Collective
1982 
1983   Input Parameters:
1984 . tao - the Tao context
1985 
1986   Output Parameters:
1987 . prefix - pointer to the prefix string used is returned
1988 
1989   Notes: On the fortran side, the user should pass in a string 'prefix' of
1990   sufficient length to hold the prefix.
1991 
1992   Level: advanced
1993 
1994 .seealso: TaoSetOptionsPrefix(), TaoAppendOptionsPrefix()
1995 @*/
1996 PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
1997 {
1998    return PetscObjectGetOptionsPrefix((PetscObject)tao,p);
1999 }
2000 
2001 #undef __FUNCT__
2002 #define __FUNCT__ "TaoSetType"
2003 /*@C
2004    TaoSetType - Sets the method for the unconstrained minimization solver.
2005 
2006    Collective on Tao
2007 
2008    Input Parameters:
2009 +  solver - the Tao solver context
2010 -  type - a known method
2011 
2012    Options Database Key:
2013 .  -tao_type <type> - Sets the method; use -help for a list
2014    of available methods (for instance, "-tao_type lmvm" or "-tao_type tron")
2015 
2016    Available methods include:
2017 +    nls - Newton's method with line search for unconstrained minimization
2018 .    ntr - Newton's method with trust region for unconstrained minimization
2019 .    ntl - Newton's method with trust region, line search for unconstrained minimization
2020 .    lmvm - Limited memory variable metric method for unconstrained minimization
2021 .    cg - Nonlinear conjugate gradient method for unconstrained minimization
2022 .    nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
2023 .    tron - Newton Trust Region method for bound constrained minimization
2024 .    gpcg - Newton Trust Region method for quadratic bound constrained minimization
2025 .    blmvm - Limited memory variable metric method for bound constrained minimization
2026 -    pounders - Model-based algorithm pounder extended for nonlinear least squares
2027 
2028   Level: intermediate
2029 
2030 .seealso: TaoCreate(), TaoGetType(), TaoType
2031 
2032 @*/
2033 PetscErrorCode TaoSetType(Tao tao, const TaoType type)
2034 {
2035   PetscErrorCode ierr;
2036   PetscErrorCode (*create_xxx)(Tao);
2037   PetscBool      issame;
2038 
2039   PetscFunctionBegin;
2040   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2041 
2042   ierr = PetscObjectTypeCompare((PetscObject)tao,type,&issame);CHKERRQ(ierr);
2043   if (issame) PetscFunctionReturn(0);
2044 
2045   ierr = PetscFunctionListFind(TaoList, type, (void(**)(void))&create_xxx);CHKERRQ(ierr);
2046   if (!create_xxx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested Tao type %s",type);
2047 
2048   /* Destroy the existing solver information */
2049   if (tao->ops->destroy) {
2050     ierr = (*tao->ops->destroy)(tao);CHKERRQ(ierr);
2051   }
2052   ierr = KSPDestroy(&tao->ksp);CHKERRQ(ierr);
2053   ierr = TaoLineSearchDestroy(&tao->linesearch);CHKERRQ(ierr);
2054   ierr = VecDestroy(&tao->gradient);CHKERRQ(ierr);
2055   ierr = VecDestroy(&tao->stepdirection);CHKERRQ(ierr);
2056 
2057   tao->ops->setup = 0;
2058   tao->ops->solve = 0;
2059   tao->ops->view  = 0;
2060   tao->ops->setfromoptions = 0;
2061   tao->ops->destroy = 0;
2062 
2063   tao->setupcalled = PETSC_FALSE;
2064 
2065   ierr = (*create_xxx)(tao);CHKERRQ(ierr);
2066   ierr = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(ierr);
2067   PetscFunctionReturn(0);
2068 }
2069 
2070 #undef __FUNCT__
2071 #define __FUNCT__ "TaoRegister"
2072 /*MC
2073    TaoRegister - Adds a method to the TAO package for unconstrained minimization.
2074 
2075    Synopsis:
2076    TaoRegister(char *name_solver,char *path,char *name_Create,int (*routine_Create)(Tao))
2077 
2078    Not collective
2079 
2080    Input Parameters:
2081 +  sname - name of a new user-defined solver
2082 -  func - routine to Create method context
2083 
2084    Notes:
2085    TaoRegister() may be called multiple times to add several user-defined solvers.
2086 
2087    Sample usage:
2088 .vb
2089    TaoRegister("my_solver",MySolverCreate);
2090 .ve
2091 
2092    Then, your solver can be chosen with the procedural interface via
2093 $     TaoSetType(tao,"my_solver")
2094    or at runtime via the option
2095 $     -tao_type my_solver
2096 
2097    Level: advanced
2098 
2099 .seealso: TaoRegisterAll(), TaoRegisterDestroy()
2100 M*/
2101 PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao))
2102 {
2103   PetscErrorCode ierr;
2104 
2105   PetscFunctionBegin;
2106   ierr = PetscFunctionListAdd(&TaoList,sname, (void (*)(void))func);CHKERRQ(ierr);
2107   PetscFunctionReturn(0);
2108 }
2109 
2110 #undef __FUNCT__
2111 #define __FUNCT__ "TaoRegisterDestroy"
2112 /*@C
2113    TaoRegisterDestroy - Frees the list of minimization solvers that were
2114    registered by TaoRegisterDynamic().
2115 
2116    Not Collective
2117 
2118    Level: advanced
2119 
2120 .seealso: TaoRegisterAll(), TaoRegister()
2121 @*/
2122 PetscErrorCode TaoRegisterDestroy(void)
2123 {
2124   PetscErrorCode ierr;
2125   PetscFunctionBegin;
2126   ierr = PetscFunctionListDestroy(&TaoList);CHKERRQ(ierr);
2127   TaoRegisterAllCalled = PETSC_FALSE;
2128   PetscFunctionReturn(0);
2129 }
2130 
2131 #undef __FUNCT__
2132 #define __FUNCT__ "TaoSetConvergedReason"
2133 /*@
2134   TaoSetConvergedReason - Sets the termination flag on a Tao object
2135 
2136   Logically Collective on Tao
2137 
2138   Input Parameters:
2139 + tao - the Tao context
2140 - reason - one of
2141 $     TAO_CONVERGED_ATOL (2),
2142 $     TAO_CONVERGED_RTOL (3),
2143 $     TAO_CONVERGED_STEPTOL (4),
2144 $     TAO_CONVERGED_MINF (5),
2145 $     TAO_CONVERGED_USER (6),
2146 $     TAO_DIVERGED_MAXITS (-2),
2147 $     TAO_DIVERGED_NAN (-4),
2148 $     TAO_DIVERGED_MAXFCN (-5),
2149 $     TAO_DIVERGED_LS_FAILURE (-6),
2150 $     TAO_DIVERGED_TR_REDUCTION (-7),
2151 $     TAO_DIVERGED_USER (-8),
2152 $     TAO_CONTINUE_ITERATING (0)
2153 
2154    Level: intermediate
2155 
2156 @*/
2157 PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2158 {
2159   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2160   PetscFunctionBegin;
2161   tao->reason = reason;
2162   PetscFunctionReturn(0);
2163 }
2164 
2165 #undef __FUNCT__
2166 #define __FUNCT__ "TaoGetConvergedReason"
2167 /*@
2168    TaoGetConvergedReason - Gets the reason the Tao iteration was stopped.
2169 
2170    Not Collective
2171 
2172    Input Parameter:
2173 .  tao - the Tao solver context
2174 
2175    Output Parameter:
2176 .  reason - one of
2177 
2178 $  TAO_CONVERGED_FATOL (1)           f(X)-f(X*) <= fatol
2179 $  TAO_CONVERGED_FRTOL (2)           |f(X) - f(X*)|/|f(X)| < frtol
2180 $  TAO_CONVERGED_GATOL (3)           ||g(X)|| < gatol
2181 $  TAO_CONVERGED_GRTOL (4)           ||g(X)|| / f(X)  < grtol
2182 $  TAO_CONVERGED_GTTOL (5)           ||g(X)|| / ||g(X0)|| < gttol
2183 $  TAO_CONVERGED_STEPTOL (6)         step size small
2184 $  TAO_CONVERGED_MINF (7)            F < F_min
2185 $  TAO_CONVERGED_USER (8)            User defined
2186 
2187 $  TAO_DIVERGED_MAXITS (-2)          its > maxits
2188 $  TAO_DIVERGED_NAN (-4)             Numerical problems
2189 $  TAO_DIVERGED_MAXFCN (-5)          fevals > max_funcsals
2190 $  TAO_DIVERGED_LS_FAILURE (-6)      line search failure
2191 $  TAO_DIVERGED_TR_REDUCTION (-7)    trust region failure
2192 $  TAO_DIVERGED_USER(-8)             (user defined)
2193 
2194 $  TAO_CONTINUE_ITERATING (0)
2195 
2196    where
2197 +  X - current solution
2198 .  X0 - initial guess
2199 .  f(X) - current function value
2200 .  f(X*) - true solution (estimated)
2201 .  g(X) - current gradient
2202 .  its - current iterate number
2203 .  maxits - maximum number of iterates
2204 .  fevals - number of function evaluations
2205 -  max_funcsals - maximum number of function evaluations
2206 
2207    Level: intermediate
2208 
2209 .seealso: TaoSetConvergenceTest(), TaoSetTolerances()
2210 
2211 @*/
2212 PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2213 {
2214   PetscFunctionBegin;
2215   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2216   PetscValidPointer(reason,2);
2217   *reason = tao->reason;
2218   PetscFunctionReturn(0);
2219 }
2220 
2221 #undef __FUNCT__
2222 #define __FUNCT__ "TaoGetSolutionStatus"
2223 /*@
2224   TaoGetSolutionStatus - Get the current iterate, objective value,
2225   residual, infeasibility, and termination
2226 
2227   Not Collective
2228 
2229    Input Parameters:
2230 .  tao - the Tao context
2231 
2232    Output Parameters:
2233 +  iterate - the current iterate number (>=0)
2234 .  f - the current function value
2235 .  gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2236 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2237 .  xdiff - the step length or trust region radius of the most recent iterate.
2238 -  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2239 
2240    Level: intermediate
2241 
2242    Note:
2243    TAO returns the values set by the solvers in the routine TaoMonitor().
2244 
2245    Note:
2246    If any of the output arguments are set to NULL, no corresponding value will be returned.
2247 
2248 .seealso: TaoMonitor(), TaoGetConvergedReason()
2249 @*/
2250 PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2251 {
2252   PetscFunctionBegin;
2253   if (its) *its=tao->niter;
2254   if (f) *f=tao->fc;
2255   if (gnorm) *gnorm=tao->residual;
2256   if (cnorm) *cnorm=tao->cnorm;
2257   if (reason) *reason=tao->reason;
2258   if (xdiff) *xdiff=tao->step;
2259   PetscFunctionReturn(0);
2260 }
2261 
2262 #undef __FUNCT__
2263 #define __FUNCT__ "TaoGetType"
2264 /*@C
2265    TaoGetType - Gets the current Tao algorithm.
2266 
2267    Not Collective
2268 
2269    Input Parameter:
2270 .  tao - the Tao solver context
2271 
2272    Output Parameter:
2273 .  type - Tao method
2274 
2275    Level: intermediate
2276 
2277 @*/
2278 PetscErrorCode TaoGetType(Tao tao, const TaoType *type)
2279 {
2280   PetscFunctionBegin;
2281   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2282   PetscValidPointer(type,2);
2283   *type=((PetscObject)tao)->type_name;
2284   PetscFunctionReturn(0);
2285 }
2286 
2287 #undef __FUNCT__
2288 #define __FUNCT__ "TaoMonitor"
2289 /*@C
2290   TaoMonitor - Monitor the solver and the current solution.  This
2291   routine will record the iteration number and residual statistics,
2292   call any monitors specified by the user, and calls the convergence-check routine.
2293 
2294    Input Parameters:
2295 +  tao - the Tao context
2296 .  its - the current iterate number (>=0)
2297 .  f - the current objective function value
2298 .  res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality.  This measure will be recorded and
2299           used for some termination tests.
2300 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2301 -  steplength - multiple of the step direction added to the previous iterate.
2302 
2303    Output Parameters:
2304 .  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2305 
2306    Options Database Key:
2307 .  -tao_monitor - Use the default monitor, which prints statistics to standard output
2308 
2309 .seealso TaoGetConvergedReason(), TaoDefaultMonitor(), TaoSetMonitor()
2310 
2311    Level: developer
2312 
2313 @*/
2314 PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength, TaoConvergedReason *reason)
2315 {
2316   PetscErrorCode ierr;
2317   PetscInt       i;
2318 
2319   PetscFunctionBegin;
2320   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2321   tao->fc = f;
2322   tao->residual = res;
2323   tao->cnorm = cnorm;
2324   tao->step = steplength;
2325   tao->niter=its;
2326   if (its == 0) {
2327     tao->cnorm0 = cnorm; tao->gnorm0 = res;
2328   }
2329   TaoLogHistory(tao,f,res,cnorm);
2330   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(res)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
2331   if (tao->ops->convergencetest) {
2332     ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr);
2333   }
2334   for (i=0;i<tao->numbermonitors;i++) {
2335     ierr = (*tao->monitor[i])(tao,tao->monitorcontext[i]);CHKERRQ(ierr);
2336   }
2337   *reason = tao->reason;
2338   PetscFunctionReturn(0);
2339 }
2340 
2341 #undef __FUNCT__
2342 #define __FUNCT__ "TaoSetHistory"
2343 /*@
2344    TaoSetHistory - Sets the array used to hold the convergence history.
2345 
2346    Logically Collective on Tao
2347 
2348    Input Parameters:
2349 +  tao - the Tao solver context
2350 .  obj   - array to hold objective value history
2351 .  resid - array to hold residual history
2352 .  cnorm - array to hold constraint violation history
2353 .  na  - size of obj, resid, and cnorm
2354 -  reset - PetscTrue indicates each new minimization resets the history counter to zero,
2355            else it continues storing new values for new minimizations after the old ones
2356 
2357    Notes:
2358    If set, TAO will fill the given arrays with the indicated
2359    information at each iteration.  If no information is desired
2360    for a given array, then NULL may be used.
2361 
2362    This routine is useful, e.g., when running a code for purposes
2363    of accurate performance monitoring, when no I/O should be done
2364    during the section of code that is being timed.
2365 
2366    Level: intermediate
2367 
2368 .seealso: TaoGetHistory()
2369 
2370 @*/
2371 PetscErrorCode TaoSetHistory(Tao tao, PetscReal *obj, PetscReal *resid, PetscReal *cnorm, PetscInt na,PetscBool reset)
2372 {
2373   PetscFunctionBegin;
2374   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2375   tao->hist_obj = obj;
2376   tao->hist_resid = resid;
2377   tao->hist_cnorm = cnorm;
2378   tao->hist_max   = na;
2379   tao->hist_reset = reset;
2380   PetscFunctionReturn(0);
2381 }
2382 
2383 #undef __FUNCT__
2384 #define __FUNCT__ "TaoGetHistory"
2385 /*@C
2386    TaoGetHistory - Gets the array used to hold the convergence history.
2387 
2388    Collective on Tao
2389 
2390    Input Parameter:
2391 .  tao - the Tao context
2392 
2393    Output Parameters:
2394 +  obj   - array used to hold objective value history
2395 .  resid - array used to hold residual history
2396 .  cnorm - array used to hold constraint violation history
2397 -  nhist  - size of obj, resid, and cnorm (will be less than or equal to na given in TaoSetHistory)
2398 
2399    Notes:
2400     The calling sequence for this routine in Fortran is
2401 $   call TaoGetHistory(Tao tao, integer nhist, integer info)
2402 
2403    This routine is useful, e.g., when running a code for purposes
2404    of accurate performance monitoring, when no I/O should be done
2405    during the section of code that is being timed.
2406 
2407    Level: advanced
2408 
2409 .seealso: TaoSetHistory()
2410 
2411 @*/
2412 PetscErrorCode TaoGetHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt *nhist)
2413 {
2414   PetscFunctionBegin;
2415   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2416   if (obj)   *obj   = tao->hist_obj;
2417   if (cnorm) *cnorm = tao->hist_cnorm;
2418   if (resid) *resid = tao->hist_resid;
2419   if (nhist) *nhist   = tao->hist_len;
2420   PetscFunctionReturn(0);
2421 }
2422 
2423 #undef __FUNCT__
2424 #define __FUNCT__ "TaoSetApplicationContext"
2425 /*@
2426    TaoSetApplicationContext - Sets the optional user-defined context for
2427    a solver.
2428 
2429    Logically Collective on Tao
2430 
2431    Input Parameters:
2432 +  tao  - the Tao context
2433 -  usrP - optional user context
2434 
2435    Level: intermediate
2436 
2437 .seealso: TaoGetApplicationContext(), TaoSetApplicationContext()
2438 @*/
2439 PetscErrorCode  TaoSetApplicationContext(Tao tao,void *usrP)
2440 {
2441   PetscFunctionBegin;
2442   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2443   tao->user = usrP;
2444   PetscFunctionReturn(0);
2445 }
2446 
2447 #undef __FUNCT__
2448 #define __FUNCT__ "TaoGetApplicationContext"
2449 /*@
2450    TaoGetApplicationContext - Gets the user-defined context for a
2451    TAO solvers.
2452 
2453    Not Collective
2454 
2455    Input Parameter:
2456 .  tao  - Tao context
2457 
2458    Output Parameter:
2459 .  usrP - user context
2460 
2461    Level: intermediate
2462 
2463 .seealso: TaoSetApplicationContext()
2464 @*/
2465 PetscErrorCode  TaoGetApplicationContext(Tao tao,void *usrP)
2466 {
2467   PetscFunctionBegin;
2468   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2469   *(void**)usrP = tao->user;
2470   PetscFunctionReturn(0);
2471 }
2472