Actual source code: ex15.c
petsc-dev 2014-02-02
2: static char help[] = "Time-dependent PDE in 2d. Modified from ex13.c for illustrating how to solve DAEs. \n";
3: /*
4: u_t = uxx + uyy
5: 0 < x < 1, 0 < y < 1;
6: At t=0: u(x,y) = exp(c*r*r*r), if r=PetscSqrtReal((x-.5)*(x-.5) + (y-.5)*(y-.5)) < .125
7: u(x,y) = 0.0 if r >= .125
10: Boundary conditions:
11: Drichlet BC:
12: At x=0, x=1, y=0, y=1: u = 0.0
14: Neumann BC:
15: At x=0, x=1: du(x,y,t)/dx = 0
16: At y=0, y=1: du(x,y,t)/dy = 0
18: mpiexec -n 2 ./ex15 -da_grid_x 40 -da_grid_y 40 -ts_max_steps 2 -snes_monitor -ksp_monitor
19: ./ex15 -da_grid_x 40 -da_grid_y 40 -draw_pause .1 -boundary 1 -ts_monitor_draw_solution
20: ./ex15 -da_grid_x 40 -da_grid_y 40 -draw_pause .1 -boundary 1 -Jtype 2 -nstencilpts 9
22: */
24: #include <petscdmda.h>
25: #include <petscts.h>
27: /*
28: User-defined data structures and routines
29: */
31: /* AppCtx: used by FormIFunction() and FormIJacobian() */
32: typedef struct {
33: DM da;
34: PetscInt nstencilpts; /* number of stencil points: 5 or 9 */
35: PetscReal c;
36: PetscInt boundary; /* Type of boundary condition */
37: PetscBool viewJacobian;
38: } AppCtx;
40: extern PetscErrorCode FormIFunction(TS,PetscReal,Vec,Vec,Vec,void*);
41: extern PetscErrorCode FormIJacobian(TS,PetscReal,Vec,Vec,PetscReal,Mat*,Mat*,MatStructure*,void*);
42: extern PetscErrorCode FormInitialSolution(Vec,void*);
46: int main(int argc,char **argv)
47: {
48: TS ts; /* nonlinear solver */
49: Vec u,r; /* solution, residual vectors */
50: Mat J,Jmf = NULL; /* Jacobian matrices */
51: PetscInt maxsteps = 1000; /* iterations for convergence */
53: DM da;
54: PetscReal dt;
55: AppCtx user; /* user-defined work context */
56: SNES snes;
57: PetscInt Jtype; /* Jacobian type
58: 0: user provide Jacobian;
59: 1: slow finite difference;
60: 2: fd with coloring; */
62: PetscInitialize(&argc,&argv,(char*)0,help);
63: /* Initialize user application context */
64: user.da = NULL;
65: user.nstencilpts = 5;
66: user.c = -30.0;
67: user.boundary = 0; /* 0: Drichlet BC; 1: Neumann BC */
68: user.viewJacobian = PETSC_FALSE;
70: PetscOptionsGetInt(NULL,"-nstencilpts",&user.nstencilpts,NULL);
71: PetscOptionsGetInt(NULL,"-boundary",&user.boundary,NULL);
72: PetscOptionsHasName(NULL,"-viewJacobian",&user.viewJacobian);
74: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75: Create distributed array (DMDA) to manage parallel grid and vectors
76: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: if (user.nstencilpts == 5) {
78: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-11,-11,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
79: } else if (user.nstencilpts == 9) {
80: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,-11,-11,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
81: } else SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"nstencilpts %d is not supported",user.nstencilpts);
82: user.da = da;
84: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85: Extract global vectors from DMDA;
86: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87: DMCreateGlobalVector(da,&u);
88: VecDuplicate(u,&r);
90: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91: Create timestepping solver context
92: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
93: TSCreate(PETSC_COMM_WORLD,&ts);
94: TSSetProblemType(ts,TS_NONLINEAR);
95: TSSetType(ts,TSBEULER);
96: TSSetDM(ts,da);
97: TSSetIFunction(ts,r,FormIFunction,&user);
98: TSSetDuration(ts,maxsteps,1.0);
100: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101: Set initial conditions
102: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
103: FormInitialSolution(u,&user);
104: TSSetSolution(ts,u);
105: dt = .01;
106: TSSetInitialTimeStep(ts,0.0,dt);
108: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109: Set Jacobian evaluation routine
110: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
111: DMSetMatType(da,MATAIJ);
112: DMCreateMatrix(da,&J);
113: Jtype = 0;
114: PetscOptionsGetInt(NULL, "-Jtype",&Jtype,NULL);
115: if (Jtype == 0) { /* use user provided Jacobian evaluation routine */
116: if (user.nstencilpts != 5) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"user Jacobian routine FormIJacobian() does not support nstencilpts=%D",user.nstencilpts);
117: TSSetIJacobian(ts,J,J,FormIJacobian,&user);
118: } else { /* use finite difference Jacobian J as preconditioner and '-snes_mf_operator' for Mat*vec */
119: TSGetSNES(ts,&snes);
120: MatCreateSNESMF(snes,&Jmf);
121: if (Jtype == 1) { /* slow finite difference J; */
122: SNESSetJacobian(snes,Jmf,J,SNESComputeJacobianDefault,NULL);
123: } else if (Jtype == 2) { /* Use coloring to compute finite difference J efficiently */
124: SNESSetJacobian(snes,Jmf,J,SNESComputeJacobianDefaultColor,0);
125: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Jtype is not supported");
126: }
128: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129: Sets various TS parameters from user options
130: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131: TSSetFromOptions(ts);
133: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134: Solve nonlinear system
135: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136: TSSolve(ts,u);
138: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139: Free work space.
140: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141: MatDestroy(&J);
142: MatDestroy(&Jmf);
143: VecDestroy(&u);
144: VecDestroy(&r);
145: TSDestroy(&ts);
146: DMDestroy(&da);
148: PetscFinalize();
149: return(0);
150: }
152: /* --------------------------------------------------------------------- */
153: /*
154: FormIFunction = Udot - RHSFunction
155: */
158: PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
159: {
161: AppCtx *user=(AppCtx*)ctx;
162: DM da = (DM)user->da;
163: PetscInt i,j,Mx,My,xs,ys,xm,ym;
164: PetscReal hx,hy,sx,sy;
165: PetscScalar u,uxx,uyy,**uarray,**f,**udot;
166: Vec localU;
169: DMGetLocalVector(da,&localU);
170: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
171: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
173: hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
174: hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);
175: if (user->nstencilpts == 9 && hx != hy) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"hx must equal hy when nstencilpts = 9 for this example");
177: /*
178: Scatter ghost points to local vector,using the 2-step process
179: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
180: By placing code between these two statements, computations can be
181: done while messages are in transition.
182: */
183: DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
184: DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);
186: /* Get pointers to vector data */
187: DMDAVecGetArray(da,localU,&uarray);
188: DMDAVecGetArray(da,F,&f);
189: DMDAVecGetArray(da,Udot,&udot);
191: /* Get local grid boundaries */
192: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
194: /* Compute function over the locally owned part of the grid */
195: for (j=ys; j<ys+ym; j++) {
196: for (i=xs; i<xs+xm; i++) {
197: /* Boundary conditions */
198: if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
199: if (user->boundary == 0) { /* Drichlet BC */
200: f[j][i] = uarray[j][i]; /* F = U */
201: } else { /* Neumann BC */
202: if (i == 0 && j == 0) { /* SW corner */
203: f[j][i] = uarray[j][i] - uarray[j+1][i+1];
204: } else if (i == Mx-1 && j == 0) { /* SE corner */
205: f[j][i] = uarray[j][i] - uarray[j+1][i-1];
206: } else if (i == 0 && j == My-1) { /* NW corner */
207: f[j][i] = uarray[j][i] - uarray[j-1][i+1];
208: } else if (i == Mx-1 && j == My-1) { /* NE corner */
209: f[j][i] = uarray[j][i] - uarray[j-1][i-1];
210: } else if (i == 0) { /* Left */
211: f[j][i] = uarray[j][i] - uarray[j][i+1];
212: } else if (i == Mx-1) { /* Right */
213: f[j][i] = uarray[j][i] - uarray[j][i-1];
214: } else if (j == 0) { /* Bottom */
215: f[j][i] = uarray[j][i] - uarray[j+1][i];
216: } else if (j == My-1) { /* Top */
217: f[j][i] = uarray[j][i] - uarray[j-1][i];
218: }
219: }
220: } else { /* Interior */
221: u = uarray[j][i];
222: /* 5-point stencil */
223: uxx = (-2.0*u + uarray[j][i-1] + uarray[j][i+1]);
224: uyy = (-2.0*u + uarray[j-1][i] + uarray[j+1][i]);
225: if (user->nstencilpts == 9) {
226: /* 9-point stencil: assume hx=hy */
227: uxx = 2.0*uxx/3.0 + (0.5*(uarray[j-1][i-1]+uarray[j-1][i+1]+uarray[j+1][i-1]+uarray[j+1][i+1]) - 2.0*u)/6.0;
228: uyy = 2.0*uyy/3.0 + (0.5*(uarray[j-1][i-1]+uarray[j-1][i+1]+uarray[j+1][i-1]+uarray[j+1][i+1]) - 2.0*u)/6.0;
229: }
230: f[j][i] = udot[j][i] - (uxx*sx + uyy*sy);
231: }
232: }
233: }
235: /* Restore vectors */
236: DMDAVecRestoreArray(da,localU,&uarray);
237: DMDAVecRestoreArray(da,F,&f);
238: DMDAVecRestoreArray(da,Udot,&udot);
239: DMRestoreLocalVector(da,&localU);
240: PetscLogFlops(11.0*ym*xm);
241: return(0);
242: }
244: /* --------------------------------------------------------------------- */
245: /*
246: FormIJacobian() - Compute IJacobian = dF/dU + a dF/dUdot
247: This routine is not used with option '-use_coloring'
248: */
251: PetscErrorCode FormIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat *J,Mat *Jpre,MatStructure *str,void *ctx)
252: {
254: PetscInt i,j,Mx,My,xs,ys,xm,ym,nc;
255: AppCtx *user = (AppCtx*)ctx;
256: DM da = (DM)user->da;
257: MatStencil col[5],row;
258: PetscScalar vals[5],hx,hy,sx,sy;
261: 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);
262: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
264: hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
265: hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);
267: for (j=ys; j<ys+ym; j++) {
268: for (i=xs; i<xs+xm; i++) {
269: nc = 0;
270: row.j = j; row.i = i;
271: if (user->boundary == 0 && (i == 0 || i == Mx-1 || j == 0 || j == My-1)) {
272: col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;
274: } else if (user->boundary > 0 && i == 0) { /* Left Neumann */
275: col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;
276: col[nc].j = j; col[nc].i = i+1; vals[nc++] = -1.0;
277: } else if (user->boundary > 0 && i == Mx-1) { /* Right Neumann */
278: col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;
279: col[nc].j = j; col[nc].i = i-1; vals[nc++] = -1.0;
280: } else if (user->boundary > 0 && j == 0) { /* Bottom Neumann */
281: col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;
282: col[nc].j = j+1; col[nc].i = i; vals[nc++] = -1.0;
283: } else if (user->boundary > 0 && j == My-1) { /* Top Neumann */
284: col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;
285: col[nc].j = j-1; col[nc].i = i; vals[nc++] = -1.0;
286: } else { /* Interior */
287: col[nc].j = j-1; col[nc].i = i; vals[nc++] = -sy;
288: col[nc].j = j; col[nc].i = i-1; vals[nc++] = -sx;
289: col[nc].j = j; col[nc].i = i; vals[nc++] = 2.0*(sx + sy) + a;
290: col[nc].j = j; col[nc].i = i+1; vals[nc++] = -sx;
291: col[nc].j = j+1; col[nc].i = i; vals[nc++] = -sy;
292: }
293: MatSetValuesStencil(*Jpre,1,&row,nc,col,vals,INSERT_VALUES);
294: }
295: }
296: MatAssemblyBegin(*Jpre,MAT_FINAL_ASSEMBLY);
297: MatAssemblyEnd(*Jpre,MAT_FINAL_ASSEMBLY);
298: if (*J != *Jpre) {
299: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
300: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
301: }
303: if (user->viewJacobian) {
304: PetscPrintf(PetscObjectComm((PetscObject)*Jpre),"Jpre:\n");
305: MatView(*Jpre,PETSC_VIEWER_STDOUT_WORLD);
306: }
307: return(0);
308: }
310: /* ------------------------------------------------------------------- */
313: PetscErrorCode FormInitialSolution(Vec U,void *ptr)
314: {
315: AppCtx *user=(AppCtx*)ptr;
316: DM da =user->da;
317: PetscReal c =user->c;
319: PetscInt i,j,xs,ys,xm,ym,Mx,My;
320: PetscScalar **u;
321: PetscReal hx,hy,x,y,r;
324: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
325: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
327: hx = 1.0/(PetscReal)(Mx-1);
328: hy = 1.0/(PetscReal)(My-1);
330: /* Get pointers to vector data */
331: DMDAVecGetArray(da,U,&u);
333: /* Get local grid boundaries */
334: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
336: /* Compute function over the locally owned part of the grid */
337: for (j=ys; j<ys+ym; j++) {
338: y = j*hy;
339: for (i=xs; i<xs+xm; i++) {
340: x = i*hx;
341: r = PetscSqrtReal((x-.3)*(x-.5) + (y-.5)*(y-.5));
342: if (r < .125) u[j][i] = PetscExpReal(c*r*r*r);
343: else u[j][i] = 0.0;
344: }
345: }
347: /* Restore vectors */
348: DMDAVecRestoreArray(da,U,&u);
349: return(0);
350: }