1 #define TAO_DLL 2 3 #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 4 5 PetscBool TaoRegisterAllCalled = PETSC_FALSE; 6 PetscFunctionList TaoList = NULL; 7 8 PetscClassId TAO_CLASSID; 9 PetscLogEvent Tao_Solve, Tao_ObjectiveEval, Tao_GradientEval, Tao_ObjGradientEval, Tao_HessianEval, Tao_ConstraintsEval, Tao_JacobianEval; 10 11 const char *TaoSubSetTypes[] = { "subvec","mask","matrixfree","TaoSubSetType","TAO_SUBSET_",0}; 12 13 struct _n_TaoMonitorDrawCtx { 14 PetscViewer viewer; 15 PetscInt howoften; /* when > 0 uses iteration % howoften, when negative only final solution plotted */ 16 }; 17 18 /*@ 19 TaoCreate - Creates a TAO solver 20 21 Collective on MPI_Comm 22 23 Input Parameter: 24 . comm - MPI communicator 25 26 Output Parameter: 27 . newtao - the new Tao context 28 29 Available methods include: 30 + nls - Newton's method with line search for unconstrained minimization 31 . ntr - Newton's method with trust region for unconstrained minimization 32 . ntl - Newton's method with trust region, line search for unconstrained minimization 33 . lmvm - Limited memory variable metric method for unconstrained minimization 34 . cg - Nonlinear conjugate gradient method for unconstrained minimization 35 . nm - Nelder-Mead algorithm for derivate-free unconstrained minimization 36 . tron - Newton Trust Region method for bound constrained minimization 37 . gpcg - Newton Trust Region method for quadratic bound constrained minimization 38 . blmvm - Limited memory variable metric method for bound constrained minimization 39 . lcl - Linearly constrained Lagrangian method for pde-constrained minimization 40 - pounders - Model-based algorithm for nonlinear least squares 41 42 Options Database Keys: 43 . -tao_type - select which method TAO should use 44 45 Level: beginner 46 47 .seealso: TaoSolve(), TaoDestroy() 48 @*/ 49 PetscErrorCode TaoCreate(MPI_Comm comm, Tao *newtao) 50 { 51 PetscErrorCode ierr; 52 Tao tao; 53 54 PetscFunctionBegin; 55 PetscValidPointer(newtao,2); 56 *newtao = NULL; 57 58 ierr = TaoInitializePackage();CHKERRQ(ierr); 59 ierr = TaoLineSearchInitializePackage();CHKERRQ(ierr); 60 61 ierr = PetscHeaderCreate(tao,TAO_CLASSID,"Tao","Optimization solver","Tao",comm,TaoDestroy,TaoView);CHKERRQ(ierr); 62 tao->ops->computeobjective=0; 63 tao->ops->computeobjectiveandgradient=0; 64 tao->ops->computegradient=0; 65 tao->ops->computehessian=0; 66 tao->ops->computeseparableobjective=0; 67 tao->ops->computeconstraints=0; 68 tao->ops->computejacobian=0; 69 tao->ops->computejacobianequality=0; 70 tao->ops->computejacobianinequality=0; 71 tao->ops->computeequalityconstraints=0; 72 tao->ops->computeinequalityconstraints=0; 73 tao->ops->convergencetest=TaoDefaultConvergenceTest; 74 tao->ops->convergencedestroy=0; 75 tao->ops->computedual=0; 76 tao->ops->setup=0; 77 tao->ops->solve=0; 78 tao->ops->view=0; 79 tao->ops->setfromoptions=0; 80 tao->ops->destroy=0; 81 82 tao->solution=NULL; 83 tao->gradient=NULL; 84 tao->sep_objective = NULL; 85 tao->constraints=NULL; 86 tao->constraints_equality=NULL; 87 tao->constraints_inequality=NULL; 88 tao->sep_weights_v=NULL; 89 tao->sep_weights_w=NULL; 90 tao->stepdirection=NULL; 91 tao->niter=0; 92 tao->ntotalits=0; 93 tao->XL = NULL; 94 tao->XU = NULL; 95 tao->IL = NULL; 96 tao->IU = NULL; 97 tao->DI = NULL; 98 tao->DE = NULL; 99 tao->gradient_norm = NULL; 100 tao->gradient_norm_tmp = NULL; 101 tao->hessian = NULL; 102 tao->hessian_pre = NULL; 103 tao->jacobian = NULL; 104 tao->jacobian_pre = NULL; 105 tao->jacobian_state = NULL; 106 tao->jacobian_state_pre = NULL; 107 tao->jacobian_state_inv = NULL; 108 tao->jacobian_design = NULL; 109 tao->jacobian_design_pre = NULL; 110 tao->jacobian_equality = NULL; 111 tao->jacobian_equality_pre = NULL; 112 tao->jacobian_inequality = NULL; 113 tao->jacobian_inequality_pre = NULL; 114 tao->state_is = NULL; 115 tao->design_is = NULL; 116 117 tao->max_it = 10000; 118 tao->max_funcs = 10000; 119 #if defined(PETSC_USE_REAL_SINGLE) 120 tao->gatol = 1e-5; 121 tao->grtol = 1e-5; 122 #else 123 tao->gatol = 1e-8; 124 tao->grtol = 1e-8; 125 #endif 126 tao->crtol = 0.0; 127 tao->catol = 0.0; 128 tao->gttol = 0.0; 129 tao->steptol = 0.0; 130 tao->trust0 = PETSC_INFINITY; 131 tao->fmin = PETSC_NINFINITY; 132 tao->hist_malloc = PETSC_FALSE; 133 tao->hist_reset = PETSC_TRUE; 134 tao->hist_max = 0; 135 tao->hist_len = 0; 136 tao->hist_obj = NULL; 137 tao->hist_resid = NULL; 138 tao->hist_cnorm = NULL; 139 tao->hist_lits = NULL; 140 141 tao->numbermonitors=0; 142 tao->viewsolution=PETSC_FALSE; 143 tao->viewhessian=PETSC_FALSE; 144 tao->viewgradient=PETSC_FALSE; 145 tao->viewjacobian=PETSC_FALSE; 146 tao->viewconstraints = PETSC_FALSE; 147 148 /* These flags prevents algorithms from overriding user options */ 149 tao->max_it_changed =PETSC_FALSE; 150 tao->max_funcs_changed=PETSC_FALSE; 151 tao->gatol_changed =PETSC_FALSE; 152 tao->grtol_changed =PETSC_FALSE; 153 tao->gttol_changed =PETSC_FALSE; 154 tao->steptol_changed =PETSC_FALSE; 155 tao->trust0_changed =PETSC_FALSE; 156 tao->fmin_changed =PETSC_FALSE; 157 tao->catol_changed =PETSC_FALSE; 158 tao->crtol_changed =PETSC_FALSE; 159 ierr = TaoResetStatistics(tao);CHKERRQ(ierr); 160 *newtao = tao; 161 PetscFunctionReturn(0); 162 } 163 164 /*@ 165 TaoSolve - Solves an optimization problem min F(x) s.t. l <= x <= u 166 167 Collective on Tao 168 169 Input Parameters: 170 . tao - the Tao context 171 172 Notes: 173 The user must set up the Tao with calls to TaoSetInitialVector(), 174 TaoSetObjectiveRoutine(), 175 TaoSetGradientRoutine(), and (if using 2nd order method) TaoSetHessianRoutine(). 176 177 You should call TaoGetConvergedReason() or run with -tao_converged_reason to determine if the optimization algorithm actually succeeded or 178 why it failed. 179 180 Level: beginner 181 182 .seealso: TaoCreate(), TaoSetObjectiveRoutine(), TaoSetGradientRoutine(), TaoSetHessianRoutine(), TaoGetConvergedReason() 183 @*/ 184 PetscErrorCode TaoSolve(Tao tao) 185 { 186 PetscErrorCode ierr; 187 static PetscBool set = PETSC_FALSE; 188 189 PetscFunctionBegin; 190 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 191 ierr = PetscCitationsRegister("@TechReport{tao-user-ref,\n" 192 "title = {Toolkit for Advanced Optimization (TAO) Users Manual},\n" 193 "author = {Todd Munson and Jason Sarich and Stefan Wild and Steve Benson and Lois Curfman McInnes},\n" 194 "Institution = {Argonne National Laboratory},\n" 195 "Year = 2014,\n" 196 "Number = {ANL/MCS-TM-322 - Revision 3.5},\n" 197 "url = {http://www.mcs.anl.gov/tao}\n}\n",&set);CHKERRQ(ierr); 198 199 ierr = TaoSetUp(tao);CHKERRQ(ierr); 200 ierr = TaoResetStatistics(tao);CHKERRQ(ierr); 201 if (tao->linesearch) { 202 ierr = TaoLineSearchReset(tao->linesearch);CHKERRQ(ierr); 203 } 204 205 ierr = PetscLogEventBegin(Tao_Solve,tao,0,0,0);CHKERRQ(ierr); 206 if (tao->ops->solve){ ierr = (*tao->ops->solve)(tao);CHKERRQ(ierr); } 207 ierr = PetscLogEventEnd(Tao_Solve,tao,0,0,0);CHKERRQ(ierr); 208 209 tao->ntotalits += tao->niter; 210 ierr = TaoViewFromOptions(tao,NULL,"-tao_view");CHKERRQ(ierr); 211 212 if (tao->printreason) { 213 if (tao->reason > 0) { 214 ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve converged due to %s iterations %D\n",TaoConvergedReasons[tao->reason],tao->niter);CHKERRQ(ierr); 215 } else { 216 ierr = PetscPrintf(((PetscObject)tao)->comm,"TAO solve did not converge due to %s iteration %D\n",TaoConvergedReasons[tao->reason],tao->niter);CHKERRQ(ierr); 217 } 218 } 219 PetscFunctionReturn(0); 220 } 221 222 /*@ 223 TaoSetUp - Sets up the internal data structures for the later use 224 of a Tao solver 225 226 Collective on tao 227 228 Input Parameters: 229 . tao - the TAO context 230 231 Notes: 232 The user will not need to explicitly call TaoSetUp(), as it will 233 automatically be called in TaoSolve(). However, if the user 234 desires to call it explicitly, it should come after TaoCreate() 235 and any TaoSetSomething() routines, but before TaoSolve(). 236 237 Level: advanced 238 239 .seealso: TaoCreate(), TaoSolve() 240 @*/ 241 PetscErrorCode TaoSetUp(Tao tao) 242 { 243 PetscErrorCode ierr; 244 245 PetscFunctionBegin; 246 PetscValidHeaderSpecific(tao, TAO_CLASSID,1); 247 if (tao->setupcalled) PetscFunctionReturn(0); 248 249 if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetInitialVector"); 250 if (tao->ops->setup) { 251 ierr = (*tao->ops->setup)(tao);CHKERRQ(ierr); 252 } 253 tao->setupcalled = PETSC_TRUE; 254 PetscFunctionReturn(0); 255 } 256 257 /*@ 258 TaoDestroy - Destroys the TAO context that was created with 259 TaoCreate() 260 261 Collective on Tao 262 263 Input Parameter: 264 . tao - the Tao context 265 266 Level: beginner 267 268 .seealso: TaoCreate(), TaoSolve() 269 @*/ 270 PetscErrorCode TaoDestroy(Tao *tao) 271 { 272 PetscErrorCode ierr; 273 274 PetscFunctionBegin; 275 if (!*tao) PetscFunctionReturn(0); 276 PetscValidHeaderSpecific(*tao,TAO_CLASSID,1); 277 if (--((PetscObject)*tao)->refct > 0) {*tao=0;PetscFunctionReturn(0);} 278 279 if ((*tao)->ops->destroy) { 280 ierr = (*((*tao))->ops->destroy)(*tao);CHKERRQ(ierr); 281 } 282 ierr = KSPDestroy(&(*tao)->ksp);CHKERRQ(ierr); 283 ierr = TaoLineSearchDestroy(&(*tao)->linesearch);CHKERRQ(ierr); 284 285 if ((*tao)->ops->convergencedestroy) { 286 ierr = (*(*tao)->ops->convergencedestroy)((*tao)->cnvP);CHKERRQ(ierr); 287 if ((*tao)->jacobian_state_inv) { 288 ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr); 289 } 290 } 291 ierr = VecDestroy(&(*tao)->solution);CHKERRQ(ierr); 292 ierr = VecDestroy(&(*tao)->gradient);CHKERRQ(ierr); 293 294 if ((*tao)->gradient_norm) { 295 ierr = PetscObjectDereference((PetscObject)(*tao)->gradient_norm);CHKERRQ(ierr); 296 ierr = VecDestroy(&(*tao)->gradient_norm_tmp);CHKERRQ(ierr); 297 } 298 299 ierr = VecDestroy(&(*tao)->XL);CHKERRQ(ierr); 300 ierr = VecDestroy(&(*tao)->XU);CHKERRQ(ierr); 301 ierr = VecDestroy(&(*tao)->IL);CHKERRQ(ierr); 302 ierr = VecDestroy(&(*tao)->IU);CHKERRQ(ierr); 303 ierr = VecDestroy(&(*tao)->DE);CHKERRQ(ierr); 304 ierr = VecDestroy(&(*tao)->DI);CHKERRQ(ierr); 305 ierr = VecDestroy(&(*tao)->constraints_equality);CHKERRQ(ierr); 306 ierr = VecDestroy(&(*tao)->constraints_inequality);CHKERRQ(ierr); 307 ierr = VecDestroy(&(*tao)->stepdirection);CHKERRQ(ierr); 308 ierr = MatDestroy(&(*tao)->hessian_pre);CHKERRQ(ierr); 309 ierr = MatDestroy(&(*tao)->hessian);CHKERRQ(ierr); 310 ierr = MatDestroy(&(*tao)->jacobian_pre);CHKERRQ(ierr); 311 ierr = MatDestroy(&(*tao)->jacobian);CHKERRQ(ierr); 312 ierr = MatDestroy(&(*tao)->jacobian_state_pre);CHKERRQ(ierr); 313 ierr = MatDestroy(&(*tao)->jacobian_state);CHKERRQ(ierr); 314 ierr = MatDestroy(&(*tao)->jacobian_state_inv);CHKERRQ(ierr); 315 ierr = MatDestroy(&(*tao)->jacobian_design);CHKERRQ(ierr); 316 ierr = MatDestroy(&(*tao)->jacobian_equality);CHKERRQ(ierr); 317 ierr = MatDestroy(&(*tao)->jacobian_equality_pre);CHKERRQ(ierr); 318 ierr = MatDestroy(&(*tao)->jacobian_inequality);CHKERRQ(ierr); 319 ierr = MatDestroy(&(*tao)->jacobian_inequality_pre);CHKERRQ(ierr); 320 ierr = ISDestroy(&(*tao)->state_is);CHKERRQ(ierr); 321 ierr = ISDestroy(&(*tao)->design_is);CHKERRQ(ierr); 322 ierr = VecDestroy(&(*tao)->sep_weights_v);CHKERRQ(ierr); 323 ierr = TaoCancelMonitors(*tao);CHKERRQ(ierr); 324 if ((*tao)->hist_malloc) { 325 ierr = PetscFree((*tao)->hist_obj);CHKERRQ(ierr); 326 ierr = PetscFree((*tao)->hist_resid);CHKERRQ(ierr); 327 ierr = PetscFree((*tao)->hist_cnorm);CHKERRQ(ierr); 328 ierr = PetscFree((*tao)->hist_lits);CHKERRQ(ierr); 329 } 330 if ((*tao)->sep_weights_n) { 331 ierr = PetscFree((*tao)->sep_weights_rows);CHKERRQ(ierr); 332 ierr = PetscFree((*tao)->sep_weights_cols);CHKERRQ(ierr); 333 ierr = PetscFree((*tao)->sep_weights_w);CHKERRQ(ierr); 334 } 335 ierr = PetscHeaderDestroy(tao);CHKERRQ(ierr); 336 PetscFunctionReturn(0); 337 } 338 339 /*@ 340 TaoSetFromOptions - Sets various Tao parameters from user 341 options. 342 343 Collective on Tao 344 345 Input Paremeter: 346 . tao - the Tao solver context 347 348 options Database Keys: 349 + -tao_type <type> - The algorithm that TAO uses (lmvm, nls, etc.) 350 . -tao_gatol <gatol> - absolute error tolerance for ||gradient|| 351 . -tao_grtol <grtol> - relative error tolerance for ||gradient|| 352 . -tao_gttol <gttol> - reduction of ||gradient|| relative to initial gradient 353 . -tao_max_it <max> - sets maximum number of iterations 354 . -tao_max_funcs <max> - sets maximum number of function evaluations 355 . -tao_fmin <fmin> - stop if function value reaches fmin 356 . -tao_steptol <tol> - stop if trust region radius less than <tol> 357 . -tao_trust0 <t> - initial trust region radius 358 . -tao_monitor - prints function value and residual at each iteration 359 . -tao_smonitor - same as tao_monitor, but truncates very small values 360 . -tao_cmonitor - prints function value, residual, and constraint norm at each iteration 361 . -tao_view_solution - prints solution vector at each iteration 362 . -tao_view_separableobjective - prints separable objective vector at each iteration 363 . -tao_view_step - prints step direction vector at each iteration 364 . -tao_view_gradient - prints gradient vector at each iteration 365 . -tao_draw_solution - graphically view solution vector at each iteration 366 . -tao_draw_step - graphically view step vector at each iteration 367 . -tao_draw_gradient - graphically view gradient at each iteration 368 . -tao_fd_gradient - use gradient computed with finite differences 369 . -tao_cancelmonitors - cancels all monitors (except those set with command line) 370 . -tao_view - prints information about the Tao after solving 371 - -tao_converged_reason - prints the reason TAO stopped iterating 372 373 Notes: 374 To see all options, run your program with the -help option or consult the 375 user's manual. Should be called after TaoCreate() but before TaoSolve() 376 377 Level: beginner 378 @*/ 379 PetscErrorCode TaoSetFromOptions(Tao tao) 380 { 381 PetscErrorCode ierr; 382 const TaoType default_type = TAOLMVM; 383 char type[256], monfilename[PETSC_MAX_PATH_LEN]; 384 PetscViewer monviewer; 385 PetscBool flg; 386 MPI_Comm comm; 387 388 PetscFunctionBegin; 389 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 390 ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr); 391 392 /* So no warnings are given about unused options */ 393 ierr = PetscOptionsHasName(((PetscObject)tao)->options,((PetscObject)tao)->prefix,"-tao_ls_type",&flg);CHKERRQ(ierr); 394 395 ierr = PetscObjectOptionsBegin((PetscObject)tao);CHKERRQ(ierr); 396 { 397 ierr = TaoRegisterAll();CHKERRQ(ierr); 398 if (((PetscObject)tao)->type_name) { 399 default_type = ((PetscObject)tao)->type_name; 400 } 401 /* Check for type from options */ 402 ierr = PetscOptionsFList("-tao_type","Tao Solver type","TaoSetType",TaoList,default_type,type,256,&flg);CHKERRQ(ierr); 403 if (flg) { 404 ierr = TaoSetType(tao,type);CHKERRQ(ierr); 405 } else if (!((PetscObject)tao)->type_name) { 406 ierr = TaoSetType(tao,default_type);CHKERRQ(ierr); 407 } 408 409 ierr = PetscOptionsReal("-tao_catol","Stop if constraints violations within","TaoSetConstraintTolerances",tao->catol,&tao->catol,&flg);CHKERRQ(ierr); 410 if (flg) tao->catol_changed=PETSC_TRUE; 411 ierr = PetscOptionsReal("-tao_crtol","Stop if relative contraint violations within","TaoSetConstraintTolerances",tao->crtol,&tao->crtol,&flg);CHKERRQ(ierr); 412 if (flg) tao->crtol_changed=PETSC_TRUE; 413 ierr = PetscOptionsReal("-tao_gatol","Stop if norm of gradient less than","TaoSetTolerances",tao->gatol,&tao->gatol,&flg);CHKERRQ(ierr); 414 if (flg) tao->gatol_changed=PETSC_TRUE; 415 ierr = PetscOptionsReal("-tao_grtol","Stop if norm of gradient divided by the function value is less than","TaoSetTolerances",tao->grtol,&tao->grtol,&flg);CHKERRQ(ierr); 416 if (flg) tao->grtol_changed=PETSC_TRUE; 417 ierr = 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);CHKERRQ(ierr); 418 if (flg) tao->gttol_changed=PETSC_TRUE; 419 ierr = PetscOptionsInt("-tao_max_it","Stop if iteration number exceeds","TaoSetMaximumIterations",tao->max_it,&tao->max_it,&flg);CHKERRQ(ierr); 420 if (flg) tao->max_it_changed=PETSC_TRUE; 421 ierr = PetscOptionsInt("-tao_max_funcs","Stop if number of function evaluations exceeds","TaoSetMaximumFunctionEvaluations",tao->max_funcs,&tao->max_funcs,&flg);CHKERRQ(ierr); 422 if (flg) tao->max_funcs_changed=PETSC_TRUE; 423 ierr = PetscOptionsReal("-tao_fmin","Stop if function less than","TaoSetFunctionLowerBound",tao->fmin,&tao->fmin,&flg);CHKERRQ(ierr); 424 if (flg) tao->fmin_changed=PETSC_TRUE; 425 ierr = PetscOptionsReal("-tao_steptol","Stop if step size or trust region radius less than","",tao->steptol,&tao->steptol,&flg);CHKERRQ(ierr); 426 if (flg) tao->steptol_changed=PETSC_TRUE; 427 ierr = PetscOptionsReal("-tao_trust0","Initial trust region radius","TaoSetTrustRegionRadius",tao->trust0,&tao->trust0,&flg);CHKERRQ(ierr); 428 if (flg) tao->trust0_changed=PETSC_TRUE; 429 ierr = PetscOptionsString("-tao_view_solution","view solution vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 430 if (flg) { 431 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 432 ierr = TaoSetMonitor(tao,TaoSolutionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 433 } 434 435 ierr = PetscOptionsBool("-tao_converged_reason","Print reason for TAO converged","TaoSolve",tao->printreason,&tao->printreason,NULL);CHKERRQ(ierr); 436 ierr = PetscOptionsString("-tao_view_gradient","view gradient vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 437 if (flg) { 438 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 439 ierr = TaoSetMonitor(tao,TaoGradientMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 440 } 441 442 ierr = PetscOptionsString("-tao_view_stepdirection","view step direction vector after each iteration","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 443 if (flg) { 444 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 445 ierr = TaoSetMonitor(tao,TaoStepDirectionMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 446 } 447 448 ierr = PetscOptionsString("-tao_view_separableobjective","view separable objective vector after each evaluation","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 449 if (flg) { 450 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 451 ierr = TaoSetMonitor(tao,TaoSeparableObjectiveMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 452 } 453 454 ierr = PetscOptionsString("-tao_monitor","Use the default convergence monitor","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 455 if (flg) { 456 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 457 ierr = TaoSetMonitor(tao,TaoDefaultMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 458 } 459 460 ierr = PetscOptionsString("-tao_smonitor","Use the short convergence monitor","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 461 if (flg) { 462 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 463 ierr = TaoSetMonitor(tao,TaoDefaultSMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 464 } 465 466 ierr = PetscOptionsString("-tao_cmonitor","Use the default convergence monitor with constraint norm","TaoSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 467 if (flg) { 468 ierr = PetscViewerASCIIOpen(comm,monfilename,&monviewer);CHKERRQ(ierr); 469 ierr = TaoSetMonitor(tao,TaoDefaultCMonitor,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 470 } 471 472 473 flg = PETSC_FALSE; 474 ierr = PetscOptionsBool("-tao_cancelmonitors","cancel all monitors and call any registered destroy routines","TaoCancelMonitors",flg,&flg,NULL);CHKERRQ(ierr); 475 if (flg) {ierr = TaoCancelMonitors(tao);CHKERRQ(ierr);} 476 477 flg = PETSC_FALSE; 478 ierr = PetscOptionsBool("-tao_draw_solution","Plot solution vector at each iteration","TaoSetMonitor",flg,&flg,NULL);CHKERRQ(ierr); 479 if (flg) { 480 TaoMonitorDrawCtx drawctx; 481 PetscInt howoften = 1; 482 ierr = TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&drawctx);CHKERRQ(ierr); 483 ierr = TaoSetMonitor(tao,TaoDrawSolutionMonitor,drawctx,(PetscErrorCode (*)(void**))TaoMonitorDrawCtxDestroy);CHKERRQ(ierr); 484 } 485 486 flg = PETSC_FALSE; 487 ierr = PetscOptionsBool("-tao_draw_step","plots step direction at each iteration","TaoSetMonitor",flg,&flg,NULL);CHKERRQ(ierr); 488 if (flg) { 489 ierr = TaoSetMonitor(tao,TaoDrawStepMonitor,NULL,NULL);CHKERRQ(ierr); 490 } 491 492 flg = PETSC_FALSE; 493 ierr = PetscOptionsBool("-tao_draw_gradient","plots gradient at each iteration","TaoSetMonitor",flg,&flg,NULL);CHKERRQ(ierr); 494 if (flg) { 495 TaoMonitorDrawCtx drawctx; 496 PetscInt howoften = 1; 497 ierr = TaoMonitorDrawCtxCreate(PetscObjectComm((PetscObject)tao),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&drawctx);CHKERRQ(ierr); 498 ierr = TaoSetMonitor(tao,TaoDrawGradientMonitor,drawctx,(PetscErrorCode (*)(void**))TaoMonitorDrawCtxDestroy);CHKERRQ(ierr); 499 } 500 flg = PETSC_FALSE; 501 ierr = PetscOptionsBool("-tao_fd_gradient","compute gradient using finite differences","TaoDefaultComputeGradient",flg,&flg,NULL);CHKERRQ(ierr); 502 if (flg) { 503 ierr = TaoSetGradientRoutine(tao,TaoDefaultComputeGradient,NULL);CHKERRQ(ierr); 504 } 505 ierr = PetscOptionsEnum("-tao_subset_type","subset type", "", TaoSubSetTypes,(PetscEnum)tao->subset_type, (PetscEnum*)&tao->subset_type, 0);CHKERRQ(ierr); 506 507 if (tao->ops->setfromoptions) { 508 ierr = (*tao->ops->setfromoptions)(PetscOptionsObject,tao);CHKERRQ(ierr); 509 } 510 } 511 ierr = PetscOptionsEnd();CHKERRQ(ierr); 512 PetscFunctionReturn(0); 513 } 514 515 /*@C 516 TaoView - Prints information about the Tao 517 518 Collective on Tao 519 520 InputParameters: 521 + tao - the Tao context 522 - viewer - visualization context 523 524 Options Database Key: 525 . -tao_view - Calls TaoView() at the end of TaoSolve() 526 527 Notes: 528 The available visualization contexts include 529 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 530 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 531 output where only the first processor opens 532 the file. All other processors send their 533 data to the first processor to print. 534 535 Level: beginner 536 537 .seealso: PetscViewerASCIIOpen() 538 @*/ 539 PetscErrorCode TaoView(Tao tao, PetscViewer viewer) 540 { 541 PetscErrorCode ierr; 542 PetscBool isascii,isstring; 543 const TaoType type; 544 545 PetscFunctionBegin; 546 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 547 if (!viewer) { 548 ierr = PetscViewerASCIIGetStdout(((PetscObject)tao)->comm,&viewer);CHKERRQ(ierr); 549 } 550 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 551 PetscCheckSameComm(tao,1,viewer,2); 552 553 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); 554 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 555 if (isascii) { 556 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)tao,viewer);CHKERRQ(ierr); 557 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 558 559 if (tao->ops->view) { 560 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 561 ierr = (*tao->ops->view)(tao,viewer);CHKERRQ(ierr); 562 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 563 } 564 if (tao->linesearch) { 565 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)(tao->linesearch),viewer);CHKERRQ(ierr); 566 } 567 if (tao->ksp) { 568 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)(tao->ksp),viewer);CHKERRQ(ierr); 569 ierr = PetscViewerASCIIPrintf(viewer,"total KSP iterations: %D\n",tao->ksp_tot_its);CHKERRQ(ierr); 570 } 571 if (tao->XL || tao->XU) { 572 ierr = PetscViewerASCIIPrintf(viewer,"Active Set subset type: %s\n",TaoSubSetTypes[tao->subset_type]);CHKERRQ(ierr); 573 } 574 575 ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: gatol=%g,",(double)tao->gatol);CHKERRQ(ierr); 576 ierr=PetscViewerASCIIPrintf(viewer," steptol=%g,",(double)tao->steptol);CHKERRQ(ierr); 577 ierr=PetscViewerASCIIPrintf(viewer," gttol=%g\n",(double)tao->gttol);CHKERRQ(ierr); 578 579 ierr = PetscViewerASCIIPrintf(viewer,"Residual in Function/Gradient:=%g\n",(double)tao->residual);CHKERRQ(ierr); 580 581 if (tao->cnorm>0 || tao->catol>0 || tao->crtol>0){ 582 ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances:");CHKERRQ(ierr); 583 ierr=PetscViewerASCIIPrintf(viewer," catol=%g,",(double)tao->catol);CHKERRQ(ierr); 584 ierr=PetscViewerASCIIPrintf(viewer," crtol=%g\n",(double)tao->crtol);CHKERRQ(ierr); 585 ierr = PetscViewerASCIIPrintf(viewer,"Residual in Constraints:=%g\n",(double)tao->cnorm);CHKERRQ(ierr); 586 } 587 588 if (tao->trust < tao->steptol){ 589 ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: steptol=%g\n",(double)tao->steptol);CHKERRQ(ierr); 590 ierr=PetscViewerASCIIPrintf(viewer,"Final trust region radius:=%g\n",(double)tao->trust);CHKERRQ(ierr); 591 } 592 593 if (tao->fmin>-1.e25){ 594 ierr=PetscViewerASCIIPrintf(viewer,"convergence tolerances: function minimum=%g\n",(double)tao->fmin);CHKERRQ(ierr); 595 } 596 ierr = PetscViewerASCIIPrintf(viewer,"Objective value=%g\n",(double)tao->fc);CHKERRQ(ierr); 597 598 ierr = PetscViewerASCIIPrintf(viewer,"total number of iterations=%D, ",tao->niter);CHKERRQ(ierr); 599 ierr = PetscViewerASCIIPrintf(viewer," (max: %D)\n",tao->max_it);CHKERRQ(ierr); 600 601 if (tao->nfuncs>0){ 602 ierr = PetscViewerASCIIPrintf(viewer,"total number of function evaluations=%D,",tao->nfuncs);CHKERRQ(ierr); 603 ierr = PetscViewerASCIIPrintf(viewer," max: %D\n",tao->max_funcs);CHKERRQ(ierr); 604 } 605 if (tao->ngrads>0){ 606 ierr = PetscViewerASCIIPrintf(viewer,"total number of gradient evaluations=%D,",tao->ngrads);CHKERRQ(ierr); 607 ierr = PetscViewerASCIIPrintf(viewer," max: %D\n",tao->max_funcs);CHKERRQ(ierr); 608 } 609 if (tao->nfuncgrads>0){ 610 ierr = PetscViewerASCIIPrintf(viewer,"total number of function/gradient evaluations=%D,",tao->nfuncgrads);CHKERRQ(ierr); 611 ierr = PetscViewerASCIIPrintf(viewer," (max: %D)\n",tao->max_funcs);CHKERRQ(ierr); 612 } 613 if (tao->nhess>0){ 614 ierr = PetscViewerASCIIPrintf(viewer,"total number of Hessian evaluations=%D\n",tao->nhess);CHKERRQ(ierr); 615 } 616 /* if (tao->linear_its>0){ 617 ierr = PetscViewerASCIIPrintf(viewer," total Krylov method iterations=%D\n",tao->linear_its);CHKERRQ(ierr); 618 }*/ 619 if (tao->nconstraints>0){ 620 ierr = PetscViewerASCIIPrintf(viewer,"total number of constraint function evaluations=%D\n",tao->nconstraints);CHKERRQ(ierr); 621 } 622 if (tao->njac>0){ 623 ierr = PetscViewerASCIIPrintf(viewer,"total number of Jacobian evaluations=%D\n",tao->njac);CHKERRQ(ierr); 624 } 625 626 if (tao->reason>0){ 627 ierr = PetscViewerASCIIPrintf(viewer, "Solution converged: ");CHKERRQ(ierr); 628 switch (tao->reason) { 629 case TAO_CONVERGED_GATOL: 630 ierr = PetscViewerASCIIPrintf(viewer," ||g(X)|| <= gatol\n");CHKERRQ(ierr); 631 break; 632 case TAO_CONVERGED_GRTOL: 633 ierr = PetscViewerASCIIPrintf(viewer," ||g(X)||/|f(X)| <= grtol\n");CHKERRQ(ierr); 634 break; 635 case TAO_CONVERGED_GTTOL: 636 ierr = PetscViewerASCIIPrintf(viewer," ||g(X)||/||g(X0)|| <= gttol\n");CHKERRQ(ierr); 637 break; 638 case TAO_CONVERGED_STEPTOL: 639 ierr = PetscViewerASCIIPrintf(viewer," Steptol -- step size small\n");CHKERRQ(ierr); 640 break; 641 case TAO_CONVERGED_MINF: 642 ierr = PetscViewerASCIIPrintf(viewer," Minf -- f < fmin\n");CHKERRQ(ierr); 643 break; 644 case TAO_CONVERGED_USER: 645 ierr = PetscViewerASCIIPrintf(viewer," User Terminated\n");CHKERRQ(ierr); 646 break; 647 default: 648 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 649 break; 650 } 651 652 } else { 653 ierr = PetscViewerASCIIPrintf(viewer,"Solver terminated: %d",tao->reason);CHKERRQ(ierr); 654 switch (tao->reason) { 655 case TAO_DIVERGED_MAXITS: 656 ierr = PetscViewerASCIIPrintf(viewer," Maximum Iterations\n");CHKERRQ(ierr); 657 break; 658 case TAO_DIVERGED_NAN: 659 ierr = PetscViewerASCIIPrintf(viewer," NAN or Inf encountered\n");CHKERRQ(ierr); 660 break; 661 case TAO_DIVERGED_MAXFCN: 662 ierr = PetscViewerASCIIPrintf(viewer," Maximum Function Evaluations\n");CHKERRQ(ierr); 663 break; 664 case TAO_DIVERGED_LS_FAILURE: 665 ierr = PetscViewerASCIIPrintf(viewer," Line Search Failure\n");CHKERRQ(ierr); 666 break; 667 case TAO_DIVERGED_TR_REDUCTION: 668 ierr = PetscViewerASCIIPrintf(viewer," Trust Region too small\n");CHKERRQ(ierr); 669 break; 670 case TAO_DIVERGED_USER: 671 ierr = PetscViewerASCIIPrintf(viewer," User Terminated\n");CHKERRQ(ierr); 672 break; 673 default: 674 ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 675 break; 676 } 677 } 678 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 679 } else if (isstring) { 680 ierr = TaoGetType(tao,&type);CHKERRQ(ierr); 681 ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 682 } 683 PetscFunctionReturn(0); 684 } 685 686 /*@ 687 TaoSetTolerances - Sets parameters used in TAO convergence tests 688 689 Logically collective on Tao 690 691 Input Parameters: 692 + tao - the Tao context 693 . gatol - stop if norm of gradient is less than this 694 . grtol - stop if relative norm of gradient is less than this 695 - gttol - stop if norm of gradient is reduced by this factor 696 697 Options Database Keys: 698 + -tao_gatol <gatol> - Sets gatol 699 . -tao_grtol <grtol> - Sets grtol 700 - -tao_gttol <gttol> - Sets gttol 701 702 Stopping Criteria: 703 $ ||g(X)|| <= gatol 704 $ ||g(X)|| / |f(X)| <= grtol 705 $ ||g(X)|| / ||g(X0)|| <= gttol 706 707 Notes: 708 Use PETSC_DEFAULT to leave one or more tolerances unchanged. 709 710 Level: beginner 711 712 .seealso: TaoGetTolerances() 713 714 @*/ 715 PetscErrorCode TaoSetTolerances(Tao tao, PetscReal gatol, PetscReal grtol, PetscReal gttol) 716 { 717 PetscErrorCode ierr; 718 719 PetscFunctionBegin; 720 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 721 722 if (gatol != PETSC_DEFAULT) { 723 if (gatol<0) { 724 ierr = PetscInfo(tao,"Tried to set negative gatol -- ignored.\n");CHKERRQ(ierr); 725 } else { 726 tao->gatol = PetscMax(0,gatol); 727 tao->gatol_changed=PETSC_TRUE; 728 } 729 } 730 731 if (grtol != PETSC_DEFAULT) { 732 if (grtol<0) { 733 ierr = PetscInfo(tao,"Tried to set negative grtol -- ignored.\n");CHKERRQ(ierr); 734 } else { 735 tao->grtol = PetscMax(0,grtol); 736 tao->grtol_changed=PETSC_TRUE; 737 } 738 } 739 740 if (gttol != PETSC_DEFAULT) { 741 if (gttol<0) { 742 ierr = PetscInfo(tao,"Tried to set negative gttol -- ignored.\n");CHKERRQ(ierr); 743 } else { 744 tao->gttol = PetscMax(0,gttol); 745 tao->gttol_changed=PETSC_TRUE; 746 } 747 } 748 PetscFunctionReturn(0); 749 } 750 751 /*@ 752 TaoSetConstraintTolerances - Sets constraint tolerance parameters used in TAO convergence tests 753 754 Logically collective on Tao 755 756 Input Parameters: 757 + tao - the Tao context 758 . catol - absolute constraint tolerance, constraint norm must be less than catol for used for gatol convergence criteria 759 - crtol - relative contraint tolerance, constraint norm must be less than crtol for used for gatol, gttol convergence criteria 760 761 Options Database Keys: 762 + -tao_catol <catol> - Sets catol 763 - -tao_crtol <crtol> - Sets crtol 764 765 Notes: 766 Use PETSC_DEFAULT to leave any tolerance unchanged. 767 768 Level: intermediate 769 770 .seealso: TaoGetTolerances(), TaoGetConstraintTolerances(), TaoSetTolerances() 771 772 @*/ 773 PetscErrorCode TaoSetConstraintTolerances(Tao tao, PetscReal catol, PetscReal crtol) 774 { 775 PetscErrorCode ierr; 776 777 PetscFunctionBegin; 778 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 779 780 if (catol != PETSC_DEFAULT) { 781 if (catol<0) { 782 ierr = PetscInfo(tao,"Tried to set negative catol -- ignored.\n");CHKERRQ(ierr); 783 } else { 784 tao->catol = PetscMax(0,catol); 785 tao->catol_changed=PETSC_TRUE; 786 } 787 } 788 789 if (crtol != PETSC_DEFAULT) { 790 if (crtol<0) { 791 ierr = PetscInfo(tao,"Tried to set negative crtol -- ignored.\n");CHKERRQ(ierr); 792 } else { 793 tao->crtol = PetscMax(0,crtol); 794 tao->crtol_changed=PETSC_TRUE; 795 } 796 } 797 PetscFunctionReturn(0); 798 } 799 800 /*@ 801 TaoGetConstraintTolerances - Gets constraint tolerance parameters used in TAO convergence tests 802 803 Not ollective 804 805 Input Parameter: 806 . tao - the Tao context 807 808 Output Parameter: 809 + catol - absolute constraint tolerance, constraint norm must be less than catol for used for gatol convergence criteria 810 - crtol - relative contraint tolerance, constraint norm must be less than crtol for used for gatol, gttol convergence criteria 811 812 Level: intermediate 813 814 .seealso: TaoGetTolerances(), TaoSetTolerances(), TaoSetConstraintTolerances() 815 816 @*/ 817 PetscErrorCode TaoGetConstraintTolerances(Tao tao, PetscReal *catol, PetscReal *crtol) 818 { 819 PetscFunctionBegin; 820 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 821 if (catol) *catol = tao->catol; 822 if (crtol) *crtol = tao->crtol; 823 PetscFunctionReturn(0); 824 } 825 826 /*@ 827 TaoSetFunctionLowerBound - Sets a bound on the solution objective value. 828 When an approximate solution with an objective value below this number 829 has been found, the solver will terminate. 830 831 Logically Collective on Tao 832 833 Input Parameters: 834 + tao - the Tao solver context 835 - fmin - the tolerance 836 837 Options Database Keys: 838 . -tao_fmin <fmin> - sets the minimum function value 839 840 Level: intermediate 841 842 .seealso: TaoSetTolerances() 843 @*/ 844 PetscErrorCode TaoSetFunctionLowerBound(Tao tao,PetscReal fmin) 845 { 846 PetscFunctionBegin; 847 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 848 tao->fmin = fmin; 849 tao->fmin_changed=PETSC_TRUE; 850 PetscFunctionReturn(0); 851 } 852 853 /*@ 854 TaoGetFunctionLowerBound - Gets the bound on the solution objective value. 855 When an approximate solution with an objective value below this number 856 has been found, the solver will terminate. 857 858 Not collective on Tao 859 860 Input Parameters: 861 . tao - the Tao solver context 862 863 OutputParameters: 864 . fmin - the minimum function value 865 866 Level: intermediate 867 868 .seealso: TaoSetFunctionLowerBound() 869 @*/ 870 PetscErrorCode TaoGetFunctionLowerBound(Tao tao,PetscReal *fmin) 871 { 872 PetscFunctionBegin; 873 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 874 *fmin = tao->fmin; 875 PetscFunctionReturn(0); 876 } 877 878 /*@ 879 TaoSetMaximumFunctionEvaluations - Sets a maximum number of 880 function evaluations. 881 882 Logically Collective on Tao 883 884 Input Parameters: 885 + tao - the Tao solver context 886 - nfcn - the maximum number of function evaluations (>=0) 887 888 Options Database Keys: 889 . -tao_max_funcs <nfcn> - sets the maximum number of function evaluations 890 891 Level: intermediate 892 893 .seealso: TaoSetTolerances(), TaoSetMaximumIterations() 894 @*/ 895 896 PetscErrorCode TaoSetMaximumFunctionEvaluations(Tao tao,PetscInt nfcn) 897 { 898 PetscFunctionBegin; 899 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 900 tao->max_funcs = PetscMax(0,nfcn); 901 tao->max_funcs_changed=PETSC_TRUE; 902 PetscFunctionReturn(0); 903 } 904 905 /*@ 906 TaoGetMaximumFunctionEvaluations - Sets a maximum number of 907 function evaluations. 908 909 Not Collective 910 911 Input Parameters: 912 . tao - the Tao solver context 913 914 Output Parameters: 915 . nfcn - the maximum number of function evaluations 916 917 Level: intermediate 918 919 .seealso: TaoSetMaximumFunctionEvaluations(), TaoGetMaximumIterations() 920 @*/ 921 922 PetscErrorCode TaoGetMaximumFunctionEvaluations(Tao tao,PetscInt *nfcn) 923 { 924 PetscFunctionBegin; 925 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 926 *nfcn = tao->max_funcs; 927 PetscFunctionReturn(0); 928 } 929 930 /*@ 931 TaoGetCurrentFunctionEvaluations - Get current number of 932 function evaluations. 933 934 Not Collective 935 936 Input Parameters: 937 . tao - the Tao solver context 938 939 Output Parameters: 940 . nfuncs - the current number of function evaluations 941 942 Level: intermediate 943 944 .seealso: TaoSetMaximumFunctionEvaluations(), TaoGetMaximumFunctionEvaluations(), TaoGetMaximumIterations() 945 @*/ 946 947 PetscErrorCode TaoGetCurrentFunctionEvaluations(Tao tao,PetscInt *nfuncs) 948 { 949 PetscFunctionBegin; 950 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 951 *nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads); 952 PetscFunctionReturn(0); 953 } 954 955 /*@ 956 TaoSetMaximumIterations - Sets a maximum number of iterates. 957 958 Logically Collective on Tao 959 960 Input Parameters: 961 + tao - the Tao solver context 962 - maxits - the maximum number of iterates (>=0) 963 964 Options Database Keys: 965 . -tao_max_it <its> - sets the maximum number of iterations 966 967 Level: intermediate 968 969 .seealso: TaoSetTolerances(), TaoSetMaximumFunctionEvaluations() 970 @*/ 971 PetscErrorCode TaoSetMaximumIterations(Tao tao,PetscInt maxits) 972 { 973 PetscFunctionBegin; 974 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 975 tao->max_it = PetscMax(0,maxits); 976 tao->max_it_changed=PETSC_TRUE; 977 PetscFunctionReturn(0); 978 } 979 980 /*@ 981 TaoGetMaximumIterations - Sets a maximum number of iterates. 982 983 Not Collective 984 985 Input Parameters: 986 . tao - the Tao solver context 987 988 Output Parameters: 989 . maxits - the maximum number of iterates 990 991 Level: intermediate 992 993 .seealso: TaoSetMaximumIterations(), TaoGetMaximumFunctionEvaluations() 994 @*/ 995 PetscErrorCode TaoGetMaximumIterations(Tao tao,PetscInt *maxits) 996 { 997 PetscFunctionBegin; 998 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 999 *maxits = tao->max_it; 1000 PetscFunctionReturn(0); 1001 } 1002 1003 /*@ 1004 TaoSetInitialTrustRegionRadius - Sets the initial trust region radius. 1005 1006 Logically collective on Tao 1007 1008 Input Parameter: 1009 + tao - a TAO optimization solver 1010 - radius - the trust region radius 1011 1012 Level: intermediate 1013 1014 Options Database Key: 1015 . -tao_trust0 <t0> - sets initial trust region radius 1016 1017 .seealso: TaoGetTrustRegionRadius(), TaoSetTrustRegionTolerance() 1018 @*/ 1019 PetscErrorCode TaoSetInitialTrustRegionRadius(Tao tao, PetscReal radius) 1020 { 1021 PetscFunctionBegin; 1022 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1023 tao->trust0 = PetscMax(0.0,radius); 1024 tao->trust0_changed=PETSC_TRUE; 1025 PetscFunctionReturn(0); 1026 } 1027 1028 /*@ 1029 TaoGetInitialTrustRegionRadius - Sets the initial trust region radius. 1030 1031 Not Collective 1032 1033 Input Parameter: 1034 . tao - a TAO optimization solver 1035 1036 Output Parameter: 1037 . radius - the trust region radius 1038 1039 Level: intermediate 1040 1041 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetCurrentTrustRegionRadius() 1042 @*/ 1043 PetscErrorCode TaoGetInitialTrustRegionRadius(Tao tao, PetscReal *radius) 1044 { 1045 PetscFunctionBegin; 1046 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1047 *radius = tao->trust0; 1048 PetscFunctionReturn(0); 1049 } 1050 1051 /*@ 1052 TaoGetCurrentTrustRegionRadius - Gets the current trust region radius. 1053 1054 Not Collective 1055 1056 Input Parameter: 1057 . tao - a TAO optimization solver 1058 1059 Output Parameter: 1060 . radius - the trust region radius 1061 1062 Level: intermediate 1063 1064 .seealso: TaoSetInitialTrustRegionRadius(), TaoGetInitialTrustRegionRadius() 1065 @*/ 1066 PetscErrorCode TaoGetCurrentTrustRegionRadius(Tao tao, PetscReal *radius) 1067 { 1068 PetscFunctionBegin; 1069 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1070 *radius = tao->trust; 1071 PetscFunctionReturn(0); 1072 } 1073 1074 /*@ 1075 TaoGetTolerances - gets the current values of tolerances 1076 1077 Not Collective 1078 1079 Input Parameters: 1080 . tao - the Tao context 1081 1082 Output Parameters: 1083 + gatol - stop if norm of gradient is less than this 1084 . grtol - stop if relative norm of gradient is less than this 1085 - gttol - stop if norm of gradient is reduced by a this factor 1086 1087 Note: NULL can be used as an argument if not all tolerances values are needed 1088 1089 .seealso TaoSetTolerances() 1090 1091 Level: intermediate 1092 @*/ 1093 PetscErrorCode TaoGetTolerances(Tao tao, PetscReal *gatol, PetscReal *grtol, PetscReal *gttol) 1094 { 1095 PetscFunctionBegin; 1096 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1097 if (gatol) *gatol=tao->gatol; 1098 if (grtol) *grtol=tao->grtol; 1099 if (gttol) *gttol=tao->gttol; 1100 PetscFunctionReturn(0); 1101 } 1102 1103 /*@ 1104 TaoGetKSP - Gets the linear solver used by the optimization solver. 1105 Application writers should use TaoGetKSP if they need direct access 1106 to the PETSc KSP object. 1107 1108 Not Collective 1109 1110 Input Parameters: 1111 . tao - the TAO solver 1112 1113 Output Parameters: 1114 . ksp - the KSP linear solver used in the optimization solver 1115 1116 Level: intermediate 1117 1118 @*/ 1119 PetscErrorCode TaoGetKSP(Tao tao, KSP *ksp) 1120 { 1121 PetscFunctionBegin; 1122 *ksp = tao->ksp; 1123 PetscFunctionReturn(0); 1124 } 1125 1126 /*@ 1127 TaoGetLinearSolveIterations - Gets the total number of linear iterations 1128 used by the TAO solver 1129 1130 Not Collective 1131 1132 Input Parameter: 1133 . tao - TAO context 1134 1135 Output Parameter: 1136 . lits - number of linear iterations 1137 1138 Notes: 1139 This counter is reset to zero for each successive call to TaoSolve() 1140 1141 Level: intermediate 1142 1143 .keywords: TAO 1144 1145 .seealso: TaoGetKSP() 1146 @*/ 1147 PetscErrorCode TaoGetLinearSolveIterations(Tao tao,PetscInt *lits) 1148 { 1149 PetscFunctionBegin; 1150 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1151 PetscValidIntPointer(lits,2); 1152 *lits = tao->ksp_tot_its; 1153 PetscFunctionReturn(0); 1154 } 1155 1156 /*@ 1157 TaoGetLineSearch - Gets the line search used by the optimization solver. 1158 Application writers should use TaoGetLineSearch if they need direct access 1159 to the TaoLineSearch object. 1160 1161 Not Collective 1162 1163 Input Parameters: 1164 . tao - the TAO solver 1165 1166 Output Parameters: 1167 . ls - the line search used in the optimization solver 1168 1169 Level: intermediate 1170 1171 @*/ 1172 PetscErrorCode TaoGetLineSearch(Tao tao, TaoLineSearch *ls) 1173 { 1174 PetscFunctionBegin; 1175 *ls = tao->linesearch; 1176 PetscFunctionReturn(0); 1177 } 1178 1179 /*@ 1180 TaoAddLineSearchCounts - Adds the number of function evaluations spent 1181 in the line search to the running total. 1182 1183 Input Parameters: 1184 + tao - the TAO solver 1185 - ls - the line search used in the optimization solver 1186 1187 Level: developer 1188 1189 .seealso: TaoLineSearchApply() 1190 @*/ 1191 PetscErrorCode TaoAddLineSearchCounts(Tao tao) 1192 { 1193 PetscErrorCode ierr; 1194 PetscBool flg; 1195 PetscInt nfeval,ngeval,nfgeval; 1196 1197 PetscFunctionBegin; 1198 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1199 if (tao->linesearch) { 1200 ierr = TaoLineSearchIsUsingTaoRoutines(tao->linesearch,&flg);CHKERRQ(ierr); 1201 if (!flg) { 1202 ierr = TaoLineSearchGetNumberFunctionEvaluations(tao->linesearch,&nfeval,&ngeval,&nfgeval);CHKERRQ(ierr); 1203 tao->nfuncs+=nfeval; 1204 tao->ngrads+=ngeval; 1205 tao->nfuncgrads+=nfgeval; 1206 } 1207 } 1208 PetscFunctionReturn(0); 1209 } 1210 1211 /*@ 1212 TaoGetSolutionVector - Returns the vector with the current TAO solution 1213 1214 Not Collective 1215 1216 Input Parameter: 1217 . tao - the Tao context 1218 1219 Output Parameter: 1220 . X - the current solution 1221 1222 Level: intermediate 1223 1224 Note: The returned vector will be the same object that was passed into TaoSetInitialVector() 1225 @*/ 1226 PetscErrorCode TaoGetSolutionVector(Tao tao, Vec *X) 1227 { 1228 PetscFunctionBegin; 1229 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1230 *X = tao->solution; 1231 PetscFunctionReturn(0); 1232 } 1233 1234 /*@ 1235 TaoGetGradientVector - Returns the vector with the current TAO gradient 1236 1237 Not Collective 1238 1239 Input Parameter: 1240 . tao - the Tao context 1241 1242 Output Parameter: 1243 . G - the current solution 1244 1245 Level: intermediate 1246 @*/ 1247 PetscErrorCode TaoGetGradientVector(Tao tao, Vec *G) 1248 { 1249 PetscFunctionBegin; 1250 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1251 *G = tao->gradient; 1252 PetscFunctionReturn(0); 1253 } 1254 1255 /*@ 1256 TaoResetStatistics - Initialize the statistics used by TAO for all of the solvers. 1257 These statistics include the iteration number, residual norms, and convergence status. 1258 This routine gets called before solving each optimization problem. 1259 1260 Collective on Tao 1261 1262 Input Parameters: 1263 . solver - the Tao context 1264 1265 Level: developer 1266 1267 .seealso: TaoCreate(), TaoSolve() 1268 @*/ 1269 PetscErrorCode TaoResetStatistics(Tao tao) 1270 { 1271 PetscFunctionBegin; 1272 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1273 tao->niter = 0; 1274 tao->nfuncs = 0; 1275 tao->nfuncgrads = 0; 1276 tao->ngrads = 0; 1277 tao->nhess = 0; 1278 tao->njac = 0; 1279 tao->nconstraints = 0; 1280 tao->ksp_its = 0; 1281 tao->ksp_tot_its = 0; 1282 tao->reason = TAO_CONTINUE_ITERATING; 1283 tao->residual = 0.0; 1284 tao->cnorm = 0.0; 1285 tao->step = 0.0; 1286 tao->lsflag = PETSC_FALSE; 1287 if (tao->hist_reset) tao->hist_len=0; 1288 PetscFunctionReturn(0); 1289 } 1290 1291 /*@C 1292 TaoSetConvergenceTest - Sets the function that is to be used to test 1293 for convergence o fthe iterative minimization solution. The new convergence 1294 testing routine will replace TAO's default convergence test. 1295 1296 Logically Collective on Tao 1297 1298 Input Parameters: 1299 + tao - the Tao object 1300 . conv - the routine to test for convergence 1301 - ctx - [optional] context for private data for the convergence routine 1302 (may be NULL) 1303 1304 Calling sequence of conv: 1305 $ PetscErrorCode conv(Tao tao, void *ctx) 1306 1307 + tao - the Tao object 1308 - ctx - [optional] convergence context 1309 1310 Note: The new convergence testing routine should call TaoSetConvergedReason(). 1311 1312 Level: advanced 1313 1314 .seealso: TaoSetConvergedReason(), TaoGetSolutionStatus(), TaoGetTolerances(), TaoSetMonitor 1315 1316 @*/ 1317 PetscErrorCode TaoSetConvergenceTest(Tao tao, PetscErrorCode (*conv)(Tao,void*), void *ctx) 1318 { 1319 PetscFunctionBegin; 1320 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1321 (tao)->ops->convergencetest = conv; 1322 (tao)->cnvP = ctx; 1323 PetscFunctionReturn(0); 1324 } 1325 1326 /*@C 1327 TaoSetMonitor - Sets an ADDITIONAL function that is to be used at every 1328 iteration of the solver to display the iteration's 1329 progress. 1330 1331 Logically Collective on Tao 1332 1333 Input Parameters: 1334 + tao - the Tao solver context 1335 . mymonitor - monitoring routine 1336 - mctx - [optional] user-defined context for private data for the 1337 monitor routine (may be NULL) 1338 1339 Calling sequence of mymonitor: 1340 $ int mymonitor(Tao tao,void *mctx) 1341 1342 + tao - the Tao solver context 1343 - mctx - [optional] monitoring context 1344 1345 1346 Options Database Keys: 1347 + -tao_monitor - sets TaoDefaultMonitor() 1348 . -tao_smonitor - sets short monitor 1349 . -tao_cmonitor - same as smonitor plus constraint norm 1350 . -tao_view_solution - view solution at each iteration 1351 . -tao_view_gradient - view gradient at each iteration 1352 . -tao_view_separableobjective - view separable objective function at each iteration 1353 - -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. 1354 1355 1356 Notes: 1357 Several different monitoring routines may be set by calling 1358 TaoSetMonitor() multiple times; all will be called in the 1359 order in which they were set. 1360 1361 Fortran Notes: Only one monitor function may be set 1362 1363 Level: intermediate 1364 1365 .seealso: TaoDefaultMonitor(), TaoCancelMonitors(), TaoSetDestroyRoutine() 1366 @*/ 1367 PetscErrorCode TaoSetMonitor(Tao tao, PetscErrorCode (*func)(Tao, void*), void *ctx,PetscErrorCode (*dest)(void**)) 1368 { 1369 PetscErrorCode ierr; 1370 PetscInt i; 1371 1372 PetscFunctionBegin; 1373 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1374 if (tao->numbermonitors >= MAXTAOMONITORS) SETERRQ1(PETSC_COMM_SELF,1,"Cannot attach another monitor -- max=",MAXTAOMONITORS); 1375 1376 for (i=0; i<tao->numbermonitors;i++) { 1377 if (func == tao->monitor[i] && dest == tao->monitordestroy[i] && ctx == tao->monitorcontext[i]) { 1378 if (dest) { 1379 ierr = (*dest)(&ctx);CHKERRQ(ierr); 1380 } 1381 PetscFunctionReturn(0); 1382 } 1383 } 1384 tao->monitor[tao->numbermonitors] = func; 1385 tao->monitorcontext[tao->numbermonitors] = ctx; 1386 tao->monitordestroy[tao->numbermonitors] = dest; 1387 ++tao->numbermonitors; 1388 PetscFunctionReturn(0); 1389 } 1390 1391 /*@ 1392 TaoCancelMonitors - Clears all the monitor functions for a Tao object. 1393 1394 Logically Collective on Tao 1395 1396 Input Parameters: 1397 . tao - the Tao solver context 1398 1399 Options Database: 1400 . -tao_cancelmonitors - cancels all monitors that have been hardwired 1401 into a code by calls to TaoSetMonitor(), but does not cancel those 1402 set via the options database 1403 1404 Notes: 1405 There is no way to clear one specific monitor from a Tao object. 1406 1407 Level: advanced 1408 1409 .seealso: TaoDefaultMonitor(), TaoSetMonitor() 1410 @*/ 1411 PetscErrorCode TaoCancelMonitors(Tao tao) 1412 { 1413 PetscInt i; 1414 PetscErrorCode ierr; 1415 1416 PetscFunctionBegin; 1417 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1418 for (i=0;i<tao->numbermonitors;i++) { 1419 if (tao->monitordestroy[i]) { 1420 ierr = (*tao->monitordestroy[i])(&tao->monitorcontext[i]);CHKERRQ(ierr); 1421 } 1422 } 1423 tao->numbermonitors=0; 1424 PetscFunctionReturn(0); 1425 } 1426 1427 /*@ 1428 TaoDefaultMonitor - Default routine for monitoring progress of the 1429 Tao solvers (default). This monitor prints the function value and gradient 1430 norm at each iteration. It can be turned on from the command line using the 1431 -tao_monitor option 1432 1433 Collective on Tao 1434 1435 Input Parameters: 1436 + tao - the Tao context 1437 - ctx - PetscViewer context or NULL 1438 1439 Options Database Keys: 1440 . -tao_monitor 1441 1442 Level: advanced 1443 1444 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1445 @*/ 1446 PetscErrorCode TaoDefaultMonitor(Tao tao, void *ctx) 1447 { 1448 PetscErrorCode ierr; 1449 PetscInt its; 1450 PetscReal fct,gnorm; 1451 PetscViewer viewer = (PetscViewer)ctx; 1452 1453 PetscFunctionBegin; 1454 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1455 its=tao->niter; 1456 fct=tao->fc; 1457 gnorm=tao->residual; 1458 ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr); 1459 ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr); 1460 if (gnorm >= PETSC_INFINITY) { 1461 ierr=PetscViewerASCIIPrintf(viewer," Residual: Inf \n");CHKERRQ(ierr); 1462 } else { 1463 ierr=PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm);CHKERRQ(ierr); 1464 } 1465 PetscFunctionReturn(0); 1466 } 1467 1468 /*@ 1469 TaoDefaultSMonitor - Default routine for monitoring progress of the 1470 solver. Same as TaoDefaultMonitor() except 1471 it prints fewer digits of the residual as the residual gets smaller. 1472 This is because the later digits are meaningless and are often 1473 different on different machines; by using this routine different 1474 machines will usually generate the same output. It can be turned on 1475 by using the -tao_smonitor option 1476 1477 Collective on Tao 1478 1479 Input Parameters: 1480 + tao - the Tao context 1481 - ctx - PetscViewer context of type ASCII 1482 1483 Options Database Keys: 1484 . -tao_smonitor 1485 1486 Level: advanced 1487 1488 .seealso: TaoDefaultMonitor(), TaoSetMonitor() 1489 @*/ 1490 PetscErrorCode TaoDefaultSMonitor(Tao tao, void *ctx) 1491 { 1492 PetscErrorCode ierr; 1493 PetscInt its; 1494 PetscReal fct,gnorm; 1495 PetscViewer viewer = (PetscViewer)ctx; 1496 1497 PetscFunctionBegin; 1498 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1499 its=tao->niter; 1500 fct=tao->fc; 1501 gnorm=tao->residual; 1502 ierr=PetscViewerASCIIPrintf(viewer,"iter = %3D,",its);CHKERRQ(ierr); 1503 ierr=PetscViewerASCIIPrintf(viewer," Function value %g,",(double)fct);CHKERRQ(ierr); 1504 if (gnorm >= PETSC_INFINITY) { 1505 ierr=PetscViewerASCIIPrintf(viewer," Residual: Inf \n");CHKERRQ(ierr); 1506 } else if (gnorm > 1.e-6) { 1507 ierr=PetscViewerASCIIPrintf(viewer," Residual: %g \n",(double)gnorm);CHKERRQ(ierr); 1508 } else if (gnorm > 1.e-11) { 1509 ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-6 \n");CHKERRQ(ierr); 1510 } else { 1511 ierr=PetscViewerASCIIPrintf(viewer," Residual: < 1.0e-11 \n");CHKERRQ(ierr); 1512 } 1513 PetscFunctionReturn(0); 1514 } 1515 1516 /*@ 1517 TaoDefaultCMonitor - same as TaoDefaultMonitor() except 1518 it prints the norm of the constraints function. It can be turned on 1519 from the command line using the -tao_cmonitor option 1520 1521 Collective on Tao 1522 1523 Input Parameters: 1524 + tao - the Tao context 1525 - ctx - PetscViewer context or NULL 1526 1527 Options Database Keys: 1528 . -tao_cmonitor 1529 1530 Level: advanced 1531 1532 .seealso: TaoDefaultMonitor(), TaoSetMonitor() 1533 @*/ 1534 PetscErrorCode TaoDefaultCMonitor(Tao tao, void *ctx) 1535 { 1536 PetscErrorCode ierr; 1537 PetscInt its; 1538 PetscReal fct,gnorm; 1539 PetscViewer viewer = (PetscViewer)ctx; 1540 1541 PetscFunctionBegin; 1542 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1543 its=tao->niter; 1544 fct=tao->fc; 1545 gnorm=tao->residual; 1546 ierr=PetscViewerASCIIPrintf(viewer,"iter = %D,",its);CHKERRQ(ierr); 1547 ierr=PetscViewerASCIIPrintf(viewer," Function value: %g,",(double)fct);CHKERRQ(ierr); 1548 ierr=PetscViewerASCIIPrintf(viewer," Residual: %g ",(double)gnorm);CHKERRQ(ierr); 1549 ierr = PetscViewerASCIIPrintf(viewer," Constraint: %g \n",(double)tao->cnorm);CHKERRQ(ierr); 1550 PetscFunctionReturn(0); 1551 } 1552 1553 /*@C 1554 TaoSolutionMonitor - Views the solution at each iteration 1555 It can be turned on from the command line using the 1556 -tao_view_solution option 1557 1558 Collective on Tao 1559 1560 Input Parameters: 1561 + tao - the Tao context 1562 - ctx - PetscViewer context or NULL 1563 1564 Options Database Keys: 1565 . -tao_view_solution 1566 1567 Level: advanced 1568 1569 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1570 @*/ 1571 PetscErrorCode TaoSolutionMonitor(Tao tao, void *ctx) 1572 { 1573 PetscErrorCode ierr; 1574 PetscViewer viewer = (PetscViewer)ctx;; 1575 1576 PetscFunctionBegin; 1577 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1578 ierr = VecView(tao->solution, viewer);CHKERRQ(ierr); 1579 PetscFunctionReturn(0); 1580 } 1581 1582 /*@C 1583 TaoGradientMonitor - Views the gradient at each iteration 1584 It can be turned on from the command line using the 1585 -tao_view_gradient option 1586 1587 Collective on Tao 1588 1589 Input Parameters: 1590 + tao - the Tao context 1591 - ctx - PetscViewer context or NULL 1592 1593 Options Database Keys: 1594 . -tao_view_gradient 1595 1596 Level: advanced 1597 1598 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1599 @*/ 1600 PetscErrorCode TaoGradientMonitor(Tao tao, void *ctx) 1601 { 1602 PetscErrorCode ierr; 1603 PetscViewer viewer = (PetscViewer)ctx; 1604 1605 PetscFunctionBegin; 1606 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1607 ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr); 1608 PetscFunctionReturn(0); 1609 } 1610 1611 /*@C 1612 TaoStepDirectionMonitor - Views the gradient at each iteration 1613 It can be turned on from the command line using the 1614 -tao_view_gradient option 1615 1616 Collective on Tao 1617 1618 Input Parameters: 1619 + tao - the Tao context 1620 - ctx - PetscViewer context or NULL 1621 1622 Options Database Keys: 1623 . -tao_view_gradient 1624 1625 Level: advanced 1626 1627 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1628 @*/ 1629 PetscErrorCode TaoStepDirectionMonitor(Tao tao, void *ctx) 1630 { 1631 PetscErrorCode ierr; 1632 PetscViewer viewer = (PetscViewer)ctx; 1633 1634 PetscFunctionBegin; 1635 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1636 ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr); 1637 PetscFunctionReturn(0); 1638 } 1639 1640 /*@C 1641 TaoDrawSolutionMonitor - Plots the solution at each iteration 1642 It can be turned on from the command line using the 1643 -tao_draw_solution option 1644 1645 Collective on Tao 1646 1647 Input Parameters: 1648 + tao - the Tao context 1649 - ctx - TaoMonitorDraw context 1650 1651 Options Database Keys: 1652 . -tao_draw_solution 1653 1654 Level: advanced 1655 1656 .seealso: TaoSolutionMonitor(), TaoSetMonitor(), TaoDrawGradientMonitor 1657 @*/ 1658 PetscErrorCode TaoDrawSolutionMonitor(Tao tao, void *ctx) 1659 { 1660 PetscErrorCode ierr; 1661 TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 1662 1663 PetscFunctionBegin; 1664 if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(0); 1665 ierr = VecView(tao->solution,ictx->viewer);CHKERRQ(ierr); 1666 PetscFunctionReturn(0); 1667 } 1668 1669 /*@C 1670 TaoDrawGradientMonitor - Plots the gradient at each iteration 1671 It can be turned on from the command line using the 1672 -tao_draw_gradient option 1673 1674 Collective on Tao 1675 1676 Input Parameters: 1677 + tao - the Tao context 1678 - ctx - PetscViewer context 1679 1680 Options Database Keys: 1681 . -tao_draw_gradient 1682 1683 Level: advanced 1684 1685 .seealso: TaoGradientMonitor(), TaoSetMonitor(), TaoDrawSolutionMonitor 1686 @*/ 1687 PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx) 1688 { 1689 PetscErrorCode ierr; 1690 TaoMonitorDrawCtx ictx = (TaoMonitorDrawCtx)ctx; 1691 1692 PetscFunctionBegin; 1693 if (!(((ictx->howoften > 0) && (!(tao->niter % ictx->howoften))) || ((ictx->howoften == -1) && tao->reason))) PetscFunctionReturn(0); 1694 ierr = VecView(tao->gradient,ictx->viewer);CHKERRQ(ierr); 1695 PetscFunctionReturn(0); 1696 } 1697 1698 /*@C 1699 TaoDrawStepMonitor - Plots the step direction at each iteration 1700 It can be turned on from the command line using the 1701 -tao_draw_step option 1702 1703 Collective on Tao 1704 1705 Input Parameters: 1706 + tao - the Tao context 1707 - ctx - PetscViewer context 1708 1709 Options Database Keys: 1710 . -tao_draw_step 1711 1712 Level: advanced 1713 1714 .seealso: TaoSetMonitor(), TaoDrawSolutionMonitor 1715 @*/ 1716 PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx) 1717 { 1718 PetscErrorCode ierr; 1719 PetscViewer viewer = (PetscViewer)(ctx); 1720 1721 PetscFunctionBegin; 1722 ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr); 1723 PetscFunctionReturn(0); 1724 } 1725 1726 /*@C 1727 TaoSeparableObjectiveMonitor - Views the separable objective function at each iteration 1728 It can be turned on from the command line using the 1729 -tao_view_separableobjective option 1730 1731 Collective on Tao 1732 1733 Input Parameters: 1734 + tao - the Tao context 1735 - ctx - PetscViewer context or NULL 1736 1737 Options Database Keys: 1738 . -tao_view_separableobjective 1739 1740 Level: advanced 1741 1742 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1743 @*/ 1744 PetscErrorCode TaoSeparableObjectiveMonitor(Tao tao, void *ctx) 1745 { 1746 PetscErrorCode ierr; 1747 PetscViewer viewer = (PetscViewer)ctx; 1748 1749 PetscFunctionBegin; 1750 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1751 ierr = VecView(tao->sep_objective,viewer);CHKERRQ(ierr); 1752 PetscFunctionReturn(0); 1753 } 1754 1755 /*@ 1756 TaoDefaultConvergenceTest - Determines whether the solver should continue iterating 1757 or terminate. 1758 1759 Collective on Tao 1760 1761 Input Parameters: 1762 + tao - the Tao context 1763 - dummy - unused dummy context 1764 1765 Output Parameter: 1766 . reason - for terminating 1767 1768 Notes: 1769 This routine checks the residual in the optimality conditions, the 1770 relative residual in the optimity conditions, the number of function 1771 evaluations, and the function value to test convergence. Some 1772 solvers may use different convergence routines. 1773 1774 Level: developer 1775 1776 .seealso: TaoSetTolerances(),TaoGetConvergedReason(),TaoSetConvergedReason() 1777 @*/ 1778 1779 PetscErrorCode TaoDefaultConvergenceTest(Tao tao,void *dummy) 1780 { 1781 PetscInt niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads); 1782 PetscInt max_funcs=tao->max_funcs; 1783 PetscReal gnorm=tao->residual, gnorm0=tao->gnorm0; 1784 PetscReal f=tao->fc, steptol=tao->steptol,trradius=tao->step; 1785 PetscReal gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol; 1786 PetscReal catol=tao->catol,crtol=tao->crtol; 1787 PetscReal fmin=tao->fmin, cnorm=tao->cnorm; 1788 TaoConvergedReason reason=tao->reason; 1789 PetscErrorCode ierr; 1790 1791 PetscFunctionBegin; 1792 PetscValidHeaderSpecific(tao, TAO_CLASSID,1); 1793 if (reason != TAO_CONTINUE_ITERATING) { 1794 PetscFunctionReturn(0); 1795 } 1796 1797 if (PetscIsInfOrNanReal(f)) { 1798 ierr = PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n");CHKERRQ(ierr); 1799 reason = TAO_DIVERGED_NAN; 1800 } else if (f <= fmin && cnorm <=catol) { 1801 ierr = PetscInfo2(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin);CHKERRQ(ierr); 1802 reason = TAO_CONVERGED_MINF; 1803 } else if (gnorm<= gatol && cnorm <=catol) { 1804 ierr = PetscInfo2(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol);CHKERRQ(ierr); 1805 reason = TAO_CONVERGED_GATOL; 1806 } else if ( f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) { 1807 ierr = PetscInfo2(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol);CHKERRQ(ierr); 1808 reason = TAO_CONVERGED_GRTOL; 1809 } else if (gnorm0 != 0 && ((gttol == 0 && gnorm == 0) || gnorm/gnorm0 < gttol) && cnorm <= crtol) { 1810 ierr = PetscInfo2(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol);CHKERRQ(ierr); 1811 reason = TAO_CONVERGED_GTTOL; 1812 } else if (nfuncs > max_funcs){ 1813 ierr = PetscInfo2(tao,"Exceeded maximum number of function evaluations: %D > %D\n", nfuncs,max_funcs);CHKERRQ(ierr); 1814 reason = TAO_DIVERGED_MAXFCN; 1815 } else if ( tao->lsflag != 0 ){ 1816 ierr = PetscInfo(tao,"Tao Line Search failure.\n");CHKERRQ(ierr); 1817 reason = TAO_DIVERGED_LS_FAILURE; 1818 } else if (trradius < steptol && niter > 0){ 1819 ierr = PetscInfo2(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol);CHKERRQ(ierr); 1820 reason = TAO_CONVERGED_STEPTOL; 1821 } else if (niter > tao->max_it) { 1822 ierr = PetscInfo2(tao,"Exceeded maximum number of iterations: %D > %D\n",niter,tao->max_it);CHKERRQ(ierr); 1823 reason = TAO_DIVERGED_MAXITS; 1824 } else { 1825 reason = TAO_CONTINUE_ITERATING; 1826 } 1827 tao->reason = reason; 1828 PetscFunctionReturn(0); 1829 } 1830 1831 /*@C 1832 TaoSetOptionsPrefix - Sets the prefix used for searching for all 1833 TAO options in the database. 1834 1835 1836 Logically Collective on Tao 1837 1838 Input Parameters: 1839 + tao - the Tao context 1840 - prefix - the prefix string to prepend to all TAO option requests 1841 1842 Notes: 1843 A hyphen (-) must NOT be given at the beginning of the prefix name. 1844 The first character of all runtime options is AUTOMATICALLY the hyphen. 1845 1846 For example, to distinguish between the runtime options for two 1847 different TAO solvers, one could call 1848 .vb 1849 TaoSetOptionsPrefix(tao1,"sys1_") 1850 TaoSetOptionsPrefix(tao2,"sys2_") 1851 .ve 1852 1853 This would enable use of different options for each system, such as 1854 .vb 1855 -sys1_tao_method blmvm -sys1_tao_gtol 1.e-3 1856 -sys2_tao_method lmvm -sys2_tao_gtol 1.e-4 1857 .ve 1858 1859 1860 Level: advanced 1861 1862 .seealso: TaoAppendOptionsPrefix(), TaoGetOptionsPrefix() 1863 @*/ 1864 1865 PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[]) 1866 { 1867 PetscErrorCode ierr; 1868 1869 PetscFunctionBegin; 1870 ierr = PetscObjectSetOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr); 1871 if (tao->linesearch) { 1872 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr); 1873 } 1874 if (tao->ksp) { 1875 ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr); 1876 } 1877 PetscFunctionReturn(0); 1878 } 1879 1880 /*@C 1881 TaoAppendOptionsPrefix - Appends to the prefix used for searching for all 1882 TAO options in the database. 1883 1884 1885 Logically Collective on Tao 1886 1887 Input Parameters: 1888 + tao - the Tao solver context 1889 - prefix - the prefix string to prepend to all TAO option requests 1890 1891 Notes: 1892 A hyphen (-) must NOT be given at the beginning of the prefix name. 1893 The first character of all runtime options is AUTOMATICALLY the hyphen. 1894 1895 1896 Level: advanced 1897 1898 .seealso: TaoSetOptionsPrefix(), TaoGetOptionsPrefix() 1899 @*/ 1900 PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[]) 1901 { 1902 PetscErrorCode ierr; 1903 1904 PetscFunctionBegin; 1905 ierr = PetscObjectAppendOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr); 1906 if (tao->linesearch) { 1907 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr); 1908 } 1909 if (tao->ksp) { 1910 ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr); 1911 } 1912 PetscFunctionReturn(0); 1913 } 1914 1915 /*@C 1916 TaoGetOptionsPrefix - Gets the prefix used for searching for all 1917 TAO options in the database 1918 1919 Not Collective 1920 1921 Input Parameters: 1922 . tao - the Tao context 1923 1924 Output Parameters: 1925 . prefix - pointer to the prefix string used is returned 1926 1927 Notes: On the fortran side, the user should pass in a string 'prefix' of 1928 sufficient length to hold the prefix. 1929 1930 Level: advanced 1931 1932 .seealso: TaoSetOptionsPrefix(), TaoAppendOptionsPrefix() 1933 @*/ 1934 PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[]) 1935 { 1936 return PetscObjectGetOptionsPrefix((PetscObject)tao,p); 1937 } 1938 1939 /*@C 1940 TaoSetType - Sets the method for the unconstrained minimization solver. 1941 1942 Collective on Tao 1943 1944 Input Parameters: 1945 + solver - the Tao solver context 1946 - type - a known method 1947 1948 Options Database Key: 1949 . -tao_type <type> - Sets the method; use -help for a list 1950 of available methods (for instance, "-tao_type lmvm" or "-tao_type tron") 1951 1952 Available methods include: 1953 + nls - Newton's method with line search for unconstrained minimization 1954 . ntr - Newton's method with trust region for unconstrained minimization 1955 . ntl - Newton's method with trust region, line search for unconstrained minimization 1956 . lmvm - Limited memory variable metric method for unconstrained minimization 1957 . cg - Nonlinear conjugate gradient method for unconstrained minimization 1958 . nm - Nelder-Mead algorithm for derivate-free unconstrained minimization 1959 . tron - Newton Trust Region method for bound constrained minimization 1960 . gpcg - Newton Trust Region method for quadratic bound constrained minimization 1961 . blmvm - Limited memory variable metric method for bound constrained minimization 1962 - pounders - Model-based algorithm pounder extended for nonlinear least squares 1963 1964 Level: intermediate 1965 1966 .seealso: TaoCreate(), TaoGetType(), TaoType 1967 1968 @*/ 1969 PetscErrorCode TaoSetType(Tao tao, const TaoType type) 1970 { 1971 PetscErrorCode ierr; 1972 PetscErrorCode (*create_xxx)(Tao); 1973 PetscBool issame; 1974 1975 PetscFunctionBegin; 1976 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 1977 1978 ierr = PetscObjectTypeCompare((PetscObject)tao,type,&issame);CHKERRQ(ierr); 1979 if (issame) PetscFunctionReturn(0); 1980 1981 ierr = PetscFunctionListFind(TaoList, type, (void(**)(void))&create_xxx);CHKERRQ(ierr); 1982 if (!create_xxx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested Tao type %s",type); 1983 1984 /* Destroy the existing solver information */ 1985 if (tao->ops->destroy) { 1986 ierr = (*tao->ops->destroy)(tao);CHKERRQ(ierr); 1987 } 1988 ierr = KSPDestroy(&tao->ksp);CHKERRQ(ierr); 1989 ierr = TaoLineSearchDestroy(&tao->linesearch);CHKERRQ(ierr); 1990 ierr = VecDestroy(&tao->gradient);CHKERRQ(ierr); 1991 ierr = VecDestroy(&tao->stepdirection);CHKERRQ(ierr); 1992 1993 tao->ops->setup = 0; 1994 tao->ops->solve = 0; 1995 tao->ops->view = 0; 1996 tao->ops->setfromoptions = 0; 1997 tao->ops->destroy = 0; 1998 1999 tao->setupcalled = PETSC_FALSE; 2000 2001 ierr = (*create_xxx)(tao);CHKERRQ(ierr); 2002 ierr = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(ierr); 2003 PetscFunctionReturn(0); 2004 } 2005 2006 /*MC 2007 TaoRegister - Adds a method to the TAO package for unconstrained minimization. 2008 2009 Synopsis: 2010 TaoRegister(char *name_solver,char *path,char *name_Create,int (*routine_Create)(Tao)) 2011 2012 Not collective 2013 2014 Input Parameters: 2015 + sname - name of a new user-defined solver 2016 - func - routine to Create method context 2017 2018 Notes: 2019 TaoRegister() may be called multiple times to add several user-defined solvers. 2020 2021 Sample usage: 2022 .vb 2023 TaoRegister("my_solver",MySolverCreate); 2024 .ve 2025 2026 Then, your solver can be chosen with the procedural interface via 2027 $ TaoSetType(tao,"my_solver") 2028 or at runtime via the option 2029 $ -tao_type my_solver 2030 2031 Level: advanced 2032 2033 .seealso: TaoRegisterAll(), TaoRegisterDestroy() 2034 M*/ 2035 PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao)) 2036 { 2037 PetscErrorCode ierr; 2038 2039 PetscFunctionBegin; 2040 ierr = PetscFunctionListAdd(&TaoList,sname, (void (*)(void))func);CHKERRQ(ierr); 2041 PetscFunctionReturn(0); 2042 } 2043 2044 /*@C 2045 TaoRegisterDestroy - Frees the list of minimization solvers that were 2046 registered by TaoRegisterDynamic(). 2047 2048 Not Collective 2049 2050 Level: advanced 2051 2052 .seealso: TaoRegisterAll(), TaoRegister() 2053 @*/ 2054 PetscErrorCode TaoRegisterDestroy(void) 2055 { 2056 PetscErrorCode ierr; 2057 PetscFunctionBegin; 2058 ierr = PetscFunctionListDestroy(&TaoList);CHKERRQ(ierr); 2059 TaoRegisterAllCalled = PETSC_FALSE; 2060 PetscFunctionReturn(0); 2061 } 2062 2063 /*@ 2064 TaoGetIterationNumber - Gets the number of Tao iterations completed 2065 at this time. 2066 2067 Not Collective 2068 2069 Input Parameter: 2070 . tao - Tao context 2071 2072 Output Parameter: 2073 . iter - iteration number 2074 2075 Notes: 2076 For example, during the computation of iteration 2 this would return 1. 2077 2078 2079 Level: intermediate 2080 2081 .keywords: Tao, nonlinear, get, iteration, number, 2082 2083 .seealso: TaoGetLinearSolveIterations() 2084 @*/ 2085 PetscErrorCode TaoGetIterationNumber(Tao tao,PetscInt *iter) 2086 { 2087 PetscFunctionBegin; 2088 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2089 PetscValidIntPointer(iter,2); 2090 *iter = tao->niter; 2091 PetscFunctionReturn(0); 2092 } 2093 2094 /*@ 2095 TaoSetIterationNumber - Sets the current iteration number. 2096 2097 Not Collective 2098 2099 Input Parameter: 2100 . tao - Tao context 2101 . iter - iteration number 2102 2103 Level: developer 2104 2105 .keywords: Tao, nonlinear, set, iteration, number, 2106 2107 .seealso: TaoGetLinearSolveIterations() 2108 @*/ 2109 PetscErrorCode TaoSetIterationNumber(Tao tao,PetscInt iter) 2110 { 2111 PetscErrorCode ierr; 2112 2113 PetscFunctionBegin; 2114 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2115 ierr = PetscObjectSAWsTakeAccess((PetscObject)tao);CHKERRQ(ierr); 2116 tao->niter = iter; 2117 ierr = PetscObjectSAWsGrantAccess((PetscObject)tao);CHKERRQ(ierr); 2118 PetscFunctionReturn(0); 2119 } 2120 2121 /*@ 2122 TaoGetTotalIterationNumber - Gets the total number of Tao iterations 2123 completed. This number keeps accumulating if multiple solves 2124 are called with the Tao object. 2125 2126 Not Collective 2127 2128 Input Parameter: 2129 . tao - Tao context 2130 2131 Output Parameter: 2132 . iter - iteration number 2133 2134 Notes: 2135 The total iteration count is updated after each solve, if there is a current 2136 TaoSolve() in progress then those iterations are not yet counted. 2137 2138 Level: intermediate 2139 2140 .keywords: Tao, nonlinear, get, iteration, number, 2141 2142 .seealso: TaoGetLinearSolveIterations() 2143 @*/ 2144 PetscErrorCode TaoGetTotalIterationNumber(Tao tao,PetscInt *iter) 2145 { 2146 PetscFunctionBegin; 2147 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2148 PetscValidIntPointer(iter,2); 2149 *iter = tao->ntotalits; 2150 PetscFunctionReturn(0); 2151 } 2152 2153 /*@ 2154 TaoSetTotalIterationNumber - Sets the current total iteration number. 2155 2156 Not Collective 2157 2158 Input Parameter: 2159 . tao - Tao context 2160 . iter - iteration number 2161 2162 Level: developer 2163 2164 .keywords: Tao, nonlinear, set, iteration, number, 2165 2166 .seealso: TaoGetLinearSolveIterations() 2167 @*/ 2168 PetscErrorCode TaoSetTotalIterationNumber(Tao tao,PetscInt iter) 2169 { 2170 PetscErrorCode ierr; 2171 2172 PetscFunctionBegin; 2173 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2174 ierr = PetscObjectSAWsTakeAccess((PetscObject)tao);CHKERRQ(ierr); 2175 tao->ntotalits = iter; 2176 ierr = PetscObjectSAWsGrantAccess((PetscObject)tao);CHKERRQ(ierr); 2177 PetscFunctionReturn(0); 2178 } 2179 2180 /*@ 2181 TaoSetConvergedReason - Sets the termination flag on a Tao object 2182 2183 Logically Collective on Tao 2184 2185 Input Parameters: 2186 + tao - the Tao context 2187 - reason - one of 2188 $ TAO_CONVERGED_ATOL (2), 2189 $ TAO_CONVERGED_RTOL (3), 2190 $ TAO_CONVERGED_STEPTOL (4), 2191 $ TAO_CONVERGED_MINF (5), 2192 $ TAO_CONVERGED_USER (6), 2193 $ TAO_DIVERGED_MAXITS (-2), 2194 $ TAO_DIVERGED_NAN (-4), 2195 $ TAO_DIVERGED_MAXFCN (-5), 2196 $ TAO_DIVERGED_LS_FAILURE (-6), 2197 $ TAO_DIVERGED_TR_REDUCTION (-7), 2198 $ TAO_DIVERGED_USER (-8), 2199 $ TAO_CONTINUE_ITERATING (0) 2200 2201 Level: intermediate 2202 2203 @*/ 2204 PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason) 2205 { 2206 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2207 PetscFunctionBegin; 2208 tao->reason = reason; 2209 PetscFunctionReturn(0); 2210 } 2211 2212 /*@ 2213 TaoGetConvergedReason - Gets the reason the Tao iteration was stopped. 2214 2215 Not Collective 2216 2217 Input Parameter: 2218 . tao - the Tao solver context 2219 2220 Output Parameter: 2221 . reason - one of 2222 $ TAO_CONVERGED_GATOL (3) ||g(X)|| < gatol 2223 $ TAO_CONVERGED_GRTOL (4) ||g(X)|| / f(X) < grtol 2224 $ TAO_CONVERGED_GTTOL (5) ||g(X)|| / ||g(X0)|| < gttol 2225 $ TAO_CONVERGED_STEPTOL (6) step size small 2226 $ TAO_CONVERGED_MINF (7) F < F_min 2227 $ TAO_CONVERGED_USER (8) User defined 2228 $ TAO_DIVERGED_MAXITS (-2) its > maxits 2229 $ TAO_DIVERGED_NAN (-4) Numerical problems 2230 $ TAO_DIVERGED_MAXFCN (-5) fevals > max_funcsals 2231 $ TAO_DIVERGED_LS_FAILURE (-6) line search failure 2232 $ TAO_DIVERGED_TR_REDUCTION (-7) trust region failure 2233 $ TAO_DIVERGED_USER(-8) (user defined) 2234 $ TAO_CONTINUE_ITERATING (0) 2235 2236 where 2237 + X - current solution 2238 . X0 - initial guess 2239 . f(X) - current function value 2240 . f(X*) - true solution (estimated) 2241 . g(X) - current gradient 2242 . its - current iterate number 2243 . maxits - maximum number of iterates 2244 . fevals - number of function evaluations 2245 - max_funcsals - maximum number of function evaluations 2246 2247 Level: intermediate 2248 2249 .seealso: TaoSetConvergenceTest(), TaoSetTolerances() 2250 2251 @*/ 2252 PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason) 2253 { 2254 PetscFunctionBegin; 2255 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2256 PetscValidPointer(reason,2); 2257 *reason = tao->reason; 2258 PetscFunctionReturn(0); 2259 } 2260 2261 /*@ 2262 TaoGetSolutionStatus - Get the current iterate, objective value, 2263 residual, infeasibility, and termination 2264 2265 Not Collective 2266 2267 Input Parameters: 2268 . tao - the Tao context 2269 2270 Output Parameters: 2271 + iterate - the current iterate number (>=0) 2272 . f - the current function value 2273 . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality. 2274 . cnorm - the infeasibility of the current solution with regard to the constraints. 2275 . xdiff - the step length or trust region radius of the most recent iterate. 2276 - reason - The termination reason, which can equal TAO_CONTINUE_ITERATING 2277 2278 Level: intermediate 2279 2280 Note: 2281 TAO returns the values set by the solvers in the routine TaoMonitor(). 2282 2283 Note: 2284 If any of the output arguments are set to NULL, no corresponding value will be returned. 2285 2286 .seealso: TaoMonitor(), TaoGetConvergedReason() 2287 @*/ 2288 PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason) 2289 { 2290 PetscFunctionBegin; 2291 if (its) *its=tao->niter; 2292 if (f) *f=tao->fc; 2293 if (gnorm) *gnorm=tao->residual; 2294 if (cnorm) *cnorm=tao->cnorm; 2295 if (reason) *reason=tao->reason; 2296 if (xdiff) *xdiff=tao->step; 2297 PetscFunctionReturn(0); 2298 } 2299 2300 /*@C 2301 TaoGetType - Gets the current Tao algorithm. 2302 2303 Not Collective 2304 2305 Input Parameter: 2306 . tao - the Tao solver context 2307 2308 Output Parameter: 2309 . type - Tao method 2310 2311 Level: intermediate 2312 2313 @*/ 2314 PetscErrorCode TaoGetType(Tao tao, const TaoType *type) 2315 { 2316 PetscFunctionBegin; 2317 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2318 PetscValidPointer(type,2); 2319 *type=((PetscObject)tao)->type_name; 2320 PetscFunctionReturn(0); 2321 } 2322 2323 /*@C 2324 TaoMonitor - Monitor the solver and the current solution. This 2325 routine will record the iteration number and residual statistics, 2326 call any monitors specified by the user, and calls the convergence-check routine. 2327 2328 Input Parameters: 2329 + tao - the Tao context 2330 . its - the current iterate number (>=0) 2331 . f - the current objective function value 2332 . res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality. This measure will be recorded and 2333 used for some termination tests. 2334 . cnorm - the infeasibility of the current solution with regard to the constraints. 2335 - steplength - multiple of the step direction added to the previous iterate. 2336 2337 Output Parameters: 2338 . reason - The termination reason, which can equal TAO_CONTINUE_ITERATING 2339 2340 Options Database Key: 2341 . -tao_monitor - Use the default monitor, which prints statistics to standard output 2342 2343 .seealso TaoGetConvergedReason(), TaoDefaultMonitor(), TaoSetMonitor() 2344 2345 Level: developer 2346 2347 @*/ 2348 PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength, TaoConvergedReason *reason) 2349 { 2350 PetscErrorCode ierr; 2351 PetscInt i; 2352 2353 PetscFunctionBegin; 2354 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2355 tao->fc = f; 2356 tao->residual = res; 2357 tao->cnorm = cnorm; 2358 tao->step = steplength; 2359 if (!its) { 2360 tao->cnorm0 = cnorm; tao->gnorm0 = res; 2361 } 2362 TaoLogConvergenceHistory(tao,f,res,cnorm,tao->ksp_its); 2363 if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(res)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); 2364 if (tao->ops->convergencetest) { 2365 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 2366 } 2367 for (i=0;i<tao->numbermonitors;i++) { 2368 ierr = (*tao->monitor[i])(tao,tao->monitorcontext[i]);CHKERRQ(ierr); 2369 } 2370 *reason = tao->reason; 2371 PetscFunctionReturn(0); 2372 } 2373 2374 /*@ 2375 TaoSetConvergenceHistory - Sets the array used to hold the convergence history. 2376 2377 Logically Collective on Tao 2378 2379 Input Parameters: 2380 + tao - the Tao solver context 2381 . obj - array to hold objective value history 2382 . resid - array to hold residual history 2383 . cnorm - array to hold constraint violation history 2384 . lits - integer array holds the number of linear iterations for each Tao iteration 2385 . na - size of obj, resid, and cnorm 2386 - reset - PetscTrue indicates each new minimization resets the history counter to zero, 2387 else it continues storing new values for new minimizations after the old ones 2388 2389 Notes: 2390 If set, TAO will fill the given arrays with the indicated 2391 information at each iteration. If 'obj','resid','cnorm','lits' are 2392 *all* NULL then space (using size na, or 1000 if na is PETSC_DECIDE or 2393 PETSC_DEFAULT) is allocated for the history. 2394 If not all are NULL, then only the non-NULL information categories 2395 will be stored, the others will be ignored. 2396 2397 Any convergence information after iteration number 'na' will not be stored. 2398 2399 This routine is useful, e.g., when running a code for purposes 2400 of accurate performance monitoring, when no I/O should be done 2401 during the section of code that is being timed. 2402 2403 Level: intermediate 2404 2405 .seealso: TaoGetConvergenceHistory() 2406 2407 @*/ 2408 PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal obj[], PetscReal resid[], PetscReal cnorm[], PetscInt lits[], PetscInt na,PetscBool reset) 2409 { 2410 PetscErrorCode ierr; 2411 2412 PetscFunctionBegin; 2413 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2414 if (obj) PetscValidScalarPointer(obj,2); 2415 if (resid) PetscValidScalarPointer(resid,3); 2416 if (cnorm) PetscValidScalarPointer(cnorm,4); 2417 if (lits) PetscValidIntPointer(lits,5); 2418 2419 if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2420 if (!obj && !resid && !cnorm && !lits) { 2421 ierr = PetscCalloc1(na,&obj);CHKERRQ(ierr); 2422 ierr = PetscCalloc1(na,&resid);CHKERRQ(ierr); 2423 ierr = PetscCalloc1(na,&cnorm);CHKERRQ(ierr); 2424 ierr = PetscCalloc1(na,&lits);CHKERRQ(ierr); 2425 tao->hist_malloc=PETSC_TRUE; 2426 } 2427 2428 tao->hist_obj = obj; 2429 tao->hist_resid = resid; 2430 tao->hist_cnorm = cnorm; 2431 tao->hist_lits = lits; 2432 tao->hist_max = na; 2433 tao->hist_reset = reset; 2434 tao->hist_len = 0; 2435 PetscFunctionReturn(0); 2436 } 2437 2438 /*@C 2439 TaoGetConvergenceHistory - Gets the arrays used to hold the convergence history. 2440 2441 Collective on Tao 2442 2443 Input Parameter: 2444 . tao - the Tao context 2445 2446 Output Parameters: 2447 + obj - array used to hold objective value history 2448 . resid - array used to hold residual history 2449 . cnorm - array used to hold constraint violation history 2450 . lits - integer array used to hold linear solver iteration count 2451 - nhist - size of obj, resid, cnorm, and lits (will be less than or equal to na given in TaoSetHistory) 2452 2453 Notes: 2454 This routine must be preceded by calls to TaoSetConvergenceHistory() 2455 and TaoSolve(), otherwise it returns useless information. 2456 2457 The calling sequence for this routine in Fortran is 2458 $ call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr) 2459 2460 This routine is useful, e.g., when running a code for purposes 2461 of accurate performance monitoring, when no I/O should be done 2462 during the section of code that is being timed. 2463 2464 Level: advanced 2465 2466 .seealso: TaoSetConvergenceHistory() 2467 2468 @*/ 2469 PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist) 2470 { 2471 PetscFunctionBegin; 2472 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2473 if (obj) *obj = tao->hist_obj; 2474 if (cnorm) *cnorm = tao->hist_cnorm; 2475 if (resid) *resid = tao->hist_resid; 2476 if (nhist) *nhist = tao->hist_len; 2477 PetscFunctionReturn(0); 2478 } 2479 2480 /*@ 2481 TaoSetApplicationContext - Sets the optional user-defined context for 2482 a solver. 2483 2484 Logically Collective on Tao 2485 2486 Input Parameters: 2487 + tao - the Tao context 2488 - usrP - optional user context 2489 2490 Level: intermediate 2491 2492 .seealso: TaoGetApplicationContext(), TaoSetApplicationContext() 2493 @*/ 2494 PetscErrorCode TaoSetApplicationContext(Tao tao,void *usrP) 2495 { 2496 PetscFunctionBegin; 2497 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2498 tao->user = usrP; 2499 PetscFunctionReturn(0); 2500 } 2501 2502 /*@ 2503 TaoGetApplicationContext - Gets the user-defined context for a 2504 TAO solvers. 2505 2506 Not Collective 2507 2508 Input Parameter: 2509 . tao - Tao context 2510 2511 Output Parameter: 2512 . usrP - user context 2513 2514 Level: intermediate 2515 2516 .seealso: TaoSetApplicationContext() 2517 @*/ 2518 PetscErrorCode TaoGetApplicationContext(Tao tao,void *usrP) 2519 { 2520 PetscFunctionBegin; 2521 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2522 *(void**)usrP = tao->user; 2523 PetscFunctionReturn(0); 2524 } 2525 2526 /*@ 2527 TaoSetGradientNorm - Sets the matrix used to define the inner product that measures the size of the gradient. 2528 2529 Collective on tao 2530 2531 Input Parameters: 2532 + tao - the Tao context 2533 - M - gradient norm 2534 2535 Level: beginner 2536 2537 .seealso: TaoGetGradientNorm(), TaoGradientNorm() 2538 @*/ 2539 PetscErrorCode TaoSetGradientNorm(Tao tao, Mat M) 2540 { 2541 PetscErrorCode ierr; 2542 2543 PetscFunctionBegin; 2544 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2545 2546 if (tao->gradient_norm) { 2547 ierr = PetscObjectDereference((PetscObject)tao->gradient_norm);CHKERRQ(ierr); 2548 ierr = VecDestroy(&tao->gradient_norm_tmp);CHKERRQ(ierr); 2549 } 2550 2551 ierr = PetscObjectReference((PetscObject)M);CHKERRQ(ierr); 2552 tao->gradient_norm = M; 2553 ierr = MatCreateVecs(M, NULL, &tao->gradient_norm_tmp);CHKERRQ(ierr); 2554 PetscFunctionReturn(0); 2555 } 2556 2557 /*@ 2558 TaoGetGradientNorm - Returns the matrix used to define the inner product for measuring the size of the gradient. 2559 2560 Not Collective 2561 2562 Input Parameter: 2563 . tao - Tao context 2564 2565 Output Parameter: 2566 . M - gradient norm 2567 2568 Level: beginner 2569 2570 .seealso: TaoSetGradientNorm(), TaoGradientNorm() 2571 @*/ 2572 PetscErrorCode TaoGetGradientNorm(Tao tao, Mat *M) 2573 { 2574 PetscFunctionBegin; 2575 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2576 *M = tao->gradient_norm; 2577 PetscFunctionReturn(0); 2578 } 2579 2580 /*c 2581 TaoGradientNorm - Compute the norm with respect to the inner product the user has set. 2582 2583 Collective on tao 2584 2585 Input Parameter: 2586 . tao - the Tao context 2587 . gradient - the gradient to be computed 2588 . norm - the norm type 2589 2590 Output Parameter: 2591 . gnorm - the gradient norm 2592 2593 Level: developer 2594 2595 .seealso: TaoSetGradientNorm(), TaoGetGradientNorm() 2596 @*/ 2597 PetscErrorCode TaoGradientNorm(Tao tao, Vec gradient, NormType type, PetscReal *gnorm) 2598 { 2599 PetscErrorCode ierr; 2600 2601 PetscFunctionBegin; 2602 PetscValidHeaderSpecific(gradient,VEC_CLASSID,1); 2603 2604 if (tao->gradient_norm) { 2605 PetscScalar gnorms; 2606 2607 if (type != NORM_2) SETERRQ(PetscObjectComm((PetscObject)gradient), PETSC_ERR_ARG_WRONGSTATE, "Norm type must be NORM_2 if an inner product for the gradient norm is set."); 2608 ierr = MatMult(tao->gradient_norm, gradient, tao->gradient_norm_tmp);CHKERRQ(ierr); 2609 ierr = VecDot(gradient, tao->gradient_norm_tmp, &gnorms);CHKERRQ(ierr); 2610 *gnorm = PetscRealPart(PetscSqrtScalar(gnorms)); 2611 } else { 2612 ierr = VecNorm(gradient, type, gnorm);CHKERRQ(ierr); 2613 } 2614 PetscFunctionReturn(0); 2615 } 2616 2617 /*@C 2618 TaoMonitorDrawCtxCreate - Creates the monitor context for TaoMonitorDrawCtx 2619 2620 Collective on Tao 2621 2622 Output Patameter: 2623 . ctx - the monitor context 2624 2625 Options Database: 2626 . -tao_draw_solution_initial - show initial guess as well as current solution 2627 2628 Level: intermediate 2629 2630 .keywords: Tao, vector, monitor, view 2631 2632 .seealso: TaoMonitorSet(), TaoMonitorDefault(), VecView(), TaoMonitorDrawCtx() 2633 @*/ 2634 PetscErrorCode TaoMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TaoMonitorDrawCtx *ctx) 2635 { 2636 PetscErrorCode ierr; 2637 2638 PetscFunctionBegin; 2639 ierr = PetscNew(ctx);CHKERRQ(ierr); 2640 ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 2641 ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 2642 (*ctx)->howoften = howoften; 2643 PetscFunctionReturn(0); 2644 } 2645 2646 /*@C 2647 TaoMonitorDrawCtxDestroy - Destroys the monitor context for TaoMonitorDrawSolution() 2648 2649 Collective on Tao 2650 2651 Input Parameters: 2652 . ctx - the monitor context 2653 2654 Level: intermediate 2655 2656 .keywords: Tao, vector, monitor, view 2657 2658 .seealso: TaoMonitorSet(), TaoMonitorDefault(), VecView(), TaoMonitorDrawSolution() 2659 @*/ 2660 PetscErrorCode TaoMonitorDrawCtxDestroy(TaoMonitorDrawCtx *ictx) 2661 { 2662 PetscErrorCode ierr; 2663 2664 PetscFunctionBegin; 2665 ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 2666 ierr = PetscFree(*ictx);CHKERRQ(ierr); 2667 PetscFunctionReturn(0); 2668 } 2669