Actual source code: ex6.c

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

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

 11: int main(int argc,char **argv)
 12: {
 13:   int            rank,M = 3,N = 5,P=3,s=1,w=2,nloc,l,i,j,k,kk;
 14:   int            m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE,ierr;
 15:   int            Xs,Xm,Ys,Ym,Zs,Zm,iloc,*ltog,*iglobal;
 16:   int            *lx = PETSC_NULL,*ly = PETSC_NULL,*lz = PETSC_NULL;
 17:   PetscTruth     test_order;
 18:   DA             da;
 19:   PetscViewer    viewer;
 20:   Vec            local,global;
 21:   PetscScalar    value;
 22:   DAPeriodicType wrap = DA_XYPERIODIC;
 23:   DAStencilType  stencil_type = DA_STENCIL_BOX;
 24:   AO             ao;
 25:   PetscTruth     flg;

 27:   PetscInitialize(&argc,&argv,(char*)0,help);
 28:   PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,400,300,&viewer);

 30:   /* Read options */
 31:   PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);
 32:   PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);
 33:   PetscOptionsGetInt(PETSC_NULL,"-P",&P,PETSC_NULL);
 34:   PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
 35:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 36:   PetscOptionsGetInt(PETSC_NULL,"-p",&p,PETSC_NULL);
 37:   PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);
 38:   PetscOptionsGetInt(PETSC_NULL,"-w",&w,PETSC_NULL);
 39:   PetscOptionsHasName(PETSC_NULL,"-star",&flg);
 40:   if (flg) stencil_type =  DA_STENCIL_STAR;
 41:   PetscOptionsHasName(PETSC_NULL,"-test_order",&test_order);

 43:   PetscOptionsHasName(PETSC_NULL,"-distribute",&flg);
 44:   if (flg) {
 45:     if (m == PETSC_DECIDE) SETERRQ(1,"Must set -m option with -distribute option");
 46:     PetscMalloc(m*sizeof(int),&lx);
 47:     for (i=0; i<m-1; i++) { lx[i] = 4;}
 48:     lx[m-1] = M - 4*(m-1);
 49:     if (n == PETSC_DECIDE) SETERRQ(1,"Must set -n option with -distribute option");
 50:     PetscMalloc(n*sizeof(int),&ly);
 51:     for (i=0; i<n-1; i++) { ly[i] = 2;}
 52:     ly[n-1] = N - 2*(n-1);
 53:     if (p == PETSC_DECIDE) SETERRQ(1,"Must set -p option with -distribute option");
 54:     PetscMalloc(p*sizeof(int),&lz);
 55:     for (i=0; i<p-1; i++) { lz[i] = 2;}
 56:     lz[p-1] = P - 2*(p-1);
 57:   }

 59:   /* Create distributed array and get vectors */
 60:   DACreate3d(PETSC_COMM_WORLD,wrap,stencil_type,M,N,P,m,n,p,w,s,
 61:                     lx,ly,lz,&da);
 62:   if (lx) {
 63:     PetscFree(lx);
 64:     PetscFree(ly);
 65:     PetscFree(lz);
 66:   }
 67:   DAView(da,viewer);
 68:   DACreateGlobalVector(da,&global);
 69:   DACreateLocalVector(da,&local);

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

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

 83:   if (!test_order) { /* turn off printing when testing ordering mappings */
 84:     if (M*N*P<40) {
 85:       PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vector:\n");
 86:       VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 87:       PetscPrintf(PETSC_COMM_WORLD,"\n");
 88:     }
 89:   }

 91:   /* Send ghost points to local vectors */
 92:   DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 93:   DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 95:   PetscOptionsHasName(PETSC_NULL,"-local_print",&flg);
 96:   if (flg) {
 97:     PetscViewer sviewer;
 98:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
 99:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
100:     VecView(local,sviewer);
101:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
102:     PetscSynchronizedFlush(PETSC_COMM_WORLD);
103:   }

105:   /* Tests mappings betweeen application/PETSc orderings */
106:   if (test_order) {
107:     DAGetGhostCorners(da,&Xs,&Ys,&Zs,&Xm,&Ym,&Zm);
108:     DAGetGlobalIndices(da,&nloc,&ltog);
109:     DAGetAO(da,&ao);
110:     /* AOView(ao,PETSC_VIEWER_STDOUT_WORLD); */
111:     PetscMalloc(nloc*sizeof(int),&iglobal);

113:     /* Set iglobal to be global indices for each processor's local and ghost nodes,
114:        using the DA ordering of grid points */
115:     kk = 0;
116:     for (k=Zs; k<Zs+Zm; k++) {
117:       for (j=Ys; j<Ys+Ym; j++) {
118:         for (i=Xs; i<Xs+Xm; i++) {
119:           iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
120:           for (l=0; l<w; l++) {
121:             iglobal[kk++] = ltog[iloc+l];
122:           }
123:         }
124:       }
125:     }

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

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

134:     /* Verify the mappings */
135:     kk=0;
136:     for (k=Zs; k<Zs+Zm; k++) {
137:       for (j=Ys; j<Ys+Ym; j++) {
138:         for (i=Xs; i<Xs+Xm; i++) {
139:           iloc = w*((k-Zs)*Xm*Ym + (j-Ys)*Xm + i-Xs);
140:           for (l=0; l<w; l++) {
141:             if (iglobal[kk] != ltog[iloc+l]) {
142:               fprintf(stdout,"[%d] Problem with mapping: z=%d, j=%d, i=%d, l=%d, petsc1=%d, petsc2=%d\n",
143:                       rank,k,j,i,l,ltog[iloc+l],iglobal[kk]);
144:             }
145:             kk++;
146:           }
147:         }
148:       }
149:     }
150:     PetscFree(iglobal);
151:   }

153:   /* Free memory */
154:   PetscViewerDestroy(viewer);
155:   VecDestroy(local);
156:   VecDestroy(global);
157:   DADestroy(da);
158:   PetscFinalize();
159:   return 0;
160: }
161: