#include /*I "petsctao.h" I*/ /*@ TaoSetSolution - Sets the vector holding the initial guess for the solve Logically collective on Tao Input Parameters: + tao - the Tao context - x0 - the initial guess Level: beginner .seealso: TaoCreate(), TaoSolve(), TaoGetSolution() @*/ PetscErrorCode TaoSetSolution(Tao tao, Vec x0) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (x0) PetscValidHeaderSpecific(x0,VEC_CLASSID,2); CHKERRQ(PetscObjectReference((PetscObject)x0)); CHKERRQ(VecDestroy(&tao->solution)); tao->solution = x0; PetscFunctionReturn(0); } PetscErrorCode TaoTestGradient(Tao tao,Vec x,Vec g1) { Vec g2,g3; PetscBool complete_print = PETSC_FALSE,test = PETSC_FALSE; PetscReal hcnorm,fdnorm,hcmax,fdmax,diffmax,diffnorm; PetscScalar dot; MPI_Comm comm; PetscViewer viewer,mviewer; PetscViewerFormat format; PetscInt tabs; static PetscBool directionsprinted = PETSC_FALSE; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscObjectOptionsBegin((PetscObject)tao);CHKERRQ(ierr); CHKERRQ(PetscOptionsName("-tao_test_gradient","Compare hand-coded and finite difference Gradients","None",&test)); CHKERRQ(PetscOptionsViewer("-tao_test_gradient_view","View difference between hand-coded and finite difference Gradients element entries","None",&mviewer,&format,&complete_print)); ierr = PetscOptionsEnd();CHKERRQ(ierr); if (!test) { if (complete_print) { CHKERRQ(PetscViewerDestroy(&mviewer)); } PetscFunctionReturn(0); } CHKERRQ(PetscObjectGetComm((PetscObject)tao,&comm)); CHKERRQ(PetscViewerASCIIGetStdout(comm,&viewer)); CHKERRQ(PetscViewerASCIIGetTab(viewer, &tabs)); CHKERRQ(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); CHKERRQ(PetscViewerASCIIPrintf(viewer," ---------- Testing Gradient -------------\n")); if (!complete_print && !directionsprinted) { CHKERRQ(PetscViewerASCIIPrintf(viewer," Run with -tao_test_gradient_view and optionally -tao_test_gradient to show difference\n")); CHKERRQ(PetscViewerASCIIPrintf(viewer," of hand-coded and finite difference gradient entries greater than .\n")); } if (!directionsprinted) { CHKERRQ(PetscViewerASCIIPrintf(viewer," Testing hand-coded Gradient, if (for double precision runs) ||G - Gfd||/||G|| is\n")); CHKERRQ(PetscViewerASCIIPrintf(viewer," O(1.e-8), the hand-coded Gradient is probably correct.\n")); directionsprinted = PETSC_TRUE; } if (complete_print) { CHKERRQ(PetscViewerPushFormat(mviewer,format)); } CHKERRQ(VecDuplicate(x,&g2)); CHKERRQ(VecDuplicate(x,&g3)); /* Compute finite difference gradient, assume the gradient is already computed by TaoComputeGradient() and put into g1 */ CHKERRQ(TaoDefaultComputeGradient(tao,x,g2,NULL)); CHKERRQ(VecNorm(g2,NORM_2,&fdnorm)); CHKERRQ(VecNorm(g1,NORM_2,&hcnorm)); CHKERRQ(VecNorm(g2,NORM_INFINITY,&fdmax)); CHKERRQ(VecNorm(g1,NORM_INFINITY,&hcmax)); CHKERRQ(VecDot(g1,g2,&dot)); CHKERRQ(VecCopy(g1,g3)); CHKERRQ(VecAXPY(g3,-1.0,g2)); CHKERRQ(VecNorm(g3,NORM_2,&diffnorm)); CHKERRQ(VecNorm(g3,NORM_INFINITY,&diffmax)); CHKERRQ(PetscViewerASCIIPrintf(viewer," ||Gfd|| %g, ||G|| = %g, angle cosine = (Gfd'G)/||Gfd||||G|| = %g\n", (double)fdnorm, (double)hcnorm, (double)(PetscRealPart(dot)/(fdnorm*hcnorm)))); CHKERRQ(PetscViewerASCIIPrintf(viewer," 2-norm ||G - Gfd||/||G|| = %g, ||G - Gfd|| = %g\n",(double)(diffnorm/PetscMax(hcnorm,fdnorm)),(double)diffnorm)); CHKERRQ(PetscViewerASCIIPrintf(viewer," max-norm ||G - Gfd||/||G|| = %g, ||G - Gfd|| = %g\n",(double)(diffmax/PetscMax(hcmax,fdmax)),(double)diffmax)); if (complete_print) { CHKERRQ(PetscViewerASCIIPrintf(viewer," Hand-coded gradient ----------\n")); CHKERRQ(VecView(g1,mviewer)); CHKERRQ(PetscViewerASCIIPrintf(viewer," Finite difference gradient ----------\n")); CHKERRQ(VecView(g2,mviewer)); CHKERRQ(PetscViewerASCIIPrintf(viewer," Hand-coded minus finite-difference gradient ----------\n")); CHKERRQ(VecView(g3,mviewer)); } CHKERRQ(VecDestroy(&g2)); CHKERRQ(VecDestroy(&g3)); if (complete_print) { CHKERRQ(PetscViewerPopFormat(mviewer)); CHKERRQ(PetscViewerDestroy(&mviewer)); } CHKERRQ(PetscViewerASCIISetTab(viewer,tabs)); PetscFunctionReturn(0); } /*@ TaoComputeGradient - Computes the gradient of the objective function Collective on Tao Input Parameters: + tao - the Tao context - X - input vector Output Parameter: . G - gradient vector Options Database Keys: + -tao_test_gradient - compare the user provided gradient with one compute via finite differences to check for errors - -tao_test_gradient_view - display the user provided gradient, the finite difference gradient and the difference between them to help users detect the location of errors in the user provided gradient Notes: TaoComputeGradient() is typically used within minimization implementations, so most users would not generally call this routine themselves. Level: advanced .seealso: TaoComputeObjective(), TaoComputeObjectiveAndGradient(), TaoSetGradient() @*/ PetscErrorCode TaoComputeGradient(Tao tao, Vec X, Vec G) { PetscReal dummy; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(X,VEC_CLASSID,2); PetscValidHeaderSpecific(G,VEC_CLASSID,3); PetscCheckSameComm(tao,1,X,2); PetscCheckSameComm(tao,1,G,3); CHKERRQ(VecLockReadPush(X)); if (tao->ops->computegradient) { CHKERRQ(PetscLogEventBegin(TAO_GradientEval,tao,X,G,NULL)); PetscStackPush("Tao user gradient evaluation routine"); CHKERRQ((*tao->ops->computegradient)(tao,X,G,tao->user_gradP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_GradientEval,tao,X,G,NULL)); tao->ngrads++; } else if (tao->ops->computeobjectiveandgradient) { CHKERRQ(PetscLogEventBegin(TAO_ObjGradEval,tao,X,G,NULL)); PetscStackPush("Tao user objective/gradient evaluation routine"); CHKERRQ((*tao->ops->computeobjectiveandgradient)(tao,X,&dummy,G,tao->user_objgradP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_ObjGradEval,tao,X,G,NULL)); tao->nfuncgrads++; } else SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetGradient() has not been called"); CHKERRQ(VecLockReadPop(X)); CHKERRQ(TaoTestGradient(tao,X,G)); PetscFunctionReturn(0); } /*@ TaoComputeObjective - Computes the objective function value at a given point Collective on Tao Input Parameters: + tao - the Tao context - X - input vector Output Parameter: . f - Objective value at X Notes: TaoComputeObjective() is typically used within minimization implementations, so most users would not generally call this routine themselves. Level: advanced .seealso: TaoComputeGradient(), TaoComputeObjectiveAndGradient(), TaoSetObjective() @*/ PetscErrorCode TaoComputeObjective(Tao tao, Vec X, PetscReal *f) { Vec temp; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(X,VEC_CLASSID,2); PetscCheckSameComm(tao,1,X,2); CHKERRQ(VecLockReadPush(X)); if (tao->ops->computeobjective) { CHKERRQ(PetscLogEventBegin(TAO_ObjectiveEval,tao,X,NULL,NULL)); PetscStackPush("Tao user objective evaluation routine"); CHKERRQ((*tao->ops->computeobjective)(tao,X,f,tao->user_objP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_ObjectiveEval,tao,X,NULL,NULL)); tao->nfuncs++; } else if (tao->ops->computeobjectiveandgradient) { CHKERRQ(PetscInfo(tao,"Duplicating variable vector in order to call func/grad routine\n")); CHKERRQ(VecDuplicate(X,&temp)); CHKERRQ(PetscLogEventBegin(TAO_ObjGradEval,tao,X,NULL,NULL)); PetscStackPush("Tao user objective/gradient evaluation routine"); CHKERRQ((*tao->ops->computeobjectiveandgradient)(tao,X,f,temp,tao->user_objgradP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_ObjGradEval,tao,X,NULL,NULL)); CHKERRQ(VecDestroy(&temp)); tao->nfuncgrads++; } else SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetObjective() has not been called"); CHKERRQ(PetscInfo(tao,"TAO Function evaluation: %20.19e\n",(double)(*f))); CHKERRQ(VecLockReadPop(X)); PetscFunctionReturn(0); } /*@ TaoComputeObjectiveAndGradient - Computes the objective function value at a given point Collective on Tao Input Parameters: + tao - the Tao context - X - input vector Output Parameters: + f - Objective value at X - g - Gradient vector at X Notes: TaoComputeObjectiveAndGradient() is typically used within minimization implementations, so most users would not generally call this routine themselves. Level: advanced .seealso: TaoComputeGradient(), TaoComputeObjectiveAndGradient(), TaoSetObjective() @*/ PetscErrorCode TaoComputeObjectiveAndGradient(Tao tao, Vec X, PetscReal *f, Vec G) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(X,VEC_CLASSID,2); PetscValidHeaderSpecific(G,VEC_CLASSID,4); PetscCheckSameComm(tao,1,X,2); PetscCheckSameComm(tao,1,G,4); CHKERRQ(VecLockReadPush(X)); if (tao->ops->computeobjectiveandgradient) { CHKERRQ(PetscLogEventBegin(TAO_ObjGradEval,tao,X,G,NULL)); if (tao->ops->computegradient == TaoDefaultComputeGradient) { CHKERRQ(TaoComputeObjective(tao,X,f)); CHKERRQ(TaoDefaultComputeGradient(tao,X,G,NULL)); } else { PetscStackPush("Tao user objective/gradient evaluation routine"); CHKERRQ((*tao->ops->computeobjectiveandgradient)(tao,X,f,G,tao->user_objgradP)); PetscStackPop; } CHKERRQ(PetscLogEventEnd(TAO_ObjGradEval,tao,X,G,NULL)); tao->nfuncgrads++; } else if (tao->ops->computeobjective && tao->ops->computegradient) { CHKERRQ(PetscLogEventBegin(TAO_ObjectiveEval,tao,X,NULL,NULL)); PetscStackPush("Tao user objective evaluation routine"); CHKERRQ((*tao->ops->computeobjective)(tao,X,f,tao->user_objP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_ObjectiveEval,tao,X,NULL,NULL)); tao->nfuncs++; CHKERRQ(PetscLogEventBegin(TAO_GradientEval,tao,X,G,NULL)); PetscStackPush("Tao user gradient evaluation routine"); CHKERRQ((*tao->ops->computegradient)(tao,X,G,tao->user_gradP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_GradientEval,tao,X,G,NULL)); tao->ngrads++; } else SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetObjective() or TaoSetGradient() not set"); CHKERRQ(PetscInfo(tao,"TAO Function evaluation: %20.19e\n",(double)(*f))); CHKERRQ(VecLockReadPop(X)); CHKERRQ(TaoTestGradient(tao,X,G)); PetscFunctionReturn(0); } /*@C TaoSetObjective - Sets the function evaluation routine for minimization Logically collective on Tao Input Parameters: + tao - the Tao context . func - the objective function - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) Calling sequence of func: $ func (Tao tao, Vec x, PetscReal *f, void *ctx); + x - input vector . f - function value - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetGradient(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoGetObjective() @*/ PetscErrorCode TaoSetObjective(Tao tao, PetscErrorCode (*func)(Tao, Vec, PetscReal*,void*),void *ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (ctx) tao->user_objP = ctx; if (func) tao->ops->computeobjective = func; PetscFunctionReturn(0); } /*@C TaoGetObjective - Gets the function evaluation routine for minimization Not collective Input Parameter: . tao - the Tao context Output Parameters + func - the objective function - ctx - the user-defined context for private data for the function evaluation Calling sequence of func: $ func (Tao tao, Vec x, PetscReal *f, void *ctx); + x - input vector . f - function value - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetGradient(), TaoSetHessian(), TaoSetObjective() @*/ PetscErrorCode TaoGetObjective(Tao tao, PetscErrorCode (**func)(Tao, Vec, PetscReal*,void*),void **ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (func) *func = tao->ops->computeobjective; if (ctx) *ctx = tao->user_objP; PetscFunctionReturn(0); } /*@C TaoSetResidualRoutine - Sets the residual evaluation routine for least-square applications Logically collective on Tao Input Parameters: + tao - the Tao context . func - the residual evaluation routine - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) Calling sequence of func: $ func (Tao tao, Vec x, Vec f, void *ctx); + x - input vector . f - function value vector - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetObjective(), TaoSetJacobianRoutine() @*/ PetscErrorCode TaoSetResidualRoutine(Tao tao, Vec res, PetscErrorCode (*func)(Tao, Vec, Vec, void*),void *ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(res,VEC_CLASSID,2); CHKERRQ(PetscObjectReference((PetscObject)res)); if (tao->ls_res) { CHKERRQ(VecDestroy(&tao->ls_res)); } tao->ls_res = res; tao->user_lsresP = ctx; tao->ops->computeresidual = func; PetscFunctionReturn(0); } /*@ TaoSetResidualWeights - Give weights for the residual values. A vector can be used if only diagonal terms are used, otherwise a matrix can be give. If this function is not used, or if sigma_v and sigma_w are both NULL, then the default identity matrix will be used for weights. Collective on Tao Input Parameters: + tao - the Tao context . sigma_v - vector of weights (diagonal terms only) . n - the number of weights (if using off-diagonal) . rows - index list of rows for sigma_w . cols - index list of columns for sigma_w - vals - array of weights Note: Either sigma_v or sigma_w (or both) should be NULL Level: intermediate .seealso: TaoSetResidualRoutine() @*/ PetscErrorCode TaoSetResidualWeights(Tao tao, Vec sigma_v, PetscInt n, PetscInt *rows, PetscInt *cols, PetscReal *vals) { PetscInt i; PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (sigma_v) PetscValidHeaderSpecific(sigma_v,VEC_CLASSID,2); CHKERRQ(PetscObjectReference((PetscObject)sigma_v)); CHKERRQ(VecDestroy(&tao->res_weights_v)); tao->res_weights_v = sigma_v; if (vals) { CHKERRQ(PetscFree(tao->res_weights_rows)); CHKERRQ(PetscFree(tao->res_weights_cols)); CHKERRQ(PetscFree(tao->res_weights_w)); CHKERRQ(PetscMalloc1(n,&tao->res_weights_rows)); CHKERRQ(PetscMalloc1(n,&tao->res_weights_cols)); CHKERRQ(PetscMalloc1(n,&tao->res_weights_w)); tao->res_weights_n = n; for (i=0;ires_weights_rows[i] = rows[i]; tao->res_weights_cols[i] = cols[i]; tao->res_weights_w[i] = vals[i]; } } else { tao->res_weights_n = 0; tao->res_weights_rows = NULL; tao->res_weights_cols = NULL; } PetscFunctionReturn(0); } /*@ TaoComputeResidual - Computes a least-squares residual vector at a given point Collective on Tao Input Parameters: + tao - the Tao context - X - input vector Output Parameter: . f - Objective vector at X Notes: TaoComputeResidual() is typically used within minimization implementations, so most users would not generally call this routine themselves. Level: advanced .seealso: TaoSetResidualRoutine() @*/ PetscErrorCode TaoComputeResidual(Tao tao, Vec X, Vec F) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); PetscValidHeaderSpecific(X,VEC_CLASSID,2); PetscValidHeaderSpecific(F,VEC_CLASSID,3); PetscCheckSameComm(tao,1,X,2); PetscCheckSameComm(tao,1,F,3); if (tao->ops->computeresidual) { CHKERRQ(PetscLogEventBegin(TAO_ObjectiveEval,tao,X,NULL,NULL)); PetscStackPush("Tao user least-squares residual evaluation routine"); CHKERRQ((*tao->ops->computeresidual)(tao,X,F,tao->user_lsresP)); PetscStackPop; CHKERRQ(PetscLogEventEnd(TAO_ObjectiveEval,tao,X,NULL,NULL)); tao->nfuncs++; } else SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetResidualRoutine() has not been called"); CHKERRQ(PetscInfo(tao,"TAO least-squares residual evaluation.\n")); PetscFunctionReturn(0); } /*@C TaoSetGradient - Sets the gradient evaluation routine for minimization Logically collective on Tao Input Parameters: + tao - the Tao context . g - [optional] the vector to internally hold the gradient computation . func - the gradient function - ctx - [optional] user-defined context for private data for the gradient evaluation routine (may be NULL) Calling sequence of func: $ func (Tao tao, Vec x, Vec g, void *ctx); + x - input vector . g - gradient value (output) - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoGetGradient() @*/ PetscErrorCode TaoSetGradient(Tao tao, Vec g, PetscErrorCode (*func)(Tao, Vec, Vec, void*),void *ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (g) { PetscValidHeaderSpecific(g,VEC_CLASSID,2); PetscCheckSameComm(tao,1,g,2); CHKERRQ(PetscObjectReference((PetscObject)g)); CHKERRQ(VecDestroy(&tao->gradient)); tao->gradient = g; } if (func) tao->ops->computegradient = func; if (ctx) tao->user_gradP = ctx; PetscFunctionReturn(0); } /*@C TaoGetGradient - Gets the gradient evaluation routine for minimization Not collective Input Parameter: . tao - the Tao context Output Parameters: + g - the vector to internally hold the gradient computation . func - the gradient function - ctx - user-defined context for private data for the gradient evaluation routine Calling sequence of func: $ func (Tao tao, Vec x, Vec g, void *ctx); + x - input vector . g - gradient value (output) - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetGradient() @*/ PetscErrorCode TaoGetGradient(Tao tao, Vec *g, PetscErrorCode (**func)(Tao, Vec, Vec, void*),void **ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (g) *g = tao->gradient; if (func) *func = tao->ops->computegradient; if (ctx) *ctx = tao->user_gradP; PetscFunctionReturn(0); } /*@C TaoSetObjectiveAndGradient - Sets a combined objective function and gradient evaluation routine for minimization Logically collective on Tao Input Parameters: + tao - the Tao context . g - [optional] the vector to internally hold the gradient computation . func - the gradient function - ctx - [optional] user-defined context for private data for the gradient evaluation routine (may be NULL) Calling sequence of func: $ func (Tao tao, Vec x, PetscReal *f, Vec g, void *ctx); + x - input vector . f - objective value (output) . g - gradient value (output) - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetGradient(), TaoGetObjectiveAndGradient() @*/ PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec g, PetscErrorCode (*func)(Tao, Vec, PetscReal*, Vec, void*), void *ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (g) { PetscValidHeaderSpecific(g,VEC_CLASSID,2); PetscCheckSameComm(tao,1,g,2); CHKERRQ(PetscObjectReference((PetscObject)g)); CHKERRQ(VecDestroy(&tao->gradient)); tao->gradient = g; } if (ctx) tao->user_objgradP = ctx; if (func) tao->ops->computeobjectiveandgradient = func; PetscFunctionReturn(0); } /*@C TaoGetObjectiveAndGradient - Gets a combined objective function and gradient evaluation routine for minimization Not collective Input Parameter: . tao - the Tao context Output Parameters: + g - the vector to internally hold the gradient computation . func - the gradient function - ctx - user-defined context for private data for the gradient evaluation routine Calling sequence of func: $ func (Tao tao, Vec x, PetscReal *f, Vec g, void *ctx); + x - input vector . f - objective value (output) . g - gradient value (output) - ctx - [optional] user-defined function context Level: beginner .seealso: TaoSetObjective(), TaoSetGradient(), TaoSetHessian(), TaoSetObjectiveAndGradient() @*/ PetscErrorCode TaoGetObjectiveAndGradient(Tao tao, Vec *g, PetscErrorCode (**func)(Tao, Vec, PetscReal*, Vec, void*), void **ctx) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (g) *g = tao->gradient; if (func) *func = tao->ops->computeobjectiveandgradient; if (ctx) *ctx = tao->user_objgradP; PetscFunctionReturn(0); } /*@ TaoIsObjectiveDefined - Checks to see if the user has declared an objective-only routine. Useful for determining when it is appropriate to call TaoComputeObjective() or TaoComputeObjectiveAndGradient() Not collective Input Parameter: . tao - the Tao context Output Parameter: . flg - PETSC_TRUE if function routine is set by user, PETSC_FALSE otherwise Level: developer .seealso: TaoSetObjective(), TaoIsGradientDefined(), TaoIsObjectiveAndGradientDefined() @*/ PetscErrorCode TaoIsObjectiveDefined(Tao tao, PetscBool *flg) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (tao->ops->computeobjective == NULL) *flg = PETSC_FALSE; else *flg = PETSC_TRUE; PetscFunctionReturn(0); } /*@ TaoIsGradientDefined - Checks to see if the user has declared an objective-only routine. Useful for determining when it is appropriate to call TaoComputeGradient() or TaoComputeGradientAndGradient() Not Collective Input Parameter: . tao - the Tao context Output Parameter: . flg - PETSC_TRUE if function routine is set by user, PETSC_FALSE otherwise Level: developer .seealso: TaoSetGradient(), TaoIsObjectiveDefined(), TaoIsObjectiveAndGradientDefined() @*/ PetscErrorCode TaoIsGradientDefined(Tao tao, PetscBool *flg) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (tao->ops->computegradient == NULL) *flg = PETSC_FALSE; else *flg = PETSC_TRUE; PetscFunctionReturn(0); } /*@ TaoIsObjectiveAndGradientDefined - Checks to see if the user has declared a joint objective/gradient routine. Useful for determining when it is appropriate to call TaoComputeObjective() or TaoComputeObjectiveAndGradient() Not Collective Input Parameter: . tao - the Tao context Output Parameter: . flg - PETSC_TRUE if function routine is set by user, PETSC_FALSE otherwise Level: developer .seealso: TaoSetObjectiveAndGradient(), TaoIsObjectiveDefined(), TaoIsGradientDefined() @*/ PetscErrorCode TaoIsObjectiveAndGradientDefined(Tao tao, PetscBool *flg) { PetscFunctionBegin; PetscValidHeaderSpecific(tao,TAO_CLASSID,1); if (tao->ops->computeobjectiveandgradient == NULL) *flg = PETSC_FALSE; else *flg = PETSC_TRUE; PetscFunctionReturn(0); }