Actual source code: ex11.c

  2: static char help[] =
  3: "This program demonstrates use of the SNES package to solve systems of\n\
  4: nonlinear equations in parallel, using 2-dimensional distributed arrays.\n\
  5: The 2-dim Bratu (SFI - solid fuel ignition) test problem is used, where\n\
  6: analytic formation of the Jacobian is the default.  \n\
  7: \n\
  8:   Solves the linear systems via 2 level additive Schwarz \n\
  9: \n\
 10: The command line\n\
 11: options are:\n\
 12:   -par <parameter>, where <parameter> indicates the problem's nonlinearity\n\
 13:      problem SFI:  <parameter> = Bratu parameter (0 <= par <= 6.81)\n\
 14:   -Mx <xg>, where <xg> = number of grid points in the x-direction on coarse grid\n\
 15:   -My <yg>, where <yg> = number of grid points in the y-direction on coarse grid\n\n";

 17: /*  
 18:     1) Solid Fuel Ignition (SFI) problem.  This problem is modeled by
 19:     the partial differential equation
 20:   
 21:             -Laplacian u - lambda*exp(u) = 0,  0 < x,y < 1 ,
 22:   
 23:     with boundary conditions
 24:    
 25:              u = 0  for  x = 0, x = 1, y = 0, y = 1.
 26:   
 27:     A finite difference approximation with the usual 5-point stencil
 28:     is used to discretize the boundary value problem to obtain a nonlinear 
 29:     system of equations.

 31:    The code has two cases for multilevel solver
 32:      I. the coarse grid Jacobian is computed in parallel 
 33:      II. the coarse grid Jacobian is computed sequentially on each processor
 34:    in both cases the coarse problem is SOLVED redundantly.

 36: */

 38:  #include petscsnes.h
 39:  #include petscda.h
 40:  #include petscmg.h

 42: /* User-defined application contexts */

 44: typedef struct {
 45:    PetscInt        mx,my;            /* number grid points in x and y direction */
 46:    Vec        localX,localF;    /* local vectors with ghost region */
 47:    DA         da;
 48:    Vec        x,b,r;            /* global vectors */
 49:    Mat        J;                /* Jacobian on grid */
 50: } GridCtx;

 52: typedef struct {
 53:    double      param;           /* test problem parameter */
 54:    GridCtx     fine;
 55:    GridCtx     coarse;
 56:    KSP        ksp_coarse;
 57:    KSP        ksp_fine;
 58:    PetscInt         ratio;
 59:    Mat         R;               /* restriction fine to coarse */
 60:    Vec         Rscale;
 61:    PetscTruth  redundant_build; /* build coarse matrix redundantly */
 62:    Vec         localall;        /* contains entire coarse vector on each processor in NATURAL order*/
 63:    VecScatter  tolocalall;      /* maps from parallel "global" coarse vector to localall */
 64:    VecScatter  fromlocalall;    /* maps from localall vector back to global coarse vector */
 65: } AppCtx;

 67: #define COARSE_LEVEL 0
 68: #define FINE_LEVEL   1


 74: /*
 75:       Mm_ratio - ration of grid lines between fine and coarse grids.
 76: */
 79: int main( int argc, char **argv )
 80: {
 81:   SNES           snes;
 82:   AppCtx         user;
 84:   PetscInt       its, N, n, Nx = PETSC_DECIDE, Ny = PETSC_DECIDE, nlocal,Nlocal;
 85:   PetscMPIInt    size;
 86:   double         bratu_lambda_max = 6.81, bratu_lambda_min = 0.;
 87:   KSP            ksp;
 88:   PC             pc;

 90:   /*
 91:       Initialize PETSc, note that default options in ex11options can be 
 92:       overridden at the command line
 93:   */
 94:   PetscInitialize( &argc, &argv,"ex11options",help );

 96:   user.ratio = 2;
 97:   user.coarse.mx = 5; user.coarse.my = 5; user.param = 6.0;
 98:   PetscOptionsGetInt(PETSC_NULL,"-Mx",&user.coarse.mx,PETSC_NULL);
 99:   PetscOptionsGetInt(PETSC_NULL,"-My",&user.coarse.my,PETSC_NULL);
100:   PetscOptionsGetInt(PETSC_NULL,"-ratio",&user.ratio,PETSC_NULL);
101:   user.fine.mx = user.ratio*(user.coarse.mx-1)+1; user.fine.my = user.ratio*(user.coarse.my-1)+1;

103:   PetscOptionsHasName(PETSC_NULL,"-redundant_build",&user.redundant_build);
104:   if (user.redundant_build) {
105:     PetscPrintf(PETSC_COMM_WORLD,"Building coarse Jacobian redundantly\n");
106:   }

108:   PetscPrintf(PETSC_COMM_WORLD,"Coarse grid size %D by %D\n",user.coarse.mx,user.coarse.my);
109:   PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",user.fine.mx,user.fine.my);

111:   PetscOptionsGetReal(PETSC_NULL,"-par",&user.param,PETSC_NULL);
112:   if (user.param >= bratu_lambda_max || user.param < bratu_lambda_min) {
113:     SETERRQ(1,"Lambda is out of range");
114:   }
115:   n = user.fine.mx*user.fine.my; N = user.coarse.mx*user.coarse.my;

117:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
118:   PetscOptionsGetInt(PETSC_NULL,"-Nx",&Nx,PETSC_NULL);
119:   PetscOptionsGetInt(PETSC_NULL,"-Ny",&Ny,PETSC_NULL);

121:   /* Set up distributed array for fine grid */
122:   DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,user.fine.mx,
123:                     user.fine.my,Nx,Ny,1,1,PETSC_NULL,PETSC_NULL,&user.fine.da);
124:   DACreateGlobalVector(user.fine.da,&user.fine.x);
125:   VecDuplicate(user.fine.x,&user.fine.r);
126:   VecDuplicate(user.fine.x,&user.fine.b);
127:   VecGetLocalSize(user.fine.x,&nlocal);
128:   DACreateLocalVector(user.fine.da,&user.fine.localX);
129:   VecDuplicate(user.fine.localX,&user.fine.localF);
130:   MatCreateMPIAIJ(PETSC_COMM_WORLD,nlocal,nlocal,n,n,5,PETSC_NULL,3,PETSC_NULL,&user.fine.J);

