xref: /petsc/src/tao/interface/taosolver.c (revision b44f4de476a10dd52a698c0f9a8f47d015bd1e19)
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 /*@
73606f75f6SBarry Smith   TaoParametersInitialize - Sets all the parameters in `tao` to their default value (when `TaoCreate()` was called) if they
74606f75f6SBarry Smith   currently contain default values. Default values are the parameter values when the object's type is set.
75606f75f6SBarry Smith 
76606f75f6SBarry Smith   Collective
77606f75f6SBarry Smith 
78606f75f6SBarry Smith   Input Parameter:
79606f75f6SBarry Smith . tao - the `Tao` object
80606f75f6SBarry Smith 
81606f75f6SBarry Smith   Level: developer
82606f75f6SBarry Smith 
83606f75f6SBarry Smith   Developer Note:
84606f75f6SBarry Smith   This is called by all the `TaoCreate_XXX()` routines.
85606f75f6SBarry Smith 
86606f75f6SBarry Smith .seealso: [](ch_snes), `Tao`, `TaoSolve()`, `TaoDestroy()`,
87606f75f6SBarry Smith           `PetscObjectParameterSetDefault()`
88606f75f6SBarry Smith @*/
89606f75f6SBarry Smith PetscErrorCode TaoParametersInitialize(Tao tao)
90606f75f6SBarry Smith {
91606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, max_it, 10000);
92606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, max_funcs, PETSC_UNLIMITED);
93606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, gatol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
94606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, grtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
95606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, crtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
96606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, catol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
97606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, gttol, 0.0);
98606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, steptol, 0.0);
99606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, fmin, PETSC_NINFINITY);
100606f75f6SBarry Smith   PetscObjectParameterSetDefault(tao, trust0, PETSC_INFINITY);
101606f75f6SBarry Smith   return PETSC_SUCCESS;
102606f75f6SBarry Smith }
103606f75f6SBarry Smith 
104606f75f6SBarry Smith /*@
10565ba42b6SBarry Smith   TaoCreate - Creates a Tao solver
106a7e14dcfSSatish Balay 
107d083f849SBarry Smith   Collective
108a7e14dcfSSatish Balay 
109a7e14dcfSSatish Balay   Input Parameter:
110a7e14dcfSSatish Balay . comm - MPI communicator
111a7e14dcfSSatish Balay 
112a7e14dcfSSatish Balay   Output Parameter:
11347450a7bSBarry Smith . newtao - the new `Tao` context
114a7e14dcfSSatish Balay 
11547450a7bSBarry Smith   Options Database Key:
11665ba42b6SBarry Smith . -tao_type - select which method Tao should use
117a7e14dcfSSatish Balay 
118a7e14dcfSSatish Balay   Level: beginner
119a7e14dcfSSatish Balay 
1200241b274SPierre Jolivet .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TaoSetFromOptions()`, `TaoSetType()`
121a7e14dcfSSatish Balay @*/
122d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao)
123d71ae5a4SJacob Faibussowitsch {
124441846f8SBarry Smith   Tao tao;
125a7e14dcfSSatish Balay 
126a7e14dcfSSatish Balay   PetscFunctionBegin;
1274f572ea9SToby Isaac   PetscAssertPointer(newtao, 2);
1289566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
1299566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchInitializePackage());
130a7e14dcfSSatish Balay 
131606f75f6SBarry Smith   PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView));
13294511df7SStefano Zampini   tao->ops->convergencetest = TaoDefaultConvergenceTest;
133606f75f6SBarry Smith 
134a7e14dcfSSatish Balay   tao->hist_reset = PETSC_TRUE;
1359566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
136a7e14dcfSSatish Balay   *newtao = tao;
1373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
138a7e14dcfSSatish Balay }
139a7e14dcfSSatish Balay 
140a7e14dcfSSatish Balay /*@
141a7e14dcfSSatish Balay   TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
142a7e14dcfSSatish Balay 
143c3339decSBarry Smith   Collective
144a7e14dcfSSatish Balay 
14547450a7bSBarry Smith   Input Parameter:
14647450a7bSBarry Smith . tao - the `Tao` context
147a7e14dcfSSatish Balay 
14867be906fSBarry Smith   Level: beginner
14967be906fSBarry Smith 
150a7e14dcfSSatish Balay   Notes:
15147450a7bSBarry Smith   The user must set up the `Tao` object  with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`.
152a7e14dcfSSatish Balay 
15367be906fSBarry Smith   You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or
154a35d58b8SBarry Smith   why it failed.
155a35d58b8SBarry Smith 
1561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()`
157a7e14dcfSSatish Balay  @*/
158d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolve(Tao tao)
159d71ae5a4SJacob Faibussowitsch {
160e2379f4fSBarry Smith   static PetscBool set = PETSC_FALSE;
16147a47007SBarry Smith 
162a7e14dcfSSatish Balay   PetscFunctionBegin;
163441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
164d0609cedSBarry Smith   PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n"
165e2379f4fSBarry Smith                                    "title   = {Toolkit for Advanced Optimization (TAO) Users Manual},\n"
166e2379f4fSBarry Smith                                    "author  = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n"
167e2379f4fSBarry Smith                                    "Institution = {Argonne National Laboratory},\n"
168e2379f4fSBarry Smith                                    "Year   = 2014,\n"
169e2379f4fSBarry Smith                                    "Number = {ANL/MCS-TM-322 - Revision 3.5},\n"
1709371c9d4SSatish Balay                                    "url    = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n",
1719371c9d4SSatish Balay                                    &set));
172494bef23SAlp Dener   tao->header_printed = PETSC_FALSE;
1739566063dSJacob Faibussowitsch   PetscCall(TaoSetUp(tao));
1749566063dSJacob Faibussowitsch   PetscCall(TaoResetStatistics(tao));
1751baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch));
176a7e14dcfSSatish Balay 
1779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0));
178dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, solve);
1799566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0));
180a7e14dcfSSatish Balay 
1819566063dSJacob Faibussowitsch   PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution"));
1827cf37e64SBarry Smith 
1838931d482SJason Sarich   tao->ntotalits += tao->niter;
184a7e14dcfSSatish Balay 
185a7e14dcfSSatish Balay   if (tao->printreason) {
186f642d338SBarry Smith     PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
187084fd5feSPierre Jolivet 
188f642d338SBarry Smith     PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel));
189a7e14dcfSSatish Balay     if (tao->reason > 0) {
190084fd5feSPierre Jolivet       if (((PetscObject)tao)->prefix) {
191084fd5feSPierre Jolivet         PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter));
192a7e14dcfSSatish Balay       } else {
193084fd5feSPierre Jolivet         PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO solve converged due to %s iterations %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter));
194084fd5feSPierre Jolivet       }
195084fd5feSPierre Jolivet     } else {
196084fd5feSPierre Jolivet       if (((PetscObject)tao)->prefix) {
197084fd5feSPierre Jolivet         PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter));
198084fd5feSPierre Jolivet       } else {
199084fd5feSPierre Jolivet         PetscCall(PetscViewerASCIIPrintf(viewer, "  TAO solve did not converge due to %s iteration %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter));
200084fd5feSPierre Jolivet       }
201a7e14dcfSSatish Balay     }
202f642d338SBarry Smith     PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel));
203a7e14dcfSSatish Balay   }
204f642d338SBarry Smith   PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view"));
2053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
206a7e14dcfSSatish Balay }
207a7e14dcfSSatish Balay 
208a7e14dcfSSatish Balay /*@
209a7e14dcfSSatish Balay   TaoSetUp - Sets up the internal data structures for the later use
210a7e14dcfSSatish Balay   of a Tao solver
211a7e14dcfSSatish Balay 
212c3339decSBarry Smith   Collective
213a7e14dcfSSatish Balay 
21447450a7bSBarry Smith   Input Parameter:
21547450a7bSBarry Smith . tao - the `Tao` context
216a7e14dcfSSatish Balay 
21767be906fSBarry Smith   Level: advanced
21867be906fSBarry Smith 
21947450a7bSBarry Smith   Note:
22065ba42b6SBarry Smith   The user will not need to explicitly call `TaoSetUp()`, as it will
22165ba42b6SBarry Smith   automatically be called in `TaoSolve()`.  However, if the user
22265ba42b6SBarry Smith   desires to call it explicitly, it should come after `TaoCreate()`
22365ba42b6SBarry Smith   and any TaoSetSomething() routines, but before `TaoSolve()`.
224a7e14dcfSSatish Balay 
2251cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
226a7e14dcfSSatish Balay @*/
227d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp(Tao tao)
228d71ae5a4SJacob Faibussowitsch {
229a7e14dcfSSatish Balay   PetscFunctionBegin;
230441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2313ba16761SJacob Faibussowitsch   if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
2320f0abf79SStefano Zampini   PetscCall(TaoSetUpEW_Private(tao));
2333c859ba3SBarry Smith   PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution");
234dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, setup);
235a7e14dcfSSatish Balay   tao->setupcalled = PETSC_TRUE;
2363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
237a7e14dcfSSatish Balay }
238a7e14dcfSSatish Balay 
2390764c050SBarry Smith /*@
24047450a7bSBarry Smith   TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()`
241a7e14dcfSSatish Balay 
242c3339decSBarry Smith   Collective
243a7e14dcfSSatish Balay 
244a7e14dcfSSatish Balay   Input Parameter:
24547450a7bSBarry Smith . tao - the `Tao` context
246a7e14dcfSSatish Balay 
247a7e14dcfSSatish Balay   Level: beginner
248a7e14dcfSSatish Balay 
2491cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
250a7e14dcfSSatish Balay @*/
251d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy(Tao *tao)
252d71ae5a4SJacob Faibussowitsch {
253a7e14dcfSSatish Balay   PetscFunctionBegin;
2543ba16761SJacob Faibussowitsch   if (!*tao) PetscFunctionReturn(PETSC_SUCCESS);
255441846f8SBarry Smith   PetscValidHeaderSpecific(*tao, TAO_CLASSID, 1);
2569371c9d4SSatish Balay   if (--((PetscObject)*tao)->refct > 0) {
2579371c9d4SSatish Balay     *tao = NULL;
2583ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
259a7e14dcfSSatish Balay   }
2609371c9d4SSatish Balay 
261213acdd3SPierre Jolivet   PetscTryTypeMethod(*tao, destroy);
2629566063dSJacob Faibussowitsch   PetscCall(KSPDestroy(&(*tao)->ksp));
2630f0abf79SStefano Zampini   PetscCall(SNESDestroy(&(*tao)->snes_ewdummy));
2649566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch));
265a7e14dcfSSatish Balay 
266a7e14dcfSSatish Balay   if ((*tao)->ops->convergencedestroy) {
2679566063dSJacob Faibussowitsch     PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP));
26848a46eb9SPierre Jolivet     if ((*tao)->jacobian_state_inv) PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
269a7e14dcfSSatish Balay   }
2709566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->solution));
2719566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->gradient));
2729566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->ls_res));
273a7e14dcfSSatish Balay 
274a9603a14SPatrick Farrell   if ((*tao)->gradient_norm) {
2759566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm));
2769566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp));
277a9603a14SPatrick Farrell   }
278a9603a14SPatrick Farrell 
2799566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XL));
2809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->XU));
2819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IL));
2829566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->IU));
2839566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DE));
2849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->DI));
2859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints));
2869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_equality));
2879566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->constraints_inequality));
2889566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->stepdirection));
2899566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian_pre));
2909566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->hessian));
2919566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac));
2929566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->ls_jac_pre));
2939566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_pre));
2949566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian));
2959566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_pre));
2969566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state));
2979566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
2989566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_design));
2999566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality));
3009566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre));
3019566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality));
3029566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre));
3039566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->state_is));
3049566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&(*tao)->design_is));
3059566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&(*tao)->res_weights_v));
306b2dc45ddSPierre Jolivet   PetscCall(TaoMonitorCancel(*tao));
30748a46eb9SPierre Jolivet   if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits));
308737f463aSAlp Dener   if ((*tao)->res_weights_n) {
3099566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_rows));
3109566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_cols));
3119566063dSJacob Faibussowitsch     PetscCall(PetscFree((*tao)->res_weights_w));
312125ddc8aSJason Sarich   }
3139566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(tao));
3143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
315a7e14dcfSSatish Balay }
316a7e14dcfSSatish Balay 
317a7e14dcfSSatish Balay /*@
3181d27aa22SBarry Smith   TaoKSPSetUseEW - Sets `SNES` to use Eisenstat-Walker method {cite}`ew96`for computing relative tolerance for linear solvers.
3190f0abf79SStefano Zampini 
320c3339decSBarry Smith   Logically Collective
3210f0abf79SStefano Zampini 
3220f0abf79SStefano Zampini   Input Parameters:
3230f0abf79SStefano Zampini + tao  - Tao context
32465ba42b6SBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE`
3250f0abf79SStefano Zampini 
32667be906fSBarry Smith   Level: advanced
32767be906fSBarry Smith 
32847450a7bSBarry Smith   Note:
32965ba42b6SBarry Smith   See `SNESKSPSetUseEW()` for customization details.
3300f0abf79SStefano Zampini 
3311cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `SNESKSPSetUseEW()`
3320f0abf79SStefano Zampini @*/
333d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag)
334d71ae5a4SJacob Faibussowitsch {
3350f0abf79SStefano Zampini   PetscFunctionBegin;
3360f0abf79SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3370f0abf79SStefano Zampini   PetscValidLogicalCollectiveBool(tao, flag, 2);
3380f0abf79SStefano Zampini   tao->ksp_ewconv = flag;
3393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3400f0abf79SStefano Zampini }
3410f0abf79SStefano Zampini 
3420f0abf79SStefano Zampini /*@
34347450a7bSBarry Smith   TaoSetFromOptions - Sets various Tao parameters from the options database
344a7e14dcfSSatish Balay 
345c3339decSBarry Smith   Collective
346a7e14dcfSSatish Balay 
34701d2d390SJose E. Roman   Input Parameter:
34847450a7bSBarry Smith . tao - the `Tao` solver context
349a7e14dcfSSatish Balay 
35047450a7bSBarry Smith   Options Database Keys:
35165ba42b6SBarry Smith + -tao_type <type>             - The algorithm that Tao uses (lmvm, nls, etc.)
352a7e14dcfSSatish Balay . -tao_gatol <gatol>           - absolute error tolerance for ||gradient||
353a7e14dcfSSatish Balay . -tao_grtol <grtol>           - relative error tolerance for ||gradient||
354a7e14dcfSSatish Balay . -tao_gttol <gttol>           - reduction of ||gradient|| relative to initial gradient
355a7e14dcfSSatish Balay . -tao_max_it <max>            - sets maximum number of iterations
356a7e14dcfSSatish Balay . -tao_max_funcs <max>         - sets maximum number of function evaluations
357a7e14dcfSSatish Balay . -tao_fmin <fmin>             - stop if function value reaches fmin
358a7e14dcfSSatish Balay . -tao_steptol <tol>           - stop if trust region radius less than <tol>
359a7e14dcfSSatish Balay . -tao_trust0 <t>              - initial trust region radius
36010978b7dSBarry Smith . -tao_view_solution           - view the solution at the end of the optimization process
36147450a7bSBarry Smith . -tao_monitor                 - prints function value and residual norm at each iteration
36210978b7dSBarry Smith . -tao_monitor_short           - same as `-tao_monitor`, but truncates very small values
36310978b7dSBarry Smith . -tao_monitor_constraint_norm - prints objective value, gradient, and constraint norm at each iteration
36410978b7dSBarry Smith . -tao_monitor_globalization   - prints information about the globalization at each iteration
36510978b7dSBarry Smith . -tao_monitor_solution        - prints solution vector at each iteration
36610978b7dSBarry Smith . -tao_monitor_ls_residual     - prints least-squares residual vector at each iteration
36710978b7dSBarry Smith . -tao_monitor_step            - prints step vector at each iteration
36810978b7dSBarry Smith . -tao_monitor_gradient        - prints gradient vector at each iteration
36910978b7dSBarry Smith . -tao_monitor_solution_draw   - graphically view solution vector at each iteration
37010978b7dSBarry Smith . -tao_monitor_step_draw       - graphically view step vector at each iteration
37110978b7dSBarry Smith . -tao_monitor_gradient_draw   - graphically view gradient at each iteration
37210978b7dSBarry Smith . -tao_monitor_cancel          - cancels all monitors (except those set with command line)
373a7e14dcfSSatish Balay . -tao_fd_gradient             - use gradient computed with finite differences
374f4c1ad5cSStefano Zampini . -tao_fd_hessian              - use hessian computed with finite differences
37510978b7dSBarry Smith . -tao_mf_hessian              - use matrix-free Hessian computed with finite differences
376441846f8SBarry Smith . -tao_view                    - prints information about the Tao after solving
37765ba42b6SBarry Smith - -tao_converged_reason        - prints the reason Tao stopped iterating
378a7e14dcfSSatish Balay 
37967be906fSBarry Smith   Level: beginner
38067be906fSBarry Smith 
38167be906fSBarry Smith   Note:
38247450a7bSBarry Smith   To see all options, run your program with the `-help` option or consult the
38365ba42b6SBarry Smith   user's manual. Should be called after `TaoCreate()` but before `TaoSolve()`
384a7e14dcfSSatish Balay 
3851cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
386a7e14dcfSSatish Balay @*/
387d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions(Tao tao)
388d71ae5a4SJacob Faibussowitsch {
389b625d6c7SJed Brown   TaoType     default_type = TAOLMVM;
390a7e14dcfSSatish Balay   char        type[256], monfilename[PETSC_MAX_PATH_LEN];
391a7e14dcfSSatish Balay   PetscViewer monviewer;
392e876fe75SStephan Köhler   PetscBool   flg, found;
393a7e14dcfSSatish Balay   MPI_Comm    comm;
394606f75f6SBarry Smith   PetscReal   catol, crtol, gatol, grtol, gttol;
395a7e14dcfSSatish Balay 
396a7e14dcfSSatish Balay   PetscFunctionBegin;
397441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
3989566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)tao, &comm));
399125ddc8aSJason Sarich 
40060b70c5cSStefano Zampini   if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name;
40160b70c5cSStefano Zampini 
402d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)tao);
403a7e14dcfSSatish Balay   /* Check for type from options */
4049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg));
405a7e14dcfSSatish Balay   if (flg) {
4069566063dSJacob Faibussowitsch     PetscCall(TaoSetType(tao, type));
407a7e14dcfSSatish Balay   } else if (!((PetscObject)tao)->type_name) {
4089566063dSJacob Faibussowitsch     PetscCall(TaoSetType(tao, default_type));
409a7e14dcfSSatish Balay   }
410a7e14dcfSSatish Balay 
41160b70c5cSStefano Zampini   /* Tao solvers do not set the prefix, set it here if not yet done
41260b70c5cSStefano Zampini      We do it after SetType since solver may have been changed */
41360b70c5cSStefano Zampini   if (tao->linesearch) {
41460b70c5cSStefano Zampini     const char *prefix;
41560b70c5cSStefano Zampini     PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix));
416f4f49eeaSPierre Jolivet     if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)tao)->prefix));
41760b70c5cSStefano Zampini   }
41860b70c5cSStefano Zampini 
419606f75f6SBarry Smith   catol = tao->catol;
420606f75f6SBarry Smith   crtol = tao->crtol;
421606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &catol, NULL));
422606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &crtol, NULL));
423606f75f6SBarry Smith   PetscCall(TaoSetConstraintTolerances(tao, catol, crtol));
424606f75f6SBarry Smith 
425606f75f6SBarry Smith   gatol = tao->gatol;
426606f75f6SBarry Smith   grtol = tao->grtol;
427606f75f6SBarry Smith   gttol = tao->gttol;
428606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &gatol, NULL));
429606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &grtol, NULL));
430606f75f6SBarry Smith   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, &gttol, NULL));
431606f75f6SBarry Smith   PetscCall(TaoSetTolerances(tao, gatol, grtol, gttol));
432606f75f6SBarry Smith 
4339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg));
434606f75f6SBarry Smith   if (flg) PetscCall(TaoSetMaximumIterations(tao, tao->max_it));
435606f75f6SBarry Smith 
4369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg));
437606f75f6SBarry Smith   if (flg) PetscCall(TaoSetMaximumFunctionEvaluations(tao, tao->max_funcs));
438606f75f6SBarry Smith 
439606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, NULL));
440606f75f6SBarry Smith   PetscCall(PetscOptionsBoundedReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, NULL, 0));
441606f75f6SBarry Smith   PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetInitialTrustRegionRadius", tao->trust0, &tao->trust0, &flg));
442606f75f6SBarry Smith   if (flg) PetscCall(TaoSetInitialTrustRegionRadius(tao, tao->trust0));
44310978b7dSBarry Smith 
44410978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_solution_monitor", "-tao_monitor_solution", "3.21", NULL));
44510978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_gradient_monitor", "-tao_monitor_gradient", "3.21", NULL));
44610978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_stepdirection_monitor", "-tao_monitor_step", "3.21", NULL));
44710978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_residual_monitor", "-tao_monitor_residual", "3.21", NULL));
44810978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_smonitor", "-tao_monitor_short", "3.21", NULL));
44910978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_cmonitor", "-tao_monitor_constraint_norm", "3.21", NULL));
45010978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_gmonitor", "-tao_monitor_globalization", "3.21", NULL));
45110978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_solution", "-tao_monitor_solution_draw", "3.21", NULL));
45210978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_gradient", "-tao_monitor_gradient_draw", "3.21", NULL));
45310978b7dSBarry Smith   PetscCall(PetscOptionsDeprecated("-tao_draw_step", "-tao_monitor_step_draw", "3.21", NULL));
45410978b7dSBarry Smith 
45510978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_solution", "View solution vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
456a7e14dcfSSatish Balay   if (flg) {
4579566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
45849abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorSolution, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
459a7e14dcfSSatish Balay   }
460a7e14dcfSSatish Balay 
46165ba42b6SBarry Smith   PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL));
46210978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_gradient", "View gradient vector for each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
463a7e14dcfSSatish Balay   if (flg) {
4649566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
46549abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGradient, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
466a7e14dcfSSatish Balay   }
467a7e14dcfSSatish Balay 
46810978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_step", "View step vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
469a7e14dcfSSatish Balay   if (flg) {
4709566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
47149abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorStep, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
472a7e14dcfSSatish Balay   }
473a7e14dcfSSatish Balay 
47410978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_residual", "View least-squares residual vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
475a7e14dcfSSatish Balay   if (flg) {
4769566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
47749abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorResidual, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
478a7e14dcfSSatish Balay   }
479a7e14dcfSSatish Balay 
48010978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor", "Use the default convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
481a7e14dcfSSatish Balay   if (flg) {
4829566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
48349abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorDefault, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
484a7e14dcfSSatish Balay   }
485a7e14dcfSSatish Balay 
48610978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_globalization", "Use the convergence monitor with extra globalization info", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
4878d5ead36SAlp Dener   if (flg) {
4889566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
48949abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGlobalization, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
4908d5ead36SAlp Dener   }
4918d5ead36SAlp Dener 
49210978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_short", "Use the short convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
493a7e14dcfSSatish Balay   if (flg) {
4949566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
49549abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorDefaultShort, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
496a7e14dcfSSatish Balay   }
497a7e14dcfSSatish Balay 
49810978b7dSBarry Smith   PetscCall(PetscOptionsString("-tao_monitor_constraint_norm", "Use the default convergence monitor with constraint norm", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg));
499a7e14dcfSSatish Balay   if (flg) {
5009566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer));
50149abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorConstraintNorm, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy));
502a7e14dcfSSatish Balay   }
503a7e14dcfSSatish Balay 
5048afaa268SBarry Smith   flg = PETSC_FALSE;
505b2dc45ddSPierre Jolivet   PetscCall(PetscOptionsDeprecated("-tao_cancelmonitors", "-tao_monitor_cancel", "3.21", NULL));
506b2dc45ddSPierre Jolivet   PetscCall(PetscOptionsBool("-tao_monitor_cancel", "cancel all monitors and call any registered destroy routines", "TaoMonitorCancel", flg, &flg, NULL));
507b2dc45ddSPierre Jolivet   if (flg) PetscCall(TaoMonitorCancel(tao));
508a7e14dcfSSatish Balay 
5098afaa268SBarry Smith   flg = PETSC_FALSE;
51010978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_solution_draw", "Plot solution vector at each iteration", "TaoMonitorSet", flg, &flg, NULL));
511a7e14dcfSSatish Balay   if (flg) {
512e882e171SHong Zhang     TaoMonitorDrawCtx drawctx;
513e882e171SHong Zhang     PetscInt          howoften = 1;
5149566063dSJacob Faibussowitsch     PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
51549abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorSolutionDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy));
516a7e14dcfSSatish Balay   }
517a7e14dcfSSatish Balay 
5188afaa268SBarry Smith   flg = PETSC_FALSE;
51910978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_step_draw", "Plots step at each iteration", "TaoMonitorSet", flg, &flg, NULL));
52010978b7dSBarry Smith   if (flg) PetscCall(TaoMonitorSet(tao, TaoMonitorStepDraw, NULL, NULL));
521a7e14dcfSSatish Balay 
5228afaa268SBarry Smith   flg = PETSC_FALSE;
52310978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_monitor_gradient_draw", "plots gradient at each iteration", "TaoMonitorSet", flg, &flg, NULL));
524a7e14dcfSSatish Balay   if (flg) {
525e882e171SHong Zhang     TaoMonitorDrawCtx drawctx;
526e882e171SHong Zhang     PetscInt          howoften = 1;
5279566063dSJacob Faibussowitsch     PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
52849abdd8aSBarry Smith     PetscCall(TaoMonitorSet(tao, TaoMonitorGradientDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy));
529a7e14dcfSSatish Balay   }
5308afaa268SBarry Smith   flg = PETSC_FALSE;
5319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL));
5321baa6e33SBarry Smith   if (flg) PetscCall(TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, NULL));
533f4c1ad5cSStefano Zampini   flg = PETSC_FALSE;
53410978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute Hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL));
535f4c1ad5cSStefano Zampini   if (flg) {
536f4c1ad5cSStefano Zampini     Mat H;
537f4c1ad5cSStefano Zampini 
5389566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H));
5399566063dSJacob Faibussowitsch     PetscCall(MatSetType(H, MATAIJ));
5409566063dSJacob Faibussowitsch     PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL));
5419566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&H));
542f4c1ad5cSStefano Zampini   }
543f4c1ad5cSStefano Zampini   flg = PETSC_FALSE;
54410978b7dSBarry Smith   PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free Hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL));
545f4c1ad5cSStefano Zampini   if (flg) {
546f4c1ad5cSStefano Zampini     Mat H;
547f4c1ad5cSStefano Zampini 
5489566063dSJacob Faibussowitsch     PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H));
5499566063dSJacob Faibussowitsch     PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL));
5509566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&H));
551f4c1ad5cSStefano Zampini   }
552e876fe75SStephan Köhler   PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, &found));
553e876fe75SStephan Köhler   if (found) PetscCall(TaoSetRecycleHistory(tao, flg));
5549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubSetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL));
555a7e14dcfSSatish Balay 
5560f0abf79SStefano Zampini   if (tao->ksp) {
5570f0abf79SStefano Zampini     PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL));
5580f0abf79SStefano Zampini     PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv));
5590f0abf79SStefano Zampini   }
5600f0abf79SStefano Zampini 
561dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject);
56260b70c5cSStefano Zampini 
56360b70c5cSStefano Zampini   /* process any options handlers added with PetscObjectAddOptionsHandler() */
56460b70c5cSStefano Zampini   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject));
565d0609cedSBarry Smith   PetscOptionsEnd();
56660b70c5cSStefano Zampini 
56760b70c5cSStefano Zampini   if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
5683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
569a7e14dcfSSatish Balay }
570a7e14dcfSSatish Balay 
571ffeef943SBarry Smith /*@
57247450a7bSBarry Smith   TaoViewFromOptions - View a `Tao` object based on values in the options database
573fe2efc57SMark 
574c3339decSBarry Smith   Collective
575fe2efc57SMark 
576fe2efc57SMark   Input Parameters:
57747450a7bSBarry Smith + A    - the  `Tao` context
57847450a7bSBarry Smith . obj  - Optional object that provides the prefix for the options database
579736c3998SJose E. Roman - name - command line option
580fe2efc57SMark 
581fe2efc57SMark   Level: intermediate
58267be906fSBarry Smith 
5831cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()`
584fe2efc57SMark @*/
585d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[])
586d71ae5a4SJacob Faibussowitsch {
587fe2efc57SMark   PetscFunctionBegin;
588fe2efc57SMark   PetscValidHeaderSpecific(A, TAO_CLASSID, 1);
5899566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
5903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
591fe2efc57SMark }
592fe2efc57SMark 
593ffeef943SBarry Smith /*@
59447450a7bSBarry Smith   TaoView - Prints information about the `Tao` object
595a7e14dcfSSatish Balay 
596c3339decSBarry Smith   Collective
597a7e14dcfSSatish Balay 
598a7e14dcfSSatish Balay   Input Parameters:
59947450a7bSBarry Smith + tao    - the `Tao` context
600a7e14dcfSSatish Balay - viewer - visualization context
601a7e14dcfSSatish Balay 
602a7e14dcfSSatish Balay   Options Database Key:
60365ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()`
604a7e14dcfSSatish Balay 
60567be906fSBarry Smith   Level: beginner
60667be906fSBarry Smith 
607a7e14dcfSSatish Balay   Notes:
608a7e14dcfSSatish Balay   The available visualization contexts include
60965ba42b6SBarry Smith +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
61065ba42b6SBarry Smith -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
611a7e14dcfSSatish Balay   output where only the first processor opens
612a7e14dcfSSatish Balay   the file.  All other processors send their
613a7e14dcfSSatish Balay   data to the first processor to print.
614a7e14dcfSSatish Balay 
6151cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `PetscViewerASCIIOpen()`
616a7e14dcfSSatish Balay @*/
617d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView(Tao tao, PetscViewer viewer)
618d71ae5a4SJacob Faibussowitsch {
619a7e14dcfSSatish Balay   PetscBool isascii, isstring;
620b625d6c7SJed Brown   TaoType   type;
62147a47007SBarry Smith 
622a7e14dcfSSatish Balay   PetscFunctionBegin;
623441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
62448a46eb9SPierre Jolivet   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer));
625a7e14dcfSSatish Balay   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
626a7e14dcfSSatish Balay   PetscCheckSameComm(tao, 1, viewer, 2);
627a7e14dcfSSatish Balay 
6289566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
6299566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
630a7e14dcfSSatish Balay   if (isascii) {
6319566063dSJacob Faibussowitsch     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer));
632a7e14dcfSSatish Balay 
6339566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPushTab(viewer));
634606f75f6SBarry Smith     PetscTryTypeMethod(tao, view, viewer);
635606f75f6SBarry Smith     if (tao->linesearch) PetscCall(TaoLineSearchView(tao->linesearch, viewer));
636a7e14dcfSSatish Balay     if (tao->ksp) {
6379566063dSJacob Faibussowitsch       PetscCall(KSPView(tao->ksp, viewer));
63863a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its));
639a7e14dcfSSatish Balay     }
64019b5c101SAlp Dener 
64148a46eb9SPierre Jolivet     if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubSetTypes[tao->subset_type]));
642a7e14dcfSSatish Balay 
6439566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol));
644606f75f6SBarry Smith     PetscCall(PetscViewerASCIIPrintf(viewer, " grtol=%g,", (double)tao->grtol));
6459566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol));
6469566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol));
6479566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual));
648a7e14dcfSSatish Balay 
6496246e8cdSAlp Dener     if (tao->constrained) {
6509566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:"));
6519566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol));
6529566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol));
6539566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm));
654a7e14dcfSSatish Balay     }
655a7e14dcfSSatish Balay 
656a7e14dcfSSatish Balay     if (tao->trust < tao->steptol) {
6579566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol));
6589566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust));
659a7e14dcfSSatish Balay     }
660a7e14dcfSSatish Balay 
66148a46eb9SPierre Jolivet     if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin));
6629566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Objective value=%g\n", (double)tao->fc));
663a7e14dcfSSatish Balay 
66463a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "total number of iterations=%" PetscInt_FMT ",          ", tao->niter));
66563a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "              (max: %" PetscInt_FMT ")\n", tao->max_it));
666a7e14dcfSSatish Balay 
667a7e14dcfSSatish Balay     if (tao->nfuncs > 0) {
66863a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function evaluations=%" PetscInt_FMT ",", tao->nfuncs));
669606f75f6SBarry Smith       if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, "                (max: unlimited)\n"));
670606f75f6SBarry Smith       else PetscCall(PetscViewerASCIIPrintf(viewer, "                (max: %" PetscInt_FMT ")\n", tao->max_funcs));
671a7e14dcfSSatish Balay     }
672a7e14dcfSSatish Balay     if (tao->ngrads > 0) {
67363a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "total number of gradient evaluations=%" PetscInt_FMT ",", tao->ngrads));
674606f75f6SBarry Smith       if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, "                (max: unlimited)\n"));
675606f75f6SBarry Smith       else PetscCall(PetscViewerASCIIPrintf(viewer, "                (max: %" PetscInt_FMT ")\n", tao->max_funcs));
676a7e14dcfSSatish Balay     }
677a7e14dcfSSatish Balay     if (tao->nfuncgrads > 0) {
67863a3b9bcSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function/gradient evaluations=%" PetscInt_FMT ",", tao->nfuncgrads));
679606f75f6SBarry Smith       if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, "    (max: unlimited)\n"));
680606f75f6SBarry Smith       else PetscCall(PetscViewerASCIIPrintf(viewer, "    (max: %" PetscInt_FMT ")\n", tao->max_funcs));
681a7e14dcfSSatish Balay     }
68248a46eb9SPierre Jolivet     if (tao->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->nhess));
68348a46eb9SPierre Jolivet     if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints));
68448a46eb9SPierre Jolivet     if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac));
685a7e14dcfSSatish Balay 
686a7e14dcfSSatish Balay     if (tao->reason > 0) {
6879566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: "));
688a7e14dcfSSatish Balay       switch (tao->reason) {
689d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GATOL:
690d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n"));
691d71ae5a4SJacob Faibussowitsch         break;
692d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GRTOL:
693d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n"));
694d71ae5a4SJacob Faibussowitsch         break;
695d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_GTTOL:
696d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n"));
697d71ae5a4SJacob Faibussowitsch         break;
698d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_STEPTOL:
699d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n"));
700d71ae5a4SJacob Faibussowitsch         break;
701d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_MINF:
702d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Minf --  f < fmin\n"));
703d71ae5a4SJacob Faibussowitsch         break;
704d71ae5a4SJacob Faibussowitsch       case TAO_CONVERGED_USER:
705d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
706d71ae5a4SJacob Faibussowitsch         break;
707d71ae5a4SJacob Faibussowitsch       default:
708606f75f6SBarry Smith         PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason));
709d71ae5a4SJacob Faibussowitsch         break;
710a7e14dcfSSatish Balay       }
711606f75f6SBarry Smith     } else if (tao->reason == TAO_CONTINUE_ITERATING) {
712606f75f6SBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver never run\n"));
713a7e14dcfSSatish Balay     } else {
714606f75f6SBarry Smith       PetscCall(PetscViewerASCIIPrintf(viewer, "Solver failed: "));
715a7e14dcfSSatish Balay       switch (tao->reason) {
716d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_MAXITS:
717d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n"));
718d71ae5a4SJacob Faibussowitsch         break;
719d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_NAN:
720d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " NAN or Inf encountered\n"));
721d71ae5a4SJacob Faibussowitsch         break;
722d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_MAXFCN:
723d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n"));
724d71ae5a4SJacob Faibussowitsch         break;
725d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_LS_FAILURE:
726d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n"));
727d71ae5a4SJacob Faibussowitsch         break;
728d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_TR_REDUCTION:
729d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n"));
730d71ae5a4SJacob Faibussowitsch         break;
731d71ae5a4SJacob Faibussowitsch       case TAO_DIVERGED_USER:
732d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
733d71ae5a4SJacob Faibussowitsch         break;
734d71ae5a4SJacob Faibussowitsch       default:
735606f75f6SBarry Smith         PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason));
736d71ae5a4SJacob Faibussowitsch         break;
737a7e14dcfSSatish Balay       }
738a7e14dcfSSatish Balay     }
7399566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPopTab(viewer));
740a7e14dcfSSatish Balay   } else if (isstring) {
7419566063dSJacob Faibussowitsch     PetscCall(TaoGetType(tao, &type));
7429566063dSJacob Faibussowitsch     PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type));
743a7e14dcfSSatish Balay   }
7443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
745a7e14dcfSSatish Balay }
746a7e14dcfSSatish Balay 
747a7e14dcfSSatish Balay /*@
748414d97d3SAlp Dener   TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using
74965ba42b6SBarry Smith   iterate information from the previous `TaoSolve()`. This feature is disabled by
750414d97d3SAlp Dener   default.
751414d97d3SAlp Dener 
75267be906fSBarry Smith   Logically Collective
75367be906fSBarry Smith 
75467be906fSBarry Smith   Input Parameters:
75547450a7bSBarry Smith + tao     - the `Tao` context
75667be906fSBarry Smith - recycle - boolean flag
75767be906fSBarry Smith 
75847450a7bSBarry Smith   Options Database Key:
75967be906fSBarry Smith . -tao_recycle_history <true,false> - reuse the history
76067be906fSBarry Smith 
76167be906fSBarry Smith   Level: intermediate
76267be906fSBarry Smith 
76367be906fSBarry Smith   Notes:
76465ba42b6SBarry Smith   For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction
76565ba42b6SBarry Smith   from the previous `TaoSolve()` call when computing the first search direction in a
766414d97d3SAlp Dener   new solution. By default, CG methods set the first search direction to the
767414d97d3SAlp Dener   negative gradient.
768414d97d3SAlp Dener 
76965ba42b6SBarry Smith   For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses
77065ba42b6SBarry Smith   the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()`
771414d97d3SAlp Dener   call. By default, QN family of methods reset the initial Hessian approximation to
772414d97d3SAlp Dener   the identity matrix.
773414d97d3SAlp Dener 
774414d97d3SAlp Dener   For any other algorithm, this setting has no effect.
775414d97d3SAlp Dener 
7761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
777414d97d3SAlp Dener @*/
778d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle)
779d71ae5a4SJacob Faibussowitsch {
780414d97d3SAlp Dener   PetscFunctionBegin;
781414d97d3SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
78294511df7SStefano Zampini   PetscValidLogicalCollectiveBool(tao, recycle, 2);
783414d97d3SAlp Dener   tao->recycle = recycle;
7843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
785414d97d3SAlp Dener }
786414d97d3SAlp Dener 
787414d97d3SAlp Dener /*@
788414d97d3SAlp Dener   TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information
78965ba42b6SBarry Smith   from the previous `TaoSolve()`. This feature is disabled by default.
790414d97d3SAlp Dener 
79167be906fSBarry Smith   Logically Collective
792414d97d3SAlp Dener 
79347450a7bSBarry Smith   Input Parameter:
79447450a7bSBarry Smith . tao - the `Tao` context
795414d97d3SAlp Dener 
79647450a7bSBarry Smith   Output Parameter:
797147403d9SBarry Smith . recycle - boolean flag
798414d97d3SAlp Dener 
799414d97d3SAlp Dener   Level: intermediate
800414d97d3SAlp Dener 
8011cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
802414d97d3SAlp Dener @*/
803d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle)
804d71ae5a4SJacob Faibussowitsch {
805414d97d3SAlp Dener   PetscFunctionBegin;
806414d97d3SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
8074f572ea9SToby Isaac   PetscAssertPointer(recycle, 2);
808414d97d3SAlp Dener   *recycle = tao->recycle;
8093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
810414d97d3SAlp Dener }
811414d97d3SAlp Dener 
812414d97d3SAlp Dener /*@
81347450a7bSBarry Smith   TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests
814a7e14dcfSSatish Balay 
81567be906fSBarry Smith   Logically Collective
816a7e14dcfSSatish Balay 
817a7e14dcfSSatish Balay   Input Parameters:
81847450a7bSBarry Smith + tao   - the `Tao` context
819a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this
820a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
821a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor
822a7e14dcfSSatish Balay 
823a7e14dcfSSatish Balay   Options Database Keys:
824e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol
825a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol
826a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol
827a7e14dcfSSatish Balay 
8283b242c63SJacob Faibussowitsch   Stopping Criteria\:
82967be906fSBarry Smith .vb
83067be906fSBarry Smith   ||g(X)||                            <= gatol
83167be906fSBarry Smith   ||g(X)|| / |f(X)|                   <= grtol
83267be906fSBarry Smith   ||g(X)|| / ||g(X0)||                <= gttol
83367be906fSBarry Smith .ve
834a7e14dcfSSatish Balay 
835a7e14dcfSSatish Balay   Level: beginner
836a7e14dcfSSatish Balay 
837606f75f6SBarry Smith   Notes:
838606f75f6SBarry Smith   Use `PETSC_CURRENT` to leave one or more tolerances unchanged.
839606f75f6SBarry Smith 
840606f75f6SBarry Smith   Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao`object's type was set
841606f75f6SBarry Smith 
842606f75f6SBarry Smith   Fortran Note:
843606f75f6SBarry Smith   Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL`
844a7e14dcfSSatish Balay 
8451cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`
846a7e14dcfSSatish Balay @*/
847d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol)
848d71ae5a4SJacob Faibussowitsch {
849a7e14dcfSSatish Balay   PetscFunctionBegin;
850441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
85194511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, gatol, 2);
85294511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, grtol, 3);
85394511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, gttol, 4);
854a7e14dcfSSatish Balay 
855606f75f6SBarry Smith   if (gatol == (PetscReal)PETSC_DETERMINE) {
856606f75f6SBarry Smith     tao->gatol = tao->default_gatol;
857606f75f6SBarry Smith   } else if (gatol != (PetscReal)PETSC_CURRENT) {
858606f75f6SBarry Smith     PetscCheck(gatol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gatol not allowed");
859606f75f6SBarry Smith     tao->gatol = gatol;
860a7e14dcfSSatish Balay   }
861a7e14dcfSSatish Balay 
862606f75f6SBarry Smith   if (grtol == (PetscReal)PETSC_DETERMINE) {
863606f75f6SBarry Smith     tao->grtol = tao->default_grtol;
864606f75f6SBarry Smith   } else if (grtol != (PetscReal)PETSC_CURRENT) {
865606f75f6SBarry Smith     PetscCheck(grtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative grtol not allowed");
866606f75f6SBarry Smith     tao->grtol = grtol;
867a7e14dcfSSatish Balay   }
868a7e14dcfSSatish Balay 
869606f75f6SBarry Smith   if (gttol == (PetscReal)PETSC_DETERMINE) {
870606f75f6SBarry Smith     tao->gttol = tao->default_gttol;
871606f75f6SBarry Smith   } else if (gttol != (PetscReal)PETSC_CURRENT) {
872606f75f6SBarry Smith     PetscCheck(gttol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gttol not allowed");
873606f75f6SBarry Smith     tao->gttol = gttol;
874a7e14dcfSSatish Balay   }
8753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
876a7e14dcfSSatish Balay }
877a7e14dcfSSatish Balay 
878a7e14dcfSSatish Balay /*@
87947450a7bSBarry Smith   TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests
880a7e14dcfSSatish Balay 
88167be906fSBarry Smith   Logically Collective
882a7e14dcfSSatish Balay 
883a7e14dcfSSatish Balay   Input Parameters:
88447450a7bSBarry Smith + tao   - the `Tao` context
885606f75f6SBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria
886606f75f6SBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria
887a7e14dcfSSatish Balay 
888a7e14dcfSSatish Balay   Options Database Keys:
889a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol
890a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol
891a7e14dcfSSatish Balay 
892a7e14dcfSSatish Balay   Level: intermediate
893a7e14dcfSSatish Balay 
89467be906fSBarry Smith   Notes:
895606f75f6SBarry Smith   Use `PETSC_CURRENT` to leave one or tolerance unchanged.
896606f75f6SBarry Smith 
897606f75f6SBarry Smith   Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao` object's type was set
898606f75f6SBarry Smith 
899606f75f6SBarry Smith   Fortran Note:
900606f75f6SBarry Smith   Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL`
901a7e14dcfSSatish Balay 
9021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()`
903a7e14dcfSSatish Balay @*/
904d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol)
905d71ae5a4SJacob Faibussowitsch {
906a7e14dcfSSatish Balay   PetscFunctionBegin;
907441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
90894511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, catol, 2);
90994511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, crtol, 3);
910a7e14dcfSSatish Balay 
911606f75f6SBarry Smith   if (catol == (PetscReal)PETSC_DETERMINE) {
912606f75f6SBarry Smith     tao->catol = tao->default_catol;
913606f75f6SBarry Smith   } else if (catol != (PetscReal)PETSC_CURRENT) {
914606f75f6SBarry Smith     PetscCheck(catol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative catol not allowed");
915606f75f6SBarry Smith     tao->catol = catol;
916a7e14dcfSSatish Balay   }
917a7e14dcfSSatish Balay 
918606f75f6SBarry Smith   if (crtol == (PetscReal)PETSC_DETERMINE) {
919606f75f6SBarry Smith     tao->crtol = tao->default_crtol;
920606f75f6SBarry Smith   } else if (crtol != (PetscReal)PETSC_CURRENT) {
921606f75f6SBarry Smith     PetscCheck(crtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative crtol not allowed");
922606f75f6SBarry Smith     tao->crtol = crtol;
923a7e14dcfSSatish Balay   }
9243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
925a7e14dcfSSatish Balay }
926a7e14dcfSSatish Balay 
92758417fe7SBarry Smith /*@
92847450a7bSBarry Smith   TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests
92958417fe7SBarry Smith 
93067be906fSBarry Smith   Not Collective
93158417fe7SBarry Smith 
93258417fe7SBarry Smith   Input Parameter:
93347450a7bSBarry Smith . tao - the `Tao` context
93458417fe7SBarry Smith 
935d8d19677SJose E. Roman   Output Parameters:
936606f75f6SBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria
937606f75f6SBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria
93858417fe7SBarry Smith 
93958417fe7SBarry Smith   Level: intermediate
94058417fe7SBarry Smith 
9411cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReasons`,`TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()`
94258417fe7SBarry Smith @*/
943d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol)
944d71ae5a4SJacob Faibussowitsch {
94558417fe7SBarry Smith   PetscFunctionBegin;
94658417fe7SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
94758417fe7SBarry Smith   if (catol) *catol = tao->catol;
94858417fe7SBarry Smith   if (crtol) *crtol = tao->crtol;
9493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
95058417fe7SBarry Smith }
95158417fe7SBarry Smith 
952a7e14dcfSSatish Balay /*@
953a7e14dcfSSatish Balay   TaoSetFunctionLowerBound - Sets a bound on the solution objective value.
954a7e14dcfSSatish Balay   When an approximate solution with an objective value below this number
955a7e14dcfSSatish Balay   has been found, the solver will terminate.
956a7e14dcfSSatish Balay 
957c3339decSBarry Smith   Logically Collective
958a7e14dcfSSatish Balay 
959a7e14dcfSSatish Balay   Input Parameters:
960441846f8SBarry Smith + tao  - the Tao solver context
961a7e14dcfSSatish Balay - fmin - the tolerance
962a7e14dcfSSatish Balay 
96347450a7bSBarry Smith   Options Database Key:
964a7e14dcfSSatish Balay . -tao_fmin <fmin> - sets the minimum function value
965a7e14dcfSSatish Balay 
966a7e14dcfSSatish Balay   Level: intermediate
967a7e14dcfSSatish Balay 
9681cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()`
969a7e14dcfSSatish Balay @*/
970d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin)
971d71ae5a4SJacob Faibussowitsch {
972a7e14dcfSSatish Balay   PetscFunctionBegin;
973441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
97494511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, fmin, 2);
975a7e14dcfSSatish Balay   tao->fmin = fmin;
9763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
977a7e14dcfSSatish Balay }
978a7e14dcfSSatish Balay 
979a7e14dcfSSatish Balay /*@
9808e96d397SJason Sarich   TaoGetFunctionLowerBound - Gets the bound on the solution objective value.
981a7e14dcfSSatish Balay   When an approximate solution with an objective value below this number
982a7e14dcfSSatish Balay   has been found, the solver will terminate.
983a7e14dcfSSatish Balay 
98467be906fSBarry Smith   Not Collective
985a7e14dcfSSatish Balay 
98647450a7bSBarry Smith   Input Parameter:
98747450a7bSBarry Smith . tao - the `Tao` solver context
988a7e14dcfSSatish Balay 
98947450a7bSBarry Smith   Output Parameter:
990a7e14dcfSSatish Balay . fmin - the minimum function value
991a7e14dcfSSatish Balay 
992a7e14dcfSSatish Balay   Level: intermediate
993a7e14dcfSSatish Balay 
9941cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()`
995a7e14dcfSSatish Balay @*/
996d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin)
997d71ae5a4SJacob Faibussowitsch {
998a7e14dcfSSatish Balay   PetscFunctionBegin;
999441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
10004f572ea9SToby Isaac   PetscAssertPointer(fmin, 2);
1001a7e14dcfSSatish Balay   *fmin = tao->fmin;
10023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1003a7e14dcfSSatish Balay }
1004a7e14dcfSSatish Balay 
1005a7e14dcfSSatish Balay /*@
100647450a7bSBarry Smith   TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`.
1007a7e14dcfSSatish Balay 
1008c3339decSBarry Smith   Logically Collective
1009a7e14dcfSSatish Balay 
1010a7e14dcfSSatish Balay   Input Parameters:
101147450a7bSBarry Smith + tao  - the `Tao` solver context
1012606f75f6SBarry Smith - nfcn - the maximum number of function evaluations (>=0), use `PETSC_UNLIMITED` to have no bound
1013a7e14dcfSSatish Balay 
101447450a7bSBarry Smith   Options Database Key:
1015a7e14dcfSSatish Balay . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations
1016a7e14dcfSSatish Balay 
1017a7e14dcfSSatish Balay   Level: intermediate
1018a7e14dcfSSatish Balay 
1019606f75f6SBarry Smith   Note:
1020606f75f6SBarry Smith   Use `PETSC_DETERMINE` to use the default maximum number of function evaluations that was set when the object type was set.
1021606f75f6SBarry Smith 
1022606f75f6SBarry Smith   Developer Note:
1023606f75f6SBarry Smith   Deprecated support for an unlimited number of function evaluations by passing a negative value.
1024606f75f6SBarry Smith 
10251cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()`
1026a7e14dcfSSatish Balay @*/
1027d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn)
1028d71ae5a4SJacob Faibussowitsch {
1029a7e14dcfSSatish Balay   PetscFunctionBegin;
1030441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
103194511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, nfcn, 2);
1032606f75f6SBarry Smith   if (nfcn == PETSC_DETERMINE) {
1033606f75f6SBarry Smith     tao->max_funcs = tao->default_max_funcs;
1034606f75f6SBarry Smith   } else if (nfcn == PETSC_UNLIMITED || nfcn < 0) {
1035606f75f6SBarry Smith     tao->max_funcs = PETSC_UNLIMITED;
10369371c9d4SSatish Balay   } else {
1037d7c1f440SPierre Jolivet     PetscCheck(nfcn >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of function evaluations  must be positive");
1038606f75f6SBarry Smith     tao->max_funcs = nfcn;
10399371c9d4SSatish Balay   }
10403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1041a7e14dcfSSatish Balay }
1042a7e14dcfSSatish Balay 
1043a7e14dcfSSatish Balay /*@
104447450a7bSBarry Smith   TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()`
1045a7e14dcfSSatish Balay 
1046c3339decSBarry Smith   Logically Collective
1047a7e14dcfSSatish Balay 
104847450a7bSBarry Smith   Input Parameter:
104947450a7bSBarry Smith . tao - the `Tao` solver context
1050a7e14dcfSSatish Balay 
105147450a7bSBarry Smith   Output Parameter:
1052a7e14dcfSSatish Balay . nfcn - the maximum number of function evaluations
1053a7e14dcfSSatish Balay 
1054a7e14dcfSSatish Balay   Level: intermediate
1055a7e14dcfSSatish Balay 
10561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1057a7e14dcfSSatish Balay @*/
1058d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn)
1059d71ae5a4SJacob Faibussowitsch {
1060a7e14dcfSSatish Balay   PetscFunctionBegin;
1061441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
10624f572ea9SToby Isaac   PetscAssertPointer(nfcn, 2);
1063a7e14dcfSSatish Balay   *nfcn = tao->max_funcs;
10643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1065a7e14dcfSSatish Balay }
1066a7e14dcfSSatish Balay 
1067770232b9SCe Qin /*@
106847450a7bSBarry Smith   TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object
1069770232b9SCe Qin 
1070770232b9SCe Qin   Not Collective
1071770232b9SCe Qin 
107247450a7bSBarry Smith   Input Parameter:
107347450a7bSBarry Smith . tao - the `Tao` solver context
1074770232b9SCe Qin 
107547450a7bSBarry Smith   Output Parameter:
107694511df7SStefano Zampini . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations)
1077770232b9SCe Qin 
1078770232b9SCe Qin   Level: intermediate
1079770232b9SCe Qin 
10801cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1081770232b9SCe Qin @*/
1082d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs)
1083d71ae5a4SJacob Faibussowitsch {
1084770232b9SCe Qin   PetscFunctionBegin;
1085770232b9SCe Qin   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
10864f572ea9SToby Isaac   PetscAssertPointer(nfuncs, 2);
1087770232b9SCe Qin   *nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads);
10883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1089770232b9SCe Qin }
1090770232b9SCe Qin 
1091a7e14dcfSSatish Balay /*@
109247450a7bSBarry Smith   TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()`
1093a7e14dcfSSatish Balay 
1094c3339decSBarry Smith   Logically Collective
1095a7e14dcfSSatish Balay 
1096a7e14dcfSSatish Balay   Input Parameters:
109747450a7bSBarry Smith + tao    - the `Tao` solver context
1098606f75f6SBarry Smith - maxits - the maximum number of iterates (>=0), use `PETSC_UNLIMITED` to have no bound
1099a7e14dcfSSatish Balay 
110047450a7bSBarry Smith   Options Database Key:
1101a7e14dcfSSatish Balay . -tao_max_it <its> - sets the maximum number of iterations
1102a7e14dcfSSatish Balay 
1103a7e14dcfSSatish Balay   Level: intermediate
1104a7e14dcfSSatish Balay 
1105606f75f6SBarry Smith   Note:
1106606f75f6SBarry Smith   Use `PETSC_DETERMINE` to use the default maximum number of iterations that was set when the object's type was set.
1107606f75f6SBarry Smith 
1108606f75f6SBarry Smith   Developer Note:
1109606f75f6SBarry Smith   DeprAlso accepts the deprecated negative values to indicate no limit
1110606f75f6SBarry Smith 
11111cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()`
1112a7e14dcfSSatish Balay @*/
1113d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits)
1114d71ae5a4SJacob Faibussowitsch {
1115a7e14dcfSSatish Balay   PetscFunctionBegin;
1116441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
111794511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, maxits, 2);
1118606f75f6SBarry Smith   if (maxits == PETSC_DETERMINE) {
1119606f75f6SBarry Smith     tao->max_it = tao->default_max_it;
1120606f75f6SBarry Smith   } else if (maxits == PETSC_UNLIMITED) {
11211690c2aeSBarry Smith     tao->max_it = PETSC_INT_MAX;
1122606f75f6SBarry Smith   } else {
1123d7c1f440SPierre Jolivet     PetscCheck(maxits > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of iterations must be positive");
1124606f75f6SBarry Smith     tao->max_it = maxits;
1125606f75f6SBarry Smith   }
11263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1127a7e14dcfSSatish Balay }
1128a7e14dcfSSatish Balay 
1129a7e14dcfSSatish Balay /*@
113065ba42b6SBarry Smith   TaoGetMaximumIterations - Gets a maximum number of iterates that will be used
1131a7e14dcfSSatish Balay 
1132a7e14dcfSSatish Balay   Not Collective
1133a7e14dcfSSatish Balay 
113447450a7bSBarry Smith   Input Parameter:
113547450a7bSBarry Smith . tao - the `Tao` solver context
1136a7e14dcfSSatish Balay 
113747450a7bSBarry Smith   Output Parameter:
1138a7e14dcfSSatish Balay . maxits - the maximum number of iterates
1139a7e14dcfSSatish Balay 
1140a7e14dcfSSatish Balay   Level: intermediate
1141a7e14dcfSSatish Balay 
11421cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()`
1143a7e14dcfSSatish Balay @*/
1144d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits)
1145d71ae5a4SJacob Faibussowitsch {
1146a7e14dcfSSatish Balay   PetscFunctionBegin;
1147441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
11484f572ea9SToby Isaac   PetscAssertPointer(maxits, 2);
1149a7e14dcfSSatish Balay   *maxits = tao->max_it;
11503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1151a7e14dcfSSatish Balay }
1152a7e14dcfSSatish Balay 
1153a7e14dcfSSatish Balay /*@
1154a7e14dcfSSatish Balay   TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
1155a7e14dcfSSatish Balay 
115667be906fSBarry Smith   Logically Collective
1157a7e14dcfSSatish Balay 
1158d8d19677SJose E. Roman   Input Parameters:
115947450a7bSBarry Smith + tao    - a `Tao` optimization solver
1160a7e14dcfSSatish Balay - radius - the trust region radius
1161a7e14dcfSSatish Balay 
1162a7e14dcfSSatish Balay   Options Database Key:
1163a7e14dcfSSatish Balay . -tao_trust0 <t0> - sets initial trust region radius
1164a7e14dcfSSatish Balay 
116547450a7bSBarry Smith   Level: intermediate
116647450a7bSBarry Smith 
1167606f75f6SBarry Smith   Note:
1168606f75f6SBarry Smith   Use `PETSC_DETERMINE` to use the default radius that was set when the object's type was set.
1169606f75f6SBarry Smith 
11701cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR`
1171a7e14dcfSSatish Balay @*/
1172d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
1173d71ae5a4SJacob Faibussowitsch {
1174a7e14dcfSSatish Balay   PetscFunctionBegin;
1175441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
117694511df7SStefano Zampini   PetscValidLogicalCollectiveReal(tao, radius, 2);
1177606f75f6SBarry Smith   if (radius == PETSC_DETERMINE) {
1178606f75f6SBarry Smith     tao->trust0 = tao->default_trust0;
1179606f75f6SBarry Smith   } else {
1180606f75f6SBarry Smith     PetscCheck(radius > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Radius must be positive");
1181606f75f6SBarry Smith     tao->trust0 = radius;
1182606f75f6SBarry Smith   }
11833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1184a7e14dcfSSatish Balay }
1185a7e14dcfSSatish Balay 
1186a7e14dcfSSatish Balay /*@
118765ba42b6SBarry Smith   TaoGetInitialTrustRegionRadius - Gets the initial trust region radius.
1188a7e14dcfSSatish Balay 
1189a7e14dcfSSatish Balay   Not Collective
1190a7e14dcfSSatish Balay 
1191a7e14dcfSSatish Balay   Input Parameter:
119247450a7bSBarry Smith . tao - a `Tao` optimization solver
1193a7e14dcfSSatish Balay 
1194a7e14dcfSSatish Balay   Output Parameter:
1195a7e14dcfSSatish Balay . radius - the trust region radius
1196a7e14dcfSSatish Balay 
1197a7e14dcfSSatish Balay   Level: intermediate
1198a7e14dcfSSatish Balay 
11991cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR`
1200a7e14dcfSSatish Balay @*/
1201d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1202d71ae5a4SJacob Faibussowitsch {
1203a7e14dcfSSatish Balay   PetscFunctionBegin;
1204441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12054f572ea9SToby Isaac   PetscAssertPointer(radius, 2);
1206a7e14dcfSSatish Balay   *radius = tao->trust0;
12073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1208a7e14dcfSSatish Balay }
1209a7e14dcfSSatish Balay 
1210a7e14dcfSSatish Balay /*@
1211a7e14dcfSSatish Balay   TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1212a7e14dcfSSatish Balay 
1213a7e14dcfSSatish Balay   Not Collective
1214a7e14dcfSSatish Balay 
1215a7e14dcfSSatish Balay   Input Parameter:
121647450a7bSBarry Smith . tao - a `Tao` optimization solver
1217a7e14dcfSSatish Balay 
1218a7e14dcfSSatish Balay   Output Parameter:
1219a7e14dcfSSatish Balay . radius - the trust region radius
1220a7e14dcfSSatish Balay 
1221a7e14dcfSSatish Balay   Level: intermediate
1222a7e14dcfSSatish Balay 
12231cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR`
1224a7e14dcfSSatish Balay @*/
1225d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1226d71ae5a4SJacob Faibussowitsch {
1227a7e14dcfSSatish Balay   PetscFunctionBegin;
1228441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12294f572ea9SToby Isaac   PetscAssertPointer(radius, 2);
1230a7e14dcfSSatish Balay   *radius = tao->trust;
12313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1232a7e14dcfSSatish Balay }
1233a7e14dcfSSatish Balay 
1234a7e14dcfSSatish Balay /*@
123547450a7bSBarry Smith   TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()`
1236a7e14dcfSSatish Balay 
1237a7e14dcfSSatish Balay   Not Collective
1238a7e14dcfSSatish Balay 
1239f899ff85SJose E. Roman   Input Parameter:
124047450a7bSBarry Smith . tao - the `Tao` context
1241a7e14dcfSSatish Balay 
1242a7e14dcfSSatish Balay   Output Parameters:
1243e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this
1244a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this
1245a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor
1246a7e14dcfSSatish Balay 
1247a7e14dcfSSatish Balay   Level: intermediate
1248a1cb98faSBarry Smith 
1249a1cb98faSBarry Smith   Note:
125047450a7bSBarry Smith   `NULL` can be used as an argument if not all tolerances values are needed
1251a1cb98faSBarry Smith 
12521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`
1253a7e14dcfSSatish Balay @*/
1254d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1255d71ae5a4SJacob Faibussowitsch {
1256a7e14dcfSSatish Balay   PetscFunctionBegin;
1257441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1258a7e14dcfSSatish Balay   if (gatol) *gatol = tao->gatol;
1259a7e14dcfSSatish Balay   if (grtol) *grtol = tao->grtol;
1260a7e14dcfSSatish Balay   if (gttol) *gttol = tao->gttol;
12613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1262a7e14dcfSSatish Balay }
1263a7e14dcfSSatish Balay 
1264a7e14dcfSSatish Balay /*@
1265a7e14dcfSSatish Balay   TaoGetKSP - Gets the linear solver used by the optimization solver.
1266a7e14dcfSSatish Balay 
1267a7e14dcfSSatish Balay   Not Collective
1268a7e14dcfSSatish Balay 
126947450a7bSBarry Smith   Input Parameter:
127047450a7bSBarry Smith . tao - the `Tao` solver
1271a7e14dcfSSatish Balay 
127247450a7bSBarry Smith   Output Parameter:
127347450a7bSBarry Smith . ksp - the `KSP` linear solver used in the optimization solver
1274a7e14dcfSSatish Balay 
1275a7e14dcfSSatish Balay   Level: intermediate
1276a7e14dcfSSatish Balay 
12771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `KSP`
1278a7e14dcfSSatish Balay @*/
1279d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
1280d71ae5a4SJacob Faibussowitsch {
1281a7e14dcfSSatish Balay   PetscFunctionBegin;
128294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
12834f572ea9SToby Isaac   PetscAssertPointer(ksp, 2);
1284a7e14dcfSSatish Balay   *ksp = tao->ksp;
12853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1286a7e14dcfSSatish Balay }
1287a7e14dcfSSatish Balay 
1288025e9500SJason Sarich /*@
1289025e9500SJason Sarich   TaoGetLinearSolveIterations - Gets the total number of linear iterations
129047450a7bSBarry Smith   used by the `Tao` solver
1291025e9500SJason Sarich 
1292025e9500SJason Sarich   Not Collective
1293025e9500SJason Sarich 
1294025e9500SJason Sarich   Input Parameter:
129547450a7bSBarry Smith . tao - the `Tao` context
1296025e9500SJason Sarich 
1297025e9500SJason Sarich   Output Parameter:
1298025e9500SJason Sarich . lits - number of linear iterations
1299025e9500SJason Sarich 
1300025e9500SJason Sarich   Level: intermediate
1301025e9500SJason Sarich 
130247450a7bSBarry Smith   Note:
130347450a7bSBarry Smith   This counter is reset to zero for each successive call to `TaoSolve()`
130447450a7bSBarry Smith 
13051cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetKSP()`
1306025e9500SJason Sarich @*/
1307d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits)
1308d71ae5a4SJacob Faibussowitsch {
1309025e9500SJason Sarich   PetscFunctionBegin;
1310025e9500SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
13114f572ea9SToby Isaac   PetscAssertPointer(lits, 2);
13122d9aa51bSJason Sarich   *lits = tao->ksp_tot_its;
13133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1314025e9500SJason Sarich }
1315025e9500SJason Sarich 
1316a7e14dcfSSatish Balay /*@
1317a7e14dcfSSatish Balay   TaoGetLineSearch - Gets the line search used by the optimization solver.
1318a7e14dcfSSatish Balay 
1319a7e14dcfSSatish Balay   Not Collective
1320a7e14dcfSSatish Balay 
132147450a7bSBarry Smith   Input Parameter:
132247450a7bSBarry Smith . tao - the `Tao` solver
1323a7e14dcfSSatish Balay 
132447450a7bSBarry Smith   Output Parameter:
1325a7e14dcfSSatish Balay . ls - the line search used in the optimization solver
1326a7e14dcfSSatish Balay 
1327a7e14dcfSSatish Balay   Level: intermediate
1328a7e14dcfSSatish Balay 
13291cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType`
1330a7e14dcfSSatish Balay @*/
1331d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
1332d71ae5a4SJacob Faibussowitsch {
1333a7e14dcfSSatish Balay   PetscFunctionBegin;
133494511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
13354f572ea9SToby Isaac   PetscAssertPointer(ls, 2);
1336a7e14dcfSSatish Balay   *ls = tao->linesearch;
13373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1338a7e14dcfSSatish Balay }
1339a7e14dcfSSatish Balay 
1340a7e14dcfSSatish Balay /*@
1341a7e14dcfSSatish Balay   TaoAddLineSearchCounts - Adds the number of function evaluations spent
1342a7e14dcfSSatish Balay   in the line search to the running total.
1343a7e14dcfSSatish Balay 
1344a7e14dcfSSatish Balay   Input Parameters:
13452fe279fdSBarry Smith . tao - the `Tao` solver
1346a7e14dcfSSatish Balay 
1347a7e14dcfSSatish Balay   Level: developer
1348a7e14dcfSSatish Balay 
13491cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()`
1350a7e14dcfSSatish Balay @*/
1351d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAddLineSearchCounts(Tao tao)
1352d71ae5a4SJacob Faibussowitsch {
1353a7e14dcfSSatish Balay   PetscBool flg;
1354a7e14dcfSSatish Balay   PetscInt  nfeval, ngeval, nfgeval;
135547a47007SBarry Smith 
1356a7e14dcfSSatish Balay   PetscFunctionBegin;
1357441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1358a7e14dcfSSatish Balay   if (tao->linesearch) {
13599566063dSJacob Faibussowitsch     PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg));
136094ae4db5SBarry Smith     if (!flg) {
13619566063dSJacob Faibussowitsch       PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval));
1362a7e14dcfSSatish Balay       tao->nfuncs += nfeval;
1363a7e14dcfSSatish Balay       tao->ngrads += ngeval;
1364a7e14dcfSSatish Balay       tao->nfuncgrads += nfgeval;
1365a7e14dcfSSatish Balay     }
1366a7e14dcfSSatish Balay   }
13673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1368a7e14dcfSSatish Balay }
1369a7e14dcfSSatish Balay 
1370a7e14dcfSSatish Balay /*@
137147450a7bSBarry Smith   TaoGetSolution - Returns the vector with the current solution from the `Tao` object
1372a7e14dcfSSatish Balay 
1373a7e14dcfSSatish Balay   Not Collective
1374a7e14dcfSSatish Balay 
1375a7e14dcfSSatish Balay   Input Parameter:
137647450a7bSBarry Smith . tao - the `Tao` context
1377a7e14dcfSSatish Balay 
1378a7e14dcfSSatish Balay   Output Parameter:
1379a7e14dcfSSatish Balay . X - the current solution
1380a7e14dcfSSatish Balay 
1381a7e14dcfSSatish Balay   Level: intermediate
1382a7e14dcfSSatish Balay 
138365ba42b6SBarry Smith   Note:
138465ba42b6SBarry Smith   The returned vector will be the same object that was passed into `TaoSetSolution()`
138565ba42b6SBarry Smith 
13861cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()`
1387a7e14dcfSSatish Balay @*/
1388d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolution(Tao tao, Vec *X)
1389d71ae5a4SJacob Faibussowitsch {
1390a7e14dcfSSatish Balay   PetscFunctionBegin;
1391441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
13924f572ea9SToby Isaac   PetscAssertPointer(X, 2);
1393a7e14dcfSSatish Balay   *X = tao->solution;
13943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1395a7e14dcfSSatish Balay }
1396a7e14dcfSSatish Balay 
1397a7e14dcfSSatish Balay /*@
139847450a7bSBarry Smith   TaoResetStatistics - Initialize the statistics collected by the `Tao` object.
1399a7e14dcfSSatish Balay   These statistics include the iteration number, residual norms, and convergence status.
1400a7e14dcfSSatish Balay   This routine gets called before solving each optimization problem.
1401a7e14dcfSSatish Balay 
1402c3339decSBarry Smith   Collective
1403a7e14dcfSSatish Balay 
140447450a7bSBarry Smith   Input Parameter:
1405e056e8ceSJacob Faibussowitsch . tao - the `Tao` context
1406a7e14dcfSSatish Balay 
1407a7e14dcfSSatish Balay   Level: developer
1408a7e14dcfSSatish Balay 
14091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
1410a7e14dcfSSatish Balay @*/
1411d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResetStatistics(Tao tao)
1412d71ae5a4SJacob Faibussowitsch {
1413a7e14dcfSSatish Balay   PetscFunctionBegin;
1414441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1415a7e14dcfSSatish Balay   tao->niter        = 0;
1416a7e14dcfSSatish Balay   tao->nfuncs       = 0;
1417a7e14dcfSSatish Balay   tao->nfuncgrads   = 0;
1418a7e14dcfSSatish Balay   tao->ngrads       = 0;
1419a7e14dcfSSatish Balay   tao->nhess        = 0;
1420a7e14dcfSSatish Balay   tao->njac         = 0;
1421a7e14dcfSSatish Balay   tao->nconstraints = 0;
1422a7e14dcfSSatish Balay   tao->ksp_its      = 0;
1423ae93cb3cSJason Sarich   tao->ksp_tot_its  = 0;
1424a7e14dcfSSatish Balay   tao->reason       = TAO_CONTINUE_ITERATING;
1425a7e14dcfSSatish Balay   tao->residual     = 0.0;
1426a7e14dcfSSatish Balay   tao->cnorm        = 0.0;
1427a7e14dcfSSatish Balay   tao->step         = 0.0;
1428a7e14dcfSSatish Balay   tao->lsflag       = PETSC_FALSE;
1429a7e14dcfSSatish Balay   if (tao->hist_reset) tao->hist_len = 0;
14303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1431a7e14dcfSSatish Balay }
1432a7e14dcfSSatish Balay 
1433a7e14dcfSSatish Balay /*@C
1434e1e80dc8SAlp Dener   TaoSetUpdate - Sets the general-purpose update function called
14352fe279fdSBarry Smith   at the beginning of every iteration of the optimization algorithm. Called after the new solution and the gradient
1436e1e80dc8SAlp Dener   is determined, but before the Hessian is computed (if applicable).
1437e1e80dc8SAlp Dener 
1438c3339decSBarry Smith   Logically Collective
1439e1e80dc8SAlp Dener 
1440e1e80dc8SAlp Dener   Input Parameters:
1441270bebe6SStefano Zampini + tao  - The `Tao` solver
1442270bebe6SStefano Zampini . func - The function
1443270bebe6SStefano Zampini - ctx  - The update function context
1444e1e80dc8SAlp Dener 
144520f4b53cSBarry Smith   Calling sequence of `func`:
1446270bebe6SStefano Zampini + tao - The optimizer context
1447270bebe6SStefano Zampini . it  - The current iteration index
1448270bebe6SStefano Zampini - ctx - The update context
1449e1e80dc8SAlp Dener 
1450e1e80dc8SAlp Dener   Level: advanced
1451e1e80dc8SAlp Dener 
1452270bebe6SStefano Zampini   Notes:
1453270bebe6SStefano Zampini   Users can modify the gradient direction or any other vector associated to the specific solver used.
1454270bebe6SStefano Zampini   The objective function value is always recomputed after a call to the update hook.
1455270bebe6SStefano Zampini 
14561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`
1457e1e80dc8SAlp Dener @*/
1458270bebe6SStefano Zampini PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao tao, PetscInt it, void *ctx), void *ctx)
1459d71ae5a4SJacob Faibussowitsch {
1460e1e80dc8SAlp Dener   PetscFunctionBegin;
1461e1e80dc8SAlp Dener   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1462e1e80dc8SAlp Dener   tao->ops->update = func;
1463e1e80dc8SAlp Dener   tao->user_update = ctx;
14643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1465e1e80dc8SAlp Dener }
1466e1e80dc8SAlp Dener 
1467e1e80dc8SAlp Dener /*@C
1468a7e14dcfSSatish Balay   TaoSetConvergenceTest - Sets the function that is to be used to test
1469a7e14dcfSSatish Balay   for convergence of the iterative minimization solution.  The new convergence
147065ba42b6SBarry Smith   testing routine will replace Tao's default convergence test.
1471a7e14dcfSSatish Balay 
1472c3339decSBarry Smith   Logically Collective
1473a7e14dcfSSatish Balay 
1474a7e14dcfSSatish Balay   Input Parameters:
147547450a7bSBarry Smith + tao  - the `Tao` object
1476a7e14dcfSSatish Balay . conv - the routine to test for convergence
1477a7e14dcfSSatish Balay - ctx  - [optional] context for private data for the convergence routine
147847450a7bSBarry Smith         (may be `NULL`)
1479a7e14dcfSSatish Balay 
148020f4b53cSBarry Smith   Calling sequence of `conv`:
148147450a7bSBarry Smith + tao - the `Tao` object
1482a7e14dcfSSatish Balay - ctx - [optional] convergence context
1483a7e14dcfSSatish Balay 
148447450a7bSBarry Smith   Level: advanced
148547450a7bSBarry Smith 
148665ba42b6SBarry Smith   Note:
148765ba42b6SBarry Smith   The new convergence testing routine should call `TaoSetConvergedReason()`.
1488a7e14dcfSSatish Balay 
148910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoMonitorSet()`
1490a7e14dcfSSatish Balay @*/
1491d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void *), void *ctx)
1492d71ae5a4SJacob Faibussowitsch {
1493a7e14dcfSSatish Balay   PetscFunctionBegin;
1494441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
149594511df7SStefano Zampini   tao->ops->convergencetest = conv;
149694511df7SStefano Zampini   tao->cnvP                 = ctx;
14973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1498a7e14dcfSSatish Balay }
1499a7e14dcfSSatish Balay 
1500a7e14dcfSSatish Balay /*@C
150110978b7dSBarry Smith   TaoMonitorSet - Sets an additional function that is to be used at every
1502a7e14dcfSSatish Balay   iteration of the solver to display the iteration's
1503a7e14dcfSSatish Balay   progress.
1504a7e14dcfSSatish Balay 
1505c3339decSBarry Smith   Logically Collective
1506a7e14dcfSSatish Balay 
1507a7e14dcfSSatish Balay   Input Parameters:
150847450a7bSBarry Smith + tao  - the `Tao` solver context
15092fe279fdSBarry Smith . func - monitoring routine
15102fe279fdSBarry Smith . ctx  - [optional] user-defined context for private data for the monitor routine (may be `NULL`)
151149abdd8aSBarry Smith - dest - [optional] function to destroy the context when the `Tao` is destroyed, see `PetscCtxDestroyFn` for the calling sequence
1512a7e14dcfSSatish Balay 
15132fe279fdSBarry Smith   Calling sequence of `func`:
151447450a7bSBarry Smith + tao - the `Tao` solver context
15152fe279fdSBarry Smith - ctx - [optional] monitoring context
15162fe279fdSBarry Smith 
151747450a7bSBarry Smith   Level: intermediate
151847450a7bSBarry Smith 
151987497f52SBarry Smith   Notes:
152010978b7dSBarry Smith   See `TaoSetFromOptions()` for a monitoring options.
152110978b7dSBarry Smith 
1522a7e14dcfSSatish Balay   Several different monitoring routines may be set by calling
152310978b7dSBarry Smith   `TaoMonitorSet()` multiple times; all will be called in the
1524a7e14dcfSSatish Balay   order in which they were set.
1525a7e14dcfSSatish Balay 
1526e056e8ceSJacob Faibussowitsch   Fortran Notes:
152795452b02SPatrick Sanan   Only one monitor function may be set
1528a7e14dcfSSatish Balay 
152949abdd8aSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoMonitorCancel()`, `TaoSetDestroyRoutine()`, `TaoView()`, `PetscCtxDestroyFn`
1530a7e14dcfSSatish Balay @*/
153149abdd8aSBarry Smith PetscErrorCode TaoMonitorSet(Tao tao, PetscErrorCode (*func)(Tao, void *), void *ctx, PetscCtxDestroyFn *dest)
1532d71ae5a4SJacob Faibussowitsch {
153308d19d1fSJason Sarich   PetscInt  i;
15348732526dSAlp Dener   PetscBool identical;
153508d19d1fSJason Sarich 
1536a7e14dcfSSatish Balay   PetscFunctionBegin;
1537441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
15383c859ba3SBarry Smith   PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS);
153908d19d1fSJason Sarich 
154008d19d1fSJason Sarich   for (i = 0; i < tao->numbermonitors; i++) {
15419566063dSJacob Faibussowitsch     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))func, ctx, dest, (PetscErrorCode (*)(void))tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical));
15423ba16761SJacob Faibussowitsch     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
154308d19d1fSJason Sarich   }
1544a7e14dcfSSatish Balay   tao->monitor[tao->numbermonitors]        = func;
1545835f2295SStefano Zampini   tao->monitorcontext[tao->numbermonitors] = ctx;
1546a7e14dcfSSatish Balay   tao->monitordestroy[tao->numbermonitors] = dest;
1547a7e14dcfSSatish Balay   ++tao->numbermonitors;
15483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1549a7e14dcfSSatish Balay }
1550a7e14dcfSSatish Balay 
1551a7e14dcfSSatish Balay /*@
1552b2dc45ddSPierre Jolivet   TaoMonitorCancel - Clears all the monitor functions for a `Tao` object.
1553a7e14dcfSSatish Balay 
1554c3339decSBarry Smith   Logically Collective
1555a7e14dcfSSatish Balay 
155647450a7bSBarry Smith   Input Parameter:
155747450a7bSBarry Smith . tao - the `Tao` solver context
1558a7e14dcfSSatish Balay 
15593c7db156SBarry Smith   Options Database Key:
1560b2dc45ddSPierre Jolivet . -tao_monitor_cancel - cancels all monitors that have been hardwired
156110978b7dSBarry Smith     into a code by calls to `TaoMonitorSet()`, but does not cancel those
1562a7e14dcfSSatish Balay     set via the options database
1563a7e14dcfSSatish Balay 
1564a7e14dcfSSatish Balay   Level: advanced
1565a7e14dcfSSatish Balay 
156647450a7bSBarry Smith   Note:
156747450a7bSBarry Smith   There is no way to clear one specific monitor from a `Tao` object.
156847450a7bSBarry Smith 
156910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1570a7e14dcfSSatish Balay @*/
1571b2dc45ddSPierre Jolivet PetscErrorCode TaoMonitorCancel(Tao tao)
1572d71ae5a4SJacob Faibussowitsch {
1573a7e14dcfSSatish Balay   PetscInt i;
157447a47007SBarry Smith 
1575a7e14dcfSSatish Balay   PetscFunctionBegin;
1576441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1577a7e14dcfSSatish Balay   for (i = 0; i < tao->numbermonitors; i++) {
157848a46eb9SPierre Jolivet     if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i]));
1579a7e14dcfSSatish Balay   }
1580a7e14dcfSSatish Balay   tao->numbermonitors = 0;
15813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1582a7e14dcfSSatish Balay }
1583a7e14dcfSSatish Balay 
15847fab98ebSJason Sarich /*@
158547450a7bSBarry Smith   TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()`
1586a7e14dcfSSatish Balay 
1587c3339decSBarry Smith   Collective
1588a7e14dcfSSatish Balay 
1589a7e14dcfSSatish Balay   Input Parameters:
159047450a7bSBarry Smith + tao - the `Tao` context
159147450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1592a7e14dcfSSatish Balay 
159347450a7bSBarry Smith   Options Database Key:
1594147403d9SBarry Smith . -tao_monitor - turn on default monitoring
1595a7e14dcfSSatish Balay 
1596a7e14dcfSSatish Balay   Level: advanced
1597a7e14dcfSSatish Balay 
159847450a7bSBarry Smith   Note:
159947450a7bSBarry Smith   This monitor prints the function value and gradient
160047450a7bSBarry Smith   norm at each iteration.
160147450a7bSBarry Smith 
160210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1603a7e14dcfSSatish Balay @*/
1604d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDefault(Tao tao, void *ctx)
1605d71ae5a4SJacob Faibussowitsch {
160663b15415SAlp Dener   PetscInt    its, tabs;
1607a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
16088163d661SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
160947a47007SBarry Smith 
1610a7e14dcfSSatish Balay   PetscFunctionBegin;
161194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
16128163d661SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1613a7e14dcfSSatish Balay   its   = tao->niter;
1614a7e14dcfSSatish Balay   fct   = tao->fc;
1615a7e14dcfSSatish Balay   gnorm = tao->residual;
16169566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
16179566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1618494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
16199566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1620494bef23SAlp Dener     tao->header_printed = PETSC_TRUE;
162163b15415SAlp Dener   }
162263a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its));
16239566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Function value: %g,", (double)fct));
162408d19d1fSJason Sarich   if (gnorm >= PETSC_INFINITY) {
16259566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: Inf \n"));
162608d19d1fSJason Sarich   } else {
16279566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g \n", (double)gnorm));
162808d19d1fSJason Sarich   }
16299566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
16303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1631a7e14dcfSSatish Balay }
1632a7e14dcfSSatish Balay 
16337fab98ebSJason Sarich /*@
163410978b7dSBarry Smith   TaoMonitorGlobalization - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method.
16358d5ead36SAlp Dener 
1636c3339decSBarry Smith   Collective
16378d5ead36SAlp Dener 
16388d5ead36SAlp Dener   Input Parameters:
163947450a7bSBarry Smith + tao - the `Tao` context
164047450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
16418d5ead36SAlp Dener 
164247450a7bSBarry Smith   Options Database Key:
164310978b7dSBarry Smith . -tao_monitor_globalization - turn on monitoring with globalization information
16448d5ead36SAlp Dener 
16458d5ead36SAlp Dener   Level: advanced
16468d5ead36SAlp Dener 
164747450a7bSBarry Smith   Note:
164847450a7bSBarry Smith   This monitor prints the function value and gradient norm at each
164947450a7bSBarry Smith   iteration, as well as the step size and trust radius. Note that the
165047450a7bSBarry Smith   step size and trust radius may be the same for some algorithms.
165147450a7bSBarry Smith 
165210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
16538d5ead36SAlp Dener @*/
165410978b7dSBarry Smith PetscErrorCode TaoMonitorGlobalization(Tao tao, void *ctx)
1655d71ae5a4SJacob Faibussowitsch {
16568d5ead36SAlp Dener   PetscInt    its, tabs;
16578d5ead36SAlp Dener   PetscReal   fct, gnorm, stp, tr;
16588d5ead36SAlp Dener   PetscViewer viewer = (PetscViewer)ctx;
16598d5ead36SAlp Dener 
16608d5ead36SAlp Dener   PetscFunctionBegin;
166194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
16628d5ead36SAlp Dener   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
16638d5ead36SAlp Dener   its   = tao->niter;
16648d5ead36SAlp Dener   fct   = tao->fc;
16658d5ead36SAlp Dener   gnorm = tao->residual;
16668d5ead36SAlp Dener   stp   = tao->step;
16678d5ead36SAlp Dener   tr    = tao->trust;
16689566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
16699566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1670494bef23SAlp Dener   if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
16719566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1672494bef23SAlp Dener     tao->header_printed = PETSC_TRUE;
16738d5ead36SAlp Dener   }
167463a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its));
16759566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Function value: %g,", (double)fct));
16768d5ead36SAlp Dener   if (gnorm >= PETSC_INFINITY) {
16779566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: Inf,"));
16788d5ead36SAlp Dener   } else {
16799566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g,", (double)gnorm));
16808d5ead36SAlp Dener   }
16819566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Step: %g,  Trust: %g\n", (double)stp, (double)tr));
16829566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
16833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16848d5ead36SAlp Dener }
16858d5ead36SAlp Dener 
16868d5ead36SAlp Dener /*@
168710978b7dSBarry Smith   TaoMonitorDefaultShort - Routine for monitoring progress of `TaoSolve()` that displays fewer digits than `TaoMonitorDefault()`
1688a7e14dcfSSatish Balay 
1689c3339decSBarry Smith   Collective
1690a7e14dcfSSatish Balay 
1691a7e14dcfSSatish Balay   Input Parameters:
169247450a7bSBarry Smith + tao - the `Tao` context
169347450a7bSBarry Smith - ctx - `PetscViewer` context of type `PETSCVIEWERASCII`
1694a7e14dcfSSatish Balay 
169547450a7bSBarry Smith   Options Database Key:
169610978b7dSBarry Smith . -tao_monitor_short - turn on default short monitoring
1697a7e14dcfSSatish Balay 
1698a7e14dcfSSatish Balay   Level: advanced
1699a7e14dcfSSatish Balay 
170047450a7bSBarry Smith   Note:
170147450a7bSBarry Smith   Same as `TaoMonitorDefault()` except
170247450a7bSBarry Smith   it prints fewer digits of the residual as the residual gets smaller.
170347450a7bSBarry Smith   This is because the later digits are meaningless and are often
170447450a7bSBarry Smith   different on different machines; by using this routine different
170547450a7bSBarry Smith   machines will usually generate the same output.
170647450a7bSBarry Smith 
170710978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1708a7e14dcfSSatish Balay @*/
170910978b7dSBarry Smith PetscErrorCode TaoMonitorDefaultShort(Tao tao, void *ctx)
1710d71ae5a4SJacob Faibussowitsch {
17112c9b8b83SAlp Dener   PetscInt    its, tabs;
1712a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
17134d4332d5SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
171447a47007SBarry Smith 
1715a7e14dcfSSatish Balay   PetscFunctionBegin;
171694511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
17174d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1718a7e14dcfSSatish Balay   its   = tao->niter;
1719a7e14dcfSSatish Balay   fct   = tao->fc;
1720a7e14dcfSSatish Balay   gnorm = tao->residual;
17219566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
17229566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
172363a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %3" PetscInt_FMT ",", its));
17249566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, " Function value %g,", (double)fct));
1725d393f493SBarry Smith   if (gnorm >= PETSC_INFINITY) {
17269566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n"));
172708d19d1fSJason Sarich   } else if (gnorm > 1.e-6) {
17289566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm));
1729a7e14dcfSSatish Balay   } else if (gnorm > 1.e-11) {
17309566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-6 \n"));
1731a7e14dcfSSatish Balay   } else {
17329566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-11 \n"));
1733a7e14dcfSSatish Balay   }
17349566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
17353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1736a7e14dcfSSatish Balay }
1737a7e14dcfSSatish Balay 
17387fab98ebSJason Sarich /*@
173910978b7dSBarry Smith   TaoMonitorConstraintNorm - same as `TaoMonitorDefault()` except
174047450a7bSBarry Smith   it prints the norm of the constraint function.
1741a7e14dcfSSatish Balay 
1742c3339decSBarry Smith   Collective
1743a7e14dcfSSatish Balay 
1744a7e14dcfSSatish Balay   Input Parameters:
174547450a7bSBarry Smith + tao - the `Tao` context
174647450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1747a7e14dcfSSatish Balay 
174847450a7bSBarry Smith   Options Database Key:
174910978b7dSBarry Smith . -tao_monitor_constraint_norm - monitor the constraints
1750a7e14dcfSSatish Balay 
1751a7e14dcfSSatish Balay   Level: advanced
1752a7e14dcfSSatish Balay 
175310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1754a7e14dcfSSatish Balay @*/
175510978b7dSBarry Smith PetscErrorCode TaoMonitorConstraintNorm(Tao tao, void *ctx)
1756d71ae5a4SJacob Faibussowitsch {
17572c9b8b83SAlp Dener   PetscInt    its, tabs;
1758a7e14dcfSSatish Balay   PetscReal   fct, gnorm;
1759d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
1760a7e14dcfSSatish Balay 
1761a7e14dcfSSatish Balay   PetscFunctionBegin;
176294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1763d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1764a7e14dcfSSatish Balay   its   = tao->niter;
1765a7e14dcfSSatish Balay   fct   = tao->fc;
1766a7e14dcfSSatish Balay   gnorm = tao->residual;
17679566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
17689566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
176963a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", its));
17709566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct));
17719566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Residual: %g ", (double)gnorm));
17729566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "  Constraint: %g \n", (double)tao->cnorm));
17739566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetTab(viewer, tabs));
17743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1775a7e14dcfSSatish Balay }
1776a7e14dcfSSatish Balay 
1777a7e14dcfSSatish Balay /*@C
177810978b7dSBarry Smith   TaoMonitorSolution - Views the solution at each iteration of `TaoSolve()`
1779a7e14dcfSSatish Balay 
1780c3339decSBarry Smith   Collective
1781a7e14dcfSSatish Balay 
1782a7e14dcfSSatish Balay   Input Parameters:
178347450a7bSBarry Smith + tao - the `Tao` context
178447450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1785a7e14dcfSSatish Balay 
178647450a7bSBarry Smith   Options Database Key:
178710978b7dSBarry Smith . -tao_monitor_solution - view the solution
1788a7e14dcfSSatish Balay 
1789a7e14dcfSSatish Balay   Level: advanced
1790a7e14dcfSSatish Balay 
179110978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1792a7e14dcfSSatish Balay @*/
179310978b7dSBarry Smith PetscErrorCode TaoMonitorSolution(Tao tao, void *ctx)
1794d71ae5a4SJacob Faibussowitsch {
1795feb237baSPierre Jolivet   PetscViewer viewer = (PetscViewer)ctx;
179647a47007SBarry Smith 
1797a7e14dcfSSatish Balay   PetscFunctionBegin;
179894511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1799d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18009566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution, viewer));
18013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1802a7e14dcfSSatish Balay }
1803a7e14dcfSSatish Balay 
1804a7e14dcfSSatish Balay /*@C
180510978b7dSBarry Smith   TaoMonitorGradient - Views the gradient at each iteration of `TaoSolve()`
1806a7e14dcfSSatish Balay 
1807c3339decSBarry Smith   Collective
1808a7e14dcfSSatish Balay 
1809a7e14dcfSSatish Balay   Input Parameters:
181047450a7bSBarry Smith + tao - the `Tao` context
181147450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1812a7e14dcfSSatish Balay 
181347450a7bSBarry Smith   Options Database Key:
181410978b7dSBarry Smith . -tao_monitor_gradient - view the gradient at each iteration
1815a7e14dcfSSatish Balay 
1816a7e14dcfSSatish Balay   Level: advanced
1817a7e14dcfSSatish Balay 
181810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1819a7e14dcfSSatish Balay @*/
182010978b7dSBarry Smith PetscErrorCode TaoMonitorGradient(Tao tao, void *ctx)
1821d71ae5a4SJacob Faibussowitsch {
1822d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
182347a47007SBarry Smith 
1824a7e14dcfSSatish Balay   PetscFunctionBegin;
182594511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1826d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18279566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient, viewer));
18283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1829a7e14dcfSSatish Balay }
1830a7e14dcfSSatish Balay 
1831a7e14dcfSSatish Balay /*@C
183210978b7dSBarry Smith   TaoMonitorStep - Views the step-direction at each iteration of `TaoSolve()`
1833a7e14dcfSSatish Balay 
1834c3339decSBarry Smith   Collective
1835a7e14dcfSSatish Balay 
1836a7e14dcfSSatish Balay   Input Parameters:
183747450a7bSBarry Smith + tao - the `Tao` context
183847450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL`
1839a7e14dcfSSatish Balay 
184047450a7bSBarry Smith   Options Database Key:
184110978b7dSBarry Smith . -tao_monitor_step - view the step vector at each iteration
1842a7e14dcfSSatish Balay 
1843a7e14dcfSSatish Balay   Level: advanced
1844a7e14dcfSSatish Balay 
184510978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1846a7e14dcfSSatish Balay @*/
184710978b7dSBarry Smith PetscErrorCode TaoMonitorStep(Tao tao, void *ctx)
1848d71ae5a4SJacob Faibussowitsch {
1849d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
1850d393f493SBarry Smith 
1851a7e14dcfSSatish Balay   PetscFunctionBegin;
185294511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1853d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
18549566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection, viewer));
18553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1856a7e14dcfSSatish Balay }
1857a7e14dcfSSatish Balay 
1858a7e14dcfSSatish Balay /*@C
185910978b7dSBarry Smith   TaoMonitorSolutionDraw - Plots the solution at each iteration of `TaoSolve()`
1860a7e14dcfSSatish Balay 
1861c3339decSBarry Smith   Collective
1862a7e14dcfSSatish Balay 
1863a7e14dcfSSatish Balay   Input Parameters:
186447450a7bSBarry Smith + tao - the `Tao` context
186565ba42b6SBarry Smith - ctx - `TaoMonitorDraw` context
1866a7e14dcfSSatish Balay 
186747450a7bSBarry Smith   Options Database Key:
186810978b7dSBarry Smith . -tao_monitor_solution_draw - draw the solution at each iteration
1869a7e14dcfSSatish Balay 
1870a7e14dcfSSatish Balay   Level: advanced
1871a7e14dcfSSatish Balay 
1872a81c3919SBarry Smith   Note:
1873a81c3919SBarry Smith   The context created by `TaoMonitorDrawCtxCreate()`, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()`
1874a81c3919SBarry Smith   are passed to `TaoMonitorSet()` to monitor the solution graphically.
1875a81c3919SBarry Smith 
1876a81c3919SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSolution()`, `TaoMonitorSet()`, `TaoMonitorGradientDraw()`, `TaoMonitorDrawCtxCreate()`,
1877a81c3919SBarry Smith           `TaoMonitorDrawCtxDestroy()`
1878a7e14dcfSSatish Balay @*/
187910978b7dSBarry Smith PetscErrorCode TaoMonitorSolutionDraw(Tao tao, void *ctx)
1880d71ae5a4SJacob Faibussowitsch {
1881e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
188247a47007SBarry Smith 
1883a7e14dcfSSatish Balay   PetscFunctionBegin;
188494511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
18853ba16761SJacob Faibussowitsch   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
18869566063dSJacob Faibussowitsch   PetscCall(VecView(tao->solution, ictx->viewer));
18873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1888a7e14dcfSSatish Balay }
1889a7e14dcfSSatish Balay 
1890a7e14dcfSSatish Balay /*@C
189110978b7dSBarry Smith   TaoMonitorGradientDraw - Plots the gradient at each iteration of `TaoSolve()`
1892a7e14dcfSSatish Balay 
1893c3339decSBarry Smith   Collective
1894a7e14dcfSSatish Balay 
1895a7e14dcfSSatish Balay   Input Parameters:
189647450a7bSBarry Smith + tao - the `Tao` context
189765ba42b6SBarry Smith - ctx - `PetscViewer` context
1898a7e14dcfSSatish Balay 
189947450a7bSBarry Smith   Options Database Key:
190010978b7dSBarry Smith . -tao_monitor_gradient_draw - draw the gradient at each iteration
1901a7e14dcfSSatish Balay 
1902a7e14dcfSSatish Balay   Level: advanced
1903a7e14dcfSSatish Balay 
190410978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorGradient()`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw()`
1905a7e14dcfSSatish Balay @*/
190610978b7dSBarry Smith PetscErrorCode TaoMonitorGradientDraw(Tao tao, void *ctx)
1907d71ae5a4SJacob Faibussowitsch {
1908e882e171SHong Zhang   TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
190947a47007SBarry Smith 
1910a7e14dcfSSatish Balay   PetscFunctionBegin;
191194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
19123ba16761SJacob Faibussowitsch   if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
19139566063dSJacob Faibussowitsch   PetscCall(VecView(tao->gradient, ictx->viewer));
19143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1915a7e14dcfSSatish Balay }
1916a7e14dcfSSatish Balay 
1917a7e14dcfSSatish Balay /*@C
191810978b7dSBarry Smith   TaoMonitorStepDraw - Plots the step direction at each iteration of `TaoSolve()`
1919a7e14dcfSSatish Balay 
1920c3339decSBarry Smith   Collective
1921a7e14dcfSSatish Balay 
1922a7e14dcfSSatish Balay   Input Parameters:
192347450a7bSBarry Smith + tao - the `Tao` context
192447450a7bSBarry Smith - ctx - the `PetscViewer` context
1925a7e14dcfSSatish Balay 
192647450a7bSBarry Smith   Options Database Key:
192710978b7dSBarry Smith . -tao_monitor_step_draw - draw the step direction at each iteration
1928a7e14dcfSSatish Balay 
1929a7e14dcfSSatish Balay   Level: advanced
1930a7e14dcfSSatish Balay 
193110978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw`
1932a7e14dcfSSatish Balay @*/
193310978b7dSBarry Smith PetscErrorCode TaoMonitorStepDraw(Tao tao, void *ctx)
1934d71ae5a4SJacob Faibussowitsch {
193594511df7SStefano Zampini   PetscViewer viewer = (PetscViewer)ctx;
193647a47007SBarry Smith 
1937a7e14dcfSSatish Balay   PetscFunctionBegin;
193894511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
193994511df7SStefano Zampini   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
19409566063dSJacob Faibussowitsch   PetscCall(VecView(tao->stepdirection, viewer));
19413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1942a7e14dcfSSatish Balay }
1943a7e14dcfSSatish Balay 
1944a7e14dcfSSatish Balay /*@C
194510978b7dSBarry Smith   TaoMonitorResidual - Views the least-squares residual at each iteration of `TaoSolve()`
1946a7e14dcfSSatish Balay 
1947c3339decSBarry Smith   Collective
1948a7e14dcfSSatish Balay 
1949a7e14dcfSSatish Balay   Input Parameters:
195047450a7bSBarry Smith + tao - the `Tao` context
195147450a7bSBarry Smith - ctx - the `PetscViewer` context or `NULL`
1952a7e14dcfSSatish Balay 
195347450a7bSBarry Smith   Options Database Key:
195410978b7dSBarry Smith . -tao_monitor_ls_residual - view the residual at each iteration
1955a7e14dcfSSatish Balay 
1956a7e14dcfSSatish Balay   Level: advanced
1957a7e14dcfSSatish Balay 
195810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()`
1959a7e14dcfSSatish Balay @*/
196010978b7dSBarry Smith PetscErrorCode TaoMonitorResidual(Tao tao, void *ctx)
1961d71ae5a4SJacob Faibussowitsch {
1962d393f493SBarry Smith   PetscViewer viewer = (PetscViewer)ctx;
196347a47007SBarry Smith 
1964a7e14dcfSSatish Balay   PetscFunctionBegin;
196594511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
1966d393f493SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
19679566063dSJacob Faibussowitsch   PetscCall(VecView(tao->ls_res, viewer));
19683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1969a7e14dcfSSatish Balay }
1970a7e14dcfSSatish Balay 
19717fab98ebSJason Sarich /*@
1972a7e14dcfSSatish Balay   TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
1973a7e14dcfSSatish Balay   or terminate.
1974a7e14dcfSSatish Balay 
1975c3339decSBarry Smith   Collective
1976a7e14dcfSSatish Balay 
1977a7e14dcfSSatish Balay   Input Parameters:
197847450a7bSBarry Smith + tao   - the `Tao` context
1979a7e14dcfSSatish Balay - dummy - unused dummy context
1980a7e14dcfSSatish Balay 
198147450a7bSBarry Smith   Level: developer
198247450a7bSBarry Smith 
1983a7e14dcfSSatish Balay   Notes:
1984a7e14dcfSSatish Balay   This routine checks the residual in the optimality conditions, the
1985a7e14dcfSSatish Balay   relative residual in the optimity conditions, the number of function
1986a7e14dcfSSatish Balay   evaluations, and the function value to test convergence.  Some
1987a7e14dcfSSatish Balay   solvers may use different convergence routines.
1988a7e14dcfSSatish Balay 
19891cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()`
1990a7e14dcfSSatish Balay @*/
1991d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy)
1992d71ae5a4SJacob Faibussowitsch {
1993a7e14dcfSSatish Balay   PetscInt           niter = tao->niter, nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads);
1994a7e14dcfSSatish Balay   PetscInt           max_funcs = tao->max_funcs;
1995a7e14dcfSSatish Balay   PetscReal          gnorm = tao->residual, gnorm0 = tao->gnorm0;
1996a7e14dcfSSatish Balay   PetscReal          f = tao->fc, steptol = tao->steptol, trradius = tao->step;
1997a7e14dcfSSatish Balay   PetscReal          gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol;
1998e52336cbSBarry Smith   PetscReal          catol = tao->catol, crtol = tao->crtol;
1999e52336cbSBarry Smith   PetscReal          fmin = tao->fmin, cnorm = tao->cnorm;
2000e4cb33bbSBarry Smith   TaoConvergedReason reason = tao->reason;
2001a7e14dcfSSatish Balay 
2002a7e14dcfSSatish Balay   PetscFunctionBegin;
2003441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
20043ba16761SJacob Faibussowitsch   if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
2005a7e14dcfSSatish Balay 
2006a7e14dcfSSatish Balay   if (PetscIsInfOrNanReal(f)) {
20079566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Failed to converged, function value is Inf or NaN\n"));
2008a7e14dcfSSatish Balay     reason = TAO_DIVERGED_NAN;
2009a7e14dcfSSatish Balay   } else if (f <= fmin && cnorm <= catol) {
20109566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin));
2011a7e14dcfSSatish Balay     reason = TAO_CONVERGED_MINF;
2012a7e14dcfSSatish Balay   } else if (gnorm <= gatol && cnorm <= catol) {
20139566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol));
2014a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GATOL;
2015a7e14dcfSSatish Balay   } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) {
20169566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol));
2017a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GRTOL;
2018e1d16bb9SBarry Smith   } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) {
20199566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol));
2020a7e14dcfSSatish Balay     reason = TAO_CONVERGED_GTTOL;
2021606f75f6SBarry Smith   } else if (max_funcs != PETSC_UNLIMITED && nfuncs > max_funcs) {
20229566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs));
2023a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXFCN;
2024a7e14dcfSSatish Balay   } else if (tao->lsflag != 0) {
20259566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Tao Line Search failure.\n"));
2026a7e14dcfSSatish Balay     reason = TAO_DIVERGED_LS_FAILURE;
2027a7e14dcfSSatish Balay   } else if (trradius < steptol && niter > 0) {
20289566063dSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol));
2029a7e14dcfSSatish Balay     reason = TAO_CONVERGED_STEPTOL;
2030e031d6f5SAlp Dener   } else if (niter >= tao->max_it) {
203163a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it));
2032a7e14dcfSSatish Balay     reason = TAO_DIVERGED_MAXITS;
2033a7e14dcfSSatish Balay   } else {
2034a7e14dcfSSatish Balay     reason = TAO_CONTINUE_ITERATING;
2035a7e14dcfSSatish Balay   }
2036a7e14dcfSSatish Balay   tao->reason = reason;
20373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2038a7e14dcfSSatish Balay }
2039a7e14dcfSSatish Balay 
2040cc4c1da9SBarry Smith /*@
2041a7e14dcfSSatish Balay   TaoSetOptionsPrefix - Sets the prefix used for searching for all
204265ba42b6SBarry Smith   Tao options in the database.
2043a7e14dcfSSatish Balay 
2044c3339decSBarry Smith   Logically Collective
2045a7e14dcfSSatish Balay 
2046a7e14dcfSSatish Balay   Input Parameters:
204747450a7bSBarry Smith + tao - the `Tao` context
2048e056e8ceSJacob Faibussowitsch - p   - the prefix string to prepend to all Tao option requests
2049a7e14dcfSSatish Balay 
20502fe279fdSBarry Smith   Level: advanced
20512fe279fdSBarry Smith 
2052a7e14dcfSSatish Balay   Notes:
2053a7e14dcfSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
2054a7e14dcfSSatish Balay   The first character of all runtime options is AUTOMATICALLY the hyphen.
2055a7e14dcfSSatish Balay 
2056a7e14dcfSSatish Balay   For example, to distinguish between the runtime options for two
205765ba42b6SBarry Smith   different Tao solvers, one could call
2058a7e14dcfSSatish Balay .vb
2059a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao1,"sys1_")
2060a7e14dcfSSatish Balay       TaoSetOptionsPrefix(tao2,"sys2_")
2061a7e14dcfSSatish Balay .ve
2062a7e14dcfSSatish Balay 
2063a7e14dcfSSatish Balay   This would enable use of different options for each system, such as
2064a7e14dcfSSatish Balay .vb
20659fa2b5dcSStefano Zampini       -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3
20669fa2b5dcSStefano Zampini       -sys2_tao_method lmvm  -sys2_tao_grtol 1.e-4
2067a7e14dcfSSatish Balay .ve
2068a7e14dcfSSatish Balay 
20691cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()`
2070a7e14dcfSSatish Balay @*/
2071d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
2072d71ae5a4SJacob Faibussowitsch {
2073a7e14dcfSSatish Balay   PetscFunctionBegin;
207494511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
20759566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p));
20761baa6e33SBarry Smith   if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p));
20771baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p));
20783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2079a7e14dcfSSatish Balay }
2080a7e14dcfSSatish Balay 
2081cc4c1da9SBarry Smith /*@
208247450a7bSBarry Smith   TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database.
2083a7e14dcfSSatish Balay 
2084c3339decSBarry Smith   Logically Collective
2085a7e14dcfSSatish Balay 
2086a7e14dcfSSatish Balay   Input Parameters:
208747450a7bSBarry Smith + tao - the `Tao` solver context
2088e056e8ceSJacob Faibussowitsch - p   - the prefix string to prepend to all `Tao` option requests
2089a7e14dcfSSatish Balay 
20902fe279fdSBarry Smith   Level: advanced
20912fe279fdSBarry Smith 
209265ba42b6SBarry Smith   Note:
2093a7e14dcfSSatish Balay   A hyphen (-) must NOT be given at the beginning of the prefix name.
209465ba42b6SBarry Smith   The first character of all runtime options is automatically the hyphen.
2095a7e14dcfSSatish Balay 
20961cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()`
2097a7e14dcfSSatish Balay @*/
2098d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
2099d71ae5a4SJacob Faibussowitsch {
2100a7e14dcfSSatish Balay   PetscFunctionBegin;
210194511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
21029566063dSJacob Faibussowitsch   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p));
21031baa6e33SBarry Smith   if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p));
21041baa6e33SBarry Smith   if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p));
21053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2106a7e14dcfSSatish Balay }
2107a7e14dcfSSatish Balay 
2108cc4c1da9SBarry Smith /*@
2109a7e14dcfSSatish Balay   TaoGetOptionsPrefix - Gets the prefix used for searching for all
211065ba42b6SBarry Smith   Tao options in the database
2111a7e14dcfSSatish Balay 
2112a7e14dcfSSatish Balay   Not Collective
2113a7e14dcfSSatish Balay 
211447450a7bSBarry Smith   Input Parameter:
211547450a7bSBarry Smith . tao - the `Tao` context
2116a7e14dcfSSatish Balay 
211747450a7bSBarry Smith   Output Parameter:
2118e056e8ceSJacob Faibussowitsch . p - pointer to the prefix string used is returned
2119a7e14dcfSSatish Balay 
2120a7e14dcfSSatish Balay   Level: advanced
2121a7e14dcfSSatish Balay 
21221cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()`
2123a7e14dcfSSatish Balay @*/
2124d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
2125d71ae5a4SJacob Faibussowitsch {
212694511df7SStefano Zampini   PetscFunctionBegin;
212794511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
21289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p));
21293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2130a7e14dcfSSatish Balay }
2131a7e14dcfSSatish Balay 
2132cc4c1da9SBarry Smith /*@
213347450a7bSBarry Smith   TaoSetType - Sets the `TaoType` for the minimization solver.
2134a7e14dcfSSatish Balay 
2135c3339decSBarry Smith   Collective
2136a7e14dcfSSatish Balay 
2137a7e14dcfSSatish Balay   Input Parameters:
2138e056e8ceSJacob Faibussowitsch + tao  - the `Tao` solver context
2139a7e14dcfSSatish Balay - type - a known method
2140a7e14dcfSSatish Balay 
2141a7e14dcfSSatish Balay   Options Database Key:
214258417fe7SBarry Smith . -tao_type <type> - Sets the method; use -help for a list
214358417fe7SBarry Smith    of available methods (for instance, "-tao_type lmvm" or "-tao_type tron")
2144a7e14dcfSSatish Balay 
2145a7e14dcfSSatish Balay   Level: intermediate
2146a7e14dcfSSatish Balay 
21471cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType`
2148a7e14dcfSSatish Balay @*/
2149d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetType(Tao tao, TaoType type)
2150d71ae5a4SJacob Faibussowitsch {
2151441846f8SBarry Smith   PetscErrorCode (*create_xxx)(Tao);
2152a7e14dcfSSatish Balay   PetscBool issame;
2153a7e14dcfSSatish Balay 
2154a7e14dcfSSatish Balay   PetscFunctionBegin;
2155441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2156a7e14dcfSSatish Balay 
21579566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame));
21583ba16761SJacob Faibussowitsch   if (issame) PetscFunctionReturn(PETSC_SUCCESS);
2159a7e14dcfSSatish Balay 
2160835f2295SStefano Zampini   PetscCall(PetscFunctionListFind(TaoList, type, &create_xxx));
21613c859ba3SBarry Smith   PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type);
2162a7e14dcfSSatish Balay 
2163a7e14dcfSSatish Balay   /* Destroy the existing solver information */
2164dbbe0bcdSBarry Smith   PetscTryTypeMethod(tao, destroy);
21651baa6e33SBarry Smith   PetscCall(KSPDestroy(&tao->ksp));
21669566063dSJacob Faibussowitsch   PetscCall(TaoLineSearchDestroy(&tao->linesearch));
216743547594SStefano Zampini 
216843547594SStefano Zampini   /* Reinitialize type-specific function pointers in TaoOps structure */
216983c8fe1dSLisandro Dalcin   tao->ops->setup          = NULL;
217043547594SStefano Zampini   tao->ops->computedual    = NULL;
217183c8fe1dSLisandro Dalcin   tao->ops->solve          = NULL;
217283c8fe1dSLisandro Dalcin   tao->ops->view           = NULL;
217383c8fe1dSLisandro Dalcin   tao->ops->setfromoptions = NULL;
217483c8fe1dSLisandro Dalcin   tao->ops->destroy        = NULL;
2175a7e14dcfSSatish Balay 
2176a7e14dcfSSatish Balay   tao->setupcalled = PETSC_FALSE;
2177a7e14dcfSSatish Balay 
21789566063dSJacob Faibussowitsch   PetscCall((*create_xxx)(tao));
21799566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type));
21803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2181a7e14dcfSSatish Balay }
218247a47007SBarry Smith 
218367be906fSBarry Smith /*@C
218447450a7bSBarry Smith   TaoRegister - Adds a method to the Tao package for minimization.
2185a7e14dcfSSatish Balay 
2186cc4c1da9SBarry Smith   Not Collective, No Fortran Support
2187a7e14dcfSSatish Balay 
2188a7e14dcfSSatish Balay   Input Parameters:
2189a7e14dcfSSatish Balay + sname - name of a new user-defined solver
2190a7e14dcfSSatish Balay - func  - routine to Create method context
2191a7e14dcfSSatish Balay 
2192e056e8ceSJacob Faibussowitsch   Example Usage:
2193a7e14dcfSSatish Balay .vb
2194441846f8SBarry Smith    TaoRegister("my_solver", MySolverCreate);
2195a7e14dcfSSatish Balay .ve
2196a7e14dcfSSatish Balay 
2197a7e14dcfSSatish Balay   Then, your solver can be chosen with the procedural interface via
2198*b44f4de4SBarry Smith .vb
2199*b44f4de4SBarry Smith   TaoSetType(tao, "my_solver")
2200*b44f4de4SBarry Smith .ve
2201a7e14dcfSSatish Balay   or at runtime via the option
2202*b44f4de4SBarry Smith .vb
2203*b44f4de4SBarry Smith   -tao_type my_solver
2204*b44f4de4SBarry Smith .ve
2205a7e14dcfSSatish Balay 
2206a7e14dcfSSatish Balay   Level: advanced
2207a7e14dcfSSatish Balay 
220867be906fSBarry Smith   Note:
220967be906fSBarry Smith   `TaoRegister()` may be called multiple times to add several user-defined solvers.
221067be906fSBarry Smith 
22111cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()`
221267be906fSBarry Smith @*/
2213d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao))
2214d71ae5a4SJacob Faibussowitsch {
2215a7e14dcfSSatish Balay   PetscFunctionBegin;
22169566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
2217835f2295SStefano Zampini   PetscCall(PetscFunctionListAdd(&TaoList, sname, func));
22183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2219a7e14dcfSSatish Balay }
2220a7e14dcfSSatish Balay 
2221a7e14dcfSSatish Balay /*@C
2222441846f8SBarry Smith   TaoRegisterDestroy - Frees the list of minimization solvers that were
222347450a7bSBarry Smith   registered by `TaoRegister()`.
2224a7e14dcfSSatish Balay 
2225a7e14dcfSSatish Balay   Not Collective
2226a7e14dcfSSatish Balay 
2227a7e14dcfSSatish Balay   Level: advanced
2228a7e14dcfSSatish Balay 
22291cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()`
2230a7e14dcfSSatish Balay @*/
2231d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegisterDestroy(void)
2232d71ae5a4SJacob Faibussowitsch {
2233a7e14dcfSSatish Balay   PetscFunctionBegin;
22349566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&TaoList));
2235441846f8SBarry Smith   TaoRegisterAllCalled = PETSC_FALSE;
22363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2237a7e14dcfSSatish Balay }
223847a47007SBarry Smith 
22398931d482SJason Sarich /*@
224047450a7bSBarry Smith   TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed
22418931d482SJason Sarich   at this time.
22428931d482SJason Sarich 
22438931d482SJason Sarich   Not Collective
22448931d482SJason Sarich 
22458931d482SJason Sarich   Input Parameter:
224647450a7bSBarry Smith . tao - the `Tao` context
22478931d482SJason Sarich 
22488931d482SJason Sarich   Output Parameter:
22498931d482SJason Sarich . iter - iteration number
22508931d482SJason Sarich 
22518931d482SJason Sarich   Notes:
22528931d482SJason Sarich   For example, during the computation of iteration 2 this would return 1.
22538931d482SJason Sarich 
22548931d482SJason Sarich   Level: intermediate
22558931d482SJason Sarich 
22561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()`
22578931d482SJason Sarich @*/
2258d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter)
2259d71ae5a4SJacob Faibussowitsch {
22608931d482SJason Sarich   PetscFunctionBegin;
22618931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
22624f572ea9SToby Isaac   PetscAssertPointer(iter, 2);
22638931d482SJason Sarich   *iter = tao->niter;
22643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22658931d482SJason Sarich }
22668931d482SJason Sarich 
22678931d482SJason Sarich /*@
226847450a7bSBarry Smith   TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient)
226979f5d8caSBarry Smith   at this time.
227079f5d8caSBarry Smith 
227179f5d8caSBarry Smith   Not Collective
227279f5d8caSBarry Smith 
227379f5d8caSBarry Smith   Input Parameter:
227447450a7bSBarry Smith . tao - the `Tao` context
227579f5d8caSBarry Smith 
227679f5d8caSBarry Smith   Output Parameter:
227779f5d8caSBarry Smith . value - the current value
227879f5d8caSBarry Smith 
227979f5d8caSBarry Smith   Level: intermediate
228079f5d8caSBarry Smith 
2281e056e8ceSJacob Faibussowitsch   Developer Notes:
228267be906fSBarry Smith   This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has
228347450a7bSBarry Smith   a different meaning. For some reason `Tao` sometimes calls the gradient the residual.
228479f5d8caSBarry Smith 
22851cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()`
228679f5d8caSBarry Smith @*/
2287d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value)
2288d71ae5a4SJacob Faibussowitsch {
228979f5d8caSBarry Smith   PetscFunctionBegin;
229079f5d8caSBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
22914f572ea9SToby Isaac   PetscAssertPointer(value, 2);
229279f5d8caSBarry Smith   *value = tao->residual;
22933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
229479f5d8caSBarry Smith }
229579f5d8caSBarry Smith 
229679f5d8caSBarry Smith /*@
22978931d482SJason Sarich   TaoSetIterationNumber - Sets the current iteration number.
22988931d482SJason Sarich 
2299c3339decSBarry Smith   Logically Collective
23008931d482SJason Sarich 
2301d8d19677SJose E. Roman   Input Parameters:
230247450a7bSBarry Smith + tao  - the `Tao` context
2303a2b725a8SWilliam Gropp - iter - iteration number
23048931d482SJason Sarich 
23058931d482SJason Sarich   Level: developer
23068931d482SJason Sarich 
23071cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
23088931d482SJason Sarich @*/
2309d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter)
2310d71ae5a4SJacob Faibussowitsch {
23118931d482SJason Sarich   PetscFunctionBegin;
23128931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
231394511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, iter, 2);
23149566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
23158931d482SJason Sarich   tao->niter = iter;
23169566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
23173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23188931d482SJason Sarich }
23198931d482SJason Sarich 
23208931d482SJason Sarich /*@
232147450a7bSBarry Smith   TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations
23228931d482SJason Sarich   completed. This number keeps accumulating if multiple solves
232347450a7bSBarry Smith   are called with the `Tao` object.
23248931d482SJason Sarich 
23258931d482SJason Sarich   Not Collective
23268931d482SJason Sarich 
23278931d482SJason Sarich   Input Parameter:
232847450a7bSBarry Smith . tao - the `Tao` context
23298931d482SJason Sarich 
23308931d482SJason Sarich   Output Parameter:
233147450a7bSBarry Smith . iter - number of iterations
23328931d482SJason Sarich 
23338931d482SJason Sarich   Level: intermediate
23348931d482SJason Sarich 
233520f4b53cSBarry Smith   Note:
233667be906fSBarry Smith   The total iteration count is updated after each solve, if there is a current
233747450a7bSBarry Smith   `TaoSolve()` in progress then those iterations are not included in the count
233867be906fSBarry Smith 
23391cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
23408931d482SJason Sarich @*/
2341d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter)
2342d71ae5a4SJacob Faibussowitsch {
23438931d482SJason Sarich   PetscFunctionBegin;
23448931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
23454f572ea9SToby Isaac   PetscAssertPointer(iter, 2);
23468931d482SJason Sarich   *iter = tao->ntotalits;
23473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23488931d482SJason Sarich }
23498931d482SJason Sarich 
23508931d482SJason Sarich /*@
23518931d482SJason Sarich   TaoSetTotalIterationNumber - Sets the current total iteration number.
23528931d482SJason Sarich 
2353c3339decSBarry Smith   Logically Collective
23548931d482SJason Sarich 
2355d8d19677SJose E. Roman   Input Parameters:
235647450a7bSBarry Smith + tao  - the `Tao` context
235747450a7bSBarry Smith - iter - the iteration number
23588931d482SJason Sarich 
23598931d482SJason Sarich   Level: developer
23608931d482SJason Sarich 
23611cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
23628931d482SJason Sarich @*/
2363d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter)
2364d71ae5a4SJacob Faibussowitsch {
23658931d482SJason Sarich   PetscFunctionBegin;
23668931d482SJason Sarich   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
236794511df7SStefano Zampini   PetscValidLogicalCollectiveInt(tao, iter, 2);
23689566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
23698931d482SJason Sarich   tao->ntotalits = iter;
23709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
23713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23728931d482SJason Sarich }
23738931d482SJason Sarich 
2374a7e14dcfSSatish Balay /*@
237547450a7bSBarry Smith   TaoSetConvergedReason - Sets the termination flag on a `Tao` object
2376a7e14dcfSSatish Balay 
2377c3339decSBarry Smith   Logically Collective
2378a7e14dcfSSatish Balay 
2379a7e14dcfSSatish Balay   Input Parameters:
238047450a7bSBarry Smith + tao    - the `Tao` context
238147450a7bSBarry Smith - reason - the `TaoConvergedReason`
2382a7e14dcfSSatish Balay 
2383a7e14dcfSSatish Balay   Level: intermediate
2384a7e14dcfSSatish Balay 
23851cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`
2386a7e14dcfSSatish Balay @*/
2387d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2388d71ae5a4SJacob Faibussowitsch {
2389a7e14dcfSSatish Balay   PetscFunctionBegin;
239094511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
239194511df7SStefano Zampini   PetscValidLogicalCollectiveEnum(tao, reason, 2);
2392a7e14dcfSSatish Balay   tao->reason = reason;
23933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2394a7e14dcfSSatish Balay }
2395a7e14dcfSSatish Balay 
2396a7e14dcfSSatish Balay /*@
239747450a7bSBarry Smith   TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped.
2398a7e14dcfSSatish Balay 
2399a7e14dcfSSatish Balay   Not Collective
2400a7e14dcfSSatish Balay 
2401a7e14dcfSSatish Balay   Input Parameter:
240247450a7bSBarry Smith . tao - the `Tao` solver context
2403a7e14dcfSSatish Balay 
2404a7e14dcfSSatish Balay   Output Parameter:
240547450a7bSBarry Smith . reason - value of `TaoConvergedReason`
2406a7e14dcfSSatish Balay 
2407a7e14dcfSSatish Balay   Level: intermediate
2408a7e14dcfSSatish Balay 
24091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()`
2410a7e14dcfSSatish Balay @*/
2411d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2412d71ae5a4SJacob Faibussowitsch {
2413a7e14dcfSSatish Balay   PetscFunctionBegin;
2414441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
24154f572ea9SToby Isaac   PetscAssertPointer(reason, 2);
2416a7e14dcfSSatish Balay   *reason = tao->reason;
24173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2418a7e14dcfSSatish Balay }
2419a7e14dcfSSatish Balay 
2420a7e14dcfSSatish Balay /*@
2421a7e14dcfSSatish Balay   TaoGetSolutionStatus - Get the current iterate, objective value,
242247450a7bSBarry Smith   residual, infeasibility, and termination from a `Tao` object
2423a7e14dcfSSatish Balay 
2424a7e14dcfSSatish Balay   Not Collective
2425a7e14dcfSSatish Balay 
2426f899ff85SJose E. Roman   Input Parameter:
242747450a7bSBarry Smith . tao - the `Tao` context
2428a7e14dcfSSatish Balay 
2429a7e14dcfSSatish Balay   Output Parameters:
2430e056e8ceSJacob Faibussowitsch + its    - the current iterate number (>=0)
2431a7e14dcfSSatish Balay . f      - the current function value
243258417fe7SBarry Smith . gnorm  - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2433a7e14dcfSSatish Balay . cnorm  - the infeasibility of the current solution with regard to the constraints.
2434a7e14dcfSSatish Balay . xdiff  - the step length or trust region radius of the most recent iterate.
243565ba42b6SBarry Smith - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING`
2436a7e14dcfSSatish Balay 
2437a7e14dcfSSatish Balay   Level: intermediate
2438a7e14dcfSSatish Balay 
243965ba42b6SBarry Smith   Notes:
244065ba42b6SBarry Smith   Tao returns the values set by the solvers in the routine `TaoMonitor()`.
2441a7e14dcfSSatish Balay 
244267be906fSBarry Smith   If any of the output arguments are set to `NULL`, no corresponding value will be returned.
2443a7e14dcfSSatish Balay 
24441cc06b55SBarry Smith .seealso: [](ch_tao), `TaoMonitor()`, `TaoGetConvergedReason()`
2445a7e14dcfSSatish Balay @*/
2446d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2447d71ae5a4SJacob Faibussowitsch {
2448a7e14dcfSSatish Balay   PetscFunctionBegin;
244994511df7SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2450a7e14dcfSSatish Balay   if (its) *its = tao->niter;
2451a7e14dcfSSatish Balay   if (f) *f = tao->fc;
2452a7e14dcfSSatish Balay   if (gnorm) *gnorm = tao->residual;
2453a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->cnorm;
2454a7e14dcfSSatish Balay   if (reason) *reason = tao->reason;
2455a7e14dcfSSatish Balay   if (xdiff) *xdiff = tao->step;
24563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2457a7e14dcfSSatish Balay }
2458a7e14dcfSSatish Balay 
2459cc4c1da9SBarry Smith /*@
246047450a7bSBarry Smith   TaoGetType - Gets the current `TaoType` being used in the `Tao` object
2461a7e14dcfSSatish Balay 
2462a7e14dcfSSatish Balay   Not Collective
2463a7e14dcfSSatish Balay 
2464a7e14dcfSSatish Balay   Input Parameter:
246547450a7bSBarry Smith . tao - the `Tao` solver context
2466a7e14dcfSSatish Balay 
2467a7e14dcfSSatish Balay   Output Parameter:
246847450a7bSBarry Smith . type - the `TaoType`
2469a7e14dcfSSatish Balay 
2470a7e14dcfSSatish Balay   Level: intermediate
2471a7e14dcfSSatish Balay 
24721cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoType`, `TaoSetType()`
2473a7e14dcfSSatish Balay @*/
2474d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetType(Tao tao, TaoType *type)
2475d71ae5a4SJacob Faibussowitsch {
2476a7e14dcfSSatish Balay   PetscFunctionBegin;
2477441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
24784f572ea9SToby Isaac   PetscAssertPointer(type, 2);
2479a7e14dcfSSatish Balay   *type = ((PetscObject)tao)->type_name;
24803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2481a7e14dcfSSatish Balay }
2482a7e14dcfSSatish Balay 
2483a7e14dcfSSatish Balay /*@C
2484a7e14dcfSSatish Balay   TaoMonitor - Monitor the solver and the current solution.  This
2485a7e14dcfSSatish Balay   routine will record the iteration number and residual statistics,
2486670c031eSStefano Zampini   and call any monitors specified by the user.
2487a7e14dcfSSatish Balay 
2488a7e14dcfSSatish Balay   Input Parameters:
248947450a7bSBarry Smith + tao        - the `Tao` context
2490a7e14dcfSSatish Balay . its        - the current iterate number (>=0)
2491a7e14dcfSSatish Balay . f          - the current objective function value
24921cb3f120SPierre Jolivet . res        - the gradient norm, square root of the duality gap, or other measure indicating distance from optimality.  This measure will be recorded and
2493a7e14dcfSSatish Balay           used for some termination tests.
2494a7e14dcfSSatish Balay . cnorm      - the infeasibility of the current solution with regard to the constraints.
2495a7e14dcfSSatish Balay - steplength - multiple of the step direction added to the previous iterate.
2496a7e14dcfSSatish Balay 
2497a7e14dcfSSatish Balay   Options Database Key:
2498a7e14dcfSSatish Balay . -tao_monitor - Use the default monitor, which prints statistics to standard output
2499a7e14dcfSSatish Balay 
2500a7e14dcfSSatish Balay   Level: developer
2501a7e14dcfSSatish Balay 
250210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoMonitorSet()`
2503a7e14dcfSSatish Balay @*/
2504d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength)
2505d71ae5a4SJacob Faibussowitsch {
250647a47007SBarry Smith   PetscInt i;
250747a47007SBarry Smith 
2508a7e14dcfSSatish Balay   PetscFunctionBegin;
2509441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2510a7e14dcfSSatish Balay   tao->fc       = f;
2511a7e14dcfSSatish Balay   tao->residual = res;
2512a7e14dcfSSatish Balay   tao->cnorm    = cnorm;
2513a7e14dcfSSatish Balay   tao->step     = steplength;
2514e52336cbSBarry Smith   if (!its) {
251594511df7SStefano Zampini     tao->cnorm0 = cnorm;
251694511df7SStefano Zampini     tao->gnorm0 = res;
2517a7e14dcfSSatish Balay   }
2518bfdcf5a2SStefano Zampini   PetscCall(VecLockReadPush(tao->solution));
251948a46eb9SPierre Jolivet   for (i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i]));
2520bfdcf5a2SStefano Zampini   PetscCall(VecLockReadPop(tao->solution));
25213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2522a7e14dcfSSatish Balay }
2523a7e14dcfSSatish Balay 
2524a7e14dcfSSatish Balay /*@
2525ae93cb3cSJason Sarich   TaoSetConvergenceHistory - Sets the array used to hold the convergence history.
2526a7e14dcfSSatish Balay 
2527c3339decSBarry Smith   Logically Collective
2528a7e14dcfSSatish Balay 
2529a7e14dcfSSatish Balay   Input Parameters:
253047450a7bSBarry Smith + tao   - the `Tao` solver context
2531a7e14dcfSSatish Balay . obj   - array to hold objective value history
2532a7e14dcfSSatish Balay . resid - array to hold residual history
2533a7e14dcfSSatish Balay . cnorm - array to hold constraint violation history
2534be1558d8SJason Sarich . lits  - integer array holds the number of linear iterations for each Tao iteration
253567be906fSBarry Smith . na    - size of `obj`, `resid`, and `cnorm`
253665ba42b6SBarry Smith - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero,
2537a7e14dcfSSatish Balay            else it continues storing new values for new minimizations after the old ones
2538a7e14dcfSSatish Balay 
253967be906fSBarry Smith   Level: intermediate
254067be906fSBarry Smith 
2541a7e14dcfSSatish Balay   Notes:
254247450a7bSBarry Smith   If set, `Tao` will fill the given arrays with the indicated
2543ae93cb3cSJason Sarich   information at each iteration.  If 'obj','resid','cnorm','lits' are
2544606f75f6SBarry Smith   *all* `NULL` then space (using size `na`, or 1000 if `na` is `PETSC_DECIDE`) is allocated for the history.
254567be906fSBarry Smith   If not all are `NULL`, then only the non-`NULL` information categories
2546ae93cb3cSJason Sarich   will be stored, the others will be ignored.
2547ae93cb3cSJason Sarich 
2548ae93cb3cSJason Sarich   Any convergence information after iteration number 'na' will not be stored.
2549a7e14dcfSSatish Balay 
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 
25541cc06b55SBarry Smith .seealso: [](ch_tao), `TaoGetConvergenceHistory()`
2555a7e14dcfSSatish Balay @*/
2556d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset)
2557d71ae5a4SJacob Faibussowitsch {
2558a7e14dcfSSatish Balay   PetscFunctionBegin;
2559441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
25604f572ea9SToby Isaac   if (obj) PetscAssertPointer(obj, 2);
25614f572ea9SToby Isaac   if (resid) PetscAssertPointer(resid, 3);
25624f572ea9SToby Isaac   if (cnorm) PetscAssertPointer(cnorm, 4);
25634f572ea9SToby Isaac   if (lits) PetscAssertPointer(lits, 5);
2564ae93cb3cSJason Sarich 
2565606f75f6SBarry Smith   if (na == PETSC_DECIDE || na == PETSC_CURRENT) na = 1000;
2566ae93cb3cSJason Sarich   if (!obj && !resid && !cnorm && !lits) {
25679566063dSJacob Faibussowitsch     PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits));
2568ae93cb3cSJason Sarich     tao->hist_malloc = PETSC_TRUE;
2569ae93cb3cSJason Sarich   }
2570ae93cb3cSJason Sarich 
2571a7e14dcfSSatish Balay   tao->hist_obj   = obj;
2572a7e14dcfSSatish Balay   tao->hist_resid = resid;
2573a7e14dcfSSatish Balay   tao->hist_cnorm = cnorm;
2574ae93cb3cSJason Sarich   tao->hist_lits  = lits;
2575a7e14dcfSSatish Balay   tao->hist_max   = na;
2576a7e14dcfSSatish Balay   tao->hist_reset = reset;
2577ae93cb3cSJason Sarich   tao->hist_len   = 0;
25783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2579a7e14dcfSSatish Balay }
2580a7e14dcfSSatish Balay 
2581a7e14dcfSSatish Balay /*@C
258265ba42b6SBarry Smith   TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history.
2583a7e14dcfSSatish Balay 
2584c3339decSBarry Smith   Collective
2585a7e14dcfSSatish Balay 
2586a7e14dcfSSatish Balay   Input Parameter:
258747450a7bSBarry Smith . tao - the `Tao` context
2588a7e14dcfSSatish Balay 
2589a7e14dcfSSatish Balay   Output Parameters:
2590a7e14dcfSSatish Balay + obj   - array used to hold objective value history
2591a7e14dcfSSatish Balay . resid - array used to hold residual history
2592a7e14dcfSSatish Balay . cnorm - array used to hold constraint violation history
2593ae93cb3cSJason Sarich . lits  - integer array used to hold linear solver iteration count
259467be906fSBarry Smith - nhist - size of `obj`, `resid`, `cnorm`, and `lits`
259567be906fSBarry Smith 
259667be906fSBarry Smith   Level: advanced
2597a7e14dcfSSatish Balay 
2598a7e14dcfSSatish Balay   Notes:
259965ba42b6SBarry Smith   This routine must be preceded by calls to `TaoSetConvergenceHistory()`
260065ba42b6SBarry Smith   and `TaoSolve()`, otherwise it returns useless information.
2601ae93cb3cSJason Sarich 
2602a7e14dcfSSatish Balay   This routine is useful, e.g., when running a code for purposes
2603a7e14dcfSSatish Balay   of accurate performance monitoring, when no I/O should be done
2604a7e14dcfSSatish Balay   during the section of code that is being timed.
2605a7e14dcfSSatish Balay 
2606e056e8ceSJacob Faibussowitsch   Fortran Notes:
260767be906fSBarry Smith   The calling sequence is
260867be906fSBarry Smith .vb
260967be906fSBarry Smith    call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr)
261067be906fSBarry Smith .ve
2611a3b724e8SBarry Smith   In other words this gets the current number of entries in the history. Access the history through the array you passed to `TaoSetConvergenceHistory()`
2612a7e14dcfSSatish Balay 
26131cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()`
2614a7e14dcfSSatish Balay @*/
2615d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist)
2616d71ae5a4SJacob Faibussowitsch {
2617a7e14dcfSSatish Balay   PetscFunctionBegin;
2618441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
2619a7e14dcfSSatish Balay   if (obj) *obj = tao->hist_obj;
2620a7e14dcfSSatish Balay   if (cnorm) *cnorm = tao->hist_cnorm;
2621a7e14dcfSSatish Balay   if (resid) *resid = tao->hist_resid;
26225b494b05SBarry Smith   if (lits) *lits = tao->hist_lits;
2623a7e14dcfSSatish Balay   if (nhist) *nhist = tao->hist_len;
26243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2625a7e14dcfSSatish Balay }
2626a7e14dcfSSatish Balay 
2627a7e14dcfSSatish Balay /*@
2628ce78bad3SBarry Smith   TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver that can be accessed later, for example in the
2629ce78bad3SBarry Smith   `Tao` callback functions with `TaoGetApplicationContext()`
2630a7e14dcfSSatish Balay 
2631c3339decSBarry Smith   Logically Collective
2632a7e14dcfSSatish Balay 
2633a7e14dcfSSatish Balay   Input Parameters:
263447450a7bSBarry Smith + tao - the `Tao` context
2635ce78bad3SBarry Smith - ctx - the user context
2636a7e14dcfSSatish Balay 
2637a7e14dcfSSatish Balay   Level: intermediate
2638a7e14dcfSSatish Balay 
2639ce78bad3SBarry Smith   Fortran Note:
2640ce78bad3SBarry Smith   This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this
2641ce78bad3SBarry Smith   function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `TaoGetApplicationContext()` for
2642ce78bad3SBarry Smith   an example.
2643ce78bad3SBarry Smith 
264442747ad1SJacob Faibussowitsch .seealso: [](ch_tao), `Tao`, `TaoGetApplicationContext()`
2645a7e14dcfSSatish Balay @*/
264649abdd8aSBarry Smith PetscErrorCode TaoSetApplicationContext(Tao tao, void *ctx)
2647d71ae5a4SJacob Faibussowitsch {
2648a7e14dcfSSatish Balay   PetscFunctionBegin;
2649441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
265049abdd8aSBarry Smith   tao->ctx = ctx;
26513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2652a7e14dcfSSatish Balay }
2653a7e14dcfSSatish Balay 
2654a7e14dcfSSatish Balay /*@
2655ce78bad3SBarry Smith   TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver provided with `TaoSetApplicationContext()`
2656a7e14dcfSSatish Balay 
2657a7e14dcfSSatish Balay   Not Collective
2658a7e14dcfSSatish Balay 
2659a7e14dcfSSatish Balay   Input Parameter:
266047450a7bSBarry Smith . tao - the `Tao` context
2661a7e14dcfSSatish Balay 
2662a7e14dcfSSatish Balay   Output Parameter:
2663ce78bad3SBarry Smith . ctx - a pointer to the user context
2664a7e14dcfSSatish Balay 
2665a7e14dcfSSatish Balay   Level: intermediate
2666a7e14dcfSSatish Balay 
2667ce78bad3SBarry Smith   Fortran Notes:
2668ce78bad3SBarry Smith   This only works when the context is a Fortran derived type (it cannot be a `PetscObject`) and you **must** write a Fortran interface definition for this
2669ce78bad3SBarry Smith   function that tells the Fortran compiler the derived data type that is returned as the `ctx` argument. For example,
2670ce78bad3SBarry Smith .vb
2671ce78bad3SBarry Smith   Interface TaoGetApplicationContext
2672ce78bad3SBarry Smith     Subroutine TaoGetApplicationContext(tao,ctx,ierr)
2673ce78bad3SBarry Smith   #include <petsc/finclude/petsctao.h>
2674ce78bad3SBarry Smith       use petsctao
2675ce78bad3SBarry Smith       Tao tao
2676ce78bad3SBarry Smith       type(tUsertype), pointer :: ctx
2677ce78bad3SBarry Smith       PetscErrorCode ierr
2678ce78bad3SBarry Smith     End Subroutine
2679ce78bad3SBarry Smith   End Interface TaoGetApplicationContext
2680ce78bad3SBarry Smith .ve
2681ce78bad3SBarry Smith 
2682bfe80ac4SPierre Jolivet   The prototype for `ctx` must be
2683ce78bad3SBarry Smith .vb
2684ce78bad3SBarry Smith   type(tUsertype), pointer :: ctx
2685ce78bad3SBarry Smith .ve
2686ce78bad3SBarry Smith 
26871cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetApplicationContext()`
2688a7e14dcfSSatish Balay @*/
2689ce78bad3SBarry Smith PetscErrorCode TaoGetApplicationContext(Tao tao, PeCtx ctx)
2690d71ae5a4SJacob Faibussowitsch {
2691a7e14dcfSSatish Balay   PetscFunctionBegin;
2692441846f8SBarry Smith   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
269349abdd8aSBarry Smith   PetscAssertPointer(ctx, 2);
269449abdd8aSBarry Smith   *(void **)ctx = tao->ctx;
26953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2696a7e14dcfSSatish Balay }
2697a9603a14SPatrick Farrell 
2698a9603a14SPatrick Farrell /*@
2699a81c3919SBarry Smith   TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient in some of the `Tao` algorithms
2700a9603a14SPatrick Farrell 
2701c3339decSBarry Smith   Collective
2702a9603a14SPatrick Farrell 
2703a9603a14SPatrick Farrell   Input Parameters:
270447450a7bSBarry Smith + tao - the `Tao` context
270565ba42b6SBarry Smith - M   - matrix that defines the norm
2706a9603a14SPatrick Farrell 
2707a9603a14SPatrick Farrell   Level: beginner
2708a9603a14SPatrick Farrell 
27091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()`
2710a9603a14SPatrick Farrell @*/
2711d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M)
2712d71ae5a4SJacob Faibussowitsch {
2713a9603a14SPatrick Farrell   PetscFunctionBegin;
2714a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
271594511df7SStefano Zampini   PetscValidHeaderSpecific(M, MAT_CLASSID, 2);
27169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)M));
27179566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&tao->gradient_norm));
27189566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&tao->gradient_norm_tmp));
2719a9603a14SPatrick Farrell   tao->gradient_norm = M;
27209566063dSJacob Faibussowitsch   PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp));
27213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2722a9603a14SPatrick Farrell }
2723a9603a14SPatrick Farrell 
2724a9603a14SPatrick Farrell /*@
2725a81c3919SBarry Smith   TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient in some of the `Tao` algorithms
2726a9603a14SPatrick Farrell 
2727a9603a14SPatrick Farrell   Not Collective
2728a9603a14SPatrick Farrell 
2729a9603a14SPatrick Farrell   Input Parameter:
273047450a7bSBarry Smith . tao - the `Tao` context
2731a9603a14SPatrick Farrell 
2732a9603a14SPatrick Farrell   Output Parameter:
2733a9603a14SPatrick Farrell . M - gradient norm
2734a9603a14SPatrick Farrell 
2735a9603a14SPatrick Farrell   Level: beginner
2736a9603a14SPatrick Farrell 
27371cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()`
2738a9603a14SPatrick Farrell @*/
2739d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M)
2740d71ae5a4SJacob Faibussowitsch {
2741a9603a14SPatrick Farrell   PetscFunctionBegin;
2742a9603a14SPatrick Farrell   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
27434f572ea9SToby Isaac   PetscAssertPointer(M, 2);
2744a9603a14SPatrick Farrell   *M = tao->gradient_norm;
27453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2746a9603a14SPatrick Farrell }
2747a9603a14SPatrick Farrell 
2748cc4c1da9SBarry Smith /*@
274947450a7bSBarry Smith   TaoGradientNorm - Compute the norm using the `NormType`, the user has selected
2750a9603a14SPatrick Farrell 
2751c3339decSBarry Smith   Collective
2752a9603a14SPatrick Farrell 
2753d8d19677SJose E. Roman   Input Parameters:
275447450a7bSBarry Smith + tao      - the `Tao` context
2755a81c3919SBarry Smith . gradient - the gradient
27563b242c63SJacob Faibussowitsch - type     - the norm type
2757a9603a14SPatrick Farrell 
2758a9603a14SPatrick Farrell   Output Parameter:
2759a9603a14SPatrick Farrell . gnorm - the gradient norm
2760a9603a14SPatrick Farrell 
276147450a7bSBarry Smith   Level: advanced
2762a9603a14SPatrick Farrell 
2763a81c3919SBarry Smith   Note:
2764a81c3919SBarry Smith   If `TaoSetGradientNorm()` has been set and `type` is `NORM_2` then the norm provided with `TaoSetGradientNorm()` is used.
2765a81c3919SBarry Smith 
2766a81c3919SBarry Smith   Developer Notes:
2767a81c3919SBarry Smith   Should be named `TaoComputeGradientNorm()`.
2768a81c3919SBarry Smith 
2769a81c3919SBarry Smith   The usage is a bit confusing, with `TaoSetGradientNorm()` plus `NORM_2` resulting in the computation of the user provided
2770a81c3919SBarry Smith   norm, perhaps a refactorization is in order.
2771a81c3919SBarry Smith 
27721cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()`
2773a9603a14SPatrick Farrell @*/
2774d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm)
2775d71ae5a4SJacob Faibussowitsch {
2776a9603a14SPatrick Farrell   PetscFunctionBegin;
27778854b543SStefano Zampini   PetscValidHeaderSpecific(tao, TAO_CLASSID, 1);
27788854b543SStefano Zampini   PetscValidHeaderSpecific(gradient, VEC_CLASSID, 2);
27798854b543SStefano Zampini   PetscValidLogicalCollectiveEnum(tao, type, 3);
27804f572ea9SToby Isaac   PetscAssertPointer(gnorm, 4);
2781a9603a14SPatrick Farrell   if (tao->gradient_norm) {
2782680e2bc7SPatrick Farrell     PetscScalar gnorms;
2783680e2bc7SPatrick Farrell 
27843c859ba3SBarry 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.");
27859566063dSJacob Faibussowitsch     PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp));
27869566063dSJacob Faibussowitsch     PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms));
27877bb79937SPatrick Farrell     *gnorm = PetscRealPart(PetscSqrtScalar(gnorms));
2788a9603a14SPatrick Farrell   } else {
27899566063dSJacob Faibussowitsch     PetscCall(VecNorm(gradient, type, gnorm));
2790a9603a14SPatrick Farrell   }
27913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2792a9603a14SPatrick Farrell }
2793a9603a14SPatrick Farrell 
2794e882e171SHong Zhang /*@C
2795a81c3919SBarry Smith   TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorSolutionDraw()`
2796a9603a14SPatrick Farrell 
2797c3339decSBarry Smith   Collective
2798e882e171SHong Zhang 
27992fe279fdSBarry Smith   Input Parameters:
28002fe279fdSBarry Smith + comm     - the communicator to share the context
28012fe279fdSBarry Smith . host     - the name of the X Windows host that will display the monitor
28022fe279fdSBarry Smith . label    - the label to put at the top of the display window
28032fe279fdSBarry Smith . x        - the horizontal coordinate of the lower left corner of the window to open
28042fe279fdSBarry Smith . y        - the vertical coordinate of the lower left corner of the window to open
28052fe279fdSBarry Smith . m        - the width of the window
28062fe279fdSBarry Smith . n        - the height of the window
28072fe279fdSBarry Smith - howoften - how many `Tao` iterations between displaying the monitor information
28082fe279fdSBarry Smith 
2809d5b43468SJose E. Roman   Output Parameter:
2810e882e171SHong Zhang . ctx - the monitor context
2811e882e171SHong Zhang 
2812a81c3919SBarry Smith   Options Database Keys:
2813a81c3919SBarry Smith + -tao_monitor_solution_draw - use `TaoMonitorSolutionDraw()` to monitor the solution
2814a81c3919SBarry Smith - -tao_draw_solution_initial - show initial guess as well as current solution
2815e882e171SHong Zhang 
2816e882e171SHong Zhang   Level: intermediate
2817e882e171SHong Zhang 
2818a81c3919SBarry Smith   Note:
2819a81c3919SBarry Smith   The context this creates, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()`
2820a81c3919SBarry Smith   are passed to `TaoMonitorSet()`.
2821a81c3919SBarry Smith 
28221cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()`
2823e882e171SHong Zhang @*/
2824d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx)
2825d71ae5a4SJacob Faibussowitsch {
2826e882e171SHong Zhang   PetscFunctionBegin;
28279566063dSJacob Faibussowitsch   PetscCall(PetscNew(ctx));
28289566063dSJacob Faibussowitsch   PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer));
28299566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetFromOptions((*ctx)->viewer));
2830e882e171SHong Zhang   (*ctx)->howoften = howoften;
28313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2832e882e171SHong Zhang }
2833e882e171SHong Zhang 
2834e882e171SHong Zhang /*@C
2835a81c3919SBarry Smith   TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorSolutionDraw()`
2836e882e171SHong Zhang 
2837c3339decSBarry Smith   Collective
2838e882e171SHong Zhang 
283947450a7bSBarry Smith   Input Parameter:
2840e056e8ceSJacob Faibussowitsch . ictx - the monitor context
2841e882e171SHong Zhang 
2842e882e171SHong Zhang   Level: intermediate
2843e882e171SHong Zhang 
2844a81c3919SBarry Smith   Note:
2845a81c3919SBarry Smith   This is passed to `TaoMonitorSet()` as the final argument, along with `TaoMonitorSolutionDraw()`, and the context
2846a81c3919SBarry Smith   obtained with `TaoMonitorDrawCtxCreate()`.
2847a81c3919SBarry Smith 
2848a81c3919SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorSolutionDraw()`
2849e882e171SHong Zhang @*/
2850d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx)
2851d71ae5a4SJacob Faibussowitsch {
2852e882e171SHong Zhang   PetscFunctionBegin;
28539566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&(*ictx)->viewer));
28549566063dSJacob Faibussowitsch   PetscCall(PetscFree(*ictx));
28553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2856e882e171SHong Zhang }
2857