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