Actual source code: ex70.c

petsc-master 2017-09-24
Report Typos and Errors
  1: static char help[] = "Poiseuille flow problem. Viscous, laminar flow in a 2D channel with parabolic velocity\n\
  2:                       profile and linear pressure drop, exact solution of the 2D Stokes\n";

  4: /*---------------------------------------------------------------------------- */
  5: /* M A R I T I M E  R E S E A R C H  I N S T I T U T E  N E T H E R L A N D S  */
  6: /*---------------------------------------------------------------------------- */
  7: /* author : Christiaan M. Klaij                                                */
  8: /*---------------------------------------------------------------------------- */
  9: /*                                                                             */
 10: /* Poiseuille flow problem.                                                    */
 11: /*                                                                             */
 12: /* Viscous, laminar flow in a 2D channel with parabolic velocity               */
 13: /* profile and linear pressure drop, exact solution of the 2D Stokes           */
 14: /* equations.                                                                  */
 15: /*                                                                             */
 16: /* Discretized with the cell-centered finite-volume method on a                */
 17: /* Cartesian grid with co-located variables. Variables ordered as              */
 18: /* [u1...uN v1...vN p1...pN]^T. Matrix [A00 A01; A10, A11] solved with         */
 19: /* PCFIELDSPLIT. Lower factorization is used to mimick the Semi-Implicit       */
 20: /* Method for Pressure Linked Equations (SIMPLE) used as preconditioner        */
 21: /* instead of solver.                                                          */
 22: /*                                                                             */
 23: /* Disclaimer: does not contain the pressure-weighed interpolation             */
 24: /* method needed to suppress spurious pressure modes in real-life              */
 25: /* problems.                                                                   */
 26: /*                                                                             */
 27: /* Usage:                                                                      */
 28: /*                                                                             */
 29: /* mpiexec -n 2 ./ex70 -nx 32 -ny 48 -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type lower -fieldsplit_1_pc_type none */
 30: /*                                                                             */
 31: /*   Runs with PCFIELDSPLIT on 32x48 grid, no PC for the Schur                 */
 32: /*   complement because A11 is zero. FGMRES is needed because                  */
 33: /*   PCFIELDSPLIT is a variable preconditioner.                                */
 34: /*                                                                             */
 35: /* mpiexec -n 2 ./ex70 -nx 32 -ny 48 -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type lower -user_pc */
 36: /*                                                                             */
 37: /*   Same as above but with user defined PC for the true Schur                 */
 38: /*   complement. PC based on the SIMPLE-type approximation (inverse of         */
 39: /*   A00 approximated by inverse of its diagonal).                             */
 40: /*                                                                             */
 41: /* mpiexec -n 2 ./ex70 -nx 32 -ny 48 -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type lower -user_ksp */
 42: /*                                                                             */
 43: /*   Replace the true Schur complement with a user defined Schur               */
 44: /*   complement based on the SIMPLE-type approximation. Same matrix is         */
 45: /*   used as PC.                                                               */
 46: /*                                                                             */
 47: /* mpiexec -n 2 ./ex70 -nx 32 -ny 48 -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type lower -fieldsplit_0_ksp_type gmres -fieldsplit_0_pc_type bjacobi -fieldsplit_1_pc_type jacobi -fieldsplit_1_inner_ksp_type preonly -fieldsplit_1_inner_pc_type jacobi -fieldsplit_1_upper_ksp_type preonly -fieldsplit_1_upper_pc_type jacobi */
 48: /*                                                                             */
 49: /*   Out-of-the-box SIMPLE-type preconditioning. The major advantage           */
 50: /*   is that the user neither needs to provide the approximation of            */
 51: /*   the Schur complement, nor the corresponding preconditioner.               */
 52: /*                                                                             */
 53: /*---------------------------------------------------------------------------- */

 55:  #include <petscksp.h>

 57: typedef struct {
 58:   PetscBool userPC, userKSP; /* user defined preconditioner and matrix for the Schur complement */
 59:   PetscInt  nx, ny;  /* nb of cells in x- and y-direction */
 60:   PetscReal hx, hy;  /* mesh size in x- and y-direction */
 61:   Mat       A;       /* block matrix */
 62:   Mat       subA[4]; /* the four blocks */
 63:   Mat       myS;     /* the approximation of the Schur complement */
 64:   Vec       x, b, y; /* solution, rhs and temporary vector */
 65:   IS        isg[2];  /* index sets of split "0" and "1" */
 66: } Stokes;

 68: PetscErrorCode StokesSetupMatBlock00(Stokes*);  /* setup the block Q */
 69: PetscErrorCode StokesSetupMatBlock01(Stokes*);  /* setup the block G */
 70: PetscErrorCode StokesSetupMatBlock10(Stokes*);  /* setup the block D (equal to the transpose of G) */
 71: PetscErrorCode StokesSetupMatBlock11(Stokes*);  /* setup the block C (equal to zero) */

 73: PetscErrorCode StokesGetPosition(Stokes*, PetscInt, PetscInt*, PetscInt*); /* row number j*nx+i corresponds to position (i,j) in grid */

 75: PetscErrorCode StokesStencilLaplacian(Stokes*, PetscInt, PetscInt, PetscInt*, PetscInt*, PetscScalar*);  /* stencil of the Laplacian operator */
 76: PetscErrorCode StokesStencilGradientX(Stokes*, PetscInt, PetscInt, PetscInt*, PetscInt*, PetscScalar*);  /* stencil of the Gradient operator (x-component) */
 77: PetscErrorCode StokesStencilGradientY(Stokes*, PetscInt, PetscInt, PetscInt*, PetscInt*, PetscScalar*);  /* stencil of the Gradient operator (y-component) */

 79: PetscErrorCode StokesRhs(Stokes*);                                               /* rhs vector */
 80: PetscErrorCode StokesRhsMomX(Stokes*, PetscInt, PetscInt, PetscScalar*);   /* right hand side of velocity (x-component) */
 81: PetscErrorCode StokesRhsMomY(Stokes*, PetscInt, PetscInt, PetscScalar*);   /* right hand side of velocity (y-component) */
 82: PetscErrorCode StokesRhsMass(Stokes*, PetscInt, PetscInt, PetscScalar*);   /* right hand side of pressure */

 84: PetscErrorCode StokesSetupApproxSchur(Stokes*);  /* approximation of the Schur complement */

 86: PetscErrorCode StokesExactSolution(Stokes*); /* exact solution vector */
 87: PetscErrorCode StokesWriteSolution(Stokes*); /* write solution to file */

 89: /* exact solution for the velocity (x-component, y-component is zero) */
 90: PetscScalar StokesExactVelocityX(const PetscScalar y)
 91: {
 92:   return 4.0*y*(1.0-y);
 93: }

 95: /* exact solution for the pressure */
 96: PetscScalar StokesExactPressure(const PetscScalar x)
 97: {
 98:   return 8.0*(2.0-x);
 99: }

