1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I "petscdmlabel.h" I*/ 30c312b8eSJed Brown #include <petscsf.h> 4e6ccafaeSMatthew G Knepley 5d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid) 6d71ae5a4SJacob Faibussowitsch { 7412e9a14SMatthew G. Knepley DMPolytopeType ct; 8412e9a14SMatthew G. Knepley 9412e9a14SMatthew G. Knepley PetscFunctionBegin; 109566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, p, &ct)); 11412e9a14SMatthew G. Knepley switch (ct) { 12412e9a14SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 13412e9a14SMatthew G. Knepley case DM_POLYTOPE_SEG_PRISM_TENSOR: 14412e9a14SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 15d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_QUAD_PRISM_TENSOR: 16d71ae5a4SJacob Faibussowitsch *isHybrid = PETSC_TRUE; 17d71ae5a4SJacob Faibussowitsch break; 18d71ae5a4SJacob Faibussowitsch default: 19d71ae5a4SJacob Faibussowitsch *isHybrid = PETSC_FALSE; 20d71ae5a4SJacob Faibussowitsch break; 21412e9a14SMatthew G. Knepley } 223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23412e9a14SMatthew G. Knepley } 24412e9a14SMatthew G. Knepley 25d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd) 26d71ae5a4SJacob Faibussowitsch { 27412e9a14SMatthew G. Knepley DMLabel ctLabel; 28412e9a14SMatthew G. Knepley 29412e9a14SMatthew G. Knepley PetscFunctionBegin; 30412e9a14SMatthew G. Knepley if (cStart) *cStart = -1; 31412e9a14SMatthew G. Knepley if (cEnd) *cEnd = -1; 329566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel)); 33412e9a14SMatthew G. Knepley switch (dim) { 34d71ae5a4SJacob Faibussowitsch case 1: 35d71ae5a4SJacob Faibussowitsch PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd)); 36d71ae5a4SJacob Faibussowitsch break; 37d71ae5a4SJacob Faibussowitsch case 2: 38d71ae5a4SJacob Faibussowitsch PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd)); 39d71ae5a4SJacob Faibussowitsch break; 40412e9a14SMatthew G. Knepley case 3: 419566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd)); 429566063dSJacob Faibussowitsch if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd)); 43412e9a14SMatthew G. Knepley break; 44d71ae5a4SJacob Faibussowitsch default: 453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46412e9a14SMatthew G. Knepley } 473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48412e9a14SMatthew G. Knepley } 49412e9a14SMatthew G. Knepley 5069941377SStefano Zampini PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label, PetscBool missing_only) 51d71ae5a4SJacob Faibussowitsch { 5269941377SStefano Zampini PetscInt depth, pStart, pEnd, fStart, fEnd, f, supportSize, nroots = -1, nleaves = -1, defval; 53429fa399SMatthew G. Knepley PetscSF sf; 5441e3e744Sksagiyam const PetscSFNode *iremote = NULL; 5541e3e744Sksagiyam const PetscInt *ilocal = NULL; 5641e3e744Sksagiyam PetscInt *leafData; 5730560a7bSMatthew G. Knepley 5830560a7bSMatthew G. Knepley PetscFunctionBegin; 5941e3e744Sksagiyam PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 6041e3e744Sksagiyam PetscCall(DMPlexGetDepth(dm, &depth)); 6141e3e744Sksagiyam if (depth >= cellHeight + 1) { 629566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd)); 6341e3e744Sksagiyam } else { 6441e3e744Sksagiyam /* Note DMPlexGetHeightStratum() returns fStart, fEnd = pStart, pEnd */ 6541e3e744Sksagiyam /* if height > depth, which is not what we want here. */ 6641e3e744Sksagiyam fStart = 0; 6741e3e744Sksagiyam fEnd = 0; 6841e3e744Sksagiyam } 6969941377SStefano Zampini PetscCall(DMLabelGetDefaultValue(label, &defval)); 7041e3e744Sksagiyam PetscCall(PetscCalloc1(pEnd - pStart, &leafData)); 718e3a54c0SPierre Jolivet leafData = PetscSafePointerPlusOffset(leafData, -pStart); 7241e3e744Sksagiyam PetscCall(DMGetPointSF(dm, &sf)); 7341e3e744Sksagiyam if (sf) PetscCall(PetscSFGetGraph(sf, &nroots, &nleaves, &ilocal, &iremote)); 7441e3e744Sksagiyam if (sf && nroots >= 0) { 7541e3e744Sksagiyam PetscInt cStart, cEnd, c, i; 7641e3e744Sksagiyam PetscInt *rootData, *rootData1, *cellOwners, hasTwoSupportCells = -2; 7741e3e744Sksagiyam const PetscInt *support; 7841e3e744Sksagiyam PetscMPIInt rank; 7930560a7bSMatthew G. Knepley 8041e3e744Sksagiyam PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 8141e3e744Sksagiyam PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd)); 8241e3e744Sksagiyam PetscCall(PetscCalloc3(pEnd - pStart, &rootData, pEnd - pStart, &rootData1, cEnd - cStart, &cellOwners)); 8341e3e744Sksagiyam rootData -= pStart; 8441e3e744Sksagiyam rootData1 -= pStart; 8541e3e744Sksagiyam for (c = cStart; c < cEnd; ++c) cellOwners[c - cStart] = (PetscInt)rank; 8641e3e744Sksagiyam for (i = 0; i < nleaves; ++i) { 8741e3e744Sksagiyam c = ilocal ? ilocal[i] : i; 8841e3e744Sksagiyam if (c >= cStart && c < cEnd) cellOwners[c - cStart] = iremote[i].rank; 8941e3e744Sksagiyam } 9041e3e744Sksagiyam for (f = fStart; f < fEnd; ++f) { 919566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, f, &supportSize)); 92e752be1aSMatthew G. Knepley if (supportSize == 1) { 9341e3e744Sksagiyam PetscCall(DMPlexGetSupport(dm, f, &support)); 9441e3e744Sksagiyam leafData[f] = cellOwners[support[0] - cStart]; 9541e3e744Sksagiyam } else { 9641e3e744Sksagiyam /* TODO: When using DMForest, we could have a parent facet (a coarse facet) */ 9741e3e744Sksagiyam /* supportSize of which alone does not tell us if it is an interior */ 9841e3e744Sksagiyam /* facet or an exterior facet. Those facets can be identified by checking */ 9941e3e744Sksagiyam /* if they are in the parent tree. We should probably skip those parent */ 10041e3e744Sksagiyam /* facets here, which will allow for including the following check, and */ 10141e3e744Sksagiyam /* see if they are exterior facets or not afterwards by checking if the */ 10241e3e744Sksagiyam /* children are exterior or not. */ 10341e3e744Sksagiyam /* PetscCheck(supportSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", supportSize, f); */ 10441e3e744Sksagiyam leafData[f] = hasTwoSupportCells; /* some negative PetscInt */ 105429fa399SMatthew G. Knepley } 10641e3e744Sksagiyam rootData[f] = leafData[f]; 10741e3e744Sksagiyam rootData1[f] = leafData[f]; 10841e3e744Sksagiyam } 10941e3e744Sksagiyam PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData, MPI_MIN)); 11041e3e744Sksagiyam PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leafData, rootData1, MPI_MAX)); 11141e3e744Sksagiyam PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData, MPI_MIN)); 11241e3e744Sksagiyam PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leafData, rootData1, MPI_MAX)); 11341e3e744Sksagiyam for (f = fStart; f < fEnd; ++f) { 11441e3e744Sksagiyam /* Store global support size of f. */ 11541e3e744Sksagiyam /* Facet f is an interior facet if and only if one of the following two is satisfied: */ 11641e3e744Sksagiyam /* 1. supportSize is 2 on some rank. */ 11741e3e744Sksagiyam /* 2. supportSize is 1 on any rank that can see f, but f is on a partition boundary; */ 11841e3e744Sksagiyam /* i.e., rootData[f] < rootData1[f]. */ 11941e3e744Sksagiyam rootData[f] = (rootData[f] == hasTwoSupportCells || (rootData[f] < rootData1[f])) ? 2 : 1; 12041e3e744Sksagiyam leafData[f] = rootData[f]; 12141e3e744Sksagiyam } 12241e3e744Sksagiyam PetscCall(PetscSFBcastBegin(sf, MPIU_INT, rootData, leafData, MPI_REPLACE)); 12341e3e744Sksagiyam PetscCall(PetscSFBcastEnd(sf, MPIU_INT, rootData, leafData, MPI_REPLACE)); 12441e3e744Sksagiyam rootData += pStart; 12541e3e744Sksagiyam rootData1 += pStart; 12641e3e744Sksagiyam PetscCall(PetscFree3(rootData, rootData1, cellOwners)); 12741e3e744Sksagiyam } else { 12841e3e744Sksagiyam for (f = fStart; f < fEnd; ++f) { 12941e3e744Sksagiyam PetscCall(DMPlexGetSupportSize(dm, f, &supportSize)); 13041e3e744Sksagiyam leafData[f] = supportSize; 13141e3e744Sksagiyam } 13241e3e744Sksagiyam } 13341e3e744Sksagiyam for (f = fStart; f < fEnd; ++f) { 13441e3e744Sksagiyam if (leafData[f] == 1) { 135e752be1aSMatthew G. Knepley if (val < 0) { 136e752be1aSMatthew G. Knepley PetscInt *closure = NULL; 137e752be1aSMatthew G. Knepley PetscInt clSize, cl, cval; 138e752be1aSMatthew G. Knepley 13969941377SStefano Zampini PetscAssert(!missing_only, PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); 1409566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure)); 141e752be1aSMatthew G. Knepley for (cl = 0; cl < clSize * 2; cl += 2) { 1429566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, closure[cl], &cval)); 143e752be1aSMatthew G. Knepley if (cval < 0) continue; 1449566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, f, cval)); 145e752be1aSMatthew G. Knepley break; 146e752be1aSMatthew G. Knepley } 1479566063dSJacob Faibussowitsch if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1)); 1489566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure)); 149e752be1aSMatthew G. Knepley } else { 15069941377SStefano Zampini if (missing_only) { 15169941377SStefano Zampini PetscInt fval; 15269941377SStefano Zampini PetscCall(DMLabelGetValue(label, f, &fval)); 15369941377SStefano Zampini if (fval != defval) PetscCall(DMLabelClearValue(label, f, fval)); 15469941377SStefano Zampini else PetscCall(DMLabelSetValue(label, f, val)); 15569941377SStefano Zampini } else { 1569566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, f, val)); 157e752be1aSMatthew G. Knepley } 15869941377SStefano Zampini } 15941e3e744Sksagiyam } else { 16041e3e744Sksagiyam /* TODO: See the above comment on DMForest */ 16141e3e744Sksagiyam /* PetscCheck(leafData[f] == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid facet support size (%" PetscInt_FMT ") on facet (%" PetscInt_FMT ")", leafData[f], f); */ 162e752be1aSMatthew G. Knepley } 16330560a7bSMatthew G. Knepley } 1648e3a54c0SPierre Jolivet leafData = PetscSafePointerPlusOffset(leafData, pStart); 16541e3e744Sksagiyam PetscCall(PetscFree(leafData)); 1663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16730560a7bSMatthew G. Knepley } 16830560a7bSMatthew G. Knepley 169cd0c2139SMatthew G Knepley /*@ 170cd0c2139SMatthew G Knepley DMPlexMarkBoundaryFaces - Mark all faces on the boundary 171cd0c2139SMatthew G Knepley 17241e3e744Sksagiyam Collective 173cd0c2139SMatthew G Knepley 174d8d19677SJose E. Roman Input Parameters: 175429fa399SMatthew G. Knepley + dm - The original `DM` 17620f4b53cSBarry Smith - val - The marker value, or `PETSC_DETERMINE` to use some value in the closure (or 1 if none are found) 177cd0c2139SMatthew G Knepley 178cd0c2139SMatthew G Knepley Output Parameter: 17920f4b53cSBarry Smith . label - The `DMLabel` marking boundary faces with the given value 180429fa399SMatthew G. Knepley 181cd0c2139SMatthew G Knepley Level: developer 182cd0c2139SMatthew G Knepley 18320f4b53cSBarry Smith Note: 18441e3e744Sksagiyam This function will use the point `PetscSF` from the input `DM` and the ownership of the support cells to exclude points on the partition boundary from being marked. If you also wish to mark the partition boundary, you can use `DMSetPointSF()` to temporarily set it to `NULL`, and then reset it to the original object after the call. 18541e3e744Sksagiyam 18641e3e744Sksagiyam In DMForest there can be facets support sizes of which alone can not determine whether they are on the boundary. Currently, this function is not guaranteed to produce the correct result in such case. 18720f4b53cSBarry Smith 1881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMLabelCreate()`, `DMCreateLabel()` 18909f723d9SJed Brown @*/ 190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label) 191d71ae5a4SJacob Faibussowitsch { 192bbb57e68SMatthew G. Knepley DM plex; 193827c4036SVaclav Hapla DMPlexInterpolatedFlag flg; 194cd0c2139SMatthew G Knepley 195cd0c2139SMatthew G Knepley PetscFunctionBegin; 196827c4036SVaclav Hapla PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 197bbb57e68SMatthew G. Knepley PetscCall(DMConvert(dm, DMPLEX, &plex)); 198bbb57e68SMatthew G. Knepley PetscCall(DMPlexIsInterpolated(plex, &flg)); 19908401ef6SPierre Jolivet PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank"); 200bbb57e68SMatthew G. Knepley PetscCall(DMPlexMarkBoundaryFaces_Internal(plex, val, 0, label, PETSC_FALSE)); 201bbb57e68SMatthew G. Knepley PetscCall(DMDestroy(&plex)); 2023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 203cd0c2139SMatthew G Knepley } 204cd0c2139SMatthew G Knepley 205d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells) 206d71ae5a4SJacob Faibussowitsch { 207b0bf5782SToby Isaac IS valueIS; 208ac51f24eSSander Arens PetscSF sfPoint; 209b0bf5782SToby Isaac const PetscInt *values; 210ac51f24eSSander Arens PetscInt numValues, v, cStart, cEnd, nroots; 211b0bf5782SToby Isaac 212b0bf5782SToby Isaac PetscFunctionBegin; 2139566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &numValues)); 2149566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 2159566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 2169566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 217b0bf5782SToby Isaac for (v = 0; v < numValues; ++v) { 218b0bf5782SToby Isaac IS pointIS; 219b0bf5782SToby Isaac const PetscInt *points; 220b0bf5782SToby Isaac PetscInt numPoints, p; 221b0bf5782SToby Isaac 2229566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints)); 2239566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS)); 2249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 225b0bf5782SToby Isaac for (p = 0; p < numPoints; ++p) { 226b0bf5782SToby Isaac PetscInt q = points[p]; 227b0bf5782SToby Isaac PetscInt *closure = NULL; 228b0bf5782SToby Isaac PetscInt closureSize, c; 229b0bf5782SToby Isaac 230b0bf5782SToby Isaac if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */ 231b0bf5782SToby Isaac continue; 232b0bf5782SToby Isaac } 2339566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure)); 23448a46eb9SPierre Jolivet for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v])); 2359566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure)); 236b0bf5782SToby Isaac } 2379566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 2389566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 239b0bf5782SToby Isaac } 2409566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 2419566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 2429566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 2439566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL)); 244ac51f24eSSander Arens if (nroots >= 0) { 24526279d81SSanderA DMLabel lblRoots, lblLeaves; 24626279d81SSanderA IS valueIS, pointIS; 24726279d81SSanderA const PetscInt *values; 24826279d81SSanderA PetscInt numValues, v; 24926279d81SSanderA 25026279d81SSanderA /* Pull point contributions from remote leaves into local roots */ 2519566063dSJacob Faibussowitsch PetscCall(DMLabelGather(label, sfPoint, &lblLeaves)); 2529566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS)); 2539566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &numValues)); 2549566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 25526279d81SSanderA for (v = 0; v < numValues; ++v) { 25626279d81SSanderA const PetscInt value = values[v]; 25726279d81SSanderA 2589566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS)); 2599566063dSJacob Faibussowitsch PetscCall(DMLabelInsertIS(label, pointIS, value)); 2609566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 26126279d81SSanderA } 2629566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 2639566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 2649566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblLeaves)); 26526279d81SSanderA /* Push point contributions from roots into remote leaves */ 2669566063dSJacob Faibussowitsch PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots)); 2679566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(lblRoots, &valueIS)); 2689566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &numValues)); 2699566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 27026279d81SSanderA for (v = 0; v < numValues; ++v) { 27126279d81SSanderA const PetscInt value = values[v]; 27226279d81SSanderA 2739566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS)); 2749566063dSJacob Faibussowitsch PetscCall(DMLabelInsertIS(label, pointIS, value)); 2759566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 27626279d81SSanderA } 2779566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 2789566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 2799566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&lblRoots)); 28026279d81SSanderA } 2813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 282b0bf5782SToby Isaac } 283b0bf5782SToby Isaac 2842be2b188SMatthew G Knepley /*@ 2852be2b188SMatthew G Knepley DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface 2862be2b188SMatthew G Knepley 2872be2b188SMatthew G Knepley Input Parameters: 28820f4b53cSBarry Smith + dm - The `DM` 28920f4b53cSBarry Smith - label - A `DMLabel` marking the surface points 2902be2b188SMatthew G Knepley 2912be2b188SMatthew G Knepley Output Parameter: 29220f4b53cSBarry Smith . label - A `DMLabel` marking all surface points in the transitive closure 2932be2b188SMatthew G Knepley 2942be2b188SMatthew G Knepley Level: developer 2952be2b188SMatthew G Knepley 2961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelCohesiveComplete()` 2972be2b188SMatthew G Knepley @*/ 298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label) 299d71ae5a4SJacob Faibussowitsch { 3002be2b188SMatthew G Knepley PetscFunctionBegin; 3019566063dSJacob Faibussowitsch PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE)); 3023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3032be2b188SMatthew G Knepley } 3042be2b188SMatthew G Knepley 3056cf0e42fSMatthew G. Knepley /*@ 306a6e0b375SMatthew G. Knepley DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point 3076cf0e42fSMatthew G. Knepley 3086cf0e42fSMatthew G. Knepley Input Parameters: 30920f4b53cSBarry Smith + dm - The `DM` 31020f4b53cSBarry Smith - label - A `DMLabel` marking the surface points 3116cf0e42fSMatthew G. Knepley 3126cf0e42fSMatthew G. Knepley Output Parameter: 31320f4b53cSBarry Smith . label - A `DMLabel` incorporating cells 3146cf0e42fSMatthew G. Knepley 3156cf0e42fSMatthew G. Knepley Level: developer 3166cf0e42fSMatthew G. Knepley 31720f4b53cSBarry Smith Note: 31820f4b53cSBarry Smith The cells allow FEM boundary conditions to be applied using the cell geometry 3196cf0e42fSMatthew G. Knepley 3201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()` 3216cf0e42fSMatthew G. Knepley @*/ 322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label) 323d71ae5a4SJacob Faibussowitsch { 3246cf0e42fSMatthew G. Knepley IS valueIS; 3256cf0e42fSMatthew G. Knepley const PetscInt *values; 3269df54392SMatthew G. Knepley PetscInt numValues, v, csStart, csEnd, chStart, chEnd; 3276cf0e42fSMatthew G. Knepley 3286cf0e42fSMatthew G. Knepley PetscFunctionBegin; 3299df54392SMatthew G. Knepley PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &csStart, &csEnd)); 3309df54392SMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, &chStart, &chEnd)); 3319566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &numValues)); 3329566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 3339566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 3346cf0e42fSMatthew G. Knepley for (v = 0; v < numValues; ++v) { 3356cf0e42fSMatthew G. Knepley IS pointIS; 3366cf0e42fSMatthew G. Knepley const PetscInt *points; 3376cf0e42fSMatthew G. Knepley PetscInt numPoints, p; 3386cf0e42fSMatthew G. Knepley 3399566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints)); 3409566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS)); 3419566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 3426cf0e42fSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 3439df54392SMatthew G. Knepley const PetscInt point = points[p]; 3446cf0e42fSMatthew G. Knepley PetscInt *closure = NULL; 3457bd9760aSJed Brown PetscInt closureSize, cl, h, cStart, cEnd; 3467bd9760aSJed Brown DMPolytopeType ct; 3479df54392SMatthew G. Knepley 3489df54392SMatthew G. Knepley // If the point is a hybrid, allow hybrid cells 3497bd9760aSJed Brown PetscCall(DMPlexGetCellType(dm, point, &ct)); 3509df54392SMatthew G. Knepley PetscCall(DMPlexGetPointHeight(dm, point, &h)); 3517bd9760aSJed Brown if (DMPolytopeTypeIsHybrid(ct)) { 3529df54392SMatthew G. Knepley cStart = chStart; 3539df54392SMatthew G. Knepley cEnd = chEnd; 3547bd9760aSJed Brown } else { 3557bd9760aSJed Brown cStart = csStart; 3567bd9760aSJed Brown cEnd = csEnd; 3579df54392SMatthew G. Knepley } 3586cf0e42fSMatthew G. Knepley 3599566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure)); 36022eabd52SMatthew G. Knepley for (cl = closureSize - 1; cl > 0; --cl) { 361a6e0b375SMatthew G. Knepley const PetscInt cell = closure[cl * 2]; 3629371c9d4SSatish Balay if ((cell >= cStart) && (cell < cEnd)) { 3639371c9d4SSatish Balay PetscCall(DMLabelSetValue(label, cell, values[v])); 3649371c9d4SSatish Balay break; 3659371c9d4SSatish Balay } 36622eabd52SMatthew G. Knepley } 3679566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure)); 3686cf0e42fSMatthew G. Knepley } 3699566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 3709566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 3716cf0e42fSMatthew G. Knepley } 3729566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 3739566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 3743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3756cf0e42fSMatthew G. Knepley } 3766cf0e42fSMatthew G. Knepley 377f402d5e4SToby Isaac /*@ 378a6e0b375SMatthew G. Knepley DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face 379a6e0b375SMatthew G. Knepley 380a6e0b375SMatthew G. Knepley Input Parameters: 38120f4b53cSBarry Smith + dm - The `DM` 38220f4b53cSBarry Smith - label - A `DMLabel` marking the surface points 383a6e0b375SMatthew G. Knepley 384a6e0b375SMatthew G. Knepley Output Parameter: 38520f4b53cSBarry Smith . label - A `DMLabel` incorporating cells 386a6e0b375SMatthew G. Knepley 387a6e0b375SMatthew G. Knepley Level: developer 388a6e0b375SMatthew G. Knepley 38920f4b53cSBarry Smith Note: 39020f4b53cSBarry Smith The cells allow FEM boundary conditions to be applied using the cell geometry 391a6e0b375SMatthew G. Knepley 3921cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()` 393a6e0b375SMatthew G. Knepley @*/ 394d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label) 395d71ae5a4SJacob Faibussowitsch { 396a6e0b375SMatthew G. Knepley IS valueIS; 397a6e0b375SMatthew G. Knepley const PetscInt *values; 398a6e0b375SMatthew G. Knepley PetscInt numValues, v, cStart, cEnd, fStart, fEnd; 399a6e0b375SMatthew G. Knepley 400a6e0b375SMatthew G. Knepley PetscFunctionBegin; 4019566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 4029566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 4039566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &numValues)); 4049566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 4059566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 406a6e0b375SMatthew G. Knepley for (v = 0; v < numValues; ++v) { 407a6e0b375SMatthew G. Knepley IS pointIS; 408a6e0b375SMatthew G. Knepley const PetscInt *points; 409a6e0b375SMatthew G. Knepley PetscInt numPoints, p; 410a6e0b375SMatthew G. Knepley 4119566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints)); 4129566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS)); 4139566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 414a6e0b375SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 415a6e0b375SMatthew G. Knepley const PetscInt face = points[p]; 416a6e0b375SMatthew G. Knepley PetscInt *closure = NULL; 417a6e0b375SMatthew G. Knepley PetscInt closureSize, cl; 418a6e0b375SMatthew G. Knepley 419a6e0b375SMatthew G. Knepley if ((face < fStart) || (face >= fEnd)) continue; 4209566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure)); 421a6e0b375SMatthew G. Knepley for (cl = closureSize - 1; cl > 0; --cl) { 422a6e0b375SMatthew G. Knepley const PetscInt cell = closure[cl * 2]; 4239371c9d4SSatish Balay if ((cell >= cStart) && (cell < cEnd)) { 4249371c9d4SSatish Balay PetscCall(DMLabelSetValue(label, cell, values[v])); 4259371c9d4SSatish Balay break; 4269371c9d4SSatish Balay } 427a6e0b375SMatthew G. Knepley } 4289566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure)); 429a6e0b375SMatthew G. Knepley } 4309566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 4319566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 432a6e0b375SMatthew G. Knepley } 4339566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 4349566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 4353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 436a6e0b375SMatthew G. Knepley } 437a6e0b375SMatthew G. Knepley 438a6e0b375SMatthew G. Knepley /*@ 439f402d5e4SToby Isaac DMPlexLabelClearCells - Remove cells from a label 440f402d5e4SToby Isaac 441f402d5e4SToby Isaac Input Parameters: 44220f4b53cSBarry Smith + dm - The `DM` 44320f4b53cSBarry Smith - label - A `DMLabel` marking surface points and their adjacent cells 444f402d5e4SToby Isaac 445f402d5e4SToby Isaac Output Parameter: 44620f4b53cSBarry Smith . label - A `DMLabel` without cells 447f402d5e4SToby Isaac 448f402d5e4SToby Isaac Level: developer 449f402d5e4SToby Isaac 45020f4b53cSBarry Smith Note: 45120f4b53cSBarry Smith This undoes `DMPlexLabelAddCells()` or `DMPlexLabelAddFaceCells()` 452f402d5e4SToby Isaac 4531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()` 454f402d5e4SToby Isaac @*/ 455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label) 456d71ae5a4SJacob Faibussowitsch { 457f402d5e4SToby Isaac IS valueIS; 458f402d5e4SToby Isaac const PetscInt *values; 459485ad865SMatthew G. Knepley PetscInt numValues, v, cStart, cEnd; 460f402d5e4SToby Isaac 461f402d5e4SToby Isaac PetscFunctionBegin; 4629566063dSJacob Faibussowitsch PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd)); 4639566063dSJacob Faibussowitsch PetscCall(DMLabelGetNumValues(label, &numValues)); 4649566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 4659566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 466f402d5e4SToby Isaac for (v = 0; v < numValues; ++v) { 467f402d5e4SToby Isaac IS pointIS; 468f402d5e4SToby Isaac const PetscInt *points; 469f402d5e4SToby Isaac PetscInt numPoints, p; 470f402d5e4SToby Isaac 4719566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints)); 4729566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS)); 4739566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 474f402d5e4SToby Isaac for (p = 0; p < numPoints; ++p) { 475f402d5e4SToby Isaac PetscInt point = points[p]; 476f402d5e4SToby Isaac 47748a46eb9SPierre Jolivet if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v])); 478f402d5e4SToby Isaac } 4799566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 4809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 481f402d5e4SToby Isaac } 4829566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 4839566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 4843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 485f402d5e4SToby Isaac } 486f402d5e4SToby Isaac 48759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending 48859eef20bSToby Isaac * index (skipping first, which is (0,0)) */ 489d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[]) 490d71ae5a4SJacob Faibussowitsch { 4912582d50cSToby Isaac PetscInt d, off = 0; 4922582d50cSToby Isaac 4932582d50cSToby Isaac PetscFunctionBegin; 49459eef20bSToby Isaac /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */ 4950974a383SToby Isaac for (d = 0; d < depth; d++) { 4962582d50cSToby Isaac PetscInt firstd = d; 4970974a383SToby Isaac PetscInt firstStart = depthShift[2 * d]; 4982582d50cSToby Isaac PetscInt e; 4992582d50cSToby Isaac 5002582d50cSToby Isaac for (e = d + 1; e <= depth; e++) { 5012582d50cSToby Isaac if (depthShift[2 * e] < firstStart) { 5022582d50cSToby Isaac firstd = e; 5032582d50cSToby Isaac firstStart = depthShift[2 * d]; 5042582d50cSToby Isaac } 5052582d50cSToby Isaac } 5062582d50cSToby Isaac if (firstd != d) { 5072582d50cSToby Isaac PetscInt swap[2]; 5082582d50cSToby Isaac 5092582d50cSToby Isaac e = firstd; 5102582d50cSToby Isaac swap[0] = depthShift[2 * d]; 5112582d50cSToby Isaac swap[1] = depthShift[2 * d + 1]; 5122582d50cSToby Isaac depthShift[2 * d] = depthShift[2 * e]; 5132582d50cSToby Isaac depthShift[2 * d + 1] = depthShift[2 * e + 1]; 5142582d50cSToby Isaac depthShift[2 * e] = swap[0]; 5152582d50cSToby Isaac depthShift[2 * e + 1] = swap[1]; 5162582d50cSToby Isaac } 5172582d50cSToby Isaac } 5182582d50cSToby Isaac /* convert (oldstart, added) to (oldstart, newstart) */ 5190974a383SToby Isaac for (d = 0; d <= depth; d++) { 5202582d50cSToby Isaac off += depthShift[2 * d + 1]; 52159eef20bSToby Isaac depthShift[2 * d + 1] = depthShift[2 * d] + off; 5222582d50cSToby Isaac } 5233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5242582d50cSToby Isaac } 5252582d50cSToby Isaac 5262582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */ 527d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[]) 528d71ae5a4SJacob Faibussowitsch { 5292582d50cSToby Isaac PetscInt d; 5302582d50cSToby Isaac PetscInt newOff = 0; 5312582d50cSToby Isaac 5322582d50cSToby Isaac for (d = 0; d <= depth; d++) { 5332582d50cSToby Isaac if (p < depthShift[2 * d]) return p + newOff; 5342582d50cSToby Isaac else newOff = depthShift[2 * d + 1] - depthShift[2 * d]; 5352582d50cSToby Isaac } 5360974a383SToby Isaac return p + newOff; 5372582d50cSToby Isaac } 5382582d50cSToby Isaac 5392582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */ 540d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[]) 541d71ae5a4SJacob Faibussowitsch { 5422582d50cSToby Isaac PetscInt d; 5432582d50cSToby Isaac PetscInt newOff = 0; 5442582d50cSToby Isaac 5452582d50cSToby Isaac for (d = 0; d <= depth; d++) { 5462582d50cSToby Isaac if (p < depthShift[2 * d + 1]) return p + newOff; 5472582d50cSToby Isaac else newOff = depthShift[2 * d] - depthShift[2 * d + 1]; 5482582d50cSToby Isaac } 5490974a383SToby Isaac return p + newOff; 550cd0c2139SMatthew G Knepley } 551cd0c2139SMatthew G Knepley 552d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew) 553d71ae5a4SJacob Faibussowitsch { 554cd0c2139SMatthew G Knepley PetscInt depth = 0, d, pStart, pEnd, p; 555fa8e8ae5SToby Isaac DMLabel depthLabel; 556cd0c2139SMatthew G Knepley 557cd0c2139SMatthew G Knepley PetscFunctionBegin; 5589566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 5593ba16761SJacob Faibussowitsch if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS); 560cd0c2139SMatthew G Knepley /* Step 1: Expand chart */ 5619566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 5620974a383SToby Isaac pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift); 5639566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dmNew, pStart, pEnd)); 5649566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dmNew, "depth")); 5659566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel)); 5669566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dmNew, "celltype")); 567cd0c2139SMatthew G Knepley /* Step 2: Set cone and support sizes */ 568cd0c2139SMatthew G Knepley for (d = 0; d <= depth; ++d) { 569fa8e8ae5SToby Isaac PetscInt pStartNew, pEndNew; 570fa8e8ae5SToby Isaac IS pIS; 571fa8e8ae5SToby Isaac 5729566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd)); 573fa8e8ae5SToby Isaac pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift); 574fa8e8ae5SToby Isaac pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift); 5759566063dSJacob Faibussowitsch PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS)); 5769566063dSJacob Faibussowitsch PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS)); 5779566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pIS)); 578cd0c2139SMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 5792582d50cSToby Isaac PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift); 580cd0c2139SMatthew G Knepley PetscInt size; 581412e9a14SMatthew G. Knepley DMPolytopeType ct; 582cd0c2139SMatthew G Knepley 5839566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, p, &size)); 5849566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(dmNew, newp, size)); 5859566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, p, &size)); 5869566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(dmNew, newp, size)); 5879566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, p, &ct)); 5889566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(dmNew, newp, ct)); 589cd0c2139SMatthew G Knepley } 590cd0c2139SMatthew G Knepley } 5913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 592cd0c2139SMatthew G Knepley } 593cd0c2139SMatthew G Knepley 594d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew) 595d71ae5a4SJacob Faibussowitsch { 5962582d50cSToby Isaac PetscInt *newpoints; 5972582d50cSToby Isaac PetscInt depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p; 598cd0c2139SMatthew G Knepley 599cd0c2139SMatthew G Knepley PetscFunctionBegin; 6009566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 6013ba16761SJacob Faibussowitsch if (depth < 0) PetscFunctionReturn(PETSC_SUCCESS); 6029566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize)); 6039566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew)); 6049566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints)); 605cd0c2139SMatthew G Knepley /* Step 5: Set cones and supports */ 6069566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 607cd0c2139SMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 608cd0c2139SMatthew G Knepley const PetscInt *points = NULL, *orientations = NULL; 6092582d50cSToby Isaac PetscInt size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift); 610cd0c2139SMatthew G Knepley 6119566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, p, &size)); 6129566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, p, &points)); 6139566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, p, &orientations)); 614ad540459SPierre Jolivet for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift); 6159566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dmNew, newp, newpoints)); 6169566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations)); 6179566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, p, &size)); 6189566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew)); 6199566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, p, &points)); 620ad540459SPierre Jolivet for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift); 621dcbb62e8SMatthew G. Knepley for (i = size; i < sizeNew; ++i) newpoints[i] = 0; 6229566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(dmNew, newp, newpoints)); 623cd0c2139SMatthew G Knepley } 6249566063dSJacob Faibussowitsch PetscCall(PetscFree(newpoints)); 6253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 626cd0c2139SMatthew G Knepley } 627cd0c2139SMatthew G Knepley 628d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew) 629d71ae5a4SJacob Faibussowitsch { 630cd0c2139SMatthew G Knepley PetscSection coordSection, newCoordSection; 631cd0c2139SMatthew G Knepley Vec coordinates, newCoordinates; 632cd0c2139SMatthew G Knepley PetscScalar *coords, *newCoords; 633f2b8cce1SMatthew G. Knepley PetscInt coordSize, sStart, sEnd; 634f2b8cce1SMatthew G. Knepley PetscInt dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v; 635f2b8cce1SMatthew G. Knepley PetscBool hasCells; 636cd0c2139SMatthew G Knepley 637cd0c2139SMatthew G Knepley PetscFunctionBegin; 6389566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dim)); 6399566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dmNew, dim)); 6409566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 641cd0c2139SMatthew G Knepley /* Step 8: Convert coordinates */ 6429566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 6439566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd)); 6449566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew)); 6459566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew)); 6469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 6479566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection)); 6489566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(newCoordSection, 1)); 6499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim)); 6509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd)); 651f2b8cce1SMatthew G. Knepley hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE; 6529566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew)); 653f2b8cce1SMatthew G. Knepley if (hasCells) { 654f2b8cce1SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 655f2b8cce1SMatthew G. Knepley PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof; 656f2b8cce1SMatthew G. Knepley 6579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, c, &dof)); 6589566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof)); 6599566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof)); 660f2b8cce1SMatthew G. Knepley } 661f2b8cce1SMatthew G. Knepley } 662cd0c2139SMatthew G Knepley for (v = vStartNew; v < vEndNew; ++v) { 6639566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(newCoordSection, v, dim)); 6649566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim)); 665cd0c2139SMatthew G Knepley } 6669566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(newCoordSection)); 6679566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection)); 6689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize)); 6699566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates)); 6709566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates")); 6719566063dSJacob Faibussowitsch PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE)); 6729566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(newCoordinates, dim)); 6739566063dSJacob Faibussowitsch PetscCall(VecSetType(newCoordinates, VECSTANDARD)); 6749566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates)); 6759566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 6769566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 6779566063dSJacob Faibussowitsch PetscCall(VecGetArray(newCoordinates, &newCoords)); 678f2b8cce1SMatthew G. Knepley if (hasCells) { 679f2b8cce1SMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 680f2b8cce1SMatthew G. Knepley PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d; 681f2b8cce1SMatthew G. Knepley 6829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, c, &dof)); 6839566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, c, &off)); 6849566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff)); 685f2b8cce1SMatthew G. Knepley for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d]; 686f2b8cce1SMatthew G. Knepley } 687f2b8cce1SMatthew G. Knepley } 688cd0c2139SMatthew G Knepley for (v = vStart; v < vEnd; ++v) { 689cd0c2139SMatthew G Knepley PetscInt dof, off, noff, d; 690cd0c2139SMatthew G Knepley 6919566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, v, &dof)); 6929566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, v, &off)); 6939566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff)); 694f2b8cce1SMatthew G. Knepley for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d]; 695cd0c2139SMatthew G Knepley } 6969566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 6979566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(newCoordinates, &newCoords)); 6989566063dSJacob Faibussowitsch PetscCall(VecDestroy(&newCoordinates)); 6999566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&newCoordSection)); 7003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 701cd0c2139SMatthew G Knepley } 702cd0c2139SMatthew G Knepley 703d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew) 704d71ae5a4SJacob Faibussowitsch { 705cd0c2139SMatthew G Knepley const PetscSFNode *remotePoints; 706cd0c2139SMatthew G Knepley PetscSFNode *gremotePoints; 707cd0c2139SMatthew G Knepley const PetscInt *localPoints; 708cd0c2139SMatthew G Knepley PetscInt *glocalPoints, *newLocation, *newRemoteLocation; 7090e33faafSMatthew G. Knepley PetscInt numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0; 710cd0c2139SMatthew G Knepley 711cd0c2139SMatthew G Knepley PetscFunctionBegin; 7129566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 7139566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 7149566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints)); 7156e21efdcSToby Isaac totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd; 716cd0c2139SMatthew G Knepley if (numRoots >= 0) { 7179566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation)); 7180e33faafSMatthew G. Knepley for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift); 7199566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE)); 7209566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE)); 7219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numLeaves, &glocalPoints)); 7229566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numLeaves, &gremotePoints)); 723cd0c2139SMatthew G Knepley for (l = 0; l < numLeaves; ++l) { 7242582d50cSToby Isaac glocalPoints[l] = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift); 725cd0c2139SMatthew G Knepley gremotePoints[l].rank = remotePoints[l].rank; 726cd0c2139SMatthew G Knepley gremotePoints[l].index = newRemoteLocation[localPoints[l]]; 727cd0c2139SMatthew G Knepley } 7289566063dSJacob Faibussowitsch PetscCall(PetscFree2(newLocation, newRemoteLocation)); 7299566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER)); 7300e33faafSMatthew G. Knepley } 7313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7320e33faafSMatthew G. Knepley } 7330e33faafSMatthew G. Knepley 734d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew) 735d71ae5a4SJacob Faibussowitsch { 7360e33faafSMatthew G. Knepley PetscSF sfPoint, sfPointNew; 7370e33faafSMatthew G. Knepley PetscBool useNatural; 7380e33faafSMatthew G. Knepley 7390e33faafSMatthew G. Knepley PetscFunctionBegin; 7400e33faafSMatthew G. Knepley /* Step 9: Convert pointSF */ 7419566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 7429566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dmNew, &sfPointNew)); 7439566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew)); 7440e33faafSMatthew G. Knepley /* Step 9b: Convert naturalSF */ 7459566063dSJacob Faibussowitsch PetscCall(DMGetUseNatural(dm, &useNatural)); 7460e33faafSMatthew G. Knepley if (useNatural) { 7470e33faafSMatthew G. Knepley PetscSF sfNat, sfNatNew; 7480e33faafSMatthew G. Knepley 7499566063dSJacob Faibussowitsch PetscCall(DMSetUseNatural(dmNew, useNatural)); 7509566063dSJacob Faibussowitsch PetscCall(DMGetNaturalSF(dm, &sfNat)); 7519566063dSJacob Faibussowitsch PetscCall(DMGetNaturalSF(dmNew, &sfNatNew)); 7529566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew)); 753cd0c2139SMatthew G Knepley } 7543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 755cd0c2139SMatthew G Knepley } 756cd0c2139SMatthew G Knepley 757d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew) 758d71ae5a4SJacob Faibussowitsch { 759d56405f8SMatthew G. Knepley PetscInt depth = 0, numLabels, l; 760cd0c2139SMatthew G Knepley 761cd0c2139SMatthew G Knepley PetscFunctionBegin; 7629566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 763cd0c2139SMatthew G Knepley /* Step 10: Convert labels */ 7649566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &numLabels)); 765cd0c2139SMatthew G Knepley for (l = 0; l < numLabels; ++l) { 766cd0c2139SMatthew G Knepley DMLabel label, newlabel; 767cd0c2139SMatthew G Knepley const char *lname; 768fa8e8ae5SToby Isaac PetscBool isDepth, isDim; 769cd0c2139SMatthew G Knepley IS valueIS; 770cd0c2139SMatthew G Knepley const PetscInt *values; 771cd0c2139SMatthew G Knepley PetscInt numValues, val; 772cd0c2139SMatthew G Knepley 7739566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(dm, l, &lname)); 7749566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "depth", &isDepth)); 775cd0c2139SMatthew G Knepley if (isDepth) continue; 7769566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "dim", &isDim)); 777fa8e8ae5SToby Isaac if (isDim) continue; 7789566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dmNew, lname)); 7799566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, lname, &label)); 7809566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dmNew, lname, &newlabel)); 7819566063dSJacob Faibussowitsch PetscCall(DMLabelGetDefaultValue(label, &val)); 7829566063dSJacob Faibussowitsch PetscCall(DMLabelSetDefaultValue(newlabel, val)); 7839566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 7849566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &numValues)); 7859566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 786cd0c2139SMatthew G Knepley for (val = 0; val < numValues; ++val) { 787cd0c2139SMatthew G Knepley IS pointIS; 788cd0c2139SMatthew G Knepley const PetscInt *points; 789cd0c2139SMatthew G Knepley PetscInt numPoints, p; 790cd0c2139SMatthew G Knepley 7919566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS)); 7929566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(pointIS, &numPoints)); 7939566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 794cd0c2139SMatthew G Knepley for (p = 0; p < numPoints; ++p) { 7952582d50cSToby Isaac const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift); 796cd0c2139SMatthew G Knepley 7979566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(newlabel, newpoint, values[val])); 798cd0c2139SMatthew G Knepley } 7999566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 8009566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 801cd0c2139SMatthew G Knepley } 8029566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 8039566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 804cd0c2139SMatthew G Knepley } 8053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 806d56405f8SMatthew G. Knepley } 807d56405f8SMatthew G. Knepley 808d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew) 809d71ae5a4SJacob Faibussowitsch { 810d56405f8SMatthew G. Knepley PetscSF sfPoint; 811d56405f8SMatthew G. Knepley DMLabel vtkLabel, ghostLabel = NULL; 812d56405f8SMatthew G. Knepley const PetscSFNode *leafRemote; 813d56405f8SMatthew G. Knepley const PetscInt *leafLocal; 814d56405f8SMatthew G. Knepley PetscInt cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l; 815d56405f8SMatthew G. Knepley PetscMPIInt rank; 816d56405f8SMatthew G. Knepley 817d56405f8SMatthew G. Knepley PetscFunctionBegin; 818cd0c2139SMatthew G Knepley /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */ 8199566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 8209566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 8219566063dSJacob Faibussowitsch PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight)); 8229566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd)); 8239566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote)); 8249566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dmNew, "vtk")); 8259566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel)); 826d56405f8SMatthew G. Knepley if (createGhostLabel) { 8279566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dmNew, "ghost")); 8289566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel)); 829d56405f8SMatthew G. Knepley } 830cd0c2139SMatthew G Knepley for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) { 83148a46eb9SPierre Jolivet for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1)); 832cd0c2139SMatthew G Knepley if (leafLocal[l] >= cEnd) break; 833cd0c2139SMatthew G Knepley if (leafRemote[l].rank == rank) { 8349566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(vtkLabel, c, 1)); 8351baa6e33SBarry Smith } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2)); 836cd0c2139SMatthew G Knepley } 83748a46eb9SPierre Jolivet for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1)); 838d56405f8SMatthew G. Knepley if (ghostLabel) { 8399566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd)); 840cd0c2139SMatthew G Knepley for (f = fStart; f < fEnd; ++f) { 841cd0c2139SMatthew G Knepley PetscInt numCells; 842cd0c2139SMatthew G Knepley 8439566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells)); 844cd0c2139SMatthew G Knepley if (numCells < 2) { 8459566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(ghostLabel, f, 1)); 846cd0c2139SMatthew G Knepley } else { 847cd0c2139SMatthew G Knepley const PetscInt *cells = NULL; 848cd0c2139SMatthew G Knepley PetscInt vA, vB; 849cd0c2139SMatthew G Knepley 8509566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dmNew, f, &cells)); 8519566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA)); 8529566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB)); 8539566063dSJacob Faibussowitsch if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1)); 854cd0c2139SMatthew G Knepley } 855cd0c2139SMatthew G Knepley } 856d56405f8SMatthew G. Knepley } 8573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 858cd0c2139SMatthew G Knepley } 859cd0c2139SMatthew G Knepley 860d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew) 861d71ae5a4SJacob Faibussowitsch { 862ca04dac2SToby Isaac DM refTree; 863ca04dac2SToby Isaac PetscSection pSec; 864ca04dac2SToby Isaac PetscInt *parents, *childIDs; 865ca04dac2SToby Isaac 866ca04dac2SToby Isaac PetscFunctionBegin; 8679566063dSJacob Faibussowitsch PetscCall(DMPlexGetReferenceTree(dm, &refTree)); 8689566063dSJacob Faibussowitsch PetscCall(DMPlexSetReferenceTree(dmNew, refTree)); 8699566063dSJacob Faibussowitsch PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL)); 870ca04dac2SToby Isaac if (pSec) { 8712582d50cSToby Isaac PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth; 872fb4630b5SToby Isaac PetscInt *childIDsShifted; 873ca04dac2SToby Isaac PetscSection pSecShifted; 874ca04dac2SToby Isaac 8759566063dSJacob Faibussowitsch PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd)); 8769566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 8772582d50cSToby Isaac pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift); 8782582d50cSToby Isaac pEndShifted = DMPlexShiftPoint_Internal(pEnd, depth, depthShift); 8799566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted)); 8809566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted)); 8819566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted)); 882ca04dac2SToby Isaac for (p = pStartShifted; p < pEndShifted; p++) { 883fb4630b5SToby Isaac /* start off assuming no children */ 8849566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(pSecShifted, p, 0)); 885fb4630b5SToby Isaac } 886fb4630b5SToby Isaac for (p = pStart; p < pEnd; p++) { 887fb4630b5SToby Isaac PetscInt dof; 888fb4630b5SToby Isaac PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift); 889ca04dac2SToby Isaac 8909566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pSec, p, &dof)); 8919566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof)); 892ca04dac2SToby Isaac } 8939566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(pSecShifted)); 894fb4630b5SToby Isaac for (p = pStart; p < pEnd; p++) { 895fb4630b5SToby Isaac PetscInt dof; 896fb4630b5SToby Isaac PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift); 897fb4630b5SToby Isaac 8989566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(pSec, p, &dof)); 899fb4630b5SToby Isaac if (dof) { 900fb4630b5SToby Isaac PetscInt off, offNew; 901fb4630b5SToby Isaac 9029566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pSec, p, &off)); 9039566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew)); 904fb4630b5SToby Isaac parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off], depth, depthShift); 905fb4630b5SToby Isaac childIDsShifted[offNew] = childIDs[off]; 906fb4630b5SToby Isaac } 907fb4630b5SToby Isaac } 9089566063dSJacob Faibussowitsch PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted)); 9099566063dSJacob Faibussowitsch PetscCall(PetscFree2(parentsShifted, childIDsShifted)); 9109566063dSJacob Faibussowitsch PetscCall(PetscSectionDestroy(&pSecShifted)); 911ca04dac2SToby Isaac } 9123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 913ca04dac2SToby Isaac } 914ca04dac2SToby Isaac 915d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm) 916d71ae5a4SJacob Faibussowitsch { 917da97024aSMatthew G. Knepley PetscSF sf; 918cd0c2139SMatthew G Knepley IS valueIS; 919da97024aSMatthew G. Knepley const PetscInt *values, *leaves; 920cd0c2139SMatthew G Knepley PetscInt *depthShift; 9212582d50cSToby Isaac PetscInt d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c; 9224fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 923cd0c2139SMatthew G Knepley 924cd0c2139SMatthew G Knepley PetscFunctionBegin; 9259566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 9269566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL)); 927da97024aSMatthew G. Knepley nleaves = PetscMax(0, nleaves); 9289566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 929cd0c2139SMatthew G Knepley /* Count ghost cells */ 9309566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 9319566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &numFS)); 9329566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 9334a6cfa73SMatthew G. Knepley Ng = 0; 934cd0c2139SMatthew G Knepley for (fs = 0; fs < numFS; ++fs) { 93546c796b9SMatthew G. Knepley IS faceIS; 93646c796b9SMatthew G. Knepley const PetscInt *faces; 93746c796b9SMatthew G. Knepley PetscInt numFaces, f, numBdFaces = 0; 938cd0c2139SMatthew G Knepley 9399566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS)); 9409566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(faceIS, &numFaces)); 9419566063dSJacob Faibussowitsch PetscCall(ISGetIndices(faceIS, &faces)); 94246c796b9SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 943ca04dac2SToby Isaac PetscInt numChildren; 944ca04dac2SToby Isaac 9459566063dSJacob Faibussowitsch PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc)); 9469566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL)); 947ca04dac2SToby Isaac /* non-local and ancestors points don't get to register ghosts */ 948ca04dac2SToby Isaac if (loc >= 0 || numChildren) continue; 94946c796b9SMatthew G. Knepley if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces; 95046c796b9SMatthew G. Knepley } 9514a6cfa73SMatthew G. Knepley Ng += numBdFaces; 9529566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(faceIS, &faces)); 9539566063dSJacob Faibussowitsch PetscCall(ISDestroy(&faceIS)); 954cd0c2139SMatthew G Knepley } 9559566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 9569566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift)); 9572582d50cSToby Isaac for (d = 0; d <= depth; d++) { 95859eef20bSToby Isaac PetscInt dEnd; 9592582d50cSToby Isaac 9609566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd)); 96159eef20bSToby Isaac depthShift[2 * d] = dEnd; 9622582d50cSToby Isaac depthShift[2 * d + 1] = 0; 9632582d50cSToby Isaac } 9642582d50cSToby Isaac if (depth >= 0) depthShift[2 * depth + 1] = Ng; 9659566063dSJacob Faibussowitsch PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift)); 9669566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm)); 967cd0c2139SMatthew G Knepley /* Step 3: Set cone/support sizes for new points */ 9689566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd)); 96948a46eb9SPierre Jolivet for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1)); 970cd0c2139SMatthew G Knepley for (fs = 0; fs < numFS; ++fs) { 971cd0c2139SMatthew G Knepley IS faceIS; 972cd0c2139SMatthew G Knepley const PetscInt *faces; 973cd0c2139SMatthew G Knepley PetscInt numFaces, f; 974cd0c2139SMatthew G Knepley 9759566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS)); 9769566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(faceIS, &numFaces)); 9779566063dSJacob Faibussowitsch PetscCall(ISGetIndices(faceIS, &faces)); 978cd0c2139SMatthew G Knepley for (f = 0; f < numFaces; ++f) { 979ca04dac2SToby Isaac PetscInt size, numChildren; 980cd0c2139SMatthew G Knepley 9819566063dSJacob Faibussowitsch PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc)); 9829566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL)); 983ca04dac2SToby Isaac if (loc >= 0 || numChildren) continue; 98446c796b9SMatthew G. Knepley if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue; 9859566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, faces[f], &size)); 98663a3b9bcSJacob Faibussowitsch PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size); 9879566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2)); 988cd0c2139SMatthew G Knepley } 9899566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(faceIS, &faces)); 9909566063dSJacob Faibussowitsch PetscCall(ISDestroy(&faceIS)); 991cd0c2139SMatthew G Knepley } 992cd0c2139SMatthew G Knepley /* Step 4: Setup ghosted DM */ 9939566063dSJacob Faibussowitsch PetscCall(DMSetUp(gdm)); 9949566063dSJacob Faibussowitsch PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm)); 995cd0c2139SMatthew G Knepley /* Step 6: Set cones and supports for new points */ 996cd0c2139SMatthew G Knepley ghostCell = cEnd; 997cd0c2139SMatthew G Knepley for (fs = 0; fs < numFS; ++fs) { 998cd0c2139SMatthew G Knepley IS faceIS; 999cd0c2139SMatthew G Knepley const PetscInt *faces; 1000cd0c2139SMatthew G Knepley PetscInt numFaces, f; 1001cd0c2139SMatthew G Knepley 10029566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS)); 10039566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(faceIS, &numFaces)); 10049566063dSJacob Faibussowitsch PetscCall(ISGetIndices(faceIS, &faces)); 100546c796b9SMatthew G. Knepley for (f = 0; f < numFaces; ++f) { 1006ca04dac2SToby Isaac PetscInt newFace = faces[f] + Ng, numChildren; 1007cd0c2139SMatthew G Knepley 10089566063dSJacob Faibussowitsch PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc)); 10099566063dSJacob Faibussowitsch PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL)); 1010ca04dac2SToby Isaac if (loc >= 0 || numChildren) continue; 101146c796b9SMatthew G. Knepley if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue; 10129566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace)); 10139566063dSJacob Faibussowitsch PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell)); 101446c796b9SMatthew G. Knepley ++ghostCell; 1015cd0c2139SMatthew G Knepley } 10169566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(faceIS, &faces)); 10179566063dSJacob Faibussowitsch PetscCall(ISDestroy(&faceIS)); 1018cd0c2139SMatthew G Knepley } 10199566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 10209566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 10219566063dSJacob Faibussowitsch PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm)); 10229566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm)); 10239566063dSJacob Faibussowitsch PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm)); 10249566063dSJacob Faibussowitsch PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm)); 10259566063dSJacob Faibussowitsch PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm)); 10269566063dSJacob Faibussowitsch PetscCall(PetscFree(depthShift)); 102748a46eb9SPierre Jolivet for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST)); 1028966c7b3fSMatthew G. Knepley /* Step 7: Periodicity */ 10294fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L)); 10304fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L)); 10314a6cfa73SMatthew G. Knepley if (numGhostCells) *numGhostCells = Ng; 10323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1033cd0c2139SMatthew G Knepley } 1034cd0c2139SMatthew G Knepley 1035cd0c2139SMatthew G Knepley /*@C 1036cd0c2139SMatthew G Knepley DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face 1037cd0c2139SMatthew G Knepley 103820f4b53cSBarry Smith Collective 1039cd0c2139SMatthew G Knepley 1040cd0c2139SMatthew G Knepley Input Parameters: 104120f4b53cSBarry Smith + dm - The original `DM` 104220f4b53cSBarry Smith - labelName - The label specifying the boundary faces, or "Face Sets" if this is `NULL` 1043cd0c2139SMatthew G Knepley 1044cd0c2139SMatthew G Knepley Output Parameters: 104520f4b53cSBarry Smith + numGhostCells - The number of ghost cells added to the `DM` 104620f4b53cSBarry Smith - dmGhosted - The new `DM` 1047cd0c2139SMatthew G Knepley 1048cd0c2139SMatthew G Knepley Level: developer 1049cd0c2139SMatthew G Knepley 105020f4b53cSBarry Smith Note: 105120f4b53cSBarry Smith If no label exists of that name, one will be created marking all boundary faces 105220f4b53cSBarry Smith 10531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()` 105431266bc0SMatthew G. Knepley @*/ 1055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted) 1056d71ae5a4SJacob Faibussowitsch { 1057cd0c2139SMatthew G Knepley DM gdm; 1058cd0c2139SMatthew G Knepley DMLabel label; 1059cd0c2139SMatthew G Knepley const char *name = labelName ? labelName : "Face Sets"; 1060412e9a14SMatthew G. Knepley PetscInt dim, Ng = 0; 1061b0441da4SMatthew G. Knepley PetscBool useCone, useClosure; 1062cd0c2139SMatthew G Knepley 1063cd0c2139SMatthew G Knepley PetscFunctionBegin; 1064cd0c2139SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 10654f572ea9SToby Isaac if (numGhostCells) PetscAssertPointer(numGhostCells, 3); 10664f572ea9SToby Isaac PetscAssertPointer(dmGhosted, 4); 10679566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm)); 10689566063dSJacob Faibussowitsch PetscCall(DMSetType(gdm, DMPLEX)); 10699566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 10709566063dSJacob Faibussowitsch PetscCall(DMSetDimension(gdm, dim)); 10719566063dSJacob Faibussowitsch PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure)); 10729566063dSJacob Faibussowitsch PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure)); 10739566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 1074cd0c2139SMatthew G Knepley if (!label) { 1075cd0c2139SMatthew G Knepley /* Get label for boundary faces */ 10769566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, name)); 10779566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 10789566063dSJacob Faibussowitsch PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label)); 1079cd0c2139SMatthew G Knepley } 10809566063dSJacob Faibussowitsch PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm)); 10819566063dSJacob Faibussowitsch PetscCall(DMCopyDisc(dm, gdm)); 10825de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm)); 1083cad26855SMatthew G. Knepley gdm->setfromoptionscalled = dm->setfromoptionscalled; 1084d80ece95SMatthew G. Knepley if (numGhostCells) *numGhostCells = Ng; 1085cd0c2139SMatthew G Knepley *dmGhosted = gdm; 10863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1087cd0c2139SMatthew G Knepley } 1088cd0c2139SMatthew G Knepley 1089d71ae5a4SJacob Faibussowitsch static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue) 1090d71ae5a4SJacob Faibussowitsch { 1091accc9626SMatthew G. Knepley PetscInt dim, d, shift = 100, *pStart, *pEnd; 1092accc9626SMatthew G. Knepley 1093accc9626SMatthew G. Knepley PetscFunctionBegin; 1094accc9626SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 1095accc9626SMatthew G. Knepley PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd)); 109648a46eb9SPierre Jolivet for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d])); 1097accc9626SMatthew G. Knepley while (!DMPlexPointQueueEmpty(queue)) { 1098accc9626SMatthew G. Knepley PetscInt cell = -1; 1099accc9626SMatthew G. Knepley PetscInt *closure = NULL; 1100accc9626SMatthew G. Knepley PetscInt closureSize, cl, cval; 1101accc9626SMatthew G. Knepley 1102accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueDequeue(queue, &cell)); 1103accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, cell, &cval)); 1104accc9626SMatthew G. Knepley PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 1105accc9626SMatthew G. Knepley /* Mark points in the cell closure that touch the fault */ 1106accc9626SMatthew G. Knepley for (d = 0; d < dim; ++d) { 1107accc9626SMatthew G. Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 1108accc9626SMatthew G. Knepley const PetscInt clp = closure[cl]; 1109accc9626SMatthew G. Knepley PetscInt clval; 1110accc9626SMatthew G. Knepley 1111accc9626SMatthew G. Knepley if ((clp < pStart[d]) || (clp >= pEnd[d])) continue; 1112accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, clp, &clval)); 1113accc9626SMatthew G. Knepley if (clval == -1) { 1114accc9626SMatthew G. Knepley const PetscInt *cone; 1115accc9626SMatthew G. Knepley PetscInt coneSize, c; 1116accc9626SMatthew G. Knepley 1117accc9626SMatthew G. Knepley /* If a cone point touches the fault, then this point touches the fault */ 1118accc9626SMatthew G. Knepley PetscCall(DMPlexGetCone(dm, clp, &cone)); 1119accc9626SMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, clp, &coneSize)); 1120accc9626SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 1121accc9626SMatthew G. Knepley PetscInt cpval; 1122accc9626SMatthew G. Knepley 1123accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, cone[c], &cpval)); 1124accc9626SMatthew G. Knepley if (cpval != -1) { 1125accc9626SMatthew G. Knepley PetscInt dep; 1126accc9626SMatthew G. Knepley 1127accc9626SMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, clp, &dep)); 1128accc9626SMatthew G. Knepley clval = cval < 0 ? -(shift + dep) : shift + dep; 1129accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, clp, clval)); 1130accc9626SMatthew G. Knepley break; 1131accc9626SMatthew G. Knepley } 1132accc9626SMatthew G. Knepley } 1133accc9626SMatthew G. Knepley } 1134accc9626SMatthew G. Knepley /* Mark neighbor cells through marked faces (these cells must also touch the fault) */ 1135accc9626SMatthew G. Knepley if (d == dim - 1 && clval != -1) { 1136accc9626SMatthew G. Knepley const PetscInt *support; 1137accc9626SMatthew G. Knepley PetscInt supportSize, s, nval; 1138accc9626SMatthew G. Knepley 1139accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupport(dm, clp, &support)); 1140accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize)); 1141accc9626SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 1142accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, support[s], &nval)); 1143accc9626SMatthew G. Knepley if (nval == -1) { 1144accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1)); 1145accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueEnqueue(queue, support[s])); 1146accc9626SMatthew G. Knepley } 1147accc9626SMatthew G. Knepley } 1148accc9626SMatthew G. Knepley } 1149accc9626SMatthew G. Knepley } 1150accc9626SMatthew G. Knepley } 1151accc9626SMatthew G. Knepley PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 1152accc9626SMatthew G. Knepley } 1153accc9626SMatthew G. Knepley PetscCall(PetscFree2(pStart, pEnd)); 11543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1155accc9626SMatthew G. Knepley } 1156accc9626SMatthew G. Knepley 1157accc9626SMatthew G. Knepley typedef struct { 1158accc9626SMatthew G. Knepley DM dm; 1159accc9626SMatthew G. Knepley DMPlexPointQueue queue; 1160accc9626SMatthew G. Knepley } PointDivision; 1161accc9626SMatthew G. Knepley 1162d71ae5a4SJacob Faibussowitsch static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx) 1163d71ae5a4SJacob Faibussowitsch { 1164accc9626SMatthew G. Knepley PointDivision *div = (PointDivision *)ctx; 1165accc9626SMatthew G. Knepley PetscInt cval = val < 0 ? val - 1 : val + 1; 1166accc9626SMatthew G. Knepley const PetscInt *support; 1167accc9626SMatthew G. Knepley PetscInt supportSize, s; 1168accc9626SMatthew G. Knepley 1169accc9626SMatthew G. Knepley PetscFunctionBegin; 1170accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupport(div->dm, p, &support)); 1171accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize)); 1172accc9626SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 1173accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, support[s], cval)); 1174accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s])); 1175accc9626SMatthew G. Knepley } 11763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1177accc9626SMatthew G. Knepley } 1178accc9626SMatthew G. Knepley 1179accc9626SMatthew G. Knepley /* Mark cells by label propagation */ 1180d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel) 1181d71ae5a4SJacob Faibussowitsch { 1182accc9626SMatthew G. Knepley DMPlexPointQueue queue = NULL; 1183accc9626SMatthew G. Knepley PointDivision div; 1184accc9626SMatthew G. Knepley PetscSF pointSF; 1185accc9626SMatthew G. Knepley IS pointIS; 1186accc9626SMatthew G. Knepley const PetscInt *points; 1187accc9626SMatthew G. Knepley PetscBool empty; 1188accc9626SMatthew G. Knepley PetscInt dim, shift = 100, n, i; 1189accc9626SMatthew G. Knepley 1190accc9626SMatthew G. Knepley PetscFunctionBegin; 1191accc9626SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 1192accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueCreate(1024, &queue)); 1193accc9626SMatthew G. Knepley div.dm = dm; 1194accc9626SMatthew G. Knepley div.queue = queue; 1195accc9626SMatthew G. Knepley /* Enqueue cells on fault */ 1196accc9626SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS)); 1197accc9626SMatthew G. Knepley if (pointIS) { 1198accc9626SMatthew G. Knepley PetscCall(ISGetLocalSize(pointIS, &n)); 1199accc9626SMatthew G. Knepley PetscCall(ISGetIndices(pointIS, &points)); 120048a46eb9SPierre Jolivet for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i])); 1201accc9626SMatthew G. Knepley PetscCall(ISRestoreIndices(pointIS, &points)); 1202accc9626SMatthew G. Knepley PetscCall(ISDestroy(&pointIS)); 1203accc9626SMatthew G. Knepley } 1204accc9626SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS)); 1205accc9626SMatthew G. Knepley if (pointIS) { 1206accc9626SMatthew G. Knepley PetscCall(ISGetLocalSize(pointIS, &n)); 1207accc9626SMatthew G. Knepley PetscCall(ISGetIndices(pointIS, &points)); 120848a46eb9SPierre Jolivet for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i])); 1209accc9626SMatthew G. Knepley PetscCall(ISRestoreIndices(pointIS, &points)); 1210accc9626SMatthew G. Knepley PetscCall(ISDestroy(&pointIS)); 1211accc9626SMatthew G. Knepley } 1212accc9626SMatthew G. Knepley 1213accc9626SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &pointSF)); 1214accc9626SMatthew G. Knepley PetscCall(DMLabelPropagateBegin(faultLabel, pointSF)); 1215accc9626SMatthew G. Knepley /* While edge queue is not empty: */ 1216accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty)); 1217accc9626SMatthew G. Knepley while (!empty) { 1218accc9626SMatthew G. Knepley PetscCall(DivideCells_Private(dm, faultLabel, queue)); 1219accc9626SMatthew G. Knepley PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div)); 1220accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty)); 1221accc9626SMatthew G. Knepley } 1222accc9626SMatthew G. Knepley PetscCall(DMLabelPropagateEnd(faultLabel, pointSF)); 1223accc9626SMatthew G. Knepley PetscCall(DMPlexPointQueueDestroy(&queue)); 12243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1225accc9626SMatthew G. Knepley } 1226accc9626SMatthew G. Knepley 1227607ab7a9SMatthew G. Knepley /* 1228faedd622SMatthew G. Knepley We are adding three kinds of points here: 1229607ab7a9SMatthew G. Knepley Replicated: Copies of points which exist in the mesh, such as vertices identified across a fault 1230faedd622SMatthew G. Knepley Non-replicated: Points which exist on the fault, but are not replicated 1231b6dfa339SMatthew G. Knepley Ghost: These are shared fault faces which are not owned by this process. These do not produce hybrid cells and do not replicate 1232607ab7a9SMatthew G. Knepley Hybrid: Entirely new points, such as cohesive cells 1233a6ae58d1SMatthew G. Knepley 1234a6ae58d1SMatthew G. Knepley When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at 1235a6ae58d1SMatthew G. Knepley each depth so that the new split/hybrid points can be inserted as a block. 1236607ab7a9SMatthew G. Knepley */ 1237d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm) 1238d71ae5a4SJacob Faibussowitsch { 1239cd0c2139SMatthew G Knepley MPI_Comm comm; 1240607ab7a9SMatthew G. Knepley IS valueIS; 1241607ab7a9SMatthew G. Knepley PetscInt numSP = 0; /* The number of depths for which we have replicated points */ 1242607ab7a9SMatthew G. Knepley const PetscInt *values; /* List of depths for which we have replicated points */ 124318c5995bSMatthew G. Knepley IS *splitIS; 124418c5995bSMatthew G. Knepley IS *unsplitIS; 1245b6dfa339SMatthew G. Knepley IS ghostIS; 1246607ab7a9SMatthew G. Knepley PetscInt *numSplitPoints; /* The number of replicated points at each depth */ 124718c5995bSMatthew G. Knepley PetscInt *numUnsplitPoints; /* The number of non-replicated points at each depth which still give rise to hybrid points */ 124836dbac82SMatthew G. Knepley PetscInt *numHybridPoints; /* The number of new hybrid points at each depth */ 124936dbac82SMatthew G. Knepley PetscInt *numHybridPointsOld; /* The number of existing hybrid points at each depth */ 1250b6dfa339SMatthew G. Knepley PetscInt numGhostPoints; /* The number of unowned, shared fault faces */ 1251607ab7a9SMatthew G. Knepley const PetscInt **splitPoints; /* Replicated points for each depth */ 125218c5995bSMatthew G. Knepley const PetscInt **unsplitPoints; /* Non-replicated points for each depth */ 1253b6dfa339SMatthew G. Knepley const PetscInt *ghostPoints; /* Ghost fault faces */ 1254cd0c2139SMatthew G Knepley PetscSection coordSection; 1255cd0c2139SMatthew G Knepley Vec coordinates; 1256cd0c2139SMatthew G Knepley PetscScalar *coords; 1257a6ae58d1SMatthew G. Knepley PetscInt *depthMax; /* The first hybrid point at each depth in the original mesh */ 1258a6ae58d1SMatthew G. Knepley PetscInt *depthEnd; /* The point limit at each depth in the original mesh */ 1259607ab7a9SMatthew G. Knepley PetscInt *depthShift; /* Number of replicated+hybrid points at each depth */ 1260607ab7a9SMatthew G. Knepley PetscInt *pMaxNew; /* The first replicated point at each depth in the new mesh, hybrids come after this */ 1261607ab7a9SMatthew G. Knepley PetscInt *coneNew, *coneONew, *supportNew; 126218c5995bSMatthew G. Knepley PetscInt shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v; 1263cd0c2139SMatthew G Knepley 1264cd0c2139SMatthew G Knepley PetscFunctionBegin; 12659566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 12669566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 12679566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 12689566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 1269412e9a14SMatthew G. Knepley /* We do not want this label automatically computed, instead we compute it here */ 12709566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(sdm, "celltype")); 1271cd0c2139SMatthew G Knepley /* Count split points and add cohesive cells */ 12729566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize)); 12739566063dSJacob Faibussowitsch PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld)); 12749566063dSJacob Faibussowitsch PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints)); 1275607ab7a9SMatthew G. Knepley for (d = 0; d <= depth; ++d) { 12769566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d])); 12779566063dSJacob Faibussowitsch PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL)); 1278a6ae58d1SMatthew G. Knepley depthEnd[d] = pMaxNew[d]; 1279a6ae58d1SMatthew G. Knepley depthMax[d] = depthMax[d] < 0 ? depthEnd[d] : depthMax[d]; 1280607ab7a9SMatthew G. Knepley numSplitPoints[d] = 0; 128118c5995bSMatthew G. Knepley numUnsplitPoints[d] = 0; 1282607ab7a9SMatthew G. Knepley numHybridPoints[d] = 0; 1283a6ae58d1SMatthew G. Knepley numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d]; 1284607ab7a9SMatthew G. Knepley splitPoints[d] = NULL; 128518c5995bSMatthew G. Knepley unsplitPoints[d] = NULL; 128618c5995bSMatthew G. Knepley splitIS[d] = NULL; 128718c5995bSMatthew G. Knepley unsplitIS[d] = NULL; 128859eef20bSToby Isaac /* we are shifting the existing hybrid points with the stratum behind them, so 128959eef20bSToby Isaac * the split comes at the end of the normal points, i.e., at depthMax[d] */ 129059eef20bSToby Isaac depthShift[2 * d] = depthMax[d]; 129159eef20bSToby Isaac depthShift[2 * d + 1] = 0; 1292607ab7a9SMatthew G. Knepley } 1293b6dfa339SMatthew G. Knepley numGhostPoints = 0; 1294b6dfa339SMatthew G. Knepley ghostPoints = NULL; 1295cd0c2139SMatthew G Knepley if (label) { 12969566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 12979566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &numSP)); 12989566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 1299cd0c2139SMatthew G Knepley } 1300cd0c2139SMatthew G Knepley for (sp = 0; sp < numSP; ++sp) { 1301cd0c2139SMatthew G Knepley const PetscInt dep = values[sp]; 1302cd0c2139SMatthew G Knepley 1303cd0c2139SMatthew G Knepley if ((dep < 0) || (dep > depth)) continue; 13049566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep])); 130518c5995bSMatthew G. Knepley if (splitIS[dep]) { 13069566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep])); 13079566063dSJacob Faibussowitsch PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep])); 130818c5995bSMatthew G. Knepley } 13099566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep])); 131018c5995bSMatthew G. Knepley if (unsplitIS[dep]) { 13119566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep])); 13129566063dSJacob Faibussowitsch PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep])); 1313cd0c2139SMatthew G Knepley } 1314cd0c2139SMatthew G Knepley } 1315b6dfa339SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS)); 1316b6dfa339SMatthew G. Knepley if (ghostIS) { 1317b6dfa339SMatthew G. Knepley PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints)); 1318b6dfa339SMatthew G. Knepley PetscCall(ISGetIndices(ghostIS, &ghostPoints)); 1319b6dfa339SMatthew G. Knepley } 1320607ab7a9SMatthew G. Knepley /* Calculate number of hybrid points */ 132118c5995bSMatthew G. Knepley for (d = 1; d <= depth; ++d) numHybridPoints[d] = numSplitPoints[d - 1] + numUnsplitPoints[d - 1]; /* There is a hybrid cell/face/edge for every split face/edge/vertex */ 13222582d50cSToby Isaac for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d]; 13239566063dSJacob Faibussowitsch PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift)); 132459eef20bSToby Isaac /* the end of the points in this stratum that come before the new points: 132559eef20bSToby Isaac * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly 132659eef20bSToby Isaac * added points */ 13272582d50cSToby Isaac for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]); 13289566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm)); 1329cd0c2139SMatthew G Knepley /* Step 3: Set cone/support sizes for new points */ 1330cd0c2139SMatthew G Knepley for (dep = 0; dep <= depth; ++dep) { 1331cd0c2139SMatthew G Knepley for (p = 0; p < numSplitPoints[dep]; ++p) { 1332cd0c2139SMatthew G Knepley const PetscInt oldp = splitPoints[dep][p]; 13332582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/; 13344c367dbcSMatthew G. Knepley const PetscInt splitp = p + pMaxNew[dep]; 1335cd0c2139SMatthew G Knepley const PetscInt *support; 1336394c2f0fSMatthew G. Knepley DMPolytopeType ct; 13374c367dbcSMatthew G. Knepley PetscInt coneSize, supportSize, qf, qn, qp, e; 1338cd0c2139SMatthew G Knepley 13399566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize)); 13409566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize)); 13419566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize)); 13429566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize)); 13439566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, oldp, &ct)); 13449566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, splitp, ct)); 1345cd0c2139SMatthew G Knepley if (dep == depth - 1) { 13464c367dbcSMatthew G. Knepley const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 13474c367dbcSMatthew G. Knepley 1348cd0c2139SMatthew G Knepley /* Add cohesive cells, they are prisms */ 13499566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize)); 1350412e9a14SMatthew G. Knepley switch (coneSize) { 1351d71ae5a4SJacob Faibussowitsch case 2: 1352d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR)); 1353d71ae5a4SJacob Faibussowitsch break; 1354d71ae5a4SJacob Faibussowitsch case 3: 1355d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR)); 1356d71ae5a4SJacob Faibussowitsch break; 1357d71ae5a4SJacob Faibussowitsch case 4: 1358d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR)); 1359d71ae5a4SJacob Faibussowitsch break; 1360412e9a14SMatthew G. Knepley } 1361b6dfa339SMatthew G. Knepley /* Shared fault faces with only one support cell now have two with the cohesive cell */ 1362b6dfa339SMatthew G. Knepley /* TODO Check thaat oldp has rootdegree == 1 */ 1363b6dfa339SMatthew G. Knepley if (supportSize == 1) { 1364b6dfa339SMatthew G. Knepley const PetscInt *support; 1365b6dfa339SMatthew G. Knepley PetscInt val; 1366b6dfa339SMatthew G. Knepley 1367b6dfa339SMatthew G. Knepley PetscCall(DMPlexGetSupport(dm, oldp, &support)); 1368b6dfa339SMatthew G. Knepley PetscCall(DMLabelGetValue(label, support[0], &val)); 1369b6dfa339SMatthew G. Knepley if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2)); 1370b6dfa339SMatthew G. Knepley else PetscCall(DMPlexSetSupportSize(sdm, newp, 2)); 1371b6dfa339SMatthew G. Knepley } 1372cd0c2139SMatthew G Knepley } else if (dep == 0) { 13734c367dbcSMatthew G. Knepley const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 1374cd0c2139SMatthew G Knepley 13759566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, oldp, &support)); 13764c367dbcSMatthew G. Knepley for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) { 1377cd0c2139SMatthew G Knepley PetscInt val; 1378cd0c2139SMatthew G Knepley 13799566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 13804c367dbcSMatthew G. Knepley if (val == 1) ++qf; 13814c367dbcSMatthew G. Knepley if ((val == 1) || (val == (shift + 1))) ++qn; 13824c367dbcSMatthew G. Knepley if ((val == 1) || (val == -(shift + 1))) ++qp; 1383cd0c2139SMatthew G Knepley } 13844c367dbcSMatthew G. Knepley /* Split old vertex: Edges into original vertex and new cohesive edge */ 13859566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1)); 13864c367dbcSMatthew G. Knepley /* Split new vertex: Edges into split vertex and new cohesive edge */ 13879566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1)); 13884c367dbcSMatthew G. Knepley /* Add hybrid edge */ 13899566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, hybedge, 2)); 13909566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf)); 13919566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR)); 1392cd0c2139SMatthew G Knepley } else if (dep == dim - 2) { 13934c367dbcSMatthew G. Knepley const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 13944c367dbcSMatthew G. Knepley 13959566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, oldp, &support)); 13964c367dbcSMatthew G. Knepley for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) { 1397cd0c2139SMatthew G Knepley PetscInt val; 1398cd0c2139SMatthew G Knepley 13999566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 14004c367dbcSMatthew G. Knepley if (val == dim - 1) ++qf; 14014c367dbcSMatthew G. Knepley if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn; 14024c367dbcSMatthew G. Knepley if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp; 1403cd0c2139SMatthew G Knepley } 14044c367dbcSMatthew G. Knepley /* Split old edge: Faces into original edge and cohesive face (positive side?) */ 14059566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1)); 14064c367dbcSMatthew G. Knepley /* Split new edge: Faces into split edge and cohesive face (negative side?) */ 14079566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1)); 14084c367dbcSMatthew G. Knepley /* Add hybrid face */ 14099566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, hybface, 4)); 14109566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, hybface, qf)); 14119566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR)); 1412cd0c2139SMatthew G Knepley } 1413cd0c2139SMatthew G Knepley } 1414cd0c2139SMatthew G Knepley } 141518c5995bSMatthew G. Knepley for (dep = 0; dep <= depth; ++dep) { 141618c5995bSMatthew G. Knepley for (p = 0; p < numUnsplitPoints[dep]; ++p) { 141718c5995bSMatthew G. Knepley const PetscInt oldp = unsplitPoints[dep][p]; 14182582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/; 141918c5995bSMatthew G. Knepley const PetscInt *support; 1420da1dd7e4SMatthew G. Knepley PetscInt coneSize, supportSize, qf, e, s; 142118c5995bSMatthew G. Knepley 14229566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize)); 14239566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize)); 14249566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, oldp, &support)); 142518c5995bSMatthew G. Knepley if (dep == 0) { 142618c5995bSMatthew G. Knepley const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep]; 142718c5995bSMatthew G. Knepley 142839254ff6SMatthew G. Knepley /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */ 142939254ff6SMatthew G. Knepley for (s = 0, qf = 0; s < supportSize; ++s, ++qf) { 14309566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e)); 143139254ff6SMatthew G. Knepley if (e >= 0) ++qf; 143239254ff6SMatthew G. Knepley } 14339566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2)); 143418c5995bSMatthew G. Knepley /* Add hybrid edge */ 14359566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, hybedge, 2)); 1436e1757548SMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 1437e1757548SMatthew G. Knepley PetscInt val; 1438e1757548SMatthew G. Knepley 14399566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1440e1757548SMatthew G. Knepley /* Split and unsplit edges produce hybrid faces */ 1441da1dd7e4SMatthew G. Knepley if (val == 1) ++qf; 1442da1dd7e4SMatthew G. Knepley if (val == (shift2 + 1)) ++qf; 1443e1757548SMatthew G. Knepley } 14449566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf)); 14459566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR)); 144618c5995bSMatthew G. Knepley } else if (dep == dim - 2) { 144718c5995bSMatthew G. Knepley const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep]; 1448cd0c2139SMatthew G Knepley PetscInt val; 1449cd0c2139SMatthew G Knepley 1450da1dd7e4SMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 14519566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1452da1dd7e4SMatthew G. Knepley if (val == dim - 1) qf += 2; 1453da1dd7e4SMatthew G. Knepley else ++qf; 1454cd0c2139SMatthew G Knepley } 145518c5995bSMatthew G. Knepley /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */ 14569566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2)); 145718c5995bSMatthew G. Knepley /* Add hybrid face */ 1458da1dd7e4SMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 14599566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1460da1dd7e4SMatthew G. Knepley if (val == dim - 1) ++qf; 1461da1dd7e4SMatthew G. Knepley } 14629566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(sdm, hybface, 4)); 14639566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupportSize(sdm, hybface, qf)); 14649566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR)); 1465cd0c2139SMatthew G Knepley } 1466cd0c2139SMatthew G Knepley } 1467cd0c2139SMatthew G Knepley } 1468cd0c2139SMatthew G Knepley /* Step 4: Setup split DM */ 14699566063dSJacob Faibussowitsch PetscCall(DMSetUp(sdm)); 14709566063dSJacob Faibussowitsch PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm)); 14719566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew)); 14729566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew)); 1473cd0c2139SMatthew G Knepley /* Step 6: Set cones and supports for new points */ 1474cd0c2139SMatthew G Knepley for (dep = 0; dep <= depth; ++dep) { 1475cd0c2139SMatthew G Knepley for (p = 0; p < numSplitPoints[dep]; ++p) { 1476cd0c2139SMatthew G Knepley const PetscInt oldp = splitPoints[dep][p]; 14772582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/; 14784c367dbcSMatthew G. Knepley const PetscInt splitp = p + pMaxNew[dep]; 1479cd0c2139SMatthew G Knepley const PetscInt *cone, *support, *ornt; 1480b5a892a1SMatthew G. Knepley DMPolytopeType ct; 14814c367dbcSMatthew G. Knepley PetscInt coneSize, supportSize, q, qf, qn, qp, v, e, s; 1482cd0c2139SMatthew G Knepley 14839566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellType(dm, oldp, &ct)); 14849566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize)); 14859566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, oldp, &cone)); 14869566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt)); 14879566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize)); 14889566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, oldp, &support)); 1489cd0c2139SMatthew G Knepley if (dep == depth - 1) { 149096a07cd0SMatthew G. Knepley PetscBool hasUnsplit = PETSC_FALSE; 14914c367dbcSMatthew G. Knepley const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 1492cd0c2139SMatthew G Knepley const PetscInt *supportF; 1493cd0c2139SMatthew G Knepley 1494b6dfa339SMatthew G. Knepley coneONew[0] = coneONew[1] = -1000; 1495cd0c2139SMatthew G Knepley /* Split face: copy in old face to new face to start */ 14969566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(sdm, newp, &supportF)); 14979566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, splitp, supportF)); 1498cd0c2139SMatthew G Knepley /* Split old face: old vertices/edges in cone so no change */ 1499cd0c2139SMatthew G Knepley /* Split new face: new vertices/edges in cone */ 1500cd0c2139SMatthew G Knepley for (q = 0; q < coneSize; ++q) { 15019566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v)); 150218c5995bSMatthew G. Knepley if (v < 0) { 15039566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v)); 150463a3b9bcSJacob Faibussowitsch PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1); 15052582d50cSToby Isaac coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/; 150696a07cd0SMatthew G. Knepley hasUnsplit = PETSC_TRUE; 150718c5995bSMatthew G. Knepley } else { 15084c367dbcSMatthew G. Knepley coneNew[2 + q] = v + pMaxNew[dep - 1]; 1509163235baSMatthew G. Knepley if (dep > 1) { 1510163235baSMatthew G. Knepley const PetscInt *econe; 1511163235baSMatthew G. Knepley PetscInt econeSize, r, vs, vu; 1512163235baSMatthew G. Knepley 15139566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize)); 15149566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, cone[q], &econe)); 1515163235baSMatthew G. Knepley for (r = 0; r < econeSize; ++r) { 15169566063dSJacob Faibussowitsch PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs)); 15179566063dSJacob Faibussowitsch PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu)); 1518163235baSMatthew G. Knepley if (vs >= 0) continue; 151963a3b9bcSJacob Faibussowitsch PetscCheck(vu >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep - 2); 1520163235baSMatthew G. Knepley hasUnsplit = PETSC_TRUE; 1521163235baSMatthew G. Knepley } 1522163235baSMatthew G. Knepley } 1523cd0c2139SMatthew G Knepley } 1524cd0c2139SMatthew G Knepley } 15259566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2])); 15269566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt)); 1527e537020bSMatthew G. Knepley /* Face support */ 1528b6dfa339SMatthew G. Knepley PetscInt vals[2]; 1529cd0c2139SMatthew G Knepley 1530b6dfa339SMatthew G. Knepley PetscCall(DMLabelGetValue(label, support[0], &vals[0])); 1531b6dfa339SMatthew G. Knepley if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1])); 1532b6dfa339SMatthew G. Knepley else vals[1] = -vals[0]; 1533b6dfa339SMatthew G. Knepley PetscCheck(vals[0] * vals[1] < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid support labels %" PetscInt_FMT " %" PetscInt_FMT, vals[0], vals[1]); 1534b6dfa339SMatthew G. Knepley 1535b6dfa339SMatthew G. Knepley for (s = 0; s < 2; ++s) { 1536b6dfa339SMatthew G. Knepley if (s >= supportSize) { 1537b6dfa339SMatthew G. Knepley if (vals[s] < 0) { 1538b6dfa339SMatthew G. Knepley /* Ghost old face: Replace negative side cell with cohesive cell */ 1539accc9626SMatthew G. Knepley PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell)); 1540b6dfa339SMatthew G. Knepley } else { 1541b6dfa339SMatthew G. Knepley /* Ghost new face: Replace positive side cell with cohesive cell */ 1542accc9626SMatthew G. Knepley PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell)); 1543b6dfa339SMatthew G. Knepley } 1544b6dfa339SMatthew G. Knepley } else { 1545b6dfa339SMatthew G. Knepley if (vals[s] < 0) { 1546cd0c2139SMatthew G Knepley /* Split old face: Replace negative side cell with cohesive cell */ 15479566063dSJacob Faibussowitsch PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell)); 1548cd0c2139SMatthew G Knepley } else { 1549cd0c2139SMatthew G Knepley /* Split new face: Replace positive side cell with cohesive cell */ 15509566063dSJacob Faibussowitsch PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell)); 1551b6dfa339SMatthew G. Knepley } 1552b6dfa339SMatthew G. Knepley } 1553b6dfa339SMatthew G. Knepley } 1554b6dfa339SMatthew G. Knepley /* Get orientation for cohesive face using the positive side cell */ 1555e537020bSMatthew G. Knepley { 1556e537020bSMatthew G. Knepley const PetscInt *ncone, *nconeO; 1557b6dfa339SMatthew G. Knepley PetscInt nconeSize, nc, ocell; 1558accc9626SMatthew G. Knepley PetscBool flip = PETSC_FALSE; 1559e537020bSMatthew G. Knepley 15609371c9d4SSatish Balay if (supportSize > 1) { 15619371c9d4SSatish Balay ocell = vals[0] < 0 ? support[1] : support[0]; 15629371c9d4SSatish Balay } else { 15639371c9d4SSatish Balay ocell = support[0]; 15649371c9d4SSatish Balay flip = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE; 15659371c9d4SSatish Balay } 1566b6dfa339SMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize)); 1567b6dfa339SMatthew G. Knepley PetscCall(DMPlexGetCone(dm, ocell, &ncone)); 1568e1a13daeSMatthew G. Knepley PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO)); 1569e537020bSMatthew G. Knepley for (nc = 0; nc < nconeSize; ++nc) { 1570e537020bSMatthew G. Knepley if (ncone[nc] == oldp) { 1571b6dfa339SMatthew G. Knepley coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc]; 1572e537020bSMatthew G. Knepley break; 1573cd0c2139SMatthew G Knepley } 1574cd0c2139SMatthew G Knepley } 1575b6dfa339SMatthew G. Knepley PetscCheck(nc < nconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %" PetscInt_FMT " in neighboring cell %" PetscInt_FMT, oldp, ocell); 1576e537020bSMatthew G. Knepley } 15774c367dbcSMatthew G. Knepley /* Cohesive cell: Old and new split face, then new cohesive faces */ 1578b6dfa339SMatthew G. Knepley { 157985036b15SMatthew G. Knepley const PetscInt No = DMPolytopeTypeGetNumArrangements(ct) / 2; 1580b6dfa339SMatthew G. Knepley PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]); 1581b6dfa339SMatthew G. Knepley } 158285036b15SMatthew G. Knepley const PetscInt *arr = DMPolytopeTypeGetArrangement(ct, coneONew[0]); 1583b5a892a1SMatthew G. Knepley 1584fd4b9f15SMatthew G. Knepley coneNew[0] = newp; /* Extracted negative side orientation above */ 15854c367dbcSMatthew G. Knepley coneNew[1] = splitp; 15864c367dbcSMatthew G. Knepley coneONew[1] = coneONew[0]; 1587e537020bSMatthew G. Knepley for (q = 0; q < coneSize; ++q) { 1588412e9a14SMatthew G. Knepley /* Hybrid faces must follow order from oriented end face */ 1589b5a892a1SMatthew G. Knepley const PetscInt qa = arr[q * 2 + 0]; 1590b5a892a1SMatthew G. Knepley const PetscInt qo = arr[q * 2 + 1]; 1591b5a892a1SMatthew G. Knepley DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT; 1592412e9a14SMatthew G. Knepley 15939566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v)); 159418c5995bSMatthew G. Knepley if (v < 0) { 15959566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v)); 159618c5995bSMatthew G. Knepley coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1]; 159718c5995bSMatthew G. Knepley } else { 159818c5995bSMatthew G. Knepley coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep]; 159918c5995bSMatthew G. Knepley } 1600b5a892a1SMatthew G. Knepley coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]); 1601e537020bSMatthew G. Knepley } 16029566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, hybcell, coneNew)); 16039566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew)); 160496a07cd0SMatthew G. Knepley /* Label the hybrid cells on the boundary of the split */ 16059566063dSJacob Faibussowitsch if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim)); 1606cd0c2139SMatthew G Knepley } else if (dep == 0) { 16074c367dbcSMatthew G. Knepley const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 1608cd0c2139SMatthew G Knepley 1609cd0c2139SMatthew G Knepley /* Split old vertex: Edges in old split faces and new cohesive edge */ 16104c367dbcSMatthew G. Knepley for (e = 0, qn = 0; e < supportSize; ++e) { 1611cd0c2139SMatthew G Knepley PetscInt val; 1612cd0c2139SMatthew G Knepley 16139566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1614ad540459SPierre Jolivet if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/; 1615cd0c2139SMatthew G Knepley } 16164c367dbcSMatthew G. Knepley supportNew[qn] = hybedge; 16179566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, newp, supportNew)); 1618cd0c2139SMatthew G Knepley /* Split new vertex: Edges in new split faces and new cohesive edge */ 16194c367dbcSMatthew G. Knepley for (e = 0, qp = 0; e < supportSize; ++e) { 1620cd0c2139SMatthew G Knepley PetscInt val, edge; 1621cd0c2139SMatthew G Knepley 16229566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1623cd0c2139SMatthew G Knepley if (val == 1) { 16249566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge)); 162563a3b9bcSJacob Faibussowitsch PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]); 16264c367dbcSMatthew G. Knepley supportNew[qp++] = edge + pMaxNew[dep + 1]; 1627cd0c2139SMatthew G Knepley } else if (val == -(shift + 1)) { 16282582d50cSToby Isaac supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/; 1629cd0c2139SMatthew G Knepley } 1630cd0c2139SMatthew G Knepley } 16314c367dbcSMatthew G. Knepley supportNew[qp] = hybedge; 16329566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, splitp, supportNew)); 16334c367dbcSMatthew G. Knepley /* Hybrid edge: Old and new split vertex */ 1634cd0c2139SMatthew G Knepley coneNew[0] = newp; 1635cd0c2139SMatthew G Knepley coneNew[1] = splitp; 16369566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, hybedge, coneNew)); 16374c367dbcSMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 16384c367dbcSMatthew G. Knepley PetscInt val, edge; 16394c367dbcSMatthew G. Knepley 16409566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 16414c367dbcSMatthew G. Knepley if (val == 1) { 16429566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge)); 164363a3b9bcSJacob Faibussowitsch PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]); 16444c367dbcSMatthew G. Knepley supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2]; 16454c367dbcSMatthew G. Knepley } 16464c367dbcSMatthew G. Knepley } 16479566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew)); 1648cd0c2139SMatthew G Knepley } else if (dep == dim - 2) { 16494c367dbcSMatthew G. Knepley const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 16504c367dbcSMatthew G. Knepley 1651cd0c2139SMatthew G Knepley /* Split old edge: old vertices in cone so no change */ 1652cd0c2139SMatthew G Knepley /* Split new edge: new vertices in cone */ 1653cd0c2139SMatthew G Knepley for (q = 0; q < coneSize; ++q) { 16549566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v)); 1655e1757548SMatthew G. Knepley if (v < 0) { 16569566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v)); 165763a3b9bcSJacob Faibussowitsch PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1); 16582582d50cSToby Isaac coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/; 1659e1757548SMatthew G. Knepley } else { 16604c367dbcSMatthew G. Knepley coneNew[q] = v + pMaxNew[dep - 1]; 1661cd0c2139SMatthew G Knepley } 1662e1757548SMatthew G. Knepley } 16639566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, splitp, coneNew)); 1664cd0c2139SMatthew G Knepley /* Split old edge: Faces in positive side cells and old split faces */ 1665cd0c2139SMatthew G Knepley for (e = 0, q = 0; e < supportSize; ++e) { 1666cd0c2139SMatthew G Knepley PetscInt val; 1667cd0c2139SMatthew G Knepley 16689566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 16694c367dbcSMatthew G. Knepley if (val == dim - 1) { 16702582d50cSToby Isaac supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/; 16714c367dbcSMatthew G. Knepley } else if (val == (shift + dim - 1)) { 16722582d50cSToby Isaac supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/; 1673cd0c2139SMatthew G Knepley } 1674cd0c2139SMatthew G Knepley } 1675b279cd2aSMatthew G. Knepley supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 16769566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, newp, supportNew)); 1677cd0c2139SMatthew G Knepley /* Split new edge: Faces in negative side cells and new split faces */ 1678cd0c2139SMatthew G Knepley for (e = 0, q = 0; e < supportSize; ++e) { 1679cd0c2139SMatthew G Knepley PetscInt val, face; 1680cd0c2139SMatthew G Knepley 16819566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 1682cd0c2139SMatthew G Knepley if (val == dim - 1) { 16839566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face)); 168463a3b9bcSJacob Faibussowitsch PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]); 16854c367dbcSMatthew G. Knepley supportNew[q++] = face + pMaxNew[dep + 1]; 1686cd0c2139SMatthew G Knepley } else if (val == -(shift + dim - 1)) { 16872582d50cSToby Isaac supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/; 1688cd0c2139SMatthew G Knepley } 1689cd0c2139SMatthew G Knepley } 1690b279cd2aSMatthew G. Knepley supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1]; 16919566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, splitp, supportNew)); 16924c367dbcSMatthew G. Knepley /* Hybrid face */ 16934c367dbcSMatthew G. Knepley coneNew[0] = newp; 16944c367dbcSMatthew G. Knepley coneNew[1] = splitp; 16954c367dbcSMatthew G. Knepley for (v = 0; v < coneSize; ++v) { 16964c367dbcSMatthew G. Knepley PetscInt vertex; 16979566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex)); 1698e1757548SMatthew G. Knepley if (vertex < 0) { 16999566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex)); 170063a3b9bcSJacob Faibussowitsch PetscCheck(vertex >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep - 1); 1701e1757548SMatthew G. Knepley coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1]; 1702e1757548SMatthew G. Knepley } else { 17034c367dbcSMatthew G. Knepley coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep]; 17044c367dbcSMatthew G. Knepley } 1705e1757548SMatthew G. Knepley } 17069566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, hybface, coneNew)); 17074c367dbcSMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 17084c367dbcSMatthew G. Knepley PetscInt val, face; 17094c367dbcSMatthew G. Knepley 17109566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 17114c367dbcSMatthew G. Knepley if (val == dim - 1) { 17129566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face)); 171363a3b9bcSJacob Faibussowitsch PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]); 17144c367dbcSMatthew G. Knepley supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2]; 17154c367dbcSMatthew G. Knepley } 17164c367dbcSMatthew G. Knepley } 17179566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, hybface, supportNew)); 1718cd0c2139SMatthew G Knepley } 1719cd0c2139SMatthew G Knepley } 1720cd0c2139SMatthew G Knepley } 172118c5995bSMatthew G. Knepley for (dep = 0; dep <= depth; ++dep) { 172218c5995bSMatthew G. Knepley for (p = 0; p < numUnsplitPoints[dep]; ++p) { 172318c5995bSMatthew G. Knepley const PetscInt oldp = unsplitPoints[dep][p]; 17242582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/; 1725b5a892a1SMatthew G. Knepley const PetscInt *cone, *support; 1726e1757548SMatthew G. Knepley PetscInt coneSize, supportSize, supportSizeNew, q, qf, e, f, s; 172718c5995bSMatthew G. Knepley 17289566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize)); 17299566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, oldp, &cone)); 17309566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize)); 17319566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, oldp, &support)); 173218c5995bSMatthew G. Knepley if (dep == 0) { 173318c5995bSMatthew G. Knepley const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep]; 173418c5995bSMatthew G. Knepley 173518c5995bSMatthew G. Knepley /* Unsplit vertex */ 17369566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew)); 173718c5995bSMatthew G. Knepley for (s = 0, q = 0; s < supportSize; ++s) { 17382582d50cSToby Isaac supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/; 17399566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e)); 1740ad540459SPierre Jolivet if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1]; 174118c5995bSMatthew G. Knepley } 174218c5995bSMatthew G. Knepley supportNew[q++] = hybedge; 174318c5995bSMatthew G. Knepley supportNew[q++] = hybedge; 174463a3b9bcSJacob Faibussowitsch PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp); 17459566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, newp, supportNew)); 174618c5995bSMatthew G. Knepley /* Hybrid edge */ 174718c5995bSMatthew G. Knepley coneNew[0] = newp; 174818c5995bSMatthew G. Knepley coneNew[1] = newp; 17499566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, hybedge, coneNew)); 175018c5995bSMatthew G. Knepley for (e = 0, qf = 0; e < supportSize; ++e) { 175118c5995bSMatthew G. Knepley PetscInt val, edge; 175218c5995bSMatthew G. Knepley 17539566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[e], &val)); 175418c5995bSMatthew G. Knepley if (val == 1) { 17559566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge)); 175663a3b9bcSJacob Faibussowitsch PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]); 175718c5995bSMatthew G. Knepley supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2]; 1758e1757548SMatthew G. Knepley } else if (val == (shift2 + 1)) { 17599566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge)); 176063a3b9bcSJacob Faibussowitsch PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]); 1761e1757548SMatthew G. Knepley supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1]; 176218c5995bSMatthew G. Knepley } 176318c5995bSMatthew G. Knepley } 17649566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew)); 1765e1757548SMatthew G. Knepley } else if (dep == dim - 2) { 1766e1757548SMatthew G. Knepley const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep]; 1767e1757548SMatthew G. Knepley 1768da1dd7e4SMatthew G. Knepley /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */ 1769e1757548SMatthew G. Knepley for (f = 0, qf = 0; f < supportSize; ++f) { 1770e1757548SMatthew G. Knepley PetscInt val, face; 1771e1757548SMatthew G. Knepley 17729566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[f], &val)); 1773e1757548SMatthew G. Knepley if (val == dim - 1) { 17749566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face)); 177563a3b9bcSJacob Faibussowitsch PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]); 17762582d50cSToby Isaac supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/; 1777e1757548SMatthew G. Knepley supportNew[qf++] = face + pMaxNew[dep + 1]; 1778e1757548SMatthew G. Knepley } else { 17792582d50cSToby Isaac supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/; 1780e1757548SMatthew G. Knepley } 1781e1757548SMatthew G. Knepley } 1782e1757548SMatthew G. Knepley supportNew[qf++] = hybface; 1783e1757548SMatthew G. Knepley supportNew[qf++] = hybface; 17849566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew)); 178563a3b9bcSJacob Faibussowitsch PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew); 17869566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, newp, supportNew)); 1787e1757548SMatthew G. Knepley /* Add hybrid face */ 1788e1757548SMatthew G. Knepley coneNew[0] = newp; 1789212cc919SMatthew G. Knepley coneNew[1] = newp; 17909566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v)); 179163a3b9bcSJacob Faibussowitsch PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]); 1792212cc919SMatthew G. Knepley coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1]; 17939566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v)); 179463a3b9bcSJacob Faibussowitsch PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]); 1795e1757548SMatthew G. Knepley coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1]; 17969566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(sdm, hybface, coneNew)); 1797da1dd7e4SMatthew G. Knepley for (f = 0, qf = 0; f < supportSize; ++f) { 1798da1dd7e4SMatthew G. Knepley PetscInt val, face; 1799da1dd7e4SMatthew G. Knepley 18009566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, support[f], &val)); 1801da1dd7e4SMatthew G. Knepley if (val == dim - 1) { 18029566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face)); 1803da1dd7e4SMatthew G. Knepley supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2]; 1804da1dd7e4SMatthew G. Knepley } 1805da1dd7e4SMatthew G. Knepley } 18069566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew)); 180763a3b9bcSJacob Faibussowitsch PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew); 18089566063dSJacob Faibussowitsch PetscCall(DMPlexSetSupport(sdm, hybface, supportNew)); 1809cd0c2139SMatthew G Knepley } 1810cd0c2139SMatthew G Knepley } 1811cd0c2139SMatthew G Knepley } 1812cd0c2139SMatthew G Knepley /* Step 6b: Replace split points in negative side cones */ 1813cd0c2139SMatthew G Knepley for (sp = 0; sp < numSP; ++sp) { 1814cd0c2139SMatthew G Knepley PetscInt dep = values[sp]; 1815cd0c2139SMatthew G Knepley IS pIS; 1816cd0c2139SMatthew G Knepley PetscInt numPoints; 1817cd0c2139SMatthew G Knepley const PetscInt *points; 1818cd0c2139SMatthew G Knepley 1819cd0c2139SMatthew G Knepley if (dep >= 0) continue; 18209566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, dep, &pIS)); 1821cd0c2139SMatthew G Knepley if (!pIS) continue; 1822cd0c2139SMatthew G Knepley dep = -dep - shift; 18239566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(pIS, &numPoints)); 18249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pIS, &points)); 1825cd0c2139SMatthew G Knepley for (p = 0; p < numPoints; ++p) { 1826cd0c2139SMatthew G Knepley const PetscInt oldp = points[p]; 18272582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/; 1828cd0c2139SMatthew G Knepley const PetscInt *cone; 1829cd0c2139SMatthew G Knepley PetscInt coneSize, c; 183050cf782dSMatthew G. Knepley /* PetscBool replaced = PETSC_FALSE; */ 1831cd0c2139SMatthew G Knepley 1832cd0c2139SMatthew G Knepley /* Negative edge: replace split vertex */ 1833cd0c2139SMatthew G Knepley /* Negative cell: replace split face */ 18349566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize)); 18359566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(sdm, newp, &cone)); 1836cd0c2139SMatthew G Knepley for (c = 0; c < coneSize; ++c) { 1837e38fbfedSToby Isaac const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift); 1838cd0c2139SMatthew G Knepley PetscInt csplitp, cp, val; 1839cd0c2139SMatthew G Knepley 18409566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, coldp, &val)); 1841cd0c2139SMatthew G Knepley if (val == dep - 1) { 18429566063dSJacob Faibussowitsch PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp)); 184363a3b9bcSJacob Faibussowitsch PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1); 1844cd0c2139SMatthew G Knepley csplitp = pMaxNew[dep - 1] + cp; 18459566063dSJacob Faibussowitsch PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp)); 184650cf782dSMatthew G. Knepley /* replaced = PETSC_TRUE; */ 1847cd0c2139SMatthew G Knepley } 1848cd0c2139SMatthew G Knepley } 18494a189a86SMatthew G. Knepley /* Cells with only a vertex or edge on the submesh have no replacement */ 185028b400f6SJacob Faibussowitsch /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */ 1851cd0c2139SMatthew G Knepley } 18529566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pIS, &points)); 18539566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pIS)); 1854cd0c2139SMatthew G Knepley } 18550e18dc48SMatthew G. Knepley PetscCall(DMPlexReorderCohesiveSupports(sdm)); 1856fa8e8ae5SToby Isaac /* Step 7: Coordinates */ 18579566063dSJacob Faibussowitsch PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm)); 18589566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(sdm, &coordSection)); 18599566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(sdm, &coordinates)); 18609566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 1861cd0c2139SMatthew G Knepley for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) { 18622582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/; 1863cd0c2139SMatthew G Knepley const PetscInt splitp = pMaxNew[0] + v; 1864cd0c2139SMatthew G Knepley PetscInt dof, off, soff, d; 1865cd0c2139SMatthew G Knepley 18669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, newp, &dof)); 18679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, newp, &off)); 18689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff)); 1869cd0c2139SMatthew G Knepley for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d]; 1870cd0c2139SMatthew G Knepley } 18719566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 1872fa8e8ae5SToby Isaac /* Step 8: SF, if I can figure this out we can split the mesh in parallel */ 18739566063dSJacob Faibussowitsch PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm)); 1874b6dfa339SMatthew G. Knepley /* TODO We need to associate the ghost points with the correct replica */ 1875fa8e8ae5SToby Isaac /* Step 9: Labels */ 18769566063dSJacob Faibussowitsch PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm)); 18779566063dSJacob Faibussowitsch PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm)); 18789566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(sdm, &numLabels)); 1879cd0c2139SMatthew G Knepley for (dep = 0; dep <= depth; ++dep) { 1880cd0c2139SMatthew G Knepley for (p = 0; p < numSplitPoints[dep]; ++p) { 18812582d50cSToby Isaac const PetscInt newp = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/; 1882cd0c2139SMatthew G Knepley const PetscInt splitp = pMaxNew[dep] + p; 1883cd0c2139SMatthew G Knepley PetscInt l; 1884cd0c2139SMatthew G Knepley 18857db7e0a7SMatthew G. Knepley if (splitLabel) { 18867db7e0a7SMatthew G. Knepley const PetscInt val = 100 + dep; 18877db7e0a7SMatthew G. Knepley 18889566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(splitLabel, newp, val)); 18899566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(splitLabel, splitp, -val)); 18907db7e0a7SMatthew G. Knepley } 1891cd0c2139SMatthew G Knepley for (l = 0; l < numLabels; ++l) { 1892cd0c2139SMatthew G Knepley DMLabel mlabel; 1893cd0c2139SMatthew G Knepley const char *lname; 1894cd0c2139SMatthew G Knepley PetscInt val; 18959a356370SMatthew G. Knepley PetscBool isDepth; 1896cd0c2139SMatthew G Knepley 18979566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(sdm, l, &lname)); 18989566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "depth", &isDepth)); 18999a356370SMatthew G. Knepley if (isDepth) continue; 19009566063dSJacob Faibussowitsch PetscCall(DMGetLabel(sdm, lname, &mlabel)); 19019566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(mlabel, newp, &val)); 190248a46eb9SPierre Jolivet if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val)); 1903cd0c2139SMatthew G Knepley } 1904cd0c2139SMatthew G Knepley } 1905cd0c2139SMatthew G Knepley } 1906cd0c2139SMatthew G Knepley for (sp = 0; sp < numSP; ++sp) { 1907cd0c2139SMatthew G Knepley const PetscInt dep = values[sp]; 1908cd0c2139SMatthew G Knepley 1909cd0c2139SMatthew G Knepley if ((dep < 0) || (dep > depth)) continue; 19109566063dSJacob Faibussowitsch if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep])); 19119566063dSJacob Faibussowitsch PetscCall(ISDestroy(&splitIS[dep])); 19129566063dSJacob Faibussowitsch if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep])); 19139566063dSJacob Faibussowitsch PetscCall(ISDestroy(&unsplitIS[dep])); 1914cd0c2139SMatthew G Knepley } 1915b6dfa339SMatthew G. Knepley if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints)); 1916b6dfa339SMatthew G. Knepley PetscCall(ISDestroy(&ghostIS)); 1917cd0c2139SMatthew G Knepley if (label) { 19189566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 19199566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 1920cd0c2139SMatthew G Knepley } 19210d4d4d06SMatthew G. Knepley for (d = 0; d <= depth; ++d) { 19229566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd)); 192336dbac82SMatthew G. Knepley pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d]; 19240d4d4d06SMatthew G. Knepley } 19259566063dSJacob Faibussowitsch PetscCall(PetscFree3(coneNew, coneONew, supportNew)); 19269566063dSJacob Faibussowitsch PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld)); 19279566063dSJacob Faibussowitsch PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints)); 19283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1929cd0c2139SMatthew G Knepley } 1930cd0c2139SMatthew G Knepley 1931cd0c2139SMatthew G Knepley /*@C 1932cd0c2139SMatthew G Knepley DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface 1933cd0c2139SMatthew G Knepley 193420f4b53cSBarry Smith Collective 1935cd0c2139SMatthew G Knepley 1936cd0c2139SMatthew G Knepley Input Parameters: 193720f4b53cSBarry Smith + dm - The original `DM` 193820f4b53cSBarry Smith - label - The `DMLabel` specifying the boundary faces (this could be auto-generated) 1939cd0c2139SMatthew G Knepley 1940cd0c2139SMatthew G Knepley Output Parameters: 194120f4b53cSBarry Smith + splitLabel - The `DMLabel` containing the split points, or `NULL` if no output is desired 194220f4b53cSBarry Smith - dmSplit - The new `DM` 1943cd0c2139SMatthew G Knepley 1944cd0c2139SMatthew G Knepley Level: developer 1945cd0c2139SMatthew G Knepley 19461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexLabelCohesiveComplete()` 1947cd0c2139SMatthew G Knepley @*/ 1948d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit) 1949d71ae5a4SJacob Faibussowitsch { 1950cd0c2139SMatthew G Knepley DM sdm; 1951cd0c2139SMatthew G Knepley PetscInt dim; 1952cd0c2139SMatthew G Knepley 1953cd0c2139SMatthew G Knepley PetscFunctionBegin; 1954cd0c2139SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 19554f572ea9SToby Isaac PetscAssertPointer(dmSplit, 4); 19569566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm)); 19579566063dSJacob Faibussowitsch PetscCall(DMSetType(sdm, DMPLEX)); 19589566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 19599566063dSJacob Faibussowitsch PetscCall(DMSetDimension(sdm, dim)); 1960cd0c2139SMatthew G Knepley switch (dim) { 1961cd0c2139SMatthew G Knepley case 2: 1962d71ae5a4SJacob Faibussowitsch case 3: 1963d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm)); 1964d71ae5a4SJacob Faibussowitsch break; 1965d71ae5a4SJacob Faibussowitsch default: 1966d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim); 1967cd0c2139SMatthew G Knepley } 1968cd0c2139SMatthew G Knepley *dmSplit = sdm; 19693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1970cd0c2139SMatthew G Knepley } 1971cd0c2139SMatthew G Knepley 19720f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */ 1973d71ae5a4SJacob Faibussowitsch static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos) 1974d71ae5a4SJacob Faibussowitsch { 19750f66a230SMatthew G. Knepley const PetscInt *cone, *ornt; 19760f66a230SMatthew G. Knepley PetscInt dim, coneSize, c; 19770f66a230SMatthew G. Knepley 19780f66a230SMatthew G. Knepley PetscFunctionBegin; 19790f66a230SMatthew G. Knepley *pos = PETSC_TRUE; 19809566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 19819566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 19829566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, cell, &cone)); 19839566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt)); 19840f66a230SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 19850f66a230SMatthew G. Knepley if (cone[c] == face) { 19860f66a230SMatthew G. Knepley PetscInt o = ornt[c]; 19870f66a230SMatthew G. Knepley 19880f66a230SMatthew G. Knepley if (subdm) { 19890f66a230SMatthew G. Knepley const PetscInt *subcone, *subornt; 19900f66a230SMatthew G. Knepley PetscInt subpoint, subface, subconeSize, sc; 19910f66a230SMatthew G. Knepley 19929566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint)); 19939566063dSJacob Faibussowitsch PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface)); 19949566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize)); 19959566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(subdm, subpoint, &subcone)); 19969566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt)); 19970f66a230SMatthew G. Knepley for (sc = 0; sc < subconeSize; ++sc) { 19980f66a230SMatthew G. Knepley if (subcone[sc] == subface) { 19990f66a230SMatthew G. Knepley o = subornt[0]; 20000f66a230SMatthew G. Knepley break; 20010f66a230SMatthew G. Knepley } 20020f66a230SMatthew G. Knepley } 200363a3b9bcSJacob Faibussowitsch PetscCheck(sc < subconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell); 20040f66a230SMatthew G. Knepley } 20050f66a230SMatthew G. Knepley if (o >= 0) *pos = PETSC_TRUE; 20060f66a230SMatthew G. Knepley else *pos = PETSC_FALSE; 20070f66a230SMatthew G. Knepley break; 20080f66a230SMatthew G. Knepley } 20090f66a230SMatthew G. Knepley } 201063a3b9bcSJacob Faibussowitsch PetscCheck(c != coneSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face); 20113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 20120f66a230SMatthew G. Knepley } 20130f66a230SMatthew G. Knepley 2014d71ae5a4SJacob Faibussowitsch static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label) 2015d71ae5a4SJacob Faibussowitsch { 2016accc9626SMatthew G. Knepley IS facePosIS, faceNegIS, dimIS; 2017accc9626SMatthew G. Knepley const PetscInt *points; 2018accc9626SMatthew G. Knepley PetscInt dim, numPoints, p, shift = 100, shift2 = 200; 2019accc9626SMatthew G. Knepley 2020accc9626SMatthew G. Knepley PetscFunctionBegin; 2021accc9626SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 2022accc9626SMatthew G. Knepley /* If any faces touching the fault divide cells on either side, split them */ 2023accc9626SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS)); 2024accc9626SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS)); 2025accc9626SMatthew G. Knepley if (!facePosIS || !faceNegIS) { 2026accc9626SMatthew G. Knepley PetscCall(ISDestroy(&facePosIS)); 2027accc9626SMatthew G. Knepley PetscCall(ISDestroy(&faceNegIS)); 20283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2029accc9626SMatthew G. Knepley } 2030accc9626SMatthew G. Knepley PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS)); 2031accc9626SMatthew G. Knepley PetscCall(ISDestroy(&facePosIS)); 2032accc9626SMatthew G. Knepley PetscCall(ISDestroy(&faceNegIS)); 2033accc9626SMatthew G. Knepley PetscCall(ISGetLocalSize(dimIS, &numPoints)); 2034accc9626SMatthew G. Knepley PetscCall(ISGetIndices(dimIS, &points)); 2035accc9626SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 2036accc9626SMatthew G. Knepley const PetscInt point = points[p]; 2037accc9626SMatthew G. Knepley const PetscInt *support; 2038accc9626SMatthew G. Knepley PetscInt supportSize, valA, valB; 2039accc9626SMatthew G. Knepley 2040accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 2041accc9626SMatthew G. Knepley if (supportSize != 2) continue; 2042accc9626SMatthew G. Knepley PetscCall(DMPlexGetSupport(dm, point, &support)); 2043accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, support[0], &valA)); 2044accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, support[1], &valB)); 2045accc9626SMatthew G. Knepley if ((valA == -1) || (valB == -1)) continue; 2046accc9626SMatthew G. Knepley if (valA * valB > 0) continue; 2047e1a13daeSMatthew G. Knepley /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */ 2048e1a13daeSMatthew G. Knepley { 2049e1a13daeSMatthew G. Knepley PetscInt *closure = NULL; 2050e1a13daeSMatthew G. Knepley PetscBool split = PETSC_FALSE; 2051e1a13daeSMatthew G. Knepley PetscInt closureSize, cl; 2052e1a13daeSMatthew G. Knepley 2053e1a13daeSMatthew G. Knepley PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2054e1a13daeSMatthew G. Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2055e1a13daeSMatthew G. Knepley PetscCall(DMLabelGetValue(label, closure[cl], &valA)); 20569371c9d4SSatish Balay if ((valA >= 0) && (valA <= dim)) { 20579371c9d4SSatish Balay split = PETSC_TRUE; 20589371c9d4SSatish Balay break; 20599371c9d4SSatish Balay } 2060e1a13daeSMatthew G. Knepley } 2061e1a13daeSMatthew G. Knepley PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2062e1a13daeSMatthew G. Knepley if (!split) continue; 2063e1a13daeSMatthew G. Knepley } 2064accc9626SMatthew G. Knepley /* Split the face */ 2065accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, point, &valA)); 2066accc9626SMatthew G. Knepley PetscCall(DMLabelClearValue(label, point, valA)); 2067accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, point, dim - 1)); 2068accc9626SMatthew G. Knepley /* Label its closure: 2069accc9626SMatthew G. Knepley unmarked: label as unsplit 2070accc9626SMatthew G. Knepley incident: relabel as split 2071accc9626SMatthew G. Knepley split: do nothing 2072accc9626SMatthew G. Knepley */ 2073accc9626SMatthew G. Knepley { 2074accc9626SMatthew G. Knepley PetscInt *closure = NULL; 2075accc9626SMatthew G. Knepley PetscInt closureSize, cl, dep; 2076accc9626SMatthew G. Knepley 2077accc9626SMatthew G. Knepley PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2078accc9626SMatthew G. Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2079accc9626SMatthew G. Knepley PetscCall(DMLabelGetValue(label, closure[cl], &valA)); 2080accc9626SMatthew G. Knepley if (valA == -1) { /* Mark as unsplit */ 2081accc9626SMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep)); 2082accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep)); 2083accc9626SMatthew G. Knepley } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) { 2084accc9626SMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep)); 2085accc9626SMatthew G. Knepley PetscCall(DMLabelClearValue(label, closure[cl], valA)); 2086accc9626SMatthew G. Knepley PetscCall(DMLabelSetValue(label, closure[cl], dep)); 2087accc9626SMatthew G. Knepley } 2088accc9626SMatthew G. Knepley } 2089accc9626SMatthew G. Knepley PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2090accc9626SMatthew G. Knepley } 2091accc9626SMatthew G. Knepley } 2092accc9626SMatthew G. Knepley PetscCall(ISRestoreIndices(dimIS, &points)); 2093accc9626SMatthew G. Knepley PetscCall(ISDestroy(&dimIS)); 20943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2095accc9626SMatthew G. Knepley } 2096accc9626SMatthew G. Knepley 2097cd0c2139SMatthew G Knepley /*@ 20980f66a230SMatthew G. Knepley DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces 2099cd0c2139SMatthew G Knepley to complete the surface 2100cd0c2139SMatthew G Knepley 2101cd0c2139SMatthew G Knepley Input Parameters: 210220f4b53cSBarry Smith + dm - The `DM` 210320f4b53cSBarry Smith . label - A `DMLabel` marking the surface 210420f4b53cSBarry Smith . blabel - A `DMLabel` marking the vertices on the boundary which will not be duplicated, or `NULL` to find them automatically 210520f4b53cSBarry Smith . bvalue - Value of `DMLabel` marking the vertices on the boundary 2106bb55d314SMatthew G. Knepley . flip - Flag to flip the submesh normal and replace points on the other side 210720f4b53cSBarry Smith - subdm - The `DM` associated with the label, or `NULL` 2108cd0c2139SMatthew G Knepley 2109cd0c2139SMatthew G Knepley Output Parameter: 211020f4b53cSBarry Smith . label - A `DMLabel` marking all surface points 21110f66a230SMatthew G. Knepley 2112cd0c2139SMatthew G Knepley Level: developer 2113cd0c2139SMatthew G Knepley 211420f4b53cSBarry Smith Note: 211520f4b53cSBarry Smith The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation. 211620f4b53cSBarry Smith 21171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()` 2118cd0c2139SMatthew G Knepley @*/ 2119d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm) 2120d71ae5a4SJacob Faibussowitsch { 2121d90583fdSMatthew G. Knepley DMLabel depthLabel; 2122accc9626SMatthew G. Knepley IS dimIS, subpointIS = NULL; 212347946fd8SMatthew G. Knepley const PetscInt *points, *subpoints; 2124bb55d314SMatthew G. Knepley const PetscInt rev = flip ? -1 : 1; 2125accc9626SMatthew G. Knepley PetscInt shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val; 2126cd0c2139SMatthew G Knepley 2127cd0c2139SMatthew G Knepley PetscFunctionBegin; 21289566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 21299566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 21309566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 213147946fd8SMatthew G. Knepley if (subdm) { 21329566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS)); 213347946fd8SMatthew G. Knepley if (subpointIS) { 21349566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(subpointIS, &numSubpoints)); 21359566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subpointIS, &subpoints)); 213647946fd8SMatthew G. Knepley } 213747946fd8SMatthew G. Knepley } 2138d7c8f101SMatthew G. Knepley /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */ 21399566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS)); 2140accc9626SMatthew G. Knepley if (!dimIS) goto divide; 21419566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(dimIS, &numPoints)); 21429566063dSJacob Faibussowitsch PetscCall(ISGetIndices(dimIS, &points)); 2143d7c8f101SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */ 2144cd0c2139SMatthew G Knepley const PetscInt *support; 2145cd0c2139SMatthew G Knepley PetscInt supportSize, s; 2146cd0c2139SMatthew G Knepley 21479566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize)); 2148c4419245SMatthew G. Knepley #if 0 2149c4419245SMatthew G. Knepley if (supportSize != 2) { 2150c4419245SMatthew G. Knepley const PetscInt *lp; 2151c4419245SMatthew G. Knepley PetscInt Nlp, pind; 2152c4419245SMatthew G. Knepley 2153c4419245SMatthew G. Knepley /* Check that for a cell with a single support face, that face is in the SF */ 2154c4419245SMatthew G. Knepley /* THis check only works for the remote side. We would need root side information */ 21559566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL)); 21569566063dSJacob Faibussowitsch PetscCall(PetscFindInt(points[p], Nlp, lp, &pind)); 215763a3b9bcSJacob Faibussowitsch PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize); 2158c4419245SMatthew G. Knepley } 2159c4419245SMatthew G. Knepley #endif 21609566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, points[p], &support)); 2161cd0c2139SMatthew G Knepley for (s = 0; s < supportSize; ++s) { 21620f66a230SMatthew G. Knepley const PetscInt *cone; 2163cd0c2139SMatthew G Knepley PetscInt coneSize, c; 21640f66a230SMatthew G. Knepley PetscBool pos; 2165cd0c2139SMatthew G Knepley 21669566063dSJacob Faibussowitsch PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos)); 21679566063dSJacob Faibussowitsch if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim))); 21689566063dSJacob Faibussowitsch else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim))); 21690f66a230SMatthew G. Knepley if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE; 21700f66a230SMatthew G. Knepley /* Put faces touching the fault in the label */ 21719566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize)); 21729566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[s], &cone)); 2173cd0c2139SMatthew G Knepley for (c = 0; c < coneSize; ++c) { 2174cd0c2139SMatthew G Knepley const PetscInt point = cone[c]; 2175cd0c2139SMatthew G Knepley 21769566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, point, &val)); 2177cd0c2139SMatthew G Knepley if (val == -1) { 2178cd0c2139SMatthew G Knepley PetscInt *closure = NULL; 2179cd0c2139SMatthew G Knepley PetscInt closureSize, cl; 2180cd0c2139SMatthew G Knepley 21819566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2182cd0c2139SMatthew G Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2183cd0c2139SMatthew G Knepley const PetscInt clp = closure[cl]; 2184a0541d8aSMatthew G. Knepley PetscInt bval = -1; 2185cd0c2139SMatthew G Knepley 21869566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, clp, &val)); 21879566063dSJacob Faibussowitsch if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval)); 2188a0541d8aSMatthew G. Knepley if ((val >= 0) && (val < dim - 1) && (bval < 0)) { 21899566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1))); 2190cd0c2139SMatthew G Knepley break; 2191cd0c2139SMatthew G Knepley } 2192cd0c2139SMatthew G Knepley } 21939566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2194cd0c2139SMatthew G Knepley } 2195cd0c2139SMatthew G Knepley } 2196cd0c2139SMatthew G Knepley } 2197cd0c2139SMatthew G Knepley } 2198accc9626SMatthew G. Knepley PetscCall(ISRestoreIndices(dimIS, &points)); 2199accc9626SMatthew G. Knepley PetscCall(ISDestroy(&dimIS)); 2200a0541d8aSMatthew G. Knepley /* Mark boundary points as unsplit */ 220186200784SMatthew G. Knepley if (blabel) { 2202accc9626SMatthew G. Knepley IS bdIS; 2203accc9626SMatthew G. Knepley 2204caf9e14dSMatthew G. Knepley PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS)); 2205accc9626SMatthew G. Knepley PetscCall(ISGetLocalSize(bdIS, &numPoints)); 2206accc9626SMatthew G. Knepley PetscCall(ISGetIndices(bdIS, &points)); 2207a0541d8aSMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 2208a0541d8aSMatthew G. Knepley const PetscInt point = points[p]; 2209a0541d8aSMatthew G. Knepley PetscInt val, bval; 2210a0541d8aSMatthew G. Knepley 22119566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(blabel, point, &bval)); 2212a0541d8aSMatthew G. Knepley if (bval >= 0) { 22139566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, point, &val)); 2214f7019248SMatthew G. Knepley if ((val < 0) || (val > dim)) { 2215f7019248SMatthew G. Knepley /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */ 22169566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(blabel, point, bval)); 2217f7019248SMatthew G. Knepley } 2218f7019248SMatthew G. Knepley } 2219f7019248SMatthew G. Knepley } 2220f7019248SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 2221f7019248SMatthew G. Knepley const PetscInt point = points[p]; 2222f7019248SMatthew G. Knepley PetscInt val, bval; 2223f7019248SMatthew G. Knepley 22249566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(blabel, point, &bval)); 2225f7019248SMatthew G. Knepley if (bval >= 0) { 222686200784SMatthew G. Knepley const PetscInt *cone, *support; 222786200784SMatthew G. Knepley PetscInt coneSize, supportSize, s, valA, valB, valE; 222886200784SMatthew G. Knepley 2229a0541d8aSMatthew G. Knepley /* Mark as unsplit */ 22309566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, point, &val)); 2231e1a13daeSMatthew G. Knepley PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val); 22329566063dSJacob Faibussowitsch PetscCall(DMLabelClearValue(label, point, val)); 22339566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, point, shift2 + val)); 22342c06a818SMatthew G. Knepley /* Check for cross-edge 22352c06a818SMatthew G. Knepley A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */ 223686200784SMatthew G. Knepley if (val != 0) continue; 22379566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 22389566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 223986200784SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 22409566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, support[s], &cone)); 22419566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize)); 224263a3b9bcSJacob Faibussowitsch PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize); 22439566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(blabel, cone[0], &valA)); 22449566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(blabel, cone[1], &valB)); 22459566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(blabel, support[s], &valE)); 22469566063dSJacob Faibussowitsch if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2)); 224786200784SMatthew G. Knepley } 2248a0541d8aSMatthew G. Knepley } 2249a0541d8aSMatthew G. Knepley } 2250accc9626SMatthew G. Knepley PetscCall(ISRestoreIndices(bdIS, &points)); 2251accc9626SMatthew G. Knepley PetscCall(ISDestroy(&bdIS)); 2252a0541d8aSMatthew G. Knepley } 2253b6dfa339SMatthew G. Knepley /* Mark ghost fault cells */ 2254b6dfa339SMatthew G. Knepley { 2255b6dfa339SMatthew G. Knepley PetscSF sf; 2256b6dfa339SMatthew G. Knepley const PetscInt *leaves; 2257b6dfa339SMatthew G. Knepley PetscInt Nl, l; 2258b6dfa339SMatthew G. Knepley 2259b6dfa339SMatthew G. Knepley PetscCall(DMGetPointSF(dm, &sf)); 2260b6dfa339SMatthew G. Knepley PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 2261accc9626SMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS)); 2262accc9626SMatthew G. Knepley if (!dimIS) goto divide; 2263accc9626SMatthew G. Knepley PetscCall(ISGetLocalSize(dimIS, &numPoints)); 2264accc9626SMatthew G. Knepley PetscCall(ISGetIndices(dimIS, &points)); 2265b6dfa339SMatthew G. Knepley if (Nl > 0) { 2266b6dfa339SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 2267b6dfa339SMatthew G. Knepley const PetscInt point = points[p]; 2268b6dfa339SMatthew G. Knepley PetscInt val; 2269b6dfa339SMatthew G. Knepley 2270b6dfa339SMatthew G. Knepley PetscCall(PetscFindInt(point, Nl, leaves, &l)); 2271b6dfa339SMatthew G. Knepley if (l >= 0) { 2272b6dfa339SMatthew G. Knepley PetscInt *closure = NULL; 2273b6dfa339SMatthew G. Knepley PetscInt closureSize, cl; 2274b6dfa339SMatthew G. Knepley 2275b6dfa339SMatthew G. Knepley PetscCall(DMLabelGetValue(label, point, &val)); 2276b6dfa339SMatthew G. Knepley PetscCheck((val == dim - 1) || (val == shift2 + dim - 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be a fault face", point, val); 2277b6dfa339SMatthew G. Knepley PetscCall(DMLabelClearValue(label, point, val)); 2278b6dfa339SMatthew G. Knepley PetscCall(DMLabelSetValue(label, point, shift3 + val)); 2279b6dfa339SMatthew G. Knepley PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2280accc9626SMatthew G. Knepley for (cl = 2; cl < closureSize * 2; cl += 2) { 2281b6dfa339SMatthew G. Knepley const PetscInt clp = closure[cl]; 2282b6dfa339SMatthew G. Knepley 2283b6dfa339SMatthew G. Knepley PetscCall(DMLabelGetValue(label, clp, &val)); 2284b6dfa339SMatthew G. Knepley PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is missing from label, but is in the closure of a fault face", point); 2285b6dfa339SMatthew G. Knepley PetscCall(DMLabelClearValue(label, clp, val)); 2286b6dfa339SMatthew G. Knepley PetscCall(DMLabelSetValue(label, clp, shift3 + val)); 2287b6dfa339SMatthew G. Knepley } 2288b6dfa339SMatthew G. Knepley PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure)); 2289b6dfa339SMatthew G. Knepley } 2290b6dfa339SMatthew G. Knepley } 2291b6dfa339SMatthew G. Knepley } 2292b6dfa339SMatthew G. Knepley PetscCall(ISRestoreIndices(dimIS, &points)); 2293b6dfa339SMatthew G. Knepley PetscCall(ISDestroy(&dimIS)); 2294accc9626SMatthew G. Knepley } 2295accc9626SMatthew G. Knepley divide: 2296b6dfa339SMatthew G. Knepley if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints)); 2297accc9626SMatthew G. Knepley PetscCall(DMPlexLabelFaultHalo(dm, label)); 2298accc9626SMatthew G. Knepley PetscCall(CheckFaultEdge_Private(dm, label)); 22993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2300cd0c2139SMatthew G Knepley } 2301cd0c2139SMatthew G Knepley 2302720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */ 230366976f2fSJacob Faibussowitsch static PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm) 2304d71ae5a4SJacob Faibussowitsch { 2305720e594eSMatthew G. Knepley IS subpointIS; 2306720e594eSMatthew G. Knepley const PetscInt *dmpoints; 2307720e594eSMatthew G. Knepley PetscInt defaultValue, cStart, cEnd, c, vStart, vEnd; 2308720e594eSMatthew G. Knepley 2309720e594eSMatthew G. Knepley PetscFunctionBegin; 23103ba16761SJacob Faibussowitsch if (!label) PetscFunctionReturn(PETSC_SUCCESS); 23119566063dSJacob Faibussowitsch PetscCall(DMLabelGetDefaultValue(label, &defaultValue)); 23129566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS)); 23133ba16761SJacob Faibussowitsch if (!subpointIS) PetscFunctionReturn(PETSC_SUCCESS); 23149566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd)); 23159566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 23169566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subpointIS, &dmpoints)); 2317720e594eSMatthew G. Knepley for (c = cStart; c < cEnd; ++c) { 2318720e594eSMatthew G. Knepley PetscBool invalidCell = PETSC_TRUE; 2319720e594eSMatthew G. Knepley PetscInt *closure = NULL; 2320720e594eSMatthew G. Knepley PetscInt closureSize, cl; 2321720e594eSMatthew G. Knepley 23229566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure)); 2323720e594eSMatthew G. Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2324720e594eSMatthew G. Knepley PetscInt value = 0; 2325720e594eSMatthew G. Knepley 2326720e594eSMatthew G. Knepley if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue; 23279566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, closure[cl], &value)); 23289371c9d4SSatish Balay if (value == defaultValue) { 23299371c9d4SSatish Balay invalidCell = PETSC_FALSE; 23309371c9d4SSatish Balay break; 23319371c9d4SSatish Balay } 2332720e594eSMatthew G. Knepley } 23339566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure)); 2334720e594eSMatthew G. Knepley if (invalidCell) { 23359566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(subpointIS, &dmpoints)); 23369566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subpointIS)); 23379566063dSJacob Faibussowitsch PetscCall(DMDestroy(&subdm)); 233863a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]); 2339720e594eSMatthew G. Knepley } 2340720e594eSMatthew G. Knepley } 23419566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(subpointIS, &dmpoints)); 23423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2343720e594eSMatthew G. Knepley } 2344720e594eSMatthew G. Knepley 2345c08575a3SMatthew G. Knepley /*@ 23463cf72582SMatthew G. Knepley DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface 23473cf72582SMatthew G. Knepley 234820f4b53cSBarry Smith Collective 23493cf72582SMatthew G. Knepley 23503cf72582SMatthew G. Knepley Input Parameters: 235120f4b53cSBarry Smith + dm - The original `DM` 2352720e594eSMatthew G. Knepley . label - The label specifying the interface vertices 2353caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices 2354caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices 23553cf72582SMatthew G. Knepley 23563cf72582SMatthew G. Knepley Output Parameters: 235720f4b53cSBarry Smith + hybridLabel - The label fully marking the interface, or `NULL` if no output is desired 235820f4b53cSBarry Smith . splitLabel - The label containing the split points, or `NULL` if no output is desired 235920f4b53cSBarry Smith . dmInterface - The new interface `DM`, or `NULL` 236020f4b53cSBarry Smith - dmHybrid - The new `DM` with cohesive cells 23613cf72582SMatthew G. Knepley 236220f4b53cSBarry Smith Level: developer 236320f4b53cSBarry Smith 236420f4b53cSBarry Smith Note: 236520f4b53cSBarry Smith The hybridLabel indicates what parts of the original mesh impinged on the division surface. For points 23666eccb800SMatthew Knepley directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be 23676eccb800SMatthew Knepley 7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with 23686eccb800SMatthew Knepley one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the 23696eccb800SMatthew Knepley surface also hits vertex 3, it would be 9 (-101) in hybridLabel. 23706eccb800SMatthew Knepley 23716eccb800SMatthew Knepley The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original 237240cdb708SMatthew Knepley mesh. The label value is $\pm 100+dim$ for each point. For example, if two edges 10 and 14 in the hybrid resulting from 23736eccb800SMatthew Knepley splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel. 23746eccb800SMatthew Knepley 237520f4b53cSBarry Smith The dmInterface is a `DM` built from the original division surface. It has a label which can be retrieved using 237620f4b53cSBarry Smith `DMPlexGetSubpointMap()` which maps each point back to the point in the surface of the original mesh. 23776eccb800SMatthew Knepley 23781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()` 23793cf72582SMatthew G. Knepley @*/ 2380d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid) 2381d71ae5a4SJacob Faibussowitsch { 23823cf72582SMatthew G. Knepley DM idm; 23837db7e0a7SMatthew G. Knepley DMLabel subpointMap, hlabel, slabel = NULL; 23843cf72582SMatthew G. Knepley PetscInt dim; 23853cf72582SMatthew G. Knepley 23863cf72582SMatthew G. Knepley PetscFunctionBegin; 23873cf72582SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 23884f572ea9SToby Isaac if (label) PetscAssertPointer(label, 2); 23894f572ea9SToby Isaac if (bdlabel) PetscAssertPointer(bdlabel, 3); 23904f572ea9SToby Isaac if (hybridLabel) PetscAssertPointer(hybridLabel, 5); 23914f572ea9SToby Isaac if (splitLabel) PetscAssertPointer(splitLabel, 6); 23924f572ea9SToby Isaac if (dmInterface) PetscAssertPointer(dmInterface, 7); 23934f572ea9SToby Isaac PetscAssertPointer(dmHybrid, 8); 23949566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 23959566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm)); 23969566063dSJacob Faibussowitsch PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm)); 23979566063dSJacob Faibussowitsch PetscCall(DMPlexOrient(idm)); 23989566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointMap(idm, &subpointMap)); 23999566063dSJacob Faibussowitsch PetscCall(DMLabelDuplicate(subpointMap, &hlabel)); 24009566063dSJacob Faibussowitsch PetscCall(DMLabelClearStratum(hlabel, dim)); 24017db7e0a7SMatthew G. Knepley if (splitLabel) { 24027db7e0a7SMatthew G. Knepley const char *name; 24037db7e0a7SMatthew G. Knepley char sname[PETSC_MAX_PATH_LEN]; 24047db7e0a7SMatthew G. Knepley 24059566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)hlabel, &name)); 2406c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(sname, name, sizeof(sname))); 2407c6a7a370SJeremy L Thompson PetscCall(PetscStrlcat(sname, " split", sizeof(sname))); 24089566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel)); 24097db7e0a7SMatthew G. Knepley } 2410caf9e14dSMatthew G. Knepley PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm)); 24119371c9d4SSatish Balay if (dmInterface) { 24129371c9d4SSatish Balay *dmInterface = idm; 24139371c9d4SSatish Balay } else PetscCall(DMDestroy(&idm)); 24149566063dSJacob Faibussowitsch PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid)); 24155de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid)); 24163cf72582SMatthew G. Knepley if (hybridLabel) *hybridLabel = hlabel; 24179566063dSJacob Faibussowitsch else PetscCall(DMLabelDestroy(&hlabel)); 24187db7e0a7SMatthew G. Knepley if (splitLabel) *splitLabel = slabel; 24194a7ee7d0SMatthew G. Knepley { 24204a7ee7d0SMatthew G. Knepley DM cdm; 24214a7ee7d0SMatthew G. Knepley DMLabel ctLabel; 24224a7ee7d0SMatthew G. Knepley 24234a7ee7d0SMatthew G. Knepley /* We need to somehow share the celltype label with the coordinate dm */ 24249566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm)); 24259566063dSJacob Faibussowitsch PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel)); 24269566063dSJacob Faibussowitsch PetscCall(DMSetLabel(cdm, ctLabel)); 24274a7ee7d0SMatthew G. Knepley } 24283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2429cd0c2139SMatthew G Knepley } 2430cd0c2139SMatthew G Knepley 2431efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that: 2432efa14ee0SMatthew G Knepley 2433efa14ee0SMatthew G Knepley For any marked cell, the marked vertices constitute a single face 2434efa14ee0SMatthew G Knepley */ 2435d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm) 2436d71ae5a4SJacob Faibussowitsch { 2437fed694aaSMatthew G. Knepley IS subvertexIS = NULL; 2438efa14ee0SMatthew G Knepley const PetscInt *subvertices; 2439412e9a14SMatthew G. Knepley PetscInt *pStart, *pEnd, pSize; 2440efa14ee0SMatthew G Knepley PetscInt depth, dim, d, numSubVerticesInitial = 0, v; 2441efa14ee0SMatthew G Knepley 2442efa14ee0SMatthew G Knepley PetscFunctionBegin; 2443efa14ee0SMatthew G Knepley *numFaces = 0; 2444efa14ee0SMatthew G Knepley *nFV = 0; 24459566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 24469566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 244777d178adSMatthew G. Knepley pSize = PetscMax(depth, dim) + 1; 24489566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd)); 244948a46eb9SPierre Jolivet for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d])); 2450efa14ee0SMatthew G Knepley /* Loop over initial vertices and mark all faces in the collective star() */ 24519566063dSJacob Faibussowitsch if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS)); 2452efa14ee0SMatthew G Knepley if (subvertexIS) { 24539566063dSJacob Faibussowitsch PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial)); 24549566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subvertexIS, &subvertices)); 2455efa14ee0SMatthew G Knepley } 2456efa14ee0SMatthew G Knepley for (v = 0; v < numSubVerticesInitial; ++v) { 2457efa14ee0SMatthew G Knepley const PetscInt vertex = subvertices[v]; 24580298fd71SBarry Smith PetscInt *star = NULL; 2459efa14ee0SMatthew G Knepley PetscInt starSize, s, numCells = 0, c; 2460efa14ee0SMatthew G Knepley 24619566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star)); 2462efa14ee0SMatthew G Knepley for (s = 0; s < starSize * 2; s += 2) { 2463efa14ee0SMatthew G Knepley const PetscInt point = star[s]; 2464efa14ee0SMatthew G Knepley if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point; 2465efa14ee0SMatthew G Knepley } 2466efa14ee0SMatthew G Knepley for (c = 0; c < numCells; ++c) { 2467efa14ee0SMatthew G Knepley const PetscInt cell = star[c]; 24680298fd71SBarry Smith PetscInt *closure = NULL; 2469efa14ee0SMatthew G Knepley PetscInt closureSize, cl; 2470efa14ee0SMatthew G Knepley PetscInt cellLoc, numCorners = 0, faceSize = 0; 2471efa14ee0SMatthew G Knepley 24729566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc)); 247365560c7fSMatthew G Knepley if (cellLoc == 2) continue; 247463a3b9bcSJacob Faibussowitsch PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc); 24759566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 2476efa14ee0SMatthew G Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2477efa14ee0SMatthew G Knepley const PetscInt point = closure[cl]; 2478efa14ee0SMatthew G Knepley PetscInt vertexLoc; 2479efa14ee0SMatthew G Knepley 2480efa14ee0SMatthew G Knepley if ((point >= pStart[0]) && (point < pEnd[0])) { 2481efa14ee0SMatthew G Knepley ++numCorners; 24829566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc)); 2483830e53efSMatthew G. Knepley if (vertexLoc == value) closure[faceSize++] = point; 2484efa14ee0SMatthew G Knepley } 2485efa14ee0SMatthew G Knepley } 24864ad8454bSPierre Jolivet if (!*nFV) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV)); 248763a3b9bcSJacob Faibussowitsch PetscCheck(faceSize <= *nFV, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize); 2488efa14ee0SMatthew G Knepley if (faceSize == *nFV) { 2489007baee2SMatthew G. Knepley const PetscInt *cells = NULL; 2490007baee2SMatthew G. Knepley PetscInt numCells, nc; 2491007baee2SMatthew G. Knepley 2492efa14ee0SMatthew G Knepley ++(*numFaces); 249348a46eb9SPierre Jolivet for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0)); 24949566063dSJacob Faibussowitsch PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells)); 249548a46eb9SPierre Jolivet for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2)); 24969566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells)); 2497efa14ee0SMatthew G Knepley } 24989566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 2499efa14ee0SMatthew G Knepley } 25009566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star)); 2501efa14ee0SMatthew G Knepley } 250248a46eb9SPierre Jolivet if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices)); 25039566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subvertexIS)); 25049566063dSJacob Faibussowitsch PetscCall(PetscFree2(pStart, pEnd)); 25053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2506efa14ee0SMatthew G Knepley } 2507efa14ee0SMatthew G Knepley 2508d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm) 2509d71ae5a4SJacob Faibussowitsch { 251034b4c39eSMatthew G. Knepley IS subvertexIS = NULL; 2511efa14ee0SMatthew G Knepley const PetscInt *subvertices; 2512412e9a14SMatthew G. Knepley PetscInt *pStart, *pEnd; 2513efa14ee0SMatthew G Knepley PetscInt dim, d, numSubVerticesInitial = 0, v; 2514efa14ee0SMatthew G Knepley 2515efa14ee0SMatthew G Knepley PetscFunctionBegin; 25169566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 25179566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd)); 251848a46eb9SPierre Jolivet for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d])); 2519efa14ee0SMatthew G Knepley /* Loop over initial vertices and mark all faces in the collective star() */ 252034b4c39eSMatthew G. Knepley if (vertexLabel) { 25219566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS)); 2522efa14ee0SMatthew G Knepley if (subvertexIS) { 25239566063dSJacob Faibussowitsch PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial)); 25249566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subvertexIS, &subvertices)); 2525efa14ee0SMatthew G Knepley } 252634b4c39eSMatthew G. Knepley } 2527efa14ee0SMatthew G Knepley for (v = 0; v < numSubVerticesInitial; ++v) { 2528efa14ee0SMatthew G Knepley const PetscInt vertex = subvertices[v]; 25290298fd71SBarry Smith PetscInt *star = NULL; 2530efa14ee0SMatthew G Knepley PetscInt starSize, s, numFaces = 0, f; 2531efa14ee0SMatthew G Knepley 25329566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star)); 2533efa14ee0SMatthew G Knepley for (s = 0; s < starSize * 2; s += 2) { 2534efa14ee0SMatthew G Knepley const PetscInt point = star[s]; 2535158acfadSMatthew G. Knepley PetscInt faceLoc; 2536158acfadSMatthew G. Knepley 2537158acfadSMatthew G. Knepley if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) { 2538158acfadSMatthew G. Knepley if (markedFaces) { 25399566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc)); 2540158acfadSMatthew G. Knepley if (faceLoc < 0) continue; 2541158acfadSMatthew G. Knepley } 2542158acfadSMatthew G. Knepley star[numFaces++] = point; 2543158acfadSMatthew G. Knepley } 2544efa14ee0SMatthew G Knepley } 2545efa14ee0SMatthew G Knepley for (f = 0; f < numFaces; ++f) { 2546efa14ee0SMatthew G Knepley const PetscInt face = star[f]; 25470298fd71SBarry Smith PetscInt *closure = NULL; 2548efa14ee0SMatthew G Knepley PetscInt closureSize, c; 2549efa14ee0SMatthew G Knepley PetscInt faceLoc; 2550efa14ee0SMatthew G Knepley 25519566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc)); 2552efa14ee0SMatthew G Knepley if (faceLoc == dim - 1) continue; 255363a3b9bcSJacob Faibussowitsch PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc); 25549566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure)); 2555efa14ee0SMatthew G Knepley for (c = 0; c < closureSize * 2; c += 2) { 2556efa14ee0SMatthew G Knepley const PetscInt point = closure[c]; 2557efa14ee0SMatthew G Knepley PetscInt vertexLoc; 2558efa14ee0SMatthew G Knepley 2559efa14ee0SMatthew G Knepley if ((point >= pStart[0]) && (point < pEnd[0])) { 25609566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc)); 2561830e53efSMatthew G. Knepley if (vertexLoc != value) break; 2562efa14ee0SMatthew G Knepley } 2563efa14ee0SMatthew G Knepley } 2564efa14ee0SMatthew G Knepley if (c == closureSize * 2) { 2565efa14ee0SMatthew G Knepley const PetscInt *support; 2566efa14ee0SMatthew G Knepley PetscInt supportSize, s; 2567efa14ee0SMatthew G Knepley 2568efa14ee0SMatthew G Knepley for (c = 0; c < closureSize * 2; c += 2) { 2569efa14ee0SMatthew G Knepley const PetscInt point = closure[c]; 2570efa14ee0SMatthew G Knepley 2571efa14ee0SMatthew G Knepley for (d = 0; d < dim; ++d) { 2572efa14ee0SMatthew G Knepley if ((point >= pStart[d]) && (point < pEnd[d])) { 25739566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(subpointMap, point, d)); 2574efa14ee0SMatthew G Knepley break; 2575efa14ee0SMatthew G Knepley } 2576efa14ee0SMatthew G Knepley } 2577efa14ee0SMatthew G Knepley } 25789566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, face, &supportSize)); 25799566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, face, &support)); 258048a46eb9SPierre Jolivet for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim)); 2581efa14ee0SMatthew G Knepley } 25829566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure)); 2583efa14ee0SMatthew G Knepley } 25849566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star)); 2585efa14ee0SMatthew G Knepley } 25869566063dSJacob Faibussowitsch if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices)); 25879566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subvertexIS)); 25889566063dSJacob Faibussowitsch PetscCall(PetscFree2(pStart, pEnd)); 25893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2590efa14ee0SMatthew G Knepley } 2591efa14ee0SMatthew G Knepley 2592d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm) 2593d71ae5a4SJacob Faibussowitsch { 259427c04023SMatthew G. Knepley DMLabel label = NULL; 2595766ab985SMatthew G. Knepley const PetscInt *cone; 25969fc93327SToby Isaac PetscInt dim, cMax, cEnd, c, subc = 0, p, coneSize = -1; 2597766ab985SMatthew G. Knepley 2598812bfc34SJed Brown PetscFunctionBegin; 2599c0ed958bSJed Brown *numFaces = 0; 2600c0ed958bSJed Brown *nFV = 0; 26019566063dSJacob Faibussowitsch if (labelname) PetscCall(DMGetLabel(dm, labelname, &label)); 2602fed694aaSMatthew G. Knepley *subCells = NULL; 26039566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 26049566063dSJacob Faibussowitsch PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd)); 26053ba16761SJacob Faibussowitsch if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS); 260627c04023SMatthew G. Knepley if (label) { 260727c04023SMatthew G. Knepley for (c = cMax; c < cEnd; ++c) { 260827c04023SMatthew G. Knepley PetscInt val; 260927c04023SMatthew G. Knepley 26109566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, c, &val)); 261127c04023SMatthew G. Knepley if (val == value) { 261227c04023SMatthew G. Knepley ++(*numFaces); 26139566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &coneSize)); 261427c04023SMatthew G. Knepley } 261527c04023SMatthew G. Knepley } 261627c04023SMatthew G. Knepley } else { 2617766ab985SMatthew G. Knepley *numFaces = cEnd - cMax; 26189566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize)); 261927c04023SMatthew G. Knepley } 26209566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(*numFaces * 2, subCells)); 26214ad8454bSPierre Jolivet if (!*numFaces) PetscFunctionReturn(PETSC_SUCCESS); 26229fc93327SToby Isaac *nFV = hasLagrange ? coneSize / 3 : coneSize / 2; 2623766ab985SMatthew G. Knepley for (c = cMax; c < cEnd; ++c) { 2624766ab985SMatthew G. Knepley const PetscInt *cells; 2625766ab985SMatthew G. Knepley PetscInt numCells; 2626766ab985SMatthew G. Knepley 262727c04023SMatthew G. Knepley if (label) { 262827c04023SMatthew G. Knepley PetscInt val; 262927c04023SMatthew G. Knepley 26309566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, c, &val)); 263127c04023SMatthew G. Knepley if (val != value) continue; 263227c04023SMatthew G. Knepley } 26339566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &cone)); 263448a46eb9SPierre Jolivet for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0)); 2635766ab985SMatthew G. Knepley /* Negative face */ 26369566063dSJacob Faibussowitsch PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells)); 263727234c99SMatthew G. Knepley /* Not true in parallel 263808401ef6SPierre Jolivet PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */ 2639766ab985SMatthew G. Knepley for (p = 0; p < numCells; ++p) { 26409566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(subpointMap, cells[p], 2)); 264127234c99SMatthew G. Knepley (*subCells)[subc++] = cells[p]; 2642766ab985SMatthew G. Knepley } 26439566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells)); 2644766ab985SMatthew G. Knepley /* Positive face is not included */ 2645766ab985SMatthew G. Knepley } 26463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2647766ab985SMatthew G. Knepley } 2648766ab985SMatthew G. Knepley 2649d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm) 2650d71ae5a4SJacob Faibussowitsch { 2651766ab985SMatthew G. Knepley PetscInt *pStart, *pEnd; 2652766ab985SMatthew G. Knepley PetscInt dim, cMax, cEnd, c, d; 2653766ab985SMatthew G. Knepley 2654812bfc34SJed Brown PetscFunctionBegin; 26559566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 26569566063dSJacob Faibussowitsch PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd)); 26573ba16761SJacob Faibussowitsch if (cMax < 0) PetscFunctionReturn(PETSC_SUCCESS); 26589566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd)); 26599566063dSJacob Faibussowitsch for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d])); 2660766ab985SMatthew G. Knepley for (c = cMax; c < cEnd; ++c) { 2661766ab985SMatthew G. Knepley const PetscInt *cone; 2662766ab985SMatthew G. Knepley PetscInt *closure = NULL; 2663b3154360SMatthew G. Knepley PetscInt fconeSize, coneSize, closureSize, cl, val; 2664766ab985SMatthew G. Knepley 266527c04023SMatthew G. Knepley if (label) { 26669566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(label, c, &val)); 266727c04023SMatthew G. Knepley if (val != value) continue; 266827c04023SMatthew G. Knepley } 26699566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, c, &coneSize)); 26709566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, c, &cone)); 26719566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize)); 26721dca8a05SBarry Smith PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); 2673b3154360SMatthew G. Knepley /* Negative face */ 26749566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure)); 2675766ab985SMatthew G. Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 2676766ab985SMatthew G. Knepley const PetscInt point = closure[cl]; 2677766ab985SMatthew G. Knepley 2678766ab985SMatthew G. Knepley for (d = 0; d <= dim; ++d) { 2679766ab985SMatthew G. Knepley if ((point >= pStart[d]) && (point < pEnd[d])) { 26809566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(subpointMap, point, d)); 2681766ab985SMatthew G. Knepley break; 2682766ab985SMatthew G. Knepley } 2683766ab985SMatthew G. Knepley } 2684766ab985SMatthew G. Knepley } 26859566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure)); 2686766ab985SMatthew G. Knepley /* Cells -- positive face is not included */ 2687766ab985SMatthew G. Knepley for (cl = 0; cl < 1; ++cl) { 2688766ab985SMatthew G. Knepley const PetscInt *support; 2689766ab985SMatthew G. Knepley PetscInt supportSize, s; 2690766ab985SMatthew G. Knepley 26919566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize)); 269208401ef6SPierre Jolivet /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */ 26939566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, cone[cl], &support)); 269448a46eb9SPierre Jolivet for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim)); 2695766ab985SMatthew G. Knepley } 2696766ab985SMatthew G. Knepley } 26979566063dSJacob Faibussowitsch PetscCall(PetscFree2(pStart, pEnd)); 26983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2699766ab985SMatthew G. Knepley } 2700766ab985SMatthew G. Knepley 2701d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) 2702d71ae5a4SJacob Faibussowitsch { 270382f516ccSBarry Smith MPI_Comm comm; 2704e6ccafaeSMatthew G Knepley PetscBool posOrient = PETSC_FALSE; 2705e6ccafaeSMatthew G Knepley const PetscInt debug = 0; 2706e6ccafaeSMatthew G Knepley PetscInt cellDim, faceSize, f; 2707e6ccafaeSMatthew G Knepley 270882f516ccSBarry Smith PetscFunctionBegin; 27099566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 27109566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &cellDim)); 271163a3b9bcSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners)); 2712e6ccafaeSMatthew G Knepley 2713ddeab2a6SMatthew G. Knepley if (cellDim == 1 && numCorners == 2) { 2714ddeab2a6SMatthew G. Knepley /* Triangle */ 2715e6ccafaeSMatthew G Knepley faceSize = numCorners - 1; 2716e6ccafaeSMatthew G Knepley posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE; 2717ddeab2a6SMatthew G. Knepley } else if (cellDim == 2 && numCorners == 3) { 2718ddeab2a6SMatthew G. Knepley /* Triangle */ 2719ddeab2a6SMatthew G. Knepley faceSize = numCorners - 1; 2720ddeab2a6SMatthew G. Knepley posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE; 2721ddeab2a6SMatthew G. Knepley } else if (cellDim == 3 && numCorners == 4) { 2722ddeab2a6SMatthew G. Knepley /* Tetrahedron */ 2723ddeab2a6SMatthew G. Knepley faceSize = numCorners - 1; 2724ddeab2a6SMatthew G. Knepley posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE; 2725e6ccafaeSMatthew G Knepley } else if (cellDim == 1 && numCorners == 3) { 2726e6ccafaeSMatthew G Knepley /* Quadratic line */ 2727e6ccafaeSMatthew G Knepley faceSize = 1; 2728e6ccafaeSMatthew G Knepley posOrient = PETSC_TRUE; 2729e6ccafaeSMatthew G Knepley } else if (cellDim == 2 && numCorners == 4) { 2730e6ccafaeSMatthew G Knepley /* Quads */ 2731e6ccafaeSMatthew G Knepley faceSize = 2; 2732e6ccafaeSMatthew G Knepley if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) { 2733e6ccafaeSMatthew G Knepley posOrient = PETSC_TRUE; 2734e6ccafaeSMatthew G Knepley } else if ((indices[0] == 3) && (indices[1] == 0)) { 2735e6ccafaeSMatthew G Knepley posOrient = PETSC_TRUE; 2736e6ccafaeSMatthew G Knepley } else { 2737e6ccafaeSMatthew G Knepley if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) { 2738e6ccafaeSMatthew G Knepley posOrient = PETSC_FALSE; 2739e6ccafaeSMatthew G Knepley } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge"); 2740e6ccafaeSMatthew G Knepley } 2741e6ccafaeSMatthew G Knepley } else if (cellDim == 2 && numCorners == 6) { 2742e6ccafaeSMatthew G Knepley /* Quadratic triangle (I hate this) */ 2743e6ccafaeSMatthew G Knepley /* Edges are determined by the first 2 vertices (corners of edges) */ 2744e6ccafaeSMatthew G Knepley const PetscInt faceSizeTri = 3; 2745e6ccafaeSMatthew G Knepley PetscInt sortedIndices[3], i, iFace; 2746e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 2747e6ccafaeSMatthew G Knepley PetscInt faceVerticesTriSorted[9] = { 2748e6ccafaeSMatthew G Knepley 0, 3, 4, /* bottom */ 2749e6ccafaeSMatthew G Knepley 1, 4, 5, /* right */ 2750e6ccafaeSMatthew G Knepley 2, 3, 5, /* left */ 2751e6ccafaeSMatthew G Knepley }; 2752e6ccafaeSMatthew G Knepley PetscInt faceVerticesTri[9] = { 2753e6ccafaeSMatthew G Knepley 0, 3, 4, /* bottom */ 2754e6ccafaeSMatthew G Knepley 1, 4, 5, /* right */ 2755e6ccafaeSMatthew G Knepley 2, 5, 3, /* left */ 2756e6ccafaeSMatthew G Knepley }; 2757e6ccafaeSMatthew G Knepley 2758e6ccafaeSMatthew G Knepley for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i]; 27599566063dSJacob Faibussowitsch PetscCall(PetscSortInt(faceSizeTri, sortedIndices)); 2760e6ccafaeSMatthew G Knepley for (iFace = 0; iFace < 3; ++iFace) { 2761e6ccafaeSMatthew G Knepley const PetscInt ii = iFace * faceSizeTri; 2762e6ccafaeSMatthew G Knepley PetscInt fVertex, cVertex; 2763e6ccafaeSMatthew G Knepley 27649371c9d4SSatish Balay if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) { 2765e6ccafaeSMatthew G Knepley for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) { 2766e6ccafaeSMatthew G Knepley for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) { 2767e6ccafaeSMatthew G Knepley if (indices[cVertex] == faceVerticesTri[ii + fVertex]) { 2768e6ccafaeSMatthew G Knepley faceVertices[fVertex] = origVertices[cVertex]; 2769e6ccafaeSMatthew G Knepley break; 2770e6ccafaeSMatthew G Knepley } 2771e6ccafaeSMatthew G Knepley } 2772e6ccafaeSMatthew G Knepley } 2773e6ccafaeSMatthew G Knepley found = PETSC_TRUE; 2774e6ccafaeSMatthew G Knepley break; 2775e6ccafaeSMatthew G Knepley } 2776e6ccafaeSMatthew G Knepley } 277728b400f6SJacob Faibussowitsch PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface"); 2778e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = PETSC_TRUE; 27793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2780e6ccafaeSMatthew G Knepley } else if (cellDim == 2 && numCorners == 9) { 2781e6ccafaeSMatthew G Knepley /* Quadratic quad (I hate this) */ 2782e6ccafaeSMatthew G Knepley /* Edges are determined by the first 2 vertices (corners of edges) */ 2783e6ccafaeSMatthew G Knepley const PetscInt faceSizeQuad = 3; 2784e6ccafaeSMatthew G Knepley PetscInt sortedIndices[3], i, iFace; 2785e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 2786e6ccafaeSMatthew G Knepley PetscInt faceVerticesQuadSorted[12] = { 2787e6ccafaeSMatthew G Knepley 0, 1, 4, /* bottom */ 2788e6ccafaeSMatthew G Knepley 1, 2, 5, /* right */ 2789e6ccafaeSMatthew G Knepley 2, 3, 6, /* top */ 2790e6ccafaeSMatthew G Knepley 0, 3, 7, /* left */ 2791e6ccafaeSMatthew G Knepley }; 2792e6ccafaeSMatthew G Knepley PetscInt faceVerticesQuad[12] = { 2793e6ccafaeSMatthew G Knepley 0, 1, 4, /* bottom */ 2794e6ccafaeSMatthew G Knepley 1, 2, 5, /* right */ 2795e6ccafaeSMatthew G Knepley 2, 3, 6, /* top */ 2796e6ccafaeSMatthew G Knepley 3, 0, 7, /* left */ 2797e6ccafaeSMatthew G Knepley }; 2798e6ccafaeSMatthew G Knepley 2799e6ccafaeSMatthew G Knepley for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i]; 28009566063dSJacob Faibussowitsch PetscCall(PetscSortInt(faceSizeQuad, sortedIndices)); 2801e6ccafaeSMatthew G Knepley for (iFace = 0; iFace < 4; ++iFace) { 2802e6ccafaeSMatthew G Knepley const PetscInt ii = iFace * faceSizeQuad; 2803e6ccafaeSMatthew G Knepley PetscInt fVertex, cVertex; 2804e6ccafaeSMatthew G Knepley 28059371c9d4SSatish Balay if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) { 2806e6ccafaeSMatthew G Knepley for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) { 2807e6ccafaeSMatthew G Knepley for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) { 2808e6ccafaeSMatthew G Knepley if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) { 2809e6ccafaeSMatthew G Knepley faceVertices[fVertex] = origVertices[cVertex]; 2810e6ccafaeSMatthew G Knepley break; 2811e6ccafaeSMatthew G Knepley } 2812e6ccafaeSMatthew G Knepley } 2813e6ccafaeSMatthew G Knepley } 2814e6ccafaeSMatthew G Knepley found = PETSC_TRUE; 2815e6ccafaeSMatthew G Knepley break; 2816e6ccafaeSMatthew G Knepley } 2817e6ccafaeSMatthew G Knepley } 281828b400f6SJacob Faibussowitsch PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface"); 2819e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = PETSC_TRUE; 28203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2821e6ccafaeSMatthew G Knepley } else if (cellDim == 3 && numCorners == 8) { 2822e6ccafaeSMatthew G Knepley /* Hexes 2823e6ccafaeSMatthew G Knepley A hex is two oriented quads with the normal of the first 2824e6ccafaeSMatthew G Knepley pointing up at the second. 2825e6ccafaeSMatthew G Knepley 2826e6ccafaeSMatthew G Knepley 7---6 2827e6ccafaeSMatthew G Knepley /| /| 2828e6ccafaeSMatthew G Knepley 4---5 | 2829ddeab2a6SMatthew G. Knepley | 1-|-2 2830e6ccafaeSMatthew G Knepley |/ |/ 2831ddeab2a6SMatthew G. Knepley 0---3 2832e6ccafaeSMatthew G Knepley 2833e6ccafaeSMatthew G Knepley Faces are determined by the first 4 vertices (corners of faces) */ 2834e6ccafaeSMatthew G Knepley const PetscInt faceSizeHex = 4; 2835e6ccafaeSMatthew G Knepley PetscInt sortedIndices[4], i, iFace; 2836e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 2837e6ccafaeSMatthew G Knepley PetscInt faceVerticesHexSorted[24] = { 2838e6ccafaeSMatthew G Knepley 0, 1, 2, 3, /* bottom */ 2839e6ccafaeSMatthew G Knepley 4, 5, 6, 7, /* top */ 2840ddeab2a6SMatthew G. Knepley 0, 3, 4, 5, /* front */ 2841ddeab2a6SMatthew G. Knepley 2, 3, 5, 6, /* right */ 2842ddeab2a6SMatthew G. Knepley 1, 2, 6, 7, /* back */ 2843ddeab2a6SMatthew G. Knepley 0, 1, 4, 7, /* left */ 2844e6ccafaeSMatthew G Knepley }; 2845e6ccafaeSMatthew G Knepley PetscInt faceVerticesHex[24] = { 2846ddeab2a6SMatthew G. Knepley 1, 2, 3, 0, /* bottom */ 2847e6ccafaeSMatthew G Knepley 4, 5, 6, 7, /* top */ 2848ddeab2a6SMatthew G. Knepley 0, 3, 5, 4, /* front */ 2849ddeab2a6SMatthew G. Knepley 3, 2, 6, 5, /* right */ 2850ddeab2a6SMatthew G. Knepley 2, 1, 7, 6, /* back */ 2851ddeab2a6SMatthew G. Knepley 1, 0, 4, 7, /* left */ 2852e6ccafaeSMatthew G Knepley }; 2853e6ccafaeSMatthew G Knepley 2854e6ccafaeSMatthew G Knepley for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i]; 28559566063dSJacob Faibussowitsch PetscCall(PetscSortInt(faceSizeHex, sortedIndices)); 2856e6ccafaeSMatthew G Knepley for (iFace = 0; iFace < 6; ++iFace) { 2857e6ccafaeSMatthew G Knepley const PetscInt ii = iFace * faceSizeHex; 2858e6ccafaeSMatthew G Knepley PetscInt fVertex, cVertex; 2859e6ccafaeSMatthew G Knepley 28609371c9d4SSatish Balay if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) { 2861e6ccafaeSMatthew G Knepley for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) { 2862e6ccafaeSMatthew G Knepley for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) { 2863e6ccafaeSMatthew G Knepley if (indices[cVertex] == faceVerticesHex[ii + fVertex]) { 2864e6ccafaeSMatthew G Knepley faceVertices[fVertex] = origVertices[cVertex]; 2865e6ccafaeSMatthew G Knepley break; 2866e6ccafaeSMatthew G Knepley } 2867e6ccafaeSMatthew G Knepley } 2868e6ccafaeSMatthew G Knepley } 2869e6ccafaeSMatthew G Knepley found = PETSC_TRUE; 2870e6ccafaeSMatthew G Knepley break; 2871e6ccafaeSMatthew G Knepley } 2872e6ccafaeSMatthew G Knepley } 287328b400f6SJacob Faibussowitsch PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface"); 2874e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = PETSC_TRUE; 28753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2876e6ccafaeSMatthew G Knepley } else if (cellDim == 3 && numCorners == 10) { 2877e6ccafaeSMatthew G Knepley /* Quadratic tet */ 2878e6ccafaeSMatthew G Knepley /* Faces are determined by the first 3 vertices (corners of faces) */ 2879e6ccafaeSMatthew G Knepley const PetscInt faceSizeTet = 6; 2880e6ccafaeSMatthew G Knepley PetscInt sortedIndices[6], i, iFace; 2881e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 2882e6ccafaeSMatthew G Knepley PetscInt faceVerticesTetSorted[24] = { 2883e6ccafaeSMatthew G Knepley 0, 1, 2, 6, 7, 8, /* bottom */ 2884e6ccafaeSMatthew G Knepley 0, 3, 4, 6, 7, 9, /* front */ 2885e6ccafaeSMatthew G Knepley 1, 4, 5, 7, 8, 9, /* right */ 2886e6ccafaeSMatthew G Knepley 2, 3, 5, 6, 8, 9, /* left */ 2887e6ccafaeSMatthew G Knepley }; 2888e6ccafaeSMatthew G Knepley PetscInt faceVerticesTet[24] = { 2889e6ccafaeSMatthew G Knepley 0, 1, 2, 6, 7, 8, /* bottom */ 2890e6ccafaeSMatthew G Knepley 0, 4, 3, 6, 7, 9, /* front */ 2891e6ccafaeSMatthew G Knepley 1, 5, 4, 7, 8, 9, /* right */ 2892e6ccafaeSMatthew G Knepley 2, 3, 5, 8, 6, 9, /* left */ 2893e6ccafaeSMatthew G Knepley }; 2894e6ccafaeSMatthew G Knepley 2895e6ccafaeSMatthew G Knepley for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i]; 28969566063dSJacob Faibussowitsch PetscCall(PetscSortInt(faceSizeTet, sortedIndices)); 2897e6ccafaeSMatthew G Knepley for (iFace = 0; iFace < 4; ++iFace) { 2898e6ccafaeSMatthew G Knepley const PetscInt ii = iFace * faceSizeTet; 2899e6ccafaeSMatthew G Knepley PetscInt fVertex, cVertex; 2900e6ccafaeSMatthew G Knepley 29019371c9d4SSatish Balay if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) { 2902e6ccafaeSMatthew G Knepley for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) { 2903e6ccafaeSMatthew G Knepley for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) { 2904e6ccafaeSMatthew G Knepley if (indices[cVertex] == faceVerticesTet[ii + fVertex]) { 2905e6ccafaeSMatthew G Knepley faceVertices[fVertex] = origVertices[cVertex]; 2906e6ccafaeSMatthew G Knepley break; 2907e6ccafaeSMatthew G Knepley } 2908e6ccafaeSMatthew G Knepley } 2909e6ccafaeSMatthew G Knepley } 2910e6ccafaeSMatthew G Knepley found = PETSC_TRUE; 2911e6ccafaeSMatthew G Knepley break; 2912e6ccafaeSMatthew G Knepley } 2913e6ccafaeSMatthew G Knepley } 291428b400f6SJacob Faibussowitsch PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface"); 2915e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = PETSC_TRUE; 29163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2917e6ccafaeSMatthew G Knepley } else if (cellDim == 3 && numCorners == 27) { 2918e6ccafaeSMatthew G Knepley /* Quadratic hexes (I hate this) 2919e6ccafaeSMatthew G Knepley A hex is two oriented quads with the normal of the first 2920e6ccafaeSMatthew G Knepley pointing up at the second. 2921e6ccafaeSMatthew G Knepley 2922e6ccafaeSMatthew G Knepley 7---6 2923e6ccafaeSMatthew G Knepley /| /| 2924e6ccafaeSMatthew G Knepley 4---5 | 2925e6ccafaeSMatthew G Knepley | 3-|-2 2926e6ccafaeSMatthew G Knepley |/ |/ 2927e6ccafaeSMatthew G Knepley 0---1 2928e6ccafaeSMatthew G Knepley 2929e6ccafaeSMatthew G Knepley Faces are determined by the first 4 vertices (corners of faces) */ 2930e6ccafaeSMatthew G Knepley const PetscInt faceSizeQuadHex = 9; 2931e6ccafaeSMatthew G Knepley PetscInt sortedIndices[9], i, iFace; 2932e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 2933e6ccafaeSMatthew G Knepley PetscInt faceVerticesQuadHexSorted[54] = { 2934e6ccafaeSMatthew G Knepley 0, 1, 2, 3, 8, 9, 10, 11, 24, /* bottom */ 2935e6ccafaeSMatthew G Knepley 4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */ 2936e6ccafaeSMatthew G Knepley 0, 1, 4, 5, 8, 12, 16, 17, 22, /* front */ 2937e6ccafaeSMatthew G Knepley 1, 2, 5, 6, 9, 13, 17, 18, 21, /* right */ 2938e6ccafaeSMatthew G Knepley 2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */ 2939e6ccafaeSMatthew G Knepley 0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */ 2940e6ccafaeSMatthew G Knepley }; 2941e6ccafaeSMatthew G Knepley PetscInt faceVerticesQuadHex[54] = { 2942e6ccafaeSMatthew G Knepley 3, 2, 1, 0, 10, 9, 8, 11, 24, /* bottom */ 2943e6ccafaeSMatthew G Knepley 4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */ 2944e6ccafaeSMatthew G Knepley 0, 1, 5, 4, 8, 17, 12, 16, 22, /* front */ 2945e6ccafaeSMatthew G Knepley 1, 2, 6, 5, 9, 18, 13, 17, 21, /* right */ 2946e6ccafaeSMatthew G Knepley 2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */ 2947e6ccafaeSMatthew G Knepley 3, 0, 4, 7, 11, 16, 15, 19, 20 /* left */ 2948e6ccafaeSMatthew G Knepley }; 2949e6ccafaeSMatthew G Knepley 2950e6ccafaeSMatthew G Knepley for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i]; 29519566063dSJacob Faibussowitsch PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices)); 2952e6ccafaeSMatthew G Knepley for (iFace = 0; iFace < 6; ++iFace) { 2953e6ccafaeSMatthew G Knepley const PetscInt ii = iFace * faceSizeQuadHex; 2954e6ccafaeSMatthew G Knepley PetscInt fVertex, cVertex; 2955e6ccafaeSMatthew G Knepley 29569371c9d4SSatish Balay if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) { 2957e6ccafaeSMatthew G Knepley for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) { 2958e6ccafaeSMatthew G Knepley for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) { 2959e6ccafaeSMatthew G Knepley if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) { 2960e6ccafaeSMatthew G Knepley faceVertices[fVertex] = origVertices[cVertex]; 2961e6ccafaeSMatthew G Knepley break; 2962e6ccafaeSMatthew G Knepley } 2963e6ccafaeSMatthew G Knepley } 2964e6ccafaeSMatthew G Knepley } 2965e6ccafaeSMatthew G Knepley found = PETSC_TRUE; 2966e6ccafaeSMatthew G Knepley break; 2967e6ccafaeSMatthew G Knepley } 2968e6ccafaeSMatthew G Knepley } 296928b400f6SJacob Faibussowitsch PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface"); 2970e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = PETSC_TRUE; 29713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2972e6ccafaeSMatthew G Knepley } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation()."); 2973e6ccafaeSMatthew G Knepley if (!posOrient) { 29749566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(comm, " Reversing initial face orientation\n")); 2975e6ccafaeSMatthew G Knepley for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f]; 2976e6ccafaeSMatthew G Knepley } else { 29779566063dSJacob Faibussowitsch if (debug) PetscCall(PetscPrintf(comm, " Keeping initial face orientation\n")); 2978e6ccafaeSMatthew G Knepley for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f]; 2979e6ccafaeSMatthew G Knepley } 2980e6ccafaeSMatthew G Knepley if (posOriented) *posOriented = posOrient; 29813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2982e6ccafaeSMatthew G Knepley } 2983e6ccafaeSMatthew G Knepley 2984c08575a3SMatthew G. Knepley /*@ 2985c08575a3SMatthew G. Knepley DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices, 2986c08575a3SMatthew G. Knepley in faceVertices. The orientation is such that the face normal points out of the cell 2987c08575a3SMatthew G. Knepley 298820f4b53cSBarry Smith Not Collective 2989c08575a3SMatthew G. Knepley 2990c08575a3SMatthew G. Knepley Input Parameters: 2991c08575a3SMatthew G. Knepley + dm - The original mesh 2992c08575a3SMatthew G. Knepley . cell - The cell mesh point 2993c08575a3SMatthew G. Knepley . faceSize - The number of vertices on the face 2994c08575a3SMatthew G. Knepley . face - The face vertices 2995c08575a3SMatthew G. Knepley . numCorners - The number of vertices on the cell 2996c08575a3SMatthew G. Knepley . indices - Local numbering of face vertices in cell cone 2997c08575a3SMatthew G. Knepley - origVertices - Original face vertices 2998c08575a3SMatthew G. Knepley 2999d8d19677SJose E. Roman Output Parameters: 3000c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented 300120f4b53cSBarry Smith - posOriented - `PETSC_TRUE` if the face was oriented with outward normal 3002c08575a3SMatthew G. Knepley 3003c08575a3SMatthew G. Knepley Level: developer 3004c08575a3SMatthew G. Knepley 30051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetCone()` 3006c08575a3SMatthew G. Knepley @*/ 3007d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) 3008d71ae5a4SJacob Faibussowitsch { 30090298fd71SBarry Smith const PetscInt *cone = NULL; 3010e6ccafaeSMatthew G Knepley PetscInt coneSize, v, f, v2; 3011e6ccafaeSMatthew G Knepley PetscInt oppositeVertex = -1; 3012e6ccafaeSMatthew G Knepley 3013e6ccafaeSMatthew G Knepley PetscFunctionBegin; 30149566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 30159566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, cell, &cone)); 3016e6ccafaeSMatthew G Knepley for (v = 0, v2 = 0; v < coneSize; ++v) { 3017e6ccafaeSMatthew G Knepley PetscBool found = PETSC_FALSE; 3018e6ccafaeSMatthew G Knepley 3019e6ccafaeSMatthew G Knepley for (f = 0; f < faceSize; ++f) { 3020e6ccafaeSMatthew G Knepley if (face[f] == cone[v]) { 30219371c9d4SSatish Balay found = PETSC_TRUE; 30229371c9d4SSatish Balay break; 3023e6ccafaeSMatthew G Knepley } 3024e6ccafaeSMatthew G Knepley } 3025e6ccafaeSMatthew G Knepley if (found) { 3026e6ccafaeSMatthew G Knepley indices[v2] = v; 3027e6ccafaeSMatthew G Knepley origVertices[v2] = cone[v]; 3028e6ccafaeSMatthew G Knepley ++v2; 3029e6ccafaeSMatthew G Knepley } else { 3030e6ccafaeSMatthew G Knepley oppositeVertex = v; 3031e6ccafaeSMatthew G Knepley } 3032e6ccafaeSMatthew G Knepley } 30339566063dSJacob Faibussowitsch PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented)); 30343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3035e6ccafaeSMatthew G Knepley } 3036e6ccafaeSMatthew G Knepley 3037e6ccafaeSMatthew G Knepley /* 3038cd0c2139SMatthew G Knepley DMPlexInsertFace_Internal - Puts a face into the mesh 3039e6ccafaeSMatthew G Knepley 304020f4b53cSBarry Smith Not Collective 3041e6ccafaeSMatthew G Knepley 3042e6ccafaeSMatthew G Knepley Input Parameters: 304320f4b53cSBarry Smith + dm - The `DMPLEX` 3044e6ccafaeSMatthew G Knepley . numFaceVertex - The number of vertices in the face 304520f4b53cSBarry Smith . faceVertices - The vertices in the face for `dm` 3046e6ccafaeSMatthew G Knepley . subfaceVertices - The vertices in the face for subdm 304720f4b53cSBarry Smith . numCorners - The number of vertices in the `cell` 304820f4b53cSBarry Smith . cell - A cell in `dm` containing the face 3049e6ccafaeSMatthew G Knepley . subcell - A cell in subdm containing the face 3050e6ccafaeSMatthew G Knepley . firstFace - First face in the mesh 3051e6ccafaeSMatthew G Knepley - newFacePoint - Next face in the mesh 3052e6ccafaeSMatthew G Knepley 30532fe279fdSBarry Smith Output Parameter: 3054e6ccafaeSMatthew G Knepley . newFacePoint - Contains next face point number on input, updated on output 3055e6ccafaeSMatthew G Knepley 3056e6ccafaeSMatthew G Knepley Level: developer 3057e6ccafaeSMatthew G Knepley */ 3058d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexInsertFace_Internal(DM dm, DM subdm, PetscInt numFaceVertices, const PetscInt faceVertices[], const PetscInt subfaceVertices[], PetscInt numCorners, PetscInt cell, PetscInt subcell, PetscInt firstFace, PetscInt *newFacePoint) 3059d71ae5a4SJacob Faibussowitsch { 306082f516ccSBarry Smith MPI_Comm comm; 3061e6ccafaeSMatthew G Knepley DM_Plex *submesh = (DM_Plex *)subdm->data; 3062e6ccafaeSMatthew G Knepley const PetscInt *faces; 3063e6ccafaeSMatthew G Knepley PetscInt numFaces, coneSize; 3064e6ccafaeSMatthew G Knepley 3065e6ccafaeSMatthew G Knepley PetscFunctionBegin; 30669566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 30679566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize)); 306863a3b9bcSJacob Faibussowitsch PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize); 3069e6ccafaeSMatthew G Knepley #if 0 3070e6ccafaeSMatthew G Knepley /* Cannot use this because support() has not been constructed yet */ 30719566063dSJacob Faibussowitsch PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces)); 3072e6ccafaeSMatthew G Knepley #else 3073e6ccafaeSMatthew G Knepley { 3074e6ccafaeSMatthew G Knepley PetscInt f; 3075e6ccafaeSMatthew G Knepley 3076e6ccafaeSMatthew G Knepley numFaces = 0; 30779566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces)); 3078e6ccafaeSMatthew G Knepley for (f = firstFace; f < *newFacePoint; ++f) { 3079e6ccafaeSMatthew G Knepley PetscInt dof, off, d; 3080e6ccafaeSMatthew G Knepley 30819566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof)); 30829566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off)); 3083e6ccafaeSMatthew G Knepley /* Yes, I know this is quadratic, but I expect the sizes to be <5 */ 3084e6ccafaeSMatthew G Knepley for (d = 0; d < dof; ++d) { 3085e6ccafaeSMatthew G Knepley const PetscInt p = submesh->cones[off + d]; 3086e6ccafaeSMatthew G Knepley PetscInt v; 3087e6ccafaeSMatthew G Knepley 3088e6ccafaeSMatthew G Knepley for (v = 0; v < numFaceVertices; ++v) { 3089e6ccafaeSMatthew G Knepley if (subfaceVertices[v] == p) break; 3090e6ccafaeSMatthew G Knepley } 3091e6ccafaeSMatthew G Knepley if (v == numFaceVertices) break; 3092e6ccafaeSMatthew G Knepley } 3093e6ccafaeSMatthew G Knepley if (d == dof) { 3094e6ccafaeSMatthew G Knepley numFaces = 1; 3095e6ccafaeSMatthew G Knepley ((PetscInt *)faces)[0] = f; 3096e6ccafaeSMatthew G Knepley } 3097e6ccafaeSMatthew G Knepley } 3098e6ccafaeSMatthew G Knepley } 3099e6ccafaeSMatthew G Knepley #endif 310063a3b9bcSJacob Faibussowitsch PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces); 3101f7d195e4SLawrence Mitchell if (numFaces == 1) { 3102e6ccafaeSMatthew G Knepley /* Add the other cell neighbor for this face */ 31039566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, subcell, faces)); 3104e6ccafaeSMatthew G Knepley } else { 3105e6ccafaeSMatthew G Knepley PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov; 3106e6ccafaeSMatthew G Knepley PetscBool posOriented; 3107e6ccafaeSMatthew G Knepley 31089566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices)); 3109e6ccafaeSMatthew G Knepley origVertices = &orientedVertices[numFaceVertices]; 3110e6ccafaeSMatthew G Knepley indices = &orientedVertices[numFaceVertices * 2]; 3111e6ccafaeSMatthew G Knepley orientedSubVertices = &orientedVertices[numFaceVertices * 3]; 31129566063dSJacob Faibussowitsch PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented)); 3113e6ccafaeSMatthew G Knepley /* TODO: I know that routine should return a permutation, not the indices */ 3114e6ccafaeSMatthew G Knepley for (v = 0; v < numFaceVertices; ++v) { 3115e6ccafaeSMatthew G Knepley const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v]; 3116e6ccafaeSMatthew G Knepley for (ov = 0; ov < numFaceVertices; ++ov) { 3117e6ccafaeSMatthew G Knepley if (orientedVertices[ov] == vertex) { 3118e6ccafaeSMatthew G Knepley orientedSubVertices[ov] = subvertex; 3119e6ccafaeSMatthew G Knepley break; 3120e6ccafaeSMatthew G Knepley } 3121e6ccafaeSMatthew G Knepley } 312263a3b9bcSJacob Faibussowitsch PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex); 3123e6ccafaeSMatthew G Knepley } 31249566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices)); 31259566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint)); 31269566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices)); 3127e6ccafaeSMatthew G Knepley ++(*newFacePoint); 3128e6ccafaeSMatthew G Knepley } 3129ef07cca7SMatthew G. Knepley #if 0 31309566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces)); 3131ef07cca7SMatthew G. Knepley #else 31329566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces)); 3133ef07cca7SMatthew G. Knepley #endif 31343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3135e6ccafaeSMatthew G Knepley } 3136e6ccafaeSMatthew G Knepley 3137d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm) 3138d71ae5a4SJacob Faibussowitsch { 313982f516ccSBarry Smith MPI_Comm comm; 314053156dfcSMatthew G. Knepley DMLabel subpointMap; 3141efa14ee0SMatthew G Knepley IS subvertexIS, subcellIS; 3142efa14ee0SMatthew G Knepley const PetscInt *subVertices, *subCells; 3143efa14ee0SMatthew G Knepley PetscInt numSubVertices, firstSubVertex, numSubCells; 3144fed694aaSMatthew G. Knepley PetscInt *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0; 3145efa14ee0SMatthew G Knepley PetscInt vStart, vEnd, c, f; 3146e6ccafaeSMatthew G Knepley 3147e6ccafaeSMatthew G Knepley PetscFunctionBegin; 31489566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 3149efa14ee0SMatthew G Knepley /* Create subpointMap which marks the submesh */ 31509566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap)); 31519566063dSJacob Faibussowitsch PetscCall(DMPlexSetSubpointMap(subdm, subpointMap)); 31529566063dSJacob Faibussowitsch if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm)); 3153efa14ee0SMatthew G Knepley /* Setup chart */ 31549566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices)); 31559566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells)); 31569566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices)); 31579566063dSJacob Faibussowitsch PetscCall(DMPlexSetVTKCellHeight(subdm, 1)); 3158e6ccafaeSMatthew G Knepley /* Set cone sizes */ 3159e6ccafaeSMatthew G Knepley firstSubVertex = numSubCells; 3160efa14ee0SMatthew G Knepley firstSubFace = numSubCells + numSubVertices; 3161e6ccafaeSMatthew G Knepley newFacePoint = firstSubFace; 31629566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS)); 31639566063dSJacob Faibussowitsch if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices)); 31649566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS)); 31659566063dSJacob Faibussowitsch if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells)); 316648a46eb9SPierre Jolivet for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1)); 316748a46eb9SPierre Jolivet for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV)); 31689566063dSJacob Faibussowitsch PetscCall(DMSetUp(subdm)); 3169d24a712aSStefano Zampini PetscCall(DMLabelDestroy(&subpointMap)); 3170e6ccafaeSMatthew G Knepley /* Create face cones */ 31719566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 31729566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL)); 31739566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface)); 3174e6ccafaeSMatthew G Knepley for (c = 0; c < numSubCells; ++c) { 3175e6ccafaeSMatthew G Knepley const PetscInt cell = subCells[c]; 3176efa14ee0SMatthew G Knepley const PetscInt subcell = c; 31770298fd71SBarry Smith PetscInt *closure = NULL; 3178efa14ee0SMatthew G Knepley PetscInt closureSize, cl, numCorners = 0, faceSize = 0; 3179e6ccafaeSMatthew G Knepley 31809566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 3181efa14ee0SMatthew G Knepley for (cl = 0; cl < closureSize * 2; cl += 2) { 3182efa14ee0SMatthew G Knepley const PetscInt point = closure[cl]; 3183e6ccafaeSMatthew G Knepley PetscInt subVertex; 3184e6ccafaeSMatthew G Knepley 3185efa14ee0SMatthew G Knepley if ((point >= vStart) && (point < vEnd)) { 3186efa14ee0SMatthew G Knepley ++numCorners; 31879566063dSJacob Faibussowitsch PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex)); 3188efa14ee0SMatthew G Knepley if (subVertex >= 0) { 3189efa14ee0SMatthew G Knepley closure[faceSize] = point; 319065560c7fSMatthew G Knepley subface[faceSize] = firstSubVertex + subVertex; 3191e6ccafaeSMatthew G Knepley ++faceSize; 3192e6ccafaeSMatthew G Knepley } 3193e6ccafaeSMatthew G Knepley } 3194e6ccafaeSMatthew G Knepley } 319563a3b9bcSJacob Faibussowitsch PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize); 319648a46eb9SPierre Jolivet if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint)); 31979566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure)); 3198e6ccafaeSMatthew G Knepley } 31999566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface)); 32009566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(subdm)); 32019566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(subdm)); 3202e6ccafaeSMatthew G Knepley /* Build coordinates */ 3203efa14ee0SMatthew G Knepley { 3204efa14ee0SMatthew G Knepley PetscSection coordSection, subCoordSection; 3205efa14ee0SMatthew G Knepley Vec coordinates, subCoordinates; 3206efa14ee0SMatthew G Knepley PetscScalar *coords, *subCoords; 3207285d324eSMatthew G. Knepley PetscInt numComp, coordSize, v; 320824640c55SToby Isaac const char *name; 3209efa14ee0SMatthew G Knepley 32109566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 32119566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 32129566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(subdm, &subCoordSection)); 32139566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(subCoordSection, 1)); 32149566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp)); 32159566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp)); 32169566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices)); 3217efa14ee0SMatthew G Knepley for (v = 0; v < numSubVertices; ++v) { 3218efa14ee0SMatthew G Knepley const PetscInt vertex = subVertices[v]; 3219efa14ee0SMatthew G Knepley const PetscInt subvertex = firstSubVertex + v; 3220efa14ee0SMatthew G Knepley PetscInt dof; 3221efa14ee0SMatthew G Knepley 32229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 32239566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof)); 32249566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof)); 3225e6ccafaeSMatthew G Knepley } 32269566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(subCoordSection)); 32279566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize)); 32289566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates)); 32299566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)coordinates, &name)); 32309566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name)); 32319566063dSJacob Faibussowitsch PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE)); 32329566063dSJacob Faibussowitsch PetscCall(VecSetType(subCoordinates, VECSTANDARD)); 3233830e53efSMatthew G. Knepley if (coordSize) { 32349566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 32359566063dSJacob Faibussowitsch PetscCall(VecGetArray(subCoordinates, &subCoords)); 3236efa14ee0SMatthew G Knepley for (v = 0; v < numSubVertices; ++v) { 3237efa14ee0SMatthew G Knepley const PetscInt vertex = subVertices[v]; 3238efa14ee0SMatthew G Knepley const PetscInt subvertex = firstSubVertex + v; 3239efa14ee0SMatthew G Knepley PetscInt dof, off, sdof, soff, d; 3240e6ccafaeSMatthew G Knepley 32419566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 32429566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, vertex, &off)); 32439566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof)); 32449566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff)); 324563a3b9bcSJacob Faibussowitsch PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof); 3246e6ccafaeSMatthew G Knepley for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d]; 3247e6ccafaeSMatthew G Knepley } 32489566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 32499566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(subCoordinates, &subCoords)); 32503b399e24SMatthew G. Knepley } 32519566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates)); 32529566063dSJacob Faibussowitsch PetscCall(VecDestroy(&subCoordinates)); 3253e6ccafaeSMatthew G Knepley } 3254efa14ee0SMatthew G Knepley /* Cleanup */ 32559566063dSJacob Faibussowitsch if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices)); 32569566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subvertexIS)); 32579566063dSJacob Faibussowitsch if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells)); 32589566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subcellIS)); 32593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3260e6ccafaeSMatthew G Knepley } 3261e6ccafaeSMatthew G Knepley 3262d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */ 3263d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[]) 3264d71ae5a4SJacob Faibussowitsch { 32653982b651SMatthew G. Knepley PetscInt subPoint; 32663982b651SMatthew G. Knepley PetscErrorCode ierr; 32673982b651SMatthew G. Knepley 32689371c9d4SSatish Balay ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); 32699371c9d4SSatish Balay if (ierr) return -1; 32703982b651SMatthew G. Knepley return subPoint < 0 ? subPoint : firstSubPoint + subPoint; 32713982b651SMatthew G. Knepley } 32723982b651SMatthew G. Knepley 3273d5b43468SJose E. Roman /* TODO: Fix this to properly propagate up error conditions it may find */ 3274d71ae5a4SJacob Faibussowitsch static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[]) 3275d71ae5a4SJacob Faibussowitsch { 3276dd05d810SMatthew G. Knepley PetscInt subPoint; 3277dd05d810SMatthew G. Knepley PetscErrorCode ierr; 3278dd05d810SMatthew G. Knepley 3279dd05d810SMatthew G. Knepley ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); 3280dd05d810SMatthew G. Knepley if (ierr) return -1; 3281dd05d810SMatthew G. Knepley return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint); 3282dd05d810SMatthew G. Knepley } 3283dd05d810SMatthew G. Knepley 3284d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm) 3285d71ae5a4SJacob Faibussowitsch { 3286695799ffSMatthew G. Knepley DMLabel depthLabel; 3287212103e5SMatthew Knepley PetscInt Nl, l, d; 3288212103e5SMatthew Knepley 3289212103e5SMatthew Knepley PetscFunctionBegin; 3290695799ffSMatthew G. Knepley // Reset depth label for fast lookup 3291695799ffSMatthew G. Knepley PetscCall(DMPlexGetDepthLabel(dm, &depthLabel)); 3292695799ffSMatthew G. Knepley PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel)); 32939566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(dm, &Nl)); 3294212103e5SMatthew Knepley for (l = 0; l < Nl; ++l) { 3295212103e5SMatthew Knepley DMLabel label, newlabel; 3296212103e5SMatthew Knepley const char *lname; 3297d56405f8SMatthew G. Knepley PetscBool isDepth, isDim, isCelltype, isVTK; 3298212103e5SMatthew Knepley IS valueIS; 3299212103e5SMatthew Knepley const PetscInt *values; 3300212103e5SMatthew Knepley PetscInt Nv, v; 3301212103e5SMatthew Knepley 33029566063dSJacob Faibussowitsch PetscCall(DMGetLabelName(dm, l, &lname)); 33039566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "depth", &isDepth)); 33049566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "dim", &isDim)); 33059566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "celltype", &isCelltype)); 33069566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(lname, "vtk", &isVTK)); 3307d56405f8SMatthew G. Knepley if (isDepth || isDim || isCelltype || isVTK) continue; 33089566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(subdm, lname)); 33099566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, lname, &label)); 33109566063dSJacob Faibussowitsch PetscCall(DMGetLabel(subdm, lname, &newlabel)); 33119566063dSJacob Faibussowitsch PetscCall(DMLabelGetDefaultValue(label, &v)); 33129566063dSJacob Faibussowitsch PetscCall(DMLabelSetDefaultValue(newlabel, v)); 33139566063dSJacob Faibussowitsch PetscCall(DMLabelGetValueIS(label, &valueIS)); 33149566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(valueIS, &Nv)); 33159566063dSJacob Faibussowitsch PetscCall(ISGetIndices(valueIS, &values)); 3316212103e5SMatthew Knepley for (v = 0; v < Nv; ++v) { 3317212103e5SMatthew Knepley IS pointIS; 3318212103e5SMatthew Knepley const PetscInt *points; 3319212103e5SMatthew Knepley PetscInt Np, p; 3320212103e5SMatthew Knepley 33219566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS)); 33229566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(pointIS, &Np)); 33239566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 3324212103e5SMatthew Knepley for (p = 0; p < Np; ++p) { 3325212103e5SMatthew Knepley const PetscInt point = points[p]; 3326212103e5SMatthew Knepley PetscInt subp; 3327212103e5SMatthew Knepley 33289566063dSJacob Faibussowitsch PetscCall(DMPlexGetPointDepth(dm, point, &d)); 3329212103e5SMatthew Knepley subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]); 33309566063dSJacob Faibussowitsch if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v])); 3331212103e5SMatthew Knepley } 33329566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(pointIS, &points)); 33339566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 3334212103e5SMatthew Knepley } 33359566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(valueIS, &values)); 33369566063dSJacob Faibussowitsch PetscCall(ISDestroy(&valueIS)); 3337212103e5SMatthew Knepley } 33383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3339212103e5SMatthew Knepley } 3340212103e5SMatthew Knepley 3341*4d00ad26Sksagiyam static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, PetscBool ignoreLabelHalo, PetscSF *ownershipTransferSF, DM subdm) 3342d71ae5a4SJacob Faibussowitsch { 334382f516ccSBarry Smith MPI_Comm comm; 334453156dfcSMatthew G. Knepley DMLabel subpointMap; 3345efa14ee0SMatthew G Knepley IS *subpointIS; 3346efa14ee0SMatthew G Knepley const PetscInt **subpoints; 33473982b651SMatthew G. Knepley PetscInt *numSubPoints, *firstSubPoint, *coneNew, *orntNew; 3348dd05d810SMatthew G. Knepley PetscInt totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v; 33490d366550SMatthew G. Knepley PetscMPIInt rank; 3350e6ccafaeSMatthew G Knepley 3351e6ccafaeSMatthew G Knepley PetscFunctionBegin; 33529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 33539566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 3354efa14ee0SMatthew G Knepley /* Create subpointMap which marks the submesh */ 33559566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap)); 33569566063dSJacob Faibussowitsch PetscCall(DMPlexSetSubpointMap(subdm, subpointMap)); 3357bec263e5SMatthew G. Knepley if (cellHeight) { 33589566063dSJacob Faibussowitsch if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm)); 33599566063dSJacob Faibussowitsch else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm)); 3360bec263e5SMatthew G. Knepley } else { 3361bec263e5SMatthew G. Knepley DMLabel depth; 3362bec263e5SMatthew G. Knepley IS pointIS; 3363bec263e5SMatthew G. Knepley const PetscInt *points; 3364*4d00ad26Sksagiyam PetscInt numPoints = 0, pStart = 0; 3365*4d00ad26Sksagiyam PetscBool *isGhost = NULL; 3366bec263e5SMatthew G. Knepley 33679566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthLabel(dm, &depth)); 33689566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(label, value, &pointIS)); 3369b85c8bf9SMatthew G. Knepley if (pointIS) { 33709566063dSJacob Faibussowitsch PetscCall(ISGetIndices(pointIS, &points)); 33719566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(pointIS, &numPoints)); 3372b85c8bf9SMatthew G. Knepley } 3373*4d00ad26Sksagiyam if (ignoreLabelHalo) { 3374*4d00ad26Sksagiyam PetscSF pointSF; 3375*4d00ad26Sksagiyam PetscInt nleaves, pEnd; 3376*4d00ad26Sksagiyam const PetscInt *ilocal = NULL; 3377*4d00ad26Sksagiyam 3378*4d00ad26Sksagiyam PetscCall(DMGetPointSF(dm, &pointSF)); 3379*4d00ad26Sksagiyam PetscCall(PetscSFGetGraph(pointSF, NULL, &nleaves, &ilocal, NULL)); 3380*4d00ad26Sksagiyam PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 3381*4d00ad26Sksagiyam PetscCall(PetscMalloc1(pEnd - pStart, &isGhost)); 3382*4d00ad26Sksagiyam for (p = 0; p < pEnd - pStart; ++p) isGhost[p] = PETSC_FALSE; 3383*4d00ad26Sksagiyam for (p = 0; p < nleaves; ++p) isGhost[(ilocal ? ilocal[p] : p) - pStart] = PETSC_TRUE; 3384*4d00ad26Sksagiyam } 3385bec263e5SMatthew G. Knepley for (p = 0; p < numPoints; ++p) { 3386bec263e5SMatthew G. Knepley PetscInt *closure = NULL; 3387bec263e5SMatthew G. Knepley PetscInt closureSize, c, pdim; 3388bec263e5SMatthew G. Knepley 3389*4d00ad26Sksagiyam if (ignoreLabelHalo && isGhost[points[p] - pStart]) continue; 33909566063dSJacob Faibussowitsch PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure)); 3391bec263e5SMatthew G. Knepley for (c = 0; c < closureSize * 2; c += 2) { 33929566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(depth, closure[c], &pdim)); 33939566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim)); 3394bec263e5SMatthew G. Knepley } 33959566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure)); 3396bec263e5SMatthew G. Knepley } 3397*4d00ad26Sksagiyam PetscCall(PetscFree(isGhost)); 33989566063dSJacob Faibussowitsch if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points)); 33999566063dSJacob Faibussowitsch PetscCall(ISDestroy(&pointIS)); 3400bec263e5SMatthew G. Knepley } 3401efa14ee0SMatthew G Knepley /* Setup chart */ 34029566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 3403dd05d810SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 34049566063dSJacob Faibussowitsch PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints)); 3405e6ccafaeSMatthew G Knepley for (d = 0; d <= dim; ++d) { 34069566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d])); 3407e6ccafaeSMatthew G Knepley totSubPoints += numSubPoints[d]; 3408e6ccafaeSMatthew G Knepley } 3409dd05d810SMatthew G. Knepley // Determine submesh dimension 3410dd05d810SMatthew G. Knepley PetscCall(DMGetDimension(subdm, &sdim)); 3411dd05d810SMatthew G. Knepley if (sdim > 0) { 3412dd05d810SMatthew G. Knepley // Calling function knows what dimension to use, and we include neighboring cells as well 3413dd05d810SMatthew G. Knepley sdim = dim; 3414dd05d810SMatthew G. Knepley } else { 3415dd05d810SMatthew G. Knepley // We reset the subdimension based on what is being selected 3416dd05d810SMatthew G. Knepley PetscInt lsdim; 3417dd05d810SMatthew G. Knepley for (lsdim = dim; lsdim >= 0; --lsdim) 3418dd05d810SMatthew G. Knepley if (numSubPoints[lsdim]) break; 3419a5a714f4SStefano Zampini PetscCall(MPIU_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPI_MAX, comm)); 3420dd05d810SMatthew G. Knepley PetscCall(DMSetDimension(subdm, sdim)); 3421dd05d810SMatthew G. Knepley PetscCall(DMSetCoordinateDim(subdm, cdim)); 3422dd05d810SMatthew G. Knepley } 34239566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(subdm, 0, totSubPoints)); 34249566063dSJacob Faibussowitsch PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight)); 3425e6ccafaeSMatthew G Knepley /* Set cone sizes */ 3426dd05d810SMatthew G. Knepley firstSubPoint[sdim] = 0; 3427dd05d810SMatthew G. Knepley firstSubPoint[0] = firstSubPoint[sdim] + numSubPoints[sdim]; 3428dd05d810SMatthew G. Knepley if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0]; 3429dd05d810SMatthew G. Knepley if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1]; 3430dd05d810SMatthew G. Knepley for (d = 0; d <= sdim; ++d) { 34319566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d])); 34329566063dSJacob Faibussowitsch if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d])); 3433e6ccafaeSMatthew G Knepley } 3434412e9a14SMatthew G. Knepley /* We do not want this label automatically computed, instead we compute it here */ 34359566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(subdm, "celltype")); 3436dd05d810SMatthew G. Knepley for (d = 0; d <= sdim; ++d) { 3437e6ccafaeSMatthew G Knepley for (p = 0; p < numSubPoints[d]; ++p) { 3438e6ccafaeSMatthew G Knepley const PetscInt point = subpoints[d][p]; 3439e6ccafaeSMatthew G Knepley const PetscInt subpoint = firstSubPoint[d] + p; 3440e6ccafaeSMatthew G Knepley const PetscInt *cone; 344115100a53SVaclav Hapla PetscInt coneSize; 3442e6ccafaeSMatthew G Knepley 34439566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, point, &coneSize)); 3444dd05d810SMatthew G. Knepley if (cellHeight && (d == sdim)) { 344515100a53SVaclav Hapla PetscInt coneSizeNew, c, val; 344615100a53SVaclav Hapla 34479566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, point, &cone)); 3448e6ccafaeSMatthew G Knepley for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 34499566063dSJacob Faibussowitsch PetscCall(DMLabelGetValue(subpointMap, cone[c], &val)); 3450e6ccafaeSMatthew G Knepley if (val >= 0) coneSizeNew++; 3451e6ccafaeSMatthew G Knepley } 34529566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew)); 34539566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST)); 345415100a53SVaclav Hapla } else { 345515100a53SVaclav Hapla DMPolytopeType ct; 345615100a53SVaclav Hapla 345715100a53SVaclav Hapla PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize)); 345815100a53SVaclav Hapla PetscCall(DMPlexGetCellType(dm, point, &ct)); 345915100a53SVaclav Hapla PetscCall(DMPlexSetCellType(subdm, subpoint, ct)); 3460e6ccafaeSMatthew G Knepley } 3461e6ccafaeSMatthew G Knepley } 3462e6ccafaeSMatthew G Knepley } 34639566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&subpointMap)); 34649566063dSJacob Faibussowitsch PetscCall(DMSetUp(subdm)); 3465e6ccafaeSMatthew G Knepley /* Set cones */ 34669566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL)); 34679566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew)); 3468dd05d810SMatthew G. Knepley for (d = 0; d <= sdim; ++d) { 3469e6ccafaeSMatthew G Knepley for (p = 0; p < numSubPoints[d]; ++p) { 3470e6ccafaeSMatthew G Knepley const PetscInt point = subpoints[d][p]; 3471e6ccafaeSMatthew G Knepley const PetscInt subpoint = firstSubPoint[d] + p; 34720e49e2e2SMatthew G. Knepley const PetscInt *cone, *ornt; 34730d366550SMatthew G. Knepley PetscInt coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0; 3474e6ccafaeSMatthew G Knepley 3475dd05d810SMatthew G. Knepley if (d == sdim - 1) { 34760d366550SMatthew G. Knepley const PetscInt *support, *cone, *ornt; 34770d366550SMatthew G. Knepley PetscInt supportSize, coneSize, s, subc; 34780d366550SMatthew G. Knepley 34799566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupport(dm, point, &support)); 34809566063dSJacob Faibussowitsch PetscCall(DMPlexGetSupportSize(dm, point, &supportSize)); 34810d366550SMatthew G. Knepley for (s = 0; s < supportSize; ++s) { 3482064cae4fSPierre Jolivet PetscBool isHybrid = PETSC_FALSE; 3483412e9a14SMatthew G. Knepley 34849566063dSJacob Faibussowitsch PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid)); 3485412e9a14SMatthew G. Knepley if (!isHybrid) continue; 34869566063dSJacob Faibussowitsch PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc)); 34870d366550SMatthew G. Knepley if (subc >= 0) { 34880d366550SMatthew G. Knepley const PetscInt ccell = subpoints[d + 1][subc]; 34890d366550SMatthew G. Knepley 34909566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, ccell, &cone)); 34919566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize)); 34929566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt)); 34930d366550SMatthew G. Knepley for (c = 0; c < coneSize; ++c) { 34940d366550SMatthew G. Knepley if (cone[c] == point) { 34950d366550SMatthew G. Knepley fornt = ornt[c]; 34960d366550SMatthew G. Knepley break; 34970d366550SMatthew G. Knepley } 34980d366550SMatthew G. Knepley } 34990d366550SMatthew G. Knepley break; 35000d366550SMatthew G. Knepley } 35010d366550SMatthew G. Knepley } 35020d366550SMatthew G. Knepley } 35039566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(dm, point, &coneSize)); 35049566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize)); 35059566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, point, &cone)); 35069566063dSJacob Faibussowitsch PetscCall(DMPlexGetConeOrientation(dm, point, &ornt)); 3507e6ccafaeSMatthew G Knepley for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 35089566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc)); 350901a2673eSMatthew G. Knepley if (subc >= 0) { 351001a2673eSMatthew G. Knepley coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc; 35113982b651SMatthew G. Knepley orntNew[coneSizeNew] = ornt[c]; 351201a2673eSMatthew G. Knepley ++coneSizeNew; 351301a2673eSMatthew G. Knepley } 3514e6ccafaeSMatthew G Knepley } 351563a3b9bcSJacob Faibussowitsch PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize); 35169566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, subpoint, coneNew)); 35179566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew)); 35189566063dSJacob Faibussowitsch if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt)); 3519e6ccafaeSMatthew G Knepley } 3520e6ccafaeSMatthew G Knepley } 35219566063dSJacob Faibussowitsch PetscCall(PetscFree2(coneNew, orntNew)); 35229566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(subdm)); 35239566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(subdm)); 3524e6ccafaeSMatthew G Knepley /* Build coordinates */ 3525e6ccafaeSMatthew G Knepley { 3526e6ccafaeSMatthew G Knepley PetscSection coordSection, subCoordSection; 3527e6ccafaeSMatthew G Knepley Vec coordinates, subCoordinates; 3528e6ccafaeSMatthew G Knepley PetscScalar *coords, *subCoords; 3529c0e8cf5fSMatthew G. Knepley PetscInt cdim, numComp, coordSize; 353024640c55SToby Isaac const char *name; 3531e6ccafaeSMatthew G Knepley 35329566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 35339566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 35349566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 35359566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(subdm, &subCoordSection)); 35369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(subCoordSection, 1)); 35379566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp)); 35389566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp)); 35399566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0])); 3540e6ccafaeSMatthew G Knepley for (v = 0; v < numSubPoints[0]; ++v) { 3541e6ccafaeSMatthew G Knepley const PetscInt vertex = subpoints[0][v]; 3542e6ccafaeSMatthew G Knepley const PetscInt subvertex = firstSubPoint[0] + v; 3543e6ccafaeSMatthew G Knepley PetscInt dof; 3544e6ccafaeSMatthew G Knepley 35459566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 35469566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof)); 35479566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof)); 3548e6ccafaeSMatthew G Knepley } 35499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(subCoordSection)); 35509566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize)); 35519566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates)); 35529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)coordinates, &name)); 35539566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name)); 35549566063dSJacob Faibussowitsch PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE)); 35559566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(subCoordinates, cdim)); 35569566063dSJacob Faibussowitsch PetscCall(VecSetType(subCoordinates, VECSTANDARD)); 35579566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 35589566063dSJacob Faibussowitsch PetscCall(VecGetArray(subCoordinates, &subCoords)); 3559e6ccafaeSMatthew G Knepley for (v = 0; v < numSubPoints[0]; ++v) { 3560e6ccafaeSMatthew G Knepley const PetscInt vertex = subpoints[0][v]; 3561e6ccafaeSMatthew G Knepley const PetscInt subvertex = firstSubPoint[0] + v; 3562e6ccafaeSMatthew G Knepley PetscInt dof, off, sdof, soff, d; 3563e6ccafaeSMatthew G Knepley 35649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 35659566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, vertex, &off)); 35669566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof)); 35679566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff)); 356863a3b9bcSJacob Faibussowitsch PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof); 3569efa14ee0SMatthew G Knepley for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d]; 3570e6ccafaeSMatthew G Knepley } 35719566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 35729566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(subCoordinates, &subCoords)); 35739566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates)); 35749566063dSJacob Faibussowitsch PetscCall(VecDestroy(&subCoordinates)); 3575e6ccafaeSMatthew G Knepley } 35763982b651SMatthew G. Knepley /* Build SF: We need this complexity because subpoints might not be selected on the owning process */ 35773982b651SMatthew G. Knepley { 35783982b651SMatthew G. Knepley PetscSF sfPoint, sfPointSub; 35793982b651SMatthew G. Knepley IS subpIS; 35803982b651SMatthew G. Knepley const PetscSFNode *remotePoints; 35815033f954SMatthew G. Knepley PetscSFNode *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL; 35825033f954SMatthew G. Knepley const PetscInt *localPoints, *subpoints, *rootdegree; 3583dd05d810SMatthew G. Knepley PetscInt *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL; 35845033f954SMatthew G. Knepley PetscInt numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p; 35855033f954SMatthew G. Knepley PetscMPIInt rank, size; 35863982b651SMatthew G. Knepley 35879566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 35885033f954SMatthew G. Knepley PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size)); 35899566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 35909566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(subdm, &sfPointSub)); 35919566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 35929566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots)); 35939566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointIS(subdm, &subpIS)); 35943982b651SMatthew G. Knepley if (subpIS) { 3595dd05d810SMatthew G. Knepley PetscBool sorted = PETSC_TRUE; 3596dd05d810SMatthew G. Knepley 35979566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subpIS, &subpoints)); 35989566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(subpIS, &numSubpoints)); 3599dd05d810SMatthew G. Knepley for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE; 3600dd05d810SMatthew G. Knepley if (!sorted) { 3601dd05d810SMatthew G. Knepley PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices)); 3602dd05d810SMatthew G. Knepley for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p; 3603dd05d810SMatthew G. Knepley PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints)); 3604dd05d810SMatthew G. Knepley PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices)); 3605dd05d810SMatthew G. Knepley } 36063982b651SMatthew G. Knepley } 36079566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints)); 36083982b651SMatthew G. Knepley if (numRoots >= 0) { 36095033f954SMatthew G. Knepley PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree)); 36105033f954SMatthew G. Knepley PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree)); 36119566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners)); 36123982b651SMatthew G. Knepley for (p = 0; p < pEnd - pStart; ++p) { 36133982b651SMatthew G. Knepley newLocalPoints[p].rank = -2; 36143982b651SMatthew G. Knepley newLocalPoints[p].index = -2; 36153982b651SMatthew G. Knepley } 36163982b651SMatthew G. Knepley /* Set subleaves */ 36173982b651SMatthew G. Knepley for (l = 0; l < numLeaves; ++l) { 36183982b651SMatthew G. Knepley const PetscInt point = localPoints[l]; 3619dd05d810SMatthew G. Knepley const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices); 36203982b651SMatthew G. Knepley 36213982b651SMatthew G. Knepley if (subpoint < 0) continue; 36223982b651SMatthew G. Knepley newLocalPoints[point - pStart].rank = rank; 36233982b651SMatthew G. Knepley newLocalPoints[point - pStart].index = subpoint; 36243982b651SMatthew G. Knepley ++numSubleaves; 36253982b651SMatthew G. Knepley } 36263982b651SMatthew G. Knepley /* Must put in owned subpoints */ 36273982b651SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 36283982b651SMatthew G. Knepley newOwners[p - pStart].rank = -3; 36293982b651SMatthew G. Knepley newOwners[p - pStart].index = -3; 36303982b651SMatthew G. Knepley } 3631d9384f27SMatthew G. Knepley for (p = 0; p < numSubpoints; ++p) { 36325033f954SMatthew G. Knepley /* Hold on to currently owned points */ 36335033f954SMatthew G. Knepley if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size; 36345033f954SMatthew G. Knepley else newOwners[subpoints[p] - pStart].rank = rank; 3635d9384f27SMatthew G. Knepley newOwners[subpoints[p] - pStart].index = p; 36363982b651SMatthew G. Knepley } 36379566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC)); 36389566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC)); 36399371c9d4SSatish Balay for (p = pStart; p < pEnd; ++p) 36409371c9d4SSatish Balay if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size; 3641fdb4e32bSksagiyam if (ownershipTransferSF) { 3642fdb4e32bSksagiyam PetscSFNode *iremote1 = NULL, *newOwners1 = NULL; 3643fdb4e32bSksagiyam PetscInt *ilocal1 = NULL; 3644fdb4e32bSksagiyam PetscInt nleaves1, point; 3645fdb4e32bSksagiyam 3646fdb4e32bSksagiyam for (p = 0; p < numSubpoints; ++p) { 3647fdb4e32bSksagiyam point = subpoints[p]; 3648fdb4e32bSksagiyam newLocalPoints[point - pStart].index = point; 3649fdb4e32bSksagiyam } 3650fdb4e32bSksagiyam PetscCall(PetscMalloc1(numRoots, &newOwners1)); 3651fdb4e32bSksagiyam for (p = 0; p < numRoots; ++p) { 3652fdb4e32bSksagiyam newOwners1[p].rank = -1; 3653fdb4e32bSksagiyam newOwners1[p].index = -1; 3654fdb4e32bSksagiyam } 3655fdb4e32bSksagiyam PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners1, MPI_MAXLOC)); 3656fdb4e32bSksagiyam PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners1, MPI_MAXLOC)); 3657fdb4e32bSksagiyam for (p = 0, nleaves1 = 0; p < numRoots; ++p) { 3658fdb4e32bSksagiyam if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) { ++nleaves1; } 3659fdb4e32bSksagiyam } 3660fdb4e32bSksagiyam PetscCall(PetscMalloc1(nleaves1, &ilocal1)); 3661fdb4e32bSksagiyam PetscCall(PetscMalloc1(nleaves1, &iremote1)); 3662fdb4e32bSksagiyam for (p = 0, nleaves1 = 0; p < numRoots; ++p) { 3663fdb4e32bSksagiyam if (newOwners[p].rank >= 0 && newOwners[p].rank != rank) { 3664fdb4e32bSksagiyam ilocal1[nleaves1] = pStart + p; 3665fdb4e32bSksagiyam iremote1[nleaves1].rank = newOwners[p].rank; 3666fdb4e32bSksagiyam iremote1[nleaves1].index = newOwners1[p].index; 3667fdb4e32bSksagiyam ++nleaves1; 3668fdb4e32bSksagiyam } 3669fdb4e32bSksagiyam } 3670fdb4e32bSksagiyam PetscCall(PetscFree(newOwners1)); 3671fdb4e32bSksagiyam PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)sfPoint), ownershipTransferSF)); 3672fdb4e32bSksagiyam PetscCall(PetscSFSetFromOptions(*ownershipTransferSF)); 3673fdb4e32bSksagiyam PetscCall(PetscSFSetGraph(*ownershipTransferSF, pEnd - pStart, nleaves1, ilocal1, PETSC_OWN_POINTER, iremote1, PETSC_OWN_POINTER)); 3674fdb4e32bSksagiyam } 36759566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE)); 36769566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE)); 36779566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numSubleaves, &slocalPoints)); 36789566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numSubleaves, &sremotePoints)); 36795033f954SMatthew G. Knepley for (l = 0; l < numLeaves; ++l) { 36803982b651SMatthew G. Knepley const PetscInt point = localPoints[l]; 3681dd05d810SMatthew G. Knepley const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices); 36823982b651SMatthew G. Knepley 36833982b651SMatthew G. Knepley if (subpoint < 0) continue; 36849371c9d4SSatish Balay if (newLocalPoints[point].rank == rank) { 36859371c9d4SSatish Balay ++ll; 36869371c9d4SSatish Balay continue; 36879371c9d4SSatish Balay } 36883982b651SMatthew G. Knepley slocalPoints[sl] = subpoint; 36893982b651SMatthew G. Knepley sremotePoints[sl].rank = newLocalPoints[point].rank; 36903982b651SMatthew G. Knepley sremotePoints[sl].index = newLocalPoints[point].index; 369163a3b9bcSJacob Faibussowitsch PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point); 369263a3b9bcSJacob Faibussowitsch PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point); 36933982b651SMatthew G. Knepley ++sl; 36943982b651SMatthew G. Knepley } 36951dca8a05SBarry Smith PetscCheck(sl + ll == numSubleaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubleaves); 36969566063dSJacob Faibussowitsch PetscCall(PetscFree2(newLocalPoints, newOwners)); 36979566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER)); 36983982b651SMatthew G. Knepley } 369948a46eb9SPierre Jolivet if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints)); 3700dd05d810SMatthew G. Knepley PetscCall(PetscFree2(sortedPoints, sortedIndices)); 37013982b651SMatthew G. Knepley } 3702212103e5SMatthew Knepley /* Filter labels */ 37039566063dSJacob Faibussowitsch PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm)); 3704efa14ee0SMatthew G Knepley /* Cleanup */ 3705dd05d810SMatthew G. Knepley for (d = 0; d <= sdim; ++d) { 37069566063dSJacob Faibussowitsch if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d])); 37079566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subpointIS[d])); 3708e6ccafaeSMatthew G Knepley } 37099566063dSJacob Faibussowitsch PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints)); 37103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3711e6ccafaeSMatthew G Knepley } 3712e6ccafaeSMatthew G Knepley 3713d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm) 3714d71ae5a4SJacob Faibussowitsch { 37153982b651SMatthew G. Knepley PetscFunctionBegin; 3716*4d00ad26Sksagiyam PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, PETSC_FALSE, NULL, subdm)); 37173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 37183982b651SMatthew G. Knepley } 37193982b651SMatthew G. Knepley 3720d0fa310fSMatthew G. Knepley /*@ 3721e6ccafaeSMatthew G Knepley DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label 3722e6ccafaeSMatthew G Knepley 3723e6ccafaeSMatthew G Knepley Input Parameters: 3724e6ccafaeSMatthew G Knepley + dm - The original mesh 372520f4b53cSBarry Smith . vertexLabel - The `DMLabel` marking points contained in the surface 3726158acfadSMatthew G. Knepley . value - The label value to use 372720f4b53cSBarry Smith - markedFaces - `PETSC_TRUE` if surface faces are marked in addition to vertices, `PETSC_FALSE` if only vertices are marked 3728e6ccafaeSMatthew G Knepley 3729e6ccafaeSMatthew G Knepley Output Parameter: 3730e6ccafaeSMatthew G Knepley . subdm - The surface mesh 3731e6ccafaeSMatthew G Knepley 3732e6ccafaeSMatthew G Knepley Level: developer 3733e6ccafaeSMatthew G Knepley 373420f4b53cSBarry Smith Note: 373520f4b53cSBarry Smith This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`. 373620f4b53cSBarry Smith 37371cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()` 3738830e53efSMatthew G. Knepley @*/ 3739d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm) 3740d71ae5a4SJacob Faibussowitsch { 3741827c4036SVaclav Hapla DMPlexInterpolatedFlag interpolated; 3742827c4036SVaclav Hapla PetscInt dim, cdim; 3743e6ccafaeSMatthew G Knepley 3744e6ccafaeSMatthew G Knepley PetscFunctionBegin; 3745e6ccafaeSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 37464f572ea9SToby Isaac PetscAssertPointer(subdm, 5); 37479566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 37489566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm)); 37499566063dSJacob Faibussowitsch PetscCall(DMSetType(*subdm, DMPLEX)); 37509566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*subdm, dim - 1)); 37519566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 37529566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(*subdm, cdim)); 37539566063dSJacob Faibussowitsch PetscCall(DMPlexIsInterpolated(dm, &interpolated)); 375408401ef6SPierre Jolivet PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes"); 3755827c4036SVaclav Hapla if (interpolated) { 37569566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm)); 3757e6ccafaeSMatthew G Knepley } else { 37589566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm)); 3759e6ccafaeSMatthew G Knepley } 37605de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm)); 37613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3762e6ccafaeSMatthew G Knepley } 3763e6ccafaeSMatthew G Knepley 3764d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm) 3765d71ae5a4SJacob Faibussowitsch { 3766766ab985SMatthew G. Knepley MPI_Comm comm; 3767766ab985SMatthew G. Knepley DMLabel subpointMap; 3768766ab985SMatthew G. Knepley IS subvertexIS; 3769766ab985SMatthew G. Knepley const PetscInt *subVertices; 3770fed694aaSMatthew G. Knepley PetscInt numSubVertices, firstSubVertex, numSubCells, *subCells = NULL; 3771766ab985SMatthew G. Knepley PetscInt *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV; 3772412e9a14SMatthew G. Knepley PetscInt c, f; 3773766ab985SMatthew G. Knepley 3774766ab985SMatthew G. Knepley PetscFunctionBegin; 37759566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 3776766ab985SMatthew G. Knepley /* Create subpointMap which marks the submesh */ 37779566063dSJacob Faibussowitsch PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap)); 37789566063dSJacob Faibussowitsch PetscCall(DMPlexSetSubpointMap(subdm, subpointMap)); 37799566063dSJacob Faibussowitsch PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm)); 3780766ab985SMatthew G. Knepley /* Setup chart */ 37819566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices)); 37829566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells)); 37839566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices)); 37849566063dSJacob Faibussowitsch PetscCall(DMPlexSetVTKCellHeight(subdm, 1)); 3785766ab985SMatthew G. Knepley /* Set cone sizes */ 3786766ab985SMatthew G. Knepley firstSubVertex = numSubCells; 3787766ab985SMatthew G. Knepley firstSubFace = numSubCells + numSubVertices; 3788766ab985SMatthew G. Knepley newFacePoint = firstSubFace; 37899566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS)); 37909566063dSJacob Faibussowitsch if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices)); 379148a46eb9SPierre Jolivet for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1)); 379248a46eb9SPierre Jolivet for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV)); 37939566063dSJacob Faibussowitsch PetscCall(DMSetUp(subdm)); 3794d24a712aSStefano Zampini PetscCall(DMLabelDestroy(&subpointMap)); 3795766ab985SMatthew G. Knepley /* Create face cones */ 37969566063dSJacob Faibussowitsch PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL)); 37979566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface)); 3798766ab985SMatthew G. Knepley for (c = 0; c < numSubCells; ++c) { 3799766ab985SMatthew G. Knepley const PetscInt cell = subCells[c]; 3800766ab985SMatthew G. Knepley const PetscInt subcell = c; 380187feddfdSMatthew G. Knepley const PetscInt *cone, *cells; 3802064cae4fSPierre Jolivet PetscBool isHybrid = PETSC_FALSE; 380387feddfdSMatthew G. Knepley PetscInt numCells, subVertex, p, v; 3804766ab985SMatthew G. Knepley 38059566063dSJacob Faibussowitsch PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid)); 3806412e9a14SMatthew G. Knepley if (!isHybrid) continue; 38079566063dSJacob Faibussowitsch PetscCall(DMPlexGetCone(dm, cell, &cone)); 380887feddfdSMatthew G. Knepley for (v = 0; v < nFV; ++v) { 38099566063dSJacob Faibussowitsch PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex)); 381087feddfdSMatthew G. Knepley subface[v] = firstSubVertex + subVertex; 381187feddfdSMatthew G. Knepley } 38129566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, newFacePoint, subface)); 38139566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint)); 38149566063dSJacob Faibussowitsch PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells)); 381527234c99SMatthew G. Knepley /* Not true in parallel 381608401ef6SPierre Jolivet PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */ 381787feddfdSMatthew G. Knepley for (p = 0; p < numCells; ++p) { 381887feddfdSMatthew G. Knepley PetscInt negsubcell; 3819064cae4fSPierre Jolivet PetscBool isHybrid = PETSC_FALSE; 3820766ab985SMatthew G. Knepley 38219566063dSJacob Faibussowitsch PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid)); 3822412e9a14SMatthew G. Knepley if (isHybrid) continue; 382387feddfdSMatthew G. Knepley /* I know this is a crap search */ 382487feddfdSMatthew G. Knepley for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) { 382587feddfdSMatthew G. Knepley if (subCells[negsubcell] == cells[p]) break; 3826766ab985SMatthew G. Knepley } 382763a3b9bcSJacob Faibussowitsch PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell); 38289566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint)); 3829766ab985SMatthew G. Knepley } 38309566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells)); 383187feddfdSMatthew G. Knepley ++newFacePoint; 3832766ab985SMatthew G. Knepley } 38339566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface)); 38349566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(subdm)); 38359566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(subdm)); 3836766ab985SMatthew G. Knepley /* Build coordinates */ 3837766ab985SMatthew G. Knepley { 3838766ab985SMatthew G. Knepley PetscSection coordSection, subCoordSection; 3839766ab985SMatthew G. Knepley Vec coordinates, subCoordinates; 3840766ab985SMatthew G. Knepley PetscScalar *coords, *subCoords; 3841c0e8cf5fSMatthew G. Knepley PetscInt cdim, numComp, coordSize, v; 384224640c55SToby Isaac const char *name; 3843766ab985SMatthew G. Knepley 38449566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 38459566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 38469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinates)); 38479566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(subdm, &subCoordSection)); 38489566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(subCoordSection, 1)); 38499566063dSJacob Faibussowitsch PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp)); 38509566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp)); 38519566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices)); 3852766ab985SMatthew G. Knepley for (v = 0; v < numSubVertices; ++v) { 3853766ab985SMatthew G. Knepley const PetscInt vertex = subVertices[v]; 3854766ab985SMatthew G. Knepley const PetscInt subvertex = firstSubVertex + v; 3855766ab985SMatthew G. Knepley PetscInt dof; 3856766ab985SMatthew G. Knepley 38579566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 38589566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof)); 38599566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof)); 3860766ab985SMatthew G. Knepley } 38619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(subCoordSection)); 38629566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize)); 38639566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates)); 38649566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)coordinates, &name)); 38659566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name)); 38669566063dSJacob Faibussowitsch PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE)); 38679566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(subCoordinates, cdim)); 38689566063dSJacob Faibussowitsch PetscCall(VecSetType(subCoordinates, VECSTANDARD)); 38699566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 38709566063dSJacob Faibussowitsch PetscCall(VecGetArray(subCoordinates, &subCoords)); 3871766ab985SMatthew G. Knepley for (v = 0; v < numSubVertices; ++v) { 3872766ab985SMatthew G. Knepley const PetscInt vertex = subVertices[v]; 3873766ab985SMatthew G. Knepley const PetscInt subvertex = firstSubVertex + v; 3874766ab985SMatthew G. Knepley PetscInt dof, off, sdof, soff, d; 3875766ab985SMatthew G. Knepley 38769566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(coordSection, vertex, &dof)); 38779566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, vertex, &off)); 38789566063dSJacob Faibussowitsch PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof)); 38799566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff)); 388063a3b9bcSJacob Faibussowitsch PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof); 3881766ab985SMatthew G. Knepley for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d]; 3882766ab985SMatthew G. Knepley } 38839566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 38849566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(subCoordinates, &subCoords)); 38859566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates)); 38869566063dSJacob Faibussowitsch PetscCall(VecDestroy(&subCoordinates)); 3887766ab985SMatthew G. Knepley } 3888aca35d17SMatthew G. Knepley /* Build SF */ 3889aca35d17SMatthew G. Knepley CHKMEMQ; 3890aca35d17SMatthew G. Knepley { 3891aca35d17SMatthew G. Knepley PetscSF sfPoint, sfPointSub; 3892aca35d17SMatthew G. Knepley const PetscSFNode *remotePoints; 3893bdcf2095SMatthew G. Knepley PetscSFNode *sremotePoints, *newLocalPoints, *newOwners; 3894aca35d17SMatthew G. Knepley const PetscInt *localPoints; 3895bdcf2095SMatthew G. Knepley PetscInt *slocalPoints; 389649c26ae4SMatthew G. Knepley PetscInt numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd; 3897bdcf2095SMatthew G. Knepley PetscMPIInt rank; 3898aca35d17SMatthew G. Knepley 38999566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 39009566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sfPoint)); 39019566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(subdm, &sfPointSub)); 39029566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 39039566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 39049566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints)); 3905aca35d17SMatthew G. Knepley if (numRoots >= 0) { 3906aca35d17SMatthew G. Knepley /* Only vertices should be shared */ 39079566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners)); 3908bdcf2095SMatthew G. Knepley for (p = 0; p < pEnd - pStart; ++p) { 3909bdcf2095SMatthew G. Knepley newLocalPoints[p].rank = -2; 3910bdcf2095SMatthew G. Knepley newLocalPoints[p].index = -2; 3911bdcf2095SMatthew G. Knepley } 39129e0823b2SMatthew G. Knepley /* Set subleaves */ 3913aca35d17SMatthew G. Knepley for (l = 0; l < numLeaves; ++l) { 3914aca35d17SMatthew G. Knepley const PetscInt point = localPoints[l]; 3915bdcf2095SMatthew G. Knepley const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices); 3916aca35d17SMatthew G. Knepley 391763a3b9bcSJacob Faibussowitsch PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point); 3918bdcf2095SMatthew G. Knepley if (subPoint < 0) continue; 3919bdcf2095SMatthew G. Knepley newLocalPoints[point - pStart].rank = rank; 3920bdcf2095SMatthew G. Knepley newLocalPoints[point - pStart].index = subPoint; 3921bdcf2095SMatthew G. Knepley ++numSubLeaves; 3922aca35d17SMatthew G. Knepley } 39239e0823b2SMatthew G. Knepley /* Must put in owned subpoints */ 39249e0823b2SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 39259e0823b2SMatthew G. Knepley const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices); 39269e0823b2SMatthew G. Knepley 39279e0823b2SMatthew G. Knepley if (subPoint < 0) { 39289e0823b2SMatthew G. Knepley newOwners[p - pStart].rank = -3; 39299e0823b2SMatthew G. Knepley newOwners[p - pStart].index = -3; 39309e0823b2SMatthew G. Knepley } else { 39319e0823b2SMatthew G. Knepley newOwners[p - pStart].rank = rank; 39329e0823b2SMatthew G. Knepley newOwners[p - pStart].index = subPoint; 39339e0823b2SMatthew G. Knepley } 3934bdcf2095SMatthew G. Knepley } 39359566063dSJacob Faibussowitsch PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC)); 39369566063dSJacob Faibussowitsch PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC)); 39379566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE)); 39389566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE)); 39399566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints)); 39409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints)); 394149c26ae4SMatthew G. Knepley for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) { 3942bdcf2095SMatthew G. Knepley const PetscInt point = localPoints[l]; 3943bdcf2095SMatthew G. Knepley const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices); 3944aca35d17SMatthew G. Knepley 3945aca35d17SMatthew G. Knepley if (subPoint < 0) continue; 39469371c9d4SSatish Balay if (newLocalPoints[point].rank == rank) { 39479371c9d4SSatish Balay ++ll; 39489371c9d4SSatish Balay continue; 39499371c9d4SSatish Balay } 3950aca35d17SMatthew G. Knepley slocalPoints[sl] = subPoint; 3951bdcf2095SMatthew G. Knepley sremotePoints[sl].rank = newLocalPoints[point].rank; 3952bdcf2095SMatthew G. Knepley sremotePoints[sl].index = newLocalPoints[point].index; 395363a3b9bcSJacob Faibussowitsch PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point); 395463a3b9bcSJacob Faibussowitsch PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point); 3955aca35d17SMatthew G. Knepley ++sl; 3956aca35d17SMatthew G. Knepley } 39579566063dSJacob Faibussowitsch PetscCall(PetscFree2(newLocalPoints, newOwners)); 39581dca8a05SBarry Smith PetscCheck(sl + ll == numSubLeaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubLeaves); 39599566063dSJacob Faibussowitsch PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER)); 3960aca35d17SMatthew G. Knepley } 3961aca35d17SMatthew G. Knepley } 3962aca35d17SMatthew G. Knepley CHKMEMQ; 3963766ab985SMatthew G. Knepley /* Cleanup */ 39649566063dSJacob Faibussowitsch if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices)); 39659566063dSJacob Faibussowitsch PetscCall(ISDestroy(&subvertexIS)); 39669566063dSJacob Faibussowitsch PetscCall(PetscFree(subCells)); 39673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3968766ab985SMatthew G. Knepley } 3969766ab985SMatthew G. Knepley 3970d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm) 3971d71ae5a4SJacob Faibussowitsch { 39723982b651SMatthew G. Knepley DMLabel label = NULL; 3973766ab985SMatthew G. Knepley 3974766ab985SMatthew G. Knepley PetscFunctionBegin; 39759566063dSJacob Faibussowitsch if (labelname) PetscCall(DMGetLabel(dm, labelname, &label)); 3976*4d00ad26Sksagiyam PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, PETSC_FALSE, NULL, subdm)); 39773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3978766ab985SMatthew G. Knepley } 3979766ab985SMatthew G. Knepley 3980c08575a3SMatthew G. Knepley /*@C 398120f4b53cSBarry Smith DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a label can be given to restrict the cells. 3982766ab985SMatthew G. Knepley 3983766ab985SMatthew G. Knepley Input Parameters: 3984766ab985SMatthew G. Knepley + dm - The original mesh 398527c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells 398620f4b53cSBarry Smith . label - A label name, or `NULL` 398727c04023SMatthew G. Knepley - value - A label value 3988766ab985SMatthew G. Knepley 3989766ab985SMatthew G. Knepley Output Parameter: 3990766ab985SMatthew G. Knepley . subdm - The surface mesh 3991766ab985SMatthew G. Knepley 3992766ab985SMatthew G. Knepley Level: developer 3993766ab985SMatthew G. Knepley 399420f4b53cSBarry Smith Note: 399520f4b53cSBarry Smith This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`. 399620f4b53cSBarry Smith 39971cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()` 3998c08575a3SMatthew G. Knepley @*/ 3999d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm) 4000d71ae5a4SJacob Faibussowitsch { 4001c0e8cf5fSMatthew G. Knepley PetscInt dim, cdim, depth; 4002766ab985SMatthew G. Knepley 4003766ab985SMatthew G. Knepley PetscFunctionBegin; 4004766ab985SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 40054f572ea9SToby Isaac PetscAssertPointer(subdm, 5); 40069566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 40079566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 40089566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm)); 40099566063dSJacob Faibussowitsch PetscCall(DMSetType(*subdm, DMPLEX)); 40109566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*subdm, dim - 1)); 40119566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &cdim)); 40129566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(*subdm, cdim)); 4013766ab985SMatthew G. Knepley if (depth == dim) { 40149566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm)); 4015766ab985SMatthew G. Knepley } else { 40169566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm)); 4017e6ccafaeSMatthew G Knepley } 40185de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm)); 40193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4020e6ccafaeSMatthew G Knepley } 4021e6ccafaeSMatthew G Knepley 4022bec263e5SMatthew G. Knepley /*@ 40230e18dc48SMatthew G. Knepley DMPlexReorderCohesiveSupports - Ensure that face supports for cohesive end caps are ordered 40240e18dc48SMatthew G. Knepley 40250e18dc48SMatthew G. Knepley Not Collective 40260e18dc48SMatthew G. Knepley 40270e18dc48SMatthew G. Knepley Input Parameter: 40280e18dc48SMatthew G. Knepley . dm - The `DM` containing cohesive cells 40290e18dc48SMatthew G. Knepley 40300e18dc48SMatthew G. Knepley Level: developer 40310e18dc48SMatthew G. Knepley 4032a4e35b19SJacob Faibussowitsch Note: 4033a4e35b19SJacob Faibussowitsch For the negative size (first) face, the cohesive cell should be first in the support, and for 4034a4e35b19SJacob Faibussowitsch the positive side (second) face, the cohesive cell should be second in the support. 40350e18dc48SMatthew G. Knepley 40360e18dc48SMatthew G. Knepley .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexCreateCohesiveSubmesh()` 40370e18dc48SMatthew G. Knepley @*/ 40380e18dc48SMatthew G. Knepley PetscErrorCode DMPlexReorderCohesiveSupports(DM dm) 40390e18dc48SMatthew G. Knepley { 40400e18dc48SMatthew G. Knepley PetscInt dim, cStart, cEnd; 40410e18dc48SMatthew G. Knepley 40420e18dc48SMatthew G. Knepley PetscFunctionBegin; 40430e18dc48SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 40440e18dc48SMatthew G. Knepley PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cStart, &cEnd)); 40450e18dc48SMatthew G. Knepley for (PetscInt c = cStart; c < cEnd; ++c) { 40460e18dc48SMatthew G. Knepley const PetscInt *cone; 40470e18dc48SMatthew G. Knepley PetscInt coneSize; 40480e18dc48SMatthew G. Knepley 40490e18dc48SMatthew G. Knepley PetscCall(DMPlexGetConeSize(dm, c, &coneSize)); 40500e18dc48SMatthew G. Knepley PetscCall(DMPlexGetCone(dm, c, &cone)); 40510e18dc48SMatthew G. Knepley PetscCheck(coneSize >= 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid cell %" PetscInt_FMT " cone size %" PetscInt_FMT " must be >= 2", c, coneSize); 40520e18dc48SMatthew G. Knepley for (PetscInt s = 0; s < 2; ++s) { 40530e18dc48SMatthew G. Knepley const PetscInt *supp; 40540e18dc48SMatthew G. Knepley PetscInt suppSize, newsupp[2]; 40550e18dc48SMatthew G. Knepley 40560e18dc48SMatthew G. Knepley PetscCall(DMPlexGetSupportSize(dm, cone[s], &suppSize)); 40570e18dc48SMatthew G. Knepley PetscCall(DMPlexGetSupport(dm, cone[s], &supp)); 40580e18dc48SMatthew G. Knepley if (suppSize == 2) { 40590e18dc48SMatthew G. Knepley /* Reorder hybrid end cap support */ 40600e18dc48SMatthew G. Knepley if (supp[s] == c) { 40610e18dc48SMatthew G. Knepley newsupp[0] = supp[1]; 40620e18dc48SMatthew G. Knepley newsupp[1] = supp[0]; 40630e18dc48SMatthew G. Knepley } else { 40640e18dc48SMatthew G. Knepley newsupp[0] = supp[0]; 40650e18dc48SMatthew G. Knepley newsupp[1] = supp[1]; 40660e18dc48SMatthew G. Knepley } 40670e18dc48SMatthew G. Knepley PetscCheck(newsupp[1 - s] == c, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Hybrid end cap %" PetscInt_FMT " support entry %" PetscInt_FMT " != %" PetscInt_FMT " hybrid cell", cone[s], newsupp[1 - s], c); 40680e18dc48SMatthew G. Knepley PetscCall(DMPlexSetSupport(dm, cone[s], newsupp)); 40690e18dc48SMatthew G. Knepley } 40700e18dc48SMatthew G. Knepley } 40710e18dc48SMatthew G. Knepley } 40720e18dc48SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS); 40730e18dc48SMatthew G. Knepley } 40740e18dc48SMatthew G. Knepley 40750e18dc48SMatthew G. Knepley /*@ 4076bec263e5SMatthew G. Knepley DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh 4077bec263e5SMatthew G. Knepley 4078bec263e5SMatthew G. Knepley Input Parameters: 4079bec263e5SMatthew G. Knepley + dm - The original mesh 408020f4b53cSBarry Smith . cellLabel - The `DMLabel` marking cells contained in the new mesh 4081*4d00ad26Sksagiyam . value - The label value to use 4082*4d00ad26Sksagiyam - ignoreLabelHalo - The flag indicating if labeled points that are in the halo are ignored 4083bec263e5SMatthew G. Knepley 4084bec263e5SMatthew G. Knepley Output Parameter: 4085fdb4e32bSksagiyam + ownershipTransferSF - The `PetscSF` representing the ownership transfers between parent local meshes due to submeshing. 4086fdb4e32bSksagiyam - subdm - The new mesh 4087bec263e5SMatthew G. Knepley 4088bec263e5SMatthew G. Knepley Level: developer 4089bec263e5SMatthew G. Knepley 409020f4b53cSBarry Smith Note: 409120f4b53cSBarry Smith This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`. 409220f4b53cSBarry Smith 4093fdb4e32bSksagiyam On a given rank, the leaves of the ownershipTransferSF are the points in the local mesh that this rank gives up ownership of (in the submesh), and 4094fdb4e32bSksagiyam the remote locations for these leaves are tuples of rank and point that represent the new owners. 4095fdb4e32bSksagiyam 40961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()` 4097bec263e5SMatthew G. Knepley @*/ 4098*4d00ad26Sksagiyam PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, PetscBool ignoreLabelHalo, PetscSF *ownershipTransferSF, DM *subdm) 4099d71ae5a4SJacob Faibussowitsch { 4100b47cd10cSMatthew G. Knepley PetscInt dim, overlap; 4101bec263e5SMatthew G. Knepley 4102bec263e5SMatthew G. Knepley PetscFunctionBegin; 4103bec263e5SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4104*4d00ad26Sksagiyam if (ownershipTransferSF) PetscAssertPointer(ownershipTransferSF, 5); 4105*4d00ad26Sksagiyam PetscAssertPointer(subdm, 6); 41069566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 41079566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm)); 41089566063dSJacob Faibussowitsch PetscCall(DMSetType(*subdm, DMPLEX)); 4109bec263e5SMatthew G. Knepley /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */ 4110*4d00ad26Sksagiyam PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, ignoreLabelHalo, ownershipTransferSF, *subdm)); 41115de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm)); 4112b47cd10cSMatthew G. Knepley // It is possible to obtain a surface mesh where some faces are in SF 4113b47cd10cSMatthew G. Knepley // We should either mark the mesh as having an overlap, or delete these from the SF 4114b47cd10cSMatthew G. Knepley PetscCall(DMPlexGetOverlap(dm, &overlap)); 4115b47cd10cSMatthew G. Knepley if (!overlap) { 4116b47cd10cSMatthew G. Knepley PetscSF sf; 4117b47cd10cSMatthew G. Knepley const PetscInt *leaves; 4118b47cd10cSMatthew G. Knepley PetscInt cStart, cEnd, Nl; 4119b47cd10cSMatthew G. Knepley PetscBool hasSubcell = PETSC_FALSE, ghasSubcell; 4120b47cd10cSMatthew G. Knepley 4121b47cd10cSMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(*subdm, 0, &cStart, &cEnd)); 4122b47cd10cSMatthew G. Knepley PetscCall(DMGetPointSF(*subdm, &sf)); 4123b47cd10cSMatthew G. Knepley PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL)); 4124b47cd10cSMatthew G. Knepley for (PetscInt l = 0; l < Nl; ++l) { 4125b47cd10cSMatthew G. Knepley const PetscInt point = leaves ? leaves[l] : l; 4126b47cd10cSMatthew G. Knepley 4127b47cd10cSMatthew G. Knepley if (point >= cStart && point < cEnd) { 4128b47cd10cSMatthew G. Knepley hasSubcell = PETSC_TRUE; 4129b47cd10cSMatthew G. Knepley break; 4130b47cd10cSMatthew G. Knepley } 4131b47cd10cSMatthew G. Knepley } 4132b47cd10cSMatthew G. Knepley PetscCall(MPIU_Allreduce(&hasSubcell, &ghasSubcell, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject)dm))); 4133b47cd10cSMatthew G. Knepley if (ghasSubcell) PetscCall(DMPlexSetOverlap(*subdm, NULL, 1)); 4134b47cd10cSMatthew G. Knepley } 41353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4136bec263e5SMatthew G. Knepley } 4137bec263e5SMatthew G. Knepley 413864beef6dSMatthew G. Knepley /*@ 413920f4b53cSBarry Smith DMPlexGetSubpointMap - Returns a `DMLabel` with point dimension as values 414064beef6dSMatthew G. Knepley 414164beef6dSMatthew G. Knepley Input Parameter: 414220f4b53cSBarry Smith . dm - The submesh `DM` 414364beef6dSMatthew G. Knepley 414464beef6dSMatthew G. Knepley Output Parameter: 414520f4b53cSBarry Smith . subpointMap - The `DMLabel` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh 414664beef6dSMatthew G. Knepley 414764beef6dSMatthew G. Knepley Level: developer 414864beef6dSMatthew G. Knepley 41491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()` 415064beef6dSMatthew G. Knepley @*/ 4151d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap) 4152d71ae5a4SJacob Faibussowitsch { 4153e6ccafaeSMatthew G Knepley PetscFunctionBegin; 4154e6ccafaeSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 41554f572ea9SToby Isaac PetscAssertPointer(subpointMap, 2); 415665663942SMatthew G. Knepley *subpointMap = ((DM_Plex *)dm->data)->subpointMap; 41573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4158e6ccafaeSMatthew G Knepley } 4159e6ccafaeSMatthew G Knepley 4160c08575a3SMatthew G. Knepley /*@ 416120f4b53cSBarry Smith DMPlexSetSubpointMap - Sets the `DMLabel` with point dimension as values 4162c08575a3SMatthew G. Knepley 4163c08575a3SMatthew G. Knepley Input Parameters: 416420f4b53cSBarry Smith + dm - The submesh `DM` 416520f4b53cSBarry Smith - subpointMap - The `DMLabel` of all the points from the original mesh in this submesh 4166c08575a3SMatthew G. Knepley 4167c08575a3SMatthew G. Knepley Level: developer 4168c08575a3SMatthew G. Knepley 416920f4b53cSBarry Smith Note: 417020f4b53cSBarry Smith Should normally not be called by the user, since it is set in `DMPlexCreateSubmesh()` 417120f4b53cSBarry Smith 41721cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()` 4173c08575a3SMatthew G. Knepley @*/ 4174d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap) 4175d71ae5a4SJacob Faibussowitsch { 4176e6ccafaeSMatthew G Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 4177285d324eSMatthew G. Knepley DMLabel tmp; 4178e6ccafaeSMatthew G Knepley 4179e6ccafaeSMatthew G Knepley PetscFunctionBegin; 4180e6ccafaeSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 4181285d324eSMatthew G. Knepley tmp = mesh->subpointMap; 4182e6ccafaeSMatthew G Knepley mesh->subpointMap = subpointMap; 41839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap)); 41849566063dSJacob Faibussowitsch PetscCall(DMLabelDestroy(&tmp)); 41853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4186e6ccafaeSMatthew G Knepley } 4187e6ccafaeSMatthew G Knepley 4188d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS) 4189d71ae5a4SJacob Faibussowitsch { 419097d8846cSMatthew Knepley DMLabel spmap; 419197d8846cSMatthew Knepley PetscInt depth, d; 419297d8846cSMatthew Knepley 419397d8846cSMatthew Knepley PetscFunctionBegin; 41949566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointMap(dm, &spmap)); 41959566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 419697d8846cSMatthew Knepley if (spmap && depth >= 0) { 419797d8846cSMatthew Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 419897d8846cSMatthew Knepley PetscInt *points, *depths; 419997d8846cSMatthew Knepley PetscInt pStart, pEnd, p, off; 420097d8846cSMatthew Knepley 42019566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 420263a3b9bcSJacob Faibussowitsch PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart); 42039566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(pEnd, &points)); 42049566063dSJacob Faibussowitsch PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths)); 420597d8846cSMatthew Knepley depths[0] = depth; 420697d8846cSMatthew Knepley depths[1] = 0; 4207ad540459SPierre Jolivet for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d; 420897d8846cSMatthew Knepley for (d = 0, off = 0; d <= depth; ++d) { 420997d8846cSMatthew Knepley const PetscInt dep = depths[d]; 421097d8846cSMatthew Knepley PetscInt depStart, depEnd, n; 421197d8846cSMatthew Knepley 42129566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd)); 42139566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumSize(spmap, dep, &n)); 421497d8846cSMatthew Knepley if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */ 421563a3b9bcSJacob Faibussowitsch PetscCheck(n == depEnd - depStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " at depth %" PetscInt_FMT " should be %" PetscInt_FMT, n, dep, depEnd - depStart); 421697d8846cSMatthew Knepley } else { 421797d8846cSMatthew Knepley if (!n) { 421897d8846cSMatthew Knepley if (d == 0) { 421997d8846cSMatthew Knepley /* Missing cells */ 422097d8846cSMatthew Knepley for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1; 422197d8846cSMatthew Knepley } else { 422297d8846cSMatthew Knepley /* Missing faces */ 422397d8846cSMatthew Knepley for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT; 422497d8846cSMatthew Knepley } 422597d8846cSMatthew Knepley } 422697d8846cSMatthew Knepley } 422797d8846cSMatthew Knepley if (n) { 422897d8846cSMatthew Knepley IS is; 422997d8846cSMatthew Knepley const PetscInt *opoints; 423097d8846cSMatthew Knepley 42319566063dSJacob Faibussowitsch PetscCall(DMLabelGetStratumIS(spmap, dep, &is)); 42329566063dSJacob Faibussowitsch PetscCall(ISGetIndices(is, &opoints)); 423397d8846cSMatthew Knepley for (p = 0; p < n; ++p, ++off) points[off] = opoints[p]; 42349566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(is, &opoints)); 42359566063dSJacob Faibussowitsch PetscCall(ISDestroy(&is)); 423697d8846cSMatthew Knepley } 423797d8846cSMatthew Knepley } 42389566063dSJacob Faibussowitsch PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths)); 423963a3b9bcSJacob Faibussowitsch PetscCheck(off == pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " should be %" PetscInt_FMT, off, pEnd); 42409566063dSJacob Faibussowitsch PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS)); 42419566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState)); 424297d8846cSMatthew Knepley } 42433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 424497d8846cSMatthew Knepley } 424597d8846cSMatthew Knepley 424664beef6dSMatthew G. Knepley /*@ 424720f4b53cSBarry Smith DMPlexGetSubpointIS - Returns an `IS` covering the entire subdm chart with the original points as data 4248e6ccafaeSMatthew G Knepley 4249e6ccafaeSMatthew G Knepley Input Parameter: 425020f4b53cSBarry Smith . dm - The submesh `DM` 4251e6ccafaeSMatthew G Knepley 4252e6ccafaeSMatthew G Knepley Output Parameter: 425320f4b53cSBarry Smith . subpointIS - The `IS` of all the points from the original mesh in this submesh, or `NULL` if this is not a submesh 425464beef6dSMatthew G. Knepley 425564beef6dSMatthew G. Knepley Level: developer 425664beef6dSMatthew G. Knepley 425720f4b53cSBarry Smith Note: 425820f4b53cSBarry Smith This `IS` is guaranteed to be sorted by the construction of the submesh 425920f4b53cSBarry Smith 42601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()` 426164beef6dSMatthew G. Knepley @*/ 4262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS) 4263d71ae5a4SJacob Faibussowitsch { 426497d8846cSMatthew Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 426597d8846cSMatthew Knepley DMLabel spmap; 426697d8846cSMatthew Knepley PetscObjectState state; 4267e6ccafaeSMatthew G Knepley 4268e6ccafaeSMatthew G Knepley PetscFunctionBegin; 4269e6ccafaeSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 42704f572ea9SToby Isaac PetscAssertPointer(subpointIS, 2); 42719566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointMap(dm, &spmap)); 42729566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)spmap, &state)); 42739566063dSJacob Faibussowitsch if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS)); 427497d8846cSMatthew Knepley *subpointIS = mesh->subpointIS; 42753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4276e6ccafaeSMatthew G Knepley } 4277559a1558SMatthew G. Knepley 4278559a1558SMatthew G. Knepley /*@ 427920f4b53cSBarry Smith DMGetEnclosureRelation - Get the relationship between `dmA` and `dmB` 4280559a1558SMatthew G. Knepley 4281559a1558SMatthew G. Knepley Input Parameters: 428220f4b53cSBarry Smith + dmA - The first `DM` 428320f4b53cSBarry Smith - dmB - The second `DM` 4284559a1558SMatthew G. Knepley 4285559a1558SMatthew G. Knepley Output Parameter: 428620f4b53cSBarry Smith . rel - The relation of `dmA` to `dmB` 4287559a1558SMatthew G. Knepley 4288a6e0b375SMatthew G. Knepley Level: intermediate 4289559a1558SMatthew G. Knepley 42901cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosurePoint()` 4291559a1558SMatthew G. Knepley @*/ 4292d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel) 4293d71ae5a4SJacob Faibussowitsch { 4294a6e0b375SMatthew G. Knepley DM plexA, plexB, sdm; 4295559a1558SMatthew G. Knepley DMLabel spmap; 4296a6e0b375SMatthew G. Knepley PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB; 4297559a1558SMatthew G. Knepley 429844171101SMatthew G. Knepley PetscFunctionBegin; 42994f572ea9SToby Isaac PetscAssertPointer(rel, 3); 4300a6e0b375SMatthew G. Knepley *rel = DM_ENC_NONE; 43013ba16761SJacob Faibussowitsch if (!dmA || !dmB) PetscFunctionReturn(PETSC_SUCCESS); 4302a6e0b375SMatthew G. Knepley PetscValidHeaderSpecific(dmA, DM_CLASSID, 1); 4303064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(dmB, DM_CLASSID, 2); 43049371c9d4SSatish Balay if (dmA == dmB) { 43059371c9d4SSatish Balay *rel = DM_ENC_EQUALITY; 43063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43079371c9d4SSatish Balay } 43089566063dSJacob Faibussowitsch PetscCall(DMConvert(dmA, DMPLEX, &plexA)); 43099566063dSJacob Faibussowitsch PetscCall(DMConvert(dmB, DMPLEX, &plexB)); 43109566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA)); 43119566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB)); 4312a6e0b375SMatthew G. Knepley /* Assumption 1: subDMs have smaller charts than the DMs that they originate from 4313a6e0b375SMatthew G. Knepley - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */ 4314a6e0b375SMatthew G. Knepley if ((pStartA == pStartB) && (pEndA == pEndB)) { 4315a6e0b375SMatthew G. Knepley *rel = DM_ENC_EQUALITY; 4316a6e0b375SMatthew G. Knepley goto end; 4317559a1558SMatthew G. Knepley } 4318a6e0b375SMatthew G. Knepley NpA = pEndA - pStartA; 4319a6e0b375SMatthew G. Knepley NpB = pEndB - pStartB; 4320a6e0b375SMatthew G. Knepley if (NpA == NpB) goto end; 4321a6e0b375SMatthew G. Knepley sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */ 43229566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointMap(sdm, &spmap)); 4323a6e0b375SMatthew G. Knepley if (!spmap) goto end; 4324a6e0b375SMatthew G. Knepley /* TODO Check the space mapped to by subpointMap is same size as dm */ 4325a6e0b375SMatthew G. Knepley if (NpA > NpB) { 4326a6e0b375SMatthew G. Knepley *rel = DM_ENC_SUPERMESH; 4327a6e0b375SMatthew G. Knepley } else { 4328a6e0b375SMatthew G. Knepley *rel = DM_ENC_SUBMESH; 4329a6e0b375SMatthew G. Knepley } 4330a6e0b375SMatthew G. Knepley end: 43319566063dSJacob Faibussowitsch PetscCall(DMDestroy(&plexA)); 43329566063dSJacob Faibussowitsch PetscCall(DMDestroy(&plexB)); 43333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4334559a1558SMatthew G. Knepley } 433544171101SMatthew G. Knepley 433644171101SMatthew G. Knepley /*@ 433720f4b53cSBarry Smith DMGetEnclosurePoint - Get the point `pA` in `dmA` which corresponds to the point `pB` in `dmB` 433844171101SMatthew G. Knepley 433944171101SMatthew G. Knepley Input Parameters: 434020f4b53cSBarry Smith + dmA - The first `DM` 434120f4b53cSBarry Smith . dmB - The second `DM` 434220f4b53cSBarry Smith . etype - The type of enclosure relation that `dmA` has to `dmB` 434320f4b53cSBarry Smith - pB - A point of `dmB` 434444171101SMatthew G. Knepley 434544171101SMatthew G. Knepley Output Parameter: 434620f4b53cSBarry Smith . pA - The corresponding point of `dmA` 434744171101SMatthew G. Knepley 4348a6e0b375SMatthew G. Knepley Level: intermediate 434944171101SMatthew G. Knepley 43501cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMGetEnclosureRelation()` 435144171101SMatthew G. Knepley @*/ 4352d71ae5a4SJacob Faibussowitsch PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA) 4353d71ae5a4SJacob Faibussowitsch { 4354a6e0b375SMatthew G. Knepley DM sdm; 4355a6e0b375SMatthew G. Knepley IS subpointIS; 4356a6e0b375SMatthew G. Knepley const PetscInt *subpoints; 4357a6e0b375SMatthew G. Knepley PetscInt numSubpoints; 435844171101SMatthew G. Knepley 435944171101SMatthew G. Knepley PetscFunctionBegin; 4360a6e0b375SMatthew G. Knepley /* TODO Cache the IS, making it look like an index */ 4361a6e0b375SMatthew G. Knepley switch (etype) { 4362a6e0b375SMatthew G. Knepley case DM_ENC_SUPERMESH: 4363a6e0b375SMatthew G. Knepley sdm = dmB; 43649566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS)); 43659566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subpointIS, &subpoints)); 4366a6e0b375SMatthew G. Knepley *pA = subpoints[pB]; 43679566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(subpointIS, &subpoints)); 4368a6e0b375SMatthew G. Knepley break; 4369a6e0b375SMatthew G. Knepley case DM_ENC_SUBMESH: 4370a6e0b375SMatthew G. Knepley sdm = dmA; 43719566063dSJacob Faibussowitsch PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS)); 43729566063dSJacob Faibussowitsch PetscCall(ISGetLocalSize(subpointIS, &numSubpoints)); 43739566063dSJacob Faibussowitsch PetscCall(ISGetIndices(subpointIS, &subpoints)); 43749566063dSJacob Faibussowitsch PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA)); 4375a6e0b375SMatthew G. Knepley if (*pA < 0) { 43769566063dSJacob Faibussowitsch PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view")); 43779566063dSJacob Faibussowitsch PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view")); 437863a3b9bcSJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB); 4379a6e0b375SMatthew G. Knepley } 43809566063dSJacob Faibussowitsch PetscCall(ISRestoreIndices(subpointIS, &subpoints)); 4381a6e0b375SMatthew G. Knepley break; 4382a6e0b375SMatthew G. Knepley case DM_ENC_EQUALITY: 4383d71ae5a4SJacob Faibussowitsch case DM_ENC_NONE: 4384d71ae5a4SJacob Faibussowitsch *pA = pB; 4385d71ae5a4SJacob Faibussowitsch break; 43869371c9d4SSatish Balay case DM_ENC_UNKNOWN: { 4387a6e0b375SMatthew G. Knepley DMEnclosureType enc; 438844171101SMatthew G. Knepley 43899566063dSJacob Faibussowitsch PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc)); 43909566063dSJacob Faibussowitsch PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA)); 43919371c9d4SSatish Balay } break; 4392d71ae5a4SJacob Faibussowitsch default: 4393d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype); 439444171101SMatthew G. Knepley } 43953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 439644171101SMatthew G. Knepley } 4397