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