Actual source code: ex46.c

petsc-3.10.3 2018-12-18
Report Typos and Errors
  1: static char help[] = "Tests DMGetCompatibility() with a 3D DMDA.\n\n";

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

  6: int main(int argc,char **argv) {
  7:   PetscInt         M = 3,N = 5,P=3,s=1,w=2,i,m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE;
  8:   PetscErrorCode   ierr;
  9:   PetscInt         *lx        = NULL,*ly = NULL,*lz = NULL;
 10:   PetscBool        test_order = PETSC_FALSE;
 11:   DM               da;
 12:   DMBoundaryType   bx           = DM_BOUNDARY_NONE,by = DM_BOUNDARY_NONE,bz = DM_BOUNDARY_NONE;
 13:   DMDAStencilType  stencil_type = DMDA_STENCIL_BOX;
 14:   PetscBool        flg = PETSC_FALSE,distribute = PETSC_FALSE;

 16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 18:   /* Read options */
 19:   PetscOptionsGetInt(NULL,NULL,"-NX",&M,NULL);
 20:   PetscOptionsGetInt(NULL,NULL,"-NY",&N,NULL);
 21:   PetscOptionsGetInt(NULL,NULL,"-NZ",&P,NULL);
 22:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 23:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 24:   PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL);
 25:   PetscOptionsGetInt(NULL,NULL,"-s",&s,NULL);
 26:   PetscOptionsGetInt(NULL,NULL,"-w",&w,NULL);
 27:   flg  = PETSC_FALSE;
 28:   PetscOptionsGetBool(NULL,NULL,"-star",&flg,NULL);
 29:   if (flg) stencil_type =  DMDA_STENCIL_STAR;
 30:   flg  = PETSC_FALSE;
 31:   PetscOptionsGetBool(NULL,NULL,"-box",&flg,NULL);
 32:   if (flg) stencil_type =  DMDA_STENCIL_BOX;

 34:   flg  = PETSC_FALSE;
 35:   PetscOptionsGetBool(NULL,NULL,"-xperiodic",&flg,NULL);
 36:   if (flg) bx = DM_BOUNDARY_PERIODIC;
 37:   flg  = PETSC_FALSE;
 38:   PetscOptionsGetBool(NULL,NULL,"-xghosted",&flg,NULL);
 39:   if (flg) bx = DM_BOUNDARY_GHOSTED;
 40:   flg  = PETSC_FALSE;
 41:   PetscOptionsGetBool(NULL,NULL,"-xnonghosted",&flg,NULL);

 43:   flg  = PETSC_FALSE;
 44:   PetscOptionsGetBool(NULL,NULL,"-yperiodic",&flg,NULL);
 45:   if (flg) by = DM_BOUNDARY_PERIODIC;
 46:   flg  = PETSC_FALSE;
 47:   PetscOptionsGetBool(NULL,NULL,"-yghosted",&flg,NULL);
 48:   if (flg) by = DM_BOUNDARY_GHOSTED;
 49:   flg  = PETSC_FALSE;
 50:   PetscOptionsGetBool(NULL,NULL,"-ynonghosted",&flg,NULL);

 52:   flg  = PETSC_FALSE;
 53:   PetscOptionsGetBool(NULL,NULL,"-zperiodic",&flg,NULL);
 54:   if (flg) bz = DM_BOUNDARY_PERIODIC;
 55:   flg  = PETSC_FALSE;
 56:   PetscOptionsGetBool(NULL,NULL,"-zghosted",&flg,NULL);
 57:   if (flg) bz = DM_BOUNDARY_GHOSTED;
 58:   flg  = PETSC_FALSE;
 59:   PetscOptionsGetBool(NULL,NULL,"-znonghosted",&flg,NULL);

 61:   PetscOptionsGetBool(NULL,NULL,"-testorder",&test_order,NULL);

 63:   flg  = PETSC_FALSE;
 64:   PetscOptionsGetBool(NULL,NULL,"-distribute",&distribute,NULL);
 65:   if (distribute) {
 66:     if (m == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -m option with -distribute option");
 67:     PetscMalloc1(m,&lx);
 68:     for (i=0; i<m-1; i++) lx[i] = 4;
 69:     lx[m-1] = M - 4*(m-1);
 70:     if (n == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -n option with -distribute option");
 71:     PetscMalloc1(n,&ly);
 72:     for (i=0; i<n-1; i++) ly[i] = 2;
 73:     ly[n-1] = N - 2*(n-1);
 74:     if (p == PETSC_DECIDE) SETERRQ(PETSC_COMM_WORLD,1,"Must set -p option with -distribute option");
 75:     PetscMalloc1(p,&lz);
 76:     for (i=0; i<p-1; i++) lz[i] = 2;
 77:     lz[p-1] = P - 2*(p-1);
 78:   }

 80:   DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da);
 81:   DMSetFromOptions(da);
 82:   DMSetUp(da);

 84:   /* Check self-compatibility */
 85:   {
 86:     PetscBool compatible,set;
 87:     DMGetCompatibility(da,da,&compatible,&set);
 88:     if (!set || !compatible) {
 89:       PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with itself\n");
 90:     }
 91:   }

 93:   /* Check compatibility with the same DM on a dup'd communicator */
 94:   {
 95:     DM        da2;
 96:     PetscBool compatible,set;
 97:     MPI_Comm  comm2;
 98:     MPI_Comm_dup(PETSC_COMM_WORLD,&comm2);
 99:     DMDACreate3d(comm2,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da2);
100:     DMSetFromOptions(da2);
101:     DMSetUp(da2);
102:     DMGetCompatibility(da,da2,&compatible,&set);
103:     if (!set || !compatible) {
104:       PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with DMDA on dup'd comm\n");
105:     }
106:     DMDestroy(&da2);
107:     MPI_Comm_free(&comm2);
108:   }

110:   /* Check compatibility with a derived DMDA */
111:   {
112:     DM        da2;
113:     PetscBool compatible,set;
114:     DMDACreateCompatibleDMDA(da,w*2,&da2);
115:     DMGetCompatibility(da,da2,&compatible,&set);
116:     if (!set || !compatible) {
117:       PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not compatible with DMDA created with DMDACreateCompatibleDMDA()\n");
118:     }
119:     DMDestroy(&da2);
120:   }

122:   /* Confirm incompatibility with different stencil width */
123:   {
124:     DM        da2;
125:     PetscBool compatible,set;
126:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,0,lx,ly,lz,&da2);
127:     DMSetUp(da2);
128:     DMGetCompatibility(da,da2,&compatible,&set);
129:     if (!set || compatible) {
130:       PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different stencil width)\n");
131:     }
132:     DMDestroy(&da2);
133:   }

