Actual source code: ex23.c

petsc-3.5.1 2014-07-24
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;
 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:   if (pt == 1) bx = DM_BOUNDARY_PERIODIC;
 33:   if (pt == 2) by = DM_BOUNDARY_PERIODIC;
 34:   if (pt == 3) {bx = DM_BOUNDARY_PERIODIC; by = DM_BOUNDARY_PERIODIC;}
 35:   if (pt == 4) bz = DM_BOUNDARY_PERIODIC;

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

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

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

 58:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);
 59:   VecSetRandom(global1,rdm);
 60:   VecView(global1,viewer);
 61:   VecSetRandom(global3,rdm);
 62:   VecView(global3,viewer);
 63:   PetscViewerDestroy(&viewer);

 65:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
 66:   VecLoad(global2,viewer);
 67:   VecLoad(global4,viewer);
 68:   PetscViewerDestroy(&viewer);

 70:   VecAXPY(global2,mone,global1);
 71:   VecNorm(global2,NORM_MAX,&norm);
 72:   if (norm != 0.0) {
 73:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 74:     PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
 75:     PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size);
 76:     PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %D %D %D %D\n",M,N,P,dof);
 77:     PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
 78:     PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3);
 79:   }
 80:   VecAXPY(global4,mone,global3);
 81:   VecNorm(global4,NORM_MAX,&norm);
 82:   if (norm != 0.0) {
 83:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 84:     PetscPrintf(PETSC_COMM_WORLD,"ex23: Norm of difference %g should be zero\n",(double)norm);
 85:     PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size);
 86:     PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %D %D %D %D\n",M,N,P,dof);
 87:     PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)bx);
 88:     PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3);
 89:   }


 92:   PetscRandomDestroy(&rdm);
 93:   DMDestroy(&da);
 94:   VecDestroy(&global1);
 95:   VecDestroy(&global2);
 96:   VecDestroy(&global3);
 97:   VecDestroy(&global4);
 98:   PetscFinalize();
 99:   return 0;
100: }