Actual source code: stag1d.c

petsc-3.11.3 2019-06-26
Report Typos and Errors
  1: /*
  2:    Functions specific to the 1-dimensional implementation of DMStag
  3: */
  4:  #include <petsc/private/dmstagimpl.h>

  6: /*@C
  7:   DMStagCreate1d - Create an object to manage data living on the faces, edges, and vertices of a parallelized regular 1D grid.

  9:   Collective

 11:   Input Parameters:
 12: + comm - MPI communicator
 13: . bndx - boundary type: DM_BOUNDARY_NONE, DM_BOUNDARY_PERIODIC, or DM_BOUNDARY_GHOSTED
 14: . M - global number of grid points
 15: . dof0 - number of degrees of freedom per vertex/point/node/0-cell
 16: . dof1 - number of degrees of freedom per element/edge/1-cell
 17: . stencilType - ghost/halo region type: DMSTAG_STENCIL_BOX or DMSTAG_STENCIL_NONE
 18: . stencilWidth - width, in elements, of halo/ghost region
 19: - lx - array of local sizes, of length equal to the comm size, summing to M

 21:   Output Parameter:
 22: . dm - the new DMStag object

 24:   Options Database Keys:
 25: + -dm_view - calls DMViewFromOptions() a the conclusion of DMSetUp()
 26: . -stag_grid_x <nx> - number of elements in the x direction
 27: . -stag_ghost_stencil_width - width of ghost region, in elements
 28: - -stag_boundary_type_x <none,ghosted,periodic> - DMBoundaryType value

 30:   Notes:
 31:   You must call DMSetUp() after this call before using the DM.
 32:   If you wish to use the options database (see the keys above) to change values in the DMStag, you must call
 33:   DMSetFromOptions() after this function but before DMSetUp().

 35:   Level: beginner

 37: .seealso: DMSTAG, DMStagCreate2d(), DMStagCreate3d(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateLocalVector(), DMLocalToGlobalBegin()
 38: @*/
 39: PETSC_EXTERN PetscErrorCode DMStagCreate1d(MPI_Comm comm,DMBoundaryType bndx,PetscInt M,PetscInt dof0,PetscInt dof1,DMStagStencilType stencilType,PetscInt stencilWidth,const PetscInt lx[],DM* dm)
 40: {
 42:   DM_Stag        *stag;
 43:   PetscMPIInt    size;

 46:   MPI_Comm_size(comm,&size);
 47:   DMCreate(comm,dm);
 48:   DMSetDimension(*dm,1); /* Must precede DMSetType */
 49:   DMSetType(*dm,DMSTAG);
 50:   stag = (DM_Stag*)(*dm)->data;

 52:   /* Global sizes and flags (derived quantities set in DMSetUp_Stag) */
 53:   stag->boundaryType[0] = bndx;
 54:   stag->N[0]            = M;
 55:   stag->nRanks[0]       = size;
 56:   stag->stencilType     = stencilType;
 57:   stag->stencilWidth    = stencilWidth;
 58:   DMStagSetDOF(*dm,dof0,dof1,0,0);
 59:   DMStagSetOwnershipRanges(*dm,lx,NULL,NULL);

 61:   return(0);
 62: }

 64: PETSC_INTERN PetscErrorCode DMStagSetUniformCoordinatesExplicit_1d(DM dm,PetscReal xmin,PetscReal xmax)
 65: {
 67:   DM_Stag        *stagCoord;
 68:   DM             dmCoord;
 69:   Vec            coordLocal,coord;
 70:   PetscReal      h,min;
 71:   PetscScalar    **arr;
 72:   PetscInt       ind,start,n,nExtra,s;
 73:   PetscInt       ileft,ielement;

 76:   DMGetCoordinateDM(dm, &dmCoord);
 77:   stagCoord = (DM_Stag*) dmCoord->data;
 78:   for (s=0; s<2; ++s) {
 79:     if (stagCoord->dof[s] !=0 && stagCoord->dof[s] != 1) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Coordinate DM in 1 dimensions must have 0 or 1 dof on each stratum, but stratum %d has %d dof",s,stagCoord->dof[s]);
 80:   }
 81:   DMGetLocalVector(dmCoord,&coordLocal);

 83:   DMStagVecGetArrayDOF(dmCoord,coordLocal,&arr);
 84:   if (stagCoord->dof[0]) {
 85:     DMStagGetLocationSlot(dmCoord,DMSTAG_LEFT,0,&ileft);
 86:   }
 87:   if (stagCoord->dof[1]) {
 88:     DMStagGetLocationSlot(dmCoord,DMSTAG_ELEMENT,0,&ielement);
 89:   }
 90:   DMStagGetCorners(dmCoord,&start,NULL,NULL,&n,NULL,NULL,&nExtra,NULL,NULL);

 92:   min = xmin;
 93:   h = (xmax-xmin)/stagCoord->N[0];

 95:   for(ind=start; ind<start + n + nExtra; ++ind) {
 96:     if (stagCoord->dof[0]) {
 97:       const PetscReal off = 0.0;
 98:         arr[ind][ileft] = min + ((PetscReal)ind + off) * h;
 99:     }
100:     if (stagCoord->dof[1]) {
101:       const PetscReal off = 0.5;
102:         arr[ind][ielement] = min + ((PetscReal)ind + off) * h;
103:     }
104:   }
105:   DMStagVecRestoreArrayDOF(dmCoord,coordLocal,&arr);
106:   DMCreateGlobalVector(dmCoord,&coord);
107:   DMLocalToGlobalBegin(dmCoord,coordLocal,INSERT_VALUES,coord);
108:   DMLocalToGlobalEnd(dmCoord,coordLocal,INSERT_VALUES,coord);
109:   DMSetCoordinates(dm,coord);
110:   PetscLogObjectParent((PetscObject)dm,(PetscObject)coord);
111:   VecDestroy(&coord);
112:   DMRestoreLocalVector(dmCoord,&coordLocal);
113:   return(0);
114: }

