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