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