116: /* Helper functions used in DMSetUp_Stag() */
117: static PetscErrorCode DMStagComputeLocationOffsets_1d(DM);

119: PETSC_INTERN PetscErrorCode DMSetUp_Stag_1d(DM dm)
120: {
121:   PetscErrorCode  ierr;
122:   DM_Stag * const stag = (DM_Stag*)dm->data;
123:   PetscMPIInt     size,rank;
124:   MPI_Comm        comm;
125:   PetscInt        j;

128:   PetscObjectGetComm((PetscObject)dm,&comm);
129:   MPI_Comm_size(comm,&size);
130:   MPI_Comm_rank(comm,&rank);

132:   /* Check Global size */
133:   if (stag->N[0] < 1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Global grid size of %D < 1 specified",stag->N[0]);

135:   /* Local sizes */
136:   if (stag->N[0] < size) SETERRQ2(comm,PETSC_ERR_ARG_OUTOFRANGE,"More ranks (%d) than elements (%D) specified",size,stag->N[0]);
137:   if (!stag->l[0]) {
138:     /* Divide equally, giving an extra elements to higher ranks */
139:     PetscMalloc1(stag->nRanks[0],&stag->l[0]);
140:     for (j=0; j<stag->nRanks[0]; ++j) stag->l[0][j] = stag->N[0]/stag->nRanks[0] + (stag->N[0] % stag->nRanks[0] > j ? 1 : 0);
141:   }
142:   {
143:     PetscInt Nchk = 0;
144:     for (j=0; j<size; ++j) Nchk += stag->l[0][j];
145:     if (Nchk != stag->N[0]) SETERRQ2(comm,PETSC_ERR_ARG_OUTOFRANGE,"Sum of specified local sizes (%D) is not equal to global size (%D)",Nchk,stag->N[0]);
146:   }
147:   stag->n[0] = stag->l[0][rank];

149:   /* Rank (trivial in 1d) */
150:   stag->rank[0]      = rank;
151:   stag->firstRank[0] = (PetscBool)(rank == 0);
152:   stag->lastRank[0]  = (PetscBool)(rank == size-1);

154:   /* Local (unghosted) numbers of entries */
155:   stag->entriesPerElement = stag->dof[0] + stag->dof[1];
156:   switch (stag->boundaryType[0]) {
157:     case DM_BOUNDARY_NONE:
158:     case DM_BOUNDARY_GHOSTED:  stag->entries = stag->n[0] * stag->entriesPerElement + (stag->lastRank[0] ?  stag->dof[0] : 0); break;
159:     case DM_BOUNDARY_PERIODIC: stag->entries = stag->n[0] * stag->entriesPerElement;                                           break;
160:     default: SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);
161:   }

163:   /* Starting element */
164:   stag->start[0] = 0;
165:   for(j=0; j<stag->rank[0]; ++j) stag->start[0] += stag->l[0][j];

167:   /* Local/ghosted size and starting element */
168:   switch (stag->boundaryType[0]) {
169:     case DM_BOUNDARY_NONE :
170:       switch (stag->stencilType) {
171:         case DMSTAG_STENCIL_NONE : /* Only dummy cells on the right */
172:           stag->startGhost[0] = stag->start[0];
173:           stag->nGhost[0]     = stag->n[0] + (stag->lastRank[0] ? 1 : 0);
174:           break;
175:         case DMSTAG_STENCIL_STAR :
176:         case DMSTAG_STENCIL_BOX :
177:           stag->startGhost[0] = stag->firstRank[0] ? stag->start[0]: stag->start[0] - stag->stencilWidth;
178:           stag->nGhost[0] = stag->n[0];
179:           stag->nGhost[0] += stag->firstRank[0] ? 0 : stag->stencilWidth;
180:           stag->nGhost[0] += stag->lastRank[0]  ? 1 : stag->stencilWidth;
181:           break;
182:         default :
183:           SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unrecognized ghost stencil type %d",stag->stencilType);
184:       }
185:       break;
186:     case DM_BOUNDARY_GHOSTED:
187:     case DM_BOUNDARY_PERIODIC:
188:       switch (stag->stencilType) {
189:         case DMSTAG_STENCIL_NONE :
190:           stag->startGhost[0] = stag->start[0];
191:           stag->nGhost[0]     = stag->n[0];
192:           break;
193:         case DMSTAG_STENCIL_STAR :
194:         case DMSTAG_STENCIL_BOX :
195:           stag->startGhost[0] = stag->start[0] - stag->stencilWidth; /* Note that this value may be negative */
196:           stag->nGhost[0] = stag->n[0] + 2*stag->stencilWidth;
197:           break;
198:         default :
199:           SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unrecognized ghost stencil type %d",stag->stencilType);
200:       }
201:       break;
202:     default :
203:       SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);
204:   }

