Actual source code: ex4.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Tests various 2-dimensional DMDA routines.\n\n";

  4: #include <petscdm.h>
  5: #include <petscdmda.h>

  9: int main(int argc,char **argv)
 10: {
 11:   PetscMPIInt      rank;
 12:   PetscErrorCode   ierr;
 13:   PetscInt         M = 10,N = 8,m = PETSC_DECIDE;
 14:   PetscInt         s =2,w=2,n = PETSC_DECIDE,nloc,l,i,j,kk;
 15:   PetscInt         Xs,Xm,Ys,Ym,iloc,*iglobal;
 16:   const PetscInt   *ltog;
 17:   PetscInt         *lx       = NULL,*ly = NULL;
 18:   PetscBool        testorder = PETSC_FALSE,flg;
 19:   DMBoundaryType   bx        = DM_BOUNDARY_NONE,by= DM_BOUNDARY_NONE;
 20:   DM               da;
 21:   PetscViewer      viewer;
 22:   Vec              local,global;
 23:   PetscScalar      value;
 24:   DMDAStencilType  st = DMDA_STENCIL_BOX;
 25:   AO               ao;

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

 30:   /* Readoptions */
 31:   PetscOptionsGetInt(NULL,"-NX",&M,NULL);
 32:   PetscOptionsGetInt(NULL,"-NY",&N,NULL);
 33:   PetscOptionsGetInt(NULL,"-m",&m,NULL);
 34:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 35:   PetscOptionsGetInt(NULL,"-s",&s,NULL);
 36:   PetscOptionsGetInt(NULL,"-w",&w,NULL);

 38:   flg  = PETSC_FALSE;
 39:   PetscOptionsGetBool(NULL,"-xperiodic",&flg,NULL); if (flg) bx = DM_BOUNDARY_PERIODIC;
 40:   flg  = PETSC_FALSE;
 41:   PetscOptionsGetBool(NULL,"-yperiodic",&flg,NULL); if (flg) by = DM_BOUNDARY_PERIODIC;
 42:   flg  = PETSC_FALSE;
 43:   PetscOptionsGetBool(NULL,"-xghosted",&flg,NULL); if (flg) bx = DM_BOUNDARY_GHOSTED;
 44:   flg  = PETSC_FALSE;
 45:   PetscOptionsGetBool(NULL,"-yghosted",&flg,NULL); if (flg) by = DM_BOUNDARY_GHOSTED;
 46:   flg  = PETSC_FALSE;
 47:   PetscOptionsGetBool(NULL,"-star",&flg,NULL); if (flg) st = DMDA_STENCIL_STAR;
 48:   flg  = PETSC_FALSE;
 49:   PetscOptionsGetBool(NULL,"-box",&flg,NULL); if (flg) st = DMDA_STENCIL_BOX;
 50:   flg  = PETSC_FALSE;
 51:   PetscOptionsGetBool(NULL,"-testorder",&testorder,NULL);
 52:   /*
 53:       Test putting two nodes in x and y on each processor, exact last processor
 54:       in x and y gets the rest.
 55:   */
 56:   flg  = PETSC_FALSE;
 57:   PetscOptionsGetBool(NULL,"-distribute",&flg,NULL);
 58:   if (flg) {
 59:     if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
 60:     PetscMalloc1(m,&lx);
 61:     for (i=0; i<m-1; i++) { lx[i] = 4;}
 62:     lx[m-1] = M - 4*(m-1);
 63:     if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
 64:     PetscMalloc1(n,&ly);
 65:     for (i=0; i<n-1; i++) { ly[i] = 2;}
 66:     ly[n-1] = N - 2*(n-1);
 67:   }


 70:   /* Create distributed array and get vectors */
 71:   DMDACreate2d(PETSC_COMM_WORLD,bx,by,st,M,N,m,n,w,s,lx,ly,&da);
 72:   PetscFree(lx);
 73:   PetscFree(ly);

 75:   DMView(da,viewer);
 76:   DMCreateGlobalVector(da,&global);
 77:   DMCreateLocalVector(da,&local);

 79:   /* Set global vector; send ghost points to local vectors */
 80:   value = 1;
 81:   VecSet(global,value);
 82:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
 83:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

 85:   /* Scale local vectors according to processor rank; pass to global vector */
 86:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 87:   value = rank;
 88:   VecScale(local,value);
 89:   DMLocalToGlobalBegin(da,local,INSERT_VALUES,global);
 90:   DMLocalToGlobalEnd(da,local,INSERT_VALUES,global);

 92:   if (!testorder) { /* turn off printing when testing ordering mappings */
 93:     PetscPrintf(PETSC_COMM_WORLD,"\nGlobal Vectors:\n");
 94:     VecView(global,PETSC_VIEWER_STDOUT_WORLD);
 95:     PetscPrintf(PETSC_COMM_WORLD,"\n\n");
 96:   }

 98:   /* Send ghost points to local vectors */
 99:   DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);
100:   DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);

102:   flg  = PETSC_FALSE;
103:   PetscOptionsGetBool(NULL,"-local_print",&flg,NULL);
104:   if (flg) {
105:     PetscViewer sviewer;
106: 
107:     PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD,PETSC_TRUE);
108:     PetscSynchronizedPrintf(PETSC_COMM_WORLD,"\nLocal Vector: processor %d\n",rank);
109:     PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
110:     VecView(local,sviewer);
111:     PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);
112:   }

114:   /* Tests mappings betweeen application/PETSc orderings */
115:   if (testorder) {
116:     ISLocalToGlobalMapping ltogm;

118:     DMGetLocalToGlobalMapping(da,&ltogm);
119:     ISLocalToGlobalMappingGetSize(ltogm,&nloc);
120:     ISLocalToGlobalMappingGetIndices(ltogm,&ltog);
121:     DMDAGetGhostCorners(da,&Xs,&Ys,NULL,&Xm,&Ym,NULL);
122:     DMDAGetAO(da,&ao);
123:     PetscMalloc1(nloc,&iglobal);

125:     /* Set iglobal to be global indices for each processor's local and ghost nodes,
126:        using the DMDA ordering of grid points */
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:           iglobal[kk++] = ltog[iloc+l];
133:         }
134:       }
135:     }

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

141:     /* Then map the application ordering back to the PETSc DMDA ordering */
142:     AOApplicationToPetsc(ao,nloc,iglobal);

144:     /* Verify the mappings */
145:     kk=0;
146:     for (j=Ys; j<Ys+Ym; j++) {
147:       for (i=Xs; i<Xs+Xm; i++) {
148:         iloc = w*((j-Ys)*Xm + i-Xs);
149:         for (l=0; l<w; l++) {
150:           if (iglobal[kk] != ltog[iloc+l]) {
151:             PetscFPrintf(PETSC_COMM_SELF,stdout,"[%d] Problem with mapping: j=%D, i=%D, l=%D, petsc1=%D, petsc2=%D\n",
152:                                 rank,j,i,l,ltog[iloc+l],iglobal[kk]);
153:           }
154:           kk++;
155:         }
156:       }
157:     }
158:     PetscFree(iglobal);
159:     ISLocalToGlobalMappingRestoreIndices(ltogm,&ltog);
160:   }

162:   /* Free memory */
163:   PetscViewerDestroy(&viewer);
164:   VecDestroy(&local);
165:   VecDestroy(&global);
166:   DMDestroy(&da);

168:   PetscFinalize();
169:   return 0;
170: }