1 #include <petsc-private/taoimpl.h> /*I "petsctao.h" I*/ 2 3 #undef __FUNCT__ 4 #define __FUNCT__ "TaoSetHessianRoutine" 5 /*@C 6 TaoSetHessianRoutine - Sets the function to compute the Hessian as well as the location to store the matrix. 7 8 Logically collective on Tao 9 10 Input Parameters: 11 + tao - the Tao context 12 . H - Matrix used for the hessian 13 . Hpre - Matrix that will be used operated on by preconditioner, can be same as H 14 . hess - Hessian evaluation routine 15 - ctx - [optional] user-defined context for private data for the 16 Hessian evaluation routine (may be NULL) 17 18 Calling sequence of hess: 19 $ hess (Tao tao,Vec x,Mat H,Mat Hpre,MatStructure *flag,void *ctx); 20 21 + tao - the Tao context 22 . x - input vector 23 . H - Hessian matrix 24 . Hpre - preconditioner matrix, usually the same as H 25 . flag - flag indicating information about the preconditioner matrix 26 structure (see below) 27 - ctx - [optional] user-defined Hessian context 28 29 30 Notes: 31 32 The flag can be used to eliminate unnecessary work in the preconditioner 33 during the repeated solution of linear systems of the same size. The 34 available options are 35 $ SAME_PRECONDITIONER - 36 $ Hpre is identical during successive linear solves. 37 $ This option is intended for folks who are using 38 $ different Amat and Pmat matrices and want to reuse the 39 $ same preconditioner matrix. For example, this option 40 $ saves work by not recomputing incomplete factorization 41 $ for ILU/ICC preconditioners. 42 $ SAME_NONZERO_PATTERN - 43 $ Hpre has the same nonzero structure during 44 $ successive linear solves. 45 $ DIFFERENT_NONZERO_PATTERN - 46 $ Hpre does not have the same nonzero structure. 47 48 Caution: 49 If you specify SAME_NONZERO_PATTERN, the software believes your assertion 50 and does not check the structure of the matrix. If you erroneously 51 claim that the structure is the same when it actually is not, the new 52 preconditioner will not function correctly. Thus, use this optimization 53 feature carefully! 54 55 If in doubt about whether your preconditioner matrix has changed 56 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 57 58 Level: beginner 59 60 @*/ 61 PetscErrorCode TaoSetHessianRoutine(Tao tao, Mat H, Mat Hpre, PetscErrorCode (*func)(Tao, Vec, Mat, Mat , MatStructure *, void*), void *ctx) 62 { 63 PetscErrorCode ierr; 64 65 PetscFunctionBegin; 66 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 67 if (H) { 68 PetscValidHeaderSpecific(H,MAT_CLASSID,2); 69 PetscCheckSameComm(tao,1,H,2); 70 } 71 if (Hpre) { 72 PetscValidHeaderSpecific(Hpre,MAT_CLASSID,3); 73 PetscCheckSameComm(tao,1,Hpre,3); 74 } 75 if (ctx) { 76 tao->user_hessP = ctx; 77 } 78 if (func) { 79 tao->ops->computehessian = func; 80 } 81 if (H) { 82 ierr = PetscObjectReference((PetscObject)H);CHKERRQ(ierr); 83 ierr = MatDestroy(&tao->hessian);CHKERRQ(ierr); 84 tao->hessian = H; 85 } 86 if (Hpre) { 87 ierr = PetscObjectReference((PetscObject)Hpre);CHKERRQ(ierr); 88 ierr = MatDestroy(&tao->hessian_pre);CHKERRQ(ierr); 89 tao->hessian_pre = Hpre; 90 } 91 PetscFunctionReturn(0); 92 } 93 94 #undef __FUNCT__ 95 #define __FUNCT__ "TaoComputeHessian" 96 /*@C 97 TaoComputeHessian - Computes the Hessian matrix that has been 98 set with TaoSetHessianRoutine(). 99 100 Collective on Tao 101 102 Input Parameters: 103 + solver - the Tao solver context 104 - xx - input vector 105 106 Output Parameters: 107 + H - Hessian matrix 108 . Hpre - Preconditioning matrix 109 - flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER) 110 111 Notes: 112 Most users should not need to explicitly call this routine, as it 113 is used internally within the minimization solvers. 114 115 TaoComputeHessian() is typically used within minimization 116 implementations, so most users would not generally call this routine 117 themselves. 118 119 Level: developer 120 121 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetHessian() 122 123 @*/ 124 PetscErrorCode TaoComputeHessian(Tao tao, Vec X, Mat H, Mat Hpre, MatStructure *flg) 125 { 126 PetscErrorCode ierr; 127 128 PetscFunctionBegin; 129 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 130 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 131 PetscValidPointer(flg,5); 132 PetscCheckSameComm(tao,1,X,2); 133 134 if (!tao->ops->computehessian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetHessian() first"); 135 *flg = DIFFERENT_NONZERO_PATTERN; 136 ++tao->nhess; 137 ierr = PetscLogEventBegin(Tao_HessianEval,tao,X,H,Hpre);CHKERRQ(ierr); 138 PetscStackPush("Tao user Hessian function"); 139 ierr = (*tao->ops->computehessian)(tao,X,H,Hpre,flg,tao->user_hessP);CHKERRQ(ierr); 140 PetscStackPop; 141 ierr = PetscLogEventEnd(Tao_HessianEval,tao,X,H,Hpre);CHKERRQ(ierr); 142 PetscFunctionReturn(0); 143 } 144 145 #undef __FUNCT__ 146 #define __FUNCT__ "TaoComputeJacobian" 147 /*@C 148 TaoComputeJacobian - Computes the Jacobian matrix that has been 149 set with TaoSetJacobianRoutine(). 150 151 Collective on Tao 152 153 Input Parameters: 154 + solver - the Tao solver context 155 - xx - input vector 156 157 Output Parameters: 158 + H - Jacobian matrix 159 . Hpre - Preconditioning matrix 160 - flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER) 161 162 Notes: 163 Most users should not need to explicitly call this routine, as it 164 is used internally within the minimization solvers. 165 166 TaoComputeJacobian() is typically used within minimization 167 implementations, so most users would not generally call this routine 168 themselves. 169 170 Level: developer 171 172 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobian() 173 174 @*/ 175 PetscErrorCode TaoComputeJacobian(Tao tao, Vec X, Mat J, Mat Jpre, MatStructure *flg) 176 { 177 PetscErrorCode ierr; 178 179 PetscFunctionBegin; 180 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 181 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 182 PetscValidPointer(flg,5); 183 PetscCheckSameComm(tao,1,X,2); 184 185 if (!tao->ops->computejacobian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobian() first"); 186 *flg = DIFFERENT_NONZERO_PATTERN; 187 ++tao->njac; 188 ierr = PetscLogEventBegin(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 189 PetscStackPush("Tao user Jacobian function"); 190 ierr = (*tao->ops->computejacobian)(tao,X,J,Jpre,flg,tao->user_jacP);CHKERRQ(ierr); 191 PetscStackPop; 192 ierr = PetscLogEventEnd(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 193 PetscFunctionReturn(0); 194 } 195 196 #undef __FUNCT__ 197 #define __FUNCT__ "TaoComputeJacobianState" 198 /*@C 199 TaoComputeJacobianState - Computes the Jacobian matrix that has been 200 set with TaoSetJacobianStateRoutine(). 201 202 Collective on Tao 203 204 Input Parameters: 205 + solver - the Tao solver context 206 - xx - input vector 207 208 Output Parameters: 209 + H - Jacobian matrix 210 . Hpre - Preconditioning matrix 211 - flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER) 212 213 Notes: 214 Most users should not need to explicitly call this routine, as it 215 is used internally within the minimization solvers. 216 217 TaoComputeJacobianState() is typically used within minimization 218 implementations, so most users would not generally call this routine 219 themselves. 220 221 Level: developer 222 223 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS() 224 225 @*/ 226 PetscErrorCode TaoComputeJacobianState(Tao tao, Vec X, Mat J, Mat Jpre, Mat Jinv, MatStructure *flg) 227 { 228 PetscErrorCode ierr; 229 230 PetscFunctionBegin; 231 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 232 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 233 PetscValidPointer(flg,5); 234 PetscCheckSameComm(tao,1,X,2); 235 236 if (!tao->ops->computejacobianstate) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianState() first"); 237 *flg = DIFFERENT_NONZERO_PATTERN; 238 ++tao->njac_state; 239 ierr = PetscLogEventBegin(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 240 PetscStackPush("Tao user Jacobian(state) function"); 241 ierr = (*tao->ops->computejacobianstate)(tao,X,J,Jpre,Jinv,flg,tao->user_jac_stateP);CHKERRQ(ierr); 242 PetscStackPop; 243 ierr = PetscLogEventEnd(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 244 PetscFunctionReturn(0); 245 } 246 247 #undef __FUNCT__ 248 #define __FUNCT__ "TaoComputeJacobianDesign" 249 /*@C 250 TaoComputeJacobianDesign - Computes the Jacobian matrix that has been 251 set with TaoSetJacobianDesignRoutine(). 252 253 Collective on Tao 254 255 Input Parameters: 256 + solver - the Tao solver context 257 - xx - input vector 258 259 Output Parameters: 260 . H - Jacobian matrix 261 262 Notes: 263 Most users should not need to explicitly call this routine, as it 264 is used internally within the minimization solvers. 265 266 TaoComputeJacobianDesign() is typically used within minimization 267 implementations, so most users would not generally call this routine 268 themselves. 269 270 Level: developer 271 272 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianDesignRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS() 273 274 @*/ 275 PetscErrorCode TaoComputeJacobianDesign(Tao tao, Vec X, Mat J) 276 { 277 PetscErrorCode ierr; 278 279 PetscFunctionBegin; 280 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 281 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 282 PetscCheckSameComm(tao,1,X,2); 283 284 if (!tao->ops->computejacobiandesign) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianDesign() first"); 285 ++tao->njac_design; 286 ierr = PetscLogEventBegin(Tao_JacobianEval,tao,X,J,NULL);CHKERRQ(ierr); 287 PetscStackPush("Tao user Jacobian(design) function"); 288 ierr = (*tao->ops->computejacobiandesign)(tao,X,J,tao->user_jac_designP);CHKERRQ(ierr); 289 PetscStackPop; 290 ierr = PetscLogEventEnd(Tao_JacobianEval,tao,X,J,NULL);CHKERRQ(ierr); 291 PetscFunctionReturn(0); 292 } 293 294 #undef __FUNCT__ 295 #define __FUNCT__ "TaoSetJacobianRoutine" 296 /*@C 297 TaoSetJacobianRoutine - Sets the function to compute the Jacobian as well as the location to store the matrix. 298 299 Logically collective on Tao 300 301 Input Parameters: 302 + tao - the Tao context 303 . J - Matrix used for the jacobian 304 . Jpre - Matrix that will be used operated on by preconditioner, can be same as J 305 . jac - Jacobian evaluation routine 306 - ctx - [optional] user-defined context for private data for the 307 Jacobian evaluation routine (may be NULL) 308 309 Calling sequence of jac: 310 $ jac (Tao tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx); 311 312 + tao - the Tao context 313 . x - input vector 314 . J - Jacobian matrix 315 . Jpre - preconditioner matrix, usually the same as J 316 . flag - flag indicating information about the preconditioner matrix 317 structure (see below) 318 - ctx - [optional] user-defined Jacobian context 319 320 Notes: 321 322 The function jac() takes Mat * as the matrix arguments rather than Mat. 323 This allows the Jacobian evaluation routine to replace A and/or B with a 324 completely new new matrix structure (not just different matrix elements) 325 when appropriate, for instance, if the nonzero structure is changing 326 throughout the global iterations. 327 328 The flag can be used to eliminate unnecessary work in the preconditioner 329 during the repeated solution of linear systems of the same size. The 330 available options are 331 $ SAME_PRECONDITIONER - 332 $ Jpre is identical during successive linear solves. 333 $ This option is intended for folks who are using 334 $ different Amat and Pmat matrices and want to reuse the 335 $ same preconditioner matrix. For example, this option 336 $ saves work by not recomputing incomplete factorization 337 $ for ILU/ICC preconditioners. 338 $ SAME_NONZERO_PATTERN - 339 $ Jpre has the same nonzero structure during 340 $ successive linear solves. 341 $ DIFFERENT_NONZERO_PATTERN - 342 $ Jpre does not have the same nonzero structure. 343 344 Caution: 345 If you specify SAME_NONZERO_PATTERN, the software believes your assertion 346 and does not check the structure of the matrix. If you erroneously 347 claim that the structure is the same when it actually is not, the new 348 preconditioner will not function correctly. Thus, use this optimization 349 feature carefully! 350 351 If in doubt about whether your preconditioner matrix has changed 352 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 353 354 Level: intermediate 355 356 @*/ 357 PetscErrorCode TaoSetJacobianRoutine(Tao tao, Mat J, Mat Jpre, PetscErrorCode (*func)(Tao, Vec, Mat, Mat, MatStructure *, void*), void *ctx) 358 { 359 PetscErrorCode ierr; 360 PetscFunctionBegin; 361 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 362 if (J) { 363 PetscValidHeaderSpecific(J,MAT_CLASSID,2); 364 PetscCheckSameComm(tao,1,J,2); 365 } 366 if (Jpre) { 367 PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3); 368 PetscCheckSameComm(tao,1,Jpre,3); 369 } 370 if (ctx) { 371 tao->user_jacP = ctx; 372 } 373 if (func) { 374 tao->ops->computejacobian = func; 375 } 376 if (J) { 377 ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 378 ierr = MatDestroy(&tao->jacobian);CHKERRQ(ierr); 379 tao->jacobian = J; 380 } 381 if (Jpre) { 382 ierr = PetscObjectReference((PetscObject)Jpre);CHKERRQ(ierr); 383 ierr = MatDestroy(&tao->jacobian_pre);CHKERRQ(ierr); 384 tao->jacobian_pre=Jpre; 385 } 386 PetscFunctionReturn(0); 387 } 388 389 #undef __FUNCT__ 390 #define __FUNCT__ "TaoSetJacobianStateRoutine" 391 /*@C 392 TaoSetJacobianStateRoutine - Sets the function to compute the Jacobian 393 (and its inverse) of the constraint function with respect to the state variables. 394 Used only for pde-constrained optimization. 395 396 Logically collective on Tao 397 398 Input Parameters: 399 + tao - the Tao context 400 . J - Matrix used for the jacobian 401 . Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J. Only used if Jinv is NULL 402 . Jinv - [optional] Matrix used to apply the inverse of the state jacobian. Use NULL to default to PETSc KSP solvers to apply the inverse. 403 . jac - Jacobian evaluation routine 404 - ctx - [optional] user-defined context for private data for the 405 Jacobian evaluation routine (may be NULL) 406 407 Calling sequence of jac: 408 $ jac (Tao tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx); 409 410 + tao - the Tao context 411 . x - input vector 412 . J - Jacobian matrix 413 . Jpre - preconditioner matrix, usually the same as J 414 . Jinv - inverse of J 415 . flag - flag indicating information about the preconditioner matrix 416 structure (see below) 417 - ctx - [optional] user-defined Jacobian context 418 419 420 Notes: 421 Because of the structure of the jacobian matrix, 422 It may be more efficient for a pde-constrained application to provide 423 its own Jinv matrix. 424 425 The function jac() takes Mat * as the matrix arguments rather than Mat. 426 This allows the Jacobian evaluation routine to replace A and/or B with a 427 completely new new maitrix structure (not just different matrix elements) 428 when appropriate, for instance, if the nonzero structure is changing 429 throughout the global iterations. 430 431 The flag can be used to eliminate unnecessary work in the preconditioner 432 during the repeated solution of linear systems of the same size. The 433 available options are 434 $ SAME_PRECONDITIONER - 435 $ Jpre is identical during successive linear solves. 436 $ This option is intended for folks who are using 437 $ different Amat and Pmat matrices and want to reuse the 438 $ same preconditioner matrix. For example, this option 439 $ saves work by not recomputing incomplete factorization 440 $ for ILU/ICC preconditioners. 441 $ SAME_NONZERO_PATTERN - 442 $ Jpre has the same nonzero structure during 443 $ successive linear solves. 444 $ DIFFERENT_NONZERO_PATTERN - 445 $ Jpre does not have the same nonzero structure. 446 447 Caution: 448 If you specify SAME_NONZERO_PATTERN, the software believes your assertion 449 and does not check the structure of the matrix. If you erroneously 450 claim that the structure is the same when it actually is not, the new 451 preconditioner will not function correctly. Thus, use this optimization 452 feature carefully! 453 454 If in doubt about whether your preconditioner matrix has changed 455 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 456 457 Level: intermediate 458 .seealse: TaoComputeJacobianState(), TaoSetJacobianDesignRoutine(), TaoSetStateDesignIS() 459 @*/ 460 PetscErrorCode TaoSetJacobianStateRoutine(Tao tao, Mat J, Mat Jpre, Mat Jinv, PetscErrorCode (*func)(Tao, Vec, Mat, Mat, Mat, MatStructure *, void*), void *ctx) 461 { 462 PetscErrorCode ierr; 463 PetscFunctionBegin; 464 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 465 if (J) { 466 PetscValidHeaderSpecific(J,MAT_CLASSID,2); 467 PetscCheckSameComm(tao,1,J,2); 468 } 469 if (Jpre) { 470 PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3); 471 PetscCheckSameComm(tao,1,Jpre,3); 472 } 473 if (Jinv) { 474 PetscValidHeaderSpecific(Jinv,MAT_CLASSID,4); 475 PetscCheckSameComm(tao,1,Jinv,4); 476 } 477 if (ctx) { 478 tao->user_jac_stateP = ctx; 479 } 480 if (func) { 481 tao->ops->computejacobianstate = func; 482 } 483 if (J) { 484 ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 485 ierr = MatDestroy(&tao->jacobian_state);CHKERRQ(ierr); 486 tao->jacobian_state = J; 487 } 488 if (Jpre) { 489 ierr = PetscObjectReference((PetscObject)Jpre);CHKERRQ(ierr); 490 ierr = MatDestroy(&tao->jacobian_state_pre);CHKERRQ(ierr); 491 tao->jacobian_state_pre=Jpre; 492 } 493 if (Jinv) { 494 ierr = PetscObjectReference((PetscObject)Jinv);CHKERRQ(ierr); 495 ierr = MatDestroy(&tao->jacobian_state_inv);CHKERRQ(ierr); 496 tao->jacobian_state_inv=Jinv; 497 } 498 PetscFunctionReturn(0); 499 } 500 501 #undef __FUNCT__ 502 #define __FUNCT__ "TaoSetJacobianDesignRoutine" 503 /*@C 504 TaoSetJacobianDesignRoutine - Sets the function to compute the Jacobian of 505 the constraint function with respect to the design variables. Used only for 506 pde-constrained optimization. 507 508 Logically collective on Tao 509 510 Input Parameters: 511 + tao - the Tao context 512 . J - Matrix used for the jacobian 513 . jac - Jacobian evaluation routine 514 - ctx - [optional] user-defined context for private data for the 515 Jacobian evaluation routine (may be NULL) 516 517 Calling sequence of jac: 518 $ jac (Tao tao,Vec x,Mat *J,void *ctx); 519 520 + tao - the Tao context 521 . x - input vector 522 . J - Jacobian matrix 523 - ctx - [optional] user-defined Jacobian context 524 525 526 Notes: 527 528 The function jac() takes Mat * as the matrix arguments rather than Mat. 529 This allows the Jacobian evaluation routine to replace A and/or B with a 530 completely new new matrix structure (not just different matrix elements) 531 when appropriate, for instance, if the nonzero structure is changing 532 throughout the global iterations. 533 534 Level: intermediate 535 .seealso: TaoComputeJacobianDesign(), TaoSetJacobianStateRoutine(), TaoSetStateDesignIS() 536 @*/ 537 PetscErrorCode TaoSetJacobianDesignRoutine(Tao tao, Mat J, PetscErrorCode (*func)(Tao, Vec, Mat, void*), void *ctx) 538 { 539 PetscErrorCode ierr; 540 541 PetscFunctionBegin; 542 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 543 if (J) { 544 PetscValidHeaderSpecific(J,MAT_CLASSID,2); 545 PetscCheckSameComm(tao,1,J,2); 546 } 547 if (ctx) { 548 tao->user_jac_designP = ctx; 549 } 550 if (func) { 551 tao->ops->computejacobiandesign = func; 552 } 553 if (J) { 554 ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 555 ierr = MatDestroy(&tao->jacobian_design);CHKERRQ(ierr); 556 tao->jacobian_design = J; 557 } 558 PetscFunctionReturn(0); 559 } 560 561 #undef __FUNCT__ 562 #define __FUNCT__ "TaoSetStateDesignIS" 563 /*@ 564 TaoSetStateDesignIS - Indicate to the Tao which variables in the 565 solution vector are state variables and which are design. Only applies to 566 pde-constrained optimization. 567 568 Logically Collective on Tao 569 570 Input Parameters: 571 + tao - The Tao context 572 . s_is - the index set corresponding to the state variables 573 - d_is - the index set corresponding to the design variables 574 575 Level: intermediate 576 577 .seealso: TaoSetJacobianStateRoutine(), TaoSetJacobianDesignRoutine() 578 @*/ 579 PetscErrorCode TaoSetStateDesignIS(Tao tao, IS s_is, IS d_is) 580 { 581 PetscErrorCode ierr; 582 583 PetscFunctionBegin; 584 ierr = PetscObjectReference((PetscObject)s_is);CHKERRQ(ierr); 585 ierr = ISDestroy(&tao->state_is);CHKERRQ(ierr); 586 tao->state_is = s_is; 587 ierr = PetscObjectReference((PetscObject)(d_is));CHKERRQ(ierr); 588 ierr = ISDestroy(&tao->design_is);CHKERRQ(ierr); 589 tao->design_is = d_is; 590 PetscFunctionReturn(0); 591 } 592 593 #undef __FUNCT__ 594 #define __FUNCT__ "TaoComputeJacobianEquality" 595 /*@C 596 TaoComputeJacobianEquality - Computes the Jacobian matrix that has been 597 set with TaoSetJacobianEqualityRoutine(). 598 599 Collective on Tao 600 601 Input Parameters: 602 + solver - the Tao solver context 603 - xx - input vector 604 605 Output Parameters: 606 + H - Jacobian matrix 607 . Hpre - Preconditioning matrix 608 - flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER) 609 610 Notes: 611 Most users should not need to explicitly call this routine, as it 612 is used internally within the minimization solvers. 613 614 Level: developer 615 616 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS() 617 618 @*/ 619 PetscErrorCode TaoComputeJacobianEquality(Tao tao, Vec X, Mat J, Mat Jpre, MatStructure *flg) 620 { 621 PetscErrorCode ierr; 622 623 PetscFunctionBegin; 624 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 625 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 626 PetscValidPointer(flg,5); 627 PetscCheckSameComm(tao,1,X,2); 628 629 if (!tao->ops->computejacobianequality) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianEquality() first"); 630 *flg = DIFFERENT_NONZERO_PATTERN; 631 ++tao->njac_equality; 632 ierr = PetscLogEventBegin(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 633 PetscStackPush("Tao user Jacobian(equality) function"); 634 ierr = (*tao->ops->computejacobianequality)(tao,X,J,Jpre,flg,tao->user_jac_equalityP);CHKERRQ(ierr); 635 PetscStackPop; 636 ierr = PetscLogEventEnd(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 637 PetscFunctionReturn(0); 638 } 639 640 #undef __FUNCT__ 641 #define __FUNCT__ "TaoComputeJacobianInequality" 642 /*@C 643 TaoComputeJacobianInequality - Computes the Jacobian matrix that has been 644 set with TaoSetJacobianInequalityRoutine(). 645 646 Collective on Tao 647 648 Input Parameters: 649 + solver - the Tao solver context 650 - xx - input vector 651 652 Output Parameters: 653 + H - Jacobian matrix 654 . Hpre - Preconditioning matrix 655 - flag - flag indicating matrix structure (SAME_NONZERO_PATTERN, DIFFERENT_NONZERO_PATTERN, or SAME_PRECONDITIONER) 656 657 Notes: 658 Most users should not need to explicitly call this routine, as it 659 is used internally within the minimization solvers. 660 661 Level: developer 662 663 .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetJacobianStateRoutine(), TaoComputeJacobianDesign(), TaoSetStateDesignIS() 664 665 @*/ 666 PetscErrorCode TaoComputeJacobianInequality(Tao tao, Vec X, Mat J, Mat Jpre, MatStructure *flg) 667 { 668 PetscErrorCode ierr; 669 670 PetscFunctionBegin; 671 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 672 PetscValidHeaderSpecific(X, VEC_CLASSID,2); 673 PetscValidPointer(flg,5); 674 PetscCheckSameComm(tao,1,X,2); 675 676 if (!tao->ops->computejacobianinequality) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TaoSetJacobianInequality() first"); 677 *flg = DIFFERENT_NONZERO_PATTERN; 678 ++tao->njac_inequality; 679 ierr = PetscLogEventBegin(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 680 PetscStackPush("Tao user Jacobian(inequality) function"); 681 ierr = (*tao->ops->computejacobianinequality)(tao,X,J,Jpre,flg,tao->user_jac_inequalityP);CHKERRQ(ierr); 682 PetscStackPop; 683 ierr = PetscLogEventEnd(Tao_JacobianEval,tao,X,J,Jpre);CHKERRQ(ierr); 684 PetscFunctionReturn(0); 685 } 686 687 #undef __FUNCT__ 688 #define __FUNCT__ "TaoSetJacobianEqualityRoutine" 689 /*@C 690 TaoSetJacobianEqualityRoutine - Sets the function to compute the Jacobian 691 (and its inverse) of the constraint function with respect to the equality variables. 692 Used only for pde-constrained optimization. 693 694 Logically collective on Tao 695 696 Input Parameters: 697 + tao - the Tao context 698 . J - Matrix used for the jacobian 699 . Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J. 700 . jac - Jacobian evaluation routine 701 - ctx - [optional] user-defined context for private data for the 702 Jacobian evaluation routine (may be NULL) 703 704 Calling sequence of jac: 705 $ jac (Tao tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx); 706 707 + tao - the Tao context 708 . x - input vector 709 . J - Jacobian matrix 710 . Jpre - preconditioner matrix, usually the same as J 711 . flag - flag indicating information about the preconditioner matrix 712 structure (see below) 713 - ctx - [optional] user-defined Jacobian context 714 715 Notes: 716 Because of the structure of the jacobian matrix, 717 It may be more efficient for a pde-constrained application to provide 718 its own Jinv matrix. 719 720 The function jac() takes Mat * as the matrix arguments rather than Mat. 721 This allows the Jacobian evaluation routine to replace A and/or B with a 722 completely new new maitrix structure (not just different matrix elements) 723 when appropriate, for instance, if the nonzero structure is changing 724 throughout the global iterations. 725 726 The flag can be used to eliminate unnecessary work in the preconditioner 727 during the repeated solution of linear systems of the same size. The 728 available options are 729 $ SAME_PRECONDITIONER - 730 $ Jpre is identical during successive linear solves. 731 $ This option is intended for folks who are using 732 $ different Amat and Pmat matrices and want to reuse the 733 $ same preconditioner matrix. For example, this option 734 $ saves work by not recomputing incomplete factorization 735 $ for ILU/ICC preconditioners. 736 $ SAME_NONZERO_PATTERN - 737 $ Jpre has the same nonzero structure during 738 $ successive linear solves. 739 $ DIFFERENT_NONZERO_PATTERN - 740 $ Jpre does not have the same nonzero structure. 741 742 Caution: 743 If you specify SAME_NONZERO_PATTERN, the software believes your assertion 744 and does not check the structure of the matrix. If you erroneously 745 claim that the structure is the same when it actually is not, the new 746 preconditioner will not function correctly. Thus, use this optimization 747 feature carefully! 748 749 If in doubt about whether your preconditioner matrix has changed 750 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 751 752 Level: intermediate 753 .seealse: TaoComputeJacobianEquality(), TaoSetJacobianDesignRoutine(), TaoSetEqualityDesignIS() 754 @*/ 755 PetscErrorCode TaoSetJacobianEqualityRoutine(Tao tao, Mat J, Mat Jpre, PetscErrorCode (*func)(Tao, Vec, Mat, Mat, MatStructure *, void*), void *ctx) 756 { 757 PetscErrorCode ierr; 758 759 PetscFunctionBegin; 760 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 761 if (J) { 762 PetscValidHeaderSpecific(J,MAT_CLASSID,2); 763 PetscCheckSameComm(tao,1,J,2); 764 } 765 if (Jpre) { 766 PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3); 767 PetscCheckSameComm(tao,1,Jpre,3); 768 } 769 if (ctx) { 770 tao->user_jac_equalityP = ctx; 771 } 772 if (func) { 773 tao->ops->computejacobianequality = func; 774 } 775 if (J) { 776 ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 777 ierr = MatDestroy(&tao->jacobian_equality);CHKERRQ(ierr); 778 tao->jacobian_equality = J; 779 } 780 if (Jpre) { 781 ierr = PetscObjectReference((PetscObject)Jpre);CHKERRQ(ierr); 782 ierr = MatDestroy(&tao->jacobian_equality_pre);CHKERRQ(ierr); 783 tao->jacobian_equality_pre=Jpre; 784 } 785 PetscFunctionReturn(0); 786 } 787 788 #undef __FUNCT__ 789 #define __FUNCT__ "TaoSetJacobianInequalityRoutine" 790 /*@C 791 TaoSetJacobianInequalityRoutine - Sets the function to compute the Jacobian 792 (and its inverse) of the constraint function with respect to the inequality variables. 793 Used only for pde-constrained optimization. 794 795 Logically collective on Tao 796 797 Input Parameters: 798 + tao - the Tao context 799 . J - Matrix used for the jacobian 800 . Jpre - Matrix that will be used operated on by PETSc preconditioner, can be same as J. 801 . jac - Jacobian evaluation routine 802 - ctx - [optional] user-defined context for private data for the 803 Jacobian evaluation routine (may be NULL) 804 805 Calling sequence of jac: 806 $ jac (Tao tao,Vec x,Mat *J,Mat *Jpre,MatStructure *flag,void *ctx); 807 808 + tao - the Tao context 809 . x - input vector 810 . J - Jacobian matrix 811 . Jpre - preconditioner matrix, usually the same as J 812 . flag - flag indicating information about the preconditioner matrix 813 structure (see below) 814 - ctx - [optional] user-defined Jacobian context 815 816 817 Notes: 818 Because of the structure of the jacobian matrix, 819 It may be more efficient for a pde-constrained application to provide 820 its own Jinv matrix. 821 822 The function jac() takes Mat * as the matrix arguments rather than Mat. 823 This allows the Jacobian evaluation routine to replace A and/or B with a 824 completely new new maitrix structure (not just different matrix elements) 825 when appropriate, for instance, if the nonzero structure is changing 826 throughout the global iterations. 827 828 The flag can be used to eliminate unnecessary work in the preconditioner 829 during the repeated solution of linear systems of the same size. The 830 available options are 831 $ SAME_PRECONDITIONER - 832 $ Jpre is identical during successive linear solves. 833 $ This option is intended for folks who are using 834 $ different Amat and Pmat matrices and want to reuse the 835 $ same preconditioner matrix. For example, this option 836 $ saves work by not recomputing incomplete factorization 837 $ for ILU/ICC preconditioners. 838 $ SAME_NONZERO_PATTERN - 839 $ Jpre has the same nonzero structure during 840 $ successive linear solves. 841 $ DIFFERENT_NONZERO_PATTERN - 842 $ Jpre does not have the same nonzero structure. 843 844 Caution: 845 If you specify SAME_NONZERO_PATTERN, the software believes your assertion 846 and does not check the structure of the matrix. If you erroneously 847 claim that the structure is the same when it actually is not, the new 848 preconditioner will not function correctly. Thus, use this optimization 849 feature carefully! 850 851 If in doubt about whether your preconditioner matrix has changed 852 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 853 854 Level: intermediate 855 .seealse: TaoComputeJacobianInequality(), TaoSetJacobianDesignRoutine(), TaoSetInequalityDesignIS() 856 @*/ 857 PetscErrorCode TaoSetJacobianInequalityRoutine(Tao tao, Mat J, Mat Jpre, PetscErrorCode (*func)(Tao, Vec, Mat, Mat, MatStructure *, void*), void *ctx) 858 { 859 PetscErrorCode ierr; 860 PetscFunctionBegin; 861 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 862 if (J) { 863 PetscValidHeaderSpecific(J,MAT_CLASSID,2); 864 PetscCheckSameComm(tao,1,J,2); 865 } 866 if (Jpre) { 867 PetscValidHeaderSpecific(Jpre,MAT_CLASSID,3); 868 PetscCheckSameComm(tao,1,Jpre,3); 869 } 870 if (ctx) { 871 tao->user_jac_inequalityP = ctx; 872 } 873 if (func) { 874 tao->ops->computejacobianinequality = func; 875 } 876 if (J) { 877 ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 878 ierr = MatDestroy(&tao->jacobian_inequality);CHKERRQ(ierr); 879 tao->jacobian_inequality = J; 880 } 881 if (Jpre) { 882 ierr = PetscObjectReference((PetscObject)Jpre);CHKERRQ(ierr); 883 ierr = MatDestroy(&tao->jacobian_inequality_pre);CHKERRQ(ierr); 884 tao->jacobian_inequality_pre=Jpre; 885 } 886 PetscFunctionReturn(0); 887 } 888