206:   /* Total size of ghosted/local represention */
207:   stag->entriesGhost = stag->nGhost[0]*stag->entriesPerElement;

209:   /* Define neighbors */
210:   PetscMalloc1(3,&stag->neighbors);
211:   if (stag->firstRank[0]) {
212:     switch (stag->boundaryType[0]) {
213:       case DM_BOUNDARY_GHOSTED:
214:       case DM_BOUNDARY_NONE:     stag->neighbors[0] = -1;                break;
215:       case DM_BOUNDARY_PERIODIC: stag->neighbors[0] = stag->nRanks[0]-1; break;
216:       default : SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);
217:     }
218:   } else {
219:     stag->neighbors[0] = stag->rank[0]-1;
220:   }
221:   stag->neighbors[1] = stag->rank[0];
222:   if (stag->lastRank[0]) {
223:     switch (stag->boundaryType[0]) {
224:       case DM_BOUNDARY_GHOSTED:
225:       case DM_BOUNDARY_NONE:     stag->neighbors[2] = -1;                break;
226:       case DM_BOUNDARY_PERIODIC: stag->neighbors[2] = 0;                 break;
227:       default : SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);
228:     }
229:   } else {
230:     stag->neighbors[2] = stag->rank[0]+1;
231:   }

233:   if (stag->n[0] < stag->stencilWidth) {
234:     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"DMStag 1d setup does not support local sizes (%d) smaller than the elementwise stencil width (%d)",stag->n[0],stag->stencilWidth);
235:   }

237:   /* Create global->local VecScatter and ISLocalToGlobalMapping */
238:   {
239:     PetscInt *idxLocal,*idxGlobal,*idxGlobalAll;
240:     PetscInt i,iLocal,d,entriesToTransferTotal,ghostOffsetStart,ghostOffsetEnd,nNonDummyGhost;
241:     IS       isLocal,isGlobal;

243:     /* The offset on the right (may not be equal to the stencil width, as we
244:        always have at least one ghost element, to account for the boundary
245:        point, and may with ghosted boundaries), and the number of non-dummy ghost elements */
246:     ghostOffsetStart = stag->start[0] - stag->startGhost[0];
247:     ghostOffsetEnd   = stag->startGhost[0]+stag->nGhost[0] - (stag->start[0]+stag->n[0]);
248:     nNonDummyGhost   = stag->nGhost[0] - (stag->lastRank[0] ? ghostOffsetEnd : 0) - (stag->firstRank[0] ? ghostOffsetStart : 0);

250:     /* Compute the number of non-dummy entries in the local representation
251:        This is equal to the number of non-dummy elements in the local (ghosted) representation,
252:        plus some extra entries on the right boundary on the last rank*/
253:     switch (stag->boundaryType[0]) {
254:       case DM_BOUNDARY_GHOSTED:
255:       case DM_BOUNDARY_NONE:
256:         entriesToTransferTotal = nNonDummyGhost * stag->entriesPerElement + (stag->lastRank[0] ? stag->dof[0] : 0);
257:         break;
258:       case DM_BOUNDARY_PERIODIC:
259:         entriesToTransferTotal = stag->entriesGhost; /* No dummy points */
260:         break;
261:       default :
262:         SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);
263:     }

