Actual source code: ex13.c
petsc-3.4.0 2013-05-13
2: static char help[] = "Time-dependent PDE in 2d. Simplified from ex7.c for illustrating how to use TS on a structured domain. \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
9: mpiexec -n 2 ./ex13 -da_grid_x 40 -da_grid_y 40 -ts_max_steps 2 -use_coloring -snes_monitor -ksp_monitor
10: ./ex13 -use_coloring
11: ./ex13 -use_coloring -draw_pause -1
12: mpiexec -n 2 ./ex13 -ts_type sundials -ts_sundials_monitor_steps
13: */
15: #include <petscdmda.h>
16: #include <petscts.h>
18: /*
19: User-defined data structures and routines
20: */
21: typedef struct {
22: PetscReal c;
23: } AppCtx;
25: extern PetscErrorCode RHSFunction(TS,PetscReal,Vec,Vec,void*);
26: extern PetscErrorCode RHSJacobian(TS,PetscReal,Vec,Mat*,Mat*,MatStructure*,void*);
27: extern PetscErrorCode FormInitialSolution(DM,Vec,void*);
31: int main(int argc,char **argv)
32: {
33: TS ts; /* nonlinear solver */
34: Vec u,r; /* solution, residual vector */
35: Mat J; /* Jacobian matrix */
36: PetscInt steps,maxsteps = 1000; /* iterations for convergence */
38: DM da;
39: PetscReal ftime,dt;
40: AppCtx user; /* user-defined work context */
41: PetscBool coloring=PETSC_FALSE;
43: PetscInitialize(&argc,&argv,(char*)0,help);
44: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
45: Create distributed array (DMDA) to manage parallel grid and vectors
46: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
47: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-8,-8,PETSC_DECIDE,PETSC_DECIDE,
48: 1,1,NULL,NULL,&da);
50: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51: Extract global vectors from DMDA;
52: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
53: DMCreateGlobalVector(da,&u);
54: VecDuplicate(u,&r);
56: /* Initialize user application context */
57: user.c = -30.0;
59: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60: Create timestepping solver context
61: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
62: TSCreate(PETSC_COMM_WORLD,&ts);
63: TSSetDM(ts,da);
64: TSSetType(ts,TSBEULER);
65: TSSetRHSFunction(ts,r,RHSFunction,&user);
67: /* Set Jacobian */
68: DMCreateMatrix(da,MATAIJ,&J);
69: TSSetRHSJacobian(ts,J,J,RHSJacobian,NULL);
71: /* Use coloring to compute Jacobian efficiently */
72: PetscOptionsGetBool(NULL,"-use_coloring",&coloring,NULL);
73: if (coloring) {
74: SNES snes;
75: TSGetSNES(ts,&snes);
76: SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefaultColor,NULL);
77: }
79: ftime = 1.0;
80: TSSetDuration(ts,maxsteps,ftime);
82: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83: Set initial conditions
84: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85: FormInitialSolution(da,u,&user);
86: dt = .01;
87: TSSetInitialTimeStep(ts,0.0,dt);
89: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90: Set runtime options
91: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
92: TSSetFromOptions(ts);
94: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95: Solve nonlinear system
96: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
97: TSSolve(ts,u);
98: TSGetSolveTime(ts,&ftime);
99: TSGetTimeStepNumber(ts,&steps);
101: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102: Free work space.
103: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104: MatDestroy(&J);
105: VecDestroy(&u);
106: VecDestroy(&r);
107: TSDestroy(&ts);
108: DMDestroy(&da);
110: PetscFinalize();
111: return(0);
112: }
113: /* ------------------------------------------------------------------- */
116: /*
117: RHSFunction - Evaluates nonlinear function, F(u).
119: Input Parameters:
120: . ts - the TS context
121: . U - input vector
122: . ptr - optional user-defined context, as set by TSSetFunction()
124: Output Parameter:
125: . F - function vector
126: */
127: PetscErrorCode RHSFunction(TS ts,PetscReal ftime,Vec U,Vec F,void *ptr)
128: {
129: /* PETSC_UNUSED AppCtx *user=(AppCtx*)ptr; */
130: DM da;
132: PetscInt i,j,Mx,My,xs,ys,xm,ym;
133: PetscReal two = 2.0,hx,hy,sx,sy;
134: PetscScalar u,uxx,uyy,**uarray,**f;
135: Vec localU;
138: TSGetDM(ts,&da);
139: DMGetLocalVector(da,&localU);
140: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
141: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
143: hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
144: hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);
146: /*
147: Scatter ghost points to local vector,using the 2-step process
148: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
149: By placing code between these two statements, computations can be
150: done while messages are in transition.
151: */
152: DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
153: DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);
155: /* Get pointers to vector data */
156: DMDAVecGetArray(da,localU,&uarray);
157: DMDAVecGetArray(da,F,&f);
159: /* Get local grid boundaries */
160: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
162: /* Compute function over the locally owned part of the grid */
163: for (j=ys; j<ys+ym; j++) {
164: for (i=xs; i<xs+xm; i++) {
165: if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
166: f[j][i] = uarray[j][i];
167: continue;
168: }
169: u = uarray[j][i];
170: uxx = (-two*u + uarray[j][i-1] + uarray[j][i+1])*sx;
171: uyy = (-two*u + uarray[j-1][i] + uarray[j+1][i])*sy;
172: f[j][i] = uxx + uyy;
173: }
174: }
176: /* Restore vectors */
177: DMDAVecRestoreArray(da,localU,&uarray);
178: DMDAVecRestoreArray(da,F,&f);
179: DMRestoreLocalVector(da,&localU);
180: PetscLogFlops(11.0*ym*xm);
181: return(0);
182: }
184: /* --------------------------------------------------------------------- */
187: /*
188: RHSJacobian - User-provided routine to compute the Jacobian of
189: the nonlinear right-hand-side function of the ODE.
191: Input Parameters:
192: ts - the TS context
193: t - current time
194: U - global input vector
195: dummy - optional user-defined context, as set by TSetRHSJacobian()
197: Output Parameters:
198: J - Jacobian matrix
199: Jpre - optionally different preconditioning matrix
200: str - flag indicating matrix structure
201: */
202: PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec U,Mat *J,Mat *Jpre,MatStructure *str,void *ctx)
203: {
205: DM da;
206: DMDALocalInfo info;
207: PetscInt i,j;
208: PetscReal hx,hy,sx,sy;
211: TSGetDM(ts,&da);
212: DMDAGetLocalInfo(da,&info);
213: hx = 1.0/(PetscReal)(info.mx-1); sx = 1.0/(hx*hx);
214: hy = 1.0/(PetscReal)(info.my-1); sy = 1.0/(hy*hy);
215: for (j=info.ys; j<info.ys+info.ym; j++) {
216: for (i=info.xs; i<info.xs+info.xm; i++) {
217: PetscInt nc = 0;
218: MatStencil row,col[5];
219: PetscScalar val[5];
220: row.i = i; row.j = j;
221: if (i == 0 || j == 0 || i == info.mx-1 || j == info.my-1) {
222: col[nc].i = i; col[nc].j = j; val[nc++] = 1.0;
223: } else {
224: col[nc].i = i-1; col[nc].j = j; val[nc++] = sx;
225: col[nc].i = i+1; col[nc].j = j; val[nc++] = sx;
226: col[nc].i = i; col[nc].j = j-1; val[nc++] = sy;
227: col[nc].i = i; col[nc].j = j+1; val[nc++] = sy;
228: col[nc].i = i; col[nc].j = j; val[nc++] = -2*sx - 2*sy;
229: }
230: MatSetValuesStencil(*Jpre,1,&row,nc,col,val,INSERT_VALUES);
231: }
232: }
233: MatAssemblyBegin(*Jpre,MAT_FINAL_ASSEMBLY);
234: MatAssemblyEnd(*Jpre,MAT_FINAL_ASSEMBLY);
235: if (*J != *Jpre) {
236: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
237: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
238: }
239: return(0);
240: }
242: /* ------------------------------------------------------------------- */
245: PetscErrorCode FormInitialSolution(DM da,Vec U,void* ptr)
246: {
247: AppCtx *user=(AppCtx*)ptr;
248: PetscReal c=user->c;
250: PetscInt i,j,xs,ys,xm,ym,Mx,My;
251: PetscScalar **u;
252: PetscReal hx,hy,x,y,r;
255: DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
256: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
258: hx = 1.0/(PetscReal)(Mx-1);
259: hy = 1.0/(PetscReal)(My-1);
261: /* Get pointers to vector data */
262: DMDAVecGetArray(da,U,&u);
264: /* Get local grid boundaries */
265: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
267: /* Compute function over the locally owned part of the grid */
268: for (j=ys; j<ys+ym; j++) {
269: y = j*hy;
270: for (i=xs; i<xs+xm; i++) {
271: x = i*hx;
272: r = PetscSqrtScalar((x-.5)*(x-.5) + (y-.5)*(y-.5));
273: if (r < .125) u[j][i] = PetscExpScalar(c*r*r*r);
274: else u[j][i] = 0.0;
275: }
276: }
278: /* Restore vectors */
279: DMDAVecRestoreArray(da,U,&u);
280: return(0);
281: }