Actual source code: tao_app_j.c

  1: #include "tao_app_impl.h"       /*I  "tao.h"  I*/



  9: /*@C
 10:    TaoAppComputeGradient - Compute the gradient of the objective function using the
 11:    routine set by TaoApplicationSetGradientRoutine().

 13:    Collective on TAO_APPLICATION

 15:    Input Parameters:
 16: +  taopp - the TAO_APPLICATION context
 17: -  X - the point where the objective should be evaluated

 19:    Output Parameter:
 20: .  f - function value

 22:    Level: developer

 24: .keywords: TAO_APPLICATION, objective

 26: .seealso: TaoAppComputeGradient(), TaoAppComputeObjectiveAndGradient()
 27: @*/
 28: int TaoAppComputeVariableBounds(TAO_APPLICATION taoapp, Vec XL, Vec XU){
 29:   int info;
 34:   info = PetscInfo(XL,"TAO:  Compute variable bounds of TAO_APPLICATION object.\n"); CHKERRQ(info);
 35:   if (taoapp->computebounds){
 36:     info = (*taoapp->computebounds)(taoapp,XL,XU,taoapp->boundctx); CHKERRQ(info);
 37:   }
 38:   return(0);
 39: }

 43: /*@C
 44:    TaoAppSetVariableBoundsRoutine - Sets a routine that evaluates the function at
 45: the specified point.

 47:    Collective on TAO_APPLICATION

 49:    Input Parameters:
 50: +  taoapp - the TAO_APPLICATION context
 51: .  func - bound evaluation routine
 52: -  ctx - [optional] user-defined context for private data for the 
 53:          bounds evaluation routine (may be TAO_NULL)

 55:    Calling sequence of func:
 56: $     func (Vec xl,Vec xu, void *ctx);

 58: +  tao - the TAO_APPLICATION context
 59: .  xl - lower bound vector
 60: .  xu - upper bound vector
 61: -  ctx - [optional] user-defined function context 

 63:    Level: beginner

 65: .seealso: TaoGetVariableBoundVecs()

 67: .keywords: TAO_APPLICATION, set, bounds
 68: @*/
 69: int TaoAppSetVariableBoundsRoutine(TAO_APPLICATION taoapp, int (*func)(TAO_APPLICATION,Vec,Vec,void*),void *ctx){
 72:   taoapp->computebounds=func;
 73:   taoapp->boundctx=ctx;
 74:   return(0);
 75: }


 80: /*@
 81:    TaoAppSetFunctionVec - Set the Vec that will be used to store the constraint function.

 83:    Collective on TAO_APPLICATION

 85:    Input Parameters:
 86: +  taoapp - the TAO_APPLICATION context
 87: -  r - vector to constrainf function values

 89:    Level: intermediate

 91: .keywords: TAO_APPLICATION, set, function

 93: .seealso: TaoAppSetJacobianMat(), TaoAppSetConstraintRoutine()

 95: @*/
 96: int TaoAppSetFunctionVec(TAO_APPLICATION taoapp, Vec r){
 97:   int info;
100:   if (r){
102:     PetscObjectReference((PetscObject)r);
103:   }
104:   if (taoapp->R){
105:     info=VecDestroy(taoapp->R); CHKERRQ(info);
106:   }
107:   taoapp->R=r;
108:   return(0);
109: }


114: /*@C
115:    TaoAppGetFunctionVec - Get the Vec that used to store the constraint function.

117:    Collective on TAO_APPLICATION

119:    Input Parameters:
120: +  taoapp - the TAO_APPLICATION context
121: -  r - vector to constrainf function values

123:    Level: intermediate

125: .keywords: TAO_APPLICATION, set, function

127: .seealso: TaoAppSetJacobianMat(), TaoAppSetConstraintRoutine()

129: @*/
130: int TaoAppGetFunctionVec(TAO_APPLICATION taoapp, Vec *r){
133:   if (r){
134:     *r=taoapp->R;
135:   }
136:   return(0);
137: }



