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