132:   /* Set up distributed array for coarse grid */
133:   DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,user.coarse.mx,
134:                     user.coarse.my,Nx,Ny,1,1,PETSC_NULL,PETSC_NULL,&user.coarse.da);
135:   DACreateGlobalVector(user.coarse.da,&user.coarse.x);
136:   VecDuplicate(user.coarse.x,&user.coarse.b);
137:   if (user.redundant_build) {
138:     /* Create scatter from parallel global numbering to redundant with natural ordering */
139:     DAGlobalToNaturalAllCreate(user.coarse.da,&user.tolocalall);
140:     DANaturalAllToGlobalCreate(user.coarse.da,&user.fromlocalall);
141:     VecCreateSeq(PETSC_COMM_SELF,N,&user.localall);
142:     /* Create sequential matrix to hold entire coarse grid Jacobian on each processor */
143:     MatCreateSeqAIJ(PETSC_COMM_SELF,N,N,5,PETSC_NULL,&user.coarse.J);
144:   } else {
145:     VecGetLocalSize(user.coarse.x,&Nlocal);
146:     DACreateLocalVector(user.coarse.da,&user.coarse.localX);
147:     VecDuplicate(user.coarse.localX,&user.coarse.localF);
148:     /* We will compute the coarse Jacobian in parallel */
149:     MatCreateMPIAIJ(PETSC_COMM_WORLD,Nlocal,Nlocal,N,N,5,PETSC_NULL,3,PETSC_NULL,&user.coarse.J);
150:   }

