Actual source code: ex5.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Demonstrates Pattern Formation with Reaction-Diffusion Equations.\n";

  4: /*
  5:      Page 21, Pattern Formation with Reaction-Diffusion Equations

  7:         u_t = D1 (u_xx + u_yy)  - u*v^2 + gama(1 -u)
  8:         v_t = D2 (v_xx + v_yy)  + u*v^2 - (gamma + kappa)v

 10:     Unlike in the book this uses periodic boundary conditions instead of Neumann
 11:     (since they are easier for finite differences).
 12: */

 14: /*
 15:       Helpful runtime monitor options:
 16:            -ts_monitor_draw_solution
 17:            -draw_save -draw_save_movie

 19:       Helpful runtime linear solver options:
 20:            -pc_type mg -pc_mg_galerkin -da_refine 1 -snes_monitor -ksp_monitor -ts_view  (note that these Jacobians are so well-conditioned multigrid may not be the best solver)

 22:       Point your browser to localhost:8080 to monitor the simulation
 23:            ./ex5  -ts_view_pre saws  -stack_view saws -draw_save -draw_save_single_file -x_virtual -ts_monitor_draw_solution -saws_root .

 25: */

 27: /*

 29:    Include "petscdmda.h" so that we can use distributed arrays (DMDAs).
 30:    Include "petscts.h" so that we can use SNES solvers.  Note that this
 31:    file automatically includes:
 32:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 33:      petscmat.h - matrices
 34:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 35:      petscviewer.h - viewers               petscpc.h  - preconditioners
 36:      petscksp.h   - linear solvers
 37: */
 38: #include <petscdm.h>
 39: #include <petscdmda.h>
 40: #include <petscts.h>

 42: typedef struct {
 43:   PetscScalar u,v;
 44: } Field;

 46: typedef struct {
 47:   PetscReal D1,D2,gamma,kappa;
 48: } AppCtx;

 50: /*
 51:    User-defined routines
 52: */
 53: extern PetscErrorCode RHSFunction(TS,PetscReal,Vec,Vec,void*),InitialConditions(DM,Vec);
 54: extern PetscErrorCode RHSJacobian(TS,PetscReal,Vec,Mat,Mat,void*);

 58: int main(int argc,char **argv)
 59: {
 60:   TS             ts;                  /* ODE integrator */
 61:   Vec            x;                   /* solution */
 63:   DM             da;
 64:   AppCtx         appctx;

 66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 67:      Initialize program
 68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 69:   PetscInitialize(&argc,&argv,(char*)0,help);

 71:   appctx.D1    = 8.0e-5;
 72:   appctx.D2    = 4.0e-5;
 73:   appctx.gamma = .024;
 74:   appctx.kappa = .06;

 76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 77:      Create distributed array (DMDA) to manage parallel grid and vectors
 78:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 79:   DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_PERIODIC,DM_BOUNDARY_PERIODIC,DMDA_STENCIL_STAR,-65,-65,PETSC_DECIDE,PETSC_DECIDE,2,1,NULL,NULL,&da);
 80:   DMDASetFieldName(da,0,"u");
 81:   DMDASetFieldName(da,1,"v");

 83:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 84:      Extract global vectors from DMDA; then duplicate for remaining
 85:      vectors that are the same types
 86:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 87:   DMCreateGlobalVector(da,&x);

 89:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 90:      Create timestepping solver context
 91:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 92:   TSCreate(PETSC_COMM_WORLD,&ts);
 93:   TSSetType(ts,TSARKIMEX);
 94:   TSARKIMEXSetFullyImplicit(ts,PETSC_TRUE);
 95:   TSSetDM(ts,da);
 96:   TSSetProblemType(ts,TS_NONLINEAR);
 97:   TSSetRHSFunction(ts,NULL,RHSFunction,&appctx);
 98:   TSSetRHSJacobian(ts,NULL,NULL,RHSJacobian,&appctx);

100:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101:      Set initial conditions
102:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
103:   InitialConditions(da,x);
104:   TSSetSolution(ts,x);

106:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107:      Set solver options
108:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
109:   TSSetDuration(ts,PETSC_DEFAULT,2000.0);
110:   TSSetInitialTimeStep(ts,0.0,.0001);
111:   TSSetFromOptions(ts);

113:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114:      Solve ODE system
115:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
116:   TSSolve(ts,x);

118:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119:      Free work space.  All PETSc objects should be destroyed when they
120:      are no longer needed.
121:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
122:   VecDestroy(&x);
123:   TSDestroy(&ts);
124:   DMDestroy(&da);

