1 #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 2 3 #undef __FUNCT__ 4 #define __FUNCT__ "TaoSetVariableBounds" 5 /*@ 6 TaoSetVariableBounds - Sets the upper and lower bounds 7 8 Logically collective on Tao 9 10 Input Parameters: 11 + tao - the Tao context 12 . XL - vector of lower bounds 13 - XU - vector of upper bounds 14 15 Level: beginner 16 17 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine() 18 @*/ 19 20 PetscErrorCode TaoSetVariableBounds(Tao tao, Vec XL, Vec XU) 21 { 22 PetscErrorCode ierr; 23 24 PetscFunctionBegin; 25 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 26 if (XL) { 27 PetscValidHeaderSpecific(XL,VEC_CLASSID,2); 28 PetscObjectReference((PetscObject)XL); 29 } 30 if (XU) { 31 PetscValidHeaderSpecific(XU,VEC_CLASSID,3); 32 PetscObjectReference((PetscObject)XU); 33 } 34 ierr = VecDestroy(&tao->XL);CHKERRQ(ierr); 35 ierr = VecDestroy(&tao->XU);CHKERRQ(ierr); 36 tao->XL = XL; 37 tao->XU = XU; 38 PetscFunctionReturn(0); 39 } 40 41 #undef __FUNCT__ 42 #define __FUNCT__ "TaoSetVariableBoundsRoutine" 43 /*@C 44 TaoSetVariableBoundsRoutine - Sets a function to be used to compute variable bounds 45 46 Logically collective on Tao 47 48 Input Parameters: 49 + tao - the Tao context 50 . func - the bounds computation routine 51 - ctx - [optional] user-defined context for private data for the bounds computation (may be NULL) 52 53 Calling sequence of func: 54 $ func (Tao tao, Vec xl, Vec xu); 55 56 + tao - the Tao 57 . xl - vector of lower bounds 58 . xu - vector of upper bounds 59 - ctx - the (optional) user-defined function context 60 61 Level: beginner 62 63 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds() 64 65 Note: The func passed in to TaoSetVariableBoundsRoutine() takes 66 precedence over any values set in TaoSetVariableBounds(). 67 68 @*/ 69 PetscErrorCode TaoSetVariableBoundsRoutine(Tao tao, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx) 70 { 71 PetscFunctionBegin; 72 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 73 tao->user_boundsP = ctx; 74 tao->ops->computebounds = func; 75 PetscFunctionReturn(0); 76 } 77 78 #undef __FUNCT__ 79 #define __FUNCT__ "TaoGetVariableBounds" 80 PetscErrorCode TaoGetVariableBounds(Tao tao, Vec *XL, Vec *XU) 81 { 82 PetscFunctionBegin; 83 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 84 if (XL) { 85 *XL=tao->XL; 86 } 87 if (XU) { 88 *XU=tao->XU; 89 } 90 PetscFunctionReturn(0); 91 } 92 93 #undef __FUNCT__ 94 #define __FUNCT__ "TaoComputeVariableBounds" 95 /*@C 96 TaoComputeVariableBounds - Compute the variable bounds using the 97 routine set by TaoSetVariableBoundsRoutine(). 98 99 Collective on Tao 100 101 Input Parameters: 102 . tao - the Tao context 103 104 Level: developer 105 106 .seealso: TaoSetVariableBoundsRoutine(), TaoSetVariableBounds() 107 @*/ 108 109 PetscErrorCode TaoComputeVariableBounds(Tao tao) 110 { 111 PetscErrorCode ierr; 112 113 PetscFunctionBegin; 114 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 115 if (!tao->ops->computebounds) PetscFunctionReturn(0); 116 if (!tao->XL || !tao->XU) { 117 if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeVariableBounds"); 118 ierr = VecDuplicate(tao->solution, &tao->XL);CHKERRQ(ierr); 119 ierr = VecSet(tao->XL, PETSC_NINFINITY);CHKERRQ(ierr); 120 ierr = VecDuplicate(tao->solution, &tao->XU);CHKERRQ(ierr); 121 ierr = VecSet(tao->XU, PETSC_INFINITY);CHKERRQ(ierr); 122 } 123 PetscStackPush("Tao compute variable bounds"); 124 ierr = (*tao->ops->computebounds)(tao,tao->XL,tao->XU,tao->user_boundsP);CHKERRQ(ierr); 125 PetscStackPop; 126 PetscFunctionReturn(0); 127 } 128 129 #undef __FUNCT__ 130 #define __FUNCT__ "TaoSetInequalityBounds" 131 /*@ 132 TaoSetInequalityBounds - Sets the upper and lower bounds 133 134 Logically collective on Tao 135 136 Input Parameters: 137 + tao - the Tao context 138 . IL - vector of lower bounds 139 - IU - vector of upper bounds 140 141 Level: beginner 142 143 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine() 144 @*/ 145 146 PetscErrorCode TaoSetInequalityBounds(Tao tao, Vec IL, Vec IU) 147 { 148 PetscErrorCode ierr; 149 150 PetscFunctionBegin; 151 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 152 if (IL) { 153 PetscValidHeaderSpecific(IL,VEC_CLASSID,2); 154 PetscObjectReference((PetscObject)IL); 155 } 156 if (IU) { 157 PetscValidHeaderSpecific(IU,VEC_CLASSID,3); 158 PetscObjectReference((PetscObject)IU); 159 } 160 ierr = VecDestroy(&tao->IL);CHKERRQ(ierr); 161 ierr = VecDestroy(&tao->IU);CHKERRQ(ierr); 162 tao->IL = IL; 163 tao->IU = IU; 164 PetscFunctionReturn(0); 165 } 166 167 168 #undef __FUNCT__ 169 #define __FUNCT__ "TaoGetInequalityBounds" 170 PetscErrorCode TaoGetInequalityBounds(Tao tao, Vec *IL, Vec *IU) 171 { 172 PetscFunctionBegin; 173 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 174 if (IL) { 175 *IL=tao->IL; 176 } 177 if (IU) { 178 *IU=tao->IU; 179 } 180 PetscFunctionReturn(0); 181 } 182 183 #undef __FUNCT__ 184 #define __FUNCT__ "TaoComputeConstraints" 185 /*@C 186 TaoComputeConstraints - Compute the variable bounds using the 187 routine set by TaoSetConstraintsRoutine(). 188 189 Collective on Tao 190 191 Input Parameters: 192 . tao - the Tao context 193 194 Level: developer 195 196 .seealso: TaoSetConstraintsRoutine(), TaoComputeJacobian() 197 @*/ 198 199 PetscErrorCode TaoComputeConstraints(Tao tao, Vec X, Vec C) 200 { 201 PetscErrorCode ierr; 202 203 PetscFunctionBegin; 204 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 205 PetscValidHeaderSpecific(X,VEC_CLASSID,2); 206 PetscValidHeaderSpecific(C,VEC_CLASSID,2); 207 PetscCheckSameComm(tao,1,X,2); 208 PetscCheckSameComm(tao,1,C,3); 209 210 if (!tao->ops->computeconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetConstraintsRoutine() has not been called"); 211 if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeConstraints"); 212 ierr = PetscLogEventBegin(Tao_ConstraintsEval,tao,X,C,NULL);CHKERRQ(ierr); 213 PetscStackPush("Tao constraints evaluation routine"); 214 ierr = (*tao->ops->computeconstraints)(tao,X,C,tao->user_conP);CHKERRQ(ierr); 215 PetscStackPop; 216 ierr = PetscLogEventEnd(Tao_ConstraintsEval,tao,X,C,NULL);CHKERRQ(ierr); 217 tao->nconstraints++; 218 PetscFunctionReturn(0); 219 } 220 221 #undef __FUNCT__ 222 #define __FUNCT__ "TaoSetConstraintsRoutine" 223 /*@C 224 TaoSetConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details 225 226 Logically collective on Tao 227 228 Input Parameters: 229 + tao - the Tao context 230 . c - A vector that will be used to store constraint evaluation 231 . func - the bounds computation routine 232 - ctx - [optional] user-defined context for private data for the constraints computation (may be NULL) 233 234 Calling sequence of func: 235 $ func (Tao tao, Vec x, Vec c, void *ctx); 236 237 + tao - the Tao 238 . x - point to evaluate constraints 239 . c - vector constraints evaluated at x 240 - ctx - the (optional) user-defined function context 241 242 Level: intermediate 243 244 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariablevBounds() 245 246 @*/ 247 PetscErrorCode TaoSetConstraintsRoutine(Tao tao, Vec c, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx) 248 { 249 PetscFunctionBegin; 250 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 251 tao->constraints = c; 252 tao->user_conP = ctx; 253 tao->ops->computeconstraints = func; 254 PetscFunctionReturn(0); 255 } 256 257 #undef __FUNCT__ 258 #define __FUNCT__ "TaoComputeDualVariables" 259 /*@ 260 TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds 261 of the variables 262 263 Collective on Tao 264 265 Input Parameters: 266 . tao - the Tao context 267 268 Output Parameter: 269 + DL - dual variable vector for the lower bounds 270 - DU - dual variable vector for the upper bounds 271 272 Level: advanced 273 274 Note: 275 DL and DU should be created before calling this routine. If calling 276 this routine after using an unconstrained solver, DL and DU are set to all 277 zeros. 278 279 Level: advanced 280 281 .seealso: TaoComputeObjective(), TaoSetVariableBounds() 282 @*/ 283 PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU) 284 { 285 PetscErrorCode ierr; 286 PetscFunctionBegin; 287 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 288 PetscValidHeaderSpecific(DL,VEC_CLASSID,2); 289 PetscValidHeaderSpecific(DU,VEC_CLASSID,2); 290 PetscCheckSameComm(tao,1,DL,2); 291 PetscCheckSameComm(tao,1,DU,3); 292 if (tao->ops->computedual) { 293 ierr = (*tao->ops->computedual)(tao,DL,DU);CHKERRQ(ierr); 294 } else { 295 ierr = VecSet(DL,0.0);CHKERRQ(ierr); 296 ierr = VecSet(DU,0.0);CHKERRQ(ierr); 297 } 298 PetscFunctionReturn(0); 299 } 300 301 #undef __FUNCT__ 302 #define __FUNCT__ "TaoGetDualVariables" 303 /*@ 304 TaoGetDualVariables - Gets pointers to the dual vectors 305 306 Collective on Tao 307 308 Input Parameters: 309 . tao - the Tao context 310 311 Output Parameter: 312 + DE - dual variable vector for the lower bounds 313 - DI - dual variable vector for the upper bounds 314 315 Level: advanced 316 317 .seealso: TaoComputeDualVariables() 318 @*/ 319 PetscErrorCode TaoGetDualVariables(Tao tao, Vec *DE, Vec *DI) 320 { 321 PetscFunctionBegin; 322 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 323 if (DE) { 324 *DE = tao->DE; 325 } 326 if (DI) { 327 *DI = tao->DI; 328 } 329 PetscFunctionReturn(0); 330 } 331 332 #undef __FUNCT__ 333 #define __FUNCT__ "TaoSetEqualityConstraintsRoutine" 334 /*@C 335 TaoSetEqualityConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details 336 337 Logically collective on Tao 338 339 Input Parameters: 340 + tao - the Tao context 341 . ce - A vector that will be used to store equality constraint evaluation 342 . func - the bounds computation routine 343 - ctx - [optional] user-defined context for private data for the equality constraints computation (may be NULL) 344 345 Calling sequence of func: 346 $ func (Tao tao, Vec x, Vec ce, void *ctx); 347 348 + tao - the Tao 349 . x - point to evaluate equality constraints 350 . ce - vector of equality constraints evaluated at x 351 - ctx - the (optional) user-defined function context 352 353 Level: intermediate 354 355 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds() 356 357 @*/ 358 PetscErrorCode TaoSetEqualityConstraintsRoutine(Tao tao, Vec ce, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx) 359 { 360 PetscErrorCode ierr; 361 362 PetscFunctionBegin; 363 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 364 if (ce) { 365 PetscValidHeaderSpecific(ce,VEC_CLASSID,2); 366 PetscObjectReference((PetscObject)ce); 367 } 368 ierr = VecDestroy(&tao->constraints_equality);CHKERRQ(ierr); 369 370 tao->constraints_equality = ce; 371 tao->user_con_equalityP = ctx; 372 tao->ops->computeequalityconstraints = func; 373 PetscFunctionReturn(0); 374 } 375 376 377 #undef __FUNCT__ 378 #define __FUNCT__ "TaoSetInequalityConstraintsRoutine" 379 /*@C 380 TaoSetInequalityConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details 381 382 Logically collective on Tao 383 384 Input Parameters: 385 + tao - the Tao context 386 . ci - A vector that will be used to store inequality constraint evaluation 387 . func - the bounds computation routine 388 - ctx - [optional] user-defined context for private data for the inequality constraints computation (may be NULL) 389 390 Calling sequence of func: 391 $ func (Tao tao, Vec x, Vec ci, void *ctx); 392 393 + tao - the Tao 394 . x - point to evaluate inequality constraints 395 . ci - vector of inequality constraints evaluated at x 396 - ctx - the (optional) user-defined function context 397 398 Level: intermediate 399 400 .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds() 401 402 @*/ 403 PetscErrorCode TaoSetInequalityConstraintsRoutine(Tao tao, Vec ci, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx) 404 { 405 PetscErrorCode ierr; 406 407 PetscFunctionBegin; 408 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 409 if (ci) { 410 PetscValidHeaderSpecific(ci,VEC_CLASSID,2); 411 PetscObjectReference((PetscObject)ci); 412 } 413 ierr = VecDestroy(&tao->constraints_inequality);CHKERRQ(ierr); 414 tao->constraints_inequality = ci; 415 416 tao->user_con_inequalityP = ctx; 417 tao->ops->computeinequalityconstraints = func; 418 PetscFunctionReturn(0); 419 } 420 421 422 #undef __FUNCT__ 423 #define __FUNCT__ "TaoComputeEqualityConstraints" 424 /*@C 425 TaoComputeEqualityConstraints - Compute the variable bounds using the 426 routine set by TaoSetEqualityConstraintsRoutine(). 427 428 Collective on Tao 429 430 Input Parameters: 431 . tao - the Tao context 432 433 Level: developer 434 435 .seealso: TaoSetEqualityConstraintsRoutine(), TaoComputeJacobianEquality() 436 @*/ 437 438 PetscErrorCode TaoComputeEqualityConstraints(Tao tao, Vec X, Vec CE) 439 { 440 PetscErrorCode ierr; 441 442 PetscFunctionBegin; 443 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 444 PetscValidHeaderSpecific(X,VEC_CLASSID,2); 445 PetscValidHeaderSpecific(CE,VEC_CLASSID,2); 446 PetscCheckSameComm(tao,1,X,2); 447 PetscCheckSameComm(tao,1,CE,3); 448 449 if (!tao->ops->computeequalityconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetEqualityConstraintsRoutine() has not been called"); 450 if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeEqualityConstraints"); 451 ierr = PetscLogEventBegin(Tao_ConstraintsEval,tao,X,CE,NULL);CHKERRQ(ierr); 452 PetscStackPush("Tao equality constraints evaluation routine"); 453 ierr = (*tao->ops->computeequalityconstraints)(tao,X,CE,tao->user_con_equalityP);CHKERRQ(ierr); 454 PetscStackPop; 455 ierr = PetscLogEventEnd(Tao_ConstraintsEval,tao,X,CE,NULL);CHKERRQ(ierr); 456 tao->nconstraints++; 457 PetscFunctionReturn(0); 458 } 459 460 461 #undef __FUNCT__ 462 #define __FUNCT__ "TaoComputeInequalityConstraints" 463 /*@C 464 TaoComputeInequalityConstraints - Compute the variable bounds using the 465 routine set by TaoSetInequalityConstraintsRoutine(). 466 467 Collective on Tao 468 469 Input Parameters: 470 . tao - the Tao context 471 472 Level: developer 473 474 .seealso: TaoSetInequalityConstraintsRoutine(), TaoComputeJacobianInequality() 475 @*/ 476 477 PetscErrorCode TaoComputeInequalityConstraints(Tao tao, Vec X, Vec CI) 478 { 479 PetscErrorCode ierr; 480 481 PetscFunctionBegin; 482 PetscValidHeaderSpecific(tao,TAO_CLASSID,1); 483 PetscValidHeaderSpecific(X,VEC_CLASSID,2); 484 PetscValidHeaderSpecific(CI,VEC_CLASSID,2); 485 PetscCheckSameComm(tao,1,X,2); 486 PetscCheckSameComm(tao,1,CI,3); 487 488 if (!tao->ops->computeinequalityconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInequalityConstraintsRoutine() has not been called"); 489 if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeInequalityConstraints"); 490 ierr = PetscLogEventBegin(Tao_ConstraintsEval,tao,X,CI,NULL);CHKERRQ(ierr); 491 PetscStackPush("Tao inequality constraints evaluation routine"); 492 ierr = (*tao->ops->computeinequalityconstraints)(tao,X,CI,tao->user_con_inequalityP);CHKERRQ(ierr); 493 PetscStackPop; 494 ierr = PetscLogEventEnd(Tao_ConstraintsEval,tao,X,CI,NULL);CHKERRQ(ierr); 495 tao->nconstraints++; 496 PetscFunctionReturn(0); 497 } 498