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