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