Actual source code: eptorsion3.c

  1: /*$Id: eptorsion.c, v 1.1 2002/08/08 10:30 lopezca@mauddib.mcs.anl.gov $*/

  3: /* Program usage: mpirun -np <proc> eptorsion [-help] [all TAO options] */

  5: /*
  6:   Include "tao.h" so we can use TAO solvers.
  7:   petscda.h for distributed array
  8:   ad_deriv.h for AD gradient
  9: */

 11: #include "petscda.h"
 12: #include "tao.h"
 13: #include "taodaapplication.h"

 15: static char help[] = "This example is based on the Elastic-Plastic Torsion (dept)\n\
 16: problem from the MINPACK-2 test suite.\n\
 17: The command line options are:\n\
 18:   -mx <xg>, where <xg> = number of grid points in the 1st coordinate direction\n\
 19:   -my <yg>, where <yg> = number of grid points in the 2nd coordinate direction\n\
 20:   -nlevels <nlevels>, where <nlevels> = number of levels in multigrid\n\
 21:   -byelement, if computation is made by functions on rectangular elements\n\
 22:   -adic, if AD is used (AD is not used by default)\n\
 23:   -u1 <u1>, where <u1> = upper limit in the 1st coordinate direction\n\
 24:   -u2 <u2>, where <u2> = upper limit in the 2nd coordinate direction\n\
 25:   -par <param>, where <param> = angle of twist per unit length\n\n";

 27: /*T
 28:    Concepts: TAO - Solving a bounded minimization problem
 29:    Routines: TaoInitialize(); TaoFinalize();
 30:    Routines: TaoCreate(); TaoDestroy();
 31:    Routines: DAApplicationCreate(); DAApplicationDestroy();
 32:    Routines: DAAppSetVariableBoundsRoutine();
 33:    Routines: DAAppSetElementObjectiveAndGradientRoutine();
 34:    Routines: DAAppSetElementHessianRoutine();
 35:    Routines: DAAppSetObjectiveAndGradientRoutine();
 36:    Routines: DAAppSetADElementFunctionGradient();
 37:    Routines: DAAppSetHessianRoutine();
 38:    Routines: TaoSetOptions();
 39:    Routines: TaoGetSolutionStatus(); TaoDAAppSolve();
 40:    Routines: DAAppSetMonitor(); TaoView();
 41:    Routines: DAAppGetSolution();
 42:    Routines: DAAppGetInterpolationMatrix();
 43:    Processors: n
 44: T*/

 46: /*
 47:    User-defined application context - contains data needed by the
 48:    application-provided call-back routines.
 49: */
 50: typedef struct {
 51:   InactiveDouble      param;
 52:   InactiveDouble      hx, hy;        /* increment size in both directions */
 53:   InactiveDouble      area;          /* area of the triangles */
 54: } ADFGCtx;

 56: typedef struct {
 57:   PetscReal      param;          /* 'c' parameter */
 58:   PetscReal      u1, u2;         /* domain upper limits (lower limits = 0) */
 59:   double      hx, hy;        /* increment size in both directions */
 60:   double      area;          /* area of the triangles */
 61:   ADFGCtx     fgctx;         /* Used only when an ADIC generated gradient is used */
 62: } AppCtx;
 63: int ad_EPTorsLocalFunction(int[2], DERIV_TYPE[4], DERIV_TYPE*, void*);

 65: /* User-defined routines found in this file */
 66: static int AppCtxInitialize(void *ptr);
 67: static int FormInitialGuess(DA, Vec);

 69: static int EPTorsLocalFunctionGradient(int[2], double x[4], double *f, double g[4], void *ptr);
 70: static int EPTorsLocalHessian(int[2], double x[4], double H[4][4], void *ptr);

 72: static int WholeEPTorsFunctionGradient(TAO_APPLICATION,DA,Vec,double *,Vec,void*);
 73: static int WholeEPTorsHessian(TAO_APPLICATION,DA,Vec,Mat,void*);

 75: static int DASetBounds(TAO_APPLICATION, DA, Vec, Vec, void*);

 77: static int MyGridMonitorBefore(TAO_APPLICATION, DA, int, void *);


 82: int main( int argc, char **argv ) {

 84:   int             info;                           /* used to check for functions returning nonzeros */
 85:   int             mx,my,Nx,Ny;
 86:   double          ff,gnorm;
 87:   int             iter, nlevels;                                                /* multigrid levels */
 88:   DA              DAarray[20];
 89:   Vec             X;
 90:   PetscTruth      flg, PreLoad = PETSC_TRUE;                                               /* flags */
 91:   TaoMethod       method = "tao_gpcg";                                       /* minimization method */
 92:   AppCtx          user;                                                /* user-defined work context */
 93:   TAO_SOLVER      tao;                                                 /* TAO_SOLVER solver context */
 94:   TAO_APPLICATION EPTorsApp;                                            /* The PETSc application */
 95:   TaoTerminateReason reason;
 96:   KSP ksp;
 97:   PC pc;

 99:   /* Initialize TAO */
100:   PetscInitialize(&argc, &argv, (char *)0, help);
101:   TaoInitialize(&argc, &argv, (char *)0, help);

103:   PreLoadBegin(PreLoad,"Solve");
104:   
105:   info = AppCtxInitialize((void*)&user); CHKERRQ(info);

107:   nlevels=5;
108:   info = PetscOptionsGetInt(PETSC_NULL,"-nlevels",&nlevels,&flg); CHKERRQ(info);
109:   mx = my = 11;                               /* these correspond to 10 segments on each dimension */
110:   info = PetscOptionsGetInt(TAO_NULL, "-mx", &mx, &flg); CHKERRQ(info);
111:   info = PetscOptionsGetInt(TAO_NULL, "-my", &my, &flg); CHKERRQ(info);
112:   if (PreLoadIt == 0) {
113:     nlevels = 1; mx = 11; my = 11; }

115:   PetscPrintf(MPI_COMM_WORLD,"\n---- Elastic-Plastic Torsion Problem -----\n\n");

117:   /* Let PETSc determine the vector distribution */
118:   Nx = PETSC_DECIDE; Ny = PETSC_DECIDE;

120:   /* Create distributed array (DA) to manage parallel grid and vectors  */
121:   info = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,mx,
122:                     my,Nx,Ny,1,1,PETSC_NULL,PETSC_NULL,&DAarray[0]); CHKERRQ(info);
123:   for (iter=1;iter<nlevels;iter++){
124:     info = DARefine(DAarray[iter-1],PETSC_COMM_WORLD,&DAarray[iter]); CHKERRQ(info);
125:   }

