xref: /petsc/src/tao/interface/taosolver.c (revision dbbe0bcd3f3a8fbab5a45420dc06f8387e5764c6)
1af0996ceSBarry Smith #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/
20f0abf79SStefano Zampini #include <petsc/private/snesimpl.h>
3a7e14dcfSSatish Balay 
4441846f8SBarry Smith PetscBool TaoRegisterAllCalled = PETSC_FALSE;
5441846f8SBarry Smith PetscFunctionList TaoList = NULL;
6a7e14dcfSSatish Balay 
7441846f8SBarry Smith PetscClassId TAO_CLASSID;
80ebee16dSLisandro Dalcin 
90ebee16dSLisandro Dalcin PetscLogEvent TAO_Solve;
100ebee16dSLisandro Dalcin PetscLogEvent TAO_ObjectiveEval;
110ebee16dSLisandro Dalcin PetscLogEvent TAO_GradientEval;
120ebee16dSLisandro Dalcin PetscLogEvent TAO_ObjGradEval;
130ebee16dSLisandro Dalcin PetscLogEvent TAO_HessianEval;
140ebee16dSLisandro Dalcin PetscLogEvent TAO_JacobianEval;
150ebee16dSLisandro Dalcin PetscLogEvent TAO_ConstraintsEval;
16a7e14dcfSSatish Balay 
1783c8fe1dSLisandro Dalcin const char *TaoSubSetTypes[] = {"subvec","mask","matrixfree","TaoSubSetType","TAO_SUBSET_",NULL};
18a7e14dcfSSatish Balay 
19e882e171SHong Zhang struct _n_TaoMonitorDrawCtx {
20e882e171SHong Zhang   PetscViewer viewer;
21e882e171SHong Zhang   PetscInt    howoften;  /* when > 0 uses iteration % howoften, when negative only final solution plotted */
22e882e171SHong Zhang };
23e882e171SHong Zhang 
240f0abf79SStefano Zampini static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, Tao tao)
250f0abf79SStefano Zampini {
260f0abf79SStefano Zampini   SNES snes_ewdummy = tao->snes_ewdummy;
270f0abf79SStefano Zampini 
280f0abf79SStefano Zampini   PetscFunctionBegin;
290f0abf79SStefano Zampini   if (!snes_ewdummy) PetscFunctionReturn(0);
300f0abf79SStefano Zampini   /* populate snes_ewdummy struct values used in KSPPreSolve_SNESEW */
310f0abf79SStefano Zampini   snes_ewdummy->vec_func = b;
320f0abf79SStefano Zampini   snes_ewdummy->rtol = tao->gttol;
330f0abf79SStefano Zampini   snes_ewdummy->iter = tao->niter;
340f0abf79SStefano Zampini   PetscCall(VecNorm(b,NORM_2,&snes_ewdummy->norm));
350f0abf79SStefano Zampini   PetscCall(KSPPreSolve_SNESEW(ksp,b,x,snes_ewdummy));
360f0abf79SStefano Zampini   snes_ewdummy->vec_func = NULL;
370f0abf79SStefano Zampini   PetscFunctionReturn(0);
380f0abf79SStefano Zampini }
390f0abf79SStefano Zampini 
400f0abf79SStefano Zampini static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, Tao tao)
410f0abf79SStefano Zampini {
420f0abf79SStefano Zampini   SNES snes_ewdummy = tao->snes_ewdummy;
430f0abf79SStefano Zampini 
440f0abf79SStefano Zampini   PetscFunctionBegin;
450f0abf79SStefano Zampini   if (!snes_ewdummy) PetscFunctionReturn(0);
460f0abf79SStefano Zampini   PetscCall(KSPPostSolve_SNESEW(ksp,b,x,snes_ewdummy));
470f0abf79SStefano Zampini   PetscFunctionReturn(0);
480f0abf79SStefano Zampini }
490f0abf79SStefano Zampini 
500f0abf79SStefano Zampini static PetscErrorCode TaoSetUpEW_Private(Tao tao)
510f0abf79SStefano Zampini {
520f0abf79SStefano Zampini   SNESKSPEW  *kctx;
530f0abf79SStefano Zampini   const char *ewprefix;
540f0abf79SStefano Zampini 
550f0abf79SStefano Zampini   PetscFunctionBegin;
560f0abf79SStefano Zampini   if (!tao->ksp) PetscFunctionReturn(0);
570f0abf79SStefano Zampini   if (tao->ksp_ewconv) {
580f0abf79SStefano Zampini     if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao),&tao->snes_ewdummy));
590f0abf79SStefano Zampini     tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE;
600f0abf79SStefano Zampini     PetscCall(KSPSetPreSolve(tao->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPreSolve_TAOEW_Private,tao));
610f0abf79SStefano Zampini     PetscCall(KSPSetPostSolve(tao->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPostSolve_TAOEW_Private,tao));
620f0abf79SStefano Zampini 
630f0abf79SStefano Zampini     PetscCall(KSPGetOptionsPrefix(tao->ksp,&ewprefix));
640f0abf79SStefano Zampini     kctx = (SNESKSPEW*)tao->snes_ewdummy->kspconvctx;
650f0abf79SStefano Zampini     PetscCall(SNESEWSetFromOptions_Private(kctx,PetscObjectComm((PetscObject)tao),ewprefix));
660f0abf79SStefano Zampini   } else PetscCall(SNESDestroy(&tao->snes_ewdummy));
670f0abf79SStefano Zampini   PetscFunctionReturn(0);
680f0abf79SStefano Zampini }
690f0abf79SStefano Zampini 
70a7e14dcfSSatish Balay /*@
7165ba42b6SBarry Smith   TaoCreate - Creates a Tao solver
72a7e14dcfSSatish Balay 
73d083f849SBarry Smith   Collective
74a7e14dcfSSatish Balay 
75a7e14dcfSSatish Balay   Input Parameter:
76a7e14dcfSSatish Balay . comm - MPI communicator
77a7e14dcfSSatish Balay 
78a7e14dcfSSatish Balay   Output Parameter:
79441846f8SBarry Smith . newtao - the new Tao context
80a7e14dcfSSatish Balay 
81a7e14dcfSSatish Balay   Available methods include:
8265ba42b6SBarry Smith +    `TAONLS` - nls Newton's method with line search for unconstrained minimization
8365ba42b6SBarry Smith .    `TAONTR` - ntr Newton's method with trust region for unconstrained minimization
8465ba42b6SBarry Smith .    `TAONTL` - ntl Newton's method with trust region, line search for unconstrained minimization
8565ba42b6SBarry Smith .    `TAOLMVM` - lmvm Limited memory variable metric method for unconstrained minimization
8665ba42b6SBarry Smith .    `TAOCG` - cg Nonlinear conjugate gradient method for unconstrained minimization
8765ba42b6SBarry Smith .    `TAONM` - nm Nelder-Mead algorithm for derivate-free unconstrained minimization
8865ba42b6SBarry Smith .    `TAOTRON` - tron Newton Trust Region method for bound constrained minimization
8965ba42b6SBarry Smith .    `TAOGPCG` - gpcg Newton Trust Region method for quadratic bound constrained minimization
9065ba42b6SBarry Smith .    `TAOBLMVM` - blmvm Limited memory variable metric method for bound constrained minimization
9165ba42b6SBarry Smith .    `TAOLCL` - lcl Linearly constrained Lagrangian method for pde-constrained minimization
9265ba42b6SBarry Smith -    `TAOPOUNDERS` - pounders Model-based algorithm for nonlinear least squares
93a7e14dcfSSatish Balay 
94a7e14dcfSSatish Balay    Options Database Keys:
9565ba42b6SBarry Smith .   -tao_type - select which method Tao should use
96a7e14dcfSSatish Balay 
97a7e14dcfSSatish Balay    Level: beginner
98a7e14dcfSSatish Balay 
9965ba42b6SBarry Smith .seealso: `Tao`, `TaoSolve()`, `TaoDestroy()`, `TAOSetFromOptions()`, `TAOSetType()`
100a7e14dcfSSatish Balay @*/
101441846f8SBarry Smith PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao)
102a7e14dcfSSatish Balay {
103441846f8SBarry Smith   Tao            tao;
104a7e14dcfSSatish Balay 
105a7e14dcfSSatish Balay   PetscFunctionBegin;
106a7e14dcfSSatish Balay   PetscValidPointer(newtao,2);
1079566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
1089566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchInitializePackage());
1099566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(tao,TAO_CLASSID,"Tao","Optimization solver","Tao",comm,TaoDestroy,TaoView));
110a7e14dcfSSatish Balay 
11194511df7SStefano Zampini   /* Set non-NULL defaults */
11294511df7SStefano Zampini   tao->ops->convergencetest = TaoDefaultConvergenceTest;
113a7e14dcfSSatish Balay 
114a7e14dcfSSatish Balay   tao->max_it    = 10000;
11594511df7SStefano Zampini   tao->max_funcs = -1;
1166f4723b1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
1176f4723b1SBarry Smith   tao->gatol     = 1e-5;
1186f4723b1SBarry Smith   tao->grtol     = 1e-5;
1196f1f5f59SAlp Dener   tao->crtol     = 1e-5;
1206f1f5f59SAlp Dener   tao->catol     = 1e-5;
1216f4723b1SBarry Smith #else
122a7e14dcfSSatish Balay   tao->gatol     = 1e-8;
123a7e14dcfSSatish Balay   tao->grtol     = 1e-8;
1246f1f5f59SAlp Dener   tao->crtol     = 1e-8;
1256f1f5f59SAlp Dener   tao->catol     = 1e-8;
1266f4723b1SBarry Smith #endif
1276552cf8aSJason Sarich   tao->gttol     = 0.0;
1284f1535bcSTodd Munson   tao->steptol   = 0.0;
129e270355aSBarry Smith   tao->trust0    = PETSC_INFINITY;
1306f4723b1SBarry Smith   tao->fmin      = PETSC_NINFINITY;
1319fa2b5dcSStefano Zampini 
132a7e14dcfSSatish Balay   tao->hist_reset  = PETSC_TRUE;
133a7e14dcfSSatish Balay 
1349566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
135a7e14dcfSSatish Balay   *newtao = tao;
136a7e14dcfSSatish Balay   PetscFunctionReturn(0);
137a7e14dcfSSatish Balay }
138a7e14dcfSSatish Balay 
139a7e14dcfSSatish Balay /*@
140a7e14dcfSSatish Balay   TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
141a7e14dcfSSatish Balay 
14265ba42b6SBarry Smith   Collective on tao
143a7e14dcfSSatish Balay 
144a7e14dcfSSatish Balay   Input Parameters:
145441846f8SBarry Smith . tao - the Tao context
146a7e14dcfSSatish Balay 
147a7e14dcfSSatish Balay   Notes:
14865ba42b6SBarry Smith   The user must set up the Tao with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`.
149a7e14dcfSSatish Balay 
15065ba42b6SBarry Smith   You should call `TaoGetConvergedReason()` or run with -tao_converged_reason to determine if the optimization algorithm actually succeeded or
151a35d58b8SBarry Smith   why it failed.
152a35d58b8SBarry Smith 
153a7e14dcfSSatish Balay   Level: beginner
154a7e14dcfSSatish Balay 
15565ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()`
156a7e14dcfSSatish Balay  @*/
157441846f8SBarry Smith PetscErrorCode TaoSolve(Tao tao)
158a7e14dcfSSatish Balay {
159e2379f4fSBarry Smith   static PetscBool set = PETSC_FALSE;
16047a47007SBarry Smith 
161a7e14dcfSSatish Balay   PetscFunctionBegin;
162441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
163d0609cedSBarry Smith   PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n"
164e2379f4fSBarry Smith                                    "title   = {Toolkit for Advanced Optimization (TAO) Users Manual},\n"
165e2379f4fSBarry Smith                                    "author  = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n"
166e2379f4fSBarry Smith                                    "Institution = {Argonne National Laboratory},\n"
167e2379f4fSBarry Smith                                    "Year   = 2014,\n"
168e2379f4fSBarry Smith                                    "Number = {ANL/MCS-TM-322 - Revision 3.5},\n"
169d0609cedSBarry Smith                                    "url    = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n",&set));
170494bef23SAlp Dener   tao->header_printed = PETSC_FALSE;
1719566063dSJacob Faibussowitsch   PetscCall(TaoSetUp(tao));
1729566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
1731baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch));
174a7e14dcfSSatish Balay 
1759566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TAO_Solve,tao,0,0,0));
176*dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao,solve);
1779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TAO_Solve,tao,0,0,0));
178a7e14dcfSSatish Balay 
1799566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(tao->solution,(PetscObject)tao,"-tao_view_solution"));
1807cf37e64SBarry Smith 
1818931d482SJason Sarich   tao->ntotalits += tao->niter;
1829566063dSJacob Faibussowitsch   PetscCall(TaoViewFromOptions(tao,NULL,"-tao_view"));
183a7e14dcfSSatish Balay 
184a7e14dcfSSatish Balay   if (tao->printreason) {
185a7e14dcfSSatish Balay     if (tao->reason > 0) {
18663a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(((PetscObject)tao)->comm,"TAO solve converged due to %s iterations %" PetscInt_FMT "\n",TaoConvergedReasons[tao->reason],tao->niter));
187a7e14dcfSSatish Balay     } else {
18863a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(((PetscObject)tao)->comm,"TAO solve did not converge due to %s iteration %" PetscInt_FMT "\n",TaoConvergedReasons[tao->reason],tao->niter));
189a7e14dcfSSatish Balay     }
190a7e14dcfSSatish Balay   }
191a7e14dcfSSatish Balay   PetscFunctionReturn(0);
192a7e14dcfSSatish Balay }
193a7e14dcfSSatish Balay 
194a7e14dcfSSatish Balay /*@
195a7e14dcfSSatish Balay   TaoSetUp - Sets up the internal data structures for the later use
196a7e14dcfSSatish Balay   of a Tao solver
197a7e14dcfSSatish Balay 
198a7e14dcfSSatish Balay   Collective on tao
199a7e14dcfSSatish Balay 
200a7e14dcfSSatish Balay   Input Parameters:
20165ba42b6SBarry Smith . tao - the Tao context
202a7e14dcfSSatish Balay 
203a7e14dcfSSatish Balay   Notes:
20465ba42b6SBarry Smith   The user will not need to explicitly call `TaoSetUp()`, as it will
20565ba42b6SBarry Smith   automatically be called in `TaoSolve()`.  However, if the user
20665ba42b6SBarry Smith   desires to call it explicitly, it should come after `TaoCreate()`
20765ba42b6SBarry Smith   and any TaoSetSomething() routines, but before `TaoSolve()`.
208a7e14dcfSSatish Balay 
209a7e14dcfSSatish Balay   Level: advanced
210a7e14dcfSSatish Balay 
21165ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoSolve()`
212a7e14dcfSSatish Balay @*/
213441846f8SBarry Smith PetscErrorCode TaoSetUp(Tao tao)
214a7e14dcfSSatish Balay {
215a7e14dcfSSatish Balay   PetscFunctionBegin;
216441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
217a7e14dcfSSatish Balay   if (tao->setupcalled) PetscFunctionReturn(0);
2180f0abf79SStefano Zampini   PetscCall(TaoSetUpEW_Private(tao));
2193c859ba3SBarry Smith   PetscCheck(tao->solution,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetSolution");
220*dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao,setup);
221a7e14dcfSSatish Balay   tao->setupcalled = PETSC_TRUE;
222a7e14dcfSSatish Balay   PetscFunctionReturn(0);
223a7e14dcfSSatish Balay }
224a7e14dcfSSatish Balay 
2251fb7b255SJunchao Zhang /*@C
22665ba42b6SBarry Smith   TaoDestroy - Destroys the Tao context that was created with `TaoCreate()`
227a7e14dcfSSatish Balay 
22865ba42b6SBarry Smith   Collective on tao
229a7e14dcfSSatish Balay 
230a7e14dcfSSatish Balay   Input Parameter:
231441846f8SBarry Smith . tao - the Tao context
232a7e14dcfSSatish Balay 
233a7e14dcfSSatish Balay   Level: beginner
234a7e14dcfSSatish Balay 
23565ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoSolve()`
236a7e14dcfSSatish Balay @*/
237441846f8SBarry Smith PetscErrorCode TaoDestroy(Tao *tao)
238a7e14dcfSSatish Balay {
239a7e14dcfSSatish Balay   PetscFunctionBegin;
240a7e14dcfSSatish Balay   if (!*tao) PetscFunctionReturn(0);
241441846f8SBarry Smith   PetscValidHeaderSpecific(*tao,TAO_CLASSID,1);
24283c8fe1dSLisandro Dalcin   if (--((PetscObject)*tao)->refct > 0) {*tao = NULL;PetscFunctionReturn(0);}
243a7e14dcfSSatish Balay 
244a7e14dcfSSatish Balay   if ((*tao)->ops->destroy) {
2459566063dSJacob Faibussowitsch     PetscCall((*((*tao))->ops->destroy)(*tao));
246a7e14dcfSSatish Balay   }
2479566063dSJacob Faibussowitsch   PetscCall(KSPDestroy(&(*tao)->ksp));
2480f0abf79SStefano Zampini   PetscCall(SNESDestroy(&(*tao)->snes_ewdummy));
2499566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch));
250a7e14dcfSSatish Balay 
251a7e14dcfSSatish Balay   if ((*tao)->ops->convergencedestroy) {
2529566063dSJacob Faibussowitsch     PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP));
253a7e14dcfSSatish Balay     if ((*tao)->jacobian_state_inv) {
2549566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
255a7e14dcfSSatish Balay     }
256a7e14dcfSSatish Balay   }
2579566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->solution));
2589566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->gradient));
2599566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->ls_res));
260a7e14dcfSSatish Balay 
261a9603a14SPatrick Farrell   if ((*tao)->gradient_norm) {
2629566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm));
2639566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp));
264a9603a14SPatrick Farrell   }
265a9603a14SPatrick Farrell 
2669566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XL));
2679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XU));
2689566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IL));
2699566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IU));
2709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DE));
2719566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DI));
2729566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints));
2739566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_equality));
2749566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_inequality));
2759566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->stepdirection));
2769566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian_pre));
2779566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian));
2789566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac));
2799566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac_pre));
2809566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_pre));
2819566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian));
2829566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_pre));
2839566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state));
2849566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
2859566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_design));
2869566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality));
2879566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre));
2889566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality));
2899566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre));
2909566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->state_is));
2919566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->design_is));
2929566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->res_weights_v));
2939566063dSJacob Faibussowitsch   PetscCall(TaoCancelMonitors(*tao));
294ae93cb3cSJason Sarich   if ((*tao)->hist_malloc) {
2959566063dSJacob Faibussowitsch     PetscCall(PetscFree4((*tao)->hist_obj,(*tao)->hist_resid,(*tao)->hist_cnorm,(*tao)->hist_lits));
296ae93cb3cSJason Sarich   }
297737f463aSAlp Dener   if ((*tao)->res_weights_n) {
2989566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_rows));
2999566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_cols));
3009566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_w));
301125ddc8aSJason Sarich   }
3029566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(tao));
303a7e14dcfSSatish Balay   PetscFunctionReturn(0);
304a7e14dcfSSatish Balay }
305a7e14dcfSSatish Balay 
306a7e14dcfSSatish Balay /*@
30765ba42b6SBarry Smith    TaoKSPSetUseEW - Sets `SNES` use Eisenstat-Walker method for
3080f0abf79SStefano Zampini    computing relative tolerance for linear solvers.
3090f0abf79SStefano Zampini 
31065ba42b6SBarry Smith    Logically Collective on tao
3110f0abf79SStefano Zampini 
3120f0abf79SStefano Zampini    Input Parameters:
3130f0abf79SStefano Zampini +  tao - Tao context
31465ba42b6SBarry Smith -  flag - `PETSC_TRUE` or `PETSC_FALSE`
3150f0abf79SStefano Zampini 
3160f0abf79SStefano Zampini    Notes:
31765ba42b6SBarry Smith    See `SNESKSPSetUseEW()` for customization details.
3180f0abf79SStefano Zampini 
3190f0abf79SStefano Zampini    Level: advanced
3200f0abf79SStefano Zampini 
3210f0abf79SStefano Zampini    Reference:
3220f0abf79SStefano Zampini    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
3230f0abf79SStefano Zampini    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
3240f0abf79SStefano Zampini 
32565ba42b6SBarry Smith .seealso: `Tao`, `SNESKSPSetUseEW()`
3260f0abf79SStefano Zampini @*/
3270f0abf79SStefano Zampini PetscErrorCode  TaoKSPSetUseEW(Tao tao,PetscBool flag)
3280f0abf79SStefano Zampini {
3290f0abf79SStefano Zampini   PetscFunctionBegin;
3300f0abf79SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
3310f0abf79SStefano Zampini   PetscValidLogicalCollectiveBool(tao,flag,2);
3320f0abf79SStefano Zampini   tao->ksp_ewconv = flag;
3330f0abf79SStefano Zampini   PetscFunctionReturn(0);
3340f0abf79SStefano Zampini }
3350f0abf79SStefano Zampini 
3360f0abf79SStefano Zampini /*@
337441846f8SBarry Smith   TaoSetFromOptions - Sets various Tao parameters from user
338a7e14dcfSSatish Balay   options.
339a7e14dcfSSatish Balay 
34065ba42b6SBarry Smith   Collective on tao
341a7e14dcfSSatish Balay 
34201d2d390SJose E. Roman   Input Parameter:
343441846f8SBarry Smith . tao - the Tao solver context
344a7e14dcfSSatish Balay 
345a7e14dcfSSatish Balay   options Database Keys:
34665ba42b6SBarry Smith + -tao_type <type> - The algorithm that Tao uses (lmvm, nls, etc.)
347a7e14dcfSSatish Balay . -tao_gatol <gatol> - absolute error tolerance for ||gradient||
348a7e14dcfSSatish Balay . -tao_grtol <grtol> - relative error tolerance for ||gradient||
349a7e14dcfSSatish Balay . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient
350a7e14dcfSSatish Balay . -tao_max_it <max> - sets maximum number of iterations
351a7e14dcfSSatish Balay . -tao_max_funcs <max> - sets maximum number of function evaluations
352a7e14dcfSSatish Balay . -tao_fmin <fmin> - stop if function value reaches fmin
353a7e14dcfSSatish Balay . -tao_steptol <tol> - stop if trust region radius less than <tol>
354a7e14dcfSSatish Balay . -tao_trust0 <t> - initial trust region radius
355a7e14dcfSSatish Balay . -tao_monitor - prints function value and residual at each iteration
356a7e14dcfSSatish Balay . -tao_smonitor - same as tao_monitor, but truncates very small values
357a7e14dcfSSatish Balay . -tao_cmonitor - prints function value, residual, and constraint norm at each iteration
358a7e14dcfSSatish Balay . -tao_view_solution - prints solution vector at each iteration
3594a48860cSAlp Dener . -tao_view_ls_residual - prints least-squares residual vector at each iteration
360147403d9SBarry Smith . -tao_view_stepdirection - prints step direction vector at each iteration
361a7e14dcfSSatish Balay . -tao_view_gradient - prints gradient vector at each iteration
362a7e14dcfSSatish Balay . -tao_draw_solution - graphically view solution vector at each iteration
363a7e14dcfSSatish Balay . -tao_draw_step - graphically view step vector at each iteration
364a7e14dcfSSatish Balay . -tao_draw_gradient - graphically view gradient at each iteration
365a7e14dcfSSatish Balay . -tao_fd_gradient - use gradient computed with finite differences
366f4c1ad5cSStefano Zampini . -tao_fd_hessian - use hessian computed with finite differences
367f4c1ad5cSStefano Zampini . -tao_mf_hessian - use matrix-free hessian computed with finite differences
368a7e14dcfSSatish Balay . -tao_cancelmonitors - cancels all monitors (except those set with command line)
369441846f8SBarry Smith . -tao_view - prints information about the Tao after solving
37065ba42b6SBarry Smith - -tao_converged_reason - prints the reason Tao stopped iterating
371a7e14dcfSSatish Balay 
372a7e14dcfSSatish Balay   Notes:
373a7e14dcfSSatish Balay   To see all options, run your program with the -help option or consult the
37465ba42b6SBarry Smith  user's manual. Should be called after `TaoCreate()` but before `TaoSolve()`
375a7e14dcfSSatish Balay 
376a7e14dcfSSatish Balay   Level: beginner
37765ba42b6SBarry Smith 
37865ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoSolve()`
379a7e14dcfSSatish Balay @*/
380441846f8SBarry Smith PetscErrorCode TaoSetFromOptions(Tao tao)
381a7e14dcfSSatish Balay {
382b625d6c7SJed Brown   TaoType        default_type = TAOLMVM;
383a7e14dcfSSatish Balay   char           type[256], monfilename[PETSC_MAX_PATH_LEN];
384a7e14dcfSSatish Balay   PetscViewer    monviewer;
385a7e14dcfSSatish Balay   PetscBool      flg;
386a7e14dcfSSatish Balay   MPI_Comm       comm;
387a7e14dcfSSatish Balay 
388a7e14dcfSSatish Balay   PetscFunctionBegin;
389441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
3909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)tao,&comm));
391125ddc8aSJason Sarich 
392a7e14dcfSSatish Balay   /* So no warnings are given about unused options */
3939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHasName(((PetscObject)tao)->options,((PetscObject)tao)->prefix,"-tao_ls_type",&flg));
394a7e14dcfSSatish Balay 
395d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)tao);
396a7e14dcfSSatish Balay   {
397a82e8c82SStefano Zampini     if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name;
398a7e14dcfSSatish Balay     /* Check for type from options */
3999566063dSJacob Faibussowitsch     PetscCall(PetscOptionsFList("-tao_type","Tao Solver type","TaoSetType",TaoList,default_type,type,256,&flg));
400a7e14dcfSSatish Balay     if (flg) {
4019566063dSJacob Faibussowitsch       PetscCall(TaoSetType(tao,type));
402a7e14dcfSSatish Balay     } else if (!((PetscObject)tao)->type_name) {
4039566063dSJacob Faibussowitsch       PetscCall(TaoSetType(tao,default_type));
404a7e14dcfSSatish Balay     }
405a7e14dcfSSatish Balay 
4069566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_catol","Stop if constraints violations within","TaoSetConstraintTolerances",tao->catol,&tao->catol,&flg));
4076552cf8aSJason Sarich     if (flg) tao->catol_changed = PETSC_TRUE;
4086aad120cSJose E. Roman     PetscCall(PetscOptionsReal("-tao_crtol","Stop if relative constraint violations within","TaoSetConstraintTolerances",tao->crtol,&tao->crtol,&flg));
4096552cf8aSJason Sarich     if (flg) tao->crtol_changed = PETSC_TRUE;
4109566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_gatol","Stop if norm of gradient less than","TaoSetTolerances",tao->gatol,&tao->gatol,&flg));
4116552cf8aSJason Sarich     if (flg) tao->gatol_changed = PETSC_TRUE;
4129566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_grtol","Stop if norm of gradient divided by the function value is less than","TaoSetTolerances",tao->grtol,&tao->grtol,&flg));
4136552cf8aSJason Sarich     if (flg) tao->grtol_changed = PETSC_TRUE;
4149566063dSJacob Faibussowitsch     PetscCall(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));
4156552cf8aSJason Sarich     if (flg) tao->gttol_changed = PETSC_TRUE;
4169566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-tao_max_it","Stop if iteration number exceeds","TaoSetMaximumIterations",tao->max_it,&tao->max_it,&flg));
4176552cf8aSJason Sarich     if (flg) tao->max_it_changed = PETSC_TRUE;
4189566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-tao_max_funcs","Stop if number of function evaluations exceeds","TaoSetMaximumFunctionEvaluations",tao->max_funcs,&tao->max_funcs,&flg));
4196552cf8aSJason Sarich     if (flg) tao->max_funcs_changed = PETSC_TRUE;
4209566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_fmin","Stop if function less than","TaoSetFunctionLowerBound",tao->fmin,&tao->fmin,&flg));
4216552cf8aSJason Sarich     if (flg) tao->fmin_changed = PETSC_TRUE;
4229566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_steptol","Stop if step size or trust region radius less than","",tao->steptol,&tao->steptol,&flg));
4236552cf8aSJason Sarich     if (flg) tao->steptol_changed = PETSC_TRUE;
4249566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-tao_trust0","Initial trust region radius","TaoSetTrustRegionRadius",tao->trust0,&tao->trust0,&flg));
4256552cf8aSJason Sarich     if (flg) tao->trust0_changed = PETSC_TRUE;
4269566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_view_solution","view solution vector after each evaluation","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
427a7e14dcfSSatish Balay     if (flg) {
4289566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4299566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoSolutionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
430a7e14dcfSSatish Balay     }
431a7e14dcfSSatish Balay 
43265ba42b6SBarry Smith     PetscCall(PetscOptionsBool("-tao_converged_reason","Print reason for Tao converged","TaoSolve",tao->printreason,&tao->printreason,NULL));
4339566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_view_gradient","view gradient vector after each evaluation","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
434a7e14dcfSSatish Balay     if (flg) {
4359566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4369566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoGradientMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
437a7e14dcfSSatish Balay     }
438a7e14dcfSSatish Balay 
4399566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_view_stepdirection","view step direction vector after each iteration","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
440a7e14dcfSSatish Balay     if (flg) {
4419566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4429566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoStepDirectionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
443a7e14dcfSSatish Balay     }
444a7e14dcfSSatish Balay 
4459566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_view_residual","view least-squares residual vector after each evaluation","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
446a7e14dcfSSatish Balay     if (flg) {
4479566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4489566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoResidualMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
449a7e14dcfSSatish Balay     }
450a7e14dcfSSatish Balay 
4519566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_monitor","Use the default convergence monitor","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
452a7e14dcfSSatish Balay     if (flg) {
4539566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4549566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
455a7e14dcfSSatish Balay     }
456a7e14dcfSSatish Balay 
4579566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_gmonitor","Use the convergence monitor with extra globalization info","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
4588d5ead36SAlp Dener     if (flg) {
4599566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4609566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoDefaultGMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
4618d5ead36SAlp Dener     }
4628d5ead36SAlp Dener 
4639566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_smonitor","Use the short convergence monitor","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
464a7e14dcfSSatish Balay     if (flg) {
4659566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4669566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoDefaultSMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
467a7e14dcfSSatish Balay     }
468a7e14dcfSSatish Balay 
4699566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-tao_cmonitor","Use the default convergence monitor with constraint norm","TaoSetMonitor","stdout",monfilename,sizeof(monfilename),&flg));
470a7e14dcfSSatish Balay     if (flg) {
4719566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIOpen(comm,monfilename,&monviewer));
4729566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoDefaultCMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy));
473a7e14dcfSSatish Balay     }
474a7e14dcfSSatish Balay 
4758afaa268SBarry Smith     flg = PETSC_FALSE;
4769566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_cancelmonitors","cancel all monitors and call any registered destroy routines","TaoCancelMonitors",flg,&flg,NULL));
4779566063dSJacob Faibussowitsch     if (flg) PetscCall(TaoCancelMonitors(tao));
478a7e14dcfSSatish Balay 
4798afaa268SBarry Smith     flg = PETSC_FALSE;
4809566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_draw_solution","Plot solution vector at each iteration","TaoSetMonitor",flg,&flg,NULL));
481a7e14dcfSSatish Balay     if (flg) {
482e882e171SHong Zhang       TaoMonitorDrawCtx drawctx;
483e882e171SHong Zhang       PetscInt          howoften = 1;
4849566063dSJacob Faibussowitsch       PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&drawctx));
4859566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoDrawSolutionMonitor,drawctx,(PetscErrorCode (*)(void**))TaoMonitorDrawCtxDestroy));
486a7e14dcfSSatish Balay     }
487a7e14dcfSSatish Balay 
4888afaa268SBarry Smith     flg = PETSC_FALSE;
4899566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_draw_step","plots step direction at each iteration","TaoSetMonitor",flg,&flg,NULL));
4901baa6e33SBarry Smith     if (flg) PetscCall(TaoSetMonitor(tao,TaoDrawStepMonitor,NULL,NULL));
491a7e14dcfSSatish Balay 
4928afaa268SBarry Smith     flg = PETSC_FALSE;
4939566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_draw_gradient","plots gradient at each iteration","TaoSetMonitor",flg,&flg,NULL));
494a7e14dcfSSatish Balay     if (flg) {
495e882e171SHong Zhang       TaoMonitorDrawCtx drawctx;
496e882e171SHong Zhang       PetscInt          howoften = 1;
4979566063dSJacob Faibussowitsch       PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&drawctx));
4989566063dSJacob Faibussowitsch       PetscCall(TaoSetMonitor(tao,TaoDrawGradientMonitor,drawctx,(PetscErrorCode (*)(void**))TaoMonitorDrawCtxDestroy));
499a7e14dcfSSatish Balay     }
5008afaa268SBarry Smith     flg = PETSC_FALSE;
5019566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_fd_gradient","compute gradient using finite differences","TaoDefaultComputeGradient",flg,&flg,NULL));
5021baa6e33SBarry Smith     if (flg) PetscCall(TaoSetGradient(tao,NULL,TaoDefaultComputeGradient,NULL));
503f4c1ad5cSStefano Zampini     flg = PETSC_FALSE;
5049566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_fd_hessian","compute hessian using finite differences","TaoDefaultComputeHessian",flg,&flg,NULL));
505f4c1ad5cSStefano Zampini     if (flg) {
506f4c1ad5cSStefano Zampini       Mat H;
507f4c1ad5cSStefano Zampini 
5089566063dSJacob Faibussowitsch       PetscCall(MatCreate(PetscObjectComm((PetscObject)tao),&H));
5099566063dSJacob Faibussowitsch       PetscCall(MatSetType(H,MATAIJ));
5109566063dSJacob Faibussowitsch       PetscCall(TaoSetHessian(tao,H,H,TaoDefaultComputeHessian,NULL));
5119566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&H));
512f4c1ad5cSStefano Zampini     }
513f4c1ad5cSStefano Zampini     flg = PETSC_FALSE;
5149566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_mf_hessian","compute matrix-free hessian using finite differences","TaoDefaultComputeHessianMFFD",flg,&flg,NULL));
515f4c1ad5cSStefano Zampini     if (flg) {
516f4c1ad5cSStefano Zampini       Mat H;
517f4c1ad5cSStefano Zampini 
5189566063dSJacob Faibussowitsch       PetscCall(MatCreate(PetscObjectComm((PetscObject)tao),&H));
5199566063dSJacob Faibussowitsch       PetscCall(TaoSetHessian(tao,H,H,TaoDefaultComputeHessianMFFD,NULL));
5209566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&H));
521f4c1ad5cSStefano Zampini     }
522414d97d3SAlp Dener     flg = PETSC_FALSE;
5239566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-tao_recycle_history","enable recycling/re-using information from the previous TaoSolve() call for some algorithms","TaoSetRecycleHistory",flg,&flg,NULL));
5241baa6e33SBarry Smith     if (flg) PetscCall(TaoSetRecycleHistory(tao,PETSC_TRUE));
5259566063dSJacob Faibussowitsch     PetscCall(PetscOptionsEnum("-tao_subset_type","subset type","",TaoSubSetTypes,(PetscEnum)tao->subset_type,(PetscEnum*)&tao->subset_type,NULL));
526a7e14dcfSSatish Balay 
5270f0abf79SStefano Zampini     if (tao->ksp) {
5280f0abf79SStefano Zampini       PetscCall(PetscOptionsBool("-tao_ksp_ew","Use Eisentat-Walker linear system convergence test","TaoKSPSetUseEW",tao->ksp_ewconv,&tao->ksp_ewconv,NULL));
5290f0abf79SStefano Zampini       PetscCall(TaoKSPSetUseEW(tao,tao->ksp_ewconv));
5300f0abf79SStefano Zampini     }
5310f0abf79SStefano Zampini 
5321baa6e33SBarry Smith     if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
533a82e8c82SStefano Zampini 
534*dbbe0bcdSBarry Smith     PetscTryTypeMethod(tao,setfromoptions,PetscOptionsObject);
535a7e14dcfSSatish Balay   }
536d0609cedSBarry Smith   PetscOptionsEnd();
537a7e14dcfSSatish Balay   PetscFunctionReturn(0);
538a7e14dcfSSatish Balay }
539a7e14dcfSSatish Balay 
540a7e14dcfSSatish Balay /*@C
54165ba42b6SBarry Smith    TaoViewFromOptions - View a Tao options from the options database
542fe2efc57SMark 
54365ba42b6SBarry Smith    Collective on tao
544fe2efc57SMark 
545fe2efc57SMark    Input Parameters:
546fe2efc57SMark +  A - the  Tao context
547736c3998SJose E. Roman .  obj - Optional object
548736c3998SJose E. Roman -  name - command line option
549fe2efc57SMark 
550fe2efc57SMark    Level: intermediate
551db781477SPatrick Sanan .seealso: `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()`
552fe2efc57SMark @*/
553fe2efc57SMark PetscErrorCode  TaoViewFromOptions(Tao A,PetscObject obj,const char name[])
554fe2efc57SMark {
555fe2efc57SMark   PetscFunctionBegin;
556fe2efc57SMark   PetscValidHeaderSpecific(A,TAO_CLASSID,1);
5579566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name));
558fe2efc57SMark   PetscFunctionReturn(0);
559fe2efc57SMark }
560fe2efc57SMark 
561fe2efc57SMark /*@C
56265ba42b6SBarry Smith   TaoView - Prints information about the Tao object
563a7e14dcfSSatish Balay 
56465ba42b6SBarry Smith   Collective on tao
565a7e14dcfSSatish Balay 
566a7e14dcfSSatish Balay   InputParameters:
567441846f8SBarry Smith + tao - the Tao context
568a7e14dcfSSatish Balay - viewer - visualization context
569a7e14dcfSSatish Balay 
570a7e14dcfSSatish Balay   Options Database Key:
57165ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()`
572a7e14dcfSSatish Balay 
573a7e14dcfSSatish Balay   Notes:
574a7e14dcfSSatish Balay   The available visualization contexts include
57565ba42b6SBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
57665ba42b6SBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
577a7e14dcfSSatish Balay          output where only the first processor opens
578a7e14dcfSSatish Balay          the file.  All other processors send their
579a7e14dcfSSatish Balay          data to the first processor to print.
580a7e14dcfSSatish Balay 
581a7e14dcfSSatish Balay   Level: beginner
582a7e14dcfSSatish Balay 
583db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()`
584a7e14dcfSSatish Balay @*/
585441846f8SBarry Smith PetscErrorCode TaoView(Tao tao, PetscViewer viewer)
586a7e14dcfSSatish Balay {
587a7e14dcfSSatish Balay   PetscBool           isascii,isstring;
588b625d6c7SJed Brown   TaoType             type;
58947a47007SBarry Smith 
590a7e14dcfSSatish Balay   PetscFunctionBegin;
591441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
592a7e14dcfSSatish Balay   if (!viewer) {
5939566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm,&viewer));
594a7e14dcfSSatish Balay   }
595a7e14dcfSSatish Balay   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
596a7e14dcfSSatish Balay   PetscCheckSameComm(tao,1,viewer,2);
597a7e14dcfSSatish Balay 
5989566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
5999566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
600a7e14dcfSSatish Balay   if (isascii) {
6019566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao,viewer));
602a7e14dcfSSatish Balay 
603a7e14dcfSSatish Balay     if (tao->ops->view) {
6049566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
605*dbbe0bcdSBarry Smith       PetscUseTypeMethod(tao,view ,viewer);
6069566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
607a7e14dcfSSatish Balay     }
6088d3f3ddaSAlp Dener     if (tao->linesearch) {
6099566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
6109566063dSJacob Faibussowitsch       PetscCall(TaoLineSearchView(tao->linesearch,viewer));
6119566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
6128d3f3ddaSAlp Dener     }
613a7e14dcfSSatish Balay     if (tao->ksp) {
6149566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
6159566063dSJacob Faibussowitsch       PetscCall(KSPView(tao->ksp,viewer));
61663a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total KSP iterations: %" PetscInt_FMT "\n",tao->ksp_tot_its));
6179566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
618a7e14dcfSSatish Balay     }
61919b5c101SAlp Dener 
6209566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
62119b5c101SAlp Dener 
622a7e14dcfSSatish Balay     if (tao->XL || tao->XU) {
6239566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Active Set subset type: %s\n",TaoSubSetTypes[tao->subset_type]));
624a7e14dcfSSatish Balay     }
625a7e14dcfSSatish Balay 
6269566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"convergence tolerances: gatol=%g,",(double)tao->gatol));
6279566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," steptol=%g,",(double)tao->steptol));
6289566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," gttol=%g\n",(double)tao->gttol));
6299566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"Residual in Function/Gradient:=%g\n",(double)tao->residual));
630a7e14dcfSSatish Balay 
6316246e8cdSAlp Dener     if (tao->constrained) {
6329566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"convergence tolerances:"));
6339566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer," catol=%g,",(double)tao->catol));
6349566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer," crtol=%g\n",(double)tao->crtol));
6359566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Residual in Constraints:=%g\n",(double)tao->cnorm));
636a7e14dcfSSatish Balay     }
637a7e14dcfSSatish Balay 
638a7e14dcfSSatish Balay     if (tao->trust < tao->steptol) {
6399566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"convergence tolerances: steptol=%g\n",(double)tao->steptol));
6409566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Final trust region radius:=%g\n",(double)tao->trust));
641a7e14dcfSSatish Balay     }
642a7e14dcfSSatish Balay 
643a7e14dcfSSatish Balay     if (tao->fmin>-1.e25) {
6449566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"convergence tolerances: function minimum=%g\n",(double)tao->fmin));
645a7e14dcfSSatish Balay     }
6469566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"Objective value=%g\n",(double)tao->fc));
647a7e14dcfSSatish Balay 
64863a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"total number of iterations=%" PetscInt_FMT ",          ",tao->niter));
64963a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"              (max: %" PetscInt_FMT ")\n",tao->max_it));
650a7e14dcfSSatish Balay 
651a7e14dcfSSatish Balay     if (tao->nfuncs>0) {
65263a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of function evaluations=%" PetscInt_FMT ",",tao->nfuncs));
65363a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"                max: %" PetscInt_FMT "\n",tao->max_funcs));
654a7e14dcfSSatish Balay     }
655a7e14dcfSSatish Balay     if (tao->ngrads>0) {
65663a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of gradient evaluations=%" PetscInt_FMT ",",tao->ngrads));
65763a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"                max: %" PetscInt_FMT "\n",tao->max_funcs));
658a7e14dcfSSatish Balay     }
659a7e14dcfSSatish Balay     if (tao->nfuncgrads>0) {
66063a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of function/gradient evaluations=%" PetscInt_FMT ",",tao->nfuncgrads));
66163a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"    (max: %" PetscInt_FMT ")\n",tao->max_funcs));
662a7e14dcfSSatish Balay     }
663a7e14dcfSSatish Balay     if (tao->nhess>0) {
66463a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of Hessian evaluations=%" PetscInt_FMT "\n",tao->nhess));
665a7e14dcfSSatish Balay     }
666a7e14dcfSSatish Balay     if (tao->nconstraints>0) {
66763a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of constraint function evaluations=%" PetscInt_FMT "\n",tao->nconstraints));
668a7e14dcfSSatish Balay     }
669a7e14dcfSSatish Balay     if (tao->njac>0) {
67063a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"total number of Jacobian evaluations=%" PetscInt_FMT "\n",tao->njac));
671a7e14dcfSSatish Balay     }
672a7e14dcfSSatish Balay 
673a7e14dcfSSatish Balay     if (tao->reason>0) {
6749566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,    "Solution converged: "));
675a7e14dcfSSatish Balay       switch (tao->reason) {
676a7e14dcfSSatish Balay       case TAO_CONVERGED_GATOL:
6779566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," ||g(X)|| <= gatol\n"));
678a7e14dcfSSatish Balay         break;
679a7e14dcfSSatish Balay       case TAO_CONVERGED_GRTOL:
6809566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," ||g(X)||/|f(X)| <= grtol\n"));
681a7e14dcfSSatish Balay         break;
682a7e14dcfSSatish Balay       case TAO_CONVERGED_GTTOL:
6839566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," ||g(X)||/||g(X0)|| <= gttol\n"));
684a7e14dcfSSatish Balay         break;
685a7e14dcfSSatish Balay       case TAO_CONVERGED_STEPTOL:
6869566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Steptol -- step size small\n"));
687a7e14dcfSSatish Balay         break;
688a7e14dcfSSatish Balay       case TAO_CONVERGED_MINF:
6899566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Minf --  f < fmin\n"));
690a7e14dcfSSatish Balay         break;
691a7e14dcfSSatish Balay       case TAO_CONVERGED_USER:
6929566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," User Terminated\n"));
693a7e14dcfSSatish Balay         break;
694a7e14dcfSSatish Balay       default:
6959566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer,"\n"));
696a7e14dcfSSatish Balay         break;
697a7e14dcfSSatish Balay       }
698a7e14dcfSSatish Balay     } else {
6999566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Solver terminated: %d",tao->reason));
700a7e14dcfSSatish Balay       switch (tao->reason) {
701a7e14dcfSSatish Balay       case TAO_DIVERGED_MAXITS:
7029566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Maximum Iterations\n"));
703a7e14dcfSSatish Balay         break;
704a7e14dcfSSatish Balay       case TAO_DIVERGED_NAN:
7059566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," NAN or Inf encountered\n"));
706a7e14dcfSSatish Balay         break;
707a7e14dcfSSatish Balay       case TAO_DIVERGED_MAXFCN:
7089566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Maximum Function Evaluations\n"));
709a7e14dcfSSatish Balay         break;
710a7e14dcfSSatish Balay       case TAO_DIVERGED_LS_FAILURE:
7119566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Line Search Failure\n"));
712a7e14dcfSSatish Balay         break;
713a7e14dcfSSatish Balay       case TAO_DIVERGED_TR_REDUCTION:
7149566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," Trust Region too small\n"));
715a7e14dcfSSatish Balay         break;
716a7e14dcfSSatish Balay       case TAO_DIVERGED_USER:
7179566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer," User Terminated\n"));
718a7e14dcfSSatish Balay         break;
719a7e14dcfSSatish Balay       default:
7209566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer,"\n"));
721a7e14dcfSSatish Balay         break;
722a7e14dcfSSatish Balay       }
723a7e14dcfSSatish Balay     }
7249566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
725a7e14dcfSSatish Balay   } else if (isstring) {
7269566063dSJacob Faibussowitsch     PetscCall(TaoGetType(tao,&type));
7279566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer," %-3.3s",type));
728a7e14dcfSSatish Balay   }
729a7e14dcfSSatish Balay   PetscFunctionReturn(0);
730a7e14dcfSSatish Balay }
731a7e14dcfSSatish Balay 
732a7e14dcfSSatish Balay /*@
733414d97d3SAlp Dener   TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using
73465ba42b6SBarry Smith   iterate information from the previous `TaoSolve()`. This feature is disabled by
735414d97d3SAlp Dener   default.
736414d97d3SAlp Dener 
73765ba42b6SBarry Smith   For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction
73865ba42b6SBarry Smith   from the previous `TaoSolve()` call when computing the first search direction in a
739414d97d3SAlp Dener   new solution. By default, CG methods set the first search direction to the
740414d97d3SAlp Dener   negative gradient.
741414d97d3SAlp Dener 
74265ba42b6SBarry Smith   For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses
74365ba42b6SBarry Smith   the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()`
744414d97d3SAlp Dener   call. By default, QN family of methods reset the initial Hessian approximation to
745414d97d3SAlp Dener   the identity matrix.
746414d97d3SAlp Dener 
747414d97d3SAlp Dener   For any other algorithm, this setting has no effect.
748414d97d3SAlp Dener 
74965ba42b6SBarry Smith   Logically collective on tao
750414d97d3SAlp Dener 
751414d97d3SAlp Dener   Input Parameters:
752414d97d3SAlp Dener + tao - the Tao context
753414d97d3SAlp Dener - recycle - boolean flag
754414d97d3SAlp Dener 
755414d97d3SAlp Dener   Options Database Keys:
756147403d9SBarry Smith . -tao_recycle_history <true,false> - reuse the history
757414d97d3SAlp Dener 
758414d97d3SAlp Dener   Level: intermediate
759414d97d3SAlp Dener 
76065ba42b6SBarry Smith .seealso: `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
761414d97d3SAlp Dener 
762414d97d3SAlp Dener @*/
763414d97d3SAlp Dener PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle)
764414d97d3SAlp Dener {
765414d97d3SAlp Dener   PetscFunctionBegin;
766414d97d3SAlp Dener   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
76794511df7SStefano Zampini   PetscValidLogicalCollectiveBool(tao,recycle,2);
768414d97d3SAlp Dener   tao->recycle = recycle;
769414d97d3SAlp Dener   PetscFunctionReturn(0);
770414d97d3SAlp Dener }
771414d97d3SAlp Dener 
772414d97d3SAlp Dener /*@
773414d97d3SAlp Dener   TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information
77465ba42b6SBarry Smith   from the previous `TaoSolve()`. This feature is disabled by default.
775414d97d3SAlp Dener 
77665ba42b6SBarry Smith   Logically collective on tao
777414d97d3SAlp Dener 
778414d97d3SAlp Dener   Input Parameters:
779147403d9SBarry Smith . tao - the Tao context
780414d97d3SAlp Dener 
781414d97d3SAlp Dener   Output Parameters:
782147403d9SBarry Smith . recycle - boolean flag
783414d97d3SAlp Dener 
784414d97d3SAlp Dener   Level: intermediate
785414d97d3SAlp Dener 
78665ba42b6SBarry Smith .seealso: `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
787414d97d3SAlp Dener 
788414d97d3SAlp Dener @*/
789414d97d3SAlp Dener PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle)
790414d97d3SAlp Dener {
791414d97d3SAlp Dener   PetscFunctionBegin;
792414d97d3SAlp Dener   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
79394511df7SStefano Zampini   PetscValidBoolPointer(recycle,2);
794414d97d3SAlp Dener   *recycle = tao->recycle;
795414d97d3SAlp Dener   PetscFunctionReturn(0);
796414d97d3SAlp Dener }
797414d97d3SAlp Dener 
798414d97d3SAlp Dener /*@
79965ba42b6SBarry Smith   TaoSetTolerances - Sets parameters used in Tao convergence tests
800a7e14dcfSSatish Balay 
80165ba42b6SBarry Smith   Logically collective on tao
802a7e14dcfSSatish Balay 
803a7e14dcfSSatish Balay   Input Parameters:
804441846f8SBarry Smith + tao - the Tao context
805a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this
806a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
807a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor
808a7e14dcfSSatish Balay 
809a7e14dcfSSatish Balay   Options Database Keys:
810e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol
811a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol
812a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol
813a7e14dcfSSatish Balay 
814a7e14dcfSSatish Balay   Stopping Criteria:
815a7e14dcfSSatish Balay $ ||g(X)||                            <= gatol
816a7e14dcfSSatish Balay $ ||g(X)|| / |f(X)|                   <= grtol
817a7e14dcfSSatish Balay $ ||g(X)|| / ||g(X0)||                <= gttol
818a7e14dcfSSatish Balay 
819a7e14dcfSSatish Balay   Notes:
820a7e14dcfSSatish Balay   Use PETSC_DEFAULT to leave one or more tolerances unchanged.
821a7e14dcfSSatish Balay 
822a7e14dcfSSatish Balay   Level: beginner
823a7e14dcfSSatish Balay 
824db781477SPatrick Sanan .seealso: `TaoGetTolerances()`
825a7e14dcfSSatish Balay 
826a7e14dcfSSatish Balay @*/
827e52336cbSBarry Smith PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol)
828a7e14dcfSSatish Balay {
829a7e14dcfSSatish Balay   PetscFunctionBegin;
830441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
83194511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,gatol,2);
83294511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,grtol,3);
83394511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,gttol,4);
834a7e14dcfSSatish Balay 
835a7e14dcfSSatish Balay   if (gatol != PETSC_DEFAULT) {
836a7e14dcfSSatish Balay     if (gatol<0) {
8379566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao,"Tried to set negative gatol -- ignored.\n"));
838a7e14dcfSSatish Balay     } else {
839a7e14dcfSSatish Balay       tao->gatol = PetscMax(0,gatol);
8406552cf8aSJason Sarich       tao->gatol_changed = PETSC_TRUE;
841a7e14dcfSSatish Balay     }
842a7e14dcfSSatish Balay   }
843a7e14dcfSSatish Balay 
844a7e14dcfSSatish Balay   if (grtol != PETSC_DEFAULT) {
845a7e14dcfSSatish Balay     if (grtol<0) {
8469566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao,"Tried to set negative grtol -- ignored.\n"));
847a7e14dcfSSatish Balay     } else {
848a7e14dcfSSatish Balay       tao->grtol = PetscMax(0,grtol);
8496552cf8aSJason Sarich       tao->grtol_changed = PETSC_TRUE;
850a7e14dcfSSatish Balay     }
851a7e14dcfSSatish Balay   }
852a7e14dcfSSatish Balay 
853a7e14dcfSSatish Balay   if (gttol != PETSC_DEFAULT) {
854a7e14dcfSSatish Balay     if (gttol<0) {
8559566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao,"Tried to set negative gttol -- ignored.\n"));
856a7e14dcfSSatish Balay     } else {
857a7e14dcfSSatish Balay       tao->gttol = PetscMax(0,gttol);
8586552cf8aSJason Sarich       tao->gttol_changed = PETSC_TRUE;
859a7e14dcfSSatish Balay     }
860a7e14dcfSSatish Balay   }
861a7e14dcfSSatish Balay   PetscFunctionReturn(0);
862a7e14dcfSSatish Balay }
863a7e14dcfSSatish Balay 
864a7e14dcfSSatish Balay /*@
86565ba42b6SBarry Smith   TaoSetConstraintTolerances - Sets constraint tolerance parameters used in Tao convergence tests
866a7e14dcfSSatish Balay 
86765ba42b6SBarry Smith   Logically collective on tao
868a7e14dcfSSatish Balay 
869a7e14dcfSSatish Balay   Input Parameters:
870441846f8SBarry Smith + tao - the Tao context
871e52336cbSBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than catol for used for gatol convergence criteria
8726aad120cSJose E. Roman - crtol - relative constraint tolerance, constraint norm must be less than crtol for used for gatol, gttol convergence criteria
873a7e14dcfSSatish Balay 
874a7e14dcfSSatish Balay   Options Database Keys:
875a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol
876a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol
877a7e14dcfSSatish Balay 
8786552cf8aSJason Sarich   Notes:
8796552cf8aSJason Sarich   Use PETSC_DEFAULT to leave any tolerance unchanged.
8806552cf8aSJason Sarich 
881a7e14dcfSSatish Balay   Level: intermediate
882a7e14dcfSSatish Balay 
883db781477SPatrick Sanan .seealso: `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()`
884a7e14dcfSSatish Balay 
885a7e14dcfSSatish Balay @*/
886441846f8SBarry Smith PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol)
887a7e14dcfSSatish Balay {
888a7e14dcfSSatish Balay   PetscFunctionBegin;
889441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
89094511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,catol,2);
89194511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,crtol,3);
892a7e14dcfSSatish Balay 
893a7e14dcfSSatish Balay   if (catol != PETSC_DEFAULT) {
894a7e14dcfSSatish Balay     if (catol<0) {
8959566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao,"Tried to set negative catol -- ignored.\n"));
896a7e14dcfSSatish Balay     } else {
897a7e14dcfSSatish Balay       tao->catol = PetscMax(0,catol);
8986552cf8aSJason Sarich       tao->catol_changed = PETSC_TRUE;
899a7e14dcfSSatish Balay     }
900a7e14dcfSSatish Balay   }
901a7e14dcfSSatish Balay 
902a7e14dcfSSatish Balay   if (crtol != PETSC_DEFAULT) {
903a7e14dcfSSatish Balay     if (crtol<0) {
9049566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao,"Tried to set negative crtol -- ignored.\n"));
905a7e14dcfSSatish Balay     } else {
906a7e14dcfSSatish Balay       tao->crtol = PetscMax(0,crtol);
9076552cf8aSJason Sarich       tao->crtol_changed = PETSC_TRUE;
908a7e14dcfSSatish Balay     }
909a7e14dcfSSatish Balay   }
910a7e14dcfSSatish Balay   PetscFunctionReturn(0);
911a7e14dcfSSatish Balay }
912a7e14dcfSSatish Balay 
91358417fe7SBarry Smith /*@
91465ba42b6SBarry Smith   TaoGetConstraintTolerances - Gets constraint tolerance parameters used in Tao  convergence tests
91558417fe7SBarry Smith 
91658417fe7SBarry Smith   Not ollective
91758417fe7SBarry Smith 
91858417fe7SBarry Smith   Input Parameter:
91958417fe7SBarry Smith . tao - the Tao context
92058417fe7SBarry Smith 
921d8d19677SJose E. Roman   Output Parameters:
922e52336cbSBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than catol for used for gatol convergence criteria
9236aad120cSJose E. Roman - crtol - relative constraint tolerance, constraint norm must be less than crtol for used for gatol, gttol convergence criteria
92458417fe7SBarry Smith 
92558417fe7SBarry Smith   Level: intermediate
92658417fe7SBarry Smith 
927db781477SPatrick Sanan .seealso: `TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()`
92858417fe7SBarry Smith 
92958417fe7SBarry Smith @*/
93058417fe7SBarry Smith PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol)
93158417fe7SBarry Smith {
93258417fe7SBarry Smith   PetscFunctionBegin;
93358417fe7SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
93458417fe7SBarry Smith   if (catol) *catol = tao->catol;
93558417fe7SBarry Smith   if (crtol) *crtol = tao->crtol;
93658417fe7SBarry Smith   PetscFunctionReturn(0);
93758417fe7SBarry Smith }
93858417fe7SBarry Smith 
939a7e14dcfSSatish Balay /*@
940a7e14dcfSSatish Balay    TaoSetFunctionLowerBound - Sets a bound on the solution objective value.
941a7e14dcfSSatish Balay    When an approximate solution with an objective value below this number
942a7e14dcfSSatish Balay    has been found, the solver will terminate.
943a7e14dcfSSatish Balay 
94465ba42b6SBarry Smith    Logically Collective on tao
945a7e14dcfSSatish Balay 
946a7e14dcfSSatish Balay    Input Parameters:
947441846f8SBarry Smith +  tao - the Tao solver context
948a7e14dcfSSatish Balay -  fmin - the tolerance
949a7e14dcfSSatish Balay 
950a7e14dcfSSatish Balay    Options Database Keys:
951a7e14dcfSSatish Balay .    -tao_fmin <fmin> - sets the minimum function value
952a7e14dcfSSatish Balay 
953a7e14dcfSSatish Balay    Level: intermediate
954a7e14dcfSSatish Balay 
955db781477SPatrick Sanan .seealso: `TaoSetTolerances()`
956a7e14dcfSSatish Balay @*/
957441846f8SBarry Smith PetscErrorCode TaoSetFunctionLowerBound(Tao tao,PetscReal fmin)
958a7e14dcfSSatish Balay {
959a7e14dcfSSatish Balay   PetscFunctionBegin;
960441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
96194511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,fmin,2);
962a7e14dcfSSatish Balay   tao->fmin = fmin;
9636552cf8aSJason Sarich   tao->fmin_changed = PETSC_TRUE;
964a7e14dcfSSatish Balay   PetscFunctionReturn(0);
965a7e14dcfSSatish Balay }
966a7e14dcfSSatish Balay 
967a7e14dcfSSatish Balay /*@
9688e96d397SJason Sarich    TaoGetFunctionLowerBound - Gets the bound on the solution objective value.
969a7e14dcfSSatish Balay    When an approximate solution with an objective value below this number
970a7e14dcfSSatish Balay    has been found, the solver will terminate.
971a7e14dcfSSatish Balay 
97265ba42b6SBarry Smith    Not collective on tao
973a7e14dcfSSatish Balay 
974a7e14dcfSSatish Balay    Input Parameters:
975441846f8SBarry Smith .  tao - the Tao solver context
976a7e14dcfSSatish Balay 
977a7e14dcfSSatish Balay    OutputParameters:
978a7e14dcfSSatish Balay .  fmin - the minimum function value
979a7e14dcfSSatish Balay 
980a7e14dcfSSatish Balay    Level: intermediate
981a7e14dcfSSatish Balay 
982db781477SPatrick Sanan .seealso: `TaoSetFunctionLowerBound()`
983a7e14dcfSSatish Balay @*/
984441846f8SBarry Smith PetscErrorCode TaoGetFunctionLowerBound(Tao tao,PetscReal *fmin)
985a7e14dcfSSatish Balay {
986a7e14dcfSSatish Balay   PetscFunctionBegin;
987441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
98894511df7SStefano Zampini   PetscValidRealPointer(fmin,2);
989a7e14dcfSSatish Balay   *fmin = tao->fmin;
990a7e14dcfSSatish Balay   PetscFunctionReturn(0);
991a7e14dcfSSatish Balay }
992a7e14dcfSSatish Balay 
993a7e14dcfSSatish Balay /*@
994a7e14dcfSSatish Balay    TaoSetMaximumFunctionEvaluations - Sets a maximum number of
995a7e14dcfSSatish Balay    function evaluations.
996a7e14dcfSSatish Balay 
99765ba42b6SBarry Smith    Logically Collective on tao
998a7e14dcfSSatish Balay 
999a7e14dcfSSatish Balay    Input Parameters:
1000441846f8SBarry Smith +  tao - the Tao solver context
1001a7e14dcfSSatish Balay -  nfcn - the maximum number of function evaluations (>=0)
1002a7e14dcfSSatish Balay 
1003a7e14dcfSSatish Balay    Options Database Keys:
1004a7e14dcfSSatish Balay .    -tao_max_funcs <nfcn> - sets the maximum number of function evaluations
1005a7e14dcfSSatish Balay 
1006a7e14dcfSSatish Balay    Level: intermediate
1007a7e14dcfSSatish Balay 
1008db781477SPatrick Sanan .seealso: `TaoSetTolerances()`, `TaoSetMaximumIterations()`
1009a7e14dcfSSatish Balay @*/
1010a7e14dcfSSatish Balay 
1011441846f8SBarry Smith PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao,PetscInt nfcn)
1012a7e14dcfSSatish Balay {
1013a7e14dcfSSatish Balay   PetscFunctionBegin;
1014441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
101594511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao,nfcn,2);
101694511df7SStefano Zampini   if (nfcn >= 0) { tao->max_funcs = PetscMax(0,nfcn); }
101794511df7SStefano Zampini   else { tao->max_funcs = -1; }
10186552cf8aSJason Sarich   tao->max_funcs_changed = PETSC_TRUE;
1019a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1020a7e14dcfSSatish Balay }
1021a7e14dcfSSatish Balay 
1022a7e14dcfSSatish Balay /*@
102365ba42b6SBarry Smith    TaoGetMaximumFunctionEvaluations - Gets a maximum number of
1024a7e14dcfSSatish Balay    function evaluations.
1025a7e14dcfSSatish Balay 
102665ba42b6SBarry Smith    Logically Collective on tao
1027a7e14dcfSSatish Balay 
1028a7e14dcfSSatish Balay    Input Parameters:
1029441846f8SBarry Smith .  tao - the Tao solver context
1030a7e14dcfSSatish Balay 
1031a7e14dcfSSatish Balay    Output Parameters:
1032a7e14dcfSSatish Balay .  nfcn - the maximum number of function evaluations
1033a7e14dcfSSatish Balay 
1034a7e14dcfSSatish Balay    Level: intermediate
1035a7e14dcfSSatish Balay 
1036db781477SPatrick Sanan .seealso: `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1037a7e14dcfSSatish Balay @*/
1038a7e14dcfSSatish Balay 
1039441846f8SBarry Smith PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao,PetscInt *nfcn)
1040a7e14dcfSSatish Balay {
1041a7e14dcfSSatish Balay   PetscFunctionBegin;
1042441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
104394511df7SStefano Zampini   PetscValidIntPointer(nfcn,2);
1044a7e14dcfSSatish Balay   *nfcn = tao->max_funcs;
1045a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1046a7e14dcfSSatish Balay }
1047a7e14dcfSSatish Balay 
1048770232b9SCe Qin /*@
1049770232b9SCe Qin    TaoGetCurrentFunctionEvaluations - Get current number of
1050770232b9SCe Qin    function evaluations.
1051770232b9SCe Qin 
1052770232b9SCe Qin    Not Collective
1053770232b9SCe Qin 
1054770232b9SCe Qin    Input Parameters:
1055770232b9SCe Qin .  tao - the Tao solver context
1056770232b9SCe Qin 
1057770232b9SCe Qin    Output Parameters:
105894511df7SStefano Zampini .  nfuncs - the current number of function evaluations (maximum between gradient and function evaluations)
1059770232b9SCe Qin 
1060770232b9SCe Qin    Level: intermediate
1061770232b9SCe Qin 
1062db781477SPatrick Sanan .seealso: `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1063770232b9SCe Qin @*/
1064770232b9SCe Qin 
1065770232b9SCe Qin PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao,PetscInt *nfuncs)
1066770232b9SCe Qin {
1067770232b9SCe Qin   PetscFunctionBegin;
1068770232b9SCe Qin   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
106994511df7SStefano Zampini   PetscValidIntPointer(nfuncs,2);
1070770232b9SCe Qin   *nfuncs = PetscMax(tao->nfuncs,tao->nfuncgrads);
1071770232b9SCe Qin   PetscFunctionReturn(0);
1072770232b9SCe Qin }
1073770232b9SCe Qin 
1074a7e14dcfSSatish Balay /*@
1075a7e14dcfSSatish Balay    TaoSetMaximumIterations - Sets a maximum number of iterates.
1076a7e14dcfSSatish Balay 
107765ba42b6SBarry Smith    Logically Collective on tao
1078a7e14dcfSSatish Balay 
1079a7e14dcfSSatish Balay    Input Parameters:
1080441846f8SBarry Smith +  tao - the Tao solver context
1081a7e14dcfSSatish Balay -  maxits - the maximum number of iterates (>=0)
1082a7e14dcfSSatish Balay 
1083a7e14dcfSSatish Balay    Options Database Keys:
1084a7e14dcfSSatish Balay .    -tao_max_it <its> - sets the maximum number of iterations
1085a7e14dcfSSatish Balay 
1086a7e14dcfSSatish Balay    Level: intermediate
1087a7e14dcfSSatish Balay 
1088db781477SPatrick Sanan .seealso: `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()`
1089a7e14dcfSSatish Balay @*/
1090441846f8SBarry Smith PetscErrorCode TaoSetMaximumIterations(Tao tao,PetscInt maxits)
1091a7e14dcfSSatish Balay {
1092a7e14dcfSSatish Balay   PetscFunctionBegin;
1093441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
109494511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao,maxits,2);
109547a47007SBarry Smith   tao->max_it = PetscMax(0,maxits);
10966552cf8aSJason Sarich   tao->max_it_changed = PETSC_TRUE;
1097a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1098a7e14dcfSSatish Balay }
1099a7e14dcfSSatish Balay 
1100a7e14dcfSSatish Balay /*@
110165ba42b6SBarry Smith    TaoGetMaximumIterations - Gets a maximum number of iterates that will be used
1102a7e14dcfSSatish Balay 
1103a7e14dcfSSatish Balay    Not Collective
1104a7e14dcfSSatish Balay 
1105a7e14dcfSSatish Balay    Input Parameters:
1106441846f8SBarry Smith .  tao - the Tao solver context
1107a7e14dcfSSatish Balay 
1108a7e14dcfSSatish Balay    Output Parameters:
1109a7e14dcfSSatish Balay .  maxits - the maximum number of iterates
1110a7e14dcfSSatish Balay 
1111a7e14dcfSSatish Balay    Level: intermediate
1112a7e14dcfSSatish Balay 
1113db781477SPatrick Sanan .seealso: `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()`
1114a7e14dcfSSatish Balay @*/
1115441846f8SBarry Smith PetscErrorCode TaoGetMaximumIterations(Tao tao,PetscInt *maxits)
1116a7e14dcfSSatish Balay {
1117a7e14dcfSSatish Balay   PetscFunctionBegin;
1118441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
111994511df7SStefano Zampini   PetscValidIntPointer(maxits,2);
1120a7e14dcfSSatish Balay   *maxits = tao->max_it;
1121a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1122a7e14dcfSSatish Balay }
1123a7e14dcfSSatish Balay 
1124a7e14dcfSSatish Balay /*@
1125a7e14dcfSSatish Balay    TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
1126a7e14dcfSSatish Balay 
112765ba42b6SBarry Smith    Logically collective on tao
1128a7e14dcfSSatish Balay 
1129d8d19677SJose E. Roman    Input Parameters:
113065ba42b6SBarry Smith +  tao - a Tao optimization solver
1131a7e14dcfSSatish Balay -  radius - the trust region radius
1132a7e14dcfSSatish Balay 
1133a7e14dcfSSatish Balay    Level: intermediate
1134a7e14dcfSSatish Balay 
1135a7e14dcfSSatish Balay    Options Database Key:
1136a7e14dcfSSatish Balay .  -tao_trust0 <t0> - sets initial trust region radius
1137a7e14dcfSSatish Balay 
113865ba42b6SBarry Smith .seealso: `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR`
1139a7e14dcfSSatish Balay @*/
1140441846f8SBarry Smith PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
1141a7e14dcfSSatish Balay {
1142a7e14dcfSSatish Balay   PetscFunctionBegin;
1143441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
114494511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao,radius,2);
1145a7e14dcfSSatish Balay   tao->trust0 = PetscMax(0.0,radius);
11466552cf8aSJason Sarich   tao->trust0_changed = PETSC_TRUE;
1147a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1148a7e14dcfSSatish Balay }
1149a7e14dcfSSatish Balay 
1150a7e14dcfSSatish Balay /*@
115165ba42b6SBarry Smith    TaoGetInitialTrustRegionRadius - Gets the initial trust region radius.
1152a7e14dcfSSatish Balay 
1153a7e14dcfSSatish Balay    Not Collective
1154a7e14dcfSSatish Balay 
1155a7e14dcfSSatish Balay    Input Parameter:
115665ba42b6SBarry Smith .  tao - a Tao optimization solver
1157a7e14dcfSSatish Balay 
1158a7e14dcfSSatish Balay    Output Parameter:
1159a7e14dcfSSatish Balay .  radius - the trust region radius
1160a7e14dcfSSatish Balay 
1161a7e14dcfSSatish Balay    Level: intermediate
1162a7e14dcfSSatish Balay 
116365ba42b6SBarry Smith .seealso: `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR`
1164a7e14dcfSSatish Balay @*/
1165441846f8SBarry Smith PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1166a7e14dcfSSatish Balay {
1167a7e14dcfSSatish Balay   PetscFunctionBegin;
1168441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
116994511df7SStefano Zampini   PetscValidRealPointer(radius,2);
1170a7e14dcfSSatish Balay   *radius = tao->trust0;
1171a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1172a7e14dcfSSatish Balay }
1173a7e14dcfSSatish Balay 
1174a7e14dcfSSatish Balay /*@
1175a7e14dcfSSatish Balay    TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1176a7e14dcfSSatish Balay 
1177a7e14dcfSSatish Balay    Not Collective
1178a7e14dcfSSatish Balay 
1179a7e14dcfSSatish Balay    Input Parameter:
118065ba42b6SBarry Smith .  tao - a Tao optimization solver
1181a7e14dcfSSatish Balay 
1182a7e14dcfSSatish Balay    Output Parameter:
1183a7e14dcfSSatish Balay .  radius - the trust region radius
1184a7e14dcfSSatish Balay 
1185a7e14dcfSSatish Balay    Level: intermediate
1186a7e14dcfSSatish Balay 
118765ba42b6SBarry Smith .seealso: `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR`
1188a7e14dcfSSatish Balay @*/
1189441846f8SBarry Smith PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1190a7e14dcfSSatish Balay {
1191a7e14dcfSSatish Balay   PetscFunctionBegin;
1192441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
119394511df7SStefano Zampini   PetscValidRealPointer(radius,2);
1194a7e14dcfSSatish Balay   *radius = tao->trust;
1195a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1196a7e14dcfSSatish Balay }
1197a7e14dcfSSatish Balay 
1198a7e14dcfSSatish Balay /*@
1199a7e14dcfSSatish Balay   TaoGetTolerances - gets the current values of tolerances
1200a7e14dcfSSatish Balay 
1201a7e14dcfSSatish Balay   Not Collective
1202a7e14dcfSSatish Balay 
1203f899ff85SJose E. Roman   Input Parameter:
1204441846f8SBarry Smith . tao - the Tao context
1205a7e14dcfSSatish Balay 
1206a7e14dcfSSatish Balay   Output Parameters:
1207e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this
1208a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
1209a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor
1210a7e14dcfSSatish Balay 
121147a47007SBarry Smith   Note: NULL can be used as an argument if not all tolerances values are needed
1212a7e14dcfSSatish Balay 
1213db781477SPatrick Sanan .seealso `TaoSetTolerances()`
1214a7e14dcfSSatish Balay 
1215a7e14dcfSSatish Balay   Level: intermediate
1216a7e14dcfSSatish Balay @*/
1217e52336cbSBarry Smith PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1218a7e14dcfSSatish Balay {
1219a7e14dcfSSatish Balay   PetscFunctionBegin;
1220441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1221a7e14dcfSSatish Balay   if (gatol) *gatol = tao->gatol;
1222a7e14dcfSSatish Balay   if (grtol) *grtol = tao->grtol;
1223a7e14dcfSSatish Balay   if (gttol) *gttol = tao->gttol;
1224a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1225a7e14dcfSSatish Balay }
1226a7e14dcfSSatish Balay 
1227a7e14dcfSSatish Balay /*@
1228a7e14dcfSSatish Balay   TaoGetKSP - Gets the linear solver used by the optimization solver.
122965ba42b6SBarry Smith   Application writers should use `TaoGetKSP()` if they need direct access
123065ba42b6SBarry Smith   to the PETSc `KSP` object.
1231a7e14dcfSSatish Balay 
1232a7e14dcfSSatish Balay   Not Collective
1233a7e14dcfSSatish Balay 
1234a7e14dcfSSatish Balay    Input Parameters:
123565ba42b6SBarry Smith .  tao - the Tao solver
1236a7e14dcfSSatish Balay 
1237a7e14dcfSSatish Balay    Output Parameters:
1238a7e14dcfSSatish Balay .  ksp - the KSP linear solver used in the optimization solver
1239a7e14dcfSSatish Balay 
1240a7e14dcfSSatish Balay    Level: intermediate
1241a7e14dcfSSatish Balay 
124265ba42b6SBarry Smith .seealso: `Tao`, `KSP`
1243a7e14dcfSSatish Balay @*/
1244441846f8SBarry Smith PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
124547a47007SBarry Smith {
1246a7e14dcfSSatish Balay   PetscFunctionBegin;
124794511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
124894511df7SStefano Zampini   PetscValidPointer(ksp,2);
1249a7e14dcfSSatish Balay   *ksp = tao->ksp;
1250a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1251a7e14dcfSSatish Balay }
1252a7e14dcfSSatish Balay 
1253025e9500SJason Sarich /*@
1254025e9500SJason Sarich    TaoGetLinearSolveIterations - Gets the total number of linear iterations
125565ba42b6SBarry Smith    used by the Tao solver
1256025e9500SJason Sarich 
1257025e9500SJason Sarich    Not Collective
1258025e9500SJason Sarich 
1259025e9500SJason Sarich    Input Parameter:
126065ba42b6SBarry Smith .  tao - Tao context
1261025e9500SJason Sarich 
1262025e9500SJason Sarich    Output Parameter:
1263025e9500SJason Sarich .  lits - number of linear iterations
1264025e9500SJason Sarich 
1265025e9500SJason Sarich    Notes:
1266025e9500SJason Sarich    This counter is reset to zero for each successive call to TaoSolve()
1267025e9500SJason Sarich 
1268025e9500SJason Sarich    Level: intermediate
1269025e9500SJason Sarich 
127065ba42b6SBarry Smith .seealso: `Tao`, `TaoGetKSP()`
1271025e9500SJason Sarich @*/
1272025e9500SJason Sarich PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits)
1273025e9500SJason Sarich {
1274025e9500SJason Sarich   PetscFunctionBegin;
1275025e9500SJason Sarich   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1276025e9500SJason Sarich   PetscValidIntPointer(lits,2);
12772d9aa51bSJason Sarich   *lits = tao->ksp_tot_its;
1278025e9500SJason Sarich   PetscFunctionReturn(0);
1279025e9500SJason Sarich }
1280025e9500SJason Sarich 
1281a7e14dcfSSatish Balay /*@
1282a7e14dcfSSatish Balay   TaoGetLineSearch - Gets the line search used by the optimization solver.
128365ba42b6SBarry Smith   Application writers should use `TaoGetLineSearch()` if they need direct access
1284a7e14dcfSSatish Balay   to the TaoLineSearch object.
1285a7e14dcfSSatish Balay 
1286a7e14dcfSSatish Balay   Not Collective
1287a7e14dcfSSatish Balay 
1288a7e14dcfSSatish Balay    Input Parameters:
128965ba42b6SBarry Smith .  tao - the Tao solver
1290a7e14dcfSSatish Balay 
1291a7e14dcfSSatish Balay    Output Parameters:
1292a7e14dcfSSatish Balay .  ls - the line search used in the optimization solver
1293a7e14dcfSSatish Balay 
1294a7e14dcfSSatish Balay    Level: intermediate
1295a7e14dcfSSatish Balay 
1296a7e14dcfSSatish Balay @*/
1297441846f8SBarry Smith PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
129847a47007SBarry Smith {
1299a7e14dcfSSatish Balay   PetscFunctionBegin;
130094511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
130194511df7SStefano Zampini   PetscValidPointer(ls,2);
1302a7e14dcfSSatish Balay   *ls = tao->linesearch;
1303a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1304a7e14dcfSSatish Balay }
1305a7e14dcfSSatish Balay 
1306a7e14dcfSSatish Balay /*@
1307a7e14dcfSSatish Balay   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1308a7e14dcfSSatish Balay   in the line search to the running total.
1309a7e14dcfSSatish Balay 
1310a7e14dcfSSatish Balay    Input Parameters:
131165ba42b6SBarry Smith +  tao - the Tao solver
1312a7e14dcfSSatish Balay -  ls - the line search used in the optimization solver
1313a7e14dcfSSatish Balay 
1314a7e14dcfSSatish Balay    Level: developer
1315a7e14dcfSSatish Balay 
131665ba42b6SBarry Smith .seealso: `TaoGetLineSearch()`, `TaoLineSearchApply()`
1317a7e14dcfSSatish Balay @*/
1318441846f8SBarry Smith PetscErrorCode TaoAddLineSearchCounts(Tao tao)
131947a47007SBarry Smith {
1320a7e14dcfSSatish Balay   PetscBool      flg;
1321a7e14dcfSSatish Balay   PetscInt       nfeval,ngeval,nfgeval;
132247a47007SBarry Smith 
1323a7e14dcfSSatish Balay   PetscFunctionBegin;
1324441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1325a7e14dcfSSatish Balay   if (tao->linesearch) {
13269566063dSJacob Faibussowitsch     PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch,&flg));
132794ae4db5SBarry Smith     if (!flg) {
13289566063dSJacob Faibussowitsch       PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch,&nfeval,&ngeval,&nfgeval));
1329a7e14dcfSSatish Balay       tao->nfuncs += nfeval;
1330a7e14dcfSSatish Balay       tao->ngrads += ngeval;
1331a7e14dcfSSatish Balay       tao->nfuncgrads += nfgeval;
1332a7e14dcfSSatish Balay     }
1333a7e14dcfSSatish Balay   }
1334a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1335a7e14dcfSSatish Balay }
1336a7e14dcfSSatish Balay 
1337a7e14dcfSSatish Balay /*@
133865ba42b6SBarry Smith   TaoGetSolution - Returns the vector with the current Tao solution
1339a7e14dcfSSatish Balay 
1340a7e14dcfSSatish Balay   Not Collective
1341a7e14dcfSSatish Balay 
1342a7e14dcfSSatish Balay   Input Parameter:
1343441846f8SBarry Smith . tao - the Tao context
1344a7e14dcfSSatish Balay 
1345a7e14dcfSSatish Balay   Output Parameter:
1346a7e14dcfSSatish Balay . X - the current solution
1347a7e14dcfSSatish Balay 
1348a7e14dcfSSatish Balay   Level: intermediate
1349a7e14dcfSSatish Balay 
135065ba42b6SBarry Smith   Note:
135165ba42b6SBarry Smith   The returned vector will be the same object that was passed into `TaoSetSolution()`
135265ba42b6SBarry Smith 
135365ba42b6SBarry Smith .seealso: `Tao`, `TaoSetSolution()`, `TaoSolve()`
1354a7e14dcfSSatish Balay @*/
1355a82e8c82SStefano Zampini PetscErrorCode TaoGetSolution(Tao tao, Vec *X)
1356a7e14dcfSSatish Balay {
1357a7e14dcfSSatish Balay   PetscFunctionBegin;
1358441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
135994511df7SStefano Zampini   PetscValidPointer(X,2);
1360a7e14dcfSSatish Balay   *X = tao->solution;
1361a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1362a7e14dcfSSatish Balay }
1363a7e14dcfSSatish Balay 
1364a7e14dcfSSatish Balay /*@
136565ba42b6SBarry Smith    TaoResetStatistics - Initialize the statistics used by Tao for all of the solvers.
1366a7e14dcfSSatish Balay    These statistics include the iteration number, residual norms, and convergence status.
1367a7e14dcfSSatish Balay    This routine gets called before solving each optimization problem.
1368a7e14dcfSSatish Balay 
136965ba42b6SBarry Smith    Collective on tao
1370a7e14dcfSSatish Balay 
1371a7e14dcfSSatish Balay    Input Parameters:
1372441846f8SBarry Smith .  solver - the Tao context
1373a7e14dcfSSatish Balay 
1374a7e14dcfSSatish Balay    Level: developer
1375a7e14dcfSSatish Balay 
137665ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoSolve()`
1377a7e14dcfSSatish Balay @*/
1378441846f8SBarry Smith PetscErrorCode TaoResetStatistics(Tao tao)
1379a7e14dcfSSatish Balay {
1380a7e14dcfSSatish Balay   PetscFunctionBegin;
1381441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1382a7e14dcfSSatish Balay   tao->niter        = 0;
1383a7e14dcfSSatish Balay   tao->nfuncs       = 0;
1384a7e14dcfSSatish Balay   tao->nfuncgrads   = 0;
1385a7e14dcfSSatish Balay   tao->ngrads       = 0;
1386a7e14dcfSSatish Balay   tao->nhess        = 0;
1387a7e14dcfSSatish Balay   tao->njac         = 0;
1388a7e14dcfSSatish Balay   tao->nconstraints = 0;
1389a7e14dcfSSatish Balay   tao->ksp_its      = 0;
1390ae93cb3cSJason Sarich   tao->ksp_tot_its  = 0;
1391a7e14dcfSSatish Balay   tao->reason       = TAO_CONTINUE_ITERATING;
1392a7e14dcfSSatish Balay   tao->residual     = 0.0;
1393a7e14dcfSSatish Balay   tao->cnorm        = 0.0;
1394a7e14dcfSSatish Balay   tao->step         = 0.0;
1395a7e14dcfSSatish Balay   tao->lsflag       = PETSC_FALSE;
1396a7e14dcfSSatish Balay   if (tao->hist_reset) tao->hist_len = 0;
1397a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1398a7e14dcfSSatish Balay }
1399a7e14dcfSSatish Balay 
1400a7e14dcfSSatish Balay /*@C
1401e1e80dc8SAlp Dener   TaoSetUpdate - Sets the general-purpose update function called
140265ba42b6SBarry Smith   at the beginning of every iteration of the optimization algorithm. Specifically
1403e1e80dc8SAlp Dener   it is called at the top of every iteration, after the new solution and the gradient
1404e1e80dc8SAlp Dener   is determined, but before the Hessian is computed (if applicable).
1405e1e80dc8SAlp Dener 
140665ba42b6SBarry Smith   Logically Collective on tao
1407e1e80dc8SAlp Dener 
1408e1e80dc8SAlp Dener   Input Parameters:
1409a2b725a8SWilliam Gropp + tao - The tao solver context
1410a2b725a8SWilliam Gropp - func - The function
1411e1e80dc8SAlp Dener 
1412e1e80dc8SAlp Dener   Calling sequence of func:
1413a2b725a8SWilliam Gropp $ func (Tao tao, PetscInt step);
1414e1e80dc8SAlp Dener 
1415e1e80dc8SAlp Dener . step - The current step of the iteration
1416e1e80dc8SAlp Dener 
1417e1e80dc8SAlp Dener   Level: advanced
1418e1e80dc8SAlp Dener 
141965ba42b6SBarry Smith .seealso `Tao`, `TaoSolve()`
1420e1e80dc8SAlp Dener @*/
14218fcddce6SStefano Zampini PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao, PetscInt, void*), void *ctx)
1422e1e80dc8SAlp Dener {
1423e1e80dc8SAlp Dener   PetscFunctionBegin;
1424e1e80dc8SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID,1);
1425e1e80dc8SAlp Dener   tao->ops->update = func;
1426e1e80dc8SAlp Dener   tao->user_update = ctx;
1427e1e80dc8SAlp Dener   PetscFunctionReturn(0);
1428e1e80dc8SAlp Dener }
1429e1e80dc8SAlp Dener 
1430e1e80dc8SAlp Dener /*@C
1431a7e14dcfSSatish Balay   TaoSetConvergenceTest - Sets the function that is to be used to test
1432a7e14dcfSSatish Balay   for convergence o fthe iterative minimization solution.  The new convergence
143365ba42b6SBarry Smith   testing routine will replace Tao's default convergence test.
1434a7e14dcfSSatish Balay 
143565ba42b6SBarry Smith   Logically Collective on tao
1436a7e14dcfSSatish Balay 
1437a7e14dcfSSatish Balay   Input Parameters:
1438441846f8SBarry Smith + tao - the Tao object
1439a7e14dcfSSatish Balay . conv - the routine to test for convergence
1440a7e14dcfSSatish Balay - ctx - [optional] context for private data for the convergence routine
144147a47007SBarry Smith         (may be NULL)
1442a7e14dcfSSatish Balay 
1443a7e14dcfSSatish Balay   Calling sequence of conv:
1444441846f8SBarry Smith $   PetscErrorCode conv(Tao tao, void *ctx)
1445a7e14dcfSSatish Balay 
1446441846f8SBarry Smith + tao - the Tao object
1447a7e14dcfSSatish Balay - ctx - [optional] convergence context
1448a7e14dcfSSatish Balay 
144965ba42b6SBarry Smith   Note:
145065ba42b6SBarry Smith   The new convergence testing routine should call `TaoSetConvergedReason()`.
1451a7e14dcfSSatish Balay 
1452a7e14dcfSSatish Balay   Level: advanced
1453a7e14dcfSSatish Balay 
145465ba42b6SBarry Smith .seealso: `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoSetMonitor`
1455a7e14dcfSSatish Balay 
1456a7e14dcfSSatish Balay @*/
1457441846f8SBarry Smith PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void*), void *ctx)
1458a7e14dcfSSatish Balay {
1459a7e14dcfSSatish Balay   PetscFunctionBegin;
1460441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
146194511df7SStefano Zampini   tao->ops->convergencetest = conv;
146294511df7SStefano Zampini   tao->cnvP = ctx;
1463a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1464a7e14dcfSSatish Balay }
1465a7e14dcfSSatish Balay 
1466a7e14dcfSSatish Balay /*@C
146765ba42b6SBarry Smith    TaoSetMonitor - Sets an additional function that is to be used at every
1468a7e14dcfSSatish Balay    iteration of the solver to display the iteration's
1469a7e14dcfSSatish Balay    progress.
1470a7e14dcfSSatish Balay 
147165ba42b6SBarry Smith    Logically Collective on tao
1472a7e14dcfSSatish Balay 
1473a7e14dcfSSatish Balay    Input Parameters:
1474441846f8SBarry Smith +  tao - the Tao solver context
1475a7e14dcfSSatish Balay .  mymonitor - monitoring routine
1476a7e14dcfSSatish Balay -  mctx - [optional] user-defined context for private data for the
147747a47007SBarry Smith           monitor routine (may be NULL)
1478a7e14dcfSSatish Balay 
1479a7e14dcfSSatish Balay    Calling sequence of mymonitor:
1480147403d9SBarry Smith .vb
1481147403d9SBarry Smith      PetscErrorCode mymonitor(Tao tao,void *mctx)
1482147403d9SBarry Smith .ve
1483a7e14dcfSSatish Balay 
1484441846f8SBarry Smith +    tao - the Tao solver context
1485a7e14dcfSSatish Balay -    mctx - [optional] monitoring context
1486a7e14dcfSSatish Balay 
1487a7e14dcfSSatish Balay    Options Database Keys:
148865ba42b6SBarry Smith +    -tao_monitor        - sets the default monitor `TaoMonitorDefault()`
1489a7e14dcfSSatish Balay .    -tao_smonitor       - sets short monitor
1490a7e14dcfSSatish Balay .    -tao_cmonitor       - same as smonitor plus constraint norm
1491a7e14dcfSSatish Balay .    -tao_view_solution   - view solution at each iteration
1492a7e14dcfSSatish Balay .    -tao_view_gradient   - view gradient at each iteration
14934a48860cSAlp Dener .    -tao_view_ls_residual - view least-squares residual vector at each iteration
1494a7e14dcfSSatish Balay -    -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.
1495a7e14dcfSSatish Balay 
149665ba42b6SBarry Smith    Notes
1497a7e14dcfSSatish Balay    Several different monitoring routines may be set by calling
149865ba42b6SBarry Smith    `TaoSetMonitor()` multiple times; all will be called in the
1499a7e14dcfSSatish Balay    order in which they were set.
1500a7e14dcfSSatish Balay 
150165ba42b6SBarry Smith    Fortran Note:
150295452b02SPatrick Sanan     Only one monitor function may be set
1503a7e14dcfSSatish Balay 
1504a7e14dcfSSatish Balay    Level: intermediate
1505a7e14dcfSSatish Balay 
150665ba42b6SBarry Smith .seealso: `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoCancelMonitors()`, `TaoSetDestroyRoutine()`, `TaoView()`
1507a7e14dcfSSatish Balay @*/
1508441846f8SBarry Smith PetscErrorCode TaoSetMonitor(Tao tao, PetscErrorCode (*func)(Tao, void*), void *ctx,PetscErrorCode (*dest)(void**))
1509a7e14dcfSSatish Balay {
151008d19d1fSJason Sarich   PetscInt       i;
15118732526dSAlp Dener   PetscBool      identical;
151208d19d1fSJason Sarich 
1513a7e14dcfSSatish Balay   PetscFunctionBegin;
1514441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
15153c859ba3SBarry Smith   PetscCheck(tao->numbermonitors < MAXTAOMONITORS,PetscObjectComm((PetscObject)tao),PETSC_ERR_SUP,"Cannot attach another monitor -- max=%d",MAXTAOMONITORS);
151608d19d1fSJason Sarich 
151708d19d1fSJason Sarich   for (i=0; i<tao->numbermonitors;i++) {
15189566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))func,ctx,dest,(PetscErrorCode (*)(void))tao->monitor[i],tao->monitorcontext[i],tao->monitordestroy[i],&identical));
15198732526dSAlp Dener     if (identical) PetscFunctionReturn(0);
152008d19d1fSJason Sarich   }
1521a7e14dcfSSatish Balay   tao->monitor[tao->numbermonitors] = func;
15228732526dSAlp Dener   tao->monitorcontext[tao->numbermonitors] = (void*)ctx;
1523a7e14dcfSSatish Balay   tao->monitordestroy[tao->numbermonitors] = dest;
1524a7e14dcfSSatish Balay   ++tao->numbermonitors;
1525a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1526a7e14dcfSSatish Balay }
1527a7e14dcfSSatish Balay 
1528a7e14dcfSSatish Balay /*@
1529441846f8SBarry Smith    TaoCancelMonitors - Clears all the monitor functions for a Tao object.
1530a7e14dcfSSatish Balay 
153165ba42b6SBarry Smith    Logically Collective on tao
1532a7e14dcfSSatish Balay 
1533a7e14dcfSSatish Balay    Input Parameters:
1534441846f8SBarry Smith .  tao - the Tao solver context
1535a7e14dcfSSatish Balay 
1536a7e14dcfSSatish Balay    Options Database:
1537a7e14dcfSSatish Balay .  -tao_cancelmonitors - cancels all monitors that have been hardwired
153865ba42b6SBarry Smith     into a code by calls to `TaoSetMonitor()`, but does not cancel those
1539a7e14dcfSSatish Balay     set via the options database
1540a7e14dcfSSatish Balay 
1541a7e14dcfSSatish Balay    Notes:
1542441846f8SBarry Smith    There is no way to clear one specific monitor from a Tao object.
1543a7e14dcfSSatish Balay 
1544a7e14dcfSSatish Balay    Level: advanced
1545a7e14dcfSSatish Balay 
154665ba42b6SBarry Smith .seealso: `Tao`, `TaoMonitorDefault()`, `TaoSetMonitor()`
1547a7e14dcfSSatish Balay @*/
1548441846f8SBarry Smith PetscErrorCode TaoCancelMonitors(Tao tao)
1549a7e14dcfSSatish Balay {
1550a7e14dcfSSatish Balay   PetscInt       i;
155147a47007SBarry Smith 
1552a7e14dcfSSatish Balay   PetscFunctionBegin;
1553441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1554a7e14dcfSSatish Balay   for (i=0;i<tao->numbermonitors;i++) {
1555a7e14dcfSSatish Balay     if (tao->monitordestroy[i]) {
15569566063dSJacob Faibussowitsch       PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i]));
1557a7e14dcfSSatish Balay     }
1558a7e14dcfSSatish Balay   }
1559a7e14dcfSSatish Balay   tao->numbermonitors = 0;
1560a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1561a7e14dcfSSatish Balay }
1562a7e14dcfSSatish Balay 
15637fab98ebSJason Sarich /*@
156498ea980cSBarry Smith    TaoMonitorDefault - Default routine for monitoring progress of the
1565441846f8SBarry Smith    Tao solvers (default).  This monitor prints the function value and gradient
1566a7e14dcfSSatish Balay    norm at each iteration.  It can be turned on from the command line using the
1567a7e14dcfSSatish Balay    -tao_monitor option
1568a7e14dcfSSatish Balay 
156965ba42b6SBarry Smith    Collective on tao
1570a7e14dcfSSatish Balay 
1571a7e14dcfSSatish Balay    Input Parameters:
1572441846f8SBarry Smith +  tao - the Tao context
157365ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1574a7e14dcfSSatish Balay 
1575a7e14dcfSSatish Balay    Options Database Keys:
1576147403d9SBarry Smith .  -tao_monitor - turn on default monitoring
1577a7e14dcfSSatish Balay 
1578a7e14dcfSSatish Balay    Level: advanced
1579a7e14dcfSSatish Balay 
1580db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
1581a7e14dcfSSatish Balay @*/
158298ea980cSBarry Smith PetscErrorCode TaoMonitorDefault(Tao tao, void *ctx)
1583a7e14dcfSSatish Balay {
158463b15415SAlp Dener   PetscInt       its, tabs;
1585a7e14dcfSSatish Balay   PetscReal      fct,gnorm;
15868163d661SBarry Smith   PetscViewer    viewer = (PetscViewer)ctx;
158747a47007SBarry Smith 
1588a7e14dcfSSatish Balay   PetscFunctionBegin;
158994511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
15908163d661SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1591a7e14dcfSSatish Balay   its = tao->niter;
1592a7e14dcfSSatish Balay   fct = tao->fc;
1593a7e14dcfSSatish Balay   gnorm = tao->residual;
15949566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
15959566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1596494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
15979566063dSJacob Faibussowitsch      PetscCall(PetscViewerASCIIPrintf(viewer,"  Iteration information for %s solve.\n",((PetscObject)tao)->prefix));
1598494bef23SAlp Dener      tao->header_printed = PETSC_TRUE;
159963b15415SAlp Dener    }
160063a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " TAO,",its));
16019566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"  Function value: %g,",(double)fct));
160208d19d1fSJason Sarich   if (gnorm >= PETSC_INFINITY) {
16039566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  Residual: Inf \n"));
160408d19d1fSJason Sarich   } else {
16059566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  Residual: %g \n",(double)gnorm));
160608d19d1fSJason Sarich   }
16079566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1608a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1609a7e14dcfSSatish Balay }
1610a7e14dcfSSatish Balay 
16117fab98ebSJason Sarich /*@
16128d5ead36SAlp Dener    TaoDefaultGMonitor - Default routine for monitoring progress of the
16138d5ead36SAlp Dener    Tao solvers (default) with extra detail on the globalization method.
16148d5ead36SAlp Dener    This monitor prints the function value and gradient norm at each
16158d5ead36SAlp Dener    iteration, as well as the step size and trust radius. Note that the
16168d5ead36SAlp Dener    step size and trust radius may be the same for some algorithms.
16178d5ead36SAlp Dener    It can be turned on from the command line using the
16188d5ead36SAlp Dener    -tao_gmonitor option
16198d5ead36SAlp Dener 
162065ba42b6SBarry Smith    Collective on tao
16218d5ead36SAlp Dener 
16228d5ead36SAlp Dener    Input Parameters:
16238d5ead36SAlp Dener +  tao - the Tao context
162465ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
16258d5ead36SAlp Dener 
16268d5ead36SAlp Dener    Options Database Keys:
1627147403d9SBarry Smith .  -tao_gmonitor - turn on monitoring with globalization information
16288d5ead36SAlp Dener 
16298d5ead36SAlp Dener    Level: advanced
16308d5ead36SAlp Dener 
1631db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
16328d5ead36SAlp Dener @*/
16338d5ead36SAlp Dener PetscErrorCode TaoDefaultGMonitor(Tao tao, void *ctx)
16348d5ead36SAlp Dener {
16358d5ead36SAlp Dener   PetscInt       its, tabs;
16368d5ead36SAlp Dener   PetscReal      fct,gnorm,stp,tr;
16378d5ead36SAlp Dener   PetscViewer    viewer = (PetscViewer)ctx;
16388d5ead36SAlp Dener 
16398d5ead36SAlp Dener   PetscFunctionBegin;
164094511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
16418d5ead36SAlp Dener   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
16428d5ead36SAlp Dener   its = tao->niter;
16438d5ead36SAlp Dener   fct = tao->fc;
16448d5ead36SAlp Dener   gnorm = tao->residual;
16458d5ead36SAlp Dener   stp = tao->step;
16468d5ead36SAlp Dener   tr = tao->trust;
16479566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
16489566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1649494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
16509566063dSJacob Faibussowitsch      PetscCall(PetscViewerASCIIPrintf(viewer,"  Iteration information for %s solve.\n",((PetscObject)tao)->prefix));
1651494bef23SAlp Dener      tao->header_printed = PETSC_TRUE;
16528d5ead36SAlp Dener    }
165363a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " TAO,",its));
16549566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"  Function value: %g,",(double)fct));
16558d5ead36SAlp Dener   if (gnorm >= PETSC_INFINITY) {
16569566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  Residual: Inf,"));
16578d5ead36SAlp Dener   } else {
16589566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  Residual: %g,",(double)gnorm));
16598d5ead36SAlp Dener   }
16609566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"  Step: %g,  Trust: %g\n",(double)stp,(double)tr));
16619566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
16628d5ead36SAlp Dener   PetscFunctionReturn(0);
16638d5ead36SAlp Dener }
16648d5ead36SAlp Dener 
16658d5ead36SAlp Dener /*@
1666a7e14dcfSSatish Balay    TaoDefaultSMonitor - Default routine for monitoring progress of the
166765ba42b6SBarry Smith    solver. Same as `TaoMonitorDefault()` except
1668a7e14dcfSSatish Balay    it prints fewer digits of the residual as the residual gets smaller.
1669a7e14dcfSSatish Balay    This is because the later digits are meaningless and are often
1670a7e14dcfSSatish Balay    different on different machines; by using this routine different
1671a7e14dcfSSatish Balay    machines will usually generate the same output. It can be turned on
1672a7e14dcfSSatish Balay    by using the -tao_smonitor option
1673a7e14dcfSSatish Balay 
167465ba42b6SBarry Smith    Collective on tao
1675a7e14dcfSSatish Balay 
1676a7e14dcfSSatish Balay    Input Parameters:
1677441846f8SBarry Smith +  tao - the Tao context
16784d4332d5SBarry Smith -  ctx - PetscViewer context of type ASCII
1679a7e14dcfSSatish Balay 
1680a7e14dcfSSatish Balay    Options Database Keys:
1681147403d9SBarry Smith .  -tao_smonitor - turn on default short monitoring
1682a7e14dcfSSatish Balay 
1683a7e14dcfSSatish Balay    Level: advanced
1684a7e14dcfSSatish Balay 
1685db781477SPatrick Sanan .seealso: `TaoMonitorDefault()`, `TaoSetMonitor()`
1686a7e14dcfSSatish Balay @*/
1687441846f8SBarry Smith PetscErrorCode TaoDefaultSMonitor(Tao tao, void *ctx)
1688a7e14dcfSSatish Balay {
16892c9b8b83SAlp Dener   PetscInt       its, tabs;
1690a7e14dcfSSatish Balay   PetscReal      fct,gnorm;
16914d4332d5SBarry Smith   PetscViewer    viewer = (PetscViewer)ctx;
169247a47007SBarry Smith 
1693a7e14dcfSSatish Balay   PetscFunctionBegin;
169494511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
16954d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1696a7e14dcfSSatish Balay   its = tao->niter;
1697a7e14dcfSSatish Balay   fct = tao->fc;
1698a7e14dcfSSatish Balay   gnorm = tao->residual;
16999566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
17009566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
170163a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"iter = %3" PetscInt_FMT ",",its));
17029566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer," Function value %g,",(double)fct));
1703d393f493SBarry Smith   if (gnorm >= PETSC_INFINITY) {
17049566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," Residual: Inf \n"));
170508d19d1fSJason Sarich   } else if (gnorm > 1.e-6) {
17069566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm));
1707a7e14dcfSSatish Balay   } else if (gnorm > 1.e-11) {
17089566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-6 \n"));
1709a7e14dcfSSatish Balay   } else {
17109566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-11 \n"));
1711a7e14dcfSSatish Balay   }
17129566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1713a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1714a7e14dcfSSatish Balay }
1715a7e14dcfSSatish Balay 
17167fab98ebSJason Sarich /*@
171765ba42b6SBarry Smith    TaoDefaultCMonitor - same as `TaoMonitorDefault()` except
1718a7e14dcfSSatish Balay    it prints the norm of the constraints function. It can be turned on
1719a7e14dcfSSatish Balay    from the command line using the -tao_cmonitor option
1720a7e14dcfSSatish Balay 
172165ba42b6SBarry Smith    Collective on tao
1722a7e14dcfSSatish Balay 
1723a7e14dcfSSatish Balay    Input Parameters:
1724441846f8SBarry Smith +  tao - the Tao context
172565ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1726a7e14dcfSSatish Balay 
1727a7e14dcfSSatish Balay    Options Database Keys:
1728147403d9SBarry Smith .  -tao_cmonitor - monitor the constraints
1729a7e14dcfSSatish Balay 
1730a7e14dcfSSatish Balay    Level: advanced
1731a7e14dcfSSatish Balay 
1732db781477SPatrick Sanan .seealso: `TaoMonitorDefault()`, `TaoSetMonitor()`
1733a7e14dcfSSatish Balay @*/
1734441846f8SBarry Smith PetscErrorCode TaoDefaultCMonitor(Tao tao, void *ctx)
1735a7e14dcfSSatish Balay {
17362c9b8b83SAlp Dener   PetscInt       its, tabs;
1737a7e14dcfSSatish Balay   PetscReal      fct,gnorm;
1738d393f493SBarry Smith   PetscViewer    viewer = (PetscViewer)ctx;
1739a7e14dcfSSatish Balay 
1740a7e14dcfSSatish Balay   PetscFunctionBegin;
174194511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1742d393f493SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1743a7e14dcfSSatish Balay   its = tao->niter;
1744a7e14dcfSSatish Balay   fct = tao->fc;
1745a7e14dcfSSatish Balay   gnorm = tao->residual;
17469566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
17479566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
174863a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"iter = %" PetscInt_FMT ",",its));
17499566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct));
17509566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"  Residual: %g ",(double)gnorm));
17519566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"  Constraint: %g \n",(double)tao->cnorm));
17529566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1753a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1754a7e14dcfSSatish Balay }
1755a7e14dcfSSatish Balay 
1756a7e14dcfSSatish Balay /*@C
1757a7e14dcfSSatish Balay    TaoSolutionMonitor - Views the solution at each iteration
1758a7e14dcfSSatish Balay    It can be turned on from the command line using the
1759a7e14dcfSSatish Balay    -tao_view_solution option
1760a7e14dcfSSatish Balay 
176165ba42b6SBarry Smith    Collective on tao
1762a7e14dcfSSatish Balay 
1763a7e14dcfSSatish Balay    Input Parameters:
1764441846f8SBarry Smith +  tao - the Tao context
176565ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1766a7e14dcfSSatish Balay 
1767a7e14dcfSSatish Balay    Options Database Keys:
1768147403d9SBarry Smith .  -tao_view_solution - view the solution
1769a7e14dcfSSatish Balay 
1770a7e14dcfSSatish Balay    Level: advanced
1771a7e14dcfSSatish Balay 
1772db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
1773a7e14dcfSSatish Balay @*/
1774441846f8SBarry Smith PetscErrorCode TaoSolutionMonitor(Tao tao, void *ctx)
1775a7e14dcfSSatish Balay {
1776feb237baSPierre Jolivet   PetscViewer    viewer  = (PetscViewer)ctx;
177747a47007SBarry Smith 
1778a7e14dcfSSatish Balay   PetscFunctionBegin;
177994511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1780d393f493SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
17819566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution,viewer));
1782a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1783a7e14dcfSSatish Balay }
1784a7e14dcfSSatish Balay 
1785a7e14dcfSSatish Balay /*@C
1786a7e14dcfSSatish Balay    TaoGradientMonitor - Views the gradient at each iteration
1787a7e14dcfSSatish Balay    It can be turned on from the command line using the
1788a7e14dcfSSatish Balay    -tao_view_gradient option
1789a7e14dcfSSatish Balay 
179065ba42b6SBarry Smith    Collective on tao
1791a7e14dcfSSatish Balay 
1792a7e14dcfSSatish Balay    Input Parameters:
1793441846f8SBarry Smith +  tao - the Tao context
179465ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1795a7e14dcfSSatish Balay 
1796a7e14dcfSSatish Balay    Options Database Keys:
1797147403d9SBarry Smith .  -tao_view_gradient - view the gradient at each iteration
1798a7e14dcfSSatish Balay 
1799a7e14dcfSSatish Balay    Level: advanced
1800a7e14dcfSSatish Balay 
1801db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
1802a7e14dcfSSatish Balay @*/
1803441846f8SBarry Smith PetscErrorCode TaoGradientMonitor(Tao tao, void *ctx)
1804a7e14dcfSSatish Balay {
1805d393f493SBarry Smith   PetscViewer    viewer = (PetscViewer)ctx;
180647a47007SBarry Smith 
1807a7e14dcfSSatish Balay   PetscFunctionBegin;
180894511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1809d393f493SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
18109566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient,viewer));
1811a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1812a7e14dcfSSatish Balay }
1813a7e14dcfSSatish Balay 
1814a7e14dcfSSatish Balay /*@C
1815147403d9SBarry Smith    TaoStepDirectionMonitor - Views the step-direction at each iteration
1816a7e14dcfSSatish Balay 
181765ba42b6SBarry Smith    Collective on tao
1818a7e14dcfSSatish Balay 
1819a7e14dcfSSatish Balay    Input Parameters:
1820441846f8SBarry Smith +  tao - the Tao context
182165ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1822a7e14dcfSSatish Balay 
1823a7e14dcfSSatish Balay    Options Database Keys:
1824147403d9SBarry Smith .  -tao_view_gradient - view the gradient at each iteration
1825a7e14dcfSSatish Balay 
1826a7e14dcfSSatish Balay    Level: advanced
1827a7e14dcfSSatish Balay 
1828db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
1829a7e14dcfSSatish Balay @*/
1830441846f8SBarry Smith PetscErrorCode TaoStepDirectionMonitor(Tao tao, void *ctx)
1831a7e14dcfSSatish Balay {
1832d393f493SBarry Smith   PetscViewer    viewer = (PetscViewer)ctx;
1833d393f493SBarry Smith 
1834a7e14dcfSSatish Balay   PetscFunctionBegin;
183594511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1836d393f493SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
18379566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection,viewer));
1838a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1839a7e14dcfSSatish Balay }
1840a7e14dcfSSatish Balay 
1841a7e14dcfSSatish Balay /*@C
1842a7e14dcfSSatish Balay    TaoDrawSolutionMonitor - Plots the solution at each iteration
1843a7e14dcfSSatish Balay    It can be turned on from the command line using the
1844a7e14dcfSSatish Balay    -tao_draw_solution option
1845a7e14dcfSSatish Balay 
184665ba42b6SBarry Smith    Collective on tao
1847a7e14dcfSSatish Balay 
1848a7e14dcfSSatish Balay    Input Parameters:
1849441846f8SBarry Smith +  tao - the Tao context
185065ba42b6SBarry Smith -  ctx - `TaoMonitorDraw` context
1851a7e14dcfSSatish Balay 
1852a7e14dcfSSatish Balay    Options Database Keys:
1853147403d9SBarry Smith .  -tao_draw_solution - draw the solution at each iteration
1854a7e14dcfSSatish Balay 
1855a7e14dcfSSatish Balay    Level: advanced
1856a7e14dcfSSatish Balay 
185765ba42b6SBarry Smith .seealso: `TaoSolutionMonitor()`, `TaoSetMonitor()`, `TaoDrawGradientMonitor`, `TaoMonitorDraw`
1858a7e14dcfSSatish Balay @*/
1859441846f8SBarry Smith PetscErrorCode TaoDrawSolutionMonitor(Tao tao, void *ctx)
1860a7e14dcfSSatish Balay {
1861e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
186247a47007SBarry Smith 
1863a7e14dcfSSatish Balay   PetscFunctionBegin;
186494511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1865e882e171SHong Zhang   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(0);
18669566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution,ictx->viewer));
1867a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1868a7e14dcfSSatish Balay }
1869a7e14dcfSSatish Balay 
1870a7e14dcfSSatish Balay /*@C
1871a7e14dcfSSatish Balay    TaoDrawGradientMonitor - Plots the gradient at each iteration
1872a7e14dcfSSatish Balay    It can be turned on from the command line using the
1873a7e14dcfSSatish Balay    -tao_draw_gradient option
1874a7e14dcfSSatish Balay 
187565ba42b6SBarry Smith    Collective on tao
1876a7e14dcfSSatish Balay 
1877a7e14dcfSSatish Balay    Input Parameters:
1878441846f8SBarry Smith +  tao - the Tao context
187965ba42b6SBarry Smith -  ctx - `PetscViewer` context
1880a7e14dcfSSatish Balay 
1881a7e14dcfSSatish Balay    Options Database Keys:
1882147403d9SBarry Smith .  -tao_draw_gradient - draw the gradient at each iteration
1883a7e14dcfSSatish Balay 
1884a7e14dcfSSatish Balay    Level: advanced
1885a7e14dcfSSatish Balay 
1886db781477SPatrick Sanan .seealso: `TaoGradientMonitor()`, `TaoSetMonitor()`, `TaoDrawSolutionMonitor`
1887a7e14dcfSSatish Balay @*/
1888441846f8SBarry Smith PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx)
1889a7e14dcfSSatish Balay {
1890e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
189147a47007SBarry Smith 
1892a7e14dcfSSatish Balay   PetscFunctionBegin;
189394511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1894e882e171SHong Zhang   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(0);
18959566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient,ictx->viewer));
1896a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1897a7e14dcfSSatish Balay }
1898a7e14dcfSSatish Balay 
1899a7e14dcfSSatish Balay /*@C
1900a7e14dcfSSatish Balay    TaoDrawStepMonitor - Plots the step direction at each iteration
1901a7e14dcfSSatish Balay 
190265ba42b6SBarry Smith    Collective on tao
1903a7e14dcfSSatish Balay 
1904a7e14dcfSSatish Balay    Input Parameters:
1905441846f8SBarry Smith +  tao - the Tao context
1906f55353a2SBarry Smith -  ctx - PetscViewer context
1907a7e14dcfSSatish Balay 
1908a7e14dcfSSatish Balay    Options Database Keys:
1909147403d9SBarry Smith .  -tao_draw_step - draw the step direction at each iteration
1910a7e14dcfSSatish Balay 
1911a7e14dcfSSatish Balay    Level: advanced
1912a7e14dcfSSatish Balay 
1913db781477SPatrick Sanan .seealso: `TaoSetMonitor()`, `TaoDrawSolutionMonitor`
1914a7e14dcfSSatish Balay @*/
1915441846f8SBarry Smith PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx)
1916a7e14dcfSSatish Balay {
191794511df7SStefano Zampini   PetscViewer    viewer = (PetscViewer)ctx;
191847a47007SBarry Smith 
1919a7e14dcfSSatish Balay   PetscFunctionBegin;
192094511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
192194511df7SStefano Zampini   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
19229566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection,viewer));
1923a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1924a7e14dcfSSatish Balay }
1925a7e14dcfSSatish Balay 
1926a7e14dcfSSatish Balay /*@C
1927737f463aSAlp Dener    TaoResidualMonitor - Views the least-squares residual at each iteration
1928a7e14dcfSSatish Balay 
192965ba42b6SBarry Smith    Collective on tao
1930a7e14dcfSSatish Balay 
1931a7e14dcfSSatish Balay    Input Parameters:
1932441846f8SBarry Smith +  tao - the Tao context
193365ba42b6SBarry Smith -  ctx - `PetscViewer` context or NULL
1934a7e14dcfSSatish Balay 
1935a7e14dcfSSatish Balay    Options Database Keys:
1936147403d9SBarry Smith .  -tao_view_ls_residual - view the least-squares residual at each iteration
1937a7e14dcfSSatish Balay 
1938a7e14dcfSSatish Balay    Level: advanced
1939a7e14dcfSSatish Balay 
1940db781477SPatrick Sanan .seealso: `TaoDefaultSMonitor()`, `TaoSetMonitor()`
1941a7e14dcfSSatish Balay @*/
1942737f463aSAlp Dener PetscErrorCode TaoResidualMonitor(Tao tao, void *ctx)
1943a7e14dcfSSatish Balay {
1944d393f493SBarry Smith   PetscViewer    viewer  = (PetscViewer)ctx;
194547a47007SBarry Smith 
1946a7e14dcfSSatish Balay   PetscFunctionBegin;
194794511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
1948d393f493SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
19499566063dSJacob Faibussowitsch   PetscCall(VecView(tao->ls_res,viewer));
1950a7e14dcfSSatish Balay   PetscFunctionReturn(0);
1951a7e14dcfSSatish Balay }
1952a7e14dcfSSatish Balay 
19537fab98ebSJason Sarich /*@
1954a7e14dcfSSatish Balay    TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1955a7e14dcfSSatish Balay    or terminate.
1956a7e14dcfSSatish Balay 
195765ba42b6SBarry Smith    Collective on tao
1958a7e14dcfSSatish Balay 
1959a7e14dcfSSatish Balay    Input Parameters:
1960441846f8SBarry Smith +  tao - the Tao context
1961a7e14dcfSSatish Balay -  dummy - unused dummy context
1962a7e14dcfSSatish Balay 
1963a7e14dcfSSatish Balay    Output Parameter:
1964a7e14dcfSSatish Balay .  reason - for terminating
1965a7e14dcfSSatish Balay 
1966a7e14dcfSSatish Balay    Notes:
1967a7e14dcfSSatish Balay    This routine checks the residual in the optimality conditions, the
1968a7e14dcfSSatish Balay    relative residual in the optimity conditions, the number of function
1969a7e14dcfSSatish Balay    evaluations, and the function value to test convergence.  Some
1970a7e14dcfSSatish Balay    solvers may use different convergence routines.
1971a7e14dcfSSatish Balay 
1972a7e14dcfSSatish Balay    Level: developer
1973a7e14dcfSSatish Balay 
1974c2e3fba1SPatrick Sanan .seealso: `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()`
1975a7e14dcfSSatish Balay @*/
1976a7e14dcfSSatish Balay 
1977441846f8SBarry Smith PetscErrorCode TaoDefaultConvergenceTest(Tao tao,void *dummy)
1978a7e14dcfSSatish Balay {
1979a7e14dcfSSatish Balay   PetscInt           niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads);
1980a7e14dcfSSatish Balay   PetscInt           max_funcs=tao->max_funcs;
1981a7e14dcfSSatish Balay   PetscReal          gnorm=tao->residual, gnorm0=tao->gnorm0;
1982a7e14dcfSSatish Balay   PetscReal          f=tao->fc, steptol=tao->steptol,trradius=tao->step;
1983a7e14dcfSSatish Balay   PetscReal          gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol;
1984e52336cbSBarry Smith   PetscReal          catol=tao->catol,crtol=tao->crtol;
1985e52336cbSBarry Smith   PetscReal          fmin=tao->fmin, cnorm=tao->cnorm;
1986e4cb33bbSBarry Smith   TaoConvergedReason reason=tao->reason;
1987a7e14dcfSSatish Balay 
1988a7e14dcfSSatish Balay   PetscFunctionBegin;
1989441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
199094511df7SStefano Zampini   if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0);
1991a7e14dcfSSatish Balay 
1992a7e14dcfSSatish Balay   if (PetscIsInfOrNanReal(f)) {
19939566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n"));
1994a7e14dcfSSatish Balay     reason = TAO_DIVERGED_NAN;
1995a7e14dcfSSatish Balay   } else if (f <= fmin && cnorm <=catol) {
19969566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin));
1997a7e14dcfSSatish Balay     reason = TAO_CONVERGED_MINF;
1998a7e14dcfSSatish Balay   } else if (gnorm<= gatol && cnorm <=catol) {
19999566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol));
2000a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GATOL;
2001a7e14dcfSSatish Balay   } else if (f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) {
20029566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol));
2003a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GRTOL;
2004e1d16bb9SBarry Smith   } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm/gnorm0 < gttol) && cnorm <= crtol) {
20059566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol));
2006a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GTTOL;
200794511df7SStefano Zampini   } else if (max_funcs >=0 && nfuncs > max_funcs) {
20089566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs,max_funcs));
2009a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXFCN;
2010a7e14dcfSSatish Balay   } else if (tao->lsflag != 0) {
20119566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Tao Line Search failure.\n"));
2012a7e14dcfSSatish Balay     reason = TAO_DIVERGED_LS_FAILURE;
2013a7e14dcfSSatish Balay   } else if (trradius < steptol && niter > 0) {
20149566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol));
2015a7e14dcfSSatish Balay     reason = TAO_CONVERGED_STEPTOL;
2016e031d6f5SAlp Dener   } else if (niter >= tao->max_it) {
201763a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(tao,"Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n",niter,tao->max_it));
2018a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXITS;
2019a7e14dcfSSatish Balay   } else {
2020a7e14dcfSSatish Balay     reason = TAO_CONTINUE_ITERATING;
2021a7e14dcfSSatish Balay   }
2022a7e14dcfSSatish Balay   tao->reason = reason;
2023a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2024a7e14dcfSSatish Balay }
2025a7e14dcfSSatish Balay 
2026a7e14dcfSSatish Balay /*@C
2027a7e14dcfSSatish Balay    TaoSetOptionsPrefix - Sets the prefix used for searching for all
202865ba42b6SBarry Smith    Tao options in the database.
2029a7e14dcfSSatish Balay 
203065ba42b6SBarry Smith    Logically Collective on tao
2031a7e14dcfSSatish Balay 
2032a7e14dcfSSatish Balay    Input Parameters:
2033441846f8SBarry Smith +  tao - the Tao context
203465ba42b6SBarry Smith -  prefix - the prefix string to prepend to all Tao option requests
2035a7e14dcfSSatish Balay 
2036a7e14dcfSSatish Balay    Notes:
2037a7e14dcfSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2038a7e14dcfSSatish Balay    The first character of all runtime options is AUTOMATICALLY the hyphen.
2039a7e14dcfSSatish Balay 
2040a7e14dcfSSatish Balay    For example, to distinguish between the runtime options for two
204165ba42b6SBarry Smith    different Tao solvers, one could call
2042a7e14dcfSSatish Balay .vb
2043a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao1,"sys1_")
2044a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao2,"sys2_")
2045a7e14dcfSSatish Balay .ve
2046a7e14dcfSSatish Balay 
2047a7e14dcfSSatish Balay    This would enable use of different options for each system, such as
2048a7e14dcfSSatish Balay .vb
20499fa2b5dcSStefano Zampini       -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3
20509fa2b5dcSStefano Zampini       -sys2_tao_method lmvm  -sys2_tao_grtol 1.e-4
2051a7e14dcfSSatish Balay .ve
2052a7e14dcfSSatish Balay 
2053a7e14dcfSSatish Balay    Level: advanced
2054a7e14dcfSSatish Balay 
205565ba42b6SBarry Smith .seealso: `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()`
2056a7e14dcfSSatish Balay @*/
2057a7e14dcfSSatish Balay 
2058441846f8SBarry Smith PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
2059a7e14dcfSSatish Balay {
2060a7e14dcfSSatish Balay   PetscFunctionBegin;
206194511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
20629566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao,p));
20631baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch,p));
20641baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp,p));
2065a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2066a7e14dcfSSatish Balay }
2067a7e14dcfSSatish Balay 
2068a7e14dcfSSatish Balay /*@C
2069a7e14dcfSSatish Balay    TaoAppendOptionsPrefix - Appends to the prefix used for searching for all
207065ba42b6SBarry Smith    Tao options in the database.
2071a7e14dcfSSatish Balay 
207265ba42b6SBarry Smith    Logically Collective on tao
2073a7e14dcfSSatish Balay 
2074a7e14dcfSSatish Balay    Input Parameters:
2075441846f8SBarry Smith +  tao - the Tao solver context
207665ba42b6SBarry Smith -  prefix - the prefix string to prepend to all Tao option requests
2077a7e14dcfSSatish Balay 
207865ba42b6SBarry Smith    Note:
2079a7e14dcfSSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
208065ba42b6SBarry Smith    The first character of all runtime options is automatically the hyphen.
2081a7e14dcfSSatish Balay 
2082a7e14dcfSSatish Balay    Level: advanced
2083a7e14dcfSSatish Balay 
208465ba42b6SBarry Smith .seealso: `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()`
2085a7e14dcfSSatish Balay @*/
2086441846f8SBarry Smith PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
2087a7e14dcfSSatish Balay {
2088a7e14dcfSSatish Balay   PetscFunctionBegin;
208994511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
20909566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao,p));
20911baa6e33SBarry Smith   if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch,p));
20921baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp,p));
2093a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2094a7e14dcfSSatish Balay }
2095a7e14dcfSSatish Balay 
2096a7e14dcfSSatish Balay /*@C
2097a7e14dcfSSatish Balay   TaoGetOptionsPrefix - Gets the prefix used for searching for all
209865ba42b6SBarry Smith   Tao options in the database
2099a7e14dcfSSatish Balay 
2100a7e14dcfSSatish Balay   Not Collective
2101a7e14dcfSSatish Balay 
2102a7e14dcfSSatish Balay   Input Parameters:
2103441846f8SBarry Smith . tao - the Tao context
2104a7e14dcfSSatish Balay 
2105a7e14dcfSSatish Balay   Output Parameters:
2106a7e14dcfSSatish Balay . prefix - pointer to the prefix string used is returned
2107a7e14dcfSSatish Balay 
210865ba42b6SBarry Smith   Fortran Note:
210995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
2110a7e14dcfSSatish Balay   sufficient length to hold the prefix.
2111a7e14dcfSSatish Balay 
2112a7e14dcfSSatish Balay   Level: advanced
2113a7e14dcfSSatish Balay 
211465ba42b6SBarry Smith .seealso: `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()`
2115a7e14dcfSSatish Balay @*/
2116441846f8SBarry Smith PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
2117a7e14dcfSSatish Balay {
211894511df7SStefano Zampini   PetscFunctionBegin;
211994511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
21209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao,p));
212194511df7SStefano Zampini   PetscFunctionReturn(0);
2122a7e14dcfSSatish Balay }
2123a7e14dcfSSatish Balay 
2124a7e14dcfSSatish Balay /*@C
2125a7e14dcfSSatish Balay    TaoSetType - Sets the method for the unconstrained minimization solver.
2126a7e14dcfSSatish Balay 
212765ba42b6SBarry Smith    Collective on tao
2128a7e14dcfSSatish Balay 
2129a7e14dcfSSatish Balay    Input Parameters:
2130441846f8SBarry Smith +  solver - the Tao solver context
2131a7e14dcfSSatish Balay -  type - a known method
2132a7e14dcfSSatish Balay 
2133a7e14dcfSSatish Balay    Options Database Key:
213458417fe7SBarry Smith .  -tao_type <type> - Sets the method; use -help for a list
213558417fe7SBarry Smith    of available methods (for instance, "-tao_type lmvm" or "-tao_type tron")
2136a7e14dcfSSatish Balay 
2137a7e14dcfSSatish Balay    Available methods include:
213865ba42b6SBarry Smith +    `TAONLS` - nls Newton's method with line search for unconstrained minimization
213965ba42b6SBarry Smith .    `TAONTR` - ntr Newton's method with trust region for unconstrained minimization
214065ba42b6SBarry Smith .    `TAONTL` - ntl Newton's method with trust region, line search for unconstrained minimization
214165ba42b6SBarry Smith .    `TAOLMVM` - lmvm Limited memory variable metric method for unconstrained minimization
214265ba42b6SBarry Smith .    `TAOCG` - cg Nonlinear conjugate gradient method for unconstrained minimization
214365ba42b6SBarry Smith .    `TAONM` - nm Nelder-Mead algorithm for derivate-free unconstrained minimization
214465ba42b6SBarry Smith .    `TAOTRON` - tron Newton Trust Region method for bound constrained minimization
214565ba42b6SBarry Smith .    `TAOGPCG` - gpcg Newton Trust Region method for quadratic bound constrained minimization
214665ba42b6SBarry Smith .    `TAOBLMVM` - blmvm Limited memory variable metric method for bound constrained minimization
214765ba42b6SBarry Smith .    `TAOLCL` - lcl Linearly constrained Lagrangian method for pde-constrained minimization
214865ba42b6SBarry Smith -    `TAOPOUNDERS` - pounders Model-based algorithm for nonlinear least squares
2149a7e14dcfSSatish Balay 
2150a7e14dcfSSatish Balay   Level: intermediate
2151a7e14dcfSSatish Balay 
215265ba42b6SBarry Smith .seealso: `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType`
2153a7e14dcfSSatish Balay 
2154a7e14dcfSSatish Balay @*/
2155b625d6c7SJed Brown PetscErrorCode TaoSetType(Tao tao, TaoType type)
2156a7e14dcfSSatish Balay {
2157441846f8SBarry Smith   PetscErrorCode (*create_xxx)(Tao);
2158a7e14dcfSSatish Balay   PetscBool      issame;
2159a7e14dcfSSatish Balay 
2160a7e14dcfSSatish Balay   PetscFunctionBegin;
2161441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2162a7e14dcfSSatish Balay 
21639566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao,type,&issame));
2164a7e14dcfSSatish Balay   if (issame) PetscFunctionReturn(0);
2165a7e14dcfSSatish Balay 
21669566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(TaoList, type, (void(**)(void))&create_xxx));
21673c859ba3SBarry Smith   PetscCheck(create_xxx,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested Tao type %s",type);
2168a7e14dcfSSatish Balay 
2169a7e14dcfSSatish Balay   /* Destroy the existing solver information */
2170*dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao,destroy);
21711baa6e33SBarry Smith   PetscCall(KSPDestroy(&tao->ksp));
21729566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&tao->linesearch));
217383c8fe1dSLisandro Dalcin   tao->ops->setup          = NULL;
217483c8fe1dSLisandro Dalcin   tao->ops->solve          = NULL;
217583c8fe1dSLisandro Dalcin   tao->ops->view           = NULL;
217683c8fe1dSLisandro Dalcin   tao->ops->setfromoptions = NULL;
217783c8fe1dSLisandro Dalcin   tao->ops->destroy        = NULL;
2178a7e14dcfSSatish Balay 
2179a7e14dcfSSatish Balay   tao->setupcalled = PETSC_FALSE;
2180a7e14dcfSSatish Balay 
21819566063dSJacob Faibussowitsch   PetscCall((*create_xxx)(tao));
21829566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)tao,type));
2183a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2184a7e14dcfSSatish Balay }
218547a47007SBarry Smith 
2186a7e14dcfSSatish Balay /*MC
218765ba42b6SBarry Smith    TaoRegister - Adds a method to the Tao package for unconstrained minimization.
2188a7e14dcfSSatish Balay 
2189a7e14dcfSSatish Balay    Synopsis:
21905bd1e576SStefano Zampini    TaoRegister(char *name_solver,char *path,char *name_Create,PetscErrorCode (*routine_Create)(Tao))
2191a7e14dcfSSatish Balay 
2192a7e14dcfSSatish Balay    Not collective
2193a7e14dcfSSatish Balay 
2194a7e14dcfSSatish Balay    Input Parameters:
2195a7e14dcfSSatish Balay +  sname - name of a new user-defined solver
2196a7e14dcfSSatish Balay -  func - routine to Create method context
2197a7e14dcfSSatish Balay 
219865ba42b6SBarry Smith    Note:
219965ba42b6SBarry Smith    `TaoRegister()` may be called multiple times to add several user-defined solvers.
2200a7e14dcfSSatish Balay 
2201a7e14dcfSSatish Balay    Sample usage:
2202a7e14dcfSSatish Balay .vb
2203441846f8SBarry Smith    TaoRegister("my_solver",MySolverCreate);
2204a7e14dcfSSatish Balay .ve
2205a7e14dcfSSatish Balay 
2206a7e14dcfSSatish Balay    Then, your solver can be chosen with the procedural interface via
2207a7e14dcfSSatish Balay $     TaoSetType(tao,"my_solver")
2208a7e14dcfSSatish Balay    or at runtime via the option
220958417fe7SBarry Smith $     -tao_type my_solver
2210a7e14dcfSSatish Balay 
2211a7e14dcfSSatish Balay    Level: advanced
2212a7e14dcfSSatish Balay 
221365ba42b6SBarry Smith .seealso: `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()`
2214a7e14dcfSSatish Balay M*/
2215441846f8SBarry Smith PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao))
2216a7e14dcfSSatish Balay {
2217a7e14dcfSSatish Balay   PetscFunctionBegin;
22189566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
22199566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&TaoList,sname,(void (*)(void))func));
2220a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2221a7e14dcfSSatish Balay }
2222a7e14dcfSSatish Balay 
2223a7e14dcfSSatish Balay /*@C
2224441846f8SBarry Smith    TaoRegisterDestroy - Frees the list of minimization solvers that were
222565ba42b6SBarry Smith    registered by `TaoRegisterDynamic()`.
2226a7e14dcfSSatish Balay 
2227a7e14dcfSSatish Balay    Not Collective
2228a7e14dcfSSatish Balay 
2229a7e14dcfSSatish Balay    Level: advanced
2230a7e14dcfSSatish Balay 
2231db781477SPatrick Sanan .seealso: `TaoRegisterAll()`, `TaoRegister()`
2232a7e14dcfSSatish Balay @*/
2233441846f8SBarry Smith PetscErrorCode TaoRegisterDestroy(void)
2234a7e14dcfSSatish Balay {
2235a7e14dcfSSatish Balay   PetscFunctionBegin;
22369566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&TaoList));
2237441846f8SBarry Smith   TaoRegisterAllCalled = PETSC_FALSE;
2238a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2239a7e14dcfSSatish Balay }
224047a47007SBarry Smith 
22418931d482SJason Sarich /*@
22428931d482SJason Sarich    TaoGetIterationNumber - Gets the number of Tao iterations completed
22438931d482SJason Sarich    at this time.
22448931d482SJason Sarich 
22458931d482SJason Sarich    Not Collective
22468931d482SJason Sarich 
22478931d482SJason Sarich    Input Parameter:
22488931d482SJason Sarich .  tao - Tao context
22498931d482SJason Sarich 
22508931d482SJason Sarich    Output Parameter:
22518931d482SJason Sarich .  iter - iteration number
22528931d482SJason Sarich 
22538931d482SJason Sarich    Notes:
22548931d482SJason Sarich    For example, during the computation of iteration 2 this would return 1.
22558931d482SJason Sarich 
22568931d482SJason Sarich    Level: intermediate
22578931d482SJason Sarich 
2258db781477SPatrick Sanan .seealso: `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()`
22598931d482SJason Sarich @*/
22608931d482SJason Sarich PetscErrorCode  TaoGetIterationNumber(Tao tao,PetscInt *iter)
22618931d482SJason Sarich {
22628931d482SJason Sarich   PetscFunctionBegin;
22638931d482SJason Sarich   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
22648931d482SJason Sarich   PetscValidIntPointer(iter,2);
22658931d482SJason Sarich   *iter = tao->niter;
22668931d482SJason Sarich   PetscFunctionReturn(0);
22678931d482SJason Sarich }
22688931d482SJason Sarich 
22698931d482SJason Sarich /*@
227079f5d8caSBarry Smith    TaoGetResidualNorm - Gets the current value of the norm of the residual
227179f5d8caSBarry Smith    at this time.
227279f5d8caSBarry Smith 
227379f5d8caSBarry Smith    Not Collective
227479f5d8caSBarry Smith 
227579f5d8caSBarry Smith    Input Parameter:
227679f5d8caSBarry Smith .  tao - Tao context
227779f5d8caSBarry Smith 
227879f5d8caSBarry Smith    Output Parameter:
227979f5d8caSBarry Smith .  value - the current value
228079f5d8caSBarry Smith 
228179f5d8caSBarry Smith    Level: intermediate
228279f5d8caSBarry Smith 
228365ba42b6SBarry Smith    Developer Note: This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has
228479f5d8caSBarry Smith                    a different meaning. For some reason Tao sometimes calls the gradient the residual.
228579f5d8caSBarry Smith 
2286db781477SPatrick Sanan .seealso: `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()`
228779f5d8caSBarry Smith @*/
228879f5d8caSBarry Smith PetscErrorCode TaoGetResidualNorm(Tao tao,PetscReal *value)
228979f5d8caSBarry Smith {
229079f5d8caSBarry Smith   PetscFunctionBegin;
229179f5d8caSBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
229279f5d8caSBarry Smith   PetscValidRealPointer(value,2);
229379f5d8caSBarry Smith   *value = tao->residual;
229479f5d8caSBarry Smith   PetscFunctionReturn(0);
229579f5d8caSBarry Smith }
229679f5d8caSBarry Smith 
229779f5d8caSBarry Smith /*@
22988931d482SJason Sarich    TaoSetIterationNumber - Sets the current iteration number.
22998931d482SJason Sarich 
230065ba42b6SBarry Smith    Logically Collective on tao
23018931d482SJason Sarich 
2302d8d19677SJose E. Roman    Input Parameters:
2303a2b725a8SWilliam Gropp +  tao - Tao context
2304a2b725a8SWilliam Gropp -  iter - iteration number
23058931d482SJason Sarich 
23068931d482SJason Sarich    Level: developer
23078931d482SJason Sarich 
2308db781477SPatrick Sanan .seealso: `TaoGetLinearSolveIterations()`
23098931d482SJason Sarich @*/
23108931d482SJason Sarich PetscErrorCode  TaoSetIterationNumber(Tao tao,PetscInt iter)
23118931d482SJason Sarich {
23128931d482SJason Sarich   PetscFunctionBegin;
23138931d482SJason Sarich   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
231494511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao,iter,2);
23159566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
23168931d482SJason Sarich   tao->niter = iter;
23179566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
23188931d482SJason Sarich   PetscFunctionReturn(0);
23198931d482SJason Sarich }
23208931d482SJason Sarich 
23218931d482SJason Sarich /*@
23228931d482SJason Sarich    TaoGetTotalIterationNumber - Gets the total number of Tao iterations
23238931d482SJason Sarich    completed. This number keeps accumulating if multiple solves
23248931d482SJason Sarich    are called with the Tao object.
23258931d482SJason Sarich 
23268931d482SJason Sarich    Not Collective
23278931d482SJason Sarich 
23288931d482SJason Sarich    Input Parameter:
23298931d482SJason Sarich .  tao - Tao context
23308931d482SJason Sarich 
23318931d482SJason Sarich    Output Parameter:
23328931d482SJason Sarich .  iter - iteration number
23338931d482SJason Sarich 
23348931d482SJason Sarich    Notes:
23358931d482SJason Sarich    The total iteration count is updated after each solve, if there is a current
23368931d482SJason Sarich    TaoSolve() in progress then those iterations are not yet counted.
23378931d482SJason Sarich 
23388931d482SJason Sarich    Level: intermediate
23398931d482SJason Sarich 
2340db781477SPatrick Sanan .seealso: `TaoGetLinearSolveIterations()`
23418931d482SJason Sarich @*/
23428931d482SJason Sarich PetscErrorCode  TaoGetTotalIterationNumber(Tao tao,PetscInt *iter)
23438931d482SJason Sarich {
23448931d482SJason Sarich   PetscFunctionBegin;
23458931d482SJason Sarich   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
23468931d482SJason Sarich   PetscValidIntPointer(iter,2);
23478931d482SJason Sarich   *iter = tao->ntotalits;
23488931d482SJason Sarich   PetscFunctionReturn(0);
23498931d482SJason Sarich }
23508931d482SJason Sarich 
23518931d482SJason Sarich /*@
23528931d482SJason Sarich    TaoSetTotalIterationNumber - Sets the current total iteration number.
23538931d482SJason Sarich 
235465ba42b6SBarry Smith    Logically Collective on tao
23558931d482SJason Sarich 
2356d8d19677SJose E. Roman    Input Parameters:
2357a2b725a8SWilliam Gropp +  tao - Tao context
2358a2b725a8SWilliam Gropp -  iter - iteration number
23598931d482SJason Sarich 
23608931d482SJason Sarich    Level: developer
23618931d482SJason Sarich 
2362db781477SPatrick Sanan .seealso: `TaoGetLinearSolveIterations()`
23638931d482SJason Sarich @*/
23648931d482SJason Sarich PetscErrorCode  TaoSetTotalIterationNumber(Tao tao,PetscInt iter)
23658931d482SJason Sarich {
23668931d482SJason Sarich   PetscFunctionBegin;
23678931d482SJason Sarich   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
236894511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao,iter,2);
23699566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
23708931d482SJason Sarich   tao->ntotalits = iter;
23719566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
23728931d482SJason Sarich   PetscFunctionReturn(0);
23738931d482SJason Sarich }
23748931d482SJason Sarich 
2375a7e14dcfSSatish Balay /*@
2376e4cb33bbSBarry Smith   TaoSetConvergedReason - Sets the termination flag on a Tao object
2377a7e14dcfSSatish Balay 
237865ba42b6SBarry Smith   Logically Collective on tao
2379a7e14dcfSSatish Balay 
2380a7e14dcfSSatish Balay   Input Parameters:
2381441846f8SBarry Smith + tao - the Tao context
2382a7e14dcfSSatish Balay - reason - one of
238365ba42b6SBarry Smith $     `TAO_CONVERGED_ATOL` (2),
238465ba42b6SBarry Smith $     `TAO_CONVERGED_RTOL` (3),
238565ba42b6SBarry Smith $     `TAO_CONVERGED_STEPTOL` (4),
238665ba42b6SBarry Smith $     `TAO_CONVERGED_MINF` (5),
238765ba42b6SBarry Smith $     `TAO_CONVERGED_USER` (6),
238865ba42b6SBarry Smith $     `TAO_DIVERGED_MAXITS` (-2),
238965ba42b6SBarry Smith $     `TAO_DIVERGED_NAN` (-4),
239065ba42b6SBarry Smith $     `TAO_DIVERGED_MAXFCN` (-5),
239165ba42b6SBarry Smith $     `TAO_DIVERGED_LS_FAILURE` (-6),
239265ba42b6SBarry Smith $     `TAO_DIVERGED_TR_REDUCTION` (-7),
239365ba42b6SBarry Smith $     `TAO_DIVERGED_USER` (-8),
239465ba42b6SBarry Smith $     `TAO_CONTINUE_ITERATING` (0)
2395a7e14dcfSSatish Balay 
2396a7e14dcfSSatish Balay    Level: intermediate
2397a7e14dcfSSatish Balay 
2398a7e14dcfSSatish Balay @*/
2399e4cb33bbSBarry Smith PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2400a7e14dcfSSatish Balay {
2401a7e14dcfSSatish Balay   PetscFunctionBegin;
240294511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
240394511df7SStefano Zampini   PetscValidLogicalCollectiveEnum(tao,reason,2);
2404a7e14dcfSSatish Balay   tao->reason = reason;
2405a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2406a7e14dcfSSatish Balay }
2407a7e14dcfSSatish Balay 
2408a7e14dcfSSatish Balay /*@
2409e4cb33bbSBarry Smith    TaoGetConvergedReason - Gets the reason the Tao iteration was stopped.
2410a7e14dcfSSatish Balay 
2411a7e14dcfSSatish Balay    Not Collective
2412a7e14dcfSSatish Balay 
2413a7e14dcfSSatish Balay    Input Parameter:
2414441846f8SBarry Smith .  tao - the Tao solver context
2415a7e14dcfSSatish Balay 
2416a7e14dcfSSatish Balay    Output Parameter:
2417a7e14dcfSSatish Balay .  reason - one of
241865ba42b6SBarry Smith $  `TAO_CONVERGED_GATOL` (3)           ||g(X)|| < gatol
241965ba42b6SBarry Smith $  `TAO_CONVERGED_GRTOL` (4)           ||g(X)|| / f(X)  < grtol
242065ba42b6SBarry Smith $  `TAO_CONVERGED_GTTOL` (5)           ||g(X)|| / ||g(X0)|| < gttol
242165ba42b6SBarry Smith $  `TAO_CONVERGED_STEPTOL` (6)         step size small
242265ba42b6SBarry Smith $  `TAO_CONVERGED_MINF` (7)            F < F_min
242365ba42b6SBarry Smith $  `TAO_CONVERGED_USER` (8)            User defined
242465ba42b6SBarry Smith $  `TAO_DIVERGED_MAXITS` (-2)          its > maxits
242565ba42b6SBarry Smith $  `TAO_DIVERGED_NAN` (-4)             Numerical problems
242665ba42b6SBarry Smith $  `TAO_DIVERGED_MAXFCN` (-5)          fevals > max_funcsals
242765ba42b6SBarry Smith $  `TAO_DIVERGED_LS_FAILURE` (-6)      line search failure
242865ba42b6SBarry Smith $  `TAO_DIVERGED_TR_REDUCTION` (-7)    trust region failure
242965ba42b6SBarry Smith $  `TAO_DIVERGED_USER` (-8)             (user defined)
243065ba42b6SBarry Smith $  `TAO_CONTINUE_ITERATING` (0)
2431a7e14dcfSSatish Balay 
2432a7e14dcfSSatish Balay    where
2433a7e14dcfSSatish Balay +  X - current solution
2434a7e14dcfSSatish Balay .  X0 - initial guess
2435a7e14dcfSSatish Balay .  f(X) - current function value
2436a7e14dcfSSatish Balay .  f(X*) - true solution (estimated)
2437a7e14dcfSSatish Balay .  g(X) - current gradient
2438a7e14dcfSSatish Balay .  its - current iterate number
2439a7e14dcfSSatish Balay .  maxits - maximum number of iterates
2440a7e14dcfSSatish Balay .  fevals - number of function evaluations
2441a7e14dcfSSatish Balay -  max_funcsals - maximum number of function evaluations
2442a7e14dcfSSatish Balay 
2443a7e14dcfSSatish Balay    Level: intermediate
2444a7e14dcfSSatish Balay 
2445db781477SPatrick Sanan .seealso: `TaoSetConvergenceTest()`, `TaoSetTolerances()`
2446a7e14dcfSSatish Balay 
2447a7e14dcfSSatish Balay @*/
2448e4cb33bbSBarry Smith PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2449a7e14dcfSSatish Balay {
2450a7e14dcfSSatish Balay   PetscFunctionBegin;
2451441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2452a7e14dcfSSatish Balay   PetscValidPointer(reason,2);
2453a7e14dcfSSatish Balay   *reason = tao->reason;
2454a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2455a7e14dcfSSatish Balay }
2456a7e14dcfSSatish Balay 
2457a7e14dcfSSatish Balay /*@
2458a7e14dcfSSatish Balay    TaoGetSolutionStatus - Get the current iterate, objective value,
2459a7e14dcfSSatish Balay    residual, infeasibility, and termination
2460a7e14dcfSSatish Balay 
2461a7e14dcfSSatish Balay    Not Collective
2462a7e14dcfSSatish Balay 
2463f899ff85SJose E. Roman    Input Parameter:
2464441846f8SBarry Smith .  tao - the Tao context
2465a7e14dcfSSatish Balay 
2466a7e14dcfSSatish Balay    Output Parameters:
2467a7e14dcfSSatish Balay +  iterate - the current iterate number (>=0)
2468a7e14dcfSSatish Balay .  f - the current function value
246958417fe7SBarry Smith .  gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2470a7e14dcfSSatish Balay .  cnorm - the infeasibility of the current solution with regard to the constraints.
2471a7e14dcfSSatish Balay .  xdiff - the step length or trust region radius of the most recent iterate.
247265ba42b6SBarry Smith -  reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING`
2473a7e14dcfSSatish Balay 
2474a7e14dcfSSatish Balay    Level: intermediate
2475a7e14dcfSSatish Balay 
247665ba42b6SBarry Smith    Notes:
247765ba42b6SBarry Smith    Tao returns the values set by the solvers in the routine `TaoMonitor()`.
2478a7e14dcfSSatish Balay 
247947a47007SBarry Smith    If any of the output arguments are set to NULL, no corresponding value will be returned.
2480a7e14dcfSSatish Balay 
2481db781477SPatrick Sanan .seealso: `TaoMonitor()`, `TaoGetConvergedReason()`
2482a7e14dcfSSatish Balay @*/
2483e4cb33bbSBarry Smith PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2484a7e14dcfSSatish Balay {
2485a7e14dcfSSatish Balay   PetscFunctionBegin;
248694511df7SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2487a7e14dcfSSatish Balay   if (its) *its = tao->niter;
2488a7e14dcfSSatish Balay   if (f) *f = tao->fc;
2489a7e14dcfSSatish Balay   if (gnorm) *gnorm = tao->residual;
2490a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->cnorm;
2491a7e14dcfSSatish Balay   if (reason) *reason = tao->reason;
2492a7e14dcfSSatish Balay   if (xdiff) *xdiff = tao->step;
2493a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2494a7e14dcfSSatish Balay }
2495a7e14dcfSSatish Balay 
2496a7e14dcfSSatish Balay /*@C
2497441846f8SBarry Smith    TaoGetType - Gets the current Tao algorithm.
2498a7e14dcfSSatish Balay 
2499a7e14dcfSSatish Balay    Not Collective
2500a7e14dcfSSatish Balay 
2501a7e14dcfSSatish Balay    Input Parameter:
2502441846f8SBarry Smith .  tao - the Tao solver context
2503a7e14dcfSSatish Balay 
2504a7e14dcfSSatish Balay    Output Parameter:
2505441846f8SBarry Smith .  type - Tao method
2506a7e14dcfSSatish Balay 
2507a7e14dcfSSatish Balay    Level: intermediate
2508a7e14dcfSSatish Balay 
250965ba42b6SBarry Smith .seealso: `Tao`, `TaoType`, `TaoSetType()`
2510a7e14dcfSSatish Balay @*/
2511b625d6c7SJed Brown PetscErrorCode TaoGetType(Tao tao,TaoType *type)
2512a7e14dcfSSatish Balay {
2513a7e14dcfSSatish Balay   PetscFunctionBegin;
2514441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2515a7e14dcfSSatish Balay   PetscValidPointer(type,2);
2516a7e14dcfSSatish Balay   *type = ((PetscObject)tao)->type_name;
2517a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2518a7e14dcfSSatish Balay }
2519a7e14dcfSSatish Balay 
2520a7e14dcfSSatish Balay /*@C
2521a7e14dcfSSatish Balay   TaoMonitor - Monitor the solver and the current solution.  This
2522a7e14dcfSSatish Balay   routine will record the iteration number and residual statistics,
2523a7e14dcfSSatish Balay   call any monitors specified by the user, and calls the convergence-check routine.
2524a7e14dcfSSatish Balay 
2525a7e14dcfSSatish Balay    Input Parameters:
2526441846f8SBarry Smith +  tao - the Tao context
2527a7e14dcfSSatish Balay .  its - the current iterate number (>=0)
2528a7e14dcfSSatish Balay .  f - the current objective function value
252958417fe7SBarry Smith .  res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality.  This measure will be recorded and
2530a7e14dcfSSatish Balay           used for some termination tests.
2531a7e14dcfSSatish Balay .  cnorm - the infeasibility of the current solution with regard to the constraints.
2532a7e14dcfSSatish Balay -  steplength - multiple of the step direction added to the previous iterate.
2533a7e14dcfSSatish Balay 
2534a7e14dcfSSatish Balay    Output Parameters:
253565ba42b6SBarry Smith .  reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING`
2536a7e14dcfSSatish Balay 
2537a7e14dcfSSatish Balay    Options Database Key:
2538a7e14dcfSSatish Balay .  -tao_monitor - Use the default monitor, which prints statistics to standard output
2539a7e14dcfSSatish Balay 
2540a7e14dcfSSatish Balay    Level: developer
2541a7e14dcfSSatish Balay 
254265ba42b6SBarry Smith .seealso `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoSetMonitor()`
2543a7e14dcfSSatish Balay @*/
25443ecd9318SAlp Dener PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength)
2545a7e14dcfSSatish Balay {
254647a47007SBarry Smith   PetscInt       i;
254747a47007SBarry Smith 
2548a7e14dcfSSatish Balay   PetscFunctionBegin;
2549441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2550a7e14dcfSSatish Balay   tao->fc = f;
2551a7e14dcfSSatish Balay   tao->residual = res;
2552a7e14dcfSSatish Balay   tao->cnorm = cnorm;
2553a7e14dcfSSatish Balay   tao->step = steplength;
2554e52336cbSBarry Smith   if (!its) {
255594511df7SStefano Zampini     tao->cnorm0 = cnorm;
255694511df7SStefano Zampini     tao->gnorm0 = res;
2557a7e14dcfSSatish Balay   }
25583c859ba3SBarry Smith   PetscCheck(!PetscIsInfOrNanReal(f) && !PetscIsInfOrNanReal(res),PetscObjectComm((PetscObject)tao),PETSC_ERR_USER, "User provided compute function generated Inf or NaN");
2559a7e14dcfSSatish Balay   for (i=0;i<tao->numbermonitors;i++) {
25609566063dSJacob Faibussowitsch     PetscCall((*tao->monitor[i])(tao,tao->monitorcontext[i]));
2561a7e14dcfSSatish Balay   }
2562a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2563a7e14dcfSSatish Balay }
2564a7e14dcfSSatish Balay 
2565a7e14dcfSSatish Balay /*@
2566ae93cb3cSJason Sarich    TaoSetConvergenceHistory - Sets the array used to hold the convergence history.
2567a7e14dcfSSatish Balay 
256865ba42b6SBarry Smith    Logically Collective on tao
2569a7e14dcfSSatish Balay 
2570a7e14dcfSSatish Balay    Input Parameters:
2571441846f8SBarry Smith +  tao - the Tao solver context
2572a7e14dcfSSatish Balay .  obj   - array to hold objective value history
2573a7e14dcfSSatish Balay .  resid - array to hold residual history
2574a7e14dcfSSatish Balay .  cnorm - array to hold constraint violation history
2575be1558d8SJason Sarich .  lits - integer array holds the number of linear iterations for each Tao iteration
2576a7e14dcfSSatish Balay .  na  - size of obj, resid, and cnorm
257765ba42b6SBarry Smith -  reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero,
2578a7e14dcfSSatish Balay            else it continues storing new values for new minimizations after the old ones
2579a7e14dcfSSatish Balay 
2580a7e14dcfSSatish Balay    Notes:
258165ba42b6SBarry Smith    If set, Tao will fill the given arrays with the indicated
2582ae93cb3cSJason Sarich    information at each iteration.  If 'obj','resid','cnorm','lits' are
258365ba42b6SBarry Smith    *all* NULL then space (using size na, or 1000 if na is `PETSC_DECIDE` or
258465ba42b6SBarry Smith    `PETSC_DEFAULT`) is allocated for the history.
2585be1558d8SJason Sarich    If not all are NULL, then only the non-NULL information categories
2586ae93cb3cSJason Sarich    will be stored, the others will be ignored.
2587ae93cb3cSJason Sarich 
2588ae93cb3cSJason Sarich    Any convergence information after iteration number 'na' will not be stored.
2589a7e14dcfSSatish Balay 
2590a7e14dcfSSatish Balay    This routine is useful, e.g., when running a code for purposes
2591a7e14dcfSSatish Balay    of accurate performance monitoring, when no I/O should be done
2592a7e14dcfSSatish Balay    during the section of code that is being timed.
2593a7e14dcfSSatish Balay 
2594a7e14dcfSSatish Balay    Level: intermediate
2595a7e14dcfSSatish Balay 
2596db781477SPatrick Sanan .seealso: `TaoGetConvergenceHistory()`
2597a7e14dcfSSatish Balay 
2598a7e14dcfSSatish Balay @*/
25991b266c99SBarry Smith PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na,PetscBool reset)
2600a7e14dcfSSatish Balay {
2601a7e14dcfSSatish Balay   PetscFunctionBegin;
2602441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2603064a246eSJacob Faibussowitsch   if (obj) PetscValidRealPointer(obj,2);
2604064a246eSJacob Faibussowitsch   if (resid) PetscValidRealPointer(resid,3);
2605064a246eSJacob Faibussowitsch   if (cnorm) PetscValidRealPointer(cnorm,4);
2606ae93cb3cSJason Sarich   if (lits) PetscValidIntPointer(lits,5);
2607ae93cb3cSJason Sarich 
2608ae93cb3cSJason Sarich   if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2609ae93cb3cSJason Sarich   if (!obj && !resid && !cnorm && !lits) {
26109566063dSJacob Faibussowitsch     PetscCall(PetscCalloc4(na,&obj,na,&resid,na,&cnorm,na,&lits));
2611ae93cb3cSJason Sarich     tao->hist_malloc = PETSC_TRUE;
2612ae93cb3cSJason Sarich   }
2613ae93cb3cSJason Sarich 
2614a7e14dcfSSatish Balay   tao->hist_obj = obj;
2615a7e14dcfSSatish Balay   tao->hist_resid = resid;
2616a7e14dcfSSatish Balay   tao->hist_cnorm = cnorm;
2617ae93cb3cSJason Sarich   tao->hist_lits = lits;
2618a7e14dcfSSatish Balay   tao->hist_max   = na;
2619a7e14dcfSSatish Balay   tao->hist_reset = reset;
2620ae93cb3cSJason Sarich   tao->hist_len = 0;
2621a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2622a7e14dcfSSatish Balay }
2623a7e14dcfSSatish Balay 
2624a7e14dcfSSatish Balay /*@C
262565ba42b6SBarry Smith    TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history.
2626a7e14dcfSSatish Balay 
262765ba42b6SBarry Smith    Collective on tao
2628a7e14dcfSSatish Balay 
2629a7e14dcfSSatish Balay    Input Parameter:
2630441846f8SBarry Smith .  tao - the Tao context
2631a7e14dcfSSatish Balay 
2632a7e14dcfSSatish Balay    Output Parameters:
2633a7e14dcfSSatish Balay +  obj   - array used to hold objective value history
2634a7e14dcfSSatish Balay .  resid - array used to hold residual history
2635a7e14dcfSSatish Balay .  cnorm - array used to hold constraint violation history
2636ae93cb3cSJason Sarich .  lits  - integer array used to hold linear solver iteration count
2637dd25e5eeSStefano Zampini -  nhist  - size of obj, resid, cnorm, and lits
2638a7e14dcfSSatish Balay 
2639a7e14dcfSSatish Balay    Notes:
264065ba42b6SBarry Smith     This routine must be preceded by calls to `TaoSetConvergenceHistory()`
264165ba42b6SBarry Smith     and `TaoSolve()`, otherwise it returns useless information.
2642ae93cb3cSJason Sarich 
2643a7e14dcfSSatish Balay     The calling sequence for this routine in Fortran is
2644ae93cb3cSJason Sarich $   call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr)
2645a7e14dcfSSatish Balay 
2646a7e14dcfSSatish Balay    This routine is useful, e.g., when running a code for purposes
2647a7e14dcfSSatish Balay    of accurate performance monitoring, when no I/O should be done
2648a7e14dcfSSatish Balay    during the section of code that is being timed.
2649a7e14dcfSSatish Balay 
2650a7e14dcfSSatish Balay    Level: advanced
2651a7e14dcfSSatish Balay 
265265ba42b6SBarry Smith .seealso: `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()`
2653a7e14dcfSSatish Balay 
2654a7e14dcfSSatish Balay @*/
2655ae93cb3cSJason Sarich PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist)
2656a7e14dcfSSatish Balay {
2657a7e14dcfSSatish Balay   PetscFunctionBegin;
2658441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2659a7e14dcfSSatish Balay   if (obj)   *obj   = tao->hist_obj;
2660a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->hist_cnorm;
2661a7e14dcfSSatish Balay   if (resid) *resid = tao->hist_resid;
2662a7e14dcfSSatish Balay   if (nhist) *nhist = tao->hist_len;
2663a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2664a7e14dcfSSatish Balay }
2665a7e14dcfSSatish Balay 
2666a7e14dcfSSatish Balay /*@
2667a7e14dcfSSatish Balay    TaoSetApplicationContext - Sets the optional user-defined context for
2668a7e14dcfSSatish Balay    a solver.
2669a7e14dcfSSatish Balay 
267065ba42b6SBarry Smith    Logically Collective on tao
2671a7e14dcfSSatish Balay 
2672a7e14dcfSSatish Balay    Input Parameters:
2673441846f8SBarry Smith +  tao  - the Tao context
2674a7e14dcfSSatish Balay -  usrP - optional user context
2675a7e14dcfSSatish Balay 
2676a7e14dcfSSatish Balay    Level: intermediate
2677a7e14dcfSSatish Balay 
267865ba42b6SBarry Smith .seealso: `Tao`, `TaoGetApplicationContext()`, `TaoSetApplicationContext()`
2679a7e14dcfSSatish Balay @*/
2680441846f8SBarry Smith PetscErrorCode  TaoSetApplicationContext(Tao tao,void *usrP)
2681a7e14dcfSSatish Balay {
2682a7e14dcfSSatish Balay   PetscFunctionBegin;
2683441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
2684a7e14dcfSSatish Balay   tao->user = usrP;
2685a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2686a7e14dcfSSatish Balay }
2687a7e14dcfSSatish Balay 
2688a7e14dcfSSatish Balay /*@
2689a7e14dcfSSatish Balay    TaoGetApplicationContext - Gets the user-defined context for a
269065ba42b6SBarry Smith    Tao solvers.
2691a7e14dcfSSatish Balay 
2692a7e14dcfSSatish Balay    Not Collective
2693a7e14dcfSSatish Balay 
2694a7e14dcfSSatish Balay    Input Parameter:
2695441846f8SBarry Smith .  tao  - Tao context
2696a7e14dcfSSatish Balay 
2697a7e14dcfSSatish Balay    Output Parameter:
2698a7e14dcfSSatish Balay .  usrP - user context
2699a7e14dcfSSatish Balay 
2700a7e14dcfSSatish Balay    Level: intermediate
2701a7e14dcfSSatish Balay 
2702db781477SPatrick Sanan .seealso: `TaoSetApplicationContext()`
2703a7e14dcfSSatish Balay @*/
2704441846f8SBarry Smith PetscErrorCode  TaoGetApplicationContext(Tao tao,void *usrP)
2705a7e14dcfSSatish Balay {
2706a7e14dcfSSatish Balay   PetscFunctionBegin;
2707441846f8SBarry Smith   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
270894511df7SStefano Zampini   PetscValidPointer(usrP,2);
2709a7e14dcfSSatish Balay   *(void**)usrP = tao->user;
2710a7e14dcfSSatish Balay   PetscFunctionReturn(0);
2711a7e14dcfSSatish Balay }
2712a9603a14SPatrick Farrell 
2713a9603a14SPatrick Farrell /*@
271465ba42b6SBarry Smith    TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient.
2715a9603a14SPatrick Farrell 
2716a9603a14SPatrick Farrell    Collective on tao
2717a9603a14SPatrick Farrell 
2718a9603a14SPatrick Farrell    Input Parameters:
2719a9603a14SPatrick Farrell +  tao  - the Tao context
272065ba42b6SBarry Smith -  M    - matrix that defines the norm
2721a9603a14SPatrick Farrell 
2722a9603a14SPatrick Farrell    Level: beginner
2723a9603a14SPatrick Farrell 
272465ba42b6SBarry Smith .seealso: `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()`
2725a9603a14SPatrick Farrell @*/
2726a9603a14SPatrick Farrell PetscErrorCode  TaoSetGradientNorm(Tao tao, Mat M)
2727a9603a14SPatrick Farrell {
2728a9603a14SPatrick Farrell   PetscFunctionBegin;
2729a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
273094511df7SStefano Zampini   PetscValidHeaderSpecific(M,MAT_CLASSID,2);
27319566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)M));
27329566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&tao->gradient_norm));
27339566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&tao->gradient_norm_tmp));
2734a9603a14SPatrick Farrell   tao->gradient_norm = M;
27359566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp));
2736a9603a14SPatrick Farrell   PetscFunctionReturn(0);
2737a9603a14SPatrick Farrell }
2738a9603a14SPatrick Farrell 
2739a9603a14SPatrick Farrell /*@
274065ba42b6SBarry Smith    TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient.
2741a9603a14SPatrick Farrell 
2742a9603a14SPatrick Farrell    Not Collective
2743a9603a14SPatrick Farrell 
2744a9603a14SPatrick Farrell    Input Parameter:
2745a9603a14SPatrick Farrell .  tao  - Tao context
2746a9603a14SPatrick Farrell 
2747a9603a14SPatrick Farrell    Output Parameter:
2748a9603a14SPatrick Farrell .  M - gradient norm
2749a9603a14SPatrick Farrell 
2750a9603a14SPatrick Farrell    Level: beginner
2751a9603a14SPatrick Farrell 
275265ba42b6SBarry Smith .seealso: `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()`
2753a9603a14SPatrick Farrell @*/
2754a9603a14SPatrick Farrell PetscErrorCode  TaoGetGradientNorm(Tao tao, Mat *M)
2755a9603a14SPatrick Farrell {
2756a9603a14SPatrick Farrell   PetscFunctionBegin;
2757a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
275894511df7SStefano Zampini   PetscValidPointer(M,2);
2759a9603a14SPatrick Farrell   *M = tao->gradient_norm;
2760a9603a14SPatrick Farrell   PetscFunctionReturn(0);
2761a9603a14SPatrick Farrell }
2762a9603a14SPatrick Farrell 
276305aca2cdSSatish Balay /*@C
276465ba42b6SBarry Smith    TaoGradientNorm - Compute the norm with respect to the norm the user has set.
2765a9603a14SPatrick Farrell 
2766a9603a14SPatrick Farrell    Collective on tao
2767a9603a14SPatrick Farrell 
2768d8d19677SJose E. Roman    Input Parameters:
27692653c764SPierre Jolivet +  tao      - the Tao context
2770a9603a14SPatrick Farrell .  gradient - the gradient to be computed
27712653c764SPierre Jolivet -  norm     - the norm type
2772a9603a14SPatrick Farrell 
2773a9603a14SPatrick Farrell    Output Parameter:
2774a9603a14SPatrick Farrell .  gnorm    - the gradient norm
2775a9603a14SPatrick Farrell 
2776a9603a14SPatrick Farrell    Level: developer
2777a9603a14SPatrick Farrell 
277865ba42b6SBarry Smith .seealso: `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()`
2779a9603a14SPatrick Farrell @*/
2780a9603a14SPatrick Farrell PetscErrorCode  TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm)
2781a9603a14SPatrick Farrell {
2782a9603a14SPatrick Farrell   PetscFunctionBegin;
27838854b543SStefano Zampini   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
27848854b543SStefano Zampini   PetscValidHeaderSpecific(gradient,VEC_CLASSID,2);
27858854b543SStefano Zampini   PetscValidLogicalCollectiveEnum(tao,type,3);
278694511df7SStefano Zampini   PetscValidRealPointer(gnorm,4);
2787a9603a14SPatrick Farrell   if (tao->gradient_norm) {
2788680e2bc7SPatrick Farrell     PetscScalar gnorms;
2789680e2bc7SPatrick Farrell 
27903c859ba3SBarry Smith     PetscCheck(type == NORM_2,PetscObjectComm((PetscObject)gradient), PETSC_ERR_ARG_WRONG, "Norm type must be NORM_2 if an inner product for the gradient norm is set.");
27919566063dSJacob Faibussowitsch     PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp));
27929566063dSJacob Faibussowitsch     PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms));
27937bb79937SPatrick Farrell     *gnorm = PetscRealPart(PetscSqrtScalar(gnorms));
2794a9603a14SPatrick Farrell   } else {
27959566063dSJacob Faibussowitsch     PetscCall(VecNorm(gradient, type, gnorm));
2796a9603a14SPatrick Farrell   }
2797a9603a14SPatrick Farrell   PetscFunctionReturn(0);
2798a9603a14SPatrick Farrell }
2799a9603a14SPatrick Farrell 
2800e882e171SHong Zhang /*@C
280165ba42b6SBarry Smith    TaoMonitorDrawCtxCreate - Creates the monitor context `TaoMonitorDrawSolution()`
2802a9603a14SPatrick Farrell 
280365ba42b6SBarry Smith    Collective on tao
2804e882e171SHong Zhang 
2805e882e171SHong Zhang    Output Patameter:
2806e882e171SHong Zhang .    ctx - the monitor context
2807e882e171SHong Zhang 
2808e882e171SHong Zhang    Options Database:
2809e882e171SHong Zhang .   -tao_draw_solution_initial - show initial guess as well as current solution
2810e882e171SHong Zhang 
2811e882e171SHong Zhang    Level: intermediate
2812e882e171SHong Zhang 
281365ba42b6SBarry Smith .seealso: `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()`
2814e882e171SHong Zhang @*/
2815e882e171SHong Zhang PetscErrorCode  TaoMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TaoMonitorDrawCtx *ctx)
2816e882e171SHong Zhang {
2817e882e171SHong Zhang   PetscFunctionBegin;
28189566063dSJacob Faibussowitsch   PetscCall(PetscNew(ctx));
28199566063dSJacob Faibussowitsch   PetscCall(PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer));
28209566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetFromOptions((*ctx)->viewer));
2821e882e171SHong Zhang   (*ctx)->howoften = howoften;
2822e882e171SHong Zhang   PetscFunctionReturn(0);
2823e882e171SHong Zhang }
2824e882e171SHong Zhang 
2825e882e171SHong Zhang /*@C
282665ba42b6SBarry Smith    TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorDrawSolution()`
2827e882e171SHong Zhang 
282865ba42b6SBarry Smith    Collective on tao
2829e882e171SHong Zhang 
2830e882e171SHong Zhang    Input Parameters:
2831e882e171SHong Zhang .    ctx - the monitor context
2832e882e171SHong Zhang 
2833e882e171SHong Zhang    Level: intermediate
2834e882e171SHong Zhang 
2835db781477SPatrick Sanan .seealso: `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawSolution()`
2836e882e171SHong Zhang @*/
2837e882e171SHong Zhang PetscErrorCode  TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx)
2838e882e171SHong Zhang {
2839e882e171SHong Zhang   PetscFunctionBegin;
28409566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*ictx)->viewer));
28419566063dSJacob Faibussowitsch   PetscCall(PetscFree(*ictx));
2842e882e171SHong Zhang   PetscFunctionReturn(0);
2843e882e171SHong Zhang }
2844