152:   /* Create nonlinear solver */
153:   SNESCreate(PETSC_COMM_WORLD,&snes);

155:   /* provide user function and Jacobian */
156:   SNESSetFunction(snes,user.fine.b,FormFunction,&user);
157:   SNESSetJacobian(snes,user.fine.J,user.fine.J,FormJacobian,&user);

159:   /* set two level additive Schwarz preconditioner */
160:   SNESGetKSP(snes,&ksp);
161:   KSPGetPC(ksp,&pc);
162:   PCSetType(pc,PCMG);
163:   PCMGSetLevels(pc,2,PETSC_NULL);
164:   PCMGSetType(pc,PC_MG_ADDITIVE);

166:   /* always solve the coarse problem redundantly with direct LU solver */
167:   PetscOptionsSetValue("-coarse_pc_type","redundant");
168:   PetscOptionsSetValue("-coarse_redundant_pc_type","lu");

170:   /* Create coarse level */
171:   PCMGGetCoarseSolve(pc,&user.ksp_coarse);
172:   KSPSetOptionsPrefix(user.ksp_coarse,"coarse_");
173:   KSPSetFromOptions(user.ksp_coarse);
174:   KSPSetOperators(user.ksp_coarse,user.coarse.J,user.coarse.J,DIFFERENT_NONZERO_PATTERN);
175:   PCMGSetX(pc,COARSE_LEVEL,user.coarse.x);
176:   PCMGSetRhs(pc,COARSE_LEVEL,user.coarse.b);
177:   if (user.redundant_build) {
178:     PC  rpc;
179:     KSPGetPC(user.ksp_coarse,&rpc);
180:     PCRedundantSetScatter(rpc,user.tolocalall,user.fromlocalall);
181:   }

183:   /* Create fine level */
184:   PCMGGetSmoother(pc,FINE_LEVEL,&user.ksp_fine);
185:   KSPSetOptionsPrefix(user.ksp_fine,"fine_");
186:   KSPSetFromOptions(user.ksp_fine);
187:   KSPSetOperators(user.ksp_fine,user.fine.J,user.fine.J,DIFFERENT_NONZERO_PATTERN);
188:   PCMGSetR(pc,FINE_LEVEL,user.fine.r);
189:   PCMGSetResidual(pc,FINE_LEVEL,PCMGDefaultResidual,user.fine.J);

191:   /* Create interpolation between the levels */
192:   FormInterpolation(&user);
193:   PCMGSetInterpolate(pc,FINE_LEVEL,user.R);
194:   PCMGSetRestriction(pc,FINE_LEVEL,user.R);

196:   /* Set options, then solve nonlinear system */
197:   SNESSetFromOptions(snes);
198:   FormInitialGuess1(&user,user.fine.x);
199:   SNESSolve(snes,PETSC_NULL,user.fine.x);
200:   SNESGetIterationNumber(snes,&its);
201:   PetscPrintf(PETSC_COMM_WORLD,"Number of Newton iterations = %D\n", its );

203:   /* Free data structures */
204:   if (user.redundant_build) {
205:     VecScatterDestroy(user.tolocalall);
206:     VecScatterDestroy(user.fromlocalall);
207:     VecDestroy(user.localall);
208:   } else {
209:     VecDestroy(user.coarse.localX);
210:     VecDestroy(user.coarse.localF);
211:   }

213:   MatDestroy(user.fine.J);
214:   VecDestroy(user.fine.x);
215:   VecDestroy(user.fine.r);
216:   VecDestroy(user.fine.b);
217:   DADestroy(user.fine.da);
218:   VecDestroy(user.fine.localX);
219:   VecDestroy(user.fine.localF);

221:   MatDestroy(user.coarse.J);
222:   VecDestroy(user.coarse.x);
223:   VecDestroy(user.coarse.b);
224:   DADestroy(user.coarse.da);

226:   SNESDestroy(snes);
227:   MatDestroy(user.R);
228:   VecDestroy(user.Rscale);
229:   PetscFinalize();

