Actual source code: ex58.c
petsc-3.4.5 2014-06-29
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: DMCreateMatrix(da,MATAIJ,&J);
79: /* Create nonlinear solver context */
80: SNESCreate(PETSC_COMM_WORLD,&snes);
81: SNESSetDM(snes,da);
83: /* Set function evaluation and Jacobian evaluation routines */
84: SNESSetFunction(snes,r,FormGradient,NULL);
85: SNESSetJacobian(snes,J,J,FormJacobian,NULL);
87: SNESSetComputeApplicationContext(snes,(PetscErrorCode (*)(SNES,void**))FormBoundaryConditions,(PetscErrorCode (*)(void**))DestroyBoundaryConditions);
89: SNESSetComputeInitialGuess(snes,ComputeInitialGuess,NULL);
91: SNESVISetComputeVariableBounds(snes,FormBounds);
93: SNESSetFromOptions(snes);
95: /* Solve the application */
96: SNESSolve(snes,NULL,x);
98: /* Free memory */
99: VecDestroy(&x);
100: VecDestroy(&r);
101: MatDestroy(&J);
102: SNESDestroy(&snes);
104: /* Free user-created data structures */
105: DMDestroy(&da);
107: PetscFinalize();
108: return 0;
109: }
111: /* -------------------------------------------------------------------- */
115: /* FormBounds - sets the upper and lower bounds
117: Input Parameters:
118: . snes - the SNES context
120: Output Parameters:
121: . xl - lower bounds
122: . xu - upper bounds
123: */
124: PetscErrorCode FormBounds(SNES snes, Vec xl, Vec xu)
125: {
127: AppCtx *ctx;
130: SNESGetApplicationContext(snes,&ctx);
131: VecSet(xl,ctx->lb);
132: VecSet(xu,ctx->ub);
133: return(0);
134: }
136: /* -------------------------------------------------------------------- */
140: /* FormGradient - Evaluates gradient of f.
142: Input Parameters:
143: . snes - the SNES context
144: . X - input vector
145: . ptr - optional user-defined context, as set by SNESSetFunction()
147: Output Parameters:
148: . G - vector containing the newly evaluated gradient
149: */
150: PetscErrorCode FormGradient(SNES snes, Vec X, Vec G, void *ptr)
151: {
152: AppCtx *user;
153: int ierr;
154: PetscInt i,j;
155: PetscInt mx, my;
156: PetscScalar hx,hy, hydhx, hxdhy;
157: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
158: PetscScalar df1dxc,df2dxc,df3dxc,df4dxc,df5dxc,df6dxc;
159: PetscScalar **g, **x;
160: PetscInt xs,xm,ys,ym;
161: Vec localX;
162: DM da;
165: SNESGetDM(snes,&da);
166: SNESGetApplicationContext(snes,(void**)&user);
167: 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);
168: hx = 1.0/(mx+1);hy=1.0/(my+1); hydhx=hy/hx; hxdhy=hx/hy;
170: VecSet(G,0.0);
172: /* Get local vector */
173: DMGetLocalVector(da,&localX);
174: /* Get ghost points */
175: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
176: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
177: /* Get pointer to local vector data */
178: DMDAVecGetArray(da,localX, &x);
179: DMDAVecGetArray(da,G, &g);
181: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
182: /* Compute function over the locally owned part of the mesh */
183: for (j=ys; j < ys+ym; j++) {
184: for (i=xs; i< xs+xm; i++) {
186: xc = x[j][i];
187: xlt=xrb=xl=xr=xb=xt=xc;
189: if (i==0) { /* left side */
190: xl = user->left[j+1];
191: xlt = user->left[j+2];
192: } else xl = x[j][i-1];
194: if (j==0) { /* bottom side */
195: xb = user->bottom[i+1];
196: xrb = user->bottom[i+2];
197: } else xb = x[j-1][i];
199: if (i+1 == mx) { /* right side */
200: xr = user->right[j+1];
201: xrb = user->right[j];
202: } else xr = x[j][i+1];
204: if (j+1==0+my) { /* top side */
205: xt = user->top[i+1];
206: xlt = user->top[i];
207: } else xt = x[j+1][i];
209: if (i>0 && j+1<my) xlt = x[j+1][i-1]; /* left top side */
210: if (j>0 && i+1<mx) xrb = x[j-1][i+1]; /* right bottom */
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[j][i] = (df1dxc+df2dxc+df3dxc+df4dxc+df5dxc+df6dxc)/2.0;
253: }
254: }
256: /* Restore vectors */
257: DMDAVecRestoreArray(da,localX, &x);
258: DMDAVecRestoreArray(da,G, &g);
259: DMRestoreLocalVector(da,&localX);
260: PetscLogFlops(67*mx*my);
261: return(0);
262: }
264: /* ------------------------------------------------------------------- */
267: /*
268: FormJacobian - Evaluates Jacobian matrix.
270: Input Parameters:
271: . snes - SNES context
272: . X - input vector
273: . ptr - optional user-defined context, as set by SNESSetJacobian()
275: Output Parameters:
276: . tH - Jacobian matrix
278: */
279: PetscErrorCode FormJacobian(SNES snes, Vec X, Mat *tH, Mat *tHPre, MatStructure *flag, void *ptr)
280: {
281: AppCtx *user;
282: Mat H = *tH;
284: PetscInt i,j,k;
285: PetscInt mx, my;
286: MatStencil row,col[7];
287: PetscScalar hx, hy, hydhx, hxdhy;
288: PetscScalar f1,f2,f3,f4,f5,f6,d1,d2,d3,d4,d5,d6,d7,d8,xc,xl,xr,xt,xb,xlt,xrb;
289: PetscScalar hl,hr,ht,hb,hc,htl,hbr;
290: PetscScalar **x, v[7];
291: PetscBool assembled;
292: PetscInt xs,xm,ys,ym;
293: Vec localX;
294: DM da;
297: SNESGetDM(snes,&da);
298: SNESGetApplicationContext(snes,(void**)&user);
299: 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);
300: hx = 1.0/(mx+1); hy=1.0/(my+1); hydhx=hy/hx; hxdhy=hx/hy;
302: /* Set various matrix options */
303: MatAssembled(H,&assembled);
304: if (assembled) {MatZeroEntries(H);}
305: *flag=SAME_NONZERO_PATTERN;
307: /* Get local vector */
308: DMGetLocalVector(da,&localX);
309: /* Get ghost points */
310: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
311: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
313: /* Get pointers to vector data */
314: DMDAVecGetArray(da,localX, &x);
316: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
317: /* Compute Jacobian over the locally owned part of the mesh */
318: for (j=ys; j< ys+ym; j++) {
319: for (i=xs; i< xs+xm; i++) {
320: xc = x[j][i];
321: xlt=xrb=xl=xr=xb=xt=xc;
323: /* Left */
324: if (i==0) {
325: xl = user->left[j+1];
326: xlt = user->left[j+2];
327: } else xl = x[j][i-1];
329: /* Bottom */
330: if (j==0) {
331: xb =user->bottom[i+1];
332: xrb = user->bottom[i+2];
333: } else xb = x[j-1][i];
335: /* Right */
336: if (i+1 == mx) {
337: xr =user->right[j+1];
338: xrb = user->right[j];
339: } else xr = x[j][i+1];
341: /* Top */
342: if (j+1==my) {
343: xt =user->top[i+1];
344: xlt = user->top[i];
345: } else xt = x[j+1][i];
347: /* Top left */
348: if (i>0 && j+1<my) xlt = x[j+1][i-1];
350: /* Bottom right */
351: if (j>0 && i+1<mx) xrb = x[j-1][i+1];
353: d1 = (xc-xl)/hx;
354: d2 = (xc-xr)/hx;
355: d3 = (xc-xt)/hy;
356: d4 = (xc-xb)/hy;
357: d5 = (xrb-xr)/hy;
358: d6 = (xrb-xb)/hx;
359: d7 = (xlt-xl)/hy;
360: d8 = (xlt-xt)/hx;
362: f1 = PetscSqrtScalar(1.0 + d1*d1 + d7*d7);
363: f2 = PetscSqrtScalar(1.0 + d1*d1 + d4*d4);
364: f3 = PetscSqrtScalar(1.0 + d3*d3 + d8*d8);
365: f4 = PetscSqrtScalar(1.0 + d3*d3 + d2*d2);
366: f5 = PetscSqrtScalar(1.0 + d2*d2 + d5*d5);
367: f6 = PetscSqrtScalar(1.0 + d4*d4 + d6*d6);
370: hl = (-hydhx*(1.0+d7*d7)+d1*d7)/(f1*f1*f1)+
371: (-hydhx*(1.0+d4*d4)+d1*d4)/(f2*f2*f2);
372: hr = (-hydhx*(1.0+d5*d5)+d2*d5)/(f5*f5*f5)+
373: (-hydhx*(1.0+d3*d3)+d2*d3)/(f4*f4*f4);
374: ht = (-hxdhy*(1.0+d8*d8)+d3*d8)/(f3*f3*f3)+
375: (-hxdhy*(1.0+d2*d2)+d2*d3)/(f4*f4*f4);
376: hb = (-hxdhy*(1.0+d6*d6)+d4*d6)/(f6*f6*f6)+
377: (-hxdhy*(1.0+d1*d1)+d1*d4)/(f2*f2*f2);
379: hbr = -d2*d5/(f5*f5*f5) - d4*d6/(f6*f6*f6);
380: htl = -d1*d7/(f1*f1*f1) - d3*d8/(f3*f3*f3);
382: hc = hydhx*(1.0+d7*d7)/(f1*f1*f1) + hxdhy*(1.0+d8*d8)/(f3*f3*f3) +
383: hydhx*(1.0+d5*d5)/(f5*f5*f5) + hxdhy*(1.0+d6*d6)/(f6*f6*f6) +
384: (hxdhy*(1.0+d1*d1)+hydhx*(1.0+d4*d4)-2.0*d1*d4)/(f2*f2*f2) +
385: (hxdhy*(1.0+d2*d2)+hydhx*(1.0+d3*d3)-2.0*d2*d3)/(f4*f4*f4);
387: hl/=2.0; hr/=2.0; ht/=2.0; hb/=2.0; hbr/=2.0; htl/=2.0; hc/=2.0;
389: k =0;
390: row.i = i;row.j= j;
391: /* Bottom */
392: if (j>0) {
393: v[k] =hb;
394: col[k].i = i; col[k].j=j-1; k++;
395: }
397: /* Bottom right */
398: if (j>0 && i < mx -1) {
399: v[k] =hbr;
400: col[k].i = i+1; col[k].j = j-1; k++;
401: }
403: /* left */
404: if (i>0) {
405: v[k] = hl;
406: col[k].i = i-1; col[k].j = j; k++;
407: }
409: /* Centre */
410: v[k]= hc; col[k].i= row.i; col[k].j = row.j; k++;
412: /* Right */
413: if (i < mx-1) {
414: v[k] = hr;
415: col[k].i= i+1; col[k].j = j;k++;
416: }
418: /* Top left */
419: if (i>0 && j < my-1) {
420: v[k] = htl;
421: col[k].i = i-1;col[k].j = j+1; k++;
422: }
424: /* Top */
425: if (j < my-1) {
426: v[k] = ht;
427: col[k].i = i; col[k].j = j+1; k++;
428: }
430: MatSetValuesStencil(H,1,&row,k,col,v,INSERT_VALUES);
431: }
432: }
434: /* Assemble the matrix */
435: MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);
436: DMDAVecRestoreArray(da,localX,&x);
437: MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);
438: DMRestoreLocalVector(da,&localX);
440: PetscLogFlops(199*mx*my);
441: return(0);
442: }
444: /* ------------------------------------------------------------------- */
447: /*
448: FormBoundaryConditions - Calculates the boundary conditions for
449: the region.
451: Input Parameter:
452: . user - user-defined application context
454: Output Parameter:
455: . user - user-defined application context
456: */
457: PetscErrorCode FormBoundaryConditions(SNES snes,AppCtx **ouser)
458: {
460: PetscInt i,j,k,limit=0,maxits=5;
461: PetscInt mx,my;
462: PetscInt bsize=0, lsize=0, tsize=0, rsize=0;
463: PetscScalar one =1.0, two=2.0, three=3.0;
464: PetscScalar det,hx,hy,xt=0,yt=0;
465: PetscReal fnorm, tol=1e-10;
466: PetscScalar u1,u2,nf1,nf2,njac11,njac12,njac21,njac22;
467: PetscScalar b=-0.5, t=0.5, l=-0.5, r=0.5;
468: PetscScalar *boundary;
469: AppCtx *user;
470: DM da;
473: SNESGetDM(snes,&da);
474: PetscNew(AppCtx,&user);
475: *ouser = user;
476: user->lb = .05;
477: user->ub = SNES_VI_INF;
478: 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);
480: /* Check if lower and upper bounds are set */
481: PetscOptionsGetScalar(NULL, "-lb", &user->lb, 0);
482: PetscOptionsGetScalar(NULL, "-ub", &user->ub, 0);
483: bsize=mx+2; lsize=my+2; rsize=my+2; tsize=mx+2;
485: PetscMalloc(bsize*sizeof(PetscScalar), &user->bottom);
486: PetscMalloc(tsize*sizeof(PetscScalar), &user->top);
487: PetscMalloc(lsize*sizeof(PetscScalar), &user->left);
488: PetscMalloc(rsize*sizeof(PetscScalar), &user->right);
490: hx= (r-l)/(mx+1.0); hy=(t-b)/(my+1.0);
492: for (j=0; j<4; j++) {
493: if (j==0) {
494: yt = b;
495: xt = l;
496: limit = bsize;
497: boundary = user->bottom;
498: } else if (j==1) {
499: yt = t;
500: xt = l;
501: limit = tsize;
502: boundary = user->top;
503: } else if (j==2) {
504: yt = b;
505: xt = l;
506: limit = lsize;
507: boundary = user->left;
508: } else { /* if (j==3) */
509: yt = b;
510: xt = r;
511: limit = rsize;
512: boundary = user->right;
513: }
515: for (i=0; i<limit; i++) {
516: u1=xt;
517: u2=-yt;
518: for (k=0; k<maxits; k++) {
519: nf1 = u1 + u1*u2*u2 - u1*u1*u1/three-xt;
520: nf2 = -u2 - u1*u1*u2 + u2*u2*u2/three-yt;
521: fnorm = PetscRealPart(PetscSqrtScalar(nf1*nf1+nf2*nf2));
522: if (fnorm <= tol) break;
523: njac11=one+u2*u2-u1*u1;
524: njac12=two*u1*u2;
525: njac21=-two*u1*u2;
526: njac22=-one - u1*u1 + u2*u2;
527: det = njac11*njac22-njac21*njac12;
528: u1 = u1-(njac22*nf1-njac12*nf2)/det;
529: u2 = u2-(njac11*nf2-njac21*nf1)/det;
530: }
532: boundary[i]=u1*u1-u2*u2;
533: if (j==0 || j==1) xt=xt+hx;
534: else yt=yt+hy; /* if (j==2 || j==3) */
535: }
536: }
537: return(0);
538: }
542: PetscErrorCode DestroyBoundaryConditions(AppCtx **ouser)
543: {
545: AppCtx *user = *ouser;
548: PetscFree(user->bottom);
549: PetscFree(user->top);
550: PetscFree(user->left);
551: PetscFree(user->right);
552: PetscFree(*ouser);
553: return(0);
554: }
557: /* ------------------------------------------------------------------- */
560: /*
561: ComputeInitialGuess - Calculates the initial guess
563: Input Parameters:
564: . user - user-defined application context
565: . X - vector for initial guess
567: Output Parameters:
568: . X - newly computed initial guess
569: */
570: PetscErrorCode ComputeInitialGuess(SNES snes, Vec X,void *dummy)
571: {
573: PetscInt i,j,mx,my;
574: DM da;
575: AppCtx *user;
576: PetscScalar **x;
577: PetscInt xs,xm,ys,ym;
580: SNESGetDM(snes,&da);
581: SNESGetApplicationContext(snes,(void**)&user);
583: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
584: 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);
586: /* Get pointers to vector data */
587: DMDAVecGetArray(da,X,&x);
588: /* Perform local computations */
589: for (j=ys; j<ys+ym; j++) {
590: for (i=xs; i< xs+xm; i++) {
591: 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;
592: }
593: }
594: /* Restore vectors */
595: DMDAVecRestoreArray(da,X,&x);
596: return(0);
597: }