Actual source code: ex45.c

petsc-3.4.5 2014-06-29
```  2: /*
3: Laplacian in 3D. Modeled by the partial differential equation

5:    - Laplacian u = 1,0 < x,y,z < 1,

7: with boundary conditions

9:    u = 1 for x = 0, x = 1, y = 0, y = 1, z = 0, z = 1.

11:    This uses multigrid to solve the linear system

13:    See src/snes/examples/tutorials/ex50.c

15:    Can also be run with -pc_type exotic -ksp_type fgmres

17: */

19: static char help[] = "Solves 3D Laplacian using multigrid.\n\n";

21: #include <petscksp.h>
22: #include <petscdmda.h>

24: extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,MatStructure*,void*);
25: extern PetscErrorCode ComputeRHS(KSP,Vec,void*);
26: extern PetscErrorCode ComputeInitialGuess(KSP,Vec,void*);

30: int main(int argc,char **argv)
31: {
33:   KSP            ksp;
34:   PetscReal      norm;
35:   DM             da;
36:   Vec            x,b,r;
37:   Mat            A;

39:   PetscInitialize(&argc,&argv,(char*)0,help);

41:   KSPCreate(PETSC_COMM_WORLD,&ksp);
42:   DMDACreate3d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-7,-7,-7,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,0,&da);
43:   KSPSetDM(ksp,da);
44:   KSPSetComputeInitialGuess(ksp,ComputeInitialGuess,NULL);
45:   KSPSetComputeRHS(ksp,ComputeRHS,NULL);
46:   KSPSetComputeOperators(ksp,ComputeMatrix,NULL);
47:   DMDestroy(&da);

49:   KSPSetFromOptions(ksp);
50:   KSPSolve(ksp,NULL,NULL);
51:   KSPGetSolution(ksp,&x);
52:   KSPGetRhs(ksp,&b);
53:   VecDuplicate(b,&r);
54:   KSPGetOperators(ksp,&A,NULL,NULL);

56:   MatMult(A,x,r);
57:   VecAXPY(r,-1.0,b);
58:   VecNorm(r,NORM_2,&norm);
59:   PetscPrintf(PETSC_COMM_WORLD,"Residual norm %G\n",norm);

61:   VecDestroy(&r);
62:   KSPDestroy(&ksp);
63:   PetscFinalize();

65:   return 0;
66: }

70: PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
71: {
73:   PetscInt       i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
74:   DM             dm;
75:   PetscScalar    Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
76:   PetscScalar    ***barray;

79:   KSPGetDM(ksp,&dm);
80:   DMDAGetInfo(dm,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);
81:   Hx      = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
82:   HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
83:   DMDAGetCorners(dm,&xs,&ys,&zs,&xm,&ym,&zm);
84:   DMDAVecGetArray(dm,b,&barray);

86:   for (k=zs; k<zs+zm; k++) {
87:     for (j=ys; j<ys+ym; j++) {
88:       for (i=xs; i<xs+xm; i++) {
89:         if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
90:           barray[k][j][i] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
91:         } else {
92:           barray[k][j][i] = Hx*Hy*Hz;
93:         }
94:       }
95:     }
96:   }
97:   DMDAVecRestoreArray(dm,b,&barray);
98:   return(0);
99: }

103: PetscErrorCode ComputeInitialGuess(KSP ksp,Vec b,void *ctx)
104: {

108:   VecSet(b,0);
109:   return(0);
110: }

114: PetscErrorCode ComputeMatrix(KSP ksp,Mat jac,Mat B,MatStructure *stflg,void *ctx)
115: {
116:   DM             da;
118:   PetscInt       i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
119:   PetscScalar    v[7],Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
120:   MatStencil     row,col[7];

123:   KSPGetDM(ksp,&da);
124:   DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);
125:   Hx      = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
126:   HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
127:   DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);

129:   for (k=zs; k<zs+zm; k++) {
130:     for (j=ys; j<ys+ym; j++) {
131:       for (i=xs; i<xs+xm; i++) {
132:         row.i = i; row.j = j; row.k = k;
133:         if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
134:           v[0] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
135:           MatSetValuesStencil(B,1,&row,1,&row,v,INSERT_VALUES);
136:         } else {
137:           v[0] = -HxHydHz;col[0].i = i; col[0].j = j; col[0].k = k-1;
138:           v[1] = -HxHzdHy;col[1].i = i; col[1].j = j-1; col[1].k = k;
139:           v[2] = -HyHzdHx;col[2].i = i-1; col[2].j = j; col[2].k = k;
140:           v[3] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);col[3].i = row.i; col[3].j = row.j; col[3].k = row.k;
141:           v[4] = -HyHzdHx;col[4].i = i+1; col[4].j = j; col[4].k = k;
142:           v[5] = -HxHzdHy;col[5].i = i; col[5].j = j+1; col[5].k = k;
143:           v[6] = -HxHydHz;col[6].i = i; col[6].j = j; col[6].k = k+1;
144:           MatSetValuesStencil(B,1,&row,7,col,v,INSERT_VALUES);
145:         }
146:       }
147:     }
148:   }
149:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
150:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
151:   *stflg = SAME_NONZERO_PATTERN;
152:   return(0);
153: }
```