101: PetscErrorCode StokesSetupPC(Stokes *s, KSP ksp)
102: {
103:   KSP            *subksp;
104:   PC             pc;
105:   PetscInt       n = 1;

109:   KSPGetPC(ksp, &pc);
110:   PCFieldSplitSetIS(pc, "0", s->isg[0]);
111:   PCFieldSplitSetIS(pc, "1", s->isg[1]);
112:   if (s->userPC) {
113:     PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_USER, s->myS);
114:   }
115:   if (s->userKSP) {
116:     PCSetUp(pc);
117:     PCFieldSplitGetSubKSP(pc, &n, &subksp);
118:     KSPSetOperators(subksp[1], s->myS, s->myS);
119:     PetscFree(subksp);
120:   }
121:   return(0);
122: }

124: PetscErrorCode StokesWriteSolution(Stokes *s)
125: {
126:   PetscMPIInt       size;
127:   PetscInt          n,i,j;
128:   const PetscScalar *array;
129:   PetscErrorCode    ierr;

132:   /* write data (*warning* only works sequential) */
133:   MPI_Comm_size(MPI_COMM_WORLD,&size);
134:   /*PetscPrintf(PETSC_COMM_WORLD," number of processors = %D\n",size);*/
135:   if (size == 1) {
136:     PetscViewer viewer;
137:     VecGetArrayRead(s->x, &array);
138:     PetscViewerASCIIOpen(PETSC_COMM_WORLD, "solution.dat", &viewer);
139:     PetscViewerASCIIPrintf(viewer, "# x, y, u, v, p\n");
140:     for (j = 0; j < s->ny; j++) {
141:       for (i = 0; i < s->nx; i++) {
142:         n    = j*s->nx+i;
143:         PetscViewerASCIIPrintf(viewer, "%.12g %.12g %.12g %.12g %.12g\n", (double)(i*s->hx+s->hx/2),(double)(j*s->hy+s->hy/2), (double)PetscRealPart(array[n]), (double)PetscRealPart(array[n+s->nx*s->ny]),(double)PetscRealPart(array[n+2*s->nx*s->ny]));
144:       }
145:     }
146:     VecRestoreArrayRead(s->x, &array);
147:     PetscViewerDestroy(&viewer);
148:   }
149:   return(0);
150: }

