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