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