Actual source code: ex29.c

petsc-master 2016-08-26
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: Added at the request of Marc Garbey.

 10: Inhomogeneous Laplacian in 2D. Modeled by the partial differential equation

 12:    -div \rho grad u = f,  0 < x,y < 1,

 14: with forcing function

 16:    f = e^{-x^2/\nu} e^{-y^2/\nu}

 18: with Dirichlet boundary conditions

 20:    u = f(x,y) for x = 0, x = 1, y = 0, y = 1

 22: or pure Neumman boundary conditions

 24: This uses multigrid to solve the linear system
 25: */

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

 29:  #include <petscdm.h>
 30:  #include <petscdmda.h>
 31:  #include <petscksp.h>

 33: extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*);
 34: extern PetscErrorCode ComputeRHS(KSP,Vec,void*);

 36: typedef enum {DIRICHLET, NEUMANN} BCType;

 38: typedef struct {
 39:   PetscReal rho;
 40:   PetscReal nu;
 41:   BCType    bcType;
 42: } UserContext;

 46: int main(int argc,char **argv)
 47: {
 48:   KSP            ksp;
 49:   DM             da;
 50:   UserContext    user;
 51:   const char     *bcTypes[2] = {"dirichlet","neumann"};
 53:   PetscInt       bc;
 54:   Vec            b,x;

 56:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 57:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 58:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,-3,-3,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
 59:   DMDASetUniformCoordinates(da,0,1,0,1,0,0);
 60:   DMDASetFieldName(da,0,"Pressure");

 62:   PetscOptionsBegin(PETSC_COMM_WORLD, "", "Options for the inhomogeneous Poisson equation", "DMqq");
 63:   user.rho    = 1.0;
 64:   PetscOptionsReal("-rho", "The conductivity", "ex29.c", user.rho, &user.rho, NULL);
 65:   user.nu     = 0.1;
 66:   PetscOptionsReal("-nu", "The width of the Gaussian source", "ex29.c", user.nu, &user.nu, NULL);
 67:   bc          = (PetscInt)DIRICHLET;
 68:   PetscOptionsEList("-bc_type","Type of boundary condition","ex29.c",bcTypes,2,bcTypes[0],&bc,NULL);
 69:   user.bcType = (BCType)bc;
 70:   PetscOptionsEnd();

 72:   KSPSetComputeRHS(ksp,ComputeRHS,&user);
 73:   KSPSetComputeOperators(ksp,ComputeMatrix,&user);
 74:   KSPSetDM(ksp,da);
 75:   KSPSetFromOptions(ksp);
 76:   KSPSetUp(ksp);
 77:   KSPSolve(ksp,NULL,NULL);
 78:   KSPGetSolution(ksp,&x);
 79:   KSPGetRhs(ksp,&b);

 81:   DMDestroy(&da);
 82:   KSPDestroy(&ksp);
 83:   PetscFinalize();
 84:   return ierr;
 85: }

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

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

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

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


129: PetscErrorCode ComputeRho(PetscInt i, PetscInt j, PetscInt mx, PetscInt my, PetscReal centerRho, PetscReal *rho)
130: {
132:   if ((i > mx/3.0) && (i < 2.0*mx/3.0) && (j > my/3.0) && (j < 2.0*my/3.0)) {
133:     *rho = centerRho;
134:   } else {
135:     *rho = 1.0;
136:   }
137:   return(0);
138: }

142: PetscErrorCode ComputeMatrix(KSP ksp,Mat J,Mat jac,void *ctx)
143: {
144:   UserContext    *user = (UserContext*)ctx;
145:   PetscReal      centerRho;
147:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
148:   PetscScalar    v[5];
149:   PetscReal      Hx,Hy,HydHx,HxdHy,rho;
150:   MatStencil     row, col[5];
151:   DM             da;

154:   KSPGetDM(ksp,&da);
155:   centerRho = user->rho;
156:   DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);
157:   Hx        = 1.0 / (PetscReal)(mx-1);
158:   Hy        = 1.0 / (PetscReal)(my-1);
159:   HxdHy     = Hx/Hy;
160:   HydHx     = Hy/Hx;
161:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
162:   for (j=ys; j<ys+ym; j++) {
163:     for (i=xs; i<xs+xm; i++) {
164:       row.i = i; row.j = j;
165:       ComputeRho(i, j, mx, my, centerRho, &rho);
166:       if (i==0 || j==0 || i==mx-1 || j==my-1) {
167:         if (user->bcType == DIRICHLET) {
168:           v[0] = 2.0*rho*(HxdHy + HydHx);
169:           MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
170:         } else if (user->bcType == NEUMANN) {
171:           PetscInt numx = 0, numy = 0, num = 0;
172:           if (j!=0) {
173:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j-1;
174:             numy++; num++;
175:           }
176:           if (i!=0) {
177:             v[num] = -rho*HydHx;              col[num].i = i-1; col[num].j = j;
178:             numx++; num++;
179:           }
180:           if (i!=mx-1) {
181:             v[num] = -rho*HydHx;              col[num].i = i+1; col[num].j = j;
182:             numx++; num++;
183:           }
184:           if (j!=my-1) {
185:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j+1;
186:             numy++; num++;
187:           }
188:           v[num] = numx*rho*HydHx + numy*rho*HxdHy; col[num].i = i;   col[num].j = j;
189:           num++;
190:           MatSetValuesStencil(jac,1,&row,num,col,v,INSERT_VALUES);
191:         }
192:       } else {
193:         v[0] = -rho*HxdHy;              col[0].i = i;   col[0].j = j-1;
194:         v[1] = -rho*HydHx;              col[1].i = i-1; col[1].j = j;
195:         v[2] = 2.0*rho*(HxdHy + HydHx); col[2].i = i;   col[2].j = j;
196:         v[3] = -rho*HydHx;              col[3].i = i+1; col[3].j = j;
197:         v[4] = -rho*HxdHy;              col[4].i = i;   col[4].j = j+1;
198:         MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
199:       }
200:     }
201:   }
202:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
203:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
204:   if (user->bcType == NEUMANN) {
205:     MatNullSpace nullspace;

207:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
208:     MatSetNullSpace(J,nullspace);
209:     MatNullSpaceDestroy(&nullspace);
210:   }
211:   return(0);
212: }