126:   PetscFinalize();
127:   return(0);
128: }
129: /* ------------------------------------------------------------------- */
132: /*
133:    RHSFunction - Evaluates nonlinear function, F(x).

135:    Input Parameters:
136: .  ts - the TS context
137: .  X - input vector
138: .  ptr - optional user-defined context, as set by TSSetRHSFunction()

140:    Output Parameter:
141: .  F - function vector
142:  */
143: PetscErrorCode RHSFunction(TS ts,PetscReal ftime,Vec U,Vec F,void *ptr)
144: {
145:   AppCtx         *appctx = (AppCtx*)ptr;
146:   DM             da;
148:   PetscInt       i,j,Mx,My,xs,ys,xm,ym;
149:   PetscReal      hx,hy,sx,sy;
150:   PetscScalar    uc,uxx,uyy,vc,vxx,vyy;
151:   Field          **u,**f;
152:   Vec            localU;

155:   TSGetDM(ts,&da);
156:   DMGetLocalVector(da,&localU);
157:   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);

159:   hx = 2.50/(PetscReal)(Mx); sx = 1.0/(hx*hx);
160:   hy = 2.50/(PetscReal)(My); sy = 1.0/(hy*hy);

162:   /*
163:      Scatter ghost points to local vector,using the 2-step process
164:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
165:      By placing code between these two statements, computations can be
166:      done while messages are in transition.
167:   */
168:   DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
169:   DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);

171:   /*
172:      Get pointers to vector data
173:   */
174:   DMDAVecGetArray(da,localU,&u);
175:   DMDAVecGetArray(da,F,&f);

177:   /*
178:      Get local grid boundaries
179:   */
180:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

182:   /*
183:      Compute function over the locally owned part of the grid
184:   */
185:   for (j=ys; j<ys+ym; j++) {
186:     for (i=xs; i<xs+xm; i++) {
187:       uc        = u[j][i].u;
188:       uxx       = (-2.0*uc + u[j][i-1].u + u[j][i+1].u)*sx;
189:       uyy       = (-2.0*uc + u[j-1][i].u + u[j+1][i].u)*sy;
190:       vc        = u[j][i].v;
191:       vxx       = (-2.0*vc + u[j][i-1].v + u[j][i+1].v)*sx;
192:       vyy       = (-2.0*vc + u[j-1][i].v + u[j+1][i].v)*sy;
193:       f[j][i].u = appctx->D1*(uxx + uyy) - uc*vc*vc + appctx->gamma*(1.0 - uc);
194:       f[j][i].v = appctx->D2*(vxx + vyy) + uc*vc*vc - (appctx->gamma + appctx->kappa)*vc;
195:     }
196:   }
197:   PetscLogFlops(16*xm*ym);

199:   /*
200:      Restore vectors
201:   */
202:   DMDAVecRestoreArray(da,localU,&u);
203:   DMDAVecRestoreArray(da,F,&f);
204:   DMRestoreLocalVector(da,&localU);
205:   return(0);
206: }

208: /* ------------------------------------------------------------------- */
211: PetscErrorCode InitialConditions(DM da,Vec U)
212: {
214:   PetscInt       i,j,xs,ys,xm,ym,Mx,My;
215:   Field          **u;
216:   PetscReal      hx,hy,x,y;

219:   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);

221:   hx = 2.5/(PetscReal)(Mx);
222:   hy = 2.5/(PetscReal)(My);

224:   /*
225:      Get pointers to vector data
226:   */
227:   DMDAVecGetArray(da,U,&u);

229:   /*
230:      Get local grid boundaries
231:   */
232:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

234:   /*
235:      Compute function over the locally owned part of the grid
236:   */
237:   for (j=ys; j<ys+ym; j++) {
238:     y = j*hy;
239:     for (i=xs; i<xs+xm; i++) {
240:       x = i*hx;
241:       if ((1.0 <= x) && (x <= 1.5) && (1.0 <= y) && (y <= 1.5)) u[j][i].v = .25*PetscPowReal(PetscSinReal(4.0*PETSC_PI*x),2.0)*PetscPowReal(PetscSinReal(4.0*PETSC_PI*y),2.0);
242:       else u[j][i].v = 0.0;

244:       u[j][i].u = 1.0 - 2.0*u[j][i].v;
245:     }
246:   }

248:   /*
249:      Restore vectors
250:   */
251:   DMDAVecRestoreArray(da,U,&u);
252:   return(0);
253: }

