Actual source code: plextransform.c

  1: #include <petsc/private/dmplextransformimpl.h>

  3: #include <petsc/private/petscfeimpl.h>

  5: PetscClassId DMPLEXTRANSFORM_CLASSID;

  7: PetscFunctionList DMPlexTransformList              = NULL;
  8: PetscBool         DMPlexTransformRegisterAllCalled = PETSC_FALSE;

 10: PetscLogEvent DMPLEXTRANSFORM_SetUp, DMPLEXTRANSFORM_Apply, DMPLEXTRANSFORM_SetConeSizes, DMPLEXTRANSFORM_SetCones, DMPLEXTRANSFORM_CreateSF, DMPLEXTRANSFORM_CreateLabels, DMPLEXTRANSFORM_SetCoordinates;

 12: /* Construct cell type order since we must loop over cell types in the same dimensional order they are stored in the plex if dm != NULL
 13:         OR in standard plex ordering if dm == NULL */
 14: static PetscErrorCode DMPlexCreateCellTypeOrder_Internal(DM dm, PetscInt dim, PetscInt *ctOrder[], PetscInt *ctOrderInv[])
 15: {
 16:   PetscInt *ctO, *ctOInv;
 17:   PetscInt  d, c, off = 0;
 18:   PetscInt  dimOrder[5] = {3, 2, 1, 0, -1};

 20:   PetscFunctionBegin;
 21:   PetscCall(PetscCalloc2(DM_NUM_POLYTOPES + 1, &ctO, DM_NUM_POLYTOPES + 1, &ctOInv));
 22:   if (dm) { // Order the dimensions by their starting location
 23:     PetscInt hStart[4] = {-1, -1, -1, -1};
 24:     for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, dim - d, &hStart[d], NULL));
 25:     PetscCall(PetscSortIntWithArray(dim + 1, hStart, &dimOrder[3 - dim]));
 26:   } else if (dim > 1) { // Standard plex ordering. dimOrder is in correct order if dim > 1
 27:     off             = 4 - dim;
 28:     dimOrder[off++] = 0;
 29:     for (d = dim - 1; d > 0; --d) dimOrder[off++] = d;
 30:   }

 32:   off = 0;
 33:   for (d = 0; d < 5; ++d) {
 34:     for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
 35:       if (c == DM_POLYTOPE_UNKNOWN_CELL || c == DM_POLYTOPE_UNKNOWN_FACE) continue;
 36:       if (DMPolytopeTypeGetDim((DMPolytopeType)c) == dimOrder[d]) ctO[off++] = c;
 37:     }
 38:   }
 39:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
 40:     if (c == DM_POLYTOPE_UNKNOWN_CELL || c == DM_POLYTOPE_UNKNOWN_FACE) ctO[off++] = c;
 41:   }
 42:   ctO[off++] = DM_NUM_POLYTOPES;
 43:   PetscCheck(off == DM_NUM_POLYTOPES + 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid offset %" PetscInt_FMT " for cell type order", off);

 45:   for (c = 0; c <= DM_NUM_POLYTOPES; ++c) ctOInv[ctO[c]] = c;

 47:   *ctOrder    = ctO;
 48:   *ctOrderInv = ctOInv;
 49:   PetscFunctionReturn(PETSC_SUCCESS);
 50: }

 52: /*@C
 53:   DMPlexTransformRegister - Adds a new transform component implementation

 55:   Not Collective

 57:   Input Parameters:
 58: + name        - The name of a new user-defined creation routine
 59: - create_func - The creation routine

 61:   Example Usage:
 62: .vb
 63:   DMPlexTransformRegister("my_transform", MyTransformCreate);
 64: .ve

 66:   Then, your transform type can be chosen with the procedural interface via
 67: .vb
 68:   DMPlexTransformCreate(MPI_Comm, DMPlexTransform *);
 69:   DMPlexTransformSetType(DMPlexTransform, "my_transform");
 70: .ve
 71:   or at runtime via the option
 72: .vb
 73:   -dm_plex_transform_type my_transform
 74: .ve

 76:   Level: advanced

 78:   Note:
 79:   `DMPlexTransformRegister()` may be called multiple times to add several user-defined transforms

 81: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformRegisterAll()`, `DMPlexTransformRegisterDestroy()`
 82: @*/
 83: PetscErrorCode DMPlexTransformRegister(const char name[], PetscErrorCode (*create_func)(DMPlexTransform))
 84: {
 85:   PetscFunctionBegin;
 86:   PetscCall(DMInitializePackage());
 87:   PetscCall(PetscFunctionListAdd(&DMPlexTransformList, name, create_func));
 88:   PetscFunctionReturn(PETSC_SUCCESS);
 89: }

 91: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_Filter(DMPlexTransform);
 92: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_Regular(DMPlexTransform);
 93: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_ToBox(DMPlexTransform);
 94: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_ToSimplex(DMPlexTransform);
 95: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_Alfeld(DMPlexTransform);
 96: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_SBR(DMPlexTransform);
 97: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_BL(DMPlexTransform);
 98: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_1D(DMPlexTransform);
 99: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_Extrude(DMPlexTransform);
100: PETSC_EXTERN PetscErrorCode DMPlexTransformCreate_Cohesive(DMPlexTransform);

102: /*@C
103:   DMPlexTransformRegisterAll - Registers all of the transform components in the `DM` package.

105:   Not Collective

107:   Level: advanced

109: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransformType`, `DMRegisterAll()`, `DMPlexTransformRegisterDestroy()`
110: @*/
111: PetscErrorCode DMPlexTransformRegisterAll(void)
112: {
113:   PetscFunctionBegin;
114:   if (DMPlexTransformRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
115:   DMPlexTransformRegisterAllCalled = PETSC_TRUE;

117:   PetscCall(DMPlexTransformRegister(DMPLEXTRANSFORMFILTER, DMPlexTransformCreate_Filter));
118:   PetscCall(DMPlexTransformRegister(DMPLEXREFINEREGULAR, DMPlexTransformCreate_Regular));
119:   PetscCall(DMPlexTransformRegister(DMPLEXREFINETOBOX, DMPlexTransformCreate_ToBox));
120:   PetscCall(DMPlexTransformRegister(DMPLEXREFINETOSIMPLEX, DMPlexTransformCreate_ToSimplex));
121:   PetscCall(DMPlexTransformRegister(DMPLEXREFINEALFELD, DMPlexTransformCreate_Alfeld));
122:   PetscCall(DMPlexTransformRegister(DMPLEXREFINEBOUNDARYLAYER, DMPlexTransformCreate_BL));
123:   PetscCall(DMPlexTransformRegister(DMPLEXREFINESBR, DMPlexTransformCreate_SBR));
124:   PetscCall(DMPlexTransformRegister(DMPLEXREFINE1D, DMPlexTransformCreate_1D));
125:   PetscCall(DMPlexTransformRegister(DMPLEXEXTRUDETYPE, DMPlexTransformCreate_Extrude));
126:   PetscCall(DMPlexTransformRegister(DMPLEXCOHESIVEEXTRUDE, DMPlexTransformCreate_Cohesive));
127:   PetscFunctionReturn(PETSC_SUCCESS);
128: }

130: /*@C
131:   DMPlexTransformRegisterDestroy - This function destroys the registered `DMPlexTransformType`. It is called from `PetscFinalize()`.

133:   Not collective

135:   Level: developer

137: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMRegisterAll()`, `DMPlexTransformType`, `PetscInitialize()`
138: @*/
139: PetscErrorCode DMPlexTransformRegisterDestroy(void)
140: {
141:   PetscFunctionBegin;
142:   PetscCall(PetscFunctionListDestroy(&DMPlexTransformList));
143:   DMPlexTransformRegisterAllCalled = PETSC_FALSE;
144:   PetscFunctionReturn(PETSC_SUCCESS);
145: }

147: /*@
148:   DMPlexTransformCreate - Creates an empty transform object. The type can then be set with `DMPlexTransformSetType()`.

150:   Collective

152:   Input Parameter:
153: . comm - The communicator for the transform object

155:   Output Parameter:
156: . tr - The transform object

158:   Level: beginner

160: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformType`, `DMPlexTransformSetType()`, `DMPLEXREFINEREGULAR`, `DMPLEXTRANSFORMFILTER`
161: @*/
162: PetscErrorCode DMPlexTransformCreate(MPI_Comm comm, DMPlexTransform *tr)
163: {
164:   DMPlexTransform t;

166:   PetscFunctionBegin;
167:   PetscAssertPointer(tr, 2);
168:   *tr = NULL;
169:   PetscCall(DMInitializePackage());

171:   PetscCall(PetscHeaderCreate(t, DMPLEXTRANSFORM_CLASSID, "DMPlexTransform", "Mesh Transform", "DMPlexTransform", comm, DMPlexTransformDestroy, DMPlexTransformView));
172:   t->setupcalled = PETSC_FALSE;
173:   t->redFactor   = 2.0;
174:   PetscCall(PetscCalloc2(DM_NUM_POLYTOPES, &t->coordFE, DM_NUM_POLYTOPES, &t->refGeom));
175:   *tr = t;
176:   PetscFunctionReturn(PETSC_SUCCESS);
177: }

179: /*@
180:   DMPlexTransformSetType - Sets the particular implementation for a transform.

182:   Collective

184:   Input Parameters:
185: + tr     - The transform
186: - method - The name of the transform type

188:   Options Database Key:
189: . -dm_plex_transform_type type - Sets the transform type; see `DMPlexTransformType`

191:   Level: intermediate

193: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformType`, `DMPlexTransformGetType()`, `DMPlexTransformCreate()`
194: @*/
195: PetscErrorCode DMPlexTransformSetType(DMPlexTransform tr, DMPlexTransformType method)
196: {
197:   PetscErrorCode (*r)(DMPlexTransform);
198:   PetscBool match;

200:   PetscFunctionBegin;
202:   PetscCall(PetscObjectTypeCompare((PetscObject)tr, method, &match));
203:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

205:   PetscCall(DMPlexTransformRegisterAll());
206:   PetscCall(PetscFunctionListFind(DMPlexTransformList, method, &r));
207:   PetscCheck(r, PetscObjectComm((PetscObject)tr), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DMPlexTransform type: %s", method);

209:   PetscTryTypeMethod(tr, destroy);
210:   PetscCall(PetscMemzero(tr->ops, sizeof(*tr->ops)));
211:   PetscCall(PetscObjectChangeTypeName((PetscObject)tr, method));
212:   PetscCall((*r)(tr));
213:   PetscFunctionReturn(PETSC_SUCCESS);
214: }

216: /*@
217:   DMPlexTransformGetType - Gets the type name (as a string) from the transform.

219:   Not Collective

221:   Input Parameter:
222: . tr - The `DMPlexTransform`

224:   Output Parameter:
225: . type - The `DMPlexTransformType` name

227:   Level: intermediate

229: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformType`, `DMPlexTransformSetType()`, `DMPlexTransformCreate()`
230: @*/
231: PetscErrorCode DMPlexTransformGetType(DMPlexTransform tr, DMPlexTransformType *type)
232: {
233:   PetscFunctionBegin;
235:   PetscAssertPointer(type, 2);
236:   PetscCall(DMPlexTransformRegisterAll());
237:   *type = ((PetscObject)tr)->type_name;
238:   PetscFunctionReturn(PETSC_SUCCESS);
239: }

241: static PetscErrorCode DMPlexTransformView_Ascii(DMPlexTransform tr, PetscViewer v)
242: {
243:   PetscViewerFormat format;

245:   PetscFunctionBegin;
246:   PetscCall(PetscViewerGetFormat(v, &format));
247:   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
248:     const PetscInt *trTypes = NULL;
249:     IS              trIS;
250:     PetscInt        cols = 8;
251:     PetscInt        Nrt  = 8, f, g;
252:     PetscMPIInt     size, rank;

254:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)tr), &rank));
255:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)tr), &size));
256:     PetscCall(PetscViewerASCIIPushSynchronized(v));
257:     if (tr->trType) PetscCall(DMLabelView(tr->trType, v));
258:     if (size > 1) PetscCall(PetscViewerASCIISynchronizedPrintf(v, "Process: %d\n", rank));
259:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "Source Starts\n"));
260:     for (g = 0; g <= cols; ++g) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14s", DMPolytopeTypes[g]));
261:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "\n"));
262:     for (f = 0; f <= cols; ++f) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14" PetscInt_FMT, tr->ctStart[f]));
263:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "\n"));
264:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "Target Starts\n"));
265:     for (g = 0; g <= cols; ++g) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14s", DMPolytopeTypes[g]));
266:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "\n"));
267:     for (f = 0; f <= cols; ++f) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14" PetscInt_FMT, tr->ctStartNew[f]));
268:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "\n"));

270:     if (tr->trType) {
271:       PetscCall(DMLabelGetNumValues(tr->trType, &Nrt));
272:       PetscCall(DMLabelGetValueIS(tr->trType, &trIS));
273:       PetscCall(ISGetIndices(trIS, &trTypes));
274:     }
275:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "Offsets\n"));
276:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "     "));
277:     for (g = 0; g < cols; ++g) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14s", DMPolytopeTypes[g]));
278:     PetscCall(PetscViewerASCIISynchronizedPrintf(v, "\n"));
279:     for (f = 0; f < Nrt; ++f) {
280:       PetscCall(PetscViewerASCIISynchronizedPrintf(v, "%2" PetscInt_FMT "  |", trTypes ? trTypes[f] : f));
281:       for (g = 0; g < cols; ++g) PetscCall(PetscViewerASCIISynchronizedPrintf(v, " %14" PetscInt_FMT, tr->offset[f * DM_NUM_POLYTOPES + g]));
282:       PetscCall(PetscViewerASCIISynchronizedPrintf(v, " |\n"));
283:     }
284:     if (tr->trType) {
285:       PetscCall(ISRestoreIndices(trIS, &trTypes));
286:       PetscCall(ISDestroy(&trIS));
287:     }
288:     PetscCall(PetscViewerFlush(v));
289:   }
290:   PetscFunctionReturn(PETSC_SUCCESS);
291: }

293: /*@
294:   DMPlexTransformView - Views a `DMPlexTransform`

296:   Collective

298:   Input Parameters:
299: + tr - the `DMPlexTransform` object to view
300: - v  - the viewer

302:   Level: beginner

304: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformType`, `PetscViewer`, `DMPlexTransformDestroy()`, `DMPlexTransformCreate()`
305: @*/
306: PetscErrorCode DMPlexTransformView(DMPlexTransform tr, PetscViewer v)
307: {
308:   PetscBool isascii;

310:   PetscFunctionBegin;
312:   if (!v) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)tr), &v));
314:   PetscCheckSameComm(tr, 1, v, 2);
315:   PetscCall(PetscViewerCheckWritable(v));
316:   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)tr, v));
317:   PetscCall(PetscObjectTypeCompare((PetscObject)v, PETSCVIEWERASCII, &isascii));
318:   if (isascii) PetscCall(DMPlexTransformView_Ascii(tr, v));
319:   PetscTryTypeMethod(tr, view, v);
320:   PetscFunctionReturn(PETSC_SUCCESS);
321: }

323: /*@
324:   DMPlexTransformSetFromOptions - Sets parameters in a transform from values in the options database

326:   Collective

328:   Input Parameter:
329: . tr - the `DMPlexTransform` object to set options for

331:   Options Database Keys:
332: + -dm_plex_transform_type type               - Set the transform type, e.g. refine_regular
333: . -dm_plex_transform_label_match_strata      - Only label points of the same stratum as the producing point
334: . -dm_plex_transform_label_replica_inc inc   - Increment for the label value to be multiplied by the replica number, so that the new label value is oldValue + r * inc
335: . -dm_plex_transform_active name             - Name for active mesh label
336: - -dm_plex_transform_active_values v0,v1,... - Values in the active label

338:   Level: intermediate

340: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformView()`, `DMPlexTransformCreate()`
341: @*/
342: PetscErrorCode DMPlexTransformSetFromOptions(DMPlexTransform tr)
343: {
344:   char        typeName[1024], active[PETSC_MAX_PATH_LEN];
345:   const char *defName = DMPLEXREFINEREGULAR;
346:   PetscBool   flg, match;

348:   PetscFunctionBegin;
350:   PetscObjectOptionsBegin((PetscObject)tr);
351:   PetscCall(PetscOptionsFList("-dm_plex_transform_type", "DMPlexTransform", "DMPlexTransformSetType", DMPlexTransformList, defName, typeName, 1024, &flg));
352:   if (flg) PetscCall(DMPlexTransformSetType(tr, typeName));
353:   else if (!((PetscObject)tr)->type_name) PetscCall(DMPlexTransformSetType(tr, defName));
354:   PetscCall(PetscOptionsBool("-dm_plex_transform_label_match_strata", "Only label points of the same stratum as the producing point", "", tr->labelMatchStrata, &match, &flg));
355:   if (flg) PetscCall(DMPlexTransformSetMatchStrata(tr, match));
356:   PetscCall(PetscOptionsInt("-dm_plex_transform_label_replica_inc", "Increment for the label value to be multiplied by the replica number", "", tr->labelReplicaInc, &tr->labelReplicaInc, NULL));
357:   PetscCall(PetscOptionsString("-dm_plex_transform_active", "Name for active mesh label", "DMPlexTransformSetActive", active, active, sizeof(active), &flg));
358:   if (flg) {
359:     DM       dm;
360:     DMLabel  label;
361:     PetscInt values[16];
362:     PetscInt n = 16;

364:     PetscCall(DMPlexTransformGetDM(tr, &dm));
365:     PetscCall(DMGetLabel(dm, active, &label));
366:     PetscCall(PetscOptionsIntArray("-dm_plex_transform_active_values", "The label values to be active", "DMPlexTransformSetActive", values, &n, &flg));
367:     if (flg && n) {
368:       DMLabel newlabel;

370:       PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Active", &newlabel));
371:       for (PetscInt i = 0; i < n; ++i) {
372:         IS is;

374:         PetscCall(DMLabelGetStratumIS(label, values[i], &is));
375:         PetscCall(DMLabelInsertIS(newlabel, is, values[i]));
376:         PetscCall(ISDestroy(&is));
377:       }
378:       PetscCall(DMPlexTransformSetActive(tr, newlabel));
379:       PetscCall(DMLabelDestroy(&newlabel));
380:     } else {
381:       PetscCall(DMPlexTransformSetActive(tr, label));
382:     }
383:   }
384:   PetscTryTypeMethod(tr, setfromoptions, PetscOptionsObject);
385:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
386:   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)tr, PetscOptionsObject));
387:   PetscOptionsEnd();
388:   PetscFunctionReturn(PETSC_SUCCESS);
389: }

