Actual source code: minsurf1.c

      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: /*
     21:    User-defined application context - contains data needed by the
     22:    application-provided call-back routines, FormFunctionGradient(),
     23:    FormHessian().
     24: */
     25: typedef struct {
     26:   PetscInt  mx, my;
     27:   PetscReal *bottom, *top, *left, *right;
     28: } AppCtx;

     30: /* -------- User-defined Routines --------- */

     32: static PetscErrorCode MSA_BoundaryConditions(AppCtx *);
     33: static PetscErrorCode MSA_InitialPoint(AppCtx *, Vec);
     34: PetscErrorCode FormConstraints(Tao, Vec, Vec, void *);
     35: PetscErrorCode FormJacobian(Tao, Vec, Mat, Mat, void *);

     37: int main(int argc, char **argv)
     38: {
     39:   Vec            x;                 /* solution vector */
     40:   Vec            c;                 /* Constraints function vector */
     41:   Vec            xl,xu;             /* Bounds on the variables */
     42:   PetscBool      flg;               /* A return variable when checking for user options */
     43:   Tao            tao;               /* TAO solver context */
     44:   Mat            J;                 /* Jacobian matrix */
     45:   PetscInt       N;                 /* Number of elements in vector */
     46:   PetscScalar    lb =  PETSC_NINFINITY;      /* lower bound constant */
     47:   PetscScalar    ub =  PETSC_INFINITY;      /* upper bound constant */
     48:   AppCtx         user;                    /* user-defined work context */

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

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

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

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

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

     66:   /* Create appropriate vectors and matrices */
     67:   VecCreateSeq(MPI_COMM_SELF, N, &x);
     68:   VecDuplicate(x, &c);
     69:   MatCreateSeqAIJ(MPI_COMM_SELF, N, N, 7, NULL, &J);

     71:   /* The TAO code begins here */

     73:   /* Create TAO solver and set desired solution method */
     74:   TaoCreate(PETSC_COMM_SELF,&tao);
     75:   TaoSetType(tao,TAOSSILS);

     77:   /* Set data structure */
     78:   TaoSetSolution(tao, x);

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

     84:   /* Set the variable bounds */
     85:   MSA_BoundaryConditions(&user);

     87:   /* Set initial solution guess */
     88:   MSA_InitialPoint(&user, x);

     90:   /* Set Bounds on variables */
     91:   VecDuplicate(x, &xl);
     92:   VecDuplicate(x, &xu);
     93:   VecSet(xl, lb);
     94:   VecSet(xu, ub);
     95:   TaoSetVariableBounds(tao,xl,xu);

     97:   /* Check for any tao command line options */
     98:   TaoSetFromOptions(tao);

    100:   /* Solve the application */
    101:   TaoSolve(tao);

    103:   /* Free Tao data structures */
    104:   TaoDestroy(&tao);

    106:   /* Free PETSc data structures */
    107:   VecDestroy(&x);
    108:   VecDestroy(&xl);
    109:   VecDestroy(&xu);
    110:   VecDestroy(&c);
    111:   MatDestroy(&J);

    113:   /* Free user-created data structures */
    114:   PetscFree(user.bottom);
    115:   PetscFree(user.top);
    116:   PetscFree(user.left);
    117:   PetscFree(user.right);

    119:   PetscFinalize();
    120:   return 0;
    121: }

    123: /* -------------------------------------------------------------------- */

    125: /*  FormConstraints - Evaluates gradient of f.

    127:     Input Parameters:
    128: .   tao  - the TAO_APPLICATION context
    129: .   X    - input vector
    130: .   ptr  - optional user-defined context, as set by TaoSetConstraintsRoutine()

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

    146:   /* Initialize vector to zero */
    147:   VecSet(G, zero);

    149:   /* Get pointers to vector data */
    150:   VecGetArray(X, &x);
    151:   VecGetArray(G, &g);

    153:   /* Compute function over the locally owned part of the mesh */
    154:   for (j=0; j<my; j++) {
    155:     for (i=0; i< mx; i++) {
    156:       row= j*mx + i;

    158:       xc = x[row];
    159:       xlt=xrb=xl=xr=xb=xt=xc;

    161:       if (i==0) { /* left side */
    162:         xl= user->left[j+1];
    163:         xlt = user->left[j+2];
    164:       } else {
    165:         xl = x[row-1];
    166:       }

    168:       if (j==0) { /* bottom side */
    169:         xb=user->bottom[i+1];
    170:         xrb = user->bottom[i+2];
    171:       } else {
    172:         xb = x[row-mx];
    173:       }

    175:       if (i+1 == mx) { /* right side */
    176:         xr=user->right[j+1];
    177:         xrb = user->right[j];
    178:       } else {
    179:         xr = x[row+1];
    180:       }

    182:       if (j+1==0+my) { /* top side */
    183:         xt=user->top[i+1];
    184:         xlt = user->top[i];
    185:       }else {
    186:         xt = x[row+mx];
    187:       }

    189:       if (i>0 && j+1<my) {
    190:         xlt = x[row-1+mx];
    191:       }
    192:       if (j>0 && i+1<mx) {
    193:         xrb = x[row+1-mx];
    194:       }

    196:       d1 = (xc-xl);
    197:       d2 = (xc-xr);
    198:       d3 = (xc-xt);
    199:       d4 = (xc-xb);
    200:       d5 = (xr-xrb);
    201:       d6 = (xrb-xb);
    202:       d7 = (xlt-xl);
    203:       d8 = (xt-xlt);

    205:       df1dxc = d1*hydhx;
    206:       df2dxc = (d1*hydhx + d4*hxdhy);
    207:       df3dxc = d3*hxdhy;
    208:       df4dxc = (d2*hydhx + d3*hxdhy);
    209:       df5dxc = d2*hydhx;
    210:       df6dxc = d4*hxdhy;

    212:       d1 /= hx;
    213:       d2 /= hx;
    214:       d3 /= hy;
    215:       d4 /= hy;
    216:       d5 /= hy;
    217:       d6 /= hx;
    218:       d7 /= hy;
    219:       d8 /= hx;

    221:       f1 = PetscSqrtScalar(1.0 + d1*d1 + d7*d7);
    222:       f2 = PetscSqrtScalar(1.0 + d1*d1 + d4*d4);
    223:       f3 = PetscSqrtScalar(1.0 + d3*d3 + d8*d8);
    224:       f4 = PetscSqrtScalar(1.0 + d3*d3 + d2*d2);
    225:       f5 = PetscSqrtScalar(1.0 + d2*d2 + d5*d5);
    226:       f6 = PetscSqrtScalar(1.0 + d4*d4 + d6*d6);

    228:       df1dxc /= f1;
    229:       df2dxc /= f2;
    230:       df3dxc /= f3;
    231:       df4dxc /= f4;
    232:       df5dxc /= f5;
    233:       df6dxc /= f6;

    235:       g[row] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc)/2.0;
    236:     }
    237:   }

    239:   /* Restore vectors */
    240:   VecRestoreArray(X, &x);
    241:   VecRestoreArray(G, &g);
    242:   PetscLogFlops(67*mx*my);
    243:   return 0;
    244: }

    246: /* ------------------------------------------------------------------- */
    247: /*
    248:    FormJacobian - Evaluates Jacobian matrix.

    250:    Input Parameters:
    251: .  tao  - the TAO_APPLICATION context
    252: .  X    - input vector
    253: .  ptr  - optional user-defined context, as set by TaoSetJacobian()

    255:    Output Parameters:
    256: .  tH    - Jacobian matrix

    258: */
    259: PetscErrorCode FormJacobian(Tao tao, Vec X, Mat H, Mat tHPre, void *ptr)
    260: {
    261:   AppCtx            *user = (AppCtx *) ptr;
    262:   PetscInt          i,j,k,row;
    263:   PetscInt          mx=user->mx, my=user->my;
    264:   PetscInt          col[7];
    265:   PetscReal         hx=1.0/(mx+1), hy=1.0/(my+1), hydhx=hy/hx, hxdhy=hx/hy;
    266:   PetscReal         f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
    267:   PetscReal         hl,hr,ht,hb,hc,htl,hbr;
    268:   const PetscScalar *x;
    269:   PetscScalar       v[7];
    270:   PetscBool         assembled;

    272:   /* Set various matrix options */
    273:   MatSetOption(H,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);
    274:   MatAssembled(H,&assembled);
    275:   if (assembled) MatZeroEntries(H);

    277:   /* Get pointers to vector data */
    278:   VecGetArrayRead(X, &x);

    280:   /* Compute Jacobian over the locally owned part of the mesh */
    281:   for (i=0; i< mx; i++) {
    282:     for (j=0; j<my; j++) {
    283:       row= j*mx + i;

    285:       xc = x[row];
    286:       xlt=xrb=xl=xr=xb=xt=xc;

    288:       /* Left side */
    289:       if (i==0) {
    290:         xl  = user->left[j+1];
    291:         xlt = user->left[j+2];
    292:       } else {
    293:         xl = x[row-1];
    294:       }

    296:       if (j==0) {
    297:         xb  = user->bottom[i+1];
    298:         xrb = user->bottom[i+2];
    299:       } else {
    300:         xb = x[row-mx];
    301:       }

    303:       if (i+1 == mx) {
    304:         xr  = user->right[j+1];
    305:         xrb = user->right[j];
    306:       } else {
    307:         xr = x[row+1];
    308:       }

    310:       if (j+1==my) {
    311:         xt  = user->top[i+1];
    312:         xlt = user->top[i];
    313:       }else {
    314:         xt = x[row+mx];
    315:       }

    317:       if (i>0 && j+1<my) {
    318:         xlt = x[row-1+mx];
    319:       }
    320:       if (j>0 && i+1<mx) {
    321:         xrb = x[row+1-mx];
    322:       }

    324:       d1 = (xc-xl)/hx;
    325:       d2 = (xc-xr)/hx;
    326:       d3 = (xc-xt)/hy;
    327:       d4 = (xc-xb)/hy;
    328:       d5 = (xrb-xr)/hy;
    329:       d6 = (xrb-xb)/hx;
    330:       d7 = (xlt-xl)/hy;
    331:       d8 = (xlt-xt)/hx;

    333:       f1 = PetscSqrtScalar(1.0 + d1*d1 + d7*d7);
    334:       f2 = PetscSqrtScalar(1.0 + d1*d1 + d4*d4);
    335:       f3 = PetscSqrtScalar(1.0 + d3*d3 + d8*d8);
    336:       f4 = PetscSqrtScalar(1.0 + d3*d3 + d2*d2);
    337:       f5 = PetscSqrtScalar(1.0 + d2*d2 + d5*d5);
    338:       f6 = PetscSqrtScalar(1.0 + d4*d4 + d6*d6);

    340:       hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+(-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
    341:       hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+(-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
    342:       ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+(-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
    343:       hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+(-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);

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

    348:       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) +
    349:            (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);

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

    353:       k=0;
    354:       if (j>0) {
    355:         v[k]=hb; col[k]=row - mx; k++;
    356:       }

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

    362:       if (i>0) {
    363:         v[k]= hl; col[k]=row - 1; k++;
    364:       }

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

    368:       if (i < mx-1) {
    369:         v[k]= hr; col[k]=row+1; k++;
    370:       }

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

    376:       if (j < my-1) {
    377:         v[k]= ht; col[k] = row+mx; k++;
    378:       }

    380:       /*
    381:          Set matrix values using local numbering, which was defined
    382:          earlier, in the main routine.
    383:       */
    384:       MatSetValues(H,1,&row,k,col,v,INSERT_VALUES);
    385:     }
    386:   }

    388:   /* Restore vectors */
    389:   VecRestoreArrayRead(X,&x);

    391:   /* Assemble the matrix */
    392:   MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);
    393:   MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);
    394:   PetscLogFlops(199*mx*my);
    395:   return 0;
    396: }

    398: /* ------------------------------------------------------------------- */
    399: /*
    400:    MSA_BoundaryConditions -  Calculates the boundary conditions for
    401:    the region.

    403:    Input Parameter:
    404: .  user - user-defined application context

    406:    Output Parameter:
    407: .  user - user-defined application context
    408: */
    409: static PetscErrorCode MSA_BoundaryConditions(AppCtx * user)
    410: {
    411:   PetscInt        i,j,k,limit=0,maxits=5;
    412:   PetscInt        mx=user->mx,my=user->my;
    413:   PetscInt        bsize=0, lsize=0, tsize=0, rsize=0;
    414:   PetscReal       one=1.0, two=2.0, three=3.0, tol=1e-10;
    415:   PetscReal       fnorm,det,hx,hy,xt=0,yt=0;
    416:   PetscReal       u1,u2,nf1,nf2,njac11,njac12,njac21,njac22;
    417:   PetscReal       b=-0.5, t=0.5, l=-0.5, r=0.5;
    418:   PetscReal       *boundary;

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

    422:   PetscMalloc1(bsize, &user->bottom);
    423:   PetscMalloc1(tsize, &user->top);
    424:   PetscMalloc1(lsize, &user->left);
    425:   PetscMalloc1(rsize, &user->right);

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

    429:   for (j=0; j<4; j++) {
    430:     if (j==0) {
    431:       yt=b;
    432:       xt=l;
    433:       limit=bsize;
    434:       boundary=user->bottom;
    435:     } else if (j==1) {
    436:       yt=t;
    437:       xt=l;
    438:       limit=tsize;
    439:       boundary=user->top;
    440:     } else if (j==2) {
    441:       yt=b;
    442:       xt=l;
    443:       limit=lsize;
    444:       boundary=user->left;
    445:     } else { /* if  (j==3) */
    446:       yt=b;
    447:       xt=r;
    448:       limit=rsize;
    449:       boundary=user->right;
    450:     }

    452:     for (i=0; i<limit; i++) {
    453:       u1=xt;
    454:       u2=-yt;
    455:       for (k=0; k<maxits; k++) {
    456:         nf1=u1 + u1*u2*u2 - u1*u1*u1/three-xt;
    457:         nf2=-u2 - u1*u1*u2 + u2*u2*u2/three-yt;
    458:         fnorm=PetscSqrtScalar(nf1*nf1+nf2*nf2);
    459:         if (fnorm <= tol) break;
    460:         njac11=one+u2*u2-u1*u1;
    461:         njac12=two*u1*u2;
    462:         njac21=-two*u1*u2;
    463:         njac22=-one - u1*u1 + u2*u2;
    464:         det = njac11*njac22-njac21*njac12;
    465:         u1 = u1-(njac22*nf1-njac12*nf2)/det;
    466:         u2 = u2-(njac11*nf2-njac21*nf1)/det;
    467:       }

    469:       boundary[i]=u1*u1-u2*u2;
    470:       if (j==0 || j==1) {
    471:         xt=xt+hx;
    472:       } else { /* if (j==2 || j==3) */
    473:         yt=yt+hy;
    474:       }
    475:     }
    476:   }
    477:   return 0;
    478: }

    480: /* ------------------------------------------------------------------- */
    481: /*
    482:    MSA_InitialPoint - Calculates the initial guess in one of three ways.

    484:    Input Parameters:
    485: .  user - user-defined application context
    486: .  X - vector for initial guess

    488:    Output Parameters:
    489: .  X - newly computed initial guess
    490: */
    491: static PetscErrorCode MSA_InitialPoint(AppCtx * user, Vec X)
    492: {
    493:   PetscInt       start=-1,i,j;
    494:   PetscScalar    zero=0.0;
    495:   PetscBool      flg;

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

    499:   if (flg && start==0) { /* The zero vector is reasonable */
    500:     VecSet(X, zero);
    501:   } else { /* Take an average of the boundary conditions */
    502:     PetscInt    row;
    503:     PetscInt    mx=user->mx,my=user->my;
    504:     PetscScalar *x;

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

    509:     /* Perform local computations */
    510:     for (j=0; j<my; j++) {
    511:       for (i=0; i< mx; i++) {
    512:         row=(j)*mx + (i);
    513:         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;
    514:       }
    515:     }

    517:     /* Restore vectors */
    518:     VecRestoreArray(X,&x);
    519:   }
    520:   return 0;
    521: }

    523: /*TEST

    525:    build:
    526:       requires: !complex

    528:    test:
    529:       args: -tao_monitor -tao_view -tao_type ssils -tao_gttol 1.e-5
    530:       requires: !single

    532:    test:
    533:       suffix: 2
    534:       args: -tao_monitor -tao_view -tao_type ssfls -tao_gttol 1.e-5

    536: TEST*/