Actual source code: ex19.c

petsc-3.13.4 2020-08-01
Report Typos and Errors

  2: static char help[] ="Solvers Laplacian with multigrid, bad way.\n\
  3:   -mx <xg>, where <xg> = number of grid points in the x-direction\n\
  4:   -my <yg>, where <yg> = number of grid points in the y-direction\n\
  5:   -Nx <npx>, where <npx> = number of processors in the x-direction\n\
  6:   -Ny <npy>, where <npy> = number of processors in the y-direction\n\n";

  8: /*
  9:     This problem is modeled by
 10:     the partial differential equation

 12:             -Laplacian u  = g,  0 < x,y < 1,

 14:     with boundary conditions

 16:              u = 0  for  x = 0, x = 1, y = 0, y = 1.

 18:     A finite difference approximation with the usual 5-point stencil
 19:     is used to discretize the boundary value problem to obtain a nonlinear
 20:     system of equations.
 21: */

 23:  #include <petscksp.h>
 24:  #include <petscdm.h>
 25:  #include <petscdmda.h>

 27: /* User-defined Section 1.5 Writing Application Codes with PETSc contexts */

 29: typedef struct {
 30:   PetscInt mx,my;               /* number grid points in x and y direction */
 31:   Vec      localX,localF;       /* local vectors with ghost region */
 32:   DM       da;
 33:   Vec      x,b,r;               /* global vectors */
 34:   Mat      J;                   /* Jacobian on grid */
 35: } GridCtx;

 37: typedef struct {
 38:   GridCtx  fine;
 39:   GridCtx  coarse;
 40:   KSP      ksp_coarse;
 41:   PetscInt ratio;
 42:   Mat      Ii;                  /* interpolation from coarse to fine */
 43: } AppCtx;

 45: #define COARSE_LEVEL 0
 46: #define FINE_LEVEL   1

 48: extern int FormJacobian_Grid(AppCtx*,GridCtx*,Mat*);

 50: /*
 51:       Mm_ratio - ration of grid lines between fine and coarse grids.
 52: */
 53: int main(int argc,char **argv)
 54: {
 55:   AppCtx         user;
 57:   PetscInt       its,N,n,Nx = PETSC_DECIDE,Ny = PETSC_DECIDE,nlocal,Nlocal;
 58:   PetscMPIInt    size;
 59:   KSP            ksp,ksp_fine;
 60:   PC             pc;
 61:   PetscScalar    one = 1.0;

 63:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
 64:   user.ratio     = 2;
 65:   user.coarse.mx = 5; user.coarse.my = 5;

 67:   PetscOptionsGetInt(NULL,NULL,"-Mx",&user.coarse.mx,NULL);
 68:   PetscOptionsGetInt(NULL,NULL,"-My",&user.coarse.my,NULL);
 69:   PetscOptionsGetInt(NULL,NULL,"-ratio",&user.ratio,NULL);

 71:   user.fine.mx = user.ratio*(user.coarse.mx-1)+1; user.fine.my = user.ratio*(user.coarse.my-1)+1;

 73:   PetscPrintf(PETSC_COMM_WORLD,"Coarse grid size %D by %D\n",user.coarse.mx,user.coarse.my);
 74:   PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",user.fine.mx,user.fine.my);

 76:   n = user.fine.mx*user.fine.my; N = user.coarse.mx*user.coarse.my;

 78:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 79:   PetscOptionsGetInt(NULL,NULL,"-Nx",&Nx,NULL);
 80:   PetscOptionsGetInt(NULL,NULL,"-Ny",&Ny,NULL);

 82:   /* Set up distributed array for fine grid */
 83:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,user.fine.mx,user.fine.my,Nx,Ny,1,1,NULL,NULL,&user.fine.da);
 84:   DMSetFromOptions(user.fine.da);
 85:   DMSetUp(user.fine.da);
 86:   DMCreateGlobalVector(user.fine.da,&user.fine.x);
 87:   VecDuplicate(user.fine.x,&user.fine.r);
 88:   VecDuplicate(user.fine.x,&user.fine.b);
 89:   VecGetLocalSize(user.fine.x,&nlocal);
 90:   DMCreateLocalVector(user.fine.da,&user.fine.localX);
 91:   VecDuplicate(user.fine.localX,&user.fine.localF);
 92:   MatCreateAIJ(PETSC_COMM_WORLD,nlocal,nlocal,n,n,5,NULL,3,NULL,&user.fine.J);

 94:   /* Set up distributed array for coarse grid */
 95:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,user.coarse.mx,user.coarse.my,Nx,Ny,1,1,NULL,NULL,&user.coarse.da);
 96:   DMSetFromOptions(user.coarse.da);
 97:   DMSetUp(user.coarse.da);
 98:   DMCreateGlobalVector(user.coarse.da,&user.coarse.x);
 99:   VecDuplicate(user.coarse.x,&user.coarse.b);
100:   VecGetLocalSize(user.coarse.x,&Nlocal);
101:   DMCreateLocalVector(user.coarse.da,&user.coarse.localX);
102:   VecDuplicate(user.coarse.localX,&user.coarse.localF);
103:   MatCreateAIJ(PETSC_COMM_WORLD,Nlocal,Nlocal,N,N,5,NULL,3,NULL,&user.coarse.J);

105:   /* Create linear solver */
106:   KSPCreate(PETSC_COMM_WORLD,&ksp);

108:   /* set two level additive Schwarz preconditioner */
109:   KSPGetPC(ksp,&pc);
110:   PCSetType(pc,PCMG);
111:   PCMGSetLevels(pc,2,NULL);
112:   PCMGSetType(pc,PC_MG_ADDITIVE);

