1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */ 2 #define PETSCDM_DLL 3 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 4 5 /*@ 6 DMPlexCreateFluentFromFile - Create a `DMPLEX` mesh from a Fluent mesh file 7 8 Collective 9 10 Input Parameters: 11 + comm - The MPI communicator 12 . filename - Name of the Fluent mesh file 13 - interpolate - Create faces and edges in the mesh 14 15 Output Parameter: 16 . dm - The `DM` object representing the mesh 17 18 Level: beginner 19 20 .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateFluent()`, `DMPlexCreate()` 21 @*/ 22 PetscErrorCode DMPlexCreateFluentFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm) 23 { 24 PetscViewer viewer; 25 26 PetscFunctionBegin; 27 /* Create file viewer and build plex */ 28 PetscCall(PetscViewerCreate(comm, &viewer)); 29 PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII)); 30 PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ)); 31 PetscCall(PetscViewerFileSetName(viewer, filename)); 32 PetscCall(DMPlexCreateFluent(comm, viewer, interpolate, dm)); 33 PetscCall(PetscViewerDestroy(&viewer)); 34 PetscFunctionReturn(PETSC_SUCCESS); 35 } 36 37 static PetscErrorCode DMPlexCreateFluent_ReadString(PetscViewer viewer, char *buffer, char delim) 38 { 39 PetscInt ret, i = 0; 40 41 PetscFunctionBegin; 42 do PetscCall(PetscViewerRead(viewer, &buffer[i++], 1, &ret, PETSC_CHAR)); 43 while (ret > 0 && buffer[i - 1] != '\0' && buffer[i - 1] != delim && i < PETSC_MAX_PATH_LEN - 1); 44 if (!ret) buffer[i - 1] = '\0'; 45 else buffer[i] = '\0'; 46 PetscCheck(i < PETSC_MAX_PATH_LEN - 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Buffer overflow! This is not a valid Fluent file."); 47 PetscFunctionReturn(PETSC_SUCCESS); 48 } 49 50 static PetscErrorCode DMPlexCreateFluent_ReadValues(PetscViewer viewer, void *data, PetscInt count, PetscDataType dtype, PetscBool binary, PetscInt *numClosingParens) 51 { 52 int fdes = 0; 53 FILE *file; 54 PetscInt i; 55 56 PetscFunctionBegin; 57 *numClosingParens = 0; 58 if (binary) { 59 /* Extract raw file descriptor to read binary block */ 60 PetscCall(PetscViewerASCIIGetPointer(viewer, &file)); 61 PetscCall(PetscFFlush(file)); 62 fdes = fileno(file); 63 } 64 65 if (!binary && dtype == PETSC_INT) { 66 char cbuf[256]; 67 unsigned int ibuf; 68 int snum; 69 /* Parse hexadecimal ascii integers */ 70 for (i = 0; i < count; i++) { 71 size_t len; 72 73 PetscCall(PetscViewerRead(viewer, cbuf, 1, NULL, PETSC_STRING)); 74 snum = sscanf(cbuf, "%x", &ibuf); 75 PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 76 ((PetscInt *)data)[i] = (PetscInt)ibuf; 77 // Check for trailing parentheses 78 PetscCall(PetscStrlen(cbuf, &len)); 79 while (cbuf[len - 1] == ')' && len > 0) { 80 ++(*numClosingParens); 81 --len; 82 } 83 } 84 } else if (binary && dtype == PETSC_INT) { 85 /* Always read 32-bit ints and cast to PetscInt */ 86 int *ibuf; 87 PetscCall(PetscMalloc1(count, &ibuf)); 88 PetscCall(PetscBinaryRead(fdes, ibuf, count, NULL, PETSC_ENUM)); 89 PetscCall(PetscByteSwap(ibuf, PETSC_ENUM, count)); 90 for (i = 0; i < count; i++) ((PetscInt *)data)[i] = ibuf[i]; 91 PetscCall(PetscFree(ibuf)); 92 93 } else if (binary && dtype == PETSC_SCALAR) { 94 float *fbuf; 95 /* Always read 32-bit floats and cast to PetscScalar */ 96 PetscCall(PetscMalloc1(count, &fbuf)); 97 PetscCall(PetscBinaryRead(fdes, fbuf, count, NULL, PETSC_FLOAT)); 98 PetscCall(PetscByteSwap(fbuf, PETSC_FLOAT, count)); 99 for (i = 0; i < count; i++) ((PetscScalar *)data)[i] = fbuf[i]; 100 PetscCall(PetscFree(fbuf)); 101 } else { 102 PetscCall(PetscViewerASCIIRead(viewer, data, count, NULL, dtype)); 103 } 104 PetscFunctionReturn(PETSC_SUCCESS); 105 } 106 107 static PetscErrorCode DMPlexCreateFluent_ReadSection(PetscViewer viewer, FluentSection *s) 108 { 109 char buffer[PETSC_MAX_PATH_LEN]; 110 int snum; 111 112 PetscFunctionBegin; 113 /* Fast-forward to next section and derive its index */ 114 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 115 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ' ')); 116 snum = sscanf(buffer, "%d", &s->index); 117 /* If we can't match an index return -1 to signal end-of-file */ 118 if (snum < 1) { 119 s->index = -1; 120 PetscFunctionReturn(PETSC_SUCCESS); 121 } 122 123 if (s->index == 0) { /* Comment */ 124 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 125 126 } else if (s->index == 2) { /* Dimension */ 127 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 128 snum = sscanf(buffer, "%d", &s->nd); 129 PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 130 131 } else if (s->index == 10 || s->index == 2010) { /* Vertices */ 132 PetscInt numClosingParens = 0; 133 134 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 135 snum = sscanf(buffer, "(%x %x %x %d %d)", &s->zoneID, &s->first, &s->last, &s->type, &s->nd); 136 PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 137 if (s->zoneID > 0) { 138 PetscInt numCoords = s->last - s->first + 1; 139 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 140 PetscCall(PetscMalloc1(s->nd * numCoords, (PetscScalar **)&s->data)); 141 PetscCall(DMPlexCreateFluent_ReadValues(viewer, s->data, s->nd * numCoords, PETSC_SCALAR, s->index == 2010 ? PETSC_TRUE : PETSC_FALSE, &numClosingParens)); 142 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 143 else --numClosingParens; 144 } 145 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 146 else --numClosingParens; 147 PetscCheck(!numClosingParens, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 148 } else if (s->index == 12 || s->index == 2012) { /* Cells */ 149 PetscInt numClosingParens = 0; 150 151 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 152 snum = sscanf(buffer, "(%x", &s->zoneID); 153 PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 154 if (s->zoneID == 0) { /* Header section */ 155 snum = sscanf(buffer, "(%x %x %x %d)", &s->zoneID, &s->first, &s->last, &s->nd); 156 PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 157 } else { /* Data section */ 158 snum = sscanf(buffer, "(%x %x %x %d %d)", &s->zoneID, &s->first, &s->last, &s->type, &s->nd); 159 PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 160 if (s->nd == 0) { 161 /* Read cell type definitions for mixed cells */ 162 PetscInt numCells = s->last - s->first + 1; 163 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 164 PetscCall(PetscMalloc1(numCells, (PetscInt **)&s->data)); 165 PetscCall(DMPlexCreateFluent_ReadValues(viewer, s->data, numCells, PETSC_INT, s->index == 2012 ? PETSC_TRUE : PETSC_FALSE, &numClosingParens)); 166 PetscCall(PetscFree(s->data)); 167 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 168 else --numClosingParens; 169 } 170 } 171 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 172 else --numClosingParens; 173 PetscCheck(!numClosingParens, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 174 } else if (s->index == 13 || s->index == 2013) { /* Faces */ 175 PetscInt numClosingParens = 0; 176 177 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 178 snum = sscanf(buffer, "(%x", &s->zoneID); 179 PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 180 if (s->zoneID == 0) { /* Header section */ 181 snum = sscanf(buffer, "(%x %x %x %d)", &s->zoneID, &s->first, &s->last, &s->nd); 182 PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 183 } else { /* Data section */ 184 PetscInt f, numEntries, numFaces; 185 snum = sscanf(buffer, "(%x %x %x %d %d)", &s->zoneID, &s->first, &s->last, &s->type, &s->nd); 186 PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 187 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 188 switch (s->nd) { 189 case 0: 190 numEntries = PETSC_DETERMINE; 191 break; 192 case 2: 193 numEntries = 2 + 2; 194 break; /* linear */ 195 case 3: 196 numEntries = 2 + 3; 197 break; /* triangular */ 198 case 4: 199 numEntries = 2 + 4; 200 break; /* quadrilateral */ 201 default: 202 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown face type in Fluent file"); 203 } 204 numFaces = s->last - s->first + 1; 205 if (numEntries != PETSC_DETERMINE) { 206 /* Allocate space only if we already know the size of the block */ 207 PetscCall(PetscMalloc1(numEntries * numFaces, (PetscInt **)&s->data)); 208 } 209 for (f = 0; f < numFaces; f++) { 210 if (s->nd == 0) { 211 /* Determine the size of the block for "mixed" facets */ 212 PetscInt numFaceVert = 0; 213 PetscCall(DMPlexCreateFluent_ReadValues(viewer, &numFaceVert, 1, PETSC_INT, s->index == 2013 ? PETSC_TRUE : PETSC_FALSE, &numClosingParens)); 214 if (numEntries == PETSC_DETERMINE) { 215 numEntries = numFaceVert + 2; 216 PetscCall(PetscMalloc1(numEntries * numFaces, (PetscInt **)&s->data)); 217 } else { 218 PetscCheck(numEntries == numFaceVert + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No support for mixed faces in Fluent files"); 219 } 220 } 221 PetscCall(DMPlexCreateFluent_ReadValues(viewer, &(((PetscInt *)s->data)[f * numEntries]), numEntries, PETSC_INT, s->index == 2013 ? PETSC_TRUE : PETSC_FALSE, &numClosingParens)); 222 } 223 PetscCall(PetscMPIIntCast(numEntries - 2, &s->nd)); 224 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 225 else --numClosingParens; 226 } 227 if (!numClosingParens) PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 228 else --numClosingParens; 229 PetscCheck(!numClosingParens, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 230 } else if (s->index == 39) { /* Label information */ 231 char labelName[PETSC_MAX_PATH_LEN]; 232 char caseName[PETSC_MAX_PATH_LEN]; 233 234 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 235 snum = sscanf(buffer, "(%u %s %s %d)", &s->zoneID, caseName, labelName, &s->nd); 236 PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file: %d", snum); 237 PetscInt depth = 1; 238 do { 239 /* Match parentheses when parsing unknown sections */ 240 do PetscCall(PetscViewerRead(viewer, &buffer[0], 1, NULL, PETSC_CHAR)); 241 while (buffer[0] != '(' && buffer[0] != ')'); 242 if (buffer[0] == '(') depth++; 243 if (buffer[0] == ')') depth--; 244 } while (depth > 0); 245 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '\n')); 246 PetscCall(PetscStrallocpy(labelName, (char **)&s->data)); 247 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Zone ID %u is label %s\n", s->zoneID, labelName)); 248 } else { /* Unknown section type */ 249 PetscInt depth = 1; 250 do { 251 /* Match parentheses when parsing unknown sections */ 252 do PetscCall(PetscViewerRead(viewer, &buffer[0], 1, NULL, PETSC_CHAR)); 253 while (buffer[0] != '(' && buffer[0] != ')'); 254 if (buffer[0] == '(') depth++; 255 if (buffer[0] == ')') depth--; 256 } while (depth > 0); 257 PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '\n')); 258 } 259 PetscFunctionReturn(PETSC_SUCCESS); 260 } 261 262 // Inserts point `face` with orientation `ornt` into the cone of point `cell` at position `c`, which is the first empty slot 263 static PetscErrorCode InsertFace(DM dm, PetscInt cell, PetscInt face, PetscInt ornt) 264 { 265 const PetscInt *cone; 266 PetscInt coneSize, c; 267 268 PetscFunctionBegin; 269 PetscCall(DMPlexGetCone(dm, cell, &cone)); 270 PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 271 for (c = 0; c < coneSize; ++c) 272 if (cone[c] < 0) break; 273 PetscCheck(c < coneSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " could not be inserted in cone of cell %" PetscInt_FMT, face, cell); 274 PetscCall(DMPlexInsertCone(dm, cell, c, face)); 275 PetscCall(DMPlexInsertConeOrientation(dm, cell, c, ornt)); 276 PetscFunctionReturn(PETSC_SUCCESS); 277 } 278 279 static PetscErrorCode ReorderPolygon(DM dm, PetscInt cell) 280 { 281 const PetscInt *cone, *ornt; 282 PetscInt coneSize, newCone[16], newOrnt[16]; 283 284 PetscFunctionBegin; 285 PetscCall(DMPlexGetOrientedCone(dm, cell, &cone, &ornt)); 286 PetscCall(DMPlexGetConeSize(dm, cell, &coneSize)); 287 newCone[0] = cone[0]; 288 newOrnt[0] = ornt[0]; 289 for (PetscInt c = 1; c < coneSize; ++c) { 290 const PetscInt *fcone; 291 PetscInt firstVertex, lastVertex, c2; 292 293 PetscCall(DMPlexGetCone(dm, newCone[c - 1], &fcone)); 294 lastVertex = newOrnt[c - 1] ? fcone[0] : fcone[1]; 295 for (c2 = 0; c2 < coneSize; ++c2) { 296 const PetscInt *fcone2; 297 298 PetscCall(DMPlexGetCone(dm, cone[c2], &fcone2)); 299 firstVertex = ornt[c2] ? fcone2[1] : fcone2[0]; 300 if (lastVertex == firstVertex) { 301 // Point `cell` matched point `lastVertex` on face `cone[c2]` with orientation `ornt[c2]` 302 break; 303 } 304 } 305 PetscCheck(c2 < coneSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " could not find a face match as position %" PetscInt_FMT, cell, c); 306 newCone[c] = cone[c2]; 307 newOrnt[c] = ornt[c2]; 308 } 309 { 310 const PetscInt *fcone, *fcone2; 311 PetscInt vertex, vertex2; 312 313 PetscCall(DMPlexGetCone(dm, newCone[coneSize - 1], &fcone)); 314 PetscCall(DMPlexGetCone(dm, newCone[0], &fcone2)); 315 vertex = newOrnt[coneSize - 1] ? fcone[0] : fcone[1]; 316 vertex2 = newOrnt[0] ? fcone2[1] : fcone2[0]; 317 PetscCheck(vertex == vertex2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " did not match at the endpoint", cell); 318 } 319 PetscCall(DMPlexSetCone(dm, cell, newCone)); 320 PetscCall(DMPlexSetConeOrientation(dm, cell, newOrnt)); 321 PetscCall(DMPlexRestoreOrientedCone(dm, cell, &cone, &ornt)); 322 PetscFunctionReturn(PETSC_SUCCESS); 323 } 324 325 static PetscErrorCode ReorderTetrahedron(PetscViewer viewer, DM dm, PetscInt cell) 326 { 327 const PetscInt *cone, *ornt, *fcone, *fornt, *farr, faces[4] = {0, 1, 3, 2}; 328 PetscInt newCone[16], newOrnt[16]; 329 330 PetscFunctionBegin; 331 PetscCall(DMPlexGetOrientedCone(dm, cell, &cone, &ornt)); 332 newCone[0] = cone[0]; 333 newOrnt[0] = ornt[0]; 334 PetscCall(DMPlexGetOrientedCone(dm, newCone[0], &fcone, &fornt)); 335 farr = DMPolytopeTypeGetArrangement(DM_POLYTOPE_TRIANGLE, newOrnt[0]); 336 // Loop over each edge in the initial triangle 337 for (PetscInt e = 0; e < 3; ++e) { 338 const PetscInt edge = fcone[farr[e * 2 + 0]], eornt = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr[e * 2 + 1], fornt[farr[e * 2 + 0]]); 339 PetscInt c; 340 341 // Loop over each remaining face in the tetrahedron 342 // On face `newCone[0]`, trying to match edge `edge` with final orientation `eornt` to an edge on another face 343 for (c = 1; c < 4; ++c) { 344 const PetscInt *fcone2, *fornt2, *farr2; 345 PetscInt c2; 346 PetscBool flip = PETSC_FALSE; 347 348 // Checking face `cone[c]` with orientation `ornt[c]` 349 PetscCall(DMPlexGetOrientedCone(dm, cone[c], &fcone2, &fornt2)); 350 farr2 = DMPolytopeTypeGetArrangement(DM_POLYTOPE_TRIANGLE, ornt[c]); 351 // Check for edge 352 for (c2 = 0; c2 < 3; ++c2) { 353 const PetscInt edge2 = fcone2[farr2[c2 * 2 + 0]], eornt2 = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr2[c2 * 2 + 1], fornt2[farr2[c2 * 2 + 0]]); 354 // Trying to match edge `edge2` with final orientation `eornt2` 355 if (edge == edge2) { 356 //PetscCheck(eornt == -(eornt2 + 1), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell % " PetscInt_FMT " edge %" PetscInt_FMT " (%" PetscInt_FMT ") found twice with the same orientation in face %" PetscInt_FMT " edge %" PetscInt_FMT, cell, edge, e, c, c2); 357 // Matched face `newCone[0]` with orientation `newOrnt[0]` to face `cone[c]` with orientation `ornt[c]` along edge `edge` 358 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Matched cell %" PetscInt_FMT " edge %" PetscInt_FMT "/%" PetscInt_FMT " (%" PetscInt_FMT ") to face %" PetscInt_FMT "/%" PetscInt_FMT " edge %" PetscInt_FMT " (%" PetscInt_FMT ")\n", cell, edge, e, eornt, cone[c], c, c2, eornt2)); 359 flip = eornt != -(eornt2 + 1) ? PETSC_TRUE : PETSC_FALSE; 360 break; 361 } 362 } 363 if (c2 < 3) { 364 newCone[faces[e + 1]] = cone[c]; 365 // Compute new orientation of face based on which edge was matched (only the first edge matches a side different from 0) 366 // Face 1 should match its edge 2 367 // Face 2 should match its edge 0 368 // Face 3 should match its edge 0 369 if (flip) { 370 newOrnt[faces[e + 1]] = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_TRIANGLE, -((c2 + (!e ? 1 : 2)) % 3 + 1), ornt[c]); 371 } else { 372 newOrnt[faces[e + 1]] = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_TRIANGLE, !e ? (c2 + 1) % 3 : c2, ornt[c]); 373 } 374 break; 375 } 376 } 377 PetscCheck(c < 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " could not find a face match for edge %" PetscInt_FMT, cell, e); 378 } 379 PetscCall(DMPlexRestoreOrientedCone(dm, newCone[0], &fcone, &fornt)); 380 PetscCall(DMPlexSetCone(dm, cell, newCone)); 381 PetscCall(DMPlexSetConeOrientation(dm, cell, newOrnt)); 382 PetscCall(DMPlexRestoreOrientedCone(dm, cell, &cone, &ornt)); 383 PetscFunctionReturn(PETSC_SUCCESS); 384 } 385 386 static PetscErrorCode ReorderHexahedron(DM dm, PetscInt cell) 387 { 388 const PetscInt *cone, *ornt, *fcone, *fornt, *farr; 389 const PetscInt faces[6] = {0, 5, 3, 4, 2, 1}; 390 PetscInt used[6] = {1, 0, 0, 0, 0, 0}; 391 PetscInt newCone[16], newOrnt[16]; 392 393 PetscFunctionBegin; 394 PetscCall(DMPlexGetOrientedCone(dm, cell, &cone, &ornt)); 395 newCone[0] = cone[0]; 396 newOrnt[0] = ornt[0]; 397 PetscCall(DMPlexGetOrientedCone(dm, newCone[0], &fcone, &fornt)); 398 farr = DMPolytopeTypeGetArrangement(DM_POLYTOPE_QUADRILATERAL, newOrnt[0]); 399 // Loop over each edge in the initial quadrilateral 400 for (PetscInt e = 0; e < 4; ++e) { 401 const PetscInt edge = fcone[farr[e * 2 + 0]], eornt = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr[e * 2 + 1], fornt[farr[e * 2 + 0]]); 402 PetscInt c; 403 404 // Loop over each remaining face in the hexahedron 405 // On face `newCone[0]`, trying to match edge `edge` with final orientation `eornt` to an edge on another face 406 for (c = 1; c < 6; ++c) { 407 const PetscInt *fcone2, *fornt2, *farr2; 408 PetscInt c2; 409 410 // Checking face `cone[c]` with orientation `ornt[c]` 411 PetscCall(DMPlexGetOrientedCone(dm, cone[c], &fcone2, &fornt2)); 412 farr2 = DMPolytopeTypeGetArrangement(DM_POLYTOPE_QUADRILATERAL, ornt[c]); 413 // Check for edge 414 for (c2 = 0; c2 < 4; ++c2) { 415 const PetscInt edge2 = fcone2[farr2[c2 * 2 + 0]], eornt2 = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr2[c2 * 2 + 1], fornt2[farr2[c2 * 2 + 0]]); 416 // Trying to match edge `edge2` with final orientation `eornt2` 417 if (edge == edge2) { 418 PetscCheck(eornt == -(eornt2 + 1), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " found twice with the same orientation", edge); 419 // Matched face `newCone[0]` with orientation `newOrnt[0]` to face `cone[c]` with orientation `ornt[c]` along edge `edge` 420 break; 421 } 422 } 423 if (c2 < 4) { 424 used[c] = 1; 425 newCone[faces[e + 1]] = cone[c]; 426 // Compute new orientation of face based on which edge was matched (only the first edge matches a side different from 0) 427 newOrnt[faces[e + 1]] = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_QUADRILATERAL, !e ? (c2 + 1) % 4 : c2, ornt[c]); 428 break; 429 } 430 } 431 PetscCheck(c < 6, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " could not find a face match for edge %" PetscInt_FMT, cell, e); 432 } 433 PetscCall(DMPlexRestoreOrientedCone(dm, newCone[0], &fcone, &fornt)); 434 // Add last face 435 { 436 PetscInt c, c2; 437 438 for (c = 1; c < 6; ++c) 439 if (!used[c]) break; 440 PetscCheck(c < 6, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " could not find an available face", cell); 441 // Match first edge to 3rd edge in newCone[2] 442 { 443 const PetscInt *fcone2, *fornt2, *farr2; 444 445 PetscCall(DMPlexGetOrientedCone(dm, newCone[2], &fcone, &fornt)); 446 farr = DMPolytopeTypeGetArrangement(DM_POLYTOPE_QUADRILATERAL, newOrnt[2]); 447 PetscCall(DMPlexGetOrientedCone(dm, cone[c], &fcone2, &fornt2)); 448 farr2 = DMPolytopeTypeGetArrangement(DM_POLYTOPE_QUADRILATERAL, ornt[c]); 449 450 const PetscInt e = 2; 451 const PetscInt edge = fcone[farr[e * 2 + 0]], eornt = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr[e * 2 + 1], fornt[farr[e * 2 + 0]]); 452 // Trying to match edge `edge` with final orientation `eornt` of face `newCone[2]` to some edge of face `cone[c]` with orientation `ornt[c]` 453 for (c2 = 0; c2 < 4; ++c2) { 454 const PetscInt edge2 = fcone2[farr2[c2 * 2 + 0]], eornt2 = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_SEGMENT, farr2[c2 * 2 + 1], fornt2[farr2[c2 * 2 + 0]]); 455 // Trying to match edge `edge2` with final orientation `eornt2` 456 if (edge == edge2) { 457 PetscCheck(eornt == -(eornt2 + 1), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " found twice with the same orientation", edge); 458 // Matched face `newCone[2]` with orientation `newOrnt[2]` to face `cone[c]` with orientation `ornt[c]` along edge `edge` 459 break; 460 } 461 } 462 PetscCheck(c2 < 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not fit last face in"); 463 } 464 newCone[faces[5]] = cone[c]; 465 // Compute new orientation of face based on which edge was matched 466 newOrnt[faces[5]] = DMPolytopeTypeComposeOrientation(DM_POLYTOPE_QUADRILATERAL, c2, ornt[c]); 467 PetscCall(DMPlexRestoreOrientedCone(dm, newCone[0], &fcone, &fornt)); 468 } 469 PetscCall(DMPlexSetCone(dm, cell, newCone)); 470 PetscCall(DMPlexSetConeOrientation(dm, cell, newOrnt)); 471 PetscCall(DMPlexRestoreOrientedCone(dm, cell, &cone, &ornt)); 472 PetscFunctionReturn(PETSC_SUCCESS); 473 } 474 475 static PetscErrorCode ReorderCell(PetscViewer viewer, DM dm, PetscInt cell, DMPolytopeType ct) 476 { 477 PetscFunctionBegin; 478 switch (ct) { 479 case DM_POLYTOPE_TRIANGLE: 480 case DM_POLYTOPE_QUADRILATERAL: 481 PetscCall(ReorderPolygon(dm, cell)); 482 break; 483 case DM_POLYTOPE_TETRAHEDRON: 484 PetscCall(ReorderTetrahedron(viewer, dm, cell)); 485 break; 486 case DM_POLYTOPE_HEXAHEDRON: 487 PetscCall(ReorderHexahedron(dm, cell)); 488 break; 489 default: 490 PetscCheck(0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Celltype %s is unsupported", DMPolytopeTypes[ct]); 491 break; 492 } 493 PetscFunctionReturn(PETSC_SUCCESS); 494 } 495 496 /*@C 497 DMPlexCreateFluent - Create a `DMPLEX` mesh from a Fluent mesh file <http://aerojet.engr.ucdavis.edu/fluenthelp/html/ug/node1490.htm>. 498 499 Collective 500 501 Input Parameters: 502 + comm - The MPI communicator 503 . viewer - The `PetscViewer` associated with a Fluent mesh file 504 - interpolate - Create faces and edges in the mesh 505 506 Output Parameter: 507 . dm - The `DM` object representing the mesh 508 509 Level: beginner 510 511 .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMCreate()` 512 @*/ 513 PetscErrorCode DMPlexCreateFluent(MPI_Comm comm, PetscViewer viewer, PetscBool interpolate, DM *dm) 514 { 515 PetscInt dim = PETSC_DETERMINE; 516 PetscInt numCells = 0; 517 PetscInt numVertices = 0; 518 PetscInt numCellFaces = PETSC_DETERMINE; 519 DMPolytopeType ct = DM_POLYTOPE_UNKNOWN_CELL; 520 PetscInt numFaces = 0; 521 PetscInt numFaceEntries = PETSC_DETERMINE; 522 PetscInt numFaceVertices = PETSC_DETERMINE; 523 PetscInt *faces = NULL; 524 PetscInt *cellVertices = NULL; 525 unsigned int *faceZoneIDs = NULL; 526 DMLabel faceSets = NULL; 527 DMLabel *zoneLabels = NULL; 528 const char **zoneNames = NULL; 529 unsigned int maxZoneID = 0; 530 PetscScalar *coordsIn = NULL; 531 PetscScalar *coords; 532 PetscSection coordSection; 533 Vec coordinates; 534 PetscInt coordSize, f; 535 PetscMPIInt rank; 536 537 PetscFunctionBegin; 538 PetscCallMPI(MPI_Comm_rank(comm, &rank)); 539 540 if (rank == 0) { 541 FluentSection s; 542 numFaces = PETSC_DETERMINE; 543 do { 544 PetscCall(DMPlexCreateFluent_ReadSection(viewer, &s)); 545 if (s.index == 2) { /* Dimension */ 546 dim = s.nd; 547 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found dimension: %" PetscInt_FMT "\n", dim)); 548 } else if (s.index == 10 || s.index == 2010) { /* Vertices */ 549 if (s.zoneID == 0) { 550 numVertices = s.last; 551 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found number of vertices: %" PetscInt_FMT "\n", numVertices)); 552 } else { 553 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found vertex coordinates\n")); 554 PetscCheck(!coordsIn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Currently no support for multiple coordinate sets in Fluent files"); 555 coordsIn = (PetscScalar *)s.data; 556 } 557 558 } else if (s.index == 12 || s.index == 2012) { /* Cells */ 559 if (s.zoneID == 0) { 560 numCells = s.last; 561 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found number of cells %" PetscInt_FMT "\n", numCells)); 562 } else { 563 switch (s.nd) { 564 case 0: 565 numCellFaces = PETSC_DETERMINE; 566 ct = DM_POLYTOPE_POINT; 567 break; 568 case 1: 569 numCellFaces = 3; 570 ct = DM_POLYTOPE_TRIANGLE; 571 break; 572 case 2: 573 numCellFaces = 4; 574 ct = DM_POLYTOPE_TETRAHEDRON; 575 break; 576 case 3: 577 numCellFaces = 4; 578 ct = DM_POLYTOPE_QUADRILATERAL; 579 break; 580 case 4: 581 numCellFaces = 6; 582 ct = DM_POLYTOPE_HEXAHEDRON; 583 break; 584 case 5: 585 numCellFaces = 5; 586 ct = DM_POLYTOPE_PYRAMID; 587 break; 588 case 6: 589 numCellFaces = 5; 590 ct = DM_POLYTOPE_TRI_PRISM; 591 break; 592 default: 593 numCellFaces = PETSC_DETERMINE; 594 } 595 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found number of cell faces %" PetscInt_FMT "\n", numCellFaces)); 596 } 597 } else if (s.index == 13 || s.index == 2013) { /* Facets */ 598 if (s.zoneID == 0) { /* Header section */ 599 numFaces = (PetscInt)(s.last - s.first + 1); 600 if (s.nd == 0 || s.nd == 5) numFaceVertices = PETSC_DETERMINE; 601 else numFaceVertices = s.nd; 602 PetscCall(PetscInfo((PetscObject)viewer, "CASE: Found number of faces %" PetscInt_FMT " face vertices: %" PetscInt_FMT "\n", numFaces, numFaceVertices)); 603 } else { /* Data section */ 604 unsigned int z; 605 606 PetscCheck(numFaceVertices == PETSC_DETERMINE || s.nd == numFaceVertices, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mixed facets in Fluent files are not supported"); 607 PetscCheck(numFaces >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No header section for facets in Fluent file"); 608 if (numFaceVertices == PETSC_DETERMINE) numFaceVertices = s.nd; 609 numFaceEntries = numFaceVertices + 2; 610 if (!faces) PetscCall(PetscMalloc1(numFaces * numFaceEntries, &faces)); 611 if (!faceZoneIDs) PetscCall(PetscMalloc1(numFaces, &faceZoneIDs)); 612 PetscCall(PetscMemcpy(&faces[(s.first - 1) * numFaceEntries], s.data, (s.last - s.first + 1) * numFaceEntries * sizeof(PetscInt))); 613 /* Record the zoneID for each face set */ 614 for (z = s.first - 1; z < s.last; z++) faceZoneIDs[z] = s.zoneID; 615 PetscCall(PetscFree(s.data)); 616 } 617 } else if (s.index == 39) { /* Label information */ 618 if (s.zoneID >= maxZoneID) { 619 DMLabel *tmpL; 620 const char **tmp; 621 unsigned int newmax = maxZoneID + 1; 622 623 while (newmax < s.zoneID + 1) newmax *= 2; 624 PetscCall(PetscCalloc2(newmax, &tmp, newmax, &tmpL)); 625 for (PetscInt i = 0; i < (PetscInt)maxZoneID; ++i) { 626 tmp[i] = zoneNames[i]; 627 tmpL[i] = zoneLabels[i]; 628 } 629 maxZoneID = newmax; 630 PetscCall(PetscFree2(zoneNames, zoneLabels)); 631 zoneNames = tmp; 632 zoneLabels = tmpL; 633 } 634 zoneNames[s.zoneID] = (const char *)s.data; 635 } 636 } while (s.index >= 0); 637 } 638 PetscCallMPI(MPI_Bcast(&dim, 1, MPIU_INT, 0, comm)); 639 PetscCheck(dim >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Fluent file does not include dimension"); 640 641 /* Allocate cell-vertex mesh */ 642 PetscCall(DMCreate(comm, dm)); 643 PetscCall(DMSetType(*dm, DMPLEX)); 644 PetscCall(DMSetDimension(*dm, dim)); 645 PetscCall(DMPlexSetChart(*dm, 0, numCells + numFaces + numVertices)); 646 if (rank == 0) { 647 PetscCheck(numCells >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown number of cells in Fluent file"); 648 /* If no cell type was given we assume simplices */ 649 if (numCellFaces == PETSC_DETERMINE) { 650 numCellFaces = numFaceVertices + 1; 651 ct = numCellFaces == 3 ? DM_POLYTOPE_TRIANGLE : (numCellFaces == 4 ? DM_POLYTOPE_TETRAHEDRON : DM_POLYTOPE_UNKNOWN_CELL); 652 } 653 for (PetscInt c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(*dm, c, numCellFaces)); 654 for (PetscInt f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(*dm, f + numCells + numVertices, numFaceVertices)); 655 } 656 PetscCall(DMSetUp(*dm)); 657 658 if (rank == 0 && faces) { 659 PetscInt *cones; 660 661 PetscCall(DMPlexGetCones(*dm, &cones)); 662 PetscCheck(numCellFaces < 16, PETSC_COMM_SELF, PETSC_ERR_SUP, "Number of cell faces %" PetscInt_FMT " exceeds temporary storage", numCellFaces); 663 PetscCheck(numFaceVertices < 16, PETSC_COMM_SELF, PETSC_ERR_SUP, "Number of face vertices %" PetscInt_FMT " exceeds temporary storage", numFaceVertices); 664 for (PetscInt c = 0; c < numCells * numCellFaces; ++c) cones[c] = -1; 665 for (PetscInt f = 0; f < numFaces; f++) { 666 const PetscInt cl = faces[f * numFaceEntries + numFaceVertices] - 1; 667 const PetscInt cr = faces[f * numFaceEntries + numFaceVertices + 1] - 1; 668 const PetscInt face = f + numCells + numVertices; 669 const PetscInt *fc = &faces[f * numFaceEntries]; 670 PetscInt fcone[16]; 671 672 // How could Fluent define the outward normal differently? Is there no end to the pain? 673 if (dim == 3) { 674 if (cl >= 0) PetscCall(InsertFace(*dm, cl, face, -1)); 675 if (cr >= 0) PetscCall(InsertFace(*dm, cr, face, 0)); 676 } else { 677 if (cl >= 0) PetscCall(InsertFace(*dm, cl, face, 0)); 678 if (cr >= 0) PetscCall(InsertFace(*dm, cr, face, -1)); 679 } 680 for (PetscInt v = 0; v < numFaceVertices; ++v) fcone[v] = fc[v] + numCells - 1; 681 PetscCall(DMPlexSetCone(*dm, face, fcone)); 682 } 683 } 684 PetscCall(DMPlexSymmetrize(*dm)); 685 PetscCall(DMPlexStratify(*dm)); 686 if (dim == 3) { 687 DM idm; 688 689 PetscCall(DMCreate(PetscObjectComm((PetscObject)*dm), &idm)); 690 PetscCall(DMSetType(idm, DMPLEX)); 691 PetscCall(DMSetDimension(idm, dim)); 692 PetscCall(DMPlexInterpolateFaces_Internal(*dm, 1, idm)); 693 PetscCall(DMDestroy(dm)); 694 *dm = idm; 695 } 696 PetscCall(DMViewFromOptions(*dm, NULL, "-cas_dm_view")); 697 if (rank == 0 && faces) { 698 for (PetscInt c = 0; c < numCells; ++c) PetscCall(ReorderCell(viewer, *dm, c, ct)); 699 } 700 701 if (rank == 0 && faces) { 702 PetscInt fi, joinSize, meetSize, *fverts, cells[2]; 703 const PetscInt *join, *meet; 704 PetscCall(PetscMalloc1(numFaceVertices, &fverts)); 705 /* Mark facets by finding the full join of all adjacent vertices */ 706 for (f = 0; f < numFaces; f++) { 707 const PetscInt cl = faces[f * numFaceEntries + numFaceVertices] - 1; 708 const PetscInt cr = faces[f * numFaceEntries + numFaceVertices + 1] - 1; 709 const PetscInt id = (PetscInt)faceZoneIDs[f]; 710 711 if (cl > 0 && cr > 0) { 712 /* If we know both adjoining cells we can use a single-level meet */ 713 cells[0] = cl; 714 cells[1] = cr; 715 PetscCall(DMPlexGetMeet(*dm, 2, cells, &meetSize, &meet)); 716 PetscCheck(meetSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not determine Plex facet for Fluent face %" PetscInt_FMT " cells: %" PetscInt_FMT ", %" PetscInt_FMT, f, cl, cr); 717 PetscCall(DMSetLabelValue_Fast(*dm, &faceSets, "Face Sets", meet[0], id)); 718 if (zoneNames && zoneNames[id]) PetscCall(DMSetLabelValue_Fast(*dm, &zoneLabels[id], zoneNames[id], meet[0], 1)); 719 PetscCall(DMPlexRestoreMeet(*dm, numFaceVertices, fverts, &meetSize, &meet)); 720 } else { 721 for (fi = 0; fi < numFaceVertices; fi++) fverts[fi] = faces[f * numFaceEntries + fi] + numCells - 1; 722 PetscCall(DMPlexGetFullJoin(*dm, numFaceVertices, fverts, &joinSize, &join)); 723 PetscCheck(joinSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not determine Plex facet for Fluent face %" PetscInt_FMT, f); 724 PetscCall(DMSetLabelValue_Fast(*dm, &faceSets, "Face Sets", join[0], id)); 725 if (zoneNames && zoneNames[id]) PetscCall(DMSetLabelValue_Fast(*dm, &zoneLabels[id], zoneNames[id], join[0], 1)); 726 PetscCall(DMPlexRestoreJoin(*dm, numFaceVertices, fverts, &joinSize, &join)); 727 } 728 } 729 PetscCall(PetscFree(fverts)); 730 } 731 732 { /* Create Face Sets label at all processes */ 733 enum { 734 n = 1 735 }; 736 PetscBool flag[n]; 737 738 flag[0] = faceSets ? PETSC_TRUE : PETSC_FALSE; 739 PetscCallMPI(MPI_Bcast(flag, n, MPIU_BOOL, 0, comm)); 740 if (flag[0]) PetscCall(DMCreateLabel(*dm, "Face Sets")); 741 // TODO Code to create all the zone labels on each process 742 } 743 744 if (!interpolate) { 745 DM udm; 746 747 PetscCall(DMPlexUninterpolate(*dm, &udm)); 748 PetscCall(DMDestroy(dm)); 749 *dm = udm; 750 } 751 752 /* Read coordinates */ 753 PetscCall(DMGetCoordinateSection(*dm, &coordSection)); 754 PetscCall(PetscSectionSetNumFields(coordSection, 1)); 755 PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 756 PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 757 for (PetscInt v = numCells; v < numCells + numVertices; ++v) { 758 PetscCall(PetscSectionSetDof(coordSection, v, dim)); 759 PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 760 } 761 PetscCall(PetscSectionSetUp(coordSection)); 762 PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 763 PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 764 PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 765 PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 766 PetscCall(VecSetType(coordinates, VECSTANDARD)); 767 PetscCall(VecGetArray(coordinates, &coords)); 768 if (rank == 0 && coordsIn) { 769 for (PetscInt v = 0; v < numVertices; ++v) { 770 for (PetscInt d = 0; d < dim; ++d) coords[v * dim + d] = coordsIn[v * dim + d]; 771 } 772 } 773 PetscCall(VecRestoreArray(coordinates, &coords)); 774 PetscCall(DMSetCoordinatesLocal(*dm, coordinates)); 775 PetscCall(VecDestroy(&coordinates)); 776 777 if (rank == 0) { 778 PetscCall(PetscFree(cellVertices)); 779 PetscCall(PetscFree(faces)); 780 PetscCall(PetscFree(faceZoneIDs)); 781 PetscCall(PetscFree(coordsIn)); 782 if (zoneNames) 783 for (PetscInt i = 0; i < (PetscInt)maxZoneID; ++i) PetscCall(PetscFree(zoneNames[i])); 784 PetscCall(PetscFree2(zoneNames, zoneLabels)); 785 } 786 PetscFunctionReturn(PETSC_SUCCESS); 787 } 788