xref: /petsc/src/tao/interface/taosolver.c (revision 270bebe6e620a500094305dce0c421b2106ec210)
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 
24f1e99121SPierre Jolivet static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, void *ctx)
25d71ae5a4SJacob Faibussowitsch {
26f1e99121SPierre Jolivet   Tao  tao          = (Tao)ctx;
270f0abf79SStefano Zampini   SNES snes_ewdummy = tao->snes_ewdummy;
280f0abf79SStefano Zampini 
290f0abf79SStefano Zampini   PetscFunctionBegin;
303ba16761SJacob Faibussowitsch   if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS);
310f0abf79SStefano Zampini   /* populate snes_ewdummy struct values used in KSPPreSolve_SNESEW */
320f0abf79SStefano Zampini   snes_ewdummy->vec_func = b;
330f0abf79SStefano Zampini   snes_ewdummy->rtol     = tao->gttol;
340f0abf79SStefano Zampini   snes_ewdummy->iter     = tao->niter;
350f0abf79SStefano Zampini   PetscCall(VecNorm(b, NORM_2, &snes_ewdummy->norm));
360f0abf79SStefano Zampini   PetscCall(KSPPreSolve_SNESEW(ksp, b, x, snes_ewdummy));
370f0abf79SStefano Zampini   snes_ewdummy->vec_func = NULL;
383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
390f0abf79SStefano Zampini }
400f0abf79SStefano Zampini 
41f1e99121SPierre Jolivet static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, void *ctx)
42d71ae5a4SJacob Faibussowitsch {
43f1e99121SPierre Jolivet   Tao  tao          = (Tao)ctx;
440f0abf79SStefano Zampini   SNES snes_ewdummy = tao->snes_ewdummy;
450f0abf79SStefano Zampini 
460f0abf79SStefano Zampini   PetscFunctionBegin;
473ba16761SJacob Faibussowitsch   if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS);
480f0abf79SStefano Zampini   PetscCall(KSPPostSolve_SNESEW(ksp, b, x, snes_ewdummy));
493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
500f0abf79SStefano Zampini }
510f0abf79SStefano Zampini 
52d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSetUpEW_Private(Tao tao)
53d71ae5a4SJacob Faibussowitsch {
540f0abf79SStefano Zampini   SNESKSPEW  *kctx;
550f0abf79SStefano Zampini   const char *ewprefix;
560f0abf79SStefano Zampini 
570f0abf79SStefano Zampini   PetscFunctionBegin;
583ba16761SJacob Faibussowitsch   if (!tao->ksp) PetscFunctionReturn(PETSC_SUCCESS);
590f0abf79SStefano Zampini   if (tao->ksp_ewconv) {
600f0abf79SStefano Zampini     if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao), &tao->snes_ewdummy));
610f0abf79SStefano Zampini     tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE;
62f1e99121SPierre Jolivet     PetscCall(KSPSetPreSolve(tao->ksp, KSPPreSolve_TAOEW_Private, tao));
63f1e99121SPierre Jolivet     PetscCall(KSPSetPostSolve(tao->ksp, KSPPostSolve_TAOEW_Private, tao));
640f0abf79SStefano Zampini 
650f0abf79SStefano Zampini     PetscCall(KSPGetOptionsPrefix(tao->ksp, &ewprefix));
660f0abf79SStefano Zampini     kctx = (SNESKSPEW *)tao->snes_ewdummy->kspconvctx;
67a4598233SStefano Zampini     PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_FALSE, PetscObjectComm((PetscObject)tao), ewprefix));
680f0abf79SStefano Zampini   } else PetscCall(SNESDestroy(&tao->snes_ewdummy));
693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
700f0abf79SStefano Zampini }
710f0abf79SStefano Zampini 
72a7e14dcfSSatish Balay /*@
7365ba42b6SBarry Smith   TaoCreate - Creates a Tao solver
74a7e14dcfSSatish Balay 
75d083f849SBarry Smith   Collective
76a7e14dcfSSatish Balay 
77a7e14dcfSSatish Balay   Input Parameter:
78a7e14dcfSSatish Balay . comm - MPI communicator
79a7e14dcfSSatish Balay 
80a7e14dcfSSatish Balay   Output Parameter:
8147450a7bSBarry Smith . newtao - the new `Tao` context
82a7e14dcfSSatish Balay 
8347450a7bSBarry Smith   Options Database Key:
8465ba42b6SBarry Smith . -tao_type - select which method Tao should use
85a7e14dcfSSatish Balay 
86a7e14dcfSSatish Balay   Level: beginner
87a7e14dcfSSatish Balay 
881cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TAOSetFromOptions()`, `TAOSetType()`
89a7e14dcfSSatish Balay @*/
90d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao)
91d71ae5a4SJacob Faibussowitsch {
92441846f8SBarry Smith   Tao tao;
93a7e14dcfSSatish Balay 
94a7e14dcfSSatish Balay   PetscFunctionBegin;
954f572ea9SToby Isaac   PetscAssertPointer(newtao, 2);
969566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
979566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchInitializePackage());
989566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView));
99a7e14dcfSSatish Balay 
10094511df7SStefano Zampini   /* Set non-NULL defaults */
10194511df7SStefano Zampini   tao->ops->convergencetest = TaoDefaultConvergenceTest;
102a7e14dcfSSatish Balay 
103a7e14dcfSSatish Balay   tao->max_it    = 10000;
10494511df7SStefano Zampini   tao->max_funcs = -1;
1056f4723b1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
1066f4723b1SBarry Smith   tao->gatol = 1e-5;
1076f4723b1SBarry Smith   tao->grtol = 1e-5;
1086f1f5f59SAlp Dener   tao->crtol = 1e-5;
1096f1f5f59SAlp Dener   tao->catol = 1e-5;
1106f4723b1SBarry Smith #else
111a7e14dcfSSatish Balay   tao->gatol = 1e-8;
112a7e14dcfSSatish Balay   tao->grtol = 1e-8;
1136f1f5f59SAlp Dener   tao->crtol = 1e-8;
1146f1f5f59SAlp Dener   tao->catol = 1e-8;
1156f4723b1SBarry Smith #endif
1166552cf8aSJason Sarich   tao->gttol   = 0.0;
1174f1535bcSTodd Munson   tao->steptol = 0.0;
118e270355aSBarry Smith   tao->trust0  = PETSC_INFINITY;
1196f4723b1SBarry Smith   tao->fmin    = PETSC_NINFINITY;
1209fa2b5dcSStefano Zampini 
121a7e14dcfSSatish Balay   tao->hist_reset = PETSC_TRUE;
122a7e14dcfSSatish Balay 
1239566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
124a7e14dcfSSatish Balay   *newtao = tao;
1253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
126a7e14dcfSSatish Balay }
127a7e14dcfSSatish Balay 
128a7e14dcfSSatish Balay /*@
129a7e14dcfSSatish Balay   TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
130a7e14dcfSSatish Balay 
131c3339decSBarry Smith   Collective
132a7e14dcfSSatish Balay 
13347450a7bSBarry Smith   Input Parameter:
13447450a7bSBarry Smith . tao - the `Tao` context
135a7e14dcfSSatish Balay 
13667be906fSBarry Smith   Level: beginner
13767be906fSBarry Smith 
138a7e14dcfSSatish Balay   Notes:
13947450a7bSBarry Smith   The user must set up the `Tao` object  with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`.
140a7e14dcfSSatish Balay 
14167be906fSBarry Smith   You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or
142a35d58b8SBarry Smith   why it failed.
143a35d58b8SBarry Smith 
1441cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()`
145a7e14dcfSSatish Balay  @*/
146d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolve(Tao tao)
147d71ae5a4SJacob Faibussowitsch {
148e2379f4fSBarry Smith   static PetscBool set = PETSC_FALSE;
14947a47007SBarry Smith 
150a7e14dcfSSatish Balay   PetscFunctionBegin;
151441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
152d0609cedSBarry Smith   PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n"
153e2379f4fSBarry Smith                                    "title   = {Toolkit for Advanced Optimization (TAO) Users Manual},\n"
154e2379f4fSBarry Smith                                    "author  = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n"
155e2379f4fSBarry Smith                                    "Institution = {Argonne National Laboratory},\n"
156e2379f4fSBarry Smith                                    "Year   = 2014,\n"
157e2379f4fSBarry Smith                                    "Number = {ANL/MCS-TM-322 - Revision 3.5},\n"
1589371c9d4SSatish Balay                                    "url    = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n",
1599371c9d4SSatish Balay                                    &set));
160494bef23SAlp Dener   tao->header_printed = PETSC_FALSE;
1619566063dSJacob Faibussowitsch   PetscCall(TaoSetUp(tao));
1629566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
1631baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch));
164a7e14dcfSSatish Balay 
1659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0));
166dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, solve);
1679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0));
168a7e14dcfSSatish Balay 
1699566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution"));
1707cf37e64SBarry Smith 
1718931d482SJason Sarich   tao->ntotalits += tao->niter;
172a7e14dcfSSatish Balay 
173a7e14dcfSSatish Balay   if (tao->printreason) {
174f642d338SBarry Smith     PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
175f642d338SBarry Smith     PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel));
176a7e14dcfSSatish Balay     if (tao->reason > 0) {
177f642d338SBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter));
178a7e14dcfSSatish Balay     } else {
179f642d338SBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter));
180a7e14dcfSSatish Balay     }
181f642d338SBarry Smith     PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel));
182a7e14dcfSSatish Balay   }
183f642d338SBarry Smith   PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view"));
1843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
185a7e14dcfSSatish Balay }
186a7e14dcfSSatish Balay 
187a7e14dcfSSatish Balay /*@
188a7e14dcfSSatish Balay   TaoSetUp - Sets up the internal data structures for the later use
189a7e14dcfSSatish Balay   of a Tao solver
190a7e14dcfSSatish Balay 
191c3339decSBarry Smith   Collective
192a7e14dcfSSatish Balay 
19347450a7bSBarry Smith   Input Parameter:
19447450a7bSBarry Smith . tao - the `Tao` context
195a7e14dcfSSatish Balay 
19667be906fSBarry Smith   Level: advanced
19767be906fSBarry Smith 
19847450a7bSBarry Smith   Note:
19965ba42b6SBarry Smith   The user will not need to explicitly call `TaoSetUp()`, as it will
20065ba42b6SBarry Smith   automatically be called in `TaoSolve()`.  However, if the user
20165ba42b6SBarry Smith   desires to call it explicitly, it should come after `TaoCreate()`
20265ba42b6SBarry Smith   and any TaoSetSomething() routines, but before `TaoSolve()`.
203a7e14dcfSSatish Balay 
2041cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
205a7e14dcfSSatish Balay @*/
206d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp(Tao tao)
207d71ae5a4SJacob Faibussowitsch {
208a7e14dcfSSatish Balay   PetscFunctionBegin;
209441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2103ba16761SJacob Faibussowitsch   if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
2110f0abf79SStefano Zampini   PetscCall(TaoSetUpEW_Private(tao));
2123c859ba3SBarry Smith   PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution");
213dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, setup);
214a7e14dcfSSatish Balay   tao->setupcalled = PETSC_TRUE;
2153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
216a7e14dcfSSatish Balay }
217a7e14dcfSSatish Balay 
2180764c050SBarry Smith /*@
21947450a7bSBarry Smith   TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()`
220a7e14dcfSSatish Balay 
221c3339decSBarry Smith   Collective
222a7e14dcfSSatish Balay 
223a7e14dcfSSatish Balay   Input Parameter:
22447450a7bSBarry Smith . tao - the `Tao` context
225a7e14dcfSSatish Balay 
226a7e14dcfSSatish Balay   Level: beginner
227a7e14dcfSSatish Balay 
2281cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
229a7e14dcfSSatish Balay @*/
230d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy(Tao *tao)
231d71ae5a4SJacob Faibussowitsch {
232a7e14dcfSSatish Balay   PetscFunctionBegin;
2333ba16761SJacob Faibussowitsch   if (!*tao) PetscFunctionReturn(PETSC_SUCCESS);
234441846f8SBarry Smith   PetscValidHeaderSpecific(*tao, TAO_CLASSID, 1);
2359371c9d4SSatish Balay   if (--((PetscObject)*tao)->refct > 0) {
2369371c9d4SSatish Balay     *tao = NULL;
2373ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
238a7e14dcfSSatish Balay   }
2399371c9d4SSatish Balay 
240213acdd3SPierre Jolivet   PetscTryTypeMethod(*tao, destroy);
2419566063dSJacob Faibussowitsch   PetscCall(KSPDestroy(&(*tao)->ksp));
2420f0abf79SStefano Zampini   PetscCall(SNESDestroy(&(*tao)->snes_ewdummy));
2439566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch));
244a7e14dcfSSatish Balay 
245a7e14dcfSSatish Balay   if ((*tao)->ops->convergencedestroy) {
2469566063dSJacob Faibussowitsch     PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP));
24748a46eb9SPierre Jolivet     if ((*tao)->jacobian_state_inv) PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
248a7e14dcfSSatish Balay   }
2499566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->solution));
2509566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->gradient));
2519566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->ls_res));
252a7e14dcfSSatish Balay 
253a9603a14SPatrick Farrell   if ((*tao)->gradient_norm) {
2549566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm));
2559566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp));
256a9603a14SPatrick Farrell   }
257a9603a14SPatrick Farrell 
2589566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XL));
2599566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XU));
2609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IL));
2619566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IU));
2629566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DE));
2639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DI));
2649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints));
2659566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_equality));
2669566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_inequality));
2679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->stepdirection));
2689566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian_pre));
2699566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian));
2709566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac));
2719566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac_pre));
2729566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_pre));
2739566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian));
2749566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_pre));
2759566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state));
2769566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
2779566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_design));
2789566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality));
2799566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre));
2809566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality));
2819566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre));
2829566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->state_is));
2839566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->design_is));
2849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->res_weights_v));
285b2dc45ddSPierre Jolivet   PetscCall(TaoMonitorCancel(*tao));
28648a46eb9SPierre Jolivet   if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits));
287737f463aSAlp Dener   if ((*tao)->res_weights_n) {
2889566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_rows));
2899566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_cols));
2909566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_w));
291125ddc8aSJason Sarich   }
2929566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(tao));
2933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
294a7e14dcfSSatish Balay }
295a7e14dcfSSatish Balay 
296a7e14dcfSSatish Balay /*@
2971d27aa22SBarry Smith   TaoKSPSetUseEW - Sets `SNES` to use Eisenstat-Walker method {cite}`ew96`for computing relative tolerance for linear solvers.
2980f0abf79SStefano Zampini 
299c3339decSBarry Smith   Logically Collective
3000f0abf79SStefano Zampini 
3010f0abf79SStefano Zampini   Input Parameters:
3020f0abf79SStefano Zampini + tao  - Tao context
30365ba42b6SBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE`
3040f0abf79SStefano Zampini 
30567be906fSBarry Smith   Level: advanced
30667be906fSBarry Smith 
30747450a7bSBarry Smith   Note:
30865ba42b6SBarry Smith   See `SNESKSPSetUseEW()` for customization details.
3090f0abf79SStefano Zampini 
3101cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `SNESKSPSetUseEW()`
3110f0abf79SStefano Zampini @*/
312d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag)
313d71ae5a4SJacob Faibussowitsch {
3140f0abf79SStefano Zampini   PetscFunctionBegin;
3150f0abf79SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3160f0abf79SStefano Zampini   PetscValidLogicalCollectiveBool(tao, flag, 2);
3170f0abf79SStefano Zampini   tao->ksp_ewconv = flag;
3183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3190f0abf79SStefano Zampini }
3200f0abf79SStefano Zampini 
3210f0abf79SStefano Zampini /*@
32247450a7bSBarry Smith   TaoSetFromOptions - Sets various Tao parameters from the options database
323a7e14dcfSSatish Balay 
324c3339decSBarry Smith   Collective
325a7e14dcfSSatish Balay 
32601d2d390SJose E. Roman   Input Parameter:
32747450a7bSBarry Smith . tao - the `Tao` solver context
328a7e14dcfSSatish Balay 
32947450a7bSBarry Smith   Options Database Keys:
33065ba42b6SBarry Smith + -tao_type <type>             - The algorithm that Tao uses (lmvm, nls, etc.)
331a7e14dcfSSatish Balay . -tao_gatol <gatol>           - absolute error tolerance for ||gradient||
332a7e14dcfSSatish Balay . -tao_grtol <grtol>           - relative error tolerance for ||gradient||
333a7e14dcfSSatish Balay . -tao_gttol <gttol>           - reduction of ||gradient|| relative to initial gradient
334a7e14dcfSSatish Balay . -tao_max_it <max>            - sets maximum number of iterations
335a7e14dcfSSatish Balay . -tao_max_funcs <max>         - sets maximum number of function evaluations
336a7e14dcfSSatish Balay . -tao_fmin <fmin>             - stop if function value reaches fmin
337a7e14dcfSSatish Balay . -tao_steptol <tol>           - stop if trust region radius less than <tol>
338a7e14dcfSSatish Balay . -tao_trust0 <t>              - initial trust region radius
33910978b7dSBarry Smith . -tao_view_solution           - view the solution at the end of the optimization process
34047450a7bSBarry Smith . -tao_monitor                 - prints function value and residual norm at each iteration
34110978b7dSBarry Smith . -tao_monitor_short           - same as `-tao_monitor`, but truncates very small values
34210978b7dSBarry Smith . -tao_monitor_constraint_norm - prints objective value, gradient, and constraint norm at each iteration
34310978b7dSBarry Smith . -tao_monitor_globalization   - prints information about the globalization at each iteration
34410978b7dSBarry Smith . -tao_monitor_solution        - prints solution vector at each iteration
34510978b7dSBarry Smith . -tao_monitor_ls_residual     - prints least-squares residual vector at each iteration
34610978b7dSBarry Smith . -tao_monitor_step            - prints step vector at each iteration
34710978b7dSBarry Smith . -tao_monitor_gradient        - prints gradient vector at each iteration
34810978b7dSBarry Smith . -tao_monitor_solution_draw   - graphically view solution vector at each iteration
34910978b7dSBarry Smith . -tao_monitor_step_draw       - graphically view step vector at each iteration
35010978b7dSBarry Smith . -tao_monitor_gradient_draw   - graphically view gradient at each iteration
35110978b7dSBarry Smith . -tao_monitor_cancel          - cancels all monitors (except those set with command line)
352a7e14dcfSSatish Balay . -tao_fd_gradient             - use gradient computed with finite differences
353f4c1ad5cSStefano Zampini . -tao_fd_hessian              - use hessian computed with finite differences
35410978b7dSBarry Smith . -tao_mf_hessian              - use matrix-free Hessian computed with finite differences
355441846f8SBarry Smith . -tao_view                    - prints information about the Tao after solving
35665ba42b6SBarry Smith - -tao_converged_reason        - prints the reason Tao stopped iterating
357a7e14dcfSSatish Balay 
35867be906fSBarry Smith   Level: beginner
35967be906fSBarry Smith 
36067be906fSBarry Smith   Note:
36147450a7bSBarry Smith   To see all options, run your program with the `-help` option or consult the
36265ba42b6SBarry Smith   user's manual. Should be called after `TaoCreate()` but before `TaoSolve()`
363a7e14dcfSSatish Balay 
3641cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
365a7e14dcfSSatish Balay @*/
366d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions(Tao tao)
367d71ae5a4SJacob Faibussowitsch {
368b625d6c7SJed Brown   TaoType     default_type = TAOLMVM;
369a7e14dcfSSatish Balay   char        type[256], monfilename[PETSC_MAX_PATH_LEN];
370a7e14dcfSSatish Balay   PetscViewer monviewer;
371e876fe75SStephan Köhler   PetscBool   flg, found;
372a7e14dcfSSatish Balay   MPI_Comm    comm;
373a7e14dcfSSatish Balay 
374a7e14dcfSSatish Balay   PetscFunctionBegin;
375441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)tao, &comm));
377125ddc8aSJason Sarich 
37860b70c5cSStefano Zampini   if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name;
37960b70c5cSStefano Zampini 
380d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)tao);
381a7e14dcfSSatish Balay   /* Check for type from options */
3829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg));
383a7e14dcfSSatish Balay   if (flg) {
3849566063dSJacob Faibussowitsch     PetscCall(TaoSetType(tao, type));
385a7e14dcfSSatish Balay   } else if (!((PetscObject)tao)->type_name) {
3869566063dSJacob Faibussowitsch     PetscCall(TaoSetType(tao, default_type));
387a7e14dcfSSatish Balay   }
388a7e14dcfSSatish Balay 
38960b70c5cSStefano Zampini   /* Tao solvers do not set the prefix, set it here if not yet done
39060b70c5cSStefano Zampini      We do it after SetType since solver may have been changed */
39160b70c5cSStefano Zampini   if (tao->linesearch) {
39260b70c5cSStefano Zampini     const char *prefix;
39360b70c5cSStefano Zampini     PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix));
394f4f49eeaSPierre Jolivet     if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)tao)->prefix));
39560b70c5cSStefano Zampini   }
39660b70c5cSStefano Zampini 
3979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &tao->catol, &flg));
3986552cf8aSJason Sarich   if (flg) tao->catol_changed = PETSC_TRUE;
3996aad120cSJose E. Roman   PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &tao->crtol, &flg));
4006552cf8aSJason Sarich   if (flg) tao->crtol_changed = PETSC_TRUE;
4019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &tao->gatol, &flg));
4026552cf8aSJason Sarich   if (flg) tao->gatol_changed = PETSC_TRUE;
4039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &tao->grtol, &flg));
4046552cf8aSJason Sarich   if (flg) tao->grtol_changed = PETSC_TRUE;
4059566063dSJacob 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));
4066552cf8aSJason Sarich   if (flg) tao->gttol_changed = PETSC_TRUE;
4079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg));
4086552cf8aSJason Sarich   if (flg) tao->max_it_changed = PETSC_TRUE;
4099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg));
4106552cf8aSJason Sarich   if (flg) tao->max_funcs_changed = PETSC_TRUE;
4119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, &flg));
4126552cf8aSJason Sarich   if (flg) tao->fmin_changed = PETSC_TRUE;
4139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, &flg));
4146552cf8aSJason Sarich   if (flg) tao->steptol_changed = PETSC_TRUE;
4159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetTrustRegionRadius", tao->trust0, &tao->trust0, &flg));
4166552cf8aSJason Sarich   if (flg) tao->trust0_changed = PETSC_TRUE;
41710978b7dSBarry Smith 
41810978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_solution_monitor", "-tao_monitor_solution", "3.21", NULL));
41910978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_gradient_monitor", "-tao_monitor_gradient", "3.21", NULL));
42010978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_stepdirection_monitor", "-tao_monitor_step", "3.21", NULL));
42110978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_residual_monitor", "-tao_monitor_residual", "3.21", NULL));
42210978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_smonitor", "-tao_monitor_short", "3.21", NULL));
42310978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_cmonitor", "-tao_monitor_constraint_norm", "3.21", NULL));
42410978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_gmonitor", "-tao_monitor_globalization", "3.21", NULL));
42510978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_solution", "-tao_monitor_solution_draw", "3.21", NULL));
42610978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_gradient", "-tao_monitor_gradient_draw", "3.21", NULL));
42710978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_step", "-tao_monitor_step_draw", "3.21", NULL));
42810978b7dSBarry Smith 
42910978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_solution", "View solution vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
430a7e14dcfSSatish Balay   if (flg) {
4319566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
43210978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorSolution, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
433a7e14dcfSSatish Balay   }
434a7e14dcfSSatish Balay 
43565ba42b6SBarry Smith   PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL));
43610978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_gradient", "View gradient vector for each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
437a7e14dcfSSatish Balay   if (flg) {
4389566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
43910978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGradient, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
440a7e14dcfSSatish Balay   }
441a7e14dcfSSatish Balay 
44210978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_step", "View step vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
443a7e14dcfSSatish Balay   if (flg) {
4449566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
44510978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorStep, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
446a7e14dcfSSatish Balay   }
447a7e14dcfSSatish Balay 
44810978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_residual", "View least-squares residual vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
449a7e14dcfSSatish Balay   if (flg) {
4509566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
45110978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorResidual, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
452a7e14dcfSSatish Balay   }
453a7e14dcfSSatish Balay 
45410978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor", "Use the default convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
455a7e14dcfSSatish Balay   if (flg) {
4569566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
45710978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorDefault, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
458a7e14dcfSSatish Balay   }
459a7e14dcfSSatish Balay 
46010978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_globalization", "Use the convergence monitor with extra globalization info", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
4618d5ead36SAlp Dener   if (flg) {
4629566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
46310978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGlobalization, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
4648d5ead36SAlp Dener   }
4658d5ead36SAlp Dener 
46610978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_short", "Use the short convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
467a7e14dcfSSatish Balay   if (flg) {
4689566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
46910978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorDefaultShort, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
470a7e14dcfSSatish Balay   }
471a7e14dcfSSatish Balay 
47210978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_constraint_norm", "Use the default convergence monitor with constraint norm", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
473a7e14dcfSSatish Balay   if (flg) {
4749566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
47510978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorConstraintNorm, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy));
476a7e14dcfSSatish Balay   }
477a7e14dcfSSatish Balay 
4788afaa268SBarry Smith   flg = PETSC_FALSE;
479b2dc45ddSPierre Jolivet   PetscCall(PetscOptionsDeprecated("-tao_cancelmonitors", "-tao_monitor_cancel", "3.21", NULL));
480b2dc45ddSPierre Jolivet   PetscCall(PetscOptionsBool("-tao_monitor_cancel", "cancel all monitors and call any registered destroy routines", "TaoMonitorCancel", flg, &flg, NULL));
481b2dc45ddSPierre Jolivet   if (flg) PetscCall(TaoMonitorCancel(tao));
482a7e14dcfSSatish Balay 
4838afaa268SBarry Smith   flg = PETSC_FALSE;
48410978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_solution_draw", "Plot solution vector at each iteration", "TaoMonitorSet", flg, &flg, NULL));
485a7e14dcfSSatish Balay   if (flg) {
486e882e171SHong Zhang     TaoMonitorDrawCtx drawctx;
487e882e171SHong Zhang     PetscInt          howoften = 1;
4889566063dSJacob Faibussowitsch     PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
48910978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorSolutionDraw, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy));
490a7e14dcfSSatish Balay   }
491a7e14dcfSSatish Balay 
4928afaa268SBarry Smith   flg = PETSC_FALSE;
49310978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_step_draw", "Plots step at each iteration", "TaoMonitorSet", flg, &flg, NULL));
49410978b7dSBarry Smith   if (flg) PetscCall(TaoMonitorSet(tao, TaoMonitorStepDraw, NULL, NULL));
495a7e14dcfSSatish Balay 
4968afaa268SBarry Smith   flg = PETSC_FALSE;
49710978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_gradient_draw", "plots gradient at each iteration", "TaoMonitorSet", flg, &flg, NULL));
498a7e14dcfSSatish Balay   if (flg) {
499e882e171SHong Zhang     TaoMonitorDrawCtx drawctx;
500e882e171SHong Zhang     PetscInt          howoften = 1;
5019566063dSJacob Faibussowitsch     PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
50210978b7dSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGradientDraw, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy));
503a7e14dcfSSatish Balay   }
5048afaa268SBarry Smith   flg = PETSC_FALSE;
5059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL));
5061baa6e33SBarry Smith   if (flg) PetscCall(TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, NULL));
507f4c1ad5cSStefano Zampini   flg = PETSC_FALSE;
50810978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute Hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL));
509f4c1ad5cSStefano Zampini   if (flg) {
510f4c1ad5cSStefano Zampini     Mat H;
511f4c1ad5cSStefano Zampini 
5129566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H));
5139566063dSJacob Faibussowitsch     PetscCall(MatSetType(H, MATAIJ));
5149566063dSJacob Faibussowitsch     PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL));
5159566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&H));
516f4c1ad5cSStefano Zampini   }
517f4c1ad5cSStefano Zampini   flg = PETSC_FALSE;
51810978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free Hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL));
519f4c1ad5cSStefano Zampini   if (flg) {
520f4c1ad5cSStefano Zampini     Mat H;
521f4c1ad5cSStefano Zampini 
5229566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H));
5239566063dSJacob Faibussowitsch     PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL));
5249566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&H));
525f4c1ad5cSStefano Zampini   }
526e876fe75SStephan Köhler   PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, &found));
527e876fe75SStephan Köhler   if (found) PetscCall(TaoSetRecycleHistory(tao, flg));
5289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubSetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL));
529a7e14dcfSSatish Balay 
5300f0abf79SStefano Zampini   if (tao->ksp) {
5310f0abf79SStefano Zampini     PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL));
5320f0abf79SStefano Zampini     PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv));
5330f0abf79SStefano Zampini   }
5340f0abf79SStefano Zampini 
535dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject);
53660b70c5cSStefano Zampini 
53760b70c5cSStefano Zampini   /* process any options handlers added with PetscObjectAddOptionsHandler() */
53860b70c5cSStefano Zampini   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject));
539d0609cedSBarry Smith   PetscOptionsEnd();
54060b70c5cSStefano Zampini 
54160b70c5cSStefano Zampini   if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
5423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
543a7e14dcfSSatish Balay }
544a7e14dcfSSatish Balay 
545ffeef943SBarry Smith /*@
54647450a7bSBarry Smith   TaoViewFromOptions - View a `Tao` object based on values in the options database
547fe2efc57SMark 
548c3339decSBarry Smith   Collective
549fe2efc57SMark 
550fe2efc57SMark   Input Parameters:
55147450a7bSBarry Smith + A    - the  `Tao` context
55247450a7bSBarry Smith . obj  - Optional object that provides the prefix for the options database
553736c3998SJose E. Roman - name - command line option
554fe2efc57SMark 
555fe2efc57SMark   Level: intermediate
55667be906fSBarry Smith 
5571cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()`
558fe2efc57SMark @*/
559d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[])
560d71ae5a4SJacob Faibussowitsch {
561fe2efc57SMark   PetscFunctionBegin;
562fe2efc57SMark   PetscValidHeaderSpecific(A, TAO_CLASSID, 1);
5639566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
5643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
565fe2efc57SMark }
566fe2efc57SMark 
567ffeef943SBarry Smith /*@
56847450a7bSBarry Smith   TaoView - Prints information about the `Tao` object
569a7e14dcfSSatish Balay 
570c3339decSBarry Smith   Collective
571a7e14dcfSSatish Balay 
572a7e14dcfSSatish Balay   Input Parameters:
57347450a7bSBarry Smith + tao    - the `Tao` context
574a7e14dcfSSatish Balay - viewer - visualization context
575a7e14dcfSSatish Balay 
576a7e14dcfSSatish Balay   Options Database Key:
57765ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()`
578a7e14dcfSSatish Balay 
57967be906fSBarry Smith   Level: beginner
58067be906fSBarry Smith 
581a7e14dcfSSatish Balay   Notes:
582a7e14dcfSSatish Balay   The available visualization contexts include
58365ba42b6SBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
58465ba42b6SBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
585a7e14dcfSSatish Balay   output where only the first processor opens
586a7e14dcfSSatish Balay   the file.  All other processors send their
587a7e14dcfSSatish Balay   data to the first processor to print.
588a7e14dcfSSatish Balay 
5891cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `PetscViewerASCIIOpen()`
590a7e14dcfSSatish Balay @*/
591d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView(Tao tao, PetscViewer viewer)
592d71ae5a4SJacob Faibussowitsch {
593a7e14dcfSSatish Balay   PetscBool isascii, isstring;
594b625d6c7SJed Brown   TaoType   type;
59547a47007SBarry Smith 
596a7e14dcfSSatish Balay   PetscFunctionBegin;
597441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
59848a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer));
599a7e14dcfSSatish Balay   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
600a7e14dcfSSatish Balay   PetscCheckSameComm(tao, 1, viewer, 2);
601a7e14dcfSSatish Balay 
6029566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
6039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
604a7e14dcfSSatish Balay   if (isascii) {
6059566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer));
606a7e14dcfSSatish Balay 
607a7e14dcfSSatish Balay     if (tao->ops->view) {
6089566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
609dbbe0bcdSBarry Smith       PetscUseTypeMethod(tao, view, viewer);
6109566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
611a7e14dcfSSatish Balay     }
6128d3f3ddaSAlp Dener     if (tao->linesearch) {
6139566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
6149566063dSJacob Faibussowitsch       PetscCall(TaoLineSearchView(tao->linesearch, viewer));
6159566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
6168d3f3ddaSAlp Dener     }
617a7e14dcfSSatish Balay     if (tao->ksp) {
6189566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPushTab(viewer));
6199566063dSJacob Faibussowitsch       PetscCall(KSPView(tao->ksp, viewer));
62063a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its));
6219566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPopTab(viewer));
622a7e14dcfSSatish Balay     }
62319b5c101SAlp Dener 
6249566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
62519b5c101SAlp Dener 
62648a46eb9SPierre Jolivet     if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubSetTypes[tao->subset_type]));
627a7e14dcfSSatish Balay 
6289566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol));
6299566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol));
6309566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol));
6319566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual));
632a7e14dcfSSatish Balay 
6336246e8cdSAlp Dener     if (tao->constrained) {
6349566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:"));
6359566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol));
6369566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol));
6379566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm));
638a7e14dcfSSatish Balay     }
639a7e14dcfSSatish Balay 
640a7e14dcfSSatish Balay     if (tao->trust < tao->steptol) {
6419566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol));
6429566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust));
643a7e14dcfSSatish Balay     }
644a7e14dcfSSatish Balay 
64548a46eb9SPierre Jolivet     if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin));
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     }
66348a46eb9SPierre Jolivet     if (tao->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->nhess));
66448a46eb9SPierre Jolivet     if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints));
66548a46eb9SPierre Jolivet     if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac));
666a7e14dcfSSatish Balay 
667a7e14dcfSSatish Balay     if (tao->reason > 0) {
6689566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: "));
669a7e14dcfSSatish Balay       switch (tao->reason) {
670d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GATOL:
671d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n"));
672d71ae5a4SJacob Faibussowitsch         break;
673d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GRTOL:
674d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n"));
675d71ae5a4SJacob Faibussowitsch         break;
676d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GTTOL:
677d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n"));
678d71ae5a4SJacob Faibussowitsch         break;
679d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_STEPTOL:
680d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n"));
681d71ae5a4SJacob Faibussowitsch         break;
682d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_MINF:
683d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Minf --  f < fmin\n"));
684d71ae5a4SJacob Faibussowitsch         break;
685d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_USER:
686d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
687d71ae5a4SJacob Faibussowitsch         break;
688d71ae5a4SJacob Faibussowitsch       default:
689d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
690d71ae5a4SJacob Faibussowitsch         break;
691a7e14dcfSSatish Balay       }
692a7e14dcfSSatish Balay     } else {
6939566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver terminated: %d", tao->reason));
694a7e14dcfSSatish Balay       switch (tao->reason) {
695d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_MAXITS:
696d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n"));
697d71ae5a4SJacob Faibussowitsch         break;
698d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_NAN:
699d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " NAN or Inf encountered\n"));
700d71ae5a4SJacob Faibussowitsch         break;
701d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_MAXFCN:
702d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n"));
703d71ae5a4SJacob Faibussowitsch         break;
704d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_LS_FAILURE:
705d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n"));
706d71ae5a4SJacob Faibussowitsch         break;
707d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_TR_REDUCTION:
708d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n"));
709d71ae5a4SJacob Faibussowitsch         break;
710d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_USER:
711d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
712d71ae5a4SJacob Faibussowitsch         break;
713d71ae5a4SJacob Faibussowitsch       default:
714d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
715d71ae5a4SJacob Faibussowitsch         break;
716a7e14dcfSSatish Balay       }
717a7e14dcfSSatish Balay     }
7189566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
719a7e14dcfSSatish Balay   } else if (isstring) {
7209566063dSJacob Faibussowitsch     PetscCall(TaoGetType(tao, &type));
7219566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type));
722a7e14dcfSSatish Balay   }
7233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
724a7e14dcfSSatish Balay }
725a7e14dcfSSatish Balay 
726a7e14dcfSSatish Balay /*@
727414d97d3SAlp Dener   TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using
72865ba42b6SBarry Smith   iterate information from the previous `TaoSolve()`. This feature is disabled by
729414d97d3SAlp Dener   default.
730414d97d3SAlp Dener 
73167be906fSBarry Smith   Logically Collective
73267be906fSBarry Smith 
73367be906fSBarry Smith   Input Parameters:
73447450a7bSBarry Smith + tao     - the `Tao` context
73567be906fSBarry Smith - recycle - boolean flag
73667be906fSBarry Smith 
73747450a7bSBarry Smith   Options Database Key:
73867be906fSBarry Smith . -tao_recycle_history <true,false> - reuse the history
73967be906fSBarry Smith 
74067be906fSBarry Smith   Level: intermediate
74167be906fSBarry Smith 
74267be906fSBarry Smith   Notes:
74365ba42b6SBarry Smith   For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction
74465ba42b6SBarry Smith   from the previous `TaoSolve()` call when computing the first search direction in a
745414d97d3SAlp Dener   new solution. By default, CG methods set the first search direction to the
746414d97d3SAlp Dener   negative gradient.
747414d97d3SAlp Dener 
74865ba42b6SBarry Smith   For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses
74965ba42b6SBarry Smith   the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()`
750414d97d3SAlp Dener   call. By default, QN family of methods reset the initial Hessian approximation to
751414d97d3SAlp Dener   the identity matrix.
752414d97d3SAlp Dener 
753414d97d3SAlp Dener   For any other algorithm, this setting has no effect.
754414d97d3SAlp Dener 
7551cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
756414d97d3SAlp Dener @*/
757d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle)
758d71ae5a4SJacob Faibussowitsch {
759414d97d3SAlp Dener   PetscFunctionBegin;
760414d97d3SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
76194511df7SStefano Zampini   PetscValidLogicalCollectiveBool(tao, recycle, 2);
762414d97d3SAlp Dener   tao->recycle = recycle;
7633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
764414d97d3SAlp Dener }
765414d97d3SAlp Dener 
766414d97d3SAlp Dener /*@
767414d97d3SAlp Dener   TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information
76865ba42b6SBarry Smith   from the previous `TaoSolve()`. This feature is disabled by default.
769414d97d3SAlp Dener 
77067be906fSBarry Smith   Logically Collective
771414d97d3SAlp Dener 
77247450a7bSBarry Smith   Input Parameter:
77347450a7bSBarry Smith . tao - the `Tao` context
774414d97d3SAlp Dener 
77547450a7bSBarry Smith   Output Parameter:
776147403d9SBarry Smith . recycle - boolean flag
777414d97d3SAlp Dener 
778414d97d3SAlp Dener   Level: intermediate
779414d97d3SAlp Dener 
7801cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
781414d97d3SAlp Dener @*/
782d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle)
783d71ae5a4SJacob Faibussowitsch {
784414d97d3SAlp Dener   PetscFunctionBegin;
785414d97d3SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
7864f572ea9SToby Isaac   PetscAssertPointer(recycle, 2);
787414d97d3SAlp Dener   *recycle = tao->recycle;
7883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
789414d97d3SAlp Dener }
790414d97d3SAlp Dener 
791414d97d3SAlp Dener /*@
79247450a7bSBarry Smith   TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests
793a7e14dcfSSatish Balay 
79467be906fSBarry Smith   Logically Collective
795a7e14dcfSSatish Balay 
796a7e14dcfSSatish Balay   Input Parameters:
79747450a7bSBarry Smith + tao   - the `Tao` context
798a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this
799a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
800a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor
801a7e14dcfSSatish Balay 
802a7e14dcfSSatish Balay   Options Database Keys:
803e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol
804a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol
805a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol
806a7e14dcfSSatish Balay 
8073b242c63SJacob Faibussowitsch   Stopping Criteria\:
80867be906fSBarry Smith .vb
80967be906fSBarry Smith   ||g(X)||                            <= gatol
81067be906fSBarry Smith   ||g(X)|| / |f(X)|                   <= grtol
81167be906fSBarry Smith   ||g(X)|| / ||g(X0)||                <= gttol
81267be906fSBarry Smith .ve
813a7e14dcfSSatish Balay 
814a7e14dcfSSatish Balay   Level: beginner
815a7e14dcfSSatish Balay 
81667be906fSBarry Smith   Note:
81767be906fSBarry Smith   Use `PETSC_DEFAULT` to leave one or more tolerances unchanged.
818a7e14dcfSSatish Balay 
8191cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`
820a7e14dcfSSatish Balay @*/
821d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol)
822d71ae5a4SJacob Faibussowitsch {
823a7e14dcfSSatish Balay   PetscFunctionBegin;
824441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
82594511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, gatol, 2);
82694511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, grtol, 3);
82794511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, gttol, 4);
828a7e14dcfSSatish Balay 
82913bcc0bdSJacob Faibussowitsch   if (gatol != (PetscReal)PETSC_DEFAULT) {
830a7e14dcfSSatish Balay     if (gatol < 0) {
8319566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao, "Tried to set negative gatol -- ignored.\n"));
832a7e14dcfSSatish Balay     } else {
833a7e14dcfSSatish Balay       tao->gatol         = PetscMax(0, gatol);
8346552cf8aSJason Sarich       tao->gatol_changed = PETSC_TRUE;
835a7e14dcfSSatish Balay     }
836a7e14dcfSSatish Balay   }
837a7e14dcfSSatish Balay 
83813bcc0bdSJacob Faibussowitsch   if (grtol != (PetscReal)PETSC_DEFAULT) {
839a7e14dcfSSatish Balay     if (grtol < 0) {
8409566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao, "Tried to set negative grtol -- ignored.\n"));
841a7e14dcfSSatish Balay     } else {
842a7e14dcfSSatish Balay       tao->grtol         = PetscMax(0, grtol);
8436552cf8aSJason Sarich       tao->grtol_changed = PETSC_TRUE;
844a7e14dcfSSatish Balay     }
845a7e14dcfSSatish Balay   }
846a7e14dcfSSatish Balay 
84713bcc0bdSJacob Faibussowitsch   if (gttol != (PetscReal)PETSC_DEFAULT) {
848a7e14dcfSSatish Balay     if (gttol < 0) {
8499566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao, "Tried to set negative gttol -- ignored.\n"));
850a7e14dcfSSatish Balay     } else {
851a7e14dcfSSatish Balay       tao->gttol         = PetscMax(0, gttol);
8526552cf8aSJason Sarich       tao->gttol_changed = PETSC_TRUE;
853a7e14dcfSSatish Balay     }
854a7e14dcfSSatish Balay   }
8553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
856a7e14dcfSSatish Balay }
857a7e14dcfSSatish Balay 
858a7e14dcfSSatish Balay /*@
85947450a7bSBarry Smith   TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests
860a7e14dcfSSatish Balay 
86167be906fSBarry Smith   Logically Collective
862a7e14dcfSSatish Balay 
863a7e14dcfSSatish Balay   Input Parameters:
86447450a7bSBarry Smith + tao   - the `Tao` context
86567be906fSBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria
86667be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria
867a7e14dcfSSatish Balay 
868a7e14dcfSSatish Balay   Options Database Keys:
869a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol
870a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol
871a7e14dcfSSatish Balay 
872a7e14dcfSSatish Balay   Level: intermediate
873a7e14dcfSSatish Balay 
87467be906fSBarry Smith   Notes:
87567be906fSBarry Smith   Use `PETSC_DEFAULT` to leave any tolerance unchanged.
876a7e14dcfSSatish Balay 
8771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()`
878a7e14dcfSSatish Balay @*/
879d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol)
880d71ae5a4SJacob Faibussowitsch {
881a7e14dcfSSatish Balay   PetscFunctionBegin;
882441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
88394511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, catol, 2);
88494511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, crtol, 3);
885a7e14dcfSSatish Balay 
88613bcc0bdSJacob Faibussowitsch   if (catol != (PetscReal)PETSC_DEFAULT) {
887a7e14dcfSSatish Balay     if (catol < 0) {
8889566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao, "Tried to set negative catol -- ignored.\n"));
889a7e14dcfSSatish Balay     } else {
890a7e14dcfSSatish Balay       tao->catol         = PetscMax(0, catol);
8916552cf8aSJason Sarich       tao->catol_changed = PETSC_TRUE;
892a7e14dcfSSatish Balay     }
893a7e14dcfSSatish Balay   }
894a7e14dcfSSatish Balay 
89513bcc0bdSJacob Faibussowitsch   if (crtol != (PetscReal)PETSC_DEFAULT) {
896a7e14dcfSSatish Balay     if (crtol < 0) {
8979566063dSJacob Faibussowitsch       PetscCall(PetscInfo(tao, "Tried to set negative crtol -- ignored.\n"));
898a7e14dcfSSatish Balay     } else {
899a7e14dcfSSatish Balay       tao->crtol         = PetscMax(0, crtol);
9006552cf8aSJason Sarich       tao->crtol_changed = PETSC_TRUE;
901a7e14dcfSSatish Balay     }
902a7e14dcfSSatish Balay   }
9033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
904a7e14dcfSSatish Balay }
905a7e14dcfSSatish Balay 
90658417fe7SBarry Smith /*@
90747450a7bSBarry Smith   TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests
90858417fe7SBarry Smith 
90967be906fSBarry Smith   Not Collective
91058417fe7SBarry Smith 
91158417fe7SBarry Smith   Input Parameter:
91247450a7bSBarry Smith . tao - the `Tao` context
91358417fe7SBarry Smith 
914d8d19677SJose E. Roman   Output Parameters:
91567be906fSBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria
91667be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria
91758417fe7SBarry Smith 
91858417fe7SBarry Smith   Level: intermediate
91958417fe7SBarry Smith 
9201cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReasons`,`TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()`
92158417fe7SBarry Smith @*/
922d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol)
923d71ae5a4SJacob Faibussowitsch {
92458417fe7SBarry Smith   PetscFunctionBegin;
92558417fe7SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
92658417fe7SBarry Smith   if (catol) *catol = tao->catol;
92758417fe7SBarry Smith   if (crtol) *crtol = tao->crtol;
9283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92958417fe7SBarry Smith }
93058417fe7SBarry Smith 
931a7e14dcfSSatish Balay /*@
932a7e14dcfSSatish Balay   TaoSetFunctionLowerBound - Sets a bound on the solution objective value.
933a7e14dcfSSatish Balay   When an approximate solution with an objective value below this number
934a7e14dcfSSatish Balay   has been found, the solver will terminate.
935a7e14dcfSSatish Balay 
936c3339decSBarry Smith   Logically Collective
937a7e14dcfSSatish Balay 
938a7e14dcfSSatish Balay   Input Parameters:
939441846f8SBarry Smith + tao  - the Tao solver context
940a7e14dcfSSatish Balay - fmin - the tolerance
941a7e14dcfSSatish Balay 
94247450a7bSBarry Smith   Options Database Key:
943a7e14dcfSSatish Balay . -tao_fmin <fmin> - sets the minimum function value
944a7e14dcfSSatish Balay 
945a7e14dcfSSatish Balay   Level: intermediate
946a7e14dcfSSatish Balay 
9471cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()`
948a7e14dcfSSatish Balay @*/
949d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin)
950d71ae5a4SJacob Faibussowitsch {
951a7e14dcfSSatish Balay   PetscFunctionBegin;
952441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
95394511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, fmin, 2);
954a7e14dcfSSatish Balay   tao->fmin         = fmin;
9556552cf8aSJason Sarich   tao->fmin_changed = PETSC_TRUE;
9563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
957a7e14dcfSSatish Balay }
958a7e14dcfSSatish Balay 
959a7e14dcfSSatish Balay /*@
9608e96d397SJason Sarich   TaoGetFunctionLowerBound - Gets the bound on the solution objective value.
961a7e14dcfSSatish Balay   When an approximate solution with an objective value below this number
962a7e14dcfSSatish Balay   has been found, the solver will terminate.
963a7e14dcfSSatish Balay 
96467be906fSBarry Smith   Not Collective
965a7e14dcfSSatish Balay 
96647450a7bSBarry Smith   Input Parameter:
96747450a7bSBarry Smith . tao - the `Tao` solver context
968a7e14dcfSSatish Balay 
96947450a7bSBarry Smith   Output Parameter:
970a7e14dcfSSatish Balay . fmin - the minimum function value
971a7e14dcfSSatish Balay 
972a7e14dcfSSatish Balay   Level: intermediate
973a7e14dcfSSatish Balay 
9741cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()`
975a7e14dcfSSatish Balay @*/
976d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin)
977d71ae5a4SJacob Faibussowitsch {
978a7e14dcfSSatish Balay   PetscFunctionBegin;
979441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
9804f572ea9SToby Isaac   PetscAssertPointer(fmin, 2);
981a7e14dcfSSatish Balay   *fmin = tao->fmin;
9823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
983a7e14dcfSSatish Balay }
984a7e14dcfSSatish Balay 
985a7e14dcfSSatish Balay /*@
98647450a7bSBarry Smith   TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`.
987a7e14dcfSSatish Balay 
988c3339decSBarry Smith   Logically Collective
989a7e14dcfSSatish Balay 
990a7e14dcfSSatish Balay   Input Parameters:
99147450a7bSBarry Smith + tao  - the `Tao` solver context
992a7e14dcfSSatish Balay - nfcn - the maximum number of function evaluations (>=0)
993a7e14dcfSSatish Balay 
99447450a7bSBarry Smith   Options Database Key:
995a7e14dcfSSatish Balay . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations
996a7e14dcfSSatish Balay 
997a7e14dcfSSatish Balay   Level: intermediate
998a7e14dcfSSatish Balay 
9991cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()`
1000a7e14dcfSSatish Balay @*/
1001d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn)
1002d71ae5a4SJacob Faibussowitsch {
1003a7e14dcfSSatish Balay   PetscFunctionBegin;
1004441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
100594511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, nfcn, 2);
10069371c9d4SSatish Balay   if (nfcn >= 0) {
10079371c9d4SSatish Balay     tao->max_funcs = PetscMax(0, nfcn);
10089371c9d4SSatish Balay   } else {
10099371c9d4SSatish Balay     tao->max_funcs = -1;
10109371c9d4SSatish Balay   }
10116552cf8aSJason Sarich   tao->max_funcs_changed = PETSC_TRUE;
10123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1013a7e14dcfSSatish Balay }
1014a7e14dcfSSatish Balay 
1015a7e14dcfSSatish Balay /*@
101647450a7bSBarry Smith   TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()`
1017a7e14dcfSSatish Balay 
1018c3339decSBarry Smith   Logically Collective
1019a7e14dcfSSatish Balay 
102047450a7bSBarry Smith   Input Parameter:
102147450a7bSBarry Smith . tao - the `Tao` solver context
1022a7e14dcfSSatish Balay 
102347450a7bSBarry Smith   Output Parameter:
1024a7e14dcfSSatish Balay . nfcn - the maximum number of function evaluations
1025a7e14dcfSSatish Balay 
1026a7e14dcfSSatish Balay   Level: intermediate
1027a7e14dcfSSatish Balay 
10281cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1029a7e14dcfSSatish Balay @*/
1030d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn)
1031d71ae5a4SJacob Faibussowitsch {
1032a7e14dcfSSatish Balay   PetscFunctionBegin;
1033441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
10344f572ea9SToby Isaac   PetscAssertPointer(nfcn, 2);
1035a7e14dcfSSatish Balay   *nfcn = tao->max_funcs;
10363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1037a7e14dcfSSatish Balay }
1038a7e14dcfSSatish Balay 
1039770232b9SCe Qin /*@
104047450a7bSBarry Smith   TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object
1041770232b9SCe Qin 
1042770232b9SCe Qin   Not Collective
1043770232b9SCe Qin 
104447450a7bSBarry Smith   Input Parameter:
104547450a7bSBarry Smith . tao - the `Tao` solver context
1046770232b9SCe Qin 
104747450a7bSBarry Smith   Output Parameter:
104894511df7SStefano Zampini . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations)
1049770232b9SCe Qin 
1050770232b9SCe Qin   Level: intermediate
1051770232b9SCe Qin 
10521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1053770232b9SCe Qin @*/
1054d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs)
1055d71ae5a4SJacob Faibussowitsch {
1056770232b9SCe Qin   PetscFunctionBegin;
1057770232b9SCe Qin   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
10584f572ea9SToby Isaac   PetscAssertPointer(nfuncs, 2);
1059770232b9SCe Qin   *nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads);
10603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1061770232b9SCe Qin }
1062770232b9SCe Qin 
1063a7e14dcfSSatish Balay /*@
106447450a7bSBarry Smith   TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()`
1065a7e14dcfSSatish Balay 
1066c3339decSBarry Smith   Logically Collective
1067a7e14dcfSSatish Balay 
1068a7e14dcfSSatish Balay   Input Parameters:
106947450a7bSBarry Smith + tao    - the `Tao` solver context
1070a7e14dcfSSatish Balay - maxits - the maximum number of iterates (>=0)
1071a7e14dcfSSatish Balay 
107247450a7bSBarry Smith   Options Database Key:
1073a7e14dcfSSatish Balay . -tao_max_it <its> - sets the maximum number of iterations
1074a7e14dcfSSatish Balay 
1075a7e14dcfSSatish Balay   Level: intermediate
1076a7e14dcfSSatish Balay 
10771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()`
1078a7e14dcfSSatish Balay @*/
1079d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits)
1080d71ae5a4SJacob Faibussowitsch {
1081a7e14dcfSSatish Balay   PetscFunctionBegin;
1082441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
108394511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, maxits, 2);
108447a47007SBarry Smith   tao->max_it         = PetscMax(0, maxits);
10856552cf8aSJason Sarich   tao->max_it_changed = PETSC_TRUE;
10863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1087a7e14dcfSSatish Balay }
1088a7e14dcfSSatish Balay 
1089a7e14dcfSSatish Balay /*@
109065ba42b6SBarry Smith   TaoGetMaximumIterations - Gets a maximum number of iterates that will be used
1091a7e14dcfSSatish Balay 
1092a7e14dcfSSatish Balay   Not Collective
1093a7e14dcfSSatish Balay 
109447450a7bSBarry Smith   Input Parameter:
109547450a7bSBarry Smith . tao - the `Tao` solver context
1096a7e14dcfSSatish Balay 
109747450a7bSBarry Smith   Output Parameter:
1098a7e14dcfSSatish Balay . maxits - the maximum number of iterates
1099a7e14dcfSSatish Balay 
1100a7e14dcfSSatish Balay   Level: intermediate
1101a7e14dcfSSatish Balay 
11021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()`
1103a7e14dcfSSatish Balay @*/
1104d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits)
1105d71ae5a4SJacob Faibussowitsch {
1106a7e14dcfSSatish Balay   PetscFunctionBegin;
1107441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
11084f572ea9SToby Isaac   PetscAssertPointer(maxits, 2);
1109a7e14dcfSSatish Balay   *maxits = tao->max_it;
11103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1111a7e14dcfSSatish Balay }
1112a7e14dcfSSatish Balay 
1113a7e14dcfSSatish Balay /*@
1114a7e14dcfSSatish Balay   TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
1115a7e14dcfSSatish Balay 
111667be906fSBarry Smith   Logically Collective
1117a7e14dcfSSatish Balay 
1118d8d19677SJose E. Roman   Input Parameters:
111947450a7bSBarry Smith + tao    - a `Tao` optimization solver
1120a7e14dcfSSatish Balay - radius - the trust region radius
1121a7e14dcfSSatish Balay 
1122a7e14dcfSSatish Balay   Options Database Key:
1123a7e14dcfSSatish Balay . -tao_trust0 <t0> - sets initial trust region radius
1124a7e14dcfSSatish Balay 
112547450a7bSBarry Smith   Level: intermediate
112647450a7bSBarry Smith 
11271cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR`
1128a7e14dcfSSatish Balay @*/
1129d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
1130d71ae5a4SJacob Faibussowitsch {
1131a7e14dcfSSatish Balay   PetscFunctionBegin;
1132441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
113394511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, radius, 2);
1134a7e14dcfSSatish Balay   tao->trust0         = PetscMax(0.0, radius);
11356552cf8aSJason Sarich   tao->trust0_changed = PETSC_TRUE;
11363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1137a7e14dcfSSatish Balay }
1138a7e14dcfSSatish Balay 
1139a7e14dcfSSatish Balay /*@
114065ba42b6SBarry Smith   TaoGetInitialTrustRegionRadius - Gets the initial trust region radius.
1141a7e14dcfSSatish Balay 
1142a7e14dcfSSatish Balay   Not Collective
1143a7e14dcfSSatish Balay 
1144a7e14dcfSSatish Balay   Input Parameter:
114547450a7bSBarry Smith . tao - a `Tao` optimization solver
1146a7e14dcfSSatish Balay 
1147a7e14dcfSSatish Balay   Output Parameter:
1148a7e14dcfSSatish Balay . radius - the trust region radius
1149a7e14dcfSSatish Balay 
1150a7e14dcfSSatish Balay   Level: intermediate
1151a7e14dcfSSatish Balay 
11521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR`
1153a7e14dcfSSatish Balay @*/
1154d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1155d71ae5a4SJacob Faibussowitsch {
1156a7e14dcfSSatish Balay   PetscFunctionBegin;
1157441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
11584f572ea9SToby Isaac   PetscAssertPointer(radius, 2);
1159a7e14dcfSSatish Balay   *radius = tao->trust0;
11603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1161a7e14dcfSSatish Balay }
1162a7e14dcfSSatish Balay 
1163a7e14dcfSSatish Balay /*@
1164a7e14dcfSSatish Balay   TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1165a7e14dcfSSatish Balay 
1166a7e14dcfSSatish Balay   Not Collective
1167a7e14dcfSSatish Balay 
1168a7e14dcfSSatish Balay   Input Parameter:
116947450a7bSBarry Smith . tao - a `Tao` optimization solver
1170a7e14dcfSSatish Balay 
1171a7e14dcfSSatish Balay   Output Parameter:
1172a7e14dcfSSatish Balay . radius - the trust region radius
1173a7e14dcfSSatish Balay 
1174a7e14dcfSSatish Balay   Level: intermediate
1175a7e14dcfSSatish Balay 
11761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR`
1177a7e14dcfSSatish Balay @*/
1178d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1179d71ae5a4SJacob Faibussowitsch {
1180a7e14dcfSSatish Balay   PetscFunctionBegin;
1181441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
11824f572ea9SToby Isaac   PetscAssertPointer(radius, 2);
1183a7e14dcfSSatish Balay   *radius = tao->trust;
11843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1185a7e14dcfSSatish Balay }
1186a7e14dcfSSatish Balay 
1187a7e14dcfSSatish Balay /*@
118847450a7bSBarry Smith   TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()`
1189a7e14dcfSSatish Balay 
1190a7e14dcfSSatish Balay   Not Collective
1191a7e14dcfSSatish Balay 
1192f899ff85SJose E. Roman   Input Parameter:
119347450a7bSBarry Smith . tao - the `Tao` context
1194a7e14dcfSSatish Balay 
1195a7e14dcfSSatish Balay   Output Parameters:
1196e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this
1197a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
1198a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor
1199a7e14dcfSSatish Balay 
1200a7e14dcfSSatish Balay   Level: intermediate
1201a1cb98faSBarry Smith 
1202a1cb98faSBarry Smith   Note:
120347450a7bSBarry Smith   `NULL` can be used as an argument if not all tolerances values are needed
1204a1cb98faSBarry Smith 
12051cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`
1206a7e14dcfSSatish Balay @*/
1207d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1208d71ae5a4SJacob Faibussowitsch {
1209a7e14dcfSSatish Balay   PetscFunctionBegin;
1210441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1211a7e14dcfSSatish Balay   if (gatol) *gatol = tao->gatol;
1212a7e14dcfSSatish Balay   if (grtol) *grtol = tao->grtol;
1213a7e14dcfSSatish Balay   if (gttol) *gttol = tao->gttol;
12143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1215a7e14dcfSSatish Balay }
1216a7e14dcfSSatish Balay 
1217a7e14dcfSSatish Balay /*@
1218a7e14dcfSSatish Balay   TaoGetKSP - Gets the linear solver used by the optimization solver.
1219a7e14dcfSSatish Balay 
1220a7e14dcfSSatish Balay   Not Collective
1221a7e14dcfSSatish Balay 
122247450a7bSBarry Smith   Input Parameter:
122347450a7bSBarry Smith . tao - the `Tao` solver
1224a7e14dcfSSatish Balay 
122547450a7bSBarry Smith   Output Parameter:
122647450a7bSBarry Smith . ksp - the `KSP` linear solver used in the optimization solver
1227a7e14dcfSSatish Balay 
1228a7e14dcfSSatish Balay   Level: intermediate
1229a7e14dcfSSatish Balay 
12301cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `KSP`
1231a7e14dcfSSatish Balay @*/
1232d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
1233d71ae5a4SJacob Faibussowitsch {
1234a7e14dcfSSatish Balay   PetscFunctionBegin;
123594511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12364f572ea9SToby Isaac   PetscAssertPointer(ksp, 2);
1237a7e14dcfSSatish Balay   *ksp = tao->ksp;
12383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1239a7e14dcfSSatish Balay }
1240a7e14dcfSSatish Balay 
1241025e9500SJason Sarich /*@
1242025e9500SJason Sarich   TaoGetLinearSolveIterations - Gets the total number of linear iterations
124347450a7bSBarry Smith   used by the `Tao` solver
1244025e9500SJason Sarich 
1245025e9500SJason Sarich   Not Collective
1246025e9500SJason Sarich 
1247025e9500SJason Sarich   Input Parameter:
124847450a7bSBarry Smith . tao - the `Tao` context
1249025e9500SJason Sarich 
1250025e9500SJason Sarich   Output Parameter:
1251025e9500SJason Sarich . lits - number of linear iterations
1252025e9500SJason Sarich 
1253025e9500SJason Sarich   Level: intermediate
1254025e9500SJason Sarich 
125547450a7bSBarry Smith   Note:
125647450a7bSBarry Smith   This counter is reset to zero for each successive call to `TaoSolve()`
125747450a7bSBarry Smith 
12581cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetKSP()`
1259025e9500SJason Sarich @*/
1260d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits)
1261d71ae5a4SJacob Faibussowitsch {
1262025e9500SJason Sarich   PetscFunctionBegin;
1263025e9500SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12644f572ea9SToby Isaac   PetscAssertPointer(lits, 2);
12652d9aa51bSJason Sarich   *lits = tao->ksp_tot_its;
12663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1267025e9500SJason Sarich }
1268025e9500SJason Sarich 
1269a7e14dcfSSatish Balay /*@
1270a7e14dcfSSatish Balay   TaoGetLineSearch - Gets the line search used by the optimization solver.
1271a7e14dcfSSatish Balay 
1272a7e14dcfSSatish Balay   Not Collective
1273a7e14dcfSSatish Balay 
127447450a7bSBarry Smith   Input Parameter:
127547450a7bSBarry Smith . tao - the `Tao` solver
1276a7e14dcfSSatish Balay 
127747450a7bSBarry Smith   Output Parameter:
1278a7e14dcfSSatish Balay . ls - the line search used in the optimization solver
1279a7e14dcfSSatish Balay 
1280a7e14dcfSSatish Balay   Level: intermediate
1281a7e14dcfSSatish Balay 
12821cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType`
1283a7e14dcfSSatish Balay @*/
1284d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
1285d71ae5a4SJacob Faibussowitsch {
1286a7e14dcfSSatish Balay   PetscFunctionBegin;
128794511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12884f572ea9SToby Isaac   PetscAssertPointer(ls, 2);
1289a7e14dcfSSatish Balay   *ls = tao->linesearch;
12903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1291a7e14dcfSSatish Balay }
1292a7e14dcfSSatish Balay 
1293a7e14dcfSSatish Balay /*@
1294a7e14dcfSSatish Balay   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1295a7e14dcfSSatish Balay   in the line search to the running total.
1296a7e14dcfSSatish Balay 
1297a7e14dcfSSatish Balay   Input Parameters:
12982fe279fdSBarry Smith . tao - the `Tao` solver
1299a7e14dcfSSatish Balay 
1300a7e14dcfSSatish Balay   Level: developer
1301a7e14dcfSSatish Balay 
13021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()`
1303a7e14dcfSSatish Balay @*/
1304d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAddLineSearchCounts(Tao tao)
1305d71ae5a4SJacob Faibussowitsch {
1306a7e14dcfSSatish Balay   PetscBool flg;
1307a7e14dcfSSatish Balay   PetscInt  nfeval, ngeval, nfgeval;
130847a47007SBarry Smith 
1309a7e14dcfSSatish Balay   PetscFunctionBegin;
1310441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1311a7e14dcfSSatish Balay   if (tao->linesearch) {
13129566063dSJacob Faibussowitsch     PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg));
131394ae4db5SBarry Smith     if (!flg) {
13149566063dSJacob Faibussowitsch       PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval));
1315a7e14dcfSSatish Balay       tao->nfuncs += nfeval;
1316a7e14dcfSSatish Balay       tao->ngrads += ngeval;
1317a7e14dcfSSatish Balay       tao->nfuncgrads += nfgeval;
1318a7e14dcfSSatish Balay     }
1319a7e14dcfSSatish Balay   }
13203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1321a7e14dcfSSatish Balay }
1322a7e14dcfSSatish Balay 
1323a7e14dcfSSatish Balay /*@
132447450a7bSBarry Smith   TaoGetSolution - Returns the vector with the current solution from the `Tao` object
1325a7e14dcfSSatish Balay 
1326a7e14dcfSSatish Balay   Not Collective
1327a7e14dcfSSatish Balay 
1328a7e14dcfSSatish Balay   Input Parameter:
132947450a7bSBarry Smith . tao - the `Tao` context
1330a7e14dcfSSatish Balay 
1331a7e14dcfSSatish Balay   Output Parameter:
1332a7e14dcfSSatish Balay . X - the current solution
1333a7e14dcfSSatish Balay 
1334a7e14dcfSSatish Balay   Level: intermediate
1335a7e14dcfSSatish Balay 
133665ba42b6SBarry Smith   Note:
133765ba42b6SBarry Smith   The returned vector will be the same object that was passed into `TaoSetSolution()`
133865ba42b6SBarry Smith 
13391cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()`
1340a7e14dcfSSatish Balay @*/
1341d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolution(Tao tao, Vec *X)
1342d71ae5a4SJacob Faibussowitsch {
1343a7e14dcfSSatish Balay   PetscFunctionBegin;
1344441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
13454f572ea9SToby Isaac   PetscAssertPointer(X, 2);
1346a7e14dcfSSatish Balay   *X = tao->solution;
13473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1348a7e14dcfSSatish Balay }
1349a7e14dcfSSatish Balay 
1350a7e14dcfSSatish Balay /*@
135147450a7bSBarry Smith   TaoResetStatistics - Initialize the statistics collected by the `Tao` object.
1352a7e14dcfSSatish Balay   These statistics include the iteration number, residual norms, and convergence status.
1353a7e14dcfSSatish Balay   This routine gets called before solving each optimization problem.
1354a7e14dcfSSatish Balay 
1355c3339decSBarry Smith   Collective
1356a7e14dcfSSatish Balay 
135747450a7bSBarry Smith   Input Parameter:
1358e056e8ceSJacob Faibussowitsch . tao - the `Tao` context
1359a7e14dcfSSatish Balay 
1360a7e14dcfSSatish Balay   Level: developer
1361a7e14dcfSSatish Balay 
13621cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
1363a7e14dcfSSatish Balay @*/
1364d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResetStatistics(Tao tao)
1365d71ae5a4SJacob Faibussowitsch {
1366a7e14dcfSSatish Balay   PetscFunctionBegin;
1367441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1368a7e14dcfSSatish Balay   tao->niter        = 0;
1369a7e14dcfSSatish Balay   tao->nfuncs       = 0;
1370a7e14dcfSSatish Balay   tao->nfuncgrads   = 0;
1371a7e14dcfSSatish Balay   tao->ngrads       = 0;
1372a7e14dcfSSatish Balay   tao->nhess        = 0;
1373a7e14dcfSSatish Balay   tao->njac         = 0;
1374a7e14dcfSSatish Balay   tao->nconstraints = 0;
1375a7e14dcfSSatish Balay   tao->ksp_its      = 0;
1376ae93cb3cSJason Sarich   tao->ksp_tot_its  = 0;
1377a7e14dcfSSatish Balay   tao->reason       = TAO_CONTINUE_ITERATING;
1378a7e14dcfSSatish Balay   tao->residual     = 0.0;
1379a7e14dcfSSatish Balay   tao->cnorm        = 0.0;
1380a7e14dcfSSatish Balay   tao->step         = 0.0;
1381a7e14dcfSSatish Balay   tao->lsflag       = PETSC_FALSE;
1382a7e14dcfSSatish Balay   if (tao->hist_reset) tao->hist_len = 0;
13833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1384a7e14dcfSSatish Balay }
1385a7e14dcfSSatish Balay 
1386a7e14dcfSSatish Balay /*@C
1387e1e80dc8SAlp Dener   TaoSetUpdate - Sets the general-purpose update function called
13882fe279fdSBarry Smith   at the beginning of every iteration of the optimization algorithm. Called after the new solution and the gradient
1389e1e80dc8SAlp Dener   is determined, but before the Hessian is computed (if applicable).
1390e1e80dc8SAlp Dener 
1391c3339decSBarry Smith   Logically Collective
1392e1e80dc8SAlp Dener 
1393e1e80dc8SAlp Dener   Input Parameters:
1394*270bebe6SStefano Zampini + tao  - The `Tao` solver
1395*270bebe6SStefano Zampini . func - The function
1396*270bebe6SStefano Zampini - ctx  - The update function context
1397e1e80dc8SAlp Dener 
139820f4b53cSBarry Smith   Calling sequence of `func`:
1399*270bebe6SStefano Zampini + tao - The optimizer context
1400*270bebe6SStefano Zampini . it  - The current iteration index
1401*270bebe6SStefano Zampini - ctx - The update context
1402e1e80dc8SAlp Dener 
1403e1e80dc8SAlp Dener   Level: advanced
1404e1e80dc8SAlp Dener 
1405*270bebe6SStefano Zampini   Notes:
1406*270bebe6SStefano Zampini   Users can modify the gradient direction or any other vector associated to the specific solver used.
1407*270bebe6SStefano Zampini   The objective function value is always recomputed after a call to the update hook.
1408*270bebe6SStefano Zampini 
14091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`
1410e1e80dc8SAlp Dener @*/
1411*270bebe6SStefano Zampini PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao tao, PetscInt it, void *ctx), void *ctx)
1412d71ae5a4SJacob Faibussowitsch {
1413e1e80dc8SAlp Dener   PetscFunctionBegin;
1414e1e80dc8SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1415e1e80dc8SAlp Dener   tao->ops->update = func;
1416e1e80dc8SAlp Dener   tao->user_update = ctx;
14173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1418e1e80dc8SAlp Dener }
1419e1e80dc8SAlp Dener 
1420e1e80dc8SAlp Dener /*@C
1421a7e14dcfSSatish Balay   TaoSetConvergenceTest - Sets the function that is to be used to test
1422a7e14dcfSSatish Balay   for convergence o fthe iterative minimization solution.  The new convergence
142365ba42b6SBarry Smith   testing routine will replace Tao's default convergence test.
1424a7e14dcfSSatish Balay 
1425c3339decSBarry Smith   Logically Collective
1426a7e14dcfSSatish Balay 
1427a7e14dcfSSatish Balay   Input Parameters:
142847450a7bSBarry Smith + tao  - the `Tao` object
1429a7e14dcfSSatish Balay . conv - the routine to test for convergence
1430a7e14dcfSSatish Balay - ctx  - [optional] context for private data for the convergence routine
143147450a7bSBarry Smith         (may be `NULL`)
1432a7e14dcfSSatish Balay 
143320f4b53cSBarry Smith   Calling sequence of `conv`:
143447450a7bSBarry Smith + tao - the `Tao` object
1435a7e14dcfSSatish Balay - ctx - [optional] convergence context
1436a7e14dcfSSatish Balay 
143747450a7bSBarry Smith   Level: advanced
143847450a7bSBarry Smith 
143965ba42b6SBarry Smith   Note:
144065ba42b6SBarry Smith   The new convergence testing routine should call `TaoSetConvergedReason()`.
1441a7e14dcfSSatish Balay 
144210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoMonitorSet()`
1443a7e14dcfSSatish Balay @*/
1444d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void *), void *ctx)
1445d71ae5a4SJacob Faibussowitsch {
1446a7e14dcfSSatish Balay   PetscFunctionBegin;
1447441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
144894511df7SStefano Zampini   tao->ops->convergencetest = conv;
144994511df7SStefano Zampini   tao->cnvP                 = ctx;
14503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1451a7e14dcfSSatish Balay }
1452a7e14dcfSSatish Balay 
1453a7e14dcfSSatish Balay /*@C
145410978b7dSBarry Smith   TaoMonitorSet - Sets an additional function that is to be used at every
1455a7e14dcfSSatish Balay   iteration of the solver to display the iteration's
1456a7e14dcfSSatish Balay   progress.
1457a7e14dcfSSatish Balay 
1458c3339decSBarry Smith   Logically Collective
1459a7e14dcfSSatish Balay 
1460a7e14dcfSSatish Balay   Input Parameters:
146147450a7bSBarry Smith + tao  - the `Tao` solver context
14622fe279fdSBarry Smith . func - monitoring routine
14632fe279fdSBarry Smith . ctx  - [optional] user-defined context for private data for the monitor routine (may be `NULL`)
14642fe279fdSBarry Smith - dest - [optional] function to destroy the context when the `Tao` is destroyed
1465a7e14dcfSSatish Balay 
14662fe279fdSBarry Smith   Calling sequence of `func`:
146747450a7bSBarry Smith + tao - the `Tao` solver context
14682fe279fdSBarry Smith - ctx - [optional] monitoring context
14692fe279fdSBarry Smith 
14702fe279fdSBarry Smith   Calling sequence of `dest`:
14712fe279fdSBarry Smith . ctx - monitoring context
1472a7e14dcfSSatish Balay 
147347450a7bSBarry Smith   Level: intermediate
147447450a7bSBarry Smith 
147587497f52SBarry Smith   Notes:
147610978b7dSBarry Smith   See `TaoSetFromOptions()` for a monitoring options.
147710978b7dSBarry Smith 
1478a7e14dcfSSatish Balay   Several different monitoring routines may be set by calling
147910978b7dSBarry Smith   `TaoMonitorSet()` multiple times; all will be called in the
1480a7e14dcfSSatish Balay   order in which they were set.
1481a7e14dcfSSatish Balay 
1482e056e8ceSJacob Faibussowitsch   Fortran Notes:
148395452b02SPatrick Sanan   Only one monitor function may be set
1484a7e14dcfSSatish Balay 
1485b2dc45ddSPierre Jolivet .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoMonitorCancel()`, `TaoSetDestroyRoutine()`, `TaoView()`
1486a7e14dcfSSatish Balay @*/
148710978b7dSBarry Smith PetscErrorCode TaoMonitorSet(Tao tao, PetscErrorCode (*func)(Tao, void *), void *ctx, PetscErrorCode (*dest)(void **))
1488d71ae5a4SJacob Faibussowitsch {
148908d19d1fSJason Sarich   PetscInt  i;
14908732526dSAlp Dener   PetscBool identical;
149108d19d1fSJason Sarich 
1492a7e14dcfSSatish Balay   PetscFunctionBegin;
1493441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
14943c859ba3SBarry Smith   PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS);
149508d19d1fSJason Sarich 
149608d19d1fSJason Sarich   for (i = 0; i < tao->numbermonitors; i++) {
14979566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))func, ctx, dest, (PetscErrorCode(*)(void))tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical));
14983ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
149908d19d1fSJason Sarich   }
1500a7e14dcfSSatish Balay   tao->monitor[tao->numbermonitors]        = func;
15018732526dSAlp Dener   tao->monitorcontext[tao->numbermonitors] = (void *)ctx;
1502a7e14dcfSSatish Balay   tao->monitordestroy[tao->numbermonitors] = dest;
1503a7e14dcfSSatish Balay   ++tao->numbermonitors;
15043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1505a7e14dcfSSatish Balay }
1506a7e14dcfSSatish Balay 
1507a7e14dcfSSatish Balay /*@
1508b2dc45ddSPierre Jolivet   TaoMonitorCancel - Clears all the monitor functions for a `Tao` object.
1509a7e14dcfSSatish Balay 
1510c3339decSBarry Smith   Logically Collective
1511a7e14dcfSSatish Balay 
151247450a7bSBarry Smith   Input Parameter:
151347450a7bSBarry Smith . tao - the `Tao` solver context
1514a7e14dcfSSatish Balay 
15153c7db156SBarry Smith   Options Database Key:
1516b2dc45ddSPierre Jolivet . -tao_monitor_cancel - cancels all monitors that have been hardwired
151710978b7dSBarry Smith     into a code by calls to `TaoMonitorSet()`, but does not cancel those
1518a7e14dcfSSatish Balay     set via the options database
1519a7e14dcfSSatish Balay 
1520a7e14dcfSSatish Balay   Level: advanced
1521a7e14dcfSSatish Balay 
152247450a7bSBarry Smith   Note:
152347450a7bSBarry Smith   There is no way to clear one specific monitor from a `Tao` object.
152447450a7bSBarry Smith 
152510978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1526a7e14dcfSSatish Balay @*/
1527b2dc45ddSPierre Jolivet PetscErrorCode TaoMonitorCancel(Tao tao)
1528d71ae5a4SJacob Faibussowitsch {
1529a7e14dcfSSatish Balay   PetscInt i;
153047a47007SBarry Smith 
1531a7e14dcfSSatish Balay   PetscFunctionBegin;
1532441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1533a7e14dcfSSatish Balay   for (i = 0; i < tao->numbermonitors; i++) {
153448a46eb9SPierre Jolivet     if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i]));
1535a7e14dcfSSatish Balay   }
1536a7e14dcfSSatish Balay   tao->numbermonitors = 0;
15373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1538a7e14dcfSSatish Balay }
1539a7e14dcfSSatish Balay 
15407fab98ebSJason Sarich /*@
154147450a7bSBarry Smith   TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()`
1542a7e14dcfSSatish Balay 
1543c3339decSBarry Smith   Collective
1544a7e14dcfSSatish Balay 
1545a7e14dcfSSatish Balay   Input Parameters:
154647450a7bSBarry Smith + tao - the `Tao` context
154747450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1548a7e14dcfSSatish Balay 
154947450a7bSBarry Smith   Options Database Key:
1550147403d9SBarry Smith . -tao_monitor - turn on default monitoring
1551a7e14dcfSSatish Balay 
1552a7e14dcfSSatish Balay   Level: advanced
1553a7e14dcfSSatish Balay 
155447450a7bSBarry Smith   Note:
155547450a7bSBarry Smith   This monitor prints the function value and gradient
155647450a7bSBarry Smith   norm at each iteration.
155747450a7bSBarry Smith 
155810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1559a7e14dcfSSatish Balay @*/
1560d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDefault(Tao tao, void *ctx)
1561d71ae5a4SJacob Faibussowitsch {
156263b15415SAlp Dener   PetscInt    its, tabs;
1563a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
15648163d661SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
156547a47007SBarry Smith 
1566a7e14dcfSSatish Balay   PetscFunctionBegin;
156794511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
15688163d661SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1569a7e14dcfSSatish Balay   its   = tao->niter;
1570a7e14dcfSSatish Balay   fct   = tao->fc;
1571a7e14dcfSSatish Balay   gnorm = tao->residual;
15729566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
15739566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1574494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
15759566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1576494bef23SAlp Dener     tao->header_printed = PETSC_TRUE;
157763b15415SAlp Dener   }
157863a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its));
15799566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Function value: %g,", (double)fct));
158008d19d1fSJason Sarich   if (gnorm >= PETSC_INFINITY) {
15819566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: Inf \n"));
158208d19d1fSJason Sarich   } else {
15839566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g \n", (double)gnorm));
158408d19d1fSJason Sarich   }
15859566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
15863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1587a7e14dcfSSatish Balay }
1588a7e14dcfSSatish Balay 
15897fab98ebSJason Sarich /*@
159010978b7dSBarry Smith   TaoMonitorGlobalization - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method.
15918d5ead36SAlp Dener 
1592c3339decSBarry Smith   Collective
15938d5ead36SAlp Dener 
15948d5ead36SAlp Dener   Input Parameters:
159547450a7bSBarry Smith + tao - the `Tao` context
159647450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
15978d5ead36SAlp Dener 
159847450a7bSBarry Smith   Options Database Key:
159910978b7dSBarry Smith . -tao_monitor_globalization - turn on monitoring with globalization information
16008d5ead36SAlp Dener 
16018d5ead36SAlp Dener   Level: advanced
16028d5ead36SAlp Dener 
160347450a7bSBarry Smith   Note:
160447450a7bSBarry Smith   This monitor prints the function value and gradient norm at each
160547450a7bSBarry Smith   iteration, as well as the step size and trust radius. Note that the
160647450a7bSBarry Smith   step size and trust radius may be the same for some algorithms.
160747450a7bSBarry Smith 
160810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
16098d5ead36SAlp Dener @*/
161010978b7dSBarry Smith PetscErrorCode TaoMonitorGlobalization(Tao tao, void *ctx)
1611d71ae5a4SJacob Faibussowitsch {
16128d5ead36SAlp Dener   PetscInt    its, tabs;
16138d5ead36SAlp Dener   PetscReal   fct, gnorm, stp, tr;
16148d5ead36SAlp Dener   PetscViewer viewer = (PetscViewer)ctx;
16158d5ead36SAlp Dener 
16168d5ead36SAlp Dener   PetscFunctionBegin;
161794511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
16188d5ead36SAlp Dener   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16198d5ead36SAlp Dener   its   = tao->niter;
16208d5ead36SAlp Dener   fct   = tao->fc;
16218d5ead36SAlp Dener   gnorm = tao->residual;
16228d5ead36SAlp Dener   stp   = tao->step;
16238d5ead36SAlp Dener   tr    = tao->trust;
16249566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
16259566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1626494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
16279566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1628494bef23SAlp Dener     tao->header_printed = PETSC_TRUE;
16298d5ead36SAlp Dener   }
163063a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its));
16319566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Function value: %g,", (double)fct));
16328d5ead36SAlp Dener   if (gnorm >= PETSC_INFINITY) {
16339566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: Inf,"));
16348d5ead36SAlp Dener   } else {
16359566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g,", (double)gnorm));
16368d5ead36SAlp Dener   }
16379566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Step: %g,  Trust: %g\n", (double)stp, (double)tr));
16389566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
16393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16408d5ead36SAlp Dener }
16418d5ead36SAlp Dener 
16428d5ead36SAlp Dener /*@
164310978b7dSBarry Smith   TaoMonitorDefaultShort - Routine for monitoring progress of `TaoSolve()` that displays fewer digits than `TaoMonitorDefault()`
1644a7e14dcfSSatish Balay 
1645c3339decSBarry Smith   Collective
1646a7e14dcfSSatish Balay 
1647a7e14dcfSSatish Balay   Input Parameters:
164847450a7bSBarry Smith + tao - the `Tao` context
164947450a7bSBarry Smith - ctx - `PetscViewer` context of type `PETSCVIEWERASCII`
1650a7e14dcfSSatish Balay 
165147450a7bSBarry Smith   Options Database Key:
165210978b7dSBarry Smith . -tao_monitor_short - turn on default short monitoring
1653a7e14dcfSSatish Balay 
1654a7e14dcfSSatish Balay   Level: advanced
1655a7e14dcfSSatish Balay 
165647450a7bSBarry Smith   Note:
165747450a7bSBarry Smith   Same as `TaoMonitorDefault()` except
165847450a7bSBarry Smith   it prints fewer digits of the residual as the residual gets smaller.
165947450a7bSBarry Smith   This is because the later digits are meaningless and are often
166047450a7bSBarry Smith   different on different machines; by using this routine different
166147450a7bSBarry Smith   machines will usually generate the same output.
166247450a7bSBarry Smith 
166310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1664a7e14dcfSSatish Balay @*/
166510978b7dSBarry Smith PetscErrorCode TaoMonitorDefaultShort(Tao tao, void *ctx)
1666d71ae5a4SJacob Faibussowitsch {
16672c9b8b83SAlp Dener   PetscInt    its, tabs;
1668a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
16694d4332d5SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
167047a47007SBarry Smith 
1671a7e14dcfSSatish Balay   PetscFunctionBegin;
167294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
16734d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1674a7e14dcfSSatish Balay   its   = tao->niter;
1675a7e14dcfSSatish Balay   fct   = tao->fc;
1676a7e14dcfSSatish Balay   gnorm = tao->residual;
16779566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
16789566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
167963a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %3" PetscInt_FMT ",", its));
16809566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, " Function value %g,", (double)fct));
1681d393f493SBarry Smith   if (gnorm >= PETSC_INFINITY) {
16829566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n"));
168308d19d1fSJason Sarich   } else if (gnorm > 1.e-6) {
16849566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm));
1685a7e14dcfSSatish Balay   } else if (gnorm > 1.e-11) {
16869566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-6 \n"));
1687a7e14dcfSSatish Balay   } else {
16889566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-11 \n"));
1689a7e14dcfSSatish Balay   }
16909566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
16913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1692a7e14dcfSSatish Balay }
1693a7e14dcfSSatish Balay 
16947fab98ebSJason Sarich /*@
169510978b7dSBarry Smith   TaoMonitorConstraintNorm - same as `TaoMonitorDefault()` except
169647450a7bSBarry Smith   it prints the norm of the constraint function.
1697a7e14dcfSSatish Balay 
1698c3339decSBarry Smith   Collective
1699a7e14dcfSSatish Balay 
1700a7e14dcfSSatish Balay   Input Parameters:
170147450a7bSBarry Smith + tao - the `Tao` context
170247450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1703a7e14dcfSSatish Balay 
170447450a7bSBarry Smith   Options Database Key:
170510978b7dSBarry Smith . -tao_monitor_constraint_norm - monitor the constraints
1706a7e14dcfSSatish Balay 
1707a7e14dcfSSatish Balay   Level: advanced
1708a7e14dcfSSatish Balay 
170910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1710a7e14dcfSSatish Balay @*/
171110978b7dSBarry Smith PetscErrorCode TaoMonitorConstraintNorm(Tao tao, void *ctx)
1712d71ae5a4SJacob Faibussowitsch {
17132c9b8b83SAlp Dener   PetscInt    its, tabs;
1714a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
1715d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
1716a7e14dcfSSatish Balay 
1717a7e14dcfSSatish Balay   PetscFunctionBegin;
171894511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1719d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1720a7e14dcfSSatish Balay   its   = tao->niter;
1721a7e14dcfSSatish Balay   fct   = tao->fc;
1722a7e14dcfSSatish Balay   gnorm = tao->residual;
17239566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
17249566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
172563a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", its));
17269566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct));
17279566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g ", (double)gnorm));
17289566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Constraint: %g \n", (double)tao->cnorm));
17299566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
17303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1731a7e14dcfSSatish Balay }
1732a7e14dcfSSatish Balay 
1733a7e14dcfSSatish Balay /*@C
173410978b7dSBarry Smith   TaoMonitorSolution - Views the solution at each iteration of `TaoSolve()`
1735a7e14dcfSSatish Balay 
1736c3339decSBarry Smith   Collective
1737a7e14dcfSSatish Balay 
1738a7e14dcfSSatish Balay   Input Parameters:
173947450a7bSBarry Smith + tao - the `Tao` context
174047450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1741a7e14dcfSSatish Balay 
174247450a7bSBarry Smith   Options Database Key:
174310978b7dSBarry Smith . -tao_monitor_solution - view the solution
1744a7e14dcfSSatish Balay 
1745a7e14dcfSSatish Balay   Level: advanced
1746a7e14dcfSSatish Balay 
174710978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1748a7e14dcfSSatish Balay @*/
174910978b7dSBarry Smith PetscErrorCode TaoMonitorSolution(Tao tao, void *ctx)
1750d71ae5a4SJacob Faibussowitsch {
1751feb237baSPierre Jolivet   PetscViewer viewer = (PetscViewer)ctx;
175247a47007SBarry Smith 
1753a7e14dcfSSatish Balay   PetscFunctionBegin;
175494511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1755d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
17569566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution, viewer));
17573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1758a7e14dcfSSatish Balay }
1759a7e14dcfSSatish Balay 
1760a7e14dcfSSatish Balay /*@C
176110978b7dSBarry Smith   TaoMonitorGradient - Views the gradient at each iteration of `TaoSolve()`
1762a7e14dcfSSatish Balay 
1763c3339decSBarry Smith   Collective
1764a7e14dcfSSatish Balay 
1765a7e14dcfSSatish Balay   Input Parameters:
176647450a7bSBarry Smith + tao - the `Tao` context
176747450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1768a7e14dcfSSatish Balay 
176947450a7bSBarry Smith   Options Database Key:
177010978b7dSBarry Smith . -tao_monitor_gradient - view the gradient at each iteration
1771a7e14dcfSSatish Balay 
1772a7e14dcfSSatish Balay   Level: advanced
1773a7e14dcfSSatish Balay 
177410978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1775a7e14dcfSSatish Balay @*/
177610978b7dSBarry Smith PetscErrorCode TaoMonitorGradient(Tao tao, void *ctx)
1777d71ae5a4SJacob Faibussowitsch {
1778d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
177947a47007SBarry Smith 
1780a7e14dcfSSatish Balay   PetscFunctionBegin;
178194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1782d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
17839566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient, viewer));
17843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1785a7e14dcfSSatish Balay }
1786a7e14dcfSSatish Balay 
1787a7e14dcfSSatish Balay /*@C
178810978b7dSBarry Smith   TaoMonitorStep - Views the step-direction at each iteration of `TaoSolve()`
1789a7e14dcfSSatish Balay 
1790c3339decSBarry Smith   Collective
1791a7e14dcfSSatish Balay 
1792a7e14dcfSSatish Balay   Input Parameters:
179347450a7bSBarry Smith + tao - the `Tao` context
179447450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1795a7e14dcfSSatish Balay 
179647450a7bSBarry Smith   Options Database Key:
179710978b7dSBarry Smith . -tao_monitor_step - view the step vector at each iteration
1798a7e14dcfSSatish Balay 
1799a7e14dcfSSatish Balay   Level: advanced
1800a7e14dcfSSatish Balay 
180110978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1802a7e14dcfSSatish Balay @*/
180310978b7dSBarry Smith PetscErrorCode TaoMonitorStep(Tao tao, void *ctx)
1804d71ae5a4SJacob Faibussowitsch {
1805d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
1806d393f493SBarry Smith 
1807a7e14dcfSSatish Balay   PetscFunctionBegin;
180894511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1809d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18109566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection, viewer));
18113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1812a7e14dcfSSatish Balay }
1813a7e14dcfSSatish Balay 
1814a7e14dcfSSatish Balay /*@C
181510978b7dSBarry Smith   TaoMonitorSolutionDraw - Plots the solution at each iteration of `TaoSolve()`
1816a7e14dcfSSatish Balay 
1817c3339decSBarry Smith   Collective
1818a7e14dcfSSatish Balay 
1819a7e14dcfSSatish Balay   Input Parameters:
182047450a7bSBarry Smith + tao - the `Tao` context
182165ba42b6SBarry Smith - ctx - `TaoMonitorDraw` context
1822a7e14dcfSSatish Balay 
182347450a7bSBarry Smith   Options Database Key:
182410978b7dSBarry Smith . -tao_monitor_solution_draw - draw the solution at each iteration
1825a7e14dcfSSatish Balay 
1826a7e14dcfSSatish Balay   Level: advanced
1827a7e14dcfSSatish Balay 
182810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSolution()`, `TaoMonitorSet()`, `TaoMonitorGradientDraw()`
1829a7e14dcfSSatish Balay @*/
183010978b7dSBarry Smith PetscErrorCode TaoMonitorSolutionDraw(Tao tao, void *ctx)
1831d71ae5a4SJacob Faibussowitsch {
1832e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
183347a47007SBarry Smith 
1834a7e14dcfSSatish Balay   PetscFunctionBegin;
183594511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
18363ba16761SJacob Faibussowitsch   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
18379566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution, ictx->viewer));
18383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1839a7e14dcfSSatish Balay }
1840a7e14dcfSSatish Balay 
1841a7e14dcfSSatish Balay /*@C
184210978b7dSBarry Smith   TaoMonitorGradientDraw - Plots the gradient at each iteration of `TaoSolve()`
1843a7e14dcfSSatish Balay 
1844c3339decSBarry Smith   Collective
1845a7e14dcfSSatish Balay 
1846a7e14dcfSSatish Balay   Input Parameters:
184747450a7bSBarry Smith + tao - the `Tao` context
184865ba42b6SBarry Smith - ctx - `PetscViewer` context
1849a7e14dcfSSatish Balay 
185047450a7bSBarry Smith   Options Database Key:
185110978b7dSBarry Smith . -tao_monitor_gradient_draw - draw the gradient at each iteration
1852a7e14dcfSSatish Balay 
1853a7e14dcfSSatish Balay   Level: advanced
1854a7e14dcfSSatish Balay 
185510978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorGradient()`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw()`
1856a7e14dcfSSatish Balay @*/
185710978b7dSBarry Smith PetscErrorCode TaoMonitorGradientDraw(Tao tao, void *ctx)
1858d71ae5a4SJacob Faibussowitsch {
1859e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
186047a47007SBarry Smith 
1861a7e14dcfSSatish Balay   PetscFunctionBegin;
186294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
18633ba16761SJacob Faibussowitsch   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
18649566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient, ictx->viewer));
18653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1866a7e14dcfSSatish Balay }
1867a7e14dcfSSatish Balay 
1868a7e14dcfSSatish Balay /*@C
186910978b7dSBarry Smith   TaoMonitorStepDraw - Plots the step direction at each iteration of `TaoSolve()`
1870a7e14dcfSSatish Balay 
1871c3339decSBarry Smith   Collective
1872a7e14dcfSSatish Balay 
1873a7e14dcfSSatish Balay   Input Parameters:
187447450a7bSBarry Smith + tao - the `Tao` context
187547450a7bSBarry Smith - ctx - the `PetscViewer` context
1876a7e14dcfSSatish Balay 
187747450a7bSBarry Smith   Options Database Key:
187810978b7dSBarry Smith . -tao_monitor_step_draw - draw the step direction at each iteration
1879a7e14dcfSSatish Balay 
1880a7e14dcfSSatish Balay   Level: advanced
1881a7e14dcfSSatish Balay 
188210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw`
1883a7e14dcfSSatish Balay @*/
188410978b7dSBarry Smith PetscErrorCode TaoMonitorStepDraw(Tao tao, void *ctx)
1885d71ae5a4SJacob Faibussowitsch {
188694511df7SStefano Zampini   PetscViewer viewer = (PetscViewer)ctx;
188747a47007SBarry Smith 
1888a7e14dcfSSatish Balay   PetscFunctionBegin;
188994511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
189094511df7SStefano Zampini   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18919566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection, viewer));
18923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1893a7e14dcfSSatish Balay }
1894a7e14dcfSSatish Balay 
1895a7e14dcfSSatish Balay /*@C
189610978b7dSBarry Smith   TaoMonitorResidual - Views the least-squares residual at each iteration of `TaoSolve()`
1897a7e14dcfSSatish Balay 
1898c3339decSBarry Smith   Collective
1899a7e14dcfSSatish Balay 
1900a7e14dcfSSatish Balay   Input Parameters:
190147450a7bSBarry Smith + tao - the `Tao` context
190247450a7bSBarry Smith - ctx - the `PetscViewer` context or `NULL`
1903a7e14dcfSSatish Balay 
190447450a7bSBarry Smith   Options Database Key:
190510978b7dSBarry Smith . -tao_monitor_ls_residual - view the residual at each iteration
1906a7e14dcfSSatish Balay 
1907a7e14dcfSSatish Balay   Level: advanced
1908a7e14dcfSSatish Balay 
190910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1910a7e14dcfSSatish Balay @*/
191110978b7dSBarry Smith PetscErrorCode TaoMonitorResidual(Tao tao, void *ctx)
1912d71ae5a4SJacob Faibussowitsch {
1913d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
191447a47007SBarry Smith 
1915a7e14dcfSSatish Balay   PetscFunctionBegin;
191694511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1917d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
19189566063dSJacob Faibussowitsch   PetscCall(VecView(tao->ls_res, viewer));
19193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1920a7e14dcfSSatish Balay }
1921a7e14dcfSSatish Balay 
19227fab98ebSJason Sarich /*@
1923a7e14dcfSSatish Balay   TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1924a7e14dcfSSatish Balay   or terminate.
1925a7e14dcfSSatish Balay 
1926c3339decSBarry Smith   Collective
1927a7e14dcfSSatish Balay 
1928a7e14dcfSSatish Balay   Input Parameters:
192947450a7bSBarry Smith + tao   - the `Tao` context
1930a7e14dcfSSatish Balay - dummy - unused dummy context
1931a7e14dcfSSatish Balay 
193247450a7bSBarry Smith   Level: developer
193347450a7bSBarry Smith 
1934a7e14dcfSSatish Balay   Notes:
1935a7e14dcfSSatish Balay   This routine checks the residual in the optimality conditions, the
1936a7e14dcfSSatish Balay   relative residual in the optimity conditions, the number of function
1937a7e14dcfSSatish Balay   evaluations, and the function value to test convergence.  Some
1938a7e14dcfSSatish Balay   solvers may use different convergence routines.
1939a7e14dcfSSatish Balay 
19401cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()`
1941a7e14dcfSSatish Balay @*/
1942d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy)
1943d71ae5a4SJacob Faibussowitsch {
1944a7e14dcfSSatish Balay   PetscInt           niter = tao->niter, nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads);
1945a7e14dcfSSatish Balay   PetscInt           max_funcs = tao->max_funcs;
1946a7e14dcfSSatish Balay   PetscReal          gnorm = tao->residual, gnorm0 = tao->gnorm0;
1947a7e14dcfSSatish Balay   PetscReal          f = tao->fc, steptol = tao->steptol, trradius = tao->step;
1948a7e14dcfSSatish Balay   PetscReal          gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol;
1949e52336cbSBarry Smith   PetscReal          catol = tao->catol, crtol = tao->crtol;
1950e52336cbSBarry Smith   PetscReal          fmin = tao->fmin, cnorm = tao->cnorm;
1951e4cb33bbSBarry Smith   TaoConvergedReason reason = tao->reason;
1952a7e14dcfSSatish Balay 
1953a7e14dcfSSatish Balay   PetscFunctionBegin;
1954441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
19553ba16761SJacob Faibussowitsch   if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
1956a7e14dcfSSatish Balay 
1957a7e14dcfSSatish Balay   if (PetscIsInfOrNanReal(f)) {
19589566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Failed to converged, function value is Inf or NaN\n"));
1959a7e14dcfSSatish Balay     reason = TAO_DIVERGED_NAN;
1960a7e14dcfSSatish Balay   } else if (f <= fmin && cnorm <= catol) {
19619566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin));
1962a7e14dcfSSatish Balay     reason = TAO_CONVERGED_MINF;
1963a7e14dcfSSatish Balay   } else if (gnorm <= gatol && cnorm <= catol) {
19649566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol));
1965a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GATOL;
1966a7e14dcfSSatish Balay   } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) {
19679566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol));
1968a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GRTOL;
1969e1d16bb9SBarry Smith   } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) {
19709566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol));
1971a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GTTOL;
197294511df7SStefano Zampini   } else if (max_funcs >= 0 && nfuncs > max_funcs) {
19739566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs));
1974a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXFCN;
1975a7e14dcfSSatish Balay   } else if (tao->lsflag != 0) {
19769566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Tao Line Search failure.\n"));
1977a7e14dcfSSatish Balay     reason = TAO_DIVERGED_LS_FAILURE;
1978a7e14dcfSSatish Balay   } else if (trradius < steptol && niter > 0) {
19799566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol));
1980a7e14dcfSSatish Balay     reason = TAO_CONVERGED_STEPTOL;
1981e031d6f5SAlp Dener   } else if (niter >= tao->max_it) {
198263a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it));
1983a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXITS;
1984a7e14dcfSSatish Balay   } else {
1985a7e14dcfSSatish Balay     reason = TAO_CONTINUE_ITERATING;
1986a7e14dcfSSatish Balay   }
1987a7e14dcfSSatish Balay   tao->reason = reason;
19883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1989a7e14dcfSSatish Balay }
1990a7e14dcfSSatish Balay 
1991cc4c1da9SBarry Smith /*@
1992a7e14dcfSSatish Balay   TaoSetOptionsPrefix - Sets the prefix used for searching for all
199365ba42b6SBarry Smith   Tao options in the database.
1994a7e14dcfSSatish Balay 
1995c3339decSBarry Smith   Logically Collective
1996a7e14dcfSSatish Balay 
1997a7e14dcfSSatish Balay   Input Parameters:
199847450a7bSBarry Smith + tao - the `Tao` context
1999e056e8ceSJacob Faibussowitsch - p   - the prefix string to prepend to all Tao option requests
2000a7e14dcfSSatish Balay 
20012fe279fdSBarry Smith   Level: advanced
20022fe279fdSBarry Smith 
2003a7e14dcfSSatish Balay   Notes:
2004a7e14dcfSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
2005a7e14dcfSSatish Balay   The first character of all runtime options is AUTOMATICALLY the hyphen.
2006a7e14dcfSSatish Balay 
2007a7e14dcfSSatish Balay   For example, to distinguish between the runtime options for two
200865ba42b6SBarry Smith   different Tao solvers, one could call
2009a7e14dcfSSatish Balay .vb
2010a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao1,"sys1_")
2011a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao2,"sys2_")
2012a7e14dcfSSatish Balay .ve
2013a7e14dcfSSatish Balay 
2014a7e14dcfSSatish Balay   This would enable use of different options for each system, such as
2015a7e14dcfSSatish Balay .vb
20169fa2b5dcSStefano Zampini       -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3
20179fa2b5dcSStefano Zampini       -sys2_tao_method lmvm  -sys2_tao_grtol 1.e-4
2018a7e14dcfSSatish Balay .ve
2019a7e14dcfSSatish Balay 
20201cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()`
2021a7e14dcfSSatish Balay @*/
2022d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
2023d71ae5a4SJacob Faibussowitsch {
2024a7e14dcfSSatish Balay   PetscFunctionBegin;
202594511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
20269566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p));
20271baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p));
20281baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p));
20293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2030a7e14dcfSSatish Balay }
2031a7e14dcfSSatish Balay 
2032cc4c1da9SBarry Smith /*@
203347450a7bSBarry Smith   TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database.
2034a7e14dcfSSatish Balay 
2035c3339decSBarry Smith   Logically Collective
2036a7e14dcfSSatish Balay 
2037a7e14dcfSSatish Balay   Input Parameters:
203847450a7bSBarry Smith + tao - the `Tao` solver context
2039e056e8ceSJacob Faibussowitsch - p   - the prefix string to prepend to all `Tao` option requests
2040a7e14dcfSSatish Balay 
20412fe279fdSBarry Smith   Level: advanced
20422fe279fdSBarry Smith 
204365ba42b6SBarry Smith   Note:
2044a7e14dcfSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
204565ba42b6SBarry Smith   The first character of all runtime options is automatically the hyphen.
2046a7e14dcfSSatish Balay 
20471cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()`
2048a7e14dcfSSatish Balay @*/
2049d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
2050d71ae5a4SJacob Faibussowitsch {
2051a7e14dcfSSatish Balay   PetscFunctionBegin;
205294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
20539566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p));
20541baa6e33SBarry Smith   if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p));
20551baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p));
20563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2057a7e14dcfSSatish Balay }
2058a7e14dcfSSatish Balay 
2059cc4c1da9SBarry Smith /*@
2060a7e14dcfSSatish Balay   TaoGetOptionsPrefix - Gets the prefix used for searching for all
206165ba42b6SBarry Smith   Tao options in the database
2062a7e14dcfSSatish Balay 
2063a7e14dcfSSatish Balay   Not Collective
2064a7e14dcfSSatish Balay 
206547450a7bSBarry Smith   Input Parameter:
206647450a7bSBarry Smith . tao - the `Tao` context
2067a7e14dcfSSatish Balay 
206847450a7bSBarry Smith   Output Parameter:
2069e056e8ceSJacob Faibussowitsch . p - pointer to the prefix string used is returned
2070a7e14dcfSSatish Balay 
2071e056e8ceSJacob Faibussowitsch   Fortran Notes:
207247450a7bSBarry Smith   Pass in a string 'prefix' of sufficient length to hold the prefix.
2073a7e14dcfSSatish Balay 
2074a7e14dcfSSatish Balay   Level: advanced
2075a7e14dcfSSatish Balay 
20761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()`
2077a7e14dcfSSatish Balay @*/
2078d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
2079d71ae5a4SJacob Faibussowitsch {
208094511df7SStefano Zampini   PetscFunctionBegin;
208194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
20829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p));
20833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2084a7e14dcfSSatish Balay }
2085a7e14dcfSSatish Balay 
2086cc4c1da9SBarry Smith /*@
208747450a7bSBarry Smith   TaoSetType - Sets the `TaoType` for the minimization solver.
2088a7e14dcfSSatish Balay 
2089c3339decSBarry Smith   Collective
2090a7e14dcfSSatish Balay 
2091a7e14dcfSSatish Balay   Input Parameters:
2092e056e8ceSJacob Faibussowitsch + tao  - the `Tao` solver context
2093a7e14dcfSSatish Balay - type - a known method
2094a7e14dcfSSatish Balay 
2095a7e14dcfSSatish Balay   Options Database Key:
209658417fe7SBarry Smith . -tao_type <type> - Sets the method; use -help for a list
209758417fe7SBarry Smith    of available methods (for instance, "-tao_type lmvm" or "-tao_type tron")
2098a7e14dcfSSatish Balay 
2099a7e14dcfSSatish Balay   Level: intermediate
2100a7e14dcfSSatish Balay 
21011cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType`
2102a7e14dcfSSatish Balay @*/
2103d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetType(Tao tao, TaoType type)
2104d71ae5a4SJacob Faibussowitsch {
2105441846f8SBarry Smith   PetscErrorCode (*create_xxx)(Tao);
2106a7e14dcfSSatish Balay   PetscBool issame;
2107a7e14dcfSSatish Balay 
2108a7e14dcfSSatish Balay   PetscFunctionBegin;
2109441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2110a7e14dcfSSatish Balay 
21119566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame));
21123ba16761SJacob Faibussowitsch   if (issame) PetscFunctionReturn(PETSC_SUCCESS);
2113a7e14dcfSSatish Balay 
21149566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(TaoList, type, (void (**)(void)) & create_xxx));
21153c859ba3SBarry Smith   PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type);
2116a7e14dcfSSatish Balay 
2117a7e14dcfSSatish Balay   /* Destroy the existing solver information */
2118dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, destroy);
21191baa6e33SBarry Smith   PetscCall(KSPDestroy(&tao->ksp));
21209566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&tao->linesearch));
212183c8fe1dSLisandro Dalcin   tao->ops->setup          = NULL;
212283c8fe1dSLisandro Dalcin   tao->ops->solve          = NULL;
212383c8fe1dSLisandro Dalcin   tao->ops->view           = NULL;
212483c8fe1dSLisandro Dalcin   tao->ops->setfromoptions = NULL;
212583c8fe1dSLisandro Dalcin   tao->ops->destroy        = NULL;
2126a7e14dcfSSatish Balay 
2127a7e14dcfSSatish Balay   tao->setupcalled = PETSC_FALSE;
2128a7e14dcfSSatish Balay 
21299566063dSJacob Faibussowitsch   PetscCall((*create_xxx)(tao));
21309566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type));
21313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2132a7e14dcfSSatish Balay }
213347a47007SBarry Smith 
213467be906fSBarry Smith /*@C
213547450a7bSBarry Smith   TaoRegister - Adds a method to the Tao package for minimization.
2136a7e14dcfSSatish Balay 
2137cc4c1da9SBarry Smith   Not Collective, No Fortran Support
2138a7e14dcfSSatish Balay 
2139a7e14dcfSSatish Balay   Input Parameters:
2140a7e14dcfSSatish Balay + sname - name of a new user-defined solver
2141a7e14dcfSSatish Balay - func  - routine to Create method context
2142a7e14dcfSSatish Balay 
2143e056e8ceSJacob Faibussowitsch   Example Usage:
2144a7e14dcfSSatish Balay .vb
2145441846f8SBarry Smith    TaoRegister("my_solver", MySolverCreate);
2146a7e14dcfSSatish Balay .ve
2147a7e14dcfSSatish Balay 
2148a7e14dcfSSatish Balay   Then, your solver can be chosen with the procedural interface via
2149a7e14dcfSSatish Balay $     TaoSetType(tao, "my_solver")
2150a7e14dcfSSatish Balay   or at runtime via the option
215158417fe7SBarry Smith $     -tao_type my_solver
2152a7e14dcfSSatish Balay 
2153a7e14dcfSSatish Balay   Level: advanced
2154a7e14dcfSSatish Balay 
215567be906fSBarry Smith   Note:
215667be906fSBarry Smith   `TaoRegister()` may be called multiple times to add several user-defined solvers.
215767be906fSBarry Smith 
21581cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()`
215967be906fSBarry Smith @*/
2160d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao))
2161d71ae5a4SJacob Faibussowitsch {
2162a7e14dcfSSatish Balay   PetscFunctionBegin;
21639566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
21649566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&TaoList, sname, (void (*)(void))func));
21653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2166a7e14dcfSSatish Balay }
2167a7e14dcfSSatish Balay 
2168a7e14dcfSSatish Balay /*@C
2169441846f8SBarry Smith   TaoRegisterDestroy - Frees the list of minimization solvers that were
217047450a7bSBarry Smith   registered by `TaoRegister()`.
2171a7e14dcfSSatish Balay 
2172a7e14dcfSSatish Balay   Not Collective
2173a7e14dcfSSatish Balay 
2174a7e14dcfSSatish Balay   Level: advanced
2175a7e14dcfSSatish Balay 
21761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()`
2177a7e14dcfSSatish Balay @*/
2178d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegisterDestroy(void)
2179d71ae5a4SJacob Faibussowitsch {
2180a7e14dcfSSatish Balay   PetscFunctionBegin;
21819566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&TaoList));
2182441846f8SBarry Smith   TaoRegisterAllCalled = PETSC_FALSE;
21833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2184a7e14dcfSSatish Balay }
218547a47007SBarry Smith 
21868931d482SJason Sarich /*@
218747450a7bSBarry Smith   TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed
21888931d482SJason Sarich   at this time.
21898931d482SJason Sarich 
21908931d482SJason Sarich   Not Collective
21918931d482SJason Sarich 
21928931d482SJason Sarich   Input Parameter:
219347450a7bSBarry Smith . tao - the `Tao` context
21948931d482SJason Sarich 
21958931d482SJason Sarich   Output Parameter:
21968931d482SJason Sarich . iter - iteration number
21978931d482SJason Sarich 
21988931d482SJason Sarich   Notes:
21998931d482SJason Sarich   For example, during the computation of iteration 2 this would return 1.
22008931d482SJason Sarich 
22018931d482SJason Sarich   Level: intermediate
22028931d482SJason Sarich 
22031cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()`
22048931d482SJason Sarich @*/
2205d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter)
2206d71ae5a4SJacob Faibussowitsch {
22078931d482SJason Sarich   PetscFunctionBegin;
22088931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
22094f572ea9SToby Isaac   PetscAssertPointer(iter, 2);
22108931d482SJason Sarich   *iter = tao->niter;
22113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22128931d482SJason Sarich }
22138931d482SJason Sarich 
22148931d482SJason Sarich /*@
221547450a7bSBarry Smith   TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient)
221679f5d8caSBarry Smith   at this time.
221779f5d8caSBarry Smith 
221879f5d8caSBarry Smith   Not Collective
221979f5d8caSBarry Smith 
222079f5d8caSBarry Smith   Input Parameter:
222147450a7bSBarry Smith . tao - the `Tao` context
222279f5d8caSBarry Smith 
222379f5d8caSBarry Smith   Output Parameter:
222479f5d8caSBarry Smith . value - the current value
222579f5d8caSBarry Smith 
222679f5d8caSBarry Smith   Level: intermediate
222779f5d8caSBarry Smith 
2228e056e8ceSJacob Faibussowitsch   Developer Notes:
222967be906fSBarry Smith   This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has
223047450a7bSBarry Smith   a different meaning. For some reason `Tao` sometimes calls the gradient the residual.
223179f5d8caSBarry Smith 
22321cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()`
223379f5d8caSBarry Smith @*/
2234d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value)
2235d71ae5a4SJacob Faibussowitsch {
223679f5d8caSBarry Smith   PetscFunctionBegin;
223779f5d8caSBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
22384f572ea9SToby Isaac   PetscAssertPointer(value, 2);
223979f5d8caSBarry Smith   *value = tao->residual;
22403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
224179f5d8caSBarry Smith }
224279f5d8caSBarry Smith 
224379f5d8caSBarry Smith /*@
22448931d482SJason Sarich   TaoSetIterationNumber - Sets the current iteration number.
22458931d482SJason Sarich 
2246c3339decSBarry Smith   Logically Collective
22478931d482SJason Sarich 
2248d8d19677SJose E. Roman   Input Parameters:
224947450a7bSBarry Smith + tao  - the `Tao` context
2250a2b725a8SWilliam Gropp - iter - iteration number
22518931d482SJason Sarich 
22528931d482SJason Sarich   Level: developer
22538931d482SJason Sarich 
22541cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
22558931d482SJason Sarich @*/
2256d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter)
2257d71ae5a4SJacob Faibussowitsch {
22588931d482SJason Sarich   PetscFunctionBegin;
22598931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
226094511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, iter, 2);
22619566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
22628931d482SJason Sarich   tao->niter = iter;
22639566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
22643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22658931d482SJason Sarich }
22668931d482SJason Sarich 
22678931d482SJason Sarich /*@
226847450a7bSBarry Smith   TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations
22698931d482SJason Sarich   completed. This number keeps accumulating if multiple solves
227047450a7bSBarry Smith   are called with the `Tao` object.
22718931d482SJason Sarich 
22728931d482SJason Sarich   Not Collective
22738931d482SJason Sarich 
22748931d482SJason Sarich   Input Parameter:
227547450a7bSBarry Smith . tao - the `Tao` context
22768931d482SJason Sarich 
22778931d482SJason Sarich   Output Parameter:
227847450a7bSBarry Smith . iter - number of iterations
22798931d482SJason Sarich 
22808931d482SJason Sarich   Level: intermediate
22818931d482SJason Sarich 
228220f4b53cSBarry Smith   Note:
228367be906fSBarry Smith   The total iteration count is updated after each solve, if there is a current
228447450a7bSBarry Smith   `TaoSolve()` in progress then those iterations are not included in the count
228567be906fSBarry Smith 
22861cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
22878931d482SJason Sarich @*/
2288d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter)
2289d71ae5a4SJacob Faibussowitsch {
22908931d482SJason Sarich   PetscFunctionBegin;
22918931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
22924f572ea9SToby Isaac   PetscAssertPointer(iter, 2);
22938931d482SJason Sarich   *iter = tao->ntotalits;
22943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22958931d482SJason Sarich }
22968931d482SJason Sarich 
22978931d482SJason Sarich /*@
22988931d482SJason Sarich   TaoSetTotalIterationNumber - Sets the current total iteration number.
22998931d482SJason Sarich 
2300c3339decSBarry Smith   Logically Collective
23018931d482SJason Sarich 
2302d8d19677SJose E. Roman   Input Parameters:
230347450a7bSBarry Smith + tao  - the `Tao` context
230447450a7bSBarry Smith - iter - the iteration number
23058931d482SJason Sarich 
23068931d482SJason Sarich   Level: developer
23078931d482SJason Sarich 
23081cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
23098931d482SJason Sarich @*/
2310d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter)
2311d71ae5a4SJacob Faibussowitsch {
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->ntotalits = iter;
23179566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
23183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23198931d482SJason Sarich }
23208931d482SJason Sarich 
2321a7e14dcfSSatish Balay /*@
232247450a7bSBarry Smith   TaoSetConvergedReason - Sets the termination flag on a `Tao` object
2323a7e14dcfSSatish Balay 
2324c3339decSBarry Smith   Logically Collective
2325a7e14dcfSSatish Balay 
2326a7e14dcfSSatish Balay   Input Parameters:
232747450a7bSBarry Smith + tao    - the `Tao` context
232847450a7bSBarry Smith - reason - the `TaoConvergedReason`
2329a7e14dcfSSatish Balay 
2330a7e14dcfSSatish Balay   Level: intermediate
2331a7e14dcfSSatish Balay 
23321cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`
2333a7e14dcfSSatish Balay @*/
2334d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2335d71ae5a4SJacob Faibussowitsch {
2336a7e14dcfSSatish Balay   PetscFunctionBegin;
233794511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
233894511df7SStefano Zampini   PetscValidLogicalCollectiveEnum(tao, reason, 2);
2339a7e14dcfSSatish Balay   tao->reason = reason;
23403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2341a7e14dcfSSatish Balay }
2342a7e14dcfSSatish Balay 
2343a7e14dcfSSatish Balay /*@
234447450a7bSBarry Smith   TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped.
2345a7e14dcfSSatish Balay 
2346a7e14dcfSSatish Balay   Not Collective
2347a7e14dcfSSatish Balay 
2348a7e14dcfSSatish Balay   Input Parameter:
234947450a7bSBarry Smith . tao - the `Tao` solver context
2350a7e14dcfSSatish Balay 
2351a7e14dcfSSatish Balay   Output Parameter:
235247450a7bSBarry Smith . reason - value of `TaoConvergedReason`
2353a7e14dcfSSatish Balay 
2354a7e14dcfSSatish Balay   Level: intermediate
2355a7e14dcfSSatish Balay 
23561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()`
2357a7e14dcfSSatish Balay @*/
2358d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2359d71ae5a4SJacob Faibussowitsch {
2360a7e14dcfSSatish Balay   PetscFunctionBegin;
2361441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
23624f572ea9SToby Isaac   PetscAssertPointer(reason, 2);
2363a7e14dcfSSatish Balay   *reason = tao->reason;
23643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2365a7e14dcfSSatish Balay }
2366a7e14dcfSSatish Balay 
2367a7e14dcfSSatish Balay /*@
2368a7e14dcfSSatish Balay   TaoGetSolutionStatus - Get the current iterate, objective value,
236947450a7bSBarry Smith   residual, infeasibility, and termination from a `Tao` object
2370a7e14dcfSSatish Balay 
2371a7e14dcfSSatish Balay   Not Collective
2372a7e14dcfSSatish Balay 
2373f899ff85SJose E. Roman   Input Parameter:
237447450a7bSBarry Smith . tao - the `Tao` context
2375a7e14dcfSSatish Balay 
2376a7e14dcfSSatish Balay   Output Parameters:
2377e056e8ceSJacob Faibussowitsch + its    - the current iterate number (>=0)
2378a7e14dcfSSatish Balay . f      - the current function value
237958417fe7SBarry Smith . gnorm  - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2380a7e14dcfSSatish Balay . cnorm  - the infeasibility of the current solution with regard to the constraints.
2381a7e14dcfSSatish Balay . xdiff  - the step length or trust region radius of the most recent iterate.
238265ba42b6SBarry Smith - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING`
2383a7e14dcfSSatish Balay 
2384a7e14dcfSSatish Balay   Level: intermediate
2385a7e14dcfSSatish Balay 
238665ba42b6SBarry Smith   Notes:
238765ba42b6SBarry Smith   Tao returns the values set by the solvers in the routine `TaoMonitor()`.
2388a7e14dcfSSatish Balay 
238967be906fSBarry Smith   If any of the output arguments are set to `NULL`, no corresponding value will be returned.
2390a7e14dcfSSatish Balay 
23911cc06b55SBarry Smith .seealso: [](ch_tao), `TaoMonitor()`, `TaoGetConvergedReason()`
2392a7e14dcfSSatish Balay @*/
2393d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2394d71ae5a4SJacob Faibussowitsch {
2395a7e14dcfSSatish Balay   PetscFunctionBegin;
239694511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2397a7e14dcfSSatish Balay   if (its) *its = tao->niter;
2398a7e14dcfSSatish Balay   if (f) *f = tao->fc;
2399a7e14dcfSSatish Balay   if (gnorm) *gnorm = tao->residual;
2400a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->cnorm;
2401a7e14dcfSSatish Balay   if (reason) *reason = tao->reason;
2402a7e14dcfSSatish Balay   if (xdiff) *xdiff = tao->step;
24033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2404a7e14dcfSSatish Balay }
2405a7e14dcfSSatish Balay 
2406cc4c1da9SBarry Smith /*@
240747450a7bSBarry Smith   TaoGetType - Gets the current `TaoType` being used in the `Tao` object
2408a7e14dcfSSatish Balay 
2409a7e14dcfSSatish Balay   Not Collective
2410a7e14dcfSSatish Balay 
2411a7e14dcfSSatish Balay   Input Parameter:
241247450a7bSBarry Smith . tao - the `Tao` solver context
2413a7e14dcfSSatish Balay 
2414a7e14dcfSSatish Balay   Output Parameter:
241547450a7bSBarry Smith . type - the `TaoType`
2416a7e14dcfSSatish Balay 
2417a7e14dcfSSatish Balay   Level: intermediate
2418a7e14dcfSSatish Balay 
24191cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoType`, `TaoSetType()`
2420a7e14dcfSSatish Balay @*/
2421d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetType(Tao tao, TaoType *type)
2422d71ae5a4SJacob Faibussowitsch {
2423a7e14dcfSSatish Balay   PetscFunctionBegin;
2424441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
24254f572ea9SToby Isaac   PetscAssertPointer(type, 2);
2426a7e14dcfSSatish Balay   *type = ((PetscObject)tao)->type_name;
24273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2428a7e14dcfSSatish Balay }
2429a7e14dcfSSatish Balay 
2430a7e14dcfSSatish Balay /*@C
2431a7e14dcfSSatish Balay   TaoMonitor - Monitor the solver and the current solution.  This
2432a7e14dcfSSatish Balay   routine will record the iteration number and residual statistics,
2433670c031eSStefano Zampini   and call any monitors specified by the user.
2434a7e14dcfSSatish Balay 
2435a7e14dcfSSatish Balay   Input Parameters:
243647450a7bSBarry Smith + tao        - the `Tao` context
2437a7e14dcfSSatish Balay . its        - the current iterate number (>=0)
2438a7e14dcfSSatish Balay . f          - the current objective function value
24391cb3f120SPierre Jolivet . res        - the gradient norm, square root of the duality gap, or other measure indicating distance from optimality.  This measure will be recorded and
2440a7e14dcfSSatish Balay           used for some termination tests.
2441a7e14dcfSSatish Balay . cnorm      - the infeasibility of the current solution with regard to the constraints.
2442a7e14dcfSSatish Balay - steplength - multiple of the step direction added to the previous iterate.
2443a7e14dcfSSatish Balay 
2444a7e14dcfSSatish Balay   Options Database Key:
2445a7e14dcfSSatish Balay . -tao_monitor - Use the default monitor, which prints statistics to standard output
2446a7e14dcfSSatish Balay 
2447a7e14dcfSSatish Balay   Level: developer
2448a7e14dcfSSatish Balay 
244910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoMonitorSet()`
2450a7e14dcfSSatish Balay @*/
2451d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength)
2452d71ae5a4SJacob Faibussowitsch {
245347a47007SBarry Smith   PetscInt i;
245447a47007SBarry Smith 
2455a7e14dcfSSatish Balay   PetscFunctionBegin;
2456441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2457a7e14dcfSSatish Balay   tao->fc       = f;
2458a7e14dcfSSatish Balay   tao->residual = res;
2459a7e14dcfSSatish Balay   tao->cnorm    = cnorm;
2460a7e14dcfSSatish Balay   tao->step     = steplength;
2461e52336cbSBarry Smith   if (!its) {
246294511df7SStefano Zampini     tao->cnorm0 = cnorm;
246394511df7SStefano Zampini     tao->gnorm0 = res;
2464a7e14dcfSSatish Balay   }
2465bfdcf5a2SStefano Zampini   PetscCall(VecLockReadPush(tao->solution));
246648a46eb9SPierre Jolivet   for (i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i]));
2467bfdcf5a2SStefano Zampini   PetscCall(VecLockReadPop(tao->solution));
24683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2469a7e14dcfSSatish Balay }
2470a7e14dcfSSatish Balay 
2471a7e14dcfSSatish Balay /*@
2472ae93cb3cSJason Sarich   TaoSetConvergenceHistory - Sets the array used to hold the convergence history.
2473a7e14dcfSSatish Balay 
2474c3339decSBarry Smith   Logically Collective
2475a7e14dcfSSatish Balay 
2476a7e14dcfSSatish Balay   Input Parameters:
247747450a7bSBarry Smith + tao   - the `Tao` solver context
2478a7e14dcfSSatish Balay . obj   - array to hold objective value history
2479a7e14dcfSSatish Balay . resid - array to hold residual history
2480a7e14dcfSSatish Balay . cnorm - array to hold constraint violation history
2481be1558d8SJason Sarich . lits  - integer array holds the number of linear iterations for each Tao iteration
248267be906fSBarry Smith . na    - size of `obj`, `resid`, and `cnorm`
248365ba42b6SBarry Smith - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero,
2484a7e14dcfSSatish Balay            else it continues storing new values for new minimizations after the old ones
2485a7e14dcfSSatish Balay 
248667be906fSBarry Smith   Level: intermediate
248767be906fSBarry Smith 
2488a7e14dcfSSatish Balay   Notes:
248947450a7bSBarry Smith   If set, `Tao` will fill the given arrays with the indicated
2490ae93cb3cSJason Sarich   information at each iteration.  If 'obj','resid','cnorm','lits' are
249167be906fSBarry Smith   *all* `NULL` then space (using size `na`, or 1000 if na is `PETSC_DECIDE` or
249265ba42b6SBarry Smith   `PETSC_DEFAULT`) is allocated for the history.
249367be906fSBarry Smith   If not all are `NULL`, then only the non-`NULL` information categories
2494ae93cb3cSJason Sarich   will be stored, the others will be ignored.
2495ae93cb3cSJason Sarich 
2496ae93cb3cSJason Sarich   Any convergence information after iteration number 'na' will not be stored.
2497a7e14dcfSSatish Balay 
2498a7e14dcfSSatish Balay   This routine is useful, e.g., when running a code for purposes
2499a7e14dcfSSatish Balay   of accurate performance monitoring, when no I/O should be done
2500a7e14dcfSSatish Balay   during the section of code that is being timed.
2501a7e14dcfSSatish Balay 
25021cc06b55SBarry Smith .seealso: [](ch_tao), `TaoGetConvergenceHistory()`
2503a7e14dcfSSatish Balay @*/
2504d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset)
2505d71ae5a4SJacob Faibussowitsch {
2506a7e14dcfSSatish Balay   PetscFunctionBegin;
2507441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
25084f572ea9SToby Isaac   if (obj) PetscAssertPointer(obj, 2);
25094f572ea9SToby Isaac   if (resid) PetscAssertPointer(resid, 3);
25104f572ea9SToby Isaac   if (cnorm) PetscAssertPointer(cnorm, 4);
25114f572ea9SToby Isaac   if (lits) PetscAssertPointer(lits, 5);
2512ae93cb3cSJason Sarich 
2513ae93cb3cSJason Sarich   if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000;
2514ae93cb3cSJason Sarich   if (!obj && !resid && !cnorm && !lits) {
25159566063dSJacob Faibussowitsch     PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits));
2516ae93cb3cSJason Sarich     tao->hist_malloc = PETSC_TRUE;
2517ae93cb3cSJason Sarich   }
2518ae93cb3cSJason Sarich 
2519a7e14dcfSSatish Balay   tao->hist_obj   = obj;
2520a7e14dcfSSatish Balay   tao->hist_resid = resid;
2521a7e14dcfSSatish Balay   tao->hist_cnorm = cnorm;
2522ae93cb3cSJason Sarich   tao->hist_lits  = lits;
2523a7e14dcfSSatish Balay   tao->hist_max   = na;
2524a7e14dcfSSatish Balay   tao->hist_reset = reset;
2525ae93cb3cSJason Sarich   tao->hist_len   = 0;
25263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2527a7e14dcfSSatish Balay }
2528a7e14dcfSSatish Balay 
2529a7e14dcfSSatish Balay /*@C
253065ba42b6SBarry Smith   TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history.
2531a7e14dcfSSatish Balay 
2532c3339decSBarry Smith   Collective
2533a7e14dcfSSatish Balay 
2534a7e14dcfSSatish Balay   Input Parameter:
253547450a7bSBarry Smith . tao - the `Tao` context
2536a7e14dcfSSatish Balay 
2537a7e14dcfSSatish Balay   Output Parameters:
2538a7e14dcfSSatish Balay + obj   - array used to hold objective value history
2539a7e14dcfSSatish Balay . resid - array used to hold residual history
2540a7e14dcfSSatish Balay . cnorm - array used to hold constraint violation history
2541ae93cb3cSJason Sarich . lits  - integer array used to hold linear solver iteration count
254267be906fSBarry Smith - nhist - size of `obj`, `resid`, `cnorm`, and `lits`
254367be906fSBarry Smith 
254467be906fSBarry Smith   Level: advanced
2545a7e14dcfSSatish Balay 
2546a7e14dcfSSatish Balay   Notes:
254765ba42b6SBarry Smith   This routine must be preceded by calls to `TaoSetConvergenceHistory()`
254865ba42b6SBarry Smith   and `TaoSolve()`, otherwise it returns useless information.
2549ae93cb3cSJason Sarich 
2550a7e14dcfSSatish Balay   This routine is useful, e.g., when running a code for purposes
2551a7e14dcfSSatish Balay   of accurate performance monitoring, when no I/O should be done
2552a7e14dcfSSatish Balay   during the section of code that is being timed.
2553a7e14dcfSSatish Balay 
2554e056e8ceSJacob Faibussowitsch   Fortran Notes:
255567be906fSBarry Smith   The calling sequence is
255667be906fSBarry Smith .vb
255767be906fSBarry Smith    call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr)
255867be906fSBarry Smith .ve
2559a3b724e8SBarry Smith   In other words this gets the current number of entries in the history. Access the history through the array you passed to `TaoSetConvergenceHistory()`
2560a7e14dcfSSatish Balay 
25611cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()`
2562a7e14dcfSSatish Balay @*/
2563d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist)
2564d71ae5a4SJacob Faibussowitsch {
2565a7e14dcfSSatish Balay   PetscFunctionBegin;
2566441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2567a7e14dcfSSatish Balay   if (obj) *obj = tao->hist_obj;
2568a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->hist_cnorm;
2569a7e14dcfSSatish Balay   if (resid) *resid = tao->hist_resid;
25705b494b05SBarry Smith   if (lits) *lits = tao->hist_lits;
2571a7e14dcfSSatish Balay   if (nhist) *nhist = tao->hist_len;
25723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2573a7e14dcfSSatish Balay }
2574a7e14dcfSSatish Balay 
2575a7e14dcfSSatish Balay /*@
257647450a7bSBarry Smith   TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver.
2577a7e14dcfSSatish Balay 
2578c3339decSBarry Smith   Logically Collective
2579a7e14dcfSSatish Balay 
2580a7e14dcfSSatish Balay   Input Parameters:
258147450a7bSBarry Smith + tao  - the `Tao` context
2582a7e14dcfSSatish Balay - usrP - optional user context
2583a7e14dcfSSatish Balay 
2584a7e14dcfSSatish Balay   Level: intermediate
2585a7e14dcfSSatish Balay 
258642747ad1SJacob Faibussowitsch .seealso: [](ch_tao), `Tao`, `TaoGetApplicationContext()`
2587a7e14dcfSSatish Balay @*/
2588d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetApplicationContext(Tao tao, void *usrP)
2589d71ae5a4SJacob Faibussowitsch {
2590a7e14dcfSSatish Balay   PetscFunctionBegin;
2591441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2592a7e14dcfSSatish Balay   tao->user = usrP;
25933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2594a7e14dcfSSatish Balay }
2595a7e14dcfSSatish Balay 
2596a7e14dcfSSatish Balay /*@
259747450a7bSBarry Smith   TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver
2598a7e14dcfSSatish Balay 
2599a7e14dcfSSatish Balay   Not Collective
2600a7e14dcfSSatish Balay 
2601a7e14dcfSSatish Balay   Input Parameter:
260247450a7bSBarry Smith . tao - the `Tao` context
2603a7e14dcfSSatish Balay 
2604a7e14dcfSSatish Balay   Output Parameter:
2605a7e14dcfSSatish Balay . usrP - user context
2606a7e14dcfSSatish Balay 
2607a7e14dcfSSatish Balay   Level: intermediate
2608a7e14dcfSSatish Balay 
26091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetApplicationContext()`
2610a7e14dcfSSatish Balay @*/
2611d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetApplicationContext(Tao tao, void *usrP)
2612d71ae5a4SJacob Faibussowitsch {
2613a7e14dcfSSatish Balay   PetscFunctionBegin;
2614441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
26154f572ea9SToby Isaac   PetscAssertPointer(usrP, 2);
2616a7e14dcfSSatish Balay   *(void **)usrP = tao->user;
26173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2618a7e14dcfSSatish Balay }
2619a9603a14SPatrick Farrell 
2620a9603a14SPatrick Farrell /*@
262165ba42b6SBarry Smith   TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient.
2622a9603a14SPatrick Farrell 
2623c3339decSBarry Smith   Collective
2624a9603a14SPatrick Farrell 
2625a9603a14SPatrick Farrell   Input Parameters:
262647450a7bSBarry Smith + tao - the `Tao` context
262765ba42b6SBarry Smith - M   - matrix that defines the norm
2628a9603a14SPatrick Farrell 
2629a9603a14SPatrick Farrell   Level: beginner
2630a9603a14SPatrick Farrell 
26311cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()`
2632a9603a14SPatrick Farrell @*/
2633d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M)
2634d71ae5a4SJacob Faibussowitsch {
2635a9603a14SPatrick Farrell   PetscFunctionBegin;
2636a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
263794511df7SStefano Zampini   PetscValidHeaderSpecific(M, MAT_CLASSID, 2);
26389566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)M));
26399566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&tao->gradient_norm));
26409566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&tao->gradient_norm_tmp));
2641a9603a14SPatrick Farrell   tao->gradient_norm = M;
26429566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp));
26433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2644a9603a14SPatrick Farrell }
2645a9603a14SPatrick Farrell 
2646a9603a14SPatrick Farrell /*@
264765ba42b6SBarry Smith   TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient.
2648a9603a14SPatrick Farrell 
2649a9603a14SPatrick Farrell   Not Collective
2650a9603a14SPatrick Farrell 
2651a9603a14SPatrick Farrell   Input Parameter:
265247450a7bSBarry Smith . tao - the `Tao` context
2653a9603a14SPatrick Farrell 
2654a9603a14SPatrick Farrell   Output Parameter:
2655a9603a14SPatrick Farrell . M - gradient norm
2656a9603a14SPatrick Farrell 
2657a9603a14SPatrick Farrell   Level: beginner
2658a9603a14SPatrick Farrell 
26591cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()`
2660a9603a14SPatrick Farrell @*/
2661d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M)
2662d71ae5a4SJacob Faibussowitsch {
2663a9603a14SPatrick Farrell   PetscFunctionBegin;
2664a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
26654f572ea9SToby Isaac   PetscAssertPointer(M, 2);
2666a9603a14SPatrick Farrell   *M = tao->gradient_norm;
26673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2668a9603a14SPatrick Farrell }
2669a9603a14SPatrick Farrell 
2670cc4c1da9SBarry Smith /*@
267147450a7bSBarry Smith   TaoGradientNorm - Compute the norm using the `NormType`, the user has selected
2672a9603a14SPatrick Farrell 
2673c3339decSBarry Smith   Collective
2674a9603a14SPatrick Farrell 
2675d8d19677SJose E. Roman   Input Parameters:
267647450a7bSBarry Smith + tao      - the `Tao` context
2677a9603a14SPatrick Farrell . gradient - the gradient to be computed
26783b242c63SJacob Faibussowitsch - type     - the norm type
2679a9603a14SPatrick Farrell 
2680a9603a14SPatrick Farrell   Output Parameter:
2681a9603a14SPatrick Farrell . gnorm - the gradient norm
2682a9603a14SPatrick Farrell 
268347450a7bSBarry Smith   Level: advanced
2684a9603a14SPatrick Farrell 
26851cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()`
2686a9603a14SPatrick Farrell @*/
2687d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm)
2688d71ae5a4SJacob Faibussowitsch {
2689a9603a14SPatrick Farrell   PetscFunctionBegin;
26908854b543SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
26918854b543SStefano Zampini   PetscValidHeaderSpecific(gradient, VEC_CLASSID, 2);
26928854b543SStefano Zampini   PetscValidLogicalCollectiveEnum(tao, type, 3);
26934f572ea9SToby Isaac   PetscAssertPointer(gnorm, 4);
2694a9603a14SPatrick Farrell   if (tao->gradient_norm) {
2695680e2bc7SPatrick Farrell     PetscScalar gnorms;
2696680e2bc7SPatrick Farrell 
26973c859ba3SBarry 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.");
26989566063dSJacob Faibussowitsch     PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp));
26999566063dSJacob Faibussowitsch     PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms));
27007bb79937SPatrick Farrell     *gnorm = PetscRealPart(PetscSqrtScalar(gnorms));
2701a9603a14SPatrick Farrell   } else {
27029566063dSJacob Faibussowitsch     PetscCall(VecNorm(gradient, type, gnorm));
2703a9603a14SPatrick Farrell   }
27043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2705a9603a14SPatrick Farrell }
2706a9603a14SPatrick Farrell 
2707e882e171SHong Zhang /*@C
270847450a7bSBarry Smith   TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorDrawSolution()`
2709a9603a14SPatrick Farrell 
2710c3339decSBarry Smith   Collective
2711e882e171SHong Zhang 
27122fe279fdSBarry Smith   Input Parameters:
27132fe279fdSBarry Smith + comm     - the communicator to share the context
27142fe279fdSBarry Smith . host     - the name of the X Windows host that will display the monitor
27152fe279fdSBarry Smith . label    - the label to put at the top of the display window
27162fe279fdSBarry Smith . x        - the horizontal coordinate of the lower left corner of the window to open
27172fe279fdSBarry Smith . y        - the vertical coordinate of the lower left corner of the window to open
27182fe279fdSBarry Smith . m        - the width of the window
27192fe279fdSBarry Smith . n        - the height of the window
27202fe279fdSBarry Smith - howoften - how many `Tao` iterations between displaying the monitor information
27212fe279fdSBarry Smith 
2722d5b43468SJose E. Roman   Output Parameter:
2723e882e171SHong Zhang . ctx - the monitor context
2724e882e171SHong Zhang 
27253c7db156SBarry Smith   Options Database Key:
2726e882e171SHong Zhang . -tao_draw_solution_initial - show initial guess as well as current solution
2727e882e171SHong Zhang 
2728e882e171SHong Zhang   Level: intermediate
2729e882e171SHong Zhang 
27301cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()`
2731e882e171SHong Zhang @*/
2732d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx)
2733d71ae5a4SJacob Faibussowitsch {
2734e882e171SHong Zhang   PetscFunctionBegin;
27359566063dSJacob Faibussowitsch   PetscCall(PetscNew(ctx));
27369566063dSJacob Faibussowitsch   PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer));
27379566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetFromOptions((*ctx)->viewer));
2738e882e171SHong Zhang   (*ctx)->howoften = howoften;
27393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2740e882e171SHong Zhang }
2741e882e171SHong Zhang 
2742e882e171SHong Zhang /*@C
274365ba42b6SBarry Smith   TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorDrawSolution()`
2744e882e171SHong Zhang 
2745c3339decSBarry Smith   Collective
2746e882e171SHong Zhang 
274747450a7bSBarry Smith   Input Parameter:
2748e056e8ceSJacob Faibussowitsch . ictx - the monitor context
2749e882e171SHong Zhang 
2750e882e171SHong Zhang   Level: intermediate
2751e882e171SHong Zhang 
27521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawSolution()`
2753e882e171SHong Zhang @*/
2754d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx)
2755d71ae5a4SJacob Faibussowitsch {
2756e882e171SHong Zhang   PetscFunctionBegin;
27579566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*ictx)->viewer));
27589566063dSJacob Faibussowitsch   PetscCall(PetscFree(*ictx));
27593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2760e882e171SHong Zhang }
2761