1e1b39ce3SMatthew G. Knepley #define PETSCDM_DLL 2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 3e1b39ce3SMatthew G. Knepley 41b7ec9c1SMatthew G. Knepley #undef I /* Very old CGNS stupidly uses I as a variable, which fails when using complex. Curse you idiot package managers */ 5e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS) 6e1b39ce3SMatthew G. Knepley #include <cgnslib.h> 7e1b39ce3SMatthew G. Knepley #include <cgns_io.h> 8e1b39ce3SMatthew G. Knepley #endif 9c4cbe8f1SToby Isaac #if !defined(CGNS_ENUMT) 10c4cbe8f1SToby Isaac #define CGNS_ENUMT(a) a 11c4cbe8f1SToby Isaac #endif 12c4cbe8f1SToby Isaac #if !defined(CGNS_ENUMV) 13c4cbe8f1SToby Isaac #define CGNS_ENUMV(a) a 14c4cbe8f1SToby Isaac #endif 15e1b39ce3SMatthew G. Knepley 169566063dSJacob Faibussowitsch #define PetscCallCGNS(ierr) \ 17c261946bSLisandro Dalcin do { \ 18c261946bSLisandro Dalcin int _cgns_ier = (ierr); \ 1928b400f6SJacob Faibussowitsch PetscCheck(!_cgns_ier,PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS error %d %s",_cgns_ier,cg_get_error()); \ 20c261946bSLisandro Dalcin } while (0) 21c261946bSLisandro Dalcin 2244cd5272SMichael Lange /*@C 2344cd5272SMichael Lange DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file. 2444cd5272SMichael Lange 25d083f849SBarry Smith Collective 2644cd5272SMichael Lange 2744cd5272SMichael Lange Input Parameters: 2844cd5272SMichael Lange + comm - The MPI communicator 2944cd5272SMichael Lange . filename - The name of the CGNS file 3044cd5272SMichael Lange - interpolate - Create faces and edges in the mesh 3144cd5272SMichael Lange 3244cd5272SMichael Lange Output Parameter: 3344cd5272SMichael Lange . dm - The DM object representing the mesh 3444cd5272SMichael Lange 3544cd5272SMichael Lange Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html 3644cd5272SMichael Lange 3744cd5272SMichael Lange Level: beginner 3844cd5272SMichael Lange 39*db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMPlexCreateCGNS()`, `DMPlexCreateExodus()` 4044cd5272SMichael Lange @*/ 4144cd5272SMichael Lange PetscErrorCode DMPlexCreateCGNSFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm) 4244cd5272SMichael Lange { 4344cd5272SMichael Lange PetscMPIInt rank; 4444cd5272SMichael Lange #if defined(PETSC_HAVE_CGNS) 4544cd5272SMichael Lange int cgid = -1; 4644cd5272SMichael Lange #endif 4744cd5272SMichael Lange 4844cd5272SMichael Lange PetscFunctionBegin; 4944cd5272SMichael Lange PetscValidCharPointer(filename, 2); 509566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 5144cd5272SMichael Lange #if defined(PETSC_HAVE_CGNS) 52dd400576SPatrick Sanan if (rank == 0) { 539566063dSJacob Faibussowitsch PetscCallCGNS(cg_open(filename, CG_MODE_READ, &cgid)); 5408401ef6SPierre Jolivet PetscCheck(cgid > 0,PETSC_COMM_SELF, PETSC_ERR_LIB, "cg_open(\"%s\",...) did not return a valid file ID", filename); 5544cd5272SMichael Lange } 569566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCGNS(comm, cgid, interpolate, dm)); 579566063dSJacob Faibussowitsch if (rank == 0) PetscCallCGNS(cg_close(cgid)); 58b458e8f1SJose E. Roman PetscFunctionReturn(0); 5944cd5272SMichael Lange #else 6044cd5272SMichael Lange SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires CGNS support. Reconfigure using --with-cgns-dir"); 6144cd5272SMichael Lange #endif 6244cd5272SMichael Lange } 6344cd5272SMichael Lange 64e1b39ce3SMatthew G. Knepley /*@ 6544cd5272SMichael Lange DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file ID. 66e1b39ce3SMatthew G. Knepley 67d083f849SBarry Smith Collective 68e1b39ce3SMatthew G. Knepley 69e1b39ce3SMatthew G. Knepley Input Parameters: 70e1b39ce3SMatthew G. Knepley + comm - The MPI communicator 71e1b39ce3SMatthew G. Knepley . cgid - The CG id associated with a file and obtained using cg_open 72e1b39ce3SMatthew G. Knepley - interpolate - Create faces and edges in the mesh 73e1b39ce3SMatthew G. Knepley 74e1b39ce3SMatthew G. Knepley Output Parameter: 75e1b39ce3SMatthew G. Knepley . dm - The DM object representing the mesh 76e1b39ce3SMatthew G. Knepley 77e1b39ce3SMatthew G. Knepley Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html 78e1b39ce3SMatthew G. Knepley 79e1b39ce3SMatthew G. Knepley Level: beginner 80e1b39ce3SMatthew G. Knepley 81*db781477SPatrick Sanan .seealso: `DMPlexCreate()`, `DMPlexCreateExodus()` 82e1b39ce3SMatthew G. Knepley @*/ 83e1b39ce3SMatthew G. Knepley PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm) 84e1b39ce3SMatthew G. Knepley { 85e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS) 86e1b39ce3SMatthew G. Knepley PetscMPIInt num_proc, rank; 87c261946bSLisandro Dalcin DM cdm; 884e6cc61cSLisandro Dalcin DMLabel label; 89e1b39ce3SMatthew G. Knepley PetscSection coordSection; 90e1b39ce3SMatthew G. Knepley Vec coordinates; 91e1b39ce3SMatthew G. Knepley PetscScalar *coords; 92c261946bSLisandro Dalcin PetscInt *cellStart, *vertStart, v; 934e6cc61cSLisandro Dalcin PetscInt labelIdRange[2], labelId; 94e1b39ce3SMatthew G. Knepley /* Read from file */ 95e1b39ce3SMatthew G. Knepley char basename[CGIO_MAX_NAME_LENGTH+1]; 96e1b39ce3SMatthew G. Knepley char buffer[CGIO_MAX_NAME_LENGTH+1]; 97c261946bSLisandro Dalcin int dim = 0, physDim = 0, coordDim =0, numVertices = 0, numCells = 0; 98e1b39ce3SMatthew G. Knepley int nzones = 0; 99e1b39ce3SMatthew G. Knepley #endif 100e1b39ce3SMatthew G. Knepley 101e1b39ce3SMatthew G. Knepley PetscFunctionBegin; 102e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS) 1039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 1049566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &num_proc)); 1059566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 1069566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 107c261946bSLisandro Dalcin 108a5b23f4aSJose E. Roman /* Open CGNS II file and read basic information on rank 0, then broadcast to all processors */ 109dd400576SPatrick Sanan if (rank == 0) { 110e1b39ce3SMatthew G. Knepley int nbases, z; 111e1b39ce3SMatthew G. Knepley 1129566063dSJacob Faibussowitsch PetscCallCGNS(cg_nbases(cgid, &nbases)); 11308401ef6SPierre Jolivet PetscCheck(nbases <= 1,PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d",nbases); 1149566063dSJacob Faibussowitsch PetscCallCGNS(cg_base_read(cgid, 1, basename, &dim, &physDim)); 1159566063dSJacob Faibussowitsch PetscCallCGNS(cg_nzones(cgid, 1, &nzones)); 1169566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(nzones+1, &cellStart, nzones+1, &vertStart)); 117e1b39ce3SMatthew G. Knepley for (z = 1; z <= nzones; ++z) { 118e1b39ce3SMatthew G. Knepley cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */ 119e1b39ce3SMatthew G. Knepley 1209566063dSJacob Faibussowitsch PetscCallCGNS(cg_zone_read(cgid, 1, z, buffer, sizes)); 121e1b39ce3SMatthew G. Knepley numVertices += sizes[0]; 122e1b39ce3SMatthew G. Knepley numCells += sizes[1]; 123f8716870SMatthew G. Knepley cellStart[z] += sizes[1] + cellStart[z-1]; 124f8716870SMatthew G. Knepley vertStart[z] += sizes[0] + vertStart[z-1]; 125f8716870SMatthew G. Knepley } 126f8716870SMatthew G. Knepley for (z = 1; z <= nzones; ++z) { 127f8716870SMatthew G. Knepley vertStart[z] += numCells; 128e1b39ce3SMatthew G. Knepley } 129c261946bSLisandro Dalcin coordDim = dim; 130e1b39ce3SMatthew G. Knepley } 1319566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm)); 1329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm)); 1339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&coordDim, 1, MPI_INT, 0, comm)); 1349566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(&nzones, 1, MPI_INT, 0, comm)); 135c261946bSLisandro Dalcin 1369566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) *dm, basename)); 1379566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*dm, dim)); 1389566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(*dm, "celltype")); 1399566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(*dm, 0, numCells+numVertices)); 140e1b39ce3SMatthew G. Knepley 141e1b39ce3SMatthew G. Knepley /* Read zone information */ 142dd400576SPatrick Sanan if (rank == 0) { 143c261946bSLisandro Dalcin int z, c, c_loc; 144e1b39ce3SMatthew G. Knepley 145e1b39ce3SMatthew G. Knepley /* Read the cell set connectivity table and build mesh topology 146e1b39ce3SMatthew G. Knepley CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */ 147e1b39ce3SMatthew G. Knepley /* First set sizes */ 148e1b39ce3SMatthew G. Knepley for (z = 1, c = 0; z <= nzones; ++z) { 149c4cbe8f1SToby Isaac CGNS_ENUMT(ZoneType_t) zonetype; 150e1b39ce3SMatthew G. Knepley int nsections; 151c4cbe8f1SToby Isaac CGNS_ENUMT(ElementType_t) cellType; 152e1b39ce3SMatthew G. Knepley cgsize_t start, end; 153e1b39ce3SMatthew G. Knepley int nbndry, parentFlag; 154e1b39ce3SMatthew G. Knepley PetscInt numCorners; 155c261946bSLisandro Dalcin DMPolytopeType ctype; 156e1b39ce3SMatthew G. Knepley 1579566063dSJacob Faibussowitsch PetscCallCGNS(cg_zone_type(cgid, 1, z, &zonetype)); 15808401ef6SPierre Jolivet PetscCheck(zonetype != CGNS_ENUMV(Structured),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS"); 1599566063dSJacob Faibussowitsch PetscCallCGNS(cg_nsections(cgid, 1, z, &nsections)); 16008401ef6SPierre Jolivet PetscCheck(nsections <= 1,PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d",nsections); 1619566063dSJacob Faibussowitsch PetscCallCGNS(cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag)); 162e1b39ce3SMatthew G. Knepley /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */ 163c4cbe8f1SToby Isaac if (cellType == CGNS_ENUMV(MIXED)) { 164e1b39ce3SMatthew G. Knepley cgsize_t elementDataSize, *elements; 165e1b39ce3SMatthew G. Knepley PetscInt off; 166e1b39ce3SMatthew G. Knepley 1679566063dSJacob Faibussowitsch PetscCallCGNS(cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize)); 1689566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(elementDataSize, &elements)); 1699566063dSJacob Faibussowitsch PetscCallCGNS(cg_poly_elements_read(cgid, 1, z, 1, elements, NULL, NULL)); 17059dfc837SStefan Lemvig Glimberg for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) { 1714c9a562dSMatthew G. Knepley switch (elements[off]) { 172c261946bSLisandro Dalcin case CGNS_ENUMV(BAR_2): numCorners = 2; ctype = DM_POLYTOPE_SEGMENT; break; 173c261946bSLisandro Dalcin case CGNS_ENUMV(TRI_3): numCorners = 3; ctype = DM_POLYTOPE_TRIANGLE; break; 174c261946bSLisandro Dalcin case CGNS_ENUMV(QUAD_4): numCorners = 4; ctype = DM_POLYTOPE_QUADRILATERAL; break; 175c261946bSLisandro Dalcin case CGNS_ENUMV(TETRA_4): numCorners = 4; ctype = DM_POLYTOPE_TETRAHEDRON; break; 176c261946bSLisandro Dalcin case CGNS_ENUMV(PENTA_6): numCorners = 6; ctype = DM_POLYTOPE_TRI_PRISM; break; 177c261946bSLisandro Dalcin case CGNS_ENUMV(HEXA_8): numCorners = 8; ctype = DM_POLYTOPE_HEXAHEDRON; break; 17898921bdaSJacob Faibussowitsch default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]); 179e1b39ce3SMatthew G. Knepley } 1809566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(*dm, c, numCorners)); 1819566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(*dm, c, ctype)); 182e1b39ce3SMatthew G. Knepley off += numCorners+1; 183e1b39ce3SMatthew G. Knepley } 1849566063dSJacob Faibussowitsch PetscCall(PetscFree(elements)); 185e1b39ce3SMatthew G. Knepley } else { 186e1b39ce3SMatthew G. Knepley switch (cellType) { 187c261946bSLisandro Dalcin case CGNS_ENUMV(BAR_2): numCorners = 2; ctype = DM_POLYTOPE_SEGMENT; break; 188c261946bSLisandro Dalcin case CGNS_ENUMV(TRI_3): numCorners = 3; ctype = DM_POLYTOPE_TRIANGLE; break; 189c261946bSLisandro Dalcin case CGNS_ENUMV(QUAD_4): numCorners = 4; ctype = DM_POLYTOPE_QUADRILATERAL; break; 190c261946bSLisandro Dalcin case CGNS_ENUMV(TETRA_4): numCorners = 4; ctype = DM_POLYTOPE_TETRAHEDRON; break; 191c261946bSLisandro Dalcin case CGNS_ENUMV(PENTA_6): numCorners = 6; ctype = DM_POLYTOPE_TRI_PRISM; break; 192c261946bSLisandro Dalcin case CGNS_ENUMV(HEXA_8): numCorners = 8; ctype = DM_POLYTOPE_HEXAHEDRON; break; 19398921bdaSJacob Faibussowitsch default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType); 194e1b39ce3SMatthew G. Knepley } 19559dfc837SStefan Lemvig Glimberg for (c_loc = start; c_loc <= end; ++c_loc, ++c) { 1969566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(*dm, c, numCorners)); 1979566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(*dm, c, ctype)); 198e1b39ce3SMatthew G. Knepley } 199e1b39ce3SMatthew G. Knepley } 200e1b39ce3SMatthew G. Knepley } 201c261946bSLisandro Dalcin for (v = numCells; v < numCells+numVertices; ++v) { 2029566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(*dm, v, DM_POLYTOPE_POINT)); 203c261946bSLisandro Dalcin } 204c261946bSLisandro Dalcin } 205c261946bSLisandro Dalcin 2069566063dSJacob Faibussowitsch PetscCall(DMSetUp(*dm)); 207c261946bSLisandro Dalcin 2089566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(*dm, "zone")); 209dd400576SPatrick Sanan if (rank == 0) { 210c261946bSLisandro Dalcin int z, c, c_loc, v_loc; 211c261946bSLisandro Dalcin 2129566063dSJacob Faibussowitsch PetscCall(DMGetLabel(*dm, "zone", &label)); 213e1b39ce3SMatthew G. Knepley for (z = 1, c = 0; z <= nzones; ++z) { 214c4cbe8f1SToby Isaac CGNS_ENUMT(ElementType_t) cellType; 215c261946bSLisandro Dalcin cgsize_t elementDataSize, *elements, start, end; 216e1b39ce3SMatthew G. Knepley int nbndry, parentFlag; 217e1b39ce3SMatthew G. Knepley PetscInt *cone, numc, numCorners, maxCorners = 27; 218e1b39ce3SMatthew G. Knepley 2199566063dSJacob Faibussowitsch PetscCallCGNS(cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag)); 220e1b39ce3SMatthew G. Knepley numc = end - start; 221e1b39ce3SMatthew G. Knepley /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */ 2229566063dSJacob Faibussowitsch PetscCallCGNS(cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize)); 2239566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(elementDataSize,&elements,maxCorners,&cone)); 2249566063dSJacob Faibussowitsch PetscCallCGNS(cg_poly_elements_read(cgid, 1, z, 1, elements, NULL, NULL)); 225c4cbe8f1SToby Isaac if (cellType == CGNS_ENUMV(MIXED)) { 226eaf898f9SPatrick Sanan /* CGNS uses Fortran-based indexing, DMPlex uses C-style and numbers cell first then vertices. */ 22759dfc837SStefan Lemvig Glimberg for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) { 2284c9a562dSMatthew G. Knepley switch (elements[v]) { 229c261946bSLisandro Dalcin case CGNS_ENUMV(BAR_2): numCorners = 2; break; 230c4cbe8f1SToby Isaac case CGNS_ENUMV(TRI_3): numCorners = 3; break; 231c4cbe8f1SToby Isaac case CGNS_ENUMV(QUAD_4): numCorners = 4; break; 232c4cbe8f1SToby Isaac case CGNS_ENUMV(TETRA_4): numCorners = 4; break; 233c261946bSLisandro Dalcin case CGNS_ENUMV(PENTA_6): numCorners = 6; break; 234c4cbe8f1SToby Isaac case CGNS_ENUMV(HEXA_8): numCorners = 8; break; 23598921bdaSJacob Faibussowitsch default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]); 236e1b39ce3SMatthew G. Knepley } 2374c9a562dSMatthew G. Knepley ++v; 238e1b39ce3SMatthew G. Knepley for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) { 239e1b39ce3SMatthew G. Knepley cone[v_loc] = elements[v]+numCells-1; 240e1b39ce3SMatthew G. Knepley } 2419566063dSJacob Faibussowitsch PetscCall(DMPlexReorderCell(*dm, c, cone)); 2429566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(*dm, c, cone)); 2439566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, c, z)); 244e1b39ce3SMatthew G. Knepley } 245e1b39ce3SMatthew G. Knepley } else { 246e1b39ce3SMatthew G. Knepley switch (cellType) { 247c261946bSLisandro Dalcin case CGNS_ENUMV(BAR_2): numCorners = 2; break; 248c4cbe8f1SToby Isaac case CGNS_ENUMV(TRI_3): numCorners = 3; break; 249c4cbe8f1SToby Isaac case CGNS_ENUMV(QUAD_4): numCorners = 4; break; 250c4cbe8f1SToby Isaac case CGNS_ENUMV(TETRA_4): numCorners = 4; break; 251c261946bSLisandro Dalcin case CGNS_ENUMV(PENTA_6): numCorners = 6; break; 252c4cbe8f1SToby Isaac case CGNS_ENUMV(HEXA_8): numCorners = 8; break; 25398921bdaSJacob Faibussowitsch default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType); 254e1b39ce3SMatthew G. Knepley } 255eaf898f9SPatrick Sanan /* CGNS uses Fortran-based indexing, DMPlex uses C-style and numbers cell first then vertices. */ 25659dfc837SStefan Lemvig Glimberg for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) { 257e1b39ce3SMatthew G. Knepley for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) { 258e1b39ce3SMatthew G. Knepley cone[v_loc] = elements[v]+numCells-1; 259e1b39ce3SMatthew G. Knepley } 2609566063dSJacob Faibussowitsch PetscCall(DMPlexReorderCell(*dm, c, cone)); 2619566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(*dm, c, cone)); 2629566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, c, z)); 263e1b39ce3SMatthew G. Knepley } 264e1b39ce3SMatthew G. Knepley } 2659566063dSJacob Faibussowitsch PetscCall(PetscFree2(elements,cone)); 266e1b39ce3SMatthew G. Knepley } 267e1b39ce3SMatthew G. Knepley } 268c261946bSLisandro Dalcin 2699566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(*dm)); 2709566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(*dm)); 271e1b39ce3SMatthew G. Knepley if (interpolate) { 2725fd9971aSMatthew G. Knepley DM idm; 273e1b39ce3SMatthew G. Knepley 2749566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 2759566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 276e1b39ce3SMatthew G. Knepley *dm = idm; 277e1b39ce3SMatthew G. Knepley } 278e1b39ce3SMatthew G. Knepley 279e1b39ce3SMatthew G. Knepley /* Read coordinates */ 2809566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(*dm, coordDim)); 2819566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(*dm, &cdm)); 2829566063dSJacob Faibussowitsch PetscCall(DMGetLocalSection(cdm, &coordSection)); 2839566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 2849566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, coordDim)); 2859566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 286e1b39ce3SMatthew G. Knepley for (v = numCells; v < numCells+numVertices; ++v) { 2879566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dim)); 2889566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, coordDim)); 289e1b39ce3SMatthew G. Knepley } 2909566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 291c261946bSLisandro Dalcin 2929566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(cdm, &coordinates)); 2939566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 294dd400576SPatrick Sanan if (rank == 0) { 295e1b39ce3SMatthew G. Knepley PetscInt off = 0; 296e1b39ce3SMatthew G. Knepley float *x[3]; 29759dfc837SStefan Lemvig Glimberg int z, d; 298e1b39ce3SMatthew G. Knepley 2999566063dSJacob Faibussowitsch PetscCall(PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2])); 30059dfc837SStefan Lemvig Glimberg for (z = 1; z <= nzones; ++z) { 301c4cbe8f1SToby Isaac CGNS_ENUMT(DataType_t) datatype; 302e1b39ce3SMatthew G. Knepley cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */ 303e1b39ce3SMatthew G. Knepley cgsize_t range_min[3] = {1, 1, 1}; 304e1b39ce3SMatthew G. Knepley cgsize_t range_max[3] = {1, 1, 1}; 305e1b39ce3SMatthew G. Knepley int ngrids, ncoords; 306e1b39ce3SMatthew G. Knepley 3079566063dSJacob Faibussowitsch PetscCallCGNS(cg_zone_read(cgid, 1, z, buffer, sizes)); 308e1b39ce3SMatthew G. Knepley range_max[0] = sizes[0]; 3099566063dSJacob Faibussowitsch PetscCallCGNS(cg_ngrids(cgid, 1, z, &ngrids)); 31008401ef6SPierre Jolivet PetscCheck(ngrids <= 1,PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d",ngrids); 3119566063dSJacob Faibussowitsch PetscCallCGNS(cg_ncoords(cgid, 1, z, &ncoords)); 31208401ef6SPierre Jolivet PetscCheck(ncoords == coordDim,PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a coordinate array for each dimension, not %d",ncoords); 313c261946bSLisandro Dalcin for (d = 0; d < coordDim; ++d) { 3149566063dSJacob Faibussowitsch PetscCallCGNS(cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer)); 3159566063dSJacob Faibussowitsch PetscCallCGNS(cg_coord_read(cgid, 1, z, buffer, CGNS_ENUMV(RealSingle), range_min, range_max, x[d])); 316e1b39ce3SMatthew G. Knepley } 317c261946bSLisandro Dalcin if (coordDim >= 1) { 318c261946bSLisandro Dalcin for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+0] = x[0][v]; 319e1b39ce3SMatthew G. Knepley } 320c261946bSLisandro Dalcin if (coordDim >= 2) { 321c261946bSLisandro Dalcin for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+1] = x[1][v]; 322e1b39ce3SMatthew G. Knepley } 323c261946bSLisandro Dalcin if (coordDim >= 3) { 324c261946bSLisandro Dalcin for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+2] = x[2][v]; 325e1b39ce3SMatthew G. Knepley } 326e1b39ce3SMatthew G. Knepley off += sizes[0]; 327e1b39ce3SMatthew G. Knepley } 3289566063dSJacob Faibussowitsch PetscCall(PetscFree3(x[0],x[1],x[2])); 329e1b39ce3SMatthew G. Knepley } 3309566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 331c261946bSLisandro Dalcin 3329566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates")); 3339566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, coordDim)); 3349566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(*dm, coordinates)); 3359566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 336c261946bSLisandro Dalcin 337f8716870SMatthew G. Knepley /* Read boundary conditions */ 3389566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(*dm, &labelIdRange[0])); 339dd400576SPatrick Sanan if (rank == 0) { 340c4cbe8f1SToby Isaac CGNS_ENUMT(BCType_t) bctype; 341c4cbe8f1SToby Isaac CGNS_ENUMT(DataType_t) datatype; 342c4cbe8f1SToby Isaac CGNS_ENUMT(PointSetType_t) pointtype; 343f8716870SMatthew G. Knepley cgsize_t *points; 344f8716870SMatthew G. Knepley PetscReal *normals; 345f8716870SMatthew G. Knepley int normal[3]; 346f8716870SMatthew G. Knepley char *bcname = buffer; 347f8716870SMatthew G. Knepley cgsize_t npoints, nnormals; 348f8716870SMatthew G. Knepley int z, nbc, bc, c, ndatasets; 349f8716870SMatthew G. Knepley 350f8716870SMatthew G. Knepley for (z = 1; z <= nzones; ++z) { 3519566063dSJacob Faibussowitsch PetscCallCGNS(cg_nbocos(cgid, 1, z, &nbc)); 352f8716870SMatthew G. Knepley for (bc = 1; bc <= nbc; ++bc) { 3539566063dSJacob Faibussowitsch PetscCallCGNS(cg_boco_info(cgid, 1, z, bc, bcname, &bctype, &pointtype, &npoints, normal, &nnormals, &datatype, &ndatasets)); 3549566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(*dm, bcname)); 3559566063dSJacob Faibussowitsch PetscCall(DMGetLabel(*dm, bcname, &label)); 3569566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(npoints, &points, nnormals, &normals)); 3579566063dSJacob Faibussowitsch PetscCallCGNS(cg_boco_read(cgid, 1, z, bc, points, (void *) normals)); 358c4cbe8f1SToby Isaac if (pointtype == CGNS_ENUMV(ElementRange)) { 359f8716870SMatthew G. Knepley /* Range of cells: assuming half-open interval since the documentation sucks */ 360f8716870SMatthew G. Knepley for (c = points[0]; c < points[1]; ++c) { 3619566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, c - cellStart[z-1], 1)); 362f8716870SMatthew G. Knepley } 363c4cbe8f1SToby Isaac } else if (pointtype == CGNS_ENUMV(ElementList)) { 364f8716870SMatthew G. Knepley /* List of cells */ 365f8716870SMatthew G. Knepley for (c = 0; c < npoints; ++c) { 3669566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, points[c] - cellStart[z-1], 1)); 367f8716870SMatthew G. Knepley } 368c4cbe8f1SToby Isaac } else if (pointtype == CGNS_ENUMV(PointRange)) { 369c4cbe8f1SToby Isaac CGNS_ENUMT(GridLocation_t) gridloc; 370f8716870SMatthew G. Knepley 371f8716870SMatthew G. Knepley /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */ 3729566063dSJacob Faibussowitsch PetscCallCGNS(cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end")); 3739566063dSJacob Faibussowitsch PetscCallCGNS(cg_gridlocation_read(&gridloc)); 374f8716870SMatthew G. Knepley /* Range of points: assuming half-open interval since the documentation sucks */ 375f8716870SMatthew G. Knepley for (c = points[0]; c < points[1]; ++c) { 3769566063dSJacob Faibussowitsch if (gridloc == CGNS_ENUMV(Vertex)) PetscCall(DMLabelSetValue(label, c - vertStart[z-1], 1)); 3779566063dSJacob Faibussowitsch else PetscCall(DMLabelSetValue(label, c - cellStart[z-1], 1)); 378f8716870SMatthew G. Knepley } 379c4cbe8f1SToby Isaac } else if (pointtype == CGNS_ENUMV(PointList)) { 380c4cbe8f1SToby Isaac CGNS_ENUMT(GridLocation_t) gridloc; 381f8716870SMatthew G. Knepley 382f8716870SMatthew G. Knepley /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */ 3839566063dSJacob Faibussowitsch PetscCallCGNS(cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end")); 3849566063dSJacob Faibussowitsch PetscCallCGNS(cg_gridlocation_read(&gridloc)); 385f8716870SMatthew G. Knepley for (c = 0; c < npoints; ++c) { 3869566063dSJacob Faibussowitsch if (gridloc == CGNS_ENUMV(Vertex)) PetscCall(DMLabelSetValue(label, points[c] - vertStart[z-1], 1)); 3879566063dSJacob Faibussowitsch else PetscCall(DMLabelSetValue(label, points[c] - cellStart[z-1], 1)); 388f8716870SMatthew G. Knepley } 38998921bdaSJacob Faibussowitsch } else SETERRQ(comm, PETSC_ERR_SUP, "Unsupported point set type %d", (int) pointtype); 3909566063dSJacob Faibussowitsch PetscCall(PetscFree2(points, normals)); 391f8716870SMatthew G. Knepley } 392f8716870SMatthew G. Knepley } 3939566063dSJacob Faibussowitsch PetscCall(PetscFree2(cellStart, vertStart)); 394f8716870SMatthew G. Knepley } 3959566063dSJacob Faibussowitsch PetscCall(DMGetNumLabels(*dm, &labelIdRange[1])); 3969566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(labelIdRange, 2, MPIU_INT, 0, comm)); 3974e6cc61cSLisandro Dalcin 3984e6cc61cSLisandro Dalcin /* Create BC labels at all processes */ 3994e6cc61cSLisandro Dalcin for (labelId = labelIdRange[0]; labelId < labelIdRange[1]; ++labelId) { 4004e6cc61cSLisandro Dalcin char *labelName = buffer; 4014e6cc61cSLisandro Dalcin size_t len = sizeof(buffer); 4024e6cc61cSLisandro Dalcin const char *locName; 4034e6cc61cSLisandro Dalcin 404dd400576SPatrick Sanan if (rank == 0) { 4059566063dSJacob Faibussowitsch PetscCall(DMGetLabelByNum(*dm, labelId, &label)); 4069566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)label, &locName)); 4079566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(labelName, locName, len)); 4084e6cc61cSLisandro Dalcin } 4099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Bcast(labelName, (PetscMPIInt)len, MPIU_INT, 0, comm)); 4109566063dSJacob Faibussowitsch PetscCallMPI(DMCreateLabel(*dm, labelName)); 4114e6cc61cSLisandro Dalcin } 412b458e8f1SJose E. Roman PetscFunctionReturn(0); 413e1b39ce3SMatthew G. Knepley #else 414e1b39ce3SMatthew G. Knepley SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir"); 415e1b39ce3SMatthew G. Knepley #endif 416e1b39ce3SMatthew G. Knepley } 417