Actual source code: taosolver_bounds.c

petsc-master 2020-08-09
Report Typos and Errors
  1:  #include <petsc/private/taoimpl.h>

  3: /*@
  4:   TaoSetVariableBounds - Sets the upper and lower bounds

  6:   Logically collective on Tao

  8:   Input Parameters:
  9: + tao - the Tao context
 10: . XL  - vector of lower bounds
 11: - XU  - vector of upper bounds

 13:   Level: beginner

 15: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine()
 16: @*/

 18: PetscErrorCode TaoSetVariableBounds(Tao tao, Vec XL, Vec XU)
 19: {

 24:   if (XL) {
 26:     PetscObjectReference((PetscObject)XL);
 27:   }
 28:   if (XU) {
 30:     PetscObjectReference((PetscObject)XU);
 31:   }
 32:   VecDestroy(&tao->XL);
 33:   VecDestroy(&tao->XU);
 34:   tao->XL = XL;
 35:   tao->XU = XU;
 36:   tao->bounded = PETSC_TRUE;
 37:   return(0);
 38: }

 40: /*@C
 41:   TaoSetVariableBoundsRoutine - Sets a function to be used to compute variable bounds

 43:   Logically collective on Tao

 45:   Input Parameters:
 46: + tao - the Tao context
 47: . func - the bounds computation routine
 48: - ctx - [optional] user-defined context for private data for the bounds computation (may be NULL)

 50:   Calling sequence of func:
 51: $      func (Tao tao, Vec xl, Vec xu);

 53: + tao - the Tao
 54: . xl  - vector of lower bounds
 55: . xu  - vector of upper bounds
 56: - ctx - the (optional) user-defined function context

 58:   Level: beginner

 60: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()

 62: Note: The func passed in to TaoSetVariableBoundsRoutine() takes
 63: precedence over any values set in TaoSetVariableBounds().

 65: @*/
 66: PetscErrorCode TaoSetVariableBoundsRoutine(Tao tao, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
 67: {
 70:   tao->user_boundsP = ctx;
 71:   tao->ops->computebounds = func;
 72:   tao->bounded = PETSC_TRUE;
 73:   return(0);
 74: }

 76: PetscErrorCode TaoGetVariableBounds(Tao tao, Vec *XL, Vec *XU)
 77: {
 80:   if (XL) {
 81:     *XL=tao->XL;
 82:   }
 83:   if (XU) {
 84:     *XU=tao->XU;
 85:   }
 86:   return(0);
 87: }

 89: /*@C
 90:    TaoComputeVariableBounds - Compute the variable bounds using the
 91:    routine set by TaoSetVariableBoundsRoutine().

 93:    Collective on Tao

 95:    Input Parameters:
 96: .  tao - the Tao context

 98:    Level: developer

100: .seealso: TaoSetVariableBoundsRoutine(), TaoSetVariableBounds()
101: @*/

103: PetscErrorCode TaoComputeVariableBounds(Tao tao)
104: {

109:   PetscStackPush("Tao compute variable bounds");
110:   if (!tao->XL || !tao->XU) {
111:     if (!tao->solution) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ORDER,"TaoSetInitialVector must be called before TaoComputeVariableBounds");
112:     VecDuplicate(tao->solution, &tao->XL);
113:     VecSet(tao->XL, PETSC_NINFINITY);
114:     VecDuplicate(tao->solution, &tao->XU);
115:     VecSet(tao->XU, PETSC_INFINITY);
116:   }
117:   if (tao->ops->computebounds) {
118:     (*tao->ops->computebounds)(tao,tao->XL,tao->XU,tao->user_boundsP);
119:   }
120:   PetscStackPop;
121:   return(0);
122: }

124: /*@
125:   TaoSetInequalityBounds - Sets the upper and lower bounds

127:   Logically collective on Tao

129:   Input Parameters:
130: + tao - the Tao context
131: . IL  - vector of lower bounds
132: - IU  - vector of upper bounds

134:   Level: beginner

136: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine()
137: @*/

