Actual source code: ex8.c
petsc-3.4.2 2013-07-02
1: #include <petscsnes.h>
2: #include <petscdmda.h>
4: static char help[] = "Parallel version of the minimum surface area problem using DMs.\n\
5: See ex10.c for the serial version. It solves a system of nonlinear equations in mixed\n\
6: complementarity form using semismooth newton algorithm.This example is based on a\n\
7: problem from the MINPACK-2 test suite. Given a rectangular 2-D domain and\n\
8: boundary values along the edges of the domain, the objective is to find the\n\
9: surface with the minimal area that satisfies the boundary conditions.\n\
10: This application solves this problem using complimentarity -- We are actually\n\
11: solving the system (grad f)_i >= 0, if x_i == l_i \n\
12: (grad f)_i = 0, if l_i < x_i < u_i \n\
13: (grad f)_i <= 0, if x_i == u_i \n\
14: where f is the function to be minimized. \n\
15: \n\
16: The command line options are:\n\
17: -da_grid_x <nx>, where <nx> = number of grid points in the 1st coordinate direction\n\
18: -da_grid_y <ny>, where <ny> = number of grid points in the 2nd coordinate direction\n\
19: -start <st>, where <st> =0 for zero vector, and an average of the boundary conditions otherwise\n\
20: -lb <value>, lower bound on the variables\n\
21: -ub <value>, upper bound on the variables\n\n";
23: /*
24: User-defined application context - contains data needed by the
25: application-provided call-back routines, FormJacobian() and
26: FormFunction().
27: */
29: typedef struct {
30: DM da;
31: PetscScalar *bottom, *top, *left, *right;
32: PetscInt mx,my;
33: } AppCtx;
36: /* -------- User-defined Routines --------- */
38: extern PetscErrorCode MSA_BoundaryConditions(AppCtx*);
39: extern PetscErrorCode MSA_InitialPoint(AppCtx*, Vec);
40: extern PetscErrorCode FormGradient(SNES, Vec, Vec, void*);
41: extern PetscErrorCode FormJacobian(SNES, Vec, Mat*, Mat*, MatStructure*,void*);
45: int main(int argc, char **argv)
46: {
47: PetscErrorCode info; /* used to check for functions returning nonzeros */
48: Vec x,r; /* solution and residual vectors */
49: Vec xl,xu; /* Bounds on the variables */
50: PetscBool flg_l,flg_u; /* flags to check if the bounds are set */
51: SNES snes; /* nonlinear solver context */
52: Mat J; /* Jacobian matrix */
53: PetscInt N; /* Number of elements in vector */
54: PetscScalar lb = .05;
55: PetscScalar ub = SNES_VI_INF;
56: AppCtx user; /* user-defined work context */
57: PetscBool flg;
59: /* Initialize PETSc */
60: PetscInitialize(&argc, &argv, (char*)0, help);
62: #if defined(PETSC_USE_COMPLEX)
63: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This example does not work for scalar type complex\n");
64: #endif
66: /* Check if lower and upper bounds are set */
67: info = PetscOptionsGetScalar(NULL, "-lb", &lb, &flg_l);CHKERRQ(info);
68: info = PetscOptionsGetScalar(NULL, "-ub", &ub, &flg_u);CHKERRQ(info);
70: /* Create distributed array to manage the 2d grid */
71: info = DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&user.da);CHKERRQ(info);
72: info = DMDAGetInfo(user.da,PETSC_IGNORE,&user.mx,&user.my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(info);
73: /* Extract global vectors from DMDA; */
74: info = DMCreateGlobalVector(user.da,&x);CHKERRQ(info);
75: info = VecDuplicate(x, &r);CHKERRQ(info);
77: N = user.mx*user.my;
78: info = DMCreateMatrix(user.da,MATAIJ,&J);CHKERRQ(info);
80: /* Create nonlinear solver context */
81: info = SNESCreate(PETSC_COMM_WORLD,&snes);CHKERRQ(info);
83: /* Set function evaluation and Jacobian evaluation routines */
84: info = SNESSetFunction(snes,r,FormGradient,&user);CHKERRQ(info);
85: info = SNESSetJacobian(snes,J,J,FormJacobian,&user);CHKERRQ(info);
87: /* Set the boundary conditions */
88: info = MSA_BoundaryConditions(&user);CHKERRQ(info);
90: /* Set initial solution guess */
91: info = MSA_InitialPoint(&user, x);CHKERRQ(info);
94: /* Set Bounds on variables */
95: info = VecDuplicate(x, &xl);CHKERRQ(info);
96: info = VecDuplicate(x, &xu);CHKERRQ(info);
97: info = VecSet(xl, lb);CHKERRQ(info);
98: info = VecSet(xu, ub);CHKERRQ(info);
100: info = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(info);
102: info = SNESSetFromOptions(snes);CHKERRQ(info);
104: /* Solve the application */
105: info = SNESSolve(snes,NULL,x);CHKERRQ(info);
107: info = PetscOptionsHasName(NULL,"-view_sol",&flg);CHKERRQ(info);
108: if (flg) { info = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(info); }
110: /* Free memory */
111: info = VecDestroy(&x);CHKERRQ(info);
112: info = VecDestroy(&xl);CHKERRQ(info);
113: info = VecDestroy(&xu);CHKERRQ(info);
114: info = VecDestroy(&r);CHKERRQ(info);
115: info = MatDestroy(&J);CHKERRQ(info);
116: info = SNESDestroy(&snes);CHKERRQ(info);
118: /* Free user-created data structures */
119: info = DMDestroy(&user.da);CHKERRQ(info);
120: info = PetscFree(user.bottom);CHKERRQ(info);
121: info = PetscFree(user.top);CHKERRQ(info);
122: info = PetscFree(user.left);CHKERRQ(info);
123: info = PetscFree(user.right);CHKERRQ(info);
125: info = PetscFinalize();
127: return 0;
128: }
130: /* -------------------------------------------------------------------- */
134: /* FormGradient - Evaluates gradient of f.
136: Input Parameters:
137: . snes - the SNES context
138: . X - input vector
139: . ptr - optional user-defined context, as set by SNESSetFunction()
141: Output Parameters:
142: . G - vector containing the newly evaluated gradient
143: */
144: PetscErrorCode FormGradient(SNES snes, Vec X, Vec G, void *ptr)
145: {
146: AppCtx *user = (AppCtx*) ptr;
147: int info;
148: PetscInt i,j;
149: PetscInt mx=user->mx, my=user->my;
150: PetscScalar hx=1.0/(mx+1),hy=1.0/(my+1), hydhx=hy/hx, hxdhy=hx/hy;
151: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
152: PetscScalar df1dxc,df2dxc,df3dxc,df4dxc,df5dxc,df6dxc;
153: PetscScalar **g, **x;
154: PetscInt xs,xm,ys,ym;
155: Vec localX;
158: /* Initialize vector to zero */
159: info = VecSet(G,0.0);CHKERRQ(info);
161: /* Get local vector */
162: info = DMGetLocalVector(user->da,&localX);CHKERRQ(info);
163: /* Get ghost points */
164: info = DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);CHKERRQ(info);
165: info = DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);CHKERRQ(info);
166: /* Get pointer to local vector data */
167: info = DMDAVecGetArray(user->da,localX, &x);CHKERRQ(info);
168: info = DMDAVecGetArray(user->da,G, &g);CHKERRQ(info);
170: info = DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(info);
171: /* Compute function over the locally owned part of the mesh */
172: for (j=ys; j < ys+ym; j++) {
173: for (i=xs; i< xs+xm; i++) {
175: xc = x[j][i];
176: xlt=xrb=xl=xr=xb=xt=xc;
178: if (i==0) { /* left side */
179: xl = user->left[j+1];
180: xlt = user->left[j+2];
181: } else xl = x[j][i-1];
183: if (j==0) { /* bottom side */
184: xb = user->bottom[i+1];
185: xrb = user->bottom[i+2];
186: } else xb = x[j-1][i];
188: if (i+1 == mx) { /* right side */
189: xr = user->right[j+1];
190: xrb = user->right[j];
191: } else xr = x[j][i+1];
193: if (j+1==0+my) { /* top side */
194: xt = user->top[i+1];
195: xlt = user->top[i];
196: } else xt = x[j+1][i];
198: if (i>0 && j+1<my) xlt = x[j+1][i-1]; /* left top side */
199: if (j>0 && i+1<mx) xrb = x[j-1][i+1]; /* right bottom */
201: d1 = (xc-xl);
202: d2 = (xc-xr);
203: d3 = (xc-xt);
204: d4 = (xc-xb);
205: d5 = (xr-xrb);
206: d6 = (xrb-xb);
207: d7 = (xlt-xl);
208: d8 = (xt-xlt);
210: df1dxc = d1*hydhx;
211: df2dxc = (d1*hydhx + d4*hxdhy);
212: df3dxc = d3*hxdhy;
213: df4dxc = (d2*hydhx + d3*hxdhy);
214: df5dxc = d2*hydhx;
215: df6dxc = d4*hxdhy;
217: d1 /= hx;
218: d2 /= hx;
219: d3 /= hy;
220: d4 /= hy;
221: d5 /= hy;
222: d6 /= hx;
223: d7 /= hy;
224: d8 /= hx;
226: f1 = PetscSqrtReal(1.0 + d1*d1 + d7*d7);
227: f2 = PetscSqrtReal(1.0 + d1*d1 + d4*d4);
228: f3 = PetscSqrtReal(1.0 + d3*d3 + d8*d8);
229: f4 = PetscSqrtReal(1.0 + d3*d3 + d2*d2);
230: f5 = PetscSqrtReal(1.0 + d2*d2 + d5*d5);
231: f6 = PetscSqrtReal(1.0 + d4*d4 + d6*d6);
233: df1dxc /= f1;
234: df2dxc /= f2;
235: df3dxc /= f3;
236: df4dxc /= f4;
237: df5dxc /= f5;
238: df6dxc /= f6;
240: g[j][i] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc)/2.0;
242: }
243: }
245: /* Restore vectors */
246: info = DMDAVecRestoreArray(user->da,localX, &x);CHKERRQ(info);
247: info = DMDAVecRestoreArray(user->da,G, &g);CHKERRQ(info);
248: info = DMRestoreLocalVector(user->da,&localX);CHKERRQ(info);
249: info = PetscLogFlops(67*mx*my);CHKERRQ(info);
250: return(0);
251: }
253: /* ------------------------------------------------------------------- */
256: /*
257: FormJacobian - Evaluates Jacobian matrix.
259: Input Parameters:
260: . snes - SNES context
261: . X - input vector
262: . ptr - optional user-defined context, as set by SNESSetJacobian()
264: Output Parameters:
265: . tH - Jacobian matrix
267: */
268: PetscErrorCode FormJacobian(SNES snes, Vec X, Mat *tH, Mat *tHPre, MatStructure *flag, void *ptr)
269: {
270: AppCtx *user = (AppCtx*) ptr;
271: Mat H = *tH;
272: PetscErrorCode info;
273: PetscInt i,j,k;
274: PetscInt mx=user->mx, my=user->my;
275: MatStencil row,col[7];
276: PetscScalar hx=1.0/(mx+1), hy=1.0/(my+1), hydhx=hy/hx, hxdhy=hx/hy;
277: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
278: PetscScalar hl,hr,ht,hb,hc,htl,hbr;
279: PetscScalar **x, v[7];
280: PetscBool assembled;
281: PetscInt xs,xm,ys,ym;
282: Vec localX;
285: /* Set various matrix options */
286: info = MatAssembled(H,&assembled);CHKERRQ(info);
287: if (assembled) {info = MatZeroEntries(H);CHKERRQ(info);}
288: *flag=SAME_NONZERO_PATTERN;
290: /* Get local vector */
291: info = DMGetLocalVector(user->da,&localX);CHKERRQ(info);
292: /* Get ghost points */
293: info = DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);CHKERRQ(info);
294: info = DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);CHKERRQ(info);
296: /* Get pointers to vector data */
297: info = DMDAVecGetArray(user->da,localX, &x);CHKERRQ(info);
299: info = DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(info);
300: /* Compute Jacobian over the locally owned part of the mesh */
301: for (j=ys; j< ys+ym; j++) {
302: for (i=xs; i< xs+xm; i++) {
303: xc = x[j][i];
304: xlt=xrb=xl=xr=xb=xt=xc;
306: /* Left */
307: if (i==0) {
308: xl = user->left[j+1];
309: xlt = user->left[j+2];
310: } else xl = x[j][i-1];
312: /* Bottom */
313: if (j==0) {
314: xb = user->bottom[i+1];
315: xrb = user->bottom[i+2];
316: } else xb = x[j-1][i];
318: /* Right */
319: if (i+1 == mx) {
320: xr = user->right[j+1];
321: xrb = user->right[j];
322: } else xr = x[j][i+1];
324: /* Top */
325: if (j+1==my) {
326: xt = user->top[i+1];
327: xlt = user->top[i];
328: } else xt = x[j+1][i];
330: /* Top left */
331: if (i>0 && j+1<my) xlt = x[j+1][i-1];
333: /* Bottom right */
334: if (j>0 && i+1<mx) xrb = x[j-1][i+1];
336: d1 = (xc-xl)/hx;
337: d2 = (xc-xr)/hx;
338: d3 = (xc-xt)/hy;
339: d4 = (xc-xb)/hy;
340: d5 = (xrb-xr)/hy;
341: d6 = (xrb-xb)/hx;
342: d7 = (xlt-xl)/hy;
343: d8 = (xlt-xt)/hx;
345: f1 = PetscSqrtReal(1.0 + d1*d1 + d7*d7);
346: f2 = PetscSqrtReal(1.0 + d1*d1 + d4*d4);
347: f3 = PetscSqrtReal(1.0 + d3*d3 + d8*d8);
348: f4 = PetscSqrtReal(1.0 + d3*d3 + d2*d2);
349: f5 = PetscSqrtReal(1.0 + d2*d2 + d5*d5);
350: f6 = PetscSqrtReal(1.0 + d4*d4 + d6*d6);
353: hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+
354: (-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
355: hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+
356: (-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
357: ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+
358: (-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
359: hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+
360: (-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);
362: hbr = -d2*d5/(f5*f5*f5) - d4*d6/(f6*f6*f6);
363: htl = -d1*d7/(f1*f1*f1) - d3*d8/(f3*f3*f3);
365: hc = hydhx*(1.0+d7*d7)/(f1*f1*f1) + hxdhy*(1.0+d8*d8)/(f3*f3*f3) +
366: hydhx*(1.0+d5*d5)/(f5*f5*f5) + hxdhy*(1.0+d6*d6)/(f6*f6*f6) +
367: (hxdhy*(1.0+d1*d1)+hydhx*(1.0+d4*d4)-2*d1*d4)/(f2*f2*f2) +
368: (hxdhy*(1.0+d2*d2)+hydhx*(1.0+d3*d3)-2*d2*d3)/(f4*f4*f4);
370: hl/=2.0; hr/=2.0; ht/=2.0; hb/=2.0; hbr/=2.0; htl/=2.0; hc/=2.0;
372: k =0;
373: row.i = i;row.j= j;
374: /* Bottom */
375: if (j>0) {
376: v[k] =hb;
377: col[k].i = i; col[k].j=j-1; k++;
378: }
380: /* Bottom right */
381: if (j>0 && i < mx -1) {
382: v[k] =hbr;
383: col[k].i = i+1; col[k].j = j-1; k++;
384: }
386: /* left */
387: if (i>0) {
388: v[k] = hl;
389: col[k].i = i-1; col[k].j = j; k++;
390: }
392: /* Centre */
393: v[k]= hc; col[k].i= row.i; col[k].j = row.j; k++;
395: /* Right */
396: if (i < mx-1) {
397: v[k] = hr;
398: col[k].i= i+1; col[k].j = j;k++;
399: }
401: /* Top left */
402: if (i>0 && j < my-1) {
403: v[k] = htl;
404: col[k].i = i-1;col[k].j = j+1; k++;
405: }
407: /* Top */
408: if (j < my-1) {
409: v[k] = ht;
410: col[k].i = i; col[k].j = j+1; k++;
411: }
413: info = MatSetValuesStencil(H,1,&row,k,col,v,INSERT_VALUES);CHKERRQ(info);
414: }
415: }
417: /* Assemble the matrix */
418: info = MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);CHKERRQ(info);
419: info = DMDAVecRestoreArray(user->da,localX,&x);CHKERRQ(info);
420: info = MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);CHKERRQ(info);
421: info = DMRestoreLocalVector(user->da,&localX);CHKERRQ(info);
423: info = PetscLogFlops(199*mx*my);CHKERRQ(info);
424: return(0);
425: }
427: /* ------------------------------------------------------------------- */
430: /*
431: MSA_BoundaryConditions - Calculates the boundary conditions for
432: the region.
434: Input Parameter:
435: . user - user-defined application context
437: Output Parameter:
438: . user - user-defined application context
439: */
440: PetscErrorCode MSA_BoundaryConditions(AppCtx * user)
441: {
442: PetscErrorCode info;
443: PetscInt i,j,k,limit=0,maxits=5;
444: PetscInt mx =user->mx,my=user->my;
445: PetscInt bsize=0, lsize=0, tsize=0, rsize=0;
446: PetscScalar one =1.0, two=2.0, three=3.0, tol=1e-10;
447: PetscScalar fnorm,det,hx,hy,xt=0,yt=0;
448: PetscScalar u1,u2,nf1,nf2,njac11,njac12,njac21,njac22;
449: PetscScalar b=-0.5, t=0.5, l=-0.5, r=0.5;
450: PetscScalar *boundary;
453: bsize=mx+2; lsize=my+2; rsize=my+2; tsize=mx+2;
455: info = PetscMalloc(bsize*sizeof(PetscScalar), &user->bottom);CHKERRQ(info);
456: info = PetscMalloc(tsize*sizeof(PetscScalar), &user->top);CHKERRQ(info);
457: info = PetscMalloc(lsize*sizeof(PetscScalar), &user->left);CHKERRQ(info);
458: info = PetscMalloc(rsize*sizeof(PetscScalar), &user->right);CHKERRQ(info);
460: hx= (r-l)/(mx+1); hy=(t-b)/(my+1);
462: for (j=0; j<4; j++) {
463: if (j==0) {
464: yt = b;
465: xt = l;
466: limit = bsize;
467: boundary = user->bottom;
468: } else if (j==1) {
469: yt = t;
470: xt = l;
471: limit = tsize;
472: boundary = user->top;
473: } else if (j==2) {
474: yt = b;
475: xt = l;
476: limit = lsize;
477: boundary = user->left;
478: } else { /* if (j==3) */
479: yt = b;
480: xt = r;
481: limit = rsize;
482: boundary = user->right;
483: }
485: for (i=0; i<limit; i++) {
486: u1=xt;
487: u2=-yt;
488: for (k=0; k<maxits; k++) {
489: nf1 = u1 + u1*u2*u2 - u1*u1*u1/three-xt;
490: nf2 = -u2 - u1*u1*u2 + u2*u2*u2/three-yt;
491: fnorm = PetscSqrtReal(nf1*nf1+nf2*nf2);
492: if (fnorm <= tol) break;
493: njac11 = one+u2*u2-u1*u1;
494: njac12 = two*u1*u2;
495: njac21 = -two*u1*u2;
496: njac22 = -one - u1*u1 + u2*u2;
497: det = njac11*njac22-njac21*njac12;
498: u1 = u1-(njac22*nf1-njac12*nf2)/det;
499: u2 = u2-(njac11*nf2-njac21*nf1)/det;
500: }
502: boundary[i]=u1*u1-u2*u2;
503: if (j==0 || j==1) xt=xt+hx;
504: else yt=yt+hy; /* if (j==2 || j==3) */
505: }
506: }
507: return(0);
508: }
510: /* ------------------------------------------------------------------- */
513: /*
514: MSA_InitialPoint - Calculates the initial guess in one of three ways.
516: Input Parameters:
517: . user - user-defined application context
518: . X - vector for initial guess
520: Output Parameters:
521: . X - newly computed initial guess
522: */
523: PetscErrorCode MSA_InitialPoint(AppCtx * user, Vec X)
524: {
525: PetscErrorCode info;
526: PetscInt start=-1,i,j;
527: PetscScalar zero =0.0;
528: PetscBool flg;
531: info = PetscOptionsGetInt(NULL,"-start",&start,&flg);CHKERRQ(info);
533: if (flg && start==0) { /* The zero vector is reasonable */
535: info = VecSet(X, zero);CHKERRQ(info);
536: /* PLogInfo(user,"Min. Surface Area Problem: Start with 0 vector \n"); */
539: } else { /* Take an average of the boundary conditions */
540: PetscInt mx=user->mx,my=user->my;
541: PetscScalar **x;
542: PetscInt xs,xm,ys,ym;
544: /* Get pointers to vector data */
545: info = DMDAVecGetArray(user->da,X,&x);CHKERRQ(info);
546: info = DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(info);
548: /* Perform local computations */
549: for (j=ys; j<ys+ym; j++) {
550: for (i=xs; i< xs+xm; i++) {
551: x[j][i] = (((j+1)*user->bottom[i+1]+(my-j+1)*user->top[i+1])/(my+2)+
552: ((i+1)*user->left[j+1]+(mx-i+1)*user->right[j+1])/(mx+2))/2.0;
553: }
554: }
556: /* Restore vectors */
557: info = DMDAVecRestoreArray(user->da,X,&x);CHKERRQ(info);
559: }
560: return(0);
561: }