Actual source code: stagutils.c

  1: /* Additional functions in the DMStag API, which are not part of the general DM API. */
  2: #include <petsc/private/dmstagimpl.h>
  3: #include <petscdmproduct.h>

  5: PetscErrorCode DMRestrictHook_Coordinates(DM, DM, void *);

  7: /*@C
  8:   DMStagGetBoundaryTypes - get boundary types

 10:   Not Collective

 12:   Input Parameter:
 13: . dm - the `DMSTAG` object

 15:   Output Parameters:
 16: + boundaryTypeX - boundary type for x direction
 17: . boundaryTypeY - boundary type for y direction, not set for one dimensional problems
 18: - boundaryTypeZ - boundary type for z direction, not set for one and two dimensional problems

 20:   Level: intermediate

 22: .seealso: [](ch_stag), `DMSTAG`, `DMBoundaryType``
 23: @*/
 24: PetscErrorCode DMStagGetBoundaryTypes(DM dm, DMBoundaryType *boundaryTypeX, DMBoundaryType *boundaryTypeY, DMBoundaryType *boundaryTypeZ)
 25: {
 26:   const DM_Stag *const stag = (DM_Stag *)dm->data;
 27:   PetscInt             dim;

 29:   PetscFunctionBegin;
 31:   PetscCall(DMGetDimension(dm, &dim));
 32:   if (boundaryTypeX) *boundaryTypeX = stag->boundaryType[0];
 33:   if (boundaryTypeY && dim > 1) *boundaryTypeY = stag->boundaryType[1];
 34:   if (boundaryTypeZ && dim > 2) *boundaryTypeZ = stag->boundaryType[2];
 35:   PetscFunctionReturn(PETSC_SUCCESS);
 36: }

 38: static PetscErrorCode DMStagGetProductCoordinateArrays_Private(DM dm, void *arrX, void *arrY, void *arrZ, PetscBool read)
 39: {
 40:   PetscInt  dim, d, dofCheck[DMSTAG_MAX_STRATA], s;
 41:   DM        dmCoord;
 42:   void     *arr[DMSTAG_MAX_DIM];
 43:   PetscBool checkDof;

 45:   PetscFunctionBegin;
 47:   PetscCall(DMGetDimension(dm, &dim));
 48:   PetscCheck(dim <= DMSTAG_MAX_DIM, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimensions", dim);
 49:   arr[0] = arrX;
 50:   arr[1] = arrY;
 51:   arr[2] = arrZ;
 52:   PetscCall(DMGetCoordinateDM(dm, &dmCoord));
 53:   PetscCheck(dmCoord, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM does not have a coordinate DM");
 54:   {
 55:     PetscBool isProduct;
 56:     DMType    dmType;
 57:     PetscCall(DMGetType(dmCoord, &dmType));
 58:     PetscCall(PetscStrcmp(DMPRODUCT, dmType, &isProduct));
 59:     PetscCheck(isProduct, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate DM is not of type DMPRODUCT");
 60:   }
 61:   for (s = 0; s < DMSTAG_MAX_STRATA; ++s) dofCheck[s] = 0;
 62:   checkDof = PETSC_FALSE;
 63:   for (d = 0; d < dim; ++d) {
 64:     DM        subDM;
 65:     DMType    dmType;
 66:     PetscBool isStag;
 67:     PetscInt  dof[DMSTAG_MAX_STRATA], subDim;
 68:     Vec       coord1d_local;

 70:     /* Ignore unrequested arrays */
 71:     if (!arr[d]) continue;

 73:     PetscCall(DMProductGetDM(dmCoord, d, &subDM));
 74:     PetscCheck(subDM, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate DM is missing sub DM %" PetscInt_FMT, d);
 75:     PetscCall(DMGetDimension(subDM, &subDim));
 76:     PetscCheck(subDim == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DM is not of dimension 1");
 77:     PetscCall(DMGetType(subDM, &dmType));
 78:     PetscCall(PetscStrcmp(DMSTAG, dmType, &isStag));
 79:     PetscCheck(isStag, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DM is not of type DMSTAG");
 80:     PetscCall(DMStagGetDOF(subDM, &dof[0], &dof[1], &dof[2], &dof[3]));
 81:     if (!checkDof) {
 82:       for (s = 0; s < DMSTAG_MAX_STRATA; ++s) dofCheck[s] = dof[s];
 83:       checkDof = PETSC_TRUE;
 84:     } else {
 85:       for (s = 0; s < DMSTAG_MAX_STRATA; ++s) PetscCheck(dofCheck[s] == dof[s], PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DMs have different dofs");
 86:     }
 87:     PetscCall(DMGetCoordinatesLocal(subDM, &coord1d_local));
 88:     if (read) {
 89:       PetscCall(DMStagVecGetArrayRead(subDM, coord1d_local, arr[d]));
 90:     } else {
 91:       PetscCall(DMStagVecGetArray(subDM, coord1d_local, arr[d]));
 92:     }
 93:   }
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 95: }

 97: /*@C
 98:   DMStagGetProductCoordinateArrays - extract local product coordinate arrays, one per dimension

100:   Logically Collective

102:   Input Parameter:
103: . dm - the `DMSTAG` object

105:   Output Parameters:
106: + arrX - local 1D coordinate arrays for x direction
107: . arrY - local 1D coordinate arrays for y direction, not set for one dimensional problems
108: - arrZ - local 1D coordinate arrays for z direction, not set for one and two dimensional problems

110:   Level: intermediate

112:   Notes:
113:   A high-level helper function to quickly extract local coordinate arrays.

115:   Note that 2-dimensional arrays are returned. See
116:   `DMStagVecGetArray()`, which is called internally to produce these arrays
117:   representing coordinates on elements and vertices (element boundaries)
118:   for a 1-dimensional `DMSTAG` in each coordinate direction.

120:   One should use `DMStagGetProductCoordinateLocationSlot()` to determine appropriate
121:   indices for the second dimension in these returned arrays. This function
122:   checks that the coordinate array is a suitable product of 1-dimensional
123:   `DMSTAG` objects.

125: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMStagGetProductCoordinateArraysRead()`, `DMStagSetUniformCoordinates()`, `DMStagSetUniformCoordinatesProduct()`, `DMStagGetProductCoordinateLocationSlot()`
126: @*/
127: PetscErrorCode DMStagGetProductCoordinateArrays(DM dm, void *arrX, void *arrY, void *arrZ)
128: {
129:   PetscFunctionBegin;
130:   PetscCall(DMStagGetProductCoordinateArrays_Private(dm, arrX, arrY, arrZ, PETSC_FALSE));
131:   PetscFunctionReturn(PETSC_SUCCESS);
132: }

134: /*@C
135:   DMStagGetProductCoordinateArraysRead - extract product coordinate arrays, read-only

137:   Logically Collective

139:   Input Parameter:
140: . dm - the `DMSTAG` object

142:   Output Parameters:
143: + arrX - local 1D coordinate arrays for x direction
144: . arrY - local 1D coordinate arrays for y direction, not set for one dimensional problems
145: - arrZ - local 1D coordinate arrays for z direction, not set for one and two dimensional problems

147:   Level: intermediate

149:   Note:
150:   See `DMStagGetProductCoordinateArrays()` for more information.

152: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMStagGetProductCoordinateArrays()`, `DMStagSetUniformCoordinates()`, `DMStagSetUniformCoordinatesProduct()`, `DMStagGetProductCoordinateLocationSlot()`
153: @*/
154: PetscErrorCode DMStagGetProductCoordinateArraysRead(DM dm, void *arrX, void *arrY, void *arrZ)
155: {
156:   PetscFunctionBegin;
157:   PetscCall(DMStagGetProductCoordinateArrays_Private(dm, arrX, arrY, arrZ, PETSC_TRUE));
158:   PetscFunctionReturn(PETSC_SUCCESS);
159: }

161: /*@C
162:   DMStagGetProductCoordinateLocationSlot - get slot for use with local product coordinate arrays

164:   Not Collective

166:   Input Parameters:
167: + dm - the `DMSTAG` object
168: - loc - the grid location

170:   Output Parameter:
171: . slot - the index to use in local arrays

173:   Level: intermediate

175:   Notes:
176:   High-level helper function to get slot indices for 1D coordinate `DM`s,
177:   for use with `DMStagGetProductCoordinateArrays()` and related functions.

179:   For `loc`, one should use `DMSTAG_LEFT`, `DMSTAG_ELEMENT`, or `DMSTAG_RIGHT` for "previous", "center" and "next"
180:   locations, respectively, in each dimension.
181:   One can equivalently use `DMSTAG_DOWN` or `DMSTAG_BACK` in place of `DMSTAG_LEFT`,
182:   and `DMSTAG_UP` or `DMSTACK_FRONT` in place of `DMSTAG_RIGHT`;

184:   This function checks that the coordinates are actually set up so that using the
185:   slots from any of the 1D coordinate sub-`DM`s are valid for all the 1D coordinate sub-`DM`s.

187: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMStagGetProductCoordinateArrays()`, `DMStagGetProductCoordinateArraysRead()`, `DMStagSetUniformCoordinates()`
188: @*/
189: PETSC_EXTERN PetscErrorCode DMStagGetProductCoordinateLocationSlot(DM dm, DMStagStencilLocation loc, PetscInt *slot)
190: {
191:   DM       dmCoord;
192:   PetscInt dim, dofCheck[DMSTAG_MAX_STRATA], s, d;

194:   PetscFunctionBegin;
196:   PetscCall(DMGetDimension(dm, &dim));
197:   PetscCall(DMGetCoordinateDM(dm, &dmCoord));
198:   PetscCheck(dmCoord, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM does not have a coordinate DM");
199:   {
200:     PetscBool isProduct;
201:     DMType    dmType;
202:     PetscCall(DMGetType(dmCoord, &dmType));
203:     PetscCall(PetscStrcmp(DMPRODUCT, dmType, &isProduct));
204:     PetscCheck(isProduct, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate DM is not of type DMPRODUCT");
205:   }
206:   for (s = 0; s < DMSTAG_MAX_STRATA; ++s) dofCheck[s] = 0;
207:   for (d = 0; d < dim; ++d) {
208:     DM        subDM;
209:     DMType    dmType;
210:     PetscBool isStag;
211:     PetscInt  dof[DMSTAG_MAX_STRATA], subDim;
212:     PetscCall(DMProductGetDM(dmCoord, d, &subDM));
213:     PetscCheck(subDM, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate DM is missing sub DM %" PetscInt_FMT, d);
214:     PetscCall(DMGetDimension(subDM, &subDim));
215:     PetscCheck(subDim == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DM is not of dimension 1");
216:     PetscCall(DMGetType(subDM, &dmType));
217:     PetscCall(PetscStrcmp(DMSTAG, dmType, &isStag));
218:     PetscCheck(isStag, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DM is not of type DMSTAG");
219:     PetscCall(DMStagGetDOF(subDM, &dof[0], &dof[1], &dof[2], &dof[3]));
220:     if (d == 0) {
221:       const PetscInt component = 0;
222:       for (s = 0; s < DMSTAG_MAX_STRATA; ++s) dofCheck[s] = dof[s];
223:       PetscCall(DMStagGetLocationSlot(subDM, loc, component, slot));
224:     } else {
225:       for (s = 0; s < DMSTAG_MAX_STRATA; ++s) PetscCheck(dofCheck[s] == dof[s], PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Coordinate sub-DMs have different dofs");
226:     }
227:   }
228:   PetscFunctionReturn(PETSC_SUCCESS);
229: }

231: /*@C
232:   DMStagGetCorners - return global element indices of the local region (excluding ghost points)

234:   Not Collective

236:   Input Parameter:
237: . dm - the `DMSTAG` object

239:   Output Parameters:
240: + x     - starting element index in first direction
241: . y     - starting element index in second direction
242: . z     - starting element index in third direction
243: . m     - element width in first direction
244: . n     - element width in second direction
245: . p     - element width in third direction
246: . nExtrax - number of extra partial elements in first direction
247: . nExtray - number of extra partial elements in second direction
248: - nExtraz - number of extra partial elements in third direction

250:   Level: beginner

252:   Notes:
253:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to NULL in this case.

255:   The number of extra partial elements is either 1 or 0.
256:   The value is 1 on right, top, and front non-periodic domain ("physical") boundaries,
257:   in the x, y, and z directions respectively, and otherwise 0.

259: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetGhostCorners()`, `DMDAGetCorners()`
260: @*/
261: PetscErrorCode DMStagGetCorners(DM dm, PetscInt *x, PetscInt *y, PetscInt *z, PetscInt *m, PetscInt *n, PetscInt *p, PetscInt *nExtrax, PetscInt *nExtray, PetscInt *nExtraz)
262: {
263:   const DM_Stag *const stag = (DM_Stag *)dm->data;

265:   PetscFunctionBegin;
267:   if (x) *x = stag->start[0];
268:   if (y) *y = stag->start[1];
269:   if (z) *z = stag->start[2];
270:   if (m) *m = stag->n[0];
271:   if (n) *n = stag->n[1];
272:   if (p) *p = stag->n[2];
273:   if (nExtrax) *nExtrax = stag->boundaryType[0] != DM_BOUNDARY_PERIODIC && stag->lastRank[0] ? 1 : 0;
274:   if (nExtray) *nExtray = stag->boundaryType[1] != DM_BOUNDARY_PERIODIC && stag->lastRank[1] ? 1 : 0;
275:   if (nExtraz) *nExtraz = stag->boundaryType[2] != DM_BOUNDARY_PERIODIC && stag->lastRank[2] ? 1 : 0;
276:   PetscFunctionReturn(PETSC_SUCCESS);
277: }

279: /*@C
280:   DMStagGetDOF - get number of DOF associated with each stratum of the grid

282:   Not Collective

284:   Input Parameter:
285: . dm - the `DMSTAG` object

287:   Output Parameters:
288: + dof0 - the number of points per 0-cell (vertex/node)
289: . dof1 - the number of points per 1-cell (element in 1D, edge in 2D and 3D)
290: . dof2 - the number of points per 2-cell (element in 2D, face in 3D)
291: - dof3 - the number of points per 3-cell (element in 3D)

293:   Level: beginner

295: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetCorners()`, `DMStagGetGhostCorners()`, `DMStagGetGlobalSizes()`, `DMStagGetStencilWidth()`, `DMStagGetBoundaryTypes()`, `DMStagGetLocationDOF()`, `DMDAGetDof()`
296: @*/
297: PetscErrorCode DMStagGetDOF(DM dm, PetscInt *dof0, PetscInt *dof1, PetscInt *dof2, PetscInt *dof3)
298: {
299:   const DM_Stag *const stag = (DM_Stag *)dm->data;

301:   PetscFunctionBegin;
303:   if (dof0) *dof0 = stag->dof[0];
304:   if (dof1) *dof1 = stag->dof[1];
305:   if (dof2) *dof2 = stag->dof[2];
306:   if (dof3) *dof3 = stag->dof[3];
307:   PetscFunctionReturn(PETSC_SUCCESS);
308: }

310: /*@C
311:   DMStagGetGhostCorners - return global element indices of the local region, including ghost points

313:   Not Collective

315:   Input Parameter:
316: . dm - the `DMSTAG` object

318:   Output Parameters:
319: + x - the starting element index in the first direction
320: . y - the starting element index in the second direction
321: . z - the starting element index in the third direction
322: . m - the element width in the first direction
323: . n - the element width in the second direction
324: - p - the element width in the third direction

326:   Level: beginner

328:   Note:
329:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

331: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetCorners()`, `DMDAGetGhostCorners()`
332: @*/
333: PetscErrorCode DMStagGetGhostCorners(DM dm, PetscInt *x, PetscInt *y, PetscInt *z, PetscInt *m, PetscInt *n, PetscInt *p)
334: {
335:   const DM_Stag *const stag = (DM_Stag *)dm->data;

337:   PetscFunctionBegin;
339:   if (x) *x = stag->startGhost[0];
340:   if (y) *y = stag->startGhost[1];
341:   if (z) *z = stag->startGhost[2];
342:   if (m) *m = stag->nGhost[0];
343:   if (n) *n = stag->nGhost[1];
344:   if (p) *p = stag->nGhost[2];
345:   PetscFunctionReturn(PETSC_SUCCESS);
346: }

348: /*@C
349:   DMStagGetGlobalSizes - get global element counts

351:   Not Collective

353:   Input Parameter:
354: . dm - the `DMSTAG` object

356:   Output Parameters:
357: + M - global element counts in the x direction
358: . N - global element counts in the y direction
359: - P - global element counts in the z direction

361:   Level: beginner

363:   Note:
364:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

366: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetLocalSizes()`, `DMDAGetInfo()`
367: @*/
368: PetscErrorCode DMStagGetGlobalSizes(DM dm, PetscInt *M, PetscInt *N, PetscInt *P)
369: {
370:   const DM_Stag *const stag = (DM_Stag *)dm->data;

372:   PetscFunctionBegin;
374:   if (M) *M = stag->N[0];
375:   if (N) *N = stag->N[1];
376:   if (P) *P = stag->N[2];
377:   PetscFunctionReturn(PETSC_SUCCESS);
378: }

380: /*@C
381:   DMStagGetIsFirstRank - get boolean value for whether this rank is first in each direction in the rank grid

383:   Not Collective

385:   Input Parameter:
386: . dm - the `DMSTAG` object

388:   Output Parameters:
389: + isFirstRank0 - whether this rank is first in the x direction
390: . isFirstRank1 - whether this rank is first in the y direction
391: - isFirstRank2 - whether this rank is first in the z direction

393:   Level: intermediate

395:   Note:
396:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

398: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetIsLastRank()`
399: @*/
400: PetscErrorCode DMStagGetIsFirstRank(DM dm, PetscBool *isFirstRank0, PetscBool *isFirstRank1, PetscBool *isFirstRank2)
401: {
402:   const DM_Stag *const stag = (DM_Stag *)dm->data;

404:   PetscFunctionBegin;
406:   if (isFirstRank0) *isFirstRank0 = stag->firstRank[0];
407:   if (isFirstRank1) *isFirstRank1 = stag->firstRank[1];
408:   if (isFirstRank2) *isFirstRank2 = stag->firstRank[2];
409:   PetscFunctionReturn(PETSC_SUCCESS);
410: }

412: /*@C
413:   DMStagGetIsLastRank - get boolean value for whether this rank is last in each direction in the rank grid

415:   Not Collective

417:   Input Parameter:
418: . dm - the `DMSTAG` object

420:   Output Parameters:
421: + isFirstRank0 - whether this rank is last in the x direction
422: . isFirstRank1 - whether this rank is last in the y direction
423: - isFirstRank2 - whether this rank is last in the z direction

425:   Level: intermediate

427:   Note:
428:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

430: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetIsFirstRank()`
431: @*/
432: PetscErrorCode DMStagGetIsLastRank(DM dm, PetscBool *isLastRank0, PetscBool *isLastRank1, PetscBool *isLastRank2)
433: {
434:   const DM_Stag *const stag = (DM_Stag *)dm->data;

436:   PetscFunctionBegin;
438:   if (isLastRank0) *isLastRank0 = stag->lastRank[0];
439:   if (isLastRank1) *isLastRank1 = stag->lastRank[1];
440:   if (isLastRank2) *isLastRank2 = stag->lastRank[2];
441:   PetscFunctionReturn(PETSC_SUCCESS);
442: }

444: /*@C
445:   DMStagGetLocalSizes - get local elementwise sizes

447:   Not Collective

449:   Input Parameter:
450: . dm - the `DMSTAG` object

452:   Output Parameters:
453: + m - local element counts (excluding ghosts) in the x direction
454: . n - local element counts (excluding ghosts) in the y direction
455: - p - local element counts (excluding ghosts) in the z direction

457:   Level: beginner

459:   Note:
460:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

462: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetGlobalSizes()`, `DMStagGetDOF()`, `DMStagGetNumRanks()`, `DMDAGetLocalInfo()`
463: @*/
464: PetscErrorCode DMStagGetLocalSizes(DM dm, PetscInt *m, PetscInt *n, PetscInt *p)
465: {
466:   const DM_Stag *const stag = (DM_Stag *)dm->data;

468:   PetscFunctionBegin;
470:   if (m) *m = stag->n[0];
471:   if (n) *n = stag->n[1];
472:   if (p) *p = stag->n[2];
473:   PetscFunctionReturn(PETSC_SUCCESS);
474: }

476: /*@C
477:   DMStagGetNumRanks - get number of ranks in each direction in the global grid decomposition

479:   Not Collective

481:   Input Parameter:
482: . dm - the `DMSTAG` object

484:   Output Parameters:
485: + nRanks0 - number of ranks in the x direction in the grid decomposition
486: . nRanks1 - number of ranks in the y direction in the grid decomposition
487: - nRanks2 - number of ranks in the z direction in the grid decomposition

489:  Level: intermediate

491: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetGlobalSizes()`, `DMStagGetLocalSize()`, `DMStagSetNumRanks()`, `DMDAGetInfo()`
492: @*/
493: PetscErrorCode DMStagGetNumRanks(DM dm, PetscInt *nRanks0, PetscInt *nRanks1, PetscInt *nRanks2)
494: {
495:   const DM_Stag *const stag = (DM_Stag *)dm->data;

497:   PetscFunctionBegin;
499:   if (nRanks0) *nRanks0 = stag->nRanks[0];
500:   if (nRanks1) *nRanks1 = stag->nRanks[1];
501:   if (nRanks2) *nRanks2 = stag->nRanks[2];
502:   PetscFunctionReturn(PETSC_SUCCESS);
503: }

505: /*@C
506:   DMStagGetEntries - get number of native entries in the global representation

508:   Not Collective

510:   Input Parameter:
511: . dm - the `DMSTAG` object

513:   Output Parameter:
514: . entries - number of rank-native entries in the global representation

516:   Level: developer

518:   Note:
519:   This is the number of entries on this rank for a global vector associated with `dm`.
520:   That is, it is value of `size` returned by `VecGetLocalSize(vec,&size)` when
521:   `DMCreateGlobalVector(dm,&vec) is used to create a `Vec`. Users would typically
522:   use these functions.

524: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetDOF()`, `DMStagGetEntriesLocal()`, `DMStagGetEntriesPerElement()`, `DMCreateLocalVector()`
525: @*/
526: PetscErrorCode DMStagGetEntries(DM dm, PetscInt *entries)
527: {
528:   const DM_Stag *const stag = (DM_Stag *)dm->data;

530:   PetscFunctionBegin;
532:   if (entries) *entries = stag->entries;
533:   PetscFunctionReturn(PETSC_SUCCESS);
534: }

536: /*@C
537:   DMStagGetEntriesLocal - get number of entries in the local representation

539:   Not Collective

541:   Input Parameter:
542: . dm - the `DMSTAG` object

544:   Output Parameter:
545: . entries - number of entries in the local representation

547:   Level: developer

549:   Note:
550:   This is the number of entries on this rank in the local representation.
551:   That is, it is value of `size` returned by `VecGetSize(vec,&size)` or
552:   `VecGetLocalSize(vec,&size)` when `DMCreateLocalVector(dm,&vec)` is used to
553:   create a `Vec`. Users would typically use these functions.

555: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetDOF()`, `DMStagGetEntries()`, `DMStagGetEntriesPerElement()`, `DMCreateLocalVector()`
556: @*/
557: PetscErrorCode DMStagGetEntriesLocal(DM dm, PetscInt *entries)
558: {
559:   const DM_Stag *const stag = (DM_Stag *)dm->data;

561:   PetscFunctionBegin;
563:   if (entries) *entries = stag->entriesGhost;
564:   PetscFunctionReturn(PETSC_SUCCESS);
565: }

567: /*@C
568:   DMStagGetEntriesPerElement - get number of entries per element in the local representation

570:   Not Collective

572:   Input Parameter:
573: . dm - the `DMSTAG` object

575:   Output Parameter:
576: . entriesPerElement - number of entries associated with each element in the local representation

578:   Level: developer

580:   Notes:
581:   This is the natural block size for most local operations. In 1D it is equal to `dof0` $+$ `dof1`,
582:   in 2D it is equal to `dof0` $+ 2$`dof1` $+$ `dof2`, and in 3D it is equal to `dof0` $+ 3$`dof1` $+ 3$`dof2` $+$ `dof3`

584: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetDOF()`
585: @*/
586: PetscErrorCode DMStagGetEntriesPerElement(DM dm, PetscInt *entriesPerElement)
587: {
588:   const DM_Stag *const stag = (DM_Stag *)dm->data;

590:   PetscFunctionBegin;
592:   if (entriesPerElement) *entriesPerElement = stag->entriesPerElement;
593:   PetscFunctionReturn(PETSC_SUCCESS);
594: }

596: /*@C
597:   DMStagGetStencilType - get elementwise ghost/halo stencil type

599:   Not Collective

601:   Input Parameter:
602: . dm - the `DMSTAG` object

604:   Output Parameter:
605: . stencilType - the elementwise ghost stencil type: `DMSTAG_STENCIL_BOX`, `DMSTAG_STENCIL_STAR`, or `DMSTAG_STENCIL_NONE`

607:   Level: beginner

609: .seealso: [](ch_stag), `DMSTAG`, `DMStagSetStencilType()`, `DMStagGetStencilWidth`, `DMStagStencilType`
610: @*/
611: PetscErrorCode DMStagGetStencilType(DM dm, DMStagStencilType *stencilType)
612: {
613:   DM_Stag *const stag = (DM_Stag *)dm->data;

615:   PetscFunctionBegin;
617:   *stencilType = stag->stencilType;
618:   PetscFunctionReturn(PETSC_SUCCESS);
619: }

621: /*@C
622:   DMStagGetStencilWidth - get elementwise stencil width

624:   Not Collective

626:   Input Parameter:
627: . dm - the `DMSTAG` object

629:   Output Parameter:
630: . stencilWidth - stencil/halo/ghost width in elements

632:   Level: beginner

634: .seealso: [](ch_stag), `DMSTAG`, `DMStagSetStencilWidth()`, `DMStagGetStencilType()`, `DMDAGetStencilType()`
635: @*/
636: PetscErrorCode DMStagGetStencilWidth(DM dm, PetscInt *stencilWidth)
637: {
638:   const DM_Stag *const stag = (DM_Stag *)dm->data;

640:   PetscFunctionBegin;
642:   if (stencilWidth) *stencilWidth = stag->stencilWidth;
643:   PetscFunctionReturn(PETSC_SUCCESS);
644: }

646: /*@C
647:   DMStagGetOwnershipRanges - get elements per rank in each direction

649:   Not Collective

651:   Input Parameter:
652: .     dm - the `DMSTAG` object

654:   Output Parameters:
655: +     lx - ownership along x direction (optional)
656: .     ly - ownership along y direction (optional)
657: -     lz - ownership along z direction (optional)

659:   Level: intermediate

661:   Notes:
662:   These correspond to the optional final arguments passed to `DMStagCreate1d()`, `DMStagCreate2d()`, and `DMStagCreate3d()`.

664:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

666:   In C you should not free these arrays, nor change the values in them.
667:   They will only have valid values while the `DMSTAG` they came from still exists (has not been destroyed).

669: .seealso: [](ch_stag), `DMSTAG`, `DMStagSetGlobalSizes()`, `DMStagSetOwnershipRanges()`, `DMStagCreate1d()`, `DMStagCreate2d()`, `DMStagCreate3d()`, `DMDAGetOwnershipRanges()`
670: @*/
671: PetscErrorCode DMStagGetOwnershipRanges(DM dm, const PetscInt *lx[], const PetscInt *ly[], const PetscInt *lz[])
672: {
673:   const DM_Stag *const stag = (DM_Stag *)dm->data;

675:   PetscFunctionBegin;
677:   if (lx) *lx = stag->l[0];
678:   if (ly) *ly = stag->l[1];
679:   if (lz) *lz = stag->l[2];
680:   PetscFunctionReturn(PETSC_SUCCESS);
681: }

683: /*@C
684:   DMStagCreateCompatibleDMStag - create a compatible `DMSTAG` with different dof/stratum

686:   Collective

688:   Input Parameters:
689: + dm - the `DMSTAG` object
690: . dof0 - number of dof on the first stratum in the new `DMSTAG`
691: . dof1 - number of dof on the second stratum in the new `DMSTAG`
692: . dof2 - number of dof on the third stratum in the new `DMSTAG`
693: - dof3 - number of dof on the fourth stratum in the new `DMSTAG`

695:   Output Parameter:
696: . newdm - the new, compatible `DMSTAG`

698:   Level: intermediate

700:   Notes:
701:   DOF supplied for strata too big for the dimension are ignored; these may be set to `0`.
702:   For example, for a 2-dimensional `DMSTAG`, `dof2` sets the number of dof per element,
703:   and `dof3` is unused. For a 3-dimensional `DMSTAG`, `dof3` sets the number of DOF per element.

705:   In contrast to `DMDACreateCompatibleDMDA()`, coordinates are not reused.

707: .seealso: [](ch_stag), `DMSTAG`, `DMDACreateCompatibleDMDA()`, `DMGetCompatibility()`, `DMStagMigrateVec()`
708: @*/
709: PetscErrorCode DMStagCreateCompatibleDMStag(DM dm, PetscInt dof0, PetscInt dof1, PetscInt dof2, PetscInt dof3, DM *newdm)
710: {
711:   PetscFunctionBegin;
713:   PetscCall(DMStagDuplicateWithoutSetup(dm, PetscObjectComm((PetscObject)dm), newdm));
714:   PetscCall(DMStagSetDOF(*newdm, dof0, dof1, dof2, dof3));
715:   PetscCall(DMSetUp(*newdm));
716:   PetscFunctionReturn(PETSC_SUCCESS);
717: }

719: /*@C
720:   DMStagGetLocationSlot - get index to use in accessing raw local arrays

722:   Not Collective

724:   Input Parameters:
725: + dm - the `DMSTAG` object
726: . loc - location relative to an element
727: - c - component

729:   Output Parameter:
730: . slot - index to use

732:   Level: beginner

734:   Notes:
735:   Provides an appropriate index to use with `DMStagVecGetArray()` and friends.
736:   This is required so that the user doesn't need to know about the ordering of
737:   dof associated with each local element.

739: .seealso: [](ch_stag), `DMSTAG`, `DMStagVecGetArray()`, `DMStagVecGetArrayRead()`, `DMStagGetDOF()`, `DMStagGetEntriesPerElement()`
740: @*/
741: PetscErrorCode DMStagGetLocationSlot(DM dm, DMStagStencilLocation loc, PetscInt c, PetscInt *slot)
742: {
743:   DM_Stag *const stag = (DM_Stag *)dm->data;

745:   PetscFunctionBegin;
747:   if (PetscDefined(USE_DEBUG)) {
748:     PetscInt dof;
749:     PetscCall(DMStagGetLocationDOF(dm, loc, &dof));
750:     PetscCheck(dof >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Location %s has no dof attached", DMStagStencilLocations[loc]);
751:     PetscCheck(c <= dof - 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Supplied component number (%" PetscInt_FMT ") for location %s is too big (maximum %" PetscInt_FMT ")", c, DMStagStencilLocations[loc], dof - 1);
752:   }
753:   *slot = stag->locationOffsets[loc] + c;
754:   PetscFunctionReturn(PETSC_SUCCESS);
755: }

757: /*@C
758:   DMStagMigrateVec - transfer a vector associated with a `DMSTAG` to a vector associated with a compatible `DMSTAG`

760:   Collective

762:   Input Parameters:
763: + dm - the source `DMSTAG` object
764: . vec - the source vector, compatible with `dm`
765: . dmTo - the compatible destination `DMSTAG` object
766: - vecTo - the destination vector, compatible with `dmTo`

768:   Level: advanced

770:   Notes:
771:   Extra dof are ignored, and unfilled dof are zeroed.
772:   Currently only implemented to migrate global vectors to global vectors.
773:   For the definition of compatibility of `DM`s, see `DMGetCompatibility()`.

775: .seealso: [](ch_stag), `DMSTAG`, `DMStagCreateCompatibleDMStag()`, `DMGetCompatibility()`, `DMStagVecSplitToDMDA()`
776: @*/
777: PetscErrorCode DMStagMigrateVec(DM dm, Vec vec, DM dmTo, Vec vecTo)
778: {
779:   DM_Stag *const     stag   = (DM_Stag *)dm->data;
780:   DM_Stag *const     stagTo = (DM_Stag *)dmTo->data;
781:   PetscInt           nLocalTo, nLocal, dim, i, j, k;
782:   PetscInt           start[DMSTAG_MAX_DIM], startGhost[DMSTAG_MAX_DIM], n[DMSTAG_MAX_DIM], nExtra[DMSTAG_MAX_DIM], offset[DMSTAG_MAX_DIM];
783:   Vec                vecToLocal, vecLocal;
784:   PetscBool          compatible, compatibleSet;
785:   const PetscScalar *arr;
786:   PetscScalar       *arrTo;
787:   const PetscInt     epe   = stag->entriesPerElement;
788:   const PetscInt     epeTo = stagTo->entriesPerElement;

790:   PetscFunctionBegin;
795:   PetscCall(DMGetCompatibility(dm, dmTo, &compatible, &compatibleSet));
796:   PetscCheck(compatibleSet && compatible, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "DMStag objects must be shown to be compatible");
797:   PetscCall(DMGetDimension(dm, &dim));
798:   PetscCall(VecGetLocalSize(vec, &nLocal));
799:   PetscCall(VecGetLocalSize(vecTo, &nLocalTo));
800:   PetscCheck(nLocal == stag->entries && nLocalTo == stagTo->entries, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Vector migration only implemented for global vector to global vector.");
801:   PetscCall(DMStagGetCorners(dm, &start[0], &start[1], &start[2], &n[0], &n[1], &n[2], &nExtra[0], &nExtra[1], &nExtra[2]));
802:   PetscCall(DMStagGetGhostCorners(dm, &startGhost[0], &startGhost[1], &startGhost[2], NULL, NULL, NULL));
803:   for (i = 0; i < DMSTAG_MAX_DIM; ++i) offset[i] = start[i] - startGhost[i];

805:   /* Proceed by transferring to a local vector, copying, and transferring back to a global vector */
806:   PetscCall(DMGetLocalVector(dm, &vecLocal));
807:   PetscCall(DMGetLocalVector(dmTo, &vecToLocal));
808:   PetscCall(DMGlobalToLocalBegin(dm, vec, INSERT_VALUES, vecLocal));
809:   PetscCall(DMGlobalToLocalEnd(dm, vec, INSERT_VALUES, vecLocal));
810:   PetscCall(VecGetArrayRead(vecLocal, &arr));
811:   PetscCall(VecGetArray(vecToLocal, &arrTo));
812:   /* Note that some superfluous copying of entries on partial dummy elements is done */
813:   if (dim == 1) {
814:     for (i = offset[0]; i < offset[0] + n[0] + nExtra[0]; ++i) {
815:       PetscInt d = 0, dTo = 0, b = 0, bTo = 0;
816:       PetscInt si;
817:       for (si = 0; si < 2; ++si) {
818:         b += stag->dof[si];
819:         bTo += stagTo->dof[si];
820:         for (; d < b && dTo < bTo; ++d, ++dTo) arrTo[i * epeTo + dTo] = arr[i * epe + d];
821:         for (; dTo < bTo; ++dTo) arrTo[i * epeTo + dTo] = 0.0;
822:         d = b;
823:       }
824:     }
825:   } else if (dim == 2) {
826:     const PetscInt epr   = stag->nGhost[0] * epe;
827:     const PetscInt eprTo = stagTo->nGhost[0] * epeTo;
828:     for (j = offset[1]; j < offset[1] + n[1] + nExtra[1]; ++j) {
829:       for (i = offset[0]; i < offset[0] + n[0] + nExtra[0]; ++i) {
830:         const PetscInt base   = j * epr + i * epe;
831:         const PetscInt baseTo = j * eprTo + i * epeTo;
832:         PetscInt       d = 0, dTo = 0, b = 0, bTo = 0;
833:         const PetscInt s[4] = {0, 1, 1, 2}; /* Dimensions of points, in order */
834:         PetscInt       si;
835:         for (si = 0; si < 4; ++si) {
836:           b += stag->dof[s[si]];
837:           bTo += stagTo->dof[s[si]];
838:           for (; d < b && dTo < bTo; ++d, ++dTo) arrTo[baseTo + dTo] = arr[base + d];
839:           for (; dTo < bTo; ++dTo) arrTo[baseTo + dTo] = 0.0;
840:           d = b;
841:         }
842:       }
843:     }
844:   } else if (dim == 3) {
845:     const PetscInt epr   = stag->nGhost[0] * epe;
846:     const PetscInt eprTo = stagTo->nGhost[0] * epeTo;
847:     const PetscInt epl   = stag->nGhost[1] * epr;
848:     const PetscInt eplTo = stagTo->nGhost[1] * eprTo;
849:     for (k = offset[2]; k < offset[2] + n[2] + nExtra[2]; ++k) {
850:       for (j = offset[1]; j < offset[1] + n[1] + nExtra[1]; ++j) {
851:         for (i = offset[0]; i < offset[0] + n[0] + nExtra[0]; ++i) {
852:           PetscInt       d = 0, dTo = 0, b = 0, bTo = 0;
853:           const PetscInt base   = k * epl + j * epr + i * epe;
854:           const PetscInt baseTo = k * eplTo + j * eprTo + i * epeTo;
855:           const PetscInt s[8]   = {0, 1, 1, 2, 1, 2, 2, 3}; /* dimensions of points, in order */
856:           PetscInt       is;
857:           for (is = 0; is < 8; ++is) {
858:             b += stag->dof[s[is]];
859:             bTo += stagTo->dof[s[is]];
860:             for (; d < b && dTo < bTo; ++d, ++dTo) arrTo[baseTo + dTo] = arr[base + d];
861:             for (; dTo < bTo; ++dTo) arrTo[baseTo + dTo] = 0.0;
862:             d = b;
863:           }
864:         }
865:       }
866:     }
867:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
868:   PetscCall(VecRestoreArrayRead(vecLocal, &arr));
869:   PetscCall(VecRestoreArray(vecToLocal, &arrTo));
870:   PetscCall(DMRestoreLocalVector(dm, &vecLocal));
871:   PetscCall(DMLocalToGlobalBegin(dmTo, vecToLocal, INSERT_VALUES, vecTo));
872:   PetscCall(DMLocalToGlobalEnd(dmTo, vecToLocal, INSERT_VALUES, vecTo));
873:   PetscCall(DMRestoreLocalVector(dmTo, &vecToLocal));
874:   PetscFunctionReturn(PETSC_SUCCESS);
875: }

877: /*@C
878:   DMStagPopulateLocalToGlobalInjective - populate an internal 1-to-1 local-to-global map

880:   Collective

882:   Creates an internal object which explicitly maps a single local degree of
883:   freedom to each global degree of freedom. This is used, if populated,
884:   instead of SCATTER_REVERSE_LOCAL with the (1-to-many, in general)
885:   global-to-local map, when DMLocalToGlobal() is called with INSERT_VALUES.
886:   This allows usage, for example, even in the periodic, 1-rank case, where
887:   the inverse of the global-to-local map, even when restricted to on-rank
888:   communication, is non-injective. This is at the cost of storing an additional
889:   VecScatter object inside each `DMSTAG` object.

891:   Input Parameter:
892: . dm - the `DMSTAG` object

894:   Level: developer

896:   Notes:
897:   In normal usage, library users shouldn't be concerned with this function,
898:   as it is called during `DMSetUp()`, when required.

900:   Returns immediately if the internal map is already populated.

902:   Developer Notes:
903:   This could, if desired, be moved up to a general `DM` routine. It would allow,
904:   for example, `DMDA` to support `DMLocalToGlobal()` with `INSERT_VALUES`,
905:   even in the single-rank periodic case.

907: .seealso: [](ch_stag), `DMSTAG`, `DMLocalToGlobal()`, `VecScatter`
908: @*/
909: PetscErrorCode DMStagPopulateLocalToGlobalInjective(DM dm)
910: {
911:   PetscInt       dim;
912:   DM_Stag *const stag = (DM_Stag *)dm->data;

914:   PetscFunctionBegin;
916:   if (stag->ltog_injective) PetscFunctionReturn(PETSC_SUCCESS); /* Don't re-populate */
917:   PetscCall(DMGetDimension(dm, &dim));
918:   switch (dim) {
919:   case 1:
920:     PetscCall(DMStagPopulateLocalToGlobalInjective_1d(dm));
921:     break;
922:   case 2:
923:     PetscCall(DMStagPopulateLocalToGlobalInjective_2d(dm));
924:     break;
925:   case 3:
926:     PetscCall(DMStagPopulateLocalToGlobalInjective_3d(dm));
927:     break;
928:   default:
929:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
930:   }
931:   PetscFunctionReturn(PETSC_SUCCESS);
932: }

934: static PetscErrorCode DMStagRestoreProductCoordinateArrays_Private(DM dm, void *arrX, void *arrY, void *arrZ, PetscBool read)
935: {
936:   PetscInt dim, d;
937:   void    *arr[DMSTAG_MAX_DIM];
938:   DM       dmCoord;

940:   PetscFunctionBegin;
942:   PetscCall(DMGetDimension(dm, &dim));
943:   PetscCheck(dim <= DMSTAG_MAX_DIM, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented for %" PetscInt_FMT " dimensions", dim);
944:   arr[0] = arrX;
945:   arr[1] = arrY;
946:   arr[2] = arrZ;
947:   PetscCall(DMGetCoordinateDM(dm, &dmCoord));
948:   for (d = 0; d < dim; ++d) {
949:     DM  subDM;
950:     Vec coord1d_local;

952:     /* Ignore unrequested arrays */
953:     if (!arr[d]) continue;

955:     PetscCall(DMProductGetDM(dmCoord, d, &subDM));
956:     PetscCall(DMGetCoordinatesLocal(subDM, &coord1d_local));
957:     if (read) {
958:       PetscCall(DMStagVecRestoreArrayRead(subDM, coord1d_local, arr[d]));
959:     } else {
960:       PetscCall(DMStagVecRestoreArray(subDM, coord1d_local, arr[d]));
961:     }
962:   }
963:   PetscFunctionReturn(PETSC_SUCCESS);
964: }

966: /*@C
967:   DMStagRestoreProductCoordinateArrays - restore local array access

969:   Logically Collective

971:   Input Parameter:
972: . dm - the `DMSTAG` object

974:   Output Parameters:
975: + arrX - local 1D coordinate arrays for x direction
976: . arrY - local 1D coordinate arrays for y direction
977: - arrZ - local 1D coordinate arrays for z direction

979:   Level: intermediate

981:   Notes:
982:   This function does not automatically perform a local->global scatter to populate global coordinates from the local coordinates.
983:   Thus, it may be required to explicitly perform these operations in some situations, as in the following partial example:

985:   ```
986:   PetscCall(DMGetCoordinateDM(dm,&cdm));
987:   for (d=0; d<3; ++d) {
988:     DM  subdm;
989:     Vec coor,coor_local;

991:     PetscCall(DMProductGetDM(cdm,d,&subdm));
992:     PetscCall(DMGetCoordinates(subdm,&coor));
993:     PetscCall(DMGetCoordinatesLocal(subdm,&coor_local));
994:     PetscCall(DMLocalToGlobal(subdm,coor_local,INSERT_VALUES,coor));
995:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Coordinates dim %" PetscInt_FMT ":\n",d));
996:     PetscCall(VecView(coor,PETSC_VIEWER_STDOUT_WORLD));
997:   }
998:    ```

1000: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetProductCoordinateArrays()`, `DMStagGetProductCoordinateArraysRead()`
1001: @*/
1002: PetscErrorCode DMStagRestoreProductCoordinateArrays(DM dm, void *arrX, void *arrY, void *arrZ)
1003: {
1004:   PetscFunctionBegin;
1005:   PetscCall(DMStagRestoreProductCoordinateArrays_Private(dm, arrX, arrY, arrZ, PETSC_FALSE));
1006:   PetscFunctionReturn(PETSC_SUCCESS);
1007: }

1009: /*@C
1010:   DMStagRestoreProductCoordinateArraysRead - restore local product array access, read-only

1012:   Logically Collective

1014:   Input Parameters:
1015: + dm - the `DMSTAG` object
1016: . arrX - local 1D coordinate arrays for x direction
1017: . arrY - local 1D coordinate arrays for y direction
1018: - arrZ - local 1D coordinate arrays for z direction

1020:   Level: intermediate

1022: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetProductCoordinateArrays()`, `DMStagGetProductCoordinateArraysRead()`
1023: @*/
1024: PetscErrorCode DMStagRestoreProductCoordinateArraysRead(DM dm, void *arrX, void *arrY, void *arrZ)
1025: {
1026:   PetscFunctionBegin;
1027:   PetscCall(DMStagRestoreProductCoordinateArrays_Private(dm, arrX, arrY, arrZ, PETSC_TRUE));
1028:   PetscFunctionReturn(PETSC_SUCCESS);
1029: }

1031: /*@C
1032:   DMStagSetBoundaryTypes - set `DMSTAG` boundary types

1034:   Logically Collective; boundaryType0, boundaryType1, and boundaryType2 must contain common values

1036:   Input Parameters:
1037: + dm - the `DMSTAG` object
1038: . boundaryTypeX - boundary type for x direction
1039: . boundaryTypeY - boundary type for y direction, not set for one dimensional problems
1040: - boundaryTypeZ - boundary type for z direction, not set for one and two dimensional problems

1042:   Level: advanced

1044:   Note:
1045:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1047: .seealso: [](ch_stag), `DMSTAG`, `DMBoundaryType`, `DMStagCreate1d()`, `DMStagCreate2d()`, `DMStagCreate3d()`, `DMDASetBoundaryType()`
1048: @*/
1049: PetscErrorCode DMStagSetBoundaryTypes(DM dm, DMBoundaryType boundaryType0, DMBoundaryType boundaryType1, DMBoundaryType boundaryType2)
1050: {
1051:   DM_Stag *const stag = (DM_Stag *)dm->data;
1052:   PetscInt       dim;

1054:   PetscFunctionBegin;
1055:   PetscCall(DMGetDimension(dm, &dim));
1060:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1061:   stag->boundaryType[0] = boundaryType0;
1062:   if (dim > 1) stag->boundaryType[1] = boundaryType1;
1063:   if (dim > 2) stag->boundaryType[2] = boundaryType2;
1064:   PetscFunctionReturn(PETSC_SUCCESS);
1065: }

1067: /*@C
1068:   DMStagSetCoordinateDMType - set DM type to store coordinates

1070:   Logically Collective; `dmtype` must contain common value

1072:   Input Parameters:
1073: + dm - the `DMSTAG` object
1074: - dmtype - DMtype for coordinates, either `DMSTAG` or `DMPRODUCT`

1076:   Level: advanced

1078: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMGetCoordinateDM()`, `DMStagSetUniformCoordinates()`, `DMStagSetUniformCoordinatesExplicit()`, `DMStagSetUniformCoordinatesProduct()`, `DMType`
1079: @*/
1080: PetscErrorCode DMStagSetCoordinateDMType(DM dm, DMType dmtype)
1081: {
1082:   DM_Stag *const stag = (DM_Stag *)dm->data;

1084:   PetscFunctionBegin;
1086:   PetscCall(PetscFree(stag->coordinateDMType));
1087:   PetscCall(PetscStrallocpy(dmtype, (char **)&stag->coordinateDMType));
1088:   PetscFunctionReturn(PETSC_SUCCESS);
1089: }

1091: /*@C
1092:   DMStagSetDOF - set dof/stratum

1094:   Logically Collective; `dof0`, `dof1`, `dof2`, and `dof3` must contain common values

1096:   Input Parameters:
1097: + dm - the `DMSTAG` object
1098: . dof0 - the number of points per 0-cell (vertex/node)
1099: . dof1 - the number of points per 1-cell (element in 1D, edge in 2D and 3D)
1100: . dof2 - the number of points per 2-cell (element in 2D, face in 3D)
1101: - dof3 - the number of points per 3-cell (element in 3D)

1103:   Level: advanced

1105:   Note:
1106:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1108: .seealso: [](ch_stag), `DMSTAG`, `DMDASetDof()`
1109: @*/
1110: PetscErrorCode DMStagSetDOF(DM dm, PetscInt dof0, PetscInt dof1, PetscInt dof2, PetscInt dof3)
1111: {
1112:   DM_Stag *const stag = (DM_Stag *)dm->data;
1113:   PetscInt       dim;

1115:   PetscFunctionBegin;
1121:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1122:   PetscCall(DMGetDimension(dm, &dim));
1123:   PetscCheck(dof0 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "dof0 cannot be negative");
1124:   PetscCheck(dof1 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "dof1 cannot be negative");
1125:   PetscCheck(dim <= 1 || dof2 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "dof2 cannot be negative");
1126:   PetscCheck(dim <= 2 || dof3 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "dof3 cannot be negative");
1127:   stag->dof[0] = dof0;
1128:   stag->dof[1] = dof1;
1129:   if (dim > 1) stag->dof[2] = dof2;
1130:   if (dim > 2) stag->dof[3] = dof3;
1131:   PetscFunctionReturn(PETSC_SUCCESS);
1132: }

1134: /*@C
1135:   DMStagSetNumRanks - set ranks in each direction in the global rank grid

1137:   Logically Collective; `nRanks0`, `nRanks1`, and `nRanks2` must contain common values

1139:   Input Parameters:
1140: + dm - the `DMSTAG` object
1141: . nRanks0 - number of ranks in the x direction
1142: . nRanks1 - number of ranks in the y direction
1143: - nRanks2 - number of ranks in the z direction

1145:   Level: developer

1147:   Note:
1148:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1150: .seealso: [](ch_stag), `DMSTAG`, `DMDASetNumProcs()`
1151: @*/
1152: PetscErrorCode DMStagSetNumRanks(DM dm, PetscInt nRanks0, PetscInt nRanks1, PetscInt nRanks2)
1153: {
1154:   DM_Stag *const stag = (DM_Stag *)dm->data;
1155:   PetscInt       dim;

1157:   PetscFunctionBegin;
1162:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1163:   PetscCall(DMGetDimension(dm, &dim));
1164:   PetscCheck(nRanks0 == PETSC_DECIDE || nRanks0 >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "number of ranks in X direction cannot be less than 1");
1165:   PetscCheck(dim <= 1 || nRanks1 == PETSC_DECIDE || nRanks1 >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "number of ranks in Y direction cannot be less than 1");
1166:   PetscCheck(dim <= 2 || nRanks2 == PETSC_DECIDE || nRanks2 >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "number of ranks in Z direction cannot be less than 1");
1167:   if (nRanks0) stag->nRanks[0] = nRanks0;
1168:   if (dim > 1 && nRanks1) stag->nRanks[1] = nRanks1;
1169:   if (dim > 2 && nRanks2) stag->nRanks[2] = nRanks2;
1170:   PetscFunctionReturn(PETSC_SUCCESS);
1171: }

1173: /*@C
1174:   DMStagSetStencilType - set elementwise ghost/halo stencil type

1176:   Logically Collective; `stencilType` must contain common value

1178:   Input Parameters:
1179: + dm - the `DMSTAG` object
1180: - stencilType - the elementwise ghost stencil type: `DMSTAG_STENCIL_BOX`, `DMSTAG_STENCIL_STAR`, or `DMSTAG_STENCIL_NONE`

1182:   Level: beginner

1184: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetStencilType()`, `DMStagSetStencilWidth()`, `DMStagStencilType`
1185: @*/
1186: PetscErrorCode DMStagSetStencilType(DM dm, DMStagStencilType stencilType)
1187: {
1188:   DM_Stag *const stag = (DM_Stag *)dm->data;

1190:   PetscFunctionBegin;
1193:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1194:   stag->stencilType = stencilType;
1195:   PetscFunctionReturn(PETSC_SUCCESS);
1196: }

1198: /*@C
1199:   DMStagSetStencilWidth - set elementwise stencil width

1201:   Logically Collective; `stencilWidth` must contain common value

1203:   Input Parameters:
1204: + dm - the `DMSTAG` object
1205: - stencilWidth - stencil/halo/ghost width in elements

1207:   Level: beginner

1209:   Note:
1210:   The width value is not used when `DMSTAG_STENCIL_NONE` is specified.

1212: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetStencilWidth()`, `DMStagGetStencilType()`, `DMStagStencilType`
1213: @*/
1214: PetscErrorCode DMStagSetStencilWidth(DM dm, PetscInt stencilWidth)
1215: {
1216:   DM_Stag *const stag = (DM_Stag *)dm->data;

1218:   PetscFunctionBegin;
1221:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1222:   PetscCheck(stencilWidth >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Stencil width must be non-negative");
1223:   stag->stencilWidth = stencilWidth;
1224:   PetscFunctionReturn(PETSC_SUCCESS);
1225: }

1227: /*@C
1228:   DMStagSetGlobalSizes - set global element counts in each direction

1230:   Logically Collective; `N0`, `N1`, and `N2` must contain common values

1232:   Input Parameters:
1233: + dm - the `DMSTAG` object
1234: . N0 - global elementwise size in the x direction
1235: . N1 - global elementwise size in the y direction
1236: - N2 - global elementwise size in the z direction

1238:   Level: advanced

1240:   Note:
1241:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1243: .seealso: [](ch_stag), `DMSTAG`, `DMStagGetGlobalSizes()`, `DMDASetSizes()`
1244: @*/
1245: PetscErrorCode DMStagSetGlobalSizes(DM dm, PetscInt N0, PetscInt N1, PetscInt N2)
1246: {
1247:   DM_Stag *const stag = (DM_Stag *)dm->data;
1248:   PetscInt       dim;

1250:   PetscFunctionBegin;
1252:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1253:   PetscCall(DMGetDimension(dm, &dim));
1254:   PetscCheck(N0 >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of elements in X direction must be positive");
1255:   PetscCheck(dim <= 1 || N1 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of elements in Y direction must be positive");
1256:   PetscCheck(dim <= 2 || N2 >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Number of elements in Z direction must be positive");
1257:   if (N0) stag->N[0] = N0;
1258:   if (N1) stag->N[1] = N1;
1259:   if (N2) stag->N[2] = N2;
1260:   PetscFunctionReturn(PETSC_SUCCESS);
1261: }

1263: /*@C
1264:   DMStagSetOwnershipRanges - set elements per rank in each direction

1266:   Logically Collective; `lx`, `ly`, and `lz` must contain common values

1268:   Input Parameters:
1269: + dm - the `DMSTAG` object
1270: . lx - element counts for each rank in the x direction
1271: . ly - element counts for each rank in the y direction
1272: - lz - element counts for each rank in the z direction

1274:   Level: developer

1276:   Note:
1277:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids. These arguments may be set to `NULL` in this case.

1279: .seealso: [](ch_stag), `DMSTAG`, `DMStagSetGlobalSizes()`, `DMStagGetOwnershipRanges()`, `DMDASetOwnershipRanges()`
1280: @*/
1281: PetscErrorCode DMStagSetOwnershipRanges(DM dm, PetscInt const *lx, PetscInt const *ly, PetscInt const *lz)
1282: {
1283:   DM_Stag *const  stag = (DM_Stag *)dm->data;
1284:   const PetscInt *lin[3];
1285:   PetscInt        d, dim;

1287:   PetscFunctionBegin;
1289:   PetscCheck(!dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called before DMSetUp()");
1290:   lin[0] = lx;
1291:   lin[1] = ly;
1292:   lin[2] = lz;
1293:   PetscCall(DMGetDimension(dm, &dim));
1294:   for (d = 0; d < dim; ++d) {
1295:     if (lin[d]) {
1296:       PetscCheck(stag->nRanks[d] >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Cannot set ownership ranges before setting number of ranks");
1297:       if (!stag->l[d]) PetscCall(PetscMalloc1(stag->nRanks[d], &stag->l[d]));
1298:       PetscCall(PetscArraycpy(stag->l[d], lin[d], stag->nRanks[d]));
1299:     }
1300:   }
1301:   PetscFunctionReturn(PETSC_SUCCESS);
1302: }

1304: /*@C
1305:   DMStagSetUniformCoordinates - set `DMSTAG` coordinates to be a uniform grid

1307:   Collective

1309:   Input Parameters:
1310: + dm - the `DMSTAG` object
1311: . xmin - minimum global coordinate value in the x direction
1312: . xmax - maximum global coordinate values in the x direction
1313: . ymin - minimum global coordinate value in the y direction
1314: . ymax - maximum global coordinate value in the y direction
1315: . zmin - minimum global coordinate value in the z direction
1316: - zmax - maximum global coordinate value in the z direction

1318:   Level: advanced

1320:   Notes:
1321:   `DMSTAG` supports 2 different types of coordinate DM: `DMSTAG` and `DMPRODUCT`.
1322:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1324:   Local coordinates are populated (using `DMSetCoordinatesLocal()`), linearly
1325:   extrapolated to ghost cells, including those outside the physical domain.
1326:   This is also done in case of periodic boundaries, meaning that the same
1327:   global point may have different coordinates in different local representations,
1328:   which are equivalent assuming a periodicity implied by the arguments to this function,
1329:   i.e. two points are equivalent if their difference is a multiple of $($`xmax` $-$ `xmin` $)$
1330:   in the x direction, $($ `ymax` $-$ `ymin` $)$ in the y direction, and $($ `zmax` $-$ `zmin` $)$ in the z direction.

1332: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMStagSetUniformCoordinatesExplicit()`, `DMStagSetUniformCoordinatesProduct()`, `DMStagSetCoordinateDMType()`, `DMGetCoordinateDM()`, `DMGetCoordinates()`, `DMDASetUniformCoordinates()`, `DMBoundaryType`
1333: @*/
1334: PetscErrorCode DMStagSetUniformCoordinates(DM dm, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax, PetscReal zmin, PetscReal zmax)
1335: {
1336:   DM_Stag *const stag = (DM_Stag *)dm->data;
1337:   PetscBool      flg_stag, flg_product;

1339:   PetscFunctionBegin;
1341:   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called after DMSetUp()");
1342:   PetscCheck(stag->coordinateDMType, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "You must first call DMStagSetCoordinateDMType()");
1343:   PetscCall(PetscStrcmp(stag->coordinateDMType, DMSTAG, &flg_stag));
1344:   PetscCall(PetscStrcmp(stag->coordinateDMType, DMPRODUCT, &flg_product));
1345:   if (flg_stag) {
1346:     PetscCall(DMStagSetUniformCoordinatesExplicit(dm, xmin, xmax, ymin, ymax, zmin, zmax));
1347:   } else if (flg_product) {
1348:     PetscCall(DMStagSetUniformCoordinatesProduct(dm, xmin, xmax, ymin, ymax, zmin, zmax));
1349:   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported DM Type %s", stag->coordinateDMType);
1350:   PetscFunctionReturn(PETSC_SUCCESS);
1351: }

1353: /*@C
1354:   DMStagSetUniformCoordinatesExplicit - set `DMSTAG` coordinates to be a uniform grid, storing all values

1356:   Collective

1358:   Input Parameters:
1359: + dm - the `DMSTAG` object
1360: . xmin - minimum global coordinate value in the x direction
1361: . xmax - maximum global coordinate values in the x direction
1362: . ymin - minimum global coordinate value in the y direction
1363: . ymax - maximum global coordinate value in the y direction
1364: . zmin - minimum global coordinate value in the z direction
1365: - zmax - maximum global coordinate value in the z direction

1367:   Level: beginner

1369:   Notes:
1370:   `DMSTAG` supports 2 different types of coordinate `DM`: either another `DMSTAG`, or a `DMPRODUCT`.
1371:   If the grid is orthogonal, using `DMPRODUCT` should be more efficient.

1373:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1375:   See the manual page for `DMStagSetUniformCoordinates()` for information on how
1376:   coordinates for dummy cells outside the physical domain boundary are populated.

1378: .seealso: [](ch_stag), `DMSTAG`, `DMStagSetUniformCoordinates()`, `DMStagSetUniformCoordinatesProduct()`, `DMStagSetCoordinateDMType()`
1379: @*/
1380: PetscErrorCode DMStagSetUniformCoordinatesExplicit(DM dm, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax, PetscReal zmin, PetscReal zmax)
1381: {
1382:   DM_Stag *const stag = (DM_Stag *)dm->data;
1383:   PetscInt       dim;
1384:   PetscBool      flg;

1386:   PetscFunctionBegin;
1388:   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called after DMSetUp()");
1389:   PetscCall(PetscStrcmp(stag->coordinateDMType, DMSTAG, &flg));
1390:   PetscCheck(!stag->coordinateDMType || flg, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Refusing to change an already-set DM coordinate type");
1391:   PetscCall(DMStagSetCoordinateDMType(dm, DMSTAG));
1392:   PetscCall(DMGetDimension(dm, &dim));
1393:   switch (dim) {
1394:   case 1:
1395:     PetscCall(DMStagSetUniformCoordinatesExplicit_1d(dm, xmin, xmax));
1396:     break;
1397:   case 2:
1398:     PetscCall(DMStagSetUniformCoordinatesExplicit_2d(dm, xmin, xmax, ymin, ymax));
1399:     break;
1400:   case 3:
1401:     PetscCall(DMStagSetUniformCoordinatesExplicit_3d(dm, xmin, xmax, ymin, ymax, zmin, zmax));
1402:     break;
1403:   default:
1404:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
1405:   }
1406:   PetscCall(DMCoarsenHookRemove(dm, DMRestrictHook_Coordinates, NULL, NULL));
1407:   PetscFunctionReturn(PETSC_SUCCESS);
1408: }

1410: /*@C
1411:   DMStagSetUniformCoordinatesProduct - create uniform coordinates, as a product of 1D arrays

1413:   Set the coordinate `DM` to be a `DMPRODUCT` of 1D `DMSTAG` objects, each of which have a coordinate `DM` (also a 1d `DMSTAG`) holding uniform coordinates.

1415:   Collective

1417:   Input Parameters:
1418: + dm - the `DMSTAG` object
1419: . xmin - minimum global coordinate value in the x direction
1420: . xmax - maximum global coordinate values in the x direction
1421: . ymin - minimum global coordinate value in the y direction
1422: . ymax - maximum global coordinate value in the y direction
1423: . zmin - minimum global coordinate value in the z direction
1424: - zmax - maximum global coordinate value in the z direction

1426:   Level: intermediate

1428:   Notes:
1429:   Arguments corresponding to higher dimensions are ignored for 1D and 2D grids.

1431:   The per-dimension 1-dimensional `DMSTAG` objects that comprise the product
1432:   always have active 0-cells (vertices, element boundaries) and 1-cells
1433:   (element centers).

1435:   See the manual page for `DMStagSetUniformCoordinates()` for information on how
1436:   coordinates for dummy cells outside the physical domain boundary are populated.

1438: .seealso: [](ch_stag), `DMSTAG`, `DMPRODUCT`, `DMStagSetUniformCoordinates()`, `DMStagSetUniformCoordinatesExplicit()`, `DMStagSetCoordinateDMType()`
1439: @*/
1440: PetscErrorCode DMStagSetUniformCoordinatesProduct(DM dm, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax, PetscReal zmin, PetscReal zmax)
1441: {
1442:   DM_Stag *const stag = (DM_Stag *)dm->data;
1443:   DM             dmc;
1444:   PetscInt       dim, d, dof0, dof1;
1445:   PetscBool      flg;

1447:   PetscFunctionBegin;
1449:   PetscCheck(dm->setupcalled, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This function must be called after DMSetUp()");
1450:   PetscCall(PetscStrcmp(stag->coordinateDMType, DMPRODUCT, &flg));
1451:   PetscCheck(!stag->coordinateDMType || flg, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Refusing to change an already-set DM coordinate type");
1452:   PetscCall(DMStagSetCoordinateDMType(dm, DMPRODUCT));
1453:   PetscCall(DMGetCoordinateDM(dm, &dmc));
1454:   PetscCall(DMGetDimension(dm, &dim));

1456:   /* Create 1D sub-DMs, living on subcommunicators.
1457:      Always include both vertex and element dof, regardless of the active strata of the DMStag */
1458:   dof0 = 1;
1459:   dof1 = 1;

1461:   for (d = 0; d < dim; ++d) {
1462:     DM                subdm;
1463:     MPI_Comm          subcomm;
1464:     PetscMPIInt       color;
1465:     const PetscMPIInt key = 0; /* let existing rank break ties */

1467:     /* Choose colors based on position in the plane orthogonal to this dim, and split */
1468:     switch (d) {
1469:     case 0:
1470:       color = (dim > 1 ? stag->rank[1] : 0) + (dim > 2 ? stag->nRanks[1] * stag->rank[2] : 0);
1471:       break;
1472:     case 1:
1473:       color = stag->rank[0] + (dim > 2 ? stag->nRanks[0] * stag->rank[2] : 0);
1474:       break;
1475:     case 2:
1476:       color = stag->rank[0] + stag->nRanks[0] * stag->rank[1];
1477:       break;
1478:     default:
1479:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension index %" PetscInt_FMT, d);
1480:     }
1481:     PetscCallMPI(MPI_Comm_split(PetscObjectComm((PetscObject)dm), color, key, &subcomm));

1483:     /* Create sub-DMs living on these new communicators (which are destroyed by DMProduct) */
1484:     PetscCall(DMStagCreate1d(subcomm, stag->boundaryType[d], stag->N[d], dof0, dof1, stag->stencilType, stag->stencilWidth, stag->l[d], &subdm));
1485:     /* Copy vector and matrix type information from parent DM */
1486:     PetscCall(DMSetVecType(subdm, dm->vectype));
1487:     PetscCall(DMSetMatType(subdm, dm->mattype));
1488:     PetscCall(DMSetUp(subdm));
1489:     switch (d) {
1490:     case 0:
1491:       PetscCall(DMStagSetUniformCoordinatesExplicit(subdm, xmin, xmax, 0, 0, 0, 0));
1492:       break;
1493:     case 1:
1494:       PetscCall(DMStagSetUniformCoordinatesExplicit(subdm, ymin, ymax, 0, 0, 0, 0));
1495:       break;
1496:     case 2:
1497:       PetscCall(DMStagSetUniformCoordinatesExplicit(subdm, zmin, zmax, 0, 0, 0, 0));
1498:       break;
1499:     default:
1500:       SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension index %" PetscInt_FMT, d);
1501:     }
1502:     PetscCall(DMProductSetDM(dmc, d, subdm));
1503:     PetscCall(DMProductSetDimensionIndex(dmc, d, 0));
1504:     PetscCall(DMDestroy(&subdm));
1505:     PetscCallMPI(MPI_Comm_free(&subcomm));
1506:   }
1507:   PetscCall(DMCoarsenHookRemove(dm, DMRestrictHook_Coordinates, NULL, NULL));
1508:   PetscFunctionReturn(PETSC_SUCCESS);
1509: }

1511: /*@C
1512:   DMStagVecGetArray - get access to local array

1514:   Logically Collective

1516:   Input Parameters:
1517: + dm - the `DMSTAG` object
1518: - vec - the `Vec` object

1520:   Output Parameter:
1521: . array - the array

1523:   Level: beginner

1525:   Note:
1526:   This function returns a (dim+1)-dimensional array for a dim-dimensional
1527:   `DMSTAG`.

1529:   The first 1-3 dimensions indicate an element in the global
1530:   numbering, using the standard C ordering.

1532:   The final dimension in this array corresponds to a degree
1533:   of freedom with respect to this element, for example corresponding to
1534:   the element or one of its neighboring faces, edges, or vertices.

1536:   For example, for a 3D `DMSTAG`, indexing is `array[k][j][i][idx]`, where `k` is the
1537:   index in the z-direction, `j` is the index in the y-direction, and `i` is the
1538:   index in the x-direction.

1540:   `idx` is obtained with `DMStagGetLocationSlot()`, since the correct offset
1541:   into the $(d+1)$-dimensional C array for a $d$-dimensional `DMSTAG` depends on the grid size and the number
1542:   of DOF stored at each location.

1544:   `DMStagVecRestoreArray()` must be called, once finished with the array

1546: .seealso: [](ch_stag), `DMSTAG`, `DMStagVecGetArrayRead()`, `DMStagGetLocationSlot()`, `DMGetLocalVector()`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMCreateGlobalVector()`, `DMDAVecGetArray()`, `DMDAVecGetArrayDOF()`
1547: @*/
1548: PetscErrorCode DMStagVecGetArray(DM dm, Vec vec, void *array)
1549: {
1550:   DM_Stag *const stag = (DM_Stag *)dm->data;
1551:   PetscInt       dim;
1552:   PetscInt       nLocal;

1554:   PetscFunctionBegin;
1557:   PetscCall(DMGetDimension(dm, &dim));
1558:   PetscCall(VecGetLocalSize(vec, &nLocal));
1559:   PetscCheck(nLocal == stag->entriesGhost, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Vector local size %" PetscInt_FMT " is not compatible with DMStag local size %" PetscInt_FMT, nLocal, stag->entriesGhost);
1560:   switch (dim) {
1561:   case 1:
1562:     PetscCall(VecGetArray2d(vec, stag->nGhost[0], stag->entriesPerElement, stag->startGhost[0], 0, (PetscScalar ***)array));
1563:     break;
1564:   case 2:
1565:     PetscCall(VecGetArray3d(vec, stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar ****)array));
1566:     break;
1567:   case 3:
1568:     PetscCall(VecGetArray4d(vec, stag->nGhost[2], stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[2], stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar *****)array));
1569:     break;
1570:   default:
1571:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
1572:   }
1573:   PetscFunctionReturn(PETSC_SUCCESS);
1574: }

1576: /*@C
1577:   DMStagVecGetArrayRead - get read-only access to a local array

1579:   Logically Collective

1581:   See the man page for `DMStagVecGetArray()` for more information.

1583:   Input Parameters:
1584: + dm - the `DMSTAG` object
1585: - vec - the `Vec` object

1587:   Output Parameter:
1588: . array - the read-only array

1590:   Level: beginner

1592:   Note:
1593:   `DMStagVecRestoreArrayRead()` must be called, once finished with the array

1595: .seealso: [](ch_stag), `DMSTAG`, `DMStagVecGetArrayRead()`, `DMStagGetLocationSlot()`, `DMGetLocalVector()`, `DMCreateLocalVector()`, `DMGetGlobalVector()`, `DMCreateGlobalVector()`, `DMDAVecGetArrayRead()`, `DMDAVecGetArrayDOFRead()`
1596: @*/
1597: PetscErrorCode DMStagVecGetArrayRead(DM dm, Vec vec, void *array)
1598: {
1599:   DM_Stag *const stag = (DM_Stag *)dm->data;
1600:   PetscInt       dim;
1601:   PetscInt       nLocal;

1603:   PetscFunctionBegin;
1606:   PetscCall(DMGetDimension(dm, &dim));
1607:   PetscCall(VecGetLocalSize(vec, &nLocal));
1608:   PetscCheck(nLocal == stag->entriesGhost, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Vector local size %" PetscInt_FMT " is not compatible with DMSTAG local size %" PetscInt_FMT, nLocal, stag->entriesGhost);
1609:   switch (dim) {
1610:   case 1:
1611:     PetscCall(VecGetArray2dRead(vec, stag->nGhost[0], stag->entriesPerElement, stag->startGhost[0], 0, (PetscScalar ***)array));
1612:     break;
1613:   case 2:
1614:     PetscCall(VecGetArray3dRead(vec, stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar ****)array));
1615:     break;
1616:   case 3:
1617:     PetscCall(VecGetArray4dRead(vec, stag->nGhost[2], stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[2], stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar *****)array));
1618:     break;
1619:   default:
1620:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
1621:   }
1622:   PetscFunctionReturn(PETSC_SUCCESS);
1623: }

1625: /*@C
1626:   DMStagVecRestoreArray - restore access to a raw array

1628:   Logically Collective

1630:   Input Parameters:
1631: + dm - the `DMSTAG` object
1632: - vec - the `Vec` object

1634:   Output Parameter:
1635: . array - the array

1637:   Level: beginner

1639: .seealso: [](ch_stag), `DMSTAG`, `DMStagVecGetArray()`, `DMDAVecRestoreArray()`, `DMDAVecRestoreArrayDOF()`
1640: @*/
1641: PetscErrorCode DMStagVecRestoreArray(DM dm, Vec vec, void *array)
1642: {
1643:   DM_Stag *const stag = (DM_Stag *)dm->data;
1644:   PetscInt       dim;
1645:   PetscInt       nLocal;

1647:   PetscFunctionBegin;
1650:   PetscCall(DMGetDimension(dm, &dim));
1651:   PetscCall(VecGetLocalSize(vec, &nLocal));
1652:   PetscCheck(nLocal == stag->entriesGhost, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Vector local size %" PetscInt_FMT " is not compatible with DMSTAG local size %" PetscInt_FMT, nLocal, stag->entriesGhost);
1653:   switch (dim) {
1654:   case 1:
1655:     PetscCall(VecRestoreArray2d(vec, stag->nGhost[0], stag->entriesPerElement, stag->startGhost[0], 0, (PetscScalar ***)array));
1656:     break;
1657:   case 2:
1658:     PetscCall(VecRestoreArray3d(vec, stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar ****)array));
1659:     break;
1660:   case 3:
1661:     PetscCall(VecRestoreArray4d(vec, stag->nGhost[2], stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[2], stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar *****)array));
1662:     break;
1663:   default:
1664:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
1665:   }
1666:   PetscFunctionReturn(PETSC_SUCCESS);
1667: }

1669: /*@C
1670:   DMStagVecRestoreArrayRead - restore read-only access to a raw array

1672:   Logically Collective

1674:   Input Parameters:
1675: + dm - the `DMSTAG` object
1676: - vec - the Vec object

1678:   Output Parameter:
1679: . array - the read-only array

1681:   Level: beginner

1683: .seealso: [](ch_stag), `DMSTAG`, `DMStagVecGetArrayRead()`, `DMDAVecRestoreArrayRead()`, `DMDAVecRestoreArrayDOFRead()`
1684: @*/
1685: PetscErrorCode DMStagVecRestoreArrayRead(DM dm, Vec vec, void *array)
1686: {
1687:   DM_Stag *const stag = (DM_Stag *)dm->data;
1688:   PetscInt       dim;
1689:   PetscInt       nLocal;

1691:   PetscFunctionBegin;
1694:   PetscCall(DMGetDimension(dm, &dim));
1695:   PetscCall(VecGetLocalSize(vec, &nLocal));
1696:   PetscCheck(nLocal == stag->entriesGhost, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Vector local size %" PetscInt_FMT " is not compatible with DMSTAG local size %" PetscInt_FMT, nLocal, stag->entriesGhost);
1697:   switch (dim) {
1698:   case 1:
1699:     PetscCall(VecRestoreArray2dRead(vec, stag->nGhost[0], stag->entriesPerElement, stag->startGhost[0], 0, (PetscScalar ***)array));
1700:     break;
1701:   case 2:
1702:     PetscCall(VecRestoreArray3dRead(vec, stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar ****)array));
1703:     break;
1704:   case 3:
1705:     PetscCall(VecRestoreArray4dRead(vec, stag->nGhost[2], stag->nGhost[1], stag->nGhost[0], stag->entriesPerElement, stag->startGhost[2], stag->startGhost[1], stag->startGhost[0], 0, (PetscScalar *****)array));
1706:     break;
1707:   default:
1708:     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %" PetscInt_FMT, dim);
1709:   }
1710:   PetscFunctionReturn(PETSC_SUCCESS);
1711: }