Actual source code: ex15.c

petsc-3.4.4 2014-03-13
  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:   DMCreateMatrix(da,MATAIJ,&J);
112:   Jtype = 0;
113:   PetscOptionsGetInt(NULL, "-Jtype",&Jtype,NULL);
114:   if (Jtype == 0) { /* use user provided Jacobian evaluation routine */
115:     if (user.nstencilpts != 5) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"user Jacobian routine FormIJacobian() does not support nstencilpts=%D",user.nstencilpts);
116:     TSSetIJacobian(ts,J,J,FormIJacobian,&user);
117:   } else { /* use finite difference Jacobian J as preconditioner and '-snes_mf_operator' for Mat*vec */
118:     TSGetSNES(ts,&snes);
119:     MatCreateSNESMF(snes,&Jmf);
120:     if (Jtype == 1) { /* slow finite difference J; */
121:       SNESSetJacobian(snes,Jmf,J,SNESComputeJacobianDefault,NULL);
122:     } else if (Jtype == 2) { /* Use coloring to compute  finite difference J efficiently */
123:       SNESSetJacobian(snes,Jmf,J,SNESComputeJacobianDefaultColor,0);
124:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Jtype is not supported");
125:   }

127:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128:    Sets various TS parameters from user options
129:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
130:   TSSetFromOptions(ts);

132:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133:      Solve nonlinear system
134:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135:   TSSolve(ts,u);

137:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138:      Free work space.
139:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140:   MatDestroy(&J);
141:   MatDestroy(&Jmf);
142:   VecDestroy(&u);
143:   VecDestroy(&r);
144:   TSDestroy(&ts);
145:   DMDestroy(&da);

147:   PetscFinalize();
148:   return(0);
149: }

151: /* --------------------------------------------------------------------- */
152: /*
153:   FormIFunction = Udot - RHSFunction
154: */
157: PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
158: {
160:   AppCtx         *user=(AppCtx*)ctx;
161:   DM             da   = (DM)user->da;
162:   PetscInt       i,j,Mx,My,xs,ys,xm,ym;
163:   PetscReal      hx,hy,sx,sy;
164:   PetscScalar    u,uxx,uyy,**uarray,**f,**udot;
165:   Vec            localU;

168:   DMGetLocalVector(da,&localU);
169:   DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
170:                      PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);

172:   hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
173:   hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);
174:   if (user->nstencilpts == 9 && hx != hy) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"hx must equal hy when nstencilpts = 9 for this example");

176:   /*
177:      Scatter ghost points to local vector,using the 2-step process
178:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
179:      By placing code between these two statements, computations can be
180:      done while messages are in transition.
181:   */
182:   DMGlobalToLocalBegin(da,U,INSERT_VALUES,localU);
183:   DMGlobalToLocalEnd(da,U,INSERT_VALUES,localU);

185:   /* Get pointers to vector data */
186:   DMDAVecGetArray(da,localU,&uarray);
187:   DMDAVecGetArray(da,F,&f);
188:   DMDAVecGetArray(da,Udot,&udot);

190:   /* Get local grid boundaries */
191:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

193:   /* Compute function over the locally owned part of the grid */
194:   for (j=ys; j<ys+ym; j++) {
195:     for (i=xs; i<xs+xm; i++) {
196:       /* Boundary conditions */
197:       if (i == 0 || j == 0 || i == Mx-1 || j == My-1) {
198:         if (user->boundary == 0) { /* Drichlet BC */
199:           f[j][i] = uarray[j][i]; /* F = U */
200:         } else {                  /* Neumann BC */
201:           if (i == 0 && j == 0) {              /* SW corner */
202:             f[j][i] = uarray[j][i] - uarray[j+1][i+1];
203:           } else if (i == Mx-1 && j == 0) {    /* SE corner */
204:             f[j][i] = uarray[j][i] - uarray[j+1][i-1];
205:           } else if (i == 0 && j == My-1) {    /* NW corner */
206:             f[j][i] = uarray[j][i] - uarray[j-1][i+1];
207:           } else if (i == Mx-1 && j == My-1) { /* NE corner */
208:             f[j][i] = uarray[j][i] - uarray[j-1][i-1];
209:           } else if (i == 0) {                  /* Left */
210:             f[j][i] = uarray[j][i] - uarray[j][i+1];
211:           } else if (i == Mx-1) {               /* Right */
212:             f[j][i] = uarray[j][i] - uarray[j][i-1];
213:           } else if (j == 0) {                 /* Bottom */
214:             f[j][i] = uarray[j][i] - uarray[j+1][i];
215:           } else if (j == My-1) {               /* Top */
216:             f[j][i] = uarray[j][i] - uarray[j-1][i];
217:           }
218:         }
219:       } else { /* Interior */
220:         u = uarray[j][i];
221:         /* 5-point stencil */
222:         uxx = (-2.0*u + uarray[j][i-1] + uarray[j][i+1]);
223:         uyy = (-2.0*u + uarray[j-1][i] + uarray[j+1][i]);
224:         if (user->nstencilpts == 9) {
225:           /* 9-point stencil: assume hx=hy */
226:           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;
227:           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;
228:         }
229:         f[j][i] = udot[j][i] - (uxx*sx + uyy*sy);
230:       }
231:     }
232:   }