139: PetscErrorCode TaoSetInequalityBounds(Tao tao, Vec IL, Vec IU)
140: {

145:   if (IL) {
147:     PetscObjectReference((PetscObject)IL);
148:   }
149:   if (IU) {
151:     PetscObjectReference((PetscObject)IU);
152:   }
153:   VecDestroy(&tao->IL);
154:   VecDestroy(&tao->IU);
155:   tao->IL = IL;
156:   tao->IU = IU;
157:   return(0);
158: }


161: PetscErrorCode TaoGetInequalityBounds(Tao tao, Vec *IL, Vec *IU)
162: {
165:   if (IL) {
166:     *IL=tao->IL;
167:   }
168:   if (IU) {
169:     *IU=tao->IU;
170:   }
171:   return(0);
172: }

174: /*@C
175:    TaoComputeConstraints - Compute the variable bounds using the
176:    routine set by TaoSetConstraintsRoutine().

178:    Collective on Tao

180:    Input Parameters:
181: .  tao - the Tao context

183:    Level: developer

185: .seealso: TaoSetConstraintsRoutine(), TaoComputeJacobian()
186: @*/

188: PetscErrorCode TaoComputeConstraints(Tao tao, Vec X, Vec C)
189: {


199:   if (!tao->ops->computeconstraints) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetConstraintsRoutine() has not been called");
200:   if (!tao->solution) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeConstraints");
201:   PetscLogEventBegin(TAO_ConstraintsEval,tao,X,C,NULL);
202:   PetscStackPush("Tao constraints evaluation routine");
203:   (*tao->ops->computeconstraints)(tao,X,C,tao->user_conP);
204:   PetscStackPop;
205:   PetscLogEventEnd(TAO_ConstraintsEval,tao,X,C,NULL);
206:   tao->nconstraints++;
207:   return(0);
208: }

210: /*@C
211:   TaoSetConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details

213:   Logically collective on Tao

215:   Input Parameters:
216: + tao - the Tao context
217: . c   - A vector that will be used to store constraint evaluation
218: . func - the bounds computation routine
219: - ctx - [optional] user-defined context for private data for the constraints computation (may be NULL)

221:   Calling sequence of func:
222: $      func (Tao tao, Vec x, Vec c, void *ctx);

224: + tao - the Tao
225: . x   - point to evaluate constraints
226: . c   - vector constraints evaluated at x
227: - ctx - the (optional) user-defined function context

229:   Level: intermediate

231: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariablevBounds()

233: @*/
234: PetscErrorCode TaoSetConstraintsRoutine(Tao tao, Vec c, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
235: {
238:     tao->constrained = PETSC_TRUE;
239:     tao->constraints = c;
240:     tao->user_conP = ctx;
241:     tao->ops->computeconstraints = func;
242:     return(0);
243: }

245: /*@
246:   TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds
247:   of the variables

249:   Collective on Tao

251:   Input Parameters:
252: . tao - the Tao context

254:   Output Parameter:
255: + DL - dual variable vector for the lower bounds
256: - DU - dual variable vector for the upper bounds

258:   Level: advanced

260:   Note:
261:   DL and DU should be created before calling this routine.  If calling
262:   this routine after using an unconstrained solver, DL and DU are set to all
263:   zeros.

265:   Level: advanced

267: .seealso: TaoComputeObjective(), TaoSetVariableBounds()
268: @*/
269: PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU)
270: {
278:   if (tao->ops->computedual) {
279:     (*tao->ops->computedual)(tao,DL,DU);
280:   }  else {
281:     VecSet(DL,0.0);
282:     VecSet(DU,0.0);
283:   }
284:   return(0);
285: }

287: /*@
288:   TaoGetDualVariables - Gets pointers to the dual vectors

290:   Collective on Tao

292:   Input Parameters:
293: . tao - the Tao context

295:   Output Parameter:
296: + DE - dual variable vector for the lower bounds
297: - DI - dual variable vector for the upper bounds

299:   Level: advanced

301: .seealso: TaoComputeDualVariables()
302: @*/
303: PetscErrorCode TaoGetDualVariables(Tao tao, Vec *DE, Vec *DI)
304: {
307:   if (DE) {
308:     *DE = tao->DE;
309:   }
310:   if (DI) {
311:     *DI = tao->DI;
312:   }
313:   return(0);
314: }

