Actual source code: minsurf1.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: #include <petsctao.h>

  3: static char  help[] =
  4: "This example demonstrates use of the TAO package to\n\
  5: solve an unconstrained system of equations.  This example is based on a\n\
  6: problem from the MINPACK-2 test suite.  Given a rectangular 2-D domain and\n\
  7: boundary values along the edges of the domain, the objective is to find the\n\
  8: surface with the minimal area that satisfies the boundary conditions.\n\
  9: This application solves this problem using complimentarity -- We are actually\n\
 10: solving the system  (grad f)_i >= 0, if x_i == l_i \n\
 11:                     (grad f)_i = 0, if l_i < x_i < u_i \n\
 12:                     (grad f)_i <= 0, if x_i == u_i  \n\
 13: where f is the function to be minimized. \n\
 14: \n\
 15: The command line options are:\n\
 16:   -mx <xg>, where <xg> = number of grid points in the 1st coordinate direction\n\
 17:   -my <yg>, where <yg> = number of grid points in the 2nd coordinate direction\n\
 18:   -start <st>, where <st> =0 for zero vector, and an average of the boundary conditions otherwise \n\n";

 20: /*T
 21:    Concepts: TAO^Solving a complementarity problem
 22:    Routines: TaoCreate(); TaoDestroy();

 24:    Processors: 1
 25: T*/


 28: /*
 29:    User-defined application context - contains data needed by the
 30:    application-provided call-back routines, FormFunctionGradient(),
 31:    FormHessian().
 32: */
 33: typedef struct {
 34:   PetscInt  mx, my;
 35:   PetscReal *bottom, *top, *left, *right;
 36: } AppCtx;


 39: /* -------- User-defined Routines --------- */

 41: static PetscErrorCode MSA_BoundaryConditions(AppCtx *);
 42: static PetscErrorCode MSA_InitialPoint(AppCtx *, Vec);
 43: PetscErrorCode FormConstraints(Tao, Vec, Vec, void *);
 44: PetscErrorCode FormJacobian(Tao, Vec, Mat, Mat, void *);

 48: int main(int argc, char **argv)
 49: {
 51:   Vec            x;                 /* solution vector */
 52:   Vec            c;                 /* Constraints function vector */
 53:   Vec            xl,xu;             /* Bounds on the variables */
 54:   PetscBool      flg;               /* A return variable when checking for user options */
 55:   Tao            tao;               /* TAO solver context */
 56:   Mat            J;                 /* Jacobian matrix */
 57:   PetscInt       N;                 /* Number of elements in vector */
 58:   PetscScalar    lb =  PETSC_NINFINITY;      /* lower bound constant */
 59:   PetscScalar    ub =  PETSC_INFINITY;      /* upper bound constant */
 60:   AppCtx         user;                    /* user-defined work context */

 62:   /* Initialize PETSc, TAO */
 63:   PetscInitialize(&argc, &argv, (char *)0, help );

 65:   /* Specify default dimension of the problem */
 66:   user.mx = 4; user.my = 4;

 68:   /* Check for any command line arguments that override defaults */
 69:   PetscOptionsGetInt(NULL, "-mx", &user.mx, &flg);
 70:   PetscOptionsGetInt(NULL, "-my", &user.my, &flg);

 72:   /* Calculate any derived values from parameters */
 73:   N = user.mx*user.my;

 75:   PetscPrintf(PETSC_COMM_SELF,"\n---- Minimum Surface Area Problem -----\n");
 76:   PetscPrintf(PETSC_COMM_SELF,"mx:%d, my:%d\n", user.mx,user.my);

 78:   /* Create appropriate vectors and matrices */
 79:   VecCreateSeq(MPI_COMM_SELF, N, &x);
 80:   VecDuplicate(x, &c);
 81:   MatCreateSeqAIJ(MPI_COMM_SELF, N, N, 7, NULL, &J);

 83:   /* The TAO code begins here */

 85:   /* Create TAO solver and set desired solution method */
 86:   TaoCreate(PETSC_COMM_SELF,&tao);
 87:   TaoSetType(tao,TAOSSILS);

 89:   /* Set data structure */
 90:   TaoSetInitialVector(tao, x);

 92:   /*  Set routines for constraints function and Jacobian evaluation */
 93:   TaoSetConstraintsRoutine(tao, c, FormConstraints, (void *)&user);
 94:   TaoSetJacobianRoutine(tao, J, J, FormJacobian, (void *)&user);

 96:   /* Set the variable bounds */
 97:   MSA_BoundaryConditions(&user);

 99:   /* Set initial solution guess */
100:   MSA_InitialPoint(&user, x);

102:   /* Set Bounds on variables */
103:   VecDuplicate(x, &xl);
104:   VecDuplicate(x, &xu);
105:   VecSet(xl, lb);
106:   VecSet(xu, ub);
107:   TaoSetVariableBounds(tao,xl,xu);

109:   /* Check for any tao command line options */
110:   TaoSetFromOptions(tao);

112:   /* Solve the application */
113:   TaoSolve(tao);

115:   /* Free Tao data structures */
116:   TaoDestroy(&tao);

118:   /* Free PETSc data structures */
119:   VecDestroy(&x);
120:   VecDestroy(&xl);
121:   VecDestroy(&xu);
122:   VecDestroy(&c);
123:   MatDestroy(&J);

125:   /* Free user-created data structures */
126:   PetscFree(user.bottom);
127:   PetscFree(user.top);
128:   PetscFree(user.left);
129:   PetscFree(user.right);

131:   PetscFinalize();
132:   return 0;
133: }