265:     PetscMalloc1(entriesToTransferTotal,&idxLocal);
266:     PetscMalloc1(entriesToTransferTotal,&idxGlobal);
267:     PetscMalloc1(stag->entriesGhost,&idxGlobalAll);
268:     if (stag->boundaryType[0] == DM_BOUNDARY_NONE) {
269:       PetscInt count = 0,countAll = 0;
270:       /* Left ghost points and native points */
271:       for (i=stag->startGhost[0], iLocal=0; iLocal<nNonDummyGhost; ++i,++iLocal) {
272:         for (d=0; d<stag->entriesPerElement; ++d,++count,++countAll) {
273:           idxLocal [count]       = iLocal * stag->entriesPerElement + d;
274:           idxGlobal[count]       = i      * stag->entriesPerElement + d;
275:           idxGlobalAll[countAll] = i      * stag->entriesPerElement + d;
276:         }
277:       }
278:       /* Ghost points on the right
279:          Special case for last (partial dummy) element on the last rank */
280:       if (stag->lastRank[0] ) {
281:         i      = stag->N[0];
282:         iLocal = (stag->nGhost[0]-ghostOffsetEnd);
283:         /* Only vertex (0-cell) dofs in global representation */
284:         for (d=0; d<stag->dof[0]; ++d,++count,++countAll) {
285:           idxGlobal[count]       = i      * stag->entriesPerElement + d;
286:           idxLocal [count]       = iLocal * stag->entriesPerElement + d;
287:           idxGlobalAll[countAll] = i      * stag->entriesPerElement + d;
288:         }
289:         for (d=stag->dof[0]; d<stag->entriesPerElement; ++d,++countAll) { /* Additional dummy entries */
290:           idxGlobalAll[countAll] = -1;
291:         }
292:       }
293:     } else if (stag->boundaryType[0] == DM_BOUNDARY_PERIODIC) {
294:       PetscInt count = 0,iLocal = 0; /* No dummy points, so idxGlobal and idxGlobalAll are identical */
295:       const PetscInt iMin = stag->firstRank[0] ? stag->start[0] : stag->startGhost[0];
296:       const PetscInt iMax = stag->lastRank[0] ? stag->startGhost[0] + stag->nGhost[0] - stag->stencilWidth : stag->startGhost[0] + stag->nGhost[0];
297:       /* Ghost points on the left */
298:       if (stag->firstRank[0]) {
299:         for (i=stag->N[0]-stag->stencilWidth; iLocal<stag->stencilWidth; ++i,++iLocal) {
300:           for (d=0; d<stag->entriesPerElement; ++d,++count) {
301:             idxGlobal[count] = i      * stag->entriesPerElement + d;
302:             idxLocal [count] = iLocal * stag->entriesPerElement + d;
303:             idxGlobalAll[count] = idxGlobal[count];
304:           }
305:         }
306:       }
307:       /* Native points */
308:       for (i=iMin; i<iMax; ++i,++iLocal) {
309:         for (d=0; d<stag->entriesPerElement; ++d,++count) {
310:           idxGlobal[count] = i      * stag->entriesPerElement + d;
311:           idxLocal [count] = iLocal * stag->entriesPerElement + d;
312:           idxGlobalAll[count] = idxGlobal[count];
313:         }
314:       }
315:       /* Ghost points on the right */
316:       if (stag->lastRank[0]) {
317:         for (i=0; iLocal<stag->nGhost[0]; ++i,++iLocal) {
318:           for (d=0; d<stag->entriesPerElement; ++d,++count) {
319:             idxGlobal[count] = i      * stag->entriesPerElement + d;
320:             idxLocal [count] = iLocal * stag->entriesPerElement + d;
321:             idxGlobalAll[count] = idxGlobal[count];
322:           }
323:         }
324:       }
325:     } else if (stag->boundaryType[0] == DM_BOUNDARY_GHOSTED) {
326:       PetscInt count = 0,countAll = 0;
327:       /* Dummy elements on the left, on the first rank */
328:       if (stag->firstRank[0]) {
329:         for(iLocal=0; iLocal<ghostOffsetStart; ++iLocal) {
330:           /* Complete elements full of dummy entries */
331:           for (d=0; d<stag->entriesPerElement; ++d,++countAll) {
332:             idxGlobalAll[countAll] = -1;
333:           }
334:         }
335:         i = 0; /* nonDummy entries start with global entry 0 */
336:       } else {
337:         /* nonDummy entries start as usual */
338:         i = stag->startGhost[0];
339:         iLocal = 0;
340:       }

342:       /* non-Dummy entries */
343:       {
344:         PetscInt iLocalNonDummyMax = stag->firstRank[0] ? nNonDummyGhost + ghostOffsetStart : nNonDummyGhost;
345:         for (; iLocal<iLocalNonDummyMax; ++i,++iLocal) {
346:           for (d=0; d<stag->entriesPerElement; ++d,++count,++countAll) {
347:             idxLocal [count]       = iLocal * stag->entriesPerElement + d;
348:             idxGlobal[count]       = i      * stag->entriesPerElement + d;
349:             idxGlobalAll[countAll] = i      * stag->entriesPerElement + d;
350:           }
351:         }
352:       }

354:       /* (partial) dummy elements on the right, on the last rank */
355:       if (stag->lastRank[0]) {
356:         /* First one is partial dummy */
357:         i      = stag->N[0];
358:         iLocal = (stag->nGhost[0]-ghostOffsetEnd);
359:         for (d=0; d<stag->dof[0]; ++d,++count,++countAll) { /* Only vertex (0-cell) dofs in global representation */
360:           idxLocal [count]       = iLocal * stag->entriesPerElement + d;
361:           idxGlobal[count]       = i      * stag->entriesPerElement + d;
362:           idxGlobalAll[countAll] = i      * stag->entriesPerElement + d;
363:         }
364:         for (d=stag->dof[0]; d<stag->entriesPerElement; ++d,++countAll) { /* Additional dummy entries */
365:           idxGlobalAll[countAll] = -1;
366:         }
367:         for (iLocal = stag->nGhost[0] - ghostOffsetEnd + 1; iLocal < stag->nGhost[0]; ++iLocal) {
368:           /* Additional dummy elements */
369:           for (d=0; d<stag->entriesPerElement; ++d,++countAll) {
370:             idxGlobalAll[countAll] = -1;
371:           }
372:         }
373:       }
374:     } else SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Unsupported x boundary type %s",DMBoundaryTypes[stag->boundaryType[0]]);

