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