152: PetscErrorCode StokesSetupIndexSets(Stokes *s)
153: {

157:   /* the two index sets */
158:   MatNestGetISs(s->A, s->isg, NULL);
159:   /*  ISView(isg[0],PETSC_VIEWER_STDOUT_WORLD); */
160:   /*  ISView(isg[1],PETSC_VIEWER_STDOUT_WORLD); */
161:   return(0);
162: }

164: PetscErrorCode StokesSetupVectors(Stokes *s)
165: {

169:   /* solution vector x */
170:   VecCreate(PETSC_COMM_WORLD, &s->x);
171:   VecSetSizes(s->x, PETSC_DECIDE, 3*s->nx*s->ny);
172:   VecSetType(s->x, VECMPI);
173:   /*  VecSetRandom(s->x, NULL); */
174:   /*  VecView(s->x, (PetscViewer) PETSC_VIEWER_DEFAULT); */

176:   /* exact solution y */
177:   VecDuplicate(s->x, &s->y);
178:   StokesExactSolution(s);
179:   /*  VecView(s->y, (PetscViewer) PETSC_VIEWER_DEFAULT); */

181:   /* rhs vector b */
182:   VecDuplicate(s->x, &s->b);
183:   StokesRhs(s);
184:   /*VecView(s->b, (PetscViewer) PETSC_VIEWER_DEFAULT);*/
185:   return(0);
186: }

188: PetscErrorCode StokesGetPosition(Stokes *s, PetscInt row, PetscInt *i, PetscInt *j)
189: {
190:   PetscInt n;

193:   /* cell number n=j*nx+i has position (i,j) in grid */
194:   n  = row%(s->nx*s->ny);
195:   *i = n%s->nx;
196:   *j = (n-(*i))/s->nx;
197:   return(0);
198: }

200: PetscErrorCode StokesExactSolution(Stokes *s)
201: {
202:   PetscInt       row, start, end, i, j;
203:   PetscScalar    val;
204:   Vec            y0,y1;

208:   /* velocity part */
209:   VecGetSubVector(s->y, s->isg[0], &y0);
210:   VecGetOwnershipRange(y0, &start, &end);
211:   for (row = start; row < end; row++) {
212:     StokesGetPosition(s, row,&i,&j);
213:     if (row < s->nx*s->ny) {
214:       val = StokesExactVelocityX(j*s->hy+s->hy/2);
215:     } else {
216:       val = 0;
217:     }
218:     VecSetValue(y0, row, val, INSERT_VALUES);
219:   }
220:   VecRestoreSubVector(s->y, s->isg[0], &y0);

222:   /* pressure part */
223:   VecGetSubVector(s->y, s->isg[1], &y1);
224:   VecGetOwnershipRange(y1, &start, &end);
225:   for (row = start; row < end; row++) {
226:     StokesGetPosition(s, row, &i, &j);
227:     val  = StokesExactPressure(i*s->hx+s->hx/2);
228:     VecSetValue(y1, row, val, INSERT_VALUES);
229:   }
230:   VecRestoreSubVector(s->y, s->isg[1], &y1);
231:   return(0);
232: }

