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