xref: /petsc/src/tao/interface/taosolver.c (revision 85f9fe2e472e494f624e07331fb0f6081bef5d80)
1 #define TAOSOLVER_DLL
2 
3 #include "tao-private/taosolver_impl.h" /*I "taosolver.h" I*/
4 
5 PetscBool TaoSolverRegisterAllCalled = PETSC_FALSE;
6 PetscFunctionList TaoSolverList = NULL;
7 
8 PetscClassId TAOSOLVER_CLASSID;
9 PetscLogEvent TaoSolver_Solve, TaoSolver_ObjectiveEval, TaoSolver_GradientEval, TaoSolver_ObjGradientEval, TaoSolver_HessianEval, TaoSolver_ConstraintsEval, TaoSolver_JacobianEval;
10 
11 
12 
13 static const char *TAO_SUBSET[64] = {
14   "subvec","mask","matrixfree"
15 };
16 
17 #undef __FUNCT__
18 #define __FUNCT__ "TaoCreate"
19 /*@
20   TaoCreate - Creates a TAO solver
21 
22   Collective on MPI_Comm
23 
24   Input Parameter:
25 . comm - MPI communicator
26 
27   Output Parameter:
28 . newtao - the new TaoSolver context
29 
30   Available methods include:
31 +    tao_nls - Newton's method with line search for unconstrained minimization
32 .    tao_ntr - Newton's method with trust region for unconstrained minimization
33 .    tao_ntl - Newton's method with trust region, line search for unconstrained minimization
34 .    tao_lmvm - Limited memory variable metric method for unconstrained minimization
35 .    tao_cg - Nonlinear conjugate gradient method for unconstrained minimization
36 .    tao_nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
37 .    tao_tron - Newton Trust Region method for bound constrained minimization
38 .    tao_gpcg - Newton Trust Region method for quadratic bound constrained minimization
39 .    tao_blmvm - Limited memory variable metric method for bound constrained minimization
40 .    tao_lcl - Linearly constrained Lagrangian method for pde-constrained minimization
41 -    tao_pounders - Model-based algorithm for nonlinear least squares
42 
43    Options Database Keys:
44 +   -tao_method - select which method TAO should use
45 -   -tao_type - identical to -tao_method
46 
47    Level: beginner
48 
49 .seealso: TaoSolve(), TaoDestroy()
50 @*/
51 PetscErrorCode TaoCreate(MPI_Comm comm, TaoSolver *newtao)
52 {
53   PetscErrorCode ierr;
54   TaoSolver      tao;
55 
56   PetscFunctionBegin;
57   PetscValidPointer(newtao,2);
58   *newtao = NULL;
59 
60   ierr = TaoInitializePackage();CHKERRQ(ierr);
61   ierr = TaoLineSearchInitializePackage();CHKERRQ(ierr);
62 
63   ierr = PetscHeaderCreate(tao,_p_TaoSolver, struct _TaoSolverOps, TAOSOLVER_CLASSID,"TaoSolver",0,0,comm,TaoDestroy,TaoView);CHKERRQ(ierr);
64   tao->ops->computeobjective=0;
65   tao->ops->computeobjectiveandgradient=0;
66   tao->ops->computegradient=0;
67   tao->ops->computehessian=0;
68   tao->ops->computeseparableobjective=0;
69   tao->ops->computeconstraints=0;
70   tao->ops->computejacobian=0;
71   tao->ops->computejacobianequality=0;
72   tao->ops->computejacobianinequality=0;
73   tao->ops->computeequalityconstraints=0;
74   tao->ops->computeinequalityconstraints=0;
75   tao->ops->convergencetest=TaoDefaultConvergenceTest;
76   tao->ops->convergencedestroy=0;
77   tao->ops->computedual=0;
78   tao->ops->setup=0;
79   tao->ops->solve=0;
80   tao->ops->view=0;
81   tao->ops->setfromoptions=0;
82   tao->ops->destroy=0;
83 
84   tao->solution=NULL;
85   tao->gradient=NULL;
86   tao->sep_objective = NULL;
87   tao->constraints=NULL;
88   tao->constraints_equality=NULL;
89   tao->constraints_inequality=NULL;
90   tao->stepdirection=NULL;
91   tao->XL = NULL;
92   tao->XU = NULL;
93   tao->IL = NULL;
94   tao->IU = NULL;
95   tao->DI = NULL;
96   tao->DE = NULL;
97   tao->hessian = NULL;
98   tao->hessian_pre = NULL;
99   tao->jacobian = NULL;
100   tao->jacobian_pre = NULL;
101   tao->jacobian_state = NULL;
102   tao->jacobian_state_pre = NULL;
103   tao->jacobian_state_inv = NULL;
104   tao->jacobian_design = NULL;
105   tao->jacobian_design_pre = NULL;
106   tao->jacobian_equality = NULL;
107   tao->jacobian_equality_pre = NULL;
108   tao->jacobian_inequality = NULL;
109   tao->jacobian_inequality_pre = NULL;
110   tao->state_is = NULL;
111   tao->design_is = NULL;
112 
113   tao->max_it     = 10000;
114   tao->max_funcs   = 10000;
115   tao->fatol       = 1e-8;
116   tao->frtol       = 1e-8;
117   tao->gatol       = 1e-8;
118   tao->grtol       = 1e-8;
119   tao->gttol       = 0.0;
120   tao->catol       = 0.0;
121   tao->crtol       = 0.0;
122   tao->xtol        = 0.0;
123   tao->steptol       = 0.0;
124   tao->trust0      = TAO_INFINITY;
125   tao->fmin        = -1e100;
126   tao->hist_reset = PETSC_TRUE;
127   tao->hist_max = 0;
128   tao->hist_len = 0;
129   tao->hist_obj = NULL;
130   tao->hist_resid = NULL;
131   tao->hist_cnorm = NULL;
132 
133   tao->numbermonitors=0;
134   tao->viewsolution=PETSC_FALSE;
135   tao->viewhessian=PETSC_FALSE;
136   tao->viewgradient=PETSC_FALSE;
137   tao->viewjacobian=PETSC_FALSE;
138   tao->viewconstraints = PETSC_FALSE;
139   tao->viewtao = PETSC_FALSE;
140 
141   ierr = TaoResetStatistics(tao);CHKERRQ(ierr);
142   *newtao = tao;
143   PetscFunctionReturn(0);
144 }
145 
146 #undef __FUNCT__
147 #define __FUNCT__ "TaoSolve"
148 /*@
149   TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
150 
151   Collective on TaoSolver
152 
153   Input Parameters:
154 . tao - the TaoSolver context
155 
156   Notes:
157   The user must set up the TaoSolver with calls to TaoSetInitialVector(),
158   TaoSetObjectiveRoutine(),
159   TaoSetGradientRoutine(), and (if using 2nd order method) TaoSetHessianRoutine().
160 
161   Level: beginner
162 
163 .seealso: TaoCreate(), TaoSetObjectiveRoutine(), TaoSetGradientRoutine(), TaoSetHessianRoutine()
164  @*/
165 PetscErrorCode TaoSolve(TaoSolver tao)
166 {
167   PetscErrorCode ierr;
168   char           filename[PETSC_MAX_PATH_LEN];
169   PetscBool      flg;
170   PetscViewer    viewer;
171 
172   PetscFunctionBegin;
173   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
174   ierr = TaoSetUp(tao);CHKERRQ(ierr);
175   ierr = TaoResetStatistics(tao);CHKERRQ(ierr);
176   if (tao->linesearch) {
177     ierr = TaoLineSearchReset(tao->linesearch);CHKERRQ(ierr);
178   }
179 
180   ierr = PetscLogEventBegin(TaoSolver_Solve,tao,0,0,0);CHKERRQ(ierr);
181   if (tao->ops->solve){ ierr = (*tao->ops->solve)(tao);CHKERRQ(ierr); }
182   ierr = PetscLogEventEnd(TaoSolver_Solve,tao,0,0,0);CHKERRQ(ierr);
183 
184   ierr = PetscOptionsGetString(((PetscObject)tao)->prefix,"-tao_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
185   if (flg && !PetscPreLoadingOn) {
186     ierr = PetscViewerASCIIOpen(((PetscObject)tao)->comm,filename,&viewer);CHKERRQ(ierr);
187     ierr = TaoView(tao,viewer);CHKERRQ(ierr);
188     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
189   }
190 
191   if (tao->printreason) {
192       if (tao->reason > 0) {
193           ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve converged due to %s\n",TaoSolverTerminationReasons[tao->reason]);CHKERRQ(ierr);
194       } else {
195           ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve did not converge due to %s\n",TaoSolverTerminationReasons[tao->reason]);CHKERRQ(ierr);
196       }
197   }
198   PetscFunctionReturn(0);
199 }
200 
201 
202 #undef __FUNCT__
203 #define __FUNCT__ "TaoSetUp"
204 /*@
205   TaoSetUp - Sets up the internal data structures for the later use
206   of a Tao solver
207 
208   Collective on tao
209 
210   Input Parameters:
211 . tao - the TAO context
212 
213   Notes:
214   The user will not need to explicitly call TaoSetUp(), as it will
215   automatically be called in TaoSolve().  However, if the user
216   desires to call it explicitly, it should come after TaoCreate()
217   and any TaoSetSomething() routines, but before TaoSolve().
218 
219   Level: advanced
220 
221 .seealso: TaoCreate(), TaoSolve()
222 @*/
223 PetscErrorCode TaoSetUp(TaoSolver tao)
224 {
225   PetscErrorCode ierr;
226 
227   PetscFunctionBegin;
228   PetscValidHeaderSpecific(tao, TAOSOLVER_CLASSID,1);
229   if (tao->setupcalled) PetscFunctionReturn(0);
230 
231   if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetInitialVector");
232   if (tao->ops->setup) {
233     ierr = (*tao->ops->setup)(tao);CHKERRQ(ierr);
234   }
235   tao->setupcalled = PETSC_TRUE;
236   PetscFunctionReturn(0);
237 }
238 
239 #undef __FUNCT__
240 #define __FUNCT__ "TaoDestroy"
241 /*@
242   TaoDestroy - Destroys the TAO context that was created with
243   TaoCreate()
244 
245   Collective on TaoSolver
246 
247   Input Parameter:
248 . tao - the TaoSolver context
249 
250   Level: beginner
251 
252 .seealso: TaoCreate(), TaoSolve()
253 @*/
254 PetscErrorCode TaoDestroy(TaoSolver *tao)
255 {
256   PetscErrorCode ierr;
257 
258   PetscFunctionBegin;
259   if (!*tao) PetscFunctionReturn(0);
260   PetscValidHeaderSpecific(*tao,TAOSOLVER_CLASSID,1);
261   if (--((PetscObject)*tao)->refct > 0) {*tao=0;PetscFunctionReturn(0);}
262 
263   if ((*tao)->ops->destroy) {
264     ierr = (*((*tao))->ops->destroy)(*tao);CHKERRQ(ierr);
265   }
266   ierr = KSPDestroy(&(*tao)->ksp);CHKERRQ(ierr);
267   ierr = TaoLineSearchDestroy(&(*tao)->linesearch);CHKERRQ(ierr);
268 
269   if ((*tao)->ops->convergencedestroy) {
270     ierr = (*(*tao)->ops->convergencedestroy)((*tao)->cnvP);CHKERRQ(ierr);
271     if ((*tao)->jacobian_state_inv) {
272       ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr);
273     }
274   }
275   ierr = VecDestroy(&(*tao)->solution);CHKERRQ(ierr);
276   ierr = VecDestroy(&(*tao)->gradient);CHKERRQ(ierr);
277 
278   ierr = VecDestroy(&(*tao)->XL);CHKERRQ(ierr);
279   ierr = VecDestroy(&(*tao)->XU);CHKERRQ(ierr);
280   ierr = VecDestroy(&(*tao)->IL);CHKERRQ(ierr);
281   ierr = VecDestroy(&(*tao)->IU);CHKERRQ(ierr);
282   ierr = VecDestroy(&(*tao)->DE);CHKERRQ(ierr);
283   ierr = VecDestroy(&(*tao)->DI);CHKERRQ(ierr);
284   ierr = VecDestroy(&(*tao)->constraints_equality);CHKERRQ(ierr);
285   ierr = VecDestroy(&(*tao)->constraints_inequality);CHKERRQ(ierr);
286   ierr = VecDestroy(&(*tao)->stepdirection);CHKERRQ(ierr);
287   ierr = MatDestroy(&(*tao)->hessian_pre);CHKERRQ(ierr);
288   ierr = MatDestroy(&(*tao)->hessian);CHKERRQ(ierr);
289   ierr = MatDestroy(&(*tao)->jacobian_pre);CHKERRQ(ierr);
290   ierr = MatDestroy(&(*tao)->jacobian);CHKERRQ(ierr);
291   ierr = MatDestroy(&(*tao)->jacobian_state_pre);CHKERRQ(ierr);
292   ierr = MatDestroy(&(*tao)->jacobian_state);CHKERRQ(ierr);
293   ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr);
294   ierr = MatDestroy(&(*tao)->jacobian_design);CHKERRQ(ierr);
295   ierr = MatDestroy(&(*tao)->jacobian_equality);CHKERRQ(ierr);
296   ierr = MatDestroy(&(*tao)->jacobian_equality_pre);CHKERRQ(ierr);
297   ierr = MatDestroy(&(*tao)->jacobian_inequality);CHKERRQ(ierr);
298   ierr = MatDestroy(&(*tao)->jacobian_inequality_pre);CHKERRQ(ierr);
299   ierr = ISDestroy(&(*tao)->state_is);CHKERRQ(ierr);
300   ierr = ISDestroy(&(*tao)->design_is);CHKERRQ(ierr);
301   ierr = TaoCancelMonitors(*tao);CHKERRQ(ierr);
302   ierr = PetscHeaderDestroy(tao);CHKERRQ(ierr);
303   PetscFunctionReturn(0);
304 }
305 
306 #undef __FUNCT__
307 #define __FUNCT__ "TaoSetFromOptions"
308 /*@
309   TaoSetFromOptions - Sets various TaoSolver parameters from user
310   options.
311 
312   Collective on TaoSolver
313 
314   Input Paremeter:
315 . tao - the TaoSolver solver context
316 
317   options Database Keys:
318 + -tao_method <type> - The algorithm that TAO uses (tao_lmvm, tao_nls, etc.)
319 . -tao_fatol <fatol> - absolute error tolerance in function value
320 . -tao_frtol <frtol> - relative error tolerance in function value
321 . -tao_gatol <gatol> - absolute error tolerance for ||gradient||
322 . -tao_grtol <grtol> - relative error tolerance for ||gradient||
323 . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient
324 . -tao_max_it <max> - sets maximum number of iterations
325 . -tao_max_funcs <max> - sets maximum number of function evaluations
326 . -tao_fmin <fmin> - stop if function value reaches fmin
327 . -tao_steptol <tol> - stop if trust region radius less than <tol>
328 . -tao_trust0 <t> - initial trust region radius
329 . -tao_monitor - prints function value and residual at each iteration
330 . -tao_smonitor - same as tao_monitor, but truncates very small values
331 . -tao_cmonitor - prints function value, residual, and constraint norm at each iteration
332 . -tao_view_solution - prints solution vector at each iteration
333 . -tao_view_separableobjective - prints separable objective vector at each iteration
334 . -tao_view_step - prints step direction vector at each iteration
335 . -tao_view_gradient - prints gradient vector at each iteration
336 . -tao_draw_solution - graphically view solution vector at each iteration
337 . -tao_draw_step - graphically view step vector at each iteration
338 . -tao_draw_gradient - graphically view gradient at each iteration
339 . -tao_fd_gradient - use gradient computed with finite differences
340 . -tao_cancelmonitors - cancels all monitors (except those set with command line)
341 . -tao_view - prints information about the TaoSolver after solving
342 - -tao_converged_reason - prints the reason TAO stopped iterating
343 
344   Notes:
345   To see all options, run your program with the -help option or consult the
346   user's manual. Should be called after TaoCreate() but before TaoSolve()
347 
348   Level: beginner
349 @*/
350 PetscErrorCode TaoSetFromOptions(TaoSolver tao)
351 {
352   PetscErrorCode      ierr;
353   const TaoSolverType default_type = "tao_lmvm";
354   const char          *prefix;
355   char                type[256], monfilename[PETSC_MAX_PATH_LEN];
356   PetscViewer         monviewer;
357   PetscBool           flg;
358   MPI_Comm            comm;
359 
360   PetscFunctionBegin;
361   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
362   ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
363   ierr = TaoGetOptionsPrefix(tao,&prefix);
364   /* So no warnings are given about unused options */
365   ierr = PetscOptionsHasName(prefix,"-tao_ksp_type",&flg);
366   ierr = PetscOptionsHasName(prefix,"-tao_pc_type",&flg);
367   ierr = PetscOptionsHasName(prefix,"-tao_ls_type",&flg);
368 
369   ierr = PetscObjectOptionsBegin((PetscObject)tao);CHKERRQ(ierr);
370   {
371     if (!TaoSolverRegisterAllCalled) {
372       ierr = TaoSolverRegisterAll();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",TaoSolverList,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",TaoSolverList,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 TaoSolver 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",
399                            "TaoSetMaximumIterations",tao->max_it,&tao->max_it,
400                            &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 = PetscOptionsEList("-tao_subset_type","subset type", "", TAO_SUBSET, TAO_SUBSET_TYPES,TAO_SUBSET[tao->subset_type], &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 TaoSolver
488 
489   Collective on TaoSolver
490 
491   InputParameters:
492 + tao - the TaoSolver 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(TaoSolver tao, PetscViewer viewer)
511 {
512   PetscErrorCode      ierr;
513   PetscBool           isascii,isstring;
514   const TaoSolverType type;
515 
516   PetscFunctionBegin;
517   PetscValidHeaderSpecific(tao,TAOSOLVER_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",TAO_SUBSET[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 TaoSolver
672 
673   Input Parameters:
674 + tao - the TaoSolver 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(TaoSolver tao, PetscReal fatol, PetscReal frtol, PetscReal gatol, PetscReal grtol, PetscReal gttol)
704 {
705   PetscErrorCode ierr;
706 
707   PetscFunctionBegin;
708   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
758 
759   Input Parameters:
760 + tao - the TaoSolver 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(TaoSolver tao, PetscReal catol, PetscReal crtol)
774 {
775   PetscErrorCode ierr;
776 
777   PetscFunctionBegin;
778   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
806 
807    Input Parameters:
808 +  tao - the TaoSolver 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(TaoSolver tao,PetscReal fmin)
819 {
820   PetscFunctionBegin;
821   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
834 
835    Input Parameters:
836 .  tao - the TaoSolver solver context
837 
838    OutputParameters:
839 .  fmin - the minimum function value
840 
841    Level: intermediate
842 
843 .seealso: TaoSetFunctionLowerBound()
844 @*/
845 PetscErrorCode TaoGetFunctionLowerBound(TaoSolver tao,PetscReal *fmin)
846 {
847   PetscFunctionBegin;
848   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
860 
861    Input Parameters:
862 +  tao - the TaoSolver 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(TaoSolver tao,PetscInt nfcn)
874 {
875   PetscFunctionBegin;
876   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver 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(TaoSolver tao,PetscInt *nfcn)
901 {
902   PetscFunctionBegin;
903   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
914 
915    Input Parameters:
916 +  tao - the TaoSolver 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(TaoSolver tao,PetscInt maxits)
927 {
928   PetscFunctionBegin;
929   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver 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(TaoSolver tao,PetscInt *maxits)
952 {
953   PetscFunctionBegin;
954   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver
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(TaoSolver tao, PetscReal radius)
978 {
979   PetscFunctionBegin;
980   PetscValidHeaderSpecific(tao,TAOSOLVER_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(TaoSolver tao, PetscReal *radius)
1003 {
1004   PetscFunctionBegin;
1005   PetscValidHeaderSpecific(tao,TAOSOLVER_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(TaoSolver tao, PetscReal *radius)
1028 {
1029   PetscFunctionBegin;
1030   PetscValidHeaderSpecific(tao,TAOSOLVER_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 TaoSolver 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(TaoSolver tao, PetscReal *fatol, PetscReal *frtol, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1059 {
1060   PetscFunctionBegin;
1061   PetscValidHeaderSpecific(tao,TAOSOLVER_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 
1071 #undef __FUNCT__
1072 #define __FUNCT__ "TaoGetKSP"
1073 /*@
1074   TaoGetKSP - Gets the linear solver used by the optimization solver.
1075   Application writers should use TaoGetKSP if they need direct access
1076   to the PETSc KSP object.
1077 
1078   Not Collective
1079 
1080    Input Parameters:
1081 .  tao - the TAO solver
1082 
1083    Output Parameters:
1084 .  ksp - the KSP linear solver used in the optimization solver
1085 
1086    Level: intermediate
1087 
1088 @*/
1089 PetscErrorCode TaoGetKSP(TaoSolver tao, KSP *ksp)
1090 {
1091   PetscFunctionBegin;
1092   *ksp = tao->ksp;
1093   PetscFunctionReturn(0);
1094 }
1095 
1096 #undef __FUNCT__
1097 #define __FUNCT__ "TaoGetLineSearch"
1098 /*@
1099   TaoGetLineSearch - Gets the line search used by the optimization solver.
1100   Application writers should use TaoGetLineSearch if they need direct access
1101   to the TaoLineSearch object.
1102 
1103   Not Collective
1104 
1105    Input Parameters:
1106 .  tao - the TAO solver
1107 
1108    Output Parameters:
1109 .  ls - the line search used in the optimization solver
1110 
1111    Level: intermediate
1112 
1113 @*/
1114 PetscErrorCode TaoGetLineSearch(TaoSolver tao, TaoLineSearch *ls)
1115 {
1116   PetscFunctionBegin;
1117   *ls = tao->linesearch;
1118   PetscFunctionReturn(0);
1119 }
1120 
1121 #undef __FUNCT__
1122 #define __FUNCT__ "TaoAddLineSearchCounts"
1123 /*@
1124   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1125   in the line search to the running total.
1126 
1127    Input Parameters:
1128 +  tao - the TAO solver
1129 -  ls - the line search used in the optimization solver
1130 
1131    Level: developer
1132 
1133 .seealso: TaoLineSearchApply()
1134 @*/
1135 PetscErrorCode TaoAddLineSearchCounts(TaoSolver tao)
1136 {
1137   PetscErrorCode ierr;
1138   PetscBool      flg;
1139   PetscInt       nfeval,ngeval,nfgeval;
1140 
1141   PetscFunctionBegin;
1142   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1143   if (tao->linesearch) {
1144     ierr = TaoLineSearchIsUsingTaoSolverRoutines(tao->linesearch,&flg);
1145     if (flg == PETSC_FALSE) {
1146       ierr = TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch,&nfeval,&ngeval,&nfgeval);CHKERRQ(ierr);
1147       tao->nfuncs+=nfeval;
1148       tao->ngrads+=ngeval;
1149       tao->nfuncgrads+=nfgeval;
1150     }
1151   }
1152   PetscFunctionReturn(0);
1153 }
1154 
1155 
1156 #undef __FUNCT__
1157 #define __FUNCT__ "TaoGetSolutionVector"
1158 /*@
1159   TaoGetSolutionVector - Returns the vector with the current TAO solution
1160 
1161   Not Collective
1162 
1163   Input Parameter:
1164 . tao - the TaoSolver context
1165 
1166   Output Parameter:
1167 . X - the current solution
1168 
1169   Level: intermediate
1170 
1171   Note:  The returned vector will be the same object that was passed into TaoSetInitialVector()
1172 @*/
1173 PetscErrorCode TaoGetSolutionVector(TaoSolver tao, Vec *X)
1174 {
1175   PetscFunctionBegin;
1176   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1177   *X = tao->solution;
1178   PetscFunctionReturn(0);
1179 }
1180 
1181 #undef __FUNCT__
1182 #define __FUNCT__ "TaoGetGradientVector"
1183 /*@
1184   TaoGetGradientVector - Returns the vector with the current TAO gradient
1185 
1186   Not Collective
1187 
1188   Input Parameter:
1189 . tao - the TaoSolver context
1190 
1191   Output Parameter:
1192 . G - the current solution
1193 
1194   Level: intermediate
1195 @*/
1196 PetscErrorCode TaoGetGradientVector(TaoSolver tao, Vec *G)
1197 {
1198   PetscFunctionBegin;
1199   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1200   *G = tao->gradient;
1201   PetscFunctionReturn(0);
1202 }
1203 
1204 #undef __FUNCT__
1205 #define __FUNCT__ "TaoResetStatistics"
1206 /*@
1207    TaoResetStatistics - Initialize the statistics used by TAO for all of the solvers.
1208    These statistics include the iteration number, residual norms, and convergence status.
1209    This routine gets called before solving each optimization problem.
1210 
1211    Collective on TaoSolver
1212 
1213    Input Parameters:
1214 .  solver - the TaoSolver context
1215 
1216    Level: developer
1217 
1218 .seealso: TaoCreate(), TaoSolve()
1219 @*/
1220 PetscErrorCode TaoResetStatistics(TaoSolver tao)
1221 {
1222   PetscFunctionBegin;
1223   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1224   tao->niter        = 0;
1225   tao->nfuncs       = 0;
1226   tao->nfuncgrads   = 0;
1227   tao->ngrads       = 0;
1228   tao->nhess        = 0;
1229   tao->njac         = 0;
1230   tao->nconstraints = 0;
1231   tao->ksp_its      = 0;
1232   tao->reason       = TAO_CONTINUE_ITERATING;
1233   tao->residual     = 0.0;
1234   tao->cnorm        = 0.0;
1235   tao->step         = 0.0;
1236   tao->lsflag       = PETSC_FALSE;
1237   if (tao->hist_reset) tao->hist_len=0;
1238   PetscFunctionReturn(0);
1239 }
1240 
1241 #undef __FUNCT__
1242 #define __FUNCT__ "TaoSetConvergenceTest"
1243 /*@C
1244   TaoSetConvergenceTest - Sets the function that is to be used to test
1245   for convergence o fthe iterative minimization solution.  The new convergence
1246   testing routine will replace TAO's default convergence test.
1247 
1248   Logically Collective on TaoSolver
1249 
1250   Input Parameters:
1251 + tao - the TaoSolver object
1252 . conv - the routine to test for convergence
1253 - ctx - [optional] context for private data for the convergence routine
1254         (may be NULL)
1255 
1256   Calling sequence of conv:
1257 $   PetscErrorCode conv(TaoSolver tao, void *ctx)
1258 
1259 + tao - the TaoSolver object
1260 - ctx - [optional] convergence context
1261 
1262   Note: The new convergence testing routine should call TaoSetTerminationReason().
1263 
1264   Level: advanced
1265 
1266 .seealso: TaoSetTerminationReason(), TaoGetSolutionStatus(), TaoGetTolerances(), TaoSetMonitor
1267 
1268 @*/
1269 PetscErrorCode TaoSetConvergenceTest(TaoSolver tao, PetscErrorCode (*conv)(TaoSolver,void*), void *ctx)
1270 {
1271   PetscFunctionBegin;
1272   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1273   (tao)->ops->convergencetest = conv;
1274   (tao)->cnvP = ctx;
1275   PetscFunctionReturn(0);
1276 }
1277 
1278 #undef __FUNCT__
1279 #define __FUNCT__ "TaoSetMonitor"
1280 /*@C
1281    TaoSetMonitor - Sets an ADDITIONAL function that is to be used at every
1282    iteration of the solver to display the iteration's
1283    progress.
1284 
1285    Logically Collective on TaoSolver
1286 
1287    Input Parameters:
1288 +  tao - the TaoSolver solver context
1289 .  mymonitor - monitoring routine
1290 -  mctx - [optional] user-defined context for private data for the
1291           monitor routine (may be NULL)
1292 
1293    Calling sequence of mymonitor:
1294 $     int mymonitor(TaoSolver tao,void *mctx)
1295 
1296 +    tao - the TaoSolver solver context
1297 -    mctx - [optional] monitoring context
1298 
1299 
1300    Options Database Keys:
1301 +    -tao_monitor        - sets TaoDefaultMonitor()
1302 .    -tao_smonitor       - sets short monitor
1303 .    -tao_cmonitor       - same as smonitor plus constraint norm
1304 .    -tao_view_solution   - view solution at each iteration
1305 .    -tao_view_gradient   - view gradient at each iteration
1306 .    -tao_view_separableobjective - view separable objective function at each iteration
1307 -    -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.
1308 
1309 
1310    Notes:
1311    Several different monitoring routines may be set by calling
1312    TaoSetMonitor() multiple times; all will be called in the
1313    order in which they were set.
1314 
1315    Fortran Notes: Only one monitor function may be set
1316 
1317    Level: intermediate
1318 
1319 .seealso: TaoDefaultMonitor(), TaoCancelMonitors(),  TaoSetDestroyRoutine()
1320 @*/
1321 PetscErrorCode TaoSetMonitor(TaoSolver tao, PetscErrorCode (*func)(TaoSolver, void*), void *ctx,PetscErrorCode (*dest)(void**))
1322 {
1323   PetscFunctionBegin;
1324   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1325   if (tao->numbermonitors >= MAXTAOMONITORS) SETERRQ1(PETSC_COMM_SELF,1,"Cannot attach another monitor -- max=",MAXTAOMONITORS);
1326   tao->monitor[tao->numbermonitors] = func;
1327   tao->monitorcontext[tao->numbermonitors] = ctx;
1328   tao->monitordestroy[tao->numbermonitors] = dest;
1329   ++tao->numbermonitors;
1330   PetscFunctionReturn(0);
1331 }
1332 
1333 #undef __FUNCT__
1334 #define __FUNCT__ "TaoCancelMonitors"
1335 /*@
1336    TaoCancelMonitors - Clears all the monitor functions for a TaoSolver object.
1337 
1338    Logically Collective on TaoSolver
1339 
1340    Input Parameters:
1341 .  tao - the TaoSolver solver context
1342 
1343    Options Database:
1344 .  -tao_cancelmonitors - cancels all monitors that have been hardwired
1345     into a code by calls to TaoSetMonitor(), but does not cancel those
1346     set via the options database
1347 
1348    Notes:
1349    There is no way to clear one specific monitor from a TaoSolver object.
1350 
1351    Level: advanced
1352 
1353 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1354 @*/
1355 PetscErrorCode TaoCancelMonitors(TaoSolver tao)
1356 {
1357   PetscInt       i;
1358   PetscErrorCode ierr;
1359 
1360   PetscFunctionBegin;
1361   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1362   for (i=0;i<tao->numbermonitors;i++) {
1363     if (tao->monitordestroy[i]) {
1364       ierr = (*tao->monitordestroy[i])(&tao->monitorcontext[i]);CHKERRQ(ierr);
1365     }
1366   }
1367   tao->numbermonitors=0;
1368   PetscFunctionReturn(0);
1369 }
1370 
1371 #undef __FUNCT__
1372 #define __FUNCT__ "TaoDefaultMonitor"
1373 /*@C
1374    TaoDefaultMonitor - Default routine for monitoring progress of the
1375    TaoSolver solvers (default).  This monitor prints the function value and gradient
1376    norm at each iteration.  It can be turned on from the command line using the
1377    -tao_monitor option
1378 
1379    Collective on TaoSolver
1380 
1381    Input Parameters:
1382 +  tao - the TaoSolver context
1383 -  ctx - PetscViewer context or NULL
1384 
1385    Options Database Keys:
1386 .  -tao_monitor
1387 
1388    Level: advanced
1389 
1390 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1391 @*/
1392 PetscErrorCode TaoDefaultMonitor(TaoSolver tao, void *ctx)
1393 {
1394   PetscErrorCode ierr;
1395   PetscInt       its;
1396   PetscReal      fct,gnorm;
1397   PetscViewer    viewer;
1398 
1399   PetscFunctionBegin;
1400   if (ctx) {
1401     viewer = (PetscViewer)ctx;
1402   } else {
1403     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1404   }
1405   its=tao->niter;
1406   fct=tao->fc;
1407   gnorm=tao->residual;
1408   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1409   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1410   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1411   PetscFunctionReturn(0);
1412 }
1413 
1414 #undef __FUNCT__
1415 #define __FUNCT__ "TaoDefaultSMonitor"
1416 /*@C
1417    TaoDefaultSMonitor - Default routine for monitoring progress of the
1418    solver. Same as TaoDefaultMonitor() except
1419    it prints fewer digits of the residual as the residual gets smaller.
1420    This is because the later digits are meaningless and are often
1421    different on different machines; by using this routine different
1422    machines will usually generate the same output. It can be turned on
1423    by using the -tao_smonitor option
1424 
1425    Collective on TaoSolver
1426 
1427    Input Parameters:
1428 +  tao - the TaoSolver context
1429 -  ctx - PetscViewer context or NULL
1430 
1431    Options Database Keys:
1432 .  -tao_smonitor
1433 
1434    Level: advanced
1435 
1436 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1437 @*/
1438 PetscErrorCode TaoDefaultSMonitor(TaoSolver tao, void *ctx)
1439 {
1440   PetscErrorCode ierr;
1441   PetscInt       its;
1442   PetscReal      fct,gnorm;
1443   PetscViewer    viewer;
1444 
1445   PetscFunctionBegin;
1446   if (ctx) {
1447     viewer = (PetscViewer)ctx;
1448   } else {
1449     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1450   }
1451   its=tao->niter;
1452   fct=tao->fc;
1453   gnorm=tao->residual;
1454   ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr);
1455   ierr=PetscViewerASCIIPrintf(viewer," Function value %g,",(double)fct);CHKERRQ(ierr);
1456   if (gnorm > 1.e-6) {
1457     ierr=PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm);CHKERRQ(ierr);
1458   } else if (gnorm > 1.e-11) {
1459     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-6 \n");CHKERRQ(ierr);
1460   } else {
1461     ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-11 \n");CHKERRQ(ierr);
1462   }
1463   PetscFunctionReturn(0);
1464 }
1465 
1466 #undef __FUNCT__
1467 #define __FUNCT__ "TaoDefaultCMonitor"
1468 /*@C
1469    TaoDefaultCMonitor - same as TaoDefaultMonitor() except
1470    it prints the norm of the constraints function. It can be turned on
1471    from the command line using the -tao_cmonitor option
1472 
1473    Collective on TaoSolver
1474 
1475    Input Parameters:
1476 +  tao - the TaoSolver context
1477 -  ctx - PetscViewer context or NULL
1478 
1479    Options Database Keys:
1480 .  -tao_cmonitor
1481 
1482    Level: advanced
1483 
1484 .seealso: TaoDefaultMonitor(), TaoSetMonitor()
1485 @*/
1486 PetscErrorCode TaoDefaultCMonitor(TaoSolver tao, void *ctx)
1487 {
1488   PetscErrorCode ierr;
1489   PetscInt       its;
1490   PetscReal      fct,gnorm;
1491   PetscViewer    viewer;
1492 
1493   PetscFunctionBegin;
1494   if (ctx) {
1495     viewer = (PetscViewer)ctx;
1496   } else {
1497     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1498   }
1499   its=tao->niter;
1500   fct=tao->fc;
1501   gnorm=tao->residual;
1502   ierr=PetscViewerASCIIPrintf(viewer,"iter = %D,",its);CHKERRQ(ierr);
1503   ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr);
1504   ierr=PetscViewerASCIIPrintf(viewer,"  Residual: %g ",(double)gnorm);CHKERRQ(ierr);
1505   ierr = PetscViewerASCIIPrintf(viewer,"  Constraint: %g \n",(double)tao->cnorm);CHKERRQ(ierr);
1506   PetscFunctionReturn(0);
1507 }
1508 
1509 #undef __FUNCT__
1510 #define __FUNCT__ "TaoSolutionMonitor"
1511 /*@C
1512    TaoSolutionMonitor - Views the solution at each iteration
1513    It can be turned on from the command line using the
1514    -tao_view_solution option
1515 
1516    Collective on TaoSolver
1517 
1518    Input Parameters:
1519 +  tao - the TaoSolver context
1520 -  ctx - PetscViewer context or NULL
1521 
1522    Options Database Keys:
1523 .  -tao_view_solution
1524 
1525    Level: advanced
1526 
1527 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1528 @*/
1529 PetscErrorCode TaoSolutionMonitor(TaoSolver tao, void *ctx)
1530 {
1531   PetscErrorCode ierr;
1532   PetscViewer viewer;
1533 
1534   PetscFunctionBegin;
1535   if (ctx) {
1536     viewer = (PetscViewer)ctx;
1537   } else {
1538     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1539   }
1540   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1541   PetscFunctionReturn(0);
1542 }
1543 
1544 #undef __FUNCT__
1545 #define __FUNCT__ "TaoGradientMonitor"
1546 /*@C
1547    TaoGradientMonitor - Views the gradient at each iteration
1548    It can be turned on from the command line using the
1549    -tao_view_gradient option
1550 
1551    Collective on TaoSolver
1552 
1553    Input Parameters:
1554 +  tao - the TaoSolver context
1555 -  ctx - PetscViewer context or NULL
1556 
1557    Options Database Keys:
1558 .  -tao_view_gradient
1559 
1560    Level: advanced
1561 
1562 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1563 @*/
1564 PetscErrorCode TaoGradientMonitor(TaoSolver tao, void *ctx)
1565 {
1566   PetscErrorCode ierr;
1567   PetscViewer viewer;
1568 
1569   PetscFunctionBegin;
1570   if (ctx) {
1571     viewer = (PetscViewer)ctx;
1572   } else {
1573     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1574   }
1575   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1576   PetscFunctionReturn(0);
1577 }
1578 
1579 #undef __FUNCT__
1580 #define __FUNCT__ "TaoStepDirectionMonitor"
1581 /*@C
1582    TaoStepDirectionMonitor - Views the gradient at each iteration
1583    It can be turned on from the command line using the
1584    -tao_view_gradient option
1585 
1586    Collective on TaoSolver
1587 
1588    Input Parameters:
1589 +  tao - the TaoSolver context
1590 -  ctx - PetscViewer context or NULL
1591 
1592    Options Database Keys:
1593 .  -tao_view_gradient
1594 
1595    Level: advanced
1596 
1597 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1598 @*/
1599 PetscErrorCode TaoStepDirectionMonitor(TaoSolver tao, void *ctx)
1600 {
1601   PetscErrorCode ierr;
1602   PetscViewer viewer;
1603   PetscFunctionBegin;
1604   if (ctx) {
1605     viewer = (PetscViewer)ctx;
1606   } else {
1607     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1608   }
1609   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1610   PetscFunctionReturn(0);
1611 }
1612 
1613 #undef __FUNCT__
1614 #define __FUNCT__ "TaoDrawSolutionMonitor"
1615 /*@C
1616    TaoDrawSolutionMonitor - Plots the solution at each iteration
1617    It can be turned on from the command line using the
1618    -tao_draw_solution option
1619 
1620    Collective on TaoSolver
1621 
1622    Input Parameters:
1623 +  tao - the TaoSolver context
1624 -  ctx - PetscViewer context or NULL
1625 
1626    Options Database Keys:
1627 .  -tao_draw_solution
1628 
1629    Level: advanced
1630 
1631 .seealso: TaoSolutionMonitor(), TaoSetMonitor(), TaoDrawGradientMonitor
1632 @*/
1633 PetscErrorCode TaoDrawSolutionMonitor(TaoSolver tao, void *ctx)
1634 {
1635   PetscErrorCode ierr;
1636   PetscViewer    viewer = (PetscViewer) ctx;
1637   MPI_Comm       comm;
1638 
1639   PetscFunctionBegin;
1640   if (!viewer) {
1641     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1642     viewer = PETSC_VIEWER_DRAW_(comm);
1643   }
1644   ierr = VecView(tao->solution, viewer);CHKERRQ(ierr);
1645   PetscFunctionReturn(0);
1646 }
1647 
1648 #undef __FUNCT__
1649 #define __FUNCT__ "TaoDrawGradientMonitor"
1650 /*@C
1651    TaoDrawGradientMonitor - Plots the gradient at each iteration
1652    It can be turned on from the command line using the
1653    -tao_draw_gradient option
1654 
1655    Collective on TaoSolver
1656 
1657    Input Parameters:
1658 +  tao - the TaoSolver context
1659 -  ctx - PetscViewer context or NULL
1660 
1661    Options Database Keys:
1662 .  -tao_draw_gradient
1663 
1664    Level: advanced
1665 
1666 .seealso: TaoGradientMonitor(), TaoSetMonitor(), TaoDrawSolutionMonitor
1667 @*/
1668 PetscErrorCode TaoDrawGradientMonitor(TaoSolver tao, void *ctx)
1669 {
1670   PetscErrorCode ierr;
1671   PetscViewer    viewer = (PetscViewer)ctx;
1672   MPI_Comm       comm;
1673 
1674   PetscFunctionBegin;
1675   if (!viewer) {
1676     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1677     viewer = PETSC_VIEWER_DRAW_(comm);
1678   }
1679   ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr);
1680   PetscFunctionReturn(0);
1681 }
1682 
1683 #undef __FUNCT__
1684 #define __FUNCT__ "TaoDrawStepMonitor"
1685 /*@C
1686    TaoDrawStepMonitor - Plots the step direction at each iteration
1687    It can be turned on from the command line using the
1688    -tao_draw_step option
1689 
1690    Collective on TaoSolver
1691 
1692    Input Parameters:
1693 +  tao - the TaoSolver context
1694 -  ctx - PetscViewer context or NULL
1695 
1696    Options Database Keys:
1697 .  -tao_draw_step
1698 
1699    Level: advanced
1700 
1701 .seealso: TaoSetMonitor(), TaoDrawSolutionMonitor
1702 @*/
1703 PetscErrorCode TaoDrawStepMonitor(TaoSolver tao, void *ctx)
1704 {
1705   PetscErrorCode ierr;
1706   PetscViewer    viewer = (PetscViewer)(ctx);
1707   MPI_Comm       comm;
1708 
1709   PetscFunctionBegin;
1710   if (!viewer) {
1711     ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr);
1712     viewer = PETSC_VIEWER_DRAW_(comm);
1713   }
1714   ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr);
1715   PetscFunctionReturn(0);
1716 }
1717 
1718 #undef __FUNCT__
1719 #define __FUNCT__ "TaoSeparableObjectiveMonitor"
1720 /*@C
1721    TaoSeparableObjectiveMonitor - Views the separable objective function at each iteration
1722    It can be turned on from the command line using the
1723    -tao_view_separableobjective option
1724 
1725    Collective on TaoSolver
1726 
1727    Input Parameters:
1728 +  tao - the TaoSolver context
1729 -  ctx - PetscViewer context or NULL
1730 
1731    Options Database Keys:
1732 .  -tao_view_separableobjective
1733 
1734    Level: advanced
1735 
1736 .seealso: TaoDefaultSMonitor(), TaoSetMonitor()
1737 @*/
1738 PetscErrorCode TaoSeparableObjectiveMonitor(TaoSolver tao, void *ctx)
1739 {
1740   PetscErrorCode ierr;
1741   PetscViewer    viewer;
1742 
1743   PetscFunctionBegin;
1744   if (ctx) {
1745     viewer = (PetscViewer)ctx;
1746   } else {
1747     viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
1748   }
1749   ierr = VecView(tao->sep_objective,viewer);CHKERRQ(ierr);
1750   PetscFunctionReturn(0);
1751 }
1752 
1753 #undef __FUNCT__
1754 #define __FUNCT__ "TaoDefaultConvergenceTest"
1755 /*@C
1756    TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1757    or terminate.
1758 
1759    Collective on TaoSolver
1760 
1761    Input Parameters:
1762 +  tao - the TaoSolver context
1763 -  dummy - unused dummy context
1764 
1765    Output Parameter:
1766 .  reason - for terminating
1767 
1768    Notes:
1769    This routine checks the residual in the optimality conditions, the
1770    relative residual in the optimity conditions, the number of function
1771    evaluations, and the function value to test convergence.  Some
1772    solvers may use different convergence routines.
1773 
1774    Level: developer
1775 
1776 .seealso: TaoSetTolerances(),TaoGetTerminationReason(),TaoSetTerminationReason()
1777 @*/
1778 
1779 PetscErrorCode TaoDefaultConvergenceTest(TaoSolver tao,void *dummy)
1780 {
1781   PetscInt                   niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads);
1782   PetscInt                   max_funcs=tao->max_funcs;
1783   PetscReal                  gnorm=tao->residual, gnorm0=tao->gnorm0;
1784   PetscReal                  f=tao->fc, steptol=tao->steptol,trradius=tao->step;
1785   PetscReal                  gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol;
1786   PetscReal                  fatol=tao->fatol,frtol=tao->frtol,catol=tao->catol,crtol=tao->crtol;
1787   PetscReal                  fmin=tao->fmin, cnorm=tao->cnorm, cnorm0=tao->cnorm0;
1788   PetscReal                  gnorm2;
1789   TaoSolverTerminationReason reason=tao->reason;
1790   PetscErrorCode             ierr;
1791 
1792   PetscFunctionBegin;
1793   PetscValidHeaderSpecific(tao, TAOSOLVER_CLASSID,1);
1794   if (reason != TAO_CONTINUE_ITERATING) {
1795     PetscFunctionReturn(0);
1796   }
1797   gnorm2=gnorm*gnorm;
1798 
1799   if (PetscIsInfOrNanReal(f)) {
1800     ierr = PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n");CHKERRQ(ierr);
1801     reason = TAO_DIVERGED_NAN;
1802   } else if (f <= fmin && cnorm <=catol) {
1803     ierr = PetscInfo2(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin);CHKERRQ(ierr);
1804     reason = TAO_CONVERGED_MINF;
1805   } else if (gnorm2 <= fatol && cnorm <=catol) {
1806     ierr = PetscInfo2(tao,"Converged due to estimated f(X) - f(X*) = %g < %g\n",(double)gnorm2,(double)fatol);CHKERRQ(ierr);
1807     reason = TAO_CONVERGED_FATOL;
1808   } else if (f != 0 && gnorm2 / PetscAbsReal(f)<= frtol && cnorm/PetscMax(cnorm0,1.0) <= crtol) {
1809     ierr = PetscInfo2(tao,"Converged due to estimated |f(X)-f(X*)|/f(X) = %g < %g\n",(double)(gnorm2/PetscAbsReal(f)),(double)frtol);CHKERRQ(ierr);
1810     reason = TAO_CONVERGED_FRTOL;
1811   } else if (gnorm<= gatol && cnorm <=catol) {
1812     ierr = PetscInfo2(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol);CHKERRQ(ierr);
1813     reason = TAO_CONVERGED_GATOL;
1814   } else if ( f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) {
1815     ierr = PetscInfo2(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol);CHKERRQ(ierr);
1816     reason = TAO_CONVERGED_GRTOL;
1817   } else if (gnorm0 != 0 && gnorm/gnorm0 <= gttol && cnorm <= crtol) {
1818     ierr = PetscInfo2(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol);CHKERRQ(ierr);
1819     reason = TAO_CONVERGED_GTTOL;
1820   } else if (nfuncs > max_funcs){
1821     ierr = PetscInfo2(tao,"Exceeded maximum number of function evaluations: %D > %D\n", nfuncs,max_funcs);CHKERRQ(ierr);
1822     reason = TAO_DIVERGED_MAXFCN;
1823   } else if ( tao->lsflag != 0 ){
1824     ierr = PetscInfo(tao,"Tao Line Search failure.\n");CHKERRQ(ierr);
1825     reason = TAO_DIVERGED_LS_FAILURE;
1826   } else if (trradius < steptol && niter > 0){
1827     ierr = PetscInfo2(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol);CHKERRQ(ierr);
1828     reason = TAO_CONVERGED_STEPTOL;
1829   } else if (niter > tao->max_it) {
1830     ierr = PetscInfo2(tao,"Exceeded maximum number of iterations: %D > %D\n",niter,tao->max_it);CHKERRQ(ierr);
1831     reason = TAO_DIVERGED_MAXITS;
1832   } else {
1833     reason = TAO_CONTINUE_ITERATING;
1834   }
1835   tao->reason = reason;
1836   PetscFunctionReturn(0);
1837 }
1838 
1839 #undef __FUNCT__
1840 #define __FUNCT__ "TaoSetOptionsPrefix"
1841 /*@C
1842    TaoSetOptionsPrefix - Sets the prefix used for searching for all
1843    TAO options in the database.
1844 
1845 
1846    Logically Collective on TaoSolver
1847 
1848    Input Parameters:
1849 +  tao - the TaoSolver context
1850 -  prefix - the prefix string to prepend to all TAO option requests
1851 
1852    Notes:
1853    A hyphen (-) must NOT be given at the beginning of the prefix name.
1854    The first character of all runtime options is AUTOMATICALLY the hyphen.
1855 
1856    For example, to distinguish between the runtime options for two
1857    different TAO solvers, one could call
1858 .vb
1859       TaoSetOptionsPrefix(tao1,"sys1_")
1860       TaoSetOptionsPrefix(tao2,"sys2_")
1861 .ve
1862 
1863    This would enable use of different options for each system, such as
1864 .vb
1865       -sys1_tao_method blmvm -sys1_tao_gtol 1.e-3
1866       -sys2_tao_method lmvm  -sys2_tao_gtol 1.e-4
1867 .ve
1868 
1869 
1870    Level: advanced
1871 
1872 .seealso: TaoAppendOptionsPrefix(), TaoGetOptionsPrefix()
1873 @*/
1874 
1875 PetscErrorCode TaoSetOptionsPrefix(TaoSolver tao, const char p[])
1876 {
1877   PetscErrorCode ierr;
1878 
1879   PetscFunctionBegin;
1880   ierr = PetscObjectSetOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1881   if (tao->linesearch) {
1882     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1883   }
1884   if (tao->ksp) {
1885     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1886   }
1887   PetscFunctionReturn(0);
1888 }
1889 
1890 #undef __FUNCT__
1891 #define __FUNCT__ "TaoAppendOptionsPrefix"
1892 /*@C
1893    TaoAppendOptionsPrefix - Appends to the prefix used for searching for all
1894    TAO options in the database.
1895 
1896 
1897    Logically Collective on TaoSolver
1898 
1899    Input Parameters:
1900 +  tao - the TaoSolver solver context
1901 -  prefix - the prefix string to prepend to all TAO option requests
1902 
1903    Notes:
1904    A hyphen (-) must NOT be given at the beginning of the prefix name.
1905    The first character of all runtime options is AUTOMATICALLY the hyphen.
1906 
1907 
1908    Level: advanced
1909 
1910 .seealso: TaoSetOptionsPrefix(), TaoGetOptionsPrefix()
1911 @*/
1912 PetscErrorCode TaoAppendOptionsPrefix(TaoSolver tao, const char p[])
1913 {
1914   PetscErrorCode ierr;
1915 
1916   PetscFunctionBegin;
1917   ierr = PetscObjectAppendOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr);
1918   if (tao->linesearch) {
1919     ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr);
1920   }
1921   if (tao->ksp) {
1922     ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr);
1923   }
1924   PetscFunctionReturn(0);
1925 }
1926 
1927 #undef __FUNCT__
1928 #define __FUNCT__ "TaoGetOptionsPrefix"
1929 /*@C
1930   TaoGetOptionsPrefix - Gets the prefix used for searching for all
1931   TAO options in the database
1932 
1933   Not Collective
1934 
1935   Input Parameters:
1936 . tao - the TaoSolver context
1937 
1938   Output Parameters:
1939 . prefix - pointer to the prefix string used is returned
1940 
1941   Notes: On the fortran side, the user should pass in a string 'prefix' of
1942   sufficient length to hold the prefix.
1943 
1944   Level: advanced
1945 
1946 .seealso: TaoSetOptionsPrefix(), TaoAppendOptionsPrefix()
1947 @*/
1948 PetscErrorCode TaoGetOptionsPrefix(TaoSolver tao, const char *p[])
1949 {
1950    return PetscObjectGetOptionsPrefix((PetscObject)tao,p);
1951 }
1952 
1953 #undef __FUNCT__
1954 #define __FUNCT__ "TaoSetDefaultKSPType"
1955 /*@
1956   TaoSetDefaultKSPType - Sets the default KSP type if a KSP object
1957   is created.
1958 
1959   Logically Collective on TaoSolver
1960 
1961   InputParameters:
1962 + tao - the TaoSolver context
1963 - ktype - the KSP type TAO will use by default
1964 
1965   Note: Some solvers may require a particular KSP type and will not work
1966   correctly if the default value is changed
1967 
1968   Options Database Key:
1969 - tao_ksp_type
1970 
1971   Level: advanced
1972 @*/
1973 PetscErrorCode TaoSetDefaultKSPType(TaoSolver tao, KSPType ktype)
1974 {
1975   const char     *prefix=0;
1976   char           *option=0;
1977   size_t         n1,n2;
1978   PetscErrorCode ierr;
1979 
1980   PetscFunctionBegin;
1981   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
1982   ierr = TaoGetOptionsPrefix(tao,&prefix);CHKERRQ(ierr);
1983   ierr = PetscStrlen(prefix,&n1);
1984   ierr = PetscStrlen("_ksp_type",&n2);
1985   ierr = PetscMalloc(n1+n2+1,&option);
1986   ierr = PetscStrncpy(option,prefix,n1+1);
1987   ierr = PetscStrncat(option,"_ksp_type",n2+1);
1988   ierr = PetscOptionsSetValue(option,ktype);CHKERRQ(ierr);
1989   PetscFunctionReturn(0);
1990 }
1991 
1992 #undef __FUNCT__
1993 #define __FUNCT__ "TaoSetDefaulLineSearchType"
1994 /*@
1995   TaoSetDefaultLineSearchType - Sets the default LineSearch type if a LineSearch object
1996   is created.
1997 
1998   Logically Collective on TaoSolver
1999 
2000   InputParameters:
2001 + tao - the TaoSolver context
2002 - lstype - the line search type TAO will use by default
2003 
2004   Note: Some solvers may require a particular line search type and will not work
2005   correctly if the default value is changed
2006 
2007   Options Database Key:
2008 - tao_ls_type
2009 
2010   Level: advanced
2011 @*/
2012 PetscErrorCode TaoSetDefaultLineSearchType(TaoSolver tao, TaoLineSearchType lstype)
2013 {
2014   const char     *prefix=0;
2015   char           *option=0;
2016   size_t         n1,n2;
2017   PetscErrorCode ierr;
2018 
2019   PetscFunctionBegin;
2020   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2021   ierr = TaoGetOptionsPrefix(tao,&prefix);CHKERRQ(ierr);
2022   ierr = PetscStrlen(prefix,&n1);
2023   ierr = PetscStrlen("_ls_type",&n2);
2024   ierr = PetscMalloc(n1+n2+1,&option);
2025   ierr = PetscStrncpy(option,prefix,n1+1);
2026   ierr = PetscStrncat(option,"_ls_type",n2+1);
2027   ierr = PetscOptionsSetValue(option,lstype);CHKERRQ(ierr);
2028   PetscFunctionReturn(0);
2029 }
2030 
2031 #undef __FUNCT__
2032 #define __FUNCT__ "TaoSetDefaultPCType"
2033 /*@
2034   TaoSetDefaultPCType - Sets the default PC type if a PC object
2035   is created.
2036 
2037   Logically Collective on TaoSolver
2038 
2039   InputParameters:
2040 + tao - the TaoSolver context
2041 - pctype - the preconditioner type TAO will use by default
2042 
2043   Note: Some solvers may require a particular PC type and will not work
2044   correctly if the default value is changed
2045 
2046   Options Database Key:
2047 - tao_pc_type
2048 
2049   Level: advanced
2050 @*/
2051 PetscErrorCode TaoSetDefaultPCType(TaoSolver tao, PCType pctype)
2052 {
2053   const char     *prefix=0;
2054   char           *option=0;
2055   size_t         n1,n2;
2056   PetscErrorCode ierr;
2057 
2058   PetscFunctionBegin;
2059   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2060   ierr = TaoGetOptionsPrefix(tao,&prefix);CHKERRQ(ierr);
2061   ierr = PetscStrlen(prefix,&n1);
2062   ierr = PetscStrlen("_pc_type",&n2);
2063   ierr = PetscMalloc(n1+n2+1,&option);
2064   ierr = PetscStrncpy(option,prefix,n1+1);
2065   ierr = PetscStrncat(option,"_pc_type",n2+1);
2066   ierr = PetscOptionsSetValue(option,pctype);CHKERRQ(ierr);
2067   PetscFunctionReturn(0);
2068 }
2069 
2070 #undef __FUNCT__
2071 #define __FUNCT__ "TaoSetType"
2072 /*@C
2073    TaoSetType - Sets the method for the unconstrained minimization solver.
2074 
2075    Collective on TaoSolver
2076 
2077    Input Parameters:
2078 +  solver - the TaoSolver solver context
2079 -  type - a known method
2080 
2081    Options Database Key:
2082 +  -tao_method <type> - Sets the method; use -help for a list
2083    of available methods (for instance, "-tao_method tao_lmvm" or
2084    "-tao_method tao_tron")
2085 -  -tao_type <type> - identical to -tao_method
2086 
2087    Available methods include:
2088 +    tao_nls - Newton's method with line search for unconstrained minimization
2089 .    tao_ntr - Newton's method with trust region for unconstrained minimization
2090 .    tao_ntl - Newton's method with trust region, line search for unconstrained minimization
2091 .    tao_lmvm - Limited memory variable metric method for unconstrained minimization
2092 .    tao_cg - Nonlinear conjugate gradient method for unconstrained minimization
2093 .    tao_nm - Nelder-Mead algorithm for derivate-free unconstrained minimization
2094 .    tao_tron - Newton Trust Region method for bound constrained minimization
2095 .    tao_gpcg - Newton Trust Region method for quadratic bound constrained minimization
2096 .    tao_blmvm - Limited memory variable metric method for bound constrained minimization
2097 +    tao_pounders - Model-based algorithm pounder extended for nonlinear least squares
2098 
2099   Level: intermediate
2100 
2101 .seealso: TaoCreate(), TaoGetType()
2102 
2103 @*/
2104 PetscErrorCode TaoSetType(TaoSolver tao, const TaoSolverType type)
2105 {
2106     PetscErrorCode ierr;
2107     PetscErrorCode (*create_xxx)(TaoSolver);
2108     PetscBool      issame;
2109 
2110     PetscFunctionBegin;
2111     PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2112 
2113     ierr = PetscObjectTypeCompare((PetscObject)tao,type,&issame);CHKERRQ(ierr);
2114     if (issame) PetscFunctionReturn(0);
2115 
2116     ierr = PetscFunctionListFind(TaoSolverList, type, (void(**)(void))&create_xxx);CHKERRQ(ierr);
2117     if (!create_xxx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested TaoSolver type %s",type);
2118 
2119     /* Destroy the existing solver information */
2120     if (tao->ops->destroy) {
2121         ierr = (*tao->ops->destroy)(tao);CHKERRQ(ierr);
2122     }
2123     ierr = KSPDestroy(&tao->ksp);CHKERRQ(ierr);
2124     ierr = TaoLineSearchDestroy(&tao->linesearch);CHKERRQ(ierr);
2125     ierr = VecDestroy(&tao->gradient);CHKERRQ(ierr);
2126     ierr = VecDestroy(&tao->stepdirection);CHKERRQ(ierr);
2127 
2128     tao->ops->setup = 0;
2129     tao->ops->solve = 0;
2130     tao->ops->view  = 0;
2131     tao->ops->setfromoptions = 0;
2132     tao->ops->destroy = 0;
2133 
2134     tao->setupcalled = PETSC_FALSE;
2135 
2136     ierr = (*create_xxx)(tao);CHKERRQ(ierr);
2137     ierr = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(ierr);
2138     PetscFunctionReturn(0);
2139 }
2140 
2141 #undef __FUNCT__
2142 #define __FUNCT__ "TaoSolverRegister"
2143 /*MC
2144    TaoSolverRegister - Adds a method to the TAO package for unconstrained minimization.
2145 
2146    Synopsis:
2147    TaoSolverRegister(char *name_solver,char *path,char *name_Create,int (*routine_Create)(TaoSolver))
2148 
2149    Not collective
2150 
2151    Input Parameters:
2152 +  sname - name of a new user-defined solver
2153 -  func - routine to Create method context
2154 
2155    Notes:
2156    TaoSolverRegister() may be called multiple times to add several user-defined solvers.
2157 
2158    Sample usage:
2159 .vb
2160    TaoSolverRegister("my_solver",MySolverCreate);
2161 .ve
2162 
2163    Then, your solver can be chosen with the procedural interface via
2164 $     TaoSetType(tao,"my_solver")
2165    or at runtime via the option
2166 $     -tao_method my_solver
2167 
2168    Level: advanced
2169 
2170 .seealso: TaoSolverRegisterAll(), TaoSolverRegisterDestroy()
2171 M*/
2172 PetscErrorCode TaoSolverRegister(const char sname[], PetscErrorCode (*func)(TaoSolver))
2173 {
2174   PetscErrorCode ierr;
2175 
2176   PetscFunctionBegin;
2177   ierr = PetscFunctionListAdd(&TaoSolverList,sname, (void (*)(void))func);CHKERRQ(ierr);
2178   PetscFunctionReturn(0);
2179 }
2180 
2181 #undef __FUNCT__
2182 #define __FUNCT__ "TaoSolverRegisterDestroy"
2183 /*@C
2184    TaoSolverRegisterDestroy - Frees the list of minimization solvers that were
2185    registered by TaoSolverRegisterDynamic().
2186 
2187    Not Collective
2188 
2189    Level: advanced
2190 
2191 .seealso: TaoSolverRegisterAll(), TaoSolverRegister()
2192 @*/
2193 PetscErrorCode TaoSolverRegisterDestroy(void)
2194 {
2195   PetscErrorCode ierr;
2196   PetscFunctionBegin;
2197   ierr = PetscFunctionListDestroy(&TaoSolverList);CHKERRQ(ierr);
2198   TaoSolverRegisterAllCalled = PETSC_FALSE;
2199   PetscFunctionReturn(0);
2200 }
2201 
2202 #undef __FUNCT__
2203 #define __FUNCT__ "TaoSetTerminationReason"
2204 /*@
2205   TaoSetTerminationReason - Sets the termination flag on a TaoSolver object
2206 
2207   Logically Collective on TaoSolver
2208 
2209   Input Parameters:
2210 + tao - the TaoSolver context
2211 - reason - one of
2212 $     TAO_CONVERGED_ATOL (2),
2213 $     TAO_CONVERGED_RTOL (3),
2214 $     TAO_CONVERGED_STEPTOL (4),
2215 $     TAO_CONVERGED_MINF (5),
2216 $     TAO_CONVERGED_USER (6),
2217 $     TAO_DIVERGED_MAXITS (-2),
2218 $     TAO_DIVERGED_NAN (-4),
2219 $     TAO_DIVERGED_MAXFCN (-5),
2220 $     TAO_DIVERGED_LS_FAILURE (-6),
2221 $     TAO_DIVERGED_TR_REDUCTION (-7),
2222 $     TAO_DIVERGED_USER (-8),
2223 $     TAO_CONTINUE_ITERATING (0)
2224 
2225    Level: intermediate
2226 
2227 @*/
2228 PetscErrorCode TaoSetTerminationReason(TaoSolver tao, TaoSolverTerminationReason reason)
2229 {
2230   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2231   PetscFunctionBegin;
2232   tao->reason = reason;
2233   PetscFunctionReturn(0);
2234 }
2235 
2236 #undef __FUNCT__
2237 #define __FUNCT__ "TaoGetTerminationReason"
2238 /*@
2239    TaoGetTerminationReason - Gets the reason the TaoSolver iteration was stopped.
2240 
2241    Not Collective
2242 
2243    Input Parameter:
2244 .  tao - the TaoSolver solver context
2245 
2246    Output Parameter:
2247 .  reason - one of
2248 
2249 
2250 $  TAO_CONVERGED_FATOL (1)           f(X)-f(X*) <= fatol
2251 $  TAO_CONVERGED_FRTOL (2)           |f(X) - f(X*)|/|f(X)| < frtol
2252 $  TAO_CONVERGED_GATOL (3)           ||g(X)|| < gatol
2253 $  TAO_CONVERGED_GRTOL (4)           ||g(X)|| / f(X)  < grtol
2254 $  TAO_CONVERGED_GTTOL (5)           ||g(X)|| / ||g(X0)|| < gttol
2255 $  TAO_CONVERGED_STEPTOL (6)         step size small
2256 $  TAO_CONVERGED_MINF (7)            F < F_min
2257 $  TAO_CONVERGED_USER (8)            User defined
2258 
2259 $  TAO_DIVERGED_MAXITS (-2)          its > maxits
2260 $  TAO_DIVERGED_NAN (-4)             Numerical problems
2261 $  TAO_DIVERGED_MAXFCN (-5)          fevals > max_funcsals
2262 $  TAO_DIVERGED_LS_FAILURE (-6)      line search failure
2263 $  TAO_DIVERGED_TR_REDUCTION (-7)    trust region failure
2264 $  TAO_DIVERGED_USER(-8)             (user defined)
2265 
2266 $  TAO_CONTINUE_ITERATING (0)
2267 
2268    where
2269 +  X - current solution
2270 .  X0 - initial guess
2271 .  f(X) - current function value
2272 .  f(X*) - true solution (estimated)
2273 .  g(X) - current gradient
2274 .  its - current iterate number
2275 .  maxits - maximum number of iterates
2276 .  fevals - number of function evaluations
2277 -  max_funcsals - maximum number of function evaluations
2278 
2279    Level: intermediate
2280 
2281 .seealso: TaoSetConvergenceTest(), TaoSetTolerances()
2282 
2283 @*/
2284 PetscErrorCode TaoGetTerminationReason(TaoSolver tao, TaoSolverTerminationReason *reason)
2285 {
2286   PetscFunctionBegin;
2287   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2288   PetscValidPointer(reason,2);
2289   *reason = tao->reason;
2290   PetscFunctionReturn(0);
2291 }
2292 
2293 #undef __FUNCT__
2294 #define __FUNCT__ "TaoGetSolutionStatus"
2295 /*@
2296   TaoGetSolutionStatus - Get the current iterate, objective value,
2297   residual, infeasibility, and termination
2298 
2299   Not Collective
2300 
2301    Input Parameters:
2302 .  tao - the TaoSolver context
2303 
2304    Output Parameters:
2305 +  iterate - the current iterate number (>=0)
2306 .  f - the current function value
2307 .  gnorm - the square of the gradient norm, duality gap, or other measure
2308 indicating distance from optimality.
2309 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2310 .  xdiff - the step length or trust region radius of the most recent iterate.
2311 -  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2312 
2313    Level: intermediate
2314 
2315    Note:
2316    TAO returns the values set by the solvers in the routine TaoMonitor().
2317 
2318    Note:
2319    If any of the output arguments are set to NULL, no corresponding value will be returned.
2320 
2321 
2322 .seealso: TaoMonitor(), TaoGetTerminationReason()
2323 @*/
2324 PetscErrorCode TaoGetSolutionStatus(TaoSolver tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoSolverTerminationReason *reason)
2325 {
2326   PetscFunctionBegin;
2327   if (its) *its=tao->niter;
2328   if (f) *f=tao->fc;
2329   if (gnorm) *gnorm=tao->residual;
2330   if (cnorm) *cnorm=tao->cnorm;
2331   if (reason) *reason=tao->reason;
2332   if (xdiff) *xdiff=tao->step;
2333   PetscFunctionReturn(0);
2334 }
2335 
2336 
2337 #undef __FUNCT__
2338 #define __FUNCT__ "TaoGetType"
2339 /*@C
2340    TaoGetType - Gets the current TaoSolver algorithm.
2341 
2342    Not Collective
2343 
2344    Input Parameter:
2345 .  tao - the TaoSolver solver context
2346 
2347    Output Parameter:
2348 .  type - TaoSolver method
2349 
2350    Level: intermediate
2351 
2352 @*/
2353 PetscErrorCode TaoGetType(TaoSolver tao, const TaoSolverType *type)
2354 {
2355   PetscFunctionBegin;
2356   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2357   PetscValidPointer(type,2);
2358   *type=((PetscObject)tao)->type_name;
2359   PetscFunctionReturn(0);
2360 }
2361 
2362 #undef __FUNCT__
2363 #define __FUNCT__ "TaoMonitor"
2364 /*@C
2365   TaoMonitor - Monitor the solver and the current solution.  This
2366   routine will record the iteration number and residual statistics,
2367   call any monitors specified by the user, and calls the convergence-check routine.
2368 
2369    Input Parameters:
2370 +  tao - the TaoSolver context
2371 .  its - the current iterate number (>=0)
2372 .  f - the current objective function value
2373 .  res - the gradient norm, square root of the duality gap, or other measure
2374 indicating distince from optimality.  This measure will be recorded and
2375 used for some termination tests.
2376 .  cnorm - the infeasibility of the current solution with regard to the constraints.
2377 -  steplength - multiple of the step direction added to the previous iterate.
2378 
2379    Output Parameters:
2380 .  reason - The termination reason, which can equal TAO_CONTINUE_ITERATING
2381 
2382    Options Database Key:
2383 .  -tao_monitor - Use the default monitor, which prints statistics to standard output
2384 
2385 .seealso TaoGetTerminationReason(), TaoDefaultMonitor(), TaoSetMonitor()
2386 
2387    Level: developer
2388 
2389 @*/
2390 PetscErrorCode TaoMonitor(TaoSolver tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength, TaoSolverTerminationReason *reason)
2391 {
2392   PetscErrorCode ierr;
2393   PetscInt       i;
2394 
2395   PetscFunctionBegin;
2396   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2397   tao->fc = f;
2398   tao->residual = res;
2399   tao->cnorm = cnorm;
2400   tao->step = steplength;
2401   tao->niter=its;
2402   if (its == 0) {
2403     tao->cnorm0 = cnorm; tao->gnorm0 = res;
2404   }
2405   TaoLogHistory(tao,f,res,cnorm);
2406   if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(res)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
2407   if (tao->ops->convergencetest) {
2408     ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr);
2409   }
2410   for (i=0;i<tao->numbermonitors;i++) {
2411     ierr = (*tao->monitor[i])(tao,tao->monitorcontext[i]);CHKERRQ(ierr);
2412   }
2413   *reason = tao->reason;
2414   PetscFunctionReturn(0);
2415 }
2416 
2417 #undef __FUNCT__
2418 #define __FUNCT__ "TaoSetHistory"
2419 /*@
2420    TaoSetHistory - Sets the array used to hold the convergence history.
2421 
2422    Logically Collective on TaoSolver
2423 
2424    Input Parameters:
2425 +  tao - the TaoSolver solver context
2426 .  obj   - array to hold objective value history
2427 .  resid - array to hold residual history
2428 .  cnorm - array to hold constraint violation history
2429 .  na  - size of obj, resid, and cnorm
2430 -  reset - PetscTrue indicates each new minimization resets the history counter to zero,
2431            else it continues storing new values for new minimizations after the old ones
2432 
2433    Notes:
2434    If set, TAO will fill the given arrays with the indicated
2435    information at each iteration.  If no information is desired
2436    for a given array, then NULL may be used.
2437 
2438    This routine is useful, e.g., when running a code for purposes
2439    of accurate performance monitoring, when no I/O should be done
2440    during the section of code that is being timed.
2441 
2442    Level: intermediate
2443 
2444 .seealso: TaoGetHistory()
2445 
2446 @*/
2447 PetscErrorCode TaoSetHistory(TaoSolver tao, PetscReal *obj, PetscReal *resid, PetscReal *cnorm, PetscInt na,PetscBool reset)
2448 {
2449   PetscFunctionBegin;
2450   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2451   tao->hist_obj = obj;
2452   tao->hist_resid = resid;
2453   tao->hist_cnorm = cnorm;
2454   tao->hist_max   = na;
2455   tao->hist_reset = reset;
2456   PetscFunctionReturn(0);
2457 }
2458 
2459 #undef __FUNCT__
2460 #define __FUNCT__ "TaoGetHistory"
2461 /*@C
2462    TaoGetHistory - Gets the array used to hold the convergence history.
2463 
2464    Collective on TaoSolver
2465 
2466    Input Parameter:
2467 .  tao - the TaoSolver context
2468 
2469    Output Parameters:
2470 +  obj   - array used to hold objective value history
2471 .  resid - array used to hold residual history
2472 .  cnorm - array used to hold constraint violation history
2473 -  nhist  - size of obj, resid, and cnorm (will be less than or equal to na given in TaoSetHistory)
2474 
2475 
2476    Notes:
2477     The calling sequence for this routine in Fortran is
2478 $   call TaoGetHistory(TaoSolver tao, integer nhist, integer info)
2479 
2480    This routine is useful, e.g., when running a code for purposes
2481    of accurate performance monitoring, when no I/O should be done
2482    during the section of code that is being timed.
2483 
2484    Level: advanced
2485 
2486 .seealso: TaoSetHistory()
2487 
2488 @*/
2489 PetscErrorCode TaoGetHistory(TaoSolver tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt *nhist)
2490 {
2491   PetscFunctionBegin;
2492   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2493   if (obj)   *obj   = tao->hist_obj;
2494   if (cnorm) *cnorm = tao->hist_cnorm;
2495   if (resid) *resid = tao->hist_resid;
2496   if (nhist) *nhist   = tao->hist_len;
2497   PetscFunctionReturn(0);
2498 }
2499 
2500 #undef __FUNCT__
2501 #define __FUNCT__ "TaoSetApplicationContext"
2502 /*@
2503    TaoSetApplicationContext - Sets the optional user-defined context for
2504    a solver.
2505 
2506    Logically Collective on TaoSolver
2507 
2508    Input Parameters:
2509 +  tao  - the TaoSolver context
2510 -  usrP - optional user context
2511 
2512    Level: intermediate
2513 
2514 .seealso: TaoGetApplicationContext(), TaoSetApplicationContext()
2515 @*/
2516 PetscErrorCode  TaoSetApplicationContext(TaoSolver tao,void *usrP)
2517 {
2518   PetscFunctionBegin;
2519   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2520   tao->user = usrP;
2521   PetscFunctionReturn(0);
2522 }
2523 
2524 #undef __FUNCT__
2525 #define __FUNCT__ "TaoGetApplicationContext"
2526 /*@
2527    TaoGetApplicationContext - Gets the user-defined context for a
2528    TAO solvers.
2529 
2530    Not Collective
2531 
2532    Input Parameter:
2533 .  tao  - TaoSolver context
2534 
2535    Output Parameter:
2536 .  usrP - user context
2537 
2538    Level: intermediate
2539 
2540 .seealso: TaoSetApplicationContext()
2541 @*/
2542 PetscErrorCode  TaoGetApplicationContext(TaoSolver tao,void *usrP)
2543 {
2544   PetscFunctionBegin;
2545   PetscValidHeaderSpecific(tao,TAOSOLVER_CLASSID,1);
2546   *(void**)usrP = tao->user;
2547   PetscFunctionReturn(0);
2548 }
2549