231:   return 0;
232: }/* --------------------  Form initial approximation ----------------- */
235: PetscErrorCode FormInitialGuess1(AppCtx *user,Vec X)
236: {
237:   PetscInt       i, j, row, mx, my, xs, ys, xm, ym, Xm, Ym, Xs, Ys;
239:   double         one = 1.0, lambda, temp1, temp, hx, hy, hxdhy, hydhx,sc;
240:   PetscScalar    *x;
241:   Vec            localX = user->fine.localX;

243:   mx = user->fine.mx;       my = user->fine.my;            lambda = user->param;
244:   hx = one/(double)(mx-1);  hy = one/(double)(my-1);
245:   sc = hx*hy*lambda;        hxdhy = hx/hy;            hydhx = hy/hx;

247:   temp1 = lambda/(lambda + one);

249:   /* Get ghost points */
250:   DAGetCorners(user->fine.da,&xs,&ys,0,&xm,&ym,0);
251:   DAGetGhostCorners(user->fine.da,&Xs,&Ys,0,&Xm,&Ym,0);
252:   VecGetArray(localX,&x);

254:   /* Compute initial guess */
255:   for (j=ys; j<ys+ym; j++) {
256:     temp = (double)(PetscMin(j,my-j-1))*hy;
257:     for (i=xs; i<xs+xm; i++) {
258:       row = i - Xs + (j - Ys)*Xm;
259:       if (i == 0 || j == 0 || i == mx-1 || j == my-1 ) {
260:         x[row] = 0.0;
261:         continue;
262:       }
263:       x[row] = temp1*sqrt( PetscMin( (double)(PetscMin(i,mx-i-1))*hx,temp) );
264:     }
265:   }
266:   VecRestoreArray(localX,&x);

268:   /* Insert values into global vector */
269:   DALocalToGlobal(user->fine.da,localX,INSERT_VALUES,X);
270:   return 0;
271: }

273:  /* --------------------  Evaluate Function F(x) --------------------- */
276: PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void *ptr)
277: {
278:   AppCtx         *user = (AppCtx *) ptr;
279:   PetscInt       i, j, row, mx, my, xs, ys, xm, ym, Xs, Ys, Xm, Ym;
281:   double         two = 2.0, one = 1.0, lambda,hx, hy, hxdhy, hydhx,sc;
282:   PetscScalar    u, uxx, uyy, *x,*f;
283:   Vec            localX = user->fine.localX, localF = user->fine.localF;

285:   mx = user->fine.mx;       my = user->fine.my;       lambda = user->param;
286:   hx = one/(double)(mx-1);  hy = one/(double)(my-1);
287:   sc = hx*hy*lambda;        hxdhy = hx/hy;            hydhx = hy/hx;

289:   /* Get ghost points */
290:   DAGlobalToLocalBegin(user->fine.da,X,INSERT_VALUES,localX);
291:   DAGlobalToLocalEnd(user->fine.da,X,INSERT_VALUES,localX);
292:   DAGetCorners(user->fine.da,&xs,&ys,0,&xm,&ym,0);
293:   DAGetGhostCorners(user->fine.da,&Xs,&Ys,0,&Xm,&Ym,0);
294:   VecGetArray(localX,&x);
295:   VecGetArray(localF,&f);

297:   /* Evaluate function */
298:   for (j=ys; j<ys+ym; j++) {
299:     row = (j - Ys)*Xm + xs - Xs - 1;
300:     for (i=xs; i<xs+xm; i++) {
301:       row++;
302:       if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
303:         u = x[row];
304:         uxx = (two*u - x[row-1] - x[row+1])*hydhx;
305:         uyy = (two*u - x[row-Xm] - x[row+Xm])*hxdhy;
306:         f[row] = uxx + uyy - sc*exp(u);
307:       } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)){
308:         f[row] = .5*two*(hydhx + hxdhy)*x[row];
309:       } else {
310:         f[row] = .25*two*(hydhx + hxdhy)*x[row];
311:       }
312:     }
313:   }
314:   VecRestoreArray(localX,&x);
315:   VecRestoreArray(localF,&f);

