Actual source code: ex45.c

petsc-master 2016-12-04
Report Typos and Errors

  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 <petscdm.h>
 23:  #include <petscdmda.h>

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

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

 40:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 42:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 43:   DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,7,7,7,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,0,&da);
 44:   DMSetFromOptions(da);
 45:   DMSetUp(da);
 46:   KSPSetDM(ksp,da);
 47:   KSPSetComputeInitialGuess(ksp,ComputeInitialGuess,NULL);
 48:   KSPSetComputeRHS(ksp,ComputeRHS,NULL);
 49:   KSPSetComputeOperators(ksp,ComputeMatrix,NULL);
 50:   DMDestroy(&da);

 52:   KSPSetFromOptions(ksp);
 53:   KSPSolve(ksp,NULL,NULL);
 54:   KSPGetSolution(ksp,&x);
 55:   KSPGetRhs(ksp,&b);
 56:   VecDuplicate(b,&r);
 57:   KSPGetOperators(ksp,&A,NULL);

 59:   MatMult(A,x,r);
 60:   VecAXPY(r,-1.0,b);
 61:   VecNorm(r,NORM_2,&norm);
 62:   PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g\n",(double)norm);

 64:   VecDestroy(&r);
 65:   KSPDestroy(&ksp);
 66:   PetscFinalize();
 67:   return ierr;
 68: }

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

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

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

105: PetscErrorCode ComputeInitialGuess(KSP ksp,Vec b,void *ctx)
106: {

110:   VecSet(b,0);
111:   return(0);
112: }

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

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

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