Actual source code: ex23.c

  1: /*$Id: ex1.c,v 1.19 2001/08/10 03:34:58 bsmith Exp $*/

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

 5:  #include petscda.h
 6:  #include petscsys.h

 10: int main(int argc,char **argv)
 11: {
 12:   int            size,M=8,ierr,dof=1,stencil_width=1,P=5;
 13:   int            N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE;
 14:   PetscTruth     flg2,flg3;
 15:   DAPeriodicType periodic = DA_NONPERIODIC;
 16:   DAStencilType  stencil_type = DA_STENCIL_STAR;
 17:   DA             da;
 18:   Vec            global1,global2;
 19:   PetscScalar    mone = -1.0;
 20:   PetscReal      norm;
 21:   PetscViewer    viewer;
 22:   PetscRandom    rand;

 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",(int*)&periodic,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",(int*)&stencil_type,PETSC_NULL);

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

 46:   DACreateGlobalVector(da,&global1);
 47:   PetscRandomCreate(PETSC_COMM_WORLD,RANDOM_DEFAULT,&rand);
 48:   VecSetRandom(rand,global1);
 49:   PetscRandomDestroy(rand);

 51:   DACreateGlobalVector(da,&global2);

 53:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",PETSC_FILE_CREATE,&viewer);
 54:   VecView(global1,viewer);
 55:   PetscViewerDestroy(viewer);

 57:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp",PETSC_FILE_RDONLY,&viewer);
 58:   VecLoadIntoVector(viewer,global2);
 59:   PetscViewerDestroy(viewer);

 61:   VecAXPY(&mone,global1,global2);
 62:   VecNorm(global2,NORM_MAX,&norm);
 63:   if (norm != 0.0) {
 64:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 65:     PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %g should be zero\n",norm);
 66:     PetscPrintf(PETSC_COMM_WORLD,"  Number of processors %d\n",size);
 67:     PetscPrintf(PETSC_COMM_WORLD,"  M,N,P,dof %d %d %d %d\n",M,N,P,dof);
 68:     PetscPrintf(PETSC_COMM_WORLD,"  stencil_width %d stencil_type %d periodic %d\n",stencil_width,stencil_type,periodic);
 69:     PetscPrintf(PETSC_COMM_WORLD,"  dimension %d\n",1 + (int) flg2 + (int) flg3);
 70:   }
 71: 
 72:   DADestroy(da);
 73:   VecDestroy(global1);
 74:   VecDestroy(global2);
 75:   PetscFinalize();
 76:   return 0;
 77: }