317:   /* Insert values into global vector */
318:   DALocalToGlobal(user->fine.da,localF,INSERT_VALUES,F);
319:   PetscLogFlops(11*ym*xm);
320:   return 0;
321: }

323: /*
324:         Computes the part of the Jacobian associated with this processor 
325: */
328: PetscErrorCode FormJacobian_Grid(AppCtx *user,GridCtx *grid,Vec X, Mat *J,Mat *B)
329: {
330:   Mat            jac = *J;
332:   PetscInt       i, j, row, mx, my, xs, ys, xm, ym, Xs, Ys, Xm, Ym, col[5], nloc, *ltog, grow;
333:   PetscScalar    two = 2.0, one = 1.0, lambda, v[5], hx, hy, hxdhy, hydhx, sc, *x, value;
334:   Vec            localX = grid->localX;

336:   mx = grid->mx;            my = grid->my;            lambda = user->param;
337:   hx = one/(double)(mx-1);  hy = one/(double)(my-1);
338:   sc = hx*hy;               hxdhy = hx/hy;            hydhx = hy/hx;

340:   /* Get ghost points */
341:   DAGlobalToLocalBegin(grid->da,X,INSERT_VALUES,localX);
342:   DAGlobalToLocalEnd(grid->da,X,INSERT_VALUES,localX);
343:   DAGetCorners(grid->da,&xs,&ys,0,&xm,&ym,0);
344:   DAGetGhostCorners(grid->da,&Xs,&Ys,0,&Xm,&Ym,0);
345:   DAGetGlobalIndices(grid->da,&nloc,&ltog);
346:   VecGetArray(localX,&x);

348:   /* Evaluate Jacobian of function */
349:   for (j=ys; j<ys+ym; j++) {
350:     row = (j - Ys)*Xm + xs - Xs - 1;
351:     for (i=xs; i<xs+xm; i++) {
352:       row++;
353:       grow = ltog[row];
354:       if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
355:         v[0] = -hxdhy; col[0] = ltog[row - Xm];
356:         v[1] = -hydhx; col[1] = ltog[row - 1];
357:         v[2] = two*(hydhx + hxdhy) - sc*lambda*exp(x[row]); col[2] = grow;
358:         v[3] = -hydhx; col[3] = ltog[row + 1];
359:         v[4] = -hxdhy; col[4] = ltog[row + Xm];
360:         MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);
361:       } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)){
362:         value = .5*two*(hydhx + hxdhy);
363:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
364:       } else {
365:         value = .25*two*(hydhx + hxdhy);
366:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
367:       }
368:     }
369:   }
370:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
371:   VecRestoreArray(localX,&x);
372:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);

374:   return 0;
375: }

377: /*
378:         Computes the ENTIRE Jacobian associated with the ENTIRE grid sequentially
379:     This is for generating the coarse grid redundantly.

381:           This is BAD code duplication, since the bulk of this routine is the
382:     same as the routine above

384:        Note the numbering of the rows/columns is the NATURAL numbering
385: */
388: PetscErrorCode FormJacobian_Coarse(AppCtx *user,GridCtx *grid,Vec X, Mat *J,Mat *B)
389: {
390:   Mat            jac = *J;
392:   PetscInt       i, j, row, mx, my, col[5];
393:   PetscScalar    two = 2.0, one = 1.0, lambda, v[5], hx, hy, hxdhy, hydhx, sc, *x, value;

395:   mx = grid->mx;            my = grid->my;            lambda = user->param;
396:   hx = one/(double)(mx-1);  hy = one/(double)(my-1);
397:   sc = hx*hy;               hxdhy = hx/hy;            hydhx = hy/hx;

399:   VecGetArray(X,&x);

401:   /* Evaluate Jacobian of function */
402:   for (j=0; j<my; j++) {
403:     row = j*mx - 1;
404:     for (i=0; i<mx; i++) {
405:       row++;
406:       if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
407:         v[0] = -hxdhy; col[0] = row - mx;
408:         v[1] = -hydhx; col[1] = row - 1;
409:         v[2] = two*(hydhx + hxdhy) - sc*lambda*exp(x[row]); col[2] = row;
410:         v[3] = -hydhx; col[3] = row + 1;
411:         v[4] = -hxdhy; col[4] = row + mx;
412:         MatSetValues(jac,1,&row,5,col,v,INSERT_VALUES);
413:       } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)){
414:         value = .5*two*(hydhx + hxdhy);
415:         MatSetValues(jac,1,&row,1,&row,&value,INSERT_VALUES);
416:       } else {
417:         value = .25*two*(hydhx + hxdhy);
418:         MatSetValues(jac,1,&row,1,&row,&value,INSERT_VALUES);
419:       }
420:     }
421:   }
422:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
423:   VecRestoreArray(X,&x);
424:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);

