1af0996ceSBarry Smith #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 20f0abf79SStefano Zampini #include <petsc/private/snesimpl.h> 3a7e14dcfSSatish Balay 4441846f8SBarry Smith PetscBool TaoRegisterAllCalled = PETSC_FALSE; 5441846f8SBarry Smith PetscFunctionList TaoList = NULL; 6a7e14dcfSSatish Balay 7441846f8SBarry Smith PetscClassId TAO_CLASSID; 80ebee16dSLisandro Dalcin 90ebee16dSLisandro Dalcin PetscLogEvent TAO_Solve; 100ebee16dSLisandro Dalcin PetscLogEvent TAO_ObjectiveEval; 110ebee16dSLisandro Dalcin PetscLogEvent TAO_GradientEval; 120ebee16dSLisandro Dalcin PetscLogEvent TAO_ObjGradEval; 130ebee16dSLisandro Dalcin PetscLogEvent TAO_HessianEval; 140ebee16dSLisandro Dalcin PetscLogEvent TAO_JacobianEval; 150ebee16dSLisandro Dalcin PetscLogEvent TAO_ConstraintsEval; 16a7e14dcfSSatish Balay 1783c8fe1dSLisandro Dalcin const char *TaoSubSetTypes[] = {"subvec", "mask", "matrixfree", "TaoSubSetType", "TAO_SUBSET_", NULL}; 18a7e14dcfSSatish Balay 19e882e171SHong Zhang struct _n_TaoMonitorDrawCtx { 20e882e171SHong Zhang PetscViewer viewer; 21e882e171SHong Zhang PetscInt howoften; /* when > 0 uses iteration % howoften, when negative only final solution plotted */ 22e882e171SHong Zhang }; 23e882e171SHong Zhang 24f1e99121SPierre Jolivet static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, void *ctx) 25d71ae5a4SJacob Faibussowitsch { 26f1e99121SPierre Jolivet Tao tao = (Tao)ctx; 270f0abf79SStefano Zampini SNES snes_ewdummy = tao->snes_ewdummy; 280f0abf79SStefano Zampini 290f0abf79SStefano Zampini PetscFunctionBegin; 303ba16761SJacob Faibussowitsch if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS); 310f0abf79SStefano Zampini /* populate snes_ewdummy struct values used in KSPPreSolve_SNESEW */ 320f0abf79SStefano Zampini snes_ewdummy->vec_func = b; 330f0abf79SStefano Zampini snes_ewdummy->rtol = tao->gttol; 340f0abf79SStefano Zampini snes_ewdummy->iter = tao->niter; 350f0abf79SStefano Zampini PetscCall(VecNorm(b, NORM_2, &snes_ewdummy->norm)); 360f0abf79SStefano Zampini PetscCall(KSPPreSolve_SNESEW(ksp, b, x, snes_ewdummy)); 370f0abf79SStefano Zampini snes_ewdummy->vec_func = NULL; 383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 390f0abf79SStefano Zampini } 400f0abf79SStefano Zampini 41f1e99121SPierre Jolivet static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, void *ctx) 42d71ae5a4SJacob Faibussowitsch { 43f1e99121SPierre Jolivet Tao tao = (Tao)ctx; 440f0abf79SStefano Zampini SNES snes_ewdummy = tao->snes_ewdummy; 450f0abf79SStefano Zampini 460f0abf79SStefano Zampini PetscFunctionBegin; 473ba16761SJacob Faibussowitsch if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS); 480f0abf79SStefano Zampini PetscCall(KSPPostSolve_SNESEW(ksp, b, x, snes_ewdummy)); 493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 500f0abf79SStefano Zampini } 510f0abf79SStefano Zampini 52d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSetUpEW_Private(Tao tao) 53d71ae5a4SJacob Faibussowitsch { 540f0abf79SStefano Zampini SNESKSPEW *kctx; 550f0abf79SStefano Zampini const char *ewprefix; 560f0abf79SStefano Zampini 570f0abf79SStefano Zampini PetscFunctionBegin; 583ba16761SJacob Faibussowitsch if (!tao->ksp) PetscFunctionReturn(PETSC_SUCCESS); 590f0abf79SStefano Zampini if (tao->ksp_ewconv) { 600f0abf79SStefano Zampini if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao), &tao->snes_ewdummy)); 610f0abf79SStefano Zampini tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE; 62f1e99121SPierre Jolivet PetscCall(KSPSetPreSolve(tao->ksp, KSPPreSolve_TAOEW_Private, tao)); 63f1e99121SPierre Jolivet PetscCall(KSPSetPostSolve(tao->ksp, KSPPostSolve_TAOEW_Private, tao)); 640f0abf79SStefano Zampini 650f0abf79SStefano Zampini PetscCall(KSPGetOptionsPrefix(tao->ksp, &ewprefix)); 660f0abf79SStefano Zampini kctx = (SNESKSPEW *)tao->snes_ewdummy->kspconvctx; 67a4598233SStefano Zampini PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_FALSE, PetscObjectComm((PetscObject)tao), ewprefix)); 680f0abf79SStefano Zampini } else PetscCall(SNESDestroy(&tao->snes_ewdummy)); 693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 700f0abf79SStefano Zampini } 710f0abf79SStefano Zampini 72a7e14dcfSSatish Balay /*@ 7365ba42b6SBarry Smith TaoCreate - Creates a Tao solver 74a7e14dcfSSatish Balay 75d083f849SBarry Smith Collective 76a7e14dcfSSatish Balay 77a7e14dcfSSatish Balay Input Parameter: 78a7e14dcfSSatish Balay . comm - MPI communicator 79a7e14dcfSSatish Balay 80a7e14dcfSSatish Balay Output Parameter: 8147450a7bSBarry Smith . newtao - the new `Tao` context 82a7e14dcfSSatish Balay 8347450a7bSBarry Smith Options Database Key: 8465ba42b6SBarry Smith . -tao_type - select which method Tao should use 85a7e14dcfSSatish Balay 86a7e14dcfSSatish Balay Level: beginner 87a7e14dcfSSatish Balay 881cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TAOSetFromOptions()`, `TAOSetType()` 89a7e14dcfSSatish Balay @*/ 90d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao) 91d71ae5a4SJacob Faibussowitsch { 92441846f8SBarry Smith Tao tao; 93a7e14dcfSSatish Balay 94a7e14dcfSSatish Balay PetscFunctionBegin; 954f572ea9SToby Isaac PetscAssertPointer(newtao, 2); 969566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 979566063dSJacob Faibussowitsch PetscCall(TaoLineSearchInitializePackage()); 989566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView)); 99a7e14dcfSSatish Balay 10094511df7SStefano Zampini /* Set non-NULL defaults */ 10194511df7SStefano Zampini tao->ops->convergencetest = TaoDefaultConvergenceTest; 102a7e14dcfSSatish Balay 103a7e14dcfSSatish Balay tao->max_it = 10000; 10494511df7SStefano Zampini tao->max_funcs = -1; 1056f4723b1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 1066f4723b1SBarry Smith tao->gatol = 1e-5; 1076f4723b1SBarry Smith tao->grtol = 1e-5; 1086f1f5f59SAlp Dener tao->crtol = 1e-5; 1096f1f5f59SAlp Dener tao->catol = 1e-5; 1106f4723b1SBarry Smith #else 111a7e14dcfSSatish Balay tao->gatol = 1e-8; 112a7e14dcfSSatish Balay tao->grtol = 1e-8; 1136f1f5f59SAlp Dener tao->crtol = 1e-8; 1146f1f5f59SAlp Dener tao->catol = 1e-8; 1156f4723b1SBarry Smith #endif 1166552cf8aSJason Sarich tao->gttol = 0.0; 1174f1535bcSTodd Munson tao->steptol = 0.0; 118e270355aSBarry Smith tao->trust0 = PETSC_INFINITY; 1196f4723b1SBarry Smith tao->fmin = PETSC_NINFINITY; 1209fa2b5dcSStefano Zampini 121a7e14dcfSSatish Balay tao->hist_reset = PETSC_TRUE; 122a7e14dcfSSatish Balay 1239566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 124a7e14dcfSSatish Balay *newtao = tao; 1253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 126a7e14dcfSSatish Balay } 127a7e14dcfSSatish Balay 128a7e14dcfSSatish Balay /*@ 129a7e14dcfSSatish Balay TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u 130a7e14dcfSSatish Balay 131c3339decSBarry Smith Collective 132a7e14dcfSSatish Balay 13347450a7bSBarry Smith Input Parameter: 13447450a7bSBarry Smith . tao - the `Tao` context 135a7e14dcfSSatish Balay 13667be906fSBarry Smith Level: beginner 13767be906fSBarry Smith 138a7e14dcfSSatish Balay Notes: 13947450a7bSBarry Smith The user must set up the `Tao` object with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`. 140a7e14dcfSSatish Balay 14167be906fSBarry Smith You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or 142a35d58b8SBarry Smith why it failed. 143a35d58b8SBarry Smith 1441cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()` 145a7e14dcfSSatish Balay @*/ 146d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolve(Tao tao) 147d71ae5a4SJacob Faibussowitsch { 148e2379f4fSBarry Smith static PetscBool set = PETSC_FALSE; 14947a47007SBarry Smith 150a7e14dcfSSatish Balay PetscFunctionBegin; 151441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 152d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n" 153e2379f4fSBarry Smith "title = {Toolkit for Advanced Optimization (TAO) Users Manual},\n" 154e2379f4fSBarry Smith "author = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n" 155e2379f4fSBarry Smith "Institution = {Argonne National Laboratory},\n" 156e2379f4fSBarry Smith "Year = 2014,\n" 157e2379f4fSBarry Smith "Number = {ANL/MCS-TM-322 - Revision 3.5},\n" 1589371c9d4SSatish Balay "url = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n", 1599371c9d4SSatish Balay &set)); 160494bef23SAlp Dener tao->header_printed = PETSC_FALSE; 1619566063dSJacob Faibussowitsch PetscCall(TaoSetUp(tao)); 1629566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 1631baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch)); 164a7e14dcfSSatish Balay 1659566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0)); 166dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, solve); 1679566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0)); 168a7e14dcfSSatish Balay 1699566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution")); 1707cf37e64SBarry Smith 1718931d482SJason Sarich tao->ntotalits += tao->niter; 172a7e14dcfSSatish Balay 173a7e14dcfSSatish Balay if (tao->printreason) { 174f642d338SBarry Smith PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm); 175f642d338SBarry Smith PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel)); 176a7e14dcfSSatish Balay if (tao->reason > 0) { 177f642d338SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter)); 178a7e14dcfSSatish Balay } else { 179f642d338SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter)); 180a7e14dcfSSatish Balay } 181f642d338SBarry Smith PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel)); 182a7e14dcfSSatish Balay } 183f642d338SBarry Smith PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view")); 1843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 185a7e14dcfSSatish Balay } 186a7e14dcfSSatish Balay 187a7e14dcfSSatish Balay /*@ 188a7e14dcfSSatish Balay TaoSetUp - Sets up the internal data structures for the later use 189a7e14dcfSSatish Balay of a Tao solver 190a7e14dcfSSatish Balay 191c3339decSBarry Smith Collective 192a7e14dcfSSatish Balay 19347450a7bSBarry Smith Input Parameter: 19447450a7bSBarry Smith . tao - the `Tao` context 195a7e14dcfSSatish Balay 19667be906fSBarry Smith Level: advanced 19767be906fSBarry Smith 19847450a7bSBarry Smith Note: 19965ba42b6SBarry Smith The user will not need to explicitly call `TaoSetUp()`, as it will 20065ba42b6SBarry Smith automatically be called in `TaoSolve()`. However, if the user 20165ba42b6SBarry Smith desires to call it explicitly, it should come after `TaoCreate()` 20265ba42b6SBarry Smith and any TaoSetSomething() routines, but before `TaoSolve()`. 203a7e14dcfSSatish Balay 2041cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 205a7e14dcfSSatish Balay @*/ 206d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp(Tao tao) 207d71ae5a4SJacob Faibussowitsch { 208a7e14dcfSSatish Balay PetscFunctionBegin; 209441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2103ba16761SJacob Faibussowitsch if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2110f0abf79SStefano Zampini PetscCall(TaoSetUpEW_Private(tao)); 2123c859ba3SBarry Smith PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution"); 213dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setup); 214a7e14dcfSSatish Balay tao->setupcalled = PETSC_TRUE; 2153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 216a7e14dcfSSatish Balay } 217a7e14dcfSSatish Balay 2181fb7b255SJunchao Zhang /*@C 21947450a7bSBarry Smith TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()` 220a7e14dcfSSatish Balay 221c3339decSBarry Smith Collective 222a7e14dcfSSatish Balay 223a7e14dcfSSatish Balay Input Parameter: 22447450a7bSBarry Smith . tao - the `Tao` context 225a7e14dcfSSatish Balay 226a7e14dcfSSatish Balay Level: beginner 227a7e14dcfSSatish Balay 2281cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 229a7e14dcfSSatish Balay @*/ 230d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy(Tao *tao) 231d71ae5a4SJacob Faibussowitsch { 232a7e14dcfSSatish Balay PetscFunctionBegin; 2333ba16761SJacob Faibussowitsch if (!*tao) PetscFunctionReturn(PETSC_SUCCESS); 234441846f8SBarry Smith PetscValidHeaderSpecific(*tao, TAO_CLASSID, 1); 2359371c9d4SSatish Balay if (--((PetscObject)*tao)->refct > 0) { 2369371c9d4SSatish Balay *tao = NULL; 2373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 238a7e14dcfSSatish Balay } 2399371c9d4SSatish Balay 240213acdd3SPierre Jolivet PetscTryTypeMethod(*tao, destroy); 2419566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&(*tao)->ksp)); 2420f0abf79SStefano Zampini PetscCall(SNESDestroy(&(*tao)->snes_ewdummy)); 2439566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch)); 244a7e14dcfSSatish Balay 245a7e14dcfSSatish Balay if ((*tao)->ops->convergencedestroy) { 2469566063dSJacob Faibussowitsch PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP)); 24748a46eb9SPierre Jolivet if ((*tao)->jacobian_state_inv) PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 248a7e14dcfSSatish Balay } 2499566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->solution)); 2509566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient)); 2519566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->ls_res)); 252a7e14dcfSSatish Balay 253a9603a14SPatrick Farrell if ((*tao)->gradient_norm) { 2549566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm)); 2559566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp)); 256a9603a14SPatrick Farrell } 257a9603a14SPatrick Farrell 2589566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XL)); 2599566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XU)); 2609566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IL)); 2619566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IU)); 2629566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DE)); 2639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DI)); 2649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints)); 2659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_equality)); 2669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_inequality)); 2679566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->stepdirection)); 2689566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian_pre)); 2699566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian)); 2709566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac)); 2719566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac_pre)); 2729566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_pre)); 2739566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian)); 2749566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_pre)); 2759566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state)); 2769566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 2779566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_design)); 2789566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality)); 2799566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre)); 2809566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality)); 2819566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre)); 2829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->state_is)); 2839566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->design_is)); 2849566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->res_weights_v)); 285b2dc45ddSPierre Jolivet PetscCall(TaoMonitorCancel(*tao)); 28648a46eb9SPierre Jolivet if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits)); 287737f463aSAlp Dener if ((*tao)->res_weights_n) { 2889566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_rows)); 2899566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_cols)); 2909566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_w)); 291125ddc8aSJason Sarich } 2929566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(tao)); 2933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 294a7e14dcfSSatish Balay } 295a7e14dcfSSatish Balay 296a7e14dcfSSatish Balay /*@ 2971d27aa22SBarry Smith TaoKSPSetUseEW - Sets `SNES` to use Eisenstat-Walker method {cite}`ew96`for computing relative tolerance for linear solvers. 2980f0abf79SStefano Zampini 299c3339decSBarry Smith Logically Collective 3000f0abf79SStefano Zampini 3010f0abf79SStefano Zampini Input Parameters: 3020f0abf79SStefano Zampini + tao - Tao context 30365ba42b6SBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE` 3040f0abf79SStefano Zampini 30567be906fSBarry Smith Level: advanced 30667be906fSBarry Smith 30747450a7bSBarry Smith Note: 30865ba42b6SBarry Smith See `SNESKSPSetUseEW()` for customization details. 3090f0abf79SStefano Zampini 3101cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `SNESKSPSetUseEW()` 3110f0abf79SStefano Zampini @*/ 312d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag) 313d71ae5a4SJacob Faibussowitsch { 3140f0abf79SStefano Zampini PetscFunctionBegin; 3150f0abf79SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3160f0abf79SStefano Zampini PetscValidLogicalCollectiveBool(tao, flag, 2); 3170f0abf79SStefano Zampini tao->ksp_ewconv = flag; 3183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3190f0abf79SStefano Zampini } 3200f0abf79SStefano Zampini 3210f0abf79SStefano Zampini /*@ 32247450a7bSBarry Smith TaoSetFromOptions - Sets various Tao parameters from the options database 323a7e14dcfSSatish Balay 324c3339decSBarry Smith Collective 325a7e14dcfSSatish Balay 32601d2d390SJose E. Roman Input Parameter: 32747450a7bSBarry Smith . tao - the `Tao` solver context 328a7e14dcfSSatish Balay 32947450a7bSBarry Smith Options Database Keys: 33065ba42b6SBarry Smith + -tao_type <type> - The algorithm that Tao uses (lmvm, nls, etc.) 331a7e14dcfSSatish Balay . -tao_gatol <gatol> - absolute error tolerance for ||gradient|| 332a7e14dcfSSatish Balay . -tao_grtol <grtol> - relative error tolerance for ||gradient|| 333a7e14dcfSSatish Balay . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient 334a7e14dcfSSatish Balay . -tao_max_it <max> - sets maximum number of iterations 335a7e14dcfSSatish Balay . -tao_max_funcs <max> - sets maximum number of function evaluations 336a7e14dcfSSatish Balay . -tao_fmin <fmin> - stop if function value reaches fmin 337a7e14dcfSSatish Balay . -tao_steptol <tol> - stop if trust region radius less than <tol> 338a7e14dcfSSatish Balay . -tao_trust0 <t> - initial trust region radius 33910978b7dSBarry Smith . -tao_view_solution - view the solution at the end of the optimization process 34047450a7bSBarry Smith . -tao_monitor - prints function value and residual norm at each iteration 34110978b7dSBarry Smith . -tao_monitor_short - same as `-tao_monitor`, but truncates very small values 34210978b7dSBarry Smith . -tao_monitor_constraint_norm - prints objective value, gradient, and constraint norm at each iteration 34310978b7dSBarry Smith . -tao_monitor_globalization - prints information about the globalization at each iteration 34410978b7dSBarry Smith . -tao_monitor_solution - prints solution vector at each iteration 34510978b7dSBarry Smith . -tao_monitor_ls_residual - prints least-squares residual vector at each iteration 34610978b7dSBarry Smith . -tao_monitor_step - prints step vector at each iteration 34710978b7dSBarry Smith . -tao_monitor_gradient - prints gradient vector at each iteration 34810978b7dSBarry Smith . -tao_monitor_solution_draw - graphically view solution vector at each iteration 34910978b7dSBarry Smith . -tao_monitor_step_draw - graphically view step vector at each iteration 35010978b7dSBarry Smith . -tao_monitor_gradient_draw - graphically view gradient at each iteration 35110978b7dSBarry Smith . -tao_monitor_cancel - cancels all monitors (except those set with command line) 352a7e14dcfSSatish Balay . -tao_fd_gradient - use gradient computed with finite differences 353f4c1ad5cSStefano Zampini . -tao_fd_hessian - use hessian computed with finite differences 35410978b7dSBarry Smith . -tao_mf_hessian - use matrix-free Hessian computed with finite differences 355441846f8SBarry Smith . -tao_view - prints information about the Tao after solving 35665ba42b6SBarry Smith - -tao_converged_reason - prints the reason Tao stopped iterating 357a7e14dcfSSatish Balay 35867be906fSBarry Smith Level: beginner 35967be906fSBarry Smith 36067be906fSBarry Smith Note: 36147450a7bSBarry Smith To see all options, run your program with the `-help` option or consult the 36265ba42b6SBarry Smith user's manual. Should be called after `TaoCreate()` but before `TaoSolve()` 363a7e14dcfSSatish Balay 3641cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 365a7e14dcfSSatish Balay @*/ 366d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions(Tao tao) 367d71ae5a4SJacob Faibussowitsch { 368b625d6c7SJed Brown TaoType default_type = TAOLMVM; 369a7e14dcfSSatish Balay char type[256], monfilename[PETSC_MAX_PATH_LEN]; 370a7e14dcfSSatish Balay PetscViewer monviewer; 371e876fe75SStephan Köhler PetscBool flg, found; 372a7e14dcfSSatish Balay MPI_Comm comm; 373a7e14dcfSSatish Balay 374a7e14dcfSSatish Balay PetscFunctionBegin; 375441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3769566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)tao, &comm)); 377125ddc8aSJason Sarich 37860b70c5cSStefano Zampini if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name; 37960b70c5cSStefano Zampini 380d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)tao); 381a7e14dcfSSatish Balay /* Check for type from options */ 3829566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg)); 383a7e14dcfSSatish Balay if (flg) { 3849566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, type)); 385a7e14dcfSSatish Balay } else if (!((PetscObject)tao)->type_name) { 3869566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, default_type)); 387a7e14dcfSSatish Balay } 388a7e14dcfSSatish Balay 38960b70c5cSStefano Zampini /* Tao solvers do not set the prefix, set it here if not yet done 39060b70c5cSStefano Zampini We do it after SetType since solver may have been changed */ 39160b70c5cSStefano Zampini if (tao->linesearch) { 39260b70c5cSStefano Zampini const char *prefix; 39360b70c5cSStefano Zampini PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix)); 394f4f49eeaSPierre Jolivet if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)tao)->prefix)); 39560b70c5cSStefano Zampini } 39660b70c5cSStefano Zampini 3979566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &tao->catol, &flg)); 3986552cf8aSJason Sarich if (flg) tao->catol_changed = PETSC_TRUE; 3996aad120cSJose E. Roman PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &tao->crtol, &flg)); 4006552cf8aSJason Sarich if (flg) tao->crtol_changed = PETSC_TRUE; 4019566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &tao->gatol, &flg)); 4026552cf8aSJason Sarich if (flg) tao->gatol_changed = PETSC_TRUE; 4039566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &tao->grtol, &flg)); 4046552cf8aSJason Sarich if (flg) tao->grtol_changed = PETSC_TRUE; 4059566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_gttol", "Stop if the norm of the gradient is less than the norm of the initial gradient times tol", "TaoSetTolerances", tao->gttol, &tao->gttol, &flg)); 4066552cf8aSJason Sarich if (flg) tao->gttol_changed = PETSC_TRUE; 4079566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg)); 4086552cf8aSJason Sarich if (flg) tao->max_it_changed = PETSC_TRUE; 4099566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg)); 4106552cf8aSJason Sarich if (flg) tao->max_funcs_changed = PETSC_TRUE; 4119566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, &flg)); 4126552cf8aSJason Sarich if (flg) tao->fmin_changed = PETSC_TRUE; 4139566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, &flg)); 4146552cf8aSJason Sarich if (flg) tao->steptol_changed = PETSC_TRUE; 4159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetTrustRegionRadius", tao->trust0, &tao->trust0, &flg)); 4166552cf8aSJason Sarich if (flg) tao->trust0_changed = PETSC_TRUE; 41710978b7dSBarry Smith 41810978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_solution_monitor", "-tao_monitor_solution", "3.21", NULL)); 41910978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_gradient_monitor", "-tao_monitor_gradient", "3.21", NULL)); 42010978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_stepdirection_monitor", "-tao_monitor_step", "3.21", NULL)); 42110978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_residual_monitor", "-tao_monitor_residual", "3.21", NULL)); 42210978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_smonitor", "-tao_monitor_short", "3.21", NULL)); 42310978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_cmonitor", "-tao_monitor_constraint_norm", "3.21", NULL)); 42410978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_gmonitor", "-tao_monitor_globalization", "3.21", NULL)); 42510978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_solution", "-tao_monitor_solution_draw", "3.21", NULL)); 42610978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_gradient", "-tao_monitor_gradient_draw", "3.21", NULL)); 42710978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_step", "-tao_monitor_step_draw", "3.21", NULL)); 42810978b7dSBarry Smith 42910978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_solution", "View solution vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 430a7e14dcfSSatish Balay if (flg) { 4319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 43210978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorSolution, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 433a7e14dcfSSatish Balay } 434a7e14dcfSSatish Balay 43565ba42b6SBarry Smith PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL)); 43610978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_gradient", "View gradient vector for each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 437a7e14dcfSSatish Balay if (flg) { 4389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 43910978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGradient, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 440a7e14dcfSSatish Balay } 441a7e14dcfSSatish Balay 44210978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_step", "View step vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 443a7e14dcfSSatish Balay if (flg) { 4449566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 44510978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorStep, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 446a7e14dcfSSatish Balay } 447a7e14dcfSSatish Balay 44810978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_residual", "View least-squares residual vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 449a7e14dcfSSatish Balay if (flg) { 4509566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 45110978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorResidual, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 452a7e14dcfSSatish Balay } 453a7e14dcfSSatish Balay 45410978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor", "Use the default convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 455a7e14dcfSSatish Balay if (flg) { 4569566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 45710978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorDefault, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 458a7e14dcfSSatish Balay } 459a7e14dcfSSatish Balay 46010978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_globalization", "Use the convergence monitor with extra globalization info", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 4618d5ead36SAlp Dener if (flg) { 4629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 46310978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGlobalization, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 4648d5ead36SAlp Dener } 4658d5ead36SAlp Dener 46610978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_short", "Use the short convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 467a7e14dcfSSatish Balay if (flg) { 4689566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 46910978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorDefaultShort, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 470a7e14dcfSSatish Balay } 471a7e14dcfSSatish Balay 47210978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_constraint_norm", "Use the default convergence monitor with constraint norm", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 473a7e14dcfSSatish Balay if (flg) { 4749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 47510978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorConstraintNorm, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 476a7e14dcfSSatish Balay } 477a7e14dcfSSatish Balay 4788afaa268SBarry Smith flg = PETSC_FALSE; 479b2dc45ddSPierre Jolivet PetscCall(PetscOptionsDeprecated("-tao_cancelmonitors", "-tao_monitor_cancel", "3.21", NULL)); 480b2dc45ddSPierre Jolivet PetscCall(PetscOptionsBool("-tao_monitor_cancel", "cancel all monitors and call any registered destroy routines", "TaoMonitorCancel", flg, &flg, NULL)); 481b2dc45ddSPierre Jolivet if (flg) PetscCall(TaoMonitorCancel(tao)); 482a7e14dcfSSatish Balay 4838afaa268SBarry Smith flg = PETSC_FALSE; 48410978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_solution_draw", "Plot solution vector at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 485a7e14dcfSSatish Balay if (flg) { 486e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 487e882e171SHong Zhang PetscInt howoften = 1; 4889566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 48910978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorSolutionDraw, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy)); 490a7e14dcfSSatish Balay } 491a7e14dcfSSatish Balay 4928afaa268SBarry Smith flg = PETSC_FALSE; 49310978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_step_draw", "Plots step at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 49410978b7dSBarry Smith if (flg) PetscCall(TaoMonitorSet(tao, TaoMonitorStepDraw, NULL, NULL)); 495a7e14dcfSSatish Balay 4968afaa268SBarry Smith flg = PETSC_FALSE; 49710978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_gradient_draw", "plots gradient at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 498a7e14dcfSSatish Balay if (flg) { 499e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 500e882e171SHong Zhang PetscInt howoften = 1; 5019566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 50210978b7dSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGradientDraw, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy)); 503a7e14dcfSSatish Balay } 5048afaa268SBarry Smith flg = PETSC_FALSE; 5059566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL)); 5061baa6e33SBarry Smith if (flg) PetscCall(TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, NULL)); 507f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 50810978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute Hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL)); 509f4c1ad5cSStefano Zampini if (flg) { 510f4c1ad5cSStefano Zampini Mat H; 511f4c1ad5cSStefano Zampini 5129566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 5139566063dSJacob Faibussowitsch PetscCall(MatSetType(H, MATAIJ)); 5149566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL)); 5159566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 516f4c1ad5cSStefano Zampini } 517f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 51810978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free Hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL)); 519f4c1ad5cSStefano Zampini if (flg) { 520f4c1ad5cSStefano Zampini Mat H; 521f4c1ad5cSStefano Zampini 5229566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 5239566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL)); 5249566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 525f4c1ad5cSStefano Zampini } 526e876fe75SStephan Köhler PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, &found)); 527e876fe75SStephan Köhler if (found) PetscCall(TaoSetRecycleHistory(tao, flg)); 5289566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubSetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL)); 529a7e14dcfSSatish Balay 5300f0abf79SStefano Zampini if (tao->ksp) { 5310f0abf79SStefano Zampini PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL)); 5320f0abf79SStefano Zampini PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv)); 5330f0abf79SStefano Zampini } 5340f0abf79SStefano Zampini 535dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject); 53660b70c5cSStefano Zampini 53760b70c5cSStefano Zampini /* process any options handlers added with PetscObjectAddOptionsHandler() */ 53860b70c5cSStefano Zampini PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject)); 539d0609cedSBarry Smith PetscOptionsEnd(); 54060b70c5cSStefano Zampini 54160b70c5cSStefano Zampini if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch)); 5423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 543a7e14dcfSSatish Balay } 544a7e14dcfSSatish Balay 545*ffeef943SBarry Smith /*@ 54647450a7bSBarry Smith TaoViewFromOptions - View a `Tao` object based on values in the options database 547fe2efc57SMark 548c3339decSBarry Smith Collective 549fe2efc57SMark 550fe2efc57SMark Input Parameters: 55147450a7bSBarry Smith + A - the `Tao` context 55247450a7bSBarry Smith . obj - Optional object that provides the prefix for the options database 553736c3998SJose E. Roman - name - command line option 554fe2efc57SMark 555fe2efc57SMark Level: intermediate 55667be906fSBarry Smith 5571cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()` 558fe2efc57SMark @*/ 559d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[]) 560d71ae5a4SJacob Faibussowitsch { 561fe2efc57SMark PetscFunctionBegin; 562fe2efc57SMark PetscValidHeaderSpecific(A, TAO_CLASSID, 1); 5639566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name)); 5643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 565fe2efc57SMark } 566fe2efc57SMark 567*ffeef943SBarry Smith /*@ 56847450a7bSBarry Smith TaoView - Prints information about the `Tao` object 569a7e14dcfSSatish Balay 570c3339decSBarry Smith Collective 571a7e14dcfSSatish Balay 572a7e14dcfSSatish Balay Input Parameters: 57347450a7bSBarry Smith + tao - the `Tao` context 574a7e14dcfSSatish Balay - viewer - visualization context 575a7e14dcfSSatish Balay 576a7e14dcfSSatish Balay Options Database Key: 57765ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()` 578a7e14dcfSSatish Balay 57967be906fSBarry Smith Level: beginner 58067be906fSBarry Smith 581a7e14dcfSSatish Balay Notes: 582a7e14dcfSSatish Balay The available visualization contexts include 58365ba42b6SBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 58465ba42b6SBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 585a7e14dcfSSatish Balay output where only the first processor opens 586a7e14dcfSSatish Balay the file. All other processors send their 587a7e14dcfSSatish Balay data to the first processor to print. 588a7e14dcfSSatish Balay 5891cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `PetscViewerASCIIOpen()` 590a7e14dcfSSatish Balay @*/ 591d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView(Tao tao, PetscViewer viewer) 592d71ae5a4SJacob Faibussowitsch { 593a7e14dcfSSatish Balay PetscBool isascii, isstring; 594b625d6c7SJed Brown TaoType type; 59547a47007SBarry Smith 596a7e14dcfSSatish Balay PetscFunctionBegin; 597441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 59848a46eb9SPierre Jolivet if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer)); 599a7e14dcfSSatish Balay PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 600a7e14dcfSSatish Balay PetscCheckSameComm(tao, 1, viewer, 2); 601a7e14dcfSSatish Balay 6029566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 6039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 604a7e14dcfSSatish Balay if (isascii) { 6059566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer)); 606a7e14dcfSSatish Balay 607a7e14dcfSSatish Balay if (tao->ops->view) { 6089566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 609dbbe0bcdSBarry Smith PetscUseTypeMethod(tao, view, viewer); 6109566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 611a7e14dcfSSatish Balay } 6128d3f3ddaSAlp Dener if (tao->linesearch) { 6139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 6149566063dSJacob Faibussowitsch PetscCall(TaoLineSearchView(tao->linesearch, viewer)); 6159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 6168d3f3ddaSAlp Dener } 617a7e14dcfSSatish Balay if (tao->ksp) { 6189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 6199566063dSJacob Faibussowitsch PetscCall(KSPView(tao->ksp, viewer)); 62063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its)); 6219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 622a7e14dcfSSatish Balay } 62319b5c101SAlp Dener 6249566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 62519b5c101SAlp Dener 62648a46eb9SPierre Jolivet if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubSetTypes[tao->subset_type])); 627a7e14dcfSSatish Balay 6289566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol)); 6299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol)); 6309566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol)); 6319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual)); 632a7e14dcfSSatish Balay 6336246e8cdSAlp Dener if (tao->constrained) { 6349566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:")); 6359566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol)); 6369566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol)); 6379566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm)); 638a7e14dcfSSatish Balay } 639a7e14dcfSSatish Balay 640a7e14dcfSSatish Balay if (tao->trust < tao->steptol) { 6419566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol)); 6429566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust)); 643a7e14dcfSSatish Balay } 644a7e14dcfSSatish Balay 64548a46eb9SPierre Jolivet if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin)); 6469566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Objective value=%g\n", (double)tao->fc)); 647a7e14dcfSSatish Balay 64863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of iterations=%" PetscInt_FMT ", ", tao->niter)); 64963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_it)); 650a7e14dcfSSatish Balay 651a7e14dcfSSatish Balay if (tao->nfuncs > 0) { 65263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function evaluations=%" PetscInt_FMT ",", tao->nfuncs)); 65363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " max: %" PetscInt_FMT "\n", tao->max_funcs)); 654a7e14dcfSSatish Balay } 655a7e14dcfSSatish Balay if (tao->ngrads > 0) { 65663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of gradient evaluations=%" PetscInt_FMT ",", tao->ngrads)); 65763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " max: %" PetscInt_FMT "\n", tao->max_funcs)); 658a7e14dcfSSatish Balay } 659a7e14dcfSSatish Balay if (tao->nfuncgrads > 0) { 66063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function/gradient evaluations=%" PetscInt_FMT ",", tao->nfuncgrads)); 66163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs)); 662a7e14dcfSSatish Balay } 66348a46eb9SPierre Jolivet if (tao->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->nhess)); 66448a46eb9SPierre Jolivet if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints)); 66548a46eb9SPierre Jolivet if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac)); 666a7e14dcfSSatish Balay 667a7e14dcfSSatish Balay if (tao->reason > 0) { 6689566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: ")); 669a7e14dcfSSatish Balay switch (tao->reason) { 670d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GATOL: 671d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n")); 672d71ae5a4SJacob Faibussowitsch break; 673d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GRTOL: 674d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n")); 675d71ae5a4SJacob Faibussowitsch break; 676d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GTTOL: 677d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n")); 678d71ae5a4SJacob Faibussowitsch break; 679d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_STEPTOL: 680d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n")); 681d71ae5a4SJacob Faibussowitsch break; 682d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_MINF: 683d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Minf -- f < fmin\n")); 684d71ae5a4SJacob Faibussowitsch break; 685d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_USER: 686d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 687d71ae5a4SJacob Faibussowitsch break; 688d71ae5a4SJacob Faibussowitsch default: 689d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "\n")); 690d71ae5a4SJacob Faibussowitsch break; 691a7e14dcfSSatish Balay } 692a7e14dcfSSatish Balay } else { 6939566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solver terminated: %d", tao->reason)); 694a7e14dcfSSatish Balay switch (tao->reason) { 695d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXITS: 696d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n")); 697d71ae5a4SJacob Faibussowitsch break; 698d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_NAN: 699d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " NAN or Inf encountered\n")); 700d71ae5a4SJacob Faibussowitsch break; 701d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXFCN: 702d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n")); 703d71ae5a4SJacob Faibussowitsch break; 704d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_LS_FAILURE: 705d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n")); 706d71ae5a4SJacob Faibussowitsch break; 707d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_TR_REDUCTION: 708d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n")); 709d71ae5a4SJacob Faibussowitsch break; 710d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_USER: 711d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 712d71ae5a4SJacob Faibussowitsch break; 713d71ae5a4SJacob Faibussowitsch default: 714d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "\n")); 715d71ae5a4SJacob Faibussowitsch break; 716a7e14dcfSSatish Balay } 717a7e14dcfSSatish Balay } 7189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 719a7e14dcfSSatish Balay } else if (isstring) { 7209566063dSJacob Faibussowitsch PetscCall(TaoGetType(tao, &type)); 7219566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type)); 722a7e14dcfSSatish Balay } 7233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 724a7e14dcfSSatish Balay } 725a7e14dcfSSatish Balay 726a7e14dcfSSatish Balay /*@ 727414d97d3SAlp Dener TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using 72865ba42b6SBarry Smith iterate information from the previous `TaoSolve()`. This feature is disabled by 729414d97d3SAlp Dener default. 730414d97d3SAlp Dener 73167be906fSBarry Smith Logically Collective 73267be906fSBarry Smith 73367be906fSBarry Smith Input Parameters: 73447450a7bSBarry Smith + tao - the `Tao` context 73567be906fSBarry Smith - recycle - boolean flag 73667be906fSBarry Smith 73747450a7bSBarry Smith Options Database Key: 73867be906fSBarry Smith . -tao_recycle_history <true,false> - reuse the history 73967be906fSBarry Smith 74067be906fSBarry Smith Level: intermediate 74167be906fSBarry Smith 74267be906fSBarry Smith Notes: 74365ba42b6SBarry Smith For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction 74465ba42b6SBarry Smith from the previous `TaoSolve()` call when computing the first search direction in a 745414d97d3SAlp Dener new solution. By default, CG methods set the first search direction to the 746414d97d3SAlp Dener negative gradient. 747414d97d3SAlp Dener 74865ba42b6SBarry Smith For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses 74965ba42b6SBarry Smith the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()` 750414d97d3SAlp Dener call. By default, QN family of methods reset the initial Hessian approximation to 751414d97d3SAlp Dener the identity matrix. 752414d97d3SAlp Dener 753414d97d3SAlp Dener For any other algorithm, this setting has no effect. 754414d97d3SAlp Dener 7551cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 756414d97d3SAlp Dener @*/ 757d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle) 758d71ae5a4SJacob Faibussowitsch { 759414d97d3SAlp Dener PetscFunctionBegin; 760414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 76194511df7SStefano Zampini PetscValidLogicalCollectiveBool(tao, recycle, 2); 762414d97d3SAlp Dener tao->recycle = recycle; 7633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 764414d97d3SAlp Dener } 765414d97d3SAlp Dener 766414d97d3SAlp Dener /*@ 767414d97d3SAlp Dener TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information 76865ba42b6SBarry Smith from the previous `TaoSolve()`. This feature is disabled by default. 769414d97d3SAlp Dener 77067be906fSBarry Smith Logically Collective 771414d97d3SAlp Dener 77247450a7bSBarry Smith Input Parameter: 77347450a7bSBarry Smith . tao - the `Tao` context 774414d97d3SAlp Dener 77547450a7bSBarry Smith Output Parameter: 776147403d9SBarry Smith . recycle - boolean flag 777414d97d3SAlp Dener 778414d97d3SAlp Dener Level: intermediate 779414d97d3SAlp Dener 7801cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 781414d97d3SAlp Dener @*/ 782d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle) 783d71ae5a4SJacob Faibussowitsch { 784414d97d3SAlp Dener PetscFunctionBegin; 785414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 7864f572ea9SToby Isaac PetscAssertPointer(recycle, 2); 787414d97d3SAlp Dener *recycle = tao->recycle; 7883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 789414d97d3SAlp Dener } 790414d97d3SAlp Dener 791414d97d3SAlp Dener /*@ 79247450a7bSBarry Smith TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests 793a7e14dcfSSatish Balay 79467be906fSBarry Smith Logically Collective 795a7e14dcfSSatish Balay 796a7e14dcfSSatish Balay Input Parameters: 79747450a7bSBarry Smith + tao - the `Tao` context 798a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this 799a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 800a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor 801a7e14dcfSSatish Balay 802a7e14dcfSSatish Balay Options Database Keys: 803e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol 804a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol 805a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol 806a7e14dcfSSatish Balay 8073b242c63SJacob Faibussowitsch Stopping Criteria\: 80867be906fSBarry Smith .vb 80967be906fSBarry Smith ||g(X)|| <= gatol 81067be906fSBarry Smith ||g(X)|| / |f(X)| <= grtol 81167be906fSBarry Smith ||g(X)|| / ||g(X0)|| <= gttol 81267be906fSBarry Smith .ve 813a7e14dcfSSatish Balay 814a7e14dcfSSatish Balay Level: beginner 815a7e14dcfSSatish Balay 81667be906fSBarry Smith Note: 81767be906fSBarry Smith Use `PETSC_DEFAULT` to leave one or more tolerances unchanged. 818a7e14dcfSSatish Balay 8191cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()` 820a7e14dcfSSatish Balay @*/ 821d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol) 822d71ae5a4SJacob Faibussowitsch { 823a7e14dcfSSatish Balay PetscFunctionBegin; 824441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 82594511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gatol, 2); 82694511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, grtol, 3); 82794511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gttol, 4); 828a7e14dcfSSatish Balay 82913bcc0bdSJacob Faibussowitsch if (gatol != (PetscReal)PETSC_DEFAULT) { 830a7e14dcfSSatish Balay if (gatol < 0) { 8319566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative gatol -- ignored.\n")); 832a7e14dcfSSatish Balay } else { 833a7e14dcfSSatish Balay tao->gatol = PetscMax(0, gatol); 8346552cf8aSJason Sarich tao->gatol_changed = PETSC_TRUE; 835a7e14dcfSSatish Balay } 836a7e14dcfSSatish Balay } 837a7e14dcfSSatish Balay 83813bcc0bdSJacob Faibussowitsch if (grtol != (PetscReal)PETSC_DEFAULT) { 839a7e14dcfSSatish Balay if (grtol < 0) { 8409566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative grtol -- ignored.\n")); 841a7e14dcfSSatish Balay } else { 842a7e14dcfSSatish Balay tao->grtol = PetscMax(0, grtol); 8436552cf8aSJason Sarich tao->grtol_changed = PETSC_TRUE; 844a7e14dcfSSatish Balay } 845a7e14dcfSSatish Balay } 846a7e14dcfSSatish Balay 84713bcc0bdSJacob Faibussowitsch if (gttol != (PetscReal)PETSC_DEFAULT) { 848a7e14dcfSSatish Balay if (gttol < 0) { 8499566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative gttol -- ignored.\n")); 850a7e14dcfSSatish Balay } else { 851a7e14dcfSSatish Balay tao->gttol = PetscMax(0, gttol); 8526552cf8aSJason Sarich tao->gttol_changed = PETSC_TRUE; 853a7e14dcfSSatish Balay } 854a7e14dcfSSatish Balay } 8553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 856a7e14dcfSSatish Balay } 857a7e14dcfSSatish Balay 858a7e14dcfSSatish Balay /*@ 85947450a7bSBarry Smith TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests 860a7e14dcfSSatish Balay 86167be906fSBarry Smith Logically Collective 862a7e14dcfSSatish Balay 863a7e14dcfSSatish Balay Input Parameters: 86447450a7bSBarry Smith + tao - the `Tao` context 86567be906fSBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria 86667be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria 867a7e14dcfSSatish Balay 868a7e14dcfSSatish Balay Options Database Keys: 869a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol 870a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol 871a7e14dcfSSatish Balay 872a7e14dcfSSatish Balay Level: intermediate 873a7e14dcfSSatish Balay 87467be906fSBarry Smith Notes: 87567be906fSBarry Smith Use `PETSC_DEFAULT` to leave any tolerance unchanged. 876a7e14dcfSSatish Balay 8771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()` 878a7e14dcfSSatish Balay @*/ 879d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol) 880d71ae5a4SJacob Faibussowitsch { 881a7e14dcfSSatish Balay PetscFunctionBegin; 882441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 88394511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, catol, 2); 88494511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, crtol, 3); 885a7e14dcfSSatish Balay 88613bcc0bdSJacob Faibussowitsch if (catol != (PetscReal)PETSC_DEFAULT) { 887a7e14dcfSSatish Balay if (catol < 0) { 8889566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative catol -- ignored.\n")); 889a7e14dcfSSatish Balay } else { 890a7e14dcfSSatish Balay tao->catol = PetscMax(0, catol); 8916552cf8aSJason Sarich tao->catol_changed = PETSC_TRUE; 892a7e14dcfSSatish Balay } 893a7e14dcfSSatish Balay } 894a7e14dcfSSatish Balay 89513bcc0bdSJacob Faibussowitsch if (crtol != (PetscReal)PETSC_DEFAULT) { 896a7e14dcfSSatish Balay if (crtol < 0) { 8979566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative crtol -- ignored.\n")); 898a7e14dcfSSatish Balay } else { 899a7e14dcfSSatish Balay tao->crtol = PetscMax(0, crtol); 9006552cf8aSJason Sarich tao->crtol_changed = PETSC_TRUE; 901a7e14dcfSSatish Balay } 902a7e14dcfSSatish Balay } 9033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 904a7e14dcfSSatish Balay } 905a7e14dcfSSatish Balay 90658417fe7SBarry Smith /*@ 90747450a7bSBarry Smith TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests 90858417fe7SBarry Smith 90967be906fSBarry Smith Not Collective 91058417fe7SBarry Smith 91158417fe7SBarry Smith Input Parameter: 91247450a7bSBarry Smith . tao - the `Tao` context 91358417fe7SBarry Smith 914d8d19677SJose E. Roman Output Parameters: 91567be906fSBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria 91667be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria 91758417fe7SBarry Smith 91858417fe7SBarry Smith Level: intermediate 91958417fe7SBarry Smith 9201cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReasons`,`TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()` 92158417fe7SBarry Smith @*/ 922d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol) 923d71ae5a4SJacob Faibussowitsch { 92458417fe7SBarry Smith PetscFunctionBegin; 92558417fe7SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 92658417fe7SBarry Smith if (catol) *catol = tao->catol; 92758417fe7SBarry Smith if (crtol) *crtol = tao->crtol; 9283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 92958417fe7SBarry Smith } 93058417fe7SBarry Smith 931a7e14dcfSSatish Balay /*@ 932a7e14dcfSSatish Balay TaoSetFunctionLowerBound - Sets a bound on the solution objective value. 933a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 934a7e14dcfSSatish Balay has been found, the solver will terminate. 935a7e14dcfSSatish Balay 936c3339decSBarry Smith Logically Collective 937a7e14dcfSSatish Balay 938a7e14dcfSSatish Balay Input Parameters: 939441846f8SBarry Smith + tao - the Tao solver context 940a7e14dcfSSatish Balay - fmin - the tolerance 941a7e14dcfSSatish Balay 94247450a7bSBarry Smith Options Database Key: 943a7e14dcfSSatish Balay . -tao_fmin <fmin> - sets the minimum function value 944a7e14dcfSSatish Balay 945a7e14dcfSSatish Balay Level: intermediate 946a7e14dcfSSatish Balay 9471cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()` 948a7e14dcfSSatish Balay @*/ 949d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin) 950d71ae5a4SJacob Faibussowitsch { 951a7e14dcfSSatish Balay PetscFunctionBegin; 952441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 95394511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, fmin, 2); 954a7e14dcfSSatish Balay tao->fmin = fmin; 9556552cf8aSJason Sarich tao->fmin_changed = PETSC_TRUE; 9563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 957a7e14dcfSSatish Balay } 958a7e14dcfSSatish Balay 959a7e14dcfSSatish Balay /*@ 9608e96d397SJason Sarich TaoGetFunctionLowerBound - Gets the bound on the solution objective value. 961a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 962a7e14dcfSSatish Balay has been found, the solver will terminate. 963a7e14dcfSSatish Balay 96467be906fSBarry Smith Not Collective 965a7e14dcfSSatish Balay 96647450a7bSBarry Smith Input Parameter: 96747450a7bSBarry Smith . tao - the `Tao` solver context 968a7e14dcfSSatish Balay 96947450a7bSBarry Smith Output Parameter: 970a7e14dcfSSatish Balay . fmin - the minimum function value 971a7e14dcfSSatish Balay 972a7e14dcfSSatish Balay Level: intermediate 973a7e14dcfSSatish Balay 9741cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()` 975a7e14dcfSSatish Balay @*/ 976d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin) 977d71ae5a4SJacob Faibussowitsch { 978a7e14dcfSSatish Balay PetscFunctionBegin; 979441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 9804f572ea9SToby Isaac PetscAssertPointer(fmin, 2); 981a7e14dcfSSatish Balay *fmin = tao->fmin; 9823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 983a7e14dcfSSatish Balay } 984a7e14dcfSSatish Balay 985a7e14dcfSSatish Balay /*@ 98647450a7bSBarry Smith TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`. 987a7e14dcfSSatish Balay 988c3339decSBarry Smith Logically Collective 989a7e14dcfSSatish Balay 990a7e14dcfSSatish Balay Input Parameters: 99147450a7bSBarry Smith + tao - the `Tao` solver context 992a7e14dcfSSatish Balay - nfcn - the maximum number of function evaluations (>=0) 993a7e14dcfSSatish Balay 99447450a7bSBarry Smith Options Database Key: 995a7e14dcfSSatish Balay . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations 996a7e14dcfSSatish Balay 997a7e14dcfSSatish Balay Level: intermediate 998a7e14dcfSSatish Balay 9991cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()` 1000a7e14dcfSSatish Balay @*/ 1001d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn) 1002d71ae5a4SJacob Faibussowitsch { 1003a7e14dcfSSatish Balay PetscFunctionBegin; 1004441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 100594511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, nfcn, 2); 10069371c9d4SSatish Balay if (nfcn >= 0) { 10079371c9d4SSatish Balay tao->max_funcs = PetscMax(0, nfcn); 10089371c9d4SSatish Balay } else { 10099371c9d4SSatish Balay tao->max_funcs = -1; 10109371c9d4SSatish Balay } 10116552cf8aSJason Sarich tao->max_funcs_changed = PETSC_TRUE; 10123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1013a7e14dcfSSatish Balay } 1014a7e14dcfSSatish Balay 1015a7e14dcfSSatish Balay /*@ 101647450a7bSBarry Smith TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()` 1017a7e14dcfSSatish Balay 1018c3339decSBarry Smith Logically Collective 1019a7e14dcfSSatish Balay 102047450a7bSBarry Smith Input Parameter: 102147450a7bSBarry Smith . tao - the `Tao` solver context 1022a7e14dcfSSatish Balay 102347450a7bSBarry Smith Output Parameter: 1024a7e14dcfSSatish Balay . nfcn - the maximum number of function evaluations 1025a7e14dcfSSatish Balay 1026a7e14dcfSSatish Balay Level: intermediate 1027a7e14dcfSSatish Balay 10281cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1029a7e14dcfSSatish Balay @*/ 1030d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn) 1031d71ae5a4SJacob Faibussowitsch { 1032a7e14dcfSSatish Balay PetscFunctionBegin; 1033441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 10344f572ea9SToby Isaac PetscAssertPointer(nfcn, 2); 1035a7e14dcfSSatish Balay *nfcn = tao->max_funcs; 10363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1037a7e14dcfSSatish Balay } 1038a7e14dcfSSatish Balay 1039770232b9SCe Qin /*@ 104047450a7bSBarry Smith TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object 1041770232b9SCe Qin 1042770232b9SCe Qin Not Collective 1043770232b9SCe Qin 104447450a7bSBarry Smith Input Parameter: 104547450a7bSBarry Smith . tao - the `Tao` solver context 1046770232b9SCe Qin 104747450a7bSBarry Smith Output Parameter: 104894511df7SStefano Zampini . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations) 1049770232b9SCe Qin 1050770232b9SCe Qin Level: intermediate 1051770232b9SCe Qin 10521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1053770232b9SCe Qin @*/ 1054d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs) 1055d71ae5a4SJacob Faibussowitsch { 1056770232b9SCe Qin PetscFunctionBegin; 1057770232b9SCe Qin PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 10584f572ea9SToby Isaac PetscAssertPointer(nfuncs, 2); 1059770232b9SCe Qin *nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 10603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1061770232b9SCe Qin } 1062770232b9SCe Qin 1063a7e14dcfSSatish Balay /*@ 106447450a7bSBarry Smith TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()` 1065a7e14dcfSSatish Balay 1066c3339decSBarry Smith Logically Collective 1067a7e14dcfSSatish Balay 1068a7e14dcfSSatish Balay Input Parameters: 106947450a7bSBarry Smith + tao - the `Tao` solver context 1070a7e14dcfSSatish Balay - maxits - the maximum number of iterates (>=0) 1071a7e14dcfSSatish Balay 107247450a7bSBarry Smith Options Database Key: 1073a7e14dcfSSatish Balay . -tao_max_it <its> - sets the maximum number of iterations 1074a7e14dcfSSatish Balay 1075a7e14dcfSSatish Balay Level: intermediate 1076a7e14dcfSSatish Balay 10771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()` 1078a7e14dcfSSatish Balay @*/ 1079d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits) 1080d71ae5a4SJacob Faibussowitsch { 1081a7e14dcfSSatish Balay PetscFunctionBegin; 1082441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 108394511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, maxits, 2); 108447a47007SBarry Smith tao->max_it = PetscMax(0, maxits); 10856552cf8aSJason Sarich tao->max_it_changed = PETSC_TRUE; 10863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1087a7e14dcfSSatish Balay } 1088a7e14dcfSSatish Balay 1089a7e14dcfSSatish Balay /*@ 109065ba42b6SBarry Smith TaoGetMaximumIterations - Gets a maximum number of iterates that will be used 1091a7e14dcfSSatish Balay 1092a7e14dcfSSatish Balay Not Collective 1093a7e14dcfSSatish Balay 109447450a7bSBarry Smith Input Parameter: 109547450a7bSBarry Smith . tao - the `Tao` solver context 1096a7e14dcfSSatish Balay 109747450a7bSBarry Smith Output Parameter: 1098a7e14dcfSSatish Balay . maxits - the maximum number of iterates 1099a7e14dcfSSatish Balay 1100a7e14dcfSSatish Balay Level: intermediate 1101a7e14dcfSSatish Balay 11021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()` 1103a7e14dcfSSatish Balay @*/ 1104d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits) 1105d71ae5a4SJacob Faibussowitsch { 1106a7e14dcfSSatish Balay PetscFunctionBegin; 1107441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 11084f572ea9SToby Isaac PetscAssertPointer(maxits, 2); 1109a7e14dcfSSatish Balay *maxits = tao->max_it; 11103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1111a7e14dcfSSatish Balay } 1112a7e14dcfSSatish Balay 1113a7e14dcfSSatish Balay /*@ 1114a7e14dcfSSatish Balay TaoSetInitialTrustRegionRadius - Sets the initial trust region radius. 1115a7e14dcfSSatish Balay 111667be906fSBarry Smith Logically Collective 1117a7e14dcfSSatish Balay 1118d8d19677SJose E. Roman Input Parameters: 111947450a7bSBarry Smith + tao - a `Tao` optimization solver 1120a7e14dcfSSatish Balay - radius - the trust region radius 1121a7e14dcfSSatish Balay 1122a7e14dcfSSatish Balay Options Database Key: 1123a7e14dcfSSatish Balay . -tao_trust0 <t0> - sets initial trust region radius 1124a7e14dcfSSatish Balay 112547450a7bSBarry Smith Level: intermediate 112647450a7bSBarry Smith 11271cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR` 1128a7e14dcfSSatish Balay @*/ 1129d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius) 1130d71ae5a4SJacob Faibussowitsch { 1131a7e14dcfSSatish Balay PetscFunctionBegin; 1132441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 113394511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, radius, 2); 1134a7e14dcfSSatish Balay tao->trust0 = PetscMax(0.0, radius); 11356552cf8aSJason Sarich tao->trust0_changed = PETSC_TRUE; 11363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1137a7e14dcfSSatish Balay } 1138a7e14dcfSSatish Balay 1139a7e14dcfSSatish Balay /*@ 114065ba42b6SBarry Smith TaoGetInitialTrustRegionRadius - Gets the initial trust region radius. 1141a7e14dcfSSatish Balay 1142a7e14dcfSSatish Balay Not Collective 1143a7e14dcfSSatish Balay 1144a7e14dcfSSatish Balay Input Parameter: 114547450a7bSBarry Smith . tao - a `Tao` optimization solver 1146a7e14dcfSSatish Balay 1147a7e14dcfSSatish Balay Output Parameter: 1148a7e14dcfSSatish Balay . radius - the trust region radius 1149a7e14dcfSSatish Balay 1150a7e14dcfSSatish Balay Level: intermediate 1151a7e14dcfSSatish Balay 11521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR` 1153a7e14dcfSSatish Balay @*/ 1154d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius) 1155d71ae5a4SJacob Faibussowitsch { 1156a7e14dcfSSatish Balay PetscFunctionBegin; 1157441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 11584f572ea9SToby Isaac PetscAssertPointer(radius, 2); 1159a7e14dcfSSatish Balay *radius = tao->trust0; 11603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1161a7e14dcfSSatish Balay } 1162a7e14dcfSSatish Balay 1163a7e14dcfSSatish Balay /*@ 1164a7e14dcfSSatish Balay TaoGetCurrentTrustRegionRadius - Gets the current trust region radius. 1165a7e14dcfSSatish Balay 1166a7e14dcfSSatish Balay Not Collective 1167a7e14dcfSSatish Balay 1168a7e14dcfSSatish Balay Input Parameter: 116947450a7bSBarry Smith . tao - a `Tao` optimization solver 1170a7e14dcfSSatish Balay 1171a7e14dcfSSatish Balay Output Parameter: 1172a7e14dcfSSatish Balay . radius - the trust region radius 1173a7e14dcfSSatish Balay 1174a7e14dcfSSatish Balay Level: intermediate 1175a7e14dcfSSatish Balay 11761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR` 1177a7e14dcfSSatish Balay @*/ 1178d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius) 1179d71ae5a4SJacob Faibussowitsch { 1180a7e14dcfSSatish Balay PetscFunctionBegin; 1181441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 11824f572ea9SToby Isaac PetscAssertPointer(radius, 2); 1183a7e14dcfSSatish Balay *radius = tao->trust; 11843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1185a7e14dcfSSatish Balay } 1186a7e14dcfSSatish Balay 1187a7e14dcfSSatish Balay /*@ 118847450a7bSBarry Smith TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()` 1189a7e14dcfSSatish Balay 1190a7e14dcfSSatish Balay Not Collective 1191a7e14dcfSSatish Balay 1192f899ff85SJose E. Roman Input Parameter: 119347450a7bSBarry Smith . tao - the `Tao` context 1194a7e14dcfSSatish Balay 1195a7e14dcfSSatish Balay Output Parameters: 1196e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this 1197a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 1198a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor 1199a7e14dcfSSatish Balay 1200a7e14dcfSSatish Balay Level: intermediate 1201a1cb98faSBarry Smith 1202a1cb98faSBarry Smith Note: 120347450a7bSBarry Smith `NULL` can be used as an argument if not all tolerances values are needed 1204a1cb98faSBarry Smith 12051cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()` 1206a7e14dcfSSatish Balay @*/ 1207d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol) 1208d71ae5a4SJacob Faibussowitsch { 1209a7e14dcfSSatish Balay PetscFunctionBegin; 1210441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1211a7e14dcfSSatish Balay if (gatol) *gatol = tao->gatol; 1212a7e14dcfSSatish Balay if (grtol) *grtol = tao->grtol; 1213a7e14dcfSSatish Balay if (gttol) *gttol = tao->gttol; 12143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1215a7e14dcfSSatish Balay } 1216a7e14dcfSSatish Balay 1217a7e14dcfSSatish Balay /*@ 1218a7e14dcfSSatish Balay TaoGetKSP - Gets the linear solver used by the optimization solver. 1219a7e14dcfSSatish Balay 1220a7e14dcfSSatish Balay Not Collective 1221a7e14dcfSSatish Balay 122247450a7bSBarry Smith Input Parameter: 122347450a7bSBarry Smith . tao - the `Tao` solver 1224a7e14dcfSSatish Balay 122547450a7bSBarry Smith Output Parameter: 122647450a7bSBarry Smith . ksp - the `KSP` linear solver used in the optimization solver 1227a7e14dcfSSatish Balay 1228a7e14dcfSSatish Balay Level: intermediate 1229a7e14dcfSSatish Balay 12301cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `KSP` 1231a7e14dcfSSatish Balay @*/ 1232d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp) 1233d71ae5a4SJacob Faibussowitsch { 1234a7e14dcfSSatish Balay PetscFunctionBegin; 123594511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12364f572ea9SToby Isaac PetscAssertPointer(ksp, 2); 1237a7e14dcfSSatish Balay *ksp = tao->ksp; 12383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1239a7e14dcfSSatish Balay } 1240a7e14dcfSSatish Balay 1241025e9500SJason Sarich /*@ 1242025e9500SJason Sarich TaoGetLinearSolveIterations - Gets the total number of linear iterations 124347450a7bSBarry Smith used by the `Tao` solver 1244025e9500SJason Sarich 1245025e9500SJason Sarich Not Collective 1246025e9500SJason Sarich 1247025e9500SJason Sarich Input Parameter: 124847450a7bSBarry Smith . tao - the `Tao` context 1249025e9500SJason Sarich 1250025e9500SJason Sarich Output Parameter: 1251025e9500SJason Sarich . lits - number of linear iterations 1252025e9500SJason Sarich 1253025e9500SJason Sarich Level: intermediate 1254025e9500SJason Sarich 125547450a7bSBarry Smith Note: 125647450a7bSBarry Smith This counter is reset to zero for each successive call to `TaoSolve()` 125747450a7bSBarry Smith 12581cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetKSP()` 1259025e9500SJason Sarich @*/ 1260d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits) 1261d71ae5a4SJacob Faibussowitsch { 1262025e9500SJason Sarich PetscFunctionBegin; 1263025e9500SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12644f572ea9SToby Isaac PetscAssertPointer(lits, 2); 12652d9aa51bSJason Sarich *lits = tao->ksp_tot_its; 12663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1267025e9500SJason Sarich } 1268025e9500SJason Sarich 1269a7e14dcfSSatish Balay /*@ 1270a7e14dcfSSatish Balay TaoGetLineSearch - Gets the line search used by the optimization solver. 1271a7e14dcfSSatish Balay 1272a7e14dcfSSatish Balay Not Collective 1273a7e14dcfSSatish Balay 127447450a7bSBarry Smith Input Parameter: 127547450a7bSBarry Smith . tao - the `Tao` solver 1276a7e14dcfSSatish Balay 127747450a7bSBarry Smith Output Parameter: 1278a7e14dcfSSatish Balay . ls - the line search used in the optimization solver 1279a7e14dcfSSatish Balay 1280a7e14dcfSSatish Balay Level: intermediate 1281a7e14dcfSSatish Balay 12821cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType` 1283a7e14dcfSSatish Balay @*/ 1284d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls) 1285d71ae5a4SJacob Faibussowitsch { 1286a7e14dcfSSatish Balay PetscFunctionBegin; 128794511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12884f572ea9SToby Isaac PetscAssertPointer(ls, 2); 1289a7e14dcfSSatish Balay *ls = tao->linesearch; 12903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1291a7e14dcfSSatish Balay } 1292a7e14dcfSSatish Balay 1293a7e14dcfSSatish Balay /*@ 1294a7e14dcfSSatish Balay TaoAddLineSearchCounts - Adds the number of function evaluations spent 1295a7e14dcfSSatish Balay in the line search to the running total. 1296a7e14dcfSSatish Balay 1297a7e14dcfSSatish Balay Input Parameters: 12982fe279fdSBarry Smith . tao - the `Tao` solver 1299a7e14dcfSSatish Balay 1300a7e14dcfSSatish Balay Level: developer 1301a7e14dcfSSatish Balay 13021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()` 1303a7e14dcfSSatish Balay @*/ 1304d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAddLineSearchCounts(Tao tao) 1305d71ae5a4SJacob Faibussowitsch { 1306a7e14dcfSSatish Balay PetscBool flg; 1307a7e14dcfSSatish Balay PetscInt nfeval, ngeval, nfgeval; 130847a47007SBarry Smith 1309a7e14dcfSSatish Balay PetscFunctionBegin; 1310441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1311a7e14dcfSSatish Balay if (tao->linesearch) { 13129566063dSJacob Faibussowitsch PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg)); 131394ae4db5SBarry Smith if (!flg) { 13149566063dSJacob Faibussowitsch PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval)); 1315a7e14dcfSSatish Balay tao->nfuncs += nfeval; 1316a7e14dcfSSatish Balay tao->ngrads += ngeval; 1317a7e14dcfSSatish Balay tao->nfuncgrads += nfgeval; 1318a7e14dcfSSatish Balay } 1319a7e14dcfSSatish Balay } 13203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1321a7e14dcfSSatish Balay } 1322a7e14dcfSSatish Balay 1323a7e14dcfSSatish Balay /*@ 132447450a7bSBarry Smith TaoGetSolution - Returns the vector with the current solution from the `Tao` object 1325a7e14dcfSSatish Balay 1326a7e14dcfSSatish Balay Not Collective 1327a7e14dcfSSatish Balay 1328a7e14dcfSSatish Balay Input Parameter: 132947450a7bSBarry Smith . tao - the `Tao` context 1330a7e14dcfSSatish Balay 1331a7e14dcfSSatish Balay Output Parameter: 1332a7e14dcfSSatish Balay . X - the current solution 1333a7e14dcfSSatish Balay 1334a7e14dcfSSatish Balay Level: intermediate 1335a7e14dcfSSatish Balay 133665ba42b6SBarry Smith Note: 133765ba42b6SBarry Smith The returned vector will be the same object that was passed into `TaoSetSolution()` 133865ba42b6SBarry Smith 13391cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()` 1340a7e14dcfSSatish Balay @*/ 1341d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolution(Tao tao, Vec *X) 1342d71ae5a4SJacob Faibussowitsch { 1343a7e14dcfSSatish Balay PetscFunctionBegin; 1344441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 13454f572ea9SToby Isaac PetscAssertPointer(X, 2); 1346a7e14dcfSSatish Balay *X = tao->solution; 13473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1348a7e14dcfSSatish Balay } 1349a7e14dcfSSatish Balay 1350a7e14dcfSSatish Balay /*@ 135147450a7bSBarry Smith TaoResetStatistics - Initialize the statistics collected by the `Tao` object. 1352a7e14dcfSSatish Balay These statistics include the iteration number, residual norms, and convergence status. 1353a7e14dcfSSatish Balay This routine gets called before solving each optimization problem. 1354a7e14dcfSSatish Balay 1355c3339decSBarry Smith Collective 1356a7e14dcfSSatish Balay 135747450a7bSBarry Smith Input Parameter: 1358e056e8ceSJacob Faibussowitsch . tao - the `Tao` context 1359a7e14dcfSSatish Balay 1360a7e14dcfSSatish Balay Level: developer 1361a7e14dcfSSatish Balay 13621cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 1363a7e14dcfSSatish Balay @*/ 1364d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResetStatistics(Tao tao) 1365d71ae5a4SJacob Faibussowitsch { 1366a7e14dcfSSatish Balay PetscFunctionBegin; 1367441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1368a7e14dcfSSatish Balay tao->niter = 0; 1369a7e14dcfSSatish Balay tao->nfuncs = 0; 1370a7e14dcfSSatish Balay tao->nfuncgrads = 0; 1371a7e14dcfSSatish Balay tao->ngrads = 0; 1372a7e14dcfSSatish Balay tao->nhess = 0; 1373a7e14dcfSSatish Balay tao->njac = 0; 1374a7e14dcfSSatish Balay tao->nconstraints = 0; 1375a7e14dcfSSatish Balay tao->ksp_its = 0; 1376ae93cb3cSJason Sarich tao->ksp_tot_its = 0; 1377a7e14dcfSSatish Balay tao->reason = TAO_CONTINUE_ITERATING; 1378a7e14dcfSSatish Balay tao->residual = 0.0; 1379a7e14dcfSSatish Balay tao->cnorm = 0.0; 1380a7e14dcfSSatish Balay tao->step = 0.0; 1381a7e14dcfSSatish Balay tao->lsflag = PETSC_FALSE; 1382a7e14dcfSSatish Balay if (tao->hist_reset) tao->hist_len = 0; 13833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1384a7e14dcfSSatish Balay } 1385a7e14dcfSSatish Balay 1386a7e14dcfSSatish Balay /*@C 1387e1e80dc8SAlp Dener TaoSetUpdate - Sets the general-purpose update function called 13882fe279fdSBarry Smith at the beginning of every iteration of the optimization algorithm. Called after the new solution and the gradient 1389e1e80dc8SAlp Dener is determined, but before the Hessian is computed (if applicable). 1390e1e80dc8SAlp Dener 1391c3339decSBarry Smith Logically Collective 1392e1e80dc8SAlp Dener 1393e1e80dc8SAlp Dener Input Parameters: 13942fe279fdSBarry Smith + tao - The `Tao` solver context 1395a2b725a8SWilliam Gropp - func - The function 1396e1e80dc8SAlp Dener 139720f4b53cSBarry Smith Calling sequence of `func`: 139820f4b53cSBarry Smith + tao - the optimizer context 1399e056e8ceSJacob Faibussowitsch - ctx - The current step of the iteration 1400e1e80dc8SAlp Dener 1401e1e80dc8SAlp Dener Level: advanced 1402e1e80dc8SAlp Dener 14031cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()` 1404e1e80dc8SAlp Dener @*/ 1405d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao, PetscInt, void *), void *ctx) 1406d71ae5a4SJacob Faibussowitsch { 1407e1e80dc8SAlp Dener PetscFunctionBegin; 1408e1e80dc8SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1409e1e80dc8SAlp Dener tao->ops->update = func; 1410e1e80dc8SAlp Dener tao->user_update = ctx; 14113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1412e1e80dc8SAlp Dener } 1413e1e80dc8SAlp Dener 1414e1e80dc8SAlp Dener /*@C 1415a7e14dcfSSatish Balay TaoSetConvergenceTest - Sets the function that is to be used to test 1416a7e14dcfSSatish Balay for convergence o fthe iterative minimization solution. The new convergence 141765ba42b6SBarry Smith testing routine will replace Tao's default convergence test. 1418a7e14dcfSSatish Balay 1419c3339decSBarry Smith Logically Collective 1420a7e14dcfSSatish Balay 1421a7e14dcfSSatish Balay Input Parameters: 142247450a7bSBarry Smith + tao - the `Tao` object 1423a7e14dcfSSatish Balay . conv - the routine to test for convergence 1424a7e14dcfSSatish Balay - ctx - [optional] context for private data for the convergence routine 142547450a7bSBarry Smith (may be `NULL`) 1426a7e14dcfSSatish Balay 142720f4b53cSBarry Smith Calling sequence of `conv`: 142847450a7bSBarry Smith + tao - the `Tao` object 1429a7e14dcfSSatish Balay - ctx - [optional] convergence context 1430a7e14dcfSSatish Balay 143147450a7bSBarry Smith Level: advanced 143247450a7bSBarry Smith 143365ba42b6SBarry Smith Note: 143465ba42b6SBarry Smith The new convergence testing routine should call `TaoSetConvergedReason()`. 1435a7e14dcfSSatish Balay 143610978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoMonitorSet()` 1437a7e14dcfSSatish Balay @*/ 1438d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void *), void *ctx) 1439d71ae5a4SJacob Faibussowitsch { 1440a7e14dcfSSatish Balay PetscFunctionBegin; 1441441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 144294511df7SStefano Zampini tao->ops->convergencetest = conv; 144394511df7SStefano Zampini tao->cnvP = ctx; 14443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1445a7e14dcfSSatish Balay } 1446a7e14dcfSSatish Balay 1447a7e14dcfSSatish Balay /*@C 144810978b7dSBarry Smith TaoMonitorSet - Sets an additional function that is to be used at every 1449a7e14dcfSSatish Balay iteration of the solver to display the iteration's 1450a7e14dcfSSatish Balay progress. 1451a7e14dcfSSatish Balay 1452c3339decSBarry Smith Logically Collective 1453a7e14dcfSSatish Balay 1454a7e14dcfSSatish Balay Input Parameters: 145547450a7bSBarry Smith + tao - the `Tao` solver context 14562fe279fdSBarry Smith . func - monitoring routine 14572fe279fdSBarry Smith . ctx - [optional] user-defined context for private data for the monitor routine (may be `NULL`) 14582fe279fdSBarry Smith - dest - [optional] function to destroy the context when the `Tao` is destroyed 1459a7e14dcfSSatish Balay 14602fe279fdSBarry Smith Calling sequence of `func`: 146147450a7bSBarry Smith + tao - the `Tao` solver context 14622fe279fdSBarry Smith - ctx - [optional] monitoring context 14632fe279fdSBarry Smith 14642fe279fdSBarry Smith Calling sequence of `dest`: 14652fe279fdSBarry Smith . ctx - monitoring context 1466a7e14dcfSSatish Balay 146747450a7bSBarry Smith Level: intermediate 146847450a7bSBarry Smith 146987497f52SBarry Smith Notes: 147010978b7dSBarry Smith See `TaoSetFromOptions()` for a monitoring options. 147110978b7dSBarry Smith 1472a7e14dcfSSatish Balay Several different monitoring routines may be set by calling 147310978b7dSBarry Smith `TaoMonitorSet()` multiple times; all will be called in the 1474a7e14dcfSSatish Balay order in which they were set. 1475a7e14dcfSSatish Balay 1476e056e8ceSJacob Faibussowitsch Fortran Notes: 147795452b02SPatrick Sanan Only one monitor function may be set 1478a7e14dcfSSatish Balay 1479b2dc45ddSPierre Jolivet .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoMonitorCancel()`, `TaoSetDestroyRoutine()`, `TaoView()` 1480a7e14dcfSSatish Balay @*/ 148110978b7dSBarry Smith PetscErrorCode TaoMonitorSet(Tao tao, PetscErrorCode (*func)(Tao, void *), void *ctx, PetscErrorCode (*dest)(void **)) 1482d71ae5a4SJacob Faibussowitsch { 148308d19d1fSJason Sarich PetscInt i; 14848732526dSAlp Dener PetscBool identical; 148508d19d1fSJason Sarich 1486a7e14dcfSSatish Balay PetscFunctionBegin; 1487441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 14883c859ba3SBarry Smith PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS); 148908d19d1fSJason Sarich 149008d19d1fSJason Sarich for (i = 0; i < tao->numbermonitors; i++) { 14919566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))func, ctx, dest, (PetscErrorCode(*)(void))tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical)); 14923ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 149308d19d1fSJason Sarich } 1494a7e14dcfSSatish Balay tao->monitor[tao->numbermonitors] = func; 14958732526dSAlp Dener tao->monitorcontext[tao->numbermonitors] = (void *)ctx; 1496a7e14dcfSSatish Balay tao->monitordestroy[tao->numbermonitors] = dest; 1497a7e14dcfSSatish Balay ++tao->numbermonitors; 14983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1499a7e14dcfSSatish Balay } 1500a7e14dcfSSatish Balay 1501a7e14dcfSSatish Balay /*@ 1502b2dc45ddSPierre Jolivet TaoMonitorCancel - Clears all the monitor functions for a `Tao` object. 1503a7e14dcfSSatish Balay 1504c3339decSBarry Smith Logically Collective 1505a7e14dcfSSatish Balay 150647450a7bSBarry Smith Input Parameter: 150747450a7bSBarry Smith . tao - the `Tao` solver context 1508a7e14dcfSSatish Balay 15093c7db156SBarry Smith Options Database Key: 1510b2dc45ddSPierre Jolivet . -tao_monitor_cancel - cancels all monitors that have been hardwired 151110978b7dSBarry Smith into a code by calls to `TaoMonitorSet()`, but does not cancel those 1512a7e14dcfSSatish Balay set via the options database 1513a7e14dcfSSatish Balay 1514a7e14dcfSSatish Balay Level: advanced 1515a7e14dcfSSatish Balay 151647450a7bSBarry Smith Note: 151747450a7bSBarry Smith There is no way to clear one specific monitor from a `Tao` object. 151847450a7bSBarry Smith 151910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1520a7e14dcfSSatish Balay @*/ 1521b2dc45ddSPierre Jolivet PetscErrorCode TaoMonitorCancel(Tao tao) 1522d71ae5a4SJacob Faibussowitsch { 1523a7e14dcfSSatish Balay PetscInt i; 152447a47007SBarry Smith 1525a7e14dcfSSatish Balay PetscFunctionBegin; 1526441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1527a7e14dcfSSatish Balay for (i = 0; i < tao->numbermonitors; i++) { 152848a46eb9SPierre Jolivet if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i])); 1529a7e14dcfSSatish Balay } 1530a7e14dcfSSatish Balay tao->numbermonitors = 0; 15313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1532a7e14dcfSSatish Balay } 1533a7e14dcfSSatish Balay 15347fab98ebSJason Sarich /*@ 153547450a7bSBarry Smith TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()` 1536a7e14dcfSSatish Balay 1537c3339decSBarry Smith Collective 1538a7e14dcfSSatish Balay 1539a7e14dcfSSatish Balay Input Parameters: 154047450a7bSBarry Smith + tao - the `Tao` context 154147450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1542a7e14dcfSSatish Balay 154347450a7bSBarry Smith Options Database Key: 1544147403d9SBarry Smith . -tao_monitor - turn on default monitoring 1545a7e14dcfSSatish Balay 1546a7e14dcfSSatish Balay Level: advanced 1547a7e14dcfSSatish Balay 154847450a7bSBarry Smith Note: 154947450a7bSBarry Smith This monitor prints the function value and gradient 155047450a7bSBarry Smith norm at each iteration. 155147450a7bSBarry Smith 155210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1553a7e14dcfSSatish Balay @*/ 1554d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDefault(Tao tao, void *ctx) 1555d71ae5a4SJacob Faibussowitsch { 155663b15415SAlp Dener PetscInt its, tabs; 1557a7e14dcfSSatish Balay PetscReal fct, gnorm; 15588163d661SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 155947a47007SBarry Smith 1560a7e14dcfSSatish Balay PetscFunctionBegin; 156194511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 15628163d661SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1563a7e14dcfSSatish Balay its = tao->niter; 1564a7e14dcfSSatish Balay fct = tao->fc; 1565a7e14dcfSSatish Balay gnorm = tao->residual; 15669566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 15679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1568494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 15699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1570494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 157163b15415SAlp Dener } 157263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 15739566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 157408d19d1fSJason Sarich if (gnorm >= PETSC_INFINITY) { 15759566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n")); 157608d19d1fSJason Sarich } else { 15779566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 157808d19d1fSJason Sarich } 15799566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 15803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1581a7e14dcfSSatish Balay } 1582a7e14dcfSSatish Balay 15837fab98ebSJason Sarich /*@ 158410978b7dSBarry Smith TaoMonitorGlobalization - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method. 15858d5ead36SAlp Dener 1586c3339decSBarry Smith Collective 15878d5ead36SAlp Dener 15888d5ead36SAlp Dener Input Parameters: 158947450a7bSBarry Smith + tao - the `Tao` context 159047450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 15918d5ead36SAlp Dener 159247450a7bSBarry Smith Options Database Key: 159310978b7dSBarry Smith . -tao_monitor_globalization - turn on monitoring with globalization information 15948d5ead36SAlp Dener 15958d5ead36SAlp Dener Level: advanced 15968d5ead36SAlp Dener 159747450a7bSBarry Smith Note: 159847450a7bSBarry Smith This monitor prints the function value and gradient norm at each 159947450a7bSBarry Smith iteration, as well as the step size and trust radius. Note that the 160047450a7bSBarry Smith step size and trust radius may be the same for some algorithms. 160147450a7bSBarry Smith 160210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 16038d5ead36SAlp Dener @*/ 160410978b7dSBarry Smith PetscErrorCode TaoMonitorGlobalization(Tao tao, void *ctx) 1605d71ae5a4SJacob Faibussowitsch { 16068d5ead36SAlp Dener PetscInt its, tabs; 16078d5ead36SAlp Dener PetscReal fct, gnorm, stp, tr; 16088d5ead36SAlp Dener PetscViewer viewer = (PetscViewer)ctx; 16098d5ead36SAlp Dener 16108d5ead36SAlp Dener PetscFunctionBegin; 161194511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16128d5ead36SAlp Dener PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 16138d5ead36SAlp Dener its = tao->niter; 16148d5ead36SAlp Dener fct = tao->fc; 16158d5ead36SAlp Dener gnorm = tao->residual; 16168d5ead36SAlp Dener stp = tao->step; 16178d5ead36SAlp Dener tr = tao->trust; 16189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1620494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 16219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1622494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 16238d5ead36SAlp Dener } 162463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 16259566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 16268d5ead36SAlp Dener if (gnorm >= PETSC_INFINITY) { 16279566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf,")); 16288d5ead36SAlp Dener } else { 16299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g,", (double)gnorm)); 16308d5ead36SAlp Dener } 16319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Step: %g, Trust: %g\n", (double)stp, (double)tr)); 16329566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16348d5ead36SAlp Dener } 16358d5ead36SAlp Dener 16368d5ead36SAlp Dener /*@ 163710978b7dSBarry Smith TaoMonitorDefaultShort - Routine for monitoring progress of `TaoSolve()` that displays fewer digits than `TaoMonitorDefault()` 1638a7e14dcfSSatish Balay 1639c3339decSBarry Smith Collective 1640a7e14dcfSSatish Balay 1641a7e14dcfSSatish Balay Input Parameters: 164247450a7bSBarry Smith + tao - the `Tao` context 164347450a7bSBarry Smith - ctx - `PetscViewer` context of type `PETSCVIEWERASCII` 1644a7e14dcfSSatish Balay 164547450a7bSBarry Smith Options Database Key: 164610978b7dSBarry Smith . -tao_monitor_short - turn on default short monitoring 1647a7e14dcfSSatish Balay 1648a7e14dcfSSatish Balay Level: advanced 1649a7e14dcfSSatish Balay 165047450a7bSBarry Smith Note: 165147450a7bSBarry Smith Same as `TaoMonitorDefault()` except 165247450a7bSBarry Smith it prints fewer digits of the residual as the residual gets smaller. 165347450a7bSBarry Smith This is because the later digits are meaningless and are often 165447450a7bSBarry Smith different on different machines; by using this routine different 165547450a7bSBarry Smith machines will usually generate the same output. 165647450a7bSBarry Smith 165710978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1658a7e14dcfSSatish Balay @*/ 165910978b7dSBarry Smith PetscErrorCode TaoMonitorDefaultShort(Tao tao, void *ctx) 1660d71ae5a4SJacob Faibussowitsch { 16612c9b8b83SAlp Dener PetscInt its, tabs; 1662a7e14dcfSSatish Balay PetscReal fct, gnorm; 16634d4332d5SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 166447a47007SBarry Smith 1665a7e14dcfSSatish Balay PetscFunctionBegin; 166694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16674d4332d5SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1668a7e14dcfSSatish Balay its = tao->niter; 1669a7e14dcfSSatish Balay fct = tao->fc; 1670a7e14dcfSSatish Balay gnorm = tao->residual; 16719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 167363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %3" PetscInt_FMT ",", its)); 16749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value %g,", (double)fct)); 1675d393f493SBarry Smith if (gnorm >= PETSC_INFINITY) { 16769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n")); 167708d19d1fSJason Sarich } else if (gnorm > 1.e-6) { 16789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 1679a7e14dcfSSatish Balay } else if (gnorm > 1.e-11) { 16809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-6 \n")); 1681a7e14dcfSSatish Balay } else { 16829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-11 \n")); 1683a7e14dcfSSatish Balay } 16849566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1686a7e14dcfSSatish Balay } 1687a7e14dcfSSatish Balay 16887fab98ebSJason Sarich /*@ 168910978b7dSBarry Smith TaoMonitorConstraintNorm - same as `TaoMonitorDefault()` except 169047450a7bSBarry Smith it prints the norm of the constraint function. 1691a7e14dcfSSatish Balay 1692c3339decSBarry Smith Collective 1693a7e14dcfSSatish Balay 1694a7e14dcfSSatish Balay Input Parameters: 169547450a7bSBarry Smith + tao - the `Tao` context 169647450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1697a7e14dcfSSatish Balay 169847450a7bSBarry Smith Options Database Key: 169910978b7dSBarry Smith . -tao_monitor_constraint_norm - monitor the constraints 1700a7e14dcfSSatish Balay 1701a7e14dcfSSatish Balay Level: advanced 1702a7e14dcfSSatish Balay 170310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1704a7e14dcfSSatish Balay @*/ 170510978b7dSBarry Smith PetscErrorCode TaoMonitorConstraintNorm(Tao tao, void *ctx) 1706d71ae5a4SJacob Faibussowitsch { 17072c9b8b83SAlp Dener PetscInt its, tabs; 1708a7e14dcfSSatish Balay PetscReal fct, gnorm; 1709d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1710a7e14dcfSSatish Balay 1711a7e14dcfSSatish Balay PetscFunctionBegin; 171294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1713d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1714a7e14dcfSSatish Balay its = tao->niter; 1715a7e14dcfSSatish Balay fct = tao->fc; 1716a7e14dcfSSatish Balay gnorm = tao->residual; 17179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 17189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 171963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", its)); 17209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 17219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g ", (double)gnorm)); 17229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Constraint: %g \n", (double)tao->cnorm)); 17239566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 17243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1725a7e14dcfSSatish Balay } 1726a7e14dcfSSatish Balay 1727a7e14dcfSSatish Balay /*@C 172810978b7dSBarry Smith TaoMonitorSolution - Views the solution at each iteration of `TaoSolve()` 1729a7e14dcfSSatish Balay 1730c3339decSBarry Smith Collective 1731a7e14dcfSSatish Balay 1732a7e14dcfSSatish Balay Input Parameters: 173347450a7bSBarry Smith + tao - the `Tao` context 173447450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1735a7e14dcfSSatish Balay 173647450a7bSBarry Smith Options Database Key: 173710978b7dSBarry Smith . -tao_monitor_solution - view the solution 1738a7e14dcfSSatish Balay 1739a7e14dcfSSatish Balay Level: advanced 1740a7e14dcfSSatish Balay 174110978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1742a7e14dcfSSatish Balay @*/ 174310978b7dSBarry Smith PetscErrorCode TaoMonitorSolution(Tao tao, void *ctx) 1744d71ae5a4SJacob Faibussowitsch { 1745feb237baSPierre Jolivet PetscViewer viewer = (PetscViewer)ctx; 174647a47007SBarry Smith 1747a7e14dcfSSatish Balay PetscFunctionBegin; 174894511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1749d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 17509566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, viewer)); 17513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1752a7e14dcfSSatish Balay } 1753a7e14dcfSSatish Balay 1754a7e14dcfSSatish Balay /*@C 175510978b7dSBarry Smith TaoMonitorGradient - Views the gradient at each iteration of `TaoSolve()` 1756a7e14dcfSSatish Balay 1757c3339decSBarry Smith Collective 1758a7e14dcfSSatish Balay 1759a7e14dcfSSatish Balay Input Parameters: 176047450a7bSBarry Smith + tao - the `Tao` context 176147450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1762a7e14dcfSSatish Balay 176347450a7bSBarry Smith Options Database Key: 176410978b7dSBarry Smith . -tao_monitor_gradient - view the gradient at each iteration 1765a7e14dcfSSatish Balay 1766a7e14dcfSSatish Balay Level: advanced 1767a7e14dcfSSatish Balay 176810978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1769a7e14dcfSSatish Balay @*/ 177010978b7dSBarry Smith PetscErrorCode TaoMonitorGradient(Tao tao, void *ctx) 1771d71ae5a4SJacob Faibussowitsch { 1772d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 177347a47007SBarry Smith 1774a7e14dcfSSatish Balay PetscFunctionBegin; 177594511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1776d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 17779566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, viewer)); 17783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1779a7e14dcfSSatish Balay } 1780a7e14dcfSSatish Balay 1781a7e14dcfSSatish Balay /*@C 178210978b7dSBarry Smith TaoMonitorStep - Views the step-direction at each iteration of `TaoSolve()` 1783a7e14dcfSSatish Balay 1784c3339decSBarry Smith Collective 1785a7e14dcfSSatish Balay 1786a7e14dcfSSatish Balay Input Parameters: 178747450a7bSBarry Smith + tao - the `Tao` context 178847450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1789a7e14dcfSSatish Balay 179047450a7bSBarry Smith Options Database Key: 179110978b7dSBarry Smith . -tao_monitor_step - view the step vector at each iteration 1792a7e14dcfSSatish Balay 1793a7e14dcfSSatish Balay Level: advanced 1794a7e14dcfSSatish Balay 179510978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1796a7e14dcfSSatish Balay @*/ 179710978b7dSBarry Smith PetscErrorCode TaoMonitorStep(Tao tao, void *ctx) 1798d71ae5a4SJacob Faibussowitsch { 1799d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1800d393f493SBarry Smith 1801a7e14dcfSSatish Balay PetscFunctionBegin; 180294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1803d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18049566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 18053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1806a7e14dcfSSatish Balay } 1807a7e14dcfSSatish Balay 1808a7e14dcfSSatish Balay /*@C 180910978b7dSBarry Smith TaoMonitorSolutionDraw - Plots the solution at each iteration of `TaoSolve()` 1810a7e14dcfSSatish Balay 1811c3339decSBarry Smith Collective 1812a7e14dcfSSatish Balay 1813a7e14dcfSSatish Balay Input Parameters: 181447450a7bSBarry Smith + tao - the `Tao` context 181565ba42b6SBarry Smith - ctx - `TaoMonitorDraw` context 1816a7e14dcfSSatish Balay 181747450a7bSBarry Smith Options Database Key: 181810978b7dSBarry Smith . -tao_monitor_solution_draw - draw the solution at each iteration 1819a7e14dcfSSatish Balay 1820a7e14dcfSSatish Balay Level: advanced 1821a7e14dcfSSatish Balay 182210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSolution()`, `TaoMonitorSet()`, `TaoMonitorGradientDraw()` 1823a7e14dcfSSatish Balay @*/ 182410978b7dSBarry Smith PetscErrorCode TaoMonitorSolutionDraw(Tao tao, void *ctx) 1825d71ae5a4SJacob Faibussowitsch { 1826e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 182747a47007SBarry Smith 1828a7e14dcfSSatish Balay PetscFunctionBegin; 182994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 18303ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 18319566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, ictx->viewer)); 18323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1833a7e14dcfSSatish Balay } 1834a7e14dcfSSatish Balay 1835a7e14dcfSSatish Balay /*@C 183610978b7dSBarry Smith TaoMonitorGradientDraw - Plots the gradient at each iteration of `TaoSolve()` 1837a7e14dcfSSatish Balay 1838c3339decSBarry Smith Collective 1839a7e14dcfSSatish Balay 1840a7e14dcfSSatish Balay Input Parameters: 184147450a7bSBarry Smith + tao - the `Tao` context 184265ba42b6SBarry Smith - ctx - `PetscViewer` context 1843a7e14dcfSSatish Balay 184447450a7bSBarry Smith Options Database Key: 184510978b7dSBarry Smith . -tao_monitor_gradient_draw - draw the gradient at each iteration 1846a7e14dcfSSatish Balay 1847a7e14dcfSSatish Balay Level: advanced 1848a7e14dcfSSatish Balay 184910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorGradient()`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw()` 1850a7e14dcfSSatish Balay @*/ 185110978b7dSBarry Smith PetscErrorCode TaoMonitorGradientDraw(Tao tao, void *ctx) 1852d71ae5a4SJacob Faibussowitsch { 1853e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 185447a47007SBarry Smith 1855a7e14dcfSSatish Balay PetscFunctionBegin; 185694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 18573ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 18589566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, ictx->viewer)); 18593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1860a7e14dcfSSatish Balay } 1861a7e14dcfSSatish Balay 1862a7e14dcfSSatish Balay /*@C 186310978b7dSBarry Smith TaoMonitorStepDraw - Plots the step direction at each iteration of `TaoSolve()` 1864a7e14dcfSSatish Balay 1865c3339decSBarry Smith Collective 1866a7e14dcfSSatish Balay 1867a7e14dcfSSatish Balay Input Parameters: 186847450a7bSBarry Smith + tao - the `Tao` context 186947450a7bSBarry Smith - ctx - the `PetscViewer` context 1870a7e14dcfSSatish Balay 187147450a7bSBarry Smith Options Database Key: 187210978b7dSBarry Smith . -tao_monitor_step_draw - draw the step direction at each iteration 1873a7e14dcfSSatish Balay 1874a7e14dcfSSatish Balay Level: advanced 1875a7e14dcfSSatish Balay 187610978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw` 1877a7e14dcfSSatish Balay @*/ 187810978b7dSBarry Smith PetscErrorCode TaoMonitorStepDraw(Tao tao, void *ctx) 1879d71ae5a4SJacob Faibussowitsch { 188094511df7SStefano Zampini PetscViewer viewer = (PetscViewer)ctx; 188147a47007SBarry Smith 1882a7e14dcfSSatish Balay PetscFunctionBegin; 188394511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 188494511df7SStefano Zampini PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18859566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 18863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1887a7e14dcfSSatish Balay } 1888a7e14dcfSSatish Balay 1889a7e14dcfSSatish Balay /*@C 189010978b7dSBarry Smith TaoMonitorResidual - Views the least-squares residual at each iteration of `TaoSolve()` 1891a7e14dcfSSatish Balay 1892c3339decSBarry Smith Collective 1893a7e14dcfSSatish Balay 1894a7e14dcfSSatish Balay Input Parameters: 189547450a7bSBarry Smith + tao - the `Tao` context 189647450a7bSBarry Smith - ctx - the `PetscViewer` context or `NULL` 1897a7e14dcfSSatish Balay 189847450a7bSBarry Smith Options Database Key: 189910978b7dSBarry Smith . -tao_monitor_ls_residual - view the residual at each iteration 1900a7e14dcfSSatish Balay 1901a7e14dcfSSatish Balay Level: advanced 1902a7e14dcfSSatish Balay 190310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1904a7e14dcfSSatish Balay @*/ 190510978b7dSBarry Smith PetscErrorCode TaoMonitorResidual(Tao tao, void *ctx) 1906d71ae5a4SJacob Faibussowitsch { 1907d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 190847a47007SBarry Smith 1909a7e14dcfSSatish Balay PetscFunctionBegin; 191094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1911d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 19129566063dSJacob Faibussowitsch PetscCall(VecView(tao->ls_res, viewer)); 19133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1914a7e14dcfSSatish Balay } 1915a7e14dcfSSatish Balay 19167fab98ebSJason Sarich /*@ 1917a7e14dcfSSatish Balay TaoDefaultConvergenceTest - Determines whether the solver should continue iterating 1918a7e14dcfSSatish Balay or terminate. 1919a7e14dcfSSatish Balay 1920c3339decSBarry Smith Collective 1921a7e14dcfSSatish Balay 1922a7e14dcfSSatish Balay Input Parameters: 192347450a7bSBarry Smith + tao - the `Tao` context 1924a7e14dcfSSatish Balay - dummy - unused dummy context 1925a7e14dcfSSatish Balay 192647450a7bSBarry Smith Level: developer 192747450a7bSBarry Smith 1928a7e14dcfSSatish Balay Notes: 1929a7e14dcfSSatish Balay This routine checks the residual in the optimality conditions, the 1930a7e14dcfSSatish Balay relative residual in the optimity conditions, the number of function 1931a7e14dcfSSatish Balay evaluations, and the function value to test convergence. Some 1932a7e14dcfSSatish Balay solvers may use different convergence routines. 1933a7e14dcfSSatish Balay 19341cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()` 1935a7e14dcfSSatish Balay @*/ 1936d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy) 1937d71ae5a4SJacob Faibussowitsch { 1938a7e14dcfSSatish Balay PetscInt niter = tao->niter, nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 1939a7e14dcfSSatish Balay PetscInt max_funcs = tao->max_funcs; 1940a7e14dcfSSatish Balay PetscReal gnorm = tao->residual, gnorm0 = tao->gnorm0; 1941a7e14dcfSSatish Balay PetscReal f = tao->fc, steptol = tao->steptol, trradius = tao->step; 1942a7e14dcfSSatish Balay PetscReal gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol; 1943e52336cbSBarry Smith PetscReal catol = tao->catol, crtol = tao->crtol; 1944e52336cbSBarry Smith PetscReal fmin = tao->fmin, cnorm = tao->cnorm; 1945e4cb33bbSBarry Smith TaoConvergedReason reason = tao->reason; 1946a7e14dcfSSatish Balay 1947a7e14dcfSSatish Balay PetscFunctionBegin; 1948441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 19493ba16761SJacob Faibussowitsch if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS); 1950a7e14dcfSSatish Balay 1951a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(f)) { 19529566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Failed to converged, function value is Inf or NaN\n")); 1953a7e14dcfSSatish Balay reason = TAO_DIVERGED_NAN; 1954a7e14dcfSSatish Balay } else if (f <= fmin && cnorm <= catol) { 19559566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin)); 1956a7e14dcfSSatish Balay reason = TAO_CONVERGED_MINF; 1957a7e14dcfSSatish Balay } else if (gnorm <= gatol && cnorm <= catol) { 19589566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol)); 1959a7e14dcfSSatish Balay reason = TAO_CONVERGED_GATOL; 1960a7e14dcfSSatish Balay } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) { 19619566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol)); 1962a7e14dcfSSatish Balay reason = TAO_CONVERGED_GRTOL; 1963e1d16bb9SBarry Smith } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) { 19649566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol)); 1965a7e14dcfSSatish Balay reason = TAO_CONVERGED_GTTOL; 196694511df7SStefano Zampini } else if (max_funcs >= 0 && nfuncs > max_funcs) { 19679566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs)); 1968a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXFCN; 1969a7e14dcfSSatish Balay } else if (tao->lsflag != 0) { 19709566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tao Line Search failure.\n")); 1971a7e14dcfSSatish Balay reason = TAO_DIVERGED_LS_FAILURE; 1972a7e14dcfSSatish Balay } else if (trradius < steptol && niter > 0) { 19739566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol)); 1974a7e14dcfSSatish Balay reason = TAO_CONVERGED_STEPTOL; 1975e031d6f5SAlp Dener } else if (niter >= tao->max_it) { 197663a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it)); 1977a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXITS; 1978a7e14dcfSSatish Balay } else { 1979a7e14dcfSSatish Balay reason = TAO_CONTINUE_ITERATING; 1980a7e14dcfSSatish Balay } 1981a7e14dcfSSatish Balay tao->reason = reason; 19823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1983a7e14dcfSSatish Balay } 1984a7e14dcfSSatish Balay 1985cc4c1da9SBarry Smith /*@ 1986a7e14dcfSSatish Balay TaoSetOptionsPrefix - Sets the prefix used for searching for all 198765ba42b6SBarry Smith Tao options in the database. 1988a7e14dcfSSatish Balay 1989c3339decSBarry Smith Logically Collective 1990a7e14dcfSSatish Balay 1991a7e14dcfSSatish Balay Input Parameters: 199247450a7bSBarry Smith + tao - the `Tao` context 1993e056e8ceSJacob Faibussowitsch - p - the prefix string to prepend to all Tao option requests 1994a7e14dcfSSatish Balay 19952fe279fdSBarry Smith Level: advanced 19962fe279fdSBarry Smith 1997a7e14dcfSSatish Balay Notes: 1998a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 1999a7e14dcfSSatish Balay The first character of all runtime options is AUTOMATICALLY the hyphen. 2000a7e14dcfSSatish Balay 2001a7e14dcfSSatish Balay For example, to distinguish between the runtime options for two 200265ba42b6SBarry Smith different Tao solvers, one could call 2003a7e14dcfSSatish Balay .vb 2004a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao1,"sys1_") 2005a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao2,"sys2_") 2006a7e14dcfSSatish Balay .ve 2007a7e14dcfSSatish Balay 2008a7e14dcfSSatish Balay This would enable use of different options for each system, such as 2009a7e14dcfSSatish Balay .vb 20109fa2b5dcSStefano Zampini -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3 20119fa2b5dcSStefano Zampini -sys2_tao_method lmvm -sys2_tao_grtol 1.e-4 2012a7e14dcfSSatish Balay .ve 2013a7e14dcfSSatish Balay 20141cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()` 2015a7e14dcfSSatish Balay @*/ 2016d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[]) 2017d71ae5a4SJacob Faibussowitsch { 2018a7e14dcfSSatish Balay PetscFunctionBegin; 201994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20209566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p)); 20211baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p)); 20221baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p)); 20233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2024a7e14dcfSSatish Balay } 2025a7e14dcfSSatish Balay 2026cc4c1da9SBarry Smith /*@ 202747450a7bSBarry Smith TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database. 2028a7e14dcfSSatish Balay 2029c3339decSBarry Smith Logically Collective 2030a7e14dcfSSatish Balay 2031a7e14dcfSSatish Balay Input Parameters: 203247450a7bSBarry Smith + tao - the `Tao` solver context 2033e056e8ceSJacob Faibussowitsch - p - the prefix string to prepend to all `Tao` option requests 2034a7e14dcfSSatish Balay 20352fe279fdSBarry Smith Level: advanced 20362fe279fdSBarry Smith 203765ba42b6SBarry Smith Note: 2038a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 203965ba42b6SBarry Smith The first character of all runtime options is automatically the hyphen. 2040a7e14dcfSSatish Balay 20411cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()` 2042a7e14dcfSSatish Balay @*/ 2043d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[]) 2044d71ae5a4SJacob Faibussowitsch { 2045a7e14dcfSSatish Balay PetscFunctionBegin; 204694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20479566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p)); 20481baa6e33SBarry Smith if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p)); 20491baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p)); 20503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2051a7e14dcfSSatish Balay } 2052a7e14dcfSSatish Balay 2053cc4c1da9SBarry Smith /*@ 2054a7e14dcfSSatish Balay TaoGetOptionsPrefix - Gets the prefix used for searching for all 205565ba42b6SBarry Smith Tao options in the database 2056a7e14dcfSSatish Balay 2057a7e14dcfSSatish Balay Not Collective 2058a7e14dcfSSatish Balay 205947450a7bSBarry Smith Input Parameter: 206047450a7bSBarry Smith . tao - the `Tao` context 2061a7e14dcfSSatish Balay 206247450a7bSBarry Smith Output Parameter: 2063e056e8ceSJacob Faibussowitsch . p - pointer to the prefix string used is returned 2064a7e14dcfSSatish Balay 2065e056e8ceSJacob Faibussowitsch Fortran Notes: 206647450a7bSBarry Smith Pass in a string 'prefix' of sufficient length to hold the prefix. 2067a7e14dcfSSatish Balay 2068a7e14dcfSSatish Balay Level: advanced 2069a7e14dcfSSatish Balay 20701cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()` 2071a7e14dcfSSatish Balay @*/ 2072d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[]) 2073d71ae5a4SJacob Faibussowitsch { 207494511df7SStefano Zampini PetscFunctionBegin; 207594511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20769566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p)); 20773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2078a7e14dcfSSatish Balay } 2079a7e14dcfSSatish Balay 2080cc4c1da9SBarry Smith /*@ 208147450a7bSBarry Smith TaoSetType - Sets the `TaoType` for the minimization solver. 2082a7e14dcfSSatish Balay 2083c3339decSBarry Smith Collective 2084a7e14dcfSSatish Balay 2085a7e14dcfSSatish Balay Input Parameters: 2086e056e8ceSJacob Faibussowitsch + tao - the `Tao` solver context 2087a7e14dcfSSatish Balay - type - a known method 2088a7e14dcfSSatish Balay 2089a7e14dcfSSatish Balay Options Database Key: 209058417fe7SBarry Smith . -tao_type <type> - Sets the method; use -help for a list 209158417fe7SBarry Smith of available methods (for instance, "-tao_type lmvm" or "-tao_type tron") 2092a7e14dcfSSatish Balay 2093a7e14dcfSSatish Balay Level: intermediate 2094a7e14dcfSSatish Balay 20951cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType` 2096a7e14dcfSSatish Balay @*/ 2097d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetType(Tao tao, TaoType type) 2098d71ae5a4SJacob Faibussowitsch { 2099441846f8SBarry Smith PetscErrorCode (*create_xxx)(Tao); 2100a7e14dcfSSatish Balay PetscBool issame; 2101a7e14dcfSSatish Balay 2102a7e14dcfSSatish Balay PetscFunctionBegin; 2103441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2104a7e14dcfSSatish Balay 21059566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame)); 21063ba16761SJacob Faibussowitsch if (issame) PetscFunctionReturn(PETSC_SUCCESS); 2107a7e14dcfSSatish Balay 21089566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(TaoList, type, (void (**)(void)) & create_xxx)); 21093c859ba3SBarry Smith PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type); 2110a7e14dcfSSatish Balay 2111a7e14dcfSSatish Balay /* Destroy the existing solver information */ 2112dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, destroy); 21131baa6e33SBarry Smith PetscCall(KSPDestroy(&tao->ksp)); 21149566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&tao->linesearch)); 211583c8fe1dSLisandro Dalcin tao->ops->setup = NULL; 211683c8fe1dSLisandro Dalcin tao->ops->solve = NULL; 211783c8fe1dSLisandro Dalcin tao->ops->view = NULL; 211883c8fe1dSLisandro Dalcin tao->ops->setfromoptions = NULL; 211983c8fe1dSLisandro Dalcin tao->ops->destroy = NULL; 2120a7e14dcfSSatish Balay 2121a7e14dcfSSatish Balay tao->setupcalled = PETSC_FALSE; 2122a7e14dcfSSatish Balay 21239566063dSJacob Faibussowitsch PetscCall((*create_xxx)(tao)); 21249566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type)); 21253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2126a7e14dcfSSatish Balay } 212747a47007SBarry Smith 212867be906fSBarry Smith /*@C 212947450a7bSBarry Smith TaoRegister - Adds a method to the Tao package for minimization. 2130a7e14dcfSSatish Balay 2131cc4c1da9SBarry Smith Not Collective, No Fortran Support 2132a7e14dcfSSatish Balay 2133a7e14dcfSSatish Balay Input Parameters: 2134a7e14dcfSSatish Balay + sname - name of a new user-defined solver 2135a7e14dcfSSatish Balay - func - routine to Create method context 2136a7e14dcfSSatish Balay 2137e056e8ceSJacob Faibussowitsch Example Usage: 2138a7e14dcfSSatish Balay .vb 2139441846f8SBarry Smith TaoRegister("my_solver", MySolverCreate); 2140a7e14dcfSSatish Balay .ve 2141a7e14dcfSSatish Balay 2142a7e14dcfSSatish Balay Then, your solver can be chosen with the procedural interface via 2143a7e14dcfSSatish Balay $ TaoSetType(tao, "my_solver") 2144a7e14dcfSSatish Balay or at runtime via the option 214558417fe7SBarry Smith $ -tao_type my_solver 2146a7e14dcfSSatish Balay 2147a7e14dcfSSatish Balay Level: advanced 2148a7e14dcfSSatish Balay 214967be906fSBarry Smith Note: 215067be906fSBarry Smith `TaoRegister()` may be called multiple times to add several user-defined solvers. 215167be906fSBarry Smith 21521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()` 215367be906fSBarry Smith @*/ 2154d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao)) 2155d71ae5a4SJacob Faibussowitsch { 2156a7e14dcfSSatish Balay PetscFunctionBegin; 21579566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 21589566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&TaoList, sname, (void (*)(void))func)); 21593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2160a7e14dcfSSatish Balay } 2161a7e14dcfSSatish Balay 2162a7e14dcfSSatish Balay /*@C 2163441846f8SBarry Smith TaoRegisterDestroy - Frees the list of minimization solvers that were 216447450a7bSBarry Smith registered by `TaoRegister()`. 2165a7e14dcfSSatish Balay 2166a7e14dcfSSatish Balay Not Collective 2167a7e14dcfSSatish Balay 2168a7e14dcfSSatish Balay Level: advanced 2169a7e14dcfSSatish Balay 21701cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()` 2171a7e14dcfSSatish Balay @*/ 2172d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegisterDestroy(void) 2173d71ae5a4SJacob Faibussowitsch { 2174a7e14dcfSSatish Balay PetscFunctionBegin; 21759566063dSJacob Faibussowitsch PetscCall(PetscFunctionListDestroy(&TaoList)); 2176441846f8SBarry Smith TaoRegisterAllCalled = PETSC_FALSE; 21773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2178a7e14dcfSSatish Balay } 217947a47007SBarry Smith 21808931d482SJason Sarich /*@ 218147450a7bSBarry Smith TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed 21828931d482SJason Sarich at this time. 21838931d482SJason Sarich 21848931d482SJason Sarich Not Collective 21858931d482SJason Sarich 21868931d482SJason Sarich Input Parameter: 218747450a7bSBarry Smith . tao - the `Tao` context 21888931d482SJason Sarich 21898931d482SJason Sarich Output Parameter: 21908931d482SJason Sarich . iter - iteration number 21918931d482SJason Sarich 21928931d482SJason Sarich Notes: 21938931d482SJason Sarich For example, during the computation of iteration 2 this would return 1. 21948931d482SJason Sarich 21958931d482SJason Sarich Level: intermediate 21968931d482SJason Sarich 21971cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()` 21988931d482SJason Sarich @*/ 2199d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter) 2200d71ae5a4SJacob Faibussowitsch { 22018931d482SJason Sarich PetscFunctionBegin; 22028931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22034f572ea9SToby Isaac PetscAssertPointer(iter, 2); 22048931d482SJason Sarich *iter = tao->niter; 22053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22068931d482SJason Sarich } 22078931d482SJason Sarich 22088931d482SJason Sarich /*@ 220947450a7bSBarry Smith TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient) 221079f5d8caSBarry Smith at this time. 221179f5d8caSBarry Smith 221279f5d8caSBarry Smith Not Collective 221379f5d8caSBarry Smith 221479f5d8caSBarry Smith Input Parameter: 221547450a7bSBarry Smith . tao - the `Tao` context 221679f5d8caSBarry Smith 221779f5d8caSBarry Smith Output Parameter: 221879f5d8caSBarry Smith . value - the current value 221979f5d8caSBarry Smith 222079f5d8caSBarry Smith Level: intermediate 222179f5d8caSBarry Smith 2222e056e8ceSJacob Faibussowitsch Developer Notes: 222367be906fSBarry Smith This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has 222447450a7bSBarry Smith a different meaning. For some reason `Tao` sometimes calls the gradient the residual. 222579f5d8caSBarry Smith 22261cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()` 222779f5d8caSBarry Smith @*/ 2228d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value) 2229d71ae5a4SJacob Faibussowitsch { 223079f5d8caSBarry Smith PetscFunctionBegin; 223179f5d8caSBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22324f572ea9SToby Isaac PetscAssertPointer(value, 2); 223379f5d8caSBarry Smith *value = tao->residual; 22343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 223579f5d8caSBarry Smith } 223679f5d8caSBarry Smith 223779f5d8caSBarry Smith /*@ 22388931d482SJason Sarich TaoSetIterationNumber - Sets the current iteration number. 22398931d482SJason Sarich 2240c3339decSBarry Smith Logically Collective 22418931d482SJason Sarich 2242d8d19677SJose E. Roman Input Parameters: 224347450a7bSBarry Smith + tao - the `Tao` context 2244a2b725a8SWilliam Gropp - iter - iteration number 22458931d482SJason Sarich 22468931d482SJason Sarich Level: developer 22478931d482SJason Sarich 22481cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 22498931d482SJason Sarich @*/ 2250d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter) 2251d71ae5a4SJacob Faibussowitsch { 22528931d482SJason Sarich PetscFunctionBegin; 22538931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 225494511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 22559566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 22568931d482SJason Sarich tao->niter = iter; 22579566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 22583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22598931d482SJason Sarich } 22608931d482SJason Sarich 22618931d482SJason Sarich /*@ 226247450a7bSBarry Smith TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations 22638931d482SJason Sarich completed. This number keeps accumulating if multiple solves 226447450a7bSBarry Smith are called with the `Tao` object. 22658931d482SJason Sarich 22668931d482SJason Sarich Not Collective 22678931d482SJason Sarich 22688931d482SJason Sarich Input Parameter: 226947450a7bSBarry Smith . tao - the `Tao` context 22708931d482SJason Sarich 22718931d482SJason Sarich Output Parameter: 227247450a7bSBarry Smith . iter - number of iterations 22738931d482SJason Sarich 22748931d482SJason Sarich Level: intermediate 22758931d482SJason Sarich 227620f4b53cSBarry Smith Note: 227767be906fSBarry Smith The total iteration count is updated after each solve, if there is a current 227847450a7bSBarry Smith `TaoSolve()` in progress then those iterations are not included in the count 227967be906fSBarry Smith 22801cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 22818931d482SJason Sarich @*/ 2282d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter) 2283d71ae5a4SJacob Faibussowitsch { 22848931d482SJason Sarich PetscFunctionBegin; 22858931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22864f572ea9SToby Isaac PetscAssertPointer(iter, 2); 22878931d482SJason Sarich *iter = tao->ntotalits; 22883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22898931d482SJason Sarich } 22908931d482SJason Sarich 22918931d482SJason Sarich /*@ 22928931d482SJason Sarich TaoSetTotalIterationNumber - Sets the current total iteration number. 22938931d482SJason Sarich 2294c3339decSBarry Smith Logically Collective 22958931d482SJason Sarich 2296d8d19677SJose E. Roman Input Parameters: 229747450a7bSBarry Smith + tao - the `Tao` context 229847450a7bSBarry Smith - iter - the iteration number 22998931d482SJason Sarich 23008931d482SJason Sarich Level: developer 23018931d482SJason Sarich 23021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 23038931d482SJason Sarich @*/ 2304d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter) 2305d71ae5a4SJacob Faibussowitsch { 23068931d482SJason Sarich PetscFunctionBegin; 23078931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 230894511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 23099566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 23108931d482SJason Sarich tao->ntotalits = iter; 23119566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 23123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23138931d482SJason Sarich } 23148931d482SJason Sarich 2315a7e14dcfSSatish Balay /*@ 231647450a7bSBarry Smith TaoSetConvergedReason - Sets the termination flag on a `Tao` object 2317a7e14dcfSSatish Balay 2318c3339decSBarry Smith Logically Collective 2319a7e14dcfSSatish Balay 2320a7e14dcfSSatish Balay Input Parameters: 232147450a7bSBarry Smith + tao - the `Tao` context 232247450a7bSBarry Smith - reason - the `TaoConvergedReason` 2323a7e14dcfSSatish Balay 2324a7e14dcfSSatish Balay Level: intermediate 2325a7e14dcfSSatish Balay 23261cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason` 2327a7e14dcfSSatish Balay @*/ 2328d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason) 2329d71ae5a4SJacob Faibussowitsch { 2330a7e14dcfSSatish Balay PetscFunctionBegin; 233194511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 233294511df7SStefano Zampini PetscValidLogicalCollectiveEnum(tao, reason, 2); 2333a7e14dcfSSatish Balay tao->reason = reason; 23343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2335a7e14dcfSSatish Balay } 2336a7e14dcfSSatish Balay 2337a7e14dcfSSatish Balay /*@ 233847450a7bSBarry Smith TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped. 2339a7e14dcfSSatish Balay 2340a7e14dcfSSatish Balay Not Collective 2341a7e14dcfSSatish Balay 2342a7e14dcfSSatish Balay Input Parameter: 234347450a7bSBarry Smith . tao - the `Tao` solver context 2344a7e14dcfSSatish Balay 2345a7e14dcfSSatish Balay Output Parameter: 234647450a7bSBarry Smith . reason - value of `TaoConvergedReason` 2347a7e14dcfSSatish Balay 2348a7e14dcfSSatish Balay Level: intermediate 2349a7e14dcfSSatish Balay 23501cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()` 2351a7e14dcfSSatish Balay @*/ 2352d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason) 2353d71ae5a4SJacob Faibussowitsch { 2354a7e14dcfSSatish Balay PetscFunctionBegin; 2355441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 23564f572ea9SToby Isaac PetscAssertPointer(reason, 2); 2357a7e14dcfSSatish Balay *reason = tao->reason; 23583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2359a7e14dcfSSatish Balay } 2360a7e14dcfSSatish Balay 2361a7e14dcfSSatish Balay /*@ 2362a7e14dcfSSatish Balay TaoGetSolutionStatus - Get the current iterate, objective value, 236347450a7bSBarry Smith residual, infeasibility, and termination from a `Tao` object 2364a7e14dcfSSatish Balay 2365a7e14dcfSSatish Balay Not Collective 2366a7e14dcfSSatish Balay 2367f899ff85SJose E. Roman Input Parameter: 236847450a7bSBarry Smith . tao - the `Tao` context 2369a7e14dcfSSatish Balay 2370a7e14dcfSSatish Balay Output Parameters: 2371e056e8ceSJacob Faibussowitsch + its - the current iterate number (>=0) 2372a7e14dcfSSatish Balay . f - the current function value 237358417fe7SBarry Smith . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality. 2374a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2375a7e14dcfSSatish Balay . xdiff - the step length or trust region radius of the most recent iterate. 237665ba42b6SBarry Smith - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING` 2377a7e14dcfSSatish Balay 2378a7e14dcfSSatish Balay Level: intermediate 2379a7e14dcfSSatish Balay 238065ba42b6SBarry Smith Notes: 238165ba42b6SBarry Smith Tao returns the values set by the solvers in the routine `TaoMonitor()`. 2382a7e14dcfSSatish Balay 238367be906fSBarry Smith If any of the output arguments are set to `NULL`, no corresponding value will be returned. 2384a7e14dcfSSatish Balay 23851cc06b55SBarry Smith .seealso: [](ch_tao), `TaoMonitor()`, `TaoGetConvergedReason()` 2386a7e14dcfSSatish Balay @*/ 2387d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason) 2388d71ae5a4SJacob Faibussowitsch { 2389a7e14dcfSSatish Balay PetscFunctionBegin; 239094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2391a7e14dcfSSatish Balay if (its) *its = tao->niter; 2392a7e14dcfSSatish Balay if (f) *f = tao->fc; 2393a7e14dcfSSatish Balay if (gnorm) *gnorm = tao->residual; 2394a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->cnorm; 2395a7e14dcfSSatish Balay if (reason) *reason = tao->reason; 2396a7e14dcfSSatish Balay if (xdiff) *xdiff = tao->step; 23973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2398a7e14dcfSSatish Balay } 2399a7e14dcfSSatish Balay 2400cc4c1da9SBarry Smith /*@ 240147450a7bSBarry Smith TaoGetType - Gets the current `TaoType` being used in the `Tao` object 2402a7e14dcfSSatish Balay 2403a7e14dcfSSatish Balay Not Collective 2404a7e14dcfSSatish Balay 2405a7e14dcfSSatish Balay Input Parameter: 240647450a7bSBarry Smith . tao - the `Tao` solver context 2407a7e14dcfSSatish Balay 2408a7e14dcfSSatish Balay Output Parameter: 240947450a7bSBarry Smith . type - the `TaoType` 2410a7e14dcfSSatish Balay 2411a7e14dcfSSatish Balay Level: intermediate 2412a7e14dcfSSatish Balay 24131cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoType`, `TaoSetType()` 2414a7e14dcfSSatish Balay @*/ 2415d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetType(Tao tao, TaoType *type) 2416d71ae5a4SJacob Faibussowitsch { 2417a7e14dcfSSatish Balay PetscFunctionBegin; 2418441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 24194f572ea9SToby Isaac PetscAssertPointer(type, 2); 2420a7e14dcfSSatish Balay *type = ((PetscObject)tao)->type_name; 24213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2422a7e14dcfSSatish Balay } 2423a7e14dcfSSatish Balay 2424a7e14dcfSSatish Balay /*@C 2425a7e14dcfSSatish Balay TaoMonitor - Monitor the solver and the current solution. This 2426a7e14dcfSSatish Balay routine will record the iteration number and residual statistics, 2427670c031eSStefano Zampini and call any monitors specified by the user. 2428a7e14dcfSSatish Balay 2429a7e14dcfSSatish Balay Input Parameters: 243047450a7bSBarry Smith + tao - the `Tao` context 2431a7e14dcfSSatish Balay . its - the current iterate number (>=0) 2432a7e14dcfSSatish Balay . f - the current objective function value 24331cb3f120SPierre Jolivet . res - the gradient norm, square root of the duality gap, or other measure indicating distance from optimality. This measure will be recorded and 2434a7e14dcfSSatish Balay used for some termination tests. 2435a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2436a7e14dcfSSatish Balay - steplength - multiple of the step direction added to the previous iterate. 2437a7e14dcfSSatish Balay 2438a7e14dcfSSatish Balay Options Database Key: 2439a7e14dcfSSatish Balay . -tao_monitor - Use the default monitor, which prints statistics to standard output 2440a7e14dcfSSatish Balay 2441a7e14dcfSSatish Balay Level: developer 2442a7e14dcfSSatish Balay 244310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoMonitorSet()` 2444a7e14dcfSSatish Balay @*/ 2445d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength) 2446d71ae5a4SJacob Faibussowitsch { 244747a47007SBarry Smith PetscInt i; 244847a47007SBarry Smith 2449a7e14dcfSSatish Balay PetscFunctionBegin; 2450441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2451a7e14dcfSSatish Balay tao->fc = f; 2452a7e14dcfSSatish Balay tao->residual = res; 2453a7e14dcfSSatish Balay tao->cnorm = cnorm; 2454a7e14dcfSSatish Balay tao->step = steplength; 2455e52336cbSBarry Smith if (!its) { 245694511df7SStefano Zampini tao->cnorm0 = cnorm; 245794511df7SStefano Zampini tao->gnorm0 = res; 2458a7e14dcfSSatish Balay } 2459bfdcf5a2SStefano Zampini PetscCall(VecLockReadPush(tao->solution)); 246048a46eb9SPierre Jolivet for (i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i])); 2461bfdcf5a2SStefano Zampini PetscCall(VecLockReadPop(tao->solution)); 24623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2463a7e14dcfSSatish Balay } 2464a7e14dcfSSatish Balay 2465a7e14dcfSSatish Balay /*@ 2466ae93cb3cSJason Sarich TaoSetConvergenceHistory - Sets the array used to hold the convergence history. 2467a7e14dcfSSatish Balay 2468c3339decSBarry Smith Logically Collective 2469a7e14dcfSSatish Balay 2470a7e14dcfSSatish Balay Input Parameters: 247147450a7bSBarry Smith + tao - the `Tao` solver context 2472a7e14dcfSSatish Balay . obj - array to hold objective value history 2473a7e14dcfSSatish Balay . resid - array to hold residual history 2474a7e14dcfSSatish Balay . cnorm - array to hold constraint violation history 2475be1558d8SJason Sarich . lits - integer array holds the number of linear iterations for each Tao iteration 247667be906fSBarry Smith . na - size of `obj`, `resid`, and `cnorm` 247765ba42b6SBarry Smith - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero, 2478a7e14dcfSSatish Balay else it continues storing new values for new minimizations after the old ones 2479a7e14dcfSSatish Balay 248067be906fSBarry Smith Level: intermediate 248167be906fSBarry Smith 2482a7e14dcfSSatish Balay Notes: 248347450a7bSBarry Smith If set, `Tao` will fill the given arrays with the indicated 2484ae93cb3cSJason Sarich information at each iteration. If 'obj','resid','cnorm','lits' are 248567be906fSBarry Smith *all* `NULL` then space (using size `na`, or 1000 if na is `PETSC_DECIDE` or 248665ba42b6SBarry Smith `PETSC_DEFAULT`) is allocated for the history. 248767be906fSBarry Smith If not all are `NULL`, then only the non-`NULL` information categories 2488ae93cb3cSJason Sarich will be stored, the others will be ignored. 2489ae93cb3cSJason Sarich 2490ae93cb3cSJason Sarich Any convergence information after iteration number 'na' will not be stored. 2491a7e14dcfSSatish Balay 2492a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2493a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2494a7e14dcfSSatish Balay during the section of code that is being timed. 2495a7e14dcfSSatish Balay 24961cc06b55SBarry Smith .seealso: [](ch_tao), `TaoGetConvergenceHistory()` 2497a7e14dcfSSatish Balay @*/ 2498d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset) 2499d71ae5a4SJacob Faibussowitsch { 2500a7e14dcfSSatish Balay PetscFunctionBegin; 2501441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 25024f572ea9SToby Isaac if (obj) PetscAssertPointer(obj, 2); 25034f572ea9SToby Isaac if (resid) PetscAssertPointer(resid, 3); 25044f572ea9SToby Isaac if (cnorm) PetscAssertPointer(cnorm, 4); 25054f572ea9SToby Isaac if (lits) PetscAssertPointer(lits, 5); 2506ae93cb3cSJason Sarich 2507ae93cb3cSJason Sarich if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2508ae93cb3cSJason Sarich if (!obj && !resid && !cnorm && !lits) { 25099566063dSJacob Faibussowitsch PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits)); 2510ae93cb3cSJason Sarich tao->hist_malloc = PETSC_TRUE; 2511ae93cb3cSJason Sarich } 2512ae93cb3cSJason Sarich 2513a7e14dcfSSatish Balay tao->hist_obj = obj; 2514a7e14dcfSSatish Balay tao->hist_resid = resid; 2515a7e14dcfSSatish Balay tao->hist_cnorm = cnorm; 2516ae93cb3cSJason Sarich tao->hist_lits = lits; 2517a7e14dcfSSatish Balay tao->hist_max = na; 2518a7e14dcfSSatish Balay tao->hist_reset = reset; 2519ae93cb3cSJason Sarich tao->hist_len = 0; 25203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2521a7e14dcfSSatish Balay } 2522a7e14dcfSSatish Balay 2523a7e14dcfSSatish Balay /*@C 252465ba42b6SBarry Smith TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history. 2525a7e14dcfSSatish Balay 2526c3339decSBarry Smith Collective 2527a7e14dcfSSatish Balay 2528a7e14dcfSSatish Balay Input Parameter: 252947450a7bSBarry Smith . tao - the `Tao` context 2530a7e14dcfSSatish Balay 2531a7e14dcfSSatish Balay Output Parameters: 2532a7e14dcfSSatish Balay + obj - array used to hold objective value history 2533a7e14dcfSSatish Balay . resid - array used to hold residual history 2534a7e14dcfSSatish Balay . cnorm - array used to hold constraint violation history 2535ae93cb3cSJason Sarich . lits - integer array used to hold linear solver iteration count 253667be906fSBarry Smith - nhist - size of `obj`, `resid`, `cnorm`, and `lits` 253767be906fSBarry Smith 253867be906fSBarry Smith Level: advanced 2539a7e14dcfSSatish Balay 2540a7e14dcfSSatish Balay Notes: 254165ba42b6SBarry Smith This routine must be preceded by calls to `TaoSetConvergenceHistory()` 254265ba42b6SBarry Smith and `TaoSolve()`, otherwise it returns useless information. 2543ae93cb3cSJason Sarich 2544a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2545a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2546a7e14dcfSSatish Balay during the section of code that is being timed. 2547a7e14dcfSSatish Balay 2548e056e8ceSJacob Faibussowitsch Fortran Notes: 254967be906fSBarry Smith The calling sequence is 255067be906fSBarry Smith .vb 255167be906fSBarry Smith call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr) 255267be906fSBarry Smith .ve 2553a3b724e8SBarry Smith In other words this gets the current number of entries in the history. Access the history through the array you passed to `TaoSetConvergenceHistory()` 2554a7e14dcfSSatish Balay 25551cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()` 2556a7e14dcfSSatish Balay @*/ 2557d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist) 2558d71ae5a4SJacob Faibussowitsch { 2559a7e14dcfSSatish Balay PetscFunctionBegin; 2560441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2561a7e14dcfSSatish Balay if (obj) *obj = tao->hist_obj; 2562a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->hist_cnorm; 2563a7e14dcfSSatish Balay if (resid) *resid = tao->hist_resid; 25645b494b05SBarry Smith if (lits) *lits = tao->hist_lits; 2565a7e14dcfSSatish Balay if (nhist) *nhist = tao->hist_len; 25663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2567a7e14dcfSSatish Balay } 2568a7e14dcfSSatish Balay 2569a7e14dcfSSatish Balay /*@ 257047450a7bSBarry Smith TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver. 2571a7e14dcfSSatish Balay 2572c3339decSBarry Smith Logically Collective 2573a7e14dcfSSatish Balay 2574a7e14dcfSSatish Balay Input Parameters: 257547450a7bSBarry Smith + tao - the `Tao` context 2576a7e14dcfSSatish Balay - usrP - optional user context 2577a7e14dcfSSatish Balay 2578a7e14dcfSSatish Balay Level: intermediate 2579a7e14dcfSSatish Balay 258042747ad1SJacob Faibussowitsch .seealso: [](ch_tao), `Tao`, `TaoGetApplicationContext()` 2581a7e14dcfSSatish Balay @*/ 2582d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetApplicationContext(Tao tao, void *usrP) 2583d71ae5a4SJacob Faibussowitsch { 2584a7e14dcfSSatish Balay PetscFunctionBegin; 2585441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2586a7e14dcfSSatish Balay tao->user = usrP; 25873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2588a7e14dcfSSatish Balay } 2589a7e14dcfSSatish Balay 2590a7e14dcfSSatish Balay /*@ 259147450a7bSBarry Smith TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver 2592a7e14dcfSSatish Balay 2593a7e14dcfSSatish Balay Not Collective 2594a7e14dcfSSatish Balay 2595a7e14dcfSSatish Balay Input Parameter: 259647450a7bSBarry Smith . tao - the `Tao` context 2597a7e14dcfSSatish Balay 2598a7e14dcfSSatish Balay Output Parameter: 2599a7e14dcfSSatish Balay . usrP - user context 2600a7e14dcfSSatish Balay 2601a7e14dcfSSatish Balay Level: intermediate 2602a7e14dcfSSatish Balay 26031cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetApplicationContext()` 2604a7e14dcfSSatish Balay @*/ 2605d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetApplicationContext(Tao tao, void *usrP) 2606d71ae5a4SJacob Faibussowitsch { 2607a7e14dcfSSatish Balay PetscFunctionBegin; 2608441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 26094f572ea9SToby Isaac PetscAssertPointer(usrP, 2); 2610a7e14dcfSSatish Balay *(void **)usrP = tao->user; 26113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2612a7e14dcfSSatish Balay } 2613a9603a14SPatrick Farrell 2614a9603a14SPatrick Farrell /*@ 261565ba42b6SBarry Smith TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient. 2616a9603a14SPatrick Farrell 2617c3339decSBarry Smith Collective 2618a9603a14SPatrick Farrell 2619a9603a14SPatrick Farrell Input Parameters: 262047450a7bSBarry Smith + tao - the `Tao` context 262165ba42b6SBarry Smith - M - matrix that defines the norm 2622a9603a14SPatrick Farrell 2623a9603a14SPatrick Farrell Level: beginner 2624a9603a14SPatrick Farrell 26251cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()` 2626a9603a14SPatrick Farrell @*/ 2627d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M) 2628d71ae5a4SJacob Faibussowitsch { 2629a9603a14SPatrick Farrell PetscFunctionBegin; 2630a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 263194511df7SStefano Zampini PetscValidHeaderSpecific(M, MAT_CLASSID, 2); 26329566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)M)); 26339566063dSJacob Faibussowitsch PetscCall(MatDestroy(&tao->gradient_norm)); 26349566063dSJacob Faibussowitsch PetscCall(VecDestroy(&tao->gradient_norm_tmp)); 2635a9603a14SPatrick Farrell tao->gradient_norm = M; 26369566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp)); 26373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2638a9603a14SPatrick Farrell } 2639a9603a14SPatrick Farrell 2640a9603a14SPatrick Farrell /*@ 264165ba42b6SBarry Smith TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient. 2642a9603a14SPatrick Farrell 2643a9603a14SPatrick Farrell Not Collective 2644a9603a14SPatrick Farrell 2645a9603a14SPatrick Farrell Input Parameter: 264647450a7bSBarry Smith . tao - the `Tao` context 2647a9603a14SPatrick Farrell 2648a9603a14SPatrick Farrell Output Parameter: 2649a9603a14SPatrick Farrell . M - gradient norm 2650a9603a14SPatrick Farrell 2651a9603a14SPatrick Farrell Level: beginner 2652a9603a14SPatrick Farrell 26531cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()` 2654a9603a14SPatrick Farrell @*/ 2655d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M) 2656d71ae5a4SJacob Faibussowitsch { 2657a9603a14SPatrick Farrell PetscFunctionBegin; 2658a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 26594f572ea9SToby Isaac PetscAssertPointer(M, 2); 2660a9603a14SPatrick Farrell *M = tao->gradient_norm; 26613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2662a9603a14SPatrick Farrell } 2663a9603a14SPatrick Farrell 2664cc4c1da9SBarry Smith /*@ 266547450a7bSBarry Smith TaoGradientNorm - Compute the norm using the `NormType`, the user has selected 2666a9603a14SPatrick Farrell 2667c3339decSBarry Smith Collective 2668a9603a14SPatrick Farrell 2669d8d19677SJose E. Roman Input Parameters: 267047450a7bSBarry Smith + tao - the `Tao` context 2671a9603a14SPatrick Farrell . gradient - the gradient to be computed 26723b242c63SJacob Faibussowitsch - type - the norm type 2673a9603a14SPatrick Farrell 2674a9603a14SPatrick Farrell Output Parameter: 2675a9603a14SPatrick Farrell . gnorm - the gradient norm 2676a9603a14SPatrick Farrell 267747450a7bSBarry Smith Level: advanced 2678a9603a14SPatrick Farrell 26791cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()` 2680a9603a14SPatrick Farrell @*/ 2681d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm) 2682d71ae5a4SJacob Faibussowitsch { 2683a9603a14SPatrick Farrell PetscFunctionBegin; 26848854b543SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 26858854b543SStefano Zampini PetscValidHeaderSpecific(gradient, VEC_CLASSID, 2); 26868854b543SStefano Zampini PetscValidLogicalCollectiveEnum(tao, type, 3); 26874f572ea9SToby Isaac PetscAssertPointer(gnorm, 4); 2688a9603a14SPatrick Farrell if (tao->gradient_norm) { 2689680e2bc7SPatrick Farrell PetscScalar gnorms; 2690680e2bc7SPatrick Farrell 26913c859ba3SBarry Smith PetscCheck(type == NORM_2, PetscObjectComm((PetscObject)gradient), PETSC_ERR_ARG_WRONG, "Norm type must be NORM_2 if an inner product for the gradient norm is set."); 26929566063dSJacob Faibussowitsch PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp)); 26939566063dSJacob Faibussowitsch PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms)); 26947bb79937SPatrick Farrell *gnorm = PetscRealPart(PetscSqrtScalar(gnorms)); 2695a9603a14SPatrick Farrell } else { 26969566063dSJacob Faibussowitsch PetscCall(VecNorm(gradient, type, gnorm)); 2697a9603a14SPatrick Farrell } 26983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2699a9603a14SPatrick Farrell } 2700a9603a14SPatrick Farrell 2701e882e171SHong Zhang /*@C 270247450a7bSBarry Smith TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorDrawSolution()` 2703a9603a14SPatrick Farrell 2704c3339decSBarry Smith Collective 2705e882e171SHong Zhang 27062fe279fdSBarry Smith Input Parameters: 27072fe279fdSBarry Smith + comm - the communicator to share the context 27082fe279fdSBarry Smith . host - the name of the X Windows host that will display the monitor 27092fe279fdSBarry Smith . label - the label to put at the top of the display window 27102fe279fdSBarry Smith . x - the horizontal coordinate of the lower left corner of the window to open 27112fe279fdSBarry Smith . y - the vertical coordinate of the lower left corner of the window to open 27122fe279fdSBarry Smith . m - the width of the window 27132fe279fdSBarry Smith . n - the height of the window 27142fe279fdSBarry Smith - howoften - how many `Tao` iterations between displaying the monitor information 27152fe279fdSBarry Smith 2716d5b43468SJose E. Roman Output Parameter: 2717e882e171SHong Zhang . ctx - the monitor context 2718e882e171SHong Zhang 27193c7db156SBarry Smith Options Database Key: 2720e882e171SHong Zhang . -tao_draw_solution_initial - show initial guess as well as current solution 2721e882e171SHong Zhang 2722e882e171SHong Zhang Level: intermediate 2723e882e171SHong Zhang 27241cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()` 2725e882e171SHong Zhang @*/ 2726d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx) 2727d71ae5a4SJacob Faibussowitsch { 2728e882e171SHong Zhang PetscFunctionBegin; 27299566063dSJacob Faibussowitsch PetscCall(PetscNew(ctx)); 27309566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer)); 27319566063dSJacob Faibussowitsch PetscCall(PetscViewerSetFromOptions((*ctx)->viewer)); 2732e882e171SHong Zhang (*ctx)->howoften = howoften; 27333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2734e882e171SHong Zhang } 2735e882e171SHong Zhang 2736e882e171SHong Zhang /*@C 273765ba42b6SBarry Smith TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorDrawSolution()` 2738e882e171SHong Zhang 2739c3339decSBarry Smith Collective 2740e882e171SHong Zhang 274147450a7bSBarry Smith Input Parameter: 2742e056e8ceSJacob Faibussowitsch . ictx - the monitor context 2743e882e171SHong Zhang 2744e882e171SHong Zhang Level: intermediate 2745e882e171SHong Zhang 27461cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawSolution()` 2747e882e171SHong Zhang @*/ 2748d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx) 2749d71ae5a4SJacob Faibussowitsch { 2750e882e171SHong Zhang PetscFunctionBegin; 27519566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&(*ictx)->viewer)); 27529566063dSJacob Faibussowitsch PetscCall(PetscFree(*ictx)); 27533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2754e882e171SHong Zhang } 2755