391: /*@
392:   DMPlexTransformDestroy - Destroys a `DMPlexTransform`

394:   Collective

396:   Input Parameter:
397: . tr - the transform object to destroy

399:   Level: beginner

401: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformView()`, `DMPlexTransformCreate()`
402: @*/
403: PetscErrorCode DMPlexTransformDestroy(DMPlexTransform *tr)
404: {
405:   PetscInt c;

407:   PetscFunctionBegin;
408:   if (!*tr) PetscFunctionReturn(PETSC_SUCCESS);
410:   if (--((PetscObject)*tr)->refct > 0) {
411:     *tr = NULL;
412:     PetscFunctionReturn(PETSC_SUCCESS);
413:   }

415:   PetscTryTypeMethod(*tr, destroy);
416:   PetscCall(DMDestroy(&(*tr)->dm));
417:   PetscCall(DMLabelDestroy(&(*tr)->active));
418:   PetscCall(DMLabelDestroy(&(*tr)->trType));
419:   PetscCall(PetscFree2((*tr)->ctOrderOld, (*tr)->ctOrderInvOld));
420:   PetscCall(PetscFree2((*tr)->ctOrderNew, (*tr)->ctOrderInvNew));
421:   PetscCall(PetscFree2((*tr)->ctStart, (*tr)->ctStartNew));
422:   PetscCall(PetscFree((*tr)->offset));
423:   PetscCall(PetscFree2((*tr)->depthStart, (*tr)->depthEnd));
424:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
425:     PetscCall(PetscFEDestroy(&(*tr)->coordFE[c]));
426:     PetscCall(PetscFEGeomDestroy(&(*tr)->refGeom[c]));
427:   }
428:   if ((*tr)->trVerts) {
429:     for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
430:       DMPolytopeType *rct;
431:       PetscInt       *rsize, *rcone, *rornt, Nct, n, r;

433:       if (DMPolytopeTypeGetDim((DMPolytopeType)c) > 0 && c != DM_POLYTOPE_UNKNOWN_CELL && c != DM_POLYTOPE_UNKNOWN_FACE) {
434:         PetscCall(DMPlexTransformCellTransform(*tr, (DMPolytopeType)c, 0, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
435:         for (n = 0; n < Nct; ++n) {
436:           if (rct[n] == DM_POLYTOPE_POINT) continue;
437:           for (r = 0; r < rsize[n]; ++r) PetscCall(PetscFree((*tr)->trSubVerts[c][rct[n]][r]));
438:           PetscCall(PetscFree((*tr)->trSubVerts[c][rct[n]]));
439:         }
440:       }
441:       PetscCall(PetscFree((*tr)->trSubVerts[c]));
442:       PetscCall(PetscFree((*tr)->trVerts[c]));
443:     }
444:   }
445:   PetscCall(PetscFree3((*tr)->trNv, (*tr)->trVerts, (*tr)->trSubVerts));
446:   PetscCall(PetscFree2((*tr)->coordFE, (*tr)->refGeom));
447:   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
448:   PetscCall(PetscHeaderDestroy(tr));
449:   PetscFunctionReturn(PETSC_SUCCESS);
450: }

452: static PetscErrorCode DMPlexTransformCreateOffset_Internal(DMPlexTransform tr, PetscInt ctOrderOld[], PetscInt ctStart[], PetscInt **offset)
453: {
454:   DMLabel  trType = tr->trType;
455:   PetscInt c, cN, *off;

457:   PetscFunctionBegin;
458:   if (trType) {
459:     DM              dm;
460:     IS              rtIS;
461:     const PetscInt *reftypes;
462:     PetscInt        Nrt;

464:     PetscCall(DMPlexTransformGetDM(tr, &dm));
465:     PetscCall(DMLabelGetNumValues(trType, &Nrt));
466:     PetscCall(DMLabelGetValueIS(trType, &rtIS));
467:     PetscCall(ISGetIndices(rtIS, &reftypes));
468:     PetscCall(PetscCalloc1(Nrt * DM_NUM_POLYTOPES, &off));
469:     for (PetscInt r = 0; r < Nrt; ++r) {
470:       const PetscInt  rt = reftypes[r];
471:       IS              rtIS;
472:       const PetscInt *points;
473:       DMPolytopeType  ct;
474:       PetscInt        np, p;

476:       PetscCall(DMLabelGetStratumIS(trType, rt, &rtIS));
477:       PetscCall(ISGetLocalSize(rtIS, &np));
478:       PetscCall(ISGetIndices(rtIS, &points));
479:       if (!np) continue;
480:       p = points[0];
481:       PetscCall(ISRestoreIndices(rtIS, &points));
482:       PetscCall(ISDestroy(&rtIS));
483:       PetscCall(DMPlexGetCellType(dm, p, &ct));
484:       for (cN = DM_POLYTOPE_POINT; cN < DM_NUM_POLYTOPES; ++cN) {
485:         const DMPolytopeType ctNew = (DMPolytopeType)cN;
486:         DMPolytopeType      *rct;
487:         PetscInt            *rsize, *cone, *ornt;
488:         PetscInt             Nct, n, s;

490:         if (DMPolytopeTypeGetDim(ct) < 0 || DMPolytopeTypeGetDim(ctNew) < 0) {
491:           off[r * DM_NUM_POLYTOPES + ctNew] = -1;
492:           break;
493:         }
494:         off[r * DM_NUM_POLYTOPES + ctNew] = 0;
495:         for (s = 0; s <= r; ++s) {
496:           const PetscInt st = reftypes[s];
497:           DMPolytopeType sct;
498:           PetscInt       q, qrt;

500:           PetscCall(DMLabelGetStratumIS(trType, st, &rtIS));
501:           PetscCall(ISGetLocalSize(rtIS, &np));
502:           PetscCall(ISGetIndices(rtIS, &points));
503:           if (!np) continue;
504:           q = points[0];
505:           PetscCall(ISRestoreIndices(rtIS, &points));
506:           PetscCall(ISDestroy(&rtIS));
507:           PetscCall(DMPlexGetCellType(dm, q, &sct));
508:           PetscCall(DMPlexTransformCellTransform(tr, sct, q, &qrt, &Nct, &rct, &rsize, &cone, &ornt));
509:           PetscCheck(st == qrt, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Refine type %" PetscInt_FMT " of point %" PetscInt_FMT " does not match predicted type %" PetscInt_FMT, qrt, q, st);
510:           if (st == rt) {
511:             for (n = 0; n < Nct; ++n)
512:               if (rct[n] == ctNew) break;
513:             if (n == Nct) off[r * DM_NUM_POLYTOPES + ctNew] = -1;
514:             break;
515:           }
516:           for (n = 0; n < Nct; ++n) {
517:             if (rct[n] == ctNew) {
518:               PetscInt sn;

520:               PetscCall(DMLabelGetStratumSize(trType, st, &sn));
521:               off[r * DM_NUM_POLYTOPES + ctNew] += sn * rsize[n];
522:             }
523:           }
524:         }
525:       }
526:     }
527:     PetscCall(ISRestoreIndices(rtIS, &reftypes));
528:     PetscCall(ISDestroy(&rtIS));
529:   } else {
530:     PetscCall(PetscCalloc1(DM_NUM_POLYTOPES * DM_NUM_POLYTOPES, &off));
531:     for (c = DM_POLYTOPE_POINT; c < DM_NUM_POLYTOPES; ++c) {
532:       const DMPolytopeType ct = (DMPolytopeType)c;
533:       for (cN = DM_POLYTOPE_POINT; cN < DM_NUM_POLYTOPES; ++cN) {
534:         const DMPolytopeType ctNew = (DMPolytopeType)cN;
535:         DMPolytopeType      *rct;
536:         PetscInt            *rsize, *cone, *ornt;
537:         PetscInt             Nct, n, i;

539:         if (DMPolytopeTypeGetDim(ct) < 0 || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE || DMPolytopeTypeGetDim(ctNew) < 0 || ctNew == DM_POLYTOPE_UNKNOWN_CELL || ctNew == DM_POLYTOPE_UNKNOWN_FACE) {
540:           off[ct * DM_NUM_POLYTOPES + ctNew] = -1;
541:           continue;
542:         }
543:         off[ct * DM_NUM_POLYTOPES + ctNew] = 0;
544:         for (i = DM_POLYTOPE_POINT; i < DM_NUM_POLYTOPES; ++i) {
545:           const DMPolytopeType ict  = (DMPolytopeType)ctOrderOld[i];
546:           const DMPolytopeType ictn = (DMPolytopeType)ctOrderOld[i + 1];

548:           PetscCall(DMPlexTransformCellTransform(tr, ict, PETSC_DETERMINE, NULL, &Nct, &rct, &rsize, &cone, &ornt));
549:           if (ict == ct) {
550:             for (n = 0; n < Nct; ++n)
551:               if (rct[n] == ctNew) break;
552:             if (n == Nct) off[ct * DM_NUM_POLYTOPES + ctNew] = -1;
553:             break;
554:           }
555:           for (n = 0; n < Nct; ++n)
556:             if (rct[n] == ctNew) off[ct * DM_NUM_POLYTOPES + ctNew] += (ctStart[ictn] - ctStart[ict]) * rsize[n];
557:         }
558:       }
559:     }
560:   }
561:   *offset = off;
562:   PetscFunctionReturn(PETSC_SUCCESS);
563: }

565: /*@
566:   DMPlexTransformSetUp - Create the tables that drive the transform

568:   Input Parameter:
569: . tr - The `DMPlexTransform` object

571:   Level: intermediate

573: .seealso: [](plex_transform_table), [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
574: @*/
575: PetscErrorCode DMPlexTransformSetUp(DMPlexTransform tr)
576: {
577:   DMPolytopeType ctCell;
578:   DM             dm;
579:   PetscInt       pStart, pEnd, p, c, celldim = 0;

581:   PetscFunctionBegin;
583:   if (tr->setupcalled) PetscFunctionReturn(PETSC_SUCCESS);
584:   PetscCall(DMPlexTransformGetDM(tr, &dm));
585:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_SetUp, tr, dm, 0, 0));
586:   PetscTryTypeMethod(tr, setup);
587:   PetscCall(DMSetSnapToGeomModel(dm, NULL));
588:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));

590:   if (pEnd > pStart) {
591:     // Ignore cells hanging off of embedded surfaces
592:     PetscInt c = pStart;

594:     ctCell = DM_POLYTOPE_FV_GHOST;
595:     while (DMPolytopeTypeGetDim(ctCell) < 0) PetscCall(DMPlexGetCellType(dm, c++, &ctCell));
596:   } else {
597:     PetscInt dim;

599:     PetscCall(DMGetDimension(dm, &dim));
600:     switch (dim) {
601:     case 0:
602:       ctCell = DM_POLYTOPE_POINT;
603:       break;
604:     case 1:
605:       ctCell = DM_POLYTOPE_SEGMENT;
606:       break;
607:     case 2:
608:       ctCell = DM_POLYTOPE_TRIANGLE;
609:       break;
610:     case 3:
611:       ctCell = DM_POLYTOPE_TETRAHEDRON;
612:       break;
613:     default:
614:       ctCell = DM_POLYTOPE_UNKNOWN;
615:     }
616:   }
617:   PetscCall(DMPlexCreateCellTypeOrder_Internal(dm, DMPolytopeTypeGetDim(ctCell), &tr->ctOrderOld, &tr->ctOrderInvOld));
618:   for (p = pStart; p < pEnd; ++p) {
619:     DMPolytopeType  ct;
620:     DMPolytopeType *rct;
621:     PetscInt       *rsize, *cone, *ornt;
622:     PetscInt        Nct;

624:     PetscCall(DMPlexGetCellType(dm, p, &ct));
625:     PetscCheck(ct != DM_POLYTOPE_UNKNOWN && ct != DM_POLYTOPE_UNKNOWN_CELL && ct != DM_POLYTOPE_UNKNOWN_FACE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell type for point %" PetscInt_FMT, p);
626:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &cone, &ornt));
627:     for (PetscInt n = 0; n < Nct; ++n) celldim = PetscMax(celldim, DMPolytopeTypeGetDim(rct[n]));
628:   }
629:   PetscCall(DMPlexCreateCellTypeOrder_Internal(NULL, celldim, &tr->ctOrderNew, &tr->ctOrderInvNew));
630:   /* Construct sizes and offsets for each cell type */
631:   if (!tr->ctStart) {
632:     PetscInt *ctS, *ctSN, *ctC, *ctCN;

634:     PetscCall(PetscCalloc2(DM_NUM_POLYTOPES + 1, &ctS, DM_NUM_POLYTOPES + 1, &ctSN));
635:     PetscCall(PetscCalloc2(DM_NUM_POLYTOPES + 1, &ctC, DM_NUM_POLYTOPES + 1, &ctCN));
636:     for (p = pStart; p < pEnd; ++p) {
637:       DMPolytopeType  ct;
638:       DMPolytopeType *rct;
639:       PetscInt       *rsize, *cone, *ornt;
640:       PetscInt        Nct;

642:       PetscCall(DMPlexGetCellType(dm, p, &ct));
643:       PetscCheck(ct != DM_POLYTOPE_UNKNOWN && ct != DM_POLYTOPE_UNKNOWN_CELL && ct != DM_POLYTOPE_UNKNOWN_FACE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell type for point %" PetscInt_FMT, p);
644:       ++ctC[ct];
645:       PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &cone, &ornt));
646:       for (PetscInt n = 0; n < Nct; ++n) ctCN[rct[n]] += rsize[n];
647:     }
648:     for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
649:       const PetscInt cto  = tr->ctOrderOld[c];
650:       const PetscInt cton = tr->ctOrderOld[c + 1];
651:       const PetscInt ctn  = tr->ctOrderNew[c];
652:       const PetscInt ctnn = tr->ctOrderNew[c + 1];

654:       ctS[cton]  = ctS[cto] + ctC[cto];
655:       ctSN[ctnn] = ctSN[ctn] + ctCN[ctn];
656:     }
657:     PetscCall(PetscFree2(ctC, ctCN));
658:     tr->ctStart    = ctS;
659:     tr->ctStartNew = ctSN;
660:   }
661:   PetscCall(DMPlexTransformCreateOffset_Internal(tr, tr->ctOrderOld, tr->ctStart, &tr->offset));
662:   // Compute depth information
663:   tr->depth = -1;
664:   for (c = 0; c < DM_NUM_POLYTOPES; ++c)
665:     if (tr->ctStartNew[tr->ctOrderNew[c + 1]] > tr->ctStartNew[tr->ctOrderNew[c]]) tr->depth = PetscMax(tr->depth, DMPolytopeTypeGetDim((DMPolytopeType)tr->ctOrderNew[c]));
666:   PetscCall(PetscMalloc2(tr->depth + 1, &tr->depthStart, tr->depth + 1, &tr->depthEnd));
667:   for (PetscInt d = 0; d <= tr->depth; ++d) {
668:     tr->depthStart[d] = PETSC_INT_MAX;
669:     tr->depthEnd[d]   = -1;
670:   }
671:   for (c = 0; c < DM_NUM_POLYTOPES; ++c) {
672:     const PetscInt dep = DMPolytopeTypeGetDim((DMPolytopeType)tr->ctOrderNew[c]);

674:     if (tr->ctStartNew[tr->ctOrderNew[c + 1]] <= tr->ctStartNew[tr->ctOrderNew[c]]) continue;
675:     tr->depthStart[dep] = PetscMin(tr->depthStart[dep], tr->ctStartNew[tr->ctOrderNew[c]]);
676:     tr->depthEnd[dep]   = PetscMax(tr->depthEnd[dep], tr->ctStartNew[tr->ctOrderNew[c + 1]]);
677:   }
678:   tr->setupcalled = PETSC_TRUE;
679:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_SetUp, tr, dm, 0, 0));
680:   PetscFunctionReturn(PETSC_SUCCESS);
681: }

