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