Actual source code: ex29.c

  1: static char help[] = "Tests ML interface. Modified from ~src/ksp/ksp/tests/ex19.c \n\
  2:   -mx <xg>, where <xg> = number of grid points in the x-direction\n\
  3:   -my <yg>, where <yg> = number of grid points in the y-direction\n\
  4:   -Nx <npx>, where <npx> = number of processors in the x-direction\n\
  5:   -Ny <npy>, where <npy> = number of processors in the y-direction\n\n";

  7: /*
  8:     This problem is modeled by
  9:     the partial differential equation

 11:             -Laplacian u  = g,  0 < x,y < 1,

 13:     with boundary conditions

 15:              u = 0  for  x = 0, x = 1, y = 0, y = 1.

 17:     A finite difference approximation with the usual 5-point stencil
 18:     is used to discretize the boundary value problem to obtain a nonlinear
 19:     system of equations.

 21:     Usage: ./ex29 -ksp_type gmres -ksp_monitor
 22:            -pc_mg_type <multiplicative> (one of) additive multiplicative full kascade
 23:            -mg_coarse_ksp_max_it 10 -mg_levels_3_ksp_max_it 10 -mg_levels_2_ksp_max_it 10
 24:            -mg_levels_1_ksp_max_it 10 -mg_fine_ksp_max_it 10
 25: */

 27: #include <petscksp.h>
 28: #include <petscdm.h>
 29: #include <petscdmda.h>

 31: /* User-defined application contexts */
 32: typedef struct {
 33:   PetscInt mx, my;         /* number grid points in x and y direction */
 34:   Vec      localX, localF; /* local vectors with ghost region */
 35:   DM       da;
 36:   Vec      x, b, r; /* global vectors */
 37:   Mat      J;       /* Jacobian on grid */
 38:   Mat      A, P, R;
 39:   KSP      ksp;
 40: } GridCtx;
 41: extern PetscErrorCode FormJacobian_Grid(GridCtx *, Mat *);

 43: int main(int argc, char **argv)
 44: {
 45:   PetscInt    its, n, Nx = PETSC_DECIDE, Ny = PETSC_DECIDE, nlocal, i;
 46:   PetscMPIInt size;
 47:   PC          pc;
 48:   PetscInt    mx, my;
 49:   Mat         A;
 50:   GridCtx     fine_ctx;
 51:   KSP         ksp;
 52:   PetscBool   flg;

 54:   PetscFunctionBeginUser;
 55:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 56:   /* set up discretization matrix for fine grid */
 57:   /* ML requires input of fine-grid matrix. It determines nlevels. */
 58:   fine_ctx.mx = 9;
 59:   fine_ctx.my = 9;
 60:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mx", &mx, &flg));
 61:   if (flg) fine_ctx.mx = mx;
 62:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-my", &my, &flg));
 63:   if (flg) fine_ctx.my = my;
 64:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Fine grid size %" PetscInt_FMT " by %" PetscInt_FMT "\n", fine_ctx.mx, fine_ctx.my));
 65:   n = fine_ctx.mx * fine_ctx.my;

 67:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 68:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-Nx", &Nx, NULL));
 69:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-Ny", &Ny, NULL));

 71:   PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, fine_ctx.mx, fine_ctx.my, Nx, Ny, 1, 1, NULL, NULL, &fine_ctx.da));
 72:   PetscCall(DMSetFromOptions(fine_ctx.da));
 73:   PetscCall(DMSetUp(fine_ctx.da));
 74:   PetscCall(DMCreateGlobalVector(fine_ctx.da, &fine_ctx.x));
 75:   PetscCall(VecDuplicate(fine_ctx.x, &fine_ctx.b));
 76:   PetscCall(VecGetLocalSize(fine_ctx.x, &nlocal));
 77:   PetscCall(DMCreateLocalVector(fine_ctx.da, &fine_ctx.localX));
 78:   PetscCall(VecDuplicate(fine_ctx.localX, &fine_ctx.localF));
 79:   PetscCall(MatCreateFromOptions(PETSC_COMM_WORLD, NULL, 1, nlocal, nlocal, n, n, &A));
 80:   PetscCall(FormJacobian_Grid(&fine_ctx, &A));

 82:   /* create linear solver */
 83:   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
 84:   PetscCall(KSPGetPC(ksp, &pc));
 85:   PetscCall(PCSetType(pc, PCML));

 87:   /* set options, then solve system */
 88:   PetscCall(KSPSetFromOptions(ksp)); /* calls PCSetFromOptions_MG/ML */

 90:   for (i = 0; i < 3; i++) {
 91:     if (i < 2) {
 92:       /* set values for rhs vector */
 93:       PetscCall(VecSet(fine_ctx.b, i + 1.0));
 94:       /* modify A */
 95:       PetscCall(MatShift(A, 1.0));
 96:       PetscCall(MatScale(A, 2.0));
 97:       PetscCall(KSPSetOperators(ksp, A, A));
 98:     } else { /* test SAME_NONZERO_PATTERN */
 99:       PetscCall(KSPSetOperators(ksp, A, A));
100:     }
101:     PetscCall(KSPSolve(ksp, fine_ctx.b, fine_ctx.x));
102:     PetscCall(KSPGetIterationNumber(ksp, &its));
103:     if (its > 6) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Warning: Number of iterations = %" PetscInt_FMT " greater than expected\n", its));
104:   }

