Date: 2022-03-31T04:48:20+00:00

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^{-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;

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

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

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

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

     81:   if (testsolver) {
     82:     KSPGetSolution(ksp,&x);
     83:     KSPGetRhs(ksp,&b);
     84:     KSPSetDMActive(ksp,PETSC_FALSE);
     85:     KSPSolve(ksp,b,x);
     86:     {
     87: #if defined(PETSC_USE_LOG)
     88:       PetscLogStage stage;
     89: #endif
     90:       PetscInt      i,n = 20;

     92:       PetscLogStageRegister("Solve only",&stage);
     93:       PetscLogStagePush(stage);
     94:       for (i=0; i<n; i++) {
     95:         KSPSolve(ksp,b,x);
     96:       }
     97:       PetscLogStagePop();
     98:     }
     99:   }

    101:   DMDestroy(&da);
    102:   KSPDestroy(&ksp);
    103:   PetscFinalize();
    104:   return 0;
    105: }

    107: PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
    108: {
    109:   UserContext    *user = (UserContext*)ctx;
    110:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
    111:   PetscScalar    Hx,Hy;
    112:   PetscScalar    **array;
    113:   DM             da;

    116:   KSPGetDM(ksp,&da);
    117:   DMDAGetInfo(da, 0, &mx, &my, 0,0,0,0,0,0,0,0,0,0);
    118:   Hx   = 1.0 / (PetscReal)(mx-1);
    119:   Hy   = 1.0 / (PetscReal)(my-1);
    120:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
    121:   DMDAVecGetArray(da, b, &array);
    122:   for (j=ys; j<ys+ym; j++) {
    123:     for (i=xs; i<xs+xm; i++) {
    124:       array[j][i] = PetscExpScalar(-((PetscReal)i*Hx)*((PetscReal)i*Hx)/user->nu)*PetscExpScalar(-((PetscReal)j*Hy)*((PetscReal)j*Hy)/user->nu)*Hx*Hy;
    125:     }
    126:   }
    127:   DMDAVecRestoreArray(da, b, &array);
    128:   VecAssemblyBegin(b);
    129:   VecAssemblyEnd(b);

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

    136:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    137:     MatNullSpaceRemove(nullspace,b);
    138:     MatNullSpaceDestroy(&nullspace);
    139:   }
    140:   return 0;
    141: }

    143: PetscErrorCode ComputeRho(PetscInt i, PetscInt j, PetscInt mx, PetscInt my, PetscReal centerRho, PetscReal *rho)
    144: {
    146:   if ((i > mx/3.0) && (i < 2.0*mx/3.0) && (j > my/3.0) && (j < 2.0*my/3.0)) {
    147:     *rho = centerRho;
    148:   } else {
    149:     *rho = 1.0;
    150:   }
    151:   return 0;
    152: }

    154: PetscErrorCode ComputeMatrix(KSP ksp,Mat J,Mat jac,void *ctx)
    155: {
    156:   UserContext    *user = (UserContext*)ctx;
    157:   PetscReal      centerRho;
    158:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
    159:   PetscScalar    v[5];
    160:   PetscReal      Hx,Hy,HydHx,HxdHy,rho;
    161:   MatStencil     row, col[5];
    162:   DM             da;
    163:   PetscBool      check_matis = PETSC_FALSE;

    166:   KSPGetDM(ksp,&da);
    167:   centerRho = user->rho;
    168:   DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);
    169:   Hx        = 1.0 / (PetscReal)(mx-1);
    170:   Hy        = 1.0 / (PetscReal)(my-1);
    171:   HxdHy     = Hx/Hy;
    172:   HydHx     = Hy/Hx;
    173:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
    174:   for (j=ys; j<ys+ym; j++) {
    175:     for (i=xs; i<xs+xm; i++) {
    176:       row.i = i; row.j = j;
    177:       ComputeRho(i, j, mx, my, centerRho, &rho);
    178:       if (i==0 || j==0 || i==mx-1 || j==my-1) {
    179:         if (user->bcType == DIRICHLET) {
    180:           v[0] = 2.0*rho*(HxdHy + HydHx);
    181:           MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
    182:         } else if (user->bcType == NEUMANN) {
    183:           PetscInt numx = 0, numy = 0, num = 0;
    184:           if (j!=0) {
    185:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j-1;
    186:             numy++; num++;
    187:           }
    188:           if (i!=0) {
    189:             v[num] = -rho*HydHx;              col[num].i = i-1; col[num].j = j;
    190:             numx++; num++;
    191:           }
    192:           if (i!=mx-1) {
    193:             v[num] = -rho*HydHx;              col[num].i = i+1; col[num].j = j;
    194:             numx++; num++;
    195:           }
    196:           if (j!=my-1) {
    197:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j+1;
    198:             numy++; num++;
    199:           }
    200:           v[num] = numx*rho*HydHx + numy*rho*HxdHy; col[num].i = i;   col[num].j = j;
    201:           num++;
    202:           MatSetValuesStencil(jac,1,&row,num,col,v,INSERT_VALUES);
    203:         }
    204:       } else {
    205:         v[0] = -rho*HxdHy;              col[0].i = i;   col[0].j = j-1;
    206:         v[1] = -rho*HydHx;              col[1].i = i-1; col[1].j = j;
    207:         v[2] = 2.0*rho*(HxdHy + HydHx); col[2].i = i;   col[2].j = j;
    208:         v[3] = -rho*HydHx;              col[3].i = i+1; col[3].j = j;
    209:         v[4] = -rho*HxdHy;              col[4].i = i;   col[4].j = j+1;
    210:         MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
    211:       }
    212:     }
    213:   }
    214:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
    215:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
    216:   MatViewFromOptions(jac,NULL,"-view_mat");
    217:   PetscOptionsGetBool(NULL,NULL,"-check_matis",&check_matis,NULL);
    218:   if (check_matis) {
    219:     void      (*f)(void);
    220:     Mat       J2;
    221:     MatType   jtype;
    222:     PetscReal nrm;

    224:     MatGetType(jac,&jtype);
    225:     MatConvert(jac,MATIS,MAT_INITIAL_MATRIX,&J2);
    226:     MatViewFromOptions(J2,NULL,"-view_conv");
    227:     MatConvert(J2,jtype,MAT_INPLACE_MATRIX,&J2);
    228:     MatGetOperation(jac,MATOP_VIEW,&f);
    229:     MatSetOperation(J2,MATOP_VIEW,f);
    230:     MatSetDM(J2,da);
    231:     MatViewFromOptions(J2,NULL,"-view_conv_assembled");
    232:     MatAXPY(J2,-1.,jac,DIFFERENT_NONZERO_PATTERN);
    233:     MatNorm(J2,NORM_FROBENIUS,&nrm);
    234:     PetscPrintf(PETSC_COMM_WORLD,"Error MATIS %g\n",(double)nrm);
    235:     MatViewFromOptions(J2,NULL,"-view_conv_err");
    236:     MatDestroy(&J2);
    237:   }
    238:   if (user->bcType == NEUMANN) {
    239:     MatNullSpace nullspace;

    241:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    242:     MatSetNullSpace(J,nullspace);
    243:     MatNullSpaceDestroy(&nullspace);
    244:   }
    245:   return 0;
    246: }

    248: /*TEST

    250:    test:
    251:       args: -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -da_refine 8 -ksp_rtol 1.e-3

    253:    test:
    254:       suffix: 2
    255:       args: -bc_type neumann -pc_type mg -pc_mg_type full -ksp_type fgmres -ksp_monitor_short -da_refine 8 -mg_coarse_pc_factor_shift_type nonzero
    256:       requires: !single

    258:    test:
    259:       suffix: telescope
    260:       nsize: 4
    261:       args: -ksp_monitor_short -da_grid_x 257 -da_grid_y 257 -pc_type mg -pc_mg_galerkin pmat -pc_mg_levels 4 -ksp_type richardson -mg_levels_ksp_type chebyshev -mg_levels_pc_type jacobi -mg_coarse_pc_type telescope -mg_coarse_pc_telescope_ignore_kspcomputeoperators -mg_coarse_telescope_pc_type mg -mg_coarse_telescope_pc_mg_galerkin pmat -mg_coarse_telescope_pc_mg_levels 3 -mg_coarse_telescope_mg_levels_ksp_type chebyshev -mg_coarse_telescope_mg_levels_pc_type jacobi -mg_coarse_pc_telescope_reduction_factor 4

    263:    test:
    264:       suffix: 3
    265:       args: -ksp_view -da_refine 2 -pc_type mg -pc_mg_distinct_smoothup -mg_levels_up_pc_type jacobi

    267:    test:
    268:       suffix: 4
    269:       args: -ksp_view -da_refine 2 -pc_type mg -pc_mg_distinct_smoothup -mg_levels_up_ksp_max_it 3 -mg_levels_ksp_max_it 4

    271:    test:
    272:       suffix: 5
    273:       nsize: 2
    274:       requires: hypre !complex
    275:       args: -pc_type mg  -da_refine 2 -ksp_monitor  -matptap_via hypre -pc_mg_galerkin both

    277:    test:
    278:       suffix: 6
    279:       args: -pc_type svd -pc_svd_monitor ::all

    281: TEST*/