426:   return 0;
427: }

429: /* --------------------  Evaluate Jacobian F'(x) --------------------- */
432: PetscErrorCode FormJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr)
433: {
434:   AppCtx         *user = (AppCtx *) ptr;
436:   KSP            ksp;
437:   PC             pc;
438:   PetscTruth     ismg;

440:   *flag = SAME_NONZERO_PATTERN;
441:   FormJacobian_Grid(user,&user->fine,X,J,B);

443:   /* create coarse grid jacobian for preconditioner */
444:   SNESGetKSP(snes,&ksp);
445:   KSPGetPC(ksp,&pc);
446: 
447:   PetscTypeCompare((PetscObject)pc,PCMG,&ismg);
448:   if (ismg) {

450:     KSPSetOperators(user->ksp_fine,user->fine.J,user->fine.J,SAME_NONZERO_PATTERN);

452:     /* restrict X to coarse grid */
453:     MatMult(user->R,X,user->coarse.x);
454:     VecPointwiseMult(user->coarse.x,user->coarse.x,user->Rscale);

456:     /* form Jacobian on coarse grid */
457:     if (user->redundant_build) {
458:       /* get copy of coarse X onto each processor */
459:       VecScatterBegin(user->coarse.x,user->localall,INSERT_VALUES,SCATTER_FORWARD,user->tolocalall);
460:       VecScatterEnd(user->coarse.x,user->localall,INSERT_VALUES,SCATTER_FORWARD,user->tolocalall);
461:       FormJacobian_Coarse(user,&user->coarse,user->localall,&user->coarse.J,&user->coarse.J);

463:     } else {
464:       /* coarse grid Jacobian computed in parallel */
465:       FormJacobian_Grid(user,&user->coarse,user->coarse.x,&user->coarse.J,&user->coarse.J);
466:     }
467:     KSPSetOperators(user->ksp_coarse,user->coarse.J,user->coarse.J,SAME_NONZERO_PATTERN);
468:   }

470:   return 0;
471: }