106:   /* free data structures */
107:   PetscCall(VecDestroy(&fine_ctx.x));
108:   PetscCall(VecDestroy(&fine_ctx.b));
109:   PetscCall(DMDestroy(&fine_ctx.da));
110:   PetscCall(VecDestroy(&fine_ctx.localX));
111:   PetscCall(VecDestroy(&fine_ctx.localF));
112:   PetscCall(MatDestroy(&A));
113:   PetscCall(KSPDestroy(&ksp));
114:   PetscCall(PetscFinalize());
115:   return 0;
116: }

118: PetscErrorCode FormJacobian_Grid(GridCtx *grid, Mat *J)
119: {
120:   Mat                    jac = *J;
121:   PetscInt               i, j, row, mx, my, xs, ys, xm, ym, Xs, Ys, Xm, Ym, col[5];
122:   PetscInt               grow;
123:   const PetscInt        *ltog;
124:   PetscScalar            two = 2.0, one = 1.0, v[5], hx, hy, hxdhy, hydhx, value;
125:   ISLocalToGlobalMapping ltogm;

127:   mx    = grid->mx;
128:   my    = grid->my;
129:   hx    = one / (PetscReal)(mx - 1);
130:   hy    = one / (PetscReal)(my - 1);
131:   hxdhy = hx / hy;
132:   hydhx = hy / hx;

134:   /* Get ghost points */
135:   PetscCall(DMDAGetCorners(grid->da, &xs, &ys, 0, &xm, &ym, 0));
136:   PetscCall(DMDAGetGhostCorners(grid->da, &Xs, &Ys, 0, &Xm, &Ym, 0));
137:   PetscCall(DMGetLocalToGlobalMapping(grid->da, &ltogm));
138:   PetscCall(ISLocalToGlobalMappingGetIndices(ltogm, &ltog));

140:   /* Evaluate Jacobian of function */
141:   for (j = ys; j < ys + ym; j++) {
142:     row = (j - Ys) * Xm + xs - Xs - 1;
143:     for (i = xs; i < xs + xm; i++) {
144:       row++;
145:       grow = ltog[row];
146:       if (i > 0 && i < mx - 1 && j > 0 && j < my - 1) {
147:         v[0]   = -hxdhy;
148:         col[0] = ltog[row - Xm];
149:         v[1]   = -hydhx;
150:         col[1] = ltog[row - 1];
151:         v[2]   = two * (hydhx + hxdhy);
152:         col[2] = grow;
153:         v[3]   = -hydhx;
154:         col[3] = ltog[row + 1];
155:         v[4]   = -hxdhy;
156:         col[4] = ltog[row + Xm];
157:         PetscCall(MatSetValues(jac, 1, &grow, 5, col, v, INSERT_VALUES));
158:       } else if ((i > 0 && i < mx - 1) || (j > 0 && j < my - 1)) {
159:         value = .5 * two * (hydhx + hxdhy);
160:         PetscCall(MatSetValues(jac, 1, &grow, 1, &grow, &value, INSERT_VALUES));
161:       } else {
162:         value = .25 * two * (hydhx + hxdhy);
163:         PetscCall(MatSetValues(jac, 1, &grow, 1, &grow, &value, INSERT_VALUES));
164:       }
165:     }
166:   }
167:   PetscCall(ISLocalToGlobalMappingRestoreIndices(ltogm, &ltog));
168:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
169:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
170:   return PETSC_SUCCESS;
171: }

173: /*TEST

175:     test:
176:       output_file: output/ex29.out
177:       args: -mat_no_inode
178:       requires: ml

180:     test:
181:       suffix: 2
182:       nsize: 3
183:       requires: ml

185: TEST*/