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