316: /*@C
317:   TaoSetEqualityConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details

319:   Logically collective on Tao

321:   Input Parameters:
322: + tao - the Tao context
323: . ce   - A vector that will be used to store equality constraint evaluation
324: . func - the bounds computation routine
325: - ctx - [optional] user-defined context for private data for the equality constraints computation (may be NULL)

327:   Calling sequence of func:
328: $      func (Tao tao, Vec x, Vec ce, void *ctx);

330: + tao - the Tao
331: . x   - point to evaluate equality constraints
332: . ce   - vector of equality constraints evaluated at x
333: - ctx - the (optional) user-defined function context

335:   Level: intermediate

337: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()

339: @*/
340: PetscErrorCode TaoSetEqualityConstraintsRoutine(Tao tao, Vec ce, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
341: {

346:   if (ce) {
348:     PetscObjectReference((PetscObject)ce);
349:   }
350:   VecDestroy(&tao->constraints_equality);

352:   tao->constraints_equality = ce;
353:   tao->user_con_equalityP = ctx;
354:   tao->ops->computeequalityconstraints = func;
355:   return(0);
356: }


359: /*@C
360:   TaoSetInequalityConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details

362:   Logically collective on Tao

364:   Input Parameters:
365: + tao - the Tao context
366: . ci   - A vector that will be used to store inequality constraint evaluation
367: . func - the bounds computation routine
368: - ctx - [optional] user-defined context for private data for the inequality constraints computation (may be NULL)

370:   Calling sequence of func:
371: $      func (Tao tao, Vec x, Vec ci, void *ctx);

373: + tao - the Tao
374: . x   - point to evaluate inequality constraints
375: . ci   - vector of inequality constraints evaluated at x
376: - ctx - the (optional) user-defined function context

378:   Level: intermediate

380: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()

382: @*/
383: PetscErrorCode TaoSetInequalityConstraintsRoutine(Tao tao, Vec ci, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
384: {

389:   if (ci) {
391:     PetscObjectReference((PetscObject)ci);
392:   }
393:   VecDestroy(&tao->constraints_inequality);
394:   tao->constraints_inequality = ci;

396:   tao->user_con_inequalityP = ctx;
397:   tao->ops->computeinequalityconstraints = func;
398:   return(0);
399: }


402: /*@C
403:    TaoComputeEqualityConstraints - Compute the variable bounds using the
404:    routine set by TaoSetEqualityConstraintsRoutine().

406:    Collective on Tao

408:    Input Parameters:
409: .  tao - the Tao context

411:    Level: developer

413: .seealso: TaoSetEqualityConstraintsRoutine(), TaoComputeJacobianEquality()
414: @*/

416: PetscErrorCode TaoComputeEqualityConstraints(Tao tao, Vec X, Vec CE)
417: {


427:   if (!tao->ops->computeequalityconstraints) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetEqualityConstraintsRoutine() has not been called");
428:   if (!tao->solution) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeEqualityConstraints");
429:   PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CE,NULL);
430:   PetscStackPush("Tao equality constraints evaluation routine");
431:   (*tao->ops->computeequalityconstraints)(tao,X,CE,tao->user_con_equalityP);
432:   PetscStackPop;
433:   PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CE,NULL);
434:   tao->nconstraints++;
435:   return(0);
436: }


439: /*@C
440:    TaoComputeInequalityConstraints - Compute the variable bounds using the
441:    routine set by TaoSetInequalityConstraintsRoutine().

443:    Collective on Tao

445:    Input Parameters:
446: .  tao - the Tao context

448:    Level: developer

450: .seealso: TaoSetInequalityConstraintsRoutine(), TaoComputeJacobianInequality()
451: @*/

453: PetscErrorCode TaoComputeInequalityConstraints(Tao tao, Vec X, Vec CI)
454: {


464:   if (!tao->ops->computeinequalityconstraints) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetInequalityConstraintsRoutine() has not been called");
465:   if (!tao->solution) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeInequalityConstraints");
466:   PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CI,NULL);
467:   PetscStackPush("Tao inequality constraints evaluation routine");
468:   (*tao->ops->computeinequalityconstraints)(tao,X,CI,tao->user_con_inequalityP);
469:   PetscStackPop;
470:   PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CI,NULL);
471:   tao->nconstraints++;
472:   return(0);
473: }