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