234: PetscErrorCode StokesRhs(Stokes *s)
235: {
236:   PetscInt       row, start, end, i, j;
237:   PetscScalar    val;
238:   Vec            b0,b1;

242:   /* velocity part */
243:   VecGetSubVector(s->b, s->isg[0], &b0);
244:   VecGetOwnershipRange(b0, &start, &end);
245:   for (row = start; row < end; row++) {
246:     StokesGetPosition(s, row, &i, &j);
247:     if (row < s->nx*s->ny) {
248:       StokesRhsMomX(s, i, j, &val);
249:     } else {
250:       StokesRhsMomY(s, i, j, &val);
251:     }
252:     VecSetValue(b0, row, val, INSERT_VALUES);
253:   }
254:   VecRestoreSubVector(s->b, s->isg[0], &b0);

256:   /* pressure part */
257:   VecGetSubVector(s->b, s->isg[1], &b1);
258:   VecGetOwnershipRange(b1, &start, &end);
259:   for (row = start; row < end; row++) {
260:     StokesGetPosition(s, row, &i, &j);
261:     StokesRhsMass(s, i, j, &val);
262:     VecSetValue(b1, row, val, INSERT_VALUES);
263:   }
264:   VecRestoreSubVector(s->b, s->isg[1], &b1);
265:   return(0);
266: }

268: PetscErrorCode StokesSetupMatBlock00(Stokes *s)
269: {
270:   PetscInt       row, start, end, sz, i, j;
271:   PetscInt       cols[5];
272:   PetscScalar    vals[5];

276:   /* A[0] is 2N-by-2N */
277:   MatCreate(PETSC_COMM_WORLD,&s->subA[0]);
278:   MatSetOptionsPrefix(s->subA[0],"a00_");
279:   MatSetSizes(s->subA[0],PETSC_DECIDE,PETSC_DECIDE,2*s->nx*s->ny,2*s->nx*s->ny);
280:   MatSetType(s->subA[0],MATMPIAIJ);
281:   MatMPIAIJSetPreallocation(s->subA[0],5,NULL,5,NULL);
282:   MatGetOwnershipRange(s->subA[0], &start, &end);

284:   for (row = start; row < end; row++) {
285:     StokesGetPosition(s, row, &i, &j);
286:     /* first part: rows 0 to (nx*ny-1) */
287:     StokesStencilLaplacian(s, i, j, &sz, cols, vals);
288:     /* second part: rows (nx*ny) to (2*nx*ny-1) */
289:     if (row >= s->nx*s->ny) {
290:       for (i = 0; i < sz; i++) cols[i] += s->nx*s->ny;
291:     }
292:     for (i = 0; i < sz; i++) vals[i] = -1.0*vals[i]; /* dynamic viscosity coef mu=-1 */
293:     MatSetValues(s->subA[0], 1, &row, sz, cols, vals, INSERT_VALUES);
294:   }
295:   MatAssemblyBegin(s->subA[0], MAT_FINAL_ASSEMBLY);
296:   MatAssemblyEnd(s->subA[0], MAT_FINAL_ASSEMBLY);
297:   return(0);
298: }

300: PetscErrorCode StokesSetupMatBlock01(Stokes *s)
301: {
302:   PetscInt       row, start, end, sz, i, j;
303:   PetscInt       cols[5];
304:   PetscScalar    vals[5];

308:   /* A[1] is 2N-by-N */
309:   MatCreate(PETSC_COMM_WORLD, &s->subA[1]);
310:   MatSetOptionsPrefix(s->subA[1],"a01_");
311:   MatSetSizes(s->subA[1],PETSC_DECIDE,PETSC_DECIDE,2*s->nx*s->ny,s->nx*s->ny);
312:   MatSetType(s->subA[1],MATMPIAIJ);
313:   MatMPIAIJSetPreallocation(s->subA[1],5,NULL,5,NULL);
314:   MatGetOwnershipRange(s->subA[1],&start,&end);

316:   MatSetOption(s->subA[1],MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);

318:   for (row = start; row < end; row++) {
319:     StokesGetPosition(s, row, &i, &j);
320:     /* first part: rows 0 to (nx*ny-1) */
321:     if (row < s->nx*s->ny) {
322:       StokesStencilGradientX(s, i, j, &sz, cols, vals);
323:     } else {    /* second part: rows (nx*ny) to (2*nx*ny-1) */
324:       StokesStencilGradientY(s, i, j, &sz, cols, vals);
325:     }
326:     MatSetValues(s->subA[1], 1, &row, sz, cols, vals, INSERT_VALUES);
327:   }
328:   MatAssemblyBegin(s->subA[1], MAT_FINAL_ASSEMBLY);
329:   MatAssemblyEnd(s->subA[1], MAT_FINAL_ASSEMBLY);
330:   return(0);
331: }

