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