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 or NULL 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 MPI_Comm comm; 1780 1781 PetscFunctionBegin; 1782 if (!viewer) { 1783 ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr); 1784 viewer = PETSC_VIEWER_DRAW_(comm); 1785 } 1786 ierr = VecView(tao->solution, viewer);CHKERRQ(ierr); 1787 PetscFunctionReturn(0); 1788 } 1789 1790 #undef __FUNCT__ 1791 #define __FUNCT__ "TaoDrawGradientMonitor" 1792 /*@C 1793 TaoDrawGradientMonitor - Plots the gradient at each iteration 1794 It can be turned on from the command line using the 1795 -tao_draw_gradient option 1796 1797 Collective on Tao 1798 1799 Input Parameters: 1800 + tao - the Tao context 1801 - ctx - PetscViewer context or NULL 1802 1803 Options Database Keys: 1804 . -tao_draw_gradient 1805 1806 Level: advanced 1807 1808 .seealso: TaoGradientMonitor(), TaoSetMonitor(), TaoDrawSolutionMonitor 1809 @*/ 1810 PetscErrorCode TaoDrawGradientMonitor(Tao tao, void *ctx) 1811 { 1812 PetscErrorCode ierr; 1813 PetscViewer viewer = (PetscViewer)ctx; 1814 MPI_Comm comm; 1815 1816 PetscFunctionBegin; 1817 if (!viewer) { 1818 ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr); 1819 viewer = PETSC_VIEWER_DRAW_(comm); 1820 } 1821 ierr = VecView(tao->gradient, viewer);CHKERRQ(ierr); 1822 PetscFunctionReturn(0); 1823 } 1824 1825 #undef __FUNCT__ 1826 #define __FUNCT__ "TaoDrawStepMonitor" 1827 /*@C 1828 TaoDrawStepMonitor - Plots the step direction at each iteration 1829 It can be turned on from the command line using the 1830 -tao_draw_step option 1831 1832 Collective on Tao 1833 1834 Input Parameters: 1835 + tao - the Tao context 1836 - ctx - PetscViewer context or NULL 1837 1838 Options Database Keys: 1839 . -tao_draw_step 1840 1841 Level: advanced 1842 1843 .seealso: TaoSetMonitor(), TaoDrawSolutionMonitor 1844 @*/ 1845 PetscErrorCode TaoDrawStepMonitor(Tao tao, void *ctx) 1846 { 1847 PetscErrorCode ierr; 1848 PetscViewer viewer = (PetscViewer)(ctx); 1849 MPI_Comm comm; 1850 1851 PetscFunctionBegin; 1852 if (!viewer) { 1853 ierr = PetscObjectGetComm((PetscObject)tao,&comm);CHKERRQ(ierr); 1854 viewer = PETSC_VIEWER_DRAW_(comm); 1855 } 1856 ierr = VecView(tao->stepdirection, viewer);CHKERRQ(ierr); 1857 PetscFunctionReturn(0); 1858 } 1859 1860 #undef __FUNCT__ 1861 #define __FUNCT__ "TaoSeparableObjectiveMonitor" 1862 /*@C 1863 TaoSeparableObjectiveMonitor - Views the separable objective function at each iteration 1864 It can be turned on from the command line using the 1865 -tao_view_separableobjective option 1866 1867 Collective on Tao 1868 1869 Input Parameters: 1870 + tao - the Tao context 1871 - ctx - PetscViewer context or NULL 1872 1873 Options Database Keys: 1874 . -tao_view_separableobjective 1875 1876 Level: advanced 1877 1878 .seealso: TaoDefaultSMonitor(), TaoSetMonitor() 1879 @*/ 1880 PetscErrorCode TaoSeparableObjectiveMonitor(Tao tao, void *ctx) 1881 { 1882 PetscErrorCode ierr; 1883 PetscViewer viewer; 1884 1885 PetscFunctionBegin; 1886 if (ctx) { 1887 viewer = (PetscViewer)ctx; 1888 } else { 1889 viewer = PETSC_VIEWER_STDOUT_(((PetscObject)tao)->comm); 1890 } 1891 ierr = VecView(tao->sep_objective,viewer);CHKERRQ(ierr); 1892 PetscFunctionReturn(0); 1893 } 1894 1895 #undef __FUNCT__ 1896 #define __FUNCT__ "TaoDefaultConvergenceTest" 1897 /*@ 1898 TaoDefaultConvergenceTest - Determines whether the solver should continue iterating 1899 or terminate. 1900 1901 Collective on Tao 1902 1903 Input Parameters: 1904 + tao - the Tao context 1905 - dummy - unused dummy context 1906 1907 Output Parameter: 1908 . reason - for terminating 1909 1910 Notes: 1911 This routine checks the residual in the optimality conditions, the 1912 relative residual in the optimity conditions, the number of function 1913 evaluations, and the function value to test convergence. Some 1914 solvers may use different convergence routines. 1915 1916 Level: developer 1917 1918 .seealso: TaoSetTolerances(),TaoGetConvergedReason(),TaoSetConvergedReason() 1919 @*/ 1920 1921 PetscErrorCode TaoDefaultConvergenceTest(Tao tao,void *dummy) 1922 { 1923 PetscInt niter=tao->niter, nfuncs=PetscMax(tao->nfuncs,tao->nfuncgrads); 1924 PetscInt max_funcs=tao->max_funcs; 1925 PetscReal gnorm=tao->residual, gnorm0=tao->gnorm0; 1926 PetscReal f=tao->fc, steptol=tao->steptol,trradius=tao->step; 1927 PetscReal gatol=tao->gatol,grtol=tao->grtol,gttol=tao->gttol; 1928 PetscReal fatol=tao->fatol,frtol=tao->frtol,catol=tao->catol,crtol=tao->crtol; 1929 PetscReal fmin=tao->fmin, cnorm=tao->cnorm, cnorm0=tao->cnorm0; 1930 PetscReal gnorm2; 1931 TaoConvergedReason reason=tao->reason; 1932 PetscErrorCode ierr; 1933 1934 PetscFunctionBegin; 1935 PetscValidHeaderSpecific(tao, TAO_CLASSID,1); 1936 if (reason != TAO_CONTINUE_ITERATING) { 1937 PetscFunctionReturn(0); 1938 } 1939 gnorm2=gnorm*gnorm; 1940 1941 if (PetscIsInfOrNanReal(f)) { 1942 ierr = PetscInfo(tao,"Failed to converged, function value is Inf or NaN\n");CHKERRQ(ierr); 1943 reason = TAO_DIVERGED_NAN; 1944 } else if (f <= fmin && cnorm <=catol) { 1945 ierr = PetscInfo2(tao,"Converged due to function value %g < minimum function value %g\n", (double)f,(double)fmin);CHKERRQ(ierr); 1946 reason = TAO_CONVERGED_MINF; 1947 } else if (gnorm2 <= fatol && cnorm <=catol) { 1948 ierr = PetscInfo2(tao,"Converged due to estimated f(X) - f(X*) = %g < %g\n",(double)gnorm2,(double)fatol);CHKERRQ(ierr); 1949 reason = TAO_CONVERGED_FATOL; 1950 } else if (f != 0 && gnorm2 / PetscAbsReal(f)<= frtol && cnorm/PetscMax(cnorm0,1.0) <= crtol) { 1951 ierr = PetscInfo2(tao,"Converged due to estimated |f(X)-f(X*)|/f(X) = %g < %g\n",(double)(gnorm2/PetscAbsReal(f)),(double)frtol);CHKERRQ(ierr); 1952 reason = TAO_CONVERGED_FRTOL; 1953 } else if (gnorm<= gatol && cnorm <=catol) { 1954 ierr = PetscInfo2(tao,"Converged due to residual norm ||g(X)||=%g < %g\n",(double)gnorm,(double)gatol);CHKERRQ(ierr); 1955 reason = TAO_CONVERGED_GATOL; 1956 } else if ( f!=0 && PetscAbsReal(gnorm/f) <= grtol && cnorm <= crtol) { 1957 ierr = PetscInfo2(tao,"Converged due to residual ||g(X)||/|f(X)| =%g < %g\n",(double)(gnorm/f),(double)grtol);CHKERRQ(ierr); 1958 reason = TAO_CONVERGED_GRTOL; 1959 } else if (gnorm0 != 0 && gnorm/gnorm0 <= gttol && cnorm <= crtol) { 1960 ierr = PetscInfo2(tao,"Converged due to relative residual norm ||g(X)||/||g(X0)|| = %g < %g\n",(double)(gnorm/gnorm0),(double)gttol);CHKERRQ(ierr); 1961 reason = TAO_CONVERGED_GTTOL; 1962 } else if (nfuncs > max_funcs){ 1963 ierr = PetscInfo2(tao,"Exceeded maximum number of function evaluations: %D > %D\n", nfuncs,max_funcs);CHKERRQ(ierr); 1964 reason = TAO_DIVERGED_MAXFCN; 1965 } else if ( tao->lsflag != 0 ){ 1966 ierr = PetscInfo(tao,"Tao Line Search failure.\n");CHKERRQ(ierr); 1967 reason = TAO_DIVERGED_LS_FAILURE; 1968 } else if (trradius < steptol && niter > 0){ 1969 ierr = PetscInfo2(tao,"Trust region/step size too small: %g < %g\n", (double)trradius,(double)steptol);CHKERRQ(ierr); 1970 reason = TAO_CONVERGED_STEPTOL; 1971 } else if (niter > tao->max_it) { 1972 ierr = PetscInfo2(tao,"Exceeded maximum number of iterations: %D > %D\n",niter,tao->max_it);CHKERRQ(ierr); 1973 reason = TAO_DIVERGED_MAXITS; 1974 } else { 1975 reason = TAO_CONTINUE_ITERATING; 1976 } 1977 tao->reason = reason; 1978 PetscFunctionReturn(0); 1979 } 1980 1981 #undef __FUNCT__ 1982 #define __FUNCT__ "TaoSetOptionsPrefix" 1983 /*@C 1984 TaoSetOptionsPrefix - Sets the prefix used for searching for all 1985 TAO options in the database. 1986 1987 1988 Logically Collective on Tao 1989 1990 Input Parameters: 1991 + tao - the Tao context 1992 - prefix - the prefix string to prepend to all TAO option requests 1993 1994 Notes: 1995 A hyphen (-) must NOT be given at the beginning of the prefix name. 1996 The first character of all runtime options is AUTOMATICALLY the hyphen. 1997 1998 For example, to distinguish between the runtime options for two 1999 different TAO solvers, one could call 2000 .vb 2001 TaoSetOptionsPrefix(tao1,"sys1_") 2002 TaoSetOptionsPrefix(tao2,"sys2_") 2003 .ve 2004 2005 This would enable use of different options for each system, such as 2006 .vb 2007 -sys1_tao_method blmvm -sys1_tao_gtol 1.e-3 2008 -sys2_tao_method lmvm -sys2_tao_gtol 1.e-4 2009 .ve 2010 2011 2012 Level: advanced 2013 2014 .seealso: TaoAppendOptionsPrefix(), TaoGetOptionsPrefix() 2015 @*/ 2016 2017 PetscErrorCode TaoSetOptionsPrefix(Tao tao, const char p[]) 2018 { 2019 PetscErrorCode ierr; 2020 2021 PetscFunctionBegin; 2022 ierr = PetscObjectSetOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr); 2023 if (tao->linesearch) { 2024 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr); 2025 } 2026 if (tao->ksp) { 2027 ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr); 2028 } 2029 PetscFunctionReturn(0); 2030 } 2031 2032 #undef __FUNCT__ 2033 #define __FUNCT__ "TaoAppendOptionsPrefix" 2034 /*@C 2035 TaoAppendOptionsPrefix - Appends to the prefix used for searching for all 2036 TAO options in the database. 2037 2038 2039 Logically Collective on Tao 2040 2041 Input Parameters: 2042 + tao - the Tao solver context 2043 - prefix - the prefix string to prepend to all TAO option requests 2044 2045 Notes: 2046 A hyphen (-) must NOT be given at the beginning of the prefix name. 2047 The first character of all runtime options is AUTOMATICALLY the hyphen. 2048 2049 2050 Level: advanced 2051 2052 .seealso: TaoSetOptionsPrefix(), TaoGetOptionsPrefix() 2053 @*/ 2054 PetscErrorCode TaoAppendOptionsPrefix(Tao tao, const char p[]) 2055 { 2056 PetscErrorCode ierr; 2057 2058 PetscFunctionBegin; 2059 ierr = PetscObjectAppendOptionsPrefix((PetscObject)tao,p);CHKERRQ(ierr); 2060 if (tao->linesearch) { 2061 ierr = TaoLineSearchSetOptionsPrefix(tao->linesearch,p);CHKERRQ(ierr); 2062 } 2063 if (tao->ksp) { 2064 ierr = KSPSetOptionsPrefix(tao->ksp,p);CHKERRQ(ierr); 2065 } 2066 PetscFunctionReturn(0); 2067 } 2068 2069 #undef __FUNCT__ 2070 #define __FUNCT__ "TaoGetOptionsPrefix" 2071 /*@C 2072 TaoGetOptionsPrefix - Gets the prefix used for searching for all 2073 TAO options in the database 2074 2075 Not Collective 2076 2077 Input Parameters: 2078 . tao - the Tao context 2079 2080 Output Parameters: 2081 . prefix - pointer to the prefix string used is returned 2082 2083 Notes: On the fortran side, the user should pass in a string 'prefix' of 2084 sufficient length to hold the prefix. 2085 2086 Level: advanced 2087 2088 .seealso: TaoSetOptionsPrefix(), TaoAppendOptionsPrefix() 2089 @*/ 2090 PetscErrorCode TaoGetOptionsPrefix(Tao tao, const char *p[]) 2091 { 2092 return PetscObjectGetOptionsPrefix((PetscObject)tao,p); 2093 } 2094 2095 #undef __FUNCT__ 2096 #define __FUNCT__ "TaoSetType" 2097 /*@C 2098 TaoSetType - Sets the method for the unconstrained minimization solver. 2099 2100 Collective on Tao 2101 2102 Input Parameters: 2103 + solver - the Tao solver context 2104 - type - a known method 2105 2106 Options Database Key: 2107 . -tao_type <type> - Sets the method; use -help for a list 2108 of available methods (for instance, "-tao_type lmvm" or "-tao_type tron") 2109 2110 Available methods include: 2111 + nls - Newton's method with line search for unconstrained minimization 2112 . ntr - Newton's method with trust region for unconstrained minimization 2113 . ntl - Newton's method with trust region, line search for unconstrained minimization 2114 . lmvm - Limited memory variable metric method for unconstrained minimization 2115 . cg - Nonlinear conjugate gradient method for unconstrained minimization 2116 . nm - Nelder-Mead algorithm for derivate-free unconstrained minimization 2117 . tron - Newton Trust Region method for bound constrained minimization 2118 . gpcg - Newton Trust Region method for quadratic bound constrained minimization 2119 . blmvm - Limited memory variable metric method for bound constrained minimization 2120 - pounders - Model-based algorithm pounder extended for nonlinear least squares 2121 2122 Level: intermediate 2123 2124 .seealso: TaoCreate(), TaoGetType(), TaoType 2125 2126 @*/ 2127 PetscErrorCode TaoSetType(Tao tao, const TaoType type) 2128 { 2129 PetscErrorCode ierr; 2130 PetscErrorCode (*create_xxx)(Tao); 2131 PetscBool issame; 2132 2133 PetscFunctionBegin; 2134 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2135 2136 ierr = PetscObjectTypeCompare((PetscObject)tao,type,&issame);CHKERRQ(ierr); 2137 if (issame) PetscFunctionReturn(0); 2138 2139 ierr = PetscFunctionListFind(TaoList, type, (void(**)(void))&create_xxx);CHKERRQ(ierr); 2140 if (!create_xxx) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested Tao type %s",type); 2141 2142 /* Destroy the existing solver information */ 2143 if (tao->ops->destroy) { 2144 ierr = (*tao->ops->destroy)(tao);CHKERRQ(ierr); 2145 } 2146 ierr = KSPDestroy(&tao->ksp);CHKERRQ(ierr); 2147 ierr = TaoLineSearchDestroy(&tao->linesearch);CHKERRQ(ierr); 2148 ierr = VecDestroy(&tao->gradient);CHKERRQ(ierr); 2149 ierr = VecDestroy(&tao->stepdirection);CHKERRQ(ierr); 2150 2151 tao->ops->setup = 0; 2152 tao->ops->solve = 0; 2153 tao->ops->view = 0; 2154 tao->ops->setfromoptions = 0; 2155 tao->ops->destroy = 0; 2156 2157 tao->setupcalled = PETSC_FALSE; 2158 2159 ierr = (*create_xxx)(tao);CHKERRQ(ierr); 2160 ierr = PetscObjectChangeTypeName((PetscObject)tao,type);CHKERRQ(ierr); 2161 PetscFunctionReturn(0); 2162 } 2163 2164 #undef __FUNCT__ 2165 #define __FUNCT__ "TaoRegister" 2166 /*MC 2167 TaoRegister - Adds a method to the TAO package for unconstrained minimization. 2168 2169 Synopsis: 2170 TaoRegister(char *name_solver,char *path,char *name_Create,int (*routine_Create)(Tao)) 2171 2172 Not collective 2173 2174 Input Parameters: 2175 + sname - name of a new user-defined solver 2176 - func - routine to Create method context 2177 2178 Notes: 2179 TaoRegister() may be called multiple times to add several user-defined solvers. 2180 2181 Sample usage: 2182 .vb 2183 TaoRegister("my_solver",MySolverCreate); 2184 .ve 2185 2186 Then, your solver can be chosen with the procedural interface via 2187 $ TaoSetType(tao,"my_solver") 2188 or at runtime via the option 2189 $ -tao_type my_solver 2190 2191 Level: advanced 2192 2193 .seealso: TaoRegisterAll(), TaoRegisterDestroy() 2194 M*/ 2195 PetscErrorCode TaoRegister(const char sname[], PetscErrorCode (*func)(Tao)) 2196 { 2197 PetscErrorCode ierr; 2198 2199 PetscFunctionBegin; 2200 ierr = PetscFunctionListAdd(&TaoList,sname, (void (*)(void))func);CHKERRQ(ierr); 2201 PetscFunctionReturn(0); 2202 } 2203 2204 #undef __FUNCT__ 2205 #define __FUNCT__ "TaoRegisterDestroy" 2206 /*@C 2207 TaoRegisterDestroy - Frees the list of minimization solvers that were 2208 registered by TaoRegisterDynamic(). 2209 2210 Not Collective 2211 2212 Level: advanced 2213 2214 .seealso: TaoRegisterAll(), TaoRegister() 2215 @*/ 2216 PetscErrorCode TaoRegisterDestroy(void) 2217 { 2218 PetscErrorCode ierr; 2219 PetscFunctionBegin; 2220 ierr = PetscFunctionListDestroy(&TaoList);CHKERRQ(ierr); 2221 TaoRegisterAllCalled = PETSC_FALSE; 2222 PetscFunctionReturn(0); 2223 } 2224 2225 #undef __FUNCT__ 2226 #define __FUNCT__ "TaoGetIterationNumber" 2227 /*@ 2228 TaoGetIterationNumber - Gets the number of Tao iterations completed 2229 at this time. 2230 2231 Not Collective 2232 2233 Input Parameter: 2234 . tao - Tao context 2235 2236 Output Parameter: 2237 . iter - iteration number 2238 2239 Notes: 2240 For example, during the computation of iteration 2 this would return 1. 2241 2242 2243 Level: intermediate 2244 2245 .keywords: Tao, nonlinear, get, iteration, number, 2246 2247 .seealso: TaoGetLinearSolveIterations() 2248 @*/ 2249 PetscErrorCode TaoGetIterationNumber(Tao tao,PetscInt *iter) 2250 { 2251 PetscFunctionBegin; 2252 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2253 PetscValidIntPointer(iter,2); 2254 *iter = tao->niter; 2255 PetscFunctionReturn(0); 2256 } 2257 2258 #undef __FUNCT__ 2259 #define __FUNCT__ "TaoSetIterationNumber" 2260 /*@ 2261 TaoSetIterationNumber - Sets the current iteration number. 2262 2263 Not Collective 2264 2265 Input Parameter: 2266 . tao - Tao context 2267 . iter - iteration number 2268 2269 Level: developer 2270 2271 .keywords: Tao, nonlinear, set, iteration, number, 2272 2273 .seealso: TaoGetLinearSolveIterations() 2274 @*/ 2275 PetscErrorCode TaoSetIterationNumber(Tao tao,PetscInt iter) 2276 { 2277 PetscErrorCode ierr; 2278 2279 PetscFunctionBegin; 2280 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2281 ierr = PetscObjectSAWsTakeAccess((PetscObject)tao);CHKERRQ(ierr); 2282 tao->niter = iter; 2283 ierr = PetscObjectSAWsGrantAccess((PetscObject)tao);CHKERRQ(ierr); 2284 PetscFunctionReturn(0); 2285 } 2286 2287 #undef __FUNCT__ 2288 #define __FUNCT__ "TaoGetTotalIterationNumber" 2289 /*@ 2290 TaoGetTotalIterationNumber - Gets the total number of Tao iterations 2291 completed. This number keeps accumulating if multiple solves 2292 are called with the Tao object. 2293 2294 Not Collective 2295 2296 Input Parameter: 2297 . tao - Tao context 2298 2299 Output Parameter: 2300 . iter - iteration number 2301 2302 Notes: 2303 The total iteration count is updated after each solve, if there is a current 2304 TaoSolve() in progress then those iterations are not yet counted. 2305 2306 Level: intermediate 2307 2308 .keywords: Tao, nonlinear, get, iteration, number, 2309 2310 .seealso: TaoGetLinearSolveIterations() 2311 @*/ 2312 PetscErrorCode TaoGetTotalIterationNumber(Tao tao,PetscInt *iter) 2313 { 2314 PetscFunctionBegin; 2315 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2316 PetscValidIntPointer(iter,2); 2317 *iter = tao->ntotalits; 2318 PetscFunctionReturn(0); 2319 } 2320 2321 #undef __FUNCT__ 2322 #define __FUNCT__ "TaoSetTotalIterationNumber" 2323 /*@ 2324 TaoSetTotalIterationNumber - Sets the current total iteration number. 2325 2326 Not Collective 2327 2328 Input Parameter: 2329 . tao - Tao context 2330 . iter - iteration number 2331 2332 Level: developer 2333 2334 .keywords: Tao, nonlinear, set, iteration, number, 2335 2336 .seealso: TaoGetLinearSolveIterations() 2337 @*/ 2338 PetscErrorCode TaoSetTotalIterationNumber(Tao tao,PetscInt iter) 2339 { 2340 PetscErrorCode ierr; 2341 2342 PetscFunctionBegin; 2343 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2344 ierr = PetscObjectSAWsTakeAccess((PetscObject)tao);CHKERRQ(ierr); 2345 tao->ntotalits = iter; 2346 ierr = PetscObjectSAWsGrantAccess((PetscObject)tao);CHKERRQ(ierr); 2347 PetscFunctionReturn(0); 2348 } 2349 2350 #undef __FUNCT__ 2351 #define __FUNCT__ "TaoSetConvergedReason" 2352 /*@ 2353 TaoSetConvergedReason - Sets the termination flag on a Tao object 2354 2355 Logically Collective on Tao 2356 2357 Input Parameters: 2358 + tao - the Tao context 2359 - reason - one of 2360 $ TAO_CONVERGED_ATOL (2), 2361 $ TAO_CONVERGED_RTOL (3), 2362 $ TAO_CONVERGED_STEPTOL (4), 2363 $ TAO_CONVERGED_MINF (5), 2364 $ TAO_CONVERGED_USER (6), 2365 $ TAO_DIVERGED_MAXITS (-2), 2366 $ TAO_DIVERGED_NAN (-4), 2367 $ TAO_DIVERGED_MAXFCN (-5), 2368 $ TAO_DIVERGED_LS_FAILURE (-6), 2369 $ TAO_DIVERGED_TR_REDUCTION (-7), 2370 $ TAO_DIVERGED_USER (-8), 2371 $ TAO_CONTINUE_ITERATING (0) 2372 2373 Level: intermediate 2374 2375 @*/ 2376 PetscErrorCode TaoSetConvergedReason(Tao tao, TaoConvergedReason reason) 2377 { 2378 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2379 PetscFunctionBegin; 2380 tao->reason = reason; 2381 PetscFunctionReturn(0); 2382 } 2383 2384 #undef __FUNCT__ 2385 #define __FUNCT__ "TaoGetConvergedReason" 2386 /*@ 2387 TaoGetConvergedReason - Gets the reason the Tao iteration was stopped. 2388 2389 Not Collective 2390 2391 Input Parameter: 2392 . tao - the Tao solver context 2393 2394 Output Parameter: 2395 . reason - one of 2396 $ TAO_CONVERGED_FATOL (1) f(X)-f(X*) <= fatol 2397 $ TAO_CONVERGED_FRTOL (2) |f(X) - f(X*)|/|f(X)| < frtol 2398 $ TAO_CONVERGED_GATOL (3) ||g(X)|| < gatol 2399 $ TAO_CONVERGED_GRTOL (4) ||g(X)|| / f(X) < grtol 2400 $ TAO_CONVERGED_GTTOL (5) ||g(X)|| / ||g(X0)|| < gttol 2401 $ TAO_CONVERGED_STEPTOL (6) step size small 2402 $ TAO_CONVERGED_MINF (7) F < F_min 2403 $ TAO_CONVERGED_USER (8) User defined 2404 $ TAO_DIVERGED_MAXITS (-2) its > maxits 2405 $ TAO_DIVERGED_NAN (-4) Numerical problems 2406 $ TAO_DIVERGED_MAXFCN (-5) fevals > max_funcsals 2407 $ TAO_DIVERGED_LS_FAILURE (-6) line search failure 2408 $ TAO_DIVERGED_TR_REDUCTION (-7) trust region failure 2409 $ TAO_DIVERGED_USER(-8) (user defined) 2410 $ TAO_CONTINUE_ITERATING (0) 2411 2412 where 2413 + X - current solution 2414 . X0 - initial guess 2415 . f(X) - current function value 2416 . f(X*) - true solution (estimated) 2417 . g(X) - current gradient 2418 . its - current iterate number 2419 . maxits - maximum number of iterates 2420 . fevals - number of function evaluations 2421 - max_funcsals - maximum number of function evaluations 2422 2423 Level: intermediate 2424 2425 .seealso: TaoSetConvergenceTest(), TaoSetTolerances() 2426 2427 @*/ 2428 PetscErrorCode TaoGetConvergedReason(Tao tao, TaoConvergedReason *reason) 2429 { 2430 PetscFunctionBegin; 2431 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2432 PetscValidPointer(reason,2); 2433 *reason = tao->reason; 2434 PetscFunctionReturn(0); 2435 } 2436 2437 #undef __FUNCT__ 2438 #define __FUNCT__ "TaoGetSolutionStatus" 2439 /*@ 2440 TaoGetSolutionStatus - Get the current iterate, objective value, 2441 residual, infeasibility, and termination 2442 2443 Not Collective 2444 2445 Input Parameters: 2446 . tao - the Tao context 2447 2448 Output Parameters: 2449 + iterate - the current iterate number (>=0) 2450 . f - the current function value 2451 . gnorm - the square of the gradient norm, duality gap, or other measure indicating distance from optimality. 2452 . cnorm - the infeasibility of the current solution with regard to the constraints. 2453 . xdiff - the step length or trust region radius of the most recent iterate. 2454 - reason - The termination reason, which can equal TAO_CONTINUE_ITERATING 2455 2456 Level: intermediate 2457 2458 Note: 2459 TAO returns the values set by the solvers in the routine TaoMonitor(). 2460 2461 Note: 2462 If any of the output arguments are set to NULL, no corresponding value will be returned. 2463 2464 .seealso: TaoMonitor(), TaoGetConvergedReason() 2465 @*/ 2466 PetscErrorCode TaoGetSolutionStatus(Tao tao, PetscInt *its, PetscReal *f, PetscReal *gnorm, PetscReal *cnorm, PetscReal *xdiff, TaoConvergedReason *reason) 2467 { 2468 PetscFunctionBegin; 2469 if (its) *its=tao->niter; 2470 if (f) *f=tao->fc; 2471 if (gnorm) *gnorm=tao->residual; 2472 if (cnorm) *cnorm=tao->cnorm; 2473 if (reason) *reason=tao->reason; 2474 if (xdiff) *xdiff=tao->step; 2475 PetscFunctionReturn(0); 2476 } 2477 2478 #undef __FUNCT__ 2479 #define __FUNCT__ "TaoGetType" 2480 /*@C 2481 TaoGetType - Gets the current Tao algorithm. 2482 2483 Not Collective 2484 2485 Input Parameter: 2486 . tao - the Tao solver context 2487 2488 Output Parameter: 2489 . type - Tao method 2490 2491 Level: intermediate 2492 2493 @*/ 2494 PetscErrorCode TaoGetType(Tao tao, const TaoType *type) 2495 { 2496 PetscFunctionBegin; 2497 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2498 PetscValidPointer(type,2); 2499 *type=((PetscObject)tao)->type_name; 2500 PetscFunctionReturn(0); 2501 } 2502 2503 #undef __FUNCT__ 2504 #define __FUNCT__ "TaoMonitor" 2505 /*@C 2506 TaoMonitor - Monitor the solver and the current solution. This 2507 routine will record the iteration number and residual statistics, 2508 call any monitors specified by the user, and calls the convergence-check routine. 2509 2510 Input Parameters: 2511 + tao - the Tao context 2512 . its - the current iterate number (>=0) 2513 . f - the current objective function value 2514 . res - the gradient norm, square root of the duality gap, or other measure indicating distince from optimality. This measure will be recorded and 2515 used for some termination tests. 2516 . cnorm - the infeasibility of the current solution with regard to the constraints. 2517 - steplength - multiple of the step direction added to the previous iterate. 2518 2519 Output Parameters: 2520 . reason - The termination reason, which can equal TAO_CONTINUE_ITERATING 2521 2522 Options Database Key: 2523 . -tao_monitor - Use the default monitor, which prints statistics to standard output 2524 2525 .seealso TaoGetConvergedReason(), TaoDefaultMonitor(), TaoSetMonitor() 2526 2527 Level: developer 2528 2529 @*/ 2530 PetscErrorCode TaoMonitor(Tao tao, PetscInt its, PetscReal f, PetscReal res, PetscReal cnorm, PetscReal steplength, TaoConvergedReason *reason) 2531 { 2532 PetscErrorCode ierr; 2533 PetscInt i; 2534 2535 PetscFunctionBegin; 2536 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2537 tao->fc = f; 2538 tao->residual = res; 2539 tao->cnorm = cnorm; 2540 tao->step = steplength; 2541 if (its == 0) { 2542 tao->cnorm0 = cnorm; tao->gnorm0 = res; 2543 } 2544 TaoLogConvergenceHistory(tao,f,res,cnorm,tao->ksp_its); 2545 if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(res)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); 2546 if (tao->ops->convergencetest) { 2547 ierr = (*tao->ops->convergencetest)(tao,tao->cnvP);CHKERRQ(ierr); 2548 } 2549 for (i=0;i<tao->numbermonitors;i++) { 2550 ierr = (*tao->monitor[i])(tao,tao->monitorcontext[i]);CHKERRQ(ierr); 2551 } 2552 *reason = tao->reason; 2553 PetscFunctionReturn(0); 2554 } 2555 2556 #undef __FUNCT__ 2557 #define __FUNCT__ "TaoSetConvergenceHistory" 2558 /*@ 2559 TaoSetConvergenceHistory - Sets the array used to hold the convergence history. 2560 2561 Logically Collective on Tao 2562 2563 Input Parameters: 2564 + tao - the Tao solver context 2565 . obj - array to hold objective value history 2566 . resid - array to hold residual history 2567 . cnorm - array to hold constraint violation history 2568 . lits - integer array holds the number of linear iterations for each Tao iteration 2569 . na - size of obj, resid, and cnorm 2570 - reset - PetscTrue indicates each new minimization resets the history counter to zero, 2571 else it continues storing new values for new minimizations after the old ones 2572 2573 Notes: 2574 If set, TAO will fill the given arrays with the indicated 2575 information at each iteration. If 'obj','resid','cnorm','lits' are 2576 *all* NULL then space (using size na, or 1000 if na is PETSC_DECIDE or 2577 PETSC_DEFAULT) is allocated for the history. 2578 If not all are NULL, then only the non-NULL information categories 2579 will be stored, the others will be ignored. 2580 2581 Any convergence information after iteration number 'na' will not be stored. 2582 2583 This routine is useful, e.g., when running a code for purposes 2584 of accurate performance monitoring, when no I/O should be done 2585 during the section of code that is being timed. 2586 2587 Level: intermediate 2588 2589 .seealso: TaoGetConvergenceHistory() 2590 2591 @*/ 2592 PetscErrorCode TaoSetConvergenceHistory(Tao tao, PetscReal *obj, PetscReal *resid, PetscReal *cnorm, PetscInt *lits, PetscInt na,PetscBool reset) 2593 { 2594 PetscErrorCode ierr; 2595 2596 PetscFunctionBegin; 2597 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2598 if (obj) PetscValidScalarPointer(obj,2); 2599 if (resid) PetscValidScalarPointer(resid,3); 2600 if (cnorm) PetscValidScalarPointer(cnorm,4); 2601 if (lits) PetscValidIntPointer(lits,5); 2602 2603 if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2604 if (!obj && !resid && !cnorm && !lits) { 2605 ierr = PetscCalloc1(na,&obj);CHKERRQ(ierr); 2606 ierr = PetscCalloc1(na,&resid);CHKERRQ(ierr); 2607 ierr = PetscCalloc1(na,&cnorm);CHKERRQ(ierr); 2608 ierr = PetscCalloc1(na,&lits);CHKERRQ(ierr); 2609 tao->hist_malloc=PETSC_TRUE; 2610 } 2611 2612 tao->hist_obj = obj; 2613 tao->hist_resid = resid; 2614 tao->hist_cnorm = cnorm; 2615 tao->hist_lits = lits; 2616 tao->hist_max = na; 2617 tao->hist_reset = reset; 2618 tao->hist_len = 0; 2619 PetscFunctionReturn(0); 2620 } 2621 2622 #undef __FUNCT__ 2623 #define __FUNCT__ "TaoGetConvergenceHistory" 2624 /*@C 2625 TaoGetConvergenceHistory - Gets the arrays used to hold the convergence history. 2626 2627 Collective on Tao 2628 2629 Input Parameter: 2630 . tao - the Tao context 2631 2632 Output Parameters: 2633 + obj - array used to hold objective value history 2634 . resid - array used to hold residual history 2635 . cnorm - array used to hold constraint violation history 2636 . lits - integer array used to hold linear solver iteration count 2637 - nhist - size of obj, resid, cnorm, and lits (will be less than or equal to na given in TaoSetHistory) 2638 2639 Notes: 2640 This routine must be preceded by calls to TaoSetConvergenceHistory() 2641 and TaoSolve(), otherwise it returns useless information. 2642 2643 The calling sequence for this routine in Fortran is 2644 $ call TaoGetConvergenceHistory(Tao tao, PetscInt nhist, PetscErrorCode ierr) 2645 2646 This routine is useful, e.g., when running a code for purposes 2647 of accurate performance monitoring, when no I/O should be done 2648 during the section of code that is being timed. 2649 2650 Level: advanced 2651 2652 .seealso: TaoSetConvergenceHistory() 2653 2654 @*/ 2655 PetscErrorCode TaoGetConvergenceHistory(Tao tao, PetscReal **obj, PetscReal **resid, PetscReal **cnorm, PetscInt **lits, PetscInt *nhist) 2656 { 2657 PetscFunctionBegin; 2658 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2659 if (obj) *obj = tao->hist_obj; 2660 if (cnorm) *cnorm = tao->hist_cnorm; 2661 if (resid) *resid = tao->hist_resid; 2662 if (nhist) *nhist = tao->hist_len; 2663 PetscFunctionReturn(0); 2664 } 2665 2666 #undef __FUNCT__ 2667 #define __FUNCT__ "TaoSetApplicationContext" 2668 /*@ 2669 TaoSetApplicationContext - Sets the optional user-defined context for 2670 a solver. 2671 2672 Logically Collective on Tao 2673 2674 Input Parameters: 2675 + tao - the Tao context 2676 - usrP - optional user context 2677 2678 Level: intermediate 2679 2680 .seealso: TaoGetApplicationContext(), TaoSetApplicationContext() 2681 @*/ 2682 PetscErrorCode TaoSetApplicationContext(Tao tao,void *usrP) 2683 { 2684 PetscFunctionBegin; 2685 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2686 tao->user = usrP; 2687 PetscFunctionReturn(0); 2688 } 2689 2690 #undef __FUNCT__ 2691 #define __FUNCT__ "TaoGetApplicationContext" 2692 /*@ 2693 TaoGetApplicationContext - Gets the user-defined context for a 2694 TAO solvers. 2695 2696 Not Collective 2697 2698 Input Parameter: 2699 . tao - Tao context 2700 2701 Output Parameter: 2702 . usrP - user context 2703 2704 Level: intermediate 2705 2706 .seealso: TaoSetApplicationContext() 2707 @*/ 2708 PetscErrorCode TaoGetApplicationContext(Tao tao,void *usrP) 2709 { 2710 PetscFunctionBegin; 2711 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 2712 *(void**)usrP = tao->user; 2713 PetscFunctionReturn(0); 2714 } 2715