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