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