1 #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 2 #include <petscsf.h> 3 4 #undef __FUNCT__ 5 #define __FUNCT__ "DMPlexMarkBoundaryFaces" 6 /*@ 7 DMPlexMarkBoundaryFaces - Mark all faces on the boundary 8 9 Not Collective 10 11 Input Parameter: 12 . dm - The original DM 13 14 Output Parameter: 15 . label - The DMLabel marking boundary faces with value 1 16 17 Level: developer 18 19 .seealso: DMLabelCreate(), DMPlexCreateLabel() 20 @*/ 21 PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, DMLabel label) 22 { 23 PetscInt fStart, fEnd, f; 24 PetscErrorCode ierr; 25 26 PetscFunctionBegin; 27 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 28 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 29 for (f = fStart; f < fEnd; ++f) { 30 PetscInt supportSize; 31 32 ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr); 33 if (supportSize == 1) { 34 ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr); 35 } 36 } 37 PetscFunctionReturn(0); 38 } 39 40 #undef __FUNCT__ 41 #define __FUNCT__ "DMPlexLabelComplete" 42 /*@ 43 DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface 44 45 Input Parameters: 46 + dm - The DM 47 - label - A DMLabel marking the surface points 48 49 Output Parameter: 50 . label - A DMLabel marking all surface points in the transitive closure 51 52 Level: developer 53 54 .seealso: DMPlexLabelCohesiveComplete() 55 @*/ 56 PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label) 57 { 58 IS valueIS; 59 const PetscInt *values; 60 PetscInt numValues, v; 61 PetscErrorCode ierr; 62 63 PetscFunctionBegin; 64 ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr); 65 ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr); 66 ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr); 67 for (v = 0; v < numValues; ++v) { 68 IS pointIS; 69 const PetscInt *points; 70 PetscInt numPoints, p; 71 72 ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr); 73 ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr); 74 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 75 for (p = 0; p < numPoints; ++p) { 76 PetscInt *closure = NULL; 77 PetscInt closureSize, c; 78 79 ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 80 for (c = 0; c < closureSize*2; c += 2) { 81 ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr); 82 } 83 ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 84 } 85 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 86 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 87 } 88 ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr); 89 ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 90 PetscFunctionReturn(0); 91 } 92 93 #undef __FUNCT__ 94 #define __FUNCT__ "DMPlexShiftPoint_Internal" 95 PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthEnd[], PetscInt depthShift[]) 96 { 97 if (depth < 0) return p; 98 /* Cells */ if (p < depthEnd[depth]) return p; 99 /* Vertices */ if (p < depthEnd[0]) return p + depthShift[depth]; 100 /* Faces */ if (p < depthEnd[depth-1]) return p + depthShift[depth] + depthShift[0]; 101 /* Edges */ return p + depthShift[depth] + depthShift[0] + depthShift[depth-1]; 102 } 103 104 #undef __FUNCT__ 105 #define __FUNCT__ "DMPlexShiftSizes_Internal" 106 static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew) 107 { 108 PetscInt *depthEnd; 109 PetscInt depth = 0, d, pStart, pEnd, p; 110 PetscErrorCode ierr; 111 112 PetscFunctionBegin; 113 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 114 if (depth < 0) PetscFunctionReturn(0); 115 ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr); 116 /* Step 1: Expand chart */ 117 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 118 for (d = 0; d <= depth; ++d) { 119 pEnd += depthShift[d]; 120 ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr); 121 } 122 ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr); 123 /* Step 2: Set cone and support sizes */ 124 for (d = 0; d <= depth; ++d) { 125 ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr); 126 for (p = pStart; p < pEnd; ++p) { 127 PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthEnd, depthShift); 128 PetscInt size; 129 130 ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr); 131 ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr); 132 ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr); 133 ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr); 134 } 135 } 136 ierr = PetscFree(depthEnd);CHKERRQ(ierr); 137 PetscFunctionReturn(0); 138 } 139 140 #undef __FUNCT__ 141 #define __FUNCT__ "DMPlexShiftPoints_Internal" 142 static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew) 143 { 144 PetscInt *depthEnd, *newpoints; 145 PetscInt depth = 0, d, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p; 146 PetscErrorCode ierr; 147 148 PetscFunctionBegin; 149 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 150 if (depth < 0) PetscFunctionReturn(0); 151 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr); 152 ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr); 153 ierr = PetscMalloc2(depth+1,PetscInt,&depthEnd,PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),PetscInt,&newpoints);CHKERRQ(ierr); 154 for (d = 0; d <= depth; ++d) { 155 ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr); 156 } 157 /* Step 5: Set cones and supports */ 158 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 159 for (p = pStart; p < pEnd; ++p) { 160 const PetscInt *points = NULL, *orientations = NULL; 161 PetscInt size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthEnd, depthShift); 162 163 ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr); 164 ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr); 165 ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr); 166 for (i = 0; i < size; ++i) { 167 newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthEnd, depthShift); 168 } 169 ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr); 170 ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr); 171 ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr); 172 ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr); 173 ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr); 174 for (i = 0; i < size; ++i) { 175 newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthEnd, depthShift); 176 } 177 for (i = size; i < sizeNew; ++i) newpoints[i] = 0; 178 ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr); 179 } 180 ierr = PetscFree2(depthEnd,newpoints);CHKERRQ(ierr); 181 PetscFunctionReturn(0); 182 } 183 184 #undef __FUNCT__ 185 #define __FUNCT__ "DMPlexShiftCoordinates_Internal" 186 static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew) 187 { 188 PetscSection coordSection, newCoordSection; 189 Vec coordinates, newCoordinates; 190 PetscScalar *coords, *newCoords; 191 PetscInt *depthEnd, coordSize; 192 PetscInt dim, depth = 0, d, vStart, vEnd, vStartNew, vEndNew, v; 193 PetscErrorCode ierr; 194 195 PetscFunctionBegin; 196 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 197 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 198 ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr); 199 for (d = 0; d <= depth; ++d) { 200 ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr); 201 } 202 /* Step 8: Convert coordinates */ 203 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 204 ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr); 205 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 206 ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr); 207 ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr); 208 ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr); 209 ierr = PetscSectionSetChart(newCoordSection, vStartNew, vEndNew);CHKERRQ(ierr); 210 for (v = vStartNew; v < vEndNew; ++v) { 211 ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr); 212 ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr); 213 } 214 ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr); 215 ierr = DMPlexSetCoordinateSection(dmNew, newCoordSection);CHKERRQ(ierr); 216 ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr); 217 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &newCoordinates);CHKERRQ(ierr); 218 ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr); 219 ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 220 ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr); 221 ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr); 222 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 223 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 224 ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr); 225 for (v = vStart; v < vEnd; ++v) { 226 PetscInt dof, off, noff, d; 227 228 ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr); 229 ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); 230 ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthEnd, depthShift), &noff);CHKERRQ(ierr); 231 for (d = 0; d < dof; ++d) { 232 newCoords[noff+d] = coords[off+d]; 233 } 234 } 235 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 236 ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr); 237 ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr); 238 ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr); 239 ierr = PetscFree(depthEnd);CHKERRQ(ierr); 240 PetscFunctionReturn(0); 241 } 242 243 #undef __FUNCT__ 244 #define __FUNCT__ "DMPlexShiftSF_Internal" 245 static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew) 246 { 247 PetscInt *depthEnd; 248 PetscInt depth = 0, d; 249 PetscSF sfPoint, sfPointNew; 250 const PetscSFNode *remotePoints; 251 PetscSFNode *gremotePoints; 252 const PetscInt *localPoints; 253 PetscInt *glocalPoints, *newLocation, *newRemoteLocation; 254 PetscInt numRoots, numLeaves, l, pStart, pEnd, totShift = 0; 255 PetscErrorCode ierr; 256 257 PetscFunctionBegin; 258 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 259 ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr); 260 for (d = 0; d <= depth; ++d) { 261 totShift += depthShift[d]; 262 ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr); 263 } 264 /* Step 9: Convert pointSF */ 265 ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 266 ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr); 267 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 268 ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr); 269 if (numRoots >= 0) { 270 ierr = PetscMalloc2(numRoots,PetscInt,&newLocation,pEnd-pStart,PetscInt,&newRemoteLocation);CHKERRQ(ierr); 271 for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthEnd, depthShift); 272 ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr); 273 ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr); 274 ierr = PetscMalloc(numLeaves * sizeof(PetscInt), &glocalPoints);CHKERRQ(ierr); 275 ierr = PetscMalloc(numLeaves * sizeof(PetscSFNode), &gremotePoints);CHKERRQ(ierr); 276 for (l = 0; l < numLeaves; ++l) { 277 glocalPoints[l] = DMPlexShiftPoint_Internal(localPoints[l], depth, depthEnd, depthShift); 278 gremotePoints[l].rank = remotePoints[l].rank; 279 gremotePoints[l].index = newRemoteLocation[localPoints[l]]; 280 } 281 ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr); 282 ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr); 283 } 284 ierr = PetscFree(depthEnd);CHKERRQ(ierr); 285 PetscFunctionReturn(0); 286 } 287 288 #undef __FUNCT__ 289 #define __FUNCT__ "DMPlexShiftLabels_Internal" 290 static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew) 291 { 292 PetscSF sfPoint; 293 DMLabel vtkLabel, ghostLabel; 294 PetscInt *depthEnd; 295 const PetscSFNode *leafRemote; 296 const PetscInt *leafLocal; 297 PetscInt depth = 0, d, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f; 298 PetscMPIInt rank; 299 PetscErrorCode ierr; 300 301 PetscFunctionBegin; 302 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 303 ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr); 304 for (d = 0; d <= depth; ++d) { 305 ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr); 306 } 307 /* Step 10: Convert labels */ 308 ierr = DMPlexGetNumLabels(dm, &numLabels);CHKERRQ(ierr); 309 for (l = 0; l < numLabels; ++l) { 310 DMLabel label, newlabel; 311 const char *lname; 312 PetscBool isDepth; 313 IS valueIS; 314 const PetscInt *values; 315 PetscInt numValues, val; 316 317 ierr = DMPlexGetLabelName(dm, l, &lname);CHKERRQ(ierr); 318 ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr); 319 if (isDepth) continue; 320 ierr = DMPlexCreateLabel(dmNew, lname);CHKERRQ(ierr); 321 ierr = DMPlexGetLabel(dm, lname, &label);CHKERRQ(ierr); 322 ierr = DMPlexGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr); 323 ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr); 324 ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr); 325 ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr); 326 for (val = 0; val < numValues; ++val) { 327 IS pointIS; 328 const PetscInt *points; 329 PetscInt numPoints, p; 330 331 ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr); 332 ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr); 333 ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr); 334 for (p = 0; p < numPoints; ++p) { 335 const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthEnd, depthShift); 336 337 ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr); 338 } 339 ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr); 340 ierr = ISDestroy(&pointIS);CHKERRQ(ierr); 341 } 342 ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr); 343 ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 344 } 345 ierr = PetscFree(depthEnd);CHKERRQ(ierr); 346 /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */ 347 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 348 ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 349 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 350 ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr); 351 ierr = DMPlexCreateLabel(dmNew, "vtk");CHKERRQ(ierr); 352 ierr = DMPlexCreateLabel(dmNew, "ghost");CHKERRQ(ierr); 353 ierr = DMPlexGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr); 354 ierr = DMPlexGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr); 355 for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) { 356 for (; c < leafLocal[l] && c < cEnd; ++c) { 357 ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr); 358 } 359 if (leafLocal[l] >= cEnd) break; 360 if (leafRemote[l].rank == rank) { 361 ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr); 362 } else { 363 ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr); 364 } 365 } 366 for (; c < cEnd; ++c) { 367 ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr); 368 } 369 if (0) { 370 ierr = PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);CHKERRQ(ierr); 371 ierr = DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 372 ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 373 } 374 ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr); 375 for (f = fStart; f < fEnd; ++f) { 376 PetscInt numCells; 377 378 ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr); 379 if (numCells < 2) { 380 ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr); 381 } else { 382 const PetscInt *cells = NULL; 383 PetscInt vA, vB; 384 385 ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr); 386 ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr); 387 ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr); 388 if (!vA && !vB) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);} 389 } 390 } 391 if (0) { 392 ierr = PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);CHKERRQ(ierr); 393 ierr = DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 394 ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); 395 } 396 PetscFunctionReturn(0); 397 } 398 399 #undef __FUNCT__ 400 #define __FUNCT__ "DMPlexConstructGhostCells_Internal" 401 static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm) 402 { 403 IS valueIS; 404 const PetscInt *values; 405 PetscInt *depthShift; 406 PetscInt depth = 0, numFS, fs, fStart, fEnd, ghostCell, cEnd, c; 407 PetscErrorCode ierr; 408 409 PetscFunctionBegin; 410 ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr); 411 /* Count ghost cells */ 412 ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr); 413 ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr); 414 ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr); 415 *numGhostCells = 0; 416 for (fs = 0; fs < numFS; ++fs) { 417 IS faceIS; 418 const PetscInt *faces; 419 PetscInt numFaces, f, numBdFaces = 0; 420 421 ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr); 422 ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 423 ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 424 for (f = 0; f < numFaces; ++f) { 425 if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces; 426 } 427 *numGhostCells += numBdFaces; 428 ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 429 } 430 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 431 ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthShift);CHKERRQ(ierr); 432 ierr = PetscMemzero(depthShift, (depth+1) * sizeof(PetscInt));CHKERRQ(ierr); 433 if (depth >= 0) depthShift[depth] = *numGhostCells; 434 ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr); 435 /* Step 3: Set cone/support sizes for new points */ 436 ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr); 437 for (c = cEnd; c < cEnd + *numGhostCells; ++c) { 438 ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr); 439 } 440 for (fs = 0; fs < numFS; ++fs) { 441 IS faceIS; 442 const PetscInt *faces; 443 PetscInt numFaces, f; 444 445 ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr); 446 ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 447 ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 448 for (f = 0; f < numFaces; ++f) { 449 PetscInt size; 450 451 if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue; 452 ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr); 453 if (size != 1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size); 454 ierr = DMPlexSetSupportSize(gdm, faces[f] + *numGhostCells, 2);CHKERRQ(ierr); 455 } 456 ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 457 ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 458 } 459 /* Step 4: Setup ghosted DM */ 460 ierr = DMSetUp(gdm);CHKERRQ(ierr); 461 ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr); 462 /* Step 6: Set cones and supports for new points */ 463 ghostCell = cEnd; 464 for (fs = 0; fs < numFS; ++fs) { 465 IS faceIS; 466 const PetscInt *faces; 467 PetscInt numFaces, f; 468 469 ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr); 470 ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr); 471 ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr); 472 for (f = 0; f < numFaces; ++f) { 473 PetscInt newFace = faces[f] + *numGhostCells; 474 475 if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue; 476 ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr); 477 ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr); 478 ++ghostCell; 479 } 480 ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr); 481 ierr = ISDestroy(&faceIS);CHKERRQ(ierr); 482 } 483 ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr); 484 ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 485 /* Step 7: Stratify */ 486 ierr = DMPlexStratify(gdm);CHKERRQ(ierr); 487 ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr); 488 ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr); 489 ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr); 490 ierr = PetscFree(depthShift);CHKERRQ(ierr); 491 PetscFunctionReturn(0); 492 } 493 494 #undef __FUNCT__ 495 #define __FUNCT__ "DMPlexConstructGhostCells" 496 /*@C 497 DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face 498 499 Collective on dm 500 501 Input Parameters: 502 + dm - The original DM 503 - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL 504 505 Output Parameters: 506 + numGhostCells - The number of ghost cells added to the DM 507 - dmGhosted - The new DM 508 509 Note: If no label exists of that name, one will be created marking all boundary faces 510 511 Level: developer 512 513 .seealso: DMCreate() 514 @*/ 515 PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted) 516 { 517 DM gdm; 518 DMLabel label; 519 const char *name = labelName ? labelName : "Face Sets"; 520 PetscInt dim; 521 PetscErrorCode ierr; 522 523 PetscFunctionBegin; 524 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 525 PetscValidPointer(numGhostCells, 3); 526 PetscValidPointer(dmGhosted, 4); 527 ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr); 528 ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr); 529 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 530 ierr = DMPlexSetDimension(gdm, dim);CHKERRQ(ierr); 531 ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr); 532 if (!label) { 533 /* Get label for boundary faces */ 534 ierr = DMPlexCreateLabel(dm, name);CHKERRQ(ierr); 535 ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr); 536 ierr = DMPlexMarkBoundaryFaces(dm, label);CHKERRQ(ierr); 537 } 538 ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr); 539 ierr = DMSetFromOptions(gdm);CHKERRQ(ierr); 540 *dmGhosted = gdm; 541 PetscFunctionReturn(0); 542 } 543 544 #undef __FUNCT__ 545 #define __FUNCT__ "DMPlexConstructCohesiveCells_Internal" 546 /* 547 We are adding two kinds of points here: 548 Replicated: Copies of points which exist in the mesh, such as vertices identified across a fault 549 Hybrid: Entirely new points, such as cohesive cells 550 */ 551 static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DM sdm) 552 { 553 MPI_Comm comm; 554 IS valueIS; 555 PetscInt numSP = 0; /* The number of depths for which we have replicated points */ 556 const PetscInt *values; /* List of depths for which we have replicated points */ 557 IS *splitIS; 558 IS *unsplitIS; 559 PetscInt *numSplitPoints; /* The number of replicated points at each depth */ 560 PetscInt *numUnsplitPoints; /* The number of non-replicated points at each depth which still give rise to hybrid points */ 561 PetscInt *numHybridPoints; /* The number of hybrid points at each depth */ 562 const PetscInt **splitPoints; /* Replicated points for each depth */ 563 const PetscInt **unsplitPoints; /* Non-replicated points for each depth */ 564 PetscSection coordSection; 565 Vec coordinates; 566 PetscScalar *coords; 567 PetscInt depths[4]; /* Depths in the order that plex points are numbered */ 568 PetscInt *depthShift; /* Number of replicated+hybrid points at each depth */ 569 PetscInt *depthOffset; /* Prefix sums of depthShift */ 570 PetscInt *pMaxNew; /* The first replicated point at each depth in the new mesh, hybrids come after this */ 571 PetscInt *coneNew, *coneONew, *supportNew; 572 PetscInt shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v; 573 PetscErrorCode ierr; 574 575 PetscFunctionBegin; 576 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 577 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 578 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 579 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 580 depths[0] = depth; 581 depths[1] = 0; 582 depths[2] = depth-1; 583 depths[3] = 1; 584 /* Count split points and add cohesive cells */ 585 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr); 586 ierr = PetscMalloc3(depth+1,PetscInt,&depthShift,depth+1,PetscInt,&depthOffset,depth+1,PetscInt,&pMaxNew);CHKERRQ(ierr); 587 ierr = PetscMalloc7(depth+1,IS,&splitIS,depth+1,IS,&unsplitIS,depth+1,PetscInt,&numSplitPoints,depth+1,PetscInt,&numUnsplitPoints,depth+1,PetscInt,&numHybridPoints,depth+1,const PetscInt*,&splitPoints,depth+1,const PetscInt*,&unsplitPoints);CHKERRQ(ierr); 588 ierr = PetscMemzero(depthShift, (depth+1) * sizeof(PetscInt));CHKERRQ(ierr); 589 ierr = PetscMemzero(depthOffset, (depth+1) * sizeof(PetscInt));CHKERRQ(ierr); 590 for (d = 0; d <= depth; ++d) { 591 ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr); 592 numSplitPoints[d] = 0; 593 numUnsplitPoints[d] = 0; 594 numHybridPoints[d] = 0; 595 splitPoints[d] = NULL; 596 unsplitPoints[d] = NULL; 597 splitIS[d] = NULL; 598 unsplitIS[d] = NULL; 599 } 600 if (label) { 601 ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr); 602 ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr); 603 ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr); 604 } 605 for (sp = 0; sp < numSP; ++sp) { 606 const PetscInt dep = values[sp]; 607 608 if ((dep < 0) || (dep > depth)) continue; 609 ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr); 610 if (splitIS[dep]) { 611 ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr); 612 ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr); 613 } 614 ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr); 615 if (unsplitIS[dep]) { 616 ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr); 617 ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr); 618 } 619 } 620 /* Calculate number of hybrid points */ 621 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 */ 622 for (d = 0; d <= depth; ++d) depthShift[d] = numSplitPoints[d] + numHybridPoints[d]; 623 for (d = 1; d <= depth; ++d) depthOffset[depths[d]] = depthOffset[depths[d-1]] + depthShift[depths[d-1]]; 624 for (d = 0; d <= depth; ++d) pMaxNew[d] += depthOffset[d]; 625 ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr); 626 /* Step 3: Set cone/support sizes for new points */ 627 for (dep = 0; dep <= depth; ++dep) { 628 for (p = 0; p < numSplitPoints[dep]; ++p) { 629 const PetscInt oldp = splitPoints[dep][p]; 630 const PetscInt newp = oldp + depthOffset[dep]; 631 const PetscInt splitp = p + pMaxNew[dep]; 632 const PetscInt *support; 633 PetscInt coneSize, supportSize, qf, qn, qp, e; 634 635 ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr); 636 ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr); 637 ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr); 638 ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr); 639 if (dep == depth-1) { 640 const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 641 642 /* Add cohesive cells, they are prisms */ 643 ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr); 644 } else if (dep == 0) { 645 const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 646 647 ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr); 648 for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) { 649 PetscInt val; 650 651 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 652 if (val == 1) ++qf; 653 if ((val == 1) || (val == (shift + 1))) ++qn; 654 if ((val == 1) || (val == -(shift + 1))) ++qp; 655 } 656 /* Split old vertex: Edges into original vertex and new cohesive edge */ 657 ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr); 658 /* Split new vertex: Edges into split vertex and new cohesive edge */ 659 ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr); 660 /* Add hybrid edge */ 661 ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr); 662 ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr); 663 } else if (dep == dim-2) { 664 const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 665 666 ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr); 667 for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) { 668 PetscInt val; 669 670 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 671 if (val == dim-1) ++qf; 672 if ((val == dim-1) || (val == (shift + dim-1))) ++qn; 673 if ((val == dim-1) || (val == -(shift + dim-1))) ++qp; 674 } 675 /* Split old edge: Faces into original edge and cohesive face (positive side?) */ 676 ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr); 677 /* Split new edge: Faces into split edge and cohesive face (negative side?) */ 678 ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr); 679 /* Add hybrid face */ 680 ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr); 681 ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr); 682 } 683 } 684 } 685 for (dep = 0; dep <= depth; ++dep) { 686 for (p = 0; p < numUnsplitPoints[dep]; ++p) { 687 const PetscInt oldp = unsplitPoints[dep][p]; 688 const PetscInt newp = oldp + depthOffset[dep]; 689 const PetscInt *support; 690 PetscInt coneSize, supportSize, qf, qn, qp, e; 691 692 ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr); 693 ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr); 694 if (dep == 0) { 695 const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep]; 696 697 /* Unsplit vertex: Edges into original vertex, split edge, and new cohesive edge twice */ 698 ierr = DMPlexSetSupportSize(sdm, newp, supportSize+3);CHKERRQ(ierr); 699 /* Add hybrid edge */ 700 ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr); 701 ierr = DMPlexSetSupportSize(sdm, hybedge, 1);CHKERRQ(ierr); 702 } else if (dep == dim-2) { 703 const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep]; 704 705 ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr); 706 for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) { 707 PetscInt val; 708 709 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 710 if (val == dim-1) ++qf; 711 if ((val == dim-1) || (val == (shift + dim-1))) ++qn; 712 if ((val == dim-1) || (val == -(shift + dim-1))) ++qp; 713 } 714 /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */ 715 ierr = DMPlexSetSupportSize(sdm, newp, supportSize+3);CHKERRQ(ierr); 716 /* Add hybrid face */ 717 ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr); 718 ierr = DMPlexSetSupportSize(sdm, hybface, 1);CHKERRQ(ierr); 719 } 720 } 721 } 722 /* Step 4: Setup split DM */ 723 ierr = DMSetUp(sdm);CHKERRQ(ierr); 724 ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr); 725 ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr); 726 ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,PetscInt,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,PetscInt,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),PetscInt,&supportNew);CHKERRQ(ierr); 727 /* Step 6: Set cones and supports for new points */ 728 for (dep = 0; dep <= depth; ++dep) { 729 for (p = 0; p < numSplitPoints[dep]; ++p) { 730 const PetscInt oldp = splitPoints[dep][p]; 731 const PetscInt newp = oldp + depthOffset[dep]; 732 const PetscInt splitp = p + pMaxNew[dep]; 733 const PetscInt *cone, *support, *ornt; 734 PetscInt coneSize, supportSize, q, qf, qn, qp, v, e, s; 735 736 ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr); 737 ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr); 738 ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr); 739 ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr); 740 ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr); 741 if (dep == depth-1) { 742 const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 743 const PetscInt *supportF; 744 745 /* Split face: copy in old face to new face to start */ 746 ierr = DMPlexGetSupport(sdm, newp, &supportF);CHKERRQ(ierr); 747 ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr); 748 /* Split old face: old vertices/edges in cone so no change */ 749 /* Split new face: new vertices/edges in cone */ 750 for (q = 0; q < coneSize; ++q) { 751 ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr); 752 if (v < 0) { 753 ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr); 754 if (v < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1); 755 coneNew[2+q] = cone[q] + depthOffset[dep-1]; 756 } else { 757 coneNew[2+q] = v + pMaxNew[dep-1]; 758 } 759 } 760 ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr); 761 ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr); 762 /* Face support */ 763 for (s = 0; s < supportSize; ++s) { 764 PetscInt val; 765 766 ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr); 767 if (val < 0) { 768 /* Split old face: Replace negative side cell with cohesive cell */ 769 ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr); 770 } else { 771 /* Split new face: Replace positive side cell with cohesive cell */ 772 ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr); 773 /* Get orientation for cohesive face */ 774 { 775 const PetscInt *ncone, *nconeO; 776 PetscInt nconeSize, nc; 777 778 ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr); 779 ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr); 780 ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr); 781 for (nc = 0; nc < nconeSize; ++nc) { 782 if (ncone[nc] == oldp) { 783 coneONew[0] = nconeO[nc]; 784 break; 785 } 786 } 787 if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]); 788 } 789 } 790 } 791 /* Cohesive cell: Old and new split face, then new cohesive faces */ 792 coneNew[0] = newp; /* Extracted negative side orientation above */ 793 coneNew[1] = splitp; 794 coneONew[1] = coneONew[0]; 795 for (q = 0; q < coneSize; ++q) { 796 ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr); 797 if (v < 0) { 798 ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr); 799 coneNew[2+q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1]; 800 coneONew[2+q] = 0; 801 } else { 802 coneNew[2+q] = v + pMaxNew[dep] + numSplitPoints[dep]; 803 } 804 coneONew[2+q] = 0; 805 } 806 ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr); 807 ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr); 808 } else if (dep == 0) { 809 const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 810 811 /* Split old vertex: Edges in old split faces and new cohesive edge */ 812 for (e = 0, qn = 0; e < supportSize; ++e) { 813 PetscInt val; 814 815 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 816 if ((val == 1) || (val == (shift + 1))) { 817 supportNew[qn++] = support[e] + depthOffset[dep+1]; 818 } 819 } 820 supportNew[qn] = hybedge; 821 ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr); 822 /* Split new vertex: Edges in new split faces and new cohesive edge */ 823 for (e = 0, qp = 0; e < supportSize; ++e) { 824 PetscInt val, edge; 825 826 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 827 if (val == 1) { 828 ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr); 829 if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]); 830 supportNew[qp++] = edge + pMaxNew[dep+1]; 831 } else if (val == -(shift + 1)) { 832 supportNew[qp++] = support[e] + depthOffset[dep+1]; 833 } 834 } 835 supportNew[qp] = hybedge; 836 ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr); 837 /* Hybrid edge: Old and new split vertex */ 838 coneNew[0] = newp; 839 coneNew[1] = splitp; 840 ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr); 841 for (e = 0, qf = 0; e < supportSize; ++e) { 842 PetscInt val, edge; 843 844 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 845 if (val == 1) { 846 ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr); 847 if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]); 848 supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2]; 849 } 850 } 851 ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr); 852 } else if (dep == dim-2) { 853 const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 854 855 /* Split old edge: old vertices in cone so no change */ 856 /* Split new edge: new vertices in cone */ 857 for (q = 0; q < coneSize; ++q) { 858 ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr); 859 860 coneNew[q] = v + pMaxNew[dep-1]; 861 } 862 ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr); 863 /* Split old edge: Faces in positive side cells and old split faces */ 864 for (e = 0, q = 0; e < supportSize; ++e) { 865 PetscInt val; 866 867 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 868 if (val == dim-1) { 869 supportNew[q++] = support[e] + depthOffset[dep+1]; 870 } else if (val == (shift + dim-1)) { 871 supportNew[q++] = support[e] + depthOffset[dep+1]; 872 } 873 } 874 supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 875 ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr); 876 /* Split new edge: Faces in negative side cells and new split faces */ 877 for (e = 0, q = 0; e < supportSize; ++e) { 878 PetscInt val, face; 879 880 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 881 if (val == dim-1) { 882 ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr); 883 if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]); 884 supportNew[q++] = face + pMaxNew[dep+1]; 885 } else if (val == -(shift + dim-1)) { 886 supportNew[q++] = support[e] + depthOffset[dep+1]; 887 } 888 } 889 supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 890 ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr); 891 /* Hybrid face */ 892 coneNew[0] = newp; 893 coneNew[1] = splitp; 894 for (v = 0; v < coneSize; ++v) { 895 PetscInt vertex; 896 ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr); 897 if (vertex < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not a split vertex", support[e]); 898 coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep]; 899 } 900 ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr); 901 for (e = 0, qf = 0; e < supportSize; ++e) { 902 PetscInt val, face; 903 904 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 905 if (val == dim-1) { 906 ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr); 907 if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]); 908 supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2]; 909 } 910 } 911 ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr); 912 } 913 } 914 } 915 for (dep = 0; dep <= depth; ++dep) { 916 for (p = 0; p < numUnsplitPoints[dep]; ++p) { 917 const PetscInt oldp = unsplitPoints[dep][p]; 918 const PetscInt newp = oldp + depthOffset[dep]; 919 const PetscInt *cone, *support, *ornt; 920 PetscInt coneSize, supportSize, supportSizeNew, q, qf, e, s; 921 922 ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr); 923 ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr); 924 ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr); 925 ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr); 926 ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr); 927 if (dep == 0) { 928 const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep]; 929 930 /* Unsplit vertex */ 931 ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr); 932 for (s = 0, q = 0; s < supportSize; ++s) { 933 supportNew[q++] = support[s] + depthOffset[dep+1]; 934 ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr); 935 if (e >= 0) { 936 supportNew[q++] = e + pMaxNew[dep+1]; 937 } 938 } 939 supportNew[q++] = hybedge; 940 supportNew[q++] = hybedge; 941 if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp); 942 ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr); 943 /* Hybrid edge */ 944 coneNew[0] = newp; 945 coneNew[1] = newp; 946 ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr); 947 for (e = 0, qf = 0; e < supportSize; ++e) { 948 PetscInt val, edge; 949 950 ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr); 951 if (val == 1) { 952 ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr); 953 if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]); 954 supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2]; 955 } 956 } 957 ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr); 958 } 959 } 960 } 961 /* Step 6b: Replace split points in negative side cones */ 962 for (sp = 0; sp < numSP; ++sp) { 963 PetscInt dep = values[sp]; 964 IS pIS; 965 PetscInt numPoints; 966 const PetscInt *points; 967 968 if (dep >= 0) continue; 969 ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr); 970 if (!pIS) continue; 971 dep = -dep - shift; 972 ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr); 973 ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr); 974 for (p = 0; p < numPoints; ++p) { 975 const PetscInt oldp = points[p]; 976 const PetscInt newp = depthOffset[dep] + oldp; 977 const PetscInt *cone; 978 PetscInt coneSize, c; 979 /* PetscBool replaced = PETSC_FALSE; */ 980 981 /* Negative edge: replace split vertex */ 982 /* Negative cell: replace split face */ 983 ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr); 984 ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr); 985 for (c = 0; c < coneSize; ++c) { 986 const PetscInt coldp = cone[c] - depthOffset[dep-1]; 987 PetscInt csplitp, cp, val; 988 989 ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr); 990 if (val == dep-1) { 991 ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr); 992 if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1); 993 csplitp = pMaxNew[dep-1] + cp; 994 ierr = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr); 995 /* replaced = PETSC_TRUE; */ 996 } 997 } 998 /* Cells with only a vertex or edge on the submesh have no replacement */ 999 /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */ 1000 } 1001 ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr); 1002 ierr = ISDestroy(&pIS);CHKERRQ(ierr); 1003 } 1004 /* Step 7: Stratify */ 1005 ierr = DMPlexStratify(sdm);CHKERRQ(ierr); 1006 /* Step 8: Coordinates */ 1007 ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr); 1008 ierr = DMPlexGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr); 1009 ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr); 1010 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 1011 for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) { 1012 const PetscInt newp = depthOffset[0] + splitPoints[0][v]; 1013 const PetscInt splitp = pMaxNew[0] + v; 1014 PetscInt dof, off, soff, d; 1015 1016 ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr); 1017 ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr); 1018 ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr); 1019 for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d]; 1020 } 1021 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 1022 /* Step 9: SF, if I can figure this out we can split the mesh in parallel */ 1023 ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr); 1024 /* Step 10: Labels */ 1025 ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr); 1026 ierr = DMPlexGetNumLabels(sdm, &numLabels);CHKERRQ(ierr); 1027 for (dep = 0; dep <= depth; ++dep) { 1028 for (p = 0; p < numSplitPoints[dep]; ++p) { 1029 const PetscInt newp = depthOffset[dep] + splitPoints[dep][p]; 1030 const PetscInt splitp = pMaxNew[dep] + p; 1031 PetscInt l; 1032 1033 for (l = 0; l < numLabels; ++l) { 1034 DMLabel mlabel; 1035 const char *lname; 1036 PetscInt val; 1037 PetscBool isDepth; 1038 1039 ierr = DMPlexGetLabelName(sdm, l, &lname);CHKERRQ(ierr); 1040 ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr); 1041 if (isDepth) continue; 1042 ierr = DMPlexGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr); 1043 ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr); 1044 if (val >= 0) { 1045 ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr); 1046 #if 0 1047 /* Do not put cohesive edges into the label */ 1048 if (dep == 0) { 1049 const PetscInt cedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 1050 ierr = DMLabelSetValue(mlabel, cedge, val);CHKERRQ(ierr); 1051 } else if (dep == dim-2) { 1052 const PetscInt cface = p + pMaxNew[dep+1] + numSplitPoints[dep+1]; 1053 ierr = DMLabelSetValue(mlabel, cface, val);CHKERRQ(ierr); 1054 } 1055 /* Do not put cohesive faces into the label */ 1056 #endif 1057 } 1058 } 1059 } 1060 } 1061 for (sp = 0; sp < numSP; ++sp) { 1062 const PetscInt dep = values[sp]; 1063 1064 if ((dep < 0) || (dep > depth)) continue; 1065 if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);} 1066 ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr); 1067 if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);} 1068 ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr); 1069 } 1070 if (label) { 1071 ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr); 1072 ierr = ISDestroy(&valueIS);CHKERRQ(ierr); 1073 } 1074 for (d = 0; d <= depth; ++d) { 1075 ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr); 1076 pMaxNew[d] = pEnd - numHybridPoints[d]; 1077 } 1078 ierr = DMPlexSetHybridBounds(sdm, depth >= 0 ? pMaxNew[depth] : PETSC_DETERMINE, depth>1 ? pMaxNew[depth-1] : PETSC_DETERMINE, depth>2 ? pMaxNew[1] : PETSC_DETERMINE, depth >= 0 ? pMaxNew[0] : PETSC_DETERMINE);CHKERRQ(ierr); 1079 ierr = PetscFree3(depthShift, depthOffset, pMaxNew);CHKERRQ(ierr); 1080 ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr); 1081 ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr); 1082 PetscFunctionReturn(0); 1083 } 1084 1085 #undef __FUNCT__ 1086 #define __FUNCT__ "DMPlexConstructCohesiveCells" 1087 /*@C 1088 DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface 1089 1090 Collective on dm 1091 1092 Input Parameters: 1093 + dm - The original DM 1094 - label - The label specifying the boundary faces (this could be auto-generated) 1095 1096 Output Parameters: 1097 - dmSplit - The new DM 1098 1099 Level: developer 1100 1101 .seealso: DMCreate(), DMPlexLabelCohesiveComplete() 1102 @*/ 1103 PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DM *dmSplit) 1104 { 1105 DM sdm; 1106 PetscInt dim; 1107 PetscErrorCode ierr; 1108 1109 PetscFunctionBegin; 1110 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1111 PetscValidPointer(dmSplit, 3); 1112 ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr); 1113 ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr); 1114 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1115 ierr = DMPlexSetDimension(sdm, dim);CHKERRQ(ierr); 1116 switch (dim) { 1117 case 2: 1118 case 3: 1119 ierr = DMPlexConstructCohesiveCells_Internal(dm, label, sdm);CHKERRQ(ierr); 1120 break; 1121 default: 1122 SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim); 1123 } 1124 *dmSplit = sdm; 1125 PetscFunctionReturn(0); 1126 } 1127 1128 #undef __FUNCT__ 1129 #define __FUNCT__ "DMPlexLabelCohesiveComplete" 1130 /*@ 1131 DMPlexLabelCohesiveComplete - Starting with a label marking vertices on an internal surface, we add all other mesh pieces 1132 to complete the surface 1133 1134 Input Parameters: 1135 + dm - The DM 1136 . label - A DMLabel marking the surface vertices 1137 . flip - Flag to flip the submesh normal and replace points on the other side 1138 - subdm - The subDM associated with the label, or NULL 1139 1140 Output Parameter: 1141 . label - A DMLabel marking all surface points 1142 1143 Level: developer 1144 1145 .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete() 1146 @*/ 1147 PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, PetscBool flip, DM subdm) 1148 { 1149 DMLabel depthLabel; 1150 IS dimIS, subpointIS, facePosIS, faceNegIS; 1151 const PetscInt *points, *subpoints; 1152 const PetscInt rev = flip ? -1 : 1; 1153 PetscInt shift = 100, shift2 = 200, dim, dep, cStart, cEnd, numPoints, numSubpoints, p, val; 1154 PetscErrorCode ierr; 1155 1156 PetscFunctionBegin; 1157 ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr); 1158 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1159 if (subdm) { 1160 ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr); 1161 if (subpointIS) { 1162 ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr); 1163 ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr); 1164 } 1165 } 1166 /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */ 1167 ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr); 1168 if (!dimIS) PetscFunctionReturn(0); 1169 ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr); 1170 ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr); 1171 for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */ 1172 const PetscInt *support; 1173 PetscInt supportSize, s; 1174 1175 ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr); 1176 if (supportSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports", points[p], supportSize); 1177 ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr); 1178 for (s = 0; s < supportSize; ++s) { 1179 const PetscInt *cone, *ornt; 1180 PetscInt coneSize, c; 1181 PetscBool pos = PETSC_TRUE; 1182 1183 ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr); 1184 ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr); 1185 ierr = DMPlexGetConeOrientation(dm, support[s], &ornt);CHKERRQ(ierr); 1186 for (c = 0; c < coneSize; ++c) { 1187 if (cone[c] == points[p]) { 1188 PetscInt o = ornt[c]; 1189 1190 if (subdm) { 1191 const PetscInt *subcone, *subornt; 1192 PetscInt subpoint, subface, subconeSize, sc; 1193 1194 ierr = PetscFindInt(support[s], numSubpoints, subpoints, &subpoint);CHKERRQ(ierr); 1195 ierr = PetscFindInt(points[p], numSubpoints, subpoints, &subface);CHKERRQ(ierr); 1196 ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr); 1197 ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr); 1198 ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr); 1199 for (sc = 0; sc < subconeSize; ++sc) { 1200 if (subcone[sc] == subface) { 1201 o = subornt[0]; 1202 break; 1203 } 1204 } 1205 if (sc >= subconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find point %d in cone for subpoint %d", points[p], subpoint); 1206 } 1207 if (o >= 0) { 1208 ierr = DMLabelSetValue(label, support[s], rev*(shift+dim));CHKERRQ(ierr); 1209 pos = rev > 0 ? PETSC_TRUE : PETSC_FALSE; 1210 } else { 1211 ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr); 1212 pos = rev > 0 ? PETSC_FALSE : PETSC_TRUE; 1213 } 1214 break; 1215 } 1216 } 1217 if (c == coneSize) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell split face %d support does not have it in the cone", points[p]); 1218 /* Put faces touching the fault in the label */ 1219 for (c = 0; c < coneSize; ++c) { 1220 const PetscInt point = cone[c]; 1221 1222 ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr); 1223 if (val == -1) { 1224 PetscInt *closure = NULL; 1225 PetscInt closureSize, cl; 1226 1227 ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1228 for (cl = 0; cl < closureSize*2; cl += 2) { 1229 const PetscInt clp = closure[cl]; 1230 1231 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr); 1232 if ((val >= 0) && (val < dim-1)) { 1233 ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr); 1234 break; 1235 } 1236 } 1237 ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1238 } 1239 } 1240 } 1241 } 1242 if (subdm) { 1243 if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);} 1244 ierr = ISDestroy(&subpointIS);CHKERRQ(ierr); 1245 } 1246 ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr); 1247 ierr = ISDestroy(&dimIS);CHKERRQ(ierr); 1248 /* Search for other cells/faces/edges connected to the fault by a vertex */ 1249 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); 1250 ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr); 1251 if (!dimIS) PetscFunctionReturn(0); 1252 ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr); 1253 ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr); 1254 for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */ 1255 PetscInt *star = NULL; 1256 PetscInt starSize, s; 1257 PetscInt again = 1; /* 0: Finished 1: Keep iterating after a change 2: No change */ 1258 1259 /* All points connected to the fault are inside a cell, so at the top level we will only check cells */ 1260 ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1261 while (again) { 1262 if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault"); 1263 again = 0; 1264 for (s = 0; s < starSize*2; s += 2) { 1265 const PetscInt point = star[s]; 1266 const PetscInt *cone; 1267 PetscInt coneSize, c; 1268 1269 if ((point < cStart) || (point >= cEnd)) continue; 1270 ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr); 1271 if (val != -1) continue; 1272 again = again == 1 ? 1 : 2; 1273 ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 1274 ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 1275 for (c = 0; c < coneSize; ++c) { 1276 ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr); 1277 if (val != -1) { 1278 const PetscInt *ccone; 1279 PetscInt cconeSize, cc, side; 1280 1281 if (abs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val); 1282 if (val > 0) side = 1; 1283 else side = -1; 1284 ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr); 1285 /* Mark cell faces which touch the fault */ 1286 ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr); 1287 ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr); 1288 for (cc = 0; cc < cconeSize; ++cc) { 1289 PetscInt *closure = NULL; 1290 PetscInt closureSize, cl; 1291 1292 ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr); 1293 if (val != -1) continue; 1294 ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1295 for (cl = 0; cl < closureSize*2; cl += 2) { 1296 const PetscInt clp = closure[cl]; 1297 1298 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr); 1299 if (val == -1) continue; 1300 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr); 1301 break; 1302 } 1303 ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1304 } 1305 again = 1; 1306 break; 1307 } 1308 } 1309 } 1310 } 1311 /* Classify the rest by cell membership */ 1312 for (s = 0; s < starSize*2; s += 2) { 1313 const PetscInt point = star[s]; 1314 1315 ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr); 1316 if (val == -1) { 1317 PetscInt *sstar = NULL; 1318 PetscInt sstarSize, ss; 1319 PetscBool marked = PETSC_FALSE; 1320 1321 ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr); 1322 for (ss = 0; ss < sstarSize*2; ss += 2) { 1323 const PetscInt spoint = sstar[ss]; 1324 1325 if ((spoint < cStart) || (spoint >= cEnd)) continue; 1326 ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr); 1327 if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point); 1328 ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr); 1329 if (val > 0) { 1330 ierr = DMLabelSetValue(label, point, shift+dep);CHKERRQ(ierr); 1331 } else { 1332 ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr); 1333 } 1334 marked = PETSC_TRUE; 1335 break; 1336 } 1337 ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr); 1338 if (!marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point); 1339 } 1340 } 1341 ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1342 } 1343 ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr); 1344 ierr = ISDestroy(&dimIS);CHKERRQ(ierr); 1345 /* If any faces touching the fault divide cells on either side, split them */ 1346 ierr = DMLabelGetStratumIS(label, shift+dim-1, &facePosIS);CHKERRQ(ierr); 1347 ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr); 1348 ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr); 1349 ierr = ISDestroy(&facePosIS);CHKERRQ(ierr); 1350 ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr); 1351 ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr); 1352 ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr); 1353 for (p = 0; p < numPoints; ++p) { 1354 const PetscInt point = points[p]; 1355 const PetscInt *support; 1356 PetscInt supportSize, valA, valB; 1357 1358 ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr); 1359 if (supportSize != 2) continue; 1360 ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr); 1361 ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr); 1362 ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr); 1363 if ((valA == -1) || (valB == -1)) continue; 1364 if (valA*valB > 0) continue; 1365 /* Split the face */ 1366 ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr); 1367 ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr); 1368 ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr); 1369 /* Label its closure as unsplit */ 1370 { 1371 PetscInt *closure = NULL; 1372 PetscInt closureSize, cl; 1373 1374 ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1375 for (cl = 0; cl < closureSize*2; cl += 2) { 1376 ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr); 1377 if (valA != -1) continue; 1378 ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr); 1379 ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr); 1380 } 1381 ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1382 } 1383 } 1384 ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr); 1385 ierr = ISDestroy(&dimIS);CHKERRQ(ierr); 1386 PetscFunctionReturn(0); 1387 } 1388 1389 #undef __FUNCT__ 1390 #define __FUNCT__ "DMPlexCreateHybridMesh" 1391 /*@C 1392 DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface 1393 1394 Collective on dm 1395 1396 Input Parameters: 1397 + dm - The original DM 1398 - labelName - The label specifying the interface vertices 1399 1400 Output Parameters: 1401 + hybridLabel - The label fully marking the interface 1402 - dmHybrid - The new DM 1403 1404 Level: developer 1405 1406 .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate() 1407 @*/ 1408 PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel *hybridLabel, DM *dmHybrid) 1409 { 1410 DM idm; 1411 DMLabel subpointMap, hlabel; 1412 PetscInt dim; 1413 PetscErrorCode ierr; 1414 1415 PetscFunctionBegin; 1416 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1417 if (hybridLabel) PetscValidPointer(hybridLabel, 3); 1418 PetscValidPointer(dmHybrid, 4); 1419 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1420 ierr = DMPlexCreateSubmesh(dm, label, 1, &idm);CHKERRQ(ierr); 1421 ierr = DMPlexOrient(idm);CHKERRQ(ierr); 1422 ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr); 1423 ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr); 1424 ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr); 1425 ierr = DMPlexLabelCohesiveComplete(dm, hlabel, PETSC_FALSE, idm);CHKERRQ(ierr); 1426 ierr = DMDestroy(&idm);CHKERRQ(ierr); 1427 ierr = DMPlexConstructCohesiveCells(dm, hlabel, dmHybrid);CHKERRQ(ierr); 1428 if (hybridLabel) *hybridLabel = hlabel; 1429 else {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);} 1430 PetscFunctionReturn(0); 1431 } 1432 1433 #undef __FUNCT__ 1434 #define __FUNCT__ "DMPlexMarkSubmesh_Uninterpolated" 1435 /* Here we need the explicit assumption that: 1436 1437 For any marked cell, the marked vertices constitute a single face 1438 */ 1439 static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm) 1440 { 1441 IS subvertexIS = NULL; 1442 const PetscInt *subvertices; 1443 PetscInt *pStart, *pEnd, *pMax, pSize; 1444 PetscInt depth, dim, d, numSubVerticesInitial = 0, v; 1445 PetscErrorCode ierr; 1446 1447 PetscFunctionBegin; 1448 *numFaces = 0; 1449 *nFV = 0; 1450 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 1451 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1452 pSize = PetscMax(depth, dim) + 1; 1453 ierr = PetscMalloc3(pSize,PetscInt,&pStart,pSize,PetscInt,&pEnd,pSize,PetscInt,&pMax);CHKERRQ(ierr); 1454 ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr); 1455 for (d = 0; d <= depth; ++d) { 1456 ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr); 1457 if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]); 1458 } 1459 /* Loop over initial vertices and mark all faces in the collective star() */ 1460 if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);} 1461 if (subvertexIS) { 1462 ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr); 1463 ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr); 1464 } 1465 for (v = 0; v < numSubVerticesInitial; ++v) { 1466 const PetscInt vertex = subvertices[v]; 1467 PetscInt *star = NULL; 1468 PetscInt starSize, s, numCells = 0, c; 1469 1470 ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1471 for (s = 0; s < starSize*2; s += 2) { 1472 const PetscInt point = star[s]; 1473 if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point; 1474 } 1475 for (c = 0; c < numCells; ++c) { 1476 const PetscInt cell = star[c]; 1477 PetscInt *closure = NULL; 1478 PetscInt closureSize, cl; 1479 PetscInt cellLoc, numCorners = 0, faceSize = 0; 1480 1481 ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr); 1482 if (cellLoc == 2) continue; 1483 if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc); 1484 ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1485 for (cl = 0; cl < closureSize*2; cl += 2) { 1486 const PetscInt point = closure[cl]; 1487 PetscInt vertexLoc; 1488 1489 if ((point >= pStart[0]) && (point < pEnd[0])) { 1490 ++numCorners; 1491 ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr); 1492 if (vertexLoc == value) closure[faceSize++] = point; 1493 } 1494 } 1495 if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);} 1496 if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize); 1497 if (faceSize == *nFV) { 1498 const PetscInt *cells = NULL; 1499 PetscInt numCells, nc; 1500 1501 ++(*numFaces); 1502 for (cl = 0; cl < faceSize; ++cl) { 1503 ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr); 1504 } 1505 ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr); 1506 for (nc = 0; nc < numCells; ++nc) { 1507 ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr); 1508 } 1509 ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr); 1510 } 1511 ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1512 } 1513 ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1514 } 1515 if (subvertexIS) { 1516 ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr); 1517 } 1518 ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr); 1519 ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr); 1520 PetscFunctionReturn(0); 1521 } 1522 1523 #undef __FUNCT__ 1524 #define __FUNCT__ "DMPlexMarkSubmesh_Interpolated" 1525 static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, DM subdm) 1526 { 1527 IS subvertexIS; 1528 const PetscInt *subvertices; 1529 PetscInt *pStart, *pEnd, *pMax; 1530 PetscInt dim, d, numSubVerticesInitial = 0, v; 1531 PetscErrorCode ierr; 1532 1533 PetscFunctionBegin; 1534 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1535 ierr = PetscMalloc3(dim+1,PetscInt,&pStart,dim+1,PetscInt,&pEnd,dim+1,PetscInt,&pMax);CHKERRQ(ierr); 1536 ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr); 1537 for (d = 0; d <= dim; ++d) { 1538 ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr); 1539 if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]); 1540 } 1541 /* Loop over initial vertices and mark all faces in the collective star() */ 1542 ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr); 1543 if (subvertexIS) { 1544 ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr); 1545 ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr); 1546 } 1547 for (v = 0; v < numSubVerticesInitial; ++v) { 1548 const PetscInt vertex = subvertices[v]; 1549 PetscInt *star = NULL; 1550 PetscInt starSize, s, numFaces = 0, f; 1551 1552 ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1553 for (s = 0; s < starSize*2; s += 2) { 1554 const PetscInt point = star[s]; 1555 if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) star[numFaces++] = point; 1556 } 1557 for (f = 0; f < numFaces; ++f) { 1558 const PetscInt face = star[f]; 1559 PetscInt *closure = NULL; 1560 PetscInt closureSize, c; 1561 PetscInt faceLoc; 1562 1563 ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr); 1564 if (faceLoc == dim-1) continue; 1565 if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc); 1566 ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1567 for (c = 0; c < closureSize*2; c += 2) { 1568 const PetscInt point = closure[c]; 1569 PetscInt vertexLoc; 1570 1571 if ((point >= pStart[0]) && (point < pEnd[0])) { 1572 ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr); 1573 if (vertexLoc != value) break; 1574 } 1575 } 1576 if (c == closureSize*2) { 1577 const PetscInt *support; 1578 PetscInt supportSize, s; 1579 1580 for (c = 0; c < closureSize*2; c += 2) { 1581 const PetscInt point = closure[c]; 1582 1583 for (d = 0; d < dim; ++d) { 1584 if ((point >= pStart[d]) && (point < pEnd[d])) { 1585 ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr); 1586 break; 1587 } 1588 } 1589 } 1590 ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr); 1591 ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr); 1592 for (s = 0; s < supportSize; ++s) { 1593 ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr); 1594 } 1595 } 1596 ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1597 } 1598 ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr); 1599 } 1600 if (subvertexIS) { 1601 ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr); 1602 } 1603 ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr); 1604 ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr); 1605 PetscFunctionReturn(0); 1606 } 1607 1608 #undef __FUNCT__ 1609 #define __FUNCT__ "DMPlexMarkCohesiveSubmesh_Uninterpolated" 1610 static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm) 1611 { 1612 DMLabel label = NULL; 1613 const PetscInt *cone; 1614 PetscInt dim, cMax, cEnd, c, subc = 0, p, coneSize; 1615 PetscErrorCode ierr; 1616 1617 PetscFunctionBegin; 1618 *numFaces = 0; 1619 *nFV = 0; 1620 if (labelname) {ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);} 1621 *subCells = NULL; 1622 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1623 ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr); 1624 ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1625 if (cMax < 0) PetscFunctionReturn(0); 1626 if (label) { 1627 for (c = cMax; c < cEnd; ++c) { 1628 PetscInt val; 1629 1630 ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr); 1631 if (val == value) { 1632 ++(*numFaces); 1633 ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr); 1634 } 1635 } 1636 } else { 1637 *numFaces = cEnd - cMax; 1638 ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr); 1639 } 1640 *nFV = hasLagrange ? coneSize/3 : coneSize/2; 1641 ierr = PetscMalloc(*numFaces *2 * sizeof(PetscInt), subCells);CHKERRQ(ierr); 1642 for (c = cMax; c < cEnd; ++c) { 1643 const PetscInt *cells; 1644 PetscInt numCells; 1645 1646 if (label) { 1647 PetscInt val; 1648 1649 ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr); 1650 if (val != value) continue; 1651 } 1652 ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr); 1653 for (p = 0; p < *nFV; ++p) { 1654 ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr); 1655 } 1656 /* Negative face */ 1657 ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr); 1658 /* Not true in parallel 1659 if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */ 1660 for (p = 0; p < numCells; ++p) { 1661 ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr); 1662 (*subCells)[subc++] = cells[p]; 1663 } 1664 ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr); 1665 /* Positive face is not included */ 1666 } 1667 PetscFunctionReturn(0); 1668 } 1669 1670 #undef __FUNCT__ 1671 #define __FUNCT__ "DMPlexMarkCohesiveSubmesh_Interpolated" 1672 static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DMLabel subpointMap, DM subdm) 1673 { 1674 DMLabel label = NULL; 1675 PetscInt *pStart, *pEnd; 1676 PetscInt dim, cMax, cEnd, c, d; 1677 PetscErrorCode ierr; 1678 1679 PetscFunctionBegin; 1680 if (labelname) {ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);} 1681 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 1682 ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr); 1683 ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 1684 if (cMax < 0) PetscFunctionReturn(0); 1685 ierr = PetscMalloc2(dim+1,PetscInt,&pStart,dim+1,PetscInt,&pEnd);CHKERRQ(ierr); 1686 for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);} 1687 for (c = cMax; c < cEnd; ++c) { 1688 const PetscInt *cone; 1689 PetscInt *closure = NULL; 1690 PetscInt fconeSize, coneSize, closureSize, cl, val; 1691 1692 if (label) { 1693 ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr); 1694 if (val != value) continue; 1695 } 1696 ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr); 1697 ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr); 1698 ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr); 1699 if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); 1700 /* Negative face */ 1701 ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1702 for (cl = 0; cl < closureSize*2; cl += 2) { 1703 const PetscInt point = closure[cl]; 1704 1705 for (d = 0; d <= dim; ++d) { 1706 if ((point >= pStart[d]) && (point < pEnd[d])) { 1707 ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr); 1708 break; 1709 } 1710 } 1711 } 1712 ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 1713 /* Cells -- positive face is not included */ 1714 for (cl = 0; cl < 1; ++cl) { 1715 const PetscInt *support; 1716 PetscInt supportSize, s; 1717 1718 ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr); 1719 if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); 1720 ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr); 1721 for (s = 0; s < supportSize; ++s) { 1722 ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr); 1723 } 1724 } 1725 } 1726 ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr); 1727 PetscFunctionReturn(0); 1728 } 1729 1730 #undef __FUNCT__ 1731 #define __FUNCT__ "DMPlexGetFaceOrientation" 1732 PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) 1733 { 1734 MPI_Comm comm; 1735 PetscBool posOrient = PETSC_FALSE; 1736 const PetscInt debug = 0; 1737 PetscInt cellDim, faceSize, f; 1738 PetscErrorCode ierr; 1739 1740 PetscFunctionBegin; 1741 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 1742 ierr = DMPlexGetDimension(dm, &cellDim);CHKERRQ(ierr); 1743 if (debug) {PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);} 1744 1745 if (cellDim == 1 && numCorners == 2) { 1746 /* Triangle */ 1747 faceSize = numCorners-1; 1748 posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE; 1749 } else if (cellDim == 2 && numCorners == 3) { 1750 /* Triangle */ 1751 faceSize = numCorners-1; 1752 posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE; 1753 } else if (cellDim == 3 && numCorners == 4) { 1754 /* Tetrahedron */ 1755 faceSize = numCorners-1; 1756 posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE; 1757 } else if (cellDim == 1 && numCorners == 3) { 1758 /* Quadratic line */ 1759 faceSize = 1; 1760 posOrient = PETSC_TRUE; 1761 } else if (cellDim == 2 && numCorners == 4) { 1762 /* Quads */ 1763 faceSize = 2; 1764 if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) { 1765 posOrient = PETSC_TRUE; 1766 } else if ((indices[0] == 3) && (indices[1] == 0)) { 1767 posOrient = PETSC_TRUE; 1768 } else { 1769 if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) { 1770 posOrient = PETSC_FALSE; 1771 } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge"); 1772 } 1773 } else if (cellDim == 2 && numCorners == 6) { 1774 /* Quadratic triangle (I hate this) */ 1775 /* Edges are determined by the first 2 vertices (corners of edges) */ 1776 const PetscInt faceSizeTri = 3; 1777 PetscInt sortedIndices[3], i, iFace; 1778 PetscBool found = PETSC_FALSE; 1779 PetscInt faceVerticesTriSorted[9] = { 1780 0, 3, 4, /* bottom */ 1781 1, 4, 5, /* right */ 1782 2, 3, 5, /* left */ 1783 }; 1784 PetscInt faceVerticesTri[9] = { 1785 0, 3, 4, /* bottom */ 1786 1, 4, 5, /* right */ 1787 2, 5, 3, /* left */ 1788 }; 1789 1790 faceSize = faceSizeTri; 1791 for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i]; 1792 ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr); 1793 for (iFace = 0; iFace < 3; ++iFace) { 1794 const PetscInt ii = iFace*faceSizeTri; 1795 PetscInt fVertex, cVertex; 1796 1797 if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) && 1798 (sortedIndices[1] == faceVerticesTriSorted[ii+1])) { 1799 for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) { 1800 for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) { 1801 if (indices[cVertex] == faceVerticesTri[ii+fVertex]) { 1802 faceVertices[fVertex] = origVertices[cVertex]; 1803 break; 1804 } 1805 } 1806 } 1807 found = PETSC_TRUE; 1808 break; 1809 } 1810 } 1811 if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface"); 1812 if (posOriented) *posOriented = PETSC_TRUE; 1813 PetscFunctionReturn(0); 1814 } else if (cellDim == 2 && numCorners == 9) { 1815 /* Quadratic quad (I hate this) */ 1816 /* Edges are determined by the first 2 vertices (corners of edges) */ 1817 const PetscInt faceSizeQuad = 3; 1818 PetscInt sortedIndices[3], i, iFace; 1819 PetscBool found = PETSC_FALSE; 1820 PetscInt faceVerticesQuadSorted[12] = { 1821 0, 1, 4, /* bottom */ 1822 1, 2, 5, /* right */ 1823 2, 3, 6, /* top */ 1824 0, 3, 7, /* left */ 1825 }; 1826 PetscInt faceVerticesQuad[12] = { 1827 0, 1, 4, /* bottom */ 1828 1, 2, 5, /* right */ 1829 2, 3, 6, /* top */ 1830 3, 0, 7, /* left */ 1831 }; 1832 1833 faceSize = faceSizeQuad; 1834 for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i]; 1835 ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr); 1836 for (iFace = 0; iFace < 4; ++iFace) { 1837 const PetscInt ii = iFace*faceSizeQuad; 1838 PetscInt fVertex, cVertex; 1839 1840 if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) && 1841 (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) { 1842 for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) { 1843 for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) { 1844 if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) { 1845 faceVertices[fVertex] = origVertices[cVertex]; 1846 break; 1847 } 1848 } 1849 } 1850 found = PETSC_TRUE; 1851 break; 1852 } 1853 } 1854 if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface"); 1855 if (posOriented) *posOriented = PETSC_TRUE; 1856 PetscFunctionReturn(0); 1857 } else if (cellDim == 3 && numCorners == 8) { 1858 /* Hexes 1859 A hex is two oriented quads with the normal of the first 1860 pointing up at the second. 1861 1862 7---6 1863 /| /| 1864 4---5 | 1865 | 1-|-2 1866 |/ |/ 1867 0---3 1868 1869 Faces are determined by the first 4 vertices (corners of faces) */ 1870 const PetscInt faceSizeHex = 4; 1871 PetscInt sortedIndices[4], i, iFace; 1872 PetscBool found = PETSC_FALSE; 1873 PetscInt faceVerticesHexSorted[24] = { 1874 0, 1, 2, 3, /* bottom */ 1875 4, 5, 6, 7, /* top */ 1876 0, 3, 4, 5, /* front */ 1877 2, 3, 5, 6, /* right */ 1878 1, 2, 6, 7, /* back */ 1879 0, 1, 4, 7, /* left */ 1880 }; 1881 PetscInt faceVerticesHex[24] = { 1882 1, 2, 3, 0, /* bottom */ 1883 4, 5, 6, 7, /* top */ 1884 0, 3, 5, 4, /* front */ 1885 3, 2, 6, 5, /* right */ 1886 2, 1, 7, 6, /* back */ 1887 1, 0, 4, 7, /* left */ 1888 }; 1889 1890 faceSize = faceSizeHex; 1891 for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i]; 1892 ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr); 1893 for (iFace = 0; iFace < 6; ++iFace) { 1894 const PetscInt ii = iFace*faceSizeHex; 1895 PetscInt fVertex, cVertex; 1896 1897 if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) && 1898 (sortedIndices[1] == faceVerticesHexSorted[ii+1]) && 1899 (sortedIndices[2] == faceVerticesHexSorted[ii+2]) && 1900 (sortedIndices[3] == faceVerticesHexSorted[ii+3])) { 1901 for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) { 1902 for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) { 1903 if (indices[cVertex] == faceVerticesHex[ii+fVertex]) { 1904 faceVertices[fVertex] = origVertices[cVertex]; 1905 break; 1906 } 1907 } 1908 } 1909 found = PETSC_TRUE; 1910 break; 1911 } 1912 } 1913 if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface"); 1914 if (posOriented) *posOriented = PETSC_TRUE; 1915 PetscFunctionReturn(0); 1916 } else if (cellDim == 3 && numCorners == 10) { 1917 /* Quadratic tet */ 1918 /* Faces are determined by the first 3 vertices (corners of faces) */ 1919 const PetscInt faceSizeTet = 6; 1920 PetscInt sortedIndices[6], i, iFace; 1921 PetscBool found = PETSC_FALSE; 1922 PetscInt faceVerticesTetSorted[24] = { 1923 0, 1, 2, 6, 7, 8, /* bottom */ 1924 0, 3, 4, 6, 7, 9, /* front */ 1925 1, 4, 5, 7, 8, 9, /* right */ 1926 2, 3, 5, 6, 8, 9, /* left */ 1927 }; 1928 PetscInt faceVerticesTet[24] = { 1929 0, 1, 2, 6, 7, 8, /* bottom */ 1930 0, 4, 3, 6, 7, 9, /* front */ 1931 1, 5, 4, 7, 8, 9, /* right */ 1932 2, 3, 5, 8, 6, 9, /* left */ 1933 }; 1934 1935 faceSize = faceSizeTet; 1936 for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i]; 1937 ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr); 1938 for (iFace=0; iFace < 4; ++iFace) { 1939 const PetscInt ii = iFace*faceSizeTet; 1940 PetscInt fVertex, cVertex; 1941 1942 if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) && 1943 (sortedIndices[1] == faceVerticesTetSorted[ii+1]) && 1944 (sortedIndices[2] == faceVerticesTetSorted[ii+2]) && 1945 (sortedIndices[3] == faceVerticesTetSorted[ii+3])) { 1946 for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) { 1947 for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) { 1948 if (indices[cVertex] == faceVerticesTet[ii+fVertex]) { 1949 faceVertices[fVertex] = origVertices[cVertex]; 1950 break; 1951 } 1952 } 1953 } 1954 found = PETSC_TRUE; 1955 break; 1956 } 1957 } 1958 if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface"); 1959 if (posOriented) *posOriented = PETSC_TRUE; 1960 PetscFunctionReturn(0); 1961 } else if (cellDim == 3 && numCorners == 27) { 1962 /* Quadratic hexes (I hate this) 1963 A hex is two oriented quads with the normal of the first 1964 pointing up at the second. 1965 1966 7---6 1967 /| /| 1968 4---5 | 1969 | 3-|-2 1970 |/ |/ 1971 0---1 1972 1973 Faces are determined by the first 4 vertices (corners of faces) */ 1974 const PetscInt faceSizeQuadHex = 9; 1975 PetscInt sortedIndices[9], i, iFace; 1976 PetscBool found = PETSC_FALSE; 1977 PetscInt faceVerticesQuadHexSorted[54] = { 1978 0, 1, 2, 3, 8, 9, 10, 11, 24, /* bottom */ 1979 4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */ 1980 0, 1, 4, 5, 8, 12, 16, 17, 22, /* front */ 1981 1, 2, 5, 6, 9, 13, 17, 18, 21, /* right */ 1982 2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */ 1983 0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */ 1984 }; 1985 PetscInt faceVerticesQuadHex[54] = { 1986 3, 2, 1, 0, 10, 9, 8, 11, 24, /* bottom */ 1987 4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */ 1988 0, 1, 5, 4, 8, 17, 12, 16, 22, /* front */ 1989 1, 2, 6, 5, 9, 18, 13, 17, 21, /* right */ 1990 2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */ 1991 3, 0, 4, 7, 11, 16, 15, 19, 20 /* left */ 1992 }; 1993 1994 faceSize = faceSizeQuadHex; 1995 for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i]; 1996 ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr); 1997 for (iFace = 0; iFace < 6; ++iFace) { 1998 const PetscInt ii = iFace*faceSizeQuadHex; 1999 PetscInt fVertex, cVertex; 2000 2001 if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) && 2002 (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) && 2003 (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) && 2004 (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) { 2005 for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) { 2006 for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) { 2007 if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) { 2008 faceVertices[fVertex] = origVertices[cVertex]; 2009 break; 2010 } 2011 } 2012 } 2013 found = PETSC_TRUE; 2014 break; 2015 } 2016 } 2017 if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface"); 2018 if (posOriented) *posOriented = PETSC_TRUE; 2019 PetscFunctionReturn(0); 2020 } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation()."); 2021 if (!posOrient) { 2022 if (debug) {ierr = PetscPrintf(comm, " Reversing initial face orientation\n");CHKERRQ(ierr);} 2023 for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f]; 2024 } else { 2025 if (debug) {ierr = PetscPrintf(comm, " Keeping initial face orientation\n");CHKERRQ(ierr);} 2026 for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f]; 2027 } 2028 if (posOriented) *posOriented = posOrient; 2029 PetscFunctionReturn(0); 2030 } 2031 2032 #undef __FUNCT__ 2033 #define __FUNCT__ "DMPlexGetOrientedFace" 2034 /* 2035 Given a cell and a face, as a set of vertices, 2036 return the oriented face, as a set of vertices, in faceVertices 2037 The orientation is such that the face normal points out of the cell 2038 */ 2039 PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) 2040 { 2041 const PetscInt *cone = NULL; 2042 PetscInt coneSize, v, f, v2; 2043 PetscInt oppositeVertex = -1; 2044 PetscErrorCode ierr; 2045 2046 PetscFunctionBegin; 2047 ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr); 2048 ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr); 2049 for (v = 0, v2 = 0; v < coneSize; ++v) { 2050 PetscBool found = PETSC_FALSE; 2051 2052 for (f = 0; f < faceSize; ++f) { 2053 if (face[f] == cone[v]) { 2054 found = PETSC_TRUE; break; 2055 } 2056 } 2057 if (found) { 2058 indices[v2] = v; 2059 origVertices[v2] = cone[v]; 2060 ++v2; 2061 } else { 2062 oppositeVertex = v; 2063 } 2064 } 2065 ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr); 2066 PetscFunctionReturn(0); 2067 } 2068 2069 #undef __FUNCT__ 2070 #define __FUNCT__ "DMPlexInsertFace_Internal" 2071 /* 2072 DMPlexInsertFace_Internal - Puts a face into the mesh 2073 2074 Not collective 2075 2076 Input Parameters: 2077 + dm - The DMPlex 2078 . numFaceVertex - The number of vertices in the face 2079 . faceVertices - The vertices in the face for dm 2080 . subfaceVertices - The vertices in the face for subdm 2081 . numCorners - The number of vertices in the cell 2082 . cell - A cell in dm containing the face 2083 . subcell - A cell in subdm containing the face 2084 . firstFace - First face in the mesh 2085 - newFacePoint - Next face in the mesh 2086 2087 Output Parameters: 2088 . newFacePoint - Contains next face point number on input, updated on output 2089 2090 Level: developer 2091 */ 2092 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) 2093 { 2094 MPI_Comm comm; 2095 DM_Plex *submesh = (DM_Plex*) subdm->data; 2096 const PetscInt *faces; 2097 PetscInt numFaces, coneSize; 2098 PetscErrorCode ierr; 2099 2100 PetscFunctionBegin; 2101 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 2102 ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr); 2103 if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize); 2104 #if 0 2105 /* Cannot use this because support() has not been constructed yet */ 2106 ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr); 2107 #else 2108 { 2109 PetscInt f; 2110 2111 numFaces = 0; 2112 ierr = DMGetWorkArray(subdm, 1, PETSC_INT, (void **) &faces);CHKERRQ(ierr); 2113 for (f = firstFace; f < *newFacePoint; ++f) { 2114 PetscInt dof, off, d; 2115 2116 ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr); 2117 ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr); 2118 /* Yes, I know this is quadratic, but I expect the sizes to be <5 */ 2119 for (d = 0; d < dof; ++d) { 2120 const PetscInt p = submesh->cones[off+d]; 2121 PetscInt v; 2122 2123 for (v = 0; v < numFaceVertices; ++v) { 2124 if (subfaceVertices[v] == p) break; 2125 } 2126 if (v == numFaceVertices) break; 2127 } 2128 if (d == dof) { 2129 numFaces = 1; 2130 ((PetscInt*) faces)[0] = f; 2131 } 2132 } 2133 } 2134 #endif 2135 if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces); 2136 else if (numFaces == 1) { 2137 /* Add the other cell neighbor for this face */ 2138 ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr); 2139 } else { 2140 PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov; 2141 PetscBool posOriented; 2142 2143 ierr = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);CHKERRQ(ierr); 2144 origVertices = &orientedVertices[numFaceVertices]; 2145 indices = &orientedVertices[numFaceVertices*2]; 2146 orientedSubVertices = &orientedVertices[numFaceVertices*3]; 2147 ierr = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr); 2148 /* TODO: I know that routine should return a permutation, not the indices */ 2149 for (v = 0; v < numFaceVertices; ++v) { 2150 const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v]; 2151 for (ov = 0; ov < numFaceVertices; ++ov) { 2152 if (orientedVertices[ov] == vertex) { 2153 orientedSubVertices[ov] = subvertex; 2154 break; 2155 } 2156 } 2157 if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex); 2158 } 2159 ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr); 2160 ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr); 2161 ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);CHKERRQ(ierr); 2162 ++(*newFacePoint); 2163 } 2164 #if 0 2165 ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr); 2166 #else 2167 ierr = DMRestoreWorkArray(subdm, 1, PETSC_INT, (void **) &faces);CHKERRQ(ierr); 2168 #endif 2169 PetscFunctionReturn(0); 2170 } 2171 2172 #undef __FUNCT__ 2173 #define __FUNCT__ "DMPlexCreateSubmesh_Uninterpolated" 2174 static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm) 2175 { 2176 MPI_Comm comm; 2177 DMLabel subpointMap; 2178 IS subvertexIS, subcellIS; 2179 const PetscInt *subVertices, *subCells; 2180 PetscInt numSubVertices, firstSubVertex, numSubCells; 2181 PetscInt *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0; 2182 PetscInt vStart, vEnd, c, f; 2183 PetscErrorCode ierr; 2184 2185 PetscFunctionBegin; 2186 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 2187 /* Create subpointMap which marks the submesh */ 2188 ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr); 2189 ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr); 2190 ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr); 2191 if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);} 2192 /* Setup chart */ 2193 ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr); 2194 ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr); 2195 ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr); 2196 ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr); 2197 /* Set cone sizes */ 2198 firstSubVertex = numSubCells; 2199 firstSubFace = numSubCells+numSubVertices; 2200 newFacePoint = firstSubFace; 2201 ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr); 2202 if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);} 2203 ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr); 2204 if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);} 2205 for (c = 0; c < numSubCells; ++c) { 2206 ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr); 2207 } 2208 for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) { 2209 ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr); 2210 } 2211 ierr = DMSetUp(subdm);CHKERRQ(ierr); 2212 /* Create face cones */ 2213 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 2214 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr); 2215 ierr = DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr); 2216 for (c = 0; c < numSubCells; ++c) { 2217 const PetscInt cell = subCells[c]; 2218 const PetscInt subcell = c; 2219 PetscInt *closure = NULL; 2220 PetscInt closureSize, cl, numCorners = 0, faceSize = 0; 2221 2222 ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 2223 for (cl = 0; cl < closureSize*2; cl += 2) { 2224 const PetscInt point = closure[cl]; 2225 PetscInt subVertex; 2226 2227 if ((point >= vStart) && (point < vEnd)) { 2228 ++numCorners; 2229 ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr); 2230 if (subVertex >= 0) { 2231 closure[faceSize] = point; 2232 subface[faceSize] = firstSubVertex+subVertex; 2233 ++faceSize; 2234 } 2235 } 2236 } 2237 if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize); 2238 if (faceSize == nFV) { 2239 ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr); 2240 } 2241 ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr); 2242 } 2243 ierr = DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr); 2244 ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr); 2245 ierr = DMPlexStratify(subdm);CHKERRQ(ierr); 2246 /* Build coordinates */ 2247 { 2248 PetscSection coordSection, subCoordSection; 2249 Vec coordinates, subCoordinates; 2250 PetscScalar *coords, *subCoords; 2251 PetscInt numComp, coordSize, v; 2252 2253 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 2254 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 2255 ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr); 2256 ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr); 2257 ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr); 2258 ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr); 2259 ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr); 2260 for (v = 0; v < numSubVertices; ++v) { 2261 const PetscInt vertex = subVertices[v]; 2262 const PetscInt subvertex = firstSubVertex+v; 2263 PetscInt dof; 2264 2265 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2266 ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr); 2267 ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr); 2268 } 2269 ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr); 2270 ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr); 2271 ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr); 2272 ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 2273 ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr); 2274 if (coordSize) { 2275 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 2276 ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2277 for (v = 0; v < numSubVertices; ++v) { 2278 const PetscInt vertex = subVertices[v]; 2279 const PetscInt subvertex = firstSubVertex+v; 2280 PetscInt dof, off, sdof, soff, d; 2281 2282 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2283 ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr); 2284 ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr); 2285 ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr); 2286 if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof); 2287 for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d]; 2288 } 2289 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 2290 ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2291 } 2292 ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr); 2293 ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr); 2294 } 2295 /* Cleanup */ 2296 if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);} 2297 ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr); 2298 if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);} 2299 ierr = ISDestroy(&subcellIS);CHKERRQ(ierr); 2300 PetscFunctionReturn(0); 2301 } 2302 2303 #undef __FUNCT__ 2304 #define __FUNCT__ "DMPlexCreateSubmesh_Interpolated" 2305 static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm) 2306 { 2307 MPI_Comm comm; 2308 DMLabel subpointMap; 2309 IS *subpointIS; 2310 const PetscInt **subpoints; 2311 PetscInt *numSubPoints, *firstSubPoint, *coneNew, *coneONew; 2312 PetscInt totSubPoints = 0, maxConeSize, dim, p, d, v; 2313 PetscErrorCode ierr; 2314 2315 PetscFunctionBegin; 2316 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 2317 /* Create subpointMap which marks the submesh */ 2318 ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr); 2319 ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr); 2320 ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr); 2321 if (vertexLabel) {ierr = DMPlexMarkSubmesh_Interpolated(dm, vertexLabel, value, subpointMap, subdm);CHKERRQ(ierr);} 2322 /* Setup chart */ 2323 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 2324 ierr = PetscMalloc4(dim+1,PetscInt,&numSubPoints,dim+1,PetscInt,&firstSubPoint,dim+1,IS,&subpointIS,dim+1,const PetscInt *,&subpoints);CHKERRQ(ierr); 2325 for (d = 0; d <= dim; ++d) { 2326 ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr); 2327 totSubPoints += numSubPoints[d]; 2328 } 2329 ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr); 2330 ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr); 2331 /* Set cone sizes */ 2332 firstSubPoint[dim] = 0; 2333 firstSubPoint[0] = firstSubPoint[dim] + numSubPoints[dim]; 2334 if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0] + numSubPoints[0];} 2335 if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];} 2336 for (d = 0; d <= dim; ++d) { 2337 ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr); 2338 ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr); 2339 } 2340 for (d = 0; d <= dim; ++d) { 2341 for (p = 0; p < numSubPoints[d]; ++p) { 2342 const PetscInt point = subpoints[d][p]; 2343 const PetscInt subpoint = firstSubPoint[d] + p; 2344 const PetscInt *cone; 2345 PetscInt coneSize, coneSizeNew, c, val; 2346 2347 ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 2348 ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr); 2349 if (d == dim) { 2350 ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 2351 for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 2352 ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr); 2353 if (val >= 0) coneSizeNew++; 2354 } 2355 ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr); 2356 } 2357 } 2358 } 2359 ierr = DMSetUp(subdm);CHKERRQ(ierr); 2360 /* Set cones */ 2361 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr); 2362 ierr = PetscMalloc2(maxConeSize,PetscInt,&coneNew,maxConeSize,PetscInt,&coneONew);CHKERRQ(ierr); 2363 for (d = 0; d <= dim; ++d) { 2364 for (p = 0; p < numSubPoints[d]; ++p) { 2365 const PetscInt point = subpoints[d][p]; 2366 const PetscInt subpoint = firstSubPoint[d] + p; 2367 const PetscInt *cone, *ornt; 2368 PetscInt coneSize, subconeSize, coneSizeNew, c, subc; 2369 2370 ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 2371 ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr); 2372 ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 2373 ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr); 2374 for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 2375 ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr); 2376 if (subc >= 0) { 2377 coneNew[coneSizeNew] = firstSubPoint[d-1] + subc; 2378 coneONew[coneSizeNew] = ornt[c]; 2379 ++coneSizeNew; 2380 } 2381 } 2382 if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize); 2383 ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr); 2384 ierr = DMPlexSetConeOrientation(subdm, subpoint, coneONew);CHKERRQ(ierr); 2385 } 2386 } 2387 ierr = PetscFree2(coneNew,coneONew);CHKERRQ(ierr); 2388 ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr); 2389 ierr = DMPlexStratify(subdm);CHKERRQ(ierr); 2390 /* Build coordinates */ 2391 { 2392 PetscSection coordSection, subCoordSection; 2393 Vec coordinates, subCoordinates; 2394 PetscScalar *coords, *subCoords; 2395 PetscInt numComp, coordSize; 2396 2397 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 2398 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 2399 ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr); 2400 ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr); 2401 ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr); 2402 ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr); 2403 ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr); 2404 for (v = 0; v < numSubPoints[0]; ++v) { 2405 const PetscInt vertex = subpoints[0][v]; 2406 const PetscInt subvertex = firstSubPoint[0]+v; 2407 PetscInt dof; 2408 2409 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2410 ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr); 2411 ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr); 2412 } 2413 ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr); 2414 ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr); 2415 ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr); 2416 ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 2417 ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr); 2418 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 2419 ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2420 for (v = 0; v < numSubPoints[0]; ++v) { 2421 const PetscInt vertex = subpoints[0][v]; 2422 const PetscInt subvertex = firstSubPoint[0]+v; 2423 PetscInt dof, off, sdof, soff, d; 2424 2425 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2426 ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr); 2427 ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr); 2428 ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr); 2429 if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof); 2430 for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d]; 2431 } 2432 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 2433 ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2434 ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr); 2435 ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr); 2436 } 2437 /* Cleanup */ 2438 for (d = 0; d <= dim; ++d) { 2439 ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr); 2440 ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr); 2441 } 2442 ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr); 2443 PetscFunctionReturn(0); 2444 } 2445 2446 #undef __FUNCT__ 2447 #define __FUNCT__ "DMPlexCreateSubmesh" 2448 /*@ 2449 DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label 2450 2451 Input Parameters: 2452 + dm - The original mesh 2453 . vertexLabel - The DMLabel marking vertices contained in the surface 2454 - value - The label value to use 2455 2456 Output Parameter: 2457 . subdm - The surface mesh 2458 2459 Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap(). 2460 2461 Level: developer 2462 2463 .seealso: DMPlexGetSubpointMap(), DMPlexGetLabel(), DMLabelSetValue() 2464 @*/ 2465 PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm) 2466 { 2467 PetscInt dim, depth; 2468 PetscErrorCode ierr; 2469 2470 PetscFunctionBegin; 2471 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2472 PetscValidPointer(subdm, 3); 2473 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 2474 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 2475 ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr); 2476 ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr); 2477 ierr = DMPlexSetDimension(*subdm, dim-1);CHKERRQ(ierr); 2478 if (depth == dim) { 2479 ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr); 2480 } else { 2481 ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr); 2482 } 2483 PetscFunctionReturn(0); 2484 } 2485 2486 #undef __FUNCT__ 2487 #define __FUNCT__ "DMPlexFilterPoint_Internal" 2488 PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[]) 2489 { 2490 PetscInt subPoint; 2491 PetscErrorCode ierr; 2492 2493 ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr; 2494 return subPoint < 0 ? subPoint : firstSubPoint+subPoint; 2495 } 2496 2497 #undef __FUNCT__ 2498 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh_Uninterpolated" 2499 static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm) 2500 { 2501 MPI_Comm comm; 2502 DMLabel subpointMap; 2503 IS subvertexIS; 2504 const PetscInt *subVertices; 2505 PetscInt numSubVertices, firstSubVertex, numSubCells, *subCells = NULL; 2506 PetscInt *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV; 2507 PetscInt cMax, c, f; 2508 PetscErrorCode ierr; 2509 2510 PetscFunctionBegin; 2511 ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr); 2512 /* Create subpointMap which marks the submesh */ 2513 ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr); 2514 ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr); 2515 ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr); 2516 ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr); 2517 /* Setup chart */ 2518 ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr); 2519 ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr); 2520 ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr); 2521 ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr); 2522 /* Set cone sizes */ 2523 firstSubVertex = numSubCells; 2524 firstSubFace = numSubCells+numSubVertices; 2525 newFacePoint = firstSubFace; 2526 ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr); 2527 if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);} 2528 for (c = 0; c < numSubCells; ++c) { 2529 ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr); 2530 } 2531 for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) { 2532 ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr); 2533 } 2534 ierr = DMSetUp(subdm);CHKERRQ(ierr); 2535 /* Create face cones */ 2536 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr); 2537 ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr); 2538 ierr = DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr); 2539 for (c = 0; c < numSubCells; ++c) { 2540 const PetscInt cell = subCells[c]; 2541 const PetscInt subcell = c; 2542 const PetscInt *cone, *cells; 2543 PetscInt numCells, subVertex, p, v; 2544 2545 if (cell < cMax) continue; 2546 ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr); 2547 for (v = 0; v < nFV; ++v) { 2548 ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr); 2549 subface[v] = firstSubVertex+subVertex; 2550 } 2551 ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr); 2552 ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr); 2553 ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr); 2554 /* Not true in parallel 2555 if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */ 2556 for (p = 0; p < numCells; ++p) { 2557 PetscInt negsubcell; 2558 2559 if (cells[p] >= cMax) continue; 2560 /* I know this is a crap search */ 2561 for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) { 2562 if (subCells[negsubcell] == cells[p]) break; 2563 } 2564 if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell); 2565 ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr); 2566 } 2567 ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr); 2568 ++newFacePoint; 2569 } 2570 ierr = DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr); 2571 ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr); 2572 ierr = DMPlexStratify(subdm);CHKERRQ(ierr); 2573 /* Build coordinates */ 2574 { 2575 PetscSection coordSection, subCoordSection; 2576 Vec coordinates, subCoordinates; 2577 PetscScalar *coords, *subCoords; 2578 PetscInt numComp, coordSize, v; 2579 2580 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 2581 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 2582 ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr); 2583 ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr); 2584 ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr); 2585 ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr); 2586 ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr); 2587 for (v = 0; v < numSubVertices; ++v) { 2588 const PetscInt vertex = subVertices[v]; 2589 const PetscInt subvertex = firstSubVertex+v; 2590 PetscInt dof; 2591 2592 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2593 ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr); 2594 ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr); 2595 } 2596 ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr); 2597 ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr); 2598 ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr); 2599 ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 2600 ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr); 2601 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 2602 ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2603 for (v = 0; v < numSubVertices; ++v) { 2604 const PetscInt vertex = subVertices[v]; 2605 const PetscInt subvertex = firstSubVertex+v; 2606 PetscInt dof, off, sdof, soff, d; 2607 2608 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2609 ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr); 2610 ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr); 2611 ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr); 2612 if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof); 2613 for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d]; 2614 } 2615 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 2616 ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2617 ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr); 2618 ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr); 2619 } 2620 /* Build SF */ 2621 CHKMEMQ; 2622 { 2623 PetscSF sfPoint, sfPointSub; 2624 const PetscSFNode *remotePoints; 2625 PetscSFNode *sremotePoints, *newLocalPoints, *newOwners; 2626 const PetscInt *localPoints; 2627 PetscInt *slocalPoints; 2628 PetscInt numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd; 2629 PetscMPIInt rank; 2630 2631 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 2632 ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr); 2633 ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr); 2634 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2635 ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); 2636 ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr); 2637 if (numRoots >= 0) { 2638 /* Only vertices should be shared */ 2639 ierr = PetscMalloc2(pEnd-pStart,PetscSFNode,&newLocalPoints,numRoots,PetscSFNode,&newOwners);CHKERRQ(ierr); 2640 for (p = 0; p < pEnd-pStart; ++p) { 2641 newLocalPoints[p].rank = -2; 2642 newLocalPoints[p].index = -2; 2643 } 2644 /* Set subleaves */ 2645 for (l = 0; l < numLeaves; ++l) { 2646 const PetscInt point = localPoints[l]; 2647 const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices); 2648 2649 if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point); 2650 if (subPoint < 0) continue; 2651 newLocalPoints[point-pStart].rank = rank; 2652 newLocalPoints[point-pStart].index = subPoint; 2653 ++numSubLeaves; 2654 } 2655 /* Must put in owned subpoints */ 2656 for (p = pStart; p < pEnd; ++p) { 2657 const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices); 2658 2659 if (subPoint < 0) { 2660 newOwners[p-pStart].rank = -3; 2661 newOwners[p-pStart].index = -3; 2662 } else { 2663 newOwners[p-pStart].rank = rank; 2664 newOwners[p-pStart].index = subPoint; 2665 } 2666 } 2667 ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr); 2668 ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr); 2669 ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr); 2670 ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr); 2671 ierr = PetscMalloc(numSubLeaves * sizeof(PetscInt), &slocalPoints);CHKERRQ(ierr); 2672 ierr = PetscMalloc(numSubLeaves * sizeof(PetscSFNode), &sremotePoints);CHKERRQ(ierr); 2673 for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) { 2674 const PetscInt point = localPoints[l]; 2675 const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices); 2676 2677 if (subPoint < 0) continue; 2678 if (newLocalPoints[point].rank == rank) {++ll; continue;} 2679 slocalPoints[sl] = subPoint; 2680 sremotePoints[sl].rank = newLocalPoints[point].rank; 2681 sremotePoints[sl].index = newLocalPoints[point].index; 2682 if (sremotePoints[sl].rank < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point); 2683 if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point); 2684 ++sl; 2685 } 2686 ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr); 2687 if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves); 2688 ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr); 2689 } 2690 } 2691 CHKMEMQ; 2692 /* Cleanup */ 2693 if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);} 2694 ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr); 2695 ierr = PetscFree(subCells);CHKERRQ(ierr); 2696 PetscFunctionReturn(0); 2697 } 2698 2699 #undef __FUNCT__ 2700 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh_Interpolated" 2701 static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char label[], PetscInt value, DM subdm) 2702 { 2703 MPI_Comm comm; 2704 DMLabel subpointMap; 2705 IS *subpointIS; 2706 const PetscInt **subpoints; 2707 PetscInt *numSubPoints, *firstSubPoint, *coneNew, *orntNew; 2708 PetscInt totSubPoints = 0, maxConeSize, dim, p, d, v; 2709 PetscErrorCode ierr; 2710 2711 PetscFunctionBegin; 2712 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 2713 /* Create subpointMap which marks the submesh */ 2714 ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr); 2715 ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr); 2716 ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr); 2717 ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr); 2718 /* Setup chart */ 2719 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 2720 ierr = PetscMalloc4(dim+1,PetscInt,&numSubPoints,dim+1,PetscInt,&firstSubPoint,dim+1,IS,&subpointIS,dim+1,const PetscInt *,&subpoints);CHKERRQ(ierr); 2721 for (d = 0; d <= dim; ++d) { 2722 ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr); 2723 totSubPoints += numSubPoints[d]; 2724 } 2725 ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr); 2726 ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr); 2727 /* Set cone sizes */ 2728 firstSubPoint[dim] = 0; 2729 firstSubPoint[0] = firstSubPoint[dim] + numSubPoints[dim]; 2730 if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0] + numSubPoints[0];} 2731 if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];} 2732 for (d = 0; d <= dim; ++d) { 2733 ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr); 2734 ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr); 2735 } 2736 for (d = 0; d <= dim; ++d) { 2737 for (p = 0; p < numSubPoints[d]; ++p) { 2738 const PetscInt point = subpoints[d][p]; 2739 const PetscInt subpoint = firstSubPoint[d] + p; 2740 const PetscInt *cone; 2741 PetscInt coneSize, coneSizeNew, c, val; 2742 2743 ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 2744 ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr); 2745 if (d == dim) { 2746 ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 2747 for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 2748 ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr); 2749 if (val >= 0) coneSizeNew++; 2750 } 2751 ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr); 2752 } 2753 } 2754 } 2755 ierr = DMSetUp(subdm);CHKERRQ(ierr); 2756 /* Set cones */ 2757 ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr); 2758 ierr = PetscMalloc2(maxConeSize,PetscInt,&coneNew,maxConeSize,PetscInt,&orntNew);CHKERRQ(ierr); 2759 for (d = 0; d <= dim; ++d) { 2760 for (p = 0; p < numSubPoints[d]; ++p) { 2761 const PetscInt point = subpoints[d][p]; 2762 const PetscInt subpoint = firstSubPoint[d] + p; 2763 const PetscInt *cone, *ornt; 2764 PetscInt coneSize, subconeSize, coneSizeNew, c, subc; 2765 2766 ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr); 2767 ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr); 2768 ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr); 2769 ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr); 2770 for (c = 0, coneSizeNew = 0; c < coneSize; ++c) { 2771 ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr); 2772 if (subc >= 0) { 2773 coneNew[coneSizeNew] = firstSubPoint[d-1] + subc; 2774 orntNew[coneSizeNew++] = ornt[c]; 2775 } 2776 } 2777 if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize); 2778 ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr); 2779 ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr); 2780 } 2781 } 2782 ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr); 2783 ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr); 2784 ierr = DMPlexStratify(subdm);CHKERRQ(ierr); 2785 /* Build coordinates */ 2786 { 2787 PetscSection coordSection, subCoordSection; 2788 Vec coordinates, subCoordinates; 2789 PetscScalar *coords, *subCoords; 2790 PetscInt numComp, coordSize; 2791 2792 ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 2793 ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); 2794 ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr); 2795 ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr); 2796 ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr); 2797 ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr); 2798 ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr); 2799 for (v = 0; v < numSubPoints[0]; ++v) { 2800 const PetscInt vertex = subpoints[0][v]; 2801 const PetscInt subvertex = firstSubPoint[0]+v; 2802 PetscInt dof; 2803 2804 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2805 ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr); 2806 ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr); 2807 } 2808 ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr); 2809 ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr); 2810 ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr); 2811 ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 2812 ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr); 2813 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 2814 ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2815 for (v = 0; v < numSubPoints[0]; ++v) { 2816 const PetscInt vertex = subpoints[0][v]; 2817 const PetscInt subvertex = firstSubPoint[0]+v; 2818 PetscInt dof, off, sdof, soff, d; 2819 2820 ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr); 2821 ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr); 2822 ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr); 2823 ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr); 2824 if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof); 2825 for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d]; 2826 } 2827 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 2828 ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr); 2829 ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr); 2830 ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr); 2831 } 2832 /* Cleanup */ 2833 for (d = 0; d <= dim; ++d) { 2834 ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr); 2835 ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr); 2836 } 2837 ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr); 2838 PetscFunctionReturn(0); 2839 } 2840 2841 #undef __FUNCT__ 2842 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh" 2843 /* 2844 DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label an be given to restrict the cells. 2845 2846 Input Parameters: 2847 + dm - The original mesh 2848 . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells 2849 . label - A label name, or PETSC_NULL 2850 - value - A label value 2851 2852 Output Parameter: 2853 . subdm - The surface mesh 2854 2855 Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap(). 2856 2857 Level: developer 2858 2859 .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh() 2860 */ 2861 PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm) 2862 { 2863 PetscInt dim, depth; 2864 PetscErrorCode ierr; 2865 2866 PetscFunctionBegin; 2867 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2868 PetscValidPointer(subdm, 5); 2869 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 2870 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 2871 ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr); 2872 ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr); 2873 ierr = DMPlexSetDimension(*subdm, dim-1);CHKERRQ(ierr); 2874 if (depth == dim) { 2875 ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr); 2876 } else { 2877 ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr); 2878 } 2879 PetscFunctionReturn(0); 2880 } 2881 2882 #undef __FUNCT__ 2883 #define __FUNCT__ "DMPlexGetSubpointMap" 2884 /*@ 2885 DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values 2886 2887 Input Parameter: 2888 . dm - The submesh DM 2889 2890 Output Parameter: 2891 . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh 2892 2893 Level: developer 2894 2895 .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS() 2896 @*/ 2897 PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap) 2898 { 2899 DM_Plex *mesh = (DM_Plex*) dm->data; 2900 2901 PetscFunctionBegin; 2902 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2903 PetscValidPointer(subpointMap, 2); 2904 *subpointMap = mesh->subpointMap; 2905 PetscFunctionReturn(0); 2906 } 2907 2908 #undef __FUNCT__ 2909 #define __FUNCT__ "DMPlexSetSubpointMap" 2910 /* Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh() */ 2911 PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap) 2912 { 2913 DM_Plex *mesh = (DM_Plex *) dm->data; 2914 DMLabel tmp; 2915 PetscErrorCode ierr; 2916 2917 PetscFunctionBegin; 2918 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2919 tmp = mesh->subpointMap; 2920 mesh->subpointMap = subpointMap; 2921 ++mesh->subpointMap->refct; 2922 ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr); 2923 PetscFunctionReturn(0); 2924 } 2925 2926 #undef __FUNCT__ 2927 #define __FUNCT__ "DMPlexCreateSubpointIS" 2928 /*@ 2929 DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data 2930 2931 Input Parameter: 2932 . dm - The submesh DM 2933 2934 Output Parameter: 2935 . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh 2936 2937 Note: This is IS is guaranteed to be sorted by the construction of the submesh 2938 2939 Level: developer 2940 2941 .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap() 2942 @*/ 2943 PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS) 2944 { 2945 MPI_Comm comm; 2946 DMLabel subpointMap; 2947 IS is; 2948 const PetscInt *opoints; 2949 PetscInt *points, *depths; 2950 PetscInt depth, depStart, depEnd, d, pStart, pEnd, p, n, off; 2951 PetscErrorCode ierr; 2952 2953 PetscFunctionBegin; 2954 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2955 PetscValidPointer(subpointIS, 2); 2956 ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 2957 *subpointIS = NULL; 2958 ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr); 2959 ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr); 2960 if (subpointMap && depth >= 0) { 2961 ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr); 2962 if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart); 2963 ierr = DMGetWorkArray(dm, depth+1, PETSC_INT, &depths);CHKERRQ(ierr); 2964 depths[0] = depth; 2965 depths[1] = 0; 2966 for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;} 2967 ierr = PetscMalloc(pEnd * sizeof(PetscInt), &points);CHKERRQ(ierr); 2968 for(d = 0, off = 0; d <= depth; ++d) { 2969 const PetscInt dep = depths[d]; 2970 2971 ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr); 2972 ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr); 2973 if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */ 2974 if (n != depEnd-depStart) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart); 2975 } else { 2976 if (!n) { 2977 if (d == 0) { 2978 /* Missing cells */ 2979 for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1; 2980 } else { 2981 /* Missing faces */ 2982 for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT; 2983 } 2984 } 2985 } 2986 if (n) { 2987 ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr); 2988 ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr); 2989 for(p = 0; p < n; ++p, ++off) points[off] = opoints[p]; 2990 ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr); 2991 ierr = ISDestroy(&is);CHKERRQ(ierr); 2992 } 2993 } 2994 ierr = DMRestoreWorkArray(dm, depth+1, PETSC_INT, &depths);CHKERRQ(ierr); 2995 if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd); 2996 ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr); 2997 } 2998 PetscFunctionReturn(0); 2999 } 3000