Actual source code: minsurf1.c

petsc-3.8.4 2018-03-24
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 *);

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

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

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

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

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

 73:   PetscPrintf(PETSC_COMM_SELF,"\n---- Minimum Surface Area Problem -----\n");
 74:   PetscPrintf(PETSC_COMM_SELF,"mx:%D, my:%D\n", user.mx,user.my);

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

 81:   /* The TAO code begins here */

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

 87:   /* Set data structure */
 88:   TaoSetInitialVector(tao, x);

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

 94:   /* Set the variable bounds */
 95:   MSA_BoundaryConditions(&user);

 97:   /* Set initial solution guess */
 98:   MSA_InitialPoint(&user, x);

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

107:   /* Check for any tao command line options */
108:   TaoSetFromOptions(tao);

110:   /* Solve the application */
111:   TaoSolve(tao);

113:   /* Free Tao data structures */
114:   TaoDestroy(&tao);

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

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

129:   PetscFinalize();
130:   return ierr;
131: }

133: /* -------------------------------------------------------------------- */

135: /*  FormConstraints - Evaluates gradient of f.

137:     Input Parameters:
138: .   tao  - the TAO_APPLICATION context
139: .   X    - input vector
140: .   ptr  - optional user-defined context, as set by TaoSetConstraintsRoutine()

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

158:   /* Initialize vector to zero */
159:   VecSet(G, zero);

161:   /* Get pointers to vector data */
162:   VecGetArray(X, &x);
163:   VecGetArray(G, &g);

165:   /* Compute function over the locally owned part of the mesh */
166:   for (j=0; j<my; j++){
167:     for (i=0; i< mx; i++){
168:       row= j*mx + i;

170:       xc = x[row];
171:       xlt=xrb=xl=xr=xb=xt=xc;

173:       if (i==0){ /* left side */
174:         xl= user->left[j+1];
175:         xlt = user->left[j+2];
176:       } else {
177:         xl = x[row-1];
178:       }

180:       if (j==0){ /* bottom side */
181:         xb=user->bottom[i+1];
182:         xrb = user->bottom[i+2];
183:       } else {
184:         xb = x[row-mx];
185:       }

187:       if (i+1 == mx){ /* right side */
188:         xr=user->right[j+1];
189:         xrb = user->right[j];
190:       } else {
191:         xr = x[row+1];
192:       }

194:       if (j+1==0+my){ /* top side */
195:         xt=user->top[i+1];
196:         xlt = user->top[i];
197:       }else {
198:         xt = x[row+mx];
199:       }

201:       if (i>0 && j+1<my){
202:         xlt = x[row-1+mx];
203:       }
204:       if (j>0 && i+1<mx){
205:         xrb = x[row+1-mx];
206:       }

208:       d1 = (xc-xl);
209:       d2 = (xc-xr);
210:       d3 = (xc-xt);
211:       d4 = (xc-xb);
212:       d5 = (xr-xrb);
213:       d6 = (xrb-xb);
214:       d7 = (xlt-xl);
215:       d8 = (xt-xlt);

217:       df1dxc = d1*hydhx;
218:       df2dxc = ( d1*hydhx + d4*hxdhy );
219:       df3dxc = d3*hxdhy;
220:       df4dxc = ( d2*hydhx + d3*hxdhy );
221:       df5dxc = d2*hydhx;
222:       df6dxc = d4*hxdhy;

224:       d1 /= hx;
225:       d2 /= hx;
226:       d3 /= hy;
227:       d4 /= hy;
228:       d5 /= hy;
229:       d6 /= hx;
230:       d7 /= hy;
231:       d8 /= hx;

233:       f1 = PetscSqrtScalar( 1.0 + d1*d1 + d7*d7);
234:       f2 = PetscSqrtScalar( 1.0 + d1*d1 + d4*d4);
235:       f3 = PetscSqrtScalar( 1.0 + d3*d3 + d8*d8);
236:       f4 = PetscSqrtScalar( 1.0 + d3*d3 + d2*d2);
237:       f5 = PetscSqrtScalar( 1.0 + d2*d2 + d5*d5);
238:       f6 = PetscSqrtScalar( 1.0 + d4*d4 + d6*d6);

240:       df1dxc /= f1;
241:       df2dxc /= f2;
242:       df3dxc /= f3;
243:       df4dxc /= f4;
244:       df5dxc /= f5;
245:       df6dxc /= f6;

247:       g[row] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc )/2.0;
248:     }
249:   }