333: PetscErrorCode StokesSetupMatBlock10(Stokes *s)
334: {

338:   /* A[2] is minus transpose of A[1] */
339:   MatTranspose(s->subA[1], MAT_INITIAL_MATRIX, &s->subA[2]);
340:   MatScale(s->subA[2], -1.0);
341:   MatSetOptionsPrefix(s->subA[2], "a10_");
342:   return(0);
343: }

345: PetscErrorCode StokesSetupMatBlock11(Stokes *s)
346: {

350:   /* A[3] is N-by-N null matrix */
351:   MatCreate(PETSC_COMM_WORLD, &s->subA[3]);
352:   MatSetOptionsPrefix(s->subA[3], "a11_");
353:   MatSetSizes(s->subA[3], PETSC_DECIDE, PETSC_DECIDE, s->nx*s->ny, s->nx*s->ny);
354:   MatSetType(s->subA[3], MATMPIAIJ);
355:   MatMPIAIJSetPreallocation(s->subA[3], 0, NULL, 0, NULL);
356:   MatAssemblyBegin(s->subA[3], MAT_FINAL_ASSEMBLY);
357:   MatAssemblyEnd(s->subA[3], MAT_FINAL_ASSEMBLY);
358:   return(0);
359: }

361: PetscErrorCode StokesSetupApproxSchur(Stokes *s)
362: {
363:   Vec            diag;

367:   /* Schur complement approximation: myS = A11 - A10 diag(A00)^(-1) A01 */
368:   /* note: A11 is zero */
369:   /* note: in real life this matrix would be build directly, */
370:   /* i.e. without MatMatMult */

372:   /* inverse of diagonal of A00 */
373:   VecCreate(PETSC_COMM_WORLD,&diag);
374:   VecSetSizes(diag,PETSC_DECIDE,2*s->nx*s->ny);
375:   VecSetType(diag,VECMPI);
376:   MatGetDiagonal(s->subA[0],diag);
377:   VecReciprocal(diag);

379:   /* compute: - A10 diag(A00)^(-1) A01 */
380:   MatDiagonalScale(s->subA[1],diag,NULL); /* (*warning* overwrites subA[1]) */
381:   MatMatMult(s->subA[2],s->subA[1],MAT_INITIAL_MATRIX,PETSC_DEFAULT,&s->myS);
382:   MatScale(s->myS,-1.0);

384:   /* restore A10 */
385:   MatGetDiagonal(s->subA[0],diag);
386:   MatDiagonalScale(s->subA[1],diag,NULL);
387:   VecDestroy(&diag);
388:   return(0);
389: }

391: PetscErrorCode StokesSetupMatrix(Stokes *s)
392: {

396:   StokesSetupMatBlock00(s);
397:   StokesSetupMatBlock01(s);
398:   StokesSetupMatBlock10(s);
399:   StokesSetupMatBlock11(s);
400:   MatCreateNest(PETSC_COMM_WORLD, 2, NULL, 2, NULL, s->subA, &s->A);
401:   StokesSetupApproxSchur(s);
402:   return(0);
403: }