114:   FormJacobian_Grid(&user,&user.coarse,&user.coarse.J);
115:   FormJacobian_Grid(&user,&user.fine,&user.fine.J);

117:   /* Create coarse level */
118:   PCMGGetCoarseSolve(pc,&user.ksp_coarse);
119:   KSPSetOptionsPrefix(user.ksp_coarse,"coarse_");
120:   KSPSetFromOptions(user.ksp_coarse);
121:   KSPSetOperators(user.ksp_coarse,user.coarse.J,user.coarse.J);
122:   PCMGSetX(pc,COARSE_LEVEL,user.coarse.x);
123:   PCMGSetRhs(pc,COARSE_LEVEL,user.coarse.b);

125:   /* Create fine level */
126:   PCMGGetSmoother(pc,FINE_LEVEL,&ksp_fine);
127:   KSPSetOptionsPrefix(ksp_fine,"fine_");
128:   KSPSetFromOptions(ksp_fine);
129:   KSPSetOperators(ksp_fine,user.fine.J,user.fine.J);
130:   PCMGSetR(pc,FINE_LEVEL,user.fine.r);

132:   /* Create interpolation between the levels */
133:   DMCreateInterpolation(user.coarse.da,user.fine.da,&user.Ii,NULL);
134:   PCMGSetInterpolation(pc,FINE_LEVEL,user.Ii);
135:   PCMGSetRestriction(pc,FINE_LEVEL,user.Ii);

137:   KSPSetOperators(ksp,user.fine.J,user.fine.J);

139:   VecSet(user.fine.b,one);

141:   /* Set options, then solve nonlinear system */
142:   KSPSetFromOptions(ksp);

144:   KSPSolve(ksp,user.fine.b,user.fine.x);
145:   KSPGetIterationNumber(ksp,&its);
146:   PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %D\n",its);

148:   /* Free data structures */
149:   MatDestroy(&user.fine.J);
150:   VecDestroy(&user.fine.x);
151:   VecDestroy(&user.fine.r);
152:   VecDestroy(&user.fine.b);
153:   DMDestroy(&user.fine.da);
154:   VecDestroy(&user.fine.localX);
155:   VecDestroy(&user.fine.localF);

157:   MatDestroy(&user.coarse.J);
158:   VecDestroy(&user.coarse.x);
159:   VecDestroy(&user.coarse.b);
160:   DMDestroy(&user.coarse.da);
161:   VecDestroy(&user.coarse.localX);
162:   VecDestroy(&user.coarse.localF);

164:   KSPDestroy(&ksp);
165:   MatDestroy(&user.Ii);
166:   PetscFinalize();
167:   return ierr;
168: }

170: int FormJacobian_Grid(AppCtx *user,GridCtx *grid,Mat *J)
171: {
172:   Mat                    jac = *J;
173:   PetscErrorCode         ierr;
174:   PetscInt               i,j,row,mx,my,xs,ys,xm,ym,Xs,Ys,Xm,Ym,col[5];
175:   PetscInt               grow;
176:   const PetscInt         *ltog;
177:   PetscScalar            two = 2.0,one = 1.0,v[5],hx,hy,hxdhy,hydhx,value;
178:   ISLocalToGlobalMapping ltogm;

180:   mx    = grid->mx;               my = grid->my;
181:   hx    = one/(PetscReal)(mx-1);  hy = one/(PetscReal)(my-1);
182:   hxdhy = hx/hy;               hydhx = hy/hx;

184:   /* Get ghost points */
185:   DMDAGetCorners(grid->da,&xs,&ys,0,&xm,&ym,0);
186:   DMDAGetGhostCorners(grid->da,&Xs,&Ys,0,&Xm,&Ym,0);
187:   DMGetLocalToGlobalMapping(grid->da,&ltogm);
188:   ISLocalToGlobalMappingGetIndices(ltogm,&ltog);

190:   /* Evaluate Jacobian of function */
191:   for (j=ys; j<ys+ym; j++) {
192:     row = (j - Ys)*Xm + xs - Xs - 1;
193:     for (i=xs; i<xs+xm; i++) {
194:       row++;
195:       grow = ltog[row];
196:       if (i > 0 && i < mx-1 && j > 0 && j < my-1) {
197:         v[0] = -hxdhy; col[0] = ltog[row - Xm];
198:         v[1] = -hydhx; col[1] = ltog[row - 1];
199:         v[2] = two*(hydhx + hxdhy); col[2] = grow;
200:         v[3] = -hydhx; col[3] = ltog[row + 1];
201:         v[4] = -hxdhy; col[4] = ltog[row + Xm];
202:         MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);
203:       } else if ((i > 0 && i < mx-1) || (j > 0 && j < my-1)) {
204:         value = .5*two*(hydhx + hxdhy);
205:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
206:       } else {
207:         value = .25*two*(hydhx + hxdhy);
208:         MatSetValues(jac,1,&grow,1,&grow,&value,INSERT_VALUES);
209:       }
210:     }
211:   }
212:   ISLocalToGlobalMappingRestoreIndices(ltogm,&ltog);
213:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
214:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);

216:   return 0;
217: }

219: /*TEST

221:     test:
222:       args: -ksp_gmres_cgs_refinement_type refine_always -pc_type jacobi -ksp_monitor_short -ksp_type gmres 

224:     test:
225:       suffix: 2
226:       nsize: 3
227:       args: -ksp_monitor_short

229: TEST*/