Actual source code: ex7.c


  2: static char help[] = "Tests DMLocalToLocalxxx() for DMDA.\n\n";

  4: #include <petscdmda.h>

  6: int main(int argc,char **argv)
  7: {
  8:   PetscMPIInt      rank;
  9:   PetscInt         M=8,dof=1,stencil_width=1,i,start,end,P=5,N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,pt = 0,st = 0;
 10:   PetscErrorCode   ierr;
 11:   PetscBool        flg = PETSC_FALSE,flg2,flg3;
 12:   DMBoundaryType   periodic;
 13:   DMDAStencilType  stencil_type;
 14:   DM               da;
 15:   Vec              local,global,local_copy;
 16:   PetscScalar      value;
 17:   PetscReal        norm,work;
 18:   PetscViewer      viewer;
 19:   char             filename[64];
 20:   FILE             *file;

 22:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 23:   PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 24:   PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 25:   PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);
 26:   PetscOptionsGetInt(NULL,NULL,"-stencil_width",&stencil_width,NULL);
 27:   PetscOptionsGetInt(NULL,NULL,"-periodic",&pt,NULL);

 29:   periodic = (DMBoundaryType) pt;

 31:   PetscOptionsGetInt(NULL,NULL,"-stencil_type",&st,NULL);

 33:   stencil_type = (DMDAStencilType) st;

 35:   PetscOptionsHasName(NULL,NULL,"-grid2d",&flg2);
 36:   PetscOptionsHasName(NULL,NULL,"-grid3d",&flg3);
 37:   if (flg2) {
 38:     DMDACreate2d(PETSC_COMM_WORLD,periodic,periodic,stencil_type,M,N,m,n,dof,stencil_width,NULL,NULL,&da);
 39:   } else if (flg3) {
 40:     DMDACreate3d(PETSC_COMM_WORLD,periodic,periodic,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,NULL,NULL,NULL,&da);
 41:   } else {
 42:     DMDACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,NULL,&da);
 43:   }
 44:   DMSetFromOptions(da);
 45:   DMSetUp(da);

 47:   DMCreateGlobalVector(da,&global);
 48:   DMCreateLocalVector(da,&local);
 49:   VecDuplicate(local,&local_copy);

 51:   /* zero out vectors so that ghostpoints are zero */
 52:   value = 0;
 53:   VecSet(local,value);
 54:   VecSet(local_copy,value);

 56:   VecGetOwnershipRange(global,&start,&end);
 57:   for (i=start; i<end; i++) {
 58:     value = i + 1;
 59:     VecSetValues(global,1,&i,&value,INSERT_VALUES);
 60:   }
 61:   VecAssemblyBegin(global);
 62:   VecAssemblyEnd(global);

 64:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 65:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 67:   DMLocalToLocalBegin(da,local,INSERT_VALUES,local_copy);
 68:   DMLocalToLocalEnd(da,local,INSERT_VALUES,local_copy);

 70:   PetscOptionsGetBool(NULL,NULL,"-save",&flg,NULL);
 71:   if (flg) {
 72:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 73:     sprintf(filename,"local.%d",rank);
 74:     PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);
 75:     PetscViewerASCIIGetPointer(viewer,&file);
 76:     VecView(local,viewer);
 77:     fprintf(file,"Vector with correct ghost points\n");
 78:     VecView(local_copy,viewer);
 79:     PetscViewerDestroy(&viewer);
 80:   }

 82:   VecAXPY(local_copy,-1.0,local);
 83:   VecNorm(local_copy,NORM_MAX,&work);
 84:   MPI_Allreduce(&work,&norm,1,MPIU_REAL,MPIU_MAX,PETSC_COMM_WORLD);
 85:   PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %g should be zero\n",(double)norm);

 87:   VecDestroy(&local_copy);
 88:   VecDestroy(&local);
 89:   VecDestroy(&global);
 90:   DMDestroy(&da);
 91:   PetscFinalize();
 92:   return ierr;
 93: }

 95: /*TEST

 97:    test:
 98:       nsize: 8
 99:       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic

101:    test:
102:       suffix: 2
103:       nsize: 8
104:       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic -grid2d
105:       output_file: output/ex7_1.out

107:    test:
108:       suffix: 3
109:       nsize: 8
110:       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic -grid3d
111:       output_file: output/ex7_1.out

113: TEST*/