127:   /* Create TAO solver and set desired solution method */
128:   info = TaoCreate(MPI_COMM_WORLD,method,&tao); CHKERRQ(info);
129:   info = TaoApplicationCreate(PETSC_COMM_WORLD,&EPTorsApp); CHKERRQ(info);
130:   info = TaoAppSetDAApp(EPTorsApp, DAarray, nlevels ); CHKERRQ(info);
131:   /* Sets routines for function, gradient and bounds evaluation */
132:   info = DAAppSetVariableBoundsRoutine(EPTorsApp,DASetBounds,(void *)&user); CHKERRQ(info);

134:   info = PetscOptionsHasName(TAO_NULL, "-byelement", &flg); CHKERRQ(info);
135:   if (flg) {

137:     /* Sets routines for function and gradient evaluation, element by element */
138:     info = PetscOptionsHasName(TAO_NULL, "-adic", &flg); CHKERRQ(info);
139:     if (flg) {
140:       info = DAAppSetADElementFunctionGradient(EPTorsApp,ad_EPTorsLocalFunction,192,(void *)&user.fgctx); CHKERRQ(info);
141:     } else {
142:       info = DAAppSetElementObjectiveAndGradientRoutine(EPTorsApp,EPTorsLocalFunctionGradient,42,(void *)&user); CHKERRQ(info);
143:     }
144:     /* Sets routines for Hessian evaluation, element by element */
145:     info = DAAppSetElementHessianRoutine(EPTorsApp,EPTorsLocalHessian,6,(void*)&user); CHKERRQ(info);

147:   } else {

149:     /* Sets routines for function and gradient evaluation, all in one routine */
150:     info = DAAppSetObjectiveAndGradientRoutine(EPTorsApp,WholeEPTorsFunctionGradient,(void *)&user); CHKERRQ(info);

152:     /* Sets routines for Hessian evaluation, all in one routine */
153:     info = DAAppSetHessianRoutine(EPTorsApp,WholeEPTorsHessian,(void*)&user); CHKERRQ(info);    
154:     
155:   }

