Actual source code: ex23.c

  2: static char help[] = "Tests VecView()/VecLoad() for DA vectors (this tests DAGlobalToNatural()).\n\n";

 4:  #include petscda.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:   PetscTruth     flg2,flg3;
 14:   DAPeriodicType periodic = DA_NONPERIODIC;
 15:   DAStencilType  stencil_type = DA_STENCIL_STAR;
 16:   DA             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:   periodic = (DAPeriodicType) pt;
 32:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",&st,PETSC_NULL);
 33:   stencil_type = (DAStencilType) st;

 35:   PetscOptionsHasName(PETSC_NULL,"-1d",&flg2);
 36:   PetscOptionsHasName(PETSC_NULL,"-2d",&flg2);
 37:   PetscOptionsHasName(PETSC_NULL,"-3d",&flg3);
 38:   if (flg2) {
 39:     DACreate2d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da);
 40:   } else if (flg3) {
 41:     DACreate3d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da);
 42:   }
 43:   else {
 44:     DACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,PETSC_NULL,&da);
 45:   }

 47:   DACreateGlobalVector(da,&global1);
 48:   PetscRandomCreate(PETSC_COMM_WORLD,&rdm);
 49:   PetscRandomSetFromOptions(rdm);
 50:   DACreateGlobalVector(da,&global2);
 51:   DACreateGlobalVector(da,&global3);
 52:   DACreateGlobalVector(da,&global4);

 54:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_WRITE,&viewer);
 55:   VecSetRandom(global1,rdm);
 56:   VecView(global1,viewer);
 57:   VecSetRandom(global3,rdm);
 58:   VecView(global3,viewer);
 59:   PetscViewerDestroy(viewer);
 60: 
 61:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",FILE_MODE_READ,&viewer);
 62:   VecLoadIntoVector(viewer,global2);
 63:   VecLoadIntoVector(viewer,global4);
 64:   PetscViewerDestroy(viewer);

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


 88:   PetscRandomDestroy(rdm);
 89:   DADestroy(da);
 90:   VecDestroy(global1);
 91:   VecDestroy(global2);
 92:   VecDestroy(global3);
 93:   VecDestroy(global4);
 94:   PetscFinalize();
 95:   return 0;
 96: }