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 DALocalToLocal().nn";

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

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


 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,"-dof",&dof,PETSC_NULL);
 29:   PetscOptionsGetInt(PETSC_NULL,"-stencil_width",&stencil_width,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-periodic",(int*)&periodic,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-stencil_type",(int*)&stencil_type,PETSC_NULL);

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

 46:   DACreateGlobalVector(da,&global);
 47:   DACreateLocalVector(da,&local);
 48:   VecDuplicate(local,&local_copy);

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

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

 64:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 65:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);


 68:   DALocalToLocalBegin(da,local,INSERT_VALUES,local_copy);
 69:   DALocalToLocalEnd(da,local,INSERT_VALUES,local_copy);

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

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