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