157:   info = DAAppSetBeforeMonitor(EPTorsApp,MyGridMonitorBefore,(void*)&user); CHKERRQ(info);
158:   info = PetscOptionsHasName(TAO_NULL,"-tao_monitor", &flg); CHKERRQ(info);
159:   if (flg){
160:     info = DAAppPrintInterpolationError(EPTorsApp); CHKERRQ(info);
161:     info = DAAppPrintStageTimes(EPTorsApp); CHKERRQ(info);
162:   }
163:   info = TaoAppSetRelativeTolerance(EPTorsApp,1.0e-6); CHKERRQ(info);
164:   info = TaoSetTolerances(tao,0,0,0,0); CHKERRQ(info);
165:   info = TaoSetGradientTolerances(tao,0,0,0); CHKERRQ(info);

167:   info = TaoAppGetKSP(EPTorsApp,&ksp);CHKERRQ(info);
168:   info = KSPSetType(ksp,KSPCG); CHKERRQ(info);
169:   info = KSPSetTolerances(ksp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,100);CHKERRQ(info);
170:   info = KSPGetPC(ksp,&pc);CHKERRQ(info);
171:   info = PCSetType(pc,PCBJACOBI);CHKERRQ(info);

173:   /* Check for any tao command line options */
174:   info = TaoSetOptions(EPTorsApp, tao); CHKERRQ(info);

176:   info = DAAppGetSolution(EPTorsApp,0,&X); CHKERRQ(info);
177:   info = FormInitialGuess(DAarray[0],X); CHKERRQ(info);
178:   info = DAAppSetInitialSolution(EPTorsApp,X); CHKERRQ(info);
179:   
180:   /* SOLVE THE APPLICATION */
181:   info = TaoDAAppSolve(EPTorsApp, tao);  CHKERRQ(info);

183:   /* Get information on termination */
184:   info = TaoGetSolutionStatus(tao,&iter,&ff,&gnorm,0,0,&reason); CHKERRQ(info);
185:   if (reason <= 0 ){
186:     PetscPrintf(MPI_COMM_WORLD,"Try a different TAO method, adjust some parameters, or check the function evaluation routines\n");
187:     PetscPrintf(MPI_COMM_WORLD," Iterations: %d,  Function Value: %4.2e, Residual: %4.2e \n",iter,ff,gnorm);
188:   }

190:   info = PetscOptionsHasName(PETSC_NULL,"-view_sol",&flg); CHKERRQ(info);
191:   if (flg){
192:     info = DAAppGetSolution(EPTorsApp,nlevels-1,&X); CHKERRQ(info);
193:     info=VecView(X,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(info);
194:   }

196:   /*  To View TAO solver information */
197:   // info = TaoView(tao); CHKERRQ(info);

199:   /* Free TAO data structures */
200:   info = TaoDestroy(tao); CHKERRQ(info);
201:   info = TaoAppDestroy(EPTorsApp); CHKERRQ(info);

203:   /* Free PETSc data structures */
204:   for (iter=0;iter<nlevels;iter++){
205:     info = DADestroy(DAarray[iter]); CHKERRQ(info);
206:   }

208:   PreLoadEnd();

210:   /* Finalize TAO */
211:   TaoFinalize();
212:   PetscFinalize();

214:   return 0;
215: } /* main */



