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