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