Date: 2022-03-04T17:39:13+00:00

Actual source code: ex32.c

      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;

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

     58:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     59:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     60:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,12,12,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
     61:   DMSetFromOptions(da);
     62:   DMSetUp(da);
     63:   DMDASetInterpolationType(da, DMDA_Q0);

     65:   KSPSetDM(ksp,da);

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

     75:   KSPSetComputeRHS(ksp,ComputeRHS,&user);
     76:   KSPSetComputeOperators(ksp,ComputeMatrix,&user);
     77:   KSPSetFromOptions(ksp);
     78:   KSPSolve(ksp,NULL,NULL);
     79:   KSPDestroy(&ksp);
     80:   DMDestroy(&da);
     81:   PetscFinalize();
     82:   return ierr;
     83: }

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

     95:   KSPGetDM(ksp,&da);
     96:   DMDAGetInfo(da, 0, &mx, &my, 0,0,0,0,0,0,0,0,0,0);
     97:   Hx   = 1.0 / (PetscReal)(mx);
     98:   Hy   = 1.0 / (PetscReal)(my);
     99:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
    100:   DMDAVecGetArray(da, b, &array);
    101:   for (j=ys; j<ys+ym; j++) {
    102:     for (i=xs; i<xs+xm; i++) {
    103:       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;
    104:     }
    105:   }
    106:   DMDAVecRestoreArray(da, b, &array);
    107:   VecAssemblyBegin(b);
    108:   VecAssemblyEnd(b);

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

    115:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    116:     MatNullSpaceRemove(nullspace,b);
    117:     MatNullSpaceDestroy(&nullspace);
    118:   }
    119:   return(0);
    120: }

    122: PetscErrorCode ComputeMatrix(KSP ksp, Mat J,Mat jac, void *ctx)
    123: {
    124:   UserContext    *user = (UserContext*)ctx;
    126:   PetscInt       i,j,mx,my,xm,ym,xs,ys,num, numi, numj;
    127:   PetscScalar    v[5],Hx,Hy,HydHx,HxdHy;
    128:   MatStencil     row, col[5];
    129:   DM             da;

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

    192:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    193:     MatSetNullSpace(J,nullspace);
    194:     MatNullSpaceDestroy(&nullspace);
    195:   }
    196:   return(0);
    197: }

    199: /*TEST

    201:    test:
    202:       args: -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -pc_mg_levels 3 -mg_coarse_pc_factor_shift_type nonzero

    204: TEST*/