Actual source code: ex25.c
2: /*
3: Partial differential equation
5: d (1 + e*sine(2*pi*k*x)) d u = 1, 0 < x < 1,
6: -- ---
7: dx dx
8: with boundary conditions
10: u = 0 for x = 0, x = 1
12: This uses multigrid to solve the linear system
14: */
16: static char help[] = "Solves 1D variable coefficient Laplacian using multigrid.\n\n";
18: #include <petscdm.h>
19: #include <petscdmda.h>
20: #include <petscksp.h>
22: static PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*);
23: static PetscErrorCode ComputeRHS(KSP,Vec,void*);
25: typedef struct {
26: PetscInt k;
27: PetscScalar e;
28: } AppCtx;
30: int main(int argc,char **argv)
31: {
32: KSP ksp;
33: DM da;
34: AppCtx user;
35: Mat A;
36: Vec b,b2;
37: Vec x;
38: PetscReal nrm;
40: PetscInitialize(&argc,&argv,(char*)0,help);
41: user.k = 1;
42: user.e = .99;
43: PetscOptionsGetInt(NULL,0,"-k",&user.k,0);
44: PetscOptionsGetScalar(NULL,0,"-e",&user.e,0);
46: KSPCreate(PETSC_COMM_WORLD,&ksp);
47: DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,128,1,1,0,&da);
48: DMSetFromOptions(da);
49: DMSetUp(da);
50: KSPSetDM(ksp,da);
51: KSPSetComputeRHS(ksp,ComputeRHS,&user);
52: KSPSetComputeOperators(ksp,ComputeMatrix,&user);
53: KSPSetFromOptions(ksp);
54: KSPSolve(ksp,NULL,NULL);
56: KSPGetOperators(ksp,&A,NULL);
57: KSPGetSolution(ksp,&x);
58: KSPGetRhs(ksp,&b);
59: VecDuplicate(b,&b2);
60: MatMult(A,x,b2);
61: VecAXPY(b2,-1.0,b);
62: VecNorm(b2,NORM_MAX,&nrm);
63: PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g\n",(double)nrm);
65: VecDestroy(&b2);
66: KSPDestroy(&ksp);
67: DMDestroy(&da);
68: PetscFinalize();
69: return 0;
70: }
72: static PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
73: {
74: PetscInt mx,idx[2];
75: PetscScalar h,v[2];
76: DM da;
79: KSPGetDM(ksp,&da);
80: DMDAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0,0,0);
81: h = 1.0/((mx-1));
82: VecSet(b,h);
83: idx[0] = 0; idx[1] = mx -1;
84: v[0] = v[1] = 0.0;
85: VecSetValues(b,2,idx,v,INSERT_VALUES);
86: VecAssemblyBegin(b);
87: VecAssemblyEnd(b);
88: return 0;
89: }
91: static PetscErrorCode ComputeMatrix(KSP ksp,Mat J,Mat jac,void *ctx)
92: {
93: AppCtx *user = (AppCtx*)ctx;
94: PetscInt i,mx,xm,xs;
95: PetscScalar v[3],h,xlow,xhigh;
96: MatStencil row,col[3];
97: DM da;
100: KSPGetDM(ksp,&da);
101: DMDAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0,0,0);
102: DMDAGetCorners(da,&xs,0,0,&xm,0,0);
103: h = 1.0/(mx-1);
105: for (i=xs; i<xs+xm; i++) {
106: row.i = i;
107: if (i==0 || i==mx-1) {
108: v[0] = 2.0/h;
109: MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
110: } else {
111: xlow = h*(PetscReal)i - .5*h;
112: xhigh = xlow + h;
113: v[0] = (-1.0 - user->e*PetscSinScalar(2.0*PETSC_PI*user->k*xlow))/h;col[0].i = i-1;
114: v[1] = (2.0 + user->e*PetscSinScalar(2.0*PETSC_PI*user->k*xlow) + user->e*PetscSinScalar(2.0*PETSC_PI*user->k*xhigh))/h;col[1].i = row.i;
115: v[2] = (-1.0 - user->e*PetscSinScalar(2.0*PETSC_PI*user->k*xhigh))/h;col[2].i = i+1;
116: MatSetValuesStencil(jac,1,&row,3,col,v,INSERT_VALUES);
117: }
118: }
119: MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
120: MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
121: return 0;
122: }
124: /*TEST
126: test:
127: args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
128: requires: !single
130: test:
131: suffix: 2
132: nsize: 2
133: args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
134: requires: !single
136: TEST*/