219: /*----- The following two routines
220:   MyGridMonitorBefore    MyGridMonitorAfter
221:   help diplay info of iterations at every grid level
222: */
225: static int MyGridMonitorBefore(TAO_APPLICATION myapp, DA da, int level, void *ctx) {

227:   AppCtx *user = (AppCtx*)ctx;
228:   int info,mx,my;

230:   info = DAGetInfo(da,PETSC_NULL,&mx,&my,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL,
231:                    PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(info);
232:   user->hx = user->u1 / (mx - 1);
233:   user->hy = user->u2 / (my - 1);
234:   user->area = 0.5 * user->hx * user->hy;
235:   user->fgctx.hx   = user->hx;
236:   user->fgctx.hy   = user->hy;
237:   user->fgctx.area = user->area;
238:   user->fgctx.param = user->param;

240:   PetscPrintf(MPI_COMM_WORLD,"Grid: %d,    mx: %d     my: %d   \n",level,mx,my);

242:   return 0;
243: }


246: /*------- USER-DEFINED: initialize the application context information -------*/
249: /*
250:   AppCtxInitialize - Sets initial values for the application context parameters

252:   Input:
253:     ptr - void user-defined application context

255:   Output:
256:     ptr - user-defined application context with the default or user-provided
257:              parameters
258: */
259: static int AppCtxInitialize(void *ptr) {

261:   AppCtx *user = (AppCtx*)ptr;
262:   PetscTruth    flg;            /* flag for PETSc calls */
263:   int info;

265:   /* Specify default parameters */
266:   user->param = 25.0;
267:   user->u1 = user->u2 = 1.0;

269:   /* Check for command line arguments that override defaults */
270:   info = PetscOptionsGetReal(TAO_NULL, "-par", &user->param, &flg); CHKERRQ(info);
271:   info = PetscOptionsGetReal(TAO_NULL, "-u1", &user->u1, &flg); CHKERRQ(info);
272:   info = PetscOptionsGetReal(TAO_NULL, "-u2", &user->u2, &flg); CHKERRQ(info);

274:   return 0;
275: } /* AppCtxInitialize */

279: static int FormInitialGuess(DA da, Vec X)
280: {
281:   int    info, i, j, mx, my;
282:   int    xs, ys, xm, ym, xe, ye;
283:   PetscReal hx, hy, temp, val;
284:   double **x;

286:   /* Get local mesh boundaries */
287:   info = DAGetInfo(da,PETSC_NULL,&mx,&my,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL,
288:                    PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(info);
289:   hx = 1.0/(mx-1);  hy = 1.0/(my-1);

291:   info = DAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL); CHKERRQ(info);
292:   xe = xs+xm; ye = ys+ym;

294:   info = DAVecGetArray(da, X, (void**)&x); CHKERRQ(info);
295:   /* Compute initial guess over locally owned part of mesh */
296:   for (j=ys; j<ye; j++) {  /*  for (j=0; j<my; j++) */
297:     temp = PetscMin(j+1,my-j)*hy;
298:     for (i=xs; i<xe; i++) {  /*  for (i=0; i<mx; i++) */
299:       val = PetscMin((PetscMin(i+1,mx-i))*hx,temp);
300:       x[j][i] = val;
301:     }
302:   }
303:   info = DAVecRestoreArray(da, X, (void**)&x); CHKERRQ(info);

305:   return 0;
306: }


309: /*------- USER-DEFINED: set the upper and lower bounds for the variables  -------*/

313: /*
314:   FormBounds - Forms bounds on the variables

316:   Input:
317:     user - user-defined application context

319:   Output:
320:     XL - vector of lower bounds
321:     XU - vector of upper bounds
322: */
323: static int DASetBounds(TAO_APPLICATION daapplication, DA da, Vec XL, Vec XU, void *ptr)
324: {
325:   AppCtx *user = (AppCtx*)ptr;
326:   int i, j, info, xs, xm, ys, ym;
327:   double hx, hy, u1, u2, dist, d1, d2, hd, vd;
328:   double **xl, **xu;

330:   hx = user->hx;
331:   hy = user->hy;
332:   u1 = user->u1;
333:   u2 = user->u2;

335:   info = DAVecGetArray(da, XL, (void**)&xl); CHKERRQ(info);
336:   info = DAVecGetArray(da, XU, (void**)&xu); CHKERRQ(info);
337:   info = DAGetCorners(da, &xs, &ys, TAO_NULL, &xm, &ym, TAO_NULL); CHKERRQ(info);

339:   for (j = ys; j < ys+ym; j++){
340:     for (i = xs; i < xs+xm; i++){
341:       d1 = i * hx; d2 = u1 - d1; hd = PetscMin(d1,d2);
342:       d1 = j * hy; d2 = u2 - d1; vd = PetscMin(d1,d2);
343:       dist = PetscMin(hd,vd);
344:       xl[j][i] = -dist;
345:       xu[j][i] = dist;
346:     }
347:   }

349:   info = DAVecRestoreArray(da, XL, (void**)&xl); CHKERRQ(info);
350:   info = DAVecRestoreArray(da, XU, (void**)&xu); CHKERRQ(info);

352:   info = PetscLogFlops(xm * ym * 4); CHKERRQ(info);
353:   return 0;

355: } /* DASetBounds */


360: /*
361:   EPTorsLocalFunctionGradient - Evaluates function and gradient over the 
362:       local rectangular element

364:   Input:
365:     coor - vector with the indices of the position of current element
366:              in the first, second and third directions
367:     x - current point (values over the current rectangular element)
368:     df - degrees of freedom at each point
369:     ptr - user-defined application context

371:   Output:
372:     f - value of the objective funtion at the local rectangular element
373:     g - gradient of the local function
374: */
375: static int EPTorsLocalFunctionGradient(int coor[2], double x[4], double *f, double g[4], void *ptr) {

377:   AppCtx *user = (AppCtx*)ptr;

379:   double fquad, flin;
380:   double hx, hy, dvdx, dvdy, area;
381:   double cdiv3, cnt;

383:   cdiv3 = user->param / 3.0;
384:   hx = user->hx;
385:   hy = user->hy;
386:   area = user->area;
387:   cnt = area * cdiv3;

389:   /* lower triangle contribution */
390:   dvdx = (x[0] - x[1]) / hx;
391:   dvdy = (x[0] - x[2]) / hy;
392:   fquad = dvdx * dvdx + dvdy * dvdy;
393:   flin = x[0] + x[1] + x[2];

395:   dvdx = 0.5 * dvdx * hy;
396:   dvdy = 0.5 * dvdy * hx;
397:   g[0] = dvdx + dvdy - cnt;
398:   g[1] = -dvdx - 2.0 * cnt;
399:   g[2] = -dvdy - 2.0 * cnt;

401:   /* upper triangle contribution */
402:   dvdx = (x[3] - x[2]) / hx;
403:   dvdy = (x[3] - x[1]) / hy;
404:   fquad += dvdx * dvdx + dvdy * dvdy;
405:   flin += x[1] + x[2] + x[3];

407:   dvdx = 0.5 * dvdx * hy;
408:   dvdy = 0.5 * dvdy * hx;
409:   g[1] += -dvdy;
410:   g[2] += -dvdx;
411:   g[3] = dvdx + dvdy - cnt;

413:   *f = area * (0.5 * fquad - flin * cdiv3);

415:   return 0;
416: } /* EPTorsLocalFunctionGradient */



420: /*------- USER-DEFINED: routine to evaluate the Hessian
421:            at a local (rectangular element) level       -------*/
424: /*
425:   EPTorsLocalHessian - Computes the Hessian of the local (partial) function
426:          defined over the current rectangle

428:   Input:
429:     coor - vector with the indices of the position of current element
430:              in the first, second and third directions
431:     x - current local solution (over the rectangle only)
432:     df - degrees of freedom at each point
433:     ptr - user-defined application context

435:   Output:
436:     H - Hessian matrix of the local function (wrt the four
437:            points of the rectangle only)
438: */
439: static int EPTorsLocalHessian(int coor[2], double x[4], double H[4][4], void *ptr) {

441:   AppCtx *user = (AppCtx*)ptr;
442:   double hx, hy, dxdy, dydx;
443:   double diagxy, bandxy, bandyx;

445:   hx = user->hx;
446:   hy = user->hy;
447:   dxdy = hx/hy;
448:   dydx = hy/hx;
449:   diagxy = 0.5 * (dxdy + dydx);
450:   bandxy = -0.5 * dxdy;
451:   bandyx = -0.5 * dydx;

453:           /* Hessian contribution at 0,0 */
454:   H[0][0] = diagxy;
455:   H[0][1] =  H[1][0] = bandyx;
456:   H[0][2] =  H[2][0] = bandxy;
457:   H[0][3] =  H[3][0] = 0.0;

459:           /* Hessian contribution at 1,0 */
460:   H[1][1] = diagxy;
461:   H[1][2] =  H[2][1] = 0.0;
462:   H[1][3] =  H[3][1] = bandxy;

464:           /* Hessian contribution at 0,1 */
465:   H[2][2] = diagxy;
466:   H[2][3] =  H[3][2] = bandyx;

468:           /* Hessian contribution at 1,1 */
469:   H[3][3] = diagxy;

471:   return 0;

473: } /* EPTorsLocalHessian */


476: /*------- USER-DEFINED: routine to evaluate the function 
477:           and gradient at the whole grid             -------*/
480: /*
481:   WholeEPTorsFunctionGradient - Evaluates function and gradient over the 
482:       whole grid

484:   Input:
485:     daapplication - TAO application object
486:     da  - distributed array
487:     X   - the current point, at which the function and gradient are evaluated
488:     ptr - user-defined application context

490:   Output:
491:     f - value of the objective funtion at X
492:     G - gradient at X
493: */
494: static int WholeEPTorsFunctionGradient(TAO_APPLICATION daapplication, DA da, Vec X, double *f, Vec G, void *ptr) {

496:   AppCtx *user = (AppCtx*)ptr;
497:   Vec localX, localG;
498:   int info, i, j;
499:   int xs, xm, gxs, gxm, xe, ys, ym, gys, gym, ye;
500:   double **x, **g;
501:   double floc = 0.0;
502:   PetscScalar zero = 0.0;

504:   double fquad, flin;
505:   double hx, hy, dvdx, dvdy, area;
506:   double cdiv3, cnt;

508:   cdiv3 = user->param / 3.0;
509:   hx = user->hx;
510:   hy = user->hy;
511:   area = user->area;
512:   cnt = area * cdiv3;

514:   info = DAGetLocalVector(da, &localX); CHKERRQ(info);
515:   info = DAGetLocalVector(da, &localG); CHKERRQ(info);
516:   info = VecSet(G, zero); CHKERRQ(info);
517:   info = VecSet(localG, zero); CHKERRQ(info);

519:   info = DAGlobalToLocalBegin(da, X, INSERT_VALUES, localX); CHKERRQ(info);
520:   info = DAGlobalToLocalEnd(da, X, INSERT_VALUES, localX); CHKERRQ(info);

522:   info = DAVecGetArray(da, localX, (void**)&x); CHKERRQ(info);
523:   info = DAVecGetArray(da, localG, (void**)&g); CHKERRQ(info);

525:   info = DAGetCorners(da, &xs, &ys, TAO_NULL, &xm, &ym, TAO_NULL); CHKERRQ(info);
526:   info = DAGetGhostCorners(da, &gxs, &gys, TAO_NULL, &gxm, &gym, TAO_NULL); CHKERRQ(info);

528:   xe = gxs + gxm - 1;
529:   ye = gys + gym - 1;
530:   for (j = ys; j < ye; j++) {
531:     for (i = xs; i < xe; i++) {

533:       /* lower triangle contribution */
534:       dvdx = (x[j][i] - x[j][i+1]) / hx;  
535:       dvdy = (x[j][i] - x[j+1][i]) / hy;
536:       fquad = dvdx * dvdx + dvdy * dvdy;
537:       flin = x[j][i] + x[j][i+1] + x[j+1][i];

539:       dvdx = 0.5 * dvdx * hy;
540:       dvdy = 0.5 * dvdy * hx;
541:       g[j][i] += dvdx + dvdy - cnt;
542:       g[j][i+1] += -dvdx - 2.0 * cnt;
543:       g[j+1][i] += -dvdy - 2.0 * cnt;

545:       /* upper triangle contribution */
546:       dvdx = (x[j+1][i+1] - x[j+1][i]) / hx;
547:       dvdy = (x[j+1][i+1] - x[j][i+1]) / hy;
548:       fquad += dvdx * dvdx + dvdy * dvdy;
549:       flin += x[j][i+1] + x[j+1][i] + x[j+1][i+1];

551:       dvdx = 0.5 * dvdx * hy;
552:       dvdy = 0.5 * dvdy * hx;
553:       g[j][i+1] += -dvdy;
554:       g[j+1][i] += -dvdx;
555:       g[j+1][i+1] += dvdx + dvdy - cnt;

557:       floc += area * (0.5 * fquad - flin * cdiv3);

559:     }
560:   }

562:   info = MPI_Allreduce(&floc, f, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); CHKERRQ(info);

564:   info = DAVecRestoreArray(da, localX, (void**)&x); CHKERRQ(info);
565:   info = DAVecRestoreArray(da, localG, (void**)&g); CHKERRQ(info);

567:   info = DALocalToGlobalBegin(da, localG, G); CHKERRQ(info);
568:   info = DALocalToGlobalEnd(da, localG, G); CHKERRQ(info);

570:   info = DARestoreLocalVector(da, &localX); CHKERRQ(info);
571:   info = DARestoreLocalVector(da, &localG); CHKERRQ(info);

573:   info = PetscLogFlops((xe-xs) * (ye-ys) * 47 + 2); CHKERRQ(info);
574:   return 0;
575: } /* WholeEPTorsFunctionGradient */


578: /*------- USER-DEFINED: routine to evaluate the Hessian 
579:           at the whole grid             -------*/
582: /*
583:   WholeEPTorsHessian - Evaluates Hessian over the whole grid

585:   Input:
586:     daapplication - TAO application object
587:     da  - distributed array
588:     X   - the current point, at which the function and gradient are evaluated
589:     ptr - user-defined application context

591:   Output:
592:     H - Hessian at X
593: */
594: static int WholeEPTorsHessian(TAO_APPLICATION daapplication, DA da, Vec X, Mat H, void *ptr) {

596:   AppCtx *user = (AppCtx*)ptr;
597:   int info, i, j, ind[4];
598:   int xs, xm, gxs, gxm, xe, ys, ym, gys, gym, ye;
599:   double smallH[4][4];

601:   double hx, hy, dxdy, dydx;
602:   double diagxy, bandxy, bandyx;
603:   PetscTruth assembled;

605:   hx = user->hx;
606:   hy = user->hy;
607:   dxdy = hx/hy;
608:   dydx = hy/hx;
609:   diagxy = 0.5 * (dxdy + dydx);
610:   bandxy = -0.5 * dxdy;
611:   bandyx = -0.5 * dydx;

613:   info = MatAssembled(H,&assembled); CHKERRQ(info);
614:   if (assembled){info = MatZeroEntries(H);  CHKERRQ(info);}


617:   info = DAGetCorners(da, &xs, &ys, TAO_NULL, &xm, &ym, TAO_NULL); CHKERRQ(info);
618:   info = DAGetGhostCorners(da, &gxs, &gys, TAO_NULL, &gxm, &gym, TAO_NULL); CHKERRQ(info);

620:   xe = gxs + gxm - 1;
621:   ye = gys + gym - 1;
622:   for (j = ys; j < ye; j++) {
623:     for (i = xs; i < xe; i++) {

625:           /* Hessian contribution at 0,0 */
626:       smallH[0][0] = diagxy;
627:       smallH[0][1] = smallH[1][0] = bandyx;
628:       smallH[0][2] = smallH[2][0] = bandxy;
629:       smallH[0][3] = smallH[3][0] = 0.0;

631:           /* Hessian contribution at 1,0 */
632:       smallH[1][1] = diagxy;
633:       smallH[1][2] = smallH[2][1] = 0.0;
634:       smallH[1][3] = smallH[3][1] = bandxy;

636:           /* Hessian contribution at 0,1 */
637:       smallH[2][2] = diagxy;
638:       smallH[2][3] = smallH[3][2] = bandyx;

640:           /* Hessian contribution at 1,1 */
641:       smallH[3][3] = diagxy;

643:       ind[0] = (j-gys) * gxm + (i-gxs);
644:       ind[1] = ind[0] + 1;
645:       ind[2] = ind[0] + gxm;
646:       ind[3] = ind[2] + 1;
647:       info = MatSetValuesLocal(H,4,ind,4,ind,(PetscScalar*)smallH,ADD_VALUES); CHKERRQ(info);

649:     }
650:   }

652:   info = MatAssemblyBegin(H, MAT_FINAL_ASSEMBLY); CHKERRQ(info);
653:   info = MatAssemblyEnd(H, MAT_FINAL_ASSEMBLY); CHKERRQ(info);
654:   info = MatSetOption(H, MAT_SYMMETRIC); CHKERRQ(info);


657:   info = PetscLogFlops(6); CHKERRQ(info);
658:   return 0;

660: } /* WholeEPTorsHessian */