683: /*@
684:   DMPlexTransformGetDM - Get the base `DM` for the transform

686:   Input Parameter:
687: . tr - The `DMPlexTransform` object

689:   Output Parameter:
690: . dm - The original `DM` which will be transformed

692:   Level: intermediate

694: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformSetDM()`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
695: @*/
696: PetscErrorCode DMPlexTransformGetDM(DMPlexTransform tr, DM *dm)
697: {
698:   PetscFunctionBegin;
700:   PetscAssertPointer(dm, 2);
701:   *dm = tr->dm;
702:   PetscFunctionReturn(PETSC_SUCCESS);
703: }

705: /*@
706:   DMPlexTransformSetDM - Set the base `DM` for the transform

708:   Input Parameters:
709: + tr - The `DMPlexTransform` object
710: - dm - The original `DM` which will be transformed

712:   Level: intermediate

714:   Note:
715:   The user does not typically call this, as it is called by `DMPlexTransformApply()`.

717: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformGetDM()`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
718: @*/
719: PetscErrorCode DMPlexTransformSetDM(DMPlexTransform tr, DM dm)
720: {
721:   PetscFunctionBegin;
723:   if (dm) {
725:     PetscCall(PetscObjectReference((PetscObject)dm));
726:   }
727:   PetscCall(DMDestroy(&tr->dm));
728:   tr->dm = dm;
729:   PetscFunctionReturn(PETSC_SUCCESS);
730: }

732: /*@
733:   DMPlexTransformGetActive - Get the `DMLabel` marking the active points for the transform

735:   Input Parameter:
736: . tr - The `DMPlexTransform` object

738:   Output Parameter:
739: . active - The `DMLabel` indicating which points will be transformed

741:   Level: intermediate

743: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformSetActive()`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
744: @*/
745: PetscErrorCode DMPlexTransformGetActive(DMPlexTransform tr, DMLabel *active)
746: {
747:   PetscFunctionBegin;
749:   PetscAssertPointer(active, 2);
750:   *active = tr->active;
751:   PetscFunctionReturn(PETSC_SUCCESS);
752: }

754: /*@
755:   DMPlexTransformSetActive - Set the `DMLabel` marking the active points for the transform

757:   Input Parameters:
758: + tr     - The `DMPlexTransform` object
759: - active - The `DMLabel` indicating which points will be transformed

761:   Level: intermediate

763:   Note:
764:   This only applies to transforms listed in [](plex_transform_table) that operate on a subset of the mesh.

766: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformGetActive()`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
767: @*/
768: PetscErrorCode DMPlexTransformSetActive(DMPlexTransform tr, DMLabel active)
769: {
770:   PetscFunctionBegin;
773:   PetscCall(PetscObjectReference((PetscObject)active));
774:   PetscCall(DMLabelDestroy(&tr->active));
775:   tr->active = active;
776:   PetscFunctionReturn(PETSC_SUCCESS);
777: }

779: /*@
780:   DMPlexTransformGetTransformTypes - Get the `DMLabel` marking the transform type of each point for the transform

782:   Input Parameter:
783: . tr - The `DMPlexTransform` object

785:   Output Parameter:
786: . trType - The `DMLabel` indicating the transform type for each point

788:   Level: intermediate

790: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexSetTransformType()`, `DMPlexTransformGetActive()`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
791: @*/
792: PetscErrorCode DMPlexTransformGetTransformTypes(DMPlexTransform tr, DMLabel *trType)
793: {
794:   PetscFunctionBegin;
796:   PetscAssertPointer(trType, 2);
797:   *trType = tr->trType;
798:   PetscFunctionReturn(PETSC_SUCCESS);
799: }

801: /*@
802:   DMPlexTransformSetTransformTypes - Set the `DMLabel` marking the transform type of each point for the transform

804:   Input Parameters:
805: + tr     - The `DMPlexTransform` object
806: - trType - The original `DM` which will be transformed

808:   Level: intermediate

810: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformGetTransformTypes()`, `DMPlexTransformGetActive())`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
811: @*/
812: PetscErrorCode DMPlexTransformSetTransformTypes(DMPlexTransform tr, DMLabel trType)
813: {
814:   PetscFunctionBegin;
817:   PetscCall(PetscObjectReference((PetscObject)trType));
818:   PetscCall(DMLabelDestroy(&tr->trType));
819:   tr->trType = trType;
820:   PetscFunctionReturn(PETSC_SUCCESS);
821: }

823: static PetscErrorCode DMPlexTransformGetCoordinateFE(DMPlexTransform tr, DMPolytopeType ct, PetscFE *fe)
824: {
825:   PetscFunctionBegin;
826:   if (!tr->coordFE[ct]) {
827:     PetscInt dim, cdim;

829:     dim = DMPolytopeTypeGetDim(ct);
830:     PetscCall(DMGetCoordinateDim(tr->dm, &cdim));
831:     PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, cdim, ct, 1, PETSC_DETERMINE, &tr->coordFE[ct]));
832:     {
833:       PetscDualSpace  dsp;
834:       PetscQuadrature quad;
835:       DM              K;
836:       PetscFEGeom    *cg;
837:       PetscScalar    *Xq;
838:       PetscReal      *xq, *wq;
839:       PetscInt        Nq;

841:       PetscCall(DMPlexTransformGetCellVertices(tr, ct, &Nq, &Xq));
842:       PetscCall(PetscMalloc1(Nq * cdim, &xq));
843:       for (PetscInt q = 0; q < Nq * cdim; ++q) xq[q] = PetscRealPart(Xq[q]);
844:       PetscCall(PetscMalloc1(Nq, &wq));
845:       for (PetscInt q = 0; q < Nq; ++q) wq[q] = 1.0;
846:       PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &quad));
847:       PetscCall(PetscQuadratureSetData(quad, dim, 1, Nq, xq, wq));
848:       PetscCall(PetscFESetQuadrature(tr->coordFE[ct], quad));

850:       PetscCall(PetscFEGetDualSpace(tr->coordFE[ct], &dsp));
851:       PetscCall(PetscDualSpaceGetDM(dsp, &K));
852:       PetscCall(PetscFEGeomCreate(quad, 1, cdim, PETSC_FEGEOM_BASIC, &tr->refGeom[ct]));
853:       cg = tr->refGeom[ct];
854:       PetscCall(DMPlexComputeCellGeometryFEM(K, 0, NULL, cg->v, cg->J, cg->invJ, cg->detJ));
855:       PetscCall(PetscQuadratureDestroy(&quad));
856:     }
857:   }
858:   *fe = tr->coordFE[ct];
859:   PetscFunctionReturn(PETSC_SUCCESS);
860: }

862: PetscErrorCode DMPlexTransformSetDimensions_Internal(DMPlexTransform tr, DM dm, DM tdm)
863: {
864:   PetscInt dim, cdim;

866:   PetscFunctionBegin;
867:   PetscCall(DMGetDimension(dm, &dim));
868:   PetscCall(DMSetDimension(tdm, dim));
869:   PetscCall(DMGetCoordinateDim(dm, &cdim));
870:   PetscCall(DMSetCoordinateDim(tdm, cdim));
871:   PetscFunctionReturn(PETSC_SUCCESS);
872: }

874: /*@
875:   DMPlexTransformSetDimensions - Set the dimensions for the transformed `DM`

877:   Input Parameters:
878: + tr - The `DMPlexTransform` object
879: - dm - The original `DM`

881:   Output Parameter:
882: . trdm - The transformed `DM`

884:   Level: advanced

886: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
887: @*/
888: PetscErrorCode DMPlexTransformSetDimensions(DMPlexTransform tr, DM dm, DM trdm)
889: {
890:   PetscFunctionBegin;
891:   PetscUseTypeMethod(tr, setdimensions, dm, trdm);
892:   PetscFunctionReturn(PETSC_SUCCESS);
893: }

895: PetscErrorCode DMPlexTransformGetChart(DMPlexTransform tr, PetscInt *pStart, PetscInt *pEnd)
896: {
897:   PetscFunctionBegin;
898:   if (pStart) *pStart = 0;
899:   if (pEnd) *pEnd = tr->ctStartNew[tr->ctOrderNew[DM_NUM_POLYTOPES]];
900:   PetscFunctionReturn(PETSC_SUCCESS);
901: }

903: PetscErrorCode DMPlexTransformGetCellType(DMPlexTransform tr, PetscInt cell, DMPolytopeType *celltype)
904: {
905:   PetscInt ctNew;

907:   PetscFunctionBegin;
909:   PetscAssertPointer(celltype, 3);
910:   /* TODO Can do bisection since everything is sorted */
911:   for (ctNew = DM_POLYTOPE_POINT; ctNew < DM_NUM_POLYTOPES; ++ctNew) {
912:     PetscInt ctSN = tr->ctStartNew[ctNew], ctEN = tr->ctStartNew[tr->ctOrderNew[tr->ctOrderInvNew[ctNew] + 1]];

914:     if (cell >= ctSN && cell < ctEN) break;
915:   }
916:   PetscCheck(ctNew < DM_NUM_POLYTOPES, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " cannot be located in the transformed mesh", cell);
917:   *celltype = (DMPolytopeType)ctNew;
918:   PetscFunctionReturn(PETSC_SUCCESS);
919: }

921: PetscErrorCode DMPlexTransformGetCellTypeStratum(DMPlexTransform tr, DMPolytopeType celltype, PetscInt *start, PetscInt *end)
922: {
923:   PetscFunctionBegin;
925:   if (start) *start = tr->ctStartNew[celltype];
926:   if (end) *end = tr->ctStartNew[tr->ctOrderNew[tr->ctOrderInvNew[celltype] + 1]];
927:   PetscFunctionReturn(PETSC_SUCCESS);
928: }

930: PetscErrorCode DMPlexTransformGetDepth(DMPlexTransform tr, PetscInt *depth)
931: {
932:   PetscFunctionBegin;
934:   *depth = tr->depth;
935:   PetscFunctionReturn(PETSC_SUCCESS);
936: }

938: PetscErrorCode DMPlexTransformGetDepthStratum(DMPlexTransform tr, PetscInt depth, PetscInt *start, PetscInt *end)
939: {
940:   PetscFunctionBegin;
942:   if (start) *start = tr->depthStart[depth];
943:   if (end) *end = tr->depthEnd[depth];
944:   PetscFunctionReturn(PETSC_SUCCESS);
945: }

947: /*@
948:   DMPlexTransformGetMatchStrata - Get the flag which determines what points get added to the transformed labels

950:   Not Collective

952:   Input Parameter:
953: . tr - The `DMPlexTransform`

955:   Output Parameter:
956: . match - If `PETSC_TRUE`, only add produced points at the same stratum as the original point to new labels

958:   Level: intermediate

960: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformSetMatchStrata()`, `DMPlexGetPointDepth()`
961: @*/
962: PetscErrorCode DMPlexTransformGetMatchStrata(DMPlexTransform tr, PetscBool *match)
963: {
964:   PetscFunctionBegin;
966:   PetscAssertPointer(match, 2);
967:   *match = tr->labelMatchStrata;
968:   PetscFunctionReturn(PETSC_SUCCESS);
969: }

971: /*@
972:   DMPlexTransformSetMatchStrata - Set the flag which determines what points get added to the transformed labels

974:   Not Collective

976:   Input Parameters:
977: + tr    - The `DMPlexTransform`
978: - match - If `PETSC_TRUE`, only add produced points at the same stratum as the original point to new labels

980:   Level: intermediate

982: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformGetMatchStrata()`, `DMPlexGetPointDepth()`
983: @*/
984: PetscErrorCode DMPlexTransformSetMatchStrata(DMPlexTransform tr, PetscBool match)
985: {
986:   PetscFunctionBegin;
988:   tr->labelMatchStrata = match;
989:   PetscFunctionReturn(PETSC_SUCCESS);
990: }

992: /*@
993:   DMPlexTransformCheck - Verify that the given `DM`, produced by this `DMPlexTransform`, is valid

995:   Input Parameters:
996: + tr - The `DMPlexTransform` object
997: - dm - The `DM` to check

999:   Level: advanced

1001: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
1002: @*/
1003: PetscErrorCode DMPlexTransformCheck(DMPlexTransform tr, DM dm)
1004: {
1005:   PetscFunctionBegin;
1006:   PetscTryTypeMethod(tr, check, dm);
1007:   PetscFunctionReturn(PETSC_SUCCESS);
1008: }

