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