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 24*2a8381b2SBarry Smith static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, PetscCtx 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 41*2a8381b2SBarry Smith static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, PetscCtx ctx) 42d71ae5a4SJacob Faibussowitsch { 43f1e99121SPierre Jolivet Tao tao = (Tao)ctx; 440f0abf79SStefano Zampini SNES snes_ewdummy = tao->snes_ewdummy; 450f0abf79SStefano Zampini 460f0abf79SStefano Zampini PetscFunctionBegin; 473ba16761SJacob Faibussowitsch if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS); 480f0abf79SStefano Zampini PetscCall(KSPPostSolve_SNESEW(ksp, b, x, snes_ewdummy)); 493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 500f0abf79SStefano Zampini } 510f0abf79SStefano Zampini 52d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSetUpEW_Private(Tao tao) 53d71ae5a4SJacob Faibussowitsch { 540f0abf79SStefano Zampini SNESKSPEW *kctx; 550f0abf79SStefano Zampini const char *ewprefix; 560f0abf79SStefano Zampini 570f0abf79SStefano Zampini PetscFunctionBegin; 583ba16761SJacob Faibussowitsch if (!tao->ksp) PetscFunctionReturn(PETSC_SUCCESS); 590f0abf79SStefano Zampini if (tao->ksp_ewconv) { 600f0abf79SStefano Zampini if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao), &tao->snes_ewdummy)); 610f0abf79SStefano Zampini tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE; 62f1e99121SPierre Jolivet PetscCall(KSPSetPreSolve(tao->ksp, KSPPreSolve_TAOEW_Private, tao)); 63f1e99121SPierre Jolivet PetscCall(KSPSetPostSolve(tao->ksp, KSPPostSolve_TAOEW_Private, tao)); 640f0abf79SStefano Zampini 650f0abf79SStefano Zampini PetscCall(KSPGetOptionsPrefix(tao->ksp, &ewprefix)); 660f0abf79SStefano Zampini kctx = (SNESKSPEW *)tao->snes_ewdummy->kspconvctx; 67a4598233SStefano Zampini PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_FALSE, PetscObjectComm((PetscObject)tao), ewprefix)); 680f0abf79SStefano Zampini } else PetscCall(SNESDestroy(&tao->snes_ewdummy)); 693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 700f0abf79SStefano Zampini } 710f0abf79SStefano Zampini 72a7e14dcfSSatish Balay /*@ 73606f75f6SBarry Smith TaoParametersInitialize - Sets all the parameters in `tao` to their default value (when `TaoCreate()` was called) if they 74606f75f6SBarry Smith currently contain default values. Default values are the parameter values when the object's type is set. 75606f75f6SBarry Smith 76606f75f6SBarry Smith Collective 77606f75f6SBarry Smith 78606f75f6SBarry Smith Input Parameter: 79606f75f6SBarry Smith . tao - the `Tao` object 80606f75f6SBarry Smith 81606f75f6SBarry Smith Level: developer 82606f75f6SBarry Smith 83606f75f6SBarry Smith Developer Note: 84606f75f6SBarry Smith This is called by all the `TaoCreate_XXX()` routines. 85606f75f6SBarry Smith 86606f75f6SBarry Smith .seealso: [](ch_snes), `Tao`, `TaoSolve()`, `TaoDestroy()`, 87606f75f6SBarry Smith `PetscObjectParameterSetDefault()` 88606f75f6SBarry Smith @*/ 89606f75f6SBarry Smith PetscErrorCode TaoParametersInitialize(Tao tao) 90606f75f6SBarry Smith { 91606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, max_it, 10000); 92606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, max_funcs, PETSC_UNLIMITED); 93606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, gatol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8); 94606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, grtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8); 95606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, crtol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8); 96606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, catol, PetscDefined(USE_REAL_SINGLE) ? 1e-5 : 1e-8); 97606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, gttol, 0.0); 98606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, steptol, 0.0); 99606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, fmin, PETSC_NINFINITY); 100606f75f6SBarry Smith PetscObjectParameterSetDefault(tao, trust0, PETSC_INFINITY); 101606f75f6SBarry Smith return PETSC_SUCCESS; 102606f75f6SBarry Smith } 103606f75f6SBarry Smith 104606f75f6SBarry Smith /*@ 10565ba42b6SBarry Smith TaoCreate - Creates a Tao solver 106a7e14dcfSSatish Balay 107d083f849SBarry Smith Collective 108a7e14dcfSSatish Balay 109a7e14dcfSSatish Balay Input Parameter: 110a7e14dcfSSatish Balay . comm - MPI communicator 111a7e14dcfSSatish Balay 112a7e14dcfSSatish Balay Output Parameter: 11347450a7bSBarry Smith . newtao - the new `Tao` context 114a7e14dcfSSatish Balay 11547450a7bSBarry Smith Options Database Key: 11665ba42b6SBarry Smith . -tao_type - select which method Tao should use 117a7e14dcfSSatish Balay 118a7e14dcfSSatish Balay Level: beginner 119a7e14dcfSSatish Balay 1200241b274SPierre Jolivet .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TaoSetFromOptions()`, `TaoSetType()` 121a7e14dcfSSatish Balay @*/ 122d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao) 123d71ae5a4SJacob Faibussowitsch { 124441846f8SBarry Smith Tao tao; 125a7e14dcfSSatish Balay 126a7e14dcfSSatish Balay PetscFunctionBegin; 1274f572ea9SToby Isaac PetscAssertPointer(newtao, 2); 1289566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 1299566063dSJacob Faibussowitsch PetscCall(TaoLineSearchInitializePackage()); 130a7e14dcfSSatish Balay 131606f75f6SBarry Smith PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView)); 13294511df7SStefano Zampini tao->ops->convergencetest = TaoDefaultConvergenceTest; 133606f75f6SBarry Smith 134a7e14dcfSSatish Balay tao->hist_reset = PETSC_TRUE; 1359566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 136a7e14dcfSSatish Balay *newtao = tao; 1373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 138a7e14dcfSSatish Balay } 139a7e14dcfSSatish Balay 140a7e14dcfSSatish Balay /*@ 141a7e14dcfSSatish Balay TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u 142a7e14dcfSSatish Balay 143c3339decSBarry Smith Collective 144a7e14dcfSSatish Balay 14547450a7bSBarry Smith Input Parameter: 14647450a7bSBarry Smith . tao - the `Tao` context 147a7e14dcfSSatish Balay 14867be906fSBarry Smith Level: beginner 14967be906fSBarry Smith 150a7e14dcfSSatish Balay Notes: 15147450a7bSBarry Smith The user must set up the `Tao` object with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`. 152a7e14dcfSSatish Balay 15367be906fSBarry Smith You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or 154a35d58b8SBarry Smith why it failed. 155a35d58b8SBarry Smith 1561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()` 157a7e14dcfSSatish Balay @*/ 158d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolve(Tao tao) 159d71ae5a4SJacob Faibussowitsch { 160e2379f4fSBarry Smith static PetscBool set = PETSC_FALSE; 16147a47007SBarry Smith 162a7e14dcfSSatish Balay PetscFunctionBegin; 163441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 164d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n" 165e2379f4fSBarry Smith "title = {Toolkit for Advanced Optimization (TAO) Users Manual},\n" 166e2379f4fSBarry Smith "author = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n" 167e2379f4fSBarry Smith "Institution = {Argonne National Laboratory},\n" 168e2379f4fSBarry Smith "Year = 2014,\n" 169e2379f4fSBarry Smith "Number = {ANL/MCS-TM-322 - Revision 3.5},\n" 1709371c9d4SSatish Balay "url = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n", 1719371c9d4SSatish Balay &set)); 172494bef23SAlp Dener tao->header_printed = PETSC_FALSE; 1739566063dSJacob Faibussowitsch PetscCall(TaoSetUp(tao)); 1749566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 1751baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch)); 176a7e14dcfSSatish Balay 1779566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0)); 178dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, solve); 1799566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0)); 180a7e14dcfSSatish Balay 1819566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution")); 1827cf37e64SBarry Smith 1838931d482SJason Sarich tao->ntotalits += tao->niter; 184a7e14dcfSSatish Balay 185a7e14dcfSSatish Balay if (tao->printreason) { 186f642d338SBarry Smith PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm); 187084fd5feSPierre Jolivet 188f642d338SBarry Smith PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel)); 189a7e14dcfSSatish Balay if (tao->reason > 0) { 190084fd5feSPierre Jolivet if (((PetscObject)tao)->prefix) { 191084fd5feSPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter)); 192a7e14dcfSSatish Balay } else { 193084fd5feSPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, " TAO solve converged due to %s iterations %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter)); 194084fd5feSPierre Jolivet } 195084fd5feSPierre Jolivet } else { 196084fd5feSPierre Jolivet if (((PetscObject)tao)->prefix) { 197084fd5feSPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix, TaoConvergedReasons[tao->reason], tao->niter)); 198084fd5feSPierre Jolivet } else { 199084fd5feSPierre Jolivet PetscCall(PetscViewerASCIIPrintf(viewer, " TAO solve did not converge due to %s iteration %" PetscInt_FMT "\n", TaoConvergedReasons[tao->reason], tao->niter)); 200084fd5feSPierre Jolivet } 201a7e14dcfSSatish Balay } 202f642d338SBarry Smith PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel)); 203a7e14dcfSSatish Balay } 204f642d338SBarry Smith PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view")); 2053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 206a7e14dcfSSatish Balay } 207a7e14dcfSSatish Balay 208a7e14dcfSSatish Balay /*@ 209a7e14dcfSSatish Balay TaoSetUp - Sets up the internal data structures for the later use 210a7e14dcfSSatish Balay of a Tao solver 211a7e14dcfSSatish Balay 212c3339decSBarry Smith Collective 213a7e14dcfSSatish Balay 21447450a7bSBarry Smith Input Parameter: 21547450a7bSBarry Smith . tao - the `Tao` context 216a7e14dcfSSatish Balay 21767be906fSBarry Smith Level: advanced 21867be906fSBarry Smith 21947450a7bSBarry Smith Note: 22065ba42b6SBarry Smith The user will not need to explicitly call `TaoSetUp()`, as it will 22165ba42b6SBarry Smith automatically be called in `TaoSolve()`. However, if the user 22265ba42b6SBarry Smith desires to call it explicitly, it should come after `TaoCreate()` 22365ba42b6SBarry Smith and any TaoSetSomething() routines, but before `TaoSolve()`. 224a7e14dcfSSatish Balay 2251cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 226a7e14dcfSSatish Balay @*/ 227d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp(Tao tao) 228d71ae5a4SJacob Faibussowitsch { 229a7e14dcfSSatish Balay PetscFunctionBegin; 230441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2313ba16761SJacob Faibussowitsch if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2320f0abf79SStefano Zampini PetscCall(TaoSetUpEW_Private(tao)); 2333c859ba3SBarry Smith PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution"); 234dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setup); 235a7e14dcfSSatish Balay tao->setupcalled = PETSC_TRUE; 2363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 237a7e14dcfSSatish Balay } 238a7e14dcfSSatish Balay 2390764c050SBarry Smith /*@ 24047450a7bSBarry Smith TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()` 241a7e14dcfSSatish Balay 242c3339decSBarry Smith Collective 243a7e14dcfSSatish Balay 244a7e14dcfSSatish Balay Input Parameter: 24547450a7bSBarry Smith . tao - the `Tao` context 246a7e14dcfSSatish Balay 247a7e14dcfSSatish Balay Level: beginner 248a7e14dcfSSatish Balay 2491cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 250a7e14dcfSSatish Balay @*/ 251d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy(Tao *tao) 252d71ae5a4SJacob Faibussowitsch { 253a7e14dcfSSatish Balay PetscFunctionBegin; 2543ba16761SJacob Faibussowitsch if (!*tao) PetscFunctionReturn(PETSC_SUCCESS); 255441846f8SBarry Smith PetscValidHeaderSpecific(*tao, TAO_CLASSID, 1); 2569371c9d4SSatish Balay if (--((PetscObject)*tao)->refct > 0) { 2579371c9d4SSatish Balay *tao = NULL; 2583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 259a7e14dcfSSatish Balay } 2609371c9d4SSatish Balay 261213acdd3SPierre Jolivet PetscTryTypeMethod(*tao, destroy); 2629566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&(*tao)->ksp)); 2630f0abf79SStefano Zampini PetscCall(SNESDestroy(&(*tao)->snes_ewdummy)); 2649566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch)); 265a7e14dcfSSatish Balay 266a7e14dcfSSatish Balay if ((*tao)->ops->convergencedestroy) { 2679566063dSJacob Faibussowitsch PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP)); 26848a46eb9SPierre Jolivet if ((*tao)->jacobian_state_inv) PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 269a7e14dcfSSatish Balay } 2709566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->solution)); 2719566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient)); 2729566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->ls_res)); 273a7e14dcfSSatish Balay 274a9603a14SPatrick Farrell if ((*tao)->gradient_norm) { 2759566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm)); 2769566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp)); 277a9603a14SPatrick Farrell } 278a9603a14SPatrick Farrell 2799566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XL)); 2809566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XU)); 2819566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IL)); 2829566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IU)); 2839566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DE)); 2849566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DI)); 2859566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints)); 2869566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_equality)); 2879566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_inequality)); 2889566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->stepdirection)); 2899566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian_pre)); 2909566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian)); 2919566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac)); 2929566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac_pre)); 2939566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_pre)); 2949566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian)); 2959566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_pre)); 2969566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state)); 2979566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 2989566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_design)); 2999566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality)); 3009566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre)); 3019566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality)); 3029566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre)); 3039566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->state_is)); 3049566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->design_is)); 3059566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->res_weights_v)); 306b2dc45ddSPierre Jolivet PetscCall(TaoMonitorCancel(*tao)); 30748a46eb9SPierre Jolivet if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits)); 308737f463aSAlp Dener if ((*tao)->res_weights_n) { 3099566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_rows)); 3109566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_cols)); 3119566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_w)); 312125ddc8aSJason Sarich } 3139566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(tao)); 3143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 315a7e14dcfSSatish Balay } 316a7e14dcfSSatish Balay 317a7e14dcfSSatish Balay /*@ 3181d27aa22SBarry Smith TaoKSPSetUseEW - Sets `SNES` to use Eisenstat-Walker method {cite}`ew96`for computing relative tolerance for linear solvers. 3190f0abf79SStefano Zampini 320c3339decSBarry Smith Logically Collective 3210f0abf79SStefano Zampini 3220f0abf79SStefano Zampini Input Parameters: 3230f0abf79SStefano Zampini + tao - Tao context 32465ba42b6SBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE` 3250f0abf79SStefano Zampini 32667be906fSBarry Smith Level: advanced 32767be906fSBarry Smith 32847450a7bSBarry Smith Note: 32965ba42b6SBarry Smith See `SNESKSPSetUseEW()` for customization details. 3300f0abf79SStefano Zampini 3311cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `SNESKSPSetUseEW()` 3320f0abf79SStefano Zampini @*/ 333d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag) 334d71ae5a4SJacob Faibussowitsch { 3350f0abf79SStefano Zampini PetscFunctionBegin; 3360f0abf79SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3370f0abf79SStefano Zampini PetscValidLogicalCollectiveBool(tao, flag, 2); 3380f0abf79SStefano Zampini tao->ksp_ewconv = flag; 3393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3400f0abf79SStefano Zampini } 3410f0abf79SStefano Zampini 3420f0abf79SStefano Zampini /*@ 34347450a7bSBarry Smith TaoSetFromOptions - Sets various Tao parameters from the options database 344a7e14dcfSSatish Balay 345c3339decSBarry Smith Collective 346a7e14dcfSSatish Balay 34701d2d390SJose E. Roman Input Parameter: 34847450a7bSBarry Smith . tao - the `Tao` solver context 349a7e14dcfSSatish Balay 35047450a7bSBarry Smith Options Database Keys: 35165ba42b6SBarry Smith + -tao_type <type> - The algorithm that Tao uses (lmvm, nls, etc.) 352a7e14dcfSSatish Balay . -tao_gatol <gatol> - absolute error tolerance for ||gradient|| 353a7e14dcfSSatish Balay . -tao_grtol <grtol> - relative error tolerance for ||gradient|| 354a7e14dcfSSatish Balay . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient 355a7e14dcfSSatish Balay . -tao_max_it <max> - sets maximum number of iterations 356a7e14dcfSSatish Balay . -tao_max_funcs <max> - sets maximum number of function evaluations 357a7e14dcfSSatish Balay . -tao_fmin <fmin> - stop if function value reaches fmin 358a7e14dcfSSatish Balay . -tao_steptol <tol> - stop if trust region radius less than <tol> 359a7e14dcfSSatish Balay . -tao_trust0 <t> - initial trust region radius 36010978b7dSBarry Smith . -tao_view_solution - view the solution at the end of the optimization process 36147450a7bSBarry Smith . -tao_monitor - prints function value and residual norm at each iteration 36210978b7dSBarry Smith . -tao_monitor_short - same as `-tao_monitor`, but truncates very small values 36310978b7dSBarry Smith . -tao_monitor_constraint_norm - prints objective value, gradient, and constraint norm at each iteration 36410978b7dSBarry Smith . -tao_monitor_globalization - prints information about the globalization at each iteration 36510978b7dSBarry Smith . -tao_monitor_solution - prints solution vector at each iteration 36610978b7dSBarry Smith . -tao_monitor_ls_residual - prints least-squares residual vector at each iteration 36710978b7dSBarry Smith . -tao_monitor_step - prints step vector at each iteration 36810978b7dSBarry Smith . -tao_monitor_gradient - prints gradient vector at each iteration 36910978b7dSBarry Smith . -tao_monitor_solution_draw - graphically view solution vector at each iteration 37010978b7dSBarry Smith . -tao_monitor_step_draw - graphically view step vector at each iteration 37110978b7dSBarry Smith . -tao_monitor_gradient_draw - graphically view gradient at each iteration 37210978b7dSBarry Smith . -tao_monitor_cancel - cancels all monitors (except those set with command line) 373a7e14dcfSSatish Balay . -tao_fd_gradient - use gradient computed with finite differences 374f4c1ad5cSStefano Zampini . -tao_fd_hessian - use hessian computed with finite differences 37510978b7dSBarry Smith . -tao_mf_hessian - use matrix-free Hessian computed with finite differences 376441846f8SBarry Smith . -tao_view - prints information about the Tao after solving 37765ba42b6SBarry Smith - -tao_converged_reason - prints the reason Tao stopped iterating 378a7e14dcfSSatish Balay 37967be906fSBarry Smith Level: beginner 38067be906fSBarry Smith 38167be906fSBarry Smith Note: 38247450a7bSBarry Smith To see all options, run your program with the `-help` option or consult the 38365ba42b6SBarry Smith user's manual. Should be called after `TaoCreate()` but before `TaoSolve()` 384a7e14dcfSSatish Balay 3851cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 386a7e14dcfSSatish Balay @*/ 387d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions(Tao tao) 388d71ae5a4SJacob Faibussowitsch { 389b625d6c7SJed Brown TaoType default_type = TAOLMVM; 390a7e14dcfSSatish Balay char type[256], monfilename[PETSC_MAX_PATH_LEN]; 391a7e14dcfSSatish Balay PetscViewer monviewer; 392e876fe75SStephan Köhler PetscBool flg, found; 393a7e14dcfSSatish Balay MPI_Comm comm; 394606f75f6SBarry Smith PetscReal catol, crtol, gatol, grtol, gttol; 395a7e14dcfSSatish Balay 396a7e14dcfSSatish Balay PetscFunctionBegin; 397441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3989566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)tao, &comm)); 399125ddc8aSJason Sarich 40060b70c5cSStefano Zampini if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name; 40160b70c5cSStefano Zampini 402d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)tao); 403a7e14dcfSSatish Balay /* Check for type from options */ 4049566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg)); 405a7e14dcfSSatish Balay if (flg) { 4069566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, type)); 407a7e14dcfSSatish Balay } else if (!((PetscObject)tao)->type_name) { 4089566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, default_type)); 409a7e14dcfSSatish Balay } 410a7e14dcfSSatish Balay 41160b70c5cSStefano Zampini /* Tao solvers do not set the prefix, set it here if not yet done 41260b70c5cSStefano Zampini We do it after SetType since solver may have been changed */ 41360b70c5cSStefano Zampini if (tao->linesearch) { 41460b70c5cSStefano Zampini const char *prefix; 41560b70c5cSStefano Zampini PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix)); 416f4f49eeaSPierre Jolivet if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)tao)->prefix)); 41760b70c5cSStefano Zampini } 41860b70c5cSStefano Zampini 419606f75f6SBarry Smith catol = tao->catol; 420606f75f6SBarry Smith crtol = tao->crtol; 421606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &catol, NULL)); 422606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &crtol, NULL)); 423606f75f6SBarry Smith PetscCall(TaoSetConstraintTolerances(tao, catol, crtol)); 424606f75f6SBarry Smith 425606f75f6SBarry Smith gatol = tao->gatol; 426606f75f6SBarry Smith grtol = tao->grtol; 427606f75f6SBarry Smith gttol = tao->gttol; 428606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &gatol, NULL)); 429606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &grtol, NULL)); 430606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_gttol", "Stop if the norm of the gradient is less than the norm of the initial gradient times tol", "TaoSetTolerances", tao->gttol, >tol, NULL)); 431606f75f6SBarry Smith PetscCall(TaoSetTolerances(tao, gatol, grtol, gttol)); 432606f75f6SBarry Smith 4339566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg)); 434606f75f6SBarry Smith if (flg) PetscCall(TaoSetMaximumIterations(tao, tao->max_it)); 435606f75f6SBarry Smith 4369566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg)); 437606f75f6SBarry Smith if (flg) PetscCall(TaoSetMaximumFunctionEvaluations(tao, tao->max_funcs)); 438606f75f6SBarry Smith 439606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, NULL)); 440606f75f6SBarry Smith PetscCall(PetscOptionsBoundedReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, NULL, 0)); 441606f75f6SBarry Smith PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetInitialTrustRegionRadius", tao->trust0, &tao->trust0, &flg)); 442606f75f6SBarry Smith if (flg) PetscCall(TaoSetInitialTrustRegionRadius(tao, tao->trust0)); 44310978b7dSBarry Smith 44410978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_solution_monitor", "-tao_monitor_solution", "3.21", NULL)); 44510978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_gradient_monitor", "-tao_monitor_gradient", "3.21", NULL)); 44610978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_stepdirection_monitor", "-tao_monitor_step", "3.21", NULL)); 44710978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_residual_monitor", "-tao_monitor_residual", "3.21", NULL)); 44810978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_smonitor", "-tao_monitor_short", "3.21", NULL)); 44910978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_cmonitor", "-tao_monitor_constraint_norm", "3.21", NULL)); 45010978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_gmonitor", "-tao_monitor_globalization", "3.21", NULL)); 45110978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_solution", "-tao_monitor_solution_draw", "3.21", NULL)); 45210978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_gradient", "-tao_monitor_gradient_draw", "3.21", NULL)); 45310978b7dSBarry Smith PetscCall(PetscOptionsDeprecated("-tao_draw_step", "-tao_monitor_step_draw", "3.21", NULL)); 45410978b7dSBarry Smith 45510978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_solution", "View solution vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 456a7e14dcfSSatish Balay if (flg) { 4579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 45849abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorSolution, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 459a7e14dcfSSatish Balay } 460a7e14dcfSSatish Balay 46165ba42b6SBarry Smith PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL)); 46210978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_gradient", "View gradient vector for each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 463a7e14dcfSSatish Balay if (flg) { 4649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 46549abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGradient, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 466a7e14dcfSSatish Balay } 467a7e14dcfSSatish Balay 46810978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_step", "View step vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 469a7e14dcfSSatish Balay if (flg) { 4709566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 47149abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorStep, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 472a7e14dcfSSatish Balay } 473a7e14dcfSSatish Balay 47410978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_residual", "View least-squares residual vector after each iteration", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 475a7e14dcfSSatish Balay if (flg) { 4769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 47749abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorResidual, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 478a7e14dcfSSatish Balay } 479a7e14dcfSSatish Balay 48010978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor", "Use the default convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 481a7e14dcfSSatish Balay if (flg) { 4829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 48349abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorDefault, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 484a7e14dcfSSatish Balay } 485a7e14dcfSSatish Balay 48610978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_globalization", "Use the convergence monitor with extra globalization info", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 4878d5ead36SAlp Dener if (flg) { 4889566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 48949abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGlobalization, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 4908d5ead36SAlp Dener } 4918d5ead36SAlp Dener 49210978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_short", "Use the short convergence monitor", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 493a7e14dcfSSatish Balay if (flg) { 4949566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 49549abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorDefaultShort, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 496a7e14dcfSSatish Balay } 497a7e14dcfSSatish Balay 49810978b7dSBarry Smith PetscCall(PetscOptionsString("-tao_monitor_constraint_norm", "Use the default convergence monitor with constraint norm", "TaoMonitorSet", "stdout", monfilename, sizeof(monfilename), &flg)); 499a7e14dcfSSatish Balay if (flg) { 5009566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 50149abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorConstraintNorm, monviewer, (PetscCtxDestroyFn *)PetscViewerDestroy)); 502a7e14dcfSSatish Balay } 503a7e14dcfSSatish Balay 5048afaa268SBarry Smith flg = PETSC_FALSE; 505b2dc45ddSPierre Jolivet PetscCall(PetscOptionsDeprecated("-tao_cancelmonitors", "-tao_monitor_cancel", "3.21", NULL)); 506b2dc45ddSPierre Jolivet PetscCall(PetscOptionsBool("-tao_monitor_cancel", "cancel all monitors and call any registered destroy routines", "TaoMonitorCancel", flg, &flg, NULL)); 507b2dc45ddSPierre Jolivet if (flg) PetscCall(TaoMonitorCancel(tao)); 508a7e14dcfSSatish Balay 5098afaa268SBarry Smith flg = PETSC_FALSE; 51010978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_solution_draw", "Plot solution vector at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 511a7e14dcfSSatish Balay if (flg) { 512e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 513e882e171SHong Zhang PetscInt howoften = 1; 5149566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 51549abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorSolutionDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy)); 516a7e14dcfSSatish Balay } 517a7e14dcfSSatish Balay 5188afaa268SBarry Smith flg = PETSC_FALSE; 51910978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_step_draw", "Plots step at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 52010978b7dSBarry Smith if (flg) PetscCall(TaoMonitorSet(tao, TaoMonitorStepDraw, NULL, NULL)); 521a7e14dcfSSatish Balay 5228afaa268SBarry Smith flg = PETSC_FALSE; 52310978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_monitor_gradient_draw", "plots gradient at each iteration", "TaoMonitorSet", flg, &flg, NULL)); 524a7e14dcfSSatish Balay if (flg) { 525e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 526e882e171SHong Zhang PetscInt howoften = 1; 5279566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 52849abdd8aSBarry Smith PetscCall(TaoMonitorSet(tao, TaoMonitorGradientDraw, drawctx, (PetscCtxDestroyFn *)TaoMonitorDrawCtxDestroy)); 529a7e14dcfSSatish Balay } 5308afaa268SBarry Smith flg = PETSC_FALSE; 5319566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL)); 5321baa6e33SBarry Smith if (flg) PetscCall(TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, NULL)); 533f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 53410978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute Hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL)); 535f4c1ad5cSStefano Zampini if (flg) { 536f4c1ad5cSStefano Zampini Mat H; 537f4c1ad5cSStefano Zampini 5389566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 5399566063dSJacob Faibussowitsch PetscCall(MatSetType(H, MATAIJ)); 5409566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL)); 5419566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 542f4c1ad5cSStefano Zampini } 543f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 54410978b7dSBarry Smith PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free Hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL)); 545f4c1ad5cSStefano Zampini if (flg) { 546f4c1ad5cSStefano Zampini Mat H; 547f4c1ad5cSStefano Zampini 5489566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 5499566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL)); 5509566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 551f4c1ad5cSStefano Zampini } 552e876fe75SStephan Köhler PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, &found)); 553e876fe75SStephan Köhler if (found) PetscCall(TaoSetRecycleHistory(tao, flg)); 5549566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubSetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL)); 555a7e14dcfSSatish Balay 5560f0abf79SStefano Zampini if (tao->ksp) { 5570f0abf79SStefano Zampini PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL)); 5580f0abf79SStefano Zampini PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv)); 5590f0abf79SStefano Zampini } 5600f0abf79SStefano Zampini 561dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject); 56260b70c5cSStefano Zampini 56360b70c5cSStefano Zampini /* process any options handlers added with PetscObjectAddOptionsHandler() */ 56460b70c5cSStefano Zampini PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject)); 565d0609cedSBarry Smith PetscOptionsEnd(); 56660b70c5cSStefano Zampini 56760b70c5cSStefano Zampini if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch)); 5683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 569a7e14dcfSSatish Balay } 570a7e14dcfSSatish Balay 571ffeef943SBarry Smith /*@ 57247450a7bSBarry Smith TaoViewFromOptions - View a `Tao` object based on values in the options database 573fe2efc57SMark 574c3339decSBarry Smith Collective 575fe2efc57SMark 576fe2efc57SMark Input Parameters: 57747450a7bSBarry Smith + A - the `Tao` context 57847450a7bSBarry Smith . obj - Optional object that provides the prefix for the options database 579736c3998SJose E. Roman - name - command line option 580fe2efc57SMark 581fe2efc57SMark Level: intermediate 58267be906fSBarry Smith 5831cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()` 584fe2efc57SMark @*/ 585d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[]) 586d71ae5a4SJacob Faibussowitsch { 587fe2efc57SMark PetscFunctionBegin; 588fe2efc57SMark PetscValidHeaderSpecific(A, TAO_CLASSID, 1); 5899566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name)); 5903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 591fe2efc57SMark } 592fe2efc57SMark 593ffeef943SBarry Smith /*@ 59447450a7bSBarry Smith TaoView - Prints information about the `Tao` object 595a7e14dcfSSatish Balay 596c3339decSBarry Smith Collective 597a7e14dcfSSatish Balay 598a7e14dcfSSatish Balay Input Parameters: 59947450a7bSBarry Smith + tao - the `Tao` context 600a7e14dcfSSatish Balay - viewer - visualization context 601a7e14dcfSSatish Balay 602a7e14dcfSSatish Balay Options Database Key: 60365ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()` 604a7e14dcfSSatish Balay 60567be906fSBarry Smith Level: beginner 60667be906fSBarry Smith 607a7e14dcfSSatish Balay Notes: 608a7e14dcfSSatish Balay The available visualization contexts include 60965ba42b6SBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 61065ba42b6SBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 611a7e14dcfSSatish Balay output where only the first processor opens 612a7e14dcfSSatish Balay the file. All other processors send their 613a7e14dcfSSatish Balay data to the first processor to print. 614a7e14dcfSSatish Balay 6151cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `PetscViewerASCIIOpen()` 616a7e14dcfSSatish Balay @*/ 617d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView(Tao tao, PetscViewer viewer) 618d71ae5a4SJacob Faibussowitsch { 619a7e14dcfSSatish Balay PetscBool isascii, isstring; 620b625d6c7SJed Brown TaoType type; 62147a47007SBarry Smith 622a7e14dcfSSatish Balay PetscFunctionBegin; 623441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 62448a46eb9SPierre Jolivet if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer)); 625a7e14dcfSSatish Balay PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 626a7e14dcfSSatish Balay PetscCheckSameComm(tao, 1, viewer, 2); 627a7e14dcfSSatish Balay 6289566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 6299566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 630a7e14dcfSSatish Balay if (isascii) { 6319566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer)); 632a7e14dcfSSatish Balay 6339566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 634606f75f6SBarry Smith PetscTryTypeMethod(tao, view, viewer); 635606f75f6SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchView(tao->linesearch, viewer)); 636a7e14dcfSSatish Balay if (tao->ksp) { 6379566063dSJacob Faibussowitsch PetscCall(KSPView(tao->ksp, viewer)); 63863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its)); 639a7e14dcfSSatish Balay } 64019b5c101SAlp Dener 64148a46eb9SPierre Jolivet if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubSetTypes[tao->subset_type])); 642a7e14dcfSSatish Balay 6439566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol)); 644606f75f6SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " grtol=%g,", (double)tao->grtol)); 6459566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol)); 6469566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol)); 6479566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual)); 648a7e14dcfSSatish Balay 6496246e8cdSAlp Dener if (tao->constrained) { 6509566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:")); 6519566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol)); 6529566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol)); 6539566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm)); 654a7e14dcfSSatish Balay } 655a7e14dcfSSatish Balay 656a7e14dcfSSatish Balay if (tao->trust < tao->steptol) { 6579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol)); 6589566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust)); 659a7e14dcfSSatish Balay } 660a7e14dcfSSatish Balay 66148a46eb9SPierre Jolivet if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin)); 6629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Objective value=%g\n", (double)tao->fc)); 663a7e14dcfSSatish Balay 66463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of iterations=%" PetscInt_FMT ", ", tao->niter)); 66563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_it)); 666a7e14dcfSSatish Balay 667a7e14dcfSSatish Balay if (tao->nfuncs > 0) { 66863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function evaluations=%" PetscInt_FMT ",", tao->nfuncs)); 669606f75f6SBarry Smith if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n")); 670606f75f6SBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs)); 671a7e14dcfSSatish Balay } 672a7e14dcfSSatish Balay if (tao->ngrads > 0) { 67363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of gradient evaluations=%" PetscInt_FMT ",", tao->ngrads)); 674606f75f6SBarry Smith if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n")); 675606f75f6SBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs)); 676a7e14dcfSSatish Balay } 677a7e14dcfSSatish Balay if (tao->nfuncgrads > 0) { 67863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function/gradient evaluations=%" PetscInt_FMT ",", tao->nfuncgrads)); 679606f75f6SBarry Smith if (tao->max_funcs == PETSC_UNLIMITED) PetscCall(PetscViewerASCIIPrintf(viewer, " (max: unlimited)\n")); 680606f75f6SBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs)); 681a7e14dcfSSatish Balay } 68248a46eb9SPierre Jolivet if (tao->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->nhess)); 68348a46eb9SPierre Jolivet if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints)); 68448a46eb9SPierre Jolivet if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac)); 685a7e14dcfSSatish Balay 686a7e14dcfSSatish Balay if (tao->reason > 0) { 6879566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: ")); 688a7e14dcfSSatish Balay switch (tao->reason) { 689d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GATOL: 690d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n")); 691d71ae5a4SJacob Faibussowitsch break; 692d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GRTOL: 693d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n")); 694d71ae5a4SJacob Faibussowitsch break; 695d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GTTOL: 696d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n")); 697d71ae5a4SJacob Faibussowitsch break; 698d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_STEPTOL: 699d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n")); 700d71ae5a4SJacob Faibussowitsch break; 701d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_MINF: 702d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Minf -- f < fmin\n")); 703d71ae5a4SJacob Faibussowitsch break; 704d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_USER: 705d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 706d71ae5a4SJacob Faibussowitsch break; 707d71ae5a4SJacob Faibussowitsch default: 708606f75f6SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason)); 709d71ae5a4SJacob Faibussowitsch break; 710a7e14dcfSSatish Balay } 711606f75f6SBarry Smith } else if (tao->reason == TAO_CONTINUE_ITERATING) { 712606f75f6SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "Solver never run\n")); 713a7e14dcfSSatish Balay } else { 714606f75f6SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, "Solver failed: ")); 715a7e14dcfSSatish Balay switch (tao->reason) { 716d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXITS: 717d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n")); 718d71ae5a4SJacob Faibussowitsch break; 719d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_NAN: 72076c63389SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " NAN or infinity encountered\n")); 721d71ae5a4SJacob Faibussowitsch break; 722d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXFCN: 723d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n")); 724d71ae5a4SJacob Faibussowitsch break; 725d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_LS_FAILURE: 726d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n")); 727d71ae5a4SJacob Faibussowitsch break; 728d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_TR_REDUCTION: 729d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n")); 730d71ae5a4SJacob Faibussowitsch break; 731d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_USER: 732d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 733d71ae5a4SJacob Faibussowitsch break; 734d71ae5a4SJacob Faibussowitsch default: 735606f75f6SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " %d\n", tao->reason)); 736d71ae5a4SJacob Faibussowitsch break; 737a7e14dcfSSatish Balay } 738a7e14dcfSSatish Balay } 7399566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 740a7e14dcfSSatish Balay } else if (isstring) { 7419566063dSJacob Faibussowitsch PetscCall(TaoGetType(tao, &type)); 7429566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type)); 743a7e14dcfSSatish Balay } 7443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 745a7e14dcfSSatish Balay } 746a7e14dcfSSatish Balay 747a7e14dcfSSatish Balay /*@ 748414d97d3SAlp Dener TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using 74965ba42b6SBarry Smith iterate information from the previous `TaoSolve()`. This feature is disabled by 750414d97d3SAlp Dener default. 751414d97d3SAlp Dener 75267be906fSBarry Smith Logically Collective 75367be906fSBarry Smith 75467be906fSBarry Smith Input Parameters: 75547450a7bSBarry Smith + tao - the `Tao` context 75667be906fSBarry Smith - recycle - boolean flag 75767be906fSBarry Smith 75847450a7bSBarry Smith Options Database Key: 75967be906fSBarry Smith . -tao_recycle_history <true,false> - reuse the history 76067be906fSBarry Smith 76167be906fSBarry Smith Level: intermediate 76267be906fSBarry Smith 76367be906fSBarry Smith Notes: 76465ba42b6SBarry Smith For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction 76565ba42b6SBarry Smith from the previous `TaoSolve()` call when computing the first search direction in a 766414d97d3SAlp Dener new solution. By default, CG methods set the first search direction to the 767414d97d3SAlp Dener negative gradient. 768414d97d3SAlp Dener 76965ba42b6SBarry Smith For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses 77065ba42b6SBarry Smith the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()` 771414d97d3SAlp Dener call. By default, QN family of methods reset the initial Hessian approximation to 772414d97d3SAlp Dener the identity matrix. 773414d97d3SAlp Dener 774414d97d3SAlp Dener For any other algorithm, this setting has no effect. 775414d97d3SAlp Dener 7761cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 777414d97d3SAlp Dener @*/ 778d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle) 779d71ae5a4SJacob Faibussowitsch { 780414d97d3SAlp Dener PetscFunctionBegin; 781414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 78294511df7SStefano Zampini PetscValidLogicalCollectiveBool(tao, recycle, 2); 783414d97d3SAlp Dener tao->recycle = recycle; 7843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 785414d97d3SAlp Dener } 786414d97d3SAlp Dener 787414d97d3SAlp Dener /*@ 788414d97d3SAlp Dener TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information 78965ba42b6SBarry Smith from the previous `TaoSolve()`. This feature is disabled by default. 790414d97d3SAlp Dener 79167be906fSBarry Smith Logically Collective 792414d97d3SAlp Dener 79347450a7bSBarry Smith Input Parameter: 79447450a7bSBarry Smith . tao - the `Tao` context 795414d97d3SAlp Dener 79647450a7bSBarry Smith Output Parameter: 797147403d9SBarry Smith . recycle - boolean flag 798414d97d3SAlp Dener 799414d97d3SAlp Dener Level: intermediate 800414d97d3SAlp Dener 8011cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 802414d97d3SAlp Dener @*/ 803d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle) 804d71ae5a4SJacob Faibussowitsch { 805414d97d3SAlp Dener PetscFunctionBegin; 806414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 8074f572ea9SToby Isaac PetscAssertPointer(recycle, 2); 808414d97d3SAlp Dener *recycle = tao->recycle; 8093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 810414d97d3SAlp Dener } 811414d97d3SAlp Dener 812414d97d3SAlp Dener /*@ 81347450a7bSBarry Smith TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests 814a7e14dcfSSatish Balay 81567be906fSBarry Smith Logically Collective 816a7e14dcfSSatish Balay 817a7e14dcfSSatish Balay Input Parameters: 81847450a7bSBarry Smith + tao - the `Tao` context 819a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this 820a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 821a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor 822a7e14dcfSSatish Balay 823a7e14dcfSSatish Balay Options Database Keys: 824e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol 825a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol 826a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol 827a7e14dcfSSatish Balay 8283b242c63SJacob Faibussowitsch Stopping Criteria\: 82967be906fSBarry Smith .vb 83067be906fSBarry Smith ||g(X)|| <= gatol 83167be906fSBarry Smith ||g(X)|| / |f(X)| <= grtol 83267be906fSBarry Smith ||g(X)|| / ||g(X0)|| <= gttol 83367be906fSBarry Smith .ve 834a7e14dcfSSatish Balay 835a7e14dcfSSatish Balay Level: beginner 836a7e14dcfSSatish Balay 837606f75f6SBarry Smith Notes: 838606f75f6SBarry Smith Use `PETSC_CURRENT` to leave one or more tolerances unchanged. 839606f75f6SBarry Smith 840606f75f6SBarry Smith Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao`object's type was set 841606f75f6SBarry Smith 842606f75f6SBarry Smith Fortran Note: 843606f75f6SBarry Smith Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL` 844a7e14dcfSSatish Balay 8451cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()` 846a7e14dcfSSatish Balay @*/ 847d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol) 848d71ae5a4SJacob Faibussowitsch { 849a7e14dcfSSatish Balay PetscFunctionBegin; 850441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 85194511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gatol, 2); 85294511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, grtol, 3); 85394511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gttol, 4); 854a7e14dcfSSatish Balay 855606f75f6SBarry Smith if (gatol == (PetscReal)PETSC_DETERMINE) { 856606f75f6SBarry Smith tao->gatol = tao->default_gatol; 857606f75f6SBarry Smith } else if (gatol != (PetscReal)PETSC_CURRENT) { 858606f75f6SBarry Smith PetscCheck(gatol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gatol not allowed"); 859606f75f6SBarry Smith tao->gatol = gatol; 860a7e14dcfSSatish Balay } 861a7e14dcfSSatish Balay 862606f75f6SBarry Smith if (grtol == (PetscReal)PETSC_DETERMINE) { 863606f75f6SBarry Smith tao->grtol = tao->default_grtol; 864606f75f6SBarry Smith } else if (grtol != (PetscReal)PETSC_CURRENT) { 865606f75f6SBarry Smith PetscCheck(grtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative grtol not allowed"); 866606f75f6SBarry Smith tao->grtol = grtol; 867a7e14dcfSSatish Balay } 868a7e14dcfSSatish Balay 869606f75f6SBarry Smith if (gttol == (PetscReal)PETSC_DETERMINE) { 870606f75f6SBarry Smith tao->gttol = tao->default_gttol; 871606f75f6SBarry Smith } else if (gttol != (PetscReal)PETSC_CURRENT) { 872606f75f6SBarry Smith PetscCheck(gttol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative gttol not allowed"); 873606f75f6SBarry Smith tao->gttol = gttol; 874a7e14dcfSSatish Balay } 8753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 876a7e14dcfSSatish Balay } 877a7e14dcfSSatish Balay 878a7e14dcfSSatish Balay /*@ 87947450a7bSBarry Smith TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests 880a7e14dcfSSatish Balay 88167be906fSBarry Smith Logically Collective 882a7e14dcfSSatish Balay 883a7e14dcfSSatish Balay Input Parameters: 88447450a7bSBarry Smith + tao - the `Tao` context 885606f75f6SBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria 886606f75f6SBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria 887a7e14dcfSSatish Balay 888a7e14dcfSSatish Balay Options Database Keys: 889a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol 890a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol 891a7e14dcfSSatish Balay 892a7e14dcfSSatish Balay Level: intermediate 893a7e14dcfSSatish Balay 89467be906fSBarry Smith Notes: 895606f75f6SBarry Smith Use `PETSC_CURRENT` to leave one or tolerance unchanged. 896606f75f6SBarry Smith 897606f75f6SBarry Smith Use `PETSC_DETERMINE` to set one or more tolerances to their values when the `tao` object's type was set 898606f75f6SBarry Smith 899606f75f6SBarry Smith Fortran Note: 900606f75f6SBarry Smith Use `PETSC_CURRENT_REAL` or `PETSC_DETERMINE_REAL` 901a7e14dcfSSatish Balay 9021cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()` 903a7e14dcfSSatish Balay @*/ 904d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol) 905d71ae5a4SJacob Faibussowitsch { 906a7e14dcfSSatish Balay PetscFunctionBegin; 907441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 90894511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, catol, 2); 90994511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, crtol, 3); 910a7e14dcfSSatish Balay 911606f75f6SBarry Smith if (catol == (PetscReal)PETSC_DETERMINE) { 912606f75f6SBarry Smith tao->catol = tao->default_catol; 913606f75f6SBarry Smith } else if (catol != (PetscReal)PETSC_CURRENT) { 914606f75f6SBarry Smith PetscCheck(catol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative catol not allowed"); 915606f75f6SBarry Smith tao->catol = catol; 916a7e14dcfSSatish Balay } 917a7e14dcfSSatish Balay 918606f75f6SBarry Smith if (crtol == (PetscReal)PETSC_DETERMINE) { 919606f75f6SBarry Smith tao->crtol = tao->default_crtol; 920606f75f6SBarry Smith } else if (crtol != (PetscReal)PETSC_CURRENT) { 921606f75f6SBarry Smith PetscCheck(crtol >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Negative crtol not allowed"); 922606f75f6SBarry Smith tao->crtol = crtol; 923a7e14dcfSSatish Balay } 9243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 925a7e14dcfSSatish Balay } 926a7e14dcfSSatish Balay 92758417fe7SBarry Smith /*@ 92847450a7bSBarry Smith TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests 92958417fe7SBarry Smith 93067be906fSBarry Smith Not Collective 93158417fe7SBarry Smith 93258417fe7SBarry Smith Input Parameter: 93347450a7bSBarry Smith . tao - the `Tao` context 93458417fe7SBarry Smith 935d8d19677SJose E. Roman Output Parameters: 936606f75f6SBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for `gatol` convergence criteria 937606f75f6SBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for `gatol`, `gttol` convergence criteria 93858417fe7SBarry Smith 93958417fe7SBarry Smith Level: intermediate 94058417fe7SBarry Smith 9411cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReasons`,`TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()` 94258417fe7SBarry Smith @*/ 943d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol) 944d71ae5a4SJacob Faibussowitsch { 94558417fe7SBarry Smith PetscFunctionBegin; 94658417fe7SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 94758417fe7SBarry Smith if (catol) *catol = tao->catol; 94858417fe7SBarry Smith if (crtol) *crtol = tao->crtol; 9493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 95058417fe7SBarry Smith } 95158417fe7SBarry Smith 952a7e14dcfSSatish Balay /*@ 953a7e14dcfSSatish Balay TaoSetFunctionLowerBound - Sets a bound on the solution objective value. 954a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 955a7e14dcfSSatish Balay has been found, the solver will terminate. 956a7e14dcfSSatish Balay 957c3339decSBarry Smith Logically Collective 958a7e14dcfSSatish Balay 959a7e14dcfSSatish Balay Input Parameters: 960441846f8SBarry Smith + tao - the Tao solver context 961a7e14dcfSSatish Balay - fmin - the tolerance 962a7e14dcfSSatish Balay 96347450a7bSBarry Smith Options Database Key: 964a7e14dcfSSatish Balay . -tao_fmin <fmin> - sets the minimum function value 965a7e14dcfSSatish Balay 966a7e14dcfSSatish Balay Level: intermediate 967a7e14dcfSSatish Balay 9681cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()` 969a7e14dcfSSatish Balay @*/ 970d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin) 971d71ae5a4SJacob Faibussowitsch { 972a7e14dcfSSatish Balay PetscFunctionBegin; 973441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 97494511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, fmin, 2); 975a7e14dcfSSatish Balay tao->fmin = fmin; 9763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 977a7e14dcfSSatish Balay } 978a7e14dcfSSatish Balay 979a7e14dcfSSatish Balay /*@ 9808e96d397SJason Sarich TaoGetFunctionLowerBound - Gets the bound on the solution objective value. 981a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 982a7e14dcfSSatish Balay has been found, the solver will terminate. 983a7e14dcfSSatish Balay 98467be906fSBarry Smith Not Collective 985a7e14dcfSSatish Balay 98647450a7bSBarry Smith Input Parameter: 98747450a7bSBarry Smith . tao - the `Tao` solver context 988a7e14dcfSSatish Balay 98947450a7bSBarry Smith Output Parameter: 990a7e14dcfSSatish Balay . fmin - the minimum function value 991a7e14dcfSSatish Balay 992a7e14dcfSSatish Balay Level: intermediate 993a7e14dcfSSatish Balay 9941cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()` 995a7e14dcfSSatish Balay @*/ 996d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin) 997d71ae5a4SJacob Faibussowitsch { 998a7e14dcfSSatish Balay PetscFunctionBegin; 999441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 10004f572ea9SToby Isaac PetscAssertPointer(fmin, 2); 1001a7e14dcfSSatish Balay *fmin = tao->fmin; 10023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1003a7e14dcfSSatish Balay } 1004a7e14dcfSSatish Balay 1005a7e14dcfSSatish Balay /*@ 100647450a7bSBarry Smith TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`. 1007a7e14dcfSSatish Balay 1008c3339decSBarry Smith Logically Collective 1009a7e14dcfSSatish Balay 1010a7e14dcfSSatish Balay Input Parameters: 101147450a7bSBarry Smith + tao - the `Tao` solver context 1012606f75f6SBarry Smith - nfcn - the maximum number of function evaluations (>=0), use `PETSC_UNLIMITED` to have no bound 1013a7e14dcfSSatish Balay 101447450a7bSBarry Smith Options Database Key: 1015a7e14dcfSSatish Balay . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations 1016a7e14dcfSSatish Balay 1017a7e14dcfSSatish Balay Level: intermediate 1018a7e14dcfSSatish Balay 1019606f75f6SBarry Smith Note: 1020606f75f6SBarry Smith Use `PETSC_DETERMINE` to use the default maximum number of function evaluations that was set when the object type was set. 1021606f75f6SBarry Smith 1022606f75f6SBarry Smith Developer Note: 1023606f75f6SBarry Smith Deprecated support for an unlimited number of function evaluations by passing a negative value. 1024606f75f6SBarry Smith 10251cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()` 1026a7e14dcfSSatish Balay @*/ 1027d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn) 1028d71ae5a4SJacob Faibussowitsch { 1029a7e14dcfSSatish Balay PetscFunctionBegin; 1030441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 103194511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, nfcn, 2); 1032606f75f6SBarry Smith if (nfcn == PETSC_DETERMINE) { 1033606f75f6SBarry Smith tao->max_funcs = tao->default_max_funcs; 1034606f75f6SBarry Smith } else if (nfcn == PETSC_UNLIMITED || nfcn < 0) { 1035606f75f6SBarry Smith tao->max_funcs = PETSC_UNLIMITED; 10369371c9d4SSatish Balay } else { 1037d7c1f440SPierre Jolivet PetscCheck(nfcn >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of function evaluations must be positive"); 1038606f75f6SBarry Smith tao->max_funcs = nfcn; 10399371c9d4SSatish Balay } 10403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1041a7e14dcfSSatish Balay } 1042a7e14dcfSSatish Balay 1043a7e14dcfSSatish Balay /*@ 104447450a7bSBarry Smith TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()` 1045a7e14dcfSSatish Balay 1046c3339decSBarry Smith Logically Collective 1047a7e14dcfSSatish Balay 104847450a7bSBarry Smith Input Parameter: 104947450a7bSBarry Smith . tao - the `Tao` solver context 1050a7e14dcfSSatish Balay 105147450a7bSBarry Smith Output Parameter: 1052a7e14dcfSSatish Balay . nfcn - the maximum number of function evaluations 1053a7e14dcfSSatish Balay 1054a7e14dcfSSatish Balay Level: intermediate 1055a7e14dcfSSatish Balay 10561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1057a7e14dcfSSatish Balay @*/ 1058d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn) 1059d71ae5a4SJacob Faibussowitsch { 1060a7e14dcfSSatish Balay PetscFunctionBegin; 1061441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 10624f572ea9SToby Isaac PetscAssertPointer(nfcn, 2); 1063a7e14dcfSSatish Balay *nfcn = tao->max_funcs; 10643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1065a7e14dcfSSatish Balay } 1066a7e14dcfSSatish Balay 1067770232b9SCe Qin /*@ 106847450a7bSBarry Smith TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object 1069770232b9SCe Qin 1070770232b9SCe Qin Not Collective 1071770232b9SCe Qin 107247450a7bSBarry Smith Input Parameter: 107347450a7bSBarry Smith . tao - the `Tao` solver context 1074770232b9SCe Qin 107547450a7bSBarry Smith Output Parameter: 107694511df7SStefano Zampini . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations) 1077770232b9SCe Qin 1078770232b9SCe Qin Level: intermediate 1079770232b9SCe Qin 10801cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1081770232b9SCe Qin @*/ 1082d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs) 1083d71ae5a4SJacob Faibussowitsch { 1084770232b9SCe Qin PetscFunctionBegin; 1085770232b9SCe Qin PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 10864f572ea9SToby Isaac PetscAssertPointer(nfuncs, 2); 1087770232b9SCe Qin *nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 10883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1089770232b9SCe Qin } 1090770232b9SCe Qin 1091a7e14dcfSSatish Balay /*@ 109247450a7bSBarry Smith TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()` 1093a7e14dcfSSatish Balay 1094c3339decSBarry Smith Logically Collective 1095a7e14dcfSSatish Balay 1096a7e14dcfSSatish Balay Input Parameters: 109747450a7bSBarry Smith + tao - the `Tao` solver context 1098606f75f6SBarry Smith - maxits - the maximum number of iterates (>=0), use `PETSC_UNLIMITED` to have no bound 1099a7e14dcfSSatish Balay 110047450a7bSBarry Smith Options Database Key: 1101a7e14dcfSSatish Balay . -tao_max_it <its> - sets the maximum number of iterations 1102a7e14dcfSSatish Balay 1103a7e14dcfSSatish Balay Level: intermediate 1104a7e14dcfSSatish Balay 1105606f75f6SBarry Smith Note: 1106606f75f6SBarry Smith Use `PETSC_DETERMINE` to use the default maximum number of iterations that was set when the object's type was set. 1107606f75f6SBarry Smith 1108606f75f6SBarry Smith Developer Note: 1109606f75f6SBarry Smith DeprAlso accepts the deprecated negative values to indicate no limit 1110606f75f6SBarry Smith 11111cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()` 1112a7e14dcfSSatish Balay @*/ 1113d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits) 1114d71ae5a4SJacob Faibussowitsch { 1115a7e14dcfSSatish Balay PetscFunctionBegin; 1116441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 111794511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, maxits, 2); 1118606f75f6SBarry Smith if (maxits == PETSC_DETERMINE) { 1119606f75f6SBarry Smith tao->max_it = tao->default_max_it; 1120606f75f6SBarry Smith } else if (maxits == PETSC_UNLIMITED) { 11211690c2aeSBarry Smith tao->max_it = PETSC_INT_MAX; 1122606f75f6SBarry Smith } else { 1123d7c1f440SPierre Jolivet PetscCheck(maxits > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of iterations must be positive"); 1124606f75f6SBarry Smith tao->max_it = maxits; 1125606f75f6SBarry Smith } 11263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1127a7e14dcfSSatish Balay } 1128a7e14dcfSSatish Balay 1129a7e14dcfSSatish Balay /*@ 113065ba42b6SBarry Smith TaoGetMaximumIterations - Gets a maximum number of iterates that will be used 1131a7e14dcfSSatish Balay 1132a7e14dcfSSatish Balay Not Collective 1133a7e14dcfSSatish Balay 113447450a7bSBarry Smith Input Parameter: 113547450a7bSBarry Smith . tao - the `Tao` solver context 1136a7e14dcfSSatish Balay 113747450a7bSBarry Smith Output Parameter: 1138a7e14dcfSSatish Balay . maxits - the maximum number of iterates 1139a7e14dcfSSatish Balay 1140a7e14dcfSSatish Balay Level: intermediate 1141a7e14dcfSSatish Balay 11421cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()` 1143a7e14dcfSSatish Balay @*/ 1144d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits) 1145d71ae5a4SJacob Faibussowitsch { 1146a7e14dcfSSatish Balay PetscFunctionBegin; 1147441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 11484f572ea9SToby Isaac PetscAssertPointer(maxits, 2); 1149a7e14dcfSSatish Balay *maxits = tao->max_it; 11503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1151a7e14dcfSSatish Balay } 1152a7e14dcfSSatish Balay 1153a7e14dcfSSatish Balay /*@ 1154a7e14dcfSSatish Balay TaoSetInitialTrustRegionRadius - Sets the initial trust region radius. 1155a7e14dcfSSatish Balay 115667be906fSBarry Smith Logically Collective 1157a7e14dcfSSatish Balay 1158d8d19677SJose E. Roman Input Parameters: 115947450a7bSBarry Smith + tao - a `Tao` optimization solver 1160a7e14dcfSSatish Balay - radius - the trust region radius 1161a7e14dcfSSatish Balay 1162a7e14dcfSSatish Balay Options Database Key: 1163a7e14dcfSSatish Balay . -tao_trust0 <t0> - sets initial trust region radius 1164a7e14dcfSSatish Balay 116547450a7bSBarry Smith Level: intermediate 116647450a7bSBarry Smith 1167606f75f6SBarry Smith Note: 1168606f75f6SBarry Smith Use `PETSC_DETERMINE` to use the default radius that was set when the object's type was set. 1169606f75f6SBarry Smith 11701cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR` 1171a7e14dcfSSatish Balay @*/ 1172d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius) 1173d71ae5a4SJacob Faibussowitsch { 1174a7e14dcfSSatish Balay PetscFunctionBegin; 1175441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 117694511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, radius, 2); 1177606f75f6SBarry Smith if (radius == PETSC_DETERMINE) { 1178606f75f6SBarry Smith tao->trust0 = tao->default_trust0; 1179606f75f6SBarry Smith } else { 1180606f75f6SBarry Smith PetscCheck(radius > 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "Radius must be positive"); 1181606f75f6SBarry Smith tao->trust0 = radius; 1182606f75f6SBarry Smith } 11833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1184a7e14dcfSSatish Balay } 1185a7e14dcfSSatish Balay 1186a7e14dcfSSatish Balay /*@ 118765ba42b6SBarry Smith TaoGetInitialTrustRegionRadius - Gets the initial trust region radius. 1188a7e14dcfSSatish Balay 1189a7e14dcfSSatish Balay Not Collective 1190a7e14dcfSSatish Balay 1191a7e14dcfSSatish Balay Input Parameter: 119247450a7bSBarry Smith . tao - a `Tao` optimization solver 1193a7e14dcfSSatish Balay 1194a7e14dcfSSatish Balay Output Parameter: 1195a7e14dcfSSatish Balay . radius - the trust region radius 1196a7e14dcfSSatish Balay 1197a7e14dcfSSatish Balay Level: intermediate 1198a7e14dcfSSatish Balay 11991cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR` 1200a7e14dcfSSatish Balay @*/ 1201d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius) 1202d71ae5a4SJacob Faibussowitsch { 1203a7e14dcfSSatish Balay PetscFunctionBegin; 1204441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12054f572ea9SToby Isaac PetscAssertPointer(radius, 2); 1206a7e14dcfSSatish Balay *radius = tao->trust0; 12073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1208a7e14dcfSSatish Balay } 1209a7e14dcfSSatish Balay 1210a7e14dcfSSatish Balay /*@ 1211a7e14dcfSSatish Balay TaoGetCurrentTrustRegionRadius - Gets the current trust region radius. 1212a7e14dcfSSatish Balay 1213a7e14dcfSSatish Balay Not Collective 1214a7e14dcfSSatish Balay 1215a7e14dcfSSatish Balay Input Parameter: 121647450a7bSBarry Smith . tao - a `Tao` optimization solver 1217a7e14dcfSSatish Balay 1218a7e14dcfSSatish Balay Output Parameter: 1219a7e14dcfSSatish Balay . radius - the trust region radius 1220a7e14dcfSSatish Balay 1221a7e14dcfSSatish Balay Level: intermediate 1222a7e14dcfSSatish Balay 12231cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR` 1224a7e14dcfSSatish Balay @*/ 1225d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius) 1226d71ae5a4SJacob Faibussowitsch { 1227a7e14dcfSSatish Balay PetscFunctionBegin; 1228441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12294f572ea9SToby Isaac PetscAssertPointer(radius, 2); 1230a7e14dcfSSatish Balay *radius = tao->trust; 12313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1232a7e14dcfSSatish Balay } 1233a7e14dcfSSatish Balay 1234a7e14dcfSSatish Balay /*@ 123547450a7bSBarry Smith TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()` 1236a7e14dcfSSatish Balay 1237a7e14dcfSSatish Balay Not Collective 1238a7e14dcfSSatish Balay 1239f899ff85SJose E. Roman Input Parameter: 124047450a7bSBarry Smith . tao - the `Tao` context 1241a7e14dcfSSatish Balay 1242a7e14dcfSSatish Balay Output Parameters: 1243e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this 1244a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 1245a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor 1246a7e14dcfSSatish Balay 1247a7e14dcfSSatish Balay Level: intermediate 1248a1cb98faSBarry Smith 1249a1cb98faSBarry Smith Note: 125047450a7bSBarry Smith `NULL` can be used as an argument if not all tolerances values are needed 1251a1cb98faSBarry Smith 12521cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()` 1253a7e14dcfSSatish Balay @*/ 1254d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol) 1255d71ae5a4SJacob Faibussowitsch { 1256a7e14dcfSSatish Balay PetscFunctionBegin; 1257441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1258a7e14dcfSSatish Balay if (gatol) *gatol = tao->gatol; 1259a7e14dcfSSatish Balay if (grtol) *grtol = tao->grtol; 1260a7e14dcfSSatish Balay if (gttol) *gttol = tao->gttol; 12613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1262a7e14dcfSSatish Balay } 1263a7e14dcfSSatish Balay 1264a7e14dcfSSatish Balay /*@ 1265a7e14dcfSSatish Balay TaoGetKSP - Gets the linear solver used by the optimization solver. 1266a7e14dcfSSatish Balay 1267a7e14dcfSSatish Balay Not Collective 1268a7e14dcfSSatish Balay 126947450a7bSBarry Smith Input Parameter: 127047450a7bSBarry Smith . tao - the `Tao` solver 1271a7e14dcfSSatish Balay 127247450a7bSBarry Smith Output Parameter: 127347450a7bSBarry Smith . ksp - the `KSP` linear solver used in the optimization solver 1274a7e14dcfSSatish Balay 1275a7e14dcfSSatish Balay Level: intermediate 1276a7e14dcfSSatish Balay 12771cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `KSP` 1278a7e14dcfSSatish Balay @*/ 1279d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp) 1280d71ae5a4SJacob Faibussowitsch { 1281a7e14dcfSSatish Balay PetscFunctionBegin; 128294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 12834f572ea9SToby Isaac PetscAssertPointer(ksp, 2); 1284a7e14dcfSSatish Balay *ksp = tao->ksp; 12853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1286a7e14dcfSSatish Balay } 1287a7e14dcfSSatish Balay 1288025e9500SJason Sarich /*@ 1289025e9500SJason Sarich TaoGetLinearSolveIterations - Gets the total number of linear iterations 129047450a7bSBarry Smith used by the `Tao` solver 1291025e9500SJason Sarich 1292025e9500SJason Sarich Not Collective 1293025e9500SJason Sarich 1294025e9500SJason Sarich Input Parameter: 129547450a7bSBarry Smith . tao - the `Tao` context 1296025e9500SJason Sarich 1297025e9500SJason Sarich Output Parameter: 1298025e9500SJason Sarich . lits - number of linear iterations 1299025e9500SJason Sarich 1300025e9500SJason Sarich Level: intermediate 1301025e9500SJason Sarich 130247450a7bSBarry Smith Note: 130347450a7bSBarry Smith This counter is reset to zero for each successive call to `TaoSolve()` 130447450a7bSBarry Smith 13051cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetKSP()` 1306025e9500SJason Sarich @*/ 1307d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits) 1308d71ae5a4SJacob Faibussowitsch { 1309025e9500SJason Sarich PetscFunctionBegin; 1310025e9500SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 13114f572ea9SToby Isaac PetscAssertPointer(lits, 2); 13122d9aa51bSJason Sarich *lits = tao->ksp_tot_its; 13133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1314025e9500SJason Sarich } 1315025e9500SJason Sarich 1316a7e14dcfSSatish Balay /*@ 1317a7e14dcfSSatish Balay TaoGetLineSearch - Gets the line search used by the optimization solver. 1318a7e14dcfSSatish Balay 1319a7e14dcfSSatish Balay Not Collective 1320a7e14dcfSSatish Balay 132147450a7bSBarry Smith Input Parameter: 132247450a7bSBarry Smith . tao - the `Tao` solver 1323a7e14dcfSSatish Balay 132447450a7bSBarry Smith Output Parameter: 1325a7e14dcfSSatish Balay . ls - the line search used in the optimization solver 1326a7e14dcfSSatish Balay 1327a7e14dcfSSatish Balay Level: intermediate 1328a7e14dcfSSatish Balay 13291cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType` 1330a7e14dcfSSatish Balay @*/ 1331d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls) 1332d71ae5a4SJacob Faibussowitsch { 1333a7e14dcfSSatish Balay PetscFunctionBegin; 133494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 13354f572ea9SToby Isaac PetscAssertPointer(ls, 2); 1336a7e14dcfSSatish Balay *ls = tao->linesearch; 13373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1338a7e14dcfSSatish Balay } 1339a7e14dcfSSatish Balay 1340a7e14dcfSSatish Balay /*@ 1341a7e14dcfSSatish Balay TaoAddLineSearchCounts - Adds the number of function evaluations spent 1342a7e14dcfSSatish Balay in the line search to the running total. 1343a7e14dcfSSatish Balay 1344a7e14dcfSSatish Balay Input Parameters: 13452fe279fdSBarry Smith . tao - the `Tao` solver 1346a7e14dcfSSatish Balay 1347a7e14dcfSSatish Balay Level: developer 1348a7e14dcfSSatish Balay 13491cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()` 1350a7e14dcfSSatish Balay @*/ 1351d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAddLineSearchCounts(Tao tao) 1352d71ae5a4SJacob Faibussowitsch { 1353a7e14dcfSSatish Balay PetscBool flg; 1354a7e14dcfSSatish Balay PetscInt nfeval, ngeval, nfgeval; 135547a47007SBarry Smith 1356a7e14dcfSSatish Balay PetscFunctionBegin; 1357441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1358a7e14dcfSSatish Balay if (tao->linesearch) { 13599566063dSJacob Faibussowitsch PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg)); 136094ae4db5SBarry Smith if (!flg) { 13619566063dSJacob Faibussowitsch PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval)); 1362a7e14dcfSSatish Balay tao->nfuncs += nfeval; 1363a7e14dcfSSatish Balay tao->ngrads += ngeval; 1364a7e14dcfSSatish Balay tao->nfuncgrads += nfgeval; 1365a7e14dcfSSatish Balay } 1366a7e14dcfSSatish Balay } 13673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1368a7e14dcfSSatish Balay } 1369a7e14dcfSSatish Balay 1370a7e14dcfSSatish Balay /*@ 137147450a7bSBarry Smith TaoGetSolution - Returns the vector with the current solution from the `Tao` object 1372a7e14dcfSSatish Balay 1373a7e14dcfSSatish Balay Not Collective 1374a7e14dcfSSatish Balay 1375a7e14dcfSSatish Balay Input Parameter: 137647450a7bSBarry Smith . tao - the `Tao` context 1377a7e14dcfSSatish Balay 1378a7e14dcfSSatish Balay Output Parameter: 1379a7e14dcfSSatish Balay . X - the current solution 1380a7e14dcfSSatish Balay 1381a7e14dcfSSatish Balay Level: intermediate 1382a7e14dcfSSatish Balay 138365ba42b6SBarry Smith Note: 138465ba42b6SBarry Smith The returned vector will be the same object that was passed into `TaoSetSolution()` 138565ba42b6SBarry Smith 13861cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()` 1387a7e14dcfSSatish Balay @*/ 1388d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolution(Tao tao, Vec *X) 1389d71ae5a4SJacob Faibussowitsch { 1390a7e14dcfSSatish Balay PetscFunctionBegin; 1391441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 13924f572ea9SToby Isaac PetscAssertPointer(X, 2); 1393a7e14dcfSSatish Balay *X = tao->solution; 13943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1395a7e14dcfSSatish Balay } 1396a7e14dcfSSatish Balay 1397a7e14dcfSSatish Balay /*@ 139847450a7bSBarry Smith TaoResetStatistics - Initialize the statistics collected by the `Tao` object. 1399a7e14dcfSSatish Balay These statistics include the iteration number, residual norms, and convergence status. 1400a7e14dcfSSatish Balay This routine gets called before solving each optimization problem. 1401a7e14dcfSSatish Balay 1402c3339decSBarry Smith Collective 1403a7e14dcfSSatish Balay 140447450a7bSBarry Smith Input Parameter: 1405e056e8ceSJacob Faibussowitsch . tao - the `Tao` context 1406a7e14dcfSSatish Balay 1407a7e14dcfSSatish Balay Level: developer 1408a7e14dcfSSatish Balay 14091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 1410a7e14dcfSSatish Balay @*/ 1411d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResetStatistics(Tao tao) 1412d71ae5a4SJacob Faibussowitsch { 1413a7e14dcfSSatish Balay PetscFunctionBegin; 1414441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1415a7e14dcfSSatish Balay tao->niter = 0; 1416a7e14dcfSSatish Balay tao->nfuncs = 0; 1417a7e14dcfSSatish Balay tao->nfuncgrads = 0; 1418a7e14dcfSSatish Balay tao->ngrads = 0; 1419a7e14dcfSSatish Balay tao->nhess = 0; 1420a7e14dcfSSatish Balay tao->njac = 0; 1421a7e14dcfSSatish Balay tao->nconstraints = 0; 1422a7e14dcfSSatish Balay tao->ksp_its = 0; 1423ae93cb3cSJason Sarich tao->ksp_tot_its = 0; 1424a7e14dcfSSatish Balay tao->reason = TAO_CONTINUE_ITERATING; 1425a7e14dcfSSatish Balay tao->residual = 0.0; 1426a7e14dcfSSatish Balay tao->cnorm = 0.0; 1427a7e14dcfSSatish Balay tao->step = 0.0; 1428a7e14dcfSSatish Balay tao->lsflag = PETSC_FALSE; 1429a7e14dcfSSatish Balay if (tao->hist_reset) tao->hist_len = 0; 14303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1431a7e14dcfSSatish Balay } 1432a7e14dcfSSatish Balay 1433a7e14dcfSSatish Balay /*@C 1434e1e80dc8SAlp Dener TaoSetUpdate - Sets the general-purpose update function called 14352fe279fdSBarry Smith at the beginning of every iteration of the optimization algorithm. Called after the new solution and the gradient 1436e1e80dc8SAlp Dener is determined, but before the Hessian is computed (if applicable). 1437e1e80dc8SAlp Dener 1438c3339decSBarry Smith Logically Collective 1439e1e80dc8SAlp Dener 1440e1e80dc8SAlp Dener Input Parameters: 1441270bebe6SStefano Zampini + tao - The `Tao` solver 1442270bebe6SStefano Zampini . func - The function 1443270bebe6SStefano Zampini - ctx - The update function context 1444e1e80dc8SAlp Dener 144520f4b53cSBarry Smith Calling sequence of `func`: 1446270bebe6SStefano Zampini + tao - The optimizer context 1447270bebe6SStefano Zampini . it - The current iteration index 1448270bebe6SStefano Zampini - ctx - The update context 1449e1e80dc8SAlp Dener 1450e1e80dc8SAlp Dener Level: advanced 1451e1e80dc8SAlp Dener 1452270bebe6SStefano Zampini Notes: 1453270bebe6SStefano Zampini Users can modify the gradient direction or any other vector associated to the specific solver used. 1454270bebe6SStefano Zampini The objective function value is always recomputed after a call to the update hook. 1455270bebe6SStefano Zampini 14561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()` 1457e1e80dc8SAlp Dener @*/ 1458*2a8381b2SBarry Smith PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao tao, PetscInt it, PetscCtx ctx), PetscCtx ctx) 1459d71ae5a4SJacob Faibussowitsch { 1460e1e80dc8SAlp Dener PetscFunctionBegin; 1461e1e80dc8SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1462e1e80dc8SAlp Dener tao->ops->update = func; 1463e1e80dc8SAlp Dener tao->user_update = ctx; 14643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1465e1e80dc8SAlp Dener } 1466e1e80dc8SAlp Dener 1467e1e80dc8SAlp Dener /*@C 1468a7e14dcfSSatish Balay TaoSetConvergenceTest - Sets the function that is to be used to test 1469a7e14dcfSSatish Balay for convergence of the iterative minimization solution. The new convergence 147065ba42b6SBarry Smith testing routine will replace Tao's default convergence test. 1471a7e14dcfSSatish Balay 1472c3339decSBarry Smith Logically Collective 1473a7e14dcfSSatish Balay 1474a7e14dcfSSatish Balay Input Parameters: 147547450a7bSBarry Smith + tao - the `Tao` object 1476a7e14dcfSSatish Balay . conv - the routine to test for convergence 1477a7e14dcfSSatish Balay - ctx - [optional] context for private data for the convergence routine 147847450a7bSBarry Smith (may be `NULL`) 1479a7e14dcfSSatish Balay 148020f4b53cSBarry Smith Calling sequence of `conv`: 148147450a7bSBarry Smith + tao - the `Tao` object 1482a7e14dcfSSatish Balay - ctx - [optional] convergence context 1483a7e14dcfSSatish Balay 148447450a7bSBarry Smith Level: advanced 148547450a7bSBarry Smith 148665ba42b6SBarry Smith Note: 148765ba42b6SBarry Smith The new convergence testing routine should call `TaoSetConvergedReason()`. 1488a7e14dcfSSatish Balay 148910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoMonitorSet()` 1490a7e14dcfSSatish Balay @*/ 1491*2a8381b2SBarry Smith PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void *), PetscCtx ctx) 1492d71ae5a4SJacob Faibussowitsch { 1493a7e14dcfSSatish Balay PetscFunctionBegin; 1494441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 149594511df7SStefano Zampini tao->ops->convergencetest = conv; 149694511df7SStefano Zampini tao->cnvP = ctx; 14973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1498a7e14dcfSSatish Balay } 1499a7e14dcfSSatish Balay 1500a7e14dcfSSatish Balay /*@C 150110978b7dSBarry Smith TaoMonitorSet - Sets an additional function that is to be used at every 1502a7e14dcfSSatish Balay iteration of the solver to display the iteration's 1503a7e14dcfSSatish Balay progress. 1504a7e14dcfSSatish Balay 1505c3339decSBarry Smith Logically Collective 1506a7e14dcfSSatish Balay 1507a7e14dcfSSatish Balay Input Parameters: 150847450a7bSBarry Smith + tao - the `Tao` solver context 15092fe279fdSBarry Smith . func - monitoring routine 15102fe279fdSBarry Smith . ctx - [optional] user-defined context for private data for the monitor routine (may be `NULL`) 151149abdd8aSBarry Smith - dest - [optional] function to destroy the context when the `Tao` is destroyed, see `PetscCtxDestroyFn` for the calling sequence 1512a7e14dcfSSatish Balay 15132fe279fdSBarry Smith Calling sequence of `func`: 151447450a7bSBarry Smith + tao - the `Tao` solver context 15152fe279fdSBarry Smith - ctx - [optional] monitoring context 15162fe279fdSBarry Smith 151747450a7bSBarry Smith Level: intermediate 151847450a7bSBarry Smith 151987497f52SBarry Smith Notes: 152010978b7dSBarry Smith See `TaoSetFromOptions()` for a monitoring options. 152110978b7dSBarry Smith 1522a7e14dcfSSatish Balay Several different monitoring routines may be set by calling 152310978b7dSBarry Smith `TaoMonitorSet()` multiple times; all will be called in the 1524a7e14dcfSSatish Balay order in which they were set. 1525a7e14dcfSSatish Balay 1526e056e8ceSJacob Faibussowitsch Fortran Notes: 152795452b02SPatrick Sanan Only one monitor function may be set 1528a7e14dcfSSatish Balay 1529e6aa7a3bSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoMonitorCancel()`, `TaoView()`, `PetscCtxDestroyFn` 1530a7e14dcfSSatish Balay @*/ 1531*2a8381b2SBarry Smith PetscErrorCode TaoMonitorSet(Tao tao, PetscErrorCode (*func)(Tao, void *), PetscCtx ctx, PetscCtxDestroyFn *dest) 1532d71ae5a4SJacob Faibussowitsch { 1533a7e14dcfSSatish Balay PetscFunctionBegin; 1534441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 15353c859ba3SBarry Smith PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS); 1536453a69bbSBarry Smith for (PetscInt i = 0; i < tao->numbermonitors; i++) { 1537453a69bbSBarry Smith PetscBool identical; 153808d19d1fSJason Sarich 1539453a69bbSBarry Smith PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))(PetscVoidFn *)func, ctx, dest, (PetscErrorCode (*)(void))(PetscVoidFn *)tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical)); 15403ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 154108d19d1fSJason Sarich } 1542a7e14dcfSSatish Balay tao->monitor[tao->numbermonitors] = func; 1543835f2295SStefano Zampini tao->monitorcontext[tao->numbermonitors] = ctx; 1544a7e14dcfSSatish Balay tao->monitordestroy[tao->numbermonitors] = dest; 1545a7e14dcfSSatish Balay ++tao->numbermonitors; 15463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1547a7e14dcfSSatish Balay } 1548a7e14dcfSSatish Balay 1549a7e14dcfSSatish Balay /*@ 1550b2dc45ddSPierre Jolivet TaoMonitorCancel - Clears all the monitor functions for a `Tao` object. 1551a7e14dcfSSatish Balay 1552c3339decSBarry Smith Logically Collective 1553a7e14dcfSSatish Balay 155447450a7bSBarry Smith Input Parameter: 155547450a7bSBarry Smith . tao - the `Tao` solver context 1556a7e14dcfSSatish Balay 15573c7db156SBarry Smith Options Database Key: 1558b2dc45ddSPierre Jolivet . -tao_monitor_cancel - cancels all monitors that have been hardwired 155910978b7dSBarry Smith into a code by calls to `TaoMonitorSet()`, but does not cancel those 1560a7e14dcfSSatish Balay set via the options database 1561a7e14dcfSSatish Balay 1562a7e14dcfSSatish Balay Level: advanced 1563a7e14dcfSSatish Balay 156447450a7bSBarry Smith Note: 156547450a7bSBarry Smith There is no way to clear one specific monitor from a `Tao` object. 156647450a7bSBarry Smith 156710978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1568a7e14dcfSSatish Balay @*/ 1569b2dc45ddSPierre Jolivet PetscErrorCode TaoMonitorCancel(Tao tao) 1570d71ae5a4SJacob Faibussowitsch { 1571a7e14dcfSSatish Balay PetscInt i; 157247a47007SBarry Smith 1573a7e14dcfSSatish Balay PetscFunctionBegin; 1574441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1575a7e14dcfSSatish Balay for (i = 0; i < tao->numbermonitors; i++) { 157648a46eb9SPierre Jolivet if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i])); 1577a7e14dcfSSatish Balay } 1578a7e14dcfSSatish Balay tao->numbermonitors = 0; 15793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1580a7e14dcfSSatish Balay } 1581a7e14dcfSSatish Balay 15827fab98ebSJason Sarich /*@ 158347450a7bSBarry Smith TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()` 1584a7e14dcfSSatish Balay 1585c3339decSBarry Smith Collective 1586a7e14dcfSSatish Balay 1587a7e14dcfSSatish Balay Input Parameters: 158847450a7bSBarry Smith + tao - the `Tao` context 158947450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1590a7e14dcfSSatish Balay 159147450a7bSBarry Smith Options Database Key: 1592147403d9SBarry Smith . -tao_monitor - turn on default monitoring 1593a7e14dcfSSatish Balay 1594a7e14dcfSSatish Balay Level: advanced 1595a7e14dcfSSatish Balay 159647450a7bSBarry Smith Note: 159747450a7bSBarry Smith This monitor prints the function value and gradient 159847450a7bSBarry Smith norm at each iteration. 159947450a7bSBarry Smith 160010978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1601a7e14dcfSSatish Balay @*/ 1602*2a8381b2SBarry Smith PetscErrorCode TaoMonitorDefault(Tao tao, PetscCtx ctx) 1603d71ae5a4SJacob Faibussowitsch { 160463b15415SAlp Dener PetscInt its, tabs; 1605a7e14dcfSSatish Balay PetscReal fct, gnorm; 16068163d661SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 160747a47007SBarry Smith 1608a7e14dcfSSatish Balay PetscFunctionBegin; 160994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16108163d661SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1611a7e14dcfSSatish Balay its = tao->niter; 1612a7e14dcfSSatish Balay fct = tao->fc; 1613a7e14dcfSSatish Balay gnorm = tao->residual; 16149566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1616494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 16179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1618494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 161963b15415SAlp Dener } 162063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 16219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 162208d19d1fSJason Sarich if (gnorm >= PETSC_INFINITY) { 162376c63389SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: infinity \n")); 162408d19d1fSJason Sarich } else { 16259566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 162608d19d1fSJason Sarich } 16279566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1629a7e14dcfSSatish Balay } 1630a7e14dcfSSatish Balay 16317fab98ebSJason Sarich /*@ 163210978b7dSBarry Smith TaoMonitorGlobalization - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method. 16338d5ead36SAlp Dener 1634c3339decSBarry Smith Collective 16358d5ead36SAlp Dener 16368d5ead36SAlp Dener Input Parameters: 163747450a7bSBarry Smith + tao - the `Tao` context 163847450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 16398d5ead36SAlp Dener 164047450a7bSBarry Smith Options Database Key: 164110978b7dSBarry Smith . -tao_monitor_globalization - turn on monitoring with globalization information 16428d5ead36SAlp Dener 16438d5ead36SAlp Dener Level: advanced 16448d5ead36SAlp Dener 164547450a7bSBarry Smith Note: 164647450a7bSBarry Smith This monitor prints the function value and gradient norm at each 164747450a7bSBarry Smith iteration, as well as the step size and trust radius. Note that the 164847450a7bSBarry Smith step size and trust radius may be the same for some algorithms. 164947450a7bSBarry Smith 165010978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 16518d5ead36SAlp Dener @*/ 1652*2a8381b2SBarry Smith PetscErrorCode TaoMonitorGlobalization(Tao tao, PetscCtx ctx) 1653d71ae5a4SJacob Faibussowitsch { 16548d5ead36SAlp Dener PetscInt its, tabs; 16558d5ead36SAlp Dener PetscReal fct, gnorm, stp, tr; 16568d5ead36SAlp Dener PetscViewer viewer = (PetscViewer)ctx; 16578d5ead36SAlp Dener 16588d5ead36SAlp Dener PetscFunctionBegin; 165994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16608d5ead36SAlp Dener PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 16618d5ead36SAlp Dener its = tao->niter; 16628d5ead36SAlp Dener fct = tao->fc; 16638d5ead36SAlp Dener gnorm = tao->residual; 16648d5ead36SAlp Dener stp = tao->step; 16658d5ead36SAlp Dener tr = tao->trust; 16669566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1668494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 16699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1670494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 16718d5ead36SAlp Dener } 167263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 16739566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 16748d5ead36SAlp Dener if (gnorm >= PETSC_INFINITY) { 16759566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf,")); 16768d5ead36SAlp Dener } else { 16779566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g,", (double)gnorm)); 16788d5ead36SAlp Dener } 16799566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Step: %g, Trust: %g\n", (double)stp, (double)tr)); 16809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16828d5ead36SAlp Dener } 16838d5ead36SAlp Dener 16848d5ead36SAlp Dener /*@ 168510978b7dSBarry Smith TaoMonitorDefaultShort - Routine for monitoring progress of `TaoSolve()` that displays fewer digits than `TaoMonitorDefault()` 1686a7e14dcfSSatish Balay 1687c3339decSBarry Smith Collective 1688a7e14dcfSSatish Balay 1689a7e14dcfSSatish Balay Input Parameters: 169047450a7bSBarry Smith + tao - the `Tao` context 169147450a7bSBarry Smith - ctx - `PetscViewer` context of type `PETSCVIEWERASCII` 1692a7e14dcfSSatish Balay 169347450a7bSBarry Smith Options Database Key: 169410978b7dSBarry Smith . -tao_monitor_short - turn on default short monitoring 1695a7e14dcfSSatish Balay 1696a7e14dcfSSatish Balay Level: advanced 1697a7e14dcfSSatish Balay 169847450a7bSBarry Smith Note: 169947450a7bSBarry Smith Same as `TaoMonitorDefault()` except 170047450a7bSBarry Smith it prints fewer digits of the residual as the residual gets smaller. 170147450a7bSBarry Smith This is because the later digits are meaningless and are often 170247450a7bSBarry Smith different on different machines; by using this routine different 170347450a7bSBarry Smith machines will usually generate the same output. 170447450a7bSBarry Smith 170510978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1706a7e14dcfSSatish Balay @*/ 1707*2a8381b2SBarry Smith PetscErrorCode TaoMonitorDefaultShort(Tao tao, PetscCtx ctx) 1708d71ae5a4SJacob Faibussowitsch { 17092c9b8b83SAlp Dener PetscInt its, tabs; 1710a7e14dcfSSatish Balay PetscReal fct, gnorm; 17114d4332d5SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 171247a47007SBarry Smith 1713a7e14dcfSSatish Balay PetscFunctionBegin; 171494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 17154d4332d5SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1716a7e14dcfSSatish Balay its = tao->niter; 1717a7e14dcfSSatish Balay fct = tao->fc; 1718a7e14dcfSSatish Balay gnorm = tao->residual; 17199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 17209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 172163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %3" PetscInt_FMT ",", its)); 17229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value %g,", (double)fct)); 1723d393f493SBarry Smith if (gnorm >= PETSC_INFINITY) { 172476c63389SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: infinity \n")); 172508d19d1fSJason Sarich } else if (gnorm > 1.e-6) { 17269566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 1727a7e14dcfSSatish Balay } else if (gnorm > 1.e-11) { 17289566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-6 \n")); 1729a7e14dcfSSatish Balay } else { 17309566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-11 \n")); 1731a7e14dcfSSatish Balay } 17329566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 17333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1734a7e14dcfSSatish Balay } 1735a7e14dcfSSatish Balay 17367fab98ebSJason Sarich /*@ 173710978b7dSBarry Smith TaoMonitorConstraintNorm - same as `TaoMonitorDefault()` except 173847450a7bSBarry Smith it prints the norm of the constraint function. 1739a7e14dcfSSatish Balay 1740c3339decSBarry Smith Collective 1741a7e14dcfSSatish Balay 1742a7e14dcfSSatish Balay Input Parameters: 174347450a7bSBarry Smith + tao - the `Tao` context 174447450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1745a7e14dcfSSatish Balay 174647450a7bSBarry Smith Options Database Key: 174710978b7dSBarry Smith . -tao_monitor_constraint_norm - monitor the constraints 1748a7e14dcfSSatish Balay 1749a7e14dcfSSatish Balay Level: advanced 1750a7e14dcfSSatish Balay 175110978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefault()`, `TaoMonitorSet()` 1752a7e14dcfSSatish Balay @*/ 1753*2a8381b2SBarry Smith PetscErrorCode TaoMonitorConstraintNorm(Tao tao, PetscCtx ctx) 1754d71ae5a4SJacob Faibussowitsch { 17552c9b8b83SAlp Dener PetscInt its, tabs; 1756a7e14dcfSSatish Balay PetscReal fct, gnorm; 1757d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1758a7e14dcfSSatish Balay 1759a7e14dcfSSatish Balay PetscFunctionBegin; 176094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1761d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1762a7e14dcfSSatish Balay its = tao->niter; 1763a7e14dcfSSatish Balay fct = tao->fc; 1764a7e14dcfSSatish Balay gnorm = tao->residual; 17659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 17669566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 176763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", its)); 17689566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 17699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g ", (double)gnorm)); 17709566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Constraint: %g \n", (double)tao->cnorm)); 17719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 17723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1773a7e14dcfSSatish Balay } 1774a7e14dcfSSatish Balay 1775a7e14dcfSSatish Balay /*@C 177610978b7dSBarry Smith TaoMonitorSolution - Views the solution at each iteration of `TaoSolve()` 1777a7e14dcfSSatish Balay 1778c3339decSBarry Smith Collective 1779a7e14dcfSSatish Balay 1780a7e14dcfSSatish Balay Input Parameters: 178147450a7bSBarry Smith + tao - the `Tao` context 178247450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1783a7e14dcfSSatish Balay 178447450a7bSBarry Smith Options Database Key: 178510978b7dSBarry Smith . -tao_monitor_solution - view the solution 1786a7e14dcfSSatish Balay 1787a7e14dcfSSatish Balay Level: advanced 1788a7e14dcfSSatish Balay 178910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1790a7e14dcfSSatish Balay @*/ 1791*2a8381b2SBarry Smith PetscErrorCode TaoMonitorSolution(Tao tao, PetscCtx ctx) 1792d71ae5a4SJacob Faibussowitsch { 1793feb237baSPierre Jolivet PetscViewer viewer = (PetscViewer)ctx; 179447a47007SBarry Smith 1795a7e14dcfSSatish Balay PetscFunctionBegin; 179694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1797d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 17989566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, viewer)); 17993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1800a7e14dcfSSatish Balay } 1801a7e14dcfSSatish Balay 1802a7e14dcfSSatish Balay /*@C 180310978b7dSBarry Smith TaoMonitorGradient - Views the gradient at each iteration of `TaoSolve()` 1804a7e14dcfSSatish Balay 1805c3339decSBarry Smith Collective 1806a7e14dcfSSatish Balay 1807a7e14dcfSSatish Balay Input Parameters: 180847450a7bSBarry Smith + tao - the `Tao` context 180947450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1810a7e14dcfSSatish Balay 181147450a7bSBarry Smith Options Database Key: 181210978b7dSBarry Smith . -tao_monitor_gradient - view the gradient at each iteration 1813a7e14dcfSSatish Balay 1814a7e14dcfSSatish Balay Level: advanced 1815a7e14dcfSSatish Balay 181610978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1817a7e14dcfSSatish Balay @*/ 1818*2a8381b2SBarry Smith PetscErrorCode TaoMonitorGradient(Tao tao, PetscCtx ctx) 1819d71ae5a4SJacob Faibussowitsch { 1820d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 182147a47007SBarry Smith 1822a7e14dcfSSatish Balay PetscFunctionBegin; 182394511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1824d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18259566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, viewer)); 18263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1827a7e14dcfSSatish Balay } 1828a7e14dcfSSatish Balay 1829a7e14dcfSSatish Balay /*@C 183010978b7dSBarry Smith TaoMonitorStep - Views the step-direction at each iteration of `TaoSolve()` 1831a7e14dcfSSatish Balay 1832c3339decSBarry Smith Collective 1833a7e14dcfSSatish Balay 1834a7e14dcfSSatish Balay Input Parameters: 183547450a7bSBarry Smith + tao - the `Tao` context 183647450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1837a7e14dcfSSatish Balay 183847450a7bSBarry Smith Options Database Key: 183910978b7dSBarry Smith . -tao_monitor_step - view the step vector at each iteration 1840a7e14dcfSSatish Balay 1841a7e14dcfSSatish Balay Level: advanced 1842a7e14dcfSSatish Balay 184310978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1844a7e14dcfSSatish Balay @*/ 1845*2a8381b2SBarry Smith PetscErrorCode TaoMonitorStep(Tao tao, PetscCtx ctx) 1846d71ae5a4SJacob Faibussowitsch { 1847d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1848d393f493SBarry Smith 1849a7e14dcfSSatish Balay PetscFunctionBegin; 185094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1851d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18529566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 18533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1854a7e14dcfSSatish Balay } 1855a7e14dcfSSatish Balay 1856a7e14dcfSSatish Balay /*@C 185710978b7dSBarry Smith TaoMonitorSolutionDraw - Plots the solution at each iteration of `TaoSolve()` 1858a7e14dcfSSatish Balay 1859c3339decSBarry Smith Collective 1860a7e14dcfSSatish Balay 1861a7e14dcfSSatish Balay Input Parameters: 186247450a7bSBarry Smith + tao - the `Tao` context 186365ba42b6SBarry Smith - ctx - `TaoMonitorDraw` context 1864a7e14dcfSSatish Balay 186547450a7bSBarry Smith Options Database Key: 186610978b7dSBarry Smith . -tao_monitor_solution_draw - draw the solution at each iteration 1867a7e14dcfSSatish Balay 1868a7e14dcfSSatish Balay Level: advanced 1869a7e14dcfSSatish Balay 1870a81c3919SBarry Smith Note: 1871a81c3919SBarry Smith The context created by `TaoMonitorDrawCtxCreate()`, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()` 1872a81c3919SBarry Smith are passed to `TaoMonitorSet()` to monitor the solution graphically. 1873a81c3919SBarry Smith 1874a81c3919SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSolution()`, `TaoMonitorSet()`, `TaoMonitorGradientDraw()`, `TaoMonitorDrawCtxCreate()`, 1875a81c3919SBarry Smith `TaoMonitorDrawCtxDestroy()` 1876a7e14dcfSSatish Balay @*/ 1877*2a8381b2SBarry Smith PetscErrorCode TaoMonitorSolutionDraw(Tao tao, PetscCtx ctx) 1878d71ae5a4SJacob Faibussowitsch { 1879e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 188047a47007SBarry Smith 1881a7e14dcfSSatish Balay PetscFunctionBegin; 188294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 18833ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 18849566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, ictx->viewer)); 18853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1886a7e14dcfSSatish Balay } 1887a7e14dcfSSatish Balay 1888a7e14dcfSSatish Balay /*@C 188910978b7dSBarry Smith TaoMonitorGradientDraw - Plots the gradient at each iteration of `TaoSolve()` 1890a7e14dcfSSatish Balay 1891c3339decSBarry Smith Collective 1892a7e14dcfSSatish Balay 1893a7e14dcfSSatish Balay Input Parameters: 189447450a7bSBarry Smith + tao - the `Tao` context 189565ba42b6SBarry Smith - ctx - `PetscViewer` context 1896a7e14dcfSSatish Balay 189747450a7bSBarry Smith Options Database Key: 189810978b7dSBarry Smith . -tao_monitor_gradient_draw - draw the gradient at each iteration 1899a7e14dcfSSatish Balay 1900a7e14dcfSSatish Balay Level: advanced 1901a7e14dcfSSatish Balay 190210978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorGradient()`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw()` 1903a7e14dcfSSatish Balay @*/ 1904*2a8381b2SBarry Smith PetscErrorCode TaoMonitorGradientDraw(Tao tao, PetscCtx ctx) 1905d71ae5a4SJacob Faibussowitsch { 1906e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 190747a47007SBarry Smith 1908a7e14dcfSSatish Balay PetscFunctionBegin; 190994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 19103ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 19119566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, ictx->viewer)); 19123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1913a7e14dcfSSatish Balay } 1914a7e14dcfSSatish Balay 1915a7e14dcfSSatish Balay /*@C 191610978b7dSBarry Smith TaoMonitorStepDraw - Plots the step direction at each iteration of `TaoSolve()` 1917a7e14dcfSSatish Balay 1918c3339decSBarry Smith Collective 1919a7e14dcfSSatish Balay 1920a7e14dcfSSatish Balay Input Parameters: 192147450a7bSBarry Smith + tao - the `Tao` context 192247450a7bSBarry Smith - ctx - the `PetscViewer` context 1923a7e14dcfSSatish Balay 192447450a7bSBarry Smith Options Database Key: 192510978b7dSBarry Smith . -tao_monitor_step_draw - draw the step direction at each iteration 1926a7e14dcfSSatish Balay 1927a7e14dcfSSatish Balay Level: advanced 1928a7e14dcfSSatish Balay 192910978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorSolutionDraw` 1930a7e14dcfSSatish Balay @*/ 1931*2a8381b2SBarry Smith PetscErrorCode TaoMonitorStepDraw(Tao tao, PetscCtx ctx) 1932d71ae5a4SJacob Faibussowitsch { 193394511df7SStefano Zampini PetscViewer viewer = (PetscViewer)ctx; 193447a47007SBarry Smith 1935a7e14dcfSSatish Balay PetscFunctionBegin; 193694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 193794511df7SStefano Zampini PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 19389566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 19393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1940a7e14dcfSSatish Balay } 1941a7e14dcfSSatish Balay 1942a7e14dcfSSatish Balay /*@C 194310978b7dSBarry Smith TaoMonitorResidual - Views the least-squares residual at each iteration of `TaoSolve()` 1944a7e14dcfSSatish Balay 1945c3339decSBarry Smith Collective 1946a7e14dcfSSatish Balay 1947a7e14dcfSSatish Balay Input Parameters: 194847450a7bSBarry Smith + tao - the `Tao` context 194947450a7bSBarry Smith - ctx - the `PetscViewer` context or `NULL` 1950a7e14dcfSSatish Balay 195147450a7bSBarry Smith Options Database Key: 195210978b7dSBarry Smith . -tao_monitor_ls_residual - view the residual at each iteration 1953a7e14dcfSSatish Balay 1954a7e14dcfSSatish Balay Level: advanced 1955a7e14dcfSSatish Balay 195610978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorDefaultShort()`, `TaoMonitorSet()` 1957a7e14dcfSSatish Balay @*/ 1958*2a8381b2SBarry Smith PetscErrorCode TaoMonitorResidual(Tao tao, PetscCtx ctx) 1959d71ae5a4SJacob Faibussowitsch { 1960d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 196147a47007SBarry Smith 1962a7e14dcfSSatish Balay PetscFunctionBegin; 196394511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1964d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 19659566063dSJacob Faibussowitsch PetscCall(VecView(tao->ls_res, viewer)); 19663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1967a7e14dcfSSatish Balay } 1968a7e14dcfSSatish Balay 19697fab98ebSJason Sarich /*@ 1970a7e14dcfSSatish Balay TaoDefaultConvergenceTest - Determines whether the solver should continue iterating 1971a7e14dcfSSatish Balay or terminate. 1972a7e14dcfSSatish Balay 1973c3339decSBarry Smith Collective 1974a7e14dcfSSatish Balay 1975a7e14dcfSSatish Balay Input Parameters: 197647450a7bSBarry Smith + tao - the `Tao` context 1977a7e14dcfSSatish Balay - dummy - unused dummy context 1978a7e14dcfSSatish Balay 197947450a7bSBarry Smith Level: developer 198047450a7bSBarry Smith 1981a7e14dcfSSatish Balay Notes: 1982a7e14dcfSSatish Balay This routine checks the residual in the optimality conditions, the 1983a7e14dcfSSatish Balay relative residual in the optimity conditions, the number of function 1984a7e14dcfSSatish Balay evaluations, and the function value to test convergence. Some 1985a7e14dcfSSatish Balay solvers may use different convergence routines. 1986a7e14dcfSSatish Balay 19871cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()` 1988a7e14dcfSSatish Balay @*/ 1989d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy) 1990d71ae5a4SJacob Faibussowitsch { 1991a7e14dcfSSatish Balay PetscInt niter = tao->niter, nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 1992a7e14dcfSSatish Balay PetscInt max_funcs = tao->max_funcs; 1993a7e14dcfSSatish Balay PetscReal gnorm = tao->residual, gnorm0 = tao->gnorm0; 1994a7e14dcfSSatish Balay PetscReal f = tao->fc, steptol = tao->steptol, trradius = tao->step; 1995a7e14dcfSSatish Balay PetscReal gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol; 1996e52336cbSBarry Smith PetscReal catol = tao->catol, crtol = tao->crtol; 1997e52336cbSBarry Smith PetscReal fmin = tao->fmin, cnorm = tao->cnorm; 1998e4cb33bbSBarry Smith TaoConvergedReason reason = tao->reason; 1999a7e14dcfSSatish Balay 2000a7e14dcfSSatish Balay PetscFunctionBegin; 2001441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20023ba16761SJacob Faibussowitsch if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS); 2003a7e14dcfSSatish Balay 2004a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(f)) { 200576c63389SBarry Smith PetscCall(PetscInfo(tao, "Failed to converged, function value is infinity or NaN\n")); 2006a7e14dcfSSatish Balay reason = TAO_DIVERGED_NAN; 2007a7e14dcfSSatish Balay } else if (f <= fmin && cnorm <= catol) { 20089566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin)); 2009a7e14dcfSSatish Balay reason = TAO_CONVERGED_MINF; 2010a7e14dcfSSatish Balay } else if (gnorm <= gatol && cnorm <= catol) { 20119566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol)); 2012a7e14dcfSSatish Balay reason = TAO_CONVERGED_GATOL; 2013a7e14dcfSSatish Balay } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) { 20149566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol)); 2015a7e14dcfSSatish Balay reason = TAO_CONVERGED_GRTOL; 2016e1d16bb9SBarry Smith } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) { 20179566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol)); 2018a7e14dcfSSatish Balay reason = TAO_CONVERGED_GTTOL; 2019606f75f6SBarry Smith } else if (max_funcs != PETSC_UNLIMITED && nfuncs > max_funcs) { 20209566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs)); 2021a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXFCN; 2022a7e14dcfSSatish Balay } else if (tao->lsflag != 0) { 20239566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tao Line Search failure.\n")); 2024a7e14dcfSSatish Balay reason = TAO_DIVERGED_LS_FAILURE; 2025a7e14dcfSSatish Balay } else if (trradius < steptol && niter > 0) { 20269566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol)); 2027a7e14dcfSSatish Balay reason = TAO_CONVERGED_STEPTOL; 2028e031d6f5SAlp Dener } else if (niter >= tao->max_it) { 202963a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it)); 2030a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXITS; 2031a7e14dcfSSatish Balay } else { 2032a7e14dcfSSatish Balay reason = TAO_CONTINUE_ITERATING; 2033a7e14dcfSSatish Balay } 2034a7e14dcfSSatish Balay tao->reason = reason; 20353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2036a7e14dcfSSatish Balay } 2037a7e14dcfSSatish Balay 2038cc4c1da9SBarry Smith /*@ 2039a7e14dcfSSatish Balay TaoSetOptionsPrefix - Sets the prefix used for searching for all 204065ba42b6SBarry Smith Tao options in the database. 2041a7e14dcfSSatish Balay 2042c3339decSBarry Smith Logically Collective 2043a7e14dcfSSatish Balay 2044a7e14dcfSSatish Balay Input Parameters: 204547450a7bSBarry Smith + tao - the `Tao` context 2046e056e8ceSJacob Faibussowitsch - p - the prefix string to prepend to all Tao option requests 2047a7e14dcfSSatish Balay 20482fe279fdSBarry Smith Level: advanced 20492fe279fdSBarry Smith 2050a7e14dcfSSatish Balay Notes: 2051a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2052a7e14dcfSSatish Balay The first character of all runtime options is AUTOMATICALLY the hyphen. 2053a7e14dcfSSatish Balay 2054a7e14dcfSSatish Balay For example, to distinguish between the runtime options for two 205565ba42b6SBarry Smith different Tao solvers, one could call 2056a7e14dcfSSatish Balay .vb 2057a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao1,"sys1_") 2058a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao2,"sys2_") 2059a7e14dcfSSatish Balay .ve 2060a7e14dcfSSatish Balay 2061a7e14dcfSSatish Balay This would enable use of different options for each system, such as 2062a7e14dcfSSatish Balay .vb 20639fa2b5dcSStefano Zampini -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3 20649fa2b5dcSStefano Zampini -sys2_tao_method lmvm -sys2_tao_grtol 1.e-4 2065a7e14dcfSSatish Balay .ve 2066a7e14dcfSSatish Balay 20671cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()` 2068a7e14dcfSSatish Balay @*/ 2069d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[]) 2070d71ae5a4SJacob Faibussowitsch { 2071a7e14dcfSSatish Balay PetscFunctionBegin; 207294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20739566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p)); 20741baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p)); 20751baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p)); 20763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2077a7e14dcfSSatish Balay } 2078a7e14dcfSSatish Balay 2079cc4c1da9SBarry Smith /*@ 208047450a7bSBarry Smith TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database. 2081a7e14dcfSSatish Balay 2082c3339decSBarry Smith Logically Collective 2083a7e14dcfSSatish Balay 2084a7e14dcfSSatish Balay Input Parameters: 208547450a7bSBarry Smith + tao - the `Tao` solver context 2086e056e8ceSJacob Faibussowitsch - p - the prefix string to prepend to all `Tao` option requests 2087a7e14dcfSSatish Balay 20882fe279fdSBarry Smith Level: advanced 20892fe279fdSBarry Smith 209065ba42b6SBarry Smith Note: 2091a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 209265ba42b6SBarry Smith The first character of all runtime options is automatically the hyphen. 2093a7e14dcfSSatish Balay 20941cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()` 2095a7e14dcfSSatish Balay @*/ 2096d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[]) 2097d71ae5a4SJacob Faibussowitsch { 2098a7e14dcfSSatish Balay PetscFunctionBegin; 209994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 21009566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p)); 21011baa6e33SBarry Smith if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p)); 21021baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p)); 21033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2104a7e14dcfSSatish Balay } 2105a7e14dcfSSatish Balay 2106cc4c1da9SBarry Smith /*@ 2107a7e14dcfSSatish Balay TaoGetOptionsPrefix - Gets the prefix used for searching for all 210865ba42b6SBarry Smith Tao options in the database 2109a7e14dcfSSatish Balay 2110a7e14dcfSSatish Balay Not Collective 2111a7e14dcfSSatish Balay 211247450a7bSBarry Smith Input Parameter: 211347450a7bSBarry Smith . tao - the `Tao` context 2114a7e14dcfSSatish Balay 211547450a7bSBarry Smith Output Parameter: 2116e056e8ceSJacob Faibussowitsch . p - pointer to the prefix string used is returned 2117a7e14dcfSSatish Balay 2118a7e14dcfSSatish Balay Level: advanced 2119a7e14dcfSSatish Balay 21201cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()` 2121a7e14dcfSSatish Balay @*/ 2122d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[]) 2123d71ae5a4SJacob Faibussowitsch { 212494511df7SStefano Zampini PetscFunctionBegin; 212594511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 21269566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p)); 21273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2128a7e14dcfSSatish Balay } 2129a7e14dcfSSatish Balay 2130cc4c1da9SBarry Smith /*@ 213147450a7bSBarry Smith TaoSetType - Sets the `TaoType` for the minimization solver. 2132a7e14dcfSSatish Balay 2133c3339decSBarry Smith Collective 2134a7e14dcfSSatish Balay 2135a7e14dcfSSatish Balay Input Parameters: 2136e056e8ceSJacob Faibussowitsch + tao - the `Tao` solver context 2137a7e14dcfSSatish Balay - type - a known method 2138a7e14dcfSSatish Balay 2139a7e14dcfSSatish Balay Options Database Key: 214058417fe7SBarry Smith . -tao_type <type> - Sets the method; use -help for a list 214158417fe7SBarry Smith of available methods (for instance, "-tao_type lmvm" or "-tao_type tron") 2142a7e14dcfSSatish Balay 2143a7e14dcfSSatish Balay Level: intermediate 2144a7e14dcfSSatish Balay 21451cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType` 2146a7e14dcfSSatish Balay @*/ 2147d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetType(Tao tao, TaoType type) 2148d71ae5a4SJacob Faibussowitsch { 2149441846f8SBarry Smith PetscErrorCode (*create_xxx)(Tao); 2150a7e14dcfSSatish Balay PetscBool issame; 2151a7e14dcfSSatish Balay 2152a7e14dcfSSatish Balay PetscFunctionBegin; 2153441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2154a7e14dcfSSatish Balay 21559566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame)); 21563ba16761SJacob Faibussowitsch if (issame) PetscFunctionReturn(PETSC_SUCCESS); 2157a7e14dcfSSatish Balay 2158835f2295SStefano Zampini PetscCall(PetscFunctionListFind(TaoList, type, &create_xxx)); 21593c859ba3SBarry Smith PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type); 2160a7e14dcfSSatish Balay 2161a7e14dcfSSatish Balay /* Destroy the existing solver information */ 2162dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, destroy); 21631baa6e33SBarry Smith PetscCall(KSPDestroy(&tao->ksp)); 21649566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&tao->linesearch)); 216543547594SStefano Zampini 216643547594SStefano Zampini /* Reinitialize type-specific function pointers in TaoOps structure */ 216783c8fe1dSLisandro Dalcin tao->ops->setup = NULL; 216843547594SStefano Zampini tao->ops->computedual = NULL; 216983c8fe1dSLisandro Dalcin tao->ops->solve = NULL; 217083c8fe1dSLisandro Dalcin tao->ops->view = NULL; 217183c8fe1dSLisandro Dalcin tao->ops->setfromoptions = NULL; 217283c8fe1dSLisandro Dalcin tao->ops->destroy = NULL; 2173a7e14dcfSSatish Balay 2174a7e14dcfSSatish Balay tao->setupcalled = PETSC_FALSE; 2175a7e14dcfSSatish Balay 21769566063dSJacob Faibussowitsch PetscCall((*create_xxx)(tao)); 21779566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type)); 21783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2179a7e14dcfSSatish Balay } 218047a47007SBarry Smith 218167be906fSBarry Smith /*@C 218247450a7bSBarry Smith TaoRegister - Adds a method to the Tao package for minimization. 2183a7e14dcfSSatish Balay 2184cc4c1da9SBarry Smith Not Collective, No Fortran Support 2185a7e14dcfSSatish Balay 2186a7e14dcfSSatish Balay Input Parameters: 2187a7e14dcfSSatish Balay + sname - name of a new user-defined solver 2188a7e14dcfSSatish Balay - func - routine to Create method context 2189a7e14dcfSSatish Balay 2190e056e8ceSJacob Faibussowitsch Example Usage: 2191a7e14dcfSSatish Balay .vb 2192441846f8SBarry Smith TaoRegister("my_solver", MySolverCreate); 2193a7e14dcfSSatish Balay .ve 2194a7e14dcfSSatish Balay 2195a7e14dcfSSatish Balay Then, your solver can be chosen with the procedural interface via 2196b44f4de4SBarry Smith .vb 2197b44f4de4SBarry Smith TaoSetType(tao, "my_solver") 2198b44f4de4SBarry Smith .ve 2199a7e14dcfSSatish Balay or at runtime via the option 2200b44f4de4SBarry Smith .vb 2201b44f4de4SBarry Smith -tao_type my_solver 2202b44f4de4SBarry Smith .ve 2203a7e14dcfSSatish Balay 2204a7e14dcfSSatish Balay Level: advanced 2205a7e14dcfSSatish Balay 220667be906fSBarry Smith Note: 220767be906fSBarry Smith `TaoRegister()` may be called multiple times to add several user-defined solvers. 220867be906fSBarry Smith 22091cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()` 221067be906fSBarry Smith @*/ 2211d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao)) 2212d71ae5a4SJacob Faibussowitsch { 2213a7e14dcfSSatish Balay PetscFunctionBegin; 22149566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 2215835f2295SStefano Zampini PetscCall(PetscFunctionListAdd(&TaoList, sname, func)); 22163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2217a7e14dcfSSatish Balay } 2218a7e14dcfSSatish Balay 2219a7e14dcfSSatish Balay /*@C 2220441846f8SBarry Smith TaoRegisterDestroy - Frees the list of minimization solvers that were 222147450a7bSBarry Smith registered by `TaoRegister()`. 2222a7e14dcfSSatish Balay 2223a7e14dcfSSatish Balay Not Collective 2224a7e14dcfSSatish Balay 2225a7e14dcfSSatish Balay Level: advanced 2226a7e14dcfSSatish Balay 22271cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()` 2228a7e14dcfSSatish Balay @*/ 2229d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegisterDestroy(void) 2230d71ae5a4SJacob Faibussowitsch { 2231a7e14dcfSSatish Balay PetscFunctionBegin; 22329566063dSJacob Faibussowitsch PetscCall(PetscFunctionListDestroy(&TaoList)); 2233441846f8SBarry Smith TaoRegisterAllCalled = PETSC_FALSE; 22343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2235a7e14dcfSSatish Balay } 223647a47007SBarry Smith 22378931d482SJason Sarich /*@ 223847450a7bSBarry Smith TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed 22398931d482SJason Sarich at this time. 22408931d482SJason Sarich 22418931d482SJason Sarich Not Collective 22428931d482SJason Sarich 22438931d482SJason Sarich Input Parameter: 224447450a7bSBarry Smith . tao - the `Tao` context 22458931d482SJason Sarich 22468931d482SJason Sarich Output Parameter: 22478931d482SJason Sarich . iter - iteration number 22488931d482SJason Sarich 22498931d482SJason Sarich Notes: 22508931d482SJason Sarich For example, during the computation of iteration 2 this would return 1. 22518931d482SJason Sarich 22528931d482SJason Sarich Level: intermediate 22538931d482SJason Sarich 22541cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()` 22558931d482SJason Sarich @*/ 2256d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter) 2257d71ae5a4SJacob Faibussowitsch { 22588931d482SJason Sarich PetscFunctionBegin; 22598931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22604f572ea9SToby Isaac PetscAssertPointer(iter, 2); 22618931d482SJason Sarich *iter = tao->niter; 22623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22638931d482SJason Sarich } 22648931d482SJason Sarich 22658931d482SJason Sarich /*@ 226647450a7bSBarry Smith TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient) 226779f5d8caSBarry Smith at this time. 226879f5d8caSBarry Smith 226979f5d8caSBarry Smith Not Collective 227079f5d8caSBarry Smith 227179f5d8caSBarry Smith Input Parameter: 227247450a7bSBarry Smith . tao - the `Tao` context 227379f5d8caSBarry Smith 227479f5d8caSBarry Smith Output Parameter: 227579f5d8caSBarry Smith . value - the current value 227679f5d8caSBarry Smith 227779f5d8caSBarry Smith Level: intermediate 227879f5d8caSBarry Smith 2279e056e8ceSJacob Faibussowitsch Developer Notes: 228067be906fSBarry Smith This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has 228147450a7bSBarry Smith a different meaning. For some reason `Tao` sometimes calls the gradient the residual. 228279f5d8caSBarry Smith 22831cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()` 228479f5d8caSBarry Smith @*/ 2285d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value) 2286d71ae5a4SJacob Faibussowitsch { 228779f5d8caSBarry Smith PetscFunctionBegin; 228879f5d8caSBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22894f572ea9SToby Isaac PetscAssertPointer(value, 2); 229079f5d8caSBarry Smith *value = tao->residual; 22913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 229279f5d8caSBarry Smith } 229379f5d8caSBarry Smith 229479f5d8caSBarry Smith /*@ 22958931d482SJason Sarich TaoSetIterationNumber - Sets the current iteration number. 22968931d482SJason Sarich 2297c3339decSBarry Smith Logically Collective 22988931d482SJason Sarich 2299d8d19677SJose E. Roman Input Parameters: 230047450a7bSBarry Smith + tao - the `Tao` context 2301a2b725a8SWilliam Gropp - iter - iteration number 23028931d482SJason Sarich 23038931d482SJason Sarich Level: developer 23048931d482SJason Sarich 23051cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 23068931d482SJason Sarich @*/ 2307d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter) 2308d71ae5a4SJacob Faibussowitsch { 23098931d482SJason Sarich PetscFunctionBegin; 23108931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 231194511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 23129566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 23138931d482SJason Sarich tao->niter = iter; 23149566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 23153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23168931d482SJason Sarich } 23178931d482SJason Sarich 23188931d482SJason Sarich /*@ 231947450a7bSBarry Smith TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations 23208931d482SJason Sarich completed. This number keeps accumulating if multiple solves 232147450a7bSBarry Smith are called with the `Tao` object. 23228931d482SJason Sarich 23238931d482SJason Sarich Not Collective 23248931d482SJason Sarich 23258931d482SJason Sarich Input Parameter: 232647450a7bSBarry Smith . tao - the `Tao` context 23278931d482SJason Sarich 23288931d482SJason Sarich Output Parameter: 232947450a7bSBarry Smith . iter - number of iterations 23308931d482SJason Sarich 23318931d482SJason Sarich Level: intermediate 23328931d482SJason Sarich 233320f4b53cSBarry Smith Note: 233467be906fSBarry Smith The total iteration count is updated after each solve, if there is a current 233547450a7bSBarry Smith `TaoSolve()` in progress then those iterations are not included in the count 233667be906fSBarry Smith 23371cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 23388931d482SJason Sarich @*/ 2339d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter) 2340d71ae5a4SJacob Faibussowitsch { 23418931d482SJason Sarich PetscFunctionBegin; 23428931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 23434f572ea9SToby Isaac PetscAssertPointer(iter, 2); 23448931d482SJason Sarich *iter = tao->ntotalits; 23453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23468931d482SJason Sarich } 23478931d482SJason Sarich 23488931d482SJason Sarich /*@ 23498931d482SJason Sarich TaoSetTotalIterationNumber - Sets the current total iteration number. 23508931d482SJason Sarich 2351c3339decSBarry Smith Logically Collective 23528931d482SJason Sarich 2353d8d19677SJose E. Roman Input Parameters: 235447450a7bSBarry Smith + tao - the `Tao` context 235547450a7bSBarry Smith - iter - the iteration number 23568931d482SJason Sarich 23578931d482SJason Sarich Level: developer 23588931d482SJason Sarich 23591cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetLinearSolveIterations()` 23608931d482SJason Sarich @*/ 2361d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter) 2362d71ae5a4SJacob Faibussowitsch { 23638931d482SJason Sarich PetscFunctionBegin; 23648931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 236594511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 23669566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 23678931d482SJason Sarich tao->ntotalits = iter; 23689566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 23693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23708931d482SJason Sarich } 23718931d482SJason Sarich 2372a7e14dcfSSatish Balay /*@ 237347450a7bSBarry Smith TaoSetConvergedReason - Sets the termination flag on a `Tao` object 2374a7e14dcfSSatish Balay 2375c3339decSBarry Smith Logically Collective 2376a7e14dcfSSatish Balay 2377a7e14dcfSSatish Balay Input Parameters: 237847450a7bSBarry Smith + tao - the `Tao` context 237947450a7bSBarry Smith - reason - the `TaoConvergedReason` 2380a7e14dcfSSatish Balay 2381a7e14dcfSSatish Balay Level: intermediate 2382a7e14dcfSSatish Balay 23831cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason` 2384a7e14dcfSSatish Balay @*/ 2385d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason) 2386d71ae5a4SJacob Faibussowitsch { 2387a7e14dcfSSatish Balay PetscFunctionBegin; 238894511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 238994511df7SStefano Zampini PetscValidLogicalCollectiveEnum(tao, reason, 2); 2390a7e14dcfSSatish Balay tao->reason = reason; 23913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2392a7e14dcfSSatish Balay } 2393a7e14dcfSSatish Balay 2394a7e14dcfSSatish Balay /*@ 239547450a7bSBarry Smith TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped. 2396a7e14dcfSSatish Balay 2397a7e14dcfSSatish Balay Not Collective 2398a7e14dcfSSatish Balay 2399a7e14dcfSSatish Balay Input Parameter: 240047450a7bSBarry Smith . tao - the `Tao` solver context 2401a7e14dcfSSatish Balay 2402a7e14dcfSSatish Balay Output Parameter: 240347450a7bSBarry Smith . reason - value of `TaoConvergedReason` 2404a7e14dcfSSatish Balay 2405a7e14dcfSSatish Balay Level: intermediate 2406a7e14dcfSSatish Balay 24071cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()` 2408a7e14dcfSSatish Balay @*/ 2409d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason) 2410d71ae5a4SJacob Faibussowitsch { 2411a7e14dcfSSatish Balay PetscFunctionBegin; 2412441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 24134f572ea9SToby Isaac PetscAssertPointer(reason, 2); 2414a7e14dcfSSatish Balay *reason = tao->reason; 24153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2416a7e14dcfSSatish Balay } 2417a7e14dcfSSatish Balay 2418a7e14dcfSSatish Balay /*@ 2419a7e14dcfSSatish Balay TaoGetSolutionStatus - Get the current iterate, objective value, 242047450a7bSBarry Smith residual, infeasibility, and termination from a `Tao` object 2421a7e14dcfSSatish Balay 2422a7e14dcfSSatish Balay Not Collective 2423a7e14dcfSSatish Balay 2424f899ff85SJose E. Roman Input Parameter: 242547450a7bSBarry Smith . tao - the `Tao` context 2426a7e14dcfSSatish Balay 2427a7e14dcfSSatish Balay Output Parameters: 2428e056e8ceSJacob Faibussowitsch + its - the current iterate number (>=0) 2429a7e14dcfSSatish Balay . f - the current function value 243058417fe7SBarry Smith . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality. 2431a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2432a7e14dcfSSatish Balay . xdiff - the step length or trust region radius of the most recent iterate. 243365ba42b6SBarry Smith - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING` 2434a7e14dcfSSatish Balay 2435a7e14dcfSSatish Balay Level: intermediate 2436a7e14dcfSSatish Balay 243765ba42b6SBarry Smith Notes: 243865ba42b6SBarry Smith Tao returns the values set by the solvers in the routine `TaoMonitor()`. 2439a7e14dcfSSatish Balay 244067be906fSBarry Smith If any of the output arguments are set to `NULL`, no corresponding value will be returned. 2441a7e14dcfSSatish Balay 24421cc06b55SBarry Smith .seealso: [](ch_tao), `TaoMonitor()`, `TaoGetConvergedReason()` 2443a7e14dcfSSatish Balay @*/ 2444d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason) 2445d71ae5a4SJacob Faibussowitsch { 2446a7e14dcfSSatish Balay PetscFunctionBegin; 244794511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2448a7e14dcfSSatish Balay if (its) *its = tao->niter; 2449a7e14dcfSSatish Balay if (f) *f = tao->fc; 2450a7e14dcfSSatish Balay if (gnorm) *gnorm = tao->residual; 2451a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->cnorm; 2452a7e14dcfSSatish Balay if (reason) *reason = tao->reason; 2453a7e14dcfSSatish Balay if (xdiff) *xdiff = tao->step; 24543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2455a7e14dcfSSatish Balay } 2456a7e14dcfSSatish Balay 2457cc4c1da9SBarry Smith /*@ 245847450a7bSBarry Smith TaoGetType - Gets the current `TaoType` being used in the `Tao` object 2459a7e14dcfSSatish Balay 2460a7e14dcfSSatish Balay Not Collective 2461a7e14dcfSSatish Balay 2462a7e14dcfSSatish Balay Input Parameter: 246347450a7bSBarry Smith . tao - the `Tao` solver context 2464a7e14dcfSSatish Balay 2465a7e14dcfSSatish Balay Output Parameter: 246647450a7bSBarry Smith . type - the `TaoType` 2467a7e14dcfSSatish Balay 2468a7e14dcfSSatish Balay Level: intermediate 2469a7e14dcfSSatish Balay 24701cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoType`, `TaoSetType()` 2471a7e14dcfSSatish Balay @*/ 2472d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetType(Tao tao, TaoType *type) 2473d71ae5a4SJacob Faibussowitsch { 2474a7e14dcfSSatish Balay PetscFunctionBegin; 2475441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 24764f572ea9SToby Isaac PetscAssertPointer(type, 2); 2477a7e14dcfSSatish Balay *type = ((PetscObject)tao)->type_name; 24783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2479a7e14dcfSSatish Balay } 2480a7e14dcfSSatish Balay 2481a7e14dcfSSatish Balay /*@C 2482a7e14dcfSSatish Balay TaoMonitor - Monitor the solver and the current solution. This 2483a7e14dcfSSatish Balay routine will record the iteration number and residual statistics, 2484670c031eSStefano Zampini and call any monitors specified by the user. 2485a7e14dcfSSatish Balay 2486a7e14dcfSSatish Balay Input Parameters: 248747450a7bSBarry Smith + tao - the `Tao` context 2488a7e14dcfSSatish Balay . its - the current iterate number (>=0) 2489a7e14dcfSSatish Balay . f - the current objective function value 24901cb3f120SPierre Jolivet . res - the gradient norm, square root of the duality gap, or other measure indicating distance from optimality. This measure will be recorded and 2491a7e14dcfSSatish Balay used for some termination tests. 2492a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2493a7e14dcfSSatish Balay - steplength - multiple of the step direction added to the previous iterate. 2494a7e14dcfSSatish Balay 2495a7e14dcfSSatish Balay Options Database Key: 2496a7e14dcfSSatish Balay . -tao_monitor - Use the default monitor, which prints statistics to standard output 2497a7e14dcfSSatish Balay 2498a7e14dcfSSatish Balay Level: developer 2499a7e14dcfSSatish Balay 250010978b7dSBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoMonitorSet()` 2501a7e14dcfSSatish Balay @*/ 2502d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength) 2503d71ae5a4SJacob Faibussowitsch { 250447a47007SBarry Smith PetscInt i; 250547a47007SBarry Smith 2506a7e14dcfSSatish Balay PetscFunctionBegin; 2507441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2508a7e14dcfSSatish Balay tao->fc = f; 2509a7e14dcfSSatish Balay tao->residual = res; 2510a7e14dcfSSatish Balay tao->cnorm = cnorm; 2511a7e14dcfSSatish Balay tao->step = steplength; 2512e52336cbSBarry Smith if (!its) { 251394511df7SStefano Zampini tao->cnorm0 = cnorm; 251494511df7SStefano Zampini tao->gnorm0 = res; 2515a7e14dcfSSatish Balay } 2516bfdcf5a2SStefano Zampini PetscCall(VecLockReadPush(tao->solution)); 251748a46eb9SPierre Jolivet for (i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i])); 2518bfdcf5a2SStefano Zampini PetscCall(VecLockReadPop(tao->solution)); 25193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2520a7e14dcfSSatish Balay } 2521a7e14dcfSSatish Balay 2522a7e14dcfSSatish Balay /*@ 2523ae93cb3cSJason Sarich TaoSetConvergenceHistory - Sets the array used to hold the convergence history. 2524a7e14dcfSSatish Balay 2525c3339decSBarry Smith Logically Collective 2526a7e14dcfSSatish Balay 2527a7e14dcfSSatish Balay Input Parameters: 252847450a7bSBarry Smith + tao - the `Tao` solver context 2529a7e14dcfSSatish Balay . obj - array to hold objective value history 2530a7e14dcfSSatish Balay . resid - array to hold residual history 2531a7e14dcfSSatish Balay . cnorm - array to hold constraint violation history 2532be1558d8SJason Sarich . lits - integer array holds the number of linear iterations for each Tao iteration 253367be906fSBarry Smith . na - size of `obj`, `resid`, and `cnorm` 253465ba42b6SBarry Smith - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero, 2535a7e14dcfSSatish Balay else it continues storing new values for new minimizations after the old ones 2536a7e14dcfSSatish Balay 253767be906fSBarry Smith Level: intermediate 253867be906fSBarry Smith 2539a7e14dcfSSatish Balay Notes: 254047450a7bSBarry Smith If set, `Tao` will fill the given arrays with the indicated 2541ae93cb3cSJason Sarich information at each iteration. If 'obj','resid','cnorm','lits' are 2542606f75f6SBarry Smith *all* `NULL` then space (using size `na`, or 1000 if `na` is `PETSC_DECIDE`) is allocated for the history. 254367be906fSBarry Smith If not all are `NULL`, then only the non-`NULL` information categories 2544ae93cb3cSJason Sarich will be stored, the others will be ignored. 2545ae93cb3cSJason Sarich 2546ae93cb3cSJason Sarich Any convergence information after iteration number 'na' will not be stored. 2547a7e14dcfSSatish Balay 2548a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2549a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2550a7e14dcfSSatish Balay during the section of code that is being timed. 2551a7e14dcfSSatish Balay 25521cc06b55SBarry Smith .seealso: [](ch_tao), `TaoGetConvergenceHistory()` 2553a7e14dcfSSatish Balay @*/ 2554d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset) 2555d71ae5a4SJacob Faibussowitsch { 2556a7e14dcfSSatish Balay PetscFunctionBegin; 2557441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 25584f572ea9SToby Isaac if (obj) PetscAssertPointer(obj, 2); 25594f572ea9SToby Isaac if (resid) PetscAssertPointer(resid, 3); 25604f572ea9SToby Isaac if (cnorm) PetscAssertPointer(cnorm, 4); 25614f572ea9SToby Isaac if (lits) PetscAssertPointer(lits, 5); 2562ae93cb3cSJason Sarich 2563606f75f6SBarry Smith if (na == PETSC_DECIDE || na == PETSC_CURRENT) na = 1000; 2564ae93cb3cSJason Sarich if (!obj && !resid && !cnorm && !lits) { 25659566063dSJacob Faibussowitsch PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits)); 2566ae93cb3cSJason Sarich tao->hist_malloc = PETSC_TRUE; 2567ae93cb3cSJason Sarich } 2568ae93cb3cSJason Sarich 2569a7e14dcfSSatish Balay tao->hist_obj = obj; 2570a7e14dcfSSatish Balay tao->hist_resid = resid; 2571a7e14dcfSSatish Balay tao->hist_cnorm = cnorm; 2572ae93cb3cSJason Sarich tao->hist_lits = lits; 2573a7e14dcfSSatish Balay tao->hist_max = na; 2574a7e14dcfSSatish Balay tao->hist_reset = reset; 2575ae93cb3cSJason Sarich tao->hist_len = 0; 25763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2577a7e14dcfSSatish Balay } 2578a7e14dcfSSatish Balay 2579a7e14dcfSSatish Balay /*@C 258065ba42b6SBarry Smith TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history. 2581a7e14dcfSSatish Balay 2582c3339decSBarry Smith Collective 2583a7e14dcfSSatish Balay 2584a7e14dcfSSatish Balay Input Parameter: 258547450a7bSBarry Smith . tao - the `Tao` context 2586a7e14dcfSSatish Balay 2587a7e14dcfSSatish Balay Output Parameters: 2588a7e14dcfSSatish Balay + obj - array used to hold objective value history 2589a7e14dcfSSatish Balay . resid - array used to hold residual history 2590a7e14dcfSSatish Balay . cnorm - array used to hold constraint violation history 2591ae93cb3cSJason Sarich . lits - integer array used to hold linear solver iteration count 259267be906fSBarry Smith - nhist - size of `obj`, `resid`, `cnorm`, and `lits` 259367be906fSBarry Smith 259467be906fSBarry Smith Level: advanced 2595a7e14dcfSSatish Balay 2596a7e14dcfSSatish Balay Notes: 259765ba42b6SBarry Smith This routine must be preceded by calls to `TaoSetConvergenceHistory()` 259865ba42b6SBarry Smith and `TaoSolve()`, otherwise it returns useless information. 2599ae93cb3cSJason Sarich 2600a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2601a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2602a7e14dcfSSatish Balay during the section of code that is being timed. 2603a7e14dcfSSatish Balay 2604e056e8ceSJacob Faibussowitsch Fortran Notes: 260567be906fSBarry Smith The calling sequence is 260667be906fSBarry Smith .vb 260767be906fSBarry Smith call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr) 260867be906fSBarry Smith .ve 2609a3b724e8SBarry Smith In other words this gets the current number of entries in the history. Access the history through the array you passed to `TaoSetConvergenceHistory()` 2610a7e14dcfSSatish Balay 26111cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()` 2612a7e14dcfSSatish Balay @*/ 2613d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist) 2614d71ae5a4SJacob Faibussowitsch { 2615a7e14dcfSSatish Balay PetscFunctionBegin; 2616441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2617a7e14dcfSSatish Balay if (obj) *obj = tao->hist_obj; 2618a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->hist_cnorm; 2619a7e14dcfSSatish Balay if (resid) *resid = tao->hist_resid; 26205b494b05SBarry Smith if (lits) *lits = tao->hist_lits; 2621a7e14dcfSSatish Balay if (nhist) *nhist = tao->hist_len; 26223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2623a7e14dcfSSatish Balay } 2624a7e14dcfSSatish Balay 2625a7e14dcfSSatish Balay /*@ 2626ce78bad3SBarry Smith TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver that can be accessed later, for example in the 2627ce78bad3SBarry Smith `Tao` callback functions with `TaoGetApplicationContext()` 2628a7e14dcfSSatish Balay 2629c3339decSBarry Smith Logically Collective 2630a7e14dcfSSatish Balay 2631a7e14dcfSSatish Balay Input Parameters: 263247450a7bSBarry Smith + tao - the `Tao` context 2633ce78bad3SBarry Smith - ctx - the user context 2634a7e14dcfSSatish Balay 2635a7e14dcfSSatish Balay Level: intermediate 2636a7e14dcfSSatish Balay 2637ce78bad3SBarry Smith Fortran Note: 2638ce78bad3SBarry Smith This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this 2639ce78bad3SBarry Smith function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `TaoGetApplicationContext()` for 2640ce78bad3SBarry Smith an example. 2641ce78bad3SBarry Smith 264242747ad1SJacob Faibussowitsch .seealso: [](ch_tao), `Tao`, `TaoGetApplicationContext()` 2643a7e14dcfSSatish Balay @*/ 2644*2a8381b2SBarry Smith PetscErrorCode TaoSetApplicationContext(Tao tao, PetscCtx ctx) 2645d71ae5a4SJacob Faibussowitsch { 2646a7e14dcfSSatish Balay PetscFunctionBegin; 2647441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 264849abdd8aSBarry Smith tao->ctx = ctx; 26493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2650a7e14dcfSSatish Balay } 2651a7e14dcfSSatish Balay 2652a7e14dcfSSatish Balay /*@ 2653ce78bad3SBarry Smith TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver provided with `TaoSetApplicationContext()` 2654a7e14dcfSSatish Balay 2655a7e14dcfSSatish Balay Not Collective 2656a7e14dcfSSatish Balay 2657a7e14dcfSSatish Balay Input Parameter: 265847450a7bSBarry Smith . tao - the `Tao` context 2659a7e14dcfSSatish Balay 2660a7e14dcfSSatish Balay Output Parameter: 2661ce78bad3SBarry Smith . ctx - a pointer to the user context 2662a7e14dcfSSatish Balay 2663a7e14dcfSSatish Balay Level: intermediate 2664a7e14dcfSSatish Balay 2665*2a8381b2SBarry Smith Fortran Note: 2666*2a8381b2SBarry Smith This only works when the context is a Fortran derived type or a `PetscObject`. Define `ctx` with 2667ce78bad3SBarry Smith .vb 2668ce78bad3SBarry Smith type(tUsertype), pointer :: ctx 2669ce78bad3SBarry Smith .ve 2670ce78bad3SBarry Smith 26711cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetApplicationContext()` 2672a7e14dcfSSatish Balay @*/ 2673*2a8381b2SBarry Smith PetscErrorCode TaoGetApplicationContext(Tao tao, PetscCtxRt ctx) 2674d71ae5a4SJacob Faibussowitsch { 2675a7e14dcfSSatish Balay PetscFunctionBegin; 2676441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 267749abdd8aSBarry Smith PetscAssertPointer(ctx, 2); 267849abdd8aSBarry Smith *(void **)ctx = tao->ctx; 26793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2680a7e14dcfSSatish Balay } 2681a9603a14SPatrick Farrell 2682a9603a14SPatrick Farrell /*@ 2683a81c3919SBarry Smith TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient in some of the `Tao` algorithms 2684a9603a14SPatrick Farrell 2685c3339decSBarry Smith Collective 2686a9603a14SPatrick Farrell 2687a9603a14SPatrick Farrell Input Parameters: 268847450a7bSBarry Smith + tao - the `Tao` context 268965ba42b6SBarry Smith - M - matrix that defines the norm 2690a9603a14SPatrick Farrell 2691a9603a14SPatrick Farrell Level: beginner 2692a9603a14SPatrick Farrell 26931cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()` 2694a9603a14SPatrick Farrell @*/ 2695d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M) 2696d71ae5a4SJacob Faibussowitsch { 2697a9603a14SPatrick Farrell PetscFunctionBegin; 2698a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 269994511df7SStefano Zampini PetscValidHeaderSpecific(M, MAT_CLASSID, 2); 27009566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)M)); 27019566063dSJacob Faibussowitsch PetscCall(MatDestroy(&tao->gradient_norm)); 27029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&tao->gradient_norm_tmp)); 2703a9603a14SPatrick Farrell tao->gradient_norm = M; 27049566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp)); 27053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2706a9603a14SPatrick Farrell } 2707a9603a14SPatrick Farrell 2708a9603a14SPatrick Farrell /*@ 2709a81c3919SBarry Smith TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient in some of the `Tao` algorithms 2710a9603a14SPatrick Farrell 2711a9603a14SPatrick Farrell Not Collective 2712a9603a14SPatrick Farrell 2713a9603a14SPatrick Farrell Input Parameter: 271447450a7bSBarry Smith . tao - the `Tao` context 2715a9603a14SPatrick Farrell 2716a9603a14SPatrick Farrell Output Parameter: 2717a9603a14SPatrick Farrell . M - gradient norm 2718a9603a14SPatrick Farrell 2719a9603a14SPatrick Farrell Level: beginner 2720a9603a14SPatrick Farrell 27211cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()` 2722a9603a14SPatrick Farrell @*/ 2723d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M) 2724d71ae5a4SJacob Faibussowitsch { 2725a9603a14SPatrick Farrell PetscFunctionBegin; 2726a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 27274f572ea9SToby Isaac PetscAssertPointer(M, 2); 2728a9603a14SPatrick Farrell *M = tao->gradient_norm; 27293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2730a9603a14SPatrick Farrell } 2731a9603a14SPatrick Farrell 2732cc4c1da9SBarry Smith /*@ 273347450a7bSBarry Smith TaoGradientNorm - Compute the norm using the `NormType`, the user has selected 2734a9603a14SPatrick Farrell 2735c3339decSBarry Smith Collective 2736a9603a14SPatrick Farrell 2737d8d19677SJose E. Roman Input Parameters: 273847450a7bSBarry Smith + tao - the `Tao` context 2739a81c3919SBarry Smith . gradient - the gradient 27403b242c63SJacob Faibussowitsch - type - the norm type 2741a9603a14SPatrick Farrell 2742a9603a14SPatrick Farrell Output Parameter: 2743a9603a14SPatrick Farrell . gnorm - the gradient norm 2744a9603a14SPatrick Farrell 274547450a7bSBarry Smith Level: advanced 2746a9603a14SPatrick Farrell 2747a81c3919SBarry Smith Note: 2748a81c3919SBarry Smith If `TaoSetGradientNorm()` has been set and `type` is `NORM_2` then the norm provided with `TaoSetGradientNorm()` is used. 2749a81c3919SBarry Smith 2750a81c3919SBarry Smith Developer Notes: 2751a81c3919SBarry Smith Should be named `TaoComputeGradientNorm()`. 2752a81c3919SBarry Smith 2753a81c3919SBarry Smith The usage is a bit confusing, with `TaoSetGradientNorm()` plus `NORM_2` resulting in the computation of the user provided 2754a81c3919SBarry Smith norm, perhaps a refactorization is in order. 2755a81c3919SBarry Smith 27561cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()` 2757a9603a14SPatrick Farrell @*/ 2758d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm) 2759d71ae5a4SJacob Faibussowitsch { 2760a9603a14SPatrick Farrell PetscFunctionBegin; 27618854b543SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 27628854b543SStefano Zampini PetscValidHeaderSpecific(gradient, VEC_CLASSID, 2); 27638854b543SStefano Zampini PetscValidLogicalCollectiveEnum(tao, type, 3); 27644f572ea9SToby Isaac PetscAssertPointer(gnorm, 4); 2765a9603a14SPatrick Farrell if (tao->gradient_norm) { 2766680e2bc7SPatrick Farrell PetscScalar gnorms; 2767680e2bc7SPatrick Farrell 27683c859ba3SBarry 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."); 27699566063dSJacob Faibussowitsch PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp)); 27709566063dSJacob Faibussowitsch PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms)); 27717bb79937SPatrick Farrell *gnorm = PetscRealPart(PetscSqrtScalar(gnorms)); 2772a9603a14SPatrick Farrell } else { 27739566063dSJacob Faibussowitsch PetscCall(VecNorm(gradient, type, gnorm)); 2774a9603a14SPatrick Farrell } 27753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2776a9603a14SPatrick Farrell } 2777a9603a14SPatrick Farrell 2778e882e171SHong Zhang /*@C 2779a81c3919SBarry Smith TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorSolutionDraw()` 2780a9603a14SPatrick Farrell 2781c3339decSBarry Smith Collective 2782e882e171SHong Zhang 27832fe279fdSBarry Smith Input Parameters: 27842fe279fdSBarry Smith + comm - the communicator to share the context 27852fe279fdSBarry Smith . host - the name of the X Windows host that will display the monitor 27862fe279fdSBarry Smith . label - the label to put at the top of the display window 27872fe279fdSBarry Smith . x - the horizontal coordinate of the lower left corner of the window to open 27882fe279fdSBarry Smith . y - the vertical coordinate of the lower left corner of the window to open 27892fe279fdSBarry Smith . m - the width of the window 27902fe279fdSBarry Smith . n - the height of the window 27912fe279fdSBarry Smith - howoften - how many `Tao` iterations between displaying the monitor information 27922fe279fdSBarry Smith 2793d5b43468SJose E. Roman Output Parameter: 2794e882e171SHong Zhang . ctx - the monitor context 2795e882e171SHong Zhang 2796a81c3919SBarry Smith Options Database Keys: 2797a81c3919SBarry Smith + -tao_monitor_solution_draw - use `TaoMonitorSolutionDraw()` to monitor the solution 2798a81c3919SBarry Smith - -tao_draw_solution_initial - show initial guess as well as current solution 2799e882e171SHong Zhang 2800e882e171SHong Zhang Level: intermediate 2801e882e171SHong Zhang 2802a81c3919SBarry Smith Note: 2803a81c3919SBarry Smith The context this creates, along with `TaoMonitorSolutionDraw()`, and `TaoMonitorDrawCtxDestroy()` 2804a81c3919SBarry Smith are passed to `TaoMonitorSet()`. 2805a81c3919SBarry Smith 28061cc06b55SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()` 2807e882e171SHong Zhang @*/ 2808d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx) 2809d71ae5a4SJacob Faibussowitsch { 2810e882e171SHong Zhang PetscFunctionBegin; 28119566063dSJacob Faibussowitsch PetscCall(PetscNew(ctx)); 28129566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer)); 28139566063dSJacob Faibussowitsch PetscCall(PetscViewerSetFromOptions((*ctx)->viewer)); 2814e882e171SHong Zhang (*ctx)->howoften = howoften; 28153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2816e882e171SHong Zhang } 2817e882e171SHong Zhang 2818e882e171SHong Zhang /*@C 2819a81c3919SBarry Smith TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorSolutionDraw()` 2820e882e171SHong Zhang 2821c3339decSBarry Smith Collective 2822e882e171SHong Zhang 282347450a7bSBarry Smith Input Parameter: 2824e056e8ceSJacob Faibussowitsch . ictx - the monitor context 2825e882e171SHong Zhang 2826e882e171SHong Zhang Level: intermediate 2827e882e171SHong Zhang 2828a81c3919SBarry Smith Note: 2829a81c3919SBarry Smith This is passed to `TaoMonitorSet()` as the final argument, along with `TaoMonitorSolutionDraw()`, and the context 2830a81c3919SBarry Smith obtained with `TaoMonitorDrawCtxCreate()`. 2831a81c3919SBarry Smith 2832a81c3919SBarry Smith .seealso: [](ch_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorSolutionDraw()` 2833e882e171SHong Zhang @*/ 2834d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx) 2835d71ae5a4SJacob Faibussowitsch { 2836e882e171SHong Zhang PetscFunctionBegin; 28379566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&(*ictx)->viewer)); 28389566063dSJacob Faibussowitsch PetscCall(PetscFree(*ictx)); 28393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2840e882e171SHong Zhang } 2841