1010: /*@
1011:   DMPlexTransformGetTargetPoint - Get the number of a point in the transformed mesh based on information from the original mesh.

1013:   Not Collective

1015:   Input Parameters:
1016: + tr    - The `DMPlexTransform`
1017: . ct    - The type of the original point which produces the new point
1018: . ctNew - The type of the new point
1019: . p     - The original point which produces the new point
1020: - r     - The replica number of the new point, meaning it is the rth point of type `ctNew` produced from `p`

1022:   Output Parameter:
1023: . pNew - The new point number

1025:   Level: developer

1027: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformGetSourcePoint()`, `DMPlexTransformCellTransform()`
1028: @*/
1029: PetscErrorCode DMPlexTransformGetTargetPoint(DMPlexTransform tr, DMPolytopeType ct, DMPolytopeType ctNew, PetscInt p, PetscInt r, PetscInt *pNew)
1030: {
1031:   DMPolytopeType *rct;
1032:   PetscInt       *rsize, *cone, *ornt;
1033:   PetscInt        rt, Nct, n, off, rp;
1034:   DMLabel         trType = tr->trType;
1035:   PetscInt        ctS = tr->ctStart[ct], ctE = tr->ctStart[tr->ctOrderOld[tr->ctOrderInvOld[ct] + 1]];
1036:   PetscInt        ctSN = tr->ctStartNew[ctNew], ctEN = tr->ctStartNew[tr->ctOrderNew[tr->ctOrderInvNew[ctNew] + 1]];
1037:   PetscInt        newp = ctSN, cind;

1039:   PetscFunctionBeginHot;
1040:   PetscCheck(p >= ctS && p < ctE, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is not a %s [%" PetscInt_FMT ", %" PetscInt_FMT ")", p, DMPolytopeTypes[ct], ctS, ctE);
1041:   PetscCall(DMPlexTransformCellTransform(tr, ct, p, &rt, &Nct, &rct, &rsize, &cone, &ornt));
1042:   if (trType) {
1043:     PetscCall(DMLabelGetValueIndex(trType, rt, &cind));
1044:     PetscCall(DMLabelGetStratumPointIndex(trType, rt, p, &rp));
1045:     PetscCheck(rp >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell type %s point %" PetscInt_FMT " does not have refine type %" PetscInt_FMT, DMPolytopeTypes[ct], p, rt);
1046:   } else {
1047:     cind = ct;
1048:     rp   = p - ctS;
1049:   }
1050:   off = tr->offset[cind * DM_NUM_POLYTOPES + ctNew];
1051:   PetscCheck(off >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell type %s (%" PetscInt_FMT ") of point %" PetscInt_FMT " does not produce type %s for transform %s", DMPolytopeTypes[ct], rt, p, DMPolytopeTypes[ctNew], tr->hdr.type_name);
1052:   newp += off;
1053:   for (n = 0; n < Nct; ++n) {
1054:     if (rct[n] == ctNew) {
1055:       PetscCheck(!rsize[n] || r < rsize[n], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Replica number %" PetscInt_FMT " for point %" PetscInt_FMT " should be in [0, %" PetscInt_FMT ") for subcell type %s in cell type %s", r, p, rsize[n], DMPolytopeTypes[rct[n]], DMPolytopeTypes[ct]);
1056:       newp += rp * rsize[n] + r;
1057:       if (!(newp >= ctSN && newp <= ctEN)) {
1058:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "Problem with point %" PetscInt_FMT " %s replica %" PetscInt_FMT "\n", p, DMPolytopeTypes[ct], r));
1059:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  n %" PetscInt_FMT " rsize %" PetscInt_FMT " rt %" PetscInt_FMT " cind %" PetscInt_FMT " rp %" PetscInt_FMT "\n", n, rsize[n], rt, cind, rp));
1060:       }
1061:       break;
1062:     }
1063:   }

1065:   PetscCheck(newp >= ctSN && newp < ctEN, PETSC_COMM_SELF, PETSC_ERR_PLIB, "New point %" PetscInt_FMT " is not a %s [%" PetscInt_FMT ", %" PetscInt_FMT ")", newp, DMPolytopeTypes[ctNew], ctSN, ctEN);
1066:   *pNew = newp;
1067:   PetscFunctionReturn(PETSC_SUCCESS);
1068: }

1070: /*@
1071:   DMPlexTransformGetSourcePoint - Get the number of a point in the original mesh based on information from the transformed mesh.

1073:   Not Collective

1075:   Input Parameters:
1076: + tr   - The `DMPlexTransform`
1077: - pNew - The new point number

1079:   Output Parameters:
1080: + ct    - The type of the original point which produces the new point
1081: . ctNew - The type of the new point
1082: . p     - The original point which produces the new point
1083: - r     - The replica number of the new point, meaning it is the rth point of type ctNew produced from p

1085:   Level: developer

1087: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformGetTargetPoint()`, `DMPlexTransformCellTransform()`
1088: @*/
1089: PetscErrorCode DMPlexTransformGetSourcePoint(DMPlexTransform tr, PetscInt pNew, DMPolytopeType *ct, DMPolytopeType *ctNew, PetscInt *p, PetscInt *r)
1090: {
1091:   DMLabel         trType = tr->trType;
1092:   DMPolytopeType *rct, ctN;
1093:   PetscInt       *rsize, *cone, *ornt;
1094:   PetscInt        rt = -1, rtTmp, Nct, n, rp = 0, rO = 0, pO;
1095:   PetscInt        offset = -1, ctS, ctE, ctO = 0, ctTmp, rtS;

1097:   PetscFunctionBegin;
1098:   PetscCall(DMPlexTransformGetCellType(tr, pNew, &ctN));
1099:   if (trType) {
1100:     DM              dm;
1101:     IS              rtIS;
1102:     const PetscInt *reftypes;
1103:     PetscInt        Nrt, r, rtStart;

1105:     PetscCall(DMPlexTransformGetDM(tr, &dm));
1106:     PetscCall(DMLabelGetNumValues(trType, &Nrt));
1107:     PetscCall(DMLabelGetValueIS(trType, &rtIS));
1108:     PetscCall(ISGetIndices(rtIS, &reftypes));
1109:     for (r = 0; r < Nrt; ++r) {
1110:       const PetscInt off = tr->offset[r * DM_NUM_POLYTOPES + ctN];

1112:       if (tr->ctStartNew[ctN] + off > pNew) continue;
1113:       /* Check that any of this refinement type exist */
1114:       /* TODO Actually keep track of the number produced here instead */
1115:       if (off > offset) {
1116:         rt     = reftypes[r];
1117:         offset = off;
1118:       }
1119:     }
1120:     PetscCall(ISRestoreIndices(rtIS, &reftypes));
1121:     PetscCall(ISDestroy(&rtIS));
1122:     PetscCheck(offset >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Source cell type for target point %" PetscInt_FMT " could be not found", pNew);
1123:     /* TODO Map refinement types to cell types */
1124:     PetscCall(DMLabelGetStratumBounds(trType, rt, &rtStart, NULL));
1125:     PetscCheck(rtStart >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Refinement type %" PetscInt_FMT " has no source points", rt);
1126:     for (ctO = 0; ctO < DM_NUM_POLYTOPES; ++ctO) {
1127:       PetscInt ctS = tr->ctStart[ctO], ctE = tr->ctStart[tr->ctOrderOld[tr->ctOrderInvOld[ctO] + 1]];

1129:       if ((rtStart >= ctS) && (rtStart < ctE)) break;
1130:     }
1131:     PetscCheck(ctO != DM_NUM_POLYTOPES, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not determine a cell type for refinement type %" PetscInt_FMT, rt);
1132:   } else {
1133:     for (ctTmp = 0; ctTmp < DM_NUM_POLYTOPES; ++ctTmp) {
1134:       const PetscInt off = tr->offset[ctTmp * DM_NUM_POLYTOPES + ctN];

1136:       if (tr->ctStartNew[ctN] + off > pNew) continue;
1137:       if (tr->ctStart[tr->ctOrderOld[tr->ctOrderInvOld[ctTmp] + 1]] <= tr->ctStart[ctTmp]) continue;
1138:       /* TODO Actually keep track of the number produced here instead */
1139:       if (off > offset) {
1140:         ctO    = ctTmp;
1141:         offset = off;
1142:       }
1143:     }
1144:     rt = -1;
1145:     PetscCheck(offset >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Source cell type for target point %" PetscInt_FMT " could be not found", pNew);
1146:   }
1147:   ctS = tr->ctStart[ctO];
1148:   ctE = tr->ctStart[tr->ctOrderOld[tr->ctOrderInvOld[ctO] + 1]];
1149:   if (trType) {
1150:     for (rtS = ctS; rtS < ctE; ++rtS) {
1151:       PetscInt val;
1152:       PetscCall(DMLabelGetValue(trType, rtS, &val));
1153:       if (val == rt) break;
1154:     }
1155:     PetscCheck(rtS < ctE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find point of type %s with refine type %" PetscInt_FMT, DMPolytopeTypes[ctO], rt);
1156:   } else rtS = ctS;
1157:   PetscCall(DMPlexTransformCellTransform(tr, (DMPolytopeType)ctO, rtS, &rtTmp, &Nct, &rct, &rsize, &cone, &ornt));
1158:   PetscCheck(!trType || rt == rtTmp, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " has refine type %" PetscInt_FMT " != %" PetscInt_FMT " refine type which produced point %" PetscInt_FMT, rtS, rtTmp, rt, pNew);
1159:   for (n = 0; n < Nct; ++n) {
1160:     if (rct[n] == ctN) {
1161:       PetscInt tmp = pNew - tr->ctStartNew[ctN] - offset, val, c;

1163:       if (trType) {
1164:         for (c = ctS; c < ctE; ++c) {
1165:           PetscCall(DMLabelGetValue(trType, c, &val));
1166:           if (val == rt) {
1167:             if (tmp < rsize[n]) break;
1168:             tmp -= rsize[n];
1169:           }
1170:         }
1171:         PetscCheck(c < ctE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Parent point for target point %" PetscInt_FMT " could be not found", pNew);
1172:         rp = c - ctS;
1173:         rO = tmp;
1174:       } else {
1175:         // This assumes that all points of type ctO transform the same way
1176:         rp = tmp / rsize[n];
1177:         rO = tmp % rsize[n];
1178:       }
1179:       break;
1180:     }
1181:   }
1182:   PetscCheck(n != Nct, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Replica number for target point %" PetscInt_FMT " could be not found", pNew);
1183:   pO = rp + ctS;
1184:   PetscCheck(!(pO < ctS) && !(pO >= ctE), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Source point %" PetscInt_FMT " is not a %s [%" PetscInt_FMT ", %" PetscInt_FMT ")", pO, DMPolytopeTypes[ctO], ctS, ctE);
1185:   if (ct) *ct = (DMPolytopeType)ctO;
1186:   if (ctNew) *ctNew = ctN;
1187:   if (p) *p = pO;
1188:   if (r) *r = rO;
1189:   PetscFunctionReturn(PETSC_SUCCESS);
1190: }

1192: /*@
1193:   DMPlexTransformCellTransform - Describes the transform of a given source cell into a set of other target cells. These produced cells become the new mesh.

1195:   Input Parameters:
1196: + tr     - The `DMPlexTransform` object
1197: . source - The source cell type
1198: - p      - The source point, which can also determine the refine type

1200:   Output Parameters:
1201: + rt     - The refine type for this point
1202: . Nt     - The number of types produced by this point
1203: . target - An array of length `Nt` giving the types produced
1204: . size   - An array of length `Nt` giving the number of cells of each type produced
1205: . cone   - An array of length `Nt`*size[t]*coneSize[t] giving the cell type for each point in the cone of each produced point
1206: - ornt   - An array of length `Nt`*size[t]*coneSize[t] giving the orientation for each point in the cone of each produced point

1208:   Level: advanced

1210:   Notes:
1211:   The cone array gives the cone of each subcell listed by the first three outputs. For each cone point, we
1212:   need the cell type, point identifier, and orientation within the subcell. The orientation is with respect to the canonical
1213:   division (described in these outputs) of the cell in the original mesh. The point identifier is given by
1214: .vb
1215:    the number of cones to be taken, or 0 for the current cell
1216:    the cell cone point number at each level from which it is subdivided
1217:    the replica number r of the subdivision.
1218: .ve
1219:   The orientation is with respect to the canonical cone orientation. For example, the prescription for edge division is
1220: .vb
1221:    Nt     = 2
1222:    target = {DM_POLYTOPE_POINT, DM_POLYTOPE_SEGMENT}
1223:    size   = {1, 2}
1224:    cone   = {DM_POLYTOPE_POINT, 1, 0, 0, DM_POLYTOPE_POINT, 0, 0,  DM_POLYTOPE_POINT, 0, 0, DM_POLYTOPE_POINT, 1, 1, 0}
1225:    ornt   = {                         0,                       0,                        0,                          0}
1226: .ve

1228: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformApply()`, `DMPlexTransformCreate()`
1229: @*/
1230: PetscErrorCode DMPlexTransformCellTransform(DMPlexTransform tr, DMPolytopeType source, PetscInt p, PetscInt *rt, PetscInt *Nt, DMPolytopeType *target[], PetscInt *size[], PetscInt *cone[], PetscInt *ornt[])
1231: {
1232:   PetscFunctionBegin;
1233:   PetscUseTypeMethod(tr, celltransform, source, p, rt, Nt, target, size, cone, ornt);
1234:   PetscFunctionReturn(PETSC_SUCCESS);
1235: }

1237: PetscErrorCode DMPlexTransformGetSubcellOrientationIdentity(DMPlexTransform tr, DMPolytopeType sct, PetscInt sp, PetscInt so, DMPolytopeType tct, PetscInt r, PetscInt o, PetscInt *rnew, PetscInt *onew)
1238: {
1239:   PetscFunctionBegin;
1240:   *rnew = r;
1241:   *onew = DMPolytopeTypeComposeOrientation(tct, o, so);
1242:   PetscFunctionReturn(PETSC_SUCCESS);
1243: }

1245: /* Returns the same thing */
1246: PetscErrorCode DMPlexTransformCellTransformIdentity(DMPlexTransform tr, DMPolytopeType source, PetscInt p, PetscInt *rt, PetscInt *Nt, DMPolytopeType *target[], PetscInt *size[], PetscInt *cone[], PetscInt *ornt[])
1247: {
1248:   static DMPolytopeType vertexT[] = {DM_POLYTOPE_POINT};
1249:   static PetscInt       vertexS[] = {1};
1250:   static PetscInt       vertexC[] = {0};
1251:   static PetscInt       vertexO[] = {0};
1252:   static DMPolytopeType edgeT[]   = {DM_POLYTOPE_SEGMENT};
1253:   static PetscInt       edgeS[]   = {1};
1254:   static PetscInt       edgeC[]   = {DM_POLYTOPE_POINT, 1, 0, 0, DM_POLYTOPE_POINT, 1, 1, 0};
1255:   static PetscInt       edgeO[]   = {0, 0};
1256:   static DMPolytopeType tedgeT[]  = {DM_POLYTOPE_POINT_PRISM_TENSOR};
1257:   static PetscInt       tedgeS[]  = {1};
1258:   static PetscInt       tedgeC[]  = {DM_POLYTOPE_POINT, 1, 0, 0, DM_POLYTOPE_POINT, 1, 1, 0};
1259:   static PetscInt       tedgeO[]  = {0, 0};
1260:   static DMPolytopeType triT[]    = {DM_POLYTOPE_TRIANGLE};
1261:   static PetscInt       triS[]    = {1};
1262:   static PetscInt       triC[]    = {DM_POLYTOPE_SEGMENT, 1, 0, 0, DM_POLYTOPE_SEGMENT, 1, 1, 0, DM_POLYTOPE_SEGMENT, 1, 2, 0};
1263:   static PetscInt       triO[]    = {0, 0, 0};
1264:   static DMPolytopeType quadT[]   = {DM_POLYTOPE_QUADRILATERAL};
1265:   static PetscInt       quadS[]   = {1};
1266:   static PetscInt       quadC[]   = {DM_POLYTOPE_SEGMENT, 1, 0, 0, DM_POLYTOPE_SEGMENT, 1, 1, 0, DM_POLYTOPE_SEGMENT, 1, 2, 0, DM_POLYTOPE_SEGMENT, 1, 3, 0};
1267:   static PetscInt       quadO[]   = {0, 0, 0, 0};
1268:   static DMPolytopeType tquadT[]  = {DM_POLYTOPE_SEG_PRISM_TENSOR};
1269:   static PetscInt       tquadS[]  = {1};
1270:   static PetscInt       tquadC[]  = {DM_POLYTOPE_SEGMENT, 1, 0, 0, DM_POLYTOPE_SEGMENT, 1, 1, 0, DM_POLYTOPE_POINT_PRISM_TENSOR, 1, 2, 0, DM_POLYTOPE_POINT_PRISM_TENSOR, 1, 3, 0};
1271:   static PetscInt       tquadO[]  = {0, 0, 0, 0};
1272:   static DMPolytopeType tetT[]    = {DM_POLYTOPE_TETRAHEDRON};
1273:   static PetscInt       tetS[]    = {1};
1274:   static PetscInt       tetC[]    = {DM_POLYTOPE_TRIANGLE, 1, 0, 0, DM_POLYTOPE_TRIANGLE, 1, 1, 0, DM_POLYTOPE_TRIANGLE, 1, 2, 0, DM_POLYTOPE_TRIANGLE, 1, 3, 0};
1275:   static PetscInt       tetO[]    = {0, 0, 0, 0};
1276:   static DMPolytopeType hexT[]    = {DM_POLYTOPE_HEXAHEDRON};
1277:   static PetscInt       hexS[]    = {1};
1278:   static PetscInt       hexC[] = {DM_POLYTOPE_QUADRILATERAL, 1, 0, 0, DM_POLYTOPE_QUADRILATERAL, 1, 1, 0, DM_POLYTOPE_QUADRILATERAL, 1, 2, 0, DM_POLYTOPE_QUADRILATERAL, 1, 3, 0, DM_POLYTOPE_QUADRILATERAL, 1, 4, 0, DM_POLYTOPE_QUADRILATERAL, 1, 5, 0};
1279:   static PetscInt       hexO[] = {0, 0, 0, 0, 0, 0};
1280:   static DMPolytopeType tripT[]   = {DM_POLYTOPE_TRI_PRISM};
1281:   static PetscInt       tripS[]   = {1};
1282:   static PetscInt       tripC[]   = {DM_POLYTOPE_TRIANGLE, 1, 0, 0, DM_POLYTOPE_TRIANGLE, 1, 1, 0, DM_POLYTOPE_QUADRILATERAL, 1, 2, 0, DM_POLYTOPE_QUADRILATERAL, 1, 3, 0, DM_POLYTOPE_QUADRILATERAL, 1, 4, 0};
1283:   static PetscInt       tripO[]   = {0, 0, 0, 0, 0};
1284:   static DMPolytopeType ttripT[]  = {DM_POLYTOPE_TRI_PRISM_TENSOR};
1285:   static PetscInt       ttripS[]  = {1};
1286:   static PetscInt       ttripC[]  = {DM_POLYTOPE_TRIANGLE, 1, 0, 0, DM_POLYTOPE_TRIANGLE, 1, 1, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 2, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 3, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 4, 0};
1287:   static PetscInt       ttripO[]  = {0, 0, 0, 0, 0};
1288:   static DMPolytopeType tquadpT[] = {DM_POLYTOPE_QUAD_PRISM_TENSOR};
1289:   static PetscInt       tquadpS[] = {1};
1290:   static PetscInt       tquadpC[] = {DM_POLYTOPE_QUADRILATERAL,    1, 0, 0, DM_POLYTOPE_QUADRILATERAL,    1, 1, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 2, 0,
1291:                                      DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 3, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 4, 0, DM_POLYTOPE_SEG_PRISM_TENSOR, 1, 5, 0};
1292:   static PetscInt       tquadpO[] = {0, 0, 0, 0, 0, 0};
1293:   static DMPolytopeType pyrT[]    = {DM_POLYTOPE_PYRAMID};
1294:   static PetscInt       pyrS[]    = {1};
1295:   static PetscInt       pyrC[]    = {DM_POLYTOPE_QUADRILATERAL, 1, 0, 0, DM_POLYTOPE_TRIANGLE, 1, 1, 0, DM_POLYTOPE_TRIANGLE, 1, 2, 0, DM_POLYTOPE_TRIANGLE, 1, 3, 0, DM_POLYTOPE_TRIANGLE, 1, 4, 0};
1296:   static PetscInt       pyrO[]    = {0, 0, 0, 0, 0};

1298:   PetscFunctionBegin;
1299:   if (rt) *rt = 0;
1300:   switch (source) {
1301:   case DM_POLYTOPE_POINT:
1302:     *Nt     = 1;
1303:     *target = vertexT;
1304:     *size   = vertexS;
1305:     *cone   = vertexC;
1306:     *ornt   = vertexO;
1307:     break;
1308:   case DM_POLYTOPE_SEGMENT:
1309:     *Nt     = 1;
1310:     *target = edgeT;
1311:     *size   = edgeS;
1312:     *cone   = edgeC;
1313:     *ornt   = edgeO;
1314:     break;
1315:   case DM_POLYTOPE_POINT_PRISM_TENSOR:
1316:     *Nt     = 1;
1317:     *target = tedgeT;
1318:     *size   = tedgeS;
1319:     *cone   = tedgeC;
1320:     *ornt   = tedgeO;
1321:     break;
1322:   case DM_POLYTOPE_TRIANGLE:
1323:     *Nt     = 1;
1324:     *target = triT;
1325:     *size   = triS;
1326:     *cone   = triC;
1327:     *ornt   = triO;
1328:     break;
1329:   case DM_POLYTOPE_QUADRILATERAL:
1330:     *Nt     = 1;
1331:     *target = quadT;
1332:     *size   = quadS;
1333:     *cone   = quadC;
1334:     *ornt   = quadO;
1335:     break;
1336:   case DM_POLYTOPE_SEG_PRISM_TENSOR:
1337:     *Nt     = 1;
1338:     *target = tquadT;
1339:     *size   = tquadS;
1340:     *cone   = tquadC;
1341:     *ornt   = tquadO;
1342:     break;
1343:   case DM_POLYTOPE_TETRAHEDRON:
1344:     *Nt     = 1;
1345:     *target = tetT;
1346:     *size   = tetS;
1347:     *cone   = tetC;
1348:     *ornt   = tetO;
1349:     break;
1350:   case DM_POLYTOPE_HEXAHEDRON:
1351:     *Nt     = 1;
1352:     *target = hexT;
1353:     *size   = hexS;
1354:     *cone   = hexC;
1355:     *ornt   = hexO;
1356:     break;
1357:   case DM_POLYTOPE_TRI_PRISM:
1358:     *Nt     = 1;
1359:     *target = tripT;
1360:     *size   = tripS;
1361:     *cone   = tripC;
1362:     *ornt   = tripO;
1363:     break;
1364:   case DM_POLYTOPE_TRI_PRISM_TENSOR:
1365:     *Nt     = 1;
1366:     *target = ttripT;
1367:     *size   = ttripS;
1368:     *cone   = ttripC;
1369:     *ornt   = ttripO;
1370:     break;
1371:   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1372:     *Nt     = 1;
1373:     *target = tquadpT;
1374:     *size   = tquadpS;
1375:     *cone   = tquadpC;
1376:     *ornt   = tquadpO;
1377:     break;
1378:   case DM_POLYTOPE_PYRAMID:
1379:     *Nt     = 1;
1380:     *target = pyrT;
1381:     *size   = pyrS;
1382:     *cone   = pyrC;
1383:     *ornt   = pyrO;
1384:     break;
1385:   default:
1386:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No refinement strategy for %s", DMPolytopeTypes[source]);
1387:   }
1388:   PetscFunctionReturn(PETSC_SUCCESS);
1389: }

1391: /*@
1392:   DMPlexTransformGetSubcellOrientation - Transform the replica number and orientation for a target point according to the group action for the source point

1394:   Not Collective

1396:   Input Parameters:
1397: + tr  - The `DMPlexTransform`
1398: . sct - The source point cell type, from whom the new cell is being produced
1399: . sp  - The source point
1400: . so  - The orientation of the source point in its enclosing parent
1401: . tct - The target point cell type
1402: . r   - The replica number requested for the produced cell type
1403: - o   - The orientation of the replica

1405:   Output Parameters:
1406: + rnew - The replica number, given the orientation of the parent
1407: - onew - The replica orientation, given the orientation of the parent

1409:   Level: advanced

1411: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformCellTransform()`, `DMPlexTransformApply()`
1412: @*/
1413: PetscErrorCode DMPlexTransformGetSubcellOrientation(DMPlexTransform tr, DMPolytopeType sct, PetscInt sp, PetscInt so, DMPolytopeType tct, PetscInt r, PetscInt o, PetscInt *rnew, PetscInt *onew)
1414: {
1415:   PetscFunctionBeginHot;
1416:   PetscUseTypeMethod(tr, getsubcellorientation, sct, sp, so, tct, r, o, rnew, onew);
1417:   PetscFunctionReturn(PETSC_SUCCESS);
1418: }

1420: static PetscErrorCode DMPlexTransformSetConeSizes(DMPlexTransform tr, DM rdm)
1421: {
1422:   DM       dm;
1423:   PetscInt pStart, pEnd, pNew;

1425:   PetscFunctionBegin;
1426:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1427:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_SetConeSizes, tr, dm, 0, 0));
1428:   /* Must create the celltype label here so that we do not automatically try to compute the types */
1429:   PetscCall(DMCreateLabel(rdm, "celltype"));
1430:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1431:   for (PetscInt p = pStart; p < pEnd; ++p) {
1432:     DMPolytopeType  ct;
1433:     DMPolytopeType *rct;
1434:     PetscInt       *rsize, *rcone, *rornt;
1435:     PetscInt        Nct, n, r;

1437:     PetscCall(DMPlexGetCellType(dm, p, &ct));
1438:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1439:     for (n = 0; n < Nct; ++n) {
1440:       for (r = 0; r < rsize[n]; ++r) {
1441:         PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &pNew));
1442:         PetscCall(DMPlexSetConeSize(rdm, pNew, DMPolytopeTypeGetConeSize(rct[n])));
1443:         PetscCall(DMPlexSetCellType(rdm, pNew, rct[n]));
1444:       }
1445:     }
1446:   }
1447:   /* Let the DM know we have set all the cell types */
1448:   {
1449:     DMLabel  ctLabel;
1450:     DM_Plex *plex = (DM_Plex *)rdm->data;

1452:     PetscCall(DMPlexGetCellTypeLabel(rdm, &ctLabel));
1453:     PetscCall(PetscObjectStateGet((PetscObject)ctLabel, &plex->celltypeState));
1454:   }
1455:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_SetConeSizes, tr, dm, 0, 0));
1456:   PetscFunctionReturn(PETSC_SUCCESS);
1457: }

1459: PetscErrorCode DMPlexTransformGetConeSize(DMPlexTransform tr, PetscInt q, PetscInt *coneSize)
1460: {
1461:   DMPolytopeType ctNew;

1463:   PetscFunctionBegin;
1465:   PetscAssertPointer(coneSize, 3);
1466:   PetscCall(DMPlexTransformGetCellType(tr, q, &ctNew));
1467:   *coneSize = DMPolytopeTypeGetConeSize(ctNew);
1468:   PetscFunctionReturn(PETSC_SUCCESS);
1469: }

1471: /* The orientation o is for the interior of the cell p */
1472: static PetscErrorCode DMPlexTransformGetCone_Internal(DMPlexTransform tr, PetscInt p, PetscInt o, DMPolytopeType ct, DMPolytopeType ctNew, const PetscInt rcone[], PetscInt *coneoff, const PetscInt rornt[], PetscInt *orntoff, PetscInt coneNew[], PetscInt orntNew[])
1473: {
1474:   DM              dm;
1475:   const PetscInt  csizeNew = DMPolytopeTypeGetConeSize(ctNew);
1476:   const PetscInt *cone;
1477:   DMPolytopeType *newft = NULL;
1478:   PetscInt        c, coff = *coneoff, ooff = *orntoff;
1479:   PetscInt        dim, cr = 0, co = 0, nr, no;

1481:   PetscFunctionBegin;
1482:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1483:   PetscCall(DMPlexGetOrientedCone(dm, p, &cone, NULL));
1484:   // Check if we have to permute this cell
1485:   PetscCall(DMGetDimension(dm, &dim));
1486:   if (DMPolytopeTypeGetDim(ctNew) == dim && DMPolytopeTypeGetDim(ct) == dim - 1) {
1487:     PetscCall(DMPlexTransformGetSubcellOrientation(tr, ct, p, o, ctNew, cr, co, &nr, &no));
1488:     if (cr != nr || co != no) PetscCall(DMGetWorkArray(dm, csizeNew, MPIU_INT, &newft));
1489:   }
1490:   for (c = 0; c < csizeNew; ++c) {
1491:     PetscInt             ppp   = -1;                            /* Parent Parent point: Parent of point pp */
1492:     PetscInt             pp    = p;                             /* Parent point: Point in the original mesh producing new cone point */
1493:     PetscInt             po    = 0;                             /* Orientation of parent point pp in parent parent point ppp */
1494:     DMPolytopeType       pct   = ct;                            /* Parent type: Cell type for parent of new cone point */
1495:     const PetscInt      *pcone = cone;                          /* Parent cone: Cone of parent point pp */
1496:     PetscInt             pr    = -1;                            /* Replica number of pp that produces new cone point  */
1497:     const DMPolytopeType ft    = (DMPolytopeType)rcone[coff++]; /* Cell type for new cone point of pNew */
1498:     const PetscInt       fn    = rcone[coff++];                 /* Number of cones of p that need to be taken when producing new cone point */
1499:     PetscInt             fo    = rornt[ooff++];                 /* Orientation of new cone point in pNew */
1500:     PetscInt             lc;

1502:     /* Get the type (pct) and point number (pp) of the parent point in the original mesh which produces this cone point */
1503:     for (lc = 0; lc < fn; ++lc) {
1504:       const PetscInt *parr = DMPolytopeTypeGetArrangement(pct, po);
1505:       const PetscInt  acp  = rcone[coff++];
1506:       const PetscInt  pcp  = parr[acp * 2];
1507:       const PetscInt  pco  = parr[acp * 2 + 1];
1508:       const PetscInt *ppornt;

1510:       ppp = pp;
1511:       pp  = pcone[pcp];
1512:       PetscCall(DMPlexGetCellType(dm, pp, &pct));
1513:       // Restore the parent cone from the last iterate
1514:       if (lc) PetscCall(DMPlexRestoreOrientedCone(dm, ppp, &pcone, NULL));
1515:       PetscCall(DMPlexGetOrientedCone(dm, pp, &pcone, NULL));
1516:       PetscCall(DMPlexGetOrientedCone(dm, ppp, NULL, &ppornt));
1517:       po = DMPolytopeTypeComposeOrientation(pct, ppornt[pcp], pco);
1518:       PetscCall(DMPlexRestoreOrientedCone(dm, ppp, NULL, &ppornt));
1519:     }
1520:     if (lc) PetscCall(DMPlexRestoreOrientedCone(dm, pp, &pcone, NULL));
1521:     pr = rcone[coff++];
1522:     /* Orientation po of pp maps (pr, fo) -> (pr', fo') */
1523:     PetscCall(DMPlexTransformGetSubcellOrientation(tr, pct, pp, fn ? po : o, ft, pr, fo, &pr, &fo));
1524:     PetscCall(DMPlexTransformGetTargetPoint(tr, pct, ft, pp, pr, &coneNew[c]));
1525:     orntNew[c] = fo;
1526:     if (newft) newft[c] = ft;
1527:   }
1528:   PetscCall(DMPlexRestoreOrientedCone(dm, p, &cone, NULL));
1529:   if (newft) {
1530:     const PetscInt *arr;
1531:     PetscInt       *newcone, *newornt;

1533:     arr = DMPolytopeTypeGetArrangement(ctNew, no);
1534:     PetscCall(DMGetWorkArray(dm, csizeNew, MPIU_INT, &newcone));
1535:     PetscCall(DMGetWorkArray(dm, csizeNew, MPIU_INT, &newornt));
1536:     for (PetscInt c = 0; c < csizeNew; ++c) {
1537:       DMPolytopeType ft = newft[c];
1538:       PetscInt       nO;

1540:       nO         = DMPolytopeTypeGetNumArrangements(ft) / 2;
1541:       newcone[c] = coneNew[arr[c * 2 + 0]];
1542:       newornt[c] = DMPolytopeTypeComposeOrientation(ft, arr[c * 2 + 1], orntNew[arr[c * 2 + 0]]);
1543:       PetscCheck(!newornt[c] || !(newornt[c] >= nO || newornt[c] < -nO), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid orientation %" PetscInt_FMT " not in [%" PetscInt_FMT ",%" PetscInt_FMT ") for %s %" PetscInt_FMT, newornt[c], -nO, nO, DMPolytopeTypes[ft], coneNew[c]);
1544:     }
1545:     for (PetscInt c = 0; c < csizeNew; ++c) {
1546:       coneNew[c] = newcone[c];
1547:       orntNew[c] = newornt[c];
1548:     }
1549:     PetscCall(DMRestoreWorkArray(dm, csizeNew, MPIU_INT, &newcone));
1550:     PetscCall(DMRestoreWorkArray(dm, csizeNew, MPIU_INT, &newornt));
1551:     PetscCall(DMRestoreWorkArray(dm, csizeNew, MPIU_INT, &newft));
1552:   }
1553:   *coneoff = coff;
1554:   *orntoff = ooff;
1555:   PetscFunctionReturn(PETSC_SUCCESS);
1556: }

1558: static PetscErrorCode DMPlexTransformSetCones(DMPlexTransform tr, DM rdm)
1559: {
1560:   DM             dm;
1561:   DMPolytopeType ct;
1562:   PetscInt      *coneNew, *orntNew;
1563:   PetscInt       maxConeSize = 0, pStart, pEnd, p, pNew;

1565:   PetscFunctionBegin;
1566:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1567:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_SetCones, tr, dm, 0, 0));
1568:   for (p = 0; p < DM_NUM_POLYTOPES; ++p) maxConeSize = PetscMax(maxConeSize, DMPolytopeTypeGetConeSize((DMPolytopeType)p));
1569:   PetscCall(DMGetWorkArray(rdm, maxConeSize, MPIU_INT, &coneNew));
1570:   PetscCall(DMGetWorkArray(rdm, maxConeSize, MPIU_INT, &orntNew));
1571:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1572:   for (p = pStart; p < pEnd; ++p) {
1573:     PetscInt        coff, ooff;
1574:     DMPolytopeType *rct;
1575:     PetscInt       *rsize, *rcone, *rornt;
1576:     PetscInt        Nct, n, r;

1578:     PetscCall(DMPlexGetCellType(dm, p, &ct));
1579:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1580:     for (n = 0, coff = 0, ooff = 0; n < Nct; ++n) {
1581:       const DMPolytopeType ctNew = rct[n];

1583:       for (r = 0; r < rsize[n]; ++r) {
1584:         PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &pNew));
1585:         PetscCall(DMPlexTransformGetCone_Internal(tr, p, 0, ct, ctNew, rcone, &coff, rornt, &ooff, coneNew, orntNew));
1586:         PetscCall(DMPlexSetCone(rdm, pNew, coneNew));
1587:         PetscCall(DMPlexSetConeOrientation(rdm, pNew, orntNew));
1588:       }
1589:     }
1590:   }
1591:   PetscCall(DMRestoreWorkArray(rdm, maxConeSize, MPIU_INT, &coneNew));
1592:   PetscCall(DMRestoreWorkArray(rdm, maxConeSize, MPIU_INT, &orntNew));
1593:   PetscCall(DMViewFromOptions(rdm, NULL, "-rdm_view"));
1594:   PetscCall(DMPlexSymmetrize(rdm));
1595:   PetscCall(DMPlexStratify(rdm));
1596:   PetscCall(DMPlexTransformOrderSupports(tr, dm, rdm));
1597:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_SetCones, tr, dm, 0, 0));
1598:   PetscFunctionReturn(PETSC_SUCCESS);
1599: }

