Actual source code: ex15.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Tests DMDA interpolation.\n\n";

  4: #include <petscdm.h>
  5: #include <petscdmda.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscInt         M1 = 3,M2,dof = 1,s = 1,ratio = 2,dim = 1;
 12:   PetscErrorCode   ierr;
 13:   DM               da_c,da_f;
 14:   Vec              v_c,v_f;
 15:   Mat              Interp;
 16:   PetscScalar      one = 1.0;
 17:   PetscBool        pt;
 18:   DMBoundaryType bx = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;

 20:   PetscInitialize(&argc,&argv,(char*)0,help);

 22:   PetscOptionsGetInt(NULL,"-dim",&dim,NULL);
 23:   PetscOptionsGetInt(NULL,"-M",&M1,NULL);
 24:   PetscOptionsGetInt(NULL,"-stencil_width",&s,NULL);
 25:   PetscOptionsGetInt(NULL,"-ratio",&ratio,NULL);
 26:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 27:   PetscOptionsGetBool(NULL,"-periodic",(PetscBool*)&pt,NULL);

 29:   if (pt) {
 30:     if (dim > 0) bx = DM_BOUNDARY_PERIODIC;
 31:     if (dim > 1) by = DM_BOUNDARY_PERIODIC;
 32:     if (dim > 2) bz = DM_BOUNDARY_PERIODIC;
 33:   }
 34:   if (bx == DM_BOUNDARY_NONE) {
 35:     M2 = ratio*(M1-1) + 1;
 36:   } else {
 37:     M2 = ratio*M1;
 38:   }

 40:   /* Set up the array */
 41:   if (dim == 1) {
 42:     DMDACreate1d(PETSC_COMM_WORLD,bx,M1,dof,s,NULL,&da_c);
 43:     DMDACreate1d(PETSC_COMM_WORLD,bx,M2,dof,s,NULL,&da_f);
 44:   } else if (dim == 2) {
 45:     DMDACreate2d(PETSC_COMM_WORLD,bx,by,DMDA_STENCIL_BOX,M1,M1,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,&da_c);
 46:     DMDACreate2d(PETSC_COMM_WORLD,bx,by,DMDA_STENCIL_BOX,M2,M2,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,&da_f);
 47:   } else if (dim == 3) {
 48:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,DMDA_STENCIL_BOX,M1,M1,M1,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,NULL,&da_c);
 49:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,DMDA_STENCIL_BOX,M2,M2,M2,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,s,NULL,NULL,NULL,&da_f);
 50:   } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"dim must be 1,2, or 3");

 52:   DMCreateGlobalVector(da_c,&v_c);
 53:   DMCreateGlobalVector(da_f,&v_f);

 55:   VecSet(v_c,one);
 56:   DMCreateInterpolation(da_c,da_f,&Interp,NULL);
 57:   MatMult(Interp,v_c,v_f);
 58:   VecView(v_f,PETSC_VIEWER_STDOUT_WORLD);
 59:   MatMultTranspose(Interp,v_f,v_c);
 60:   VecView(v_c,PETSC_VIEWER_STDOUT_WORLD);

 62:   MatDestroy(&Interp);
 63:   VecDestroy(&v_c);
 64:   DMDestroy(&da_c);
 65:   VecDestroy(&v_f);
 66:   DMDestroy(&da_f);
 67:   PetscFinalize();
 68:   return 0;
 69: }