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