1601: PetscErrorCode DMPlexTransformGetConeOriented(DMPlexTransform tr, PetscInt q, PetscInt po, const PetscInt *cone[], const PetscInt *ornt[])
1602: {
1603:   DM              dm;
1604:   DMPolytopeType  ct, qct;
1605:   DMPolytopeType *rct;
1606:   PetscInt       *rsize, *rcone, *rornt, *qcone, *qornt;
1607:   PetscInt        maxConeSize = 0, Nct, p, r, n, nr, coff = 0, ooff = 0;

1609:   PetscFunctionBegin;
1611:   PetscAssertPointer(cone, 4);
1612:   PetscAssertPointer(ornt, 5);
1613:   for (p = 0; p < DM_NUM_POLYTOPES; ++p) maxConeSize = PetscMax(maxConeSize, DMPolytopeTypeGetConeSize((DMPolytopeType)p));
1614:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1615:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &qcone));
1616:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &qornt));
1617:   PetscCall(DMPlexTransformGetSourcePoint(tr, q, &ct, &qct, &p, &r));
1618:   PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1619:   for (n = 0; n < Nct; ++n) {
1620:     const DMPolytopeType ctNew    = rct[n];
1621:     const PetscInt       csizeNew = DMPolytopeTypeGetConeSize(ctNew);
1622:     PetscInt             Nr       = rsize[n], fn, c;

1624:     if (ctNew == qct) Nr = r;
1625:     for (nr = 0; nr < Nr; ++nr) {
1626:       for (c = 0; c < csizeNew; ++c) {
1627:         ++coff;             /* Cell type of new cone point */
1628:         fn = rcone[coff++]; /* Number of cones of p that need to be taken when producing new cone point */
1629:         coff += fn;
1630:         ++coff; /* Replica number of new cone point */
1631:         ++ooff; /* Orientation of new cone point */
1632:       }
1633:     }
1634:     if (ctNew == qct) break;
1635:   }
1636:   PetscCall(DMPlexTransformGetCone_Internal(tr, p, po, ct, qct, rcone, &coff, rornt, &ooff, qcone, qornt));
1637:   *cone = qcone;
1638:   *ornt = qornt;
1639:   PetscFunctionReturn(PETSC_SUCCESS);
1640: }