143: /*@C
144:    TaoAppSetConstraintRoutine - Sets the routine that evaluates
145:    equality constraint functions.

147:    Collective on TAO_APPLICATION

149:    Input Parameters:
150: +  taoapp - the TAO_APPLICATION context
151: .  func - the constraint function evaluation routine
152: -  ctx - [optional] user-defined function context 

154:    Calling sequence of func:
155: $     func (TAO_APPLICATION taoapp,Vec x,Vec r,void *ctx);

157: +  taoapp - the TAO_APPLICATION  context
158: .  x - input vector
159: .  r - constraint vector
160: -  ctx - user-defined function gradient context set from TaoAppSetConstraintRoutine()

162:    Level: intermediate

164: .keywords: TAO_APPLICATION, set, function

166: .seealso: TaoAppSetFunctionVec(), TaoAppSetJacobianRoutine()

168: @*/
169: int TaoAppSetConstraintRoutine(TAO_APPLICATION taoapp, int (*func)(TAO_APPLICATION,Vec,Vec,void*),void *ctx){
172:   taoapp->usrfvctx = ctx;
173:   taoapp->computevfunc = func;
174:   return(0);
175: }

179: /*@C
180:    TaoAppComputeFunction - Compute the constraint vector using the
181:    routine set by TaoApplicationSetConstraintsRoutine().

183:    Collective on TAO_APPLICATION

185:    Input Parameters:
186: +  taopp - the TAO_APPLICATION context
187: -  X - the point where the objective should be evaluated

189:    Output Parameter:
190: .  R - constraint vector

192:    Level: developer

194: .keywords: TAO_APPLICATION, objective

196: .seealso: TaoAppComputeJacobian() TaoAppSetConstraintRoutine()
197: @*/
198: int TaoAppComputeFunction(TAO_APPLICATION taoapp, Vec X, Vec R){
199:   int     info;

205:   if (taoapp->computevfunc){
206:     info = PetscLogEventBegin(Tao_FunctionEval,taoapp,X,R,0);CHKERRQ(info);
207:     PetscStackPush("Tao user ConstraintsFunction");
208:     info = (*taoapp->computevfunc)(taoapp,X,R,taoapp->usrfvctx);
209:     CHKERRQ(info);
210:     PetscStackPop;
211:     info = PetscLogEventEnd(Tao_FunctionEval,taoapp,X,R,0);
212:   } else {
213:     SETERRQ(1,"TAO ERROR: Must set Constraint function");
214:   }
215:   return(0);
216: }


221: /*@
222:    TaoAppSetJacobianMat - Sets the matrix to be used for the Jacobian.

224:    Collective on TAO_APPLICATION and Mat

226:    Input Parameters:
227: +  taoapp - the TAO_APPLICATION context
228: .  J - Jacobian matrix
229: -  JP - Preconditioner for Jacobian

231:    Level: intermediate

233: .keywords: TAO_APPLICATION, Jacobian

235: .seealso: TaoAppSetJacobianRoutine(), TaoAppSetFunctionVec()
236: @*/
237: int TaoAppSetJacobianMat(TAO_APPLICATION taoapp,Mat J, Mat JP){
238:   int info;
241:   if (J || JP){
244:     PetscObjectReference((PetscObject)J);
245:     PetscObjectReference((PetscObject)JP);
246:   }

248:   if (taoapp->J || taoapp->JP){
249:     info=MatDestroy(taoapp->J);CHKERRQ(info);
250:     info=MatDestroy(taoapp->JP);CHKERRQ(info);
251:   }
252:   taoapp->J=J;
253:   taoapp->JP=JP;
254:   return(0);
255: }

