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 24d71ae5a4SJacob Faibussowitsch static PetscErrorCode KSPPreSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, Tao tao) 25d71ae5a4SJacob Faibussowitsch { 260f0abf79SStefano Zampini SNES snes_ewdummy = tao->snes_ewdummy; 270f0abf79SStefano Zampini 280f0abf79SStefano Zampini PetscFunctionBegin; 293ba16761SJacob Faibussowitsch if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS); 300f0abf79SStefano Zampini /* populate snes_ewdummy struct values used in KSPPreSolve_SNESEW */ 310f0abf79SStefano Zampini snes_ewdummy->vec_func = b; 320f0abf79SStefano Zampini snes_ewdummy->rtol = tao->gttol; 330f0abf79SStefano Zampini snes_ewdummy->iter = tao->niter; 340f0abf79SStefano Zampini PetscCall(VecNorm(b, NORM_2, &snes_ewdummy->norm)); 350f0abf79SStefano Zampini PetscCall(KSPPreSolve_SNESEW(ksp, b, x, snes_ewdummy)); 360f0abf79SStefano Zampini snes_ewdummy->vec_func = NULL; 373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 380f0abf79SStefano Zampini } 390f0abf79SStefano Zampini 40d71ae5a4SJacob Faibussowitsch static PetscErrorCode KSPPostSolve_TAOEW_Private(KSP ksp, Vec b, Vec x, Tao tao) 41d71ae5a4SJacob Faibussowitsch { 420f0abf79SStefano Zampini SNES snes_ewdummy = tao->snes_ewdummy; 430f0abf79SStefano Zampini 440f0abf79SStefano Zampini PetscFunctionBegin; 453ba16761SJacob Faibussowitsch if (!snes_ewdummy) PetscFunctionReturn(PETSC_SUCCESS); 460f0abf79SStefano Zampini PetscCall(KSPPostSolve_SNESEW(ksp, b, x, snes_ewdummy)); 473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 480f0abf79SStefano Zampini } 490f0abf79SStefano Zampini 50d71ae5a4SJacob Faibussowitsch static PetscErrorCode TaoSetUpEW_Private(Tao tao) 51d71ae5a4SJacob Faibussowitsch { 520f0abf79SStefano Zampini SNESKSPEW *kctx; 530f0abf79SStefano Zampini const char *ewprefix; 540f0abf79SStefano Zampini 550f0abf79SStefano Zampini PetscFunctionBegin; 563ba16761SJacob Faibussowitsch if (!tao->ksp) PetscFunctionReturn(PETSC_SUCCESS); 570f0abf79SStefano Zampini if (tao->ksp_ewconv) { 580f0abf79SStefano Zampini if (!tao->snes_ewdummy) PetscCall(SNESCreate(PetscObjectComm((PetscObject)tao), &tao->snes_ewdummy)); 590f0abf79SStefano Zampini tao->snes_ewdummy->ksp_ewconv = PETSC_TRUE; 600f0abf79SStefano Zampini PetscCall(KSPSetPreSolve(tao->ksp, (PetscErrorCode(*)(KSP, Vec, Vec, void *))KSPPreSolve_TAOEW_Private, tao)); 610f0abf79SStefano Zampini PetscCall(KSPSetPostSolve(tao->ksp, (PetscErrorCode(*)(KSP, Vec, Vec, void *))KSPPostSolve_TAOEW_Private, tao)); 620f0abf79SStefano Zampini 630f0abf79SStefano Zampini PetscCall(KSPGetOptionsPrefix(tao->ksp, &ewprefix)); 640f0abf79SStefano Zampini kctx = (SNESKSPEW *)tao->snes_ewdummy->kspconvctx; 65*a4598233SStefano Zampini PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_FALSE, PetscObjectComm((PetscObject)tao), ewprefix)); 660f0abf79SStefano Zampini } else PetscCall(SNESDestroy(&tao->snes_ewdummy)); 673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 680f0abf79SStefano Zampini } 690f0abf79SStefano Zampini 70a7e14dcfSSatish Balay /*@ 7165ba42b6SBarry Smith TaoCreate - Creates a Tao solver 72a7e14dcfSSatish Balay 73d083f849SBarry Smith Collective 74a7e14dcfSSatish Balay 75a7e14dcfSSatish Balay Input Parameter: 76a7e14dcfSSatish Balay . comm - MPI communicator 77a7e14dcfSSatish Balay 78a7e14dcfSSatish Balay Output Parameter: 7947450a7bSBarry Smith . newtao - the new `Tao` context 80a7e14dcfSSatish Balay 8147450a7bSBarry Smith Options Database Key: 8265ba42b6SBarry Smith . -tao_type - select which method Tao should use 83a7e14dcfSSatish Balay 84a7e14dcfSSatish Balay Level: beginner 85a7e14dcfSSatish Balay 8647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolve()`, `TaoDestroy()`, `TAOSetFromOptions()`, `TAOSetType()` 87a7e14dcfSSatish Balay @*/ 88d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao) 89d71ae5a4SJacob Faibussowitsch { 90441846f8SBarry Smith Tao tao; 91a7e14dcfSSatish Balay 92a7e14dcfSSatish Balay PetscFunctionBegin; 93a7e14dcfSSatish Balay PetscValidPointer(newtao, 2); 949566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 959566063dSJacob Faibussowitsch PetscCall(TaoLineSearchInitializePackage()); 969566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(tao, TAO_CLASSID, "Tao", "Optimization solver", "Tao", comm, TaoDestroy, TaoView)); 97a7e14dcfSSatish Balay 9894511df7SStefano Zampini /* Set non-NULL defaults */ 9994511df7SStefano Zampini tao->ops->convergencetest = TaoDefaultConvergenceTest; 100a7e14dcfSSatish Balay 101a7e14dcfSSatish Balay tao->max_it = 10000; 10294511df7SStefano Zampini tao->max_funcs = -1; 1036f4723b1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 1046f4723b1SBarry Smith tao->gatol = 1e-5; 1056f4723b1SBarry Smith tao->grtol = 1e-5; 1066f1f5f59SAlp Dener tao->crtol = 1e-5; 1076f1f5f59SAlp Dener tao->catol = 1e-5; 1086f4723b1SBarry Smith #else 109a7e14dcfSSatish Balay tao->gatol = 1e-8; 110a7e14dcfSSatish Balay tao->grtol = 1e-8; 1116f1f5f59SAlp Dener tao->crtol = 1e-8; 1126f1f5f59SAlp Dener tao->catol = 1e-8; 1136f4723b1SBarry Smith #endif 1146552cf8aSJason Sarich tao->gttol = 0.0; 1154f1535bcSTodd Munson tao->steptol = 0.0; 116e270355aSBarry Smith tao->trust0 = PETSC_INFINITY; 1176f4723b1SBarry Smith tao->fmin = PETSC_NINFINITY; 1189fa2b5dcSStefano Zampini 119a7e14dcfSSatish Balay tao->hist_reset = PETSC_TRUE; 120a7e14dcfSSatish Balay 1219566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 122a7e14dcfSSatish Balay *newtao = tao; 1233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 124a7e14dcfSSatish Balay } 125a7e14dcfSSatish Balay 126a7e14dcfSSatish Balay /*@ 127a7e14dcfSSatish Balay TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u 128a7e14dcfSSatish Balay 129c3339decSBarry Smith Collective 130a7e14dcfSSatish Balay 13147450a7bSBarry Smith Input Parameter: 13247450a7bSBarry Smith . tao - the `Tao` context 133a7e14dcfSSatish Balay 13467be906fSBarry Smith Level: beginner 13567be906fSBarry Smith 136a7e14dcfSSatish Balay Notes: 13747450a7bSBarry Smith The user must set up the `Tao` object with calls to `TaoSetSolution()`, `TaoSetObjective()`, `TaoSetGradient()`, and (if using 2nd order method) `TaoSetHessian()`. 138a7e14dcfSSatish Balay 13967be906fSBarry Smith You should call `TaoGetConvergedReason()` or run with `-tao_converged_reason` to determine if the optimization algorithm actually succeeded or 140a35d58b8SBarry Smith why it failed. 141a35d58b8SBarry Smith 14247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoGetConvergedReason()`, `TaoSetUp()` 143a7e14dcfSSatish Balay @*/ 144d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolve(Tao tao) 145d71ae5a4SJacob Faibussowitsch { 146e2379f4fSBarry Smith static PetscBool set = PETSC_FALSE; 14747a47007SBarry Smith 148a7e14dcfSSatish Balay PetscFunctionBegin; 149441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 150d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@TechReport{tao-user-ref,\n" 151e2379f4fSBarry Smith "title = {Toolkit for Advanced Optimization (TAO) Users Manual},\n" 152e2379f4fSBarry Smith "author = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n" 153e2379f4fSBarry Smith "Institution = {Argonne National Laboratory},\n" 154e2379f4fSBarry Smith "Year = 2014,\n" 155e2379f4fSBarry Smith "Number = {ANL/MCS-TM-322 - Revision 3.5},\n" 1569371c9d4SSatish Balay "url = {https://www.mcs.anl.gov/research/projects/tao/}\n}\n", 1579371c9d4SSatish Balay &set)); 158494bef23SAlp Dener tao->header_printed = PETSC_FALSE; 1599566063dSJacob Faibussowitsch PetscCall(TaoSetUp(tao)); 1609566063dSJacob Faibussowitsch PetscCall(TaoResetStatistics(tao)); 1611baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchReset(tao->linesearch)); 162a7e14dcfSSatish Balay 1639566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TAO_Solve, tao, 0, 0, 0)); 164dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, solve); 1659566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TAO_Solve, tao, 0, 0, 0)); 166a7e14dcfSSatish Balay 1679566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(tao->solution, (PetscObject)tao, "-tao_view_solution")); 1687cf37e64SBarry Smith 1698931d482SJason Sarich tao->ntotalits += tao->niter; 170a7e14dcfSSatish Balay 171a7e14dcfSSatish Balay if (tao->printreason) { 172f642d338SBarry Smith PetscViewer viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm); 173f642d338SBarry Smith PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)tao)->tablevel)); 174a7e14dcfSSatish Balay if (tao->reason > 0) { 175f642d338SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter)); 176a7e14dcfSSatish Balay } else { 177f642d338SBarry Smith PetscCall(PetscViewerASCIIPrintf(viewer, " TAO %s solve did not converge due to %s iteration %" PetscInt_FMT "\n", ((PetscObject)tao)->prefix ? ((PetscObject)tao)->prefix : "", TaoConvergedReasons[tao->reason], tao->niter)); 178a7e14dcfSSatish Balay } 179f642d338SBarry Smith PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)tao)->tablevel)); 180a7e14dcfSSatish Balay } 181f642d338SBarry Smith PetscCall(TaoViewFromOptions(tao, NULL, "-tao_view")); 1823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 183a7e14dcfSSatish Balay } 184a7e14dcfSSatish Balay 185a7e14dcfSSatish Balay /*@ 186a7e14dcfSSatish Balay TaoSetUp - Sets up the internal data structures for the later use 187a7e14dcfSSatish Balay of a Tao solver 188a7e14dcfSSatish Balay 189c3339decSBarry Smith Collective 190a7e14dcfSSatish Balay 19147450a7bSBarry Smith Input Parameter: 19247450a7bSBarry Smith . tao - the `Tao` context 193a7e14dcfSSatish Balay 19467be906fSBarry Smith Level: advanced 19567be906fSBarry Smith 19647450a7bSBarry Smith Note: 19765ba42b6SBarry Smith The user will not need to explicitly call `TaoSetUp()`, as it will 19865ba42b6SBarry Smith automatically be called in `TaoSolve()`. However, if the user 19965ba42b6SBarry Smith desires to call it explicitly, it should come after `TaoCreate()` 20065ba42b6SBarry Smith and any TaoSetSomething() routines, but before `TaoSolve()`. 201a7e14dcfSSatish Balay 20247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 203a7e14dcfSSatish Balay @*/ 204d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUp(Tao tao) 205d71ae5a4SJacob Faibussowitsch { 206a7e14dcfSSatish Balay PetscFunctionBegin; 207441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2083ba16761SJacob Faibussowitsch if (tao->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2090f0abf79SStefano Zampini PetscCall(TaoSetUpEW_Private(tao)); 2103c859ba3SBarry Smith PetscCheck(tao->solution, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "Must call TaoSetSolution"); 211dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setup); 212a7e14dcfSSatish Balay tao->setupcalled = PETSC_TRUE; 2133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 214a7e14dcfSSatish Balay } 215a7e14dcfSSatish Balay 2161fb7b255SJunchao Zhang /*@C 21747450a7bSBarry Smith TaoDestroy - Destroys the `Tao` context that was created with `TaoCreate()` 218a7e14dcfSSatish Balay 219c3339decSBarry Smith Collective 220a7e14dcfSSatish Balay 221a7e14dcfSSatish Balay Input Parameter: 22247450a7bSBarry Smith . tao - the `Tao` context 223a7e14dcfSSatish Balay 224a7e14dcfSSatish Balay Level: beginner 225a7e14dcfSSatish Balay 22647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 227a7e14dcfSSatish Balay @*/ 228d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDestroy(Tao *tao) 229d71ae5a4SJacob Faibussowitsch { 230a7e14dcfSSatish Balay PetscFunctionBegin; 2313ba16761SJacob Faibussowitsch if (!*tao) PetscFunctionReturn(PETSC_SUCCESS); 232441846f8SBarry Smith PetscValidHeaderSpecific(*tao, TAO_CLASSID, 1); 2339371c9d4SSatish Balay if (--((PetscObject)*tao)->refct > 0) { 2349371c9d4SSatish Balay *tao = NULL; 2353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 236a7e14dcfSSatish Balay } 2379371c9d4SSatish Balay 23848a46eb9SPierre Jolivet if ((*tao)->ops->destroy) PetscCall((*((*tao))->ops->destroy)(*tao)); 2399566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&(*tao)->ksp)); 2400f0abf79SStefano Zampini PetscCall(SNESDestroy(&(*tao)->snes_ewdummy)); 2419566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&(*tao)->linesearch)); 242a7e14dcfSSatish Balay 243a7e14dcfSSatish Balay if ((*tao)->ops->convergencedestroy) { 2449566063dSJacob Faibussowitsch PetscCall((*(*tao)->ops->convergencedestroy)((*tao)->cnvP)); 24548a46eb9SPierre Jolivet if ((*tao)->jacobian_state_inv) PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 246a7e14dcfSSatish Balay } 2479566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->solution)); 2489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient)); 2499566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->ls_res)); 250a7e14dcfSSatish Balay 251a9603a14SPatrick Farrell if ((*tao)->gradient_norm) { 2529566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)(*tao)->gradient_norm)); 2539566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->gradient_norm_tmp)); 254a9603a14SPatrick Farrell } 255a9603a14SPatrick Farrell 2569566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XL)); 2579566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->XU)); 2589566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IL)); 2599566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->IU)); 2609566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DE)); 2619566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->DI)); 2629566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints)); 2639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_equality)); 2649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->constraints_inequality)); 2659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->stepdirection)); 2669566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian_pre)); 2679566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->hessian)); 2689566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac)); 2699566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->ls_jac_pre)); 2709566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_pre)); 2719566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian)); 2729566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_pre)); 2739566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state)); 2749566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_state_inv)); 2759566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_design)); 2769566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality)); 2779566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_equality_pre)); 2789566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality)); 2799566063dSJacob Faibussowitsch PetscCall(MatDestroy(&(*tao)->jacobian_inequality_pre)); 2809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->state_is)); 2819566063dSJacob Faibussowitsch PetscCall(ISDestroy(&(*tao)->design_is)); 2829566063dSJacob Faibussowitsch PetscCall(VecDestroy(&(*tao)->res_weights_v)); 2839566063dSJacob Faibussowitsch PetscCall(TaoCancelMonitors(*tao)); 28448a46eb9SPierre Jolivet if ((*tao)->hist_malloc) PetscCall(PetscFree4((*tao)->hist_obj, (*tao)->hist_resid, (*tao)->hist_cnorm, (*tao)->hist_lits)); 285737f463aSAlp Dener if ((*tao)->res_weights_n) { 2869566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_rows)); 2879566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_cols)); 2889566063dSJacob Faibussowitsch PetscCall(PetscFree((*tao)->res_weights_w)); 289125ddc8aSJason Sarich } 2909566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(tao)); 2913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 292a7e14dcfSSatish Balay } 293a7e14dcfSSatish Balay 294a7e14dcfSSatish Balay /*@ 29547450a7bSBarry Smith TaoKSPSetUseEW - Sets `SNES` use Eisenstat-Walker method for computing relative tolerance for linear solvers. 2960f0abf79SStefano Zampini 297c3339decSBarry Smith Logically Collective 2980f0abf79SStefano Zampini 2990f0abf79SStefano Zampini Input Parameters: 3000f0abf79SStefano Zampini + tao - Tao context 30165ba42b6SBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE` 3020f0abf79SStefano Zampini 30367be906fSBarry Smith Level: advanced 30467be906fSBarry Smith 30547450a7bSBarry Smith Note: 30665ba42b6SBarry Smith See `SNESKSPSetUseEW()` for customization details. 3070f0abf79SStefano Zampini 3080f0abf79SStefano Zampini Reference: 30947450a7bSBarry Smith . * - S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an inexact Newton method", SISC 17 (1), pp.16-32, 1996. 3100f0abf79SStefano Zampini 31147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `SNESKSPSetUseEW()` 3120f0abf79SStefano Zampini @*/ 313d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoKSPSetUseEW(Tao tao, PetscBool flag) 314d71ae5a4SJacob Faibussowitsch { 3150f0abf79SStefano Zampini PetscFunctionBegin; 3160f0abf79SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3170f0abf79SStefano Zampini PetscValidLogicalCollectiveBool(tao, flag, 2); 3180f0abf79SStefano Zampini tao->ksp_ewconv = flag; 3193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3200f0abf79SStefano Zampini } 3210f0abf79SStefano Zampini 3220f0abf79SStefano Zampini /*@ 32347450a7bSBarry Smith TaoSetFromOptions - Sets various Tao parameters from the options database 324a7e14dcfSSatish Balay 325c3339decSBarry Smith Collective 326a7e14dcfSSatish Balay 32701d2d390SJose E. Roman Input Parameter: 32847450a7bSBarry Smith . tao - the `Tao` solver context 329a7e14dcfSSatish Balay 33047450a7bSBarry Smith Options Database Keys: 33165ba42b6SBarry Smith + -tao_type <type> - The algorithm that Tao uses (lmvm, nls, etc.) 332a7e14dcfSSatish Balay . -tao_gatol <gatol> - absolute error tolerance for ||gradient|| 333a7e14dcfSSatish Balay . -tao_grtol <grtol> - relative error tolerance for ||gradient|| 334a7e14dcfSSatish Balay . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient 335a7e14dcfSSatish Balay . -tao_max_it <max> - sets maximum number of iterations 336a7e14dcfSSatish Balay . -tao_max_funcs <max> - sets maximum number of function evaluations 337a7e14dcfSSatish Balay . -tao_fmin <fmin> - stop if function value reaches fmin 338a7e14dcfSSatish Balay . -tao_steptol <tol> - stop if trust region radius less than <tol> 339a7e14dcfSSatish Balay . -tao_trust0 <t> - initial trust region radius 34047450a7bSBarry Smith . -tao_monitor - prints function value and residual norm at each iteration 341a7e14dcfSSatish Balay . -tao_smonitor - same as tao_monitor, but truncates very small values 342a7e14dcfSSatish Balay . -tao_cmonitor - prints function value, residual, and constraint norm at each iteration 343a7e14dcfSSatish Balay . -tao_view_solution - prints solution vector at each iteration 3444a48860cSAlp Dener . -tao_view_ls_residual - prints least-squares residual vector at each iteration 345147403d9SBarry Smith . -tao_view_stepdirection - prints step direction vector at each iteration 346a7e14dcfSSatish Balay . -tao_view_gradient - prints gradient vector at each iteration 347a7e14dcfSSatish Balay . -tao_draw_solution - graphically view solution vector at each iteration 348a7e14dcfSSatish Balay . -tao_draw_step - graphically view step vector at each iteration 349a7e14dcfSSatish Balay . -tao_draw_gradient - graphically view gradient at each iteration 350a7e14dcfSSatish Balay . -tao_fd_gradient - use gradient computed with finite differences 351f4c1ad5cSStefano Zampini . -tao_fd_hessian - use hessian computed with finite differences 352f4c1ad5cSStefano Zampini . -tao_mf_hessian - use matrix-free hessian computed with finite differences 353a7e14dcfSSatish Balay . -tao_cancelmonitors - cancels all monitors (except those set with command line) 354441846f8SBarry Smith . -tao_view - prints information about the Tao after solving 35565ba42b6SBarry Smith - -tao_converged_reason - prints the reason Tao stopped iterating 356a7e14dcfSSatish Balay 35767be906fSBarry Smith Level: beginner 35867be906fSBarry Smith 35967be906fSBarry Smith Note: 36047450a7bSBarry Smith To see all options, run your program with the `-help` option or consult the 36165ba42b6SBarry Smith user's manual. Should be called after `TaoCreate()` but before `TaoSolve()` 362a7e14dcfSSatish Balay 36347450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 364a7e14dcfSSatish Balay @*/ 365d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFromOptions(Tao tao) 366d71ae5a4SJacob Faibussowitsch { 367b625d6c7SJed Brown TaoType default_type = TAOLMVM; 368a7e14dcfSSatish Balay char type[256], monfilename[PETSC_MAX_PATH_LEN]; 369a7e14dcfSSatish Balay PetscViewer monviewer; 370a7e14dcfSSatish Balay PetscBool flg; 371a7e14dcfSSatish Balay MPI_Comm comm; 372a7e14dcfSSatish Balay 373a7e14dcfSSatish Balay PetscFunctionBegin; 374441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 3759566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)tao, &comm)); 376125ddc8aSJason Sarich 37760b70c5cSStefano Zampini if (((PetscObject)tao)->type_name) default_type = ((PetscObject)tao)->type_name; 37860b70c5cSStefano Zampini 379d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)tao); 380a7e14dcfSSatish Balay /* Check for type from options */ 3819566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-tao_type", "Tao Solver type", "TaoSetType", TaoList, default_type, type, 256, &flg)); 382a7e14dcfSSatish Balay if (flg) { 3839566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, type)); 384a7e14dcfSSatish Balay } else if (!((PetscObject)tao)->type_name) { 3859566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao, default_type)); 386a7e14dcfSSatish Balay } 387a7e14dcfSSatish Balay 38860b70c5cSStefano Zampini /* Tao solvers do not set the prefix, set it here if not yet done 38960b70c5cSStefano Zampini We do it after SetType since solver may have been changed */ 39060b70c5cSStefano Zampini if (tao->linesearch) { 39160b70c5cSStefano Zampini const char *prefix; 39260b70c5cSStefano Zampini PetscCall(TaoLineSearchGetOptionsPrefix(tao->linesearch, &prefix)); 39360b70c5cSStefano Zampini if (!prefix) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, ((PetscObject)(tao))->prefix)); 39460b70c5cSStefano Zampini } 39560b70c5cSStefano Zampini 3969566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_catol", "Stop if constraints violations within", "TaoSetConstraintTolerances", tao->catol, &tao->catol, &flg)); 3976552cf8aSJason Sarich if (flg) tao->catol_changed = PETSC_TRUE; 3986aad120cSJose E. Roman PetscCall(PetscOptionsReal("-tao_crtol", "Stop if relative constraint violations within", "TaoSetConstraintTolerances", tao->crtol, &tao->crtol, &flg)); 3996552cf8aSJason Sarich if (flg) tao->crtol_changed = PETSC_TRUE; 4009566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_gatol", "Stop if norm of gradient less than", "TaoSetTolerances", tao->gatol, &tao->gatol, &flg)); 4016552cf8aSJason Sarich if (flg) tao->gatol_changed = PETSC_TRUE; 4029566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_grtol", "Stop if norm of gradient divided by the function value is less than", "TaoSetTolerances", tao->grtol, &tao->grtol, &flg)); 4036552cf8aSJason Sarich if (flg) tao->grtol_changed = PETSC_TRUE; 4049566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_gttol", "Stop if the norm of the gradient is less than the norm of the initial gradient times tol", "TaoSetTolerances", tao->gttol, &tao->gttol, &flg)); 4056552cf8aSJason Sarich if (flg) tao->gttol_changed = PETSC_TRUE; 4069566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_it", "Stop if iteration number exceeds", "TaoSetMaximumIterations", tao->max_it, &tao->max_it, &flg)); 4076552cf8aSJason Sarich if (flg) tao->max_it_changed = PETSC_TRUE; 4089566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-tao_max_funcs", "Stop if number of function evaluations exceeds", "TaoSetMaximumFunctionEvaluations", tao->max_funcs, &tao->max_funcs, &flg)); 4096552cf8aSJason Sarich if (flg) tao->max_funcs_changed = PETSC_TRUE; 4109566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_fmin", "Stop if function less than", "TaoSetFunctionLowerBound", tao->fmin, &tao->fmin, &flg)); 4116552cf8aSJason Sarich if (flg) tao->fmin_changed = PETSC_TRUE; 4129566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_steptol", "Stop if step size or trust region radius less than", "", tao->steptol, &tao->steptol, &flg)); 4136552cf8aSJason Sarich if (flg) tao->steptol_changed = PETSC_TRUE; 4149566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-tao_trust0", "Initial trust region radius", "TaoSetTrustRegionRadius", tao->trust0, &tao->trust0, &flg)); 4156552cf8aSJason Sarich if (flg) tao->trust0_changed = PETSC_TRUE; 4169566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_view_solution", "view solution vector after each evaluation", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 417a7e14dcfSSatish Balay if (flg) { 4189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4199566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoSolutionMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 420a7e14dcfSSatish Balay } 421a7e14dcfSSatish Balay 42265ba42b6SBarry Smith PetscCall(PetscOptionsBool("-tao_converged_reason", "Print reason for Tao converged", "TaoSolve", tao->printreason, &tao->printreason, NULL)); 4239566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_view_gradient", "view gradient vector after each evaluation", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 424a7e14dcfSSatish Balay if (flg) { 4259566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4269566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoGradientMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 427a7e14dcfSSatish Balay } 428a7e14dcfSSatish Balay 4299566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_view_stepdirection", "view step direction vector after each iteration", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 430a7e14dcfSSatish Balay if (flg) { 4319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4329566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoStepDirectionMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 433a7e14dcfSSatish Balay } 434a7e14dcfSSatish Balay 4359566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_view_residual", "view least-squares residual vector after each evaluation", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 436a7e14dcfSSatish Balay if (flg) { 4379566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4389566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoResidualMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 439a7e14dcfSSatish Balay } 440a7e14dcfSSatish Balay 4419566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_monitor", "Use the default convergence monitor", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 442a7e14dcfSSatish Balay if (flg) { 4439566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4449566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoMonitorDefault, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 445a7e14dcfSSatish Balay } 446a7e14dcfSSatish Balay 4479566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_gmonitor", "Use the convergence monitor with extra globalization info", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 4488d5ead36SAlp Dener if (flg) { 4499566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4509566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoDefaultGMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 4518d5ead36SAlp Dener } 4528d5ead36SAlp Dener 4539566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_smonitor", "Use the short convergence monitor", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 454a7e14dcfSSatish Balay if (flg) { 4559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4569566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoDefaultSMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 457a7e14dcfSSatish Balay } 458a7e14dcfSSatish Balay 4599566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-tao_cmonitor", "Use the default convergence monitor with constraint norm", "TaoSetMonitor", "stdout", monfilename, sizeof(monfilename), &flg)); 460a7e14dcfSSatish Balay if (flg) { 4619566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(comm, monfilename, &monviewer)); 4629566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoDefaultCMonitor, monviewer, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 463a7e14dcfSSatish Balay } 464a7e14dcfSSatish Balay 4658afaa268SBarry Smith flg = PETSC_FALSE; 4669566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_cancelmonitors", "cancel all monitors and call any registered destroy routines", "TaoCancelMonitors", flg, &flg, NULL)); 4679566063dSJacob Faibussowitsch if (flg) PetscCall(TaoCancelMonitors(tao)); 468a7e14dcfSSatish Balay 4698afaa268SBarry Smith flg = PETSC_FALSE; 4709566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_draw_solution", "Plot solution vector at each iteration", "TaoSetMonitor", flg, &flg, NULL)); 471a7e14dcfSSatish Balay if (flg) { 472e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 473e882e171SHong Zhang PetscInt howoften = 1; 4749566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 4759566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoDrawSolutionMonitor, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy)); 476a7e14dcfSSatish Balay } 477a7e14dcfSSatish Balay 4788afaa268SBarry Smith flg = PETSC_FALSE; 4799566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_draw_step", "plots step direction at each iteration", "TaoSetMonitor", flg, &flg, NULL)); 4801baa6e33SBarry Smith if (flg) PetscCall(TaoSetMonitor(tao, TaoDrawStepMonitor, NULL, NULL)); 481a7e14dcfSSatish Balay 4828afaa268SBarry Smith flg = PETSC_FALSE; 4839566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_draw_gradient", "plots gradient at each iteration", "TaoSetMonitor", flg, &flg, NULL)); 484a7e14dcfSSatish Balay if (flg) { 485e882e171SHong Zhang TaoMonitorDrawCtx drawctx; 486e882e171SHong Zhang PetscInt howoften = 1; 4879566063dSJacob Faibussowitsch PetscCall(TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &drawctx)); 4889566063dSJacob Faibussowitsch PetscCall(TaoSetMonitor(tao, TaoDrawGradientMonitor, drawctx, (PetscErrorCode(*)(void **))TaoMonitorDrawCtxDestroy)); 489a7e14dcfSSatish Balay } 4908afaa268SBarry Smith flg = PETSC_FALSE; 4919566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_fd_gradient", "compute gradient using finite differences", "TaoDefaultComputeGradient", flg, &flg, NULL)); 4921baa6e33SBarry Smith if (flg) PetscCall(TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, NULL)); 493f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 4949566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_fd_hessian", "compute hessian using finite differences", "TaoDefaultComputeHessian", flg, &flg, NULL)); 495f4c1ad5cSStefano Zampini if (flg) { 496f4c1ad5cSStefano Zampini Mat H; 497f4c1ad5cSStefano Zampini 4989566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 4999566063dSJacob Faibussowitsch PetscCall(MatSetType(H, MATAIJ)); 5009566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessian, NULL)); 5019566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 502f4c1ad5cSStefano Zampini } 503f4c1ad5cSStefano Zampini flg = PETSC_FALSE; 5049566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_mf_hessian", "compute matrix-free hessian using finite differences", "TaoDefaultComputeHessianMFFD", flg, &flg, NULL)); 505f4c1ad5cSStefano Zampini if (flg) { 506f4c1ad5cSStefano Zampini Mat H; 507f4c1ad5cSStefano Zampini 5089566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)tao), &H)); 5099566063dSJacob Faibussowitsch PetscCall(TaoSetHessian(tao, H, H, TaoDefaultComputeHessianMFFD, NULL)); 5109566063dSJacob Faibussowitsch PetscCall(MatDestroy(&H)); 511f4c1ad5cSStefano Zampini } 512414d97d3SAlp Dener flg = PETSC_FALSE; 5139566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-tao_recycle_history", "enable recycling/re-using information from the previous TaoSolve() call for some algorithms", "TaoSetRecycleHistory", flg, &flg, NULL)); 5141baa6e33SBarry Smith if (flg) PetscCall(TaoSetRecycleHistory(tao, PETSC_TRUE)); 5159566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-tao_subset_type", "subset type", "", TaoSubSetTypes, (PetscEnum)tao->subset_type, (PetscEnum *)&tao->subset_type, NULL)); 516a7e14dcfSSatish Balay 5170f0abf79SStefano Zampini if (tao->ksp) { 5180f0abf79SStefano Zampini PetscCall(PetscOptionsBool("-tao_ksp_ew", "Use Eisentat-Walker linear system convergence test", "TaoKSPSetUseEW", tao->ksp_ewconv, &tao->ksp_ewconv, NULL)); 5190f0abf79SStefano Zampini PetscCall(TaoKSPSetUseEW(tao, tao->ksp_ewconv)); 5200f0abf79SStefano Zampini } 5210f0abf79SStefano Zampini 522dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, setfromoptions, PetscOptionsObject); 52360b70c5cSStefano Zampini 52460b70c5cSStefano Zampini /* process any options handlers added with PetscObjectAddOptionsHandler() */ 52560b70c5cSStefano Zampini PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tao, PetscOptionsObject)); 526d0609cedSBarry Smith PetscOptionsEnd(); 52760b70c5cSStefano Zampini 52860b70c5cSStefano Zampini if (tao->linesearch) PetscCall(TaoLineSearchSetFromOptions(tao->linesearch)); 5293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 530a7e14dcfSSatish Balay } 531a7e14dcfSSatish Balay 532a7e14dcfSSatish Balay /*@C 53347450a7bSBarry Smith TaoViewFromOptions - View a `Tao` object based on values in the options database 534fe2efc57SMark 535c3339decSBarry Smith Collective 536fe2efc57SMark 537fe2efc57SMark Input Parameters: 53847450a7bSBarry Smith + A - the `Tao` context 53947450a7bSBarry Smith . obj - Optional object that provides the prefix for the options database 540736c3998SJose E. Roman - name - command line option 541fe2efc57SMark 542fe2efc57SMark Level: intermediate 54367be906fSBarry Smith 54447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoView`, `PetscObjectViewFromOptions()`, `TaoCreate()` 545fe2efc57SMark @*/ 546d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoViewFromOptions(Tao A, PetscObject obj, const char name[]) 547d71ae5a4SJacob Faibussowitsch { 548fe2efc57SMark PetscFunctionBegin; 549fe2efc57SMark PetscValidHeaderSpecific(A, TAO_CLASSID, 1); 5509566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name)); 5513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 552fe2efc57SMark } 553fe2efc57SMark 554fe2efc57SMark /*@C 55547450a7bSBarry Smith TaoView - Prints information about the `Tao` object 556a7e14dcfSSatish Balay 557c3339decSBarry Smith Collective 558a7e14dcfSSatish Balay 559a7e14dcfSSatish Balay InputParameters: 56047450a7bSBarry Smith + tao - the `Tao` context 561a7e14dcfSSatish Balay - viewer - visualization context 562a7e14dcfSSatish Balay 563a7e14dcfSSatish Balay Options Database Key: 56465ba42b6SBarry Smith . -tao_view - Calls `TaoView()` at the end of `TaoSolve()` 565a7e14dcfSSatish Balay 56667be906fSBarry Smith Level: beginner 56767be906fSBarry Smith 568a7e14dcfSSatish Balay Notes: 569a7e14dcfSSatish Balay The available visualization contexts include 57065ba42b6SBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 57165ba42b6SBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 572a7e14dcfSSatish Balay output where only the first processor opens 573a7e14dcfSSatish Balay the file. All other processors send their 574a7e14dcfSSatish Balay data to the first processor to print. 575a7e14dcfSSatish Balay 57647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `PetscViewerASCIIOpen()` 577a7e14dcfSSatish Balay @*/ 578d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoView(Tao tao, PetscViewer viewer) 579d71ae5a4SJacob Faibussowitsch { 580a7e14dcfSSatish Balay PetscBool isascii, isstring; 581b625d6c7SJed Brown TaoType type; 58247a47007SBarry Smith 583a7e14dcfSSatish Balay PetscFunctionBegin; 584441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 58548a46eb9SPierre Jolivet if (!viewer) PetscCall(PetscViewerASCIIGetStdout(((PetscObject)tao)->comm, &viewer)); 586a7e14dcfSSatish Balay PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 587a7e14dcfSSatish Balay PetscCheckSameComm(tao, 1, viewer, 2); 588a7e14dcfSSatish Balay 5899566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 5909566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 591a7e14dcfSSatish Balay if (isascii) { 5929566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tao, viewer)); 593a7e14dcfSSatish Balay 594a7e14dcfSSatish Balay if (tao->ops->view) { 5959566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 596dbbe0bcdSBarry Smith PetscUseTypeMethod(tao, view, viewer); 5979566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 598a7e14dcfSSatish Balay } 5998d3f3ddaSAlp Dener if (tao->linesearch) { 6009566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 6019566063dSJacob Faibussowitsch PetscCall(TaoLineSearchView(tao->linesearch, viewer)); 6029566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 6038d3f3ddaSAlp Dener } 604a7e14dcfSSatish Balay if (tao->ksp) { 6059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 6069566063dSJacob Faibussowitsch PetscCall(KSPView(tao->ksp, viewer)); 60763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total KSP iterations: %" PetscInt_FMT "\n", tao->ksp_tot_its)); 6089566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 609a7e14dcfSSatish Balay } 61019b5c101SAlp Dener 6119566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 61219b5c101SAlp Dener 61348a46eb9SPierre Jolivet if (tao->XL || tao->XU) PetscCall(PetscViewerASCIIPrintf(viewer, "Active Set subset type: %s\n", TaoSubSetTypes[tao->subset_type])); 614a7e14dcfSSatish Balay 6159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: gatol=%g,", (double)tao->gatol)); 6169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " steptol=%g,", (double)tao->steptol)); 6179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " gttol=%g\n", (double)tao->gttol)); 6189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Function/Gradient:=%g\n", (double)tao->residual)); 619a7e14dcfSSatish Balay 6206246e8cdSAlp Dener if (tao->constrained) { 6219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances:")); 6229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " catol=%g,", (double)tao->catol)); 6239566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " crtol=%g\n", (double)tao->crtol)); 6249566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Residual in Constraints:=%g\n", (double)tao->cnorm)); 625a7e14dcfSSatish Balay } 626a7e14dcfSSatish Balay 627a7e14dcfSSatish Balay if (tao->trust < tao->steptol) { 6289566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: steptol=%g\n", (double)tao->steptol)); 6299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Final trust region radius:=%g\n", (double)tao->trust)); 630a7e14dcfSSatish Balay } 631a7e14dcfSSatish Balay 63248a46eb9SPierre Jolivet if (tao->fmin > -1.e25) PetscCall(PetscViewerASCIIPrintf(viewer, "convergence tolerances: function minimum=%g\n", (double)tao->fmin)); 6339566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Objective value=%g\n", (double)tao->fc)); 634a7e14dcfSSatish Balay 63563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of iterations=%" PetscInt_FMT ", ", tao->niter)); 63663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_it)); 637a7e14dcfSSatish Balay 638a7e14dcfSSatish Balay if (tao->nfuncs > 0) { 63963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function evaluations=%" PetscInt_FMT ",", tao->nfuncs)); 64063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " max: %" PetscInt_FMT "\n", tao->max_funcs)); 641a7e14dcfSSatish Balay } 642a7e14dcfSSatish Balay if (tao->ngrads > 0) { 64363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of gradient evaluations=%" PetscInt_FMT ",", tao->ngrads)); 64463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " max: %" PetscInt_FMT "\n", tao->max_funcs)); 645a7e14dcfSSatish Balay } 646a7e14dcfSSatish Balay if (tao->nfuncgrads > 0) { 64763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "total number of function/gradient evaluations=%" PetscInt_FMT ",", tao->nfuncgrads)); 64863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " (max: %" PetscInt_FMT ")\n", tao->max_funcs)); 649a7e14dcfSSatish Balay } 65048a46eb9SPierre Jolivet if (tao->nhess > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Hessian evaluations=%" PetscInt_FMT "\n", tao->nhess)); 65148a46eb9SPierre Jolivet if (tao->nconstraints > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of constraint function evaluations=%" PetscInt_FMT "\n", tao->nconstraints)); 65248a46eb9SPierre Jolivet if (tao->njac > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "total number of Jacobian evaluations=%" PetscInt_FMT "\n", tao->njac)); 653a7e14dcfSSatish Balay 654a7e14dcfSSatish Balay if (tao->reason > 0) { 6559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solution converged: ")); 656a7e14dcfSSatish Balay switch (tao->reason) { 657d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GATOL: 658d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)|| <= gatol\n")); 659d71ae5a4SJacob Faibussowitsch break; 660d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GRTOL: 661d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/|f(X)| <= grtol\n")); 662d71ae5a4SJacob Faibussowitsch break; 663d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_GTTOL: 664d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||g(X)||/||g(X0)|| <= gttol\n")); 665d71ae5a4SJacob Faibussowitsch break; 666d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_STEPTOL: 667d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Steptol -- step size small\n")); 668d71ae5a4SJacob Faibussowitsch break; 669d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_MINF: 670d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Minf -- f < fmin\n")); 671d71ae5a4SJacob Faibussowitsch break; 672d71ae5a4SJacob Faibussowitsch case TAO_CONVERGED_USER: 673d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 674d71ae5a4SJacob Faibussowitsch break; 675d71ae5a4SJacob Faibussowitsch default: 676d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "\n")); 677d71ae5a4SJacob Faibussowitsch break; 678a7e14dcfSSatish Balay } 679a7e14dcfSSatish Balay } else { 6809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Solver terminated: %d", tao->reason)); 681a7e14dcfSSatish Balay switch (tao->reason) { 682d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXITS: 683d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Iterations\n")); 684d71ae5a4SJacob Faibussowitsch break; 685d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_NAN: 686d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " NAN or Inf encountered\n")); 687d71ae5a4SJacob Faibussowitsch break; 688d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_MAXFCN: 689d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Maximum Function Evaluations\n")); 690d71ae5a4SJacob Faibussowitsch break; 691d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_LS_FAILURE: 692d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Line Search Failure\n")); 693d71ae5a4SJacob Faibussowitsch break; 694d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_TR_REDUCTION: 695d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Trust Region too small\n")); 696d71ae5a4SJacob Faibussowitsch break; 697d71ae5a4SJacob Faibussowitsch case TAO_DIVERGED_USER: 698d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " User Terminated\n")); 699d71ae5a4SJacob Faibussowitsch break; 700d71ae5a4SJacob Faibussowitsch default: 701d71ae5a4SJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "\n")); 702d71ae5a4SJacob Faibussowitsch break; 703a7e14dcfSSatish Balay } 704a7e14dcfSSatish Balay } 7059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 706a7e14dcfSSatish Balay } else if (isstring) { 7079566063dSJacob Faibussowitsch PetscCall(TaoGetType(tao, &type)); 7089566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " %-3.3s", type)); 709a7e14dcfSSatish Balay } 7103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 711a7e14dcfSSatish Balay } 712a7e14dcfSSatish Balay 713a7e14dcfSSatish Balay /*@ 714414d97d3SAlp Dener TaoSetRecycleHistory - Sets the boolean flag to enable/disable re-using 71565ba42b6SBarry Smith iterate information from the previous `TaoSolve()`. This feature is disabled by 716414d97d3SAlp Dener default. 717414d97d3SAlp Dener 71867be906fSBarry Smith Logically Collective 71967be906fSBarry Smith 72067be906fSBarry Smith Input Parameters: 72147450a7bSBarry Smith + tao - the `Tao` context 72267be906fSBarry Smith - recycle - boolean flag 72367be906fSBarry Smith 72447450a7bSBarry Smith Options Database Key: 72567be906fSBarry Smith . -tao_recycle_history <true,false> - reuse the history 72667be906fSBarry Smith 72767be906fSBarry Smith Level: intermediate 72867be906fSBarry Smith 72967be906fSBarry Smith Notes: 73065ba42b6SBarry Smith For conjugate gradient methods (`TAOBNCG`), this re-uses the latest search direction 73165ba42b6SBarry Smith from the previous `TaoSolve()` call when computing the first search direction in a 732414d97d3SAlp Dener new solution. By default, CG methods set the first search direction to the 733414d97d3SAlp Dener negative gradient. 734414d97d3SAlp Dener 73565ba42b6SBarry Smith For quasi-Newton family of methods (`TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL`), this re-uses 73665ba42b6SBarry Smith the accumulated quasi-Newton Hessian approximation from the previous `TaoSolve()` 737414d97d3SAlp Dener call. By default, QN family of methods reset the initial Hessian approximation to 738414d97d3SAlp Dener the identity matrix. 739414d97d3SAlp Dener 740414d97d3SAlp Dener For any other algorithm, this setting has no effect. 741414d97d3SAlp Dener 74247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 743414d97d3SAlp Dener @*/ 744d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetRecycleHistory(Tao tao, PetscBool recycle) 745d71ae5a4SJacob Faibussowitsch { 746414d97d3SAlp Dener PetscFunctionBegin; 747414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 74894511df7SStefano Zampini PetscValidLogicalCollectiveBool(tao, recycle, 2); 749414d97d3SAlp Dener tao->recycle = recycle; 7503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 751414d97d3SAlp Dener } 752414d97d3SAlp Dener 753414d97d3SAlp Dener /*@ 754414d97d3SAlp Dener TaoGetRecycleHistory - Retrieve the boolean flag for re-using iterate information 75565ba42b6SBarry Smith from the previous `TaoSolve()`. This feature is disabled by default. 756414d97d3SAlp Dener 75767be906fSBarry Smith Logically Collective 758414d97d3SAlp Dener 75947450a7bSBarry Smith Input Parameter: 76047450a7bSBarry Smith . tao - the `Tao` context 761414d97d3SAlp Dener 76247450a7bSBarry Smith Output Parameter: 763147403d9SBarry Smith . recycle - boolean flag 764414d97d3SAlp Dener 765414d97d3SAlp Dener Level: intermediate 766414d97d3SAlp Dener 76747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetRecycleHistory()`, `TAOBNCG`, `TAOBQNLS`, `TAOBQNKLS`, `TAOBQNKTR`, `TAOBQNKTL` 768414d97d3SAlp Dener @*/ 769d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetRecycleHistory(Tao tao, PetscBool *recycle) 770d71ae5a4SJacob Faibussowitsch { 771414d97d3SAlp Dener PetscFunctionBegin; 772414d97d3SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 77394511df7SStefano Zampini PetscValidBoolPointer(recycle, 2); 774414d97d3SAlp Dener *recycle = tao->recycle; 7753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 776414d97d3SAlp Dener } 777414d97d3SAlp Dener 778414d97d3SAlp Dener /*@ 77947450a7bSBarry Smith TaoSetTolerances - Sets parameters used in `TaoSolve()` convergence tests 780a7e14dcfSSatish Balay 78167be906fSBarry Smith Logically Collective 782a7e14dcfSSatish Balay 783a7e14dcfSSatish Balay Input Parameters: 78447450a7bSBarry Smith + tao - the `Tao` context 785a7e14dcfSSatish Balay . gatol - stop if norm of gradient is less than this 786a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 787a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by this factor 788a7e14dcfSSatish Balay 789a7e14dcfSSatish Balay Options Database Keys: 790e52336cbSBarry Smith + -tao_gatol <gatol> - Sets gatol 791a7e14dcfSSatish Balay . -tao_grtol <grtol> - Sets grtol 792a7e14dcfSSatish Balay - -tao_gttol <gttol> - Sets gttol 793a7e14dcfSSatish Balay 794a7e14dcfSSatish Balay Stopping Criteria: 79567be906fSBarry Smith .vb 79667be906fSBarry Smith ||g(X)|| <= gatol 79767be906fSBarry Smith ||g(X)|| / |f(X)| <= grtol 79867be906fSBarry Smith ||g(X)|| / ||g(X0)|| <= gttol 79967be906fSBarry Smith .ve 800a7e14dcfSSatish Balay 801a7e14dcfSSatish Balay Level: beginner 802a7e14dcfSSatish Balay 80367be906fSBarry Smith Note: 80467be906fSBarry Smith Use `PETSC_DEFAULT` to leave one or more tolerances unchanged. 805a7e14dcfSSatish Balay 80647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()` 807a7e14dcfSSatish Balay @*/ 808d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol) 809d71ae5a4SJacob Faibussowitsch { 810a7e14dcfSSatish Balay PetscFunctionBegin; 811441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 81294511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gatol, 2); 81394511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, grtol, 3); 81494511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, gttol, 4); 815a7e14dcfSSatish Balay 81613bcc0bdSJacob Faibussowitsch if (gatol != (PetscReal)PETSC_DEFAULT) { 817a7e14dcfSSatish Balay if (gatol < 0) { 8189566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative gatol -- ignored.\n")); 819a7e14dcfSSatish Balay } else { 820a7e14dcfSSatish Balay tao->gatol = PetscMax(0, gatol); 8216552cf8aSJason Sarich tao->gatol_changed = PETSC_TRUE; 822a7e14dcfSSatish Balay } 823a7e14dcfSSatish Balay } 824a7e14dcfSSatish Balay 82513bcc0bdSJacob Faibussowitsch if (grtol != (PetscReal)PETSC_DEFAULT) { 826a7e14dcfSSatish Balay if (grtol < 0) { 8279566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative grtol -- ignored.\n")); 828a7e14dcfSSatish Balay } else { 829a7e14dcfSSatish Balay tao->grtol = PetscMax(0, grtol); 8306552cf8aSJason Sarich tao->grtol_changed = PETSC_TRUE; 831a7e14dcfSSatish Balay } 832a7e14dcfSSatish Balay } 833a7e14dcfSSatish Balay 83413bcc0bdSJacob Faibussowitsch if (gttol != (PetscReal)PETSC_DEFAULT) { 835a7e14dcfSSatish Balay if (gttol < 0) { 8369566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative gttol -- ignored.\n")); 837a7e14dcfSSatish Balay } else { 838a7e14dcfSSatish Balay tao->gttol = PetscMax(0, gttol); 8396552cf8aSJason Sarich tao->gttol_changed = PETSC_TRUE; 840a7e14dcfSSatish Balay } 841a7e14dcfSSatish Balay } 8423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 843a7e14dcfSSatish Balay } 844a7e14dcfSSatish Balay 845a7e14dcfSSatish Balay /*@ 84647450a7bSBarry Smith TaoSetConstraintTolerances - Sets constraint tolerance parameters used in `TaoSolve()` convergence tests 847a7e14dcfSSatish Balay 84867be906fSBarry Smith Logically Collective 849a7e14dcfSSatish Balay 850a7e14dcfSSatish Balay Input Parameters: 85147450a7bSBarry Smith + tao - the `Tao` context 85267be906fSBarry Smith . catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria 85367be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria 854a7e14dcfSSatish Balay 855a7e14dcfSSatish Balay Options Database Keys: 856a7e14dcfSSatish Balay + -tao_catol <catol> - Sets catol 857a7e14dcfSSatish Balay - -tao_crtol <crtol> - Sets crtol 858a7e14dcfSSatish Balay 859a7e14dcfSSatish Balay Level: intermediate 860a7e14dcfSSatish Balay 86167be906fSBarry Smith Notes: 86267be906fSBarry Smith Use `PETSC_DEFAULT` to leave any tolerance unchanged. 863a7e14dcfSSatish Balay 86447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason`, `TaoGetTolerances()`, `TaoGetConstraintTolerances()`, `TaoSetTolerances()` 865a7e14dcfSSatish Balay @*/ 866d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol) 867d71ae5a4SJacob Faibussowitsch { 868a7e14dcfSSatish Balay PetscFunctionBegin; 869441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 87094511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, catol, 2); 87194511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, crtol, 3); 872a7e14dcfSSatish Balay 87313bcc0bdSJacob Faibussowitsch if (catol != (PetscReal)PETSC_DEFAULT) { 874a7e14dcfSSatish Balay if (catol < 0) { 8759566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative catol -- ignored.\n")); 876a7e14dcfSSatish Balay } else { 877a7e14dcfSSatish Balay tao->catol = PetscMax(0, catol); 8786552cf8aSJason Sarich tao->catol_changed = PETSC_TRUE; 879a7e14dcfSSatish Balay } 880a7e14dcfSSatish Balay } 881a7e14dcfSSatish Balay 88213bcc0bdSJacob Faibussowitsch if (crtol != (PetscReal)PETSC_DEFAULT) { 883a7e14dcfSSatish Balay if (crtol < 0) { 8849566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tried to set negative crtol -- ignored.\n")); 885a7e14dcfSSatish Balay } else { 886a7e14dcfSSatish Balay tao->crtol = PetscMax(0, crtol); 8876552cf8aSJason Sarich tao->crtol_changed = PETSC_TRUE; 888a7e14dcfSSatish Balay } 889a7e14dcfSSatish Balay } 8903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 891a7e14dcfSSatish Balay } 892a7e14dcfSSatish Balay 89358417fe7SBarry Smith /*@ 89447450a7bSBarry Smith TaoGetConstraintTolerances - Gets constraint tolerance parameters used in `TaoSolve()` convergence tests 89558417fe7SBarry Smith 89667be906fSBarry Smith Not Collective 89758417fe7SBarry Smith 89858417fe7SBarry Smith Input Parameter: 89947450a7bSBarry Smith . tao - the `Tao` context 90058417fe7SBarry Smith 901d8d19677SJose E. Roman Output Parameters: 90267be906fSBarry Smith + catol - absolute constraint tolerance, constraint norm must be less than `catol` for used for gatol convergence criteria 90367be906fSBarry Smith - crtol - relative constraint tolerance, constraint norm must be less than `crtol` for used for gatol, gttol convergence criteria 90458417fe7SBarry Smith 90558417fe7SBarry Smith Level: intermediate 90658417fe7SBarry Smith 90747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReasons`,`TaoGetTolerances()`, `TaoSetTolerances()`, `TaoSetConstraintTolerances()` 90858417fe7SBarry Smith @*/ 909d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol) 910d71ae5a4SJacob Faibussowitsch { 91158417fe7SBarry Smith PetscFunctionBegin; 91258417fe7SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 91358417fe7SBarry Smith if (catol) *catol = tao->catol; 91458417fe7SBarry Smith if (crtol) *crtol = tao->crtol; 9153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 91658417fe7SBarry Smith } 91758417fe7SBarry Smith 918a7e14dcfSSatish Balay /*@ 919a7e14dcfSSatish Balay TaoSetFunctionLowerBound - Sets a bound on the solution objective value. 920a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 921a7e14dcfSSatish Balay has been found, the solver will terminate. 922a7e14dcfSSatish Balay 923c3339decSBarry Smith Logically Collective 924a7e14dcfSSatish Balay 925a7e14dcfSSatish Balay Input Parameters: 926441846f8SBarry Smith + tao - the Tao solver context 927a7e14dcfSSatish Balay - fmin - the tolerance 928a7e14dcfSSatish Balay 92947450a7bSBarry Smith Options Database Key: 930a7e14dcfSSatish Balay . -tao_fmin <fmin> - sets the minimum function value 931a7e14dcfSSatish Balay 932a7e14dcfSSatish Balay Level: intermediate 933a7e14dcfSSatish Balay 93447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason`, `TaoSetTolerances()` 935a7e14dcfSSatish Balay @*/ 936d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetFunctionLowerBound(Tao tao, PetscReal fmin) 937d71ae5a4SJacob Faibussowitsch { 938a7e14dcfSSatish Balay PetscFunctionBegin; 939441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 94094511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, fmin, 2); 941a7e14dcfSSatish Balay tao->fmin = fmin; 9426552cf8aSJason Sarich tao->fmin_changed = PETSC_TRUE; 9433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 944a7e14dcfSSatish Balay } 945a7e14dcfSSatish Balay 946a7e14dcfSSatish Balay /*@ 9478e96d397SJason Sarich TaoGetFunctionLowerBound - Gets the bound on the solution objective value. 948a7e14dcfSSatish Balay When an approximate solution with an objective value below this number 949a7e14dcfSSatish Balay has been found, the solver will terminate. 950a7e14dcfSSatish Balay 95167be906fSBarry Smith Not Collective 952a7e14dcfSSatish Balay 95347450a7bSBarry Smith Input Parameter: 95447450a7bSBarry Smith . tao - the `Tao` solver context 955a7e14dcfSSatish Balay 95647450a7bSBarry Smith OutputParameter: 957a7e14dcfSSatish Balay . fmin - the minimum function value 958a7e14dcfSSatish Balay 959a7e14dcfSSatish Balay Level: intermediate 960a7e14dcfSSatish Balay 96147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason`, `TaoSetFunctionLowerBound()` 962a7e14dcfSSatish Balay @*/ 963d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetFunctionLowerBound(Tao tao, PetscReal *fmin) 964d71ae5a4SJacob Faibussowitsch { 965a7e14dcfSSatish Balay PetscFunctionBegin; 966441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 96794511df7SStefano Zampini PetscValidRealPointer(fmin, 2); 968a7e14dcfSSatish Balay *fmin = tao->fmin; 9693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 970a7e14dcfSSatish Balay } 971a7e14dcfSSatish Balay 972a7e14dcfSSatish Balay /*@ 97347450a7bSBarry Smith TaoSetMaximumFunctionEvaluations - Sets a maximum number of function evaluations allowed for a `TaoSolve()`. 974a7e14dcfSSatish Balay 975c3339decSBarry Smith Logically Collective 976a7e14dcfSSatish Balay 977a7e14dcfSSatish Balay Input Parameters: 97847450a7bSBarry Smith + tao - the `Tao` solver context 979a7e14dcfSSatish Balay - nfcn - the maximum number of function evaluations (>=0) 980a7e14dcfSSatish Balay 98147450a7bSBarry Smith Options Database Key: 982a7e14dcfSSatish Balay . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations 983a7e14dcfSSatish Balay 984a7e14dcfSSatish Balay Level: intermediate 985a7e14dcfSSatish Balay 98647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumIterations()` 987a7e14dcfSSatish Balay @*/ 988d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao, PetscInt nfcn) 989d71ae5a4SJacob Faibussowitsch { 990a7e14dcfSSatish Balay PetscFunctionBegin; 991441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 99294511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, nfcn, 2); 9939371c9d4SSatish Balay if (nfcn >= 0) { 9949371c9d4SSatish Balay tao->max_funcs = PetscMax(0, nfcn); 9959371c9d4SSatish Balay } else { 9969371c9d4SSatish Balay tao->max_funcs = -1; 9979371c9d4SSatish Balay } 9986552cf8aSJason Sarich tao->max_funcs_changed = PETSC_TRUE; 9993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1000a7e14dcfSSatish Balay } 1001a7e14dcfSSatish Balay 1002a7e14dcfSSatish Balay /*@ 100347450a7bSBarry Smith TaoGetMaximumFunctionEvaluations - Gets a maximum number of function evaluations allowed for a `TaoSolve()` 1004a7e14dcfSSatish Balay 1005c3339decSBarry Smith Logically Collective 1006a7e14dcfSSatish Balay 100747450a7bSBarry Smith Input Parameter: 100847450a7bSBarry Smith . tao - the `Tao` solver context 1009a7e14dcfSSatish Balay 101047450a7bSBarry Smith Output Parameter: 1011a7e14dcfSSatish Balay . nfcn - the maximum number of function evaluations 1012a7e14dcfSSatish Balay 1013a7e14dcfSSatish Balay Level: intermediate 1014a7e14dcfSSatish Balay 101547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1016a7e14dcfSSatish Balay @*/ 1017d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao, PetscInt *nfcn) 1018d71ae5a4SJacob Faibussowitsch { 1019a7e14dcfSSatish Balay PetscFunctionBegin; 1020441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 102194511df7SStefano Zampini PetscValidIntPointer(nfcn, 2); 1022a7e14dcfSSatish Balay *nfcn = tao->max_funcs; 10233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1024a7e14dcfSSatish Balay } 1025a7e14dcfSSatish Balay 1026770232b9SCe Qin /*@ 102747450a7bSBarry Smith TaoGetCurrentFunctionEvaluations - Get current number of function evaluations used by a `Tao` object 1028770232b9SCe Qin 1029770232b9SCe Qin Not Collective 1030770232b9SCe Qin 103147450a7bSBarry Smith Input Parameter: 103247450a7bSBarry Smith . tao - the `Tao` solver context 1033770232b9SCe Qin 103447450a7bSBarry Smith Output Parameter: 103594511df7SStefano Zampini . nfuncs - the current number of function evaluations (maximum between gradient and function evaluations) 1036770232b9SCe Qin 1037770232b9SCe Qin Level: intermediate 1038770232b9SCe Qin 103947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetMaximumFunctionEvaluations()`, `TaoGetMaximumFunctionEvaluations()`, `TaoGetMaximumIterations()` 1040770232b9SCe Qin @*/ 1041d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao, PetscInt *nfuncs) 1042d71ae5a4SJacob Faibussowitsch { 1043770232b9SCe Qin PetscFunctionBegin; 1044770232b9SCe Qin PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 104594511df7SStefano Zampini PetscValidIntPointer(nfuncs, 2); 1046770232b9SCe Qin *nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 10473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1048770232b9SCe Qin } 1049770232b9SCe Qin 1050a7e14dcfSSatish Balay /*@ 105147450a7bSBarry Smith TaoSetMaximumIterations - Sets a maximum number of iterates to be used in `TaoSolve()` 1052a7e14dcfSSatish Balay 1053c3339decSBarry Smith Logically Collective 1054a7e14dcfSSatish Balay 1055a7e14dcfSSatish Balay Input Parameters: 105647450a7bSBarry Smith + tao - the `Tao` solver context 1057a7e14dcfSSatish Balay - maxits - the maximum number of iterates (>=0) 1058a7e14dcfSSatish Balay 105947450a7bSBarry Smith Options Database Key: 1060a7e14dcfSSatish Balay . -tao_max_it <its> - sets the maximum number of iterations 1061a7e14dcfSSatish Balay 1062a7e14dcfSSatish Balay Level: intermediate 1063a7e14dcfSSatish Balay 106447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetTolerances()`, `TaoSetMaximumFunctionEvaluations()` 1065a7e14dcfSSatish Balay @*/ 1066d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMaximumIterations(Tao tao, PetscInt maxits) 1067d71ae5a4SJacob Faibussowitsch { 1068a7e14dcfSSatish Balay PetscFunctionBegin; 1069441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 107094511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, maxits, 2); 107147a47007SBarry Smith tao->max_it = PetscMax(0, maxits); 10726552cf8aSJason Sarich tao->max_it_changed = PETSC_TRUE; 10733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1074a7e14dcfSSatish Balay } 1075a7e14dcfSSatish Balay 1076a7e14dcfSSatish Balay /*@ 107765ba42b6SBarry Smith TaoGetMaximumIterations - Gets a maximum number of iterates that will be used 1078a7e14dcfSSatish Balay 1079a7e14dcfSSatish Balay Not Collective 1080a7e14dcfSSatish Balay 108147450a7bSBarry Smith Input Parameter: 108247450a7bSBarry Smith . tao - the `Tao` solver context 1083a7e14dcfSSatish Balay 108447450a7bSBarry Smith Output Parameter: 1085a7e14dcfSSatish Balay . maxits - the maximum number of iterates 1086a7e14dcfSSatish Balay 1087a7e14dcfSSatish Balay Level: intermediate 1088a7e14dcfSSatish Balay 108947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetMaximumIterations()`, `TaoGetMaximumFunctionEvaluations()` 1090a7e14dcfSSatish Balay @*/ 1091d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetMaximumIterations(Tao tao, PetscInt *maxits) 1092d71ae5a4SJacob Faibussowitsch { 1093a7e14dcfSSatish Balay PetscFunctionBegin; 1094441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 109594511df7SStefano Zampini PetscValidIntPointer(maxits, 2); 1096a7e14dcfSSatish Balay *maxits = tao->max_it; 10973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1098a7e14dcfSSatish Balay } 1099a7e14dcfSSatish Balay 1100a7e14dcfSSatish Balay /*@ 1101a7e14dcfSSatish Balay TaoSetInitialTrustRegionRadius - Sets the initial trust region radius. 1102a7e14dcfSSatish Balay 110367be906fSBarry Smith Logically Collective 1104a7e14dcfSSatish Balay 1105d8d19677SJose E. Roman Input Parameters: 110647450a7bSBarry Smith + tao - a `Tao` optimization solver 1107a7e14dcfSSatish Balay - radius - the trust region radius 1108a7e14dcfSSatish Balay 1109a7e14dcfSSatish Balay Options Database Key: 1110a7e14dcfSSatish Balay . -tao_trust0 <t0> - sets initial trust region radius 1111a7e14dcfSSatish Balay 111247450a7bSBarry Smith Level: intermediate 111347450a7bSBarry Smith 111447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetTrustRegionRadius()`, `TaoSetTrustRegionTolerance()`, `TAONTR` 1115a7e14dcfSSatish Balay @*/ 1116d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius) 1117d71ae5a4SJacob Faibussowitsch { 1118a7e14dcfSSatish Balay PetscFunctionBegin; 1119441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 112094511df7SStefano Zampini PetscValidLogicalCollectiveReal(tao, radius, 2); 1121a7e14dcfSSatish Balay tao->trust0 = PetscMax(0.0, radius); 11226552cf8aSJason Sarich tao->trust0_changed = PETSC_TRUE; 11233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1124a7e14dcfSSatish Balay } 1125a7e14dcfSSatish Balay 1126a7e14dcfSSatish Balay /*@ 112765ba42b6SBarry Smith TaoGetInitialTrustRegionRadius - Gets the initial trust region radius. 1128a7e14dcfSSatish Balay 1129a7e14dcfSSatish Balay Not Collective 1130a7e14dcfSSatish Balay 1131a7e14dcfSSatish Balay Input Parameter: 113247450a7bSBarry Smith . tao - a `Tao` optimization solver 1133a7e14dcfSSatish Balay 1134a7e14dcfSSatish Balay Output Parameter: 1135a7e14dcfSSatish Balay . radius - the trust region radius 1136a7e14dcfSSatish Balay 1137a7e14dcfSSatish Balay Level: intermediate 1138a7e14dcfSSatish Balay 113947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetCurrentTrustRegionRadius()`, `TAONTR` 1140a7e14dcfSSatish Balay @*/ 1141d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius) 1142d71ae5a4SJacob Faibussowitsch { 1143a7e14dcfSSatish Balay PetscFunctionBegin; 1144441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 114594511df7SStefano Zampini PetscValidRealPointer(radius, 2); 1146a7e14dcfSSatish Balay *radius = tao->trust0; 11473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1148a7e14dcfSSatish Balay } 1149a7e14dcfSSatish Balay 1150a7e14dcfSSatish Balay /*@ 1151a7e14dcfSSatish Balay TaoGetCurrentTrustRegionRadius - Gets the current trust region radius. 1152a7e14dcfSSatish Balay 1153a7e14dcfSSatish Balay Not Collective 1154a7e14dcfSSatish Balay 1155a7e14dcfSSatish Balay Input Parameter: 115647450a7bSBarry Smith . tao - a `Tao` optimization solver 1157a7e14dcfSSatish Balay 1158a7e14dcfSSatish Balay Output Parameter: 1159a7e14dcfSSatish Balay . radius - the trust region radius 1160a7e14dcfSSatish Balay 1161a7e14dcfSSatish Balay Level: intermediate 1162a7e14dcfSSatish Balay 116347450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetInitialTrustRegionRadius()`, `TaoGetInitialTrustRegionRadius()`, `TAONTR` 1164a7e14dcfSSatish Balay @*/ 1165d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius) 1166d71ae5a4SJacob Faibussowitsch { 1167a7e14dcfSSatish Balay PetscFunctionBegin; 1168441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 116994511df7SStefano Zampini PetscValidRealPointer(radius, 2); 1170a7e14dcfSSatish Balay *radius = tao->trust; 11713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1172a7e14dcfSSatish Balay } 1173a7e14dcfSSatish Balay 1174a7e14dcfSSatish Balay /*@ 117547450a7bSBarry Smith TaoGetTolerances - gets the current values of some tolerances used for the convergence testing of `TaoSolve()` 1176a7e14dcfSSatish Balay 1177a7e14dcfSSatish Balay Not Collective 1178a7e14dcfSSatish Balay 1179f899ff85SJose E. Roman Input Parameter: 118047450a7bSBarry Smith . tao - the `Tao` context 1181a7e14dcfSSatish Balay 1182a7e14dcfSSatish Balay Output Parameters: 1183e52336cbSBarry Smith + gatol - stop if norm of gradient is less than this 1184a7e14dcfSSatish Balay . grtol - stop if relative norm of gradient is less than this 1185a7e14dcfSSatish Balay - gttol - stop if norm of gradient is reduced by a this factor 1186a7e14dcfSSatish Balay 1187a7e14dcfSSatish Balay Level: intermediate 1188a1cb98faSBarry Smith 1189a1cb98faSBarry Smith Note: 119047450a7bSBarry Smith `NULL` can be used as an argument if not all tolerances values are needed 1191a1cb98faSBarry Smith 119247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetTolerances()` 1193a7e14dcfSSatish Balay @*/ 1194d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol) 1195d71ae5a4SJacob Faibussowitsch { 1196a7e14dcfSSatish Balay PetscFunctionBegin; 1197441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1198a7e14dcfSSatish Balay if (gatol) *gatol = tao->gatol; 1199a7e14dcfSSatish Balay if (grtol) *grtol = tao->grtol; 1200a7e14dcfSSatish Balay if (gttol) *gttol = tao->gttol; 12013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1202a7e14dcfSSatish Balay } 1203a7e14dcfSSatish Balay 1204a7e14dcfSSatish Balay /*@ 1205a7e14dcfSSatish Balay TaoGetKSP - Gets the linear solver used by the optimization solver. 1206a7e14dcfSSatish Balay 1207a7e14dcfSSatish Balay Not Collective 1208a7e14dcfSSatish Balay 120947450a7bSBarry Smith Input Parameter: 121047450a7bSBarry Smith . tao - the `Tao` solver 1211a7e14dcfSSatish Balay 121247450a7bSBarry Smith Output Parameter: 121347450a7bSBarry Smith . ksp - the `KSP` linear solver used in the optimization solver 1214a7e14dcfSSatish Balay 1215a7e14dcfSSatish Balay Level: intermediate 1216a7e14dcfSSatish Balay 121747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `KSP` 1218a7e14dcfSSatish Balay @*/ 1219d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp) 1220d71ae5a4SJacob Faibussowitsch { 1221a7e14dcfSSatish Balay PetscFunctionBegin; 122294511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 122394511df7SStefano Zampini PetscValidPointer(ksp, 2); 1224a7e14dcfSSatish Balay *ksp = tao->ksp; 12253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1226a7e14dcfSSatish Balay } 1227a7e14dcfSSatish Balay 1228025e9500SJason Sarich /*@ 1229025e9500SJason Sarich TaoGetLinearSolveIterations - Gets the total number of linear iterations 123047450a7bSBarry Smith used by the `Tao` solver 1231025e9500SJason Sarich 1232025e9500SJason Sarich Not Collective 1233025e9500SJason Sarich 1234025e9500SJason Sarich Input Parameter: 123547450a7bSBarry Smith . tao - the `Tao` context 1236025e9500SJason Sarich 1237025e9500SJason Sarich Output Parameter: 1238025e9500SJason Sarich . lits - number of linear iterations 1239025e9500SJason Sarich 1240025e9500SJason Sarich Level: intermediate 1241025e9500SJason Sarich 124247450a7bSBarry Smith Note: 124347450a7bSBarry Smith This counter is reset to zero for each successive call to `TaoSolve()` 124447450a7bSBarry Smith 124547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetKSP()` 1246025e9500SJason Sarich @*/ 1247d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLinearSolveIterations(Tao tao, PetscInt *lits) 1248d71ae5a4SJacob Faibussowitsch { 1249025e9500SJason Sarich PetscFunctionBegin; 1250025e9500SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1251025e9500SJason Sarich PetscValidIntPointer(lits, 2); 12522d9aa51bSJason Sarich *lits = tao->ksp_tot_its; 12533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1254025e9500SJason Sarich } 1255025e9500SJason Sarich 1256a7e14dcfSSatish Balay /*@ 1257a7e14dcfSSatish Balay TaoGetLineSearch - Gets the line search used by the optimization solver. 1258a7e14dcfSSatish Balay 1259a7e14dcfSSatish Balay Not Collective 1260a7e14dcfSSatish Balay 126147450a7bSBarry Smith Input Parameter: 126247450a7bSBarry Smith . tao - the `Tao` solver 1263a7e14dcfSSatish Balay 126447450a7bSBarry Smith Output Parameter: 1265a7e14dcfSSatish Balay . ls - the line search used in the optimization solver 1266a7e14dcfSSatish Balay 1267a7e14dcfSSatish Balay Level: intermediate 1268a7e14dcfSSatish Balay 126947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoLineSearch`, `TaoLineSearchType` 1270a7e14dcfSSatish Balay @*/ 1271d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls) 1272d71ae5a4SJacob Faibussowitsch { 1273a7e14dcfSSatish Balay PetscFunctionBegin; 127494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 127594511df7SStefano Zampini PetscValidPointer(ls, 2); 1276a7e14dcfSSatish Balay *ls = tao->linesearch; 12773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1278a7e14dcfSSatish Balay } 1279a7e14dcfSSatish Balay 1280a7e14dcfSSatish Balay /*@ 1281a7e14dcfSSatish Balay TaoAddLineSearchCounts - Adds the number of function evaluations spent 1282a7e14dcfSSatish Balay in the line search to the running total. 1283a7e14dcfSSatish Balay 1284a7e14dcfSSatish Balay Input Parameters: 128547450a7bSBarry Smith + tao - the `Tao` solver 1286a7e14dcfSSatish Balay - ls - the line search used in the optimization solver 1287a7e14dcfSSatish Balay 1288a7e14dcfSSatish Balay Level: developer 1289a7e14dcfSSatish Balay 129047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLineSearch()`, `TaoLineSearchApply()` 1291a7e14dcfSSatish Balay @*/ 1292d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAddLineSearchCounts(Tao tao) 1293d71ae5a4SJacob Faibussowitsch { 1294a7e14dcfSSatish Balay PetscBool flg; 1295a7e14dcfSSatish Balay PetscInt nfeval, ngeval, nfgeval; 129647a47007SBarry Smith 1297a7e14dcfSSatish Balay PetscFunctionBegin; 1298441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1299a7e14dcfSSatish Balay if (tao->linesearch) { 13009566063dSJacob Faibussowitsch PetscCall(TaoLineSearchIsUsingTaoRoutines(tao->linesearch, &flg)); 130194ae4db5SBarry Smith if (!flg) { 13029566063dSJacob Faibussowitsch PetscCall(TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch, &nfeval, &ngeval, &nfgeval)); 1303a7e14dcfSSatish Balay tao->nfuncs += nfeval; 1304a7e14dcfSSatish Balay tao->ngrads += ngeval; 1305a7e14dcfSSatish Balay tao->nfuncgrads += nfgeval; 1306a7e14dcfSSatish Balay } 1307a7e14dcfSSatish Balay } 13083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1309a7e14dcfSSatish Balay } 1310a7e14dcfSSatish Balay 1311a7e14dcfSSatish Balay /*@ 131247450a7bSBarry Smith TaoGetSolution - Returns the vector with the current solution from the `Tao` object 1313a7e14dcfSSatish Balay 1314a7e14dcfSSatish Balay Not Collective 1315a7e14dcfSSatish Balay 1316a7e14dcfSSatish Balay Input Parameter: 131747450a7bSBarry Smith . tao - the `Tao` context 1318a7e14dcfSSatish Balay 1319a7e14dcfSSatish Balay Output Parameter: 1320a7e14dcfSSatish Balay . X - the current solution 1321a7e14dcfSSatish Balay 1322a7e14dcfSSatish Balay Level: intermediate 1323a7e14dcfSSatish Balay 132465ba42b6SBarry Smith Note: 132565ba42b6SBarry Smith The returned vector will be the same object that was passed into `TaoSetSolution()` 132665ba42b6SBarry Smith 132747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetSolution()`, `TaoSolve()` 1328a7e14dcfSSatish Balay @*/ 1329d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolution(Tao tao, Vec *X) 1330d71ae5a4SJacob Faibussowitsch { 1331a7e14dcfSSatish Balay PetscFunctionBegin; 1332441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 133394511df7SStefano Zampini PetscValidPointer(X, 2); 1334a7e14dcfSSatish Balay *X = tao->solution; 13353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1336a7e14dcfSSatish Balay } 1337a7e14dcfSSatish Balay 1338a7e14dcfSSatish Balay /*@ 133947450a7bSBarry Smith TaoResetStatistics - Initialize the statistics collected by the `Tao` object. 1340a7e14dcfSSatish Balay These statistics include the iteration number, residual norms, and convergence status. 1341a7e14dcfSSatish Balay This routine gets called before solving each optimization problem. 1342a7e14dcfSSatish Balay 1343c3339decSBarry Smith Collective 1344a7e14dcfSSatish Balay 134547450a7bSBarry Smith Input Parameter: 134647450a7bSBarry Smith . solver - the `Tao` context 1347a7e14dcfSSatish Balay 1348a7e14dcfSSatish Balay Level: developer 1349a7e14dcfSSatish Balay 135047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoSolve()` 1351a7e14dcfSSatish Balay @*/ 1352d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResetStatistics(Tao tao) 1353d71ae5a4SJacob Faibussowitsch { 1354a7e14dcfSSatish Balay PetscFunctionBegin; 1355441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1356a7e14dcfSSatish Balay tao->niter = 0; 1357a7e14dcfSSatish Balay tao->nfuncs = 0; 1358a7e14dcfSSatish Balay tao->nfuncgrads = 0; 1359a7e14dcfSSatish Balay tao->ngrads = 0; 1360a7e14dcfSSatish Balay tao->nhess = 0; 1361a7e14dcfSSatish Balay tao->njac = 0; 1362a7e14dcfSSatish Balay tao->nconstraints = 0; 1363a7e14dcfSSatish Balay tao->ksp_its = 0; 1364ae93cb3cSJason Sarich tao->ksp_tot_its = 0; 1365a7e14dcfSSatish Balay tao->reason = TAO_CONTINUE_ITERATING; 1366a7e14dcfSSatish Balay tao->residual = 0.0; 1367a7e14dcfSSatish Balay tao->cnorm = 0.0; 1368a7e14dcfSSatish Balay tao->step = 0.0; 1369a7e14dcfSSatish Balay tao->lsflag = PETSC_FALSE; 1370a7e14dcfSSatish Balay if (tao->hist_reset) tao->hist_len = 0; 13713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1372a7e14dcfSSatish Balay } 1373a7e14dcfSSatish Balay 1374a7e14dcfSSatish Balay /*@C 1375e1e80dc8SAlp Dener TaoSetUpdate - Sets the general-purpose update function called 137665ba42b6SBarry Smith at the beginning of every iteration of the optimization algorithm. Specifically 1377e1e80dc8SAlp Dener it is called at the top of every iteration, after the new solution and the gradient 1378e1e80dc8SAlp Dener is determined, but before the Hessian is computed (if applicable). 1379e1e80dc8SAlp Dener 1380c3339decSBarry Smith Logically Collective 1381e1e80dc8SAlp Dener 1382e1e80dc8SAlp Dener Input Parameters: 1383a2b725a8SWilliam Gropp + tao - The tao solver context 1384a2b725a8SWilliam Gropp - func - The function 1385e1e80dc8SAlp Dener 138620f4b53cSBarry Smith Calling sequence of `func`: 138720f4b53cSBarry Smith $ PetscErrorCode func(Tao tao, PetscInt step); 138820f4b53cSBarry Smith + tao - the optimizer context 138920f4b53cSBarry Smith - step - The current step of the iteration 1390e1e80dc8SAlp Dener 1391e1e80dc8SAlp Dener Level: advanced 1392e1e80dc8SAlp Dener 139347450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolve()` 1394e1e80dc8SAlp Dener @*/ 1395d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetUpdate(Tao tao, PetscErrorCode (*func)(Tao, PetscInt, void *), void *ctx) 1396d71ae5a4SJacob Faibussowitsch { 1397e1e80dc8SAlp Dener PetscFunctionBegin; 1398e1e80dc8SAlp Dener PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1399e1e80dc8SAlp Dener tao->ops->update = func; 1400e1e80dc8SAlp Dener tao->user_update = ctx; 14013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1402e1e80dc8SAlp Dener } 1403e1e80dc8SAlp Dener 1404e1e80dc8SAlp Dener /*@C 1405a7e14dcfSSatish Balay TaoSetConvergenceTest - Sets the function that is to be used to test 1406a7e14dcfSSatish Balay for convergence o fthe iterative minimization solution. The new convergence 140765ba42b6SBarry Smith testing routine will replace Tao's default convergence test. 1408a7e14dcfSSatish Balay 1409c3339decSBarry Smith Logically Collective 1410a7e14dcfSSatish Balay 1411a7e14dcfSSatish Balay Input Parameters: 141247450a7bSBarry Smith + tao - the `Tao` object 1413a7e14dcfSSatish Balay . conv - the routine to test for convergence 1414a7e14dcfSSatish Balay - ctx - [optional] context for private data for the convergence routine 141547450a7bSBarry Smith (may be `NULL`) 1416a7e14dcfSSatish Balay 141720f4b53cSBarry Smith Calling sequence of `conv`: 1418441846f8SBarry Smith $ PetscErrorCode conv(Tao tao, void *ctx) 141947450a7bSBarry Smith + tao - the `Tao` object 1420a7e14dcfSSatish Balay - ctx - [optional] convergence context 1421a7e14dcfSSatish Balay 142247450a7bSBarry Smith Level: advanced 142347450a7bSBarry Smith 142465ba42b6SBarry Smith Note: 142565ba42b6SBarry Smith The new convergence testing routine should call `TaoSetConvergedReason()`. 1426a7e14dcfSSatish Balay 142747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolve()`, `TaoSetConvergedReason()`, `TaoGetSolutionStatus()`, `TaoGetTolerances()`, `TaoSetMonitor` 1428a7e14dcfSSatish Balay @*/ 1429d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao, void *), void *ctx) 1430d71ae5a4SJacob Faibussowitsch { 1431a7e14dcfSSatish Balay PetscFunctionBegin; 1432441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 143394511df7SStefano Zampini tao->ops->convergencetest = conv; 143494511df7SStefano Zampini tao->cnvP = ctx; 14353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1436a7e14dcfSSatish Balay } 1437a7e14dcfSSatish Balay 1438a7e14dcfSSatish Balay /*@C 143965ba42b6SBarry Smith TaoSetMonitor - Sets an additional function that is to be used at every 1440a7e14dcfSSatish Balay iteration of the solver to display the iteration's 1441a7e14dcfSSatish Balay progress. 1442a7e14dcfSSatish Balay 1443c3339decSBarry Smith Logically Collective 1444a7e14dcfSSatish Balay 1445a7e14dcfSSatish Balay Input Parameters: 144647450a7bSBarry Smith + tao - the `Tao` solver context 1447a7e14dcfSSatish Balay . mymonitor - monitoring routine 1448a7e14dcfSSatish Balay - mctx - [optional] user-defined context for private data for the 144947450a7bSBarry Smith monitor routine (may be `NULL`) 1450a7e14dcfSSatish Balay 145120f4b53cSBarry Smith Calling sequence of `mymonitor`: 1452147403d9SBarry Smith .vb 1453147403d9SBarry Smith PetscErrorCode mymonitor(Tao tao, void *mctx) 1454147403d9SBarry Smith .ve 145547450a7bSBarry Smith + tao - the `Tao` solver context 1456a7e14dcfSSatish Balay - mctx - [optional] monitoring context 1457a7e14dcfSSatish Balay 1458a7e14dcfSSatish Balay Options Database Keys: 145965ba42b6SBarry Smith + -tao_monitor - sets the default monitor `TaoMonitorDefault()` 1460a7e14dcfSSatish Balay . -tao_smonitor - sets short monitor 1461a7e14dcfSSatish Balay . -tao_cmonitor - same as smonitor plus constraint norm 1462a7e14dcfSSatish Balay . -tao_view_solution - view solution at each iteration 1463a7e14dcfSSatish Balay . -tao_view_gradient - view gradient at each iteration 14644a48860cSAlp Dener . -tao_view_ls_residual - view least-squares residual vector at each iteration 1465a7e14dcfSSatish Balay - -tao_cancelmonitors - cancels all monitors that have been hardwired into a code by calls to TaoSetMonitor(), but does not cancel those set via the options database. 1466a7e14dcfSSatish Balay 146747450a7bSBarry Smith Level: intermediate 146847450a7bSBarry Smith 146987497f52SBarry Smith Notes: 1470a7e14dcfSSatish Balay Several different monitoring routines may be set by calling 147165ba42b6SBarry Smith `TaoSetMonitor()` multiple times; all will be called in the 1472a7e14dcfSSatish Balay order in which they were set. 1473a7e14dcfSSatish Balay 147465ba42b6SBarry Smith Fortran Note: 147595452b02SPatrick Sanan Only one monitor function may be set 1476a7e14dcfSSatish Balay 147747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolve()`, `TaoMonitorDefault()`, `TaoCancelMonitors()`, `TaoSetDestroyRoutine()`, `TaoView()` 1478a7e14dcfSSatish Balay @*/ 1479d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetMonitor(Tao tao, PetscErrorCode (*func)(Tao, void *), void *ctx, PetscErrorCode (*dest)(void **)) 1480d71ae5a4SJacob Faibussowitsch { 148108d19d1fSJason Sarich PetscInt i; 14828732526dSAlp Dener PetscBool identical; 148308d19d1fSJason Sarich 1484a7e14dcfSSatish Balay PetscFunctionBegin; 1485441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 14863c859ba3SBarry Smith PetscCheck(tao->numbermonitors < MAXTAOMONITORS, PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Cannot attach another monitor -- max=%d", MAXTAOMONITORS); 148708d19d1fSJason Sarich 148808d19d1fSJason Sarich for (i = 0; i < tao->numbermonitors; i++) { 14899566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))func, ctx, dest, (PetscErrorCode(*)(void))tao->monitor[i], tao->monitorcontext[i], tao->monitordestroy[i], &identical)); 14903ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 149108d19d1fSJason Sarich } 1492a7e14dcfSSatish Balay tao->monitor[tao->numbermonitors] = func; 14938732526dSAlp Dener tao->monitorcontext[tao->numbermonitors] = (void *)ctx; 1494a7e14dcfSSatish Balay tao->monitordestroy[tao->numbermonitors] = dest; 1495a7e14dcfSSatish Balay ++tao->numbermonitors; 14963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1497a7e14dcfSSatish Balay } 1498a7e14dcfSSatish Balay 1499a7e14dcfSSatish Balay /*@ 150047450a7bSBarry Smith TaoCancelMonitors - Clears all the monitor functions for a `Tao` object. 1501a7e14dcfSSatish Balay 1502c3339decSBarry Smith Logically Collective 1503a7e14dcfSSatish Balay 150447450a7bSBarry Smith Input Parameter: 150547450a7bSBarry Smith . tao - the `Tao` solver context 1506a7e14dcfSSatish Balay 15073c7db156SBarry Smith Options Database Key: 1508a7e14dcfSSatish Balay . -tao_cancelmonitors - cancels all monitors that have been hardwired 150965ba42b6SBarry Smith into a code by calls to `TaoSetMonitor()`, but does not cancel those 1510a7e14dcfSSatish Balay set via the options database 1511a7e14dcfSSatish Balay 1512a7e14dcfSSatish Balay Level: advanced 1513a7e14dcfSSatish Balay 151447450a7bSBarry Smith Note: 151547450a7bSBarry Smith There is no way to clear one specific monitor from a `Tao` object. 151647450a7bSBarry Smith 151747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoMonitorDefault()`, `TaoSetMonitor()` 1518a7e14dcfSSatish Balay @*/ 1519d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoCancelMonitors(Tao tao) 1520d71ae5a4SJacob Faibussowitsch { 1521a7e14dcfSSatish Balay PetscInt i; 152247a47007SBarry Smith 1523a7e14dcfSSatish Balay PetscFunctionBegin; 1524441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1525a7e14dcfSSatish Balay for (i = 0; i < tao->numbermonitors; i++) { 152648a46eb9SPierre Jolivet if (tao->monitordestroy[i]) PetscCall((*tao->monitordestroy[i])(&tao->monitorcontext[i])); 1527a7e14dcfSSatish Balay } 1528a7e14dcfSSatish Balay tao->numbermonitors = 0; 15293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1530a7e14dcfSSatish Balay } 1531a7e14dcfSSatish Balay 15327fab98ebSJason Sarich /*@ 153347450a7bSBarry Smith TaoMonitorDefault - Default routine for monitoring progress of `TaoSolve()` 1534a7e14dcfSSatish Balay 1535c3339decSBarry Smith Collective 1536a7e14dcfSSatish Balay 1537a7e14dcfSSatish Balay Input Parameters: 153847450a7bSBarry Smith + tao - the `Tao` context 153947450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1540a7e14dcfSSatish Balay 154147450a7bSBarry Smith Options Database Key: 1542147403d9SBarry Smith . -tao_monitor - turn on default monitoring 1543a7e14dcfSSatish Balay 1544a7e14dcfSSatish Balay Level: advanced 1545a7e14dcfSSatish Balay 154647450a7bSBarry Smith Note: 154747450a7bSBarry Smith This monitor prints the function value and gradient 154847450a7bSBarry Smith norm at each iteration. 154947450a7bSBarry Smith 155047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 1551a7e14dcfSSatish Balay @*/ 1552d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDefault(Tao tao, void *ctx) 1553d71ae5a4SJacob Faibussowitsch { 155463b15415SAlp Dener PetscInt its, tabs; 1555a7e14dcfSSatish Balay PetscReal fct, gnorm; 15568163d661SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 155747a47007SBarry Smith 1558a7e14dcfSSatish Balay PetscFunctionBegin; 155994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 15608163d661SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1561a7e14dcfSSatish Balay its = tao->niter; 1562a7e14dcfSSatish Balay fct = tao->fc; 1563a7e14dcfSSatish Balay gnorm = tao->residual; 15649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 15659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1566494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 15679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1568494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 156963b15415SAlp Dener } 157063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 15719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 157208d19d1fSJason Sarich if (gnorm >= PETSC_INFINITY) { 15739566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n")); 157408d19d1fSJason Sarich } else { 15759566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 157608d19d1fSJason Sarich } 15779566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 15783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1579a7e14dcfSSatish Balay } 1580a7e14dcfSSatish Balay 15817fab98ebSJason Sarich /*@ 158247450a7bSBarry Smith TaoDefaultGMonitor - Default routine for monitoring progress of `TaoSolve()` with extra detail on the globalization method. 15838d5ead36SAlp Dener 1584c3339decSBarry Smith Collective 15858d5ead36SAlp Dener 15868d5ead36SAlp Dener Input Parameters: 158747450a7bSBarry Smith + tao - the `Tao` context 158847450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 15898d5ead36SAlp Dener 159047450a7bSBarry Smith Options Database Key: 1591147403d9SBarry Smith . -tao_gmonitor - turn on monitoring with globalization information 15928d5ead36SAlp Dener 15938d5ead36SAlp Dener Level: advanced 15948d5ead36SAlp Dener 159547450a7bSBarry Smith Note: 159647450a7bSBarry Smith This monitor prints the function value and gradient norm at each 159747450a7bSBarry Smith iteration, as well as the step size and trust radius. Note that the 159847450a7bSBarry Smith step size and trust radius may be the same for some algorithms. 159947450a7bSBarry Smith 160047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 16018d5ead36SAlp Dener @*/ 1602d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultGMonitor(Tao tao, void *ctx) 1603d71ae5a4SJacob Faibussowitsch { 16048d5ead36SAlp Dener PetscInt its, tabs; 16058d5ead36SAlp Dener PetscReal fct, gnorm, stp, tr; 16068d5ead36SAlp Dener PetscViewer viewer = (PetscViewer)ctx; 16078d5ead36SAlp Dener 16088d5ead36SAlp Dener PetscFunctionBegin; 160994511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16108d5ead36SAlp Dener PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 16118d5ead36SAlp Dener its = tao->niter; 16128d5ead36SAlp Dener fct = tao->fc; 16138d5ead36SAlp Dener gnorm = tao->residual; 16148d5ead36SAlp Dener stp = tao->step; 16158d5ead36SAlp Dener tr = tao->trust; 16169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 1618494bef23SAlp Dener if (its == 0 && ((PetscObject)tao)->prefix && !tao->header_printed) { 16199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Iteration information for %s solve.\n", ((PetscObject)tao)->prefix)); 1620494bef23SAlp Dener tao->header_printed = PETSC_TRUE; 16218d5ead36SAlp Dener } 162263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " TAO,", its)); 16239566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 16248d5ead36SAlp Dener if (gnorm >= PETSC_INFINITY) { 16259566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf,")); 16268d5ead36SAlp Dener } else { 16279566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g,", (double)gnorm)); 16288d5ead36SAlp Dener } 16299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Step: %g, Trust: %g\n", (double)stp, (double)tr)); 16309566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16328d5ead36SAlp Dener } 16338d5ead36SAlp Dener 16348d5ead36SAlp Dener /*@ 163547450a7bSBarry Smith TaoDefaultSMonitor - Default routine for monitoring progress of `TaoSolve()` 1636a7e14dcfSSatish Balay 1637c3339decSBarry Smith Collective 1638a7e14dcfSSatish Balay 1639a7e14dcfSSatish Balay Input Parameters: 164047450a7bSBarry Smith + tao - the `Tao` context 164147450a7bSBarry Smith - ctx - `PetscViewer` context of type `PETSCVIEWERASCII` 1642a7e14dcfSSatish Balay 164347450a7bSBarry Smith Options Database Key: 1644147403d9SBarry Smith . -tao_smonitor - turn on default short monitoring 1645a7e14dcfSSatish Balay 1646a7e14dcfSSatish Balay Level: advanced 1647a7e14dcfSSatish Balay 164847450a7bSBarry Smith Note: 164947450a7bSBarry Smith Same as `TaoMonitorDefault()` except 165047450a7bSBarry Smith it prints fewer digits of the residual as the residual gets smaller. 165147450a7bSBarry Smith This is because the later digits are meaningless and are often 165247450a7bSBarry Smith different on different machines; by using this routine different 165347450a7bSBarry Smith machines will usually generate the same output. 165447450a7bSBarry Smith 165547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoMonitorDefault()`, `TaoSetMonitor()` 1656a7e14dcfSSatish Balay @*/ 1657d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultSMonitor(Tao tao, void *ctx) 1658d71ae5a4SJacob Faibussowitsch { 16592c9b8b83SAlp Dener PetscInt its, tabs; 1660a7e14dcfSSatish Balay PetscReal fct, gnorm; 16614d4332d5SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 166247a47007SBarry Smith 1663a7e14dcfSSatish Balay PetscFunctionBegin; 166494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 16654d4332d5SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1666a7e14dcfSSatish Balay its = tao->niter; 1667a7e14dcfSSatish Balay fct = tao->fc; 1668a7e14dcfSSatish Balay gnorm = tao->residual; 16699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 16709566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 167163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %3" PetscInt_FMT ",", its)); 16729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value %g,", (double)fct)); 1673d393f493SBarry Smith if (gnorm >= PETSC_INFINITY) { 16749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: Inf \n")); 167508d19d1fSJason Sarich } else if (gnorm > 1.e-6) { 16769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g \n", (double)gnorm)); 1677a7e14dcfSSatish Balay } else if (gnorm > 1.e-11) { 16789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-6 \n")); 1679a7e14dcfSSatish Balay } else { 16809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: < 1.0e-11 \n")); 1681a7e14dcfSSatish Balay } 16829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 16833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1684a7e14dcfSSatish Balay } 1685a7e14dcfSSatish Balay 16867fab98ebSJason Sarich /*@ 168765ba42b6SBarry Smith TaoDefaultCMonitor - same as `TaoMonitorDefault()` except 168847450a7bSBarry Smith it prints the norm of the constraint function. 1689a7e14dcfSSatish Balay 1690c3339decSBarry Smith Collective 1691a7e14dcfSSatish Balay 1692a7e14dcfSSatish Balay Input Parameters: 169347450a7bSBarry Smith + tao - the `Tao` context 169447450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1695a7e14dcfSSatish Balay 169647450a7bSBarry Smith Options Database Key: 1697147403d9SBarry Smith . -tao_cmonitor - monitor the constraints 1698a7e14dcfSSatish Balay 1699a7e14dcfSSatish Balay Level: advanced 1700a7e14dcfSSatish Balay 170147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoMonitorDefault()`, `TaoSetMonitor()` 1702a7e14dcfSSatish Balay @*/ 1703d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultCMonitor(Tao tao, void *ctx) 1704d71ae5a4SJacob Faibussowitsch { 17052c9b8b83SAlp Dener PetscInt its, tabs; 1706a7e14dcfSSatish Balay PetscReal fct, gnorm; 1707d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1708a7e14dcfSSatish Balay 1709a7e14dcfSSatish Balay PetscFunctionBegin; 171094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1711d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1712a7e14dcfSSatish Balay its = tao->niter; 1713a7e14dcfSSatish Balay fct = tao->fc; 1714a7e14dcfSSatish Balay gnorm = tao->residual; 17159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 17169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 171763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "iter = %" PetscInt_FMT ",", its)); 17189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Function value: %g,", (double)fct)); 17199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Residual: %g ", (double)gnorm)); 17209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Constraint: %g \n", (double)tao->cnorm)); 17219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 17223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1723a7e14dcfSSatish Balay } 1724a7e14dcfSSatish Balay 1725a7e14dcfSSatish Balay /*@C 172647450a7bSBarry Smith TaoSolutionMonitor - Views the solution at each iteration of `TaoSolve()` 1727a7e14dcfSSatish Balay 1728c3339decSBarry Smith Collective 1729a7e14dcfSSatish Balay 1730a7e14dcfSSatish Balay Input Parameters: 173147450a7bSBarry Smith + tao - the `Tao` context 173247450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1733a7e14dcfSSatish Balay 173447450a7bSBarry Smith Options Database Key: 1735147403d9SBarry Smith . -tao_view_solution - view the solution 1736a7e14dcfSSatish Balay 1737a7e14dcfSSatish Balay Level: advanced 1738a7e14dcfSSatish Balay 173947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 1740a7e14dcfSSatish Balay @*/ 1741d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSolutionMonitor(Tao tao, void *ctx) 1742d71ae5a4SJacob Faibussowitsch { 1743feb237baSPierre Jolivet PetscViewer viewer = (PetscViewer)ctx; 174447a47007SBarry Smith 1745a7e14dcfSSatish Balay PetscFunctionBegin; 174694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1747d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 17489566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, viewer)); 17493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1750a7e14dcfSSatish Balay } 1751a7e14dcfSSatish Balay 1752a7e14dcfSSatish Balay /*@C 175347450a7bSBarry Smith TaoGradientMonitor - Views the gradient at each iteration of `TaoSolve()` 1754a7e14dcfSSatish Balay 1755c3339decSBarry Smith Collective 1756a7e14dcfSSatish Balay 1757a7e14dcfSSatish Balay Input Parameters: 175847450a7bSBarry Smith + tao - the `Tao` context 175947450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1760a7e14dcfSSatish Balay 176147450a7bSBarry Smith Options Database Key: 1762147403d9SBarry Smith . -tao_view_gradient - view the gradient at each iteration 1763a7e14dcfSSatish Balay 1764a7e14dcfSSatish Balay Level: advanced 1765a7e14dcfSSatish Balay 176647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 1767a7e14dcfSSatish Balay @*/ 1768d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientMonitor(Tao tao, void *ctx) 1769d71ae5a4SJacob Faibussowitsch { 1770d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 177147a47007SBarry Smith 1772a7e14dcfSSatish Balay PetscFunctionBegin; 177394511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1774d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 17759566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, viewer)); 17763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1777a7e14dcfSSatish Balay } 1778a7e14dcfSSatish Balay 1779a7e14dcfSSatish Balay /*@C 178047450a7bSBarry Smith TaoStepDirectionMonitor - Views the step-direction at each iteration of `TaoSolve()` 1781a7e14dcfSSatish Balay 1782c3339decSBarry Smith Collective 1783a7e14dcfSSatish Balay 1784a7e14dcfSSatish Balay Input Parameters: 178547450a7bSBarry Smith + tao - the `Tao` context 178647450a7bSBarry Smith - ctx - `PetscViewer` context or `NULL` 1787a7e14dcfSSatish Balay 178847450a7bSBarry Smith Options Database Key: 178947450a7bSBarry Smith . -tao_view_stepdirection - view the step direction vector at each iteration 1790a7e14dcfSSatish Balay 1791a7e14dcfSSatish Balay Level: advanced 1792a7e14dcfSSatish Balay 179347450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 1794a7e14dcfSSatish Balay @*/ 1795d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoStepDirectionMonitor(Tao tao, void *ctx) 1796d71ae5a4SJacob Faibussowitsch { 1797d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 1798d393f493SBarry Smith 1799a7e14dcfSSatish Balay PetscFunctionBegin; 180094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1801d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18029566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 18033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1804a7e14dcfSSatish Balay } 1805a7e14dcfSSatish Balay 1806a7e14dcfSSatish Balay /*@C 180747450a7bSBarry Smith TaoDrawSolutionMonitor - Plots the solution at each iteration of `TaoSolve()` 1808a7e14dcfSSatish Balay 1809c3339decSBarry Smith Collective 1810a7e14dcfSSatish Balay 1811a7e14dcfSSatish Balay Input Parameters: 181247450a7bSBarry Smith + tao - the `Tao` context 181365ba42b6SBarry Smith - ctx - `TaoMonitorDraw` context 1814a7e14dcfSSatish Balay 181547450a7bSBarry Smith Options Database Key: 1816147403d9SBarry Smith . -tao_draw_solution - draw the solution at each iteration 1817a7e14dcfSSatish Balay 1818a7e14dcfSSatish Balay Level: advanced 1819a7e14dcfSSatish Balay 182047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolutionMonitor()`, `TaoSetMonitor()`, `TaoDrawGradientMonitor`, `TaoMonitorDraw` 1821a7e14dcfSSatish Balay @*/ 1822d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDrawSolutionMonitor(Tao tao, void *ctx) 1823d71ae5a4SJacob Faibussowitsch { 1824e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 182547a47007SBarry Smith 1826a7e14dcfSSatish Balay PetscFunctionBegin; 182794511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 18283ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 18299566063dSJacob Faibussowitsch PetscCall(VecView(tao->solution, ictx->viewer)); 18303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1831a7e14dcfSSatish Balay } 1832a7e14dcfSSatish Balay 1833a7e14dcfSSatish Balay /*@C 183447450a7bSBarry Smith TaoDrawGradientMonitor - Plots the gradient at each iteration of `TaoSolve()` 1835a7e14dcfSSatish Balay 1836c3339decSBarry Smith Collective 1837a7e14dcfSSatish Balay 1838a7e14dcfSSatish Balay Input Parameters: 183947450a7bSBarry Smith + tao - the `Tao` context 184065ba42b6SBarry Smith - ctx - `PetscViewer` context 1841a7e14dcfSSatish Balay 184247450a7bSBarry Smith Options Database Key: 1843147403d9SBarry Smith . -tao_draw_gradient - draw the gradient at each iteration 1844a7e14dcfSSatish Balay 1845a7e14dcfSSatish Balay Level: advanced 1846a7e14dcfSSatish Balay 184747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGradientMonitor()`, `TaoSetMonitor()`, `TaoDrawSolutionMonitor` 1848a7e14dcfSSatish Balay @*/ 1849d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx) 1850d71ae5a4SJacob Faibussowitsch { 1851e882e171SHong Zhang TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 185247a47007SBarry Smith 1853a7e14dcfSSatish Balay PetscFunctionBegin; 185494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 18553ba16761SJacob Faibussowitsch if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(PETSC_SUCCESS); 18569566063dSJacob Faibussowitsch PetscCall(VecView(tao->gradient, ictx->viewer)); 18573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1858a7e14dcfSSatish Balay } 1859a7e14dcfSSatish Balay 1860a7e14dcfSSatish Balay /*@C 186147450a7bSBarry Smith TaoDrawStepMonitor - Plots the step direction at each iteration of `TaoSolve()` 1862a7e14dcfSSatish Balay 1863c3339decSBarry Smith Collective 1864a7e14dcfSSatish Balay 1865a7e14dcfSSatish Balay Input Parameters: 186647450a7bSBarry Smith + tao - the `Tao` context 186747450a7bSBarry Smith - ctx - the `PetscViewer` context 1868a7e14dcfSSatish Balay 186947450a7bSBarry Smith Options Database Key: 1870147403d9SBarry Smith . -tao_draw_step - draw the step direction at each iteration 1871a7e14dcfSSatish Balay 1872a7e14dcfSSatish Balay Level: advanced 1873a7e14dcfSSatish Balay 187447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetMonitor()`, `TaoDrawSolutionMonitor` 1875a7e14dcfSSatish Balay @*/ 1876d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx) 1877d71ae5a4SJacob Faibussowitsch { 187894511df7SStefano Zampini PetscViewer viewer = (PetscViewer)ctx; 187947a47007SBarry Smith 1880a7e14dcfSSatish Balay PetscFunctionBegin; 188194511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 188294511df7SStefano Zampini PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18839566063dSJacob Faibussowitsch PetscCall(VecView(tao->stepdirection, viewer)); 18843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1885a7e14dcfSSatish Balay } 1886a7e14dcfSSatish Balay 1887a7e14dcfSSatish Balay /*@C 188847450a7bSBarry Smith TaoResidualMonitor - Views the least-squares residual at each iteration of `TaoSolve()` 1889a7e14dcfSSatish Balay 1890c3339decSBarry Smith Collective 1891a7e14dcfSSatish Balay 1892a7e14dcfSSatish Balay Input Parameters: 189347450a7bSBarry Smith + tao - the `Tao` context 189447450a7bSBarry Smith - ctx - the `PetscViewer` context or `NULL` 1895a7e14dcfSSatish Balay 189647450a7bSBarry Smith Options Database Key: 189747450a7bSBarry Smith . -tao_view_ls_residual - view the residual at each iteration 1898a7e14dcfSSatish Balay 1899a7e14dcfSSatish Balay Level: advanced 1900a7e14dcfSSatish Balay 190147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoDefaultSMonitor()`, `TaoSetMonitor()` 1902a7e14dcfSSatish Balay @*/ 1903d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoResidualMonitor(Tao tao, void *ctx) 1904d71ae5a4SJacob Faibussowitsch { 1905d393f493SBarry Smith PetscViewer viewer = (PetscViewer)ctx; 190647a47007SBarry Smith 1907a7e14dcfSSatish Balay PetscFunctionBegin; 190894511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 1909d393f493SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 19109566063dSJacob Faibussowitsch PetscCall(VecView(tao->ls_res, viewer)); 19113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1912a7e14dcfSSatish Balay } 1913a7e14dcfSSatish Balay 19147fab98ebSJason Sarich /*@ 1915a7e14dcfSSatish Balay TaoDefaultConvergenceTest - Determines whether the solver should continue iterating 1916a7e14dcfSSatish Balay or terminate. 1917a7e14dcfSSatish Balay 1918c3339decSBarry Smith Collective 1919a7e14dcfSSatish Balay 1920a7e14dcfSSatish Balay Input Parameters: 192147450a7bSBarry Smith + tao - the `Tao` context 1922a7e14dcfSSatish Balay - dummy - unused dummy context 1923a7e14dcfSSatish Balay 1924a7e14dcfSSatish Balay Output Parameter: 1925a7e14dcfSSatish Balay . reason - for terminating 1926a7e14dcfSSatish Balay 192747450a7bSBarry Smith Level: developer 192847450a7bSBarry Smith 1929a7e14dcfSSatish Balay Notes: 1930a7e14dcfSSatish Balay This routine checks the residual in the optimality conditions, the 1931a7e14dcfSSatish Balay relative residual in the optimity conditions, the number of function 1932a7e14dcfSSatish Balay evaluations, and the function value to test convergence. Some 1933a7e14dcfSSatish Balay solvers may use different convergence routines. 1934a7e14dcfSSatish Balay 193547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetTolerances()`, `TaoGetConvergedReason()`, `TaoSetConvergedReason()` 1936a7e14dcfSSatish Balay @*/ 1937d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoDefaultConvergenceTest(Tao tao, void *dummy) 1938d71ae5a4SJacob Faibussowitsch { 1939a7e14dcfSSatish Balay PetscInt niter = tao->niter, nfuncs = PetscMax(tao->nfuncs, tao->nfuncgrads); 1940a7e14dcfSSatish Balay PetscInt max_funcs = tao->max_funcs; 1941a7e14dcfSSatish Balay PetscReal gnorm = tao->residual, gnorm0 = tao->gnorm0; 1942a7e14dcfSSatish Balay PetscReal f = tao->fc, steptol = tao->steptol, trradius = tao->step; 1943a7e14dcfSSatish Balay PetscReal gatol = tao->gatol, grtol = tao->grtol, gttol = tao->gttol; 1944e52336cbSBarry Smith PetscReal catol = tao->catol, crtol = tao->crtol; 1945e52336cbSBarry Smith PetscReal fmin = tao->fmin, cnorm = tao->cnorm; 1946e4cb33bbSBarry Smith TaoConvergedReason reason = tao->reason; 1947a7e14dcfSSatish Balay 1948a7e14dcfSSatish Balay PetscFunctionBegin; 1949441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 19503ba16761SJacob Faibussowitsch if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS); 1951a7e14dcfSSatish Balay 1952a7e14dcfSSatish Balay if (PetscIsInfOrNanReal(f)) { 19539566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Failed to converged, function value is Inf or NaN\n")); 1954a7e14dcfSSatish Balay reason = TAO_DIVERGED_NAN; 1955a7e14dcfSSatish Balay } else if (f <= fmin && cnorm <= catol) { 19569566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to function value %g < minimum function value %g\n", (double)f, (double)fmin)); 1957a7e14dcfSSatish Balay reason = TAO_CONVERGED_MINF; 1958a7e14dcfSSatish Balay } else if (gnorm <= gatol && cnorm <= catol) { 19599566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual norm ||g(X)||=%g < %g\n", (double)gnorm, (double)gatol)); 1960a7e14dcfSSatish Balay reason = TAO_CONVERGED_GATOL; 1961a7e14dcfSSatish Balay } else if (f != 0 && PetscAbsReal(gnorm / f) <= grtol && cnorm <= crtol) { 19629566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to residual ||g(X)||/|f(X)| =%g < %g\n", (double)(gnorm / f), (double)grtol)); 1963a7e14dcfSSatish Balay reason = TAO_CONVERGED_GRTOL; 1964e1d16bb9SBarry Smith } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm / gnorm0 < gttol) && cnorm <= crtol) { 19659566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n", (double)(gnorm / gnorm0), (double)gttol)); 1966a7e14dcfSSatish Balay reason = TAO_CONVERGED_GTTOL; 196794511df7SStefano Zampini } else if (max_funcs >= 0 && nfuncs > max_funcs) { 19689566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of function evaluations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", nfuncs, max_funcs)); 1969a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXFCN; 1970a7e14dcfSSatish Balay } else if (tao->lsflag != 0) { 19719566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Tao Line Search failure.\n")); 1972a7e14dcfSSatish Balay reason = TAO_DIVERGED_LS_FAILURE; 1973a7e14dcfSSatish Balay } else if (trradius < steptol && niter > 0) { 19749566063dSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Trust region/step size too small: %g < %g\n", (double)trradius, (double)steptol)); 1975a7e14dcfSSatish Balay reason = TAO_CONVERGED_STEPTOL; 1976e031d6f5SAlp Dener } else if (niter >= tao->max_it) { 197763a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(tao, "Exceeded maximum number of iterations: %" PetscInt_FMT " > %" PetscInt_FMT "\n", niter, tao->max_it)); 1978a7e14dcfSSatish Balay reason = TAO_DIVERGED_MAXITS; 1979a7e14dcfSSatish Balay } else { 1980a7e14dcfSSatish Balay reason = TAO_CONTINUE_ITERATING; 1981a7e14dcfSSatish Balay } 1982a7e14dcfSSatish Balay tao->reason = reason; 19833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1984a7e14dcfSSatish Balay } 1985a7e14dcfSSatish Balay 1986a7e14dcfSSatish Balay /*@C 1987a7e14dcfSSatish Balay TaoSetOptionsPrefix - Sets the prefix used for searching for all 198865ba42b6SBarry Smith Tao options in the database. 1989a7e14dcfSSatish Balay 1990c3339decSBarry Smith Logically Collective 1991a7e14dcfSSatish Balay 1992a7e14dcfSSatish Balay Input Parameters: 199347450a7bSBarry Smith + tao - the `Tao` context 199465ba42b6SBarry Smith - prefix - the prefix string to prepend to all Tao option requests 1995a7e14dcfSSatish Balay 1996a7e14dcfSSatish Balay Notes: 1997a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 1998a7e14dcfSSatish Balay The first character of all runtime options is AUTOMATICALLY the hyphen. 1999a7e14dcfSSatish Balay 2000a7e14dcfSSatish Balay For example, to distinguish between the runtime options for two 200165ba42b6SBarry Smith different Tao solvers, one could call 2002a7e14dcfSSatish Balay .vb 2003a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao1,"sys1_") 2004a7e14dcfSSatish Balay TaoSetOptionsPrefix(tao2,"sys2_") 2005a7e14dcfSSatish Balay .ve 2006a7e14dcfSSatish Balay 2007a7e14dcfSSatish Balay This would enable use of different options for each system, such as 2008a7e14dcfSSatish Balay .vb 20099fa2b5dcSStefano Zampini -sys1_tao_method blmvm -sys1_tao_grtol 1.e-3 20109fa2b5dcSStefano Zampini -sys2_tao_method lmvm -sys2_tao_grtol 1.e-4 2011a7e14dcfSSatish Balay .ve 2012a7e14dcfSSatish Balay 2013a7e14dcfSSatish Balay Level: advanced 2014a7e14dcfSSatish Balay 201547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetFromOptions()`, `TaoAppendOptionsPrefix()`, `TaoGetOptionsPrefix()` 2016a7e14dcfSSatish Balay @*/ 2017d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[]) 2018d71ae5a4SJacob Faibussowitsch { 2019a7e14dcfSSatish Balay PetscFunctionBegin; 202094511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20219566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tao, p)); 20221baa6e33SBarry Smith if (tao->linesearch) PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, p)); 20231baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPSetOptionsPrefix(tao->ksp, p)); 20243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2025a7e14dcfSSatish Balay } 2026a7e14dcfSSatish Balay 2027a7e14dcfSSatish Balay /*@C 202847450a7bSBarry Smith TaoAppendOptionsPrefix - Appends to the prefix used for searching for all Tao options in the database. 2029a7e14dcfSSatish Balay 2030c3339decSBarry Smith Logically Collective 2031a7e14dcfSSatish Balay 2032a7e14dcfSSatish Balay Input Parameters: 203347450a7bSBarry Smith + tao - the `Tao` solver context 203447450a7bSBarry Smith - prefix - the prefix string to prepend to all `Tao` option requests 2035a7e14dcfSSatish Balay 203665ba42b6SBarry Smith Note: 2037a7e14dcfSSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 203865ba42b6SBarry Smith The first character of all runtime options is automatically the hyphen. 2039a7e14dcfSSatish Balay 2040a7e14dcfSSatish Balay Level: advanced 2041a7e14dcfSSatish Balay 204247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoGetOptionsPrefix()` 2043a7e14dcfSSatish Balay @*/ 2044d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[]) 2045d71ae5a4SJacob Faibussowitsch { 2046a7e14dcfSSatish Balay PetscFunctionBegin; 204794511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20489566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao, p)); 20491baa6e33SBarry Smith if (tao->linesearch) PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)tao->linesearch, p)); 20501baa6e33SBarry Smith if (tao->ksp) PetscCall(KSPAppendOptionsPrefix(tao->ksp, p)); 20513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2052a7e14dcfSSatish Balay } 2053a7e14dcfSSatish Balay 2054a7e14dcfSSatish Balay /*@C 2055a7e14dcfSSatish Balay TaoGetOptionsPrefix - Gets the prefix used for searching for all 205665ba42b6SBarry Smith Tao options in the database 2057a7e14dcfSSatish Balay 2058a7e14dcfSSatish Balay Not Collective 2059a7e14dcfSSatish Balay 206047450a7bSBarry Smith Input Parameter: 206147450a7bSBarry Smith . tao - the `Tao` context 2062a7e14dcfSSatish Balay 206347450a7bSBarry Smith Output Parameter: 2064a7e14dcfSSatish Balay . prefix - pointer to the prefix string used is returned 2065a7e14dcfSSatish Balay 206665ba42b6SBarry Smith Fortran Note: 206747450a7bSBarry Smith Pass in a string 'prefix' of sufficient length to hold the prefix. 2068a7e14dcfSSatish Balay 2069a7e14dcfSSatish Balay Level: advanced 2070a7e14dcfSSatish Balay 207147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetFromOptions()`, `TaoSetOptionsPrefix()`, `TaoAppendOptionsPrefix()` 2072a7e14dcfSSatish Balay @*/ 2073d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[]) 2074d71ae5a4SJacob Faibussowitsch { 207594511df7SStefano Zampini PetscFunctionBegin; 207694511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 20779566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)tao, p)); 20783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2079a7e14dcfSSatish Balay } 2080a7e14dcfSSatish Balay 2081a7e14dcfSSatish Balay /*@C 208247450a7bSBarry Smith TaoSetType - Sets the `TaoType` for the minimization solver. 2083a7e14dcfSSatish Balay 2084c3339decSBarry Smith Collective 2085a7e14dcfSSatish Balay 2086a7e14dcfSSatish Balay Input Parameters: 208747450a7bSBarry Smith + solver - the `Tao` solver context 2088a7e14dcfSSatish Balay - type - a known method 2089a7e14dcfSSatish Balay 2090a7e14dcfSSatish Balay Options Database Key: 209158417fe7SBarry Smith . -tao_type <type> - Sets the method; use -help for a list 209258417fe7SBarry Smith of available methods (for instance, "-tao_type lmvm" or "-tao_type tron") 2093a7e14dcfSSatish Balay 2094a7e14dcfSSatish Balay Level: intermediate 2095a7e14dcfSSatish Balay 209647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoCreate()`, `TaoGetType()`, `TaoType` 2097a7e14dcfSSatish Balay @*/ 2098d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetType(Tao tao, TaoType type) 2099d71ae5a4SJacob Faibussowitsch { 2100441846f8SBarry Smith PetscErrorCode (*create_xxx)(Tao); 2101a7e14dcfSSatish Balay PetscBool issame; 2102a7e14dcfSSatish Balay 2103a7e14dcfSSatish Balay PetscFunctionBegin; 2104441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2105a7e14dcfSSatish Balay 21069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)tao, type, &issame)); 21073ba16761SJacob Faibussowitsch if (issame) PetscFunctionReturn(PETSC_SUCCESS); 2108a7e14dcfSSatish Balay 21099566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(TaoList, type, (void (**)(void)) & create_xxx)); 21103c859ba3SBarry Smith PetscCheck(create_xxx, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested Tao type %s", type); 2111a7e14dcfSSatish Balay 2112a7e14dcfSSatish Balay /* Destroy the existing solver information */ 2113dbbe0bcdSBarry Smith PetscTryTypeMethod(tao, destroy); 21141baa6e33SBarry Smith PetscCall(KSPDestroy(&tao->ksp)); 21159566063dSJacob Faibussowitsch PetscCall(TaoLineSearchDestroy(&tao->linesearch)); 211683c8fe1dSLisandro Dalcin tao->ops->setup = NULL; 211783c8fe1dSLisandro Dalcin tao->ops->solve = NULL; 211883c8fe1dSLisandro Dalcin tao->ops->view = NULL; 211983c8fe1dSLisandro Dalcin tao->ops->setfromoptions = NULL; 212083c8fe1dSLisandro Dalcin tao->ops->destroy = NULL; 2121a7e14dcfSSatish Balay 2122a7e14dcfSSatish Balay tao->setupcalled = PETSC_FALSE; 2123a7e14dcfSSatish Balay 21249566063dSJacob Faibussowitsch PetscCall((*create_xxx)(tao)); 21259566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)tao, type)); 21263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2127a7e14dcfSSatish Balay } 212847a47007SBarry Smith 212967be906fSBarry Smith /*@C 213047450a7bSBarry Smith TaoRegister - Adds a method to the Tao package for minimization. 2131a7e14dcfSSatish Balay 2132a7e14dcfSSatish Balay Synopsis: 21335bd1e576SStefano Zampini TaoRegister(char *name_solver,char *path,char *name_Create,PetscErrorCode (*routine_Create)(Tao)) 2134a7e14dcfSSatish Balay 213520f4b53cSBarry Smith Not Collective 2136a7e14dcfSSatish Balay 2137a7e14dcfSSatish Balay Input Parameters: 2138a7e14dcfSSatish Balay + sname - name of a new user-defined solver 2139a7e14dcfSSatish Balay - func - routine to Create method context 2140a7e14dcfSSatish Balay 2141a7e14dcfSSatish Balay Sample usage: 2142a7e14dcfSSatish Balay .vb 2143441846f8SBarry Smith TaoRegister("my_solver",MySolverCreate); 2144a7e14dcfSSatish Balay .ve 2145a7e14dcfSSatish Balay 2146a7e14dcfSSatish Balay Then, your solver can be chosen with the procedural interface via 2147a7e14dcfSSatish Balay $ TaoSetType(tao,"my_solver") 2148a7e14dcfSSatish Balay or at runtime via the option 214958417fe7SBarry Smith $ -tao_type my_solver 2150a7e14dcfSSatish Balay 2151a7e14dcfSSatish Balay Level: advanced 2152a7e14dcfSSatish Balay 215367be906fSBarry Smith Note: 215467be906fSBarry Smith `TaoRegister()` may be called multiple times to add several user-defined solvers. 215567be906fSBarry Smith 215647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetType()`, `TaoRegisterAll()`, `TaoRegisterDestroy()` 215767be906fSBarry Smith @*/ 2158d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao)) 2159d71ae5a4SJacob Faibussowitsch { 2160a7e14dcfSSatish Balay PetscFunctionBegin; 21619566063dSJacob Faibussowitsch PetscCall(TaoInitializePackage()); 21629566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&TaoList, sname, (void (*)(void))func)); 21633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2164a7e14dcfSSatish Balay } 2165a7e14dcfSSatish Balay 2166a7e14dcfSSatish Balay /*@C 2167441846f8SBarry Smith TaoRegisterDestroy - Frees the list of minimization solvers that were 216847450a7bSBarry Smith registered by `TaoRegister()`. 2169a7e14dcfSSatish Balay 2170a7e14dcfSSatish Balay Not Collective 2171a7e14dcfSSatish Balay 2172a7e14dcfSSatish Balay Level: advanced 2173a7e14dcfSSatish Balay 217447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoRegisterAll()`, `TaoRegister()` 2175a7e14dcfSSatish Balay @*/ 2176d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoRegisterDestroy(void) 2177d71ae5a4SJacob Faibussowitsch { 2178a7e14dcfSSatish Balay PetscFunctionBegin; 21799566063dSJacob Faibussowitsch PetscCall(PetscFunctionListDestroy(&TaoList)); 2180441846f8SBarry Smith TaoRegisterAllCalled = PETSC_FALSE; 21813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2182a7e14dcfSSatish Balay } 218347a47007SBarry Smith 21848931d482SJason Sarich /*@ 218547450a7bSBarry Smith TaoGetIterationNumber - Gets the number of `TaoSolve()` iterations completed 21868931d482SJason Sarich at this time. 21878931d482SJason Sarich 21888931d482SJason Sarich Not Collective 21898931d482SJason Sarich 21908931d482SJason Sarich Input Parameter: 219147450a7bSBarry Smith . tao - the `Tao` context 21928931d482SJason Sarich 21938931d482SJason Sarich Output Parameter: 21948931d482SJason Sarich . iter - iteration number 21958931d482SJason Sarich 21968931d482SJason Sarich Notes: 21978931d482SJason Sarich For example, during the computation of iteration 2 this would return 1. 21988931d482SJason Sarich 21998931d482SJason Sarich Level: intermediate 22008931d482SJason Sarich 220147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetResidualNorm()`, `TaoGetObjective()` 22028931d482SJason Sarich @*/ 2203d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetIterationNumber(Tao tao, PetscInt *iter) 2204d71ae5a4SJacob Faibussowitsch { 22058931d482SJason Sarich PetscFunctionBegin; 22068931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22078931d482SJason Sarich PetscValidIntPointer(iter, 2); 22088931d482SJason Sarich *iter = tao->niter; 22093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22108931d482SJason Sarich } 22118931d482SJason Sarich 22128931d482SJason Sarich /*@ 221347450a7bSBarry Smith TaoGetResidualNorm - Gets the current value of the norm of the residual (gradient) 221479f5d8caSBarry Smith at this time. 221579f5d8caSBarry Smith 221679f5d8caSBarry Smith Not Collective 221779f5d8caSBarry Smith 221879f5d8caSBarry Smith Input Parameter: 221947450a7bSBarry Smith . tao - the `Tao` context 222079f5d8caSBarry Smith 222179f5d8caSBarry Smith Output Parameter: 222279f5d8caSBarry Smith . value - the current value 222379f5d8caSBarry Smith 222479f5d8caSBarry Smith Level: intermediate 222579f5d8caSBarry Smith 222667be906fSBarry Smith Developer Note: 222767be906fSBarry Smith This is the 2-norm of the residual, we cannot use `TaoGetGradientNorm()` because that has 222847450a7bSBarry Smith a different meaning. For some reason `Tao` sometimes calls the gradient the residual. 222979f5d8caSBarry Smith 223047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLinearSolveIterations()`, `TaoGetIterationNumber()`, `TaoGetObjective()` 223179f5d8caSBarry Smith @*/ 2232d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetResidualNorm(Tao tao, PetscReal *value) 2233d71ae5a4SJacob Faibussowitsch { 223479f5d8caSBarry Smith PetscFunctionBegin; 223579f5d8caSBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 223679f5d8caSBarry Smith PetscValidRealPointer(value, 2); 223779f5d8caSBarry Smith *value = tao->residual; 22383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 223979f5d8caSBarry Smith } 224079f5d8caSBarry Smith 224179f5d8caSBarry Smith /*@ 22428931d482SJason Sarich TaoSetIterationNumber - Sets the current iteration number. 22438931d482SJason Sarich 2244c3339decSBarry Smith Logically Collective 22458931d482SJason Sarich 2246d8d19677SJose E. Roman Input Parameters: 224747450a7bSBarry Smith + tao - the `Tao` context 2248a2b725a8SWilliam Gropp - iter - iteration number 22498931d482SJason Sarich 22508931d482SJason Sarich Level: developer 22518931d482SJason Sarich 225247450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLinearSolveIterations()` 22538931d482SJason Sarich @*/ 2254d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetIterationNumber(Tao tao, PetscInt iter) 2255d71ae5a4SJacob Faibussowitsch { 22568931d482SJason Sarich PetscFunctionBegin; 22578931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 225894511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 22599566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 22608931d482SJason Sarich tao->niter = iter; 22619566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 22623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22638931d482SJason Sarich } 22648931d482SJason Sarich 22658931d482SJason Sarich /*@ 226647450a7bSBarry Smith TaoGetTotalIterationNumber - Gets the total number of `TaoSolve()` iterations 22678931d482SJason Sarich completed. This number keeps accumulating if multiple solves 226847450a7bSBarry Smith are called with the `Tao` object. 22698931d482SJason Sarich 22708931d482SJason Sarich Not Collective 22718931d482SJason Sarich 22728931d482SJason Sarich Input Parameter: 227347450a7bSBarry Smith . tao - the `Tao` context 22748931d482SJason Sarich 22758931d482SJason Sarich Output Parameter: 227647450a7bSBarry Smith . iter - number of iterations 22778931d482SJason Sarich 22788931d482SJason Sarich Level: intermediate 22798931d482SJason Sarich 228020f4b53cSBarry Smith Note: 228167be906fSBarry Smith The total iteration count is updated after each solve, if there is a current 228247450a7bSBarry Smith `TaoSolve()` in progress then those iterations are not included in the count 228367be906fSBarry Smith 228447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLinearSolveIterations()` 22858931d482SJason Sarich @*/ 2286d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetTotalIterationNumber(Tao tao, PetscInt *iter) 2287d71ae5a4SJacob Faibussowitsch { 22888931d482SJason Sarich PetscFunctionBegin; 22898931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 22908931d482SJason Sarich PetscValidIntPointer(iter, 2); 22918931d482SJason Sarich *iter = tao->ntotalits; 22923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22938931d482SJason Sarich } 22948931d482SJason Sarich 22958931d482SJason Sarich /*@ 22968931d482SJason Sarich TaoSetTotalIterationNumber - Sets the current total iteration number. 22978931d482SJason Sarich 2298c3339decSBarry Smith Logically Collective 22998931d482SJason Sarich 2300d8d19677SJose E. Roman Input Parameters: 230147450a7bSBarry Smith + tao - the `Tao` context 230247450a7bSBarry Smith - iter - the iteration number 23038931d482SJason Sarich 23048931d482SJason Sarich Level: developer 23058931d482SJason Sarich 230647450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetLinearSolveIterations()` 23078931d482SJason Sarich @*/ 2308d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetTotalIterationNumber(Tao tao, PetscInt iter) 2309d71ae5a4SJacob Faibussowitsch { 23108931d482SJason Sarich PetscFunctionBegin; 23118931d482SJason Sarich PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 231294511df7SStefano Zampini PetscValidLogicalCollectiveInt(tao, iter, 2); 23139566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)tao)); 23148931d482SJason Sarich tao->ntotalits = iter; 23159566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)tao)); 23163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23178931d482SJason Sarich } 23188931d482SJason Sarich 2319a7e14dcfSSatish Balay /*@ 232047450a7bSBarry Smith TaoSetConvergedReason - Sets the termination flag on a `Tao` object 2321a7e14dcfSSatish Balay 2322c3339decSBarry Smith Logically Collective 2323a7e14dcfSSatish Balay 2324a7e14dcfSSatish Balay Input Parameters: 232547450a7bSBarry Smith + tao - the `Tao` context 232647450a7bSBarry Smith - reason - the `TaoConvergedReason` 2327a7e14dcfSSatish Balay 2328a7e14dcfSSatish Balay Level: intermediate 2329a7e14dcfSSatish Balay 233047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason` 2331a7e14dcfSSatish Balay @*/ 2332d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason) 2333d71ae5a4SJacob Faibussowitsch { 2334a7e14dcfSSatish Balay PetscFunctionBegin; 233594511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 233694511df7SStefano Zampini PetscValidLogicalCollectiveEnum(tao, reason, 2); 2337a7e14dcfSSatish Balay tao->reason = reason; 23383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2339a7e14dcfSSatish Balay } 2340a7e14dcfSSatish Balay 2341a7e14dcfSSatish Balay /*@ 234247450a7bSBarry Smith TaoGetConvergedReason - Gets the reason the `TaoSolve()` was stopped. 2343a7e14dcfSSatish Balay 2344a7e14dcfSSatish Balay Not Collective 2345a7e14dcfSSatish Balay 2346a7e14dcfSSatish Balay Input Parameter: 234747450a7bSBarry Smith . tao - the `Tao` solver context 2348a7e14dcfSSatish Balay 2349a7e14dcfSSatish Balay Output Parameter: 235047450a7bSBarry Smith . reason - value of `TaoConvergedReason` 2351a7e14dcfSSatish Balay 2352a7e14dcfSSatish Balay Level: intermediate 2353a7e14dcfSSatish Balay 235447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoConvergedReason`, `TaoSetConvergenceTest()`, `TaoSetTolerances()` 2355a7e14dcfSSatish Balay @*/ 2356d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason) 2357d71ae5a4SJacob Faibussowitsch { 2358a7e14dcfSSatish Balay PetscFunctionBegin; 2359441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2360a7e14dcfSSatish Balay PetscValidPointer(reason, 2); 2361a7e14dcfSSatish Balay *reason = tao->reason; 23623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2363a7e14dcfSSatish Balay } 2364a7e14dcfSSatish Balay 2365a7e14dcfSSatish Balay /*@ 2366a7e14dcfSSatish Balay TaoGetSolutionStatus - Get the current iterate, objective value, 236747450a7bSBarry Smith residual, infeasibility, and termination from a `Tao` object 2368a7e14dcfSSatish Balay 2369a7e14dcfSSatish Balay Not Collective 2370a7e14dcfSSatish Balay 2371f899ff85SJose E. Roman Input Parameter: 237247450a7bSBarry Smith . tao - the `Tao` context 2373a7e14dcfSSatish Balay 2374a7e14dcfSSatish Balay Output Parameters: 2375a7e14dcfSSatish Balay + iterate - the current iterate number (>=0) 2376a7e14dcfSSatish Balay . f - the current function value 237758417fe7SBarry Smith . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality. 2378a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2379a7e14dcfSSatish Balay . xdiff - the step length or trust region radius of the most recent iterate. 238065ba42b6SBarry Smith - reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING` 2381a7e14dcfSSatish Balay 2382a7e14dcfSSatish Balay Level: intermediate 2383a7e14dcfSSatish Balay 238465ba42b6SBarry Smith Notes: 238565ba42b6SBarry Smith Tao returns the values set by the solvers in the routine `TaoMonitor()`. 2386a7e14dcfSSatish Balay 238767be906fSBarry Smith If any of the output arguments are set to `NULL`, no corresponding value will be returned. 2388a7e14dcfSSatish Balay 238947450a7bSBarry Smith .seealso: [](chapter_tao), `TaoMonitor()`, `TaoGetConvergedReason()` 2390a7e14dcfSSatish Balay @*/ 2391d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason) 2392d71ae5a4SJacob Faibussowitsch { 2393a7e14dcfSSatish Balay PetscFunctionBegin; 239494511df7SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2395a7e14dcfSSatish Balay if (its) *its = tao->niter; 2396a7e14dcfSSatish Balay if (f) *f = tao->fc; 2397a7e14dcfSSatish Balay if (gnorm) *gnorm = tao->residual; 2398a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->cnorm; 2399a7e14dcfSSatish Balay if (reason) *reason = tao->reason; 2400a7e14dcfSSatish Balay if (xdiff) *xdiff = tao->step; 24013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2402a7e14dcfSSatish Balay } 2403a7e14dcfSSatish Balay 2404a7e14dcfSSatish Balay /*@C 240547450a7bSBarry Smith TaoGetType - Gets the current `TaoType` being used in the `Tao` object 2406a7e14dcfSSatish Balay 2407a7e14dcfSSatish Balay Not Collective 2408a7e14dcfSSatish Balay 2409a7e14dcfSSatish Balay Input Parameter: 241047450a7bSBarry Smith . tao - the `Tao` solver context 2411a7e14dcfSSatish Balay 2412a7e14dcfSSatish Balay Output Parameter: 241347450a7bSBarry Smith . type - the `TaoType` 2414a7e14dcfSSatish Balay 2415a7e14dcfSSatish Balay Level: intermediate 2416a7e14dcfSSatish Balay 241747450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoType`, `TaoSetType()` 2418a7e14dcfSSatish Balay @*/ 2419d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetType(Tao tao, TaoType *type) 2420d71ae5a4SJacob Faibussowitsch { 2421a7e14dcfSSatish Balay PetscFunctionBegin; 2422441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2423a7e14dcfSSatish Balay PetscValidPointer(type, 2); 2424a7e14dcfSSatish Balay *type = ((PetscObject)tao)->type_name; 24253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2426a7e14dcfSSatish Balay } 2427a7e14dcfSSatish Balay 2428a7e14dcfSSatish Balay /*@C 2429a7e14dcfSSatish Balay TaoMonitor - Monitor the solver and the current solution. This 2430a7e14dcfSSatish Balay routine will record the iteration number and residual statistics, 2431670c031eSStefano Zampini and call any monitors specified by the user. 2432a7e14dcfSSatish Balay 2433a7e14dcfSSatish Balay Input Parameters: 243447450a7bSBarry Smith + tao - the `Tao` context 2435a7e14dcfSSatish Balay . its - the current iterate number (>=0) 2436a7e14dcfSSatish Balay . f - the current objective function value 243758417fe7SBarry Smith . res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality. This measure will be recorded and 2438a7e14dcfSSatish Balay used for some termination tests. 2439a7e14dcfSSatish Balay . cnorm - the infeasibility of the current solution with regard to the constraints. 2440a7e14dcfSSatish Balay - steplength - multiple of the step direction added to the previous iterate. 2441a7e14dcfSSatish Balay 244247450a7bSBarry Smith Output Parameter: 244365ba42b6SBarry Smith . reason - The termination reason, which can equal `TAO_CONTINUE_ITERATING` 2444a7e14dcfSSatish Balay 2445a7e14dcfSSatish Balay Options Database Key: 2446a7e14dcfSSatish Balay . -tao_monitor - Use the default monitor, which prints statistics to standard output 2447a7e14dcfSSatish Balay 2448a7e14dcfSSatish Balay Level: developer 2449a7e14dcfSSatish Balay 245047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetConvergedReason()`, `TaoMonitorDefault()`, `TaoSetMonitor()` 2451a7e14dcfSSatish Balay @*/ 2452d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength) 2453d71ae5a4SJacob Faibussowitsch { 245447a47007SBarry Smith PetscInt i; 245547a47007SBarry Smith 2456a7e14dcfSSatish Balay PetscFunctionBegin; 2457441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2458a7e14dcfSSatish Balay tao->fc = f; 2459a7e14dcfSSatish Balay tao->residual = res; 2460a7e14dcfSSatish Balay tao->cnorm = cnorm; 2461a7e14dcfSSatish Balay tao->step = steplength; 2462e52336cbSBarry Smith if (!its) { 246394511df7SStefano Zampini tao->cnorm0 = cnorm; 246494511df7SStefano Zampini tao->gnorm0 = res; 2465a7e14dcfSSatish Balay } 24663c859ba3SBarry Smith PetscCheck(!PetscIsInfOrNanReal(f) && !PetscIsInfOrNanReal(res), PetscObjectComm((PetscObject)tao), PETSC_ERR_USER, "User provided compute function generated Inf or NaN"); 246748a46eb9SPierre Jolivet for (i = 0; i < tao->numbermonitors; i++) PetscCall((*tao->monitor[i])(tao, tao->monitorcontext[i])); 24683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2469a7e14dcfSSatish Balay } 2470a7e14dcfSSatish Balay 2471a7e14dcfSSatish Balay /*@ 2472ae93cb3cSJason Sarich TaoSetConvergenceHistory - Sets the array used to hold the convergence history. 2473a7e14dcfSSatish Balay 2474c3339decSBarry Smith Logically Collective 2475a7e14dcfSSatish Balay 2476a7e14dcfSSatish Balay Input Parameters: 247747450a7bSBarry Smith + tao - the `Tao` solver context 2478a7e14dcfSSatish Balay . obj - array to hold objective value history 2479a7e14dcfSSatish Balay . resid - array to hold residual history 2480a7e14dcfSSatish Balay . cnorm - array to hold constraint violation history 2481be1558d8SJason Sarich . lits - integer array holds the number of linear iterations for each Tao iteration 248267be906fSBarry Smith . na - size of `obj`, `resid`, and `cnorm` 248365ba42b6SBarry Smith - reset - `PETSC_TRUE` indicates each new minimization resets the history counter to zero, 2484a7e14dcfSSatish Balay else it continues storing new values for new minimizations after the old ones 2485a7e14dcfSSatish Balay 248667be906fSBarry Smith Level: intermediate 248767be906fSBarry Smith 2488a7e14dcfSSatish Balay Notes: 248947450a7bSBarry Smith If set, `Tao` will fill the given arrays with the indicated 2490ae93cb3cSJason Sarich information at each iteration. If 'obj','resid','cnorm','lits' are 249167be906fSBarry Smith *all* `NULL` then space (using size `na`, or 1000 if na is `PETSC_DECIDE` or 249265ba42b6SBarry Smith `PETSC_DEFAULT`) is allocated for the history. 249367be906fSBarry Smith If not all are `NULL`, then only the non-`NULL` information categories 2494ae93cb3cSJason Sarich will be stored, the others will be ignored. 2495ae93cb3cSJason Sarich 2496ae93cb3cSJason Sarich Any convergence information after iteration number 'na' will not be stored. 2497a7e14dcfSSatish Balay 2498a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2499a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2500a7e14dcfSSatish Balay during the section of code that is being timed. 2501a7e14dcfSSatish Balay 250247450a7bSBarry Smith .seealso: [](chapter_tao), `TaoGetConvergenceHistory()` 2503a7e14dcfSSatish Balay @*/ 2504d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na, PetscBool reset) 2505d71ae5a4SJacob Faibussowitsch { 2506a7e14dcfSSatish Balay PetscFunctionBegin; 2507441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2508064a246eSJacob Faibussowitsch if (obj) PetscValidRealPointer(obj, 2); 2509064a246eSJacob Faibussowitsch if (resid) PetscValidRealPointer(resid, 3); 2510064a246eSJacob Faibussowitsch if (cnorm) PetscValidRealPointer(cnorm, 4); 2511ae93cb3cSJason Sarich if (lits) PetscValidIntPointer(lits, 5); 2512ae93cb3cSJason Sarich 2513ae93cb3cSJason Sarich if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2514ae93cb3cSJason Sarich if (!obj && !resid && !cnorm && !lits) { 25159566063dSJacob Faibussowitsch PetscCall(PetscCalloc4(na, &obj, na, &resid, na, &cnorm, na, &lits)); 2516ae93cb3cSJason Sarich tao->hist_malloc = PETSC_TRUE; 2517ae93cb3cSJason Sarich } 2518ae93cb3cSJason Sarich 2519a7e14dcfSSatish Balay tao->hist_obj = obj; 2520a7e14dcfSSatish Balay tao->hist_resid = resid; 2521a7e14dcfSSatish Balay tao->hist_cnorm = cnorm; 2522ae93cb3cSJason Sarich tao->hist_lits = lits; 2523a7e14dcfSSatish Balay tao->hist_max = na; 2524a7e14dcfSSatish Balay tao->hist_reset = reset; 2525ae93cb3cSJason Sarich tao->hist_len = 0; 25263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2527a7e14dcfSSatish Balay } 2528a7e14dcfSSatish Balay 2529a7e14dcfSSatish Balay /*@C 253065ba42b6SBarry Smith TaoGetConvergenceHistory - Gets the arrays used that hold the convergence history. 2531a7e14dcfSSatish Balay 2532c3339decSBarry Smith Collective 2533a7e14dcfSSatish Balay 2534a7e14dcfSSatish Balay Input Parameter: 253547450a7bSBarry Smith . tao - the `Tao` context 2536a7e14dcfSSatish Balay 2537a7e14dcfSSatish Balay Output Parameters: 2538a7e14dcfSSatish Balay + obj - array used to hold objective value history 2539a7e14dcfSSatish Balay . resid - array used to hold residual history 2540a7e14dcfSSatish Balay . cnorm - array used to hold constraint violation history 2541ae93cb3cSJason Sarich . lits - integer array used to hold linear solver iteration count 254267be906fSBarry Smith - nhist - size of `obj`, `resid`, `cnorm`, and `lits` 254367be906fSBarry Smith 254467be906fSBarry Smith Level: advanced 2545a7e14dcfSSatish Balay 2546a7e14dcfSSatish Balay Notes: 254765ba42b6SBarry Smith This routine must be preceded by calls to `TaoSetConvergenceHistory()` 254865ba42b6SBarry Smith and `TaoSolve()`, otherwise it returns useless information. 2549ae93cb3cSJason Sarich 2550a7e14dcfSSatish Balay This routine is useful, e.g., when running a code for purposes 2551a7e14dcfSSatish Balay of accurate performance monitoring, when no I/O should be done 2552a7e14dcfSSatish Balay during the section of code that is being timed. 2553a7e14dcfSSatish Balay 255467be906fSBarry Smith Fortran Note: 255567be906fSBarry Smith The calling sequence is 255667be906fSBarry Smith .vb 255767be906fSBarry Smith call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr) 255867be906fSBarry Smith .ve 2559a7e14dcfSSatish Balay 256047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSolve()`, `TaoSetConvergenceHistory()` 2561a7e14dcfSSatish Balay @*/ 2562d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist) 2563d71ae5a4SJacob Faibussowitsch { 2564a7e14dcfSSatish Balay PetscFunctionBegin; 2565441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2566a7e14dcfSSatish Balay if (obj) *obj = tao->hist_obj; 2567a7e14dcfSSatish Balay if (cnorm) *cnorm = tao->hist_cnorm; 2568a7e14dcfSSatish Balay if (resid) *resid = tao->hist_resid; 25695b494b05SBarry Smith if (lits) *lits = tao->hist_lits; 2570a7e14dcfSSatish Balay if (nhist) *nhist = tao->hist_len; 25713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2572a7e14dcfSSatish Balay } 2573a7e14dcfSSatish Balay 2574a7e14dcfSSatish Balay /*@ 257547450a7bSBarry Smith TaoSetApplicationContext - Sets the optional user-defined context for a `Tao` solver. 2576a7e14dcfSSatish Balay 2577c3339decSBarry Smith Logically Collective 2578a7e14dcfSSatish Balay 2579a7e14dcfSSatish Balay Input Parameters: 258047450a7bSBarry Smith + tao - the `Tao` context 2581a7e14dcfSSatish Balay - usrP - optional user context 2582a7e14dcfSSatish Balay 2583a7e14dcfSSatish Balay Level: intermediate 2584a7e14dcfSSatish Balay 258547450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetApplicationContext()`, `TaoSetApplicationContext()` 2586a7e14dcfSSatish Balay @*/ 2587d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetApplicationContext(Tao tao, void *usrP) 2588d71ae5a4SJacob Faibussowitsch { 2589a7e14dcfSSatish Balay PetscFunctionBegin; 2590441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 2591a7e14dcfSSatish Balay tao->user = usrP; 25923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2593a7e14dcfSSatish Balay } 2594a7e14dcfSSatish Balay 2595a7e14dcfSSatish Balay /*@ 259647450a7bSBarry Smith TaoGetApplicationContext - Gets the user-defined context for a `Tao` solver 2597a7e14dcfSSatish Balay 2598a7e14dcfSSatish Balay Not Collective 2599a7e14dcfSSatish Balay 2600a7e14dcfSSatish Balay Input Parameter: 260147450a7bSBarry Smith . tao - the `Tao` context 2602a7e14dcfSSatish Balay 2603a7e14dcfSSatish Balay Output Parameter: 2604a7e14dcfSSatish Balay . usrP - user context 2605a7e14dcfSSatish Balay 2606a7e14dcfSSatish Balay Level: intermediate 2607a7e14dcfSSatish Balay 260847450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetApplicationContext()` 2609a7e14dcfSSatish Balay @*/ 2610d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetApplicationContext(Tao tao, void *usrP) 2611d71ae5a4SJacob Faibussowitsch { 2612a7e14dcfSSatish Balay PetscFunctionBegin; 2613441846f8SBarry Smith PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 261494511df7SStefano Zampini PetscValidPointer(usrP, 2); 2615a7e14dcfSSatish Balay *(void **)usrP = tao->user; 26163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2617a7e14dcfSSatish Balay } 2618a9603a14SPatrick Farrell 2619a9603a14SPatrick Farrell /*@ 262065ba42b6SBarry Smith TaoSetGradientNorm - Sets the matrix used to define the norm that measures the size of the gradient. 2621a9603a14SPatrick Farrell 2622c3339decSBarry Smith Collective 2623a9603a14SPatrick Farrell 2624a9603a14SPatrick Farrell Input Parameters: 262547450a7bSBarry Smith + tao - the `Tao` context 262665ba42b6SBarry Smith - M - matrix that defines the norm 2627a9603a14SPatrick Farrell 2628a9603a14SPatrick Farrell Level: beginner 2629a9603a14SPatrick Farrell 263047450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoGetGradientNorm()`, `TaoGradientNorm()` 2631a9603a14SPatrick Farrell @*/ 2632d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M) 2633d71ae5a4SJacob Faibussowitsch { 2634a9603a14SPatrick Farrell PetscFunctionBegin; 2635a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 263694511df7SStefano Zampini PetscValidHeaderSpecific(M, MAT_CLASSID, 2); 26379566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)M)); 26389566063dSJacob Faibussowitsch PetscCall(MatDestroy(&tao->gradient_norm)); 26399566063dSJacob Faibussowitsch PetscCall(VecDestroy(&tao->gradient_norm_tmp)); 2640a9603a14SPatrick Farrell tao->gradient_norm = M; 26419566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(M, NULL, &tao->gradient_norm_tmp)); 26423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2643a9603a14SPatrick Farrell } 2644a9603a14SPatrick Farrell 2645a9603a14SPatrick Farrell /*@ 264665ba42b6SBarry Smith TaoGetGradientNorm - Returns the matrix used to define the norm used for measuring the size of the gradient. 2647a9603a14SPatrick Farrell 2648a9603a14SPatrick Farrell Not Collective 2649a9603a14SPatrick Farrell 2650a9603a14SPatrick Farrell Input Parameter: 265147450a7bSBarry Smith . tao - the `Tao` context 2652a9603a14SPatrick Farrell 2653a9603a14SPatrick Farrell Output Parameter: 2654a9603a14SPatrick Farrell . M - gradient norm 2655a9603a14SPatrick Farrell 2656a9603a14SPatrick Farrell Level: beginner 2657a9603a14SPatrick Farrell 265847450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGradientNorm()` 2659a9603a14SPatrick Farrell @*/ 2660d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M) 2661d71ae5a4SJacob Faibussowitsch { 2662a9603a14SPatrick Farrell PetscFunctionBegin; 2663a9603a14SPatrick Farrell PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 266494511df7SStefano Zampini PetscValidPointer(M, 2); 2665a9603a14SPatrick Farrell *M = tao->gradient_norm; 26663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2667a9603a14SPatrick Farrell } 2668a9603a14SPatrick Farrell 266905aca2cdSSatish Balay /*@C 267047450a7bSBarry Smith TaoGradientNorm - Compute the norm using the `NormType`, the user has selected 2671a9603a14SPatrick Farrell 2672c3339decSBarry Smith Collective 2673a9603a14SPatrick Farrell 2674d8d19677SJose E. Roman Input Parameters: 267547450a7bSBarry Smith + tao - the `Tao` context 2676a9603a14SPatrick Farrell . gradient - the gradient to be computed 26772653c764SPierre Jolivet - norm - the norm type 2678a9603a14SPatrick Farrell 2679a9603a14SPatrick Farrell Output Parameter: 2680a9603a14SPatrick Farrell . gnorm - the gradient norm 2681a9603a14SPatrick Farrell 268247450a7bSBarry Smith Level: advanced 2683a9603a14SPatrick Farrell 268447450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoSetGradientNorm()`, `TaoGetGradientNorm()` 2685a9603a14SPatrick Farrell @*/ 2686d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm) 2687d71ae5a4SJacob Faibussowitsch { 2688a9603a14SPatrick Farrell PetscFunctionBegin; 26898854b543SStefano Zampini PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 26908854b543SStefano Zampini PetscValidHeaderSpecific(gradient, VEC_CLASSID, 2); 26918854b543SStefano Zampini PetscValidLogicalCollectiveEnum(tao, type, 3); 269294511df7SStefano Zampini PetscValidRealPointer(gnorm, 4); 2693a9603a14SPatrick Farrell if (tao->gradient_norm) { 2694680e2bc7SPatrick Farrell PetscScalar gnorms; 2695680e2bc7SPatrick Farrell 26963c859ba3SBarry 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."); 26979566063dSJacob Faibussowitsch PetscCall(MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp)); 26989566063dSJacob Faibussowitsch PetscCall(VecDot(gradient, tao->gradient_norm_tmp, &gnorms)); 26997bb79937SPatrick Farrell *gnorm = PetscRealPart(PetscSqrtScalar(gnorms)); 2700a9603a14SPatrick Farrell } else { 27019566063dSJacob Faibussowitsch PetscCall(VecNorm(gradient, type, gnorm)); 2702a9603a14SPatrick Farrell } 27033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2704a9603a14SPatrick Farrell } 2705a9603a14SPatrick Farrell 2706e882e171SHong Zhang /*@C 270747450a7bSBarry Smith TaoMonitorDrawCtxCreate - Creates the monitor context for `TaoMonitorDrawSolution()` 2708a9603a14SPatrick Farrell 2709c3339decSBarry Smith Collective 2710e882e171SHong Zhang 2711d5b43468SJose E. Roman Output Parameter: 2712e882e171SHong Zhang . ctx - the monitor context 2713e882e171SHong Zhang 27143c7db156SBarry Smith Options Database Key: 2715e882e171SHong Zhang . -tao_draw_solution_initial - show initial guess as well as current solution 2716e882e171SHong Zhang 2717e882e171SHong Zhang Level: intermediate 2718e882e171SHong Zhang 271947450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawCtx()` 2720e882e171SHong Zhang @*/ 2721d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm, const char host[], const char label[], int x, int y, int m, int n, PetscInt howoften, TaoMonitorDrawCtx *ctx) 2722d71ae5a4SJacob Faibussowitsch { 2723e882e171SHong Zhang PetscFunctionBegin; 27249566063dSJacob Faibussowitsch PetscCall(PetscNew(ctx)); 27259566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(comm, host, label, x, y, m, n, &(*ctx)->viewer)); 27269566063dSJacob Faibussowitsch PetscCall(PetscViewerSetFromOptions((*ctx)->viewer)); 2727e882e171SHong Zhang (*ctx)->howoften = howoften; 27283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2729e882e171SHong Zhang } 2730e882e171SHong Zhang 2731e882e171SHong Zhang /*@C 273265ba42b6SBarry Smith TaoMonitorDrawCtxDestroy - Destroys the monitor context for `TaoMonitorDrawSolution()` 2733e882e171SHong Zhang 2734c3339decSBarry Smith Collective 2735e882e171SHong Zhang 273647450a7bSBarry Smith Input Parameter: 2737e882e171SHong Zhang . ctx - the monitor context 2738e882e171SHong Zhang 2739e882e171SHong Zhang Level: intermediate 2740e882e171SHong Zhang 274147450a7bSBarry Smith .seealso: [](chapter_tao), `Tao`, `TaoMonitorSet()`, `TaoMonitorDefault()`, `VecView()`, `TaoMonitorDrawSolution()` 2742e882e171SHong Zhang @*/ 2743d71ae5a4SJacob Faibussowitsch PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx) 2744d71ae5a4SJacob Faibussowitsch { 2745e882e171SHong Zhang PetscFunctionBegin; 27469566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&(*ictx)->viewer)); 27479566063dSJacob Faibussowitsch PetscCall(PetscFree(*ictx)); 27483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2749e882e171SHong Zhang } 2750