Actual source code: ex23.c

petsc-3.4.5 2014-06-29
  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;
 12:   PetscErrorCode   ierr;
 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(NULL,"-M",&M,NULL);
 26:   PetscOptionsGetInt(NULL,"-N",&N,NULL);
 27:   PetscOptionsGetInt(NULL,"-P",&P,NULL);
 28:   PetscOptionsGetInt(NULL,"-dof",&dof,NULL);
 29:   PetscOptionsGetInt(NULL,"-stencil_width",&stencil_width,NULL);
 30:   PetscOptionsGetInt(NULL,"-periodic",&pt,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(NULL,"-stencil_type",&st,NULL);
 37:   stencil_type = (DMDAStencilType) st;

 39:   PetscOptionsHasName(NULL,"-one",&flg2);
 40:   PetscOptionsHasName(NULL,"-two",&flg2);
 41:   PetscOptionsHasName(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:   } else {
 47:     DMDACreate1d(PETSC_COMM_WORLD,bx,M,dof,stencil_width,NULL,&da);
 48:   }

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

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

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

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


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