135: /* -------------------------------------------------------------------- */

139: /*  FormConstraints - Evaluates gradient of f.

141:     Input Parameters:
142: .   tao  - the TAO_APPLICATION context
143: .   X    - input vector
144: .   ptr  - optional user-defined context, as set by TaoSetConstraintsRoutine()

146:     Output Parameters:
147: .   G - vector containing the newly evaluated gradient
148: */
149: PetscErrorCode FormConstraints(Tao tao, Vec X, Vec G, void *ptr)
150: {
151:   AppCtx         *user = (AppCtx *) ptr;
153:   PetscInt       i,j,row;
154:   PetscInt       mx=user->mx, my=user->my;
155:   PetscReal      hx=1.0/(mx+1),hy=1.0/(my+1), hydhx=hy/hx, hxdhy=hx/hy;
156:   PetscReal      f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
157:   PetscReal      df1dxc,df2dxc,df3dxc,df4dxc,df5dxc,df6dxc;
158:   PetscScalar    zero=0.0;
159:   PetscScalar    *g, *x;

162:   /* Initialize vector to zero */
163:   VecSet(G, zero);

165:   /* Get pointers to vector data */
166:   VecGetArray(X, &x);
167:   VecGetArray(G, &g);

169:   /* Compute function over the locally owned part of the mesh */
170:   for (j=0; j<my; j++){
171:     for (i=0; i< mx; i++){
172:       row= j*mx + i;

174:       xc = x[row];
175:       xlt=xrb=xl=xr=xb=xt=xc;

177:       if (i==0){ /* left side */
178:         xl= user->left[j+1];
179:         xlt = user->left[j+2];
180:       } else {
181:         xl = x[row-1];
182:       }

184:       if (j==0){ /* bottom side */
185:         xb=user->bottom[i+1];
186:         xrb = user->bottom[i+2];
187:       } else {
188:         xb = x[row-mx];
189:       }

191:       if (i+1 == mx){ /* right side */
192:         xr=user->right[j+1];
193:         xrb = user->right[j];
194:       } else {
195:         xr = x[row+1];
196:       }

198:       if (j+1==0+my){ /* top side */
199:         xt=user->top[i+1];
200:         xlt = user->top[i];
201:       }else {
202:         xt = x[row+mx];
203:       }

205:       if (i>0 && j+1<my){
206:         xlt = x[row-1+mx];
207:       }
208:       if (j>0 && i+1<mx){
209:         xrb = x[row+1-mx];
210:       }

212:       d1 = (xc-xl);
213:       d2 = (xc-xr);
214:       d3 = (xc-xt);
215:       d4 = (xc-xb);
216:       d5 = (xr-xrb);
217:       d6 = (xrb-xb);
218:       d7 = (xlt-xl);
219:       d8 = (xt-xlt);

221:       df1dxc = d1*hydhx;
222:       df2dxc = ( d1*hydhx + d4*hxdhy );
223:       df3dxc = d3*hxdhy;
224:       df4dxc = ( d2*hydhx + d3*hxdhy );
225:       df5dxc = d2*hydhx;
226:       df6dxc = d4*hxdhy;

228:       d1 /= hx;
229:       d2 /= hx;
230:       d3 /= hy;
231:       d4 /= hy;
232:       d5 /= hy;
233:       d6 /= hx;
234:       d7 /= hy;
235:       d8 /= hx;

237:       f1 = PetscSqrtScalar( 1.0 + d1*d1 + d7*d7);
238:       f2 = PetscSqrtScalar( 1.0 + d1*d1 + d4*d4);
239:       f3 = PetscSqrtScalar( 1.0 + d3*d3 + d8*d8);
240:       f4 = PetscSqrtScalar( 1.0 + d3*d3 + d2*d2);
241:       f5 = PetscSqrtScalar( 1.0 + d2*d2 + d5*d5);
242:       f6 = PetscSqrtScalar( 1.0 + d4*d4 + d6*d6);

244:       df1dxc /= f1;
245:       df2dxc /= f2;
246:       df3dxc /= f3;
247:       df4dxc /= f4;
248:       df5dxc /= f5;
249:       df6dxc /= f6;

251:       g[row] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc )/2.0;
252:     }
253:   }