135:   /* Confirm incompatibility with different boundary types */
136:   {
137:     DM        da2;
138:     PetscBool compatible,set;
139:     DMBoundaryType bz2;
140:     bz2 = bz == DM_BOUNDARY_NONE ? DM_BOUNDARY_GHOSTED : DM_BOUNDARY_NONE;
141:     DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz2,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz,&da2);
142:     DMSetUp(da2);
143:     DMGetCompatibility(da,da2,&compatible,&set);
144:     if (!set || compatible) {
145:       PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different boundary type)\n");
146:     }
147:     DMDestroy(&da2);
148:   }

150:   if (!distribute) {
151:     /* Confirm incompatibility with different global sizes */
152:     {
153:       DM        da2;
154:       PetscBool compatible,set;
155:       DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P*2,m,n,p,w,s,lx,ly,lz,&da2);
156:       DMSetUp(da2);
157:       DMGetCompatibility(da,da2,&compatible,&set);
158:       if (!set || compatible) {
159:         PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different global sizes)\n");
160:       }
161:       DMDestroy(&da2);
162:     }
163:   }

165:   if (distribute && p > 1) {
166:     /* Confirm incompatibility with different local size */
167:     {
168:       DM          da2;
169:       PetscBool   compatible,set;
170:       PetscMPIInt rank;
171:       PetscInt    *lz2;
172:       PetscMalloc1(p,&lz2);
173:       for (i=0; i<p-1; i++) lz2[i] = 1; /* One point per rank instead of 2 */
174:       lz2[p-1] = P - (p-1);
175:       MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
176:       DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stencil_type,M,N,P,m,n,p,w,s,lx,ly,lz2,&da2);
177:       DMSetUp(da2);
178:       DMGetCompatibility(da,da2,&compatible,&set);
179:       if (!set || compatible) {
180:         PetscPrintf(PetscObjectComm((PetscObject)da),"Error: DM not determined incompatible with known-incompatible DMDA (different local sizes) \n");
181:       }
182:       DMDestroy(&da2);
183:       PetscFree(lz2);
184:     }
185:   }

187:   /* Free memory */
188:   PetscFree(lx);
189:   PetscFree(ly);
190:   PetscFree(lz);
191:   DMDestroy(&da);
192:   PetscFinalize();
193:   return ierr;
194: }

196: /*TEST

198:    test:
199:       suffix: 1

201:    test:
202:       suffix: 2
203:       nsize: 3
204:       args: distribute -m 1 -n 1 -p 3 -NZ 20

206:  TEST*/