376:     /* Create Local IS (transferring pointer ownership) */
377:     ISCreateGeneral(PetscObjectComm((PetscObject)dm),entriesToTransferTotal,idxLocal,PETSC_OWN_POINTER,&isLocal);

379:     /* Create Global IS (transferring pointer ownership) */
380:     ISCreateGeneral(PetscObjectComm((PetscObject)dm),entriesToTransferTotal,idxGlobal,PETSC_OWN_POINTER,&isGlobal);

382:     /* Create stag->gtol, which doesn't include dummy entries */
383:     {
384:       Vec local,global;
385:       VecCreateMPIWithArray(PetscObjectComm((PetscObject)dm),1,stag->entries,PETSC_DECIDE,NULL,&global);
386:       VecCreateSeqWithArray(PETSC_COMM_SELF,stag->entriesPerElement,stag->entriesGhost,NULL,&local);
387:       VecScatterCreate(global,isGlobal,local,isLocal,&stag->gtol);
388:       VecDestroy(&global);
389:       VecDestroy(&local);
390:     }

392:     /* Destroy ISs */
393:     ISDestroy(&isLocal);
394:     ISDestroy(&isGlobal);

396:     /* Create local-to-global map (transferring pointer ownership) */
397:     ISLocalToGlobalMappingCreate(comm,1,stag->entriesGhost,idxGlobalAll,PETSC_OWN_POINTER,&dm->ltogmap);
398:     PetscLogObjectParent((PetscObject)dm,(PetscObject)dm->ltogmap);
399:   }

401:   /* Precompute location offsets */
402:   DMStagComputeLocationOffsets_1d(dm);

404:   /* View from Options */
405:   DMViewFromOptions(dm,NULL,"-dm_view");

407:  return(0);
408: }

410: static PetscErrorCode DMStagComputeLocationOffsets_1d(DM dm)
411: {
412:   PetscErrorCode  ierr;
413:   DM_Stag * const stag = (DM_Stag*)dm->data;
414:   const PetscInt epe = stag->entriesPerElement;

417:   PetscMalloc1(DMSTAG_NUMBER_LOCATIONS,&stag->locationOffsets);
418:   stag->locationOffsets[DMSTAG_LEFT]    = 0;
419:   stag->locationOffsets[DMSTAG_ELEMENT] = stag->locationOffsets[DMSTAG_LEFT] + stag->dof[0];
420:   stag->locationOffsets[DMSTAG_RIGHT]   = stag->locationOffsets[DMSTAG_LEFT] + epe;
421:   return(0);
422: }