405: PetscErrorCode StokesStencilLaplacian(Stokes *s, PetscInt i, PetscInt j, PetscInt *sz, PetscInt *cols, PetscScalar *vals)
406: {
407:   PetscInt    p =j*s->nx+i, w=p-1, e=p+1, s2=p-s->nx, n=p+s->nx;
408:   PetscScalar ae=s->hy/s->hx, aeb=0;
409:   PetscScalar aw=s->hy/s->hx, awb=s->hy/(s->hx/2);
410:   PetscScalar as=s->hx/s->hy, asb=s->hx/(s->hy/2);
411:   PetscScalar an=s->hx/s->hy, anb=s->hx/(s->hy/2);

414:   if (i==0 && j==0) { /* south-west corner */
415:     *sz  =3;
416:     cols[0]=p; vals[0]=-(ae+awb+asb+an);
417:     cols[1]=e; vals[1]=ae;
418:     cols[2]=n; vals[2]=an;
419:   } else if (i==0 && j==s->ny-1) { /* north-west corner */
420:     *sz  =3;
421:     cols[0]=s2; vals[0]=as;
422:     cols[1]=p; vals[1]=-(ae+awb+as+anb);
423:     cols[2]=e; vals[2]=ae;
424:   } else if (i==s->nx-1 && j==0) { /* south-east corner */
425:     *sz  =3;
426:     cols[0]=w; vals[0]=aw;
427:     cols[1]=p; vals[1]=-(aeb+aw+asb+an);
428:     cols[2]=n; vals[2]=an;
429:   } else if (i==s->nx-1 && j==s->ny-1) { /* north-east corner */
430:     *sz  =3;
431:     cols[0]=s2; vals[0]=as;
432:     cols[1]=w; vals[1]=aw;
433:     cols[2]=p; vals[2]=-(aeb+aw+as+anb);
434:   } else if (i==0) { /* west boundary */
435:     *sz  =4;
436:     cols[0]=s2; vals[0]=as;
437:     cols[1]=p; vals[1]=-(ae+awb+as+an);
438:     cols[2]=e; vals[2]=ae;
439:     cols[3]=n; vals[3]=an;
440:   } else if (i==s->nx-1) { /* east boundary */
441:     *sz  =4;
442:     cols[0]=s2; vals[0]=as;
443:     cols[1]=w; vals[1]=aw;
444:     cols[2]=p; vals[2]=-(aeb+aw+as+an);
445:     cols[3]=n; vals[3]=an;
446:   } else if (j==0) { /* south boundary */
447:     *sz  =4;
448:     cols[0]=w; vals[0]=aw;
449:     cols[1]=p; vals[1]=-(ae+aw+asb+an);
450:     cols[2]=e; vals[2]=ae;
451:     cols[3]=n; vals[3]=an;
452:   } else if (j==s->ny-1) { /* north boundary */
453:     *sz  =4;
454:     cols[0]=s2; vals[0]=as;
455:     cols[1]=w; vals[1]=aw;
456:     cols[2]=p; vals[2]=-(ae+aw+as+anb);
457:     cols[3]=e; vals[3]=ae;
458:   } else { /* interior */
459:     *sz  =5;
460:     cols[0]=s2; vals[0]=as;
461:     cols[1]=w; vals[1]=aw;
462:     cols[2]=p; vals[2]=-(ae+aw+as+an);
463:     cols[3]=e; vals[3]=ae;
464:     cols[4]=n; vals[4]=an;
465:   }
466:   return(0);
467: }

469: PetscErrorCode StokesStencilGradientX(Stokes *s, PetscInt i, PetscInt j, PetscInt *sz, PetscInt *cols, PetscScalar *vals)
470: {
471:   PetscInt    p =j*s->nx+i, w=p-1, e=p+1;
472:   PetscScalar ae= s->hy/2, aeb=s->hy;
473:   PetscScalar aw=-s->hy/2, awb=0;

476:   if (i==0 && j==0) { /* south-west corner */
477:     *sz  =2;
478:     cols[0]=p; vals[0]=-(ae+awb);
479:     cols[1]=e; vals[1]=ae;
480:   } else if (i==0 && j==s->ny-1) { /* north-west corner */
481:     *sz  =2;
482:     cols[0]=p; vals[0]=-(ae+awb);
483:     cols[1]=e; vals[1]=ae;
484:   } else if (i==s->nx-1 && j==0) { /* south-east corner */
485:     *sz  =2;
486:     cols[0]=w; vals[0]=aw;
487:     cols[1]=p; vals[1]=-(aeb+aw);
488:   } else if (i==s->nx-1 && j==s->ny-1) { /* north-east corner */
489:     *sz  =2;
490:     cols[0]=w; vals[0]=aw;
491:     cols[1]=p; vals[1]=-(aeb+aw);
492:   } else if (i==0) { /* west boundary */
493:     *sz  =2;
494:     cols[0]=p; vals[0]=-(ae+awb);
495:     cols[1]=e; vals[1]=ae;
496:   } else if (i==s->nx-1) { /* east boundary */
497:     *sz  =2;
498:     cols[0]=w; vals[0]=aw;
499:     cols[1]=p; vals[1]=-(aeb+aw);
500:   } else if (j==0) { /* south boundary */
501:     *sz  =3;
502:     cols[0]=w; vals[0]=aw;
503:     cols[1]=p; vals[1]=-(ae+aw);
504:     cols[2]=e; vals[2]=ae;
505:   } else if (j==s->ny-1) { /* north boundary */
506:     *sz  =3;
507:     cols[0]=w; vals[0]=aw;
508:     cols[1]=p; vals[1]=-(ae+aw);
509:     cols[2]=e; vals[2]=ae;
510:   } else { /* interior */
511:     *sz  =3;
512:     cols[0]=w; vals[0]=aw;
513:     cols[1]=p; vals[1]=-(ae+aw);
514:     cols[2]=e; vals[2]=ae;
515:   }
516:   return(0);
517: }

