Actual source code: ex5.c
2: static char help[] = "Bratu nonlinear PDE in 2d.\n\
3: We solve the Bratu (SFI - solid fuel ignition) problem in a 2D rectangular\n\
4: domain, using distributed arrays (DMDAs) to partition the parallel grid.\n\
5: The command line options include:\n\
6: -par <parameter>, where <parameter> indicates the problem's nonlinearity\n\
7: problem SFI: <parameter> = Bratu parameter (0 <= par <= 6.81)\n\n";
9: /*T
10: Concepts: SNES^parallel Bratu example
11: Concepts: DMDA^using distributed arrays;
12: Concepts: IS coloirng types;
13: Processors: n
14: T*/
16: /* ------------------------------------------------------------------------
18: Solid Fuel Ignition (SFI) problem. This problem is modeled by
19: the partial differential equation
20:
21: -Laplacian u - lambda*exp(u) = 0, 0 < x,y < 1,
22:
23: with boundary conditions
24:
25: u = 0 for x = 0, x = 1, y = 0, y = 1.
26:
27: A finite difference approximation with the usual 5-point stencil
28: is used to discretize the boundary value problem to obtain a nonlinear
29: system of equations.
31: Program usage: mpiexec -n <procs> ex5 [-help] [all PETSc options]
32: e.g.,
33:
34: This example shows how geometric multigrid can be run transparently with a nonlinear solver so long
35: as SNESSetDM() is provided. Example usage
37: ./ex5 -pc_type mg -ksp_monitor -snes_view -pc_mg_levels 3 -pc_mg_galerkin -da_grid_x 17 -da_grid_y 17
38: -mg_levels_ksp_monitor -snes_monitor -mg_levels_pc_type sor -pc_mg_type full
40: or to run with grid sequencing on the nonlinear problem (note that you do not need to provide the number of
41: multigrid levels, it will be determined automatically based on the number of refinements done)
43: ./ex5 -pc_type mg -ksp_monitor -snes_view -pc_mg_galerkin -snes_grid_sequence 3
44: -mg_levels_ksp_monitor -snes_monitor -mg_levels_pc_type sor -pc_mg_type full
46: ------------------------------------------------------------------------- */
48: /*
49: Include "petscdmda.h" so that we can use distributed arrays (DMDAs).
50: Include "petscsnes.h" so that we can use SNES solvers. Note that this
51: */
52: #include <petscdmda.h>
53: #include <petscsnes.h>
55: /*
56: User-defined application context - contains data needed by the
57: application-provided call-back routines, FormJacobianLocal() and
58: FormFunctionLocal().
59: */
60: typedef struct {
61: PassiveReal param; /* test problem parameter */
62: } AppCtx;
64: /*
65: User-defined routines
66: */
67: extern PetscErrorCode FormInitialGuess(DM,AppCtx*,Vec);
68: extern PetscErrorCode FormFunctionLocal(DMDALocalInfo*,PetscScalar**,PetscScalar**,AppCtx*);
69: extern PetscErrorCode FormJacobianLocal(DMDALocalInfo*,PetscScalar**,Mat,AppCtx*);
70: #if defined(PETSC_HAVE_MATLAB_ENGINE)
71: extern PetscErrorCode FormFunctionMatlab(SNES,Vec,Vec,void *);
72: #endif
73: extern PetscErrorCode NonlinearGS(SNES,Vec,Vec,void*);
77: int main(int argc,char **argv)
78: {
79: SNES snes; /* nonlinear solver */
80: Vec x; /* solution vector */
81: AppCtx user; /* user-defined work context */
82: PetscInt its; /* iterations for convergence */
83: PetscErrorCode ierr;
84: PetscReal bratu_lambda_max = 6.81;
85: PetscReal bratu_lambda_min = 0.;
86: PetscBool flg = PETSC_FALSE;
87: DM da;
88: PetscBool matlab_function = PETSC_FALSE;
90: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91: Initialize program
92: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
94: PetscInitialize(&argc,&argv,(char *)0,help);
96: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97: Initialize problem parameters
98: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99: user.param = 6.0;
100: PetscOptionsGetReal(PETSC_NULL,"-par",&user.param,PETSC_NULL);
101: if (user.param >= bratu_lambda_max || user.param <= bratu_lambda_min) SETERRQ3(PETSC_COMM_SELF,1,"Lambda, %g, is out of range, [%g, %g]", user.param, bratu_lambda_min, bratu_lambda_max);
103: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104: Create nonlinear solver context
105: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106: SNESCreate(PETSC_COMM_WORLD,&snes);
107: SNESSetGS(snes, NonlinearGS, PETSC_NULL);
109: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110: Create distributed array (DMDA) to manage parallel grid and vectors
111: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
112: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,PETSC_NULL,PETSC_NULL,&da);
113: DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
114: DMSetApplicationContext(da,&user);
115: SNESSetDM(snes,da);
116: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
117: Extract global vectors from DMDA; then duplicate for remaining
118: vectors that are the same types
119: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
120: DMCreateGlobalVector(da,&x);
122: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123: Set local function evaluation routine
124: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
125: DMDASetLocalFunction(da,(DMDALocalFunction1)FormFunctionLocal);
126: PetscOptionsGetBool(PETSC_NULL,"-fd",&flg,PETSC_NULL);
127: if (!flg) {
128: DMDASetLocalJacobian(da,(DMDALocalFunction1)FormJacobianLocal);
129: }
131: /* Decide which FormFunction to use */
132: PetscOptionsGetBool(PETSC_NULL,"-matlab_function",&matlab_function,0);
134: #if defined(PETSC_HAVE_MATLAB_ENGINE)
135: Vec r;
136: if (matlab_function) {
137: VecDuplicate(x,&r);
138: SNESSetFunction(snes,r,FormFunctionMatlab,&user);
139: }
140: #endif
142: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143: Customize nonlinear solver; set runtime options
144: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145: SNESSetFromOptions(snes);
147: FormInitialGuess(da,&user,x);
149: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150: Solve nonlinear system
151: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
152: SNESSolve(snes,PETSC_NULL,x);
153: SNESGetIterationNumber(snes,&its);
155: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
156: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
158: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159: Free work space. All PETSc objects should be destroyed when they
160: are no longer needed.
161: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
162: #if defined(PETSC_HAVE_MATLAB_ENGINE)
163: if (r){VecDestroy(&r);}
164: #endif
165: VecDestroy(&x);
166: SNESDestroy(&snes);
167: DMDestroy(&da);
168: PetscFinalize();
170: return(0);
171: }
172: /* ------------------------------------------------------------------- */
175: /*
176: FormInitialGuess - Forms initial approximation.
178: Input Parameters:
179: user - user-defined application context
180: X - vector
182: Output Parameter:
183: X - vector
184: */
185: PetscErrorCode FormInitialGuess(DM da,AppCtx *user,Vec X)
186: {
187: PetscInt i,j,Mx,My,xs,ys,xm,ym;
189: PetscReal lambda,temp1,temp,hx,hy;
190: PetscScalar **x;
193: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
194: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
196: lambda = user->param;
197: hx = 1.0/(PetscReal)(Mx-1);
198: hy = 1.0/(PetscReal)(My-1);
199: temp1 = lambda/(lambda + 1.0);
201: /*
202: Get a pointer to vector data.
203: - For default PETSc vectors, VecGetArray() returns a pointer to
204: the data array. Otherwise, the routine is implementation dependent.
205: - You MUST call VecRestoreArray() when you no longer need access to
206: the array.
207: */
208: DMDAVecGetArray(da,X,&x);
210: /*
211: Get local grid boundaries (for 2-dimensional DMDA):
212: xs, ys - starting grid indices (no ghost points)
213: xm, ym - widths of local grid (no ghost points)
215: */
216: DMDAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL);
218: /*
219: Compute initial guess over the locally owned part of the grid
220: */
221: for (j=ys; j<ys+ym; j++) {
222: temp = (PetscReal)(PetscMin(j,My-j-1))*hy;
223: for (i=xs; i<xs+xm; i++) {
224: if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
225: /* boundary conditions are all zero Dirichlet */
226: x[j][i] = 0.0;
227: } else {
228: x[j][i] = temp1*sqrt(PetscMin((PetscReal)(PetscMin(i,Mx-i-1))*hx,temp));
229: }
230: }
231: }
233: /*
234: Restore vector
235: */
236: DMDAVecRestoreArray(da,X,&x);
238: return(0);
239: }
240: /* ------------------------------------------------------------------- */
243: /*
244: FormFunctionLocal - Evaluates nonlinear function, F(x) on local process patch
247: */
248: PetscErrorCode FormFunctionLocal(DMDALocalInfo *info,PetscScalar **x,PetscScalar **f,AppCtx *user)
249: {
251: PetscInt i,j;
252: PetscReal lambda,hx,hy,hxdhy,hydhx,sc;
253: PetscScalar u,uxx,uyy;
257: lambda = user->param;
258: hx = 1.0/(PetscReal)(info->mx-1);
259: hy = 1.0/(PetscReal)(info->my-1);
260: sc = hx*hy*lambda;
261: hxdhy = hx/hy;
262: hydhx = hy/hx;
263: /*
264: Compute function over the locally owned part of the grid
265: */
266: for (j=info->ys; j<info->ys+info->ym; j++) {
267: for (i=info->xs; i<info->xs+info->xm; i++) {
268: if (i == 0 || j == 0 || i == info->mx-1 || j == info->my-1) {
269: f[j][i] = 2.0*(hydhx+hxdhy)*x[j][i];
270: } else {
271: u = x[j][i];
272: uxx = (2.0*u - x[j][i-1] - x[j][i+1])*hydhx;
273: uyy = (2.0*u - x[j-1][i] - x[j+1][i])*hxdhy;
274: f[j][i] = uxx + uyy - sc*PetscExpScalar(u);
275: }
276: }
277: }
278: PetscLogFlops(11.0*info->ym*info->xm);
279: return(0);
280: }
284: /*
285: FormJacobianLocal - Evaluates Jacobian matrix on local process patch
286: */
287: PetscErrorCode FormJacobianLocal(DMDALocalInfo *info,PetscScalar **x,Mat jac,AppCtx *user)
288: {
290: PetscInt i,j;
291: MatStencil col[5],row;
292: PetscScalar lambda,v[5],hx,hy,hxdhy,hydhx,sc;
295: lambda = user->param;
296: hx = 1.0/(PetscReal)(info->mx-1);
297: hy = 1.0/(PetscReal)(info->my-1);
298: sc = hx*hy*lambda;
299: hxdhy = hx/hy;
300: hydhx = hy/hx;
303: /*
304: Compute entries for the locally owned part of the Jacobian.
305: - Currently, all PETSc parallel matrix formats are partitioned by
306: contiguous chunks of rows across the processors.
307: - Each processor needs to insert only elements that it owns
308: locally (but any non-local elements will be sent to the
309: appropriate processor during matrix assembly).
310: - Here, we set all entries for a particular row at once.
311: - We can set matrix entries either using either
312: MatSetValuesLocal() or MatSetValues(), as discussed above.
313: */
314: for (j=info->ys; j<info->ys+info->ym; j++) {
315: for (i=info->xs; i<info->xs+info->xm; i++) {
316: row.j = j; row.i = i;
317: /* boundary points */
318: if (i == 0 || j == 0 || i == info->mx-1 || j == info->my-1) {
319: v[0] = 2.0*(hydhx + hxdhy);
320: MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
321: } else {
322: /* interior grid points */
323: v[0] = -hxdhy; col[0].j = j - 1; col[0].i = i;
324: v[1] = -hydhx; col[1].j = j; col[1].i = i-1;
325: v[2] = 2.0*(hydhx + hxdhy) - sc*PetscExpScalar(x[j][i]); col[2].j = row.j; col[2].i = row.i;
326: v[3] = -hydhx; col[3].j = j; col[3].i = i+1;
327: v[4] = -hxdhy; col[4].j = j + 1; col[4].i = i;
328: MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
329: }
330: }
331: }
333: /*
334: Assemble matrix, using the 2-step process:
335: MatAssemblyBegin(), MatAssemblyEnd().
336: */
337: MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
338: MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
339: /*
340: Tell the matrix we will never add a new nonzero location to the
341: matrix. If we do, it will generate an error.
342: */
343: MatSetOption(jac,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
344: return(0);
345: }
347: #if defined(PETSC_HAVE_MATLAB_ENGINE)
350: PetscErrorCode FormFunctionMatlab(SNES snes,Vec X,Vec F,void *ptr)
351: {
352: AppCtx *user = (AppCtx*)ptr;
354: PetscInt Mx,My;
355: PetscReal lambda,hx,hy;
356: Vec localX,localF;
357: MPI_Comm comm;
358: DM da;
361: SNESGetDM(snes,&da);
362: DMGetLocalVector(da,&localX);
363: DMGetLocalVector(da,&localF);
364: PetscObjectSetName((PetscObject)localX,"localX");
365: PetscObjectSetName((PetscObject)localF,"localF");
366: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
367: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
369: lambda = user->param;
370: hx = 1.0/(PetscReal)(Mx-1);
371: hy = 1.0/(PetscReal)(My-1);
373: PetscObjectGetComm((PetscObject)snes,&comm);
374: /*
375: Scatter ghost points to local vector,using the 2-step process
376: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
377: By placing code between these two statements, computations can be
378: done while messages are in transition.
379: */
380: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
381: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
382: PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localX);
383: PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(comm),"localF=ex5m(localX,%18.16e,%18.16e,%18.16e)",hx,hy,lambda);
384: PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localF);
386: /*
387: Insert values into global vector
388: */
389: DMLocalToGlobalBegin(da,localF,INSERT_VALUES,F);
390: DMLocalToGlobalEnd(da,localF,INSERT_VALUES,F);
391: DMRestoreLocalVector(da,&localX);
392: DMRestoreLocalVector(da,&localF);
393: return(0);
394: }
395: #endif
397: /* ------------------------------------------------------------------- */
400: /*
401: Applies some sweeps on nonlinear Gauss-Seidel on each process
403: */
404: PetscErrorCode NonlinearGS(SNES snes,Vec X, Vec B, void * ctx)
405: {
406: PetscInt i,j,Mx,My,xs,ys,xm,ym,k,its,l;
408: PetscReal lambda,hx,hy,hxdhy,hydhx,sc;
409: PetscScalar **x,**b,bij,F,J,u,uxx,uyy;
410: DM da;
411: AppCtx *user;
412: Vec localX,localB;
415: SNESGetTolerances(snes,PETSC_NULL,PETSC_NULL,PETSC_NULL,&its,PETSC_NULL);
416: SNESGetDM(snes,&da);
417: DMGetApplicationContext(da,(void**)&user);
419: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
420: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
422: lambda = user->param;
423: hx = 1.0/(PetscReal)(Mx-1);
424: hy = 1.0/(PetscReal)(My-1);
425: sc = hx*hy*lambda;
426: hxdhy = hx/hy;
427: hydhx = hy/hx;
430: DMGetLocalVector(da,&localX);
431: if (B) {
432: DMGetLocalVector(da,&localB);
433: }
434: for (l=0; l<1; l++) {
436: DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
437: DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
438: if (B) {
439: DMGlobalToLocalBegin(da,B,INSERT_VALUES,localB);
440: DMGlobalToLocalEnd(da,B,INSERT_VALUES,localB);
441: }
442: /*
443: Get a pointer to vector data.
444: - For default PETSc vectors, VecGetArray() returns a pointer to
445: the data array. Otherwise, the routine is implementation dependent.
446: - You MUST call VecRestoreArray() when you no longer need access to
447: the array.
448: */
449: DMDAVecGetArray(da,localX,&x);
450: if (B) DMDAVecGetArray(da,localB,&b);
451: /*
452: Get local grid boundaries (for 2-dimensional DMDA):
453: xs, ys - starting grid indices (no ghost points)
454: xm, ym - widths of local grid (no ghost points)
455: */
456: DMDAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL);
458: for (j=ys; j<ys+ym; j++) {
459: for (i=xs; i<xs+xm; i++) {
460: if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
461: /* boundary conditions are all zero Dirichlet */
462: x[j][i] = 0.0;
463: } else {
464: if (B) {
465: bij = b[j][i];
466: } else {
467: bij = 0.;
468: }
469: u = x[j][i];
470: for (k=0; k<1; k++) {
471: uxx = (2.0*u - x[j][i-1] - x[j][i+1])*hydhx;
472: uyy = (2.0*u - x[j-1][i] - x[j+1][i])*hxdhy;
473: F = uxx + uyy - sc*PetscExpScalar(u) - bij;
474: J = 2.0*(hydhx + hxdhy) - sc*PetscExpScalar(u);
475: u = u - F/J;
476: }
477: x[j][i] = u;
478: }
479: }
480: }
481: /*
482: Restore vector
483: */
484: DMDAVecRestoreArray(da,localX,&x);
485: DMLocalToGlobalBegin(da,localX,INSERT_VALUES,X);
486: DMLocalToGlobalEnd(da,localX,INSERT_VALUES,X);
487: }
488: PetscLogFlops((11.0 + 5)*ym*xm);
489: DMRestoreLocalVector(da,&localX);
490: if (B) {
491: DMDAVecRestoreArray(da,localB,&b);
492: DMRestoreLocalVector(da,&localB);
493: }
494: return(0);
495: }