1642: PetscErrorCode DMPlexTransformGetCone(DMPlexTransform tr, PetscInt q, const PetscInt *cone[], const PetscInt *ornt[])
1643: {
1644:   DM              dm;
1645:   DMPolytopeType  ct, qct;
1646:   DMPolytopeType *rct;
1647:   PetscInt       *rsize, *rcone, *rornt, *qcone, *qornt;
1648:   PetscInt        maxConeSize = 0, Nct, p, r, n, nr, coff = 0, ooff = 0;

1650:   PetscFunctionBegin;
1652:   if (cone) PetscAssertPointer(cone, 3);
1653:   if (ornt) PetscAssertPointer(ornt, 4);
1654:   for (p = 0; p < DM_NUM_POLYTOPES; ++p) maxConeSize = PetscMax(maxConeSize, DMPolytopeTypeGetConeSize((DMPolytopeType)p));
1655:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1656:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &qcone));
1657:   PetscCall(DMGetWorkArray(dm, maxConeSize, MPIU_INT, &qornt));
1658:   PetscCall(DMPlexTransformGetSourcePoint(tr, q, &ct, &qct, &p, &r));
1659:   PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1660:   for (n = 0; n < Nct; ++n) {
1661:     const DMPolytopeType ctNew    = rct[n];
1662:     const PetscInt       csizeNew = DMPolytopeTypeGetConeSize(ctNew);
1663:     PetscInt             Nr       = rsize[n], fn, c;

1665:     if (ctNew == qct) Nr = r;
1666:     for (nr = 0; nr < Nr; ++nr) {
1667:       for (c = 0; c < csizeNew; ++c) {
1668:         ++coff;             /* Cell type of new cone point */
1669:         fn = rcone[coff++]; /* Number of cones of p that need to be taken when producing new cone point */
1670:         coff += fn;
1671:         ++coff; /* Replica number of new cone point */
1672:         ++ooff; /* Orientation of new cone point */
1673:       }
1674:     }
1675:     if (ctNew == qct) break;
1676:   }
1677:   PetscCall(DMPlexTransformGetCone_Internal(tr, p, 0, ct, qct, rcone, &coff, rornt, &ooff, qcone, qornt));
1678:   if (cone) *cone = qcone;
1679:   else PetscCall(DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &qcone));
1680:   if (ornt) *ornt = qornt;
1681:   else PetscCall(DMRestoreWorkArray(dm, maxConeSize, MPIU_INT, &qornt));
1682:   PetscFunctionReturn(PETSC_SUCCESS);
1683: }

1685: PetscErrorCode DMPlexTransformRestoreCone(DMPlexTransform tr, PetscInt q, const PetscInt *cone[], const PetscInt *ornt[])
1686: {
1687:   DM dm;

1689:   PetscFunctionBegin;
1691:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1692:   if (cone) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, cone));
1693:   if (ornt) PetscCall(DMRestoreWorkArray(dm, 0, MPIU_INT, ornt));
1694:   PetscFunctionReturn(PETSC_SUCCESS);
1695: }

1697: static PetscErrorCode DMPlexTransformCreateCellVertices_Internal(DMPlexTransform tr)
1698: {
1699:   PetscFunctionBegin;
1700:   PetscCall(PetscCalloc3(DM_NUM_POLYTOPES, &tr->trNv, DM_NUM_POLYTOPES, &tr->trVerts, DM_NUM_POLYTOPES, &tr->trSubVerts));
1701:   for (PetscInt ict = DM_POLYTOPE_POINT; ict < DM_NUM_POLYTOPES; ++ict) {
1702:     const DMPolytopeType ct = (DMPolytopeType)ict;
1703:     DMPlexTransform      reftr;
1704:     DM                   refdm, trdm;
1705:     Vec                  coordinates;
1706:     const PetscScalar   *coords;
1707:     DMPolytopeType      *rct;
1708:     PetscInt            *rsize, *rcone, *rornt;
1709:     PetscInt             Nct, n, r, pNew = 0;
1710:     PetscInt             trdim, vStart, vEnd, Nc;
1711:     const PetscInt       debug = 0;
1712:     const char          *typeName;

1714:     /* Since points are 0-dimensional, coordinates make no sense */
1715:     if (DMPolytopeTypeGetDim(ct) <= 0 || ct == DM_POLYTOPE_UNKNOWN_CELL || ct == DM_POLYTOPE_UNKNOWN_FACE) continue;
1716:     PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &refdm));
1717:     PetscCall(DMPlexTransformCreate(PETSC_COMM_SELF, &reftr));
1718:     PetscCall(DMPlexTransformSetDM(reftr, refdm));
1719:     PetscCall(DMPlexTransformGetType(tr, &typeName));
1720:     PetscCall(DMPlexTransformSetType(reftr, typeName));
1721:     PetscCall(DMPlexTransformSetUp(reftr));
1722:     PetscCall(DMPlexTransformApply(reftr, refdm, &trdm));

1724:     PetscCall(DMGetDimension(trdm, &trdim));
1725:     PetscCall(DMPlexGetDepthStratum(trdm, 0, &vStart, &vEnd));
1726:     tr->trNv[ct] = vEnd - vStart;
1727:     PetscCall(DMGetCoordinatesLocal(trdm, &coordinates));
1728:     PetscCall(VecGetLocalSize(coordinates, &Nc));
1729:     PetscCheck(tr->trNv[ct] * trdim == Nc, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Cell type %s, transformed coordinate size %" PetscInt_FMT " != %" PetscInt_FMT " size of coordinate storage", DMPolytopeTypes[ct], tr->trNv[ct] * trdim, Nc);
1730:     PetscCall(PetscCalloc1(Nc, &tr->trVerts[ct]));
1731:     PetscCall(VecGetArrayRead(coordinates, &coords));
1732:     PetscCall(PetscArraycpy(tr->trVerts[ct], coords, Nc));
1733:     PetscCall(VecRestoreArrayRead(coordinates, &coords));

1735:     PetscCall(PetscCalloc1(DM_NUM_POLYTOPES, &tr->trSubVerts[ct]));
1736:     PetscCall(DMPlexTransformCellTransform(reftr, ct, 0, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1737:     for (n = 0; n < Nct; ++n) {
1738:       /* Since points are 0-dimensional, coordinates make no sense */
1739:       if (rct[n] == DM_POLYTOPE_POINT) continue;
1740:       PetscCall(PetscCalloc1(rsize[n], &tr->trSubVerts[ct][rct[n]]));
1741:       for (r = 0; r < rsize[n]; ++r) {
1742:         PetscInt *closure = NULL;
1743:         PetscInt  clSize, cl, Nv = 0;

1745:         PetscCall(PetscCalloc1(DMPolytopeTypeGetNumVertices(rct[n]), &tr->trSubVerts[ct][rct[n]][r]));
1746:         PetscCall(DMPlexTransformGetTargetPoint(reftr, ct, rct[n], 0, r, &pNew));
1747:         PetscCall(DMPlexGetTransitiveClosure(trdm, pNew, PETSC_TRUE, &clSize, &closure));
1748:         for (cl = 0; cl < clSize * 2; cl += 2) {
1749:           const PetscInt sv = closure[cl];

1751:           if ((sv >= vStart) && (sv < vEnd)) tr->trSubVerts[ct][rct[n]][r][Nv++] = sv - vStart;
1752:         }
1753:         PetscCall(DMPlexRestoreTransitiveClosure(trdm, pNew, PETSC_TRUE, &clSize, &closure));
1754:         PetscCheck(Nv == DMPolytopeTypeGetNumVertices(rct[n]), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of vertices %" PetscInt_FMT " != %" PetscInt_FMT " for %s subcell %" PetscInt_FMT " from cell %s", Nv, DMPolytopeTypeGetNumVertices(rct[n]), DMPolytopeTypes[rct[n]], r, DMPolytopeTypes[ct]);
1755:       }
1756:     }
1757:     if (debug) {
1758:       DMPolytopeType *rct;
1759:       PetscInt       *rsize, *rcone, *rornt;
1760:       PetscInt        v, dE = trdim, d, off = 0;

1762:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "%s: %" PetscInt_FMT " vertices\n", DMPolytopeTypes[ct], tr->trNv[ct]));
1763:       for (v = 0; v < tr->trNv[ct]; ++v) {
1764:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "  "));
1765:         for (d = 0; d < dE; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g ", (double)PetscRealPart(tr->trVerts[ct][off++])));
1766:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1767:       }

1769:       PetscCall(DMPlexTransformCellTransform(reftr, ct, 0, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1770:       for (n = 0; n < Nct; ++n) {
1771:         if (rct[n] == DM_POLYTOPE_POINT) continue;
1772:         PetscCall(PetscPrintf(PETSC_COMM_SELF, "%s: %s subvertices %" PetscInt_FMT "\n", DMPolytopeTypes[ct], DMPolytopeTypes[rct[n]], tr->trNv[ct]));
1773:         for (r = 0; r < rsize[n]; ++r) {
1774:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "  "));
1775:           for (v = 0; v < DMPolytopeTypeGetNumVertices(rct[n]); ++v) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT " ", tr->trSubVerts[ct][rct[n]][r][v]));
1776:           PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1777:         }
1778:       }
1779:     }
1780:     PetscCall(DMDestroy(&refdm));
1781:     PetscCall(DMDestroy(&trdm));
1782:     PetscCall(DMPlexTransformDestroy(&reftr));
1783:   }
1784:   PetscFunctionReturn(PETSC_SUCCESS);
1785: }

1787: /*@C
1788:   DMPlexTransformGetCellVertices - Get the set of transformed vertices lying in the closure of a reference cell of given type

1790:   Input Parameters:
1791: + tr - The `DMPlexTransform` object
1792: - ct - The cell type

1794:   Output Parameters:
1795: + Nv      - The number of transformed vertices in the closure of the reference cell of given type
1796: - trVerts - The coordinates of these vertices in the reference cell

1798:   Level: developer

1800: .seealso: `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformGetSubcellVertices()`
1801: @*/
1802: PetscErrorCode DMPlexTransformGetCellVertices(DMPlexTransform tr, DMPolytopeType ct, PetscInt *Nv, PetscScalar *trVerts[])
1803: {
1804:   PetscFunctionBegin;
1805:   if (!tr->trNv) PetscCall(DMPlexTransformCreateCellVertices_Internal(tr));
1806:   if (Nv) *Nv = tr->trNv[ct];
1807:   if (trVerts) *trVerts = tr->trVerts[ct];
1808:   PetscFunctionReturn(PETSC_SUCCESS);
1809: }

1811: /*@C
1812:   DMPlexTransformGetSubcellVertices - Get the set of transformed vertices defining a subcell in the reference cell of given type

1814:   Input Parameters:
1815: + tr  - The `DMPlexTransform` object
1816: . ct  - The cell type
1817: . rct - The subcell type
1818: - r   - The subcell index

1820:   Output Parameter:
1821: . subVerts - The indices of these vertices in the set of vertices returned by `DMPlexTransformGetCellVertices()`

1823:   Level: developer

1825: .seealso: `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformGetCellVertices()`
1826: @*/
1827: PetscErrorCode DMPlexTransformGetSubcellVertices(DMPlexTransform tr, DMPolytopeType ct, DMPolytopeType rct, PetscInt r, PetscInt *subVerts[])
1828: {
1829:   PetscFunctionBegin;
1830:   if (!tr->trNv) PetscCall(DMPlexTransformCreateCellVertices_Internal(tr));
1831:   PetscCheck(tr->trSubVerts[ct][rct], PetscObjectComm((PetscObject)tr), PETSC_ERR_ARG_WRONG, "Cell type %s does not produce %s", DMPolytopeTypes[ct], DMPolytopeTypes[rct]);
1832:   if (subVerts) *subVerts = tr->trSubVerts[ct][rct][r];
1833:   PetscFunctionReturn(PETSC_SUCCESS);
1834: }

1836: /*@
1837:   DMPlexTransformOrderSupports - Reorder newly introduced point supports

1839:   Collective

1841:   Input Parameters:
1842: + tr   - The `DMPlexTransform`
1843: . dm   - The original `DM`
1844: - trdm - The transformed `DM` which is reordered

1846:   Level: intermediate

1848: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformApply()`
1849: @*/
1850: PetscErrorCode DMPlexTransformOrderSupports(DMPlexTransform tr, DM dm, DM trdm)
1851: {
1852:   PetscFunctionBegin;
1856:   PetscTryTypeMethod(tr, ordersupports, dm, trdm);
1857:   PetscFunctionReturn(PETSC_SUCCESS);
1858: }

1860: /* Computes new vertex as the barycenter, or centroid */
1861: PetscErrorCode DMPlexTransformMapCoordinatesBarycenter_Internal(DMPlexTransform tr, DMPolytopeType pct, DMPolytopeType ct, PetscInt p, PetscInt r, PetscInt Nv, PetscInt dE, const PetscScalar in[], PetscScalar out[])
1862: {
1863:   PetscInt v, d;

1865:   PetscFunctionBeginHot;
1866:   PetscCheck(ct == DM_POLYTOPE_POINT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not for refined point type %s", DMPolytopeTypes[ct]);
1867:   for (d = 0; d < dE; ++d) out[d] = 0.0;
1868:   for (v = 0; v < Nv; ++v)
1869:     for (d = 0; d < dE; ++d) out[d] += in[v * dE + d];
1870:   for (d = 0; d < dE; ++d) out[d] /= Nv;
1871:   PetscFunctionReturn(PETSC_SUCCESS);
1872: }

1874: /*@
1875:   DMPlexTransformMapCoordinates - Calculate new coordinates for produced points

1877:   Not collective

1879:   Input Parameters:
1880: + tr  - The `DMPlexTransform`
1881: . pct - The cell type of the parent, from whom the new cell is being produced
1882: . ct  - The type being produced
1883: . p   - The original point
1884: . r   - The replica number requested for the produced cell type
1885: . Nv  - Number of vertices in the closure of the parent cell
1886: . dE  - Spatial dimension
1887: - in  - array of size Nv*dE, holding coordinates of the vertices in the closure of the parent cell

1889:   Output Parameter:
1890: . out - The coordinates of the new vertices

1892:   Level: intermediate

1894: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexTransformApply()`
1895: @*/
1896: PetscErrorCode DMPlexTransformMapCoordinates(DMPlexTransform tr, DMPolytopeType pct, DMPolytopeType ct, PetscInt p, PetscInt r, PetscInt Nv, PetscInt dE, const PetscScalar in[], PetscScalar out[])
1897: {
1898:   PetscFunctionBeginHot;
1899:   if (Nv) PetscUseTypeMethod(tr, mapcoordinates, pct, ct, p, r, Nv, dE, in, out);
1900:   PetscFunctionReturn(PETSC_SUCCESS);
1901: }

1903: /*
1904:   DMPlexTransformLabelProducedPoint_Private - Label a produced point based on its parent label

1906:   Not Collective

1908:   Input Parameters:
1909: + tr    - The `DMPlexTransform`
1910: . label - The label in the transformed mesh
1911: . pp    - The parent point in the original mesh
1912: . pct   - The cell type of the parent point
1913: . p     - The point in the transformed mesh
1914: . ct    - The cell type of the point
1915: . r     - The replica number of the point
1916: - val   - The label value of the parent point

1918:   Level: developer

1920: .seealso: `DMPlexTransformCreateLabels()`, `RefineLabel_Internal()`
1921: */
1922: static PetscErrorCode DMPlexTransformLabelProducedPoint_Private(DMPlexTransform tr, DMLabel label, PetscInt pp, DMPolytopeType pct, PetscInt p, DMPolytopeType ct, PetscInt r, PetscInt val)
1923: {
1924:   PetscFunctionBeginHot;
1925:   if (tr->labelMatchStrata && pct != ct) PetscFunctionReturn(PETSC_SUCCESS);
1926:   PetscCall(DMLabelSetValue(label, p, val + tr->labelReplicaInc * r));
1927:   PetscFunctionReturn(PETSC_SUCCESS);
1928: }

1930: static PetscErrorCode RefineLabel_Internal(DMPlexTransform tr, DMLabel label, DMLabel labelNew)
1931: {
1932:   DM              dm;
1933:   IS              valueIS;
1934:   const PetscInt *values;
1935:   PetscInt        defVal, Nv, val;

1937:   PetscFunctionBegin;
1938:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1939:   PetscCall(DMLabelGetDefaultValue(label, &defVal));
1940:   PetscCall(DMLabelSetDefaultValue(labelNew, defVal));
1941:   PetscCall(DMLabelGetValueIS(label, &valueIS));
1942:   PetscCall(ISGetLocalSize(valueIS, &Nv));
1943:   PetscCall(ISGetIndices(valueIS, &values));
1944:   for (val = 0; val < Nv; ++val) {
1945:     IS              pointIS;
1946:     const PetscInt *points;
1947:     PetscInt        numPoints, p;

1949:     /* Ensure refined label is created with same number of strata as
1950:      * original (even if no entries here). */
1951:     PetscCall(DMLabelAddStratum(labelNew, values[val]));
1952:     PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
1953:     PetscCall(ISGetLocalSize(pointIS, &numPoints));
1954:     PetscCall(ISGetIndices(pointIS, &points));
1955:     for (p = 0; p < numPoints; ++p) {
1956:       const PetscInt  point = points[p];
1957:       DMPolytopeType  ct;
1958:       DMPolytopeType *rct;
1959:       PetscInt       *rsize, *rcone, *rornt;
1960:       PetscInt        Nct, n, r, pNew = 0;

1962:       PetscCall(DMPlexGetCellType(dm, point, &ct));
1963:       PetscCall(DMPlexTransformCellTransform(tr, ct, point, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
1964:       for (n = 0; n < Nct; ++n) {
1965:         for (r = 0; r < rsize[n]; ++r) {
1966:           PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], point, r, &pNew));
1967:           PetscCall(DMPlexTransformLabelProducedPoint_Private(tr, labelNew, point, ct, pNew, rct[n], r, values[val]));
1968:         }
1969:       }
1970:     }
1971:     PetscCall(ISRestoreIndices(pointIS, &points));
1972:     PetscCall(ISDestroy(&pointIS));
1973:   }
1974:   PetscCall(ISRestoreIndices(valueIS, &values));
1975:   PetscCall(ISDestroy(&valueIS));
1976:   PetscFunctionReturn(PETSC_SUCCESS);
1977: }

