Actual source code: ex26.c
1: static char help[] ="Solvers Laplacian with multigrid, bad way.\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: /* Modified from ~src/ksp/examples/tests/ex19.c. Used for testing ML 3.0 interface.
9: This problem is modeled by
10: the partial differential equation
11:
12: -Laplacian u = g, 0 < x,y < 1,
13:
14: with boundary conditions
15:
16: u = 0 for x = 0, x = 1, y = 0, y = 1.
17:
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: ex26 -ksp_smonitor -pc_type ml
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 petscda.h
30: /* User-defined application contexts */
31: typedef struct {
32: PetscInt mx,my; /* number grid points in x and y direction */
33: Vec localX,localF; /* local vectors with ghost region */
34: DA da;
35: Vec x,b,r; /* global vectors */
36: Mat J; /* Jacobian on grid */
37: Mat A,P,R;
38: KSP ksp;
39: } GridCtx;
44: int main(int argc,char **argv)
45: {
47: PetscInt its,n,Nx=PETSC_DECIDE,Ny=PETSC_DECIDE,nlocal;
48: PetscMPIInt size;
49: PC pc;
50: PetscScalar one = 1.0;
51: PetscInt mx,my;
52: Mat A;
53: GridCtx fine_ctx;
54: KSP ksp;
55: PetscTruth flg;
57: PetscInitialize(&argc,&argv,(char *)0,help);
58: /* set up discretization matrix for fine grid */
59: fine_ctx.mx = 9; fine_ctx.my = 9;
60: PetscOptionsGetInt(PETSC_NULL,"-mx",&mx,&flg);
61: if (flg) fine_ctx.mx = mx;
62: PetscOptionsGetInt(PETSC_NULL,"-my",&my,&flg);
63: if (flg) fine_ctx.my = my;
64: PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",fine_ctx.mx,fine_ctx.my);
65: n = fine_ctx.mx*fine_ctx.my;
67: MPI_Comm_size(PETSC_COMM_WORLD,&size);
68: PetscOptionsGetInt(PETSC_NULL,"-Nx",&Nx,PETSC_NULL);
69: PetscOptionsGetInt(PETSC_NULL,"-Ny",&Ny,PETSC_NULL);
71: DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,fine_ctx.mx,
72: fine_ctx.my,Nx,Ny,1,1,PETSC_NULL,PETSC_NULL,&fine_ctx.da);
73: DACreateGlobalVector(fine_ctx.da,&fine_ctx.x);
74: VecDuplicate(fine_ctx.x,&fine_ctx.b);
75: VecGetLocalSize(fine_ctx.x,&nlocal);
76: DACreateLocalVector(fine_ctx.da,&fine_ctx.localX);
77: VecDuplicate(fine_ctx.localX,&fine_ctx.localF);
78: MatCreateMPIAIJ(PETSC_COMM_WORLD,nlocal,nlocal,n,n,5,PETSC_NULL,3,PETSC_NULL,&A);
79: FormJacobian_Grid(&fine_ctx,&A);
81: /* create linear solver */
82: KSPCreate(PETSC_COMM_WORLD,&ksp);
84: /* set values for rhs vector */
85: VecSet(fine_ctx.b,one);
86: {
87: PetscRandom rdm;
88: PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT,&rdm);
89: VecSetRandom(fine_ctx.b,rdm);
90: PetscRandomDestroy(rdm);
91: }
93: /* set options, then solve system */
94: KSPSetFromOptions(ksp); /* calls PCSetFromOptions_ML if 'pc_type=ml' */
95: KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
96: KSPSolve(ksp,fine_ctx.b,fine_ctx.x);
97: KSPGetIterationNumber(ksp,&its);
98: PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %D\n",its);
100: /* free data structures */
101: VecDestroy(fine_ctx.x);
102: VecDestroy(fine_ctx.b);
103: DADestroy(fine_ctx.da);
104: VecDestroy(fine_ctx.localX);
105: VecDestroy(fine_ctx.localF);
106: MatDestroy(A);
107: KSPDestroy(ksp);
109: PetscFinalize();
110: return 0;
111: }
115: int FormJacobian_Grid(GridCtx *grid,Mat *J)
116: {
117: Mat jac = *J;
119: PetscInt i,j,row,mx,my,xs,ys,xm,ym,Xs,Ys,Xm,Ym,col[5];
120: PetscInt nloc,*ltog,grow;
121: PetscScalar two = 2.0,one = 1.0,v[5],hx,hy,hxdhy,hydhx,value;
123: mx = grid->mx; my = grid->my;
124: hx = one/(PetscReal)(mx-1); hy = one/(PetscReal)(my-1);
125: hxdhy = hx/hy; hydhx = hy/hx;
127: /* Get ghost points */
128: DAGetCorners(grid->da,&xs,&ys,0,&xm,&ym,0);
129: DAGetGhostCorners(grid->da,&Xs,&Ys,0,&Xm,&Ym,0);
130: DAGetGlobalIndices(grid->da,&nloc,<og);
132: /* Evaluate Jacobian of function */
133: for (j=ys; j<ys+ym; j++) {
134: row = (j - Ys)*Xm + xs - Xs - 1;
135: for (i=xs; i<xs+xm; i++) {
136: row++;
137: grow = ltog[row];
138: if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
139: v[0] = -hxdhy; col[0] = ltog[row - Xm];
140: v[1] = -hydhx; col[1] = ltog[row - 1];
141: v[2] = two*(hydhx + hxdhy); col[2] = grow;
142: v[3] = -hydhx; col[3] = ltog[row + 1];
143: v[4] = -hxdhy; col[4] = ltog[row + Xm];
144: MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);
145: } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)){
146: value = .5*two*(hydhx + hxdhy);
147: MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
148: } else {
149: value = .25*two*(hydhx + hxdhy);
150: MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
151: }
152: }
153: }
154: MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
155: MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
156: return 0;
157: }