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