519: PetscErrorCode StokesStencilGradientY(Stokes *s, PetscInt i, PetscInt j, PetscInt *sz, PetscInt *cols, PetscScalar *vals)
520: {
521:   PetscInt    p =j*s->nx+i, s2=p-s->nx, n=p+s->nx;
522:   PetscScalar as=-s->hx/2, asb=0;
523:   PetscScalar an= s->hx/2, anb=0;

526:   if (i==0 && j==0) { /* south-west corner */
527:     *sz  =2;
528:     cols[0]=p; vals[0]=-(asb+an);
529:     cols[1]=n; vals[1]=an;
530:   } else if (i==0 && j==s->ny-1) { /* north-west corner */
531:     *sz  =2;
532:     cols[0]=s2; vals[0]=as;
533:     cols[1]=p; vals[1]=-(as+anb);
534:   } else if (i==s->nx-1 && j==0) { /* south-east corner */
535:     *sz  =2;
536:     cols[0]=p; vals[0]=-(asb+an);
537:     cols[1]=n; vals[1]=an;
538:   } else if (i==s->nx-1 && j==s->ny-1) { /* north-east corner */
539:     *sz  =2;
540:     cols[0]=s2; vals[0]=as;
541:     cols[1]=p; vals[1]=-(as+anb);
542:   } else if (i==0) { /* west boundary */
543:     *sz  =3;
544:     cols[0]=s2; vals[0]=as;
545:     cols[1]=p; vals[1]=-(as+an);
546:     cols[2]=n; vals[2]=an;
547:   } else if (i==s->nx-1) { /* east boundary */
548:     *sz  =3;
549:     cols[0]=s2; vals[0]=as;
550:     cols[1]=p; vals[1]=-(as+an);
551:     cols[2]=n; vals[2]=an;
552:   } else if (j==0) { /* south boundary */
553:     *sz  =2;
554:     cols[0]=p; vals[0]=-(asb+an);
555:     cols[1]=n; vals[1]=an;
556:   } else if (j==s->ny-1) { /* north boundary */
557:     *sz  =2;
558:     cols[0]=s2; vals[0]=as;
559:     cols[1]=p; vals[1]=-(as+anb);
560:   } else { /* interior */
561:     *sz  =3;
562:     cols[0]=s2; vals[0]=as;
563:     cols[1]=p; vals[1]=-(as+an);
564:     cols[2]=n; vals[2]=an;
565:   }
566:   return(0);
567: }

569: PetscErrorCode StokesRhsMomX(Stokes *s, PetscInt i, PetscInt j, PetscScalar *val)
570: {
571:   PetscScalar y   = j*s->hy+s->hy/2;
572:   PetscScalar awb = s->hy/(s->hx/2);

575:   if (i == 0) { /* west boundary */
576:     *val = awb*StokesExactVelocityX(y);
577:   } else {
578:     *val = 0.0;
579:   }
580:   return(0);
581: }

583: PetscErrorCode StokesRhsMomY(Stokes *s, PetscInt i, PetscInt j, PetscScalar *val)
584: {
586:   *val = 0.0;
587:   return(0);
588: }

590: PetscErrorCode StokesRhsMass(Stokes *s, PetscInt i, PetscInt j, PetscScalar *val)
591: {
592:   PetscScalar y   = j*s->hy+s->hy/2;
593:   PetscScalar aeb = s->hy;

596:   if (i == 0) { /* west boundary */
597:     *val = aeb*StokesExactVelocityX(y);
598:   } else {
599:     *val = 0.0;
600:   }
601:   return(0);
602: }

