Actual source code: ex23.c

petsc-3.3-p6 2013-02-11
  2: static char help[] = "Tests VecView()/VecLoad() for DMDA vectors (this tests DMDAGlobalToNatural()).\n\n";

  4: #include <petscdmda.h>

  8: int main(int argc,char **argv)
  9: {
 10:   PetscMPIInt    size;
 11:   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:   PetscBool      flg2,flg3;
 14:   DMDABoundaryType bx = DMDA_BOUNDARY_NONE,by = DMDA_BOUNDARY_NONE,bz = DMDA_BOUNDARY_NONE;
 15:   DMDAStencilType  stencil_type = DMDA_STENCIL_STAR;
 16:   DM             da;
 17:   Vec            global1,global2,global3,global4;
 18:   PetscScalar    mone = -1.0;
 19:   PetscReal      norm;
 20:   PetscViewer    viewer;
 21:   PetscRandom    rdm;

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

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

 36:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",&st,PETSC_NULL);
 37:   stencil_type = (DMDAStencilType) st;

 39:   PetscOptionsHasName(PETSC_NULL,"-one",&flg2);
 40:   PetscOptionsHasName(PETSC_NULL,"-two",&flg2);
 41:   PetscOptionsHasName(PETSC_NULL,"-three",&flg3);
 42:   if (flg2) {
 43:     DMDACreate2d(PETSC_COMM_WORLD,bx,by,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da);
 44:   } else if (flg3) {
 45:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da);
 46:   }
 47:   else {
 48:     DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,PETSC_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);
 64: 
 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",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",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: }