#include /*I "petsctao.h" I*/ #include #include /* For finited difference computations of the Hessian, we use PETSc's SNESComputeJacobianDefault */ #undef __FUNCT__ #define __FUNCT__ "Fsnes" static PetscErrorCode Fsnes(SNES snes ,Vec X,Vec G,void*ctx) { PetscErrorCode ierr; Tao tao = (Tao)ctx; PetscFunctionBegin; PetscValidHeaderSpecific(ctx,TAO_CLASSID,4); ierr=TaoComputeGradient(tao,X,G);CHKERRQ(ierr); PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "TaoDefaultComputeGradient" /*@C TaoDefaultComputeGradient - computes the gradient using finite differences. Collective on Tao Input Parameters: + tao - the Tao context . X - compute gradient at this point - dummy - not used Output Parameters: . G - Gradient Vector Options Database Key: + -tao_fd_gradient - Activates TaoDefaultComputeGradient() - -tao_fd_delta - change in x used to calculate finite differences Level: advanced Note: This routine is slow and expensive, and is not currently optimized to take advantage of sparsity in the problem. Although TaoAppDefaultComputeGradient is not recommended for general use in large-scale applications, It can be useful in checking the correctness of a user-provided gradient. Use the tao method TAOTEST to get an indication of whether your gradient is correct. Note: This finite difference gradient evaluation can be set using the routine TaoSetGradientRoutine() or by using the command line option -tao_fd_gradient .seealso: TaoSetGradientRoutine() @*/ PetscErrorCode TaoDefaultComputeGradient(Tao tao,Vec X,Vec G,void *dummy) { PetscScalar *x,*g; PetscReal f, f2; PetscErrorCode ierr; PetscInt low,high,N,i; PetscBool flg; PetscReal h=PETSC_SQRT_MACHINE_EPSILON; PetscFunctionBegin; ierr = TaoComputeObjective(tao, X,&f);CHKERRQ(ierr); ierr = PetscOptionsGetReal(NULL,"-tao_fd_delta",&h,&flg);CHKERRQ(ierr); ierr = VecGetSize(X,&N);CHKERRQ(ierr); ierr = VecGetOwnershipRange(X,&low,&high);CHKERRQ(ierr); ierr = VecGetArray(G,&g);CHKERRQ(ierr); for (i=0;i=low && i=low && i=low && i