Actual source code: ex15.c

petsc-master 2016-12-03
Report Typos and Errors

  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);if (ierr) return ierr;
 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,NULL,"-nstencilpts",&user.nstencilpts,NULL);
 72:   PetscOptionsGetInt(NULL,NULL,"-boundary",&user.boundary,NULL);
 73:   PetscOptionsHasName(NULL,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:   DMSetFromOptions(da);
 84:   DMSetUp(da);
 85:   user.da = da;

 87:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 88:      Extract global vectors from DMDA;
 89:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 90:   DMCreateGlobalVector(da,&u);
 91:   VecDuplicate(u,&r);

 93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 94:      Create timestepping solver context
 95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 96:   TSCreate(PETSC_COMM_WORLD,&ts);
 97:   TSSetProblemType(ts,TS_NONLINEAR);
 98:   TSSetType(ts,TSBEULER);
 99:   TSSetDM(ts,da);
100:   TSSetIFunction(ts,r,FormIFunction,&user);
101:   TSSetDuration(ts,maxsteps,1.0);
102:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);

104:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105:      Set initial conditions
106:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
107:   FormInitialSolution(u,&user);
108:   TSSetSolution(ts,u);
109:   dt   = .01;
110:   TSSetInitialTimeStep(ts,0.0,dt);

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

132:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133:    Sets various TS parameters from user options
134:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135:   TSSetFromOptions(ts);

137:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138:      Solve nonlinear system
139:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140:   TSSolve(ts,u);

142:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143:      Free work space.
144:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145:   MatDestroy(&J);
146:   MatDestroy(&Jmf);
147:   VecDestroy(&u);
148:   VecDestroy(&r);
149:   TSDestroy(&ts);
150:   DMDestroy(&da);

152:   PetscFinalize();
153:   return ierr;
154: }

156: /* --------------------------------------------------------------------- */
157: /*
158:   FormIFunction = Udot - RHSFunction
159: */
162: PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
163: {
165:   AppCtx         *user=(AppCtx*)ctx;
166:   DM             da   = (DM)user->da;
167:   PetscInt       i,j,Mx,My,xs,ys,xm,ym;
168:   PetscReal      hx,hy,sx,sy;
169:   PetscScalar    u,uxx,uyy,**uarray,**f,**udot;
170:   Vec            localU;

173:   DMGetLocalVector(da,&localU);
174:   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);

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

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

189:   /* Get pointers to vector data */
190:   DMDAVecGetArrayRead(da,localU,&uarray);
191:   DMDAVecGetArray(da,F,&f);
192:   DMDAVecGetArray(da,Udot,&udot);

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

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

238:   /* Restore vectors */
239:   DMDAVecRestoreArrayRead(da,localU,&uarray);
240:   DMDAVecRestoreArray(da,F,&f);
241:   DMDAVecRestoreArray(da,Udot,&udot);
242:   DMRestoreLocalVector(da,&localU);
243:   PetscLogFlops(11.0*ym*xm);
244:   return(0);
245: }

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

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

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

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

277:       } else if (user->boundary > 0 && i == 0) {  /* Left 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 && i == Mx-1) { /* Right Neumann */
281:         col[nc].j = j; col[nc].i = i;   vals[nc++] = 1.0;
282:         col[nc].j = j; col[nc].i = i-1; vals[nc++] = -1.0;
283:       } else if (user->boundary > 0 && j == 0) {  /* Bottom 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 if (user->boundary > 0 && j == My-1) { /* Top Neumann */
287:         col[nc].j = j;   col[nc].i = i;  vals[nc++] = 1.0;
288:         col[nc].j = j-1; col[nc].i = i;  vals[nc++] = -1.0;
289:       } else {   /* Interior */
290:         col[nc].j = j-1; col[nc].i = i;   vals[nc++] = -sy;
291:         col[nc].j = j;   col[nc].i = i-1; vals[nc++] = -sx;
292:         col[nc].j = j;   col[nc].i = i;   vals[nc++] = 2.0*(sx + sy) + a;
293:         col[nc].j = j;   col[nc].i = i+1; vals[nc++] = -sx;
294:         col[nc].j = j+1; col[nc].i = i;   vals[nc++] = -sy;
295:       }
296:       MatSetValuesStencil(Jpre,1,&row,nc,col,vals,INSERT_VALUES);
297:     }
298:   }
299:   MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY);
300:   MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY);
301:   if (J != Jpre) {
302:     MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
303:     MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
304:   }

306:   if (user->viewJacobian) {
307:     PetscPrintf(PetscObjectComm((PetscObject)Jpre),"Jpre:\n");
308:     MatView(Jpre,PETSC_VIEWER_STDOUT_WORLD);
309:   }
310:   return(0);
311: }

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

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

329:   hx = 1.0/(PetscReal)(Mx-1);
330:   hy = 1.0/(PetscReal)(My-1);

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

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

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

349:   /* Restore vectors */
350:   DMDAVecRestoreArray(da,U,&u);
351:   return(0);
352: }