Actual source code: ex29.c


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

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

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

 14:     with boundary conditions

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

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

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

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

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

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

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

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

 72:   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));
 73:   PetscCall(DMSetFromOptions(fine_ctx.da));
 74:   PetscCall(DMSetUp(fine_ctx.da));
 75:   PetscCall(DMCreateGlobalVector(fine_ctx.da, &fine_ctx.x));
 76:   PetscCall(VecDuplicate(fine_ctx.x, &fine_ctx.b));
 77:   PetscCall(VecGetLocalSize(fine_ctx.x, &nlocal));
 78:   PetscCall(DMCreateLocalVector(fine_ctx.da, &fine_ctx.localX));
 79:   PetscCall(VecDuplicate(fine_ctx.localX, &fine_ctx.localF));
 80:   PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, nlocal, nlocal, n, n, 5, NULL, 3, NULL, &A));
 81:   PetscCall(FormJacobian_Grid(&fine_ctx, &A));

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

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

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

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

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

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

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

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

174: /*TEST

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

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

186: TEST*/