Date: 2022-03-31T04:48:20+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);
     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 0;
     83: }

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

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

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

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

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

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

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

    197: /*TEST

    199:    test:
    200:       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

    202: TEST*/