Actual source code: ex32.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  1: /*T
  2:    Concepts: KSP^solving a system of linear equations
  3:    Concepts: KSP^Laplacian, 2d
  4:    Processors: n
  5: T*/

  7: /*
  8: Laplacian in 2D. Modeled by the partial differential equation

 10:    div  grad u = f,  0 < x,y < 1,

 12: with forcing function

 14:    f = e^{-(1 - x)^2/\nu} e^{-(1 - y)^2/\nu}

 16: with pure Neumann boundary conditions

 18: The functions are cell-centered

 20: This uses multigrid to solve the linear system

 22:        Contributed by Andrei Draganescu <aidraga@sandia.gov>

 24: Note the nice multigrid convergence despite the fact it is only using
 25: peicewise constant interpolation/restriction. This is because cell-centered multigrid
 26: does not need the same rule:

 28:     polynomial degree(interpolation) + polynomial degree(restriction) + 2 > degree of PDE

 30: that vertex based multigrid needs.
 31: */

 33: static char help[] = "Solves 2D inhomogeneous Laplacian using multigrid.\n\n";

 35: #include <petscdm.h>
 36: #include <petscdmda.h>
 37: #include <petscksp.h>

 39: extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*);
 40: extern PetscErrorCode ComputeRHS(KSP,Vec,void*);

 42: typedef enum {DIRICHLET, NEUMANN} BCType;

 44: typedef struct {
 45:   PetscScalar nu;
 46:   BCType      bcType;
 47: } UserContext;

 51: int main(int argc,char **argv)
 52: {
 53:   KSP            ksp;
 54:   DM             da;
 55:   UserContext    user;
 56:   const char     *bcTypes[2] = {"dirichlet","neumann"};
 58:   PetscInt       bc;

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

 62:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 63:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,12,12,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
 64:   DMDASetInterpolationType(da, DMDA_Q0);

 66:   KSPSetDM(ksp,da);


 69:   PetscOptionsBegin(PETSC_COMM_WORLD, "", "Options for the inhomogeneous Poisson equation", "DM");
 70:   user.nu     = 0.1;
 71:   PetscOptionsScalar("-nu", "The width of the Gaussian source", "ex29.c", 0.1, &user.nu, NULL);
 72:   bc          = (PetscInt)NEUMANN;
 73:   PetscOptionsEList("-bc_type","Type of boundary condition","ex29.c",bcTypes,2,bcTypes[0],&bc,NULL);
 74:   user.bcType = (BCType)bc;
 75:   PetscOptionsEnd();

 77:   KSPSetComputeRHS(ksp,ComputeRHS,&user);
 78:   KSPSetComputeOperators(ksp,ComputeMatrix,&user);
 79:   KSPSetFromOptions(ksp);

 81:   KSPSolve(ksp,NULL,NULL);

 83:   KSPDestroy(&ksp);
 84:   DMDestroy(&da);
 85:   PetscFinalize();
 86:   return 0;
 87: }

 91: PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
 92: {
 93:   UserContext    *user = (UserContext*)ctx;
 95:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
 96:   PetscScalar    Hx,Hy;
 97:   PetscScalar    **array;
 98:   DM             da;

101:   KSPGetDM(ksp,&da);
102:   DMDAGetInfo(da, 0, &mx, &my, 0,0,0,0,0,0,0,0,0,0);
103:   Hx   = 1.0 / (PetscReal)(mx);
104:   Hy   = 1.0 / (PetscReal)(my);
105:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
106:   DMDAVecGetArray(da, b, &array);
107:   for (j=ys; j<ys+ym; j++) {
108:     for (i=xs; i<xs+xm; i++) {
109:       array[j][i] = PetscExpScalar(-(((PetscReal)i+0.5)*Hx)*(((PetscReal)i+0.5)*Hx)/user->nu)*PetscExpScalar(-(((PetscReal)j+0.5)*Hy)*(((PetscReal)j+0.5)*Hy)/user->nu)*Hx*Hy;
110:     }
111:   }
112:   DMDAVecRestoreArray(da, b, &array);
113:   VecAssemblyBegin(b);
114:   VecAssemblyEnd(b);

116:   /* force right hand side to be consistent for singular matrix */
117:   /* note this is really a hack, normally the model would provide you with a consistent right handside */
118:   if (user->bcType == NEUMANN) {
119:     MatNullSpace nullspace;

121:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
122:     MatNullSpaceRemove(nullspace,b);
123:     MatNullSpaceDestroy(&nullspace);
124:   }
125:   return(0);
126: }


131: PetscErrorCode ComputeMatrix(KSP ksp, Mat J,Mat jac, void *ctx)
132: {
133:   UserContext    *user = (UserContext*)ctx;
135:   PetscInt       i,j,mx,my,xm,ym,xs,ys,num, numi, numj;
136:   PetscScalar    v[5],Hx,Hy,HydHx,HxdHy;
137:   MatStencil     row, col[5];
138:   DM             da;

141:   KSPGetDM(ksp,&da);
142:   DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);
143:   Hx    = 1.0 / (PetscReal)(mx);
144:   Hy    = 1.0 / (PetscReal)(my);
145:   HxdHy = Hx/Hy;
146:   HydHx = Hy/Hx;
147:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
148:   for (j=ys; j<ys+ym; j++) {
149:     for (i=xs; i<xs+xm; i++) {
150:       row.i = i; row.j = j;
151:       if (i==0 || j==0 || i==mx-1 || j==my-1) {
152:         if (user->bcType == DIRICHLET) {
153:           v[0] = 2.0*(HxdHy + HydHx);
154:           MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
155:           SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Dirichlet boundary conditions not supported !\n");
156:         } else if (user->bcType == NEUMANN) {
157:           num = 0; numi=0; numj=0;
158:           if (j!=0) {
159:             v[num] = -HxdHy;
160:             col[num].i = i;
161:             col[num].j = j-1;
162:             num++; numj++;
163:           }
164:           if (i!=0) {
165:             v[num]     = -HydHx;
166:             col[num].i = i-1;
167:             col[num].j = j;
168:             num++; numi++;
169:           }
170:           if (i!=mx-1) {
171:             v[num]     = -HydHx;
172:             col[num].i = i+1;
173:             col[num].j = j;
174:             num++; numi++;
175:           }
176:           if (j!=my-1) {
177:             v[num]     = -HxdHy;
178:             col[num].i = i;
179:             col[num].j = j+1;
180:             num++; numj++;
181:           }
182:           v[num] = (PetscReal)(numj)*HxdHy + (PetscReal)(numi)*HydHx; col[num].i = i;   col[num].j = j;
183:           num++;
184:           MatSetValuesStencil(jac,1,&row,num,col,v,INSERT_VALUES);
185:         }
186:       } else {
187:         v[0] = -HxdHy;              col[0].i = i;   col[0].j = j-1;
188:         v[1] = -HydHx;              col[1].i = i-1; col[1].j = j;
189:         v[2] = 2.0*(HxdHy + HydHx); col[2].i = i;   col[2].j = j;
190:         v[3] = -HydHx;              col[3].i = i+1; col[3].j = j;
191:         v[4] = -HxdHy;              col[4].i = i;   col[4].j = j+1;
192:         MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
193:       }
194:     }
195:   }
196:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
197:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
198:   if (user->bcType == NEUMANN) {
199:     MatNullSpace nullspace;

201:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
202:     MatSetNullSpace(J,nullspace);
203:     MatNullSpaceDestroy(&nullspace);
204:   }
205:   return(0);
206: }