1979: static PetscErrorCode DMPlexTransformCreateLabels(DMPlexTransform tr, DM rdm)
1980: {
1981:   DM       dm;
1982:   PetscInt numLabels, l;

1984:   PetscFunctionBegin;
1985:   PetscCall(DMPlexTransformGetDM(tr, &dm));
1986:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_CreateLabels, tr, dm, 0, 0));
1987:   PetscCall(DMGetNumLabels(dm, &numLabels));
1988:   for (l = 0; l < numLabels; ++l) {
1989:     DMLabel     label, labelNew;
1990:     const char *lname;
1991:     PetscBool   isDepth, isCellType;

1993:     PetscCall(DMGetLabelName(dm, l, &lname));
1994:     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
1995:     if (isDepth) continue;
1996:     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
1997:     if (isCellType) continue;
1998:     PetscCall(DMCreateLabel(rdm, lname));
1999:     PetscCall(DMGetLabel(dm, lname, &label));
2000:     PetscCall(DMGetLabel(rdm, lname, &labelNew));
2001:     PetscCall(RefineLabel_Internal(tr, label, labelNew));
2002:   }
2003:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_CreateLabels, tr, dm, 0, 0));
2004:   PetscFunctionReturn(PETSC_SUCCESS);
2005: }

2007: /* This refines the labels which define regions for fields and DSes since they are not in the list of labels for the DM */
2008: PetscErrorCode DMPlexTransformCreateDiscLabels(DMPlexTransform tr, DM rdm)
2009: {
2010:   DM       dm;
2011:   PetscInt Nf, f, Nds, s;

2013:   PetscFunctionBegin;
2014:   PetscCall(DMPlexTransformGetDM(tr, &dm));
2015:   PetscCall(DMGetNumFields(dm, &Nf));
2016:   for (f = 0; f < Nf; ++f) {
2017:     DMLabel     label, labelNew;
2018:     PetscObject obj;
2019:     const char *lname;

2021:     PetscCall(DMGetField(rdm, f, &label, &obj));
2022:     if (!label) continue;
2023:     PetscCall(PetscObjectGetName((PetscObject)label, &lname));
2024:     PetscCall(DMLabelCreate(PETSC_COMM_SELF, lname, &labelNew));
2025:     PetscCall(RefineLabel_Internal(tr, label, labelNew));
2026:     PetscCall(DMSetField_Internal(rdm, f, labelNew, obj));
2027:     PetscCall(DMLabelDestroy(&labelNew));
2028:   }
2029:   PetscCall(DMGetNumDS(dm, &Nds));
2030:   for (s = 0; s < Nds; ++s) {
2031:     DMLabel     label, labelNew;
2032:     const char *lname;

2034:     PetscCall(DMGetRegionNumDS(rdm, s, &label, NULL, NULL, NULL));
2035:     if (!label) continue;
2036:     PetscCall(PetscObjectGetName((PetscObject)label, &lname));
2037:     PetscCall(DMLabelCreate(PETSC_COMM_SELF, lname, &labelNew));
2038:     PetscCall(RefineLabel_Internal(tr, label, labelNew));
2039:     PetscCall(DMSetRegionNumDS(rdm, s, labelNew, NULL, NULL, NULL));
2040:     PetscCall(DMLabelDestroy(&labelNew));
2041:   }
2042:   PetscFunctionReturn(PETSC_SUCCESS);
2043: }

2045: static PetscErrorCode DMPlexTransformCreateSF(DMPlexTransform tr, DM rdm)
2046: {
2047:   DM                 dm;
2048:   PetscSF            sf, sfNew;
2049:   PetscInt           numRoots, numLeaves, numLeavesNew = 0, l, m;
2050:   const PetscInt    *localPoints;
2051:   const PetscSFNode *remotePoints;
2052:   PetscInt          *localPointsNew;
2053:   PetscSFNode       *remotePointsNew;
2054:   PetscInt           pStartNew, pEndNew, pNew;
2055:   /* Brute force algorithm */
2056:   PetscSF         rsf;
2057:   PetscSection    s;
2058:   const PetscInt *rootdegree;
2059:   PetscInt       *rootPointsNew, *remoteOffsets;
2060:   PetscInt        numPointsNew, pStart, pEnd, p;

2062:   PetscFunctionBegin;
2063:   PetscCall(DMPlexTransformGetDM(tr, &dm));
2064:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_CreateSF, tr, dm, 0, 0));
2065:   PetscCall(DMPlexGetChart(rdm, &pStartNew, &pEndNew));
2066:   PetscCall(DMGetPointSF(dm, &sf));
2067:   PetscCall(DMGetPointSF(rdm, &sfNew));
2068:   /* Calculate size of new SF */
2069:   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
2070:   if (numRoots < 0) {
2071:     PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_CreateSF, tr, dm, 0, 0));
2072:     PetscFunctionReturn(PETSC_SUCCESS);
2073:   }
2074:   for (l = 0; l < numLeaves; ++l) {
2075:     const PetscInt  p = localPoints ? localPoints[l] : l;
2076:     DMPolytopeType  ct;
2077:     DMPolytopeType *rct;
2078:     PetscInt       *rsize, *rcone, *rornt;
2079:     PetscInt        Nct, n;

2081:     PetscCall(DMPlexGetCellType(dm, p, &ct));
2082:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2083:     for (n = 0; n < Nct; ++n) numLeavesNew += rsize[n];
2084:   }
2085:   /* Send new root point numbers
2086:        It is possible to optimize for regular transforms by sending only the cell type offsets, but it seems a needless complication
2087:   */
2088:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
2089:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &s));
2090:   PetscCall(PetscSectionSetChart(s, pStart, pEnd));
2091:   for (p = pStart; p < pEnd; ++p) {
2092:     DMPolytopeType  ct;
2093:     DMPolytopeType *rct;
2094:     PetscInt       *rsize, *rcone, *rornt;
2095:     PetscInt        Nct, n;

2097:     PetscCall(DMPlexGetCellType(dm, p, &ct));
2098:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2099:     for (n = 0; n < Nct; ++n) PetscCall(PetscSectionAddDof(s, p, rsize[n]));
2100:   }
2101:   PetscCall(PetscSectionSetUp(s));
2102:   PetscCall(PetscSectionGetStorageSize(s, &numPointsNew));
2103:   PetscCall(PetscSFCreateRemoteOffsets(sf, s, s, &remoteOffsets));
2104:   PetscCall(PetscSFCreateSectionSF(sf, s, remoteOffsets, s, &rsf));
2105:   PetscCall(PetscFree(remoteOffsets));
2106:   PetscCall(PetscSFComputeDegreeBegin(sf, &rootdegree));
2107:   PetscCall(PetscSFComputeDegreeEnd(sf, &rootdegree));
2108:   PetscCall(PetscMalloc1(numPointsNew, &rootPointsNew));
2109:   for (p = 0; p < numPointsNew; ++p) rootPointsNew[p] = -1;
2110:   for (p = pStart; p < pEnd; ++p) {
2111:     DMPolytopeType  ct;
2112:     DMPolytopeType *rct;
2113:     PetscInt       *rsize, *rcone, *rornt;
2114:     PetscInt        Nct, n, r, off;

2116:     if (!rootdegree[p - pStart]) continue;
2117:     PetscCall(PetscSectionGetOffset(s, p, &off));
2118:     PetscCall(DMPlexGetCellType(dm, p, &ct));
2119:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2120:     for (n = 0, m = 0; n < Nct; ++n) {
2121:       for (r = 0; r < rsize[n]; ++r, ++m) {
2122:         PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &pNew));
2123:         rootPointsNew[off + m] = pNew;
2124:       }
2125:     }
2126:   }
2127:   PetscCall(PetscSFBcastBegin(rsf, MPIU_INT, rootPointsNew, rootPointsNew, MPI_REPLACE));
2128:   PetscCall(PetscSFBcastEnd(rsf, MPIU_INT, rootPointsNew, rootPointsNew, MPI_REPLACE));
2129:   PetscCall(PetscSFDestroy(&rsf));
2130:   PetscCall(PetscMalloc1(numLeavesNew, &localPointsNew));
2131:   PetscCall(PetscMalloc1(numLeavesNew, &remotePointsNew));
2132:   for (l = 0, m = 0; l < numLeaves; ++l) {
2133:     const PetscInt  p = localPoints ? localPoints[l] : l;
2134:     DMPolytopeType  ct;
2135:     DMPolytopeType *rct;
2136:     PetscInt       *rsize, *rcone, *rornt;
2137:     PetscInt        Nct, n, r, q, off;

2139:     PetscCall(PetscSectionGetOffset(s, p, &off));
2140:     PetscCall(DMPlexGetCellType(dm, p, &ct));
2141:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2142:     for (n = 0, q = 0; n < Nct; ++n) {
2143:       for (r = 0; r < rsize[n]; ++r, ++m, ++q) {
2144:         PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &pNew));
2145:         localPointsNew[m]        = pNew;
2146:         remotePointsNew[m].index = rootPointsNew[off + q];
2147:         remotePointsNew[m].rank  = remotePoints[l].rank;
2148:       }
2149:     }
2150:   }
2151:   PetscCall(PetscSectionDestroy(&s));
2152:   PetscCall(PetscFree(rootPointsNew));
2153:   /* SF needs sorted leaves to correctly calculate Gather */
2154:   {
2155:     PetscSFNode *rp, *rtmp;
2156:     PetscInt    *lp, *idx, *ltmp, i;

2158:     PetscCall(PetscMalloc1(numLeavesNew, &idx));
2159:     PetscCall(PetscMalloc1(numLeavesNew, &lp));
2160:     PetscCall(PetscMalloc1(numLeavesNew, &rp));
2161:     for (i = 0; i < numLeavesNew; ++i) {
2162:       PetscCheck(!(localPointsNew[i] < pStartNew) && !(localPointsNew[i] >= pEndNew), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local SF point %" PetscInt_FMT " (%" PetscInt_FMT ") not in [%" PetscInt_FMT ", %" PetscInt_FMT ")", localPointsNew[i], i, pStartNew, pEndNew);
2163:       idx[i] = i;
2164:     }
2165:     PetscCall(PetscSortIntWithPermutation(numLeavesNew, localPointsNew, idx));
2166:     for (i = 0; i < numLeavesNew; ++i) {
2167:       lp[i] = localPointsNew[idx[i]];
2168:       rp[i] = remotePointsNew[idx[i]];
2169:     }
2170:     ltmp            = localPointsNew;
2171:     localPointsNew  = lp;
2172:     rtmp            = remotePointsNew;
2173:     remotePointsNew = rp;
2174:     PetscCall(PetscFree(idx));
2175:     PetscCall(PetscFree(ltmp));
2176:     PetscCall(PetscFree(rtmp));
2177:   }
2178:   PetscCall(PetscSFSetGraph(sfNew, pEndNew - pStartNew, numLeavesNew, localPointsNew, PETSC_OWN_POINTER, remotePointsNew, PETSC_OWN_POINTER));
2179:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_CreateSF, tr, dm, 0, 0));
2180:   if (PetscDefined(USE_DEBUG)) {
2181:     PetscInt overlap;

2183:     // Need to set overlap because some transforms put cells in the overlap
2184:     PetscCall(DMPlexGetOverlap(rdm, &overlap));
2185:     PetscCall(DMPlexSetOverlap(rdm, NULL, 1));
2186:     PetscCall(DMPlexCheckPointSF(rdm, sfNew, PETSC_FALSE));
2187:     PetscCall(DMPlexSetOverlap(rdm, NULL, overlap));
2188:   }
2189:   PetscFunctionReturn(PETSC_SUCCESS);
2190: }

2192: /*
2193:   DMPlexCellRefinerMapLocalizedCoordinates - Given a cell of `DMPolytopeType` `ct` with localized coordinates `x`, generate localized coordinates `xr` for subcell `r` of type `rct`.

2195:   Not Collective

2197:   Input Parameters:
2198: + tr  - The `DMPlexTransform`
2199: . ct  - The type of the parent cell
2200: . rct - The type of the produced cell
2201: . r   - The index of the produced cell
2202: - x   - The localized coordinates for the parent cell

2204:   Output Parameter:
2205: . xr  - The localized coordinates for the produced cell

2207:   Level: developer

2209: .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPolytopeType`, `DMPlexCellRefinerSetCoordinates()`
2210: */
2211: static PetscErrorCode DMPlexTransformMapLocalizedCoordinates(DMPlexTransform tr, DMPolytopeType ct, DMPolytopeType rct, PetscInt r, const PetscScalar x[], PetscScalar xr[])
2212: {
2213:   PetscFE  fe = NULL;
2214:   PetscInt cdim, v, *subcellV;

2216:   PetscFunctionBegin;
2217:   PetscCall(DMPlexTransformGetCoordinateFE(tr, ct, &fe));
2218:   PetscCall(DMPlexTransformGetSubcellVertices(tr, ct, rct, r, &subcellV));
2219:   PetscCall(PetscFEGetNumComponents(fe, &cdim));
2220:   for (v = 0; v < DMPolytopeTypeGetNumVertices(rct); ++v) PetscCall(PetscFEInterpolate_Static(fe, x, tr->refGeom[ct], subcellV[v], &xr[v * cdim]));
2221:   PetscFunctionReturn(PETSC_SUCCESS);
2222: }

2224: static PetscErrorCode DMPlexTransformSetCoordinates(DMPlexTransform tr, DM rdm)
2225: {
2226:   DM                 dm, cdm, cdmCell, cdmNew, cdmCellNew;
2227:   PetscSection       coordSection, coordSectionNew, coordSectionCell, coordSectionCellNew;
2228:   Vec                coordsLocal, coordsLocalNew, coordsLocalCell = NULL, coordsLocalCellNew;
2229:   const PetscScalar *coords;
2230:   PetscScalar       *coordsNew;
2231:   const PetscReal   *maxCell, *Lstart, *L;
2232:   PetscBool          localized, localizeVertices = PETSC_FALSE, localizeCells = PETSC_FALSE, sparseLocalize;
2233:   PetscInt           dE, dEo, d, cStart, cEnd, c, cStartNew, cEndNew, vStartNew, vEndNew, v, pStart, pEnd, p;

2235:   PetscFunctionBegin;
2236:   // Need to clear the DMField for coordinates
2237:   PetscCall(DMSetCoordinateField(rdm, NULL));
2238:   PetscCall(DMPlexTransformGetDM(tr, &dm));
2239:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_SetCoordinates, tr, dm, 0, 0));
2240:   PetscCall(DMGetCoordinateDM(dm, &cdm));
2241:   PetscCall(DMGetCellCoordinateDM(dm, &cdmCell));
2242:   PetscCall(DMGetCoordinatesLocalized(dm, &localized));
2243:   PetscCall(DMGetSparseLocalize(dm, &sparseLocalize));
2244:   PetscCall(DMSetSparseLocalize(rdm, sparseLocalize));
2245:   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
2246:   if (localized) {
2247:     /* Localize coordinates of new vertices */
2248:     localizeVertices = PETSC_TRUE;
2249:     /* If we do not have a mechanism for automatically localizing cell coordinates, we need to compute them explicitly for every divided cell */
2250:     if (!maxCell) localizeCells = PETSC_TRUE;
2251:   }
2252:   PetscCall(DMGetCoordinateSection(dm, &coordSection));
2253:   PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &dEo));
2254:   PetscCall(DMGetCoordinateDim(rdm, &dE));
2255:   if (maxCell) {
2256:     PetscReal *LstartNew, *LNew, *maxCellNew;

2258:     PetscCall(PetscMalloc3(dE, &LstartNew, dE, &LNew, dE, &maxCellNew));
2259:     for (d = 0; d < dEo; ++d) {
2260:       LstartNew[d]  = Lstart[d];
2261:       LNew[d]       = L[d];
2262:       maxCellNew[d] = maxCell[d] / tr->redFactor;
2263:     }
2264:     for (d = dEo; d < dE; ++d) {
2265:       LstartNew[d]  = 0.;
2266:       LNew[d]       = -1.;
2267:       maxCellNew[d] = -1.;
2268:     }
2269:     PetscCall(DMSetPeriodicity(rdm, maxCellNew, LstartNew, LNew));
2270:     PetscCall(PetscFree3(LstartNew, LNew, maxCellNew));
2271:   }
2272:   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)rdm), &coordSectionNew));
2273:   PetscCall(PetscSectionSetNumFields(coordSectionNew, 1));
2274:   PetscCall(PetscSectionSetFieldComponents(coordSectionNew, 0, dE));
2275:   PetscCall(DMPlexGetDepthStratum(rdm, 0, &vStartNew, &vEndNew));
2276:   PetscCall(PetscSectionSetChart(coordSectionNew, vStartNew, vEndNew));
2277:   /* Localization should be inherited */
2278:   /*   Stefano calculates parent cells for each new cell for localization */
2279:   /*   Localized cells need coordinates of closure */
2280:   for (v = vStartNew; v < vEndNew; ++v) {
2281:     PetscCall(PetscSectionSetDof(coordSectionNew, v, dE));
2282:     PetscCall(PetscSectionSetFieldDof(coordSectionNew, v, 0, dE));
2283:   }
2284:   PetscCall(PetscSectionSetUp(coordSectionNew));
2285:   PetscCall(DMSetCoordinateSection(rdm, PETSC_DETERMINE, coordSectionNew));