476: /*
477:       Forms the interpolation (and restriction) operator from 
478: coarse grid to fine.
479: */
480: PetscErrorCode FormInterpolation(AppCtx *user)
481: {
483:   PetscInt       i,j,i_start,m_fine,j_start,m,n,*idx;
484:   PetscInt       m_ghost,n_ghost,*idx_c,m_ghost_c,n_ghost_c,m_coarse;
485:   PetscInt       row,i_start_ghost,j_start_ghost,cols[4], m_c;
486:   PetscInt       nc,ratio = user->ratio,m_c_local,m_fine_locaol;
487:   PetscInt       i_c,j_c,i_start_c,j_start_c,n_c,i_start_ghost_c,j_start_ghost_c,col;
488:   PetscScalar    v[4],x,y, one = 1.0;
489:   Mat            mat;
490:   Vec            Rscale;
491: 
492:   DAGetCorners(user->fine.da,&i_start,&j_start,0,&m,&n,0);
493:   DAGetGhostCorners(user->fine.da,&i_start_ghost,&j_start_ghost,0,&m_ghost,&n_ghost,0);
494:   DAGetGlobalIndices(user->fine.da,PETSC_NULL,&idx);

496:   DAGetCorners(user->coarse.da,&i_start_c,&j_start_c,0,&m_c,&n_c,0);
497:   DAGetGhostCorners(user->coarse.da,&i_start_ghost_c,&j_start_ghost_c,0,&m_ghost_c,&n_ghost_c,0);
498:   DAGetGlobalIndices(user->coarse.da,PETSC_NULL,&idx_c);

500:   /* create interpolation matrix */
501:   VecGetLocalSize(user->fine.x,&m_fine_local);
502:   VecGetLocalSize(user->coarse.x,&m_c_local);
503:   VecGetSize(user->fine.x,&m_fine);
504:   VecGetSize(user->coarse.x,&m_coarse);
505:   MatCreateMPIAIJ(PETSC_COMM_WORLD,m_fine_local,m_c_local,m_fine,m_coarse,
506:                          5,0,3,0,&mat);

508:   /* loop over local fine grid nodes setting interpolation for those*/
509:   for ( j=j_start; j<j_start+n; j++ ) {
510:     for ( i=i_start; i<i_start+m; i++ ) {
511:       /* convert to local "natural" numbering and then to PETSc global numbering */
512:       row    = idx[m_ghost*(j-j_start_ghost) + (i-i_start_ghost)];

514:       i_c = (i/ratio);    /* coarse grid node to left of fine grid node */
515:       j_c = (j/ratio);    /* coarse grid node below fine grid node */

517:       /* 
518:          Only include those interpolation points that are truly 
519:          nonzero. Note this is very important for final grid lines
520:          in x and y directions; since they have no right/top neighbors
521:       */
522:       x  = ((double)(i - i_c*ratio))/((double)ratio);
523:       y  = ((double)(j - j_c*ratio))/((double)ratio);
524:       nc = 0;
525:       /* one left and below; or we are right on it */
526:       if (j_c < j_start_ghost_c || j_c > j_start_ghost_c+n_ghost_c) {
527:         SETERRQ3(1,"Sorry j %D %D %D",j_c,j_start_ghost_c,j_start_ghost_c+n_ghost_c);
528:       }
529:       if (i_c < i_start_ghost_c || i_c > i_start_ghost_c+m_ghost_c) {
530:         SETERRQ3(1,"Sorry i %D %D %D",i_c,i_start_ghost_c,i_start_ghost_c+m_ghost_c);
531:       }
532:       col      = m_ghost_c*(j_c-j_start_ghost_c) + (i_c-i_start_ghost_c);
533:       cols[nc] = idx_c[col];
534:       v[nc++]  = x*y - x - y + 1.0;
535:       /* one right and below */
536:       if (i_c*ratio != i) {
537:         cols[nc] = idx_c[col+1];
538:         v[nc++]  = -x*y + x;
539:       }
540:       /* one left and above */
541:       if (j_c*ratio != j) {
542:         cols[nc] = idx_c[col+m_ghost_c];
543:         v[nc++]  = -x*y + y;
544:       }
545:       /* one right and above */
546:       if (j_c*ratio != j && i_c*ratio != i) {
547:         cols[nc] = idx_c[col+m_ghost_c+1];
548:         v[nc++]  = x*y;
549:       }
550:       MatSetValues(mat,1,&row,nc,cols,v,INSERT_VALUES);
551:     }
552:   }
553:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
554:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);

556:   VecDuplicate(user->coarse.x,&Rscale);
557:   VecSet(user->fine.x,one);
558:   MatMultTranspose(mat,user->fine.x,Rscale);
559:   VecReciprocal(Rscale);
560:   user->Rscale = Rscale;
561:   user->R = mat;
562:   return 0;
563: }