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