Actual source code: ex5.c
2: static char help[] = "Tests the multigrid code. The input parameters are:\n\
3: -x N Use a mesh in the x direction of N. \n\
4: -c N Use N V-cycles. \n\
5: -l N Use N Levels. \n\
6: -smooths N Use N pre smooths and N post smooths. \n\
7: -j Use Jacobi smoother. \n\
8: -a use additive multigrid \n\
9: -f use full multigrid (preconditioner variant) \n\
10: This example also demonstrates matrix-free methods\n\n";
12: /*
13: This is not a good example to understand the use of multigrid with PETSc.
14: */
16: #include <petscksp.h>
18: PetscErrorCode residual(Mat,Vec,Vec,Vec);
19: PetscErrorCode gauss_seidel(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*);
20: PetscErrorCode jacobi_smoother(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*);
21: PetscErrorCode interpolate(Mat,Vec,Vec,Vec);
22: PetscErrorCode restrct(Mat,Vec,Vec);
23: PetscErrorCode Create1dLaplacian(PetscInt,Mat*);
24: PetscErrorCode CalculateRhs(Vec);
25: PetscErrorCode CalculateError(Vec,Vec,Vec,PetscReal*);
26: PetscErrorCode CalculateSolution(PetscInt,Vec*);
27: PetscErrorCode amult(Mat,Vec,Vec);
28: PetscErrorCode apply_pc(PC,Vec,Vec);
30: int main(int Argc,char **Args)
31: {
32: PetscInt x_mesh = 15,levels = 3,cycles = 1,use_jacobi = 0;
33: PetscInt i,smooths = 1,*N,its;
35: PCMGType am = PC_MG_MULTIPLICATIVE;
36: Mat cmat,mat[20],fmat;
37: KSP cksp,ksp[20],kspmg;
38: PetscReal e[3]; /* l_2 error,max error, residual */
39: const char *shellname;
40: Vec x,solution,X[20],R[20],B[20];
41: PC pcmg,pc;
42: PetscBool flg;
44: PetscInitialize(&Argc,&Args,(char*)0,help);if (ierr) return ierr;
45: PetscOptionsGetInt(NULL,NULL,"-x",&x_mesh,NULL);
46: PetscOptionsGetInt(NULL,NULL,"-l",&levels,NULL);
47: PetscOptionsGetInt(NULL,NULL,"-c",&cycles,NULL);
48: PetscOptionsGetInt(NULL,NULL,"-smooths",&smooths,NULL);
49: PetscOptionsHasName(NULL,NULL,"-a",&flg);
51: if (flg) am = PC_MG_ADDITIVE;
52: PetscOptionsHasName(NULL,NULL,"-f",&flg);
53: if (flg) am = PC_MG_FULL;
54: PetscOptionsHasName(NULL,NULL,"-j",&flg);
55: if (flg) use_jacobi = 1;
57: PetscMalloc1(levels,&N);
58: N[0] = x_mesh;
59: for (i=1; i<levels; i++) {
60: N[i] = N[i-1]/2;
61: if (N[i] < 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Too many levels or N is not large enough");
62: }
64: Create1dLaplacian(N[levels-1],&cmat);
66: KSPCreate(PETSC_COMM_WORLD,&kspmg);
67: KSPGetPC(kspmg,&pcmg);
68: KSPSetFromOptions(kspmg);
69: PCSetType(pcmg,PCMG);
70: PCMGSetLevels(pcmg,levels,NULL);
71: PCMGSetType(pcmg,am);
73: PCMGGetCoarseSolve(pcmg,&cksp);
74: KSPSetOperators(cksp,cmat,cmat);
75: KSPGetPC(cksp,&pc);
76: PCSetType(pc,PCLU);
77: KSPSetType(cksp,KSPPREONLY);
79: /* zero is finest level */
80: for (i=0; i<levels-1; i++) {
81: Mat dummy;
83: PCMGSetResidual(pcmg,levels - 1 - i,residual,NULL);
84: MatCreateShell(PETSC_COMM_WORLD,N[i+1],N[i],N[i+1],N[i],NULL,&mat[i]);
85: MatShellSetOperation(mat[i],MATOP_MULT,(void (*)(void))restrct);
86: MatShellSetOperation(mat[i],MATOP_MULT_TRANSPOSE_ADD,(void (*)(void))interpolate);
87: PCMGSetInterpolation(pcmg,levels - 1 - i,mat[i]);
88: PCMGSetRestriction(pcmg,levels - 1 - i,mat[i]);
89: PCMGSetCycleTypeOnLevel(pcmg,levels - 1 - i,(PCMGCycleType)cycles);
91: /* set smoother */
92: PCMGGetSmoother(pcmg,levels - 1 - i,&ksp[i]);
93: KSPGetPC(ksp[i],&pc);
94: PCSetType(pc,PCSHELL);
95: PCShellSetName(pc,"user_precond");
96: PCShellGetName(pc,&shellname);
97: PetscPrintf(PETSC_COMM_WORLD,"level=%D, PCShell name is %s\n",i,shellname);
99: /* this is not used unless different options are passed to the solver */
100: MatCreateShell(PETSC_COMM_WORLD,N[i],N[i],N[i],N[i],NULL,&dummy);
101: MatShellSetOperation(dummy,MATOP_MULT,(void (*)(void))amult);
102: KSPSetOperators(ksp[i],dummy,dummy);
103: MatDestroy(&dummy);
105: /*
106: We override the matrix passed in by forcing it to use Richardson with
107: a user provided application. This is non-standard and this practice
108: should be avoided.
109: */
110: PCShellSetApply(pc,apply_pc);
111: PCShellSetApplyRichardson(pc,gauss_seidel);
112: if (use_jacobi) {
113: PCShellSetApplyRichardson(pc,jacobi_smoother);
114: }
115: KSPSetType(ksp[i],KSPRICHARDSON);
116: KSPSetInitialGuessNonzero(ksp[i],PETSC_TRUE);
117: KSPSetTolerances(ksp[i],PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,smooths);
119: VecCreateSeq(PETSC_COMM_SELF,N[i],&x);
121: X[levels - 1 - i] = x;
122: if (i > 0) {
123: PCMGSetX(pcmg,levels - 1 - i,x);
124: }
125: VecCreateSeq(PETSC_COMM_SELF,N[i],&x);
127: B[levels -1 - i] = x;
128: if (i > 0) {
129: PCMGSetRhs(pcmg,levels - 1 - i,x);
130: }
131: VecCreateSeq(PETSC_COMM_SELF,N[i],&x);
133: R[levels - 1 - i] = x;
135: PCMGSetR(pcmg,levels - 1 - i,x);
136: }
137: /* create coarse level vectors */
138: VecCreateSeq(PETSC_COMM_SELF,N[levels-1],&x);
139: PCMGSetX(pcmg,0,x); X[0] = x;
140: VecCreateSeq(PETSC_COMM_SELF,N[levels-1],&x);
141: PCMGSetRhs(pcmg,0,x); B[0] = x;
143: /* create matrix multiply for finest level */
144: MatCreateShell(PETSC_COMM_WORLD,N[0],N[0],N[0],N[0],NULL,&fmat);
145: MatShellSetOperation(fmat,MATOP_MULT,(void (*)(void))amult);
146: KSPSetOperators(kspmg,fmat,fmat);
148: CalculateSolution(N[0],&solution);
149: CalculateRhs(B[levels-1]);
150: VecSet(X[levels-1],0.0);
152: residual((Mat)0,B[levels-1],X[levels-1],R[levels-1]);
153: CalculateError(solution,X[levels-1],R[levels-1],e);
154: PetscPrintf(PETSC_COMM_SELF,"l_2 error %g max error %g resi %g\n",(double)e[0],(double)e[1],(double)e[2]);
156: KSPSolve(kspmg,B[levels-1],X[levels-1]);
157: KSPGetIterationNumber(kspmg,&its);
158: residual((Mat)0,B[levels-1],X[levels-1],R[levels-1]);
159: CalculateError(solution,X[levels-1],R[levels-1],e);
160: PetscPrintf(PETSC_COMM_SELF,"its %D l_2 error %g max error %g resi %g\n",its,(double)e[0],(double)e[1],(double)e[2]);
162: PetscFree(N);
163: VecDestroy(&solution);
165: /* note we have to keep a list of all vectors allocated, this is
166: not ideal, but putting it in MGDestroy is not so good either*/
167: for (i=0; i<levels; i++) {
168: VecDestroy(&X[i]);
169: VecDestroy(&B[i]);
170: if (i) {VecDestroy(&R[i]);}
171: }
172: for (i=0; i<levels-1; i++) {
173: MatDestroy(&mat[i]);
174: }
175: MatDestroy(&cmat);
176: MatDestroy(&fmat);
177: KSPDestroy(&kspmg);
178: PetscFinalize();
179: return ierr;
180: }
182: /* --------------------------------------------------------------------- */
183: PetscErrorCode residual(Mat mat,Vec bb,Vec xx,Vec rr)
184: {
185: PetscInt i,n1;
186: PetscErrorCode ierr;
187: PetscScalar *x,*r;
188: const PetscScalar *b;
191: VecGetSize(bb,&n1);
192: VecGetArrayRead(bb,&b);
193: VecGetArray(xx,&x);
194: VecGetArray(rr,&r);
195: n1--;
196: r[0] = b[0] + x[1] - 2.0*x[0];
197: r[n1] = b[n1] + x[n1-1] - 2.0*x[n1];
198: for (i=1; i<n1; i++) r[i] = b[i] + x[i+1] + x[i-1] - 2.0*x[i];
199: VecRestoreArrayRead(bb,&b);
200: VecRestoreArray(xx,&x);
201: VecRestoreArray(rr,&r);
202: return(0);
203: }
205: PetscErrorCode amult(Mat mat,Vec xx,Vec yy)
206: {
207: PetscInt i,n1;
208: PetscErrorCode ierr;
209: PetscScalar *y;
210: const PetscScalar *x;
213: VecGetSize(xx,&n1);
214: VecGetArrayRead(xx,&x);
215: VecGetArray(yy,&y);
216: n1--;
217: y[0] = -x[1] + 2.0*x[0];
218: y[n1] = -x[n1-1] + 2.0*x[n1];
219: for (i=1; i<n1; i++) y[i] = -x[i+1] - x[i-1] + 2.0*x[i];
220: VecRestoreArrayRead(xx,&x);
221: VecRestoreArray(yy,&y);
222: return(0);
223: }
225: /* --------------------------------------------------------------------- */
226: PetscErrorCode apply_pc(PC pc,Vec bb,Vec xx)
227: {
229: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Not implemented");
230: }
232: PetscErrorCode gauss_seidel(PC pc,Vec bb,Vec xx,Vec w,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt m,PetscBool guesszero,PetscInt *its,PCRichardsonConvergedReason *reason)
233: {
234: PetscInt i,n1;
235: PetscErrorCode ierr;
236: PetscScalar *x;
237: const PetscScalar *b;
240: *its = m;
241: *reason = PCRICHARDSON_CONVERGED_ITS;
242: VecGetSize(bb,&n1); n1--;
243: VecGetArrayRead(bb,&b);
244: VecGetArray(xx,&x);
245: while (m--) {
246: x[0] = .5*(x[1] + b[0]);
247: for (i=1; i<n1; i++) x[i] = .5*(x[i+1] + x[i-1] + b[i]);
248: x[n1] = .5*(x[n1-1] + b[n1]);
249: for (i=n1-1; i>0; i--) x[i] = .5*(x[i+1] + x[i-1] + b[i]);
250: x[0] = .5*(x[1] + b[0]);
251: }
252: VecRestoreArrayRead(bb,&b);
253: VecRestoreArray(xx,&x);
254: return(0);
255: }
256: /* --------------------------------------------------------------------- */
257: PetscErrorCode jacobi_smoother(PC pc,Vec bb,Vec xx,Vec w,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt m,PetscBool guesszero,PetscInt *its,PCRichardsonConvergedReason *reason)
258: {
259: PetscInt i,n,n1;
260: PetscErrorCode ierr;
261: PetscScalar *r,*x;
262: const PetscScalar *b;
265: *its = m;
266: *reason = PCRICHARDSON_CONVERGED_ITS;
267: VecGetSize(bb,&n); n1 = n - 1;
268: VecGetArrayRead(bb,&b);
269: VecGetArray(xx,&x);
270: VecGetArray(w,&r);
272: while (m--) {
273: r[0] = .5*(x[1] + b[0]);
274: for (i=1; i<n1; i++) r[i] = .5*(x[i+1] + x[i-1] + b[i]);
275: r[n1] = .5*(x[n1-1] + b[n1]);
276: for (i=0; i<n; i++) x[i] = (2.0*r[i] + x[i])/3.0;
277: }
278: VecRestoreArrayRead(bb,&b);
279: VecRestoreArray(xx,&x);
280: VecRestoreArray(w,&r);
281: return(0);
282: }
283: /*
284: We know for this application that yy and zz are the same
285: */
286: /* --------------------------------------------------------------------- */
287: PetscErrorCode interpolate(Mat mat,Vec xx,Vec yy,Vec zz)
288: {
289: PetscInt i,n,N,i2;
290: PetscErrorCode ierr;
291: PetscScalar *y;
292: const PetscScalar *x;
295: VecGetSize(yy,&N);
296: VecGetArrayRead(xx,&x);
297: VecGetArray(yy,&y);
298: n = N/2;
299: for (i=0; i<n; i++) {
300: i2 = 2*i;
301: y[i2] += .5*x[i];
302: y[i2+1] += x[i];
303: y[i2+2] += .5*x[i];
304: }
305: VecRestoreArrayRead(xx,&x);
306: VecRestoreArray(yy,&y);
307: return(0);
308: }
309: /* --------------------------------------------------------------------- */
310: PetscErrorCode restrct(Mat mat,Vec rr,Vec bb)
311: {
312: PetscInt i,n,N,i2;
313: PetscErrorCode ierr;
314: PetscScalar *b;
315: const PetscScalar *r;
318: VecGetSize(rr,&N);
319: VecGetArrayRead(rr,&r);
320: VecGetArray(bb,&b);
321: n = N/2;
323: for (i=0; i<n; i++) {
324: i2 = 2*i;
325: b[i] = (r[i2] + 2.0*r[i2+1] + r[i2+2]);
326: }
327: VecRestoreArrayRead(rr,&r);
328: VecRestoreArray(bb,&b);
329: return(0);
330: }
331: /* --------------------------------------------------------------------- */
332: PetscErrorCode Create1dLaplacian(PetscInt n,Mat *mat)
333: {
334: PetscScalar mone = -1.0,two = 2.0;
335: PetscInt i,idx;
339: MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,NULL,mat);
341: idx = n-1;
342: MatSetValues(*mat,1,&idx,1,&idx,&two,INSERT_VALUES);
343: for (i=0; i<n-1; i++) {
344: MatSetValues(*mat,1,&i,1,&i,&two,INSERT_VALUES);
345: idx = i+1;
346: MatSetValues(*mat,1,&idx,1,&i,&mone,INSERT_VALUES);
347: MatSetValues(*mat,1,&i,1,&idx,&mone,INSERT_VALUES);
348: }
349: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
350: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
351: return(0);
352: }
353: /* --------------------------------------------------------------------- */
354: PetscErrorCode CalculateRhs(Vec u)
355: {
357: PetscInt i,n;
358: PetscReal h,x = 0.0;
359: PetscScalar uu;
362: VecGetSize(u,&n);
363: h = 1.0/((PetscReal)(n+1));
364: for (i=0; i<n; i++) {
365: x += h; uu = 2.0*h*h;
366: VecSetValues(u,1,&i,&uu,INSERT_VALUES);
367: }
368: return(0);
369: }
370: /* --------------------------------------------------------------------- */
371: PetscErrorCode CalculateSolution(PetscInt n,Vec *solution)
372: {
374: PetscInt i;
375: PetscReal h,x = 0.0;
376: PetscScalar uu;
379: VecCreateSeq(PETSC_COMM_SELF,n,solution);
380: h = 1.0/((PetscReal)(n+1));
381: for (i=0; i<n; i++) {
382: x += h; uu = x*(1.-x);
383: VecSetValues(*solution,1,&i,&uu,INSERT_VALUES);
384: }
385: return(0);
386: }
387: /* --------------------------------------------------------------------- */
388: PetscErrorCode CalculateError(Vec solution,Vec u,Vec r,PetscReal *e)
389: {
393: VecNorm(r,NORM_2,e+2);
394: VecWAXPY(r,-1.0,u,solution);
395: VecNorm(r,NORM_2,e);
396: VecNorm(r,NORM_1,e+1);
397: return(0);
398: }
400: /*TEST
402: test:
404: TEST*/