1 #define PETSCDM_DLL 2 #include <petsc-private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 3 #include <petscdmda.h> 4 #include <petscsf.h> 5 6 #undef __FUNCT__ 7 #define __FUNCT__ "DMSetFromOptions_Plex" 8 PetscErrorCode DMSetFromOptions_Plex(DM dm) 9 { 10 DM_Plex *mesh = (DM_Plex*) dm->data; 11 PetscErrorCode ierr; 12 13 PetscFunctionBegin; 14 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 15 ierr = PetscOptionsHead("DMPlex Options");CHKERRQ(ierr); 16 /* Handle DMPlex refinement */ 17 /* Handle associated vectors */ 18 /* Handle viewing */ 19 ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMView", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr); 20 ierr = PetscOptionsInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMView", 0, &mesh->printFEM, NULL);CHKERRQ(ierr); 21 ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMView", PETSC_FALSE, &mesh->printTol, NULL);CHKERRQ(ierr); 22 ierr = PetscOptionsTail();CHKERRQ(ierr); 23 PetscFunctionReturn(0); 24 } 25 26 #undef __FUNCT__ 27 #define __FUNCT__ "DMPlexCreateSquareBoundary" 28 /* 29 Simple square boundary: 30 31 18--5-17--4--16 32 | | | 33 6 10 3 34 | | | 35 19-11-20--9--15 36 | | | 37 7 8 2 38 | | | 39 12--0-13--1--14 40 */ 41 PetscErrorCode DMPlexCreateSquareBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[]) 42 { 43 PetscInt numVertices = (edges[0]+1)*(edges[1]+1); 44 PetscInt numEdges = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1]; 45 PetscInt markerTop = 1; 46 PetscInt markerBottom = 1; 47 PetscInt markerRight = 1; 48 PetscInt markerLeft = 1; 49 PetscBool markerSeparate = PETSC_FALSE; 50 Vec coordinates; 51 PetscSection coordSection; 52 PetscScalar *coords; 53 PetscInt coordSize; 54 PetscMPIInt rank; 55 PetscInt v, vx, vy; 56 PetscErrorCode ierr; 57 58 PetscFunctionBegin; 59 ierr = PetscOptionsGetBool(((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr); 60 if (markerSeparate) { 61 markerTop = 1; 62 markerBottom = 0; 63 markerRight = 0; 64 markerLeft = 0; 65 } 66 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 67 if (!rank) { 68 PetscInt e, ex, ey; 69 70 ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr); 71 for (e = 0; e < numEdges; ++e) { 72 ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr); 73 } 74 ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */ 75 for (vx = 0; vx <= edges[0]; vx++) { 76 for (ey = 0; ey < edges[1]; ey++) { 77 PetscInt edge = vx*edges[1] + ey + edges[0]*(edges[1]+1); 78 PetscInt vertex = ey*(edges[0]+1) + vx + numEdges; 79 PetscInt cone[2]; 80 81 cone[0] = vertex; cone[1] = vertex+edges[0]+1; 82 ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr); 83 if (vx == edges[0]) { 84 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr); 85 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr); 86 if (ey == edges[1]-1) { 87 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr); 88 } 89 } else if (vx == 0) { 90 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr); 91 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr); 92 if (ey == edges[1]-1) { 93 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr); 94 } 95 } 96 } 97 } 98 for (vy = 0; vy <= edges[1]; vy++) { 99 for (ex = 0; ex < edges[0]; ex++) { 100 PetscInt edge = vy*edges[0] + ex; 101 PetscInt vertex = vy*(edges[0]+1) + ex + numEdges; 102 PetscInt cone[2]; 103 104 cone[0] = vertex; cone[1] = vertex+1; 105 ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr); 106 if (vy == edges[1]) { 107 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr); 108 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr); 109 if (ex == edges[0]-1) { 110 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr); 111 } 112 } else if (vy == 0) { 113 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr); 114 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr); 115 if (ex == edges[0]-1) { 116 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr); 117 } 118 } 119 } 120 } 121 } 122 ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr); 123 ierr = DMPlexStratify(dm);CHKERRQ(ierr); 124 /* Build coordinates */ 125 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 126 ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr); 127 for (v = numEdges; v < numEdges+numVertices; ++v) { 128 ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr); 129 } 130 ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); 131 ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); 132 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr); 133 ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 134 ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); 135 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 136 for (vy = 0; vy <= edges[1]; ++vy) { 137 for (vx = 0; vx <= edges[0]; ++vx) { 138 coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx; 139 coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy; 140 } 141 } 142 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 143 ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 144 ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 145 PetscFunctionReturn(0); 146 } 147 148 #undef __FUNCT__ 149 #define __FUNCT__ "DMPlexCreateCubeBoundary" 150 /* 151 Simple cubic boundary: 152 153 2-------3 154 /| /| 155 6-------7 | 156 | | | | 157 | 0-----|-1 158 |/ |/ 159 4-------5 160 */ 161 PetscErrorCode DMPlexCreateCubeBoundary(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[]) 162 { 163 PetscInt numVertices = (faces[0]+1)*(faces[1]+1)*(faces[2]+1); 164 PetscInt numFaces = 6; 165 Vec coordinates; 166 PetscSection coordSection; 167 PetscScalar *coords; 168 PetscInt coordSize; 169 PetscMPIInt rank; 170 PetscInt v, vx, vy, vz; 171 PetscErrorCode ierr; 172 173 PetscFunctionBegin; 174 if ((faces[0] < 1) || (faces[1] < 1) || (faces[2] < 1)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side"); 175 if ((faces[0] > 1) || (faces[1] > 1) || (faces[2] > 1)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Currently can't handle more than 1 face per side"); 176 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 177 if (!rank) { 178 PetscInt f; 179 180 ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr); 181 for (f = 0; f < numFaces; ++f) { 182 ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr); 183 } 184 ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */ 185 for (v = 0; v < numFaces+numVertices; ++v) { 186 ierr = DMPlexSetLabelValue(dm, "marker", v, 1);CHKERRQ(ierr); 187 } 188 { /* Side 0 (Front) */ 189 PetscInt cone[4]; 190 cone[0] = numFaces+4; cone[1] = numFaces+5; cone[2] = numFaces+7; cone[3] = numFaces+6; 191 ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr); 192 } 193 { /* Side 1 (Back) */ 194 PetscInt cone[4]; 195 cone[0] = numFaces+1; cone[1] = numFaces+0; cone[2] = numFaces+2; cone[3] = numFaces+3; 196 ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr); 197 } 198 { /* Side 2 (Bottom) */ 199 PetscInt cone[4]; 200 cone[0] = numFaces+0; cone[1] = numFaces+1; cone[2] = numFaces+5; cone[3] = numFaces+4; 201 ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr); 202 } 203 { /* Side 3 (Top) */ 204 PetscInt cone[4]; 205 cone[0] = numFaces+6; cone[1] = numFaces+7; cone[2] = numFaces+3; cone[3] = numFaces+2; 206 ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr); 207 } 208 { /* Side 4 (Left) */ 209 PetscInt cone[4]; 210 cone[0] = numFaces+0; cone[1] = numFaces+4; cone[2] = numFaces+6; cone[3] = numFaces+2; 211 ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr); 212 } 213 { /* Side 5 (Right) */ 214 PetscInt cone[4]; 215 cone[0] = numFaces+5; cone[1] = numFaces+1; cone[2] = numFaces+3; cone[3] = numFaces+7; 216 ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr); 217 } 218 } 219 ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr); 220 ierr = DMPlexStratify(dm);CHKERRQ(ierr); 221 /* Build coordinates */ 222 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 223 ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr); 224 for (v = numFaces; v < numFaces+numVertices; ++v) { 225 ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr); 226 } 227 ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); 228 ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); 229 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr); 230 ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr); 231 ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 232 ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); 233 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 234 for (vz = 0; vz <= faces[2]; ++vz) { 235 for (vy = 0; vy <= faces[1]; ++vy) { 236 for (vx = 0; vx <= faces[0]; ++vx) { 237 coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx; 238 coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy; 239 coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz; 240 } 241 } 242 } 243 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 244 ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 245 ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 246 PetscFunctionReturn(0); 247 } 248 249 #undef __FUNCT__ 250 #define __FUNCT__ "DMPlexCreateSquareMesh" 251 /* 252 Simple square mesh: 253 254 22--8-23--9--24 255 | | | 256 13 2 14 3 15 257 | | | 258 19--6-20--7--21 259 | | | 260 10 0 11 1 12 261 | | | 262 16--4-17--5--18 263 */ 264 PetscErrorCode DMPlexCreateSquareMesh(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[]) 265 { 266 PetscInt markerTop = 1; 267 PetscInt markerBottom = 1; 268 PetscInt markerRight = 1; 269 PetscInt markerLeft = 1; 270 PetscBool markerSeparate = PETSC_FALSE; 271 PetscMPIInt rank; 272 PetscErrorCode ierr; 273 274 PetscFunctionBegin; 275 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr); 276 ierr = PetscOptionsGetBool(((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr); 277 if (markerSeparate) { 278 markerTop = 3; 279 markerBottom = 1; 280 markerRight = 2; 281 markerLeft = 4; 282 } 283 { 284 const PetscInt numXEdges = !rank ? edges[0] : 0; 285 const PetscInt numYEdges = !rank ? edges[1] : 0; 286 const PetscInt numXVertices = !rank ? edges[0]+1 : 0; 287 const PetscInt numYVertices = !rank ? edges[1]+1 : 0; 288 const PetscInt numTotXEdges = numXEdges*numYVertices; 289 const PetscInt numTotYEdges = numYEdges*numXVertices; 290 const PetscInt numVertices = numXVertices*numYVertices; 291 const PetscInt numEdges = numTotXEdges + numTotYEdges; 292 const PetscInt numFaces = numXEdges*numYEdges; 293 const PetscInt firstVertex = numFaces; 294 const PetscInt firstXEdge = numFaces + numVertices; 295 const PetscInt firstYEdge = numFaces + numVertices + numTotXEdges; 296 Vec coordinates; 297 PetscSection coordSection; 298 PetscScalar *coords; 299 PetscInt coordSize; 300 PetscInt v, vx, vy; 301 PetscInt f, fx, fy, e, ex, ey; 302 303 ierr = DMPlexSetChart(dm, 0, numFaces+numEdges+numVertices);CHKERRQ(ierr); 304 for (f = 0; f < numFaces; ++f) { 305 ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr); 306 } 307 for (e = firstXEdge; e < firstXEdge+numEdges; ++e) { 308 ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr); 309 } 310 ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */ 311 /* Build faces */ 312 for (fy = 0; fy < numYEdges; fy++) { 313 for (fx = 0; fx < numXEdges; fx++) { 314 const PetscInt face = fy*numXEdges + fx; 315 const PetscInt edgeL = firstYEdge + fx*numYEdges + fy; 316 const PetscInt edgeB = firstXEdge + fy*numXEdges + fx; 317 const PetscInt ornt[4] = {0, 0, -2, -2}; 318 PetscInt cone[4]; 319 320 cone[0] = edgeB; cone[1] = edgeL+numYEdges; cone[2] = edgeB+numXEdges; cone[3] = edgeL; 321 ierr = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr); 322 ierr = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr); 323 } 324 } 325 /* Build Y edges*/ 326 for (vx = 0; vx < numXVertices; vx++) { 327 for (ey = 0; ey < numYEdges; ey++) { 328 const PetscInt edge = firstYEdge + vx*numYEdges + ey; 329 const PetscInt vertex = firstVertex + ey*numXVertices + vx; 330 PetscInt cone[2]; 331 332 cone[0] = vertex; cone[1] = vertex+numXVertices; 333 ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr); 334 if (vx == numXVertices-1) { 335 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr); 336 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr); 337 if (ey == numYEdges-1) { 338 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr); 339 } 340 } else if (vx == 0) { 341 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr); 342 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr); 343 if (ey == numYEdges-1) { 344 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr); 345 } 346 } 347 } 348 } 349 /* Build X edges*/ 350 for (vy = 0; vy < numYVertices; vy++) { 351 for (ex = 0; ex < numXEdges; ex++) { 352 const PetscInt edge = firstXEdge + vy*numXEdges + ex; 353 const PetscInt vertex = firstVertex + vy*numXVertices + ex; 354 PetscInt cone[2]; 355 356 cone[0] = vertex; cone[1] = vertex+1; 357 ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr); 358 if (vy == numYVertices-1) { 359 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr); 360 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr); 361 if (ex == numXEdges-1) { 362 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr); 363 } 364 } else if (vy == 0) { 365 ierr = DMPlexSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr); 366 ierr = DMPlexSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr); 367 if (ex == numXEdges-1) { 368 ierr = DMPlexSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr); 369 } 370 } 371 } 372 } 373 ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr); 374 ierr = DMPlexStratify(dm);CHKERRQ(ierr); 375 /* Build coordinates */ 376 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 377 ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr); 378 for (v = firstVertex; v < firstVertex+numVertices; ++v) { 379 ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr); 380 } 381 ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); 382 ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); 383 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr); 384 ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 385 ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); 386 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 387 for (vy = 0; vy < numYVertices; ++vy) { 388 for (vx = 0; vx < numXVertices; ++vx) { 389 coords[(vy*numXVertices+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx; 390 coords[(vy*numXVertices+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy; 391 } 392 } 393 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 394 ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 395 ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 396 } 397 PetscFunctionReturn(0); 398 } 399 400 #undef __FUNCT__ 401 #define __FUNCT__ "DMPlexCreateBoxMesh" 402 /*@ 403 DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box). 404 405 Collective on MPI_Comm 406 407 Input Parameters: 408 + comm - The communicator for the DM object 409 . dim - The spatial dimension 410 - interpolate - Flag to create intermediate mesh pieces (edges, faces) 411 412 Output Parameter: 413 . dm - The DM object 414 415 Level: beginner 416 417 .keywords: DM, create 418 .seealso: DMSetType(), DMCreate() 419 @*/ 420 PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool interpolate, DM *dm) 421 { 422 DM boundary; 423 PetscErrorCode ierr; 424 425 PetscFunctionBegin; 426 PetscValidPointer(dm, 4); 427 ierr = DMCreate(comm, &boundary);CHKERRQ(ierr); 428 PetscValidLogicalCollectiveInt(boundary,dim,2); 429 ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr); 430 ierr = DMPlexSetDimension(boundary, dim-1);CHKERRQ(ierr); 431 switch (dim) { 432 case 2: 433 { 434 PetscReal lower[2] = {0.0, 0.0}; 435 PetscReal upper[2] = {1.0, 1.0}; 436 PetscInt edges[2] = {2, 2}; 437 438 ierr = DMPlexCreateSquareBoundary(boundary, lower, upper, edges);CHKERRQ(ierr); 439 break; 440 } 441 case 3: 442 { 443 PetscReal lower[3] = {0.0, 0.0, 0.0}; 444 PetscReal upper[3] = {1.0, 1.0, 1.0}; 445 PetscInt faces[3] = {1, 1, 1}; 446 447 ierr = DMPlexCreateCubeBoundary(boundary, lower, upper, faces);CHKERRQ(ierr); 448 break; 449 } 450 default: 451 SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim); 452 } 453 ierr = DMPlexGenerate(boundary, NULL, interpolate, dm);CHKERRQ(ierr); 454 ierr = DMDestroy(&boundary);CHKERRQ(ierr); 455 PetscFunctionReturn(0); 456 } 457 458 #undef __FUNCT__ 459 #define __FUNCT__ "DMPlexCreateHexBoxMesh" 460 PetscErrorCode DMPlexCreateHexBoxMesh(MPI_Comm comm, PetscInt dim, const PetscInt cells[], DM *dm) 461 { 462 PetscErrorCode ierr; 463 464 PetscFunctionBegin; 465 PetscValidPointer(dm, 4); 466 ierr = DMCreate(comm, dm);CHKERRQ(ierr); 467 PetscValidLogicalCollectiveInt(*dm,dim,2); 468 ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); 469 ierr = DMPlexSetDimension(*dm, dim);CHKERRQ(ierr); 470 switch (dim) { 471 case 2: 472 { 473 PetscReal lower[2] = {0.0, 0.0}; 474 PetscReal upper[2] = {1.0, 1.0}; 475 476 ierr = DMPlexCreateSquareMesh(*dm, lower, upper, cells);CHKERRQ(ierr); 477 break; 478 } 479 #if 0 480 case 3: 481 { 482 PetscReal lower[3] = {0.0, 0.0, 0.0}; 483 PetscReal upper[3] = {1.0, 1.0, 1.0}; 484 485 ierr = DMPlexCreateCubeMesh(boundary, lower, upper, cells);CHKERRQ(ierr); 486 break; 487 } 488 #endif 489 default: 490 SETERRQ1(comm, PETSC_ERR_SUP, "Dimension not supported: %d", dim); 491 } 492 PetscFunctionReturn(0); 493 } 494 495 /* External function declarations here */ 496 extern PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling); 497 extern PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J); 498 extern PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm); 499 extern PetscErrorCode DMRefine_Plex(DM dm, MPI_Comm comm, DM *dmRefined); 500 extern PetscErrorCode DMClone_Plex(DM dm, DM *newdm); 501 extern PetscErrorCode DMSetUp_Plex(DM dm); 502 extern PetscErrorCode DMDestroy_Plex(DM dm); 503 extern PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer); 504 extern PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm); 505 extern PetscErrorCode DMLocatePoints_Plex(DM dm, Vec v, IS *cellIS); 506 507 #undef __FUNCT__ 508 #define __FUNCT__ "DMCreateGlobalVector_Plex" 509 static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec) 510 { 511 PetscErrorCode ierr; 512 513 PetscFunctionBegin; 514 ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr); 515 /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */ 516 ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex);CHKERRQ(ierr); 517 PetscFunctionReturn(0); 518 } 519 520 #undef __FUNCT__ 521 #define __FUNCT__ "DMCreateLocalVector_Plex" 522 static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec) 523 { 524 PetscErrorCode ierr; 525 526 PetscFunctionBegin; 527 ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr); 528 ierr = VecSetOperation(*vec, VECOP_VIEW, (void(*)(void)) VecView_Plex_Local);CHKERRQ(ierr); 529 PetscFunctionReturn(0); 530 } 531 532 #undef __FUNCT__ 533 #define __FUNCT__ "DMInitialize_Plex" 534 PetscErrorCode DMInitialize_Plex(DM dm) 535 { 536 PetscFunctionBegin; 537 dm->ops->view = DMView_Plex; 538 dm->ops->setfromoptions = DMSetFromOptions_Plex; 539 dm->ops->clone = DMClone_Plex; 540 dm->ops->setup = DMSetUp_Plex; 541 dm->ops->createglobalvector = DMCreateGlobalVector_Plex; 542 dm->ops->createlocalvector = DMCreateLocalVector_Plex; 543 dm->ops->getlocaltoglobalmapping = NULL; 544 dm->ops->getlocaltoglobalmappingblock = NULL; 545 dm->ops->createfieldis = NULL; 546 dm->ops->createcoordinatedm = DMCreateCoordinateDM_Plex; 547 dm->ops->getcoloring = 0; 548 dm->ops->creatematrix = DMCreateMatrix_Plex; 549 dm->ops->createinterpolation = 0; 550 dm->ops->getaggregates = 0; 551 dm->ops->getinjection = 0; 552 dm->ops->refine = DMRefine_Plex; 553 dm->ops->coarsen = 0; 554 dm->ops->refinehierarchy = 0; 555 dm->ops->coarsenhierarchy = 0; 556 dm->ops->globaltolocalbegin = NULL; 557 dm->ops->globaltolocalend = NULL; 558 dm->ops->localtoglobalbegin = NULL; 559 dm->ops->localtoglobalend = NULL; 560 dm->ops->destroy = DMDestroy_Plex; 561 dm->ops->createsubdm = DMCreateSubDM_Plex; 562 dm->ops->locatepoints = DMLocatePoints_Plex; 563 PetscFunctionReturn(0); 564 } 565 566 #undef __FUNCT__ 567 #define __FUNCT__ "DMClone_Plex" 568 PetscErrorCode DMClone_Plex(DM dm, DM *newdm) 569 { 570 DM_Plex *mesh = (DM_Plex *) dm->data; 571 PetscErrorCode ierr; 572 573 PetscFunctionBegin; 574 mesh->refct++; 575 (*newdm)->data = mesh; 576 ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr); 577 ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr); 578 PetscFunctionReturn(0); 579 } 580 581 /*MC 582 DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram. 583 In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is 584 specified by a PetscSection object. Ownership in the global representation is determined by 585 ownership of the underlying DMPlex points. This is specified by another PetscSection object. 586 587 Level: intermediate 588 589 .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType() 590 M*/ 591 592 #undef __FUNCT__ 593 #define __FUNCT__ "DMCreate_Plex" 594 PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm) 595 { 596 DM_Plex *mesh; 597 PetscInt unit, d; 598 PetscErrorCode ierr; 599 600 PetscFunctionBegin; 601 PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 602 ierr = PetscNewLog(dm, DM_Plex, &mesh);CHKERRQ(ierr); 603 dm->data = mesh; 604 605 mesh->refct = 1; 606 mesh->dim = 0; 607 ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr); 608 mesh->maxConeSize = 0; 609 mesh->cones = NULL; 610 mesh->coneOrientations = NULL; 611 ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr); 612 mesh->maxSupportSize = 0; 613 mesh->supports = NULL; 614 mesh->refinementUniform = PETSC_TRUE; 615 mesh->refinementLimit = -1.0; 616 617 mesh->facesTmp = NULL; 618 619 mesh->subpointMap = NULL; 620 621 for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0; 622 623 mesh->labels = NULL; 624 mesh->depthLabel = NULL; 625 mesh->globalVertexNumbers = NULL; 626 mesh->globalCellNumbers = NULL; 627 for (d = 0; d < 8; ++d) mesh->hybridPointMax[d] = PETSC_DETERMINE; 628 mesh->vtkCellHeight = 0; 629 mesh->preallocCenterDim = -1; 630 631 mesh->printSetValues = PETSC_FALSE; 632 mesh->printFEM = 0; 633 mesh->printTol = 1.0e-10; 634 635 ierr = DMInitialize_Plex(dm);CHKERRQ(ierr); 636 PetscFunctionReturn(0); 637 } 638 639 #undef __FUNCT__ 640 #define __FUNCT__ "DMPlexCreate" 641 /*@ 642 DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram. 643 644 Collective on MPI_Comm 645 646 Input Parameter: 647 . comm - The communicator for the DMPlex object 648 649 Output Parameter: 650 . mesh - The DMPlex object 651 652 Level: beginner 653 654 .keywords: DMPlex, create 655 @*/ 656 PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh) 657 { 658 PetscErrorCode ierr; 659 660 PetscFunctionBegin; 661 PetscValidPointer(mesh,2); 662 ierr = DMCreate(comm, mesh);CHKERRQ(ierr); 663 ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr); 664 PetscFunctionReturn(0); 665 } 666 667 #undef __FUNCT__ 668 #define __FUNCT__ "DMPlexBuildFromCellList_Private" 669 /* 670 This takes as input the common mesh generator output, a list of the vertices for each cell 671 */ 672 PetscErrorCode DMPlexBuildFromCellList_Private(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const int cells[]) 673 { 674 PetscInt *cone, c, p; 675 PetscErrorCode ierr; 676 677 PetscFunctionBegin; 678 ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr); 679 for (c = 0; c < numCells; ++c) { 680 ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr); 681 } 682 ierr = DMSetUp(dm);CHKERRQ(ierr); 683 ierr = DMGetWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr); 684 for (c = 0; c < numCells; ++c) { 685 for (p = 0; p < numCorners; ++p) { 686 cone[p] = cells[c*numCorners+p]+numCells; 687 } 688 ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr); 689 } 690 ierr = DMRestoreWorkArray(dm, numCorners, PETSC_INT, &cone);CHKERRQ(ierr); 691 ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr); 692 ierr = DMPlexStratify(dm);CHKERRQ(ierr); 693 PetscFunctionReturn(0); 694 } 695 696 #undef __FUNCT__ 697 #define __FUNCT__ "DMPlexBuildCoordinates_Private" 698 /* 699 This takes as input the coordinates for each vertex 700 */ 701 PetscErrorCode DMPlexBuildCoordinates_Private(DM dm, PetscInt spaceDim, PetscInt numCells, PetscInt numVertices, const double vertexCoords[]) 702 { 703 PetscSection coordSection; 704 Vec coordinates; 705 PetscScalar *coords; 706 PetscInt coordSize, v, d; 707 PetscErrorCode ierr; 708 709 PetscFunctionBegin; 710 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 711 ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr); 712 ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr); 713 ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr); 714 for (v = numCells; v < numCells+numVertices; ++v) { 715 ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr); 716 ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr); 717 } 718 ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); 719 ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); 720 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr); 721 ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr); 722 ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 723 ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); 724 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 725 for (v = 0; v < numVertices; ++v) { 726 for (d = 0; d < spaceDim; ++d) { 727 coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d]; 728 } 729 } 730 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 731 ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 732 ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 733 PetscFunctionReturn(0); 734 } 735 736 #undef __FUNCT__ 737 #define __FUNCT__ "DMPlexCreateFromCellList" 738 /*@C 739 DMPlexCreateFromCellList - This takes as input common mesh generator output, a list of the vertices for each cell, and produces a DM 740 741 Input Parameters: 742 + comm - The communicator 743 . dim - The topological dimension of the mesh 744 . numCells - The number of cells 745 . numVertices - The number of vertices 746 . numCorners - The number of vertices for each cell 747 . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically 748 . cells - An array of numCells*numCorners numbers, the vertices for each cell 749 . spaceDim - The spatial dimension used for coordinates 750 - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex 751 752 Output Parameter: 753 . dm - The DM 754 755 Note: Two triangles sharing a face 756 $ 757 $ 2 758 $ / | \ 759 $ / | \ 760 $ / | \ 761 $ 0 0 | 1 3 762 $ \ | / 763 $ \ | / 764 $ \ | / 765 $ 1 766 would have input 767 $ numCells = 2, numVertices = 4 768 $ cells = [0 1 2 1 3 2] 769 $ 770 which would result in the DMPlex 771 $ 772 $ 4 773 $ / | \ 774 $ / | \ 775 $ / | \ 776 $ 2 0 | 1 5 777 $ \ | / 778 $ \ | / 779 $ \ | / 780 $ 3 781 782 Level: beginner 783 784 .seealso: DMPlexCreate() 785 @*/ 786 PetscErrorCode DMPlexCreateFromCellList(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const int cells[], PetscInt spaceDim, const double vertexCoords[], DM *dm) 787 { 788 PetscErrorCode ierr; 789 790 PetscFunctionBegin; 791 ierr = DMCreate(comm, dm);CHKERRQ(ierr); 792 ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); 793 ierr = DMPlexSetDimension(*dm, dim);CHKERRQ(ierr); 794 ierr = DMPlexBuildFromCellList_Private(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr); 795 if (interpolate) { 796 DM idm; 797 798 ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr); 799 ierr = DMDestroy(dm);CHKERRQ(ierr); 800 *dm = idm; 801 } 802 ierr = DMPlexBuildCoordinates_Private(*dm, spaceDim, numCells, numVertices, vertexCoords);CHKERRQ(ierr); 803 PetscFunctionReturn(0); 804 } 805 806 #undef __FUNCT__ 807 #define __FUNCT__ "DMPlexCreateFromDAG" 808 /* 809 This takes as input the raw Hasse Diagram data 810 */ 811 PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[]) 812 { 813 Vec coordinates; 814 PetscSection coordSection; 815 PetscScalar *coords; 816 PetscInt coordSize, firstVertex = numPoints[depth], pStart = 0, pEnd = 0, p, v, dim, d, off; 817 PetscErrorCode ierr; 818 819 PetscFunctionBegin; 820 ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr); 821 for (d = 0; d <= depth; ++d) pEnd += numPoints[d]; 822 ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr); 823 for (p = pStart; p < pEnd; ++p) { 824 ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr); 825 } 826 ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */ 827 for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) { 828 ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr); 829 ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr); 830 } 831 ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr); 832 ierr = DMPlexStratify(dm);CHKERRQ(ierr); 833 /* Build coordinates */ 834 ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); 835 ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr); 836 ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr); 837 ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr); 838 for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) { 839 ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr); 840 ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr); 841 } 842 ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); 843 ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); 844 ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr); 845 ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr); 846 ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); 847 ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); 848 ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); 849 for (v = 0; v < numPoints[0]; ++v) { 850 PetscInt off; 851 852 ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr); 853 for (d = 0; d < dim; ++d) { 854 coords[off+d] = vertexCoords[v*dim+d]; 855 } 856 } 857 ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); 858 ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr); 859 ierr = VecDestroy(&coordinates);CHKERRQ(ierr); 860 PetscFunctionReturn(0); 861 } 862