255:   /* Restore vectors */
256:   VecRestoreArray(X, &x);
257:   VecRestoreArray(G, &g);
258:   PetscLogFlops(67*mx*my);
259:   return(0);
260: }

262: /* ------------------------------------------------------------------- */
265: /*
266:    FormJacobian - Evaluates Jacobian matrix.

268:    Input Parameters:
269: .  tao  - the TAO_APPLICATION context
270: .  X    - input vector
271: .  ptr  - optional user-defined context, as set by TaoSetJacobian()

273:    Output Parameters:
274: .  tH    - Jacobian matrix

276: */
277: PetscErrorCode FormJacobian(Tao tao, Vec X, Mat H, Mat tHPre, void *ptr)
278: {
279:   AppCtx         *user = (AppCtx *) ptr;
281:   PetscInt       i,j,k,row;
282:   PetscInt       mx=user->mx, my=user->my;
283:   PetscInt       col[7];
284:   PetscReal      hx=1.0/(mx+1), hy=1.0/(my+1), hydhx=hy/hx, hxdhy=hx/hy;
285:   PetscReal      f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
286:   PetscReal      hl,hr,ht,hb,hc,htl,hbr;
287:   PetscScalar    *x, v[7];
288:   PetscBool      assembled;

290:   /* Set various matrix options */
291:   MatSetOption(H,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);
292:   MatAssembled(H,&assembled);
293:   if (assembled){MatZeroEntries(H); }

295:   /* Get pointers to vector data */
296:   VecGetArray(X, &x);

298:   /* Compute Jacobian over the locally owned part of the mesh */
299:   for (i=0; i< mx; i++){
300:     for (j=0; j<my; j++){
301:       row= j*mx + i;

303:       xc = x[row];
304:       xlt=xrb=xl=xr=xb=xt=xc;

306:       /* Left side */
307:       if (i==0){
308:         xl= user->left[j+1];
309:         xlt = user->left[j+2];
310:       } else {
311:         xl = x[row-1];
312:       }

314:       if (j==0){
315:         xb=user->bottom[i+1];
316:         xrb = user->bottom[i+2];
317:       } else {
318:         xb = x[row-mx];
319:       }

321:       if (i+1 == mx){
322:         xr=user->right[j+1];
323:         xrb = user->right[j];
324:       } else {
325:         xr = x[row+1];
326:       }

328:       if (j+1==my){
329:         xt=user->top[i+1];
330:         xlt = user->top[i];
331:       }else {
332:         xt = x[row+mx];
333:       }

335:       if (i>0 && j+1<my){
336:         xlt = x[row-1+mx];
337:       }
338:       if (j>0 && i+1<mx){
339:         xrb = x[row+1-mx];
340:       }


343:       d1 = (xc-xl)/hx;
344:       d2 = (xc-xr)/hx;
345:       d3 = (xc-xt)/hy;
346:       d4 = (xc-xb)/hy;
347:       d5 = (xrb-xr)/hy;
348:       d6 = (xrb-xb)/hx;
349:       d7 = (xlt-xl)/hy;
350:       d8 = (xlt-xt)/hx;

352:       f1 = PetscSqrtScalar( 1.0 + d1*d1 + d7*d7);
353:       f2 = PetscSqrtScalar( 1.0 + d1*d1 + d4*d4);
354:       f3 = PetscSqrtScalar( 1.0 + d3*d3 + d8*d8);
355:       f4 = PetscSqrtScalar( 1.0 + d3*d3 + d2*d2);
356:       f5 = PetscSqrtScalar( 1.0 + d2*d2 + d5*d5);
357:       f6 = PetscSqrtScalar( 1.0 + d4*d4 + d6*d6);


360:       hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+(-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
361:       hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+(-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
362:       ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+(-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
363:       hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+(-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);

365:       hbr = -d2*d5/(f5*f5*f5) - d4*d6/(f6*f6*f6);
366:       htl = -d1*d7/(f1*f1*f1) - d3*d8/(f3*f3*f3);

368:       hc = hydhx*(1.0+d7*d7)/(f1*f1*f1) + hxdhy*(1.0+d8*d8)/(f3*f3*f3) + hydhx*(1.0+d5*d5)/(f5*f5*f5) + hxdhy*(1.0+d6*d6)/(f6*f6*f6) +
369:            (hxdhy*(1.0+d1*d1)+hydhx*(1.0+d4*d4)-2*d1*d4)/(f2*f2*f2) + (hxdhy*(1.0+d2*d2)+hydhx*(1.0+d3*d3)-2*d2*d3)/(f4*f4*f4);

371:       hl/=2.0; hr/=2.0; ht/=2.0; hb/=2.0; hbr/=2.0; htl/=2.0;  hc/=2.0;

373:       k=0;
374:       if (j>0){
375:         v[k]=hb; col[k]=row - mx; k++;
376:       }

378:       if (j>0 && i < mx -1){
379:         v[k]=hbr; col[k]=row - mx+1; k++;
380:       }

382:       if (i>0){
383:         v[k]= hl; col[k]=row - 1; k++;
384:       }

386:       v[k]= hc; col[k]=row; k++;

388:       if (i < mx-1 ){
389:         v[k]= hr; col[k]=row+1; k++;
390:       }

392:       if (i>0 && j < my-1 ){
393:         v[k]= htl; col[k] = row+mx-1; k++;
394:       }

396:       if (j < my-1 ){
397:         v[k]= ht; col[k] = row+mx; k++;
398:       }

400:       /*
401:          Set matrix values using local numbering, which was defined
402:          earlier, in the main routine.
403:       */
404:       MatSetValues(H,1,&row,k,col,v,INSERT_VALUES);
405:     }
406:   }

408:   /* Restore vectors */
409:   VecRestoreArray(X,&x);

411:   /* Assemble the matrix */
412:   MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);
413:   MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);
414:   PetscLogFlops(199*mx*my);
415:   return(0);
416: }

418: /* ------------------------------------------------------------------- */
421: /*
422:    MSA_BoundaryConditions -  Calculates the boundary conditions for
423:    the region.

425:    Input Parameter:
426: .  user - user-defined application context

428:    Output Parameter:
429: .  user - user-defined application context
430: */
431: static PetscErrorCode MSA_BoundaryConditions(AppCtx * user)
432: {
433:   PetscErrorCode  ierr;
434:   PetscInt        i,j,k,limit=0,maxits=5;
435:   PetscInt        mx=user->mx,my=user->my;
436:   PetscInt        bsize=0, lsize=0, tsize=0, rsize=0;
437:   PetscReal       one=1.0, two=2.0, three=3.0, tol=1e-10;
438:   PetscReal       fnorm,det,hx,hy,xt=0,yt=0;
439:   PetscReal       u1,u2,nf1,nf2,njac11,njac12,njac21,njac22;
440:   PetscReal       b=-0.5, t=0.5, l=-0.5, r=0.5;
441:   PetscReal       *boundary;

444:   bsize=mx+2; lsize=my+2; rsize=my+2; tsize=mx+2;

446:   PetscMalloc1(bsize, &user->bottom);
447:   PetscMalloc1(tsize, &user->top);
448:   PetscMalloc1(lsize, &user->left);
449:   PetscMalloc1(rsize, &user->right);

451:   hx= (r-l)/(mx+1); hy=(t-b)/(my+1);

453:   for (j=0; j<4; j++){
454:     if (j==0){
455:       yt=b;
456:       xt=l;
457:       limit=bsize;
458:       boundary=user->bottom;
459:     } else if (j==1){
460:       yt=t;
461:       xt=l;
462:       limit=tsize;
463:       boundary=user->top;
464:     } else if (j==2){
465:       yt=b;
466:       xt=l;
467:       limit=lsize;
468:       boundary=user->left;
469:     } else { /* if  (j==3) */
470:       yt=b;
471:       xt=r;
472:       limit=rsize;
473:       boundary=user->right;
474:     }

476:     for (i=0; i<limit; i++){
477:       u1=xt;
478:       u2=-yt;
479:       for (k=0; k<maxits; k++){
480:         nf1=u1 + u1*u2*u2 - u1*u1*u1/three-xt;
481:         nf2=-u2 - u1*u1*u2 + u2*u2*u2/three-yt;
482:         fnorm=PetscSqrtScalar(nf1*nf1+nf2*nf2);
483:         if (fnorm <= tol) break;
484:         njac11=one+u2*u2-u1*u1;
485:         njac12=two*u1*u2;
486:         njac21=-two*u1*u2;
487:         njac22=-one - u1*u1 + u2*u2;
488:         det = njac11*njac22-njac21*njac12;
489:         u1 = u1-(njac22*nf1-njac12*nf2)/det;
490:         u2 = u2-(njac11*nf2-njac21*nf1)/det;
491:       }

493:       boundary[i]=u1*u1-u2*u2;
494:       if (j==0 || j==1) {
495:         xt=xt+hx;
496:       } else { /* if (j==2 || j==3) */
497:         yt=yt+hy;
498:       }
499:     }
500:   }
501:   return(0);
502: }

504: /* ------------------------------------------------------------------- */
507: /*
508:    MSA_InitialPoint - Calculates the initial guess in one of three ways.

510:    Input Parameters:
511: .  user - user-defined application context
512: .  X - vector for initial guess

514:    Output Parameters:
515: .  X - newly computed initial guess
516: */
517: static PetscErrorCode MSA_InitialPoint(AppCtx * user, Vec X)
518: {
520:   PetscInt       start=-1,i,j;
521:   PetscScalar    zero=0.0;
522:   PetscBool      flg;

525:   PetscOptionsGetInt(NULL,"-start",&start,&flg);

527:   if (flg && start==0){ /* The zero vector is reasonable */
528:     VecSet(X, zero);
529:   } else { /* Take an average of the boundary conditions */
530:     PetscInt    row;
531:     PetscInt    mx=user->mx,my=user->my;
532:     PetscScalar *x;

534:     /* Get pointers to vector data */
535:     VecGetArray(X,&x);

537:     /* Perform local computations */
538:     for (j=0; j<my; j++){
539:       for (i=0; i< mx; i++){
540:         row=(j)*mx + (i);
541:         x[row] = ( ((j+1)*user->bottom[i+1]+(my-j+1)*user->top[i+1])/(my+2)+ ((i+1)*user->left[j+1]+(mx-i+1)*user->right[j+1])/(mx+2))/2.0;
542:       }
543:     }

545:     /* Restore vectors */
546:     VecRestoreArray(X,&x);
547:   }
548:   return(0);
549: }