Actual source code: ex4.c

  1: /*$Id: ex4.c,v 1.53 2001/08/07 03:04:42 balay Exp $*/
  2: 
  3: static char help[] = "Tests various 2-dimensional DA routines.\n\n";

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

 10: int main(int argc,char **argv)
 11: {
 12:   int            rank,M = 10,N = 8,m = PETSC_DECIDE,ierr;
 13:   int            s=2,w=2,n = PETSC_DECIDE,nloc,l,i,j,kk;
 14:   int            Xs,Xm,Ys,Ym,iloc,*iglobal,*ltog;
 15:   int            *lx = PETSC_NULL,*ly = PETSC_NULL;
 16:   PetscTruth     testorder,flg;
 17:   DAPeriodicType wrap = DA_NONPERIODIC;
 18:   DA             da;
 19:   PetscViewer    viewer;
 20:   Vec            local,global;
 21:   PetscScalar    value;
 22:   DAStencilType  st = DA_STENCIL_BOX;
 23:   AO             ao;
 24: 
 25:   PetscInitialize(&argc,&argv,(char*)0,help);
 26:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,400,400,&viewer);
 27: 
 28:   /* Readoptions */
 29:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 30:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 31:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 33:   PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
 34:   PetscOptionsGetInt(PETSC_NULL,"-w",&w,PETSC_NULL);
 35:   PetscOptionsHasName(PETSC_NULL,"-xwrap",&flg); if (flg)  wrap = DA_XPERIODIC;
 36:   PetscOptionsHasName(PETSC_NULL,"-ywrap",&flg); if (flg)  wrap = DA_YPERIODIC;
 37:   PetscOptionsHasName(PETSC_NULL,"-xywrap",&flg); if (flg) wrap = DA_XYPERIODIC;
 38:   PetscOptionsHasName(PETSC_NULL,"-star",&flg); if (flg)   st = DA_STENCIL_STAR;
 39:   PetscOptionsHasName(PETSC_NULL,"-testorder",&testorder);
 40:   /*
 41:       Test putting two nodes in x and y on each processor, exact last processor 
 42:       in x and y gets the rest.
 43:   */
 44:   PetscOptionsHasName(PETSC_NULL,"-distribute",&flg);
 45:   if (flg) {
 46:     if (m == PETSC_DECIDE) SETERRQ(1,"Must set -m option with -distribute option");
 47:     PetscMalloc(m*sizeof(int),&lx);
 48:     for (i=0; i<m-1; i++) { lx[i] = 4;}
 49:     lx[m-1] = M - 4*(m-1);
 50:     if (n == PETSC_DECIDE) SETERRQ(1,"Must set -n option with -distribute option");
 51:     PetscMalloc(n*sizeof(int),&ly);
 52:     for (i=0; i<n-1; i++) { ly[i] = 2;}
 53:     ly[n-1] = N - 2*(n-1);
 54:   }


 57:   /* Create distributed array and get vectors */
 58:   DACreate2d(PETSC_COMM_WORLD,wrap,st,M,N,m,n,w,s,lx,ly,&da);
 59:   if (lx) {
 60:     PetscFree(lx);
 61:     PetscFree(ly);
 62:   }

 64:   DAView(da,viewer);
 65:   DACreateGlobalVector(da,&global);
 66:   DACreateLocalVector(da,&local);

 68:   /* Set global vector; send ghost points to local vectors */
 69:   value = 1;
 70:   VecSet(&value,global);
 71:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 72:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 74:   /* Scale local vectors according to processor rank; pass to global vector */
 75:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 76:   value = rank;
 77:   VecScale(&value,local);
 78:   DALocalToGlobal(da,local,INSERT_VALUES,global);

 80:   if (!testorder) { /* turn off printing when testing ordering mappings */
 81:     PetscPrintf (PETSC_COMM_WORLD,"\nGlobal Vectors:\n");
 82:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_NATIVE);
 83:     VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 84:     PetscPrintf (PETSC_COMM_WORLD,"\n\n");
 85:   }

 87:   /* Send ghost points to local vectors */
 88:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 89:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 91:   PetscOptionsHasName(PETSC_NULL,"-local_print",&flg);
 92:   if (flg) {
 93:     PetscViewer sviewer;
 94:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
 95:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 96:     VecView(local,sviewer);
 97:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
 98:   }

100:   /* Tests mappings betweeen application/PETSc orderings */
101:   if (testorder) {
102:     DAGetGhostCorners(da,&Xs,&Ys,PETSC_NULL,&Xm,&Ym,PETSC_NULL);
103:     DAGetGlobalIndices(da,&nloc,&ltog);
104:     DAGetAO(da,&ao);
105:     PetscMalloc(nloc*sizeof(int),&iglobal);

107:     /* Set iglobal to be global indices for each processor's local and ghost nodes,
108:        using the DA ordering of grid points */
109:     kk = 0;
110:     for (j=Ys; j<Ys+Ym; j++) {
111:       for (i=Xs; i<Xs+Xm; i++) {
112:         iloc = w*((j-Ys)*Xm + i-Xs);
113:         for (l=0; l<w; l++) {
114:           iglobal[kk++] = ltog[iloc+l];
115:         }
116:       }
117:     }

119:     /* Map this to the application ordering (which for DAs is just the natural ordering
120:        that would be used for 1 processor, numbering most rapidly by x, then y) */
121:     AOPetscToApplication(ao,nloc,iglobal);

123:     /* Then map the application ordering back to the PETSc DA ordering */
124:     AOApplicationToPetsc(ao,nloc,iglobal);

126:     /* Verify the mappings */
127:     kk=0;
128:     for (j=Ys; j<Ys+Ym; j++) {
129:       for (i=Xs; i<Xs+Xm; i++) {
130:         iloc = w*((j-Ys)*Xm + i-Xs);
131:         for (l=0; l<w; l++) {
132:           if (iglobal[kk] != ltog[iloc+l]) {fprintf(stdout,
133:             "[%d] Problem with mapping: j=%d, i=%d, l=%d, petsc1=%d, petsc2=%d\n",
134:              rank,j,i,l,ltog[iloc+l],iglobal[kk]);}
135:           kk++;
136:         }
137:       }
138:     }
139:     PetscFree(iglobal);
140:   }

142:   /* Free memory */
143:   PetscViewerDestroy(viewer);
144:   VecDestroy(local);
145:   VecDestroy(global);
146:   DADestroy(da);

148:   PetscFinalize();
149:   return 0;
150: }