Actual source code: ex46.c

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1: static char help[] = "Tests DMGetCompatibility() with a 3D DMDA.\n\n";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

188:   /* Check compatibility with a DM of different type (DMStag) */
189:   {
190:     DM        dm2;
191:     PetscBool compatible,set;
192:     DMStagCreate3d(PETSC_COMM_WORLD,bx,by,bz,M,N,P,m,n,p,1,1,1,1,DMSTAG_STENCIL_STAR,w,lx,ly,lz,&dm2);
193:     DMSetUp(dm2);
194:     DMGetCompatibility(da,dm2,&compatible,&set);
195:     /* Don't interpret the result, but note that one can run with -info */
196:     DMDestroy(&dm2);
197:   }

199:   /* Free memory */
200:   PetscFree(lx);
201:   PetscFree(ly);
202:   PetscFree(lz);
203:   DMDestroy(&da);
204:   PetscFinalize();
205:   return ierr;
206: }

208: /*TEST

210:    test:
211:       suffix: 1

213:    test:
214:       suffix: 2
215:       nsize: 3
216:       args: distribute -m 1 -n 1 -p 3 -NZ 20

218:  TEST*/