251:   /* Restore vectors */
252:   VecRestoreArray(X, &x);
253:   VecRestoreArray(G, &g);
254:   PetscLogFlops(67*mx*my);
255:   return(0);
256: }

258: /* ------------------------------------------------------------------- */
259: /*
260:    FormJacobian - Evaluates Jacobian matrix.

262:    Input Parameters:
263: .  tao  - the TAO_APPLICATION context
264: .  X    - input vector
265: .  ptr  - optional user-defined context, as set by TaoSetJacobian()

267:    Output Parameters:
268: .  tH    - Jacobian matrix

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

284:   /* Set various matrix options */
285:   MatSetOption(H,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);
286:   MatAssembled(H,&assembled);
287:   if (assembled){MatZeroEntries(H); }

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

292:   /* Compute Jacobian over the locally owned part of the mesh */
293:   for (i=0; i< mx; i++){
294:     for (j=0; j<my; j++){
295:       row= j*mx + i;

297:       xc = x[row];
298:       xlt=xrb=xl=xr=xb=xt=xc;

300:       /* Left side */
301:       if (i==0){
302:         xl= user->left[j+1];
303:         xlt = user->left[j+2];
304:       } else {
305:         xl = x[row-1];
306:       }

308:       if (j==0){
309:         xb=user->bottom[i+1];
310:         xrb = user->bottom[i+2];
311:       } else {
312:         xb = x[row-mx];
313:       }

315:       if (i+1 == mx){
316:         xr=user->right[j+1];
317:         xrb = user->right[j];
318:       } else {
319:         xr = x[row+1];
320:       }

322:       if (j+1==my){
323:         xt=user->top[i+1];
324:         xlt = user->top[i];
325:       }else {
326:         xt = x[row+mx];
327:       }

329:       if (i>0 && j+1<my){
330:         xlt = x[row-1+mx];
331:       }
332:       if (j>0 && i+1<mx){
333:         xrb = x[row+1-mx];
334:       }


337:       d1 = (xc-xl)/hx;
338:       d2 = (xc-xr)/hx;
339:       d3 = (xc-xt)/hy;
340:       d4 = (xc-xb)/hy;
341:       d5 = (xrb-xr)/hy;
342:       d6 = (xrb-xb)/hx;
343:       d7 = (xlt-xl)/hy;
344:       d8 = (xlt-xt)/hx;

346:       f1 = PetscSqrtScalar( 1.0 + d1*d1 + d7*d7);
347:       f2 = PetscSqrtScalar( 1.0 + d1*d1 + d4*d4);
348:       f3 = PetscSqrtScalar( 1.0 + d3*d3 + d8*d8);
349:       f4 = PetscSqrtScalar( 1.0 + d3*d3 + d2*d2);
350:       f5 = PetscSqrtScalar( 1.0 + d2*d2 + d5*d5);
351:       f6 = PetscSqrtScalar( 1.0 + d4*d4 + d6*d6);


354:       hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+(-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
355:       hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+(-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
356:       ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+(-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
357:       hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+(-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);

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

362:       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) +
363:            (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);

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

367:       k=0;
368:       if (j>0){
369:         v[k]=hb; col[k]=row - mx; k++;
370:       }

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

376:       if (i>0){
377:         v[k]= hl; col[k]=row - 1; k++;
378:       }

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

382:       if (i < mx-1 ){
383:         v[k]= hr; col[k]=row+1; k++;
384:       }

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

390:       if (j < my-1 ){
391:         v[k]= ht; col[k] = row+mx; k++;
392:       }

394:       /*
395:          Set matrix values using local numbering, which was defined
396:          earlier, in the main routine.
397:       */
398:       MatSetValues(H,1,&row,k,col,v,INSERT_VALUES);
399:     }
400:   }

402:   /* Restore vectors */
403:   VecRestoreArray(X,&x);

405:   /* Assemble the matrix */
406:   MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);
407:   MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);
408:   PetscLogFlops(199*mx*my);
409:   return(0);
410: }