2287:   if (localizeCells) {
2288:     PetscCall(DMGetCoordinateDM(rdm, &cdmNew));
2289:     PetscCall(DMClone(cdmNew, &cdmCellNew));
2290:     PetscCall(DMSetCellCoordinateDM(rdm, cdmCellNew));
2291:     PetscCall(DMDestroy(&cdmCellNew));

2293:     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)rdm), &coordSectionCellNew));
2294:     PetscCall(PetscSectionSetNumFields(coordSectionCellNew, 1));
2295:     PetscCall(PetscSectionSetFieldComponents(coordSectionCellNew, 0, dE));
2296:     PetscCall(DMPlexGetHeightStratum(rdm, 0, &cStartNew, &cEndNew));
2297:     PetscCall(PetscSectionSetChart(coordSectionCellNew, cStartNew, cEndNew));

2299:     PetscCall(DMGetCellCoordinateSection(dm, &coordSectionCell));
2300:     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
2301:     for (c = cStart; c < cEnd; ++c) {
2302:       PetscInt dof;

2304:       PetscCall(PetscSectionGetDof(coordSectionCell, c, &dof));
2305:       if (dof) {
2306:         DMPolytopeType  ct;
2307:         DMPolytopeType *rct;
2308:         PetscInt       *rsize, *rcone, *rornt;
2309:         PetscInt        dim, cNew, Nct, n, r;

2311:         PetscCall(DMPlexGetCellType(dm, c, &ct));
2312:         dim = DMPolytopeTypeGetDim(ct);
2313:         PetscCall(DMPlexTransformCellTransform(tr, ct, c, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2314:         /* This allows for different cell types */
2315:         for (n = 0; n < Nct; ++n) {
2316:           if (dim != DMPolytopeTypeGetDim(rct[n])) continue;
2317:           for (r = 0; r < rsize[n]; ++r) {
2318:             PetscInt *closure = NULL;
2319:             PetscInt  clSize, cl, Nv = 0;

2321:             PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], c, r, &cNew));
2322:             PetscCall(DMPlexGetTransitiveClosure(rdm, cNew, PETSC_TRUE, &clSize, &closure));
2323:             for (cl = 0; cl < clSize * 2; cl += 2) {
2324:               if ((closure[cl] >= vStartNew) && (closure[cl] < vEndNew)) ++Nv;
2325:             }
2326:             PetscCall(DMPlexRestoreTransitiveClosure(rdm, cNew, PETSC_TRUE, &clSize, &closure));
2327:             PetscCall(PetscSectionSetDof(coordSectionCellNew, cNew, Nv * dE));
2328:             PetscCall(PetscSectionSetFieldDof(coordSectionCellNew, cNew, 0, Nv * dE));
2329:           }
2330:         }
2331:       }
2332:     }
2333:     PetscCall(PetscSectionSetUp(coordSectionCellNew));
2334:     PetscCall(DMSetCellCoordinateSection(rdm, PETSC_DETERMINE, coordSectionCellNew));
2335:   }
2336:   PetscCall(DMViewFromOptions(dm, NULL, "-coarse_dm_view"));
2337:   {
2338:     VecType     vtype;
2339:     PetscInt    coordSizeNew, bs;
2340:     const char *name;

2342:     PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
2343:     PetscCall(VecCreate(PETSC_COMM_SELF, &coordsLocalNew));
2344:     PetscCall(PetscSectionGetStorageSize(coordSectionNew, &coordSizeNew));
2345:     PetscCall(VecSetSizes(coordsLocalNew, coordSizeNew, PETSC_DETERMINE));
2346:     PetscCall(PetscObjectGetName((PetscObject)coordsLocal, &name));
2347:     PetscCall(PetscObjectSetName((PetscObject)coordsLocalNew, name));
2348:     PetscCall(VecGetBlockSize(coordsLocal, &bs));
2349:     PetscCall(VecSetBlockSize(coordsLocalNew, dEo == dE ? bs : dE));
2350:     PetscCall(VecGetType(coordsLocal, &vtype));
2351:     PetscCall(VecSetType(coordsLocalNew, vtype));
2352:   }
2353:   PetscCall(VecGetArrayRead(coordsLocal, &coords));
2354:   PetscCall(VecGetArray(coordsLocalNew, &coordsNew));
2355:   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
2356:   /* First set coordinates for vertices */
2357:   for (p = pStart; p < pEnd; ++p) {
2358:     DMPolytopeType  ct;
2359:     DMPolytopeType *rct;
2360:     PetscInt       *rsize, *rcone, *rornt;
2361:     PetscInt        Nct, n, r;
2362:     PetscBool       hasVertex = PETSC_FALSE;

2364:     PetscCall(DMPlexGetCellType(dm, p, &ct));
2365:     PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2366:     for (n = 0; n < Nct; ++n) {
2367:       if (rct[n] == DM_POLYTOPE_POINT) {
2368:         hasVertex = PETSC_TRUE;
2369:         break;
2370:       }
2371:     }
2372:     if (hasVertex) {
2373:       const PetscScalar *icoords = NULL;
2374:       const PetscScalar *array   = NULL;
2375:       PetscScalar       *pcoords = NULL;
2376:       PetscBool          isDG;
2377:       PetscInt           Nc, Nv, v, d;

2379:       PetscCall(DMPlexGetCellCoordinates(dm, p, &isDG, &Nc, &array, &pcoords));

2381:       icoords = pcoords;
2382:       Nv      = Nc / dEo;
2383:       if (ct != DM_POLYTOPE_POINT) {
2384:         if (localizeVertices && maxCell) {
2385:           PetscScalar anchor[3];

2387:           for (d = 0; d < dEo; ++d) anchor[d] = pcoords[d];
2388:           for (v = 0; v < Nv; ++v) PetscCall(DMLocalizeCoordinate_Internal(dm, dEo, anchor, &pcoords[v * dEo], &pcoords[v * dEo]));
2389:         }
2390:       }
2391:       for (n = 0; n < Nct; ++n) {
2392:         if (rct[n] != DM_POLYTOPE_POINT) continue;
2393:         for (r = 0; r < rsize[n]; ++r) {
2394:           PetscScalar vcoords[3];
2395:           PetscInt    vNew, off;

2397:           PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &vNew));
2398:           PetscCall(PetscSectionGetOffset(coordSectionNew, vNew, &off));
2399:           PetscCall(DMPlexTransformMapCoordinates(tr, ct, rct[n], p, r, Nv, dEo, icoords, vcoords));
2400:           PetscCall(DMSnapToGeomModel(dm, p, dE, vcoords, &coordsNew[off]));
2401:         }
2402:       }
2403:       PetscCall(DMPlexRestoreCellCoordinates(dm, p, &isDG, &Nc, &array, &pcoords));
2404:     }
2405:   }
2406:   PetscCall(VecRestoreArrayRead(coordsLocal, &coords));
2407:   PetscCall(VecRestoreArray(coordsLocalNew, &coordsNew));
2408:   PetscCall(DMSetCoordinatesLocal(rdm, coordsLocalNew));
2409:   PetscCall(VecDestroy(&coordsLocalNew));
2410:   PetscCall(PetscSectionDestroy(&coordSectionNew));
2411:   /* Then set coordinates for cells by localizing */
2412:   if (!localizeCells) PetscCall(DMLocalizeCoordinates(rdm));
2413:   else {
2414:     VecType     vtype;
2415:     PetscInt    coordSizeNew, bs;
2416:     const char *name;

2418:     PetscCall(DMGetCellCoordinatesLocal(dm, &coordsLocalCell));
2419:     PetscCall(VecCreate(PETSC_COMM_SELF, &coordsLocalCellNew));
2420:     PetscCall(PetscSectionGetStorageSize(coordSectionCellNew, &coordSizeNew));
2421:     PetscCall(VecSetSizes(coordsLocalCellNew, coordSizeNew, PETSC_DETERMINE));
2422:     PetscCall(PetscObjectGetName((PetscObject)coordsLocalCell, &name));
2423:     PetscCall(PetscObjectSetName((PetscObject)coordsLocalCellNew, name));
2424:     PetscCall(VecGetBlockSize(coordsLocalCell, &bs));
2425:     PetscCall(VecSetBlockSize(coordsLocalCellNew, dEo == dE ? bs : dE));
2426:     PetscCall(VecGetType(coordsLocalCell, &vtype));
2427:     PetscCall(VecSetType(coordsLocalCellNew, vtype));
2428:     PetscCall(VecGetArrayRead(coordsLocalCell, &coords));
2429:     PetscCall(VecGetArray(coordsLocalCellNew, &coordsNew));

2431:     for (p = pStart; p < pEnd; ++p) {
2432:       DMPolytopeType  ct;
2433:       DMPolytopeType *rct;
2434:       PetscInt       *rsize, *rcone, *rornt;
2435:       PetscInt        dof = 0, Nct, n, r;

2437:       PetscCall(DMPlexGetCellType(dm, p, &ct));
2438:       PetscCall(DMPlexTransformCellTransform(tr, ct, p, NULL, &Nct, &rct, &rsize, &rcone, &rornt));
2439:       if (p >= cStart && p < cEnd) PetscCall(PetscSectionGetDof(coordSectionCell, p, &dof));
2440:       if (dof) {
2441:         const PetscScalar *pcoords;

2443:         PetscCall(DMPlexPointLocalRead(cdmCell, p, coords, &pcoords));
2444:         for (n = 0; n < Nct; ++n) {
2445:           const PetscInt Nr = rsize[n];

2447:           if (DMPolytopeTypeGetDim(ct) != DMPolytopeTypeGetDim(rct[n])) continue;
2448:           for (r = 0; r < Nr; ++r) {
2449:             PetscInt pNew, offNew;

2451:             /* It looks like Stefano and Lisandro are allowing localized coordinates without defining the periodic boundary, which means that
2452:                DMLocalizeCoordinate_Internal() will not work. Localized coordinates will have to have obtained by the affine map of the larger
2453:                cell to the ones it produces. */
2454:             PetscCall(DMPlexTransformGetTargetPoint(tr, ct, rct[n], p, r, &pNew));
2455:             PetscCall(PetscSectionGetOffset(coordSectionCellNew, pNew, &offNew));
2456:             PetscCall(DMPlexTransformMapLocalizedCoordinates(tr, ct, rct[n], r, pcoords, &coordsNew[offNew]));
2457:           }
2458:         }
2459:       }
2460:     }
2461:     PetscCall(VecRestoreArrayRead(coordsLocalCell, &coords));
2462:     PetscCall(VecRestoreArray(coordsLocalCellNew, &coordsNew));
2463:     PetscCall(DMSetCellCoordinatesLocal(rdm, coordsLocalCellNew));
2464:     PetscCall(VecDestroy(&coordsLocalCellNew));
2465:     PetscCall(PetscSectionDestroy(&coordSectionCellNew));
2466:   }
2467:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_SetCoordinates, tr, dm, 0, 0));
2468:   PetscFunctionReturn(PETSC_SUCCESS);
2469: }

2471: /*@
2472:   DMPlexTransformApply - Execute the transformation, producing another `DM`

2474:   Collective

2476:   Input Parameters:
2477: + tr - The `DMPlexTransform` object
2478: - dm - The original `DM`

2480:   Output Parameter:
2481: . trdm - The transformed `DM`

2483:   Level: intermediate

2485:   Options Database Keys:
2486: + -dm_plex_transform_label_match_strata    - Only label points of the same stratum as the producing point
2487: . -dm_plex_transform_label_replica_inc num - Increment for the label value to be multiplied by the replica number
2488: - -dm_plex_transform_active name           - Name for active mesh label

2490: .seealso: [](plex_transform_table), [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexTransform`, `DMPlexTransformCreate()`, `DMPlexTransformSetDM()`
2491: @*/
2492: PetscErrorCode DMPlexTransformApply(DMPlexTransform tr, DM dm, DM *trdm)
2493: {
2494:   DM                     rdm;
2495:   DMPlexInterpolatedFlag interp;
2496:   PetscInt               pStart, pEnd;

2498:   PetscFunctionBegin;
2501:   PetscAssertPointer(trdm, 3);
2502:   PetscCall(PetscLogEventBegin(DMPLEXTRANSFORM_Apply, tr, dm, 0, 0));
2503:   PetscCall(DMPlexTransformSetDM(tr, dm));

2505:   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &rdm));
2506:   PetscCall(DMSetType(rdm, DMPLEX));
2507:   PetscCall(DMPlexTransformSetDimensions(tr, dm, rdm));
2508:   /* Calculate number of new points of each depth */
2509:   PetscCall(DMPlexIsInterpolatedCollective(dm, &interp));
2510:   PetscCheck(interp == DMPLEX_INTERPOLATED_FULL, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be fully interpolated for regular refinement");
2511:   /* Step 1: Set chart */
2512:   PetscCall(DMPlexTransformGetChart(tr, &pStart, &pEnd));
2513:   PetscCall(DMPlexSetChart(rdm, pStart, pEnd));
2514:   /* Step 2: Set cone/support sizes (automatically stratifies) */
2515:   PetscCall(DMPlexTransformSetConeSizes(tr, rdm));
2516:   /* Step 3: Setup refined DM */
2517:   PetscCall(DMSetUp(rdm));
2518:   /* Step 4: Set cones and supports (automatically symmetrizes) */
2519:   PetscCall(DMPlexTransformSetCones(tr, rdm));
2520:   /* Step 5: Create pointSF */
2521:   PetscCall(DMPlexTransformCreateSF(tr, rdm));
2522:   /* Step 6: Create labels */
2523:   PetscCall(DMPlexTransformCreateLabels(tr, rdm));
2524:   /* Step 7: Set coordinates */
2525:   PetscCall(DMPlexTransformSetCoordinates(tr, rdm));
2526:   //   Do not copy periodicity, which was handled in DMPlexTransformSetCoordinates()
2527:   PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_TRUE, rdm));
2528:   // If the original DM was configured from options, the transformed DM should be as well
2529:   rdm->setfromoptionscalled = dm->setfromoptionscalled;
2530:   PetscCall(PetscLogEventEnd(DMPLEXTRANSFORM_Apply, tr, dm, 0, 0));
2531:   *trdm = rdm;
2532:   PetscFunctionReturn(PETSC_SUCCESS);
2533: }

2535: PetscErrorCode DMPlexTransformAdaptLabel(DM dm, PETSC_UNUSED Vec metric, DMLabel adaptLabel, PETSC_UNUSED DMLabel rgLabel, DM *rdm)
2536: {
2537:   DMPlexTransform tr;
2538:   DM              cdm, rcdm;
2539:   const char     *prefix;
2540:   PetscBool       save;

2542:   PetscFunctionBegin;
2543:   PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
2544:   PetscCall(PetscObjectSetName((PetscObject)tr, "Adapt Label Transform"));
2545:   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
2546:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
2547:   PetscCall(DMPlexTransformSetDM(tr, dm));
2548:   PetscCall(DMPlexTransformSetFromOptions(tr));
2549:   if (adaptLabel) PetscCall(DMPlexTransformSetActive(tr, adaptLabel));
2550:   PetscCall(DMPlexTransformSetUp(tr));
2551:   PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_transform_view"));
2552:   PetscCall(DMPlexTransformApply(tr, dm, rdm));
2553:   PetscCall(DMCopyDisc(dm, *rdm));
2554:   PetscCall(DMGetCoordinateDM(dm, &cdm));
2555:   PetscCall(DMGetCoordinateDM(*rdm, &rcdm));
2556:   PetscCall(DMCopyDisc(cdm, rcdm));
2557:   PetscCall(DMPlexTransformCreateDiscLabels(tr, *rdm));
2558:   PetscCall(DMCopyDisc(dm, *rdm));
2559:   PetscCall(DMPlexGetSaveTransform(dm, &save));
2560:   if (save) PetscCall(DMPlexSetTransform(*rdm, tr));
2561:   PetscCall(DMPlexTransformDestroy(&tr));
2562:   ((DM_Plex *)(*rdm)->data)->useHashLocation = ((DM_Plex *)dm->data)->useHashLocation;
2563:   PetscFunctionReturn(PETSC_SUCCESS);
2564: }