xref: /petsc/src/tao/interface/taosolver.c (revision 770232b93b0ce534b7a05f58202c0496d87265ec)
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__ "TaoGetCurrentFunctionEvaluations"
928 /*@
929    TaoGetCurrentFunctionEvaluations - Get current number of
930    function evaluations.
931 
932    Not Collective
933 
934    Input Parameters:
935 .  tao - the Tao solver context
936 
937    Output Parameters:
938 .  nfuncs - the current number of function evaluations
939 
940    Level: intermediate
941 
942 .seealso: TaoSetMaximumFunctionEvaluations(), TaoGetMaximumFunctionEvaluations(), TaoGetMaximumIterations()
943 @*/
944 
945 PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao,PetscInt *nfuncs)
946 {
947   PetscFunctionBegin;
948   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
949   *nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads);
950   PetscFunctionReturn(0);
951 }
952 
953 #undef __FUNCT__
954 #define __FUNCT__ "TaoSetMaximumIterations"
955 /*@
956    TaoSetMaximumIterations - Sets a maximum number of iterates.
957 
958    Logically Collective on Tao
959 
960    Input Parameters:
961 +  tao - the Tao solver context
962 -  maxits - the maximum number of iterates (>=0)
963 
964    Options Database Keys:
965 .    -tao_max_it <its> - sets the maximum number of iterations
966 
967    Level: intermediate
968 
969 .seealso: TaoSetTolerances(), TaoSetMaximumFunctionEvaluations()
970 @*/
971 PetscErrorCode TaoSetMaximumIterations(Tao tao,PetscInt maxits)
972 {
973   PetscFunctionBegin;
974   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
975   tao->max_it = PetscMax(0,maxits);
976   PetscFunctionReturn(0);
977 }
978 
979 #undef __FUNCT__
980 #define __FUNCT__ "TaoGetMaximumIterations"
981 /*@
982    TaoGetMaximumIterations - Sets a maximum number of iterates.
983 
984    Not Collective
985 
986    Input Parameters:
987 .  tao - the Tao solver context
988 
989    Output Parameters:
990 .  maxits - the maximum number of iterates
991 
992    Level: intermediate
993 
994 .seealso: TaoSetMaximumIterations(), TaoGetMaximumFunctionEvaluations()
995 @*/
996 PetscErrorCode TaoGetMaximumIterations(Tao tao,PetscInt *maxits)
997 {
998   PetscFunctionBegin;
999   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1000   *maxits = tao->max_it;
1001   PetscFunctionReturn(0);
1002 }
1003 
1004 #undef __FUNCT__
1005 #define __FUNCT__ "TaoSetInitialTrustRegionRadius"
1006 /*@
1007    TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
1008 
1009    Logically collective on Tao
1010 
1011    Input Parameter:
1012 +  tao - a TAO optimization solver
1013 -  radius - the trust region radius
1014 
1015    Level: intermediate
1016 
1017    Options Database Key:
1018 .  -tao_trust0 <t0> - sets initial trust region radius
1019 
1020 .seealso: TaoGetTrustRegionRadius(), TaoSetTrustRegionTolerance()
1021 @*/
1022 PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
1023 {
1024   PetscFunctionBegin;
1025   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1026   tao->trust0 = PetscMax(0.0,radius);
1027   PetscFunctionReturn(0);
1028 }
1029 
1030 #undef __FUNCT__
1031 #define __FUNCT__ "TaoGetInitialTrustRegionRadius"
1032 /*@
1033    TaoGetInitialTrustRegionRadius - Sets the initial trust region radius.
1034 
1035    Not Collective
1036 
1037    Input Parameter:
1038 .  tao - a TAO optimization solver
1039 
1040    Output Parameter:
1041 .  radius - the trust region radius
1042 
1043    Level: intermediate
1044 
1045 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetCurrentTrustRegionRadius()
1046 @*/
1047 PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1048 {
1049   PetscFunctionBegin;
1050   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1051   *radius = tao->trust0;
1052   PetscFunctionReturn(0);
1053 }
1054 
1055 #undef __FUNCT__
1056 #define __FUNCT__ "TaoGetCurrentTrustRegionRadius"
1057 /*@
1058    TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1059 
1060    Not Collective
1061 
1062    Input Parameter:
1063 .  tao - a TAO optimization solver
1064 
1065    Output Parameter:
1066 .  radius - the trust region radius
1067 
1068    Level: intermediate
1069 
1070 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetInitialTrustRegionRadius()
1071 @*/
1072 PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1073 {
1074   PetscFunctionBegin;
1075   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1076   *radius = tao->trust;
1077   PetscFunctionReturn(0);
1078 }
1079 
1080 #undef __FUNCT__
1081 #define __FUNCT__ "TaoGetTolerances"
1082 /*@
1083   TaoGetTolerances - gets the current values of tolerances
1084 
1085   Not Collective
1086 
1087   Input Parameters:
1088 . tao - the Tao context
1089 
1090   Output Parameters:
1091 + fatol - absolute convergence tolerance
1092 . frtol - relative convergence tolerance
1093 . gatol - stop if norm of gradient is less than this
1094 . grtol - stop if relative norm of gradient is less than this
1095 - gttol - stop if norm of gradient is reduced by a this factor
1096 
1097   Note: NULL can be used as an argument if not all tolerances values are needed
1098 
1099 .seealso TaoSetTolerances()
1100 
1101   Level: intermediate
1102 @*/
1103 PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *fatol, PetscReal *frtol, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1104 {
1105   PetscFunctionBegin;
1106   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1107   if (fatol) *fatol=tao->fatol;
1108   if (frtol) *frtol=tao->frtol;
1109   if (gatol) *gatol=tao->gatol;
1110   if (grtol) *grtol=tao->grtol;
1111   if (gttol) *gttol=tao->gttol;
1112   PetscFunctionReturn(0);
1113 }
1114 
1115 #undef __FUNCT__
1116 #define __FUNCT__ "TaoGetKSP"
1117 /*@
1118   TaoGetKSP - Gets the linear solver used by the optimization solver.
1119   Application writers should use TaoGetKSP if they need direct access
1120   to the PETSc KSP object.
1121 
1122   Not Collective
1123 
1124    Input Parameters:
1125 .  tao - the TAO solver
1126 
1127    Output Parameters:
1128 .  ksp - the KSP linear solver used in the optimization solver
1129 
1130    Level: intermediate
1131 
1132 @*/
1133 PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
1134 {
1135   PetscFunctionBegin;
1136   *ksp = tao->ksp;
1137   PetscFunctionReturn(0);
1138 }
1139 
1140 #undef __FUNCT__
1141 #define __FUNCT__ "TaoGetLineSearch"
1142 /*@
1143   TaoGetLineSearch - Gets the line search used by the optimization solver.
1144   Application writers should use TaoGetLineSearch if they need direct access
1145   to the TaoLineSearch object.
1146 
1147   Not Collective
1148 
1149    Input Parameters:
1150 .  tao - the TAO solver
1151 
1152    Output Parameters:
1153 .  ls - the line search used in the optimization solver
1154 
1155    Level: intermediate
1156 
1157 @*/
1158 PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
1159 {
1160   PetscFunctionBegin;
1161   *ls = tao->linesearch;
1162   PetscFunctionReturn(0);
1163 }
1164 
1165 #undef __FUNCT__
1166 #define __FUNCT__ "TaoAddLineSearchCounts"
1167 /*@
1168   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1169   in the line search to the running total.
1170 
1171    Input Parameters:
1172 +  tao - the TAO solver
1173 -  ls - the line search used in the optimization solver
1174 
1175    Level: developer
1176 
1177 .seealso: TaoLineSearchApply()
1178 @*/
1179 PetscErrorCode TaoAddLineSearchCounts(Tao tao)
1180 {
1181   PetscErrorCode ierr;
1182   PetscBool      flg;
1183   PetscInt       nfeval,ngeval,nfgeval;
1184 
1185   PetscFunctionBegin;
1186   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1187   if (tao->linesearch) {
1188     ierr = TaoLineSearchIsUsingTaoRoutines(tao->linesearch,&flg);
1189     if (flg == PETSC_FALSE) {
1190       ierr = TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch,&nfeval,&ngeval,&nfgeval);CHKERRQ(ierr);
1191       tao->nfuncs+=nfeval;
1192       tao->ngrads+=ngeval;
1193       tao->nfuncgrads+=nfgeval;
1194     }
1195   }
1196   PetscFunctionReturn(0);
1197 }
1198 
1199 #undef __FUNCT__
1200 #define __FUNCT__ "TaoGetSolutionVector"
1201 /*@
1202   TaoGetSolutionVector - Returns the vector with the current TAO solution
1203 
1204   Not Collective
1205 
1206   Input Parameter:
1207 . tao - the Tao context
1208 
1209   Output Parameter:
1210 . X - the current solution
1211 
1212   Level: intermediate
1213 
1214   Note:  The returned vector will be the same object that was passed into TaoSetInitialVector()
1215 @*/
1216 PetscErrorCode TaoGetSolutionVector(Tao tao, Vec *X)
1217 {
1218   PetscFunctionBegin;
1219   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1220   *X = tao->solution;
1221   PetscFunctionReturn(0);
1222 }
1223 
1224 #undef __FUNCT__
1225 #define __FUNCT__ "TaoGetGradientVector"
1226 /*@
1227   TaoGetGradientVector - Returns the vector with the current TAO gradient
1228 
1229   Not Collective
1230 
1231   Input Parameter:
1232 . tao - the Tao context
1233 
1234   Output Parameter:
1235 . G - the current solution
1236 
1237   Level: intermediate
1238 @*/
1239 PetscErrorCode TaoGetGradientVector(Tao tao, Vec *G)
1240 {
1241   PetscFunctionBegin;
1242   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1243   *G = tao->gradient;
1244   PetscFunctionReturn(0);
1245 }
1246 
1247 #undef __FUNCT__
1248 #define __FUNCT__ "TaoResetStatistics"
1249 /*@
1250    TaoResetStatistics - Initialize the statistics used by TAO for all of the solvers.
1251    These statistics include the iteration number, residual norms, and convergence status.
1252    This routine gets called before solving each optimization problem.
1253 
1254    Collective on Tao
1255 
1256    Input Parameters:
1257 .  solver - the Tao context
1258 
1259    Level: developer
1260 
1261 .seealso: TaoCreate(), TaoSolve()
1262 @*/
1263 PetscErrorCode TaoResetStatistics(Tao tao)
1264 {
1265   PetscFunctionBegin;
1266   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1267   tao->niter        = 0;
1268   tao->nfuncs       = 0;
1269   tao->nfuncgrads   = 0;
1270   tao->ngrads       = 0;
1271   tao->nhess        = 0;
1272   tao->njac         = 0;
1273   tao->nconstraints = 0;
1274   tao->ksp_its      = 0;
1275   tao->reason       = TAO_CONTINUE_ITERATING;
1276   tao->residual     = 0.0;
1277   tao->cnorm        = 0.0;
1278   tao->step         = 0.0;
1279   tao->lsflag       = PETSC_FALSE;
1280   if (tao->hist_reset) tao->hist_len=0;
1281   PetscFunctionReturn(0);
1282 }
1283 
1284 #undef __FUNCT__
1285 #define __FUNCT__ "TaoSetConvergenceTest"
1286 /*@C
1287   TaoSetConvergenceTest - Sets the function that is to be used to test
1288   for convergence o fthe iterative minimization solution.  The new convergence
1289   testing routine will replace TAO's default convergence test.
1290 
1291   Logically Collective on Tao
1292 
1293   Input Parameters:
1294 + tao - the Tao object
1295 . conv - the routine to test for convergence
1296 - ctx - [optional] context for private data for the convergence routine
1297         (may be NULL)
1298 
1299   Calling sequence of conv:
1300 $   PetscErrorCode conv(Tao tao, void *ctx)
1301 
1302 + tao - the Tao object
1303 - ctx - [optional] convergence context
1304 
1305   Note: The new convergence testing routine should call TaoSetConvergedReason().
1306 
1307   Level: advanced
1308 
1309 .seealso: TaoSetConvergedReason(), TaoGetSolutionStatus(), TaoGetTolerances(), TaoSetMonitor
1310 
1311 @*/
1312 PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao,void*), void *ctx)
1313 {
1314   PetscFunctionBegin;
1315   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1316   (tao)->ops->convergencetest = conv;
1317   (tao)->cnvP = ctx;
1318   PetscFunctionReturn(0);
1319 }
1320 
1321 #undef __FUNCT__
1322 #define __FUNCT__ "TaoSetMonitor"
1323 /*@C
1324    TaoSetMonitor - Sets an ADDITIONAL function that is to be used at every
1325    iteration of the solver to display the iteration's
1326    progress.
1327 
1328    Logically Collective on Tao
1329 
1330    Input Parameters:
1331 +  tao - the Tao solver context
1332 .  mymonitor - monitoring routine
1333 -  mctx - [optional] user-defined context for private data for the
1334           monitor routine (may be NULL)
1335 
1336    Calling sequence of mymonitor:
1337 $     int mymonitor(Tao tao,void *mctx)
1338 
1339 +    tao - the Tao solver context
1340 -    mctx - [optional] monitoring context
1341 
1342 
1343    Options Database Keys:
1344 +    -tao_monitor        - sets TaoDefaultMonitor()
1345 .    -tao_smonitor       - sets short monitor
1346 .    -tao_cmonitor       - same as smonitor plus constraint norm
1347 .    -tao_view_solution   - view solution at each iteration
1348 .    -tao_view_gradient   - view gradient at each iteration
1349 .    -tao_view_separableobjective - view separable objective function at each iteration
1350 -    -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.
1351 
1352 
1353    Notes:
1354    Several different monitoring routines may be set by calling
1355    TaoSetMonitor() multiple times; all will be called in the
1356    order in which they were set.
1357 
1358    Fortran Notes: Only one monitor function may be set
1359 
1360    Level: intermediate
1361 
1362 .seealso: TaoDefaultMonitor(), TaoCancelMonitors(),  TaoSetDestroyRoutine()
1363 @*/
1364 PetscErrorCode TaoSetMonitor(Tao tao, PetscErrorCode (*func)(Tao, void*), void *ctx,PetscErrorCode (*dest)(void**))
1365 {
1366   PetscFunctionBegin;
1367   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1368   if (tao->numbermonitors >= MAXTAOMONITORS) SETERRQ1(PETSC_COMM_SELF,1,"Cannot attach another monitor -- max=",MAXTAOMONITORS);
1369   tao->monitor[tao->numbermonitors] = func;
1370   tao->monitorcontext[tao->numbermonitors] = ctx;
1371   tao->monitordestroy[tao->numbermonitors] = dest;
1372   ++tao->numbermonitors;
1373   PetscFunctionReturn(0);
1374 }
1375 
1376 #undef __FUNCT__
1377 #define __FUNCT__ "TaoCancelMonitors"
1378 /*@
1379    TaoCancelMonitors - Clears all the monitor functions for a Tao object.
1380 
1381    Logically Collective on Tao
1382 
1383    Input Parameters:
1384 .  tao - the Tao solver context
1385 
1386    Options Database:
1387 .  -tao_cancelmonitors - cancels all monitors that have been hardwired
1388     into a code by calls to TaoSetMonitor(), but does not cancel those
1389     set via the options database
1390 
1391    Notes:
1392    There is no way to clear one specific monitor from a Tao object.
1393 
1394    Level: advanced
1395 
1396 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1397 @*/
1398 PetscErrorCode TaoCancelMonitors(Tao tao)
1399 {
1400   PetscInt       i;
1401   PetscErrorCode ierr;
1402 
1403   PetscFunctionBegin;
1404   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1405   for (i=0;i<tao->numbermonitors;i++) {
1406     if (tao->monitordestroy[i]) {
1407       ierr = (*tao->monitordestroy[i])(&tao->monitorcontext[i]);CHKERRQ(ierr);
1408     }
1409   }
1410   tao->numbermonitors=0;
1411   PetscFunctionReturn(0);
1412 }
1413 
1414 #undef __FUNCT__
1415 #define __FUNCT__ "TaoDefaultMonitor"
1416 /*@C
1417    TaoDefaultMonitor - Default routine for monitoring progress of the
1418    Tao solvers (default).  This monitor prints the function value and gradient
1419    norm at each iteration.  It can be turned on from the command line using the
1420    -tao_monitor option
1421 
1422    Collective on Tao
1423 
1424    Input Parameters:
1425 +  tao - the Tao context
1426 -  ctx - PetscViewer context or NULL
1427 
1428    Options Database Keys:
1429 .  -tao_monitor
1430 
1431    Level: advanced
1432 
1433 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1434 @*/
1435 PetscErrorCode TaoDefaultMonitor(Tao tao, void *ctx)
1436 {
1437   PetscErrorCode ierr;
1438   PetscInt       its;
1439   PetscReal      fct,gnorm;
1440   PetscViewer    viewer;
1441 
1442   PetscFunctionBegin;
1443   if (ctx) {
1444     viewer = (PetscViewer)ctx;
1445   } else {
1446     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1447   }
1448   its=tao->niter;
1449   fct=tao->fc;
1450   gnorm=tao->residual;
1451   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1452   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1453   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1454   PetscFunctionReturn(0);
1455 }
1456 
1457 #undef __FUNCT__
1458 #define __FUNCT__ "TaoDefaultSMonitor"
1459 /*@C
1460    TaoDefaultSMonitor - Default routine for monitoring progress of the
1461    solver. Same as TaoDefaultMonitor() except
1462    it prints fewer digits of the residual as the residual gets smaller.
1463    This is because the later digits are meaningless and are often
1464    different on different machines; by using this routine different
1465    machines will usually generate the same output. It can be turned on
1466    by using the -tao_smonitor option
1467 
1468    Collective on Tao
1469 
1470    Input Parameters:
1471 +  tao - the Tao context
1472 -  ctx - PetscViewer context or NULL
1473 
1474    Options Database Keys:
1475 .  -tao_smonitor
1476 
1477    Level: advanced
1478 
1479 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1480 @*/
1481 PetscErrorCode TaoDefaultSMonitor(Tao tao, void *ctx)
1482 {
1483   PetscErrorCode ierr;
1484   PetscInt       its;
1485   PetscReal      fct,gnorm;
1486   PetscViewer    viewer;
1487 
1488   PetscFunctionBegin;
1489   if (ctx) {
1490     viewer = (PetscViewer)ctx;
1491   } else {
1492     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1493   }
1494   its=tao->niter;
1495   fct=tao->fc;
1496   gnorm=tao->residual;
1497   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1498   ierr=PetscViewerASCIIPrintf(viewer," Function value %g,",(double)fct);CHKERRQ(ierr);
1499   if (gnorm > 1.e-6) {
1500     ierr=PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1501   } else if (gnorm > 1.e-11) {
1502     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-6 \n");CHKERRQ(ierr);
1503   } else {
1504     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-11 \n");CHKERRQ(ierr);
1505   }
1506   PetscFunctionReturn(0);
1507 }
1508 
1509 #undef __FUNCT__
1510 #define __FUNCT__ "TaoDefaultCMonitor"
1511 /*@C
1512    TaoDefaultCMonitor - same as TaoDefaultMonitor() except
1513    it prints the norm of the constraints function. It can be turned on
1514    from the command line using the -tao_cmonitor option
1515 
1516    Collective on Tao
1517 
1518    Input Parameters:
1519 +  tao - the Tao context
1520 -  ctx - PetscViewer context or NULL
1521 
1522    Options Database Keys:
1523 .  -tao_cmonitor
1524 
1525    Level: advanced
1526 
1527 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1528 @*/
1529 PetscErrorCode TaoDefaultCMonitor(Tao tao, void *ctx)
1530 {
1531   PetscErrorCode ierr;
1532   PetscInt       its;
1533   PetscReal      fct,gnorm;
1534   PetscViewer    viewer;
1535 
1536   PetscFunctionBegin;
1537   if (ctx) {
1538     viewer = (PetscViewer)ctx;
1539   } else {
1540     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1541   }
1542   its=tao->niter;
1543   fct=tao->fc;
1544   gnorm=tao->residual;
1545   ierr=PetscViewerASCIIPrintf(viewer,"iter = %D,",its);CHKERRQ(ierr);
1546   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1547   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g ",(double)gnorm);CHKERRQ(ierr);
1548   ierr = PetscViewerASCIIPrintf(viewer,"  Constraint: %g \n",(double)tao->cnorm);CHKERRQ(ierr);
1549   PetscFunctionReturn(0);
1550 }
1551 
1552 #undef __FUNCT__
1553 #define __FUNCT__ "TaoSolutionMonitor"
1554 /*@C
1555    TaoSolutionMonitor - Views the solution at each iteration
1556    It can be turned on from the command line using the
1557    -tao_view_solution option
1558 
1559    Collective on Tao
1560 
1561    Input Parameters:
1562 +  tao - the Tao context
1563 -  ctx - PetscViewer context or NULL
1564 
1565    Options Database Keys:
1566 .  -tao_view_solution
1567 
1568    Level: advanced
1569 
1570 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1571 @*/
1572 PetscErrorCode TaoSolutionMonitor(Tao tao, void *ctx)
1573 {
1574   PetscErrorCode ierr;
1575   PetscViewer viewer;
1576 
1577   PetscFunctionBegin;
1578   if (ctx) {
1579     viewer = (PetscViewer)ctx;
1580   } else {
1581     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1582   }
1583   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1584   PetscFunctionReturn(0);
1585 }
1586 
1587 #undef __FUNCT__
1588 #define __FUNCT__ "TaoGradientMonitor"
1589 /*@C
1590    TaoGradientMonitor - Views the gradient at each iteration
1591    It can be turned on from the command line using the
1592    -tao_view_gradient option
1593 
1594    Collective on Tao
1595 
1596    Input Parameters:
1597 +  tao - the Tao context
1598 -  ctx - PetscViewer context or NULL
1599 
1600    Options Database Keys:
1601 .  -tao_view_gradient
1602 
1603    Level: advanced
1604 
1605 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1606 @*/
1607 PetscErrorCode TaoGradientMonitor(Tao tao, void *ctx)
1608 {
1609   PetscErrorCode ierr;
1610   PetscViewer viewer;
1611 
1612   PetscFunctionBegin;
1613   if (ctx) {
1614     viewer = (PetscViewer)ctx;
1615   } else {
1616     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1617   }
1618   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1619   PetscFunctionReturn(0);
1620 }
1621 
1622 #undef __FUNCT__
1623 #define __FUNCT__ "TaoStepDirectionMonitor"
1624 /*@C
1625    TaoStepDirectionMonitor - Views the gradient at each iteration
1626    It can be turned on from the command line using the
1627    -tao_view_gradient option
1628 
1629    Collective on Tao
1630 
1631    Input Parameters:
1632 +  tao - the Tao context
1633 -  ctx - PetscViewer context or NULL
1634 
1635    Options Database Keys:
1636 .  -tao_view_gradient
1637 
1638    Level: advanced
1639 
1640 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1641 @*/
1642 PetscErrorCode TaoStepDirectionMonitor(Tao tao, void *ctx)
1643 {
1644   PetscErrorCode ierr;
1645   PetscViewer viewer;
1646   PetscFunctionBegin;
1647   if (ctx) {
1648     viewer = (PetscViewer)ctx;
1649   } else {
1650     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1651   }
1652   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1653   PetscFunctionReturn(0);
1654 }
1655 
1656 #undef __FUNCT__
1657 #define __FUNCT__ "TaoDrawSolutionMonitor"
1658 /*@C
1659    TaoDrawSolutionMonitor - Plots the solution at each iteration
1660    It can be turned on from the command line using the
1661    -tao_draw_solution option
1662 
1663    Collective on Tao
1664 
1665    Input Parameters:
1666 +  tao - the Tao context
1667 -  ctx - PetscViewer context or NULL
1668 
1669    Options Database Keys:
1670 .  -tao_draw_solution
1671 
1672    Level: advanced
1673 
1674 .seealso: TaoSolutionMonitor(), TaoSetMonitor(), TaoDrawGradientMonitor
1675 @*/
1676 PetscErrorCode TaoDrawSolutionMonitor(Tao tao, void *ctx)
1677 {
1678   PetscErrorCode ierr;
1679   PetscViewer    viewer = (PetscViewer) ctx;
1680   MPI_Comm       comm;
1681 
1682   PetscFunctionBegin;
1683   if (!viewer) {
1684     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1685     viewer = PETSC_VIEWER_DRAW_(comm);
1686   }
1687   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1688   PetscFunctionReturn(0);
1689 }
1690 
1691 #undef __FUNCT__
1692 #define __FUNCT__ "TaoDrawGradientMonitor"
1693 /*@C
1694    TaoDrawGradientMonitor - Plots the gradient at each iteration
1695    It can be turned on from the command line using the
1696    -tao_draw_gradient option
1697 
1698    Collective on Tao
1699 
1700    Input Parameters:
1701 +  tao - the Tao context
1702 -  ctx - PetscViewer context or NULL
1703 
1704    Options Database Keys:
1705 .  -tao_draw_gradient
1706 
1707    Level: advanced
1708 
1709 .seealso: TaoGradientMonitor(), TaoSetMonitor(), TaoDrawSolutionMonitor
1710 @*/
1711 PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx)
1712 {
1713   PetscErrorCode ierr;
1714   PetscViewer    viewer = (PetscViewer)ctx;
1715   MPI_Comm       comm;
1716 
1717   PetscFunctionBegin;
1718   if (!viewer) {
1719     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1720     viewer = PETSC_VIEWER_DRAW_(comm);
1721   }
1722   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1723   PetscFunctionReturn(0);
1724 }
1725 
1726 #undef __FUNCT__
1727 #define __FUNCT__ "TaoDrawStepMonitor"
1728 /*@C
1729    TaoDrawStepMonitor - Plots the step direction at each iteration
1730    It can be turned on from the command line using the
1731    -tao_draw_step option
1732 
1733    Collective on Tao
1734 
1735    Input Parameters:
1736 +  tao - the Tao context
1737 -  ctx - PetscViewer context or NULL
1738 
1739    Options Database Keys:
1740 .  -tao_draw_step
1741 
1742    Level: advanced
1743 
1744 .seealso: TaoSetMonitor(), TaoDrawSolutionMonitor
1745 @*/
1746 PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx)
1747 {
1748   PetscErrorCode ierr;
1749   PetscViewer    viewer = (PetscViewer)(ctx);
1750   MPI_Comm       comm;
1751 
1752   PetscFunctionBegin;
1753   if (!viewer) {
1754     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1755     viewer = PETSC_VIEWER_DRAW_(comm);
1756   }
1757   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1758   PetscFunctionReturn(0);
1759 }
1760 
1761 #undef __FUNCT__
1762 #define __FUNCT__ "TaoSeparableObjectiveMonitor"
1763 /*@C
1764    TaoSeparableObjectiveMonitor - Views the separable objective function at each iteration
1765    It can be turned on from the command line using the
1766    -tao_view_separableobjective option
1767 
1768    Collective on Tao
1769 
1770    Input Parameters:
1771 +  tao - the Tao context
1772 -  ctx - PetscViewer context or NULL
1773 
1774    Options Database Keys:
1775 .  -tao_view_separableobjective
1776 
1777    Level: advanced
1778 
1779 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1780 @*/
1781 PetscErrorCode TaoSeparableObjectiveMonitor(Tao tao, void *ctx)
1782 {
1783   PetscErrorCode ierr;
1784   PetscViewer    viewer;
1785 
1786   PetscFunctionBegin;
1787   if (ctx) {
1788     viewer = (PetscViewer)ctx;
1789   } else {
1790     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1791   }
1792   ierr = VecView(tao->sep_objective,viewer);CHKERRQ(ierr);
1793   PetscFunctionReturn(0);
1794 }
1795 
1796 #undef __FUNCT__
1797 #define __FUNCT__ "TaoDefaultConvergenceTest"
1798 /*@C
1799    TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1800    or terminate.
1801 
1802    Collective on Tao
1803 
1804    Input Parameters:
1805 +  tao - the Tao context
1806 -  dummy - unused dummy context
1807 
1808    Output Parameter:
1809 .  reason - for terminating
1810 
1811    Notes:
1812    This routine checks the residual in the optimality conditions, the
1813    relative residual in the optimity conditions, the number of function
1814    evaluations, and the function value to test convergence.  Some
1815    solvers may use different convergence routines.
1816 
1817    Level: developer
1818 
1819 .seealso: TaoSetTolerances(),TaoGetConvergedReason(),TaoSetConvergedReason()
1820 @*/
1821 
1822 PetscErrorCode TaoDefaultConvergenceTest(Tao tao,void *dummy)
1823 {
1824   PetscInt           niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads);
1825   PetscInt           max_funcs=tao->max_funcs;
1826   PetscReal          gnorm=tao->residual, gnorm0=tao->gnorm0;
1827   PetscReal          f=tao->fc, steptol=tao->steptol,trradius=tao->step;
1828   PetscReal          gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol;
1829   PetscReal          fatol=tao->fatol,frtol=tao->frtol,catol=tao->catol,crtol=tao->crtol;
1830   PetscReal          fmin=tao->fmin, cnorm=tao->cnorm, cnorm0=tao->cnorm0;
1831   PetscReal          gnorm2;
1832   TaoConvergedReason reason=tao->reason;
1833   PetscErrorCode     ierr;
1834 
1835   PetscFunctionBegin;
1836   PetscValidHeaderSpecific(tao, TAO_CLASSID,1);
1837   if (reason != TAO_CONTINUE_ITERATING) {
1838     PetscFunctionReturn(0);
1839   }
1840   gnorm2=gnorm*gnorm;
1841 
1842   if (PetscIsInfOrNanReal(f)) {
1843     ierr = PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n");CHKERRQ(ierr);
1844     reason = TAO_DIVERGED_NAN;
1845   } else if (f <= fmin && cnorm <=catol) {
1846     ierr = PetscInfo2(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin);CHKERRQ(ierr);
1847     reason = TAO_CONVERGED_MINF;
1848   } else if (gnorm2 <= fatol && cnorm <=catol) {
1849     ierr = PetscInfo2(tao,"Converged due to estimated f(X) - f(X*) = %g < %g\n",(double)gnorm2,(double)fatol);CHKERRQ(ierr);
1850     reason = TAO_CONVERGED_FATOL;
1851   } else if (f != 0 && gnorm2 / PetscAbsReal(f)<= frtol && cnorm/PetscMax(cnorm0,1.0) <= crtol) {
1852     ierr = PetscInfo2(tao,"Converged due to estimated |f(X)-f(X*)|/f(X) = %g < %g\n",(double)(gnorm2/PetscAbsReal(f)),(double)frtol);CHKERRQ(ierr);
1853     reason = TAO_CONVERGED_FRTOL;
1854   } else if (gnorm<= gatol && cnorm <=catol) {
1855     ierr = PetscInfo2(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol);CHKERRQ(ierr);
1856     reason = TAO_CONVERGED_GATOL;
1857   } else if ( f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) {
1858     ierr = PetscInfo2(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol);CHKERRQ(ierr);
1859     reason = TAO_CONVERGED_GRTOL;
1860   } else if (gnorm0 != 0 && gnorm/gnorm0 <= gttol && cnorm <= crtol) {
1861     ierr = PetscInfo2(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol);CHKERRQ(ierr);
1862     reason = TAO_CONVERGED_GTTOL;
1863   } else if (nfuncs > max_funcs){
1864     ierr = PetscInfo2(tao,"Exceeded maximum number of function evaluations: %D > %D\n", nfuncs,max_funcs);CHKERRQ(ierr);
1865     reason = TAO_DIVERGED_MAXFCN;
1866   } else if ( tao->lsflag != 0 ){
1867     ierr = PetscInfo(tao,"Tao Line Search failure.\n");CHKERRQ(ierr);
1868     reason = TAO_DIVERGED_LS_FAILURE;
1869   } else if (trradius < steptol && niter > 0){
1870     ierr = PetscInfo2(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol);CHKERRQ(ierr);
1871     reason = TAO_CONVERGED_STEPTOL;
1872   } else if (niter > tao->max_it) {
1873     ierr = PetscInfo2(tao,"Exceeded maximum number of iterations: %D > %D\n",niter,tao->max_it);CHKERRQ(ierr);
1874     reason = TAO_DIVERGED_MAXITS;
1875   } else {
1876     reason = TAO_CONTINUE_ITERATING;
1877   }
1878   tao->reason = reason;
1879   PetscFunctionReturn(0);
1880 }
1881 
1882 #undef __FUNCT__
1883 #define __FUNCT__ "TaoSetOptionsPrefix"
1884 /*@C
1885    TaoSetOptionsPrefix - Sets the prefix used for searching for all
1886    TAO options in the database.
1887 
1888 
1889    Logically Collective on Tao
1890 
1891    Input Parameters:
1892 +  tao - the Tao context
1893 -  prefix - the prefix string to prepend to all TAO option requests
1894 
1895    Notes:
1896    A hyphen (-) must NOT be given at the beginning of the prefix name.
1897    The first character of all runtime options is AUTOMATICALLY the hyphen.
1898 
1899    For example, to distinguish between the runtime options for two
1900    different TAO solvers, one could call
1901 .vb
1902       TaoSetOptionsPrefix(tao1,"sys1_")
1903       TaoSetOptionsPrefix(tao2,"sys2_")
1904 .ve
1905 
1906    This would enable use of different options for each system, such as
1907 .vb
1908       -sys1_tao_method blmvm -sys1_tao_gtol 1.e-3
1909       -sys2_tao_method lmvm  -sys2_tao_gtol 1.e-4
1910 .ve
1911 
1912 
1913    Level: advanced
1914 
1915 .seealso: TaoAppendOptionsPrefix(), TaoGetOptionsPrefix()
1916 @*/
1917 
1918 PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
1919 {
1920   PetscErrorCode ierr;
1921 
1922   PetscFunctionBegin;
1923   ierr = PetscObjectSetOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1924   if (tao->linesearch) {
1925     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1926   }
1927   if (tao->ksp) {
1928     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1929   }
1930   PetscFunctionReturn(0);
1931 }
1932 
1933 #undef __FUNCT__
1934 #define __FUNCT__ "TaoAppendOptionsPrefix"
1935 /*@C
1936    TaoAppendOptionsPrefix - Appends to the prefix used for searching for all
1937    TAO options in the database.
1938 
1939 
1940    Logically Collective on Tao
1941 
1942    Input Parameters:
1943 +  tao - the Tao solver context
1944 -  prefix - the prefix string to prepend to all TAO option requests
1945 
1946    Notes:
1947    A hyphen (-) must NOT be given at the beginning of the prefix name.
1948    The first character of all runtime options is AUTOMATICALLY the hyphen.
1949 
1950 
1951    Level: advanced
1952 
1953 .seealso: TaoSetOptionsPrefix(), TaoGetOptionsPrefix()
1954 @*/
1955 PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
1956 {
1957   PetscErrorCode ierr;
1958 
1959   PetscFunctionBegin;
1960   ierr = PetscObjectAppendOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1961   if (tao->linesearch) {
1962     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1963   }
1964   if (tao->ksp) {
1965     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1966   }
1967   PetscFunctionReturn(0);
1968 }
1969 
1970 #undef __FUNCT__
1971 #define __FUNCT__ "TaoGetOptionsPrefix"
1972 /*@C
1973   TaoGetOptionsPrefix - Gets the prefix used for searching for all
1974   TAO options in the database
1975 
1976   Not Collective
1977 
1978   Input Parameters:
1979 . tao - the Tao context
1980 
1981   Output Parameters:
1982 . prefix - pointer to the prefix string used is returned
1983 
1984   Notes: On the fortran side, the user should pass in a string 'prefix' of
1985   sufficient length to hold the prefix.
1986 
1987   Level: advanced
1988 
1989 .seealso: TaoSetOptionsPrefix(), TaoAppendOptionsPrefix()
1990 @*/
1991 PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
1992 {
1993    return PetscObjectGetOptionsPrefix((PetscObject)tao,p);
1994 }
1995 
1996 #undef __FUNCT__
1997 #define __FUNCT__ "TaoSetType"
1998 /*@C
1999    TaoSetType - Sets the method for the unconstrained minimization solver.
2000 
2001    Collective on Tao
2002 
2003    Input Parameters:
2004 +  solver - the Tao solver context
2005 -  type - a known method
2006 
2007    Options Database Key:
2008 .  -tao_type <type> - Sets the method; use -help for a list
2009    of available methods (for instance, "-tao_type lmvm" or "-tao_type tron")
2010 
2011    Available methods include:
2012 +    nls - Newton's method with line search for unconstrained minimization
2013 .    ntr - Newton's method with trust region for unconstrained minimization
2014 .    ntl - Newton's method with trust region, line search for unconstrained minimization
2015 .    lmvm - Limited memory variable metric method for unconstrained minimization
2016 .    cg - Nonlinear conjugate gradient method for unconstrained minimization
2017 .    nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
2018 .    tron - Newton Trust Region method for bound constrained minimization
2019 .    gpcg - Newton Trust Region method for quadratic bound constrained minimization
2020 .    blmvm - Limited memory variable metric method for bound constrained minimization
2021 -    pounders - Model-based algorithm pounder extended for nonlinear least squares
2022 
2023   Level: intermediate
2024 
2025 .seealso: TaoCreate(), TaoGetType(), TaoType
2026 
2027 @*/
2028 PetscErrorCode TaoSetType(Tao tao, const TaoType type)
2029 {
2030   PetscErrorCode ierr;
2031   PetscErrorCode (*create_xxx)(Tao);
2032   PetscBool      issame;
2033 
2034   PetscFunctionBegin;
2035   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2036 
2037   ierr = PetscObjectTypeCompare((PetscObject)tao,type,&issame);CHKERRQ(ierr);
2038   if (issame) PetscFunctionReturn(0);
2039 
2040   ierr = PetscFunctionListFind(TaoList, type, (void(**)(void))&create_xxx);CHKERRQ(ierr);
2041   if (!create_xxx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested Tao type %s",type);
2042 
2043   /* Destroy the existing solver information */
2044   if (tao->ops->destroy) {
2045     ierr = (*tao->ops->destroy)(tao);CHKERRQ(ierr);
2046   }
2047   ierr = KSPDestroy(&tao->ksp);CHKERRQ(ierr);
2048   ierr = TaoLineSearchDestroy(&tao->linesearch);CHKERRQ(ierr);
2049   ierr = VecDestroy(&tao->gradient);CHKERRQ(ierr);
2050   ierr = VecDestroy(&tao->stepdirection);CHKERRQ(ierr);
2051 
2052   tao->ops->setup = 0;
2053   tao->ops->solve = 0;
2054   tao->ops->view  = 0;
2055   tao->ops->setfromoptions = 0;
2056   tao->ops->destroy = 0;
2057 
2058   tao->setupcalled = PETSC_FALSE;
2059 
2060   ierr = (*create_xxx)(tao);CHKERRQ(ierr);
2061   ierr = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(ierr);
2062   PetscFunctionReturn(0);
2063 }
2064 
2065 #undef __FUNCT__
2066 #define __FUNCT__ "TaoRegister"
2067 /*MC
2068    TaoRegister - Adds a method to the TAO package for unconstrained minimization.
2069 
2070    Synopsis:
2071    TaoRegister(char *name_solver,char *path,char *name_Create,int (*routine_Create)(Tao))
2072 
2073    Not collective
2074 
2075    Input Parameters:
2076 +  sname - name of a new user-defined solver
2077 -  func - routine to Create method context
2078 
2079    Notes:
2080    TaoRegister() may be called multiple times to add several user-defined solvers.
2081 
2082    Sample usage:
2083 .vb
2084    TaoRegister("my_solver",MySolverCreate);
2085 .ve
2086 
2087    Then, your solver can be chosen with the procedural interface via
2088 $     TaoSetType(tao,"my_solver")
2089    or at runtime via the option
2090 $     -tao_type my_solver
2091 
2092    Level: advanced
2093 
2094 .seealso: TaoRegisterAll(), TaoRegisterDestroy()
2095 M*/
2096 PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao))
2097 {
2098   PetscErrorCode ierr;
2099 
2100   PetscFunctionBegin;
2101   ierr = PetscFunctionListAdd(&TaoList,sname, (void (*)(void))func);CHKERRQ(ierr);
2102   PetscFunctionReturn(0);
2103 }
2104 
2105 #undef __FUNCT__
2106 #define __FUNCT__ "TaoRegisterDestroy"
2107 /*@C
2108    TaoRegisterDestroy - Frees the list of minimization solvers that were
2109    registered by TaoRegisterDynamic().
2110 
2111    Not Collective
2112 
2113    Level: advanced
2114 
2115 .seealso: TaoRegisterAll(), TaoRegister()
2116 @*/
2117 PetscErrorCode TaoRegisterDestroy(void)
2118 {
2119   PetscErrorCode ierr;
2120   PetscFunctionBegin;
2121   ierr = PetscFunctionListDestroy(&TaoList);CHKERRQ(ierr);
2122   TaoRegisterAllCalled = PETSC_FALSE;
2123   PetscFunctionReturn(0);
2124 }
2125 
2126 #undef __FUNCT__
2127 #define __FUNCT__ "TaoSetConvergedReason"
2128 /*@
2129   TaoSetConvergedReason - Sets the termination flag on a Tao object
2130 
2131   Logically Collective on Tao
2132 
2133   Input Parameters:
2134 + tao - the Tao context
2135 - reason - one of
2136 $     TAO_CONVERGED_ATOL (2),
2137 $     TAO_CONVERGED_RTOL (3),
2138 $     TAO_CONVERGED_STEPTOL (4),
2139 $     TAO_CONVERGED_MINF (5),
2140 $     TAO_CONVERGED_USER (6),
2141 $     TAO_DIVERGED_MAXITS (-2),
2142 $     TAO_DIVERGED_NAN (-4),
2143 $     TAO_DIVERGED_MAXFCN (-5),
2144 $     TAO_DIVERGED_LS_FAILURE (-6),
2145 $     TAO_DIVERGED_TR_REDUCTION (-7),
2146 $     TAO_DIVERGED_USER (-8),
2147 $     TAO_CONTINUE_ITERATING (0)
2148 
2149    Level: intermediate
2150 
2151 @*/
2152 PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2153 {
2154   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2155   PetscFunctionBegin;
2156   tao->reason = reason;
2157   PetscFunctionReturn(0);
2158 }
2159 
2160 #undef __FUNCT__
2161 #define __FUNCT__ "TaoGetConvergedReason"
2162 /*@
2163    TaoGetConvergedReason - Gets the reason the Tao iteration was stopped.
2164 
2165    Not Collective
2166 
2167    Input Parameter:
2168 .  tao - the Tao solver context
2169 
2170    Output Parameter:
2171 .  reason - one of
2172 
2173 $  TAO_CONVERGED_FATOL (1)           f(X)-f(X*) <= fatol
2174 $  TAO_CONVERGED_FRTOL (2)           |f(X) - f(X*)|/|f(X)| < frtol
2175 $  TAO_CONVERGED_GATOL (3)           ||g(X)|| < gatol
2176 $  TAO_CONVERGED_GRTOL (4)           ||g(X)|| / f(X)  < grtol
2177 $  TAO_CONVERGED_GTTOL (5)           ||g(X)|| / ||g(X0)|| < gttol
2178 $  TAO_CONVERGED_STEPTOL (6)         step size small
2179 $  TAO_CONVERGED_MINF (7)            F < F_min
2180 $  TAO_CONVERGED_USER (8)            User defined
2181 
2182 $  TAO_DIVERGED_MAXITS (-2)          its > maxits
2183 $  TAO_DIVERGED_NAN (-4)             Numerical problems
2184 $  TAO_DIVERGED_MAXFCN (-5)          fevals > max_funcsals
2185 $  TAO_DIVERGED_LS_FAILURE (-6)      line search failure
2186 $  TAO_DIVERGED_TR_REDUCTION (-7)    trust region failure
2187 $  TAO_DIVERGED_USER(-8)             (user defined)
2188 
2189 $  TAO_CONTINUE_ITERATING (0)
2190 
2191    where
2192 +  X - current solution
2193 .  X0 - initial guess
2194 .  f(X) - current function value
2195 .  f(X*) - true solution (estimated)
2196 .  g(X) - current gradient
2197 .  its - current iterate number
2198 .  maxits - maximum number of iterates
2199 .  fevals - number of function evaluations
2200 -  max_funcsals - maximum number of function evaluations
2201 
2202    Level: intermediate
2203 
2204 .seealso: TaoSetConvergenceTest(), TaoSetTolerances()
2205 
2206 @*/
2207 PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2208 {
2209   PetscFunctionBegin;
2210   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2211   PetscValidPointer(reason,2);
2212   *reason = tao->reason;
2213   PetscFunctionReturn(0);
2214 }
2215 
2216 #undef __FUNCT__
2217 #define __FUNCT__ "TaoGetSolutionStatus"
2218 /*@
2219   TaoGetSolutionStatus - Get the current iterate, objective value,
2220   residual, infeasibility, and termination
2221 
2222   Not Collective
2223 
2224    Input Parameters:
2225 .  tao - the Tao context
2226 
2227    Output Parameters:
2228 +  iterate - the current iterate number (>=0)
2229 .  f - the current function value
2230 .  gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2231 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2232 .  xdiff - the step length or trust region radius of the most recent iterate.
2233 -  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2234 
2235    Level: intermediate
2236 
2237    Note:
2238    TAO returns the values set by the solvers in the routine TaoMonitor().
2239 
2240    Note:
2241    If any of the output arguments are set to NULL, no corresponding value will be returned.
2242 
2243 .seealso: TaoMonitor(), TaoGetConvergedReason()
2244 @*/
2245 PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2246 {
2247   PetscFunctionBegin;
2248   if (its) *its=tao->niter;
2249   if (f) *f=tao->fc;
2250   if (gnorm) *gnorm=tao->residual;
2251   if (cnorm) *cnorm=tao->cnorm;
2252   if (reason) *reason=tao->reason;
2253   if (xdiff) *xdiff=tao->step;
2254   PetscFunctionReturn(0);
2255 }
2256 
2257 #undef __FUNCT__
2258 #define __FUNCT__ "TaoGetType"
2259 /*@C
2260    TaoGetType - Gets the current Tao algorithm.
2261 
2262    Not Collective
2263 
2264    Input Parameter:
2265 .  tao - the Tao solver context
2266 
2267    Output Parameter:
2268 .  type - Tao method
2269 
2270    Level: intermediate
2271 
2272 @*/
2273 PetscErrorCode TaoGetType(Tao tao, const TaoType *type)
2274 {
2275   PetscFunctionBegin;
2276   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2277   PetscValidPointer(type,2);
2278   *type=((PetscObject)tao)->type_name;
2279   PetscFunctionReturn(0);
2280 }
2281 
2282 #undef __FUNCT__
2283 #define __FUNCT__ "TaoMonitor"
2284 /*@C
2285   TaoMonitor - Monitor the solver and the current solution.  This
2286   routine will record the iteration number and residual statistics,
2287   call any monitors specified by the user, and calls the convergence-check routine.
2288 
2289    Input Parameters:
2290 +  tao - the Tao context
2291 .  its - the current iterate number (>=0)
2292 .  f - the current objective function value
2293 .  res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality.  This measure will be recorded and
2294           used for some termination tests.
2295 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2296 -  steplength - multiple of the step direction added to the previous iterate.
2297 
2298    Output Parameters:
2299 .  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2300 
2301    Options Database Key:
2302 .  -tao_monitor - Use the default monitor, which prints statistics to standard output
2303 
2304 .seealso TaoGetConvergedReason(), TaoDefaultMonitor(), TaoSetMonitor()
2305 
2306    Level: developer
2307 
2308 @*/
2309 PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength, TaoConvergedReason *reason)
2310 {
2311   PetscErrorCode ierr;
2312   PetscInt       i;
2313 
2314   PetscFunctionBegin;
2315   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2316   tao->fc = f;
2317   tao->residual = res;
2318   tao->cnorm = cnorm;
2319   tao->step = steplength;
2320   tao->niter=its;
2321   if (its == 0) {
2322     tao->cnorm0 = cnorm; tao->gnorm0 = res;
2323   }
2324   TaoLogHistory(tao,f,res,cnorm);
2325   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(res)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
2326   if (tao->ops->convergencetest) {
2327     ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr);
2328   }
2329   for (i=0;i<tao->numbermonitors;i++) {
2330     ierr = (*tao->monitor[i])(tao,tao->monitorcontext[i]);CHKERRQ(ierr);
2331   }
2332   *reason = tao->reason;
2333   PetscFunctionReturn(0);
2334 }
2335 
2336 #undef __FUNCT__
2337 #define __FUNCT__ "TaoSetHistory"
2338 /*@
2339    TaoSetHistory - Sets the array used to hold the convergence history.
2340 
2341    Logically Collective on Tao
2342 
2343    Input Parameters:
2344 +  tao - the Tao solver context
2345 .  obj   - array to hold objective value history
2346 .  resid - array to hold residual history
2347 .  cnorm - array to hold constraint violation history
2348 .  na  - size of obj, resid, and cnorm
2349 -  reset - PetscTrue indicates each new minimization resets the history counter to zero,
2350            else it continues storing new values for new minimizations after the old ones
2351 
2352    Notes:
2353    If set, TAO will fill the given arrays with the indicated
2354    information at each iteration.  If no information is desired
2355    for a given array, then NULL may be used.
2356 
2357    This routine is useful, e.g., when running a code for purposes
2358    of accurate performance monitoring, when no I/O should be done
2359    during the section of code that is being timed.
2360 
2361    Level: intermediate
2362 
2363 .seealso: TaoGetHistory()
2364 
2365 @*/
2366 PetscErrorCode TaoSetHistory(Tao tao, PetscReal *obj, PetscReal *resid, PetscReal *cnorm, PetscInt na,PetscBool reset)
2367 {
2368   PetscFunctionBegin;
2369   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2370   tao->hist_obj = obj;
2371   tao->hist_resid = resid;
2372   tao->hist_cnorm = cnorm;
2373   tao->hist_max   = na;
2374   tao->hist_reset = reset;
2375   PetscFunctionReturn(0);
2376 }
2377 
2378 #undef __FUNCT__
2379 #define __FUNCT__ "TaoGetHistory"
2380 /*@C
2381    TaoGetHistory - Gets the array used to hold the convergence history.
2382 
2383    Collective on Tao
2384 
2385    Input Parameter:
2386 .  tao - the Tao context
2387 
2388    Output Parameters:
2389 +  obj   - array used to hold objective value history
2390 .  resid - array used to hold residual history
2391 .  cnorm - array used to hold constraint violation history
2392 -  nhist  - size of obj, resid, and cnorm (will be less than or equal to na given in TaoSetHistory)
2393 
2394    Notes:
2395     The calling sequence for this routine in Fortran is
2396 $   call TaoGetHistory(Tao tao, integer nhist, integer info)
2397 
2398    This routine is useful, e.g., when running a code for purposes
2399    of accurate performance monitoring, when no I/O should be done
2400    during the section of code that is being timed.
2401 
2402    Level: advanced
2403 
2404 .seealso: TaoSetHistory()
2405 
2406 @*/
2407 PetscErrorCode TaoGetHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt *nhist)
2408 {
2409   PetscFunctionBegin;
2410   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2411   if (obj)   *obj   = tao->hist_obj;
2412   if (cnorm) *cnorm = tao->hist_cnorm;
2413   if (resid) *resid = tao->hist_resid;
2414   if (nhist) *nhist   = tao->hist_len;
2415   PetscFunctionReturn(0);
2416 }
2417 
2418 #undef __FUNCT__
2419 #define __FUNCT__ "TaoSetApplicationContext"
2420 /*@
2421    TaoSetApplicationContext - Sets the optional user-defined context for
2422    a solver.
2423 
2424    Logically Collective on Tao
2425 
2426    Input Parameters:
2427 +  tao  - the Tao context
2428 -  usrP - optional user context
2429 
2430    Level: intermediate
2431 
2432 .seealso: TaoGetApplicationContext(), TaoSetApplicationContext()
2433 @*/
2434 PetscErrorCode  TaoSetApplicationContext(Tao tao,void *usrP)
2435 {
2436   PetscFunctionBegin;
2437   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2438   tao->user = usrP;
2439   PetscFunctionReturn(0);
2440 }
2441 
2442 #undef __FUNCT__
2443 #define __FUNCT__ "TaoGetApplicationContext"
2444 /*@
2445    TaoGetApplicationContext - Gets the user-defined context for a
2446    TAO solvers.
2447 
2448    Not Collective
2449 
2450    Input Parameter:
2451 .  tao  - Tao context
2452 
2453    Output Parameter:
2454 .  usrP - user context
2455 
2456    Level: intermediate
2457 
2458 .seealso: TaoSetApplicationContext()
2459 @*/
2460 PetscErrorCode  TaoGetApplicationContext(Tao tao,void *usrP)
2461 {
2462   PetscFunctionBegin;
2463   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2464   *(void**)usrP = tao->user;
2465   PetscFunctionReturn(0);
2466 }
2467