Actual source code: ex29.c

  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^{-(1 - x)^2/\nu} e^{-(1 - 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 petscda.h
 30:  #include petscksp.h
 31:  #include petscmg.h
 32:  #include petscdmmg.h


 38: typedef enum {DIRICHLET, NEUMANN} BCType;

 40: typedef struct {
 41:   PetscScalar   nu;
 42:   BCType        bcType;
 43: } UserContext;

 47: int main(int argc,char **argv)
 48: {
 49:   DMMG           *dmmg;
 50:   DA             da;
 51:   UserContext    user;
 52:   PetscReal      norm;
 53:   const char     *bcTypes[2] = {"dirichlet","neumann"};
 55:   PetscInt       l,bc;

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

 59:   DMMGCreate(PETSC_COMM_WORLD,3,PETSC_NULL,&dmmg);
 60:   DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,-3,-3,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
 61:   DMMGSetDM(dmmg,(DM)da);
 62:   DADestroy(da);
 63:   for (l = 0; l < DMMGGetLevels(dmmg); l++) {
 64:     DMMGSetUser(dmmg,l,&user);
 65:   }

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

 75:   DMMGSetKSP(dmmg,ComputeRHS,ComputeJacobian);
 76:   if (user.bcType == NEUMANN) {
 77:     DMMGSetNullSpace(dmmg,PETSC_TRUE,0,PETSC_NULL);
 78:   }

 80:   DMMGSolve(dmmg);

 82:   MatMult(DMMGGetJ(dmmg),DMMGGetx(dmmg),DMMGGetr(dmmg));
 83:   VecAXPY(DMMGGetr(dmmg),-1.0,DMMGGetRHS(dmmg));
 84:   VecNorm(DMMGGetr(dmmg),NORM_2,&norm);
 85:   /* PetscPrintf(PETSC_COMM_WORLD,"Residual norm %G\n",norm); */
 86:   VecAssemblyBegin(DMMGGetx(dmmg));
 87:   VecAssemblyEnd(DMMGGetx(dmmg));
 88:   VecView_VTK(DMMGGetRHS(dmmg), "rhs.vtk", bcTypes[user.bcType]);
 89:   VecView_VTK(DMMGGetx(dmmg), "solution.vtk", bcTypes[user.bcType]);

 91:   DMMGDestroy(dmmg);
 92:   PetscFinalize();

 94:   return 0;
 95: }

 99: PetscErrorCode ComputeRHS(DMMG dmmg, Vec b)
100: {
101:   DA             da = (DA)dmmg->dm;
102:   UserContext    *user = (UserContext *) dmmg->user;
104:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
105:   PetscScalar    Hx,Hy;
106:   PetscScalar    **array;

109:   DAGetInfo(da, 0, &mx, &my, 0,0,0,0,0,0,0,0);
110:   Hx   = 1.0 / (PetscReal)(mx-1);
111:   Hy   = 1.0 / (PetscReal)(my-1);
112:   DAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
113:   DAVecGetArray(da, b, &array);
114:   for (j=ys; j<ys+ym; j++){
115:     for(i=xs; i<xs+xm; i++){
116:       array[j][i] = PetscExpScalar(-((PetscReal)i*Hx)*((PetscReal)i*Hx)/user->nu)*PetscExpScalar(-((PetscReal)j*Hy)*((PetscReal)j*Hy)/user->nu)*Hx*Hy;
117:     }
118:   }
119:   DAVecRestoreArray(da, b, &array);
120:   VecAssemblyBegin(b);
121:   VecAssemblyEnd(b);

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

128:     KSPGetNullSpace(dmmg->ksp,&nullspace);
129:     MatNullSpaceRemove(nullspace,b,PETSC_NULL);
130:   }
131:   return(0);
132: }

134: 
137: PetscErrorCode ComputeRho(PetscInt i, PetscInt j, PetscInt mx, PetscInt my, PetscScalar *rho)
138: {
140:   if ((i > mx/3.0) && (i < 2.0*mx/3.0) && (j > my/3.0) && (j < 2.0*my/3.0)) {
141:     *rho = 100.0;
142:   } else {
143:     *rho = 1.0;
144:   }
145:   return(0);
146: }

150: PetscErrorCode ComputeJacobian(DMMG dmmg, Mat J,Mat jac)
151: {
152:   DA             da = (DA) dmmg->dm;
153:   UserContext    *user = (UserContext *) dmmg->user;
155:   PetscInt       i,j,mx,my,xm,ym,xs,ys,num;
156:   PetscScalar    v[5],Hx,Hy,HydHx,HxdHy,rho;
157:   MatStencil     row, col[5];

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

213: PetscErrorCode VecView_VTK(Vec x, const char filename[], const char bcName[])
214: {
215:   MPI_Comm           comm;
216:   DA                 da;
217:   Vec                coords;
218:   PetscViewer        viewer;
219:   PetscScalar       *array, *values;
220:   PetscInt           n, N, maxn, mx, my, dof;
221:   PetscInt           i, p;
222:   MPI_Status         status;
223:   PetscMPIInt        rank, size, tag;
224:   PetscErrorCode     ierr;

227:   PetscObjectGetComm((PetscObject) x, &comm);
228:   PetscViewerASCIIOpen(comm, filename, &viewer);

230:   VecGetSize(x, &N);
231:   VecGetLocalSize(x, &n);
232:   PetscObjectQuery((PetscObject) x, "DA", (PetscObject *) &da);
233:   if (!da) SETERRQ(PETSC_ERR_ARG_WRONG,"Vector not generated from a DA");

235:   DAGetInfo(da, 0, &mx, &my, 0,0,0,0, &dof,0,0,0);

237:   PetscViewerASCIIPrintf(viewer, "# vtk DataFile Version 2.0\n");
238:   PetscViewerASCIIPrintf(viewer, "Inhomogeneous Poisson Equation with %s boundary conditions\n", bcName);
239:   PetscViewerASCIIPrintf(viewer, "ASCII\n");
240:   /* get coordinates of nodes */
241:   DAGetCoordinates(da, &coords);
242:   if (!coords) {
243:     DASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0);
244:     DAGetCoordinates(da, &coords);
245:   }
246:   PetscViewerASCIIPrintf(viewer, "DATASET RECTILINEAR_GRID\n");
247:   PetscViewerASCIIPrintf(viewer, "DIMENSIONS %d %d %d\n", mx, my, 1);
248:   VecGetArray(coords, &array);
249:   PetscViewerASCIIPrintf(viewer, "X_COORDINATES %d double\n", mx);
250:   for(i = 0; i < mx; i++) {
251:     PetscViewerASCIIPrintf(viewer, "%G ", PetscRealPart(array[i*2]));
252:   }
253:   PetscViewerASCIIPrintf(viewer, "\n");
254:   PetscViewerASCIIPrintf(viewer, "Y_COORDINATES %d double\n", my);
255:   for(i = 0; i < my; i++) {
256:     PetscViewerASCIIPrintf(viewer, "%G ", PetscRealPart(array[i*mx*2+1]));
257:   }
258:   PetscViewerASCIIPrintf(viewer, "\n");
259:   PetscViewerASCIIPrintf(viewer, "Z_COORDINATES %d double\n", 1);
260:   PetscViewerASCIIPrintf(viewer, "%G\n", 0.0);
261:   VecRestoreArray(coords, &array);
262:   PetscViewerASCIIPrintf(viewer, "POINT_DATA %d\n", N);
263:   PetscViewerASCIIPrintf(viewer, "SCALARS scalars double %d\n", dof);
264:   PetscViewerASCIIPrintf(viewer, "LOOKUP_TABLE default\n");
265:   VecGetArray(x, &array);
266:   /* Determine maximum message to arrive */
267:   MPI_Comm_rank(comm, &rank);
268:   MPI_Comm_size(comm, &size);
269:   MPI_Reduce(&n, &maxn, 1, MPIU_INT, MPI_MAX, 0, comm);
270:   tag  = ((PetscObject) viewer)->tag;
271:   if (!rank) {
272:     PetscMalloc((maxn+1) * sizeof(PetscScalar), &values);
273:     for(i = 0; i < n; i++) {
274:       PetscViewerASCIIPrintf(viewer, "%G\n", PetscRealPart(array[i]));
275:     }
276:     for(p = 1; p < size; p++) {
277:       MPI_Recv(values, (PetscMPIInt) n, MPIU_SCALAR, p, tag, comm, &status);
278:       MPI_Get_count(&status, MPIU_SCALAR, &n);
279:       for(i = 0; i < n; i++) {
280:         PetscViewerASCIIPrintf(viewer, "%G\n", PetscRealPart(array[i]));
281:       }
282:     }
283:     PetscFree(values);
284:   } else {
285:     MPI_Send(array, n, MPIU_SCALAR, 0, tag, comm);
286:   }
287:   VecRestoreArray(x, &array);
288:   PetscViewerFlush(viewer);
289:   PetscViewerDestroy(viewer);
290:   return(0);
291: }