Actual source code: ex23.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: static char help[] = "Tests VecView()/VecLoad() for DMDA vectors (this tests DMDAGlobalToNatural()).\n\n";

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

  9: int main(int argc,char **argv)
 10: {
 11:   PetscMPIInt      size;
 12:   PetscInt         N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,M=8,dof=1,stencil_width=1,P=5,pt = 0,st = 0;
 13:   PetscErrorCode   ierr;
 14:   PetscBool        flg2,flg3,native = PETSC_FALSE;
 15:   DMBoundaryType   bx           = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;
 16:   DMDAStencilType  stencil_type = DMDA_STENCIL_STAR;
 17:   DM               da;
 18:   Vec              global1,global2,global3,global4;
 19:   PetscScalar      mone = -1.0;
 20:   PetscReal        norm;
 21:   PetscViewer      viewer;
 22:   PetscRandom      rdm;

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

 26:   PetscOptionsGetInt(NULL,"-M",&M,NULL);
 27:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 28:   PetscOptionsGetInt(NULL,"-P",&P,NULL);
 29:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 30:   PetscOptionsGetInt(NULL,"-stencil_width",&stencil_width,NULL);
 31:   PetscOptionsGetInt(NULL,"-periodic",&pt,NULL);
 32:   PetscOptionsGetBool(NULL,"-native",&native,NULL);
 33:   if (pt == 1) bx = DM_BOUNDARY_PERIODIC;
 34:   if (pt == 2) by = DM_BOUNDARY_PERIODIC;
 35:   if (pt == 3) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;}
 36:   if (pt == 4) bz = DM_BOUNDARY_PERIODIC;

 38:   PetscOptionsGetInt(NULL,"-stencil_type",&st,NULL);
 39:   stencil_type = (DMDAStencilType) st;

 41:   PetscOptionsHasName(NULL,"-one",&flg2);
 42:   PetscOptionsHasName(NULL,"-two",&flg2);
 43:   PetscOptionsHasName(NULL,"-three",&flg3);
 44:   if (flg2) {
 45:     DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da);
 46:   } else if (flg3) {
 47:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da);
 48:   } else {
 49:     DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da);
 50:   }

 52:   DMCreateGlobalVector(da,&global1);
 53:   PetscRandomCreate(PETSC_COMM_WORLD,&rdm);
 54:   PetscRandomSetFromOptions(rdm);
 55:   DMCreateGlobalVector(da,&global2);
 56:   DMCreateGlobalVector(da,&global3);
 57:   DMCreateGlobalVector(da,&global4);

 59:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);
 60:   if (native) {PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);}
 61:   VecSetRandom(global1,rdm);
 62:   VecView(global1,viewer);
 63:   VecSetRandom(global3,rdm);
 64:   VecView(global3,viewer);
 65:   PetscViewerDestroy(&viewer);

 67:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
 68:   if (native) {PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);}
 69:   VecLoad(global2,viewer);
 70:   VecLoad(global4,viewer);
 71:   PetscViewerDestroy(&viewer);

 73:   if (native) {
 74:     Vec filenative;
 75:     PetscBool same;
 76:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
 77:     DMDACreateNaturalVector(da,&filenative);
 78:     /* DMDA "natural" Vec does not commandeer VecLoad.  The following load will only work when run on the same process
 79:      * layout, where as the standard VecView/VecLoad (using DMDA and not PETSC_VIEWER_NATIVE) can be read on a different
 80:      * number of processors. */
 81:     VecLoad(filenative,viewer);
 82:     VecEqual(global2,filenative,&same);
 83:     if (!same) {
 84:       PetscPrintf(PETSC_COMM_WORLD,"ex23: global vector does not match contents of file\n");
 85:       VecView(global2,0);
 86:       VecView(filenative,0);
 87:     }
 88:     PetscViewerDestroy(&viewer);
 89:     VecDestroy(&filenative);
 90:   }

 92:   VecAXPY(global2,mone,global1);
 93:   VecNorm(global2,NORM_MAX,&norm);
 94:   if (norm != 0.0) {
 95:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 96:     PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
 97:     PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size);
 98:     PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %D %D %D %D\n",M,N,P,dof);
 99:     PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
100:     PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3);
101:   }
102:   VecAXPY(global4,mone,global3);
103:   VecNorm(global4,NORM_MAX,&norm);
104:   if (norm != 0.0) {
105:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
106:     PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
107:     PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size);
108:     PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %D %D %D %D\n",M,N,P,dof);
109:     PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
110:     PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3);
111:   }


114:   PetscRandomDestroy(&rdm);
115:   DMDestroy(&da);
116:   VecDestroy(&global1);
117:   VecDestroy(&global2);
118:   VecDestroy(&global3);
119:   VecDestroy(&global4);
120:   PetscFinalize();
121:   return 0;
122: }