259: /*@
260:    TaoAppGetJacobianMat - Get the matrix to be used for the Jacobian.

262:    Collective on TAO_APPLICATION and Mat

264:    Input Parameters:
265: +  taoapp - the TAO_APPLICATION context
266: -  J - Jacobian matrix

268:    Level: intermediate

270: .keywords: TAO_APPLICATION, Jacobian

272: .seealso: TaoAppSetJacobianMat()
273: @*/
274: int TaoAppGetJacobianMat(TAO_APPLICATION taoapp,Mat *J, Mat *JP){
277:   if (J) { *J=taoapp->J;}
278:   if (JP){ *JP=taoapp->JP; }
279:   return(0);
280: }

284: /*@C
285:    TaoAppSetJacobianRoutine - Sets the function to compute the Jacobian of
286: the equality constraint function as well as the
287:    location to store the matrix.

289:    Collective on TAO_APPLICATION and Mat

291:    Input Parameters:
292: +  taoapp - the TAO_APPLICATION context
293: .  jac - Jacobian evaluation routine
294: -  ctx - [optional] user-defined context for private data for the 
295:          Hessian evaluation routine (may be TAO_NULL)

297:    Calling sequence of func:
298: $    jac (TAO_APPLICATION taoapp,Vec x,Mat *J,Mat *JPre, void *ctx);

300: +  taoapp - the TAO_APPLICATION  context
301: .  x - input vector
302: .  J - Jacobian matrix
303: .  JPre - matrix to precondition J
304: -  ctx - [optional] user-defined Hessian context

306:    Notes: 

308:    The function jac() takes Mat * as the matrix arguments rather than Mat.  
309:    This allows the Jacobian evaluation routine to replace J with a 
310:    completely new new matrix structure (not just different matrix elements)
311:    when appropriate, for instance, if the nonzero structure is changing
312:    throughout the global iterations.

314:    Level: intermediate

316: .keywords: TAO_APPLICATION, Jacobian

318: .seealso: TaoAppSetJacobianMat(), TaoAppSetConstraintRoutine()
319: @*/
320: int TaoAppSetJacobianRoutine(TAO_APPLICATION taoapp,int (*jac)(TAO_APPLICATION,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx){
323:   taoapp->computejacobian=jac;
324:   taoapp->usrjctx=ctx;
325:   return(0);
326: }


331: /*@C
332:    TaoAppComputeJacobian - Compute the Jacobian of the nonlinear equations using the
333:    routine set by TaoApplicationSetJacobianRoutine().

335:    Collective on TAO_APPLICATION

337:    Input Parameters:
338: +  taopp - the TAO_APPLICATION context
339: .  X - the variable vector
340: .  H - the Jacobian matrix
341: .  HP - the preconditioner for the Jacobian matrix.
342: -  flag - flag used in KSPSetOperators()

344:    Output Parameter:
345: +  H - the Jacobian matrix
346: .  HP - the preconditioner for the Jacobian matrix.
347: -  flag - flag used in KSPSetOperators()

349:    Level: developer

351: .keywords: TAO_APPLICATION, objective

353: .seealso: TaoAppComputeFunction(), TaoAppSetJacobianRoutine()
354: @*/
355: int TaoAppComputeJacobian(TAO_APPLICATION taoapp, Vec X, Mat *JJ, Mat *JJPre, MatStructure*flag){

357:   int     info;
358:   Mat J=*JJ,JPre=*JJPre;
359:   MatStructure pflag=*flag;

365:   if (taoapp->computejacobian){
366:     PetscStackPush("TAO User Jacobian Evaluation");
367:     info = PetscLogEventBegin(Tao_JacobianEval,taoapp,X,J,0);CHKERRQ(info);
368:     info = (*taoapp->computejacobian)(taoapp,X,&J,&JPre, &pflag, taoapp->usrjctx);
369:     CHKERRQ(info);
370:     info = PetscLogEventEnd(Tao_JacobianEval,taoapp,X,J,0);CHKERRQ(info);
371:     PetscStackPop;
372:   } else {
373:     SETERRQ(1,"TAO Error:  No Jacobian Routine Available.");
374:   }
375:   *JJ=J;*JJPre=JPre; *flag=pflag;
376:   return(0);
377: }