257: PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat BB,void *ctx)
258: {
259:   AppCtx         *appctx = (AppCtx*)ctx;     /* user-defined application context */
260:   DM             da;
262:   PetscInt       i,j,Mx,My,xs,ys,xm,ym;
263:   PetscReal      hx,hy,sx,sy;
264:   PetscScalar    uc,vc;
265:   Field          **u;
266:   Vec            localU;
267:   MatStencil     stencil[6],rowstencil;
268:   PetscScalar    entries[6];

271:   TSGetDM(ts,&da);
272:   DMGetLocalVector(da,&localU);
273:   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);

275:   hx = 2.50/(PetscReal)(Mx); sx = 1.0/(hx*hx);
276:   hy = 2.50/(PetscReal)(My); sy = 1.0/(hy*hy);

278:   /*
279:      Scatter ghost points to local vector,using the 2-step process
280:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
281:      By placing code between these two statements, computations can be
282:      done while messages are in transition.
283:   */
284:   DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
285:   DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);

287:   /*
288:      Get pointers to vector data
289:   */
290:   DMDAVecGetArray(da,localU,&u);

292:   /*
293:      Get local grid boundaries
294:   */
295:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

297:   stencil[0].k = 0;
298:   stencil[1].k = 0;
299:   stencil[2].k = 0;
300:   stencil[3].k = 0;
301:   stencil[4].k = 0;
302:   stencil[5].k = 0;
303:   rowstencil.k = 0;
304:   /*
305:      Compute function over the locally owned part of the grid
306:   */
307:   for (j=ys; j<ys+ym; j++) {

309:     stencil[0].j = j-1;
310:     stencil[1].j = j+1;
311:     stencil[2].j = j;
312:     stencil[3].j = j;
313:     stencil[4].j = j;
314:     stencil[5].j = j;
315:     rowstencil.k = 0; rowstencil.j = j;
316:     for (i=xs; i<xs+xm; i++) {
317:       uc = u[j][i].u;
318:       vc = u[j][i].v;

320:       /*      uxx       = (-2.0*uc + u[j][i-1].u + u[j][i+1].u)*sx;
321:       uyy       = (-2.0*uc + u[j-1][i].u + u[j+1][i].u)*sy;

323:       vxx       = (-2.0*vc + u[j][i-1].v + u[j][i+1].v)*sx;
324:       vyy       = (-2.0*vc + u[j-1][i].v + u[j+1][i].v)*sy;
325:        f[j][i].u = appctx->D1*(uxx + uyy) - uc*vc*vc + appctx->gamma*(1.0 - uc);*/

327:       stencil[0].i = i; stencil[0].c = 0; entries[0] = appctx->D1*sy;
328:       stencil[1].i = i; stencil[1].c = 0; entries[1] = appctx->D1*sy;
329:       stencil[2].i = i-1; stencil[2].c = 0; entries[2] = appctx->D1*sx;
330:       stencil[3].i = i+1; stencil[3].c = 0; entries[3] = appctx->D1*sx;
331:       stencil[4].i = i; stencil[4].c = 0; entries[4] = -2.0*appctx->D1*(sx + sy) - vc*vc - appctx->gamma;
332:       stencil[5].i = i; stencil[5].c = 1; entries[5] = -2.0*uc*vc;
333:       rowstencil.i = i; rowstencil.c = 0;

335:       MatSetValuesStencil(A,1,&rowstencil,6,stencil,entries,INSERT_VALUES);

337:       stencil[0].c = 1; entries[0] = appctx->D2*sy;
338:       stencil[1].c = 1; entries[1] = appctx->D2*sy;
339:       stencil[2].c = 1; entries[2] = appctx->D2*sx;
340:       stencil[3].c = 1; entries[3] = appctx->D2*sx;
341:       stencil[4].c = 1; entries[4] = -2.0*appctx->D2*(sx + sy) + 2.0*uc*vc - appctx->gamma - appctx->kappa;
342:       stencil[5].c = 0; entries[5] = vc*vc;
343:       rowstencil.c = 1;

345:       MatSetValuesStencil(A,1,&rowstencil,6,stencil,entries,INSERT_VALUES);
346:       /* f[j][i].v = appctx->D2*(vxx + vyy) + uc*vc*vc - (appctx->gamma + appctx->kappa)*vc; */
347:     }
348:   }

350:   /*
351:      Restore vectors
352:   */
353:   PetscLogFlops(19*xm*ym);
354:   DMDAVecRestoreArray(da,localU,&u);
355:   DMRestoreLocalVector(da,&localU);
356:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
357:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
358:   MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
359:   return(0);
360: }