Actual source code: ex56.c
petsc-3.12.4 2020-02-04
Report Typos and Errors

      2: /*
      3: Inhomogeneous Laplacian in 2D. Modeled by the partial differential equation

      5:    -div \rho grad u = f,  0 < x,y < 1,

      7: with forcing function

      9:    f = e^{-x^2/\nu} e^{-y^2/\nu}

     11: with Dirichlet boundary conditions

     13:    u = f(x,y) for x = 0, x = 1, y = 0, y = 1

     15: or pure Neumman boundary conditions

     17: This uses multigrid to solve the linear system
     18: */

     20: static char help[] = "Solves 2D inhomogeneous Laplacian using multigrid.\n\n";

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

     26: extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*);
     27: extern PetscErrorCode ComputeRHS(KSP,Vec,void*);

     29: typedef enum {DIRICHLET, NEUMANN} BCType;

     31: typedef struct {
     32:   PetscReal rho;
     33:   PetscReal nu;
     34:   BCType    bcType;
     35: } UserContext;

     37: int main(int argc,char **argv)
     38: {
     39:   KSP            ksp;
     40:   DM             da;
     41:   UserContext    user;
     43:   PetscInt       bc,i, m,n,its=100;
     44:   Vec            x,y;
     45:   PC             pc;
     46:   Mat            C;
     47:   PetscMPIInt    srank;
     48:   PetscScalar    *array;
     49: #if defined(PETSC_USE_LOG)
     50:   PetscLogStage stages[2];
     51: #endif

     53:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     54:   MPI_Comm_rank(PETSC_COMM_WORLD,&srank);

     56:   PetscLogStageRegister("Setup",&stages[0]);
     57:   PetscLogStageRegister("MatMult MPI",&stages[1]);

     59:   KSPCreate(PETSC_COMM_WORLD,&ksp);
     60:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,3,3,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da);
     61:   DMSetFromOptions(da);
     62:   DMSetUp(da);
     63:   DMDASetUniformCoordinates(da,0,1,0,1,0,0);
     64:   DMDASetFieldName(da,0,"Pressure");

     66:   user.rho    = 1.0;
     67:   user.nu     = 0.1;
     68:   bc          = (PetscInt)DIRICHLET;
     69:   user.bcType = (BCType)bc;

     71:   KSPSetComputeRHS(ksp,ComputeRHS,&user);
     72:   KSPSetComputeOperators(ksp,ComputeMatrix,&user);
     73:   KSPSetDM(ksp,da);
     74:   KSPSetFromOptions(ksp);
     75:   KSPSetUp(ksp);

     77:   KSPGetPC(ksp,&pc);
     78:   PCGetOperators(pc,&C,NULL);

     80:   /* Create x and y with shared memory */
     81:   /*-----------------------------------*/
     82:   MatGetLocalSize(C,&m,&n);
     83:   VecCreate(PETSC_COMM_WORLD,&x);
     84:   VecSetSizes(x,n,PETSC_DECIDE);
     85:   VecSetType(x,VECNODE);
     86:   VecSetFromOptions(x);

     88:   VecGetArray(x,&array);
     89:   for (i=0; i<n; i++) {
     90:     array[i] = (PetscScalar)(srank+1);
     91:   }
     92:   VecRestoreArray(x,&array);

     94:   VecCreate(PETSC_COMM_WORLD,&y);
     95:   VecSetSizes(y,m,PETSC_DECIDE);
     96:   VecSetType(y,VECNODE);
     97:   VecSetFromOptions(y);
     98:   VecSet(y,0.0);

    100:   /* Compute y = C*x */
    101:   /*-----------------*/
    102:   MPI_Barrier(PETSC_COMM_WORLD);
    103:   PetscLogStagePush(stages[1]);
    104:   for (i=0; i<its; i++) {
    105:     MatMult(C,x,y);
    106:   }
    107:   PetscLogStagePop();

    109:   /* Free spaces */
    110:   VecDestroy(&x);
    111:   VecDestroy(&y);
    112:   DMDestroy(&da);
    113:   KSPDestroy(&ksp);
    114:   PetscFinalize();
    115:   return ierr;
    116: }

    118: PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
    119: {
    120:   UserContext    *user = (UserContext*)ctx;
    122:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
    123:   PetscScalar    Hx,Hy;
    124:   PetscScalar    **array;
    125:   DM             da;

    128:   KSPGetDM(ksp,&da);
    129:   DMDAGetInfo(da, 0, &mx, &my, 0,0,0,0,0,0,0,0,0,0);
    130:   Hx   = 1.0 / (PetscReal)(mx-1);
    131:   Hy   = 1.0 / (PetscReal)(my-1);
    132:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
    133:   DMDAVecGetArray(da, b, &array);
    134:   for (j=ys; j<ys+ym; j++) {
    135:     for (i=xs; i<xs+xm; i++) {
    136:       array[j][i] = PetscExpScalar(-((PetscReal)i*Hx)*((PetscReal)i*Hx)/user->nu)*PetscExpScalar(-((PetscReal)j*Hy)*((PetscReal)j*Hy)/user->nu)*Hx*Hy;
    137:     }
    138:   }
    139:   DMDAVecRestoreArray(da, b, &array);
    140:   VecAssemblyBegin(b);
    141:   VecAssemblyEnd(b);

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

    148:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    149:     MatNullSpaceRemove(nullspace,b);
    150:     MatNullSpaceDestroy(&nullspace);
    151:   }
    152:   return(0);
    153: }

    155: PetscErrorCode ComputeRho(PetscInt i, PetscInt j, PetscInt mx, PetscInt my, PetscReal centerRho, PetscReal *rho)
    156: {
    158:   if ((i > mx/3.0) && (i < 2.0*mx/3.0) && (j > my/3.0) && (j < 2.0*my/3.0)) {
    159:     *rho = centerRho;
    160:   } else {
    161:     *rho = 1.0;
    162:   }
    163:   return(0);
    164: }

    166: PetscErrorCode ComputeMatrix(KSP ksp,Mat J,Mat jac,void *ctx)
    167: {
    168:   UserContext    *user = (UserContext*)ctx;
    169:   PetscReal      centerRho;
    171:   PetscInt       i,j,mx,my,xm,ym,xs,ys;
    172:   PetscScalar    v[5];
    173:   PetscReal      Hx,Hy,HydHx,HxdHy,rho;
    174:   MatStencil     row, col[5];
    175:   DM             da;

    178:   KSPGetDM(ksp,&da);
    179:   centerRho = user->rho;
    180:   DMDAGetInfo(da,0,&mx,&my,0,0,0,0,0,0,0,0,0,0);
    181:   Hx        = 1.0 / (PetscReal)(mx-1);
    182:   Hy        = 1.0 / (PetscReal)(my-1);
    183:   HxdHy     = Hx/Hy;
    184:   HydHx     = Hy/Hx;
    185:   DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);
    186:   for (j=ys; j<ys+ym; j++) {
    187:     for (i=xs; i<xs+xm; i++) {
    188:       row.i = i; row.j = j;
    189:       ComputeRho(i, j, mx, my, centerRho, &rho);
    190:       if (i==0 || j==0 || i==mx-1 || j==my-1) {
    191:         if (user->bcType == DIRICHLET) {
    192:           v[0] = 2.0*rho*(HxdHy + HydHx);
    193:           MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
    194:         } else if (user->bcType == NEUMANN) {
    195:           PetscInt numx = 0, numy = 0, num = 0;
    196:           if (j!=0) {
    197:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j-1;
    198:             numy++; num++;
    199:           }
    200:           if (i!=0) {
    201:             v[num] = -rho*HydHx;              col[num].i = i-1; col[num].j = j;
    202:             numx++; num++;
    203:           }
    204:           if (i!=mx-1) {
    205:             v[num] = -rho*HydHx;              col[num].i = i+1; col[num].j = j;
    206:             numx++; num++;
    207:           }
    208:           if (j!=my-1) {
    209:             v[num] = -rho*HxdHy;              col[num].i = i;   col[num].j = j+1;
    210:             numy++; num++;
    211:           }
    212:           v[num] = numx*rho*HydHx + numy*rho*HxdHy; col[num].i = i;   col[num].j = j;
    213:           num++;
    214:           MatSetValuesStencil(jac,1,&row,num,col,v,INSERT_VALUES);
    215:         }
    216:       } else {
    217:         v[0] = -rho*HxdHy;              col[0].i = i;   col[0].j = j-1;
    218:         v[1] = -rho*HydHx;              col[1].i = i-1; col[1].j = j;
    219:         v[2] = 2.0*rho*(HxdHy + HydHx); col[2].i = i;   col[2].j = j;
    220:         v[3] = -rho*HydHx;              col[3].i = i+1; col[3].j = j;
    221:         v[4] = -rho*HxdHy;              col[4].i = i;   col[4].j = j+1;
    222:         MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
    223:       }
    224:     }
    225:   }
    226:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
    227:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
    228:   if (user->bcType == NEUMANN) {
    229:     MatNullSpace nullspace;

    231:     MatNullSpaceCreate(PETSC_COMM_WORLD,PETSC_TRUE,0,0,&nullspace);
    232:     MatSetNullSpace(J,nullspace);
    233:     MatNullSpaceDestroy(&nullspace);
    234:   }
    235:   return(0);
    236: }

    238: /*TEST

    240:    build:
    241:     test:

    243: TEST*/