10039db0dSBarry Smith #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for fileno() */ 22f0bd6dcSMichael Lange #define PETSCDM_DLL 3af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 42f0bd6dcSMichael Lange 52f0bd6dcSMichael Lange /*@C 62f0bd6dcSMichael Lange DMPlexCreateFluentFromFile - Create a DMPlex mesh from a Fluent mesh file 72f0bd6dcSMichael Lange 82f0bd6dcSMichael Lange + comm - The MPI communicator 92f0bd6dcSMichael Lange . filename - Name of the Fluent mesh file 102f0bd6dcSMichael Lange - interpolate - Create faces and edges in the mesh 112f0bd6dcSMichael Lange 122f0bd6dcSMichael Lange Output Parameter: 132f0bd6dcSMichael Lange . dm - The DM object representing the mesh 142f0bd6dcSMichael Lange 152f0bd6dcSMichael Lange Level: beginner 162f0bd6dcSMichael Lange 17db781477SPatrick Sanan .seealso: `DMPlexCreateFromFile()`, `DMPlexCreateFluent()`, `DMPlexCreate()` 182f0bd6dcSMichael Lange @*/ 199371c9d4SSatish Balay PetscErrorCode DMPlexCreateFluentFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm) { 202f0bd6dcSMichael Lange PetscViewer viewer; 212f0bd6dcSMichael Lange 222f0bd6dcSMichael Lange PetscFunctionBegin; 232f0bd6dcSMichael Lange /* Create file viewer and build plex */ 249566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(comm, &viewer)); 259566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII)); 269566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ)); 279566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetName(viewer, filename)); 289566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent(comm, viewer, interpolate, dm)); 299566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 302f0bd6dcSMichael Lange PetscFunctionReturn(0); 312f0bd6dcSMichael Lange } 322f0bd6dcSMichael Lange 339371c9d4SSatish Balay static PetscErrorCode DMPlexCreateFluent_ReadString(PetscViewer viewer, char *buffer, char delim) { 344b375a39SMichael Lange PetscInt ret, i = 0; 352f0bd6dcSMichael Lange 362f0bd6dcSMichael Lange PetscFunctionBegin; 379566063dSJacob Faibussowitsch do PetscCall(PetscViewerRead(viewer, &(buffer[i++]), 1, &ret, PETSC_CHAR)); 384b375a39SMichael Lange while (ret > 0 && buffer[i - 1] != '\0' && buffer[i - 1] != delim); 399371c9d4SSatish Balay if (!ret) buffer[i - 1] = '\0'; 409371c9d4SSatish Balay else buffer[i] = '\0'; 412f0bd6dcSMichael Lange PetscFunctionReturn(0); 422f0bd6dcSMichael Lange } 432f0bd6dcSMichael Lange 449371c9d4SSatish Balay static PetscErrorCode DMPlexCreateFluent_ReadValues(PetscViewer viewer, void *data, PetscInt count, PetscDataType dtype, PetscBool binary) { 4570c9a859SSatish Balay int fdes = 0; 4619d58f9dSMichael Lange FILE *file; 4719d58f9dSMichael Lange PetscInt i; 4819d58f9dSMichael Lange 4919d58f9dSMichael Lange PetscFunctionBegin; 5019d58f9dSMichael Lange if (binary) { 5119d58f9dSMichael Lange /* Extract raw file descriptor to read binary block */ 529566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetPointer(viewer, &file)); 539371c9d4SSatish Balay fflush(file); 549371c9d4SSatish Balay fdes = fileno(file); 5519d58f9dSMichael Lange } 5619d58f9dSMichael Lange 5719d58f9dSMichael Lange if (!binary && dtype == PETSC_INT) { 5819d58f9dSMichael Lange char cbuf[256]; 59cfb60857SMatthew G. Knepley unsigned int ibuf; 60cfb60857SMatthew G. Knepley int snum; 6119d58f9dSMichael Lange /* Parse hexadecimal ascii integers */ 6219d58f9dSMichael Lange for (i = 0; i < count; i++) { 639566063dSJacob Faibussowitsch PetscCall(PetscViewerRead(viewer, cbuf, 1, NULL, PETSC_STRING)); 6419d58f9dSMichael Lange snum = sscanf(cbuf, "%x", &ibuf); 6508401ef6SPierre Jolivet PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 6619d58f9dSMichael Lange ((PetscInt *)data)[i] = (PetscInt)ibuf; 6719d58f9dSMichael Lange } 6819d58f9dSMichael Lange } else if (binary && dtype == PETSC_INT) { 6919d58f9dSMichael Lange /* Always read 32-bit ints and cast to PetscInt */ 7019d58f9dSMichael Lange int *ibuf; 719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(count, &ibuf)); 729566063dSJacob Faibussowitsch PetscCall(PetscBinaryRead(fdes, ibuf, count, NULL, PETSC_ENUM)); 739566063dSJacob Faibussowitsch PetscCall(PetscByteSwap(ibuf, PETSC_ENUM, count)); 7419d58f9dSMichael Lange for (i = 0; i < count; i++) ((PetscInt *)data)[i] = (PetscInt)(ibuf[i]); 759566063dSJacob Faibussowitsch PetscCall(PetscFree(ibuf)); 7619d58f9dSMichael Lange 7719d58f9dSMichael Lange } else if (binary && dtype == PETSC_SCALAR) { 7819d58f9dSMichael Lange float *fbuf; 7919d58f9dSMichael Lange /* Always read 32-bit floats and cast to PetscScalar */ 809566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(count, &fbuf)); 819566063dSJacob Faibussowitsch PetscCall(PetscBinaryRead(fdes, fbuf, count, NULL, PETSC_FLOAT)); 829566063dSJacob Faibussowitsch PetscCall(PetscByteSwap(fbuf, PETSC_FLOAT, count)); 8319d58f9dSMichael Lange for (i = 0; i < count; i++) ((PetscScalar *)data)[i] = (PetscScalar)(fbuf[i]); 849566063dSJacob Faibussowitsch PetscCall(PetscFree(fbuf)); 8519d58f9dSMichael Lange } else { 869566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIRead(viewer, data, count, NULL, dtype)); 8719d58f9dSMichael Lange } 8819d58f9dSMichael Lange PetscFunctionReturn(0); 8919d58f9dSMichael Lange } 9019d58f9dSMichael Lange 919371c9d4SSatish Balay static PetscErrorCode DMPlexCreateFluent_ReadSection(PetscViewer viewer, FluentSection *s) { 922f0bd6dcSMichael Lange char buffer[PETSC_MAX_PATH_LEN]; 932f0bd6dcSMichael Lange int snum; 942f0bd6dcSMichael Lange 952f0bd6dcSMichael Lange PetscFunctionBegin; 962f0bd6dcSMichael Lange /* Fast-forward to next section and derive its index */ 979566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 989566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ' ')); 992f0bd6dcSMichael Lange snum = sscanf(buffer, "%d", &(s->index)); 1002f0bd6dcSMichael Lange /* If we can't match an index return -1 to signal end-of-file */ 1019371c9d4SSatish Balay if (snum < 1) { 1029371c9d4SSatish Balay s->index = -1; 1039371c9d4SSatish Balay PetscFunctionReturn(0); 1049371c9d4SSatish Balay } 1052f0bd6dcSMichael Lange 1062f0bd6dcSMichael Lange if (s->index == 0) { /* Comment */ 1079566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1082f0bd6dcSMichael Lange 1092f0bd6dcSMichael Lange } else if (s->index == 2) { /* Dimension */ 1109566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1112f0bd6dcSMichael Lange snum = sscanf(buffer, "%d", &(s->nd)); 11208401ef6SPierre Jolivet PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 1132f0bd6dcSMichael Lange 11419d58f9dSMichael Lange } else if (s->index == 10 || s->index == 2010) { /* Vertices */ 1159566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1162f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x %x %x %d %d)", &(s->zoneID), &(s->first), &(s->last), &(s->type), &(s->nd)); 11708401ef6SPierre Jolivet PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 1183f6dc66eSMichael Lange if (s->zoneID > 0) { 1193f6dc66eSMichael Lange PetscInt numCoords = s->last - s->first + 1; 1209566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 1219566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(s->nd * numCoords, (PetscScalar **)&s->data)); 1229566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadValues(viewer, s->data, s->nd * numCoords, PETSC_SCALAR, s->index == 2010 ? PETSC_TRUE : PETSC_FALSE)); 1239566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1243f6dc66eSMichael Lange } 1259566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1262f0bd6dcSMichael Lange 12719d58f9dSMichael Lange } else if (s->index == 12 || s->index == 2012) { /* Cells */ 1289566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1292f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x", &(s->zoneID)); 13008401ef6SPierre Jolivet PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 1312f0bd6dcSMichael Lange if (s->zoneID == 0) { /* Header section */ 1322f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x %x %x %d)", &(s->zoneID), &(s->first), &(s->last), &(s->nd)); 13308401ef6SPierre Jolivet PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 134f7320561SMichael Lange } else { /* Data section */ 1352f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x %x %x %d %d)", &(s->zoneID), &(s->first), &(s->last), &(s->type), &(s->nd)); 13608401ef6SPierre Jolivet PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 137895fcc3eSMichael Lange if (s->nd == 0) { 138895fcc3eSMichael Lange /* Read cell type definitions for mixed cells */ 13919d58f9dSMichael Lange PetscInt numCells = s->last - s->first + 1; 1409566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 1419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells, (PetscInt **)&s->data)); 1429566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadValues(viewer, s->data, numCells, PETSC_INT, s->index == 2012 ? PETSC_TRUE : PETSC_FALSE)); 1439566063dSJacob Faibussowitsch PetscCall(PetscFree(s->data)); 1449566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 145895fcc3eSMichael Lange } 1462f0bd6dcSMichael Lange } 1479566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1482f0bd6dcSMichael Lange 14919d58f9dSMichael Lange } else if (s->index == 13 || s->index == 2013) { /* Faces */ 1509566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1512f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x", &(s->zoneID)); 15208401ef6SPierre Jolivet PetscCheck(snum == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 1532f0bd6dcSMichael Lange if (s->zoneID == 0) { /* Header section */ 1542f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x %x %x %d)", &(s->zoneID), &(s->first), &(s->last), &(s->nd)); 15508401ef6SPierre Jolivet PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 156f7320561SMichael Lange } else { /* Data section */ 157137d0469SJed Brown PetscInt f, numEntries, numFaces; 1582f0bd6dcSMichael Lange snum = sscanf(buffer, "(%x %x %x %d %d)", &(s->zoneID), &(s->first), &(s->last), &(s->type), &(s->nd)); 15908401ef6SPierre Jolivet PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "File is not a valid Fluent file"); 1609566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '(')); 161f7320561SMichael Lange switch (s->nd) { 162895fcc3eSMichael Lange case 0: numEntries = PETSC_DETERMINE; break; 163f7320561SMichael Lange case 2: numEntries = 2 + 2; break; /* linear */ 164f7320561SMichael Lange case 3: numEntries = 2 + 3; break; /* triangular */ 165f7320561SMichael Lange case 4: numEntries = 2 + 4; break; /* quadrilateral */ 166f7320561SMichael Lange default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown face type in Fluent file"); 167f7320561SMichael Lange } 168f7320561SMichael Lange numFaces = s->last - s->first + 1; 169895fcc3eSMichael Lange if (numEntries != PETSC_DETERMINE) { 170895fcc3eSMichael Lange /* Allocate space only if we already know the size of the block */ 1719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numEntries * numFaces, (PetscInt **)&s->data)); 172895fcc3eSMichael Lange } 173f7320561SMichael Lange for (f = 0; f < numFaces; f++) { 174895fcc3eSMichael Lange if (s->nd == 0) { 175895fcc3eSMichael Lange /* Determine the size of the block for "mixed" facets */ 176137d0469SJed Brown PetscInt numFaceVert = 0; 1779566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadValues(viewer, &numFaceVert, 1, PETSC_INT, s->index == 2013 ? PETSC_TRUE : PETSC_FALSE)); 178895fcc3eSMichael Lange if (numEntries == PETSC_DETERMINE) { 179895fcc3eSMichael Lange numEntries = numFaceVert + 2; 1809566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numEntries * numFaces, (PetscInt **)&s->data)); 181895fcc3eSMichael Lange } else { 1821dca8a05SBarry Smith PetscCheck(numEntries == numFaceVert + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No support for mixed faces in Fluent files"); 183895fcc3eSMichael Lange } 184895fcc3eSMichael Lange } 1859566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadValues(viewer, &(((PetscInt *)s->data)[f * numEntries]), numEntries, PETSC_INT, s->index == 2013 ? PETSC_TRUE : PETSC_FALSE)); 186f7320561SMichael Lange } 187895fcc3eSMichael Lange s->nd = numEntries - 2; 1889566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1892f0bd6dcSMichael Lange } 1909566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, ')')); 1912f0bd6dcSMichael Lange 1922f0bd6dcSMichael Lange } else { /* Unknown section type */ 193060da220SMatthew G. Knepley PetscInt depth = 1; 1942f0bd6dcSMichael Lange do { 1952f0bd6dcSMichael Lange /* Match parentheses when parsing unknown sections */ 1969566063dSJacob Faibussowitsch do PetscCall(PetscViewerRead(viewer, &(buffer[0]), 1, NULL, PETSC_CHAR)); 1972f0bd6dcSMichael Lange while (buffer[0] != '(' && buffer[0] != ')'); 1982f0bd6dcSMichael Lange if (buffer[0] == '(') depth++; 1992f0bd6dcSMichael Lange if (buffer[0] == ')') depth--; 2002f0bd6dcSMichael Lange } while (depth > 0); 2019566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadString(viewer, buffer, '\n')); 2022f0bd6dcSMichael Lange } 2032f0bd6dcSMichael Lange PetscFunctionReturn(0); 2042f0bd6dcSMichael Lange } 2052f0bd6dcSMichael Lange 2062f0bd6dcSMichael Lange /*@C 2072f0bd6dcSMichael Lange DMPlexCreateFluent - Create a DMPlex mesh from a Fluent mesh file. 2082f0bd6dcSMichael Lange 209d083f849SBarry Smith Collective 2102f0bd6dcSMichael Lange 2112f0bd6dcSMichael Lange Input Parameters: 2122f0bd6dcSMichael Lange + comm - The MPI communicator 2132f0bd6dcSMichael Lange . viewer - The Viewer associated with a Fluent mesh file 2142f0bd6dcSMichael Lange - interpolate - Create faces and edges in the mesh 2152f0bd6dcSMichael Lange 2162f0bd6dcSMichael Lange Output Parameter: 2172f0bd6dcSMichael Lange . dm - The DM object representing the mesh 2182f0bd6dcSMichael Lange 2192f0bd6dcSMichael Lange Note: http://aerojet.engr.ucdavis.edu/fluenthelp/html/ug/node1490.htm 2202f0bd6dcSMichael Lange 2212f0bd6dcSMichael Lange Level: beginner 2222f0bd6dcSMichael Lange 223db781477SPatrick Sanan .seealso: `DMPLEX`, `DMCreate()` 2242f0bd6dcSMichael Lange @*/ 2259371c9d4SSatish Balay PetscErrorCode DMPlexCreateFluent(MPI_Comm comm, PetscViewer viewer, PetscBool interpolate, DM *dm) { 2262f0bd6dcSMichael Lange PetscMPIInt rank; 227c2fc583bSSatish Balay PetscInt c, v, dim = PETSC_DETERMINE, numCells = 0, numVertices = 0, numCellVertices = PETSC_DETERMINE; 228cf554e2cSMatthew G. Knepley PetscInt numFaces = PETSC_DETERMINE, f, numFaceEntries = PETSC_DETERMINE, numFaceVertices = PETSC_DETERMINE; 2293ed8799eSMichael Lange PetscInt *faces = NULL, *cellVertices = NULL, *faceZoneIDs = NULL; 2307368db69SLisandro Dalcin DMLabel faceSets = NULL; 2313f6dc66eSMichael Lange PetscInt d, coordSize; 2323f6dc66eSMichael Lange PetscScalar *coords, *coordsIn = NULL; 2333f6dc66eSMichael Lange PetscSection coordSection; 2343f6dc66eSMichael Lange Vec coordinates; 2352f0bd6dcSMichael Lange 2362f0bd6dcSMichael Lange PetscFunctionBegin; 2379566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 2382f0bd6dcSMichael Lange 239dd400576SPatrick Sanan if (rank == 0) { 2402f0bd6dcSMichael Lange FluentSection s; 2412f0bd6dcSMichael Lange do { 2429566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluent_ReadSection(viewer, &s)); 2432f0bd6dcSMichael Lange if (s.index == 2) { /* Dimension */ 244f7320561SMichael Lange dim = s.nd; 2452f0bd6dcSMichael Lange 24619d58f9dSMichael Lange } else if (s.index == 10 || s.index == 2010) { /* Vertices */ 247f7320561SMichael Lange if (s.zoneID == 0) numVertices = s.last; 2483f6dc66eSMichael Lange else { 24928b400f6SJacob Faibussowitsch PetscCheck(!coordsIn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Currently no support for multiple coordinate sets in Fluent files"); 2501a9c30ecSMatthew G. Knepley coordsIn = (PetscScalar *)s.data; 2513f6dc66eSMichael Lange } 2522f0bd6dcSMichael Lange 25319d58f9dSMichael Lange } else if (s.index == 12 || s.index == 2012) { /* Cells */ 254f7320561SMichael Lange if (s.zoneID == 0) numCells = s.last; 255f7320561SMichael Lange else { 256f7320561SMichael Lange switch (s.nd) { 257895fcc3eSMichael Lange case 0: numCellVertices = PETSC_DETERMINE; break; 258f7320561SMichael Lange case 1: numCellVertices = 3; break; /* triangular */ 259f7320561SMichael Lange case 2: numCellVertices = 4; break; /* tetrahedral */ 260f7320561SMichael Lange case 3: numCellVertices = 4; break; /* quadrilateral */ 261f7320561SMichael Lange case 4: numCellVertices = 8; break; /* hexahedral */ 262f7320561SMichael Lange case 5: numCellVertices = 5; break; /* pyramid */ 263f7320561SMichael Lange case 6: numCellVertices = 6; break; /* wedge */ 264895fcc3eSMichael Lange default: numCellVertices = PETSC_DETERMINE; 265f7320561SMichael Lange } 266f7320561SMichael Lange } 2672f0bd6dcSMichael Lange 26819d58f9dSMichael Lange } else if (s.index == 13 || s.index == 2013) { /* Facets */ 269f7320561SMichael Lange if (s.zoneID == 0) { /* Header section */ 270930bae4bSMatthew G. Knepley numFaces = (PetscInt)(s.last - s.first + 1); 27135462f7fSMichael Lange if (s.nd == 0 || s.nd == 5) numFaceVertices = PETSC_DETERMINE; 272895fcc3eSMichael Lange else numFaceVertices = s.nd; 273f7320561SMichael Lange } else { /* Data section */ 274cf554e2cSMatthew G. Knepley unsigned int z; 275cf554e2cSMatthew G. Knepley 2761dca8a05SBarry Smith PetscCheck(numFaceVertices == PETSC_DETERMINE || s.nd == numFaceVertices, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mixed facets in Fluent files are not supported"); 27708401ef6SPierre Jolivet PetscCheck(numFaces >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No header section for facets in Fluent file"); 278f7320561SMichael Lange if (numFaceVertices == PETSC_DETERMINE) numFaceVertices = s.nd; 279f7320561SMichael Lange numFaceEntries = numFaceVertices + 2; 2809566063dSJacob Faibussowitsch if (!faces) PetscCall(PetscMalloc1(numFaces * numFaceEntries, &faces)); 2819566063dSJacob Faibussowitsch if (!faceZoneIDs) PetscCall(PetscMalloc1(numFaces, &faceZoneIDs)); 2829566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(&faces[(s.first - 1) * numFaceEntries], s.data, (s.last - s.first + 1) * numFaceEntries * sizeof(PetscInt))); 283ec78a56aSMichael Lange /* Record the zoneID for each face set */ 284cf554e2cSMatthew G. Knepley for (z = s.first - 1; z < s.last; z++) faceZoneIDs[z] = s.zoneID; 2859566063dSJacob Faibussowitsch PetscCall(PetscFree(s.data)); 286f7320561SMichael Lange } 2872f0bd6dcSMichael Lange } 2882f0bd6dcSMichael Lange } while (s.index >= 0); 2892f0bd6dcSMichael Lange } 2909566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&dim, 1, MPIU_INT, 0, comm)); 29108401ef6SPierre Jolivet PetscCheck(dim >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Fluent file does not include dimension"); 292f7320561SMichael Lange 293f7320561SMichael Lange /* Allocate cell-vertex mesh */ 2949566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 2959566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 2969566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*dm, dim)); 2979566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(*dm, 0, numCells + numVertices)); 298dd400576SPatrick Sanan if (rank == 0) { 29908401ef6SPierre Jolivet PetscCheck(numCells >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown number of cells in Fluent file"); 300895fcc3eSMichael Lange /* If no cell type was given we assume simplices */ 301895fcc3eSMichael Lange if (numCellVertices == PETSC_DETERMINE) numCellVertices = numFaceVertices + 1; 3029566063dSJacob Faibussowitsch for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(*dm, c, numCellVertices)); 303f7320561SMichael Lange } 3049566063dSJacob Faibussowitsch PetscCall(DMSetUp(*dm)); 305f7320561SMichael Lange 306dd400576SPatrick Sanan if (rank == 0 && faces) { 307f7320561SMichael Lange /* Derive cell-vertex list from face-vertex and face-cell maps */ 3089566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numCells * numCellVertices, &cellVertices)); 309f7320561SMichael Lange for (c = 0; c < numCells * numCellVertices; c++) cellVertices[c] = -1; 310f7320561SMichael Lange for (f = 0; f < numFaces; f++) { 311f7320561SMichael Lange PetscInt *cell; 312f7320561SMichael Lange const PetscInt cl = faces[f * numFaceEntries + numFaceVertices]; 313f7320561SMichael Lange const PetscInt cr = faces[f * numFaceEntries + numFaceVertices + 1]; 314f7320561SMichael Lange const PetscInt *face = &(faces[f * numFaceEntries]); 315f7320561SMichael Lange 316f7320561SMichael Lange if (cl > 0) { 317f7320561SMichael Lange cell = &(cellVertices[(cl - 1) * numCellVertices]); 318f7320561SMichael Lange for (v = 0; v < numFaceVertices; v++) { 319f7320561SMichael Lange PetscBool found = PETSC_FALSE; 320f7320561SMichael Lange for (c = 0; c < numCellVertices; c++) { 321f7320561SMichael Lange if (cell[c] < 0) break; 3229371c9d4SSatish Balay if (cell[c] == face[v] - 1 + numCells) { 3239371c9d4SSatish Balay found = PETSC_TRUE; 3249371c9d4SSatish Balay break; 3259371c9d4SSatish Balay } 326f7320561SMichael Lange } 327f7320561SMichael Lange if (!found) cell[c] = face[v] - 1 + numCells; 328f7320561SMichael Lange } 329f7320561SMichael Lange } 330f7320561SMichael Lange if (cr > 0) { 331f7320561SMichael Lange cell = &(cellVertices[(cr - 1) * numCellVertices]); 332f7320561SMichael Lange for (v = 0; v < numFaceVertices; v++) { 333f7320561SMichael Lange PetscBool found = PETSC_FALSE; 334f7320561SMichael Lange for (c = 0; c < numCellVertices; c++) { 335f7320561SMichael Lange if (cell[c] < 0) break; 3369371c9d4SSatish Balay if (cell[c] == face[v] - 1 + numCells) { 3379371c9d4SSatish Balay found = PETSC_TRUE; 3389371c9d4SSatish Balay break; 3399371c9d4SSatish Balay } 340f7320561SMichael Lange } 341f7320561SMichael Lange if (!found) cell[c] = face[v] - 1 + numCells; 342f7320561SMichael Lange } 343f7320561SMichael Lange } 344f7320561SMichael Lange } 345*48a46eb9SPierre Jolivet for (c = 0; c < numCells; c++) PetscCall(DMPlexSetCone(*dm, c, &(cellVertices[c * numCellVertices]))); 346f7320561SMichael Lange } 3479566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(*dm)); 3489566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(*dm)); 349f7320561SMichael Lange if (interpolate) { 3505fd9971aSMatthew G. Knepley DM idm; 351f7320561SMichael Lange 3529566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 3539566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 354f7320561SMichael Lange *dm = idm; 355f7320561SMichael Lange } 356f7320561SMichael Lange 357dd400576SPatrick Sanan if (rank == 0 && faces) { 358631eb916SMichael Lange PetscInt fi, joinSize, meetSize, *fverts, cells[2]; 359631eb916SMichael Lange const PetscInt *join, *meet; 3609566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numFaceVertices, &fverts)); 361ec78a56aSMichael Lange /* Mark facets by finding the full join of all adjacent vertices */ 362ec78a56aSMichael Lange for (f = 0; f < numFaces; f++) { 363631eb916SMichael Lange const PetscInt cl = faces[f * numFaceEntries + numFaceVertices] - 1; 364631eb916SMichael Lange const PetscInt cr = faces[f * numFaceEntries + numFaceVertices + 1] - 1; 365631eb916SMichael Lange if (cl > 0 && cr > 0) { 366631eb916SMichael Lange /* If we know both adjoining cells we can use a single-level meet */ 3679371c9d4SSatish Balay cells[0] = cl; 3689371c9d4SSatish Balay cells[1] = cr; 3699566063dSJacob Faibussowitsch PetscCall(DMPlexGetMeet(*dm, 2, cells, &meetSize, &meet)); 37063a3b9bcSJacob Faibussowitsch PetscCheck(meetSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not determine Plex facet for Fluent face %" PetscInt_FMT, f); 3719566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue_Fast(*dm, &faceSets, "Face Sets", meet[0], faceZoneIDs[f])); 3729566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreMeet(*dm, numFaceVertices, fverts, &meetSize, &meet)); 373631eb916SMichael Lange } else { 374ec78a56aSMichael Lange for (fi = 0; fi < numFaceVertices; fi++) fverts[fi] = faces[f * numFaceEntries + fi] + numCells - 1; 3759566063dSJacob Faibussowitsch PetscCall(DMPlexGetFullJoin(*dm, numFaceVertices, fverts, &joinSize, &join)); 37663a3b9bcSJacob Faibussowitsch PetscCheck(joinSize == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not determine Plex facet for Fluent face %" PetscInt_FMT, f); 3779566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue_Fast(*dm, &faceSets, "Face Sets", join[0], faceZoneIDs[f])); 3789566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(*dm, numFaceVertices, fverts, &joinSize, &join)); 379ec78a56aSMichael Lange } 380631eb916SMichael Lange } 3819566063dSJacob Faibussowitsch PetscCall(PetscFree(fverts)); 382ec78a56aSMichael Lange } 383ec78a56aSMichael Lange 3847368db69SLisandro Dalcin { /* Create Face Sets label at all processes */ 3859371c9d4SSatish Balay enum { 3869371c9d4SSatish Balay n = 1 3879371c9d4SSatish Balay }; 3887368db69SLisandro Dalcin PetscBool flag[n]; 3897368db69SLisandro Dalcin 3907368db69SLisandro Dalcin flag[0] = faceSets ? PETSC_TRUE : PETSC_FALSE; 3919566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(flag, n, MPIU_BOOL, 0, comm)); 3929566063dSJacob Faibussowitsch if (flag[0]) PetscCall(DMCreateLabel(*dm, "Face Sets")); 3937368db69SLisandro Dalcin } 3947368db69SLisandro Dalcin 3953f6dc66eSMichael Lange /* Read coordinates */ 3969566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(*dm, &coordSection)); 3979566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 3989566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 3999566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 4003f6dc66eSMichael Lange for (v = numCells; v < numCells + numVertices; ++v) { 4019566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dim)); 4029566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 4033f6dc66eSMichael Lange } 4049566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 4059566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 4069566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 4079566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 4089566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 4099566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 4109566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 411dd400576SPatrick Sanan if (rank == 0 && coordsIn) { 4123f6dc66eSMichael Lange for (v = 0; v < numVertices; ++v) { 4139371c9d4SSatish Balay for (d = 0; d < dim; ++d) { coords[v * dim + d] = coordsIn[v * dim + d]; } 4143f6dc66eSMichael Lange } 4153f6dc66eSMichael Lange } 4169566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 4179566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(*dm, coordinates)); 4189566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 4197368db69SLisandro Dalcin 420dd400576SPatrick Sanan if (rank == 0) { 4219566063dSJacob Faibussowitsch PetscCall(PetscFree(cellVertices)); 4229566063dSJacob Faibussowitsch PetscCall(PetscFree(faces)); 4239566063dSJacob Faibussowitsch PetscCall(PetscFree(faceZoneIDs)); 4249566063dSJacob Faibussowitsch PetscCall(PetscFree(coordsIn)); 425f7320561SMichael Lange } 4262f0bd6dcSMichael Lange PetscFunctionReturn(0); 4272f0bd6dcSMichael Lange } 428