234:   /* Restore vectors */
235:   DMDAVecRestoreArray(da,localU,&uarray);
236:   DMDAVecRestoreArray(da,F,&f);
237:   DMDAVecRestoreArray(da,Udot,&udot);
238:   DMRestoreLocalVector(da,&localU);
239:   PetscLogFlops(11.0*ym*xm);
240:   return(0);
241: }

243: /* --------------------------------------------------------------------- */
244: /*
245:   FormIJacobian() - Compute IJacobian = dF/dU + a dF/dUdot
246:   This routine is not used with option '-use_coloring'
247: */
250: PetscErrorCode FormIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat *J,Mat *Jpre,MatStructure *str,void *ctx)
251: {
253:   PetscInt       i,j,Mx,My,xs,ys,xm,ym,nc;
254:   AppCtx         *user = (AppCtx*)ctx;
255:   DM             da    = (DM)user->da;
256:   MatStencil     col[5],row;
257:   PetscScalar    vals[5],hx,hy,sx,sy;

260:   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);
261:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

263:   hx = 1.0/(PetscReal)(Mx-1); sx = 1.0/(hx*hx);
264:   hy = 1.0/(PetscReal)(My-1); sy = 1.0/(hy*hy);

266:   for (j=ys; j<ys+ym; j++) {
267:     for (i=xs; i<xs+xm; i++) {
268:       nc    = 0;
269:       row.j = j; row.i = i;
270:       if (user->boundary == 0 && (i == 0 || i == Mx-1 || j == 0 || j == My-1)) {
271:         col[nc].j = j; col[nc].i = i; vals[nc++] = 1.0;

273:       } else if (user->boundary > 0 && i == 0) {  /* Left Neumann */
274:         col[nc].j = j; col[nc].i = i;   vals[nc++] = 1.0;
275:         col[nc].j = j; col[nc].i = i+1; vals[nc++] = -1.0;
276:       } else if (user->boundary > 0 && i == Mx-1) { /* Right Neumann */
277:         col[nc].j = j; col[nc].i = i;   vals[nc++] = 1.0;
278:         col[nc].j = j; col[nc].i = i-1; vals[nc++] = -1.0;
279:       } else if (user->boundary > 0 && j == 0) {  /* Bottom Neumann */
280:         col[nc].j = j;   col[nc].i = i; vals[nc++] = 1.0;
281:         col[nc].j = j+1; col[nc].i = i; vals[nc++] = -1.0;
282:       } else if (user->boundary > 0 && j == My-1) { /* Top Neumann */
283:         col[nc].j = j;   col[nc].i = i;  vals[nc++] = 1.0;
284:         col[nc].j = j-1; col[nc].i = i;  vals[nc++] = -1.0;
285:       } else {   /* Interior */
286:         col[nc].j = j-1; col[nc].i = i;   vals[nc++] = -sy;
287:         col[nc].j = j;   col[nc].i = i-1; vals[nc++] = -sx;
288:         col[nc].j = j;   col[nc].i = i;   vals[nc++] = 2.0*(sx + sy) + a;
289:         col[nc].j = j;   col[nc].i = i+1; vals[nc++] = -sx;
290:         col[nc].j = j+1; col[nc].i = i;   vals[nc++] = -sy;
291:       }
292:       MatSetValuesStencil(*Jpre,1,&row,nc,col,vals,INSERT_VALUES);
293:     }
294:   }
295:   MatAssemblyBegin(*Jpre,MAT_FINAL_ASSEMBLY);
296:   MatAssemblyEnd(*Jpre,MAT_FINAL_ASSEMBLY);
297:   if (*J != *Jpre) {
298:     MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
299:     MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
300:   }

302:   if (user->viewJacobian) {
303:     PetscPrintf(PetscObjectComm((PetscObject)*Jpre),"Jpre:\n");
304:     MatView(*Jpre,PETSC_VIEWER_STDOUT_WORLD);
305:   }
306:   return(0);
307: }

309: /* ------------------------------------------------------------------- */
312: PetscErrorCode FormInitialSolution(Vec U,void *ptr)
313: {
314:   AppCtx         *user=(AppCtx*)ptr;
315:   DM             da   =user->da;
316:   PetscReal      c    =user->c;
318:   PetscInt       i,j,xs,ys,xm,ym,Mx,My;
319:   PetscScalar    **u;
320:   PetscReal      hx,hy,x,y,r;

323:   DMDAGetInfo(da,PETSC_IGNORE,&Mx,&My,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
324:                      PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);

326:   hx = 1.0/(PetscReal)(Mx-1);
327:   hy = 1.0/(PetscReal)(My-1);

329:   /* Get pointers to vector data */
330:   DMDAVecGetArray(da,U,&u);

332:   /* Get local grid boundaries */
333:   DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);

335:   /* Compute function over the locally owned part of the grid */
336:   for (j=ys; j<ys+ym; j++) {
337:     y = j*hy;
338:     for (i=xs; i<xs+xm; i++) {
339:       x = i*hx;
340:       r = PetscSqrtScalar((x-.5)*(x-.5) + (y-.5)*(y-.5));
341:       if (r < .125) u[j][i] = PetscExpScalar(c*r*r*r);
342:       else u[j][i] = 0.0;
343:     }
344:   }

346:   /* Restore vectors */
347:   DMDAVecRestoreArray(da,U,&u);
348:   return(0);
349: }