Actual source code: taosolver.c
1: #include <petsc/private/taoimpl.h>
2: #include <petsc/private/snesimpl.h>
3: #include <petsc/private/kspimpl.h>
4: #include <petscdmshell.h>
6: PetscBool TaoRegisterAllCalled = PETSC_FALSE;
7: PetscFunctionList TaoList = NULL;
9: PetscClassId TAO_CLASSID = 0;
11: PetscLogEvent TAO_Solve;
12: PetscLogEvent TAO_ResidualEval;
13: PetscLogEvent TAO_JacobianEval;
14: PetscLogEvent TAO_ConstraintsEval;
16: const char *const TaoSubsetTypes[] = {"subvec", "mask", "matrixfree", "TaoSubsetType", "TAO_SUBSET_", NULL};
18: struct _n_TaoMonitorDrawCtx {
19: PetscViewer viewer;
20: PetscInt howoften; /* when > 0 uses iteration % howoften, when negative only final solution plotted */
21: };
23: static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, PetscCtx ctx)
24: {
25: Tao tao = (Tao)ctx;
26: SNES snes_ewdummy = tao->snes_ewdummy;
28: PetscFunctionBegin;
29: if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS);
30: /* populate snes_ewdummy struct values used in KSPPreSolve_SNESEW */
31: snes_ewdummy->vec_func = b;
32: snes_ewdummy->rtol = tao->gttol;
33: snes_ewdummy->iter = tao->niter;
34: PetscCall(VecNorm(b, NORM_2, &snes_ewdummy->norm));
35: PetscCall(KSPPreSolve_SNESEW(ksp, b, x, snes_ewdummy));
36: snes_ewdummy->vec_func = NULL;
37: PetscFunctionReturn(PETSC_SUCCESS);
38: }
40: static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, PetscCtx ctx)
41: {
42: Tao tao = (Tao)ctx;
43: SNES snes_ewdummy = tao->snes_ewdummy;
45: PetscFunctionBegin;
46: if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS);
47: PetscCall(KSPPostSolve_SNESEW(ksp, b, x, snes_ewdummy));
48: PetscFunctionReturn(PETSC_SUCCESS);
49: }
51: static PetscErrorCode TaoSetUpEW_Private(Tao tao)
52: {
53: SNESKSPEW *kctx;
54: const char *ewprefix;
56: PetscFunctionBegin;
57: if (!tao->ksp) PetscFunctionReturn(PETSC_SUCCESS);
58: if (tao->ksp_ewconv) {
59: if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao), &tao->snes_ewdummy));
60: tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE;
62: tao->ksp->presolve_ew = KSPPreSolve_TAOEW_Private;
63: tao->ksp->prectx_ew = tao;
64: tao->ksp->postsolve_ew = KSPPostSolve_TAOEW_Private;
65: tao->ksp->postctx_ew = tao;
67: PetscCall(KSPGetOptionsPrefix(tao->ksp, &ewprefix));
68: kctx = (SNESKSPEW *)tao->snes_ewdummy->kspconvctx;
69: PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_FALSE, PetscObjectComm((PetscObject)tao), ewprefix));
70: } else PetscCall(SNESDestroy(&tao->snes_ewdummy));
71: PetscFunctionReturn(PETSC_SUCCESS);
72: }
74: /*@
75: TaoParametersInitialize - Sets the base defaults for parameters in `tao`, updating a parameter's current value when it matches its previously recorded default.
77: Logically collective
79: Input Parameter:
80: . tao - the `Tao` object
82: Level: developer
84: Notes:
86: The base defaults are the non-type-specific values established when the `Tao` is created. A `TaoType` constructor may subsequently replace them with type-specific defaults.
88: Developer Notes:
90: `TaoCreate()` calls this routine to establish the base defaults. `TaoSetType()` calls it before constructing a new `TaoType`, so the recorded defaults associated with the previous type are replaced before the new type installs its own defaults.
92: Default tracking is based on value equality, not on whether a setter was called. Consequently, an explicitly assigned value that equals the recorded default may be updated when the type changes.
94: .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `PetscObjectParameterSetDefault()`
95: @*/
96: PetscErrorCode TaoParametersInitialize(Tao tao)
97: {
98: PetscObjectParameterSetDefault(tao, max_it, 10000);
99: PetscObjectParameterSetDefault(tao, max_funcs, PETSC_UNLIMITED);
100: PetscObjectParameterSetDefault(tao, gatol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
101: PetscObjectParameterSetDefault(tao, grtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
102: PetscObjectParameterSetDefault(tao, crtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
103: PetscObjectParameterSetDefault(tao, catol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8);
104: PetscObjectParameterSetDefault(tao, gttol, 0.0);
105: PetscObjectParameterSetDefault(tao, steptol, 0.0);
106: PetscObjectParameterSetDefault(tao, fmin, PETSC_NINFINITY);
107: PetscObjectParameterSetDefault(tao, trust0, PETSC_INFINITY);
108: return PETSC_SUCCESS;
109: }
111: /*@
112: TaoCreate - Creates a Tao solver
114: Collective
116: Input Parameter:
117: . comm - MPI communicator
119: Output Parameter:
120: . newtao - the new `Tao` context
122: Options Database Key:
123: . -tao_type - select which method Tao should use
125: Level: beginner
127: .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TaoSetFromOptions()`, `TaoSetType()`
128: @*/
129: PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao)
130: {
131: Tao tao;
133: PetscFunctionBegin;
134: PetscAssertPointer(newtao, 2);
135: PetscCall(TaoInitializePackage());
136: PetscCall(TaoLineSearchInitializePackage());
138: PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView));
139: PetscCall(TaoParametersInitialize(tao));
140: tao->hist_reset = PETSC_TRUE;
142: tao->ops->convergencetest = TaoDefaultConvergenceTest;
144: PetscCall(TaoTermCreateCallbacks(tao, &tao->callbacks));
145: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao->callbacks, "callbacks_"));
146: PetscCall(TaoTermMappingSetData(&tao->objective_term, NULL, 1.0, tao->callbacks, NULL));
147: PetscCall(TaoResetStatistics(tao));
148: *newtao = tao;
149: PetscFunctionReturn(PETSC_SUCCESS);
150: }
152: /*@
153: TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u
155: Collective
157: Input Parameter:
158: . tao - the `Tao` context
160: Level: beginner
162: Notes:
163: The user must set up the `Tao` object with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`.
165: You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or
166: why it failed.
168: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()`
169: @*/
170: PetscErrorCode TaoSolve(Tao tao)
171: {
172: static PetscBool set = PETSC_FALSE;
174: PetscFunctionBegin;
176: PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n"
177: "title = {Toolkit for Advanced Optimization (TAO) Users Manual},\n"
178: "author = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n"
179: "Institution = {Argonne National Laboratory},\n"
180: "Year = 2014,\n"
181: "Number = {ANL/MCS-TM-322 - Revision 3.5},\n"
182: "url = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n",
183: &set));
184: tao->header_printed = PETSC_FALSE;
185: PetscCall(TaoSetUp(tao));
186: PetscCall(TaoResetStatistics(tao));
187: if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch));
189: PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0));
190: PetscTryTypeMethod(tao, solve);
191: PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0));
193: PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution"));
195: tao->ntotalits += tao->niter;
197: if (tao->printreason) {
198: PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm);
200: PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel));
201: if (tao->reason > 0) {
202: if (((PetscObject)tao)->prefix) {
203: PetscCall(PetscViewerASCIIPrintf(viewer, "TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter));
204: } else {
205: PetscCall(PetscViewerASCIIPrintf(viewer, "TAO solve converged due to %s iterations %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter));
206: }
207: } else {
208: if (((PetscObject)tao)->prefix) {
209: PetscCall(PetscViewerASCIIPrintf(viewer, "TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter));
210: } else {
211: PetscCall(PetscViewerASCIIPrintf(viewer, "TAO solve did not converge due to %s iteration %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter));
212: }
213: }
214: PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel));
215: }
216: PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view"));
217: PetscFunctionReturn(PETSC_SUCCESS);
218: }
220: /*@
221: TaoSetUp - Sets up the internal data structures for the later use
222: of a Tao solver
224: Collective
226: Input Parameter:
227: . tao - the `Tao` context
229: Level: advanced
231: Note:
232: The user will not need to explicitly call `TaoSetUp()`, as it will
233: automatically be called in `TaoSolve()`. However, if the user
234: desires to call it explicitly, it should come after `TaoCreate()`
235: and any TaoSetSomething() routines, but before `TaoSolve()`.
237: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
238: @*/
239: PetscErrorCode TaoSetUp(Tao tao)
240: {
241: PetscFunctionBegin;
243: if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
244: PetscCall(TaoSetUpEW_Private(tao));
245: PetscCall(TaoTermMappingSetUp(&tao->objective_term));
246: if (!tao->solution) PetscCall(TaoTermMappingCreateSolutionVec(&tao->objective_term, &tao->solution));
247: PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution()");
248: if (tao->uses_gradient && !tao->gradient) PetscCall(VecDuplicate(tao->solution, &tao->gradient));
249: if (tao->uses_hessian_matrices) {
250: // TaoSetHessian has been called, but as terms have been added,
251: // subterms' Hessian and PtAP routines, if needed, have to be created
252: // TODO Function to set TAOTERMSUM's Hessian.
253: if (!tao->hessian) {
254: PetscBool is_defined;
256: // TAOTERMSUM's Hessian will follow layout and type of first term's Hessian
257: PetscCall(TaoTermIsCreateHessianMatricesDefined(tao->objective_term.term, &is_defined));
258: if (is_defined) PetscCall(TaoTermMappingCreateHessianMatrices(&tao->objective_term, &tao->hessian, &tao->hessian_pre));
259: }
260: PetscCheck(tao->hessian, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetHessian()");
261: }
262: PetscTryTypeMethod(tao, setup);
263: tao->setupcalled = PETSC_TRUE;
264: PetscFunctionReturn(PETSC_SUCCESS);
265: }
267: /*@
268: TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()`
270: Collective
272: Input Parameter:
273: . tao - the `Tao` context
275: Level: beginner
277: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
278: @*/
279: PetscErrorCode TaoDestroy(Tao *tao)
280: {
281: PetscFunctionBegin;
282: if (!*tao) PetscFunctionReturn(PETSC_SUCCESS);
284: if (--((PetscObject)*tao)->refct > 0) {
285: *tao = NULL;
286: PetscFunctionReturn(PETSC_SUCCESS);
287: }
289: PetscTryTypeMethod(*tao, destroy);
290: PetscCall(TaoTermMappingReset(&(*tao)->objective_term));
291: PetscCall(VecDestroy(&(*tao)->objective_parameters));
292: PetscCall(TaoTermDestroy(&(*tao)->callbacks));
293: PetscCall(DMDestroy(&(*tao)->dm));
294: PetscCall(KSPDestroy(&(*tao)->ksp));
295: PetscCall(SNESDestroy(&(*tao)->snes_ewdummy));
296: PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch));
298: if ((*tao)->ops->convergencedestroy) {
299: PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP));
300: PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
301: }
302: PetscCall(VecDestroy(&(*tao)->solution));
303: PetscCall(VecDestroy(&(*tao)->gradient));
304: PetscCall(VecDestroy(&(*tao)->ls_res));
306: if ((*tao)->gradient_norm) {
307: PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm));
308: PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp));
309: }
311: PetscCall(VecDestroy(&(*tao)->XL));
312: PetscCall(VecDestroy(&(*tao)->XU));
313: PetscCall(VecDestroy(&(*tao)->IL));
314: PetscCall(VecDestroy(&(*tao)->IU));
315: PetscCall(VecDestroy(&(*tao)->DE));
316: PetscCall(VecDestroy(&(*tao)->DI));
317: PetscCall(VecDestroy(&(*tao)->constraints));
318: PetscCall(VecDestroy(&(*tao)->constraints_equality));
319: PetscCall(VecDestroy(&(*tao)->constraints_inequality));
320: PetscCall(VecDestroy(&(*tao)->stepdirection));
321: PetscCall(MatDestroy(&(*tao)->hessian_pre));
322: PetscCall(MatDestroy(&(*tao)->hessian));
323: PetscCall(MatDestroy(&(*tao)->ls_jac));
324: PetscCall(MatDestroy(&(*tao)->ls_jac_pre));
325: PetscCall(MatDestroy(&(*tao)->jacobian_pre));
326: PetscCall(MatDestroy(&(*tao)->jacobian));
327: PetscCall(MatDestroy(&(*tao)->jacobian_state_pre));
328: PetscCall(MatDestroy(&(*tao)->jacobian_state));
329: PetscCall(MatDestroy(&(*tao)->jacobian_state_inv));
330: PetscCall(MatDestroy(&(*tao)->jacobian_design));
331: PetscCall(MatDestroy(&(*tao)->jacobian_equality));
332: PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre));
333: PetscCall(MatDestroy(&(*tao)->jacobian_inequality));
334: PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre));
335: PetscCall(ISDestroy(&(*tao)->state_is));
336: PetscCall(ISDestroy(&(*tao)->design_is));
337: PetscCall(VecDestroy(&(*tao)->res_weights_v));
338: PetscCall(TaoMonitorCancel(*tao));
339: if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits));
340: if ((*tao)->res_weights_n) {
341: PetscCall(PetscFree((*tao)->res_weights_rows));
342: PetscCall(PetscFree((*tao)->res_weights_cols));
343: PetscCall(PetscFree((*tao)->res_weights_w));
344: }
345: PetscCall(PetscHeaderDestroy(tao));
346: PetscFunctionReturn(PETSC_SUCCESS);
347: }
349: /*@
350: TaoKSPSetUseEW - Sets `SNES` to use Eisenstat-Walker method {cite}`ew96` for computing relative tolerance for linear solvers.
352: Logically Collective
354: Input Parameters:
355: + tao - Tao context
356: - flag - `PETSC_TRUE` or `PETSC_FALSE`
358: Level: advanced
360: Note:
361: See `SNESKSPSetUseEW()` for customization details.
363: .seealso: [](ch_tao), `Tao`, `SNESKSPSetUseEW()`
364: @*/
365: PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag)
366: {
367: PetscFunctionBegin;
370: tao->ksp_ewconv = flag;
371: PetscFunctionReturn(PETSC_SUCCESS);
372: }
374: /*@
375: TaoMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
377: Collective
379: Input Parameters:
380: + tao - `Tao` object you wish to monitor
381: . name - the monitor type one is seeking
382: . help - message indicating what monitoring is done
383: . manual - manual page for the monitor
384: - monitor - the monitor function, this must use a `PetscViewerFormat` as its context
386: Level: developer
388: .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `PetscOptionsCreateViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
389: `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`,
390: `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
391: `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
392: `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
393: `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
394: `PetscOptionsFList()`, `PetscOptionsEList()`
395: @*/
396: PetscErrorCode TaoMonitorSetFromOptions(Tao tao, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(Tao, PetscViewerAndFormat *))
397: {
398: PetscViewer viewer;
399: PetscViewerFormat format;
400: PetscBool flg;
402: PetscFunctionBegin;
403: PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)tao), ((PetscObject)tao)->options, ((PetscObject)tao)->prefix, name, &viewer, &format, &flg));
404: if (flg) {
405: PetscViewerAndFormat *vf;
406: char interval_key[1024];
408: PetscCall(PetscSNPrintf(interval_key, sizeof interval_key, "%s_interval", name));
409: PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf));
410: vf->view_interval = 1;
411: PetscCall(PetscOptionsGetInt(((PetscObject)tao)->options, ((PetscObject)tao)->prefix, interval_key, &vf->view_interval, NULL));
413: PetscCall(PetscViewerDestroy(&viewer));
414: PetscCall(TaoMonitorSet(tao, (PetscErrorCode (*)(Tao, PetscCtx))monitor, vf, (PetscCtxDestroyFn *)PetscViewerAndFormatDestroy));
415: }
416: PetscFunctionReturn(PETSC_SUCCESS);
417: }
419: /*@
420: TaoSetFromOptions - Sets various Tao parameters from the options database
422: Collective
424: Input Parameter:
425: . tao - the `Tao` solver context
427: Options Database Keys:
428: + -tao_type type - The algorithm that Tao uses (lmvm, nls, etc.)
429: . -tao_gatol gatol - absolute error tolerance for ||gradient||
430: . -tao_grtol grtol - relative error tolerance for ||gradient||
431: . -tao_gttol gttol - reduction of ||gradient|| relative to initial gradient
432: . -tao_max_it max - sets maximum number of iterations
433: . -tao_max_funcs max - sets maximum number of function evaluations
434: . -tao_fmin fmin - stop if function value reaches `fmin`
435: . -tao_steptol tol - stop if trust region radius less than `tol`
436: . -tao_trust0 t - initial trust region radius
437: . -tao_view_solution - view the solution at the end of the optimization process
438: . -tao_monitor - prints function value and residual norm at each iteration
439: . -tao_monitor_interval interval - run the default monitor every `interval` iterations, and the last iteration
440: . -tao_monitor_constraint_norm [ascii][:filename] - prints objective value, gradient, and constraint norm at each iteration
441: . -tao_monitor_constraint_norm_interval interval - run the constraint norm monitor every `interval` iterations, and the last iteration
442: . -tao_monitor_globalization - prints information about the globalization at each iteration
443: . -tao_monitor_globalization_interval interval - run the globalization norm monitor every `interval` iterations, and the last iteration
444: . -tao_monitor_solution [viewertype][:filename][:viewerformat] - view solution vector at each iteration
445: . -tao_monitor_solution_interval interval - run the solution monitor every `interval` iterations, and the last iteration
446: . -tao_monitor_residual [viewertype][:filename][:viewerformat] - view least-squares residual vector at each iteration
447: . -tao_monitor_residual_interval interval - run the least-squares residual monitor every `interval` iterations, and the last iteration
448: . -tao_monitor_step [viewertype][:filename][:viewerformat] - view step vector at each iteration
449: . -tao_monitor_step_interval interval - run the step monitor every `interval` iterations, and the last iteration
450: . -tao_monitor_gradient [viewertype][:filename][:viewerformat] - view gradient vector at each iteration
451: . -tao_monitor_gradient_interval interval - run the gradient monitor every `interval` iterations, and the last iteration
452: . -tao_monitor_solution_draw - graphically view solution vector at each iteration
453: . -tao_monitor_solution_draw_interval interval - run the solution draw monitor every `interval` iterations, and the last iteration
454: . -tao_monitor_step_draw - graphically view step vector at each iteration
455: . -tao_monitor_step_draw_interval interval - run the step draw monitor every `interval` iterations, and the last iteration
456: . -tao_monitor_gradient_draw - graphically view gradient at each iteration
457: . -tao_monitor_gradient_draw_interval interval - run the gradient draw monitor every `interval` iterations, and the last iteration
458: . -tao_monitor_cancel - cancels all monitors (except those set with command line)
459: . -tao_fd_gradient - use gradient computed with finite differences
460: . -tao_fd_hessian - use hessian computed with finite differences
461: . -tao_mf_hessian - use matrix-free Hessian computed with finite differences
462: . -tao_recycle_history - enable recycling/re-using information from the previous `TaoSolve()` call for some algorithms
463: . -tao_subset_type (subvec|mask|matrixfree) - the method to use for subsetting in active-set methods, the default is `subvec`
464: . -tao_ksp_ew - use Eisenstat-Walker linear system convergence test
465: . -tao_view - prints information about the Tao after solving
466: . -tao_converged_reason - prints the reason Tao stopped iterating
467: - -tao_add_terms - takes a comma-separated list of up to 16 options prefixes, a `TaoTerm` will be created for each and added to the objective function
469: Level: beginner
471: Notes:
472: To see all options, run your program with the `-help` option or consult the
473: user's manual. Should be called after `TaoCreate()` but before `TaoSolve()`.
475: The `-tao_add_terms` option accepts at most 16 prefixes.
477: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
478: @*/
479: PetscErrorCode TaoSetFromOptions(Tao tao)
480: {
481: TaoType default_type = TAOLMVM;
482: char type[256];
483: PetscBool flg, found;
484: MPI_Comm comm;
485: PetscReal catol, crtol, gatol, grtol, gttol;
487: PetscFunctionBegin;
489: PetscCall(PetscObjectGetComm((PetscObject)tao, &comm));
491: if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name;
493: PetscObjectOptionsBegin((PetscObject)tao);
494: /* Check for type from options */
495: PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg));
496: if (flg) PetscCall(TaoSetType(tao, type));
497: else if (!((PetscObject)tao)->type_name) PetscCall(TaoSetType(tao, default_type));
499: /* Tao solvers do not set the prefix, set it here if not yet done
500: We do it after SetType since solver may have been changed */
501: if (tao->linesearch) {
502: const char *prefix;
503: PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix));
504: if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)tao)->prefix));
505: }
507: catol = tao->catol;
508: crtol = tao->crtol;
509: PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &catol, NULL));
510: PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &crtol, NULL));
511: PetscCall(TaoSetConstraintTolerances(tao, catol, crtol));
513: gatol = tao->gatol;
514: grtol = tao->grtol;
515: gttol = tao->gttol;
516: PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &gatol, NULL));
517: PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &grtol, NULL));
518: 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, >tol, NULL));
519: PetscCall(TaoSetTolerances(tao, gatol, grtol, gttol));
521: PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg));
522: if (flg) PetscCall(TaoSetMaximumIterations(tao, tao->max_it));
524: PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg));
525: if (flg) PetscCall(TaoSetMaximumFunctionEvaluations(tao, tao->max_funcs));
527: PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, NULL));
528: PetscCall(PetscOptionsBoundedReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, NULL, 0));
529: PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetInitialTrustRegionRadius", tao->trust0, &tao->trust0, &flg));
530: if (flg) PetscCall(TaoSetInitialTrustRegionRadius(tao, tao->trust0));
532: PetscCall(PetscOptionsDeprecated("-tao_solution_monitor", "-tao_monitor_solution", "3.21", NULL));
533: PetscCall(PetscOptionsDeprecated("-tao_gradient_monitor", "-tao_monitor_gradient", "3.21", NULL));
534: PetscCall(PetscOptionsDeprecated("-tao_stepdirection_monitor", "-tao_monitor_step", "3.21", NULL));
535: PetscCall(PetscOptionsDeprecated("-tao_residual_monitor", "-tao_monitor_residual", "3.21", NULL));
536: PetscCall(PetscOptionsDeprecated("-tao_smonitor", "-tao_monitor", "3.21", NULL));
537: PetscCall(PetscOptionsDeprecated("-tao_monitor_short", "-tao_monitor", "3.26", NULL));
538: PetscCall(PetscOptionsDeprecated("-tao_monitor_short_interval", "-tao_monitor_interval", "3.26", NULL));
539: PetscCall(PetscOptionsDeprecated("-tao_cmonitor", "-tao_monitor_constraint_norm", "3.21", NULL));
540: PetscCall(PetscOptionsDeprecated("-tao_gmonitor", "-tao_monitor_globalization", "3.21", NULL));
541: PetscCall(PetscOptionsDeprecated("-tao_draw_solution", "-tao_monitor_solution_draw", "3.21", NULL));
542: PetscCall(PetscOptionsDeprecated("-tao_draw_gradient", "-tao_monitor_gradient_draw", "3.21", NULL));
543: PetscCall(PetscOptionsDeprecated("-tao_draw_step", "-tao_monitor_step_draw", "3.21", NULL));
545: PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL));
547: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_solution", "View solution vector after each iteration", "TaoMonitorSolution", TaoMonitorSolution));
548: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_gradient", "View gradient vector for each iteration", "TaoMonitorGradient", TaoMonitorGradient));
550: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_step", "View step vector after each iteration", "TaoMonitorStep", TaoMonitorStep));
551: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_residual", "View least-squares residual vector after each iteration", "TaoMonitorResidual", TaoMonitorResidual));
552: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor", "Use the default convergence monitor", "TaoMonitorDefault", TaoMonitorDefault));
553: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_globalization", "Use the convergence monitor with extra globalization info", "TaoMonitorGlobalization", TaoMonitorGlobalization));
554: PetscCall(TaoMonitorSetFromOptions(tao, "-tao_monitor_constraint_norm", "Use the default convergence monitor with constraint norm", "TaoMonitorConstraintNorm", TaoMonitorConstraintNorm));
556: flg = PETSC_FALSE;
557: PetscCall(PetscOptionsDeprecated("-tao_cancelmonitors", "-tao_monitor_cancel", "3.21", NULL));
558: PetscCall(PetscOptionsBool("-tao_monitor_cancel", "cancel all monitors and call any registered destroy routines", "TaoMonitorCancel", flg, &flg, NULL));
559: if (flg) PetscCall(TaoMonitorCancel(tao));
561: flg = PETSC_FALSE;
562: PetscCall(PetscOptionsBool("-tao_monitor_solution_draw", "Plot solution vector at each iteration", "TaoMonitorSet", flg, &flg, NULL));
563: if (flg) {
564: TaoMonitorDrawCtx drawctx;
565: PetscInt howoften = 1;
566: PetscCall(PetscOptionsInt("-tao_monitor_solution_draw_interval", "Only draw every interval iterations, and the final value", "TaoMonitorSet", howoften, &howoften, NULL));
567: PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
568: PetscCall(TaoMonitorSet(tao, TaoMonitorSolutionDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy));
569: }
571: flg = PETSC_FALSE;
572: PetscCall(PetscOptionsBool("-tao_monitor_step_draw", "Plots step at each iteration", "TaoMonitorSet", flg, &flg, NULL));
573: if (flg) {
574: TaoMonitorDrawCtx drawctx;
575: PetscInt howoften = 1;
576: PetscCall(PetscOptionsInt("-tao_monitor_step_draw_interval", "Only draw every interval iterations, and the final value", "TaoMonitorSet", howoften, &howoften, NULL));
577: PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
578: PetscCall(TaoMonitorSet(tao, TaoMonitorStepDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy));
579: }
581: flg = PETSC_FALSE;
582: PetscCall(PetscOptionsBool("-tao_monitor_gradient_draw", "plots gradient at each iteration", "TaoMonitorSet", flg, &flg, NULL));
583: if (flg) {
584: TaoMonitorDrawCtx drawctx;
585: PetscInt howoften = 1;
586: PetscCall(PetscOptionsInt("-tao_monitor_gradient_draw_interval", "Only draw every interval iterations, and the final value", "TaoMonitorSet", howoften, &howoften, NULL));
587: PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx));
588: PetscCall(TaoMonitorSet(tao, TaoMonitorGradientDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy));
589: }
591: flg = PETSC_FALSE;
592: PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL));
593: if (flg) PetscCall(TaoTermComputeGradientSetUseFD(tao->objective_term.term, PETSC_TRUE));
594: flg = PETSC_FALSE;
595: PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute Hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL));
596: if (flg) {
597: Mat H;
599: PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H));
600: PetscCall(MatSetType(H, MATAIJ));
601: PetscCall(MatSetOption(H, MAT_SYMMETRIC, PETSC_TRUE));
602: PetscCall(MatSetOption(H, MAT_SYMMETRY_ETERNAL, PETSC_TRUE));
603: PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL));
604: PetscCall(TaoTermComputeHessianSetUseFD(tao->objective_term.term, PETSC_TRUE));
605: PetscCall(MatDestroy(&H));
606: }
607: flg = PETSC_FALSE;
608: PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free Hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL));
609: if (flg) {
610: PetscBool is_callback;
611: Mat H;
613: // Check that tao has only one TaoTerm with type TAOTERMCALLBACK
614: PetscCall(PetscObjectTypeCompare((PetscObject)tao->objective_term.term, TAOTERMCALLBACKS, &is_callback));
615: if (is_callback) {
616: // Create Hessian via TaoTermCreateHessianMFFD
617: PetscCall(TaoTermCreateHessianMFFD(tao->objective_term.term, &H));
618: PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL));
619: PetscCall(MatDestroy(&H));
620: } else {
621: PetscCall(PetscInfo(tao, "-tao_mf_hessian only works when Tao has a single TAOTERMCALLBACK term. Ignoring.\n"));
622: }
623: }
624: PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, &found));
625: if (found) PetscCall(TaoSetRecycleHistory(tao, flg));
626: PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubsetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL));
628: if (tao->ksp) {
629: PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL));
630: PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv));
631: }
633: PetscCall(TaoTermSetFromOptions(tao->callbacks));
635: {
636: char *term_prefixes[16];
637: PetscInt n_terms = PETSC_STATIC_ARRAY_LENGTH(term_prefixes);
639: PetscCall(PetscOptionsStringArray("-tao_add_terms", "a list of prefixes for terms to add to the Tao objective function", "TaoAddTerm", term_prefixes, &n_terms, NULL));
640: for (PetscInt i = 0; i < n_terms; i++) {
641: TaoTerm term;
642: const char *prefix;
644: PetscCall(TaoTermDuplicate(tao->objective_term.term, TAOTERM_DUPLICATE_SIZEONLY, &term));
645: PetscCall(TaoGetOptionsPrefix(tao, &prefix));
646: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)term, prefix));
647: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)term, term_prefixes[i]));
648: PetscCall(TaoTermSetFromOptions(term));
649: PetscCall(TaoAddTerm(tao, term_prefixes[i], 1.0, term, NULL, NULL));
650: PetscCall(TaoTermDestroy(&term));
651: PetscCall(PetscFree(term_prefixes[i]));
652: }
653: }
655: if (tao->objective_term.term != tao->callbacks) PetscCall(TaoTermSetFromOptions(tao->objective_term.term));
657: PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject);
659: /* process any options handlers added with PetscObjectAddOptionsHandler() */
660: PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject));
661: PetscOptionsEnd();
663: if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
664: PetscFunctionReturn(PETSC_SUCCESS);
665: }
667: /*@
668: TaoViewFromOptions - View a `Tao` object based on values in the options database
670: Collective
672: Input Parameters:
673: + A - the `Tao` context
674: . obj - Optional object that provides the prefix for the options database
675: - name - command line option
677: Options Database Key:
678: . -name [viewertype][:...] - option name and values. See `PetscObjectViewFromOptions()` for the possible arguments
680: Level: intermediate
682: .seealso: [](ch_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()`
683: @*/
684: PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[])
685: {
686: PetscFunctionBegin;
688: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
689: PetscFunctionReturn(PETSC_SUCCESS);
690: }
692: /*@
693: TaoView - Prints information about the `Tao` object
695: Collective
697: Input Parameters:
698: + tao - the `Tao` context
699: - viewer - visualization context
701: Options Database Key:
702: . -tao_view - Calls `TaoView()` at the end of `TaoSolve()`
704: Level: beginner
706: Notes:
707: The available visualization contexts include
708: + `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
709: - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
710: output where only the first processor opens
711: the file. All other processors send their
712: data to the first processor to print.
714: To view all the `TaoTerm` inside of `Tao`, use `PETSC_VIEWER_ASCII_INFO_DETAIL`,
715: or pass `-tao_view ::ascii_info_detail` flag
717: .seealso: [](ch_tao), `Tao`, `PetscViewerASCIIOpen()`
718: @*/
719: PetscErrorCode TaoView(Tao tao, PetscViewer viewer)
720: {
721: PetscBool isascii, isstring;
722: TaoType type;
724: PetscFunctionBegin;
726: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer));
728: PetscCheckSameComm(tao, 1, viewer, 2);
730: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
731: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
732: if (isascii) {
733: PetscViewerFormat format;
735: PetscCall(PetscViewerGetFormat(viewer, &format));
736: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer));
738: PetscCall(PetscViewerASCIIPushTab(viewer));
739: PetscTryTypeMethod(tao, view, viewer);
740: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
741: PetscCall(PetscViewerASCIIPrintf(viewer, "Objective function:\n"));
742: PetscCall(PetscViewerASCIIPushTab(viewer));
743: PetscCall(PetscViewerASCIIPrintf(viewer, "Scale (tao_objective_scale): %g\n", (double)tao->objective_term.scale));
744: PetscCall(PetscViewerASCIIPrintf(viewer, "Function:\n"));
745: PetscCall(PetscViewerASCIIPushTab(viewer));
746: PetscCall(TaoTermView(tao->objective_term.term, viewer));
747: PetscCall(PetscViewerASCIIPopTab(viewer));
748: if (tao->objective_term.map) {
749: PetscCall(PetscViewerASCIIPrintf(viewer, "Map:\n"));
750: PetscCall(PetscViewerASCIIPushTab(viewer));
751: PetscCall(MatView(tao->objective_term.map, viewer));
752: PetscCall(PetscViewerASCIIPopTab(viewer));
753: } else PetscCall(PetscViewerASCIIPrintf(viewer, "Map: unmapped\n"));
754: PetscCall(PetscViewerASCIIPopTab(viewer));
755: } else if (tao->num_terms > 0 || tao->term_set) {
756: if (tao->objective_term.scale == 1.0 && tao->objective_term.map == NULL) {
757: PetscCall(PetscViewerASCIIPrintf(viewer, "Objective function:\n"));
758: PetscCall(PetscViewerASCIIPushTab(viewer));
759: PetscCall(TaoTermView(tao->objective_term.term, viewer));
760: PetscCall(PetscViewerASCIIPopTab(viewer));
761: } else {
762: PetscCall(PetscViewerASCIIPrintf(viewer, "Objective function:\n"));
763: PetscCall(PetscViewerASCIIPushTab(viewer));
764: if (tao->objective_term.scale != 1.0) PetscCall(PetscViewerASCIIPrintf(viewer, "Scale: %g\n", (double)tao->objective_term.scale));
765: PetscCall(PetscViewerASCIIPrintf(viewer, "Function:\n"));
766: PetscCall(PetscViewerASCIIPushTab(viewer));
767: PetscCall(TaoTermView(tao->objective_term.term, viewer));
768: PetscCall(PetscViewerASCIIPopTab(viewer));
769: if (tao->objective_term.map) {
770: PetscCall(PetscViewerASCIIPrintf(viewer, "Map:\n"));
771: PetscCall(PetscViewerASCIIPushTab(viewer));
772: PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO));
773: PetscCall(MatView(tao->objective_term.map, viewer));
774: PetscCall(PetscViewerPopFormat(viewer));
775: PetscCall(PetscViewerASCIIPopTab(viewer));
776: }
777: PetscCall(PetscViewerASCIIPopTab(viewer));
778: }
779: }
780: if (tao->linesearch) PetscCall(TaoLineSearchView(tao->linesearch, viewer));
781: if (tao->ksp) {
782: PetscCall(KSPView(tao->ksp, viewer));
783: PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its));
784: }
786: if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubsetTypes[tao->subset_type]));
788: PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol));
789: PetscCall(PetscViewerASCIIPrintf(viewer, " grtol=%g,", (double)tao->grtol));
790: PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol));
791: PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol));
792: PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual));
794: if (tao->constrained) {
795: PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:"));
796: PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol));
797: PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol));
798: PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm));
799: }
801: if (tao->trust < tao->steptol) {
802: PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol));
803: PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust));
804: }
806: if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin));
807: PetscCall(PetscViewerASCIIPrintf(viewer, "Objective value=%g\n", (double)tao->fc));
809: PetscCall(PetscViewerASCIIPrintf(viewer, "total number of iterations=%" PetscInt_FMT ", ", tao->niter));
810: PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_it));
812: if (tao->objective_term.term->nobj > 0) {
813: PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function evaluations=%" PetscInt_FMT ",", tao->objective_term.term->nobj));
814: if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n"));
815: else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs));
816: }
817: if (tao->objective_term.term->ngrad > 0) {
818: PetscCall(PetscViewerASCIIPrintf(viewer, "total number of gradient evaluations=%" PetscInt_FMT ",", tao->objective_term.term->ngrad));
819: if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n"));
820: else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs));
821: }
822: if (tao->objective_term.term->nobjgrad > 0) {
823: PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function/gradient evaluations=%" PetscInt_FMT ",", tao->objective_term.term->nobjgrad));
824: if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n"));
825: else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs));
826: }
827: if (tao->nres > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of residual evaluations=%" PetscInt_FMT "\n", tao->nres));
828: if (tao->objective_term.term->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->objective_term.term->nhess));
829: if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints));
830: if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac));
832: if (tao->reason > 0) {
833: PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: "));
834: switch (tao->reason) {
835: case TAO_CONVERGED_GATOL:
836: PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n"));
837: break;
838: case TAO_CONVERGED_GRTOL:
839: PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n"));
840: break;
841: case TAO_CONVERGED_GTTOL:
842: PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n"));
843: break;
844: case TAO_CONVERGED_STEPTOL:
845: PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n"));
846: break;
847: case TAO_CONVERGED_MINF:
848: PetscCall(PetscViewerASCIIPrintf(viewer, " Minf -- f < fmin\n"));
849: break;
850: case TAO_CONVERGED_USER:
851: PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
852: break;
853: default:
854: PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason));
855: break;
856: }
857: } else if (tao->reason == TAO_CONTINUE_ITERATING) {
858: PetscCall(PetscViewerASCIIPrintf(viewer, "Solver never run\n"));
859: } else {
860: PetscCall(PetscViewerASCIIPrintf(viewer, "Solver failed: "));
861: switch (tao->reason) {
862: case TAO_DIVERGED_MAXITS:
863: PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n"));
864: break;
865: case TAO_DIVERGED_NAN:
866: PetscCall(PetscViewerASCIIPrintf(viewer, " NaN or infinity encountered\n"));
867: break;
868: case TAO_DIVERGED_MAXFCN:
869: PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n"));
870: break;
871: case TAO_DIVERGED_LS_FAILURE:
872: PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n"));
873: break;
874: case TAO_DIVERGED_TR_REDUCTION:
875: PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n"));
876: break;
877: case TAO_DIVERGED_USER:
878: PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n"));
879: break;
880: default:
881: PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason));
882: break;
883: }
884: }
885: PetscCall(PetscViewerASCIIPopTab(viewer));
886: } else if (isstring) {
887: PetscCall(TaoGetType(tao, &type));
888: PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type));
889: }
890: PetscFunctionReturn(PETSC_SUCCESS);
891: }
893: /*@
894: TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using
895: iterate information from the previous `TaoSolve()`. This feature is disabled by
896: default.
898: Logically Collective
900: Input Parameters:
901: + tao - the `Tao` context
902: - recycle - boolean flag
904: Options Database Key:
905: . -tao_recycle_history (true|false) - reuse the history
907: Level: intermediate
909: Notes:
910: For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction
911: from the previous `TaoSolve()` call when computing the first search direction in a
912: new solution. By default, CG methods set the first search direction to the
913: negative gradient.
915: For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses
916: the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()`
917: call. By default, QN family of methods reset the initial Hessian approximation to
918: the identity matrix.
920: For any other algorithm, this setting has no effect.
922: .seealso: [](ch_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
923: @*/
924: PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle)
925: {
926: PetscFunctionBegin;
929: tao->recycle = recycle;
930: PetscFunctionReturn(PETSC_SUCCESS);
931: }
933: /*@
934: TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information
935: from the previous `TaoSolve()`. This feature is disabled by default.
937: Logically Collective
939: Input Parameter:
940: . tao - the `Tao` context
942: Output Parameter:
943: . recycle - boolean flag
945: Level: intermediate
947: .seealso: [](ch_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`
948: @*/
949: PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle)
950: {
951: PetscFunctionBegin;
953: PetscAssertPointer(recycle, 2);
954: *recycle = tao->recycle;
955: PetscFunctionReturn(PETSC_SUCCESS);
956: }
958: /*@
959: TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests
961: Logically Collective
963: Input Parameters:
964: + tao - the `Tao` context
965: . gatol - stop if norm of gradient is less than this
966: . grtol - stop if relative norm of gradient is less than this
967: - gttol - stop if norm of gradient is reduced by this factor
969: Options Database Keys:
970: + -tao_gatol gatol - Sets gatol
971: . -tao_grtol grtol - Sets grtol
972: - -tao_gttol gttol - Sets gttol
974: Stopping Criteria\:
975: .vb
976: ||g(X)|| <= gatol
977: ||g(X)|| / |f(X)| <= grtol
978: ||g(X)|| / ||g(X0)|| <= gttol
979: .ve
981: Level: beginner
983: Notes:
984: Use `PETSC_CURRENT` to leave one or more tolerances unchanged.
986: Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao`object's type was set
988: Fortran Note:
989: Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL`
991: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`
992: @*/
993: PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol)
994: {
995: PetscFunctionBegin;
1001: if (gatol == (PetscReal)PETSC_DETERMINE) {
1002: tao->gatol = tao->default_gatol;
1003: } else if (gatol != (PetscReal)PETSC_CURRENT) {
1004: PetscCheck(gatol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gatol not allowed");
1005: tao->gatol = gatol;
1006: }
1008: if (grtol == (PetscReal)PETSC_DETERMINE) {
1009: tao->grtol = tao->default_grtol;
1010: } else if (grtol != (PetscReal)PETSC_CURRENT) {
1011: PetscCheck(grtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative grtol not allowed");
1012: tao->grtol = grtol;
1013: }
1015: if (gttol == (PetscReal)PETSC_DETERMINE) {
1016: tao->gttol = tao->default_gttol;
1017: } else if (gttol != (PetscReal)PETSC_CURRENT) {
1018: PetscCheck(gttol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gttol not allowed");
1019: tao->gttol = gttol;
1020: }
1021: PetscFunctionReturn(PETSC_SUCCESS);
1022: }
1024: /*@
1025: TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests
1027: Logically Collective
1029: Input Parameters:
1030: + tao - the `Tao` context
1031: . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria
1032: - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria
1034: Options Database Keys:
1035: + -tao_catol catol - Sets catol
1036: - -tao_crtol crtol - Sets crtol
1038: Level: intermediate
1040: Notes:
1041: Use `PETSC_CURRENT` to leave one or tolerance unchanged.
1043: Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao` object's type was set
1045: Fortran Note:
1046: Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL`
1048: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()`
1049: @*/
1050: PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol)
1051: {
1052: PetscFunctionBegin;
1057: if (catol == (PetscReal)PETSC_DETERMINE) {
1058: tao->catol = tao->default_catol;
1059: } else if (catol != (PetscReal)PETSC_CURRENT) {
1060: PetscCheck(catol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative catol not allowed");
1061: tao->catol = catol;
1062: }
1064: if (crtol == (PetscReal)PETSC_DETERMINE) {
1065: tao->crtol = tao->default_crtol;
1066: } else if (crtol != (PetscReal)PETSC_CURRENT) {
1067: PetscCheck(crtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative crtol not allowed");
1068: tao->crtol = crtol;
1069: }
1070: PetscFunctionReturn(PETSC_SUCCESS);
1071: }
1073: /*@
1074: TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests
1076: Not Collective
1078: Input Parameter:
1079: . tao - the `Tao` context
1081: Output Parameters:
1082: + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria
1083: - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria
1085: Level: intermediate
1087: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()`
1088: @*/
1089: PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol)
1090: {
1091: PetscFunctionBegin;
1093: if (catol) *catol = tao->catol;
1094: if (crtol) *crtol = tao->crtol;
1095: PetscFunctionReturn(PETSC_SUCCESS);
1096: }
1098: /*@
1099: TaoSetFunctionLowerBound - Sets a bound on the solution objective value.
1100: When an approximate solution with an objective value below this number
1101: has been found, the solver will terminate.
1103: Logically Collective
1105: Input Parameters:
1106: + tao - the Tao solver context
1107: - fmin - the tolerance
1109: Options Database Key:
1110: . -tao_fmin fmin - sets the minimum function value
1112: Level: intermediate
1114: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()`
1115: @*/
1116: PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin)
1117: {
1118: PetscFunctionBegin;
1121: tao->fmin = fmin;
1122: PetscFunctionReturn(PETSC_SUCCESS);
1123: }
1125: /*@
1126: TaoGetFunctionLowerBound - Gets the bound on the solution objective value.
1127: When an approximate solution with an objective value below this number
1128: has been found, the solver will terminate.
1130: Not Collective
1132: Input Parameter:
1133: . tao - the `Tao` solver context
1135: Output Parameter:
1136: . fmin - the minimum function value
1138: Level: intermediate
1140: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()`
1141: @*/
1142: PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin)
1143: {
1144: PetscFunctionBegin;
1146: PetscAssertPointer(fmin, 2);
1147: *fmin = tao->fmin;
1148: PetscFunctionReturn(PETSC_SUCCESS);
1149: }
1151: /*@
1152: TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`.
1154: Logically Collective
1156: Input Parameters:
1157: + tao - the `Tao` solver context
1158: - nfcn - the maximum number of function evaluations (>=0), use `PETSC_UNLIMITED` to have no bound
1160: Options Database Key:
1161: . -tao_max_funcs nfcn - sets the maximum number of function evaluations
1163: Level: intermediate
1165: Note:
1166: Use `PETSC_DETERMINE` to use the default maximum number of function evaluations that was set when the object type was set.
1168: Developer Note:
1169: Deprecated support for an unlimited number of function evaluations by passing a negative value.
1171: .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()`
1172: @*/
1173: PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn)
1174: {
1175: PetscFunctionBegin;
1178: if (nfcn == PETSC_DETERMINE) {
1179: tao->max_funcs = tao->default_max_funcs;
1180: } else if (nfcn == PETSC_UNLIMITED || nfcn < 0) {
1181: tao->max_funcs = PETSC_UNLIMITED;
1182: } else {
1183: PetscCheck(nfcn >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of function evaluations must be positive");
1184: tao->max_funcs = nfcn;
1185: }
1186: PetscFunctionReturn(PETSC_SUCCESS);
1187: }
1189: /*@
1190: TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()`
1192: Logically Collective
1194: Input Parameter:
1195: . tao - the `Tao` solver context
1197: Output Parameter:
1198: . nfcn - the maximum number of function evaluations
1200: Level: intermediate
1202: .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1203: @*/
1204: PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn)
1205: {
1206: PetscFunctionBegin;
1208: PetscAssertPointer(nfcn, 2);
1209: *nfcn = tao->max_funcs;
1210: PetscFunctionReturn(PETSC_SUCCESS);
1211: }
1213: /*@
1214: TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object
1216: Not Collective
1218: Input Parameter:
1219: . tao - the `Tao` solver context
1221: Output Parameter:
1222: . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations)
1224: Level: intermediate
1226: .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()`
1227: @*/
1228: PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs)
1229: {
1230: PetscFunctionBegin;
1232: PetscAssertPointer(nfuncs, 2);
1233: *nfuncs = PetscMax(tao->objective_term.term->nobj, tao->objective_term.term->nobjgrad);
1234: PetscFunctionReturn(PETSC_SUCCESS);
1235: }
1237: /*@
1238: TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()`
1240: Logically Collective
1242: Input Parameters:
1243: + tao - the `Tao` solver context
1244: - maxits - the maximum number of iterates (>=0), use `PETSC_UNLIMITED` to have no bound
1246: Options Database Key:
1247: . -tao_max_it its - sets the maximum number of iterations
1249: Level: intermediate
1251: Note:
1252: Use `PETSC_DETERMINE` to use the default maximum number of iterations that was set when the object's type was set.
1254: Developer Note:
1255: Also accepts the deprecated negative values to indicate no limit
1257: .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()`
1258: @*/
1259: PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits)
1260: {
1261: PetscFunctionBegin;
1264: if (maxits == PETSC_DETERMINE) {
1265: tao->max_it = tao->default_max_it;
1266: } else if (maxits == PETSC_UNLIMITED) {
1267: tao->max_it = PETSC_INT_MAX;
1268: } else {
1269: PetscCheck(maxits > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of iterations must be positive");
1270: tao->max_it = maxits;
1271: }
1272: PetscFunctionReturn(PETSC_SUCCESS);
1273: }
1275: /*@
1276: TaoGetMaximumIterations - Gets a maximum number of iterates that will be used
1278: Not Collective
1280: Input Parameter:
1281: . tao - the `Tao` solver context
1283: Output Parameter:
1284: . maxits - the maximum number of iterates
1286: Level: intermediate
1288: .seealso: [](ch_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()`
1289: @*/
1290: PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits)
1291: {
1292: PetscFunctionBegin;
1294: PetscAssertPointer(maxits, 2);
1295: *maxits = tao->max_it;
1296: PetscFunctionReturn(PETSC_SUCCESS);
1297: }
1299: /*@
1300: TaoSetInitialTrustRegionRadius - Sets the initial trust region radius.
1302: Logically Collective
1304: Input Parameters:
1305: + tao - a `Tao` optimization solver
1306: - radius - the trust region radius
1308: Options Database Key:
1309: . -tao_trust0 radius - sets initial trust region radius
1311: Level: intermediate
1313: Note:
1314: Use `PETSC_DETERMINE` to use the default radius that was set when the object's type was set.
1316: .seealso: [](ch_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR`
1317: @*/
1318: PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius)
1319: {
1320: PetscFunctionBegin;
1323: if (radius == PETSC_DETERMINE) {
1324: tao->trust0 = tao->default_trust0;
1325: } else {
1326: PetscCheck(radius > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Radius must be positive");
1327: tao->trust0 = radius;
1328: }
1329: PetscFunctionReturn(PETSC_SUCCESS);
1330: }
1332: /*@
1333: TaoGetInitialTrustRegionRadius - Gets the initial trust region radius.
1335: Not Collective
1337: Input Parameter:
1338: . tao - a `Tao` optimization solver
1340: Output Parameter:
1341: . radius - the trust region radius
1343: Level: intermediate
1345: .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR`
1346: @*/
1347: PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius)
1348: {
1349: PetscFunctionBegin;
1351: PetscAssertPointer(radius, 2);
1352: *radius = tao->trust0;
1353: PetscFunctionReturn(PETSC_SUCCESS);
1354: }
1356: /*@
1357: TaoGetCurrentTrustRegionRadius - Gets the current trust region radius.
1359: Not Collective
1361: Input Parameter:
1362: . tao - a `Tao` optimization solver
1364: Output Parameter:
1365: . radius - the trust region radius
1367: Level: intermediate
1369: .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR`
1370: @*/
1371: PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius)
1372: {
1373: PetscFunctionBegin;
1375: PetscAssertPointer(radius, 2);
1376: *radius = tao->trust;
1377: PetscFunctionReturn(PETSC_SUCCESS);
1378: }
1380: /*@
1381: TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()`
1383: Not Collective
1385: Input Parameter:
1386: . tao - the `Tao` context
1388: Output Parameters:
1389: + gatol - stop if norm of gradient is less than this
1390: . grtol - stop if relative norm of gradient is less than this
1391: - gttol - stop if norm of gradient is reduced by a this factor
1393: Level: intermediate
1395: Note:
1396: `NULL` can be used as an argument if not all tolerances values are needed
1398: .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`
1399: @*/
1400: PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol)
1401: {
1402: PetscFunctionBegin;
1404: if (gatol) *gatol = tao->gatol;
1405: if (grtol) *grtol = tao->grtol;
1406: if (gttol) *gttol = tao->gttol;
1407: PetscFunctionReturn(PETSC_SUCCESS);
1408: }
1410: /*@
1411: TaoGetKSP - Gets the linear solver used by the optimization solver.
1413: Not Collective
1415: Input Parameter:
1416: . tao - the `Tao` solver
1418: Output Parameter:
1419: . ksp - the `KSP` linear solver used in the optimization solver
1421: Level: intermediate
1423: .seealso: [](ch_tao), `Tao`, `KSP`
1424: @*/
1425: PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp)
1426: {
1427: PetscFunctionBegin;
1429: PetscAssertPointer(ksp, 2);
1430: *ksp = tao->ksp;
1431: PetscFunctionReturn(PETSC_SUCCESS);
1432: }
1434: /*@
1435: TaoGetLinearSolveIterations - Gets the total number of linear iterations
1436: used by the `Tao` solver
1438: Not Collective
1440: Input Parameter:
1441: . tao - the `Tao` context
1443: Output Parameter:
1444: . lits - number of linear iterations
1446: Level: intermediate
1448: Note:
1449: This counter is reset to zero for each successive call to `TaoSolve()`
1451: .seealso: [](ch_tao), `Tao`, `TaoGetKSP()`
1452: @*/
1453: PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits)
1454: {
1455: PetscFunctionBegin;
1457: PetscAssertPointer(lits, 2);
1458: *lits = tao->ksp_tot_its;
1459: PetscFunctionReturn(PETSC_SUCCESS);
1460: }
1462: /*@
1463: TaoGetLineSearch - Gets the line search used by the optimization solver.
1465: Not Collective
1467: Input Parameter:
1468: . tao - the `Tao` solver
1470: Output Parameter:
1471: . ls - the line search used in the optimization solver
1473: Level: intermediate
1475: .seealso: [](ch_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType`
1476: @*/
1477: PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls)
1478: {
1479: PetscFunctionBegin;
1481: PetscAssertPointer(ls, 2);
1482: *ls = tao->linesearch;
1483: PetscFunctionReturn(PETSC_SUCCESS);
1484: }
1486: /*@
1487: TaoAddLineSearchCounts - Adds the number of function evaluations spent
1488: in the line search to the running total.
1490: Input Parameters:
1491: . tao - the `Tao` solver
1493: Level: developer
1495: .seealso: [](ch_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()`
1496: @*/
1497: PetscErrorCode TaoAddLineSearchCounts(Tao tao)
1498: {
1499: PetscBool flg;
1500: PetscInt nfeval, ngeval, nfgeval;
1502: PetscFunctionBegin;
1504: if (tao->linesearch) {
1505: PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg));
1506: if (!flg) {
1507: PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval));
1508: tao->objective_term.term->nobj += nfeval;
1509: tao->objective_term.term->ngrad += ngeval;
1510: tao->objective_term.term->nobjgrad += nfgeval;
1511: }
1512: }
1513: PetscFunctionReturn(PETSC_SUCCESS);
1514: }
1516: /*@
1517: TaoGetSolution - Returns the vector with the current solution from the `Tao` object
1519: Not Collective
1521: Input Parameter:
1522: . tao - the `Tao` context
1524: Output Parameter:
1525: . X - the current solution
1527: Level: intermediate
1529: Note:
1530: The returned vector will be the same object that was passed into `TaoSetSolution()`
1532: .seealso: [](ch_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()`
1533: @*/
1534: PetscErrorCode TaoGetSolution(Tao tao, Vec *X)
1535: {
1536: PetscFunctionBegin;
1538: PetscAssertPointer(X, 2);
1539: *X = tao->solution;
1540: PetscFunctionReturn(PETSC_SUCCESS);
1541: }
1543: /*@
1544: TaoResetStatistics - Initialize the statistics collected by the `Tao` object.
1545: These statistics include the iteration number, residual norms, and convergence status.
1546: This routine gets called before solving each optimization problem.
1548: Collective
1550: Input Parameter:
1551: . tao - the `Tao` context
1553: Level: developer
1555: Note:
1556: This function does not reset the statistics of internal `TaoTerm`
1558: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()`
1559: @*/
1560: PetscErrorCode TaoResetStatistics(Tao tao)
1561: {
1562: PetscFunctionBegin;
1564: tao->niter = 0;
1565: tao->nres = 0;
1566: tao->njac = 0;
1567: tao->nconstraints = 0;
1568: tao->ksp_its = 0;
1569: tao->ksp_tot_its = 0;
1570: tao->reason = TAO_CONTINUE_ITERATING;
1571: tao->residual = 0.0;
1572: tao->cnorm = 0.0;
1573: tao->step = 0.0;
1574: tao->lsflag = PETSC_FALSE;
1575: if (tao->hist_reset) tao->hist_len = 0;
1576: PetscFunctionReturn(PETSC_SUCCESS);
1577: }
1579: /*@
1580: TaoSetUpdate - Sets the general-purpose update function called
1581: at the beginning of every iteration of the optimization algorithm. Called after the new solution and the gradient
1582: is determined, but before the Hessian is computed (if applicable).
1584: Logically Collective
1586: Input Parameters:
1587: + tao - The `Tao` solver
1588: . func - The function
1589: - ctx - The update function context
1591: Calling sequence of `func`:
1592: + tao - The optimizer context
1593: . it - The current iteration index
1594: - ctx - The update context
1596: Level: advanced
1598: Notes:
1599: Users can modify the gradient direction or any other vector associated to the specific solver used.
1600: The objective function value is always recomputed after a call to the update hook.
1602: .seealso: [](ch_tao), `Tao`, `TaoSolve()`
1603: @*/
1604: PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao tao, PetscInt it, PetscCtx ctx), PetscCtx ctx)
1605: {
1606: PetscFunctionBegin;
1608: tao->ops->update = func;
1609: tao->user_update = ctx;
1610: PetscFunctionReturn(PETSC_SUCCESS);
1611: }
1613: /*@
1614: TaoSetConvergenceTest - Sets the function that is to be used to test
1615: for convergence of the iterative minimization solution. The new convergence
1616: testing routine will replace Tao's default convergence test.
1618: Logically Collective
1620: Input Parameters:
1621: + tao - the `Tao` object
1622: . conv - the routine to test for convergence
1623: - ctx - [optional] context for private data for the convergence routine (may be `NULL`)
1625: Calling sequence of `conv`:
1626: + tao - the `Tao` object
1627: - ctx - [optional] convergence context
1629: Level: advanced
1631: Note:
1632: The new convergence testing routine should call `TaoSetConvergedReason()`.
1634: .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoMonitorSet()`
1635: @*/
1636: PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao tao, PetscCtx ctx), PetscCtx ctx)
1637: {
1638: PetscFunctionBegin;
1640: tao->ops->convergencetest = conv;
1641: tao->cnvP = ctx;
1642: PetscFunctionReturn(PETSC_SUCCESS);
1643: }
1645: /*@
1646: TaoMonitorSet - Sets an additional function that is to be used at every
1647: iteration of the solver to display the iteration's
1648: progress.
1650: Logically Collective
1652: Input Parameters:
1653: + tao - the `Tao` solver context
1654: . func - monitoring routine
1655: . ctx - [optional] user-defined context for private data for the monitor routine (may be `NULL`)
1656: - dest - [optional] function to destroy the context when the `Tao` is destroyed, see `PetscCtxDestroyFn` for the calling sequence
1658: Calling sequence of `func`:
1659: + tao - the `Tao` solver context
1660: - ctx - [optional] monitoring context
1662: Level: intermediate
1664: Notes:
1665: See `TaoSetFromOptions()` for a monitoring options.
1667: Several different monitoring routines may be set by calling
1668: `TaoMonitorSet()` multiple times; all will be called in the
1669: order in which they were set.
1671: Fortran Notes:
1672: Only one monitor function may be set
1674: .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoMonitorCancel()`, `TaoView()`, `PetscCtxDestroyFn`
1675: @*/
1676: PetscErrorCode TaoMonitorSet(Tao tao, PetscErrorCode (*func)(Tao tao, PetscCtx ctx), PetscCtx ctx, PetscCtxDestroyFn *dest)
1677: {
1678: PetscFunctionBegin;
1680: PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS);
1681: for (PetscInt i = 0; i < tao->numbermonitors; i++) {
1682: PetscBool identical;
1684: PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))(PetscVoidFn *)func, ctx, dest, (PetscErrorCode (*)(void))(PetscVoidFn *)tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical));
1685: if (identical) PetscFunctionReturn(PETSC_SUCCESS);
1686: }
1687: tao->monitor[tao->numbermonitors] = func;
1688: tao->monitorcontext[tao->numbermonitors] = ctx;
1689: tao->monitordestroy[tao->numbermonitors] = dest;
1690: ++tao->numbermonitors;
1691: PetscFunctionReturn(PETSC_SUCCESS);
1692: }
1694: /*@
1695: TaoMonitorCancel - Clears all the monitor functions for a `Tao` object.
1697: Logically Collective
1699: Input Parameter:
1700: . tao - the `Tao` solver context
1702: Options Database Key:
1703: . -tao_monitor_cancel - cancels all monitors that have been hardwired
1704: into a code by calls to `TaoMonitorSet()`, but does not cancel those
1705: set via the options database
1707: Level: advanced
1709: Note:
1710: There is no way to clear one specific monitor from a `Tao` object.
1712: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1713: @*/
1714: PetscErrorCode TaoMonitorCancel(Tao tao)
1715: {
1716: PetscFunctionBegin;
1718: for (PetscInt i = 0; i < tao->numbermonitors; i++) {
1719: if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i]));
1720: }
1721: tao->numbermonitors = 0;
1722: PetscFunctionReturn(PETSC_SUCCESS);
1723: }
1725: /*@
1726: TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()`
1728: Collective
1730: Input Parameters:
1731: + tao - the `Tao` context
1732: - vf - `PetscViewerAndFormat` context
1734: Options Database Keys:
1735: + -tao_monitor [ascii][:filename] - monitor function and residual norms at each iteration, only ASCII viewers supported
1736: - -tao_monitor_interval interval - only monitor function and residual norms every `interval` iterations, and the last iteration
1738: Level: advanced
1740: Note:
1741: This monitor prints the function value and gradient
1742: norm at each iteration.
1744: .seealso: [](ch_tao), `Tao`, `TaoMonitorGlobalization()`, `TaoMonitorSet()`
1745: @*/
1746: PetscErrorCode TaoMonitorDefault(Tao tao, PetscViewerAndFormat *vf)
1747: {
1748: PetscViewer viewer = vf->viewer;
1749: PetscBool isascii;
1750: PetscInt tabs;
1752: PetscFunctionBegin;
1754: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1756: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1757: PetscCall(PetscViewerPushFormat(viewer, vf->format));
1758: if (isascii) {
1759: PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
1761: PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1762: if (tao->niter == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
1763: PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1764: tao->header_printed = PETSC_TRUE;
1765: }
1766: PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", tao->niter));
1767: PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)tao->fc));
1768: if (tao->residual >= PETSC_INFINITY) {
1769: PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: infinity \n"));
1770: } else {
1771: PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)tao->residual));
1772: }
1773: PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1774: }
1775: PetscCall(PetscViewerPopFormat(viewer));
1776: PetscFunctionReturn(PETSC_SUCCESS);
1777: }
1779: /*@
1780: TaoMonitorGlobalization - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method.
1782: Collective
1784: Input Parameters:
1785: + tao - the `Tao` context
1786: - vf - `PetscViewerAndFormat` context
1788: Options Database Keys:
1789: + -tao_monitor_globalization [ascii][:filename] - monitor globalization information at each iteration, only ASCII viewers are supported
1790: - -tao_monitor_globalization_interval interval - only monitor globalization information every `interval` iterations, and the last iteration
1792: Level: advanced
1794: Note:
1795: This monitor prints the function value and gradient norm at each
1796: iteration, as well as the step size and trust radius. Note that the
1797: step size and trust radius may be the same for some algorithms.
1799: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1800: @*/
1801: PetscErrorCode TaoMonitorGlobalization(Tao tao, PetscViewerAndFormat *vf)
1802: {
1803: PetscViewer viewer = vf->viewer;
1804: PetscBool isascii;
1805: PetscInt tabs;
1807: PetscFunctionBegin;
1809: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1811: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1812: PetscCall(PetscViewerPushFormat(viewer, vf->format));
1813: if (isascii) {
1814: PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
1815: PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1816: if (tao->niter == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) {
1817: PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix));
1818: tao->header_printed = PETSC_TRUE;
1819: }
1820: PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", tao->niter));
1821: PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)tao->fc));
1822: if (tao->residual >= PETSC_INFINITY) {
1823: PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf,"));
1824: } else {
1825: PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g,", (double)tao->residual));
1826: }
1827: PetscCall(PetscViewerASCIIPrintf(viewer, " Step: %g, Trust: %g\n", (double)tao->step, (double)tao->trust));
1828: PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1829: }
1830: PetscCall(PetscViewerPopFormat(viewer));
1831: PetscFunctionReturn(PETSC_SUCCESS);
1832: }
1834: /*@
1835: TaoMonitorConstraintNorm - same as `TaoMonitorDefault()` except
1836: it prints the norm of the constraint function.
1838: Collective
1840: Input Parameters:
1841: + tao - the `Tao` context
1842: - vf - `PetscViewerAndFormat` context
1844: Options Database Keys:
1845: + -tao_monitor_constraint_norm [ascii][:filename] - monitor the constraints at each iteration, only ASCII viewers are supported
1846: - -tao_monitor_constraint_norm_interval interval - only monitor the constraints every `interval` iterations, and the last iteration
1848: Level: advanced
1850: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1851: @*/
1852: PetscErrorCode TaoMonitorConstraintNorm(Tao tao, PetscViewerAndFormat *vf)
1853: {
1854: PetscViewer viewer = vf->viewer;
1855: PetscBool isascii;
1856: PetscInt tabs;
1858: PetscFunctionBegin;
1860: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1862: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1863: PetscCall(PetscViewerPushFormat(viewer, vf->format));
1864: if (isascii) {
1865: PetscCall(PetscViewerASCIIGetTab(viewer, &tabs));
1866: PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel));
1867: PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", tao->niter));
1868: PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)tao->fc));
1869: PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g ", (double)tao->residual));
1870: PetscCall(PetscViewerASCIIPrintf(viewer, " Constraint: %g \n", (double)tao->cnorm));
1871: PetscCall(PetscViewerASCIISetTab(viewer, tabs));
1872: }
1873: PetscCall(PetscViewerPopFormat(viewer));
1874: PetscFunctionReturn(PETSC_SUCCESS);
1875: }
1877: /*@
1878: TaoMonitorSolution - Views the solution at each iteration of `TaoSolve()`
1880: Collective
1882: Input Parameters:
1883: + tao - the `Tao` context
1884: - vf - `PetscViewerAndFormat` context
1886: Options Database Keys:
1887: + -tao_monitor_solution [viewertype][:filename][:viewerformat] - view the solution vector at each iteration
1888: - -tao_monitor_solution_interval interval - only view the solution every `interval` iterations, and the last iteration
1890: Level: advanced
1892: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1893: @*/
1894: PetscErrorCode TaoMonitorSolution(Tao tao, PetscViewerAndFormat *vf)
1895: {
1896: PetscFunctionBegin;
1898: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1899: PetscCall(PetscViewerPushFormat(vf->viewer, vf->format));
1900: PetscCall(VecView(tao->solution, vf->viewer));
1901: PetscCall(PetscViewerPopFormat(vf->viewer));
1902: PetscFunctionReturn(PETSC_SUCCESS);
1903: }
1905: /*@
1906: TaoMonitorGradient - Views the gradient at each iteration of `TaoSolve()`
1908: Collective
1910: Input Parameters:
1911: + tao - the `Tao` context
1912: - vf - `PetscViewerAndFormat` context
1914: Options Database Keys:
1915: + -tao_monitor_gradient [viewertype][:filename][:viewerformat] - view the gradient at each iteration
1916: - -tao_monitor_gradient_interval interval - only view the gradient every `interval` iterations, and the last iteration
1918: Level: advanced
1920: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1921: @*/
1922: PetscErrorCode TaoMonitorGradient(Tao tao, PetscViewerAndFormat *vf)
1923: {
1924: PetscFunctionBegin;
1926: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1927: PetscCall(PetscViewerPushFormat(vf->viewer, vf->format));
1928: PetscCall(VecView(tao->gradient, vf->viewer));
1929: PetscCall(PetscViewerPopFormat(vf->viewer));
1930: PetscFunctionReturn(PETSC_SUCCESS);
1931: }
1933: /*@
1934: TaoMonitorStep - Views the step-direction at each iteration of `TaoSolve()`
1936: Collective
1938: Input Parameters:
1939: + tao - the `Tao` context
1940: - vf - `PetscViewerAndFormat` context
1942: Options Database Keys:
1943: + -tao_monitor_step [viewertype][:filename][:viewerformat] - view the step vector at each iteration
1944: - -tao_monitor_step_interval interval - only view the step vector every `interval` iterations, and the last iteration
1946: Level: advanced
1948: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
1949: @*/
1950: PetscErrorCode TaoMonitorStep(Tao tao, PetscViewerAndFormat *vf)
1951: {
1952: PetscFunctionBegin;
1954: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
1955: PetscCall(PetscViewerPushFormat(vf->viewer, vf->format));
1956: PetscCall(VecView(tao->stepdirection, vf->viewer));
1957: PetscCall(PetscViewerPopFormat(vf->viewer));
1958: PetscFunctionReturn(PETSC_SUCCESS);
1959: }
1961: /*@
1962: TaoMonitorSolutionDraw - Plots the solution at each iteration of `TaoSolve()`
1964: Collective
1966: Input Parameters:
1967: + tao - the `Tao` context
1968: - ctx - `TaoMonitorDrawCtx` context
1970: Options Database Keys:
1971: + -tao_monitor_solution_draw - draw the solution at each iteration
1972: - -tao_monitor_solution_draw_interval interval - only draw the solution every `interval` iterations and final value, or only final value if negative
1974: Level: advanced
1976: Note:
1977: The context created by `TaoMonitorDrawCtxCreate()`, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()`
1978: are passed to `TaoMonitorSet()` to monitor the solution graphically.
1980: .seealso: [](ch_tao), `Tao`, `TaoMonitorSolution()`, `TaoMonitorSet()`, `TaoMonitorGradientDraw()`, `TaoMonitorDrawCtxCreate()`,
1981: `TaoMonitorDrawCtxDestroy()`
1982: @*/
1983: PetscErrorCode TaoMonitorSolutionDraw(Tao tao, PetscCtx ctx)
1984: {
1985: TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
1987: PetscFunctionBegin;
1989: if (!((ictx->howoften > 0 && !((tao->niter % ictx->howoften) && !tao->reason)) || (ictx->howoften < 0 && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
1990: PetscCall(VecView(tao->solution, ictx->viewer));
1991: PetscFunctionReturn(PETSC_SUCCESS);
1992: }
1994: /*@
1995: TaoMonitorGradientDraw - Plots the gradient at each iteration of `TaoSolve()`
1997: Collective
1999: Input Parameters:
2000: + tao - the `Tao` context
2001: - ctx - `TaoMonitorDrawCtx` context
2003: Options Database Keys:
2004: + -tao_monitor_gradient_draw - draw the gradient at each iteration
2005: - -tao_monitor_gradient_draw_interval interval - only draw the gradient every `interval` iterations and final value, or only final value if negative
2007: Level: advanced
2009: .seealso: [](ch_tao), `Tao`, `TaoMonitorGradient()`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw()`
2010: @*/
2011: PetscErrorCode TaoMonitorGradientDraw(Tao tao, PetscCtx ctx)
2012: {
2013: TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
2015: PetscFunctionBegin;
2017: if (!((ictx->howoften > 0 && !((tao->niter % ictx->howoften) && !tao->reason)) || (ictx->howoften < 0 && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
2018: PetscCall(VecView(tao->gradient, ictx->viewer));
2019: PetscFunctionReturn(PETSC_SUCCESS);
2020: }
2022: /*@
2023: TaoMonitorStepDraw - Plots the step direction at each iteration of `TaoSolve()`
2025: Collective
2027: Input Parameters:
2028: + tao - the `Tao` context
2029: - ctx - the `TaoMonitorDrawCtx` context
2031: Options Database Keys:
2032: + -tao_monitor_step_draw - draw the step direction at each iteration
2033: - -tao_monitor_step_draw_interval interval - only draw the step direction every `interval` iterations and final value, or only final value if negative
2035: Level: advanced
2037: .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw`
2038: @*/
2039: PetscErrorCode TaoMonitorStepDraw(Tao tao, PetscCtx ctx)
2040: {
2041: TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx;
2043: PetscFunctionBegin;
2045: if (!((ictx->howoften > 0 && !((tao->niter % ictx->howoften) && !tao->reason)) || (ictx->howoften < 0 && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS);
2046: PetscCall(VecView(tao->stepdirection, ictx->viewer));
2047: PetscFunctionReturn(PETSC_SUCCESS);
2048: }
2050: /*@
2051: TaoMonitorResidual - Views the least-squares residual at each iteration of `TaoSolve()`
2053: Collective
2055: Input Parameters:
2056: + tao - the `Tao` context
2057: - vf - `PetscViewerAndFormat` context
2059: Options Database Keys:
2060: + -tao_monitor_residual - view the residual at each iteration
2061: - -tao_monitor_residual_interval interval - only view residual every `interval` iterations, and the last iteration
2063: Level: advanced
2065: .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()`
2066: @*/
2067: PetscErrorCode TaoMonitorResidual(Tao tao, PetscViewerAndFormat *vf)
2068: {
2069: PetscFunctionBegin;
2071: if (vf->view_interval > 0 && tao->niter % vf->view_interval && !tao->reason) PetscFunctionReturn(PETSC_SUCCESS);
2072: PetscCall(PetscViewerPushFormat(vf->viewer, vf->format));
2073: PetscCall(VecView(tao->ls_res, vf->viewer));
2074: PetscCall(PetscViewerPopFormat(vf->viewer));
2075: PetscFunctionReturn(PETSC_SUCCESS);
2076: }
2078: /*@
2079: TaoDefaultConvergenceTest - Determines whether the solver should continue iterating
2080: or terminate.
2082: Collective
2084: Input Parameters:
2085: + tao - the `Tao` context
2086: - dummy - unused dummy context
2088: Level: developer
2090: Notes:
2091: This routine checks the residual in the optimality conditions, the
2092: relative residual in the optimity conditions, the number of function
2093: evaluations, and the function value to test convergence. Some
2094: solvers may use different convergence routines.
2096: .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()`
2097: @*/
2098: PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy)
2099: {
2100: PetscInt niter = tao->niter, nfuncs;
2101: PetscInt max_funcs = tao->max_funcs;
2102: PetscReal gnorm = tao->residual, gnorm0 = tao->gnorm0;
2103: PetscReal f = tao->fc, steptol = tao->steptol, trradius = tao->step;
2104: PetscReal gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol;
2105: PetscReal catol = tao->catol, crtol = tao->crtol;
2106: PetscReal fmin = tao->fmin, cnorm = tao->cnorm;
2107: TaoConvergedReason reason = tao->reason;
2109: PetscFunctionBegin;
2111: if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);
2113: PetscCall(TaoGetCurrentFunctionEvaluations(tao, &nfuncs));
2114: if (PetscIsInfOrNanReal(f)) {
2115: PetscCall(PetscInfo(tao, "Failed to converged, function value is infinity or NaN\n"));
2116: reason = TAO_DIVERGED_NAN;
2117: } else if (f <= fmin && cnorm <= catol) {
2118: PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin));
2119: reason = TAO_CONVERGED_MINF;
2120: } else if (gnorm <= gatol && cnorm <= catol) {
2121: PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol));
2122: reason = TAO_CONVERGED_GATOL;
2123: } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) {
2124: PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol));
2125: reason = TAO_CONVERGED_GRTOL;
2126: } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) {
2127: PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol));
2128: reason = TAO_CONVERGED_GTTOL;
2129: } else if (max_funcs != PETSC_UNLIMITED && nfuncs > max_funcs) {
2130: PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs));
2131: reason = TAO_DIVERGED_MAXFCN;
2132: } else if (tao->lsflag != 0) {
2133: PetscCall(PetscInfo(tao, "Tao Line Search failure.\n"));
2134: reason = TAO_DIVERGED_LS_FAILURE;
2135: } else if (trradius < steptol && niter > 0) {
2136: PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol));
2137: reason = TAO_CONVERGED_STEPTOL;
2138: } else if (niter >= tao->max_it) {
2139: PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it));
2140: reason = TAO_DIVERGED_MAXITS;
2141: } else {
2142: reason = TAO_CONTINUE_ITERATING;
2143: }
2144: tao->reason = reason;
2145: PetscFunctionReturn(PETSC_SUCCESS);
2146: }
2148: /*@
2149: TaoSetOptionsPrefix - Sets the prefix used for searching for all
2150: Tao options in the database.
2152: Logically Collective
2154: Input Parameters:
2155: + tao - the `Tao` context
2156: - p - the prefix string to prepend to all Tao option requests
2158: Level: advanced
2160: Notes:
2161: A hyphen (-) must NOT be given at the beginning of the prefix name.
2162: The first character of all runtime options is AUTOMATICALLY the hyphen.
2164: For example, to distinguish between the runtime options for two
2165: different Tao solvers, one could call
2166: .vb
2167: TaoSetOptionsPrefix(tao1,"sys1_")
2168: TaoSetOptionsPrefix(tao2,"sys2_")
2169: .ve
2171: This would enable use of different options for each system, such as
2172: .vb
2173: -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3
2174: -sys2_tao_method lmvm -sys2_tao_grtol 1.e-4
2175: .ve
2177: .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()`
2178: @*/
2179: PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[])
2180: {
2181: PetscFunctionBegin;
2183: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p));
2184: if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p));
2185: if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p));
2186: if (tao->callbacks) {
2187: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao->callbacks, p));
2188: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->callbacks, "callbacks_"));
2189: }
2190: PetscFunctionReturn(PETSC_SUCCESS);
2191: }
2193: /*@
2194: TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database.
2196: Logically Collective
2198: Input Parameters:
2199: + tao - the `Tao` solver context
2200: - p - the prefix string to prepend to all `Tao` option requests
2202: Level: advanced
2204: Note:
2205: A hyphen (-) must NOT be given at the beginning of the prefix name.
2206: The first character of all runtime options is automatically the hyphen.
2208: .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()`
2209: @*/
2210: PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[])
2211: {
2212: PetscFunctionBegin;
2214: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p));
2215: if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p));
2216: if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p));
2217: if (tao->callbacks) {
2218: const char *prefix;
2220: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, &prefix));
2221: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao->callbacks, prefix));
2222: PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->callbacks, "callbacks_"));
2223: }
2224: PetscFunctionReturn(PETSC_SUCCESS);
2225: }
2227: /*@
2228: TaoGetOptionsPrefix - Gets the prefix used for searching for all
2229: Tao options in the database
2231: Not Collective
2233: Input Parameter:
2234: . tao - the `Tao` context
2236: Output Parameter:
2237: . p - pointer to the prefix string used is returned
2239: Level: advanced
2241: .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()`
2242: @*/
2243: PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[])
2244: {
2245: PetscFunctionBegin;
2247: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p));
2248: PetscFunctionReturn(PETSC_SUCCESS);
2249: }
2251: /*@
2252: TaoSetType - Sets the `TaoType` for the minimization solver.
2254: Collective
2256: Input Parameters:
2257: + tao - the `Tao` solver context
2258: - type - a known method
2260: Options Database Key:
2261: . -tao_type type - Sets the method; see `TaoType`
2263: Level: intermediate
2265: Note:
2266: Calling this function resets the convergence test to `TaoDefaultConvergenceTest()`.
2267: If a custom convergence test has been set with `TaoSetConvergenceTest()`, it must
2268: be set again after calling `TaoSetType()`.
2270: .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType`
2271: @*/
2272: PetscErrorCode TaoSetType(Tao tao, TaoType type)
2273: {
2274: PetscErrorCode (*create_xxx)(Tao);
2275: PetscBool issame;
2277: PetscFunctionBegin;
2280: PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame));
2281: if (issame) PetscFunctionReturn(PETSC_SUCCESS);
2283: PetscCall(PetscFunctionListFind(TaoList, type, &create_xxx));
2284: PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type);
2286: /* Destroy the existing solver information */
2287: PetscTryTypeMethod(tao, destroy);
2288: PetscCall(KSPDestroy(&tao->ksp));
2289: PetscCall(TaoLineSearchDestroy(&tao->linesearch));
2291: /* Reinitialize type-specific function pointers in TaoOps structure */
2292: tao->ops->setup = NULL;
2293: tao->ops->computedual = NULL;
2294: tao->ops->solve = NULL;
2295: tao->ops->view = NULL;
2296: tao->ops->setfromoptions = NULL;
2297: tao->ops->destroy = NULL;
2298: tao->ops->convergencetest = TaoDefaultConvergenceTest;
2300: tao->setupcalled = PETSC_FALSE;
2301: tao->uses_gradient = PETSC_FALSE;
2302: tao->uses_hessian_matrices = PETSC_FALSE;
2304: PetscCall(TaoParametersInitialize(tao));
2306: PetscCall((*create_xxx)(tao));
2307: PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type));
2308: PetscFunctionReturn(PETSC_SUCCESS);
2309: }
2311: /*@
2312: TaoRegister - Adds a method to the Tao package for minimization.
2314: Not Collective, No Fortran Support
2316: Input Parameters:
2317: + sname - name of a new user-defined solver
2318: - func - routine to create `TaoType` specific method context
2320: Calling sequence of `func`:
2321: . tao - the `Tao` object to be created
2323: Example Usage:
2324: .vb
2325: TaoRegister("my_solver", MySolverCreate);
2326: .ve
2328: Then, your solver can be chosen with the procedural interface via
2329: .vb
2330: TaoSetType(tao, "my_solver")
2331: .ve
2332: or at runtime via the option
2333: .vb
2334: -tao_type my_solver
2335: .ve
2337: Level: advanced
2339: Note:
2340: `TaoRegister()` may be called multiple times to add several user-defined solvers.
2342: .seealso: [](ch_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()`
2343: @*/
2344: PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao tao))
2345: {
2346: PetscFunctionBegin;
2347: PetscCall(TaoInitializePackage());
2348: PetscCall(PetscFunctionListAdd(&TaoList, sname, func));
2349: PetscFunctionReturn(PETSC_SUCCESS);
2350: }
2352: /*@
2353: TaoRegisterDestroy - Frees the list of minimization solvers that were
2354: registered by `TaoRegister()`.
2356: Not Collective
2358: Level: advanced
2360: .seealso: [](ch_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()`
2361: @*/
2362: PetscErrorCode TaoRegisterDestroy(void)
2363: {
2364: PetscFunctionBegin;
2365: PetscCall(PetscFunctionListDestroy(&TaoList));
2366: TaoRegisterAllCalled = PETSC_FALSE;
2367: PetscFunctionReturn(PETSC_SUCCESS);
2368: }
2370: /*@
2371: TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed
2372: at this time.
2374: Not Collective
2376: Input Parameter:
2377: . tao - the `Tao` context
2379: Output Parameter:
2380: . iter - iteration number
2382: Notes:
2383: For example, during the computation of iteration 2 this would return 1.
2385: Level: intermediate
2387: .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()`
2388: @*/
2389: PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter)
2390: {
2391: PetscFunctionBegin;
2393: PetscAssertPointer(iter, 2);
2394: *iter = tao->niter;
2395: PetscFunctionReturn(PETSC_SUCCESS);
2396: }
2398: /*@
2399: TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient)
2400: at this time.
2402: Not Collective
2404: Input Parameter:
2405: . tao - the `Tao` context
2407: Output Parameter:
2408: . value - the current value
2410: Level: intermediate
2412: Developer Notes:
2413: This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has
2414: a different meaning. For some reason `Tao` sometimes calls the gradient the residual.
2416: .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()`
2417: @*/
2418: PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value)
2419: {
2420: PetscFunctionBegin;
2422: PetscAssertPointer(value, 2);
2423: *value = tao->residual;
2424: PetscFunctionReturn(PETSC_SUCCESS);
2425: }
2427: /*@
2428: TaoSetIterationNumber - Sets the current iteration number.
2430: Logically Collective
2432: Input Parameters:
2433: + tao - the `Tao` context
2434: - iter - iteration number
2436: Level: developer
2438: .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
2439: @*/
2440: PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter)
2441: {
2442: PetscFunctionBegin;
2445: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
2446: tao->niter = iter;
2447: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
2448: PetscFunctionReturn(PETSC_SUCCESS);
2449: }
2451: /*@
2452: TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations
2453: completed. This number keeps accumulating if multiple solves
2454: are called with the `Tao` object.
2456: Not Collective
2458: Input Parameter:
2459: . tao - the `Tao` context
2461: Output Parameter:
2462: . iter - number of iterations
2464: Level: intermediate
2466: Note:
2467: The total iteration count is updated after each solve, if there is a current
2468: `TaoSolve()` in progress then those iterations are not included in the count
2470: .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
2471: @*/
2472: PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter)
2473: {
2474: PetscFunctionBegin;
2476: PetscAssertPointer(iter, 2);
2477: *iter = tao->ntotalits;
2478: PetscFunctionReturn(PETSC_SUCCESS);
2479: }
2481: /*@
2482: TaoSetTotalIterationNumber - Sets the current total iteration number.
2484: Logically Collective
2486: Input Parameters:
2487: + tao - the `Tao` context
2488: - iter - the iteration number
2490: Level: developer
2492: .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`
2493: @*/
2494: PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter)
2495: {
2496: PetscFunctionBegin;
2499: PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao));
2500: tao->ntotalits = iter;
2501: PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao));
2502: PetscFunctionReturn(PETSC_SUCCESS);
2503: }
2505: /*@
2506: TaoSetConvergedReason - Sets the termination flag on a `Tao` object
2508: Logically Collective
2510: Input Parameters:
2511: + tao - the `Tao` context
2512: - reason - the `TaoConvergedReason`
2514: Level: intermediate
2516: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`
2517: @*/
2518: PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason)
2519: {
2520: PetscFunctionBegin;
2523: tao->reason = reason;
2524: PetscFunctionReturn(PETSC_SUCCESS);
2525: }
2527: /*@
2528: TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped.
2530: Not Collective
2532: Input Parameter:
2533: . tao - the `Tao` solver context
2535: Output Parameter:
2536: . reason - value of `TaoConvergedReason`
2538: Level: intermediate
2540: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()`, `TaoGetConvergedReasonString()`
2541: @*/
2542: PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason)
2543: {
2544: PetscFunctionBegin;
2546: PetscAssertPointer(reason, 2);
2547: *reason = tao->reason;
2548: PetscFunctionReturn(PETSC_SUCCESS);
2549: }
2551: /*@
2552: TaoGetConvergedReasonString - Return a human readable string for a `TaoConvergedReason`
2554: Not Collective
2556: Input Parameter:
2557: . tao - the `Tao` solver context
2559: Output Parameter:
2560: . strreason - a human readable string that describes the `Tao` converged reason
2562: Level: beginner
2564: .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetConvergedReason()`
2565: @*/
2566: PetscErrorCode TaoGetConvergedReasonString(Tao tao, const char *strreason[])
2567: {
2568: PetscFunctionBegin;
2570: PetscAssertPointer(strreason, 2);
2571: *strreason = TaoConvergedReasons[tao->reason];
2572: PetscFunctionReturn(PETSC_SUCCESS);
2573: }
2575: /*@
2576: TaoGetSolutionStatus - Get the current iterate, objective value,
2577: residual, infeasibility, and termination from a `Tao` object
2579: Not Collective
2581: Input Parameter:
2582: . tao - the `Tao` context
2584: Output Parameters:
2585: + its - the current iterate number (>=0)
2586: . f - the current function value
2587: . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality.
2588: . cnorm - the infeasibility of the current solution with regard to the constraints.
2589: . xdiff - the step length or trust region radius of the most recent iterate.
2590: - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING`
2592: Level: intermediate
2594: Notes:
2595: Tao returns the values set by the solvers in the routine `TaoMonitor()`.
2597: If any of the output arguments are set to `NULL`, no corresponding value will be returned.
2599: .seealso: [](ch_tao), `TaoMonitor()`, `TaoGetConvergedReason()`
2600: @*/
2601: PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason)
2602: {
2603: PetscFunctionBegin;
2605: if (its) *its = tao->niter;
2606: if (f) *f = tao->fc;
2607: if (gnorm) *gnorm = tao->residual;
2608: if (cnorm) *cnorm = tao->cnorm;
2609: if (reason) *reason = tao->reason;
2610: if (xdiff) *xdiff = tao->step;
2611: PetscFunctionReturn(PETSC_SUCCESS);
2612: }
2614: /*@
2615: TaoGetType - Gets the current `TaoType` being used in the `Tao` object
2617: Not Collective
2619: Input Parameter:
2620: . tao - the `Tao` solver context
2622: Output Parameter:
2623: . type - the `TaoType`
2625: Level: intermediate
2627: Note:
2628: `type` should not be retained for later use as it will be an invalid pointer if the `TaoType` of `tao` is changed.
2630: .seealso: [](ch_tao), `Tao`, `TaoType`, `TaoSetType()`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`
2631: @*/
2632: PetscErrorCode TaoGetType(Tao tao, TaoType *type)
2633: {
2634: PetscFunctionBegin;
2636: PetscAssertPointer(type, 2);
2637: *type = ((PetscObject)tao)->type_name;
2638: PetscFunctionReturn(PETSC_SUCCESS);
2639: }
2641: /*@
2642: TaoMonitor - Monitor the solver and the current solution. This
2643: routine will record the iteration number and residual statistics,
2644: and call any monitors specified by the user.
2646: Input Parameters:
2647: + tao - the `Tao` context
2648: . its - the current iterate number (>=0)
2649: . f - the current objective function value
2650: . res - the gradient norm, square root of the duality gap, or other measure indicating distance from optimality. This measure will be recorded and
2651: used for some termination tests.
2652: . cnorm - the infeasibility of the current solution with regard to the constraints.
2653: - steplength - multiple of the step direction added to the previous iterate.
2655: Options Database Key:
2656: . -tao_monitor - Use the default monitor, which prints statistics to standard output
2658: Level: developer
2660: .seealso: [](ch_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoMonitorSet()`
2661: @*/
2662: PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength)
2663: {
2664: PetscFunctionBegin;
2666: tao->fc = f;
2667: tao->residual = res;
2668: tao->cnorm = cnorm;
2669: tao->step = steplength;
2670: if (!its) {
2671: tao->cnorm0 = cnorm;
2672: tao->gnorm0 = res;
2673: }
2674: PetscCall(VecLockReadPush(tao->solution));
2675: for (PetscInt i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i]));
2676: PetscCall(VecLockReadPop(tao->solution));
2677: PetscFunctionReturn(PETSC_SUCCESS);
2678: }
2680: /*@
2681: TaoSetConvergenceHistory - Sets the array used to hold the convergence history.
2683: Logically Collective
2685: Input Parameters:
2686: + tao - the `Tao` solver context
2687: . obj - array to hold objective value history
2688: . resid - array to hold residual history
2689: . cnorm - array to hold constraint violation history
2690: . lits - integer array holds the number of linear iterations for each Tao iteration
2691: . na - size of `obj`, `resid`, and `cnorm`
2692: - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero,
2693: else it continues storing new values for new minimizations after the old ones
2695: Level: intermediate
2697: Notes:
2698: If set, `Tao` will fill the given arrays with the indicated
2699: information at each iteration. If 'obj','resid','cnorm','lits' are
2700: *all* `NULL` then space (using size `na`, or 1000 if `na` is `PETSC_DECIDE`) is allocated for the history.
2701: If not all are `NULL`, then only the non-`NULL` information categories
2702: will be stored, the others will be ignored.
2704: Any convergence information after iteration number 'na' will not be stored.
2706: This routine is useful, e.g., when running a code for purposes
2707: of accurate performance monitoring, when no I/O should be done
2708: during the section of code that is being timed.
2710: .seealso: [](ch_tao), `TaoGetConvergenceHistory()`
2711: @*/
2712: PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset)
2713: {
2714: PetscFunctionBegin;
2716: if (obj) PetscAssertPointer(obj, 2);
2717: if (resid) PetscAssertPointer(resid, 3);
2718: if (cnorm) PetscAssertPointer(cnorm, 4);
2719: if (lits) PetscAssertPointer(lits, 5);
2721: if (na == PETSC_DECIDE || na == PETSC_CURRENT) na = 1000;
2722: if (!obj && !resid && !cnorm && !lits) {
2723: PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits));
2724: tao->hist_malloc = PETSC_TRUE;
2725: }
2727: tao->hist_obj = obj;
2728: tao->hist_resid = resid;
2729: tao->hist_cnorm = cnorm;
2730: tao->hist_lits = lits;
2731: tao->hist_max = na;
2732: tao->hist_reset = reset;
2733: tao->hist_len = 0;
2734: PetscFunctionReturn(PETSC_SUCCESS);
2735: }
2737: /*@
2738: TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history.
2740: Collective
2742: Input Parameter:
2743: . tao - the `Tao` context
2745: Output Parameters:
2746: + obj - array used to hold objective value history
2747: . resid - array used to hold residual history
2748: . cnorm - array used to hold constraint violation history
2749: . lits - integer array used to hold linear solver iteration count
2750: - nhist - size of `obj`, `resid`, `cnorm`, and `lits`
2752: Level: advanced
2754: Notes:
2755: This routine must be preceded by calls to `TaoSetConvergenceHistory()`
2756: and `TaoSolve()`, otherwise it returns useless information.
2758: This routine is useful, e.g., when running a code for purposes
2759: of accurate performance monitoring, when no I/O should be done
2760: during the section of code that is being timed.
2762: Fortran Notes:
2763: The calling sequence is
2764: .vb
2765: call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr)
2766: .ve
2767: In other words this gets the current number of entries in the history. Access the history through the array you passed to `TaoSetConvergenceHistory()`
2769: .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()`
2770: @*/
2771: PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist)
2772: {
2773: PetscFunctionBegin;
2775: if (obj) *obj = tao->hist_obj;
2776: if (cnorm) *cnorm = tao->hist_cnorm;
2777: if (resid) *resid = tao->hist_resid;
2778: if (lits) *lits = tao->hist_lits;
2779: if (nhist) *nhist = tao->hist_len;
2780: PetscFunctionReturn(PETSC_SUCCESS);
2781: }
2783: /*@
2784: TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver that can be accessed later, for example in the
2785: `Tao` callback functions with `TaoGetApplicationContext()`
2787: Logically Collective
2789: Input Parameters:
2790: + tao - the `Tao` context
2791: - ctx - the application context
2793: Level: intermediate
2795: Fortran Note:
2796: This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this
2797: function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `TaoGetApplicationContext()` for
2798: an example.
2800: .seealso: [](ch_tao), `Tao`, `TaoGetApplicationContext()`
2801: @*/
2802: PetscErrorCode TaoSetApplicationContext(Tao tao, PetscCtx ctx)
2803: {
2804: PetscFunctionBegin;
2806: tao->ctx = ctx;
2807: PetscFunctionReturn(PETSC_SUCCESS);
2808: }
2810: /*@
2811: TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver provided with `TaoSetApplicationContext()`
2813: Not Collective
2815: Input Parameter:
2816: . tao - the `Tao` context
2818: Output Parameter:
2819: . ctx - a pointer to the application context
2821: Level: intermediate
2823: Fortran Note:
2824: This only works when the context is a Fortran derived type or a `PetscObject`. Define `ctx` with
2825: .vb
2826: type(tUsertype), pointer :: ctx
2827: .ve
2829: .seealso: [](ch_tao), `Tao`, `TaoSetApplicationContext()`
2830: @*/
2831: PetscErrorCode TaoGetApplicationContext(Tao tao, PetscCtxRt ctx)
2832: {
2833: PetscFunctionBegin;
2835: PetscAssertPointer(ctx, 2);
2836: *(void **)ctx = tao->ctx;
2837: PetscFunctionReturn(PETSC_SUCCESS);
2838: }
2840: /*@
2841: TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient in some of the `Tao` algorithms
2843: Collective
2845: Input Parameters:
2846: + tao - the `Tao` context
2847: - M - matrix that defines the norm
2849: Level: beginner
2851: .seealso: [](ch_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()`
2852: @*/
2853: PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M)
2854: {
2855: PetscFunctionBegin;
2858: PetscCall(PetscObjectReference((PetscObject)M));
2859: PetscCall(MatDestroy(&tao->gradient_norm));
2860: PetscCall(VecDestroy(&tao->gradient_norm_tmp));
2861: tao->gradient_norm = M;
2862: PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp));
2863: PetscFunctionReturn(PETSC_SUCCESS);
2864: }
2866: /*@
2867: TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient in some of the `Tao` algorithms
2869: Not Collective
2871: Input Parameter:
2872: . tao - the `Tao` context
2874: Output Parameter:
2875: . M - gradient norm
2877: Level: beginner
2879: .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()`
2880: @*/
2881: PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M)
2882: {
2883: PetscFunctionBegin;
2885: PetscAssertPointer(M, 2);
2886: *M = tao->gradient_norm;
2887: PetscFunctionReturn(PETSC_SUCCESS);
2888: }
2890: /*@
2891: TaoGradientNorm - Compute the norm using the `NormType`, the user has selected
2893: Collective
2895: Input Parameters:
2896: + tao - the `Tao` context
2897: . gradient - the gradient
2898: - type - the norm type
2900: Output Parameter:
2901: . gnorm - the gradient norm
2903: Level: advanced
2905: Note:
2906: If `TaoSetGradientNorm()` has been set and `type` is `NORM_2` then the norm provided with `TaoSetGradientNorm()` is used.
2908: Developer Notes:
2909: Should be named `TaoComputeGradientNorm()`.
2911: The usage is a bit confusing, with `TaoSetGradientNorm()` plus `NORM_2` resulting in the computation of the user provided
2912: norm, perhaps a refactorization is in order.
2914: .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()`
2915: @*/
2916: PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm)
2917: {
2918: PetscFunctionBegin;
2922: PetscAssertPointer(gnorm, 4);
2923: if (tao->gradient_norm) {
2924: PetscScalar gnorms;
2926: 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.");
2927: PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp));
2928: PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms));
2929: *gnorm = PetscRealPart(PetscSqrtScalar(gnorms));
2930: } else {
2931: PetscCall(VecNorm(gradient, type, gnorm));
2932: }
2933: PetscFunctionReturn(PETSC_SUCCESS);
2934: }
2936: /*@
2937: TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorSolutionDraw()`
2939: Collective
2941: Input Parameters:
2942: + comm - the communicator to share the context
2943: . host - the name of the X Windows host that will display the monitor
2944: . label - the label to put at the top of the display window
2945: . x - the horizontal coordinate of the lower left corner of the window to open
2946: . y - the vertical coordinate of the lower left corner of the window to open
2947: . m - the width of the window
2948: . n - the height of the window
2949: - howoften - how many `Tao` iterations between displaying the monitor information
2951: Output Parameter:
2952: . ctx - the monitor context
2954: Options Database Keys:
2955: + -tao_monitor_solution_draw - use `TaoMonitorSolutionDraw()` to monitor the solution
2956: - -tao_draw_solution_initial - show initial guess as well as current solution
2958: Level: intermediate
2960: Note:
2961: The context this creates, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()`
2962: are passed to `TaoMonitorSet()`.
2964: .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()`
2965: @*/
2966: PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx)
2967: {
2968: PetscFunctionBegin;
2969: PetscCall(PetscNew(ctx));
2970: PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer));
2971: PetscCall(PetscViewerSetFromOptions((*ctx)->viewer));
2972: (*ctx)->howoften = howoften;
2973: PetscFunctionReturn(PETSC_SUCCESS);
2974: }
2976: /*@
2977: TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorSolutionDraw()`
2979: Collective
2981: Input Parameter:
2982: . ictx - the monitor context
2984: Level: intermediate
2986: Note:
2987: This is passed to `TaoMonitorSet()` as the final argument, along with `TaoMonitorSolutionDraw()`, and the context
2988: obtained with `TaoMonitorDrawCtxCreate()`.
2990: .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorSolutionDraw()`
2991: @*/
2992: PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx)
2993: {
2994: PetscFunctionBegin;
2995: PetscCall(PetscViewerDestroy(&(*ictx)->viewer));
2996: PetscCall(PetscFree(*ictx));
2997: PetscFunctionReturn(PETSC_SUCCESS);
2998: }
3000: /*@
3001: TaoGetTerm - Get the entire objective function of the `Tao` as a
3002: single `TaoTerm` in the form $\alpha f(Ax; p)$, where $\alpha$ is a scaling
3003: coefficient, $f$ is a `TaoTerm`, $A$ is an (optional) map and $p$ are the parameters of $f$.
3005: Not collective
3007: Input Parameter:
3008: . tao - a `Tao` context
3010: Output Parameters:
3011: + scale - the scale of the term
3012: . term - a `TaoTerm` for the real-valued function defining the objective
3013: . params - the vector of parameters for `term`, or `NULL` if no parameters were specified for `term`
3014: - map - a map from the solution space of `tao` to the solution space of `term`, if `NULL` then the map is the identity
3016: Level: intermediate
3018: Notes:
3019: If the objective function was defined by providing function callbacks directly to `Tao` (for example, with `TaoSetObjectiveAndGradient()`), then
3020: `TaoGetTerm` will return a `TaoTerm` with the type `TAOTERMCALLBACKS` that encapsulates
3021: those functions.
3023: If multiple `TaoTerms` were provided to `Tao` via, for example, `TaoAddTerm()`, or in combination with giving functions directly to `Tao`, then the type `TAOTERMSUM` is returned.
3025: .seealso: [](ch_tao), `Tao`, `TaoTerm`, `TAOTERMSUM`, `TaoAddTerm()`
3026: @*/
3027: PetscErrorCode TaoGetTerm(Tao tao, PetscReal *scale, TaoTerm *term, Vec *params, Mat *map)
3028: {
3029: PetscFunctionBegin;
3031: if (scale) PetscAssertPointer(scale, 2);
3032: if (term) PetscAssertPointer(term, 3);
3033: if (params) PetscAssertPointer(params, 4);
3034: if (map) PetscAssertPointer(map, 5);
3035: PetscCall(TaoTermMappingGetData(&tao->objective_term, NULL, scale, term, map));
3036: if (params) *params = tao->objective_parameters;
3037: PetscFunctionReturn(PETSC_SUCCESS);
3038: }
3040: /*@
3041: TaoAddTerm - Add a `term` to the objective function. If `Tao` is empty,
3042: `term` will be the objective of `Tao`.
3044: Collective
3046: Input Parameters:
3047: + tao - a `Tao` solver context
3048: . prefix - the prefix used for configuring the new term (if `NULL`, the index of the term will be used as a prefix, e.g. "0_", "1_", etc.)
3049: . scale - scaling coefficient for the new term
3050: . term - the real-valued function defining the new term
3051: . params - (optional) parameters for the new term. It is up to each implementation of `TaoTerm` to determine how it behaves when parameters are omitted.
3052: - map - (optional) a map from the `tao` solution space to the `term` solution space; if `NULL` the map is assumed to be the identity
3054: Level: beginner
3056: Notes:
3057: If the objective function was $f(x)$, after calling `TaoAddTerm()` it becomes
3058: $f(x) + \alpha g(Ax; p)$, where $\alpha$ is the `scale`, $g$ is the `term`, $A$ is the
3059: (optional) `map`, and $p$ are the (optional) `params` of $g$.
3061: The `map` $A$ transforms the `Tao` solution vector into the term's solution space.
3062: For example, if the `Tao` solution vector is $x \in \mathbb{R}^n$ and the mapping
3063: matrix is $A \in \mathbb{R}^{m \times n}$, then the term evaluates $g(Ax; p)$ with
3064: $Ax \in \mathbb{R}^m$. The term's solution space is therefore $\mathbb{R}^m$. If the map is
3065: `NULL`, the identity is used and the term's solution space must match the `Tao` solution space.
3066: `Tao` automatically applies the chain rule for gradients ($A^T \nabla g$) and Hessians
3067: ($A^T \nabla^2 g \, A$) with respect to $x$.
3069: The `params` $p$ are fixed data that are not optimized over. Some `TaoTermType`s
3070: require the parameter space to be related to the term's solution space (e.g., the same
3071: size); when a mapping matrix $A$ is used, the parameter space may depend on either the row
3072: or column space of $A$. See the documentation for each `TaoTermType`.
3074: Currently, `TaoAddTerm()` does not support bounded Newton solvers (`TAOBNK`,`TAOBNLS`,`TAOBNTL`,`TAOBNTR`,and `TAOBQNK`)
3076: .seealso: [](ch_tao), `Tao`, `TaoTerm`, `TAOTERMSUM`, `TaoGetTerm()`
3077: @*/
3078: PetscErrorCode TaoAddTerm(Tao tao, const char prefix[], PetscReal scale, TaoTerm term, Vec params, Mat map)
3079: {
3080: PetscBool is_sum, is_callback;
3081: PetscInt num_old_terms;
3082: Vec *vec_list = NULL;
3084: PetscFunctionBegin;
3086: if (prefix) PetscAssertPointer(prefix, 2);
3089: PetscCheckSameComm(tao, 1, term, 4);
3090: if (params) {
3092: PetscCheckSameComm(tao, 1, params, 5);
3093: }
3094: if (map) {
3096: PetscCheckSameComm(tao, 1, map, 6);
3097: }
3098: // If user is using TaoAddTerm, before setting any terms or callbacks,
3099: // then tao->objective_term.term is empty callback, which we want to remove.
3100: PetscCall(PetscObjectTypeCompare((PetscObject)tao->objective_term.term, TAOTERMCALLBACKS, &is_callback));
3101: PetscCall(PetscObjectTypeCompare((PetscObject)term, TAOTERMSUM, &is_sum));
3102: PetscCheck(!is_sum, PetscObjectComm((PetscObject)term), PETSC_ERR_ARG_WRONG, "TaoAddTerm does not support adding TAOTERMSUM");
3103: if (is_callback) {
3104: PetscBool is_obj, is_objgrad, is_grad;
3106: PetscCall(TaoTermIsObjectiveDefined(tao->objective_term.term, &is_obj));
3107: PetscCall(TaoTermIsObjectiveAndGradientDefined(tao->objective_term.term, &is_objgrad));
3108: PetscCall(TaoTermIsGradientDefined(tao->objective_term.term, &is_grad));
3109: // Empty callback term
3110: if (!(is_obj || is_objgrad || is_grad)) {
3111: PetscCall(TaoTermMappingSetData(&tao->objective_term, NULL, scale, term, map));
3112: PetscCall(PetscObjectReference((PetscObject)params));
3113: PetscCall(VecDestroy(&tao->objective_parameters));
3114: // Empty callback term. Destroy hessians, as they are not needed
3115: PetscCall(MatDestroy(&tao->hessian));
3116: PetscCall(MatDestroy(&tao->hessian_pre));
3117: tao->objective_parameters = params;
3118: tao->term_set = PETSC_TRUE;
3119: PetscFunctionReturn(PETSC_SUCCESS);
3120: }
3121: }
3122: PetscCall(PetscObjectTypeCompare((PetscObject)tao->objective_term.term, TAOTERMSUM, &is_sum));
3123: // One TaoTerm has been set. Create TAOTERMSUM to store that, and the new one
3124: if (!is_sum) {
3125: TaoTerm old_sum;
3126: const char *tao_prefix;
3127: const char *term_prefix;
3129: PetscCall(TaoTermDuplicate(tao->objective_term.term, TAOTERM_DUPLICATE_SIZEONLY, &old_sum));
3130: if (tao->objective_term.map) {
3131: VecType map_vectype;
3132: VecType param_vectype;
3133: PetscLayout cmap, param_layout;
3135: PetscCall(MatGetVecType(tao->objective_term.map, &map_vectype));
3136: PetscCall(MatGetLayouts(tao->objective_term.map, NULL, &cmap));
3137: PetscCall(TaoTermGetParametersVecType(old_sum, ¶m_vectype));
3138: PetscCall(TaoTermGetParametersLayout(old_sum, ¶m_layout));
3140: PetscCall(TaoTermSetSolutionVecType(old_sum, map_vectype));
3141: PetscCall(TaoTermSetParametersVecType(old_sum, param_vectype));
3142: PetscCall(TaoTermSetSolutionLayout(old_sum, cmap));
3143: PetscCall(TaoTermSetParametersLayout(old_sum, param_layout));
3144: }
3146: PetscCall(TaoTermSetType(old_sum, TAOTERMSUM));
3147: PetscCall(TaoGetOptionsPrefix(tao, &tao_prefix));
3148: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)old_sum, tao_prefix));
3149: PetscCall(TaoTermSumSetNumberTerms(old_sum, 1));
3150: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao->objective_term.term, &term_prefix));
3151: PetscCall(TaoTermSumSetTerm(old_sum, 0, term_prefix, tao->objective_term.scale, tao->objective_term.term, tao->objective_term.map));
3152: PetscCall(TaoTermSumSetTermHessianMatrices(old_sum, 0, NULL, NULL, tao->hessian, tao->hessian_pre));
3153: PetscCall(MatDestroy(&tao->hessian));
3154: PetscCall(MatDestroy(&tao->hessian_pre));
3155: PetscCall(TaoTermMappingReset(&tao->objective_term));
3156: PetscCall(TaoTermMappingSetData(&tao->objective_term, NULL, 1.0, old_sum, NULL));
3157: if (tao->objective_parameters) {
3158: // convert the parameters to a VECNEST
3159: Vec subvecs[1];
3161: subvecs[0] = tao->objective_parameters;
3162: tao->objective_parameters = NULL;
3163: PetscCall(TaoTermSumParametersPack(old_sum, subvecs, &tao->objective_parameters));
3164: PetscCall(VecDestroy(&subvecs[0]));
3165: }
3166: PetscCall(TaoTermDestroy(&old_sum));
3167: tao->num_terms = 1;
3168: }
3169: PetscCall(TaoTermSumGetNumberTerms(tao->objective_term.term, &num_old_terms));
3170: if (tao->objective_parameters || params) {
3171: PetscCall(PetscCalloc1(num_old_terms + 1, &vec_list));
3172: if (tao->objective_parameters) PetscCall(TaoTermSumParametersUnpack(tao->objective_term.term, &tao->objective_parameters, vec_list));
3173: PetscCall(PetscObjectReference((PetscObject)params));
3174: vec_list[num_old_terms] = params;
3175: }
3176: PetscCall(TaoTermSumAddTerm(tao->objective_term.term, prefix, scale, term, map, NULL));
3177: tao->num_terms++;
3178: if (vec_list) {
3179: PetscInt num_terms = num_old_terms + 1;
3180: PetscCall(TaoTermSumParametersPack(tao->objective_term.term, vec_list, &tao->objective_parameters));
3181: for (PetscInt i = 0; i < num_terms; i++) PetscCall(VecDestroy(&vec_list[i]));
3182: PetscCall(PetscFree(vec_list));
3183: }
3184: PetscFunctionReturn(PETSC_SUCCESS);
3185: }
3187: /*@
3188: TaoSetDM - Sets the `DM` that may be used by some `TAO` solvers or their underlying solvers and preconditioners
3190: Logically Collective
3192: Input Parameters:
3193: + tao - the nonlinear solver context
3194: - dm - the `DM`, cannot be `NULL`
3196: Level: intermediate
3198: Note:
3199: A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`,
3200: even when not using interfaces like `DMSNESSetFunction()`. Use `DMClone()` to get a distinct `DM` when solving different
3201: problems using the same function space.
3203: .seealso: [](ch_snes), `DM`, `TAO`, `TaoGetDM()`, `SNESSetDM()`, `SNESGetDM()`, `KSPSetDM()`, `KSPGetDM()`
3204: @*/
3205: PetscErrorCode TaoSetDM(Tao tao, DM dm)
3206: {
3207: KSP ksp;
3209: PetscFunctionBegin;
3212: PetscCall(PetscObjectReference((PetscObject)dm));
3213: PetscCall(DMDestroy(&tao->dm));
3214: tao->dm = dm;
3216: PetscCall(TaoGetKSP(tao, &ksp));
3217: if (ksp) {
3218: PetscCall(KSPSetDM(ksp, dm));
3219: PetscCall(KSPSetDMActive(ksp, KSP_DMACTIVE_ALL, PETSC_FALSE));
3220: }
3221: PetscFunctionReturn(PETSC_SUCCESS);
3222: }
3224: /*@
3225: TaoGetDM - Gets the `DM` that may be used by some `TAO` solvers or their underlying solvers and preconditioners
3227: Not Collective but `dm` obtained is parallel on `tao`
3229: Input Parameter:
3230: . tao - the `TAO` context
3232: Output Parameter:
3233: . dm - the `DM`
3235: Level: intermediate
3237: .seealso: [](ch_snes), `DM`, `TAO`, `TaoSetDM()`, `SNESSetDM()`, `SNESGetDM()`, `KSPSetDM()`, `KSPGetDM()`
3238: @*/
3239: PetscErrorCode TaoGetDM(Tao tao, DM *dm)
3240: {
3241: PetscFunctionBegin;
3243: PetscAssertPointer(dm, 2);
3244: if (!tao->dm) PetscCall(DMShellCreate(PetscObjectComm((PetscObject)tao), &tao->dm));
3245: *dm = tao->dm;
3246: PetscFunctionReturn(PETSC_SUCCESS);
3247: }