604: PetscErrorCode StokesCalcResidual(Stokes *s)
605: {
606:   PetscReal      val;
607:   Vec            b0, b1;

611:   /* residual Ax-b (*warning* overwrites b) */
612:   VecScale(s->b, -1.0);
613:   MatMultAdd(s->A, s->x, s->b, s->b);
614:   /*  VecView(s->b, (PetscViewer)PETSC_VIEWER_DEFAULT); */

616:   /* residual velocity */
617:   VecGetSubVector(s->b, s->isg[0], &b0);
618:   VecNorm(b0, NORM_2, &val);
619:   PetscPrintf(PETSC_COMM_WORLD," residual u = %g\n",(double)val);
620:   VecRestoreSubVector(s->b, s->isg[0], &b0);

622:   /* residual pressure */
623:   VecGetSubVector(s->b, s->isg[1], &b1);
624:   VecNorm(b1, NORM_2, &val);
625:   PetscPrintf(PETSC_COMM_WORLD," residual p = %g\n",(double)val);
626:   VecRestoreSubVector(s->b, s->isg[1], &b1);

628:   /* total residual */
629:   VecNorm(s->b, NORM_2, &val);
630:   PetscPrintf(PETSC_COMM_WORLD," residual [u,p] = %g\n", (double)val);
631:   return(0);
632: }

634: PetscErrorCode StokesCalcError(Stokes *s)
635: {
636:   PetscScalar    scale = PetscSqrtReal((double)s->nx*s->ny);
637:   PetscReal      val;
638:   Vec            y0, y1;

642:   /* error y-x */
643:   VecAXPY(s->y, -1.0, s->x);
644:   /* VecView(s->y, (PetscViewer)PETSC_VIEWER_DEFAULT); */

646:   /* error in velocity */
647:   VecGetSubVector(s->y, s->isg[0], &y0);
648:   VecNorm(y0, NORM_2, &val);
649:   PetscPrintf(PETSC_COMM_WORLD," discretization error u = %g\n",(double)(PetscRealPart(val/scale)));
650:   VecRestoreSubVector(s->y, s->isg[0], &y0);

652:   /* error in pressure */
653:   VecGetSubVector(s->y, s->isg[1], &y1);
654:   VecNorm(y1, NORM_2, &val);
655:   PetscPrintf(PETSC_COMM_WORLD," discretization error p = %g\n",(double)(PetscRealPart(val/scale)));
656:   VecRestoreSubVector(s->y, s->isg[1], &y1);

658:   /* total error */
659:   VecNorm(s->y, NORM_2, &val);
660:   PetscPrintf(PETSC_COMM_WORLD," discretization error [u,p] = %g\n", (double)PetscRealPart((val/scale)));
661:   return(0);
662: }

664: int main(int argc, char **argv)
665: {
666:   Stokes         s;
667:   KSP            ksp;

670:   PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
671:   s.nx     = 4;
672:   s.ny     = 6;
673:   PetscOptionsGetInt(NULL,NULL, "-nx", &s.nx, NULL);
674:   PetscOptionsGetInt(NULL,NULL, "-ny", &s.ny, NULL);
675:   s.hx     = 2.0/s.nx;
676:   s.hy     = 1.0/s.ny;
677:   s.userPC = s.userKSP = PETSC_FALSE;
678:   PetscOptionsHasName(NULL,NULL, "-user_pc", &s.userPC);
679:   PetscOptionsHasName(NULL,NULL, "-user_ksp", &s.userKSP);

681:   StokesSetupMatrix(&s);
682:   StokesSetupIndexSets(&s);
683:   StokesSetupVectors(&s);

685:   KSPCreate(PETSC_COMM_WORLD, &ksp);
686:   KSPSetOperators(ksp, s.A, s.A);
687:   KSPSetFromOptions(ksp);
688:   StokesSetupPC(&s, ksp);
689:   KSPSolve(ksp, s.b, s.x);

691:   /* don't trust, verify! */
692:   StokesCalcResidual(&s);
693:   StokesCalcError(&s);
694:   StokesWriteSolution(&s);

696:   KSPDestroy(&ksp);
697:   MatDestroy(&s.subA[0]);
698:   MatDestroy(&s.subA[1]);
699:   MatDestroy(&s.subA[2]);
700:   MatDestroy(&s.subA[3]);
701:   MatDestroy(&s.A);
702:   VecDestroy(&s.x);
703:   VecDestroy(&s.b);
704:   VecDestroy(&s.y);
705:   MatDestroy(&s.myS);
706:   PetscFinalize();
707:   return ierr;
708: }