Actual source code: ex23.c

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

 4:  #include petscda.h
 5:  #include petscsys.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;
 14:   PetscTruth     flg2,flg3;
 15:   DAPeriodicType periodic = DA_NONPERIODIC;
 16:   DAStencilType  stencil_type = DA_STENCIL_STAR;
 17:   DA             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(PETSC_NULL,"-M",&M,PETSC_NULL);
 27:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 28:   PetscOptionsGetInt(PETSC_NULL,"-P",&P,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-stencil_width",&stencil_width,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-periodic",&pt,PETSC_NULL);
 32:   periodic = (DAPeriodicType) pt;
 33:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",&st,PETSC_NULL);
 34:   stencil_type = (DAStencilType) st;

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

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

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

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


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