412: /* ------------------------------------------------------------------- */
413: /*
414:    MSA_BoundaryConditions -  Calculates the boundary conditions for
415:    the region.

417:    Input Parameter:
418: .  user - user-defined application context

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

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

438:   PetscMalloc1(bsize, &user->bottom);
439:   PetscMalloc1(tsize, &user->top);
440:   PetscMalloc1(lsize, &user->left);
441:   PetscMalloc1(rsize, &user->right);

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

445:   for (j=0; j<4; j++){
446:     if (j==0){
447:       yt=b;
448:       xt=l;
449:       limit=bsize;
450:       boundary=user->bottom;
451:     } else if (j==1){
452:       yt=t;
453:       xt=l;
454:       limit=tsize;
455:       boundary=user->top;
456:     } else if (j==2){
457:       yt=b;
458:       xt=l;
459:       limit=lsize;
460:       boundary=user->left;
461:     } else { /* if  (j==3) */
462:       yt=b;
463:       xt=r;
464:       limit=rsize;
465:       boundary=user->right;
466:     }

468:     for (i=0; i<limit; i++){
469:       u1=xt;
470:       u2=-yt;
471:       for (k=0; k<maxits; k++){
472:         nf1=u1 + u1*u2*u2 - u1*u1*u1/three-xt;
473:         nf2=-u2 - u1*u1*u2 + u2*u2*u2/three-yt;
474:         fnorm=PetscSqrtScalar(nf1*nf1+nf2*nf2);
475:         if (fnorm <= tol) break;
476:         njac11=one+u2*u2-u1*u1;
477:         njac12=two*u1*u2;
478:         njac21=-two*u1*u2;
479:         njac22=-one - u1*u1 + u2*u2;
480:         det = njac11*njac22-njac21*njac12;
481:         u1 = u1-(njac22*nf1-njac12*nf2)/det;
482:         u2 = u2-(njac11*nf2-njac21*nf1)/det;
483:       }

485:       boundary[i]=u1*u1-u2*u2;
486:       if (j==0 || j==1) {
487:         xt=xt+hx;
488:       } else { /* if (j==2 || j==3) */
489:         yt=yt+hy;
490:       }
491:     }
492:   }
493:   return(0);
494: }

496: /* ------------------------------------------------------------------- */
497: /*
498:    MSA_InitialPoint - Calculates the initial guess in one of three ways.

500:    Input Parameters:
501: .  user - user-defined application context
502: .  X - vector for initial guess

504:    Output Parameters:
505: .  X - newly computed initial guess
506: */
507: static PetscErrorCode MSA_InitialPoint(AppCtx * user, Vec X)
508: {
510:   PetscInt       start=-1,i,j;
511:   PetscScalar    zero=0.0;
512:   PetscBool      flg;

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

517:   if (flg && start==0){ /* The zero vector is reasonable */
518:     VecSet(X, zero);
519:   } else { /* Take an average of the boundary conditions */
520:     PetscInt    row;
521:     PetscInt    mx=user->mx,my=user->my;
522:     PetscScalar *x;

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

527:     /* Perform local computations */
528:     for (j=0; j<my; j++){
529:       for (i=0; i< mx; i++){
530:         row=(j)*mx + (i);
531:         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;
532:       }
533:     }

535:     /* Restore vectors */
536:     VecRestoreArray(X,&x);
537:   }
538:   return(0);
539: }