Actual source code: ex7.c

  1: /*$Id: ex7.c,v 1.30 2001/08/10 03:34:45 bsmith Exp $*/

  3: static char help[] = "Tests DALocalToLocalxxx().\n\n";

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

 10: int main(int argc,char **argv)
 11: {
 12:   int            rank,M=8,ierr,dof=1,stencil_width=1,i,start,end,P=5;
 13:   PetscTruth     flg,flg2,flg3;
 14:   int            N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE;
 15:   DAPeriodicType periodic;
 16:   DAStencilType  stencil_type;
 17:   DA             da;
 18:   Vec            local,global,local_copy;
 19:   PetscScalar    value,mone = -1.0;
 20:   double         norm,work;
 21:   PetscViewer    viewer;
 22:   char           filename[64];
 23:   FILE           *file;


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

 28:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-stencil_width",&stencil_width,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-periodic",(int*)&periodic,PETSC_NULL);
 33:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",(int*)&stencil_type,PETSC_NULL);

 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,
 39:                       PETSC_NULL,PETSC_NULL,&da);
 40:   } else if (flg3) {
 41:     DACreate3d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,
 42:                       PETSC_NULL,PETSC_NULL,PETSC_NULL,&da);
 43:   }
 44:   else {
 45:     DACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,PETSC_NULL,&da);
 46:   }

 48:   DACreateGlobalVector(da,&global);
 49:   DACreateLocalVector(da,&local);
 50:   VecDuplicate(local,&local_copy);

 52: 
 53:   /* zero out vectors so that ghostpoints are zero */
 54:   value = 0;
 55:   VecSet(&value,local);
 56:   VecSet(&value,local_copy);

 58:   VecGetOwnershipRange(global,&start,&end);
 59:   for (i=start; i<end; i++) {
 60:     value = i + 1;
 61:     VecSetValues(global,1,&i,&value,INSERT_VALUES);
 62:   }
 63:   VecAssemblyBegin(global);
 64:   VecAssemblyEnd(global);

 66:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 67:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);


 70:   DALocalToLocalBegin(da,local,INSERT_VALUES,local_copy);
 71:   DALocalToLocalEnd(da,local,INSERT_VALUES,local_copy);

 73:   PetscOptionsHasName(PETSC_NULL,"-save",&flg);
 74:   if (flg) {
 75:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 76:     sprintf(filename,"local.%d",rank);
 77:     PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);
 78:     PetscViewerASCIIGetPointer(viewer,&file);
 79:     VecView(local,viewer);
 80:     fprintf(file,"Vector with correct ghost points\n");
 81:     VecView(local_copy,viewer);
 82:     PetscViewerDestroy(viewer);
 83:   }

 85:   VecAXPY(&mone,local,local_copy);
 86:   VecNorm(local_copy,NORM_MAX,&work);
 87:   MPI_Allreduce(&work,&norm,1,MPIU_REAL,MPI_MAX,PETSC_COMM_WORLD);
 88:   PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %g should be zero\n",norm);
 89: 
 90:   VecDestroy(local_copy);
 91:   VecDestroy(local);
 92:   VecDestroy(global);
 93:   DADestroy(da);
 94:   PetscFinalize();
 95:   return 0;
 96: }