Actual source code: ex7.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  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);


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

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

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


 69:   DMLocalToLocalBegin(da,local,INSERT_VALUES,local_copy);
 70:   DMLocalToLocalEnd(da,local,INSERT_VALUES,local_copy);


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

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

 90:   VecDestroy(&local_copy);
 91:   VecDestroy(&local);
 92:   VecDestroy(&global);
 93:   DMDestroy(&da);
 94:   PetscFinalize();
 95:   return ierr;
 96: }


 99: /*TEST

101:    test:
102:       nsize: 8
103:       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic

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

111:    test:
112:       suffix: 3
113:       nsize: 8
114:       args: -dof 3 -stencil_width 2 -M 50 -N 50 -periodic -grid3d
115:       output_file: output/ex7_1.out

117: TEST*/