Actual source code: ex58.c
petsc-dev 2014-02-02
1: #include <petscsnes.h>
2: #include <petscdmda.h>
4: static const char help[] = "Parallel version of the minimum surface area problem in 2D using DMDA.\n\
5: It solves a system of nonlinear equations in mixed\n\
6: complementarity form.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: /*
30: This is a new version of the ../tests/ex8.c code
32: Run, for example, with the options ./ex58 -snes_vi_monitor -ksp_monitor -mg_levels_ksp_monitor -pc_type mg -pc_mg_levels 2 -pc_mg_galerkin -ksp_type fgmres
34: Or to run with grid sequencing on the nonlinear problem (note that you do not need to provide the number of
35: multigrid levels, it will be determined automatically based on the number of refinements done)
37: ./ex58 -pc_type mg -ksp_monitor -snes_view -pc_mg_galerkin -snes_grid_sequence 3
38: -mg_levels_ksp_monitor -snes_vi_monitor -mg_levels_pc_type sor -pc_mg_type full
41: */
43: typedef struct {
44: PetscScalar *bottom, *top, *left, *right;
45: PetscScalar lb,ub;
46: } AppCtx;
49: /* -------- User-defined Routines --------- */
51: extern PetscErrorCode FormBoundaryConditions(SNES,AppCtx**);
52: extern PetscErrorCode DestroyBoundaryConditions(AppCtx**);
53: extern PetscErrorCode ComputeInitialGuess(SNES, Vec,void*);
54: extern PetscErrorCode FormGradient(SNES, Vec, Vec, void*);
55: extern PetscErrorCode FormJacobian(SNES, Vec, Mat*, Mat*, MatStructure*,void*);
56: extern PetscErrorCode FormBounds(SNES,Vec,Vec);
60: int main(int argc, char **argv)
61: {
63: Vec x,r; /* solution and residual vectors */
64: SNES snes; /* nonlinear solver context */
65: Mat J; /* Jacobian matrix */
66: DM da;
68: PetscInitialize(&argc, &argv, (char*)0, help);
70: /* Create distributed array to manage the 2d grid */
71: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
73: /* Extract global vectors from DMDA; */
74: DMCreateGlobalVector(da,&x);
75: VecDuplicate(x, &r);
77: DMSetMatType(da,MATAIJ);
78: DMCreateMatrix(da,&J);
80: /* Create nonlinear solver context */
81: SNESCreate(PETSC_COMM_WORLD,&snes);
82: SNESSetDM(snes,da);
84: /* Set function evaluation and Jacobian evaluation routines */
85: SNESSetFunction(snes,r,FormGradient,NULL);
86: SNESSetJacobian(snes,J,J,FormJacobian,NULL);
88: SNESSetComputeApplicationContext(snes,(PetscErrorCode (*)(SNES,void**))FormBoundaryConditions,(PetscErrorCode (*)(void**))DestroyBoundaryConditions);
90: SNESSetComputeInitialGuess(snes,ComputeInitialGuess,NULL);
92: SNESVISetComputeVariableBounds(snes,FormBounds);
94: SNESSetFromOptions(snes);
96: /* Solve the application */
97: SNESSolve(snes,NULL,x);
99: /* Free memory */
100: VecDestroy(&x);
101: VecDestroy(&r);
102: MatDestroy(&J);
103: SNESDestroy(&snes);
105: /* Free user-created data structures */
106: DMDestroy(&da);
108: PetscFinalize();
109: return 0;
110: }
112: /* -------------------------------------------------------------------- */
116: /* FormBounds - sets the upper and lower bounds
118: Input Parameters:
119: . snes - the SNES context
121: Output Parameters:
122: . xl - lower bounds
123: . xu - upper bounds
124: */
125: PetscErrorCode FormBounds(SNES snes, Vec xl, Vec xu)
126: {
128: AppCtx *ctx;
131: SNESGetApplicationContext(snes,&ctx);
132: VecSet(xl,ctx->lb);
133: VecSet(xu,ctx->ub);
134: return(0);
135: }
137: /* -------------------------------------------------------------------- */
141: /* FormGradient - Evaluates gradient of f.
143: Input Parameters:
144: . snes - the SNES context
145: . X - input vector
146: . ptr - optional user-defined context, as set by SNESSetFunction()
148: Output Parameters:
149: . G - vector containing the newly evaluated gradient
150: */
151: PetscErrorCode FormGradient(SNES snes, Vec X, Vec G, void *ptr)
152: {
153: AppCtx *user;
154: int ierr;
155: PetscInt i,j;
156: PetscInt mx, my;
157: PetscScalar hx,hy, hydhx, hxdhy;
158: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
159: PetscScalar df1dxc,df2dxc,df3dxc,df4dxc,df5dxc,df6dxc;
160: PetscScalar **g, **x;
161: PetscInt xs,xm,ys,ym;
162: Vec localX;
163: DM da;
166: SNESGetDM(snes,&da);
167: SNESGetApplicationContext(snes,(void**)&user);
168: DMDAGetInfo(da,PETSC_IGNORE,&mx,&my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
169: hx = 1.0/(mx+1);hy=1.0/(my+1); hydhx=hy/hx; hxdhy=hx/hy;
171: VecSet(G,0.0);
173: /* Get local vector */
174: DMGetLocalVector(da,&localX);
175: /* Get ghost points */
176: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
177: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
178: /* Get pointer to local vector data */
179: DMDAVecGetArray(da,localX, &x);
180: DMDAVecGetArray(da,G, &g);
182: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
183: /* Compute function over the locally owned part of the mesh */
184: for (j=ys; j < ys+ym; j++) {
185: for (i=xs; i< xs+xm; i++) {
187: xc = x[j][i];
188: xlt=xrb=xl=xr=xb=xt=xc;
190: if (i==0) { /* left side */
191: xl = user->left[j+1];
192: xlt = user->left[j+2];
193: } else xl = x[j][i-1];
195: if (j==0) { /* bottom side */
196: xb = user->bottom[i+1];
197: xrb = user->bottom[i+2];
198: } else xb = x[j-1][i];
200: if (i+1 == mx) { /* right side */
201: xr = user->right[j+1];
202: xrb = user->right[j];
203: } else xr = x[j][i+1];
205: if (j+1==0+my) { /* top side */
206: xt = user->top[i+1];
207: xlt = user->top[i];
208: } else xt = x[j+1][i];
210: if (i>0 && j+1<my) xlt = x[j+1][i-1]; /* left top side */
211: if (j>0 && i+1<mx) xrb = x[j-1][i+1]; /* right bottom */
213: d1 = (xc-xl);
214: d2 = (xc-xr);
215: d3 = (xc-xt);
216: d4 = (xc-xb);
217: d5 = (xr-xrb);
218: d6 = (xrb-xb);
219: d7 = (xlt-xl);
220: d8 = (xt-xlt);
222: df1dxc = d1*hydhx;
223: df2dxc = (d1*hydhx + d4*hxdhy);
224: df3dxc = d3*hxdhy;
225: df4dxc = (d2*hydhx + d3*hxdhy);
226: df5dxc = d2*hydhx;
227: df6dxc = d4*hxdhy;
229: d1 /= hx;
230: d2 /= hx;
231: d3 /= hy;
232: d4 /= hy;
233: d5 /= hy;
234: d6 /= hx;
235: d7 /= hy;
236: d8 /= hx;
238: f1 = PetscSqrtScalar(1.0 + d1*d1 + d7*d7);
239: f2 = PetscSqrtScalar(1.0 + d1*d1 + d4*d4);
240: f3 = PetscSqrtScalar(1.0 + d3*d3 + d8*d8);
241: f4 = PetscSqrtScalar(1.0 + d3*d3 + d2*d2);
242: f5 = PetscSqrtScalar(1.0 + d2*d2 + d5*d5);
243: f6 = PetscSqrtScalar(1.0 + d4*d4 + d6*d6);
245: df1dxc /= f1;
246: df2dxc /= f2;
247: df3dxc /= f3;
248: df4dxc /= f4;
249: df5dxc /= f5;
250: df6dxc /= f6;
252: g[j][i] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc)/2.0;
254: }
255: }
257: /* Restore vectors */
258: DMDAVecRestoreArray(da,localX, &x);
259: DMDAVecRestoreArray(da,G, &g);
260: DMRestoreLocalVector(da,&localX);
261: PetscLogFlops(67*mx*my);
262: return(0);
263: }
265: /* ------------------------------------------------------------------- */
268: /*
269: FormJacobian - Evaluates Jacobian matrix.
271: Input Parameters:
272: . snes - SNES context
273: . X - input vector
274: . ptr - optional user-defined context, as set by SNESSetJacobian()
276: Output Parameters:
277: . tH - Jacobian matrix
279: */
280: PetscErrorCode FormJacobian(SNES snes, Vec X, Mat *tH, Mat *tHPre, MatStructure *flag, void *ptr)
281: {
282: AppCtx *user;
283: Mat H = *tH;
285: PetscInt i,j,k;
286: PetscInt mx, my;
287: MatStencil row,col[7];
288: PetscScalar hx, hy, hydhx, hxdhy;
289: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
290: PetscScalar hl,hr,ht,hb,hc,htl,hbr;
291: PetscScalar **x, v[7];
292: PetscBool assembled;
293: PetscInt xs,xm,ys,ym;
294: Vec localX;
295: DM da;
298: SNESGetDM(snes,&da);
299: SNESGetApplicationContext(snes,(void**)&user);
300: DMDAGetInfo(da,PETSC_IGNORE,&mx,&my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
301: hx = 1.0/(mx+1); hy=1.0/(my+1); hydhx=hy/hx; hxdhy=hx/hy;
303: /* Set various matrix options */
304: MatAssembled(H,&assembled);
305: if (assembled) {MatZeroEntries(H);}
306: *flag=SAME_NONZERO_PATTERN;
308: /* Get local vector */
309: DMGetLocalVector(da,&localX);
310: /* Get ghost points */
311: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
312: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
314: /* Get pointers to vector data */
315: DMDAVecGetArray(da,localX, &x);
317: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
318: /* Compute Jacobian over the locally owned part of the mesh */
319: for (j=ys; j< ys+ym; j++) {
320: for (i=xs; i< xs+xm; i++) {
321: xc = x[j][i];
322: xlt=xrb=xl=xr=xb=xt=xc;
324: /* Left */
325: if (i==0) {
326: xl = user->left[j+1];
327: xlt = user->left[j+2];
328: } else xl = x[j][i-1];
330: /* Bottom */
331: if (j==0) {
332: xb =user->bottom[i+1];
333: xrb = user->bottom[i+2];
334: } else xb = x[j-1][i];
336: /* Right */
337: if (i+1 == mx) {
338: xr =user->right[j+1];
339: xrb = user->right[j];
340: } else xr = x[j][i+1];
342: /* Top */
343: if (j+1==my) {
344: xt =user->top[i+1];
345: xlt = user->top[i];
346: } else xt = x[j+1][i];
348: /* Top left */
349: if (i>0 && j+1<my) xlt = x[j+1][i-1];
351: /* Bottom right */
352: if (j>0 && i+1<mx) xrb = x[j-1][i+1];
354: d1 = (xc-xl)/hx;
355: d2 = (xc-xr)/hx;
356: d3 = (xc-xt)/hy;
357: d4 = (xc-xb)/hy;
358: d5 = (xrb-xr)/hy;
359: d6 = (xrb-xb)/hx;
360: d7 = (xlt-xl)/hy;
361: d8 = (xlt-xt)/hx;
363: f1 = PetscSqrtScalar(1.0 + d1*d1 + d7*d7);
364: f2 = PetscSqrtScalar(1.0 + d1*d1 + d4*d4);
365: f3 = PetscSqrtScalar(1.0 + d3*d3 + d8*d8);
366: f4 = PetscSqrtScalar(1.0 + d3*d3 + d2*d2);
367: f5 = PetscSqrtScalar(1.0 + d2*d2 + d5*d5);
368: f6 = PetscSqrtScalar(1.0 + d4*d4 + d6*d6);
371: hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+
372: (-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
373: hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+
374: (-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
375: ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+
376: (-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
377: hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+
378: (-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);
380: hbr = -d2*d5/(f5*f5*f5) - d4*d6/(f6*f6*f6);
381: htl = -d1*d7/(f1*f1*f1) - d3*d8/(f3*f3*f3);
383: hc = hydhx*(1.0+d7*d7)/(f1*f1*f1) + hxdhy*(1.0+d8*d8)/(f3*f3*f3) +
384: hydhx*(1.0+d5*d5)/(f5*f5*f5) + hxdhy*(1.0+d6*d6)/(f6*f6*f6) +
385: (hxdhy*(1.0+d1*d1)+hydhx*(1.0+d4*d4)-2.0*d1*d4)/(f2*f2*f2) +
386: (hxdhy*(1.0+d2*d2)+hydhx*(1.0+d3*d3)-2.0*d2*d3)/(f4*f4*f4);
388: hl/=2.0; hr/=2.0; ht/=2.0; hb/=2.0; hbr/=2.0; htl/=2.0; hc/=2.0;
390: k =0;
391: row.i = i;row.j= j;
392: /* Bottom */
393: if (j>0) {
394: v[k] =hb;
395: col[k].i = i; col[k].j=j-1; k++;
396: }
398: /* Bottom right */
399: if (j>0 && i < mx -1) {
400: v[k] =hbr;
401: col[k].i = i+1; col[k].j = j-1; k++;
402: }
404: /* left */
405: if (i>0) {
406: v[k] = hl;
407: col[k].i = i-1; col[k].j = j; k++;
408: }
410: /* Centre */
411: v[k]= hc; col[k].i= row.i; col[k].j = row.j; k++;
413: /* Right */
414: if (i < mx-1) {
415: v[k] = hr;
416: col[k].i= i+1; col[k].j = j;k++;
417: }
419: /* Top left */
420: if (i>0 && j < my-1) {
421: v[k] = htl;
422: col[k].i = i-1;col[k].j = j+1; k++;
423: }
425: /* Top */
426: if (j < my-1) {
427: v[k] = ht;
428: col[k].i = i; col[k].j = j+1; k++;
429: }
431: MatSetValuesStencil(H,1,&row,k,col,v,INSERT_VALUES);
432: }
433: }
435: /* Assemble the matrix */
436: MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);
437: DMDAVecRestoreArray(da,localX,&x);
438: MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);
439: DMRestoreLocalVector(da,&localX);
441: PetscLogFlops(199*mx*my);
442: return(0);
443: }
445: /* ------------------------------------------------------------------- */
448: /*
449: FormBoundaryConditions - Calculates the boundary conditions for
450: the region.
452: Input Parameter:
453: . user - user-defined application context
455: Output Parameter:
456: . user - user-defined application context
457: */
458: PetscErrorCode FormBoundaryConditions(SNES snes,AppCtx **ouser)
459: {
461: PetscInt i,j,k,limit=0,maxits=5;
462: PetscInt mx,my;
463: PetscInt bsize=0, lsize=0, tsize=0, rsize=0;
464: PetscScalar one =1.0, two=2.0, three=3.0;
465: PetscScalar det,hx,hy,xt=0,yt=0;
466: PetscReal fnorm, tol=1e-10;
467: PetscScalar u1,u2,nf1,nf2,njac11,njac12,njac21,njac22;
468: PetscScalar b=-0.5, t=0.5, l=-0.5, r=0.5;
469: PetscScalar *boundary;
470: AppCtx *user;
471: DM da;
474: SNESGetDM(snes,&da);
475: PetscNew(&user);
476: *ouser = user;
477: user->lb = .05;
478: user->ub = PETSC_INFINITY;
479: DMDAGetInfo(da,PETSC_IGNORE,&mx,&my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
481: /* Check if lower and upper bounds are set */
482: PetscOptionsGetScalar(NULL, "-lb", &user->lb, 0);
483: PetscOptionsGetScalar(NULL, "-ub", &user->ub, 0);
484: bsize=mx+2; lsize=my+2; rsize=my+2; tsize=mx+2;
486: PetscMalloc1(bsize, &user->bottom);
487: PetscMalloc1(tsize, &user->top);
488: PetscMalloc1(lsize, &user->left);
489: PetscMalloc1(rsize, &user->right);
491: hx= (r-l)/(mx+1.0); hy=(t-b)/(my+1.0);
493: for (j=0; j<4; j++) {
494: if (j==0) {
495: yt = b;
496: xt = l;
497: limit = bsize;
498: boundary = user->bottom;
499: } else if (j==1) {
500: yt = t;
501: xt = l;
502: limit = tsize;
503: boundary = user->top;
504: } else if (j==2) {
505: yt = b;
506: xt = l;
507: limit = lsize;
508: boundary = user->left;
509: } else { /* if (j==3) */
510: yt = b;
511: xt = r;
512: limit = rsize;
513: boundary = user->right;
514: }
516: for (i=0; i<limit; i++) {
517: u1=xt;
518: u2=-yt;
519: for (k=0; k<maxits; k++) {
520: nf1 = u1 + u1*u2*u2 - u1*u1*u1/three-xt;
521: nf2 = -u2 - u1*u1*u2 + u2*u2*u2/three-yt;
522: fnorm = PetscRealPart(PetscSqrtScalar(nf1*nf1+nf2*nf2));
523: if (fnorm <= tol) break;
524: njac11=one+u2*u2-u1*u1;
525: njac12=two*u1*u2;
526: njac21=-two*u1*u2;
527: njac22=-one - u1*u1 + u2*u2;
528: det = njac11*njac22-njac21*njac12;
529: u1 = u1-(njac22*nf1-njac12*nf2)/det;
530: u2 = u2-(njac11*nf2-njac21*nf1)/det;
531: }
533: boundary[i]=u1*u1-u2*u2;
534: if (j==0 || j==1) xt=xt+hx;
535: else yt=yt+hy; /* if (j==2 || j==3) */
536: }
537: }
538: return(0);
539: }
543: PetscErrorCode DestroyBoundaryConditions(AppCtx **ouser)
544: {
546: AppCtx *user = *ouser;
549: PetscFree(user->bottom);
550: PetscFree(user->top);
551: PetscFree(user->left);
552: PetscFree(user->right);
553: PetscFree(*ouser);
554: return(0);
555: }
558: /* ------------------------------------------------------------------- */
561: /*
562: ComputeInitialGuess - Calculates the initial guess
564: Input Parameters:
565: . user - user-defined application context
566: . X - vector for initial guess
568: Output Parameters:
569: . X - newly computed initial guess
570: */
571: PetscErrorCode ComputeInitialGuess(SNES snes, Vec X,void *dummy)
572: {
574: PetscInt i,j,mx,my;
575: DM da;
576: AppCtx *user;
577: PetscScalar **x;
578: PetscInt xs,xm,ys,ym;
581: SNESGetDM(snes,&da);
582: SNESGetApplicationContext(snes,(void**)&user);
584: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
585: DMDAGetInfo(da,PETSC_IGNORE,&mx,&my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
587: /* Get pointers to vector data */
588: DMDAVecGetArray(da,X,&x);
589: /* Perform local computations */
590: for (j=ys; j<ys+ym; j++) {
591: for (i=xs; i< xs+xm; i++) {
592: x[j][i] = (((j+1.0)*user->bottom[i+1]+(my-j+1.0)*user->top[i+1])/(my+2.0)+((i+1.0)*user->left[j+1]+(mx-i+1.0)*user->right[j+1])/(mx+2.0))/2.0;
593: }
594: }
595: /* Restore vectors */
596: DMDAVecRestoreArray(da,X,&x);
597: return(0);
598: }