Actual source code: plexcgns.c

      1: #define PETSCDM_DLL
      2: #include <petsc/private/dmpleximpl.h>

      4: #undef I /* Very old CGNS stupidly uses I as a variable, which fails when using complex. Curse you idiot package managers */
      5: #if defined(PETSC_HAVE_CGNS)
      6: #include <cgnslib.h>
      7: #include <cgns_io.h>
      8: #endif
      9: #if !defined(CGNS_ENUMT)
     10: #define CGNS_ENUMT(a) a
     11: #endif
     12: #if !defined(CGNS_ENUMV)
     13: #define CGNS_ENUMV(a) a
     14: #endif

     16: #define PetscCallCGNS(ierr) \
     17: do { \
     18:   int _cgns_ier = (ierr); \
     20: } while (0)

     22: /*@C
     23:   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file.

     25:   Collective

     27:   Input Parameters:
     28: + comm  - The MPI communicator
     29: . filename - The name of the CGNS file
     30: - interpolate - Create faces and edges in the mesh

     32:   Output Parameter:
     33: . dm  - The DM object representing the mesh

     35:   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html

     37:   Level: beginner

     39: .seealso: DMPlexCreate(), DMPlexCreateCGNS(), DMPlexCreateExodus()
     40: @*/
     41: PetscErrorCode DMPlexCreateCGNSFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
     42: {
     43:   PetscMPIInt    rank;
     44: #if defined(PETSC_HAVE_CGNS)
     45:   int cgid = -1;
     46: #endif

     49:   MPI_Comm_rank(comm, &rank);
     50: #if defined(PETSC_HAVE_CGNS)
     51:   if (rank == 0) {
     52:     cg_open(filename, CG_MODE_READ, &cgid);
     54:   }
     55:   DMPlexCreateCGNS(comm, cgid, interpolate, dm);
     56:   if (rank == 0) cg_close(cgid);
     57:   return 0;
     58: #else
     59:   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires CGNS support. Reconfigure using --with-cgns-dir");
     60: #endif
     61: }

     63: /*@
     64:   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file ID.

     66:   Collective

     68:   Input Parameters:
     69: + comm  - The MPI communicator
     70: . cgid - The CG id associated with a file and obtained using cg_open
     71: - interpolate - Create faces and edges in the mesh

     73:   Output Parameter:
     74: . dm  - The DM object representing the mesh

     76:   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html

     78:   Level: beginner

     80: .seealso: DMPlexCreate(), DMPlexCreateExodus()
     81: @*/
     82: PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm)
     83: {
     84: #if defined(PETSC_HAVE_CGNS)
     85:   PetscMPIInt    num_proc, rank;
     86:   DM             cdm;
     87:   DMLabel        label;
     88:   PetscSection   coordSection;
     89:   Vec            coordinates;
     90:   PetscScalar   *coords;
     91:   PetscInt      *cellStart, *vertStart, v;
     92:   PetscInt       labelIdRange[2], labelId;
     93:   /* Read from file */
     94:   char basename[CGIO_MAX_NAME_LENGTH+1];
     95:   char buffer[CGIO_MAX_NAME_LENGTH+1];
     96:   int  dim    = 0, physDim = 0, coordDim =0, numVertices = 0, numCells = 0;
     97:   int  nzones = 0;
     98: #endif

    100: #if defined(PETSC_HAVE_CGNS)
    101:   MPI_Comm_rank(comm, &rank);
    102:   MPI_Comm_size(comm, &num_proc);
    103:   DMCreate(comm, dm);
    104:   DMSetType(*dm, DMPLEX);

    106:   /* Open CGNS II file and read basic information on rank 0, then broadcast to all processors */
    107:   if (rank == 0) {
    108:     int nbases, z;

    110:     cg_nbases(cgid, &nbases);
    112:     cg_base_read(cgid, 1, basename, &dim, &physDim);
    113:     cg_nzones(cgid, 1, &nzones);
    114:     PetscCalloc2(nzones+1, &cellStart, nzones+1, &vertStart);
    115:     for (z = 1; z <= nzones; ++z) {
    116:       cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */

    118:       cg_zone_read(cgid, 1, z, buffer, sizes);
    119:       numVertices += sizes[0];
    120:       numCells    += sizes[1];
    121:       cellStart[z] += sizes[1] + cellStart[z-1];
    122:       vertStart[z] += sizes[0] + vertStart[z-1];
    123:     }
    124:     for (z = 1; z <= nzones; ++z) {
    125:       vertStart[z] += numCells;
    126:     }
    127:     coordDim = dim;
    128:   }
    129:   MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);
    130:   MPI_Bcast(&dim, 1, MPI_INT, 0, comm);
    131:   MPI_Bcast(&coordDim, 1, MPI_INT, 0, comm);
    132:   MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);

    134:   PetscObjectSetName((PetscObject) *dm, basename);
    135:   DMSetDimension(*dm, dim);
    136:   DMCreateLabel(*dm, "celltype");
    137:   DMPlexSetChart(*dm, 0, numCells+numVertices);

    139:   /* Read zone information */
    140:   if (rank == 0) {
    141:     int z, c, c_loc;

    143:     /* Read the cell set connectivity table and build mesh topology
    144:        CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */
    145:     /* First set sizes */
    146:     for (z = 1, c = 0; z <= nzones; ++z) {
    147:       CGNS_ENUMT(ZoneType_t)    zonetype;
    148:       int                       nsections;
    149:       CGNS_ENUMT(ElementType_t) cellType;
    150:       cgsize_t                  start, end;
    151:       int                       nbndry, parentFlag;
    152:       PetscInt                  numCorners;
    153:       DMPolytopeType            ctype;

    155:       cg_zone_type(cgid, 1, z, &zonetype);
    157:       cg_nsections(cgid, 1, z, &nsections);
    159:       cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);
    160:       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
    161:       if (cellType == CGNS_ENUMV(MIXED)) {
    162:         cgsize_t elementDataSize, *elements;
    163:         PetscInt off;

    165:         cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);
    166:         PetscMalloc1(elementDataSize, &elements);
    167:         cg_poly_elements_read(cgid, 1, z, 1, elements, NULL, NULL);
    168:         for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) {
    169:           switch (elements[off]) {
    170:           case CGNS_ENUMV(BAR_2):   numCorners = 2; ctype = DM_POLYTOPE_SEGMENT;       break;
    171:           case CGNS_ENUMV(TRI_3):   numCorners = 3; ctype = DM_POLYTOPE_TRIANGLE;      break;
    172:           case CGNS_ENUMV(QUAD_4):  numCorners = 4; ctype = DM_POLYTOPE_QUADRILATERAL; break;
    173:           case CGNS_ENUMV(TETRA_4): numCorners = 4; ctype = DM_POLYTOPE_TETRAHEDRON;   break;
    174:           case CGNS_ENUMV(PENTA_6): numCorners = 6; ctype = DM_POLYTOPE_TRI_PRISM;     break;
    175:           case CGNS_ENUMV(HEXA_8):  numCorners = 8; ctype = DM_POLYTOPE_HEXAHEDRON;    break;
    176:           default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]);
    177:           }
    178:           DMPlexSetConeSize(*dm, c, numCorners);
    179:           DMPlexSetCellType(*dm, c, ctype);
    180:           off += numCorners+1;
    181:         }
    182:         PetscFree(elements);
    183:       } else {
    184:         switch (cellType) {
    185:         case CGNS_ENUMV(BAR_2):   numCorners = 2; ctype = DM_POLYTOPE_SEGMENT;       break;
    186:         case CGNS_ENUMV(TRI_3):   numCorners = 3; ctype = DM_POLYTOPE_TRIANGLE;      break;
    187:         case CGNS_ENUMV(QUAD_4):  numCorners = 4; ctype = DM_POLYTOPE_QUADRILATERAL; break;
    188:         case CGNS_ENUMV(TETRA_4): numCorners = 4; ctype = DM_POLYTOPE_TETRAHEDRON;   break;
    189:         case CGNS_ENUMV(PENTA_6): numCorners = 6; ctype = DM_POLYTOPE_TRI_PRISM;     break;
    190:         case CGNS_ENUMV(HEXA_8):  numCorners = 8; ctype = DM_POLYTOPE_HEXAHEDRON;    break;
    191:         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
    192:         }
    193:         for (c_loc = start; c_loc <= end; ++c_loc, ++c) {
    194:           DMPlexSetConeSize(*dm, c, numCorners);
    195:           DMPlexSetCellType(*dm, c, ctype);
    196:         }
    197:       }
    198:     }
    199:     for (v = numCells; v < numCells+numVertices; ++v) {
    200:       DMPlexSetCellType(*dm, v, DM_POLYTOPE_POINT);
    201:     }
    202:   }

    204:   DMSetUp(*dm);

    206:   DMCreateLabel(*dm, "zone");
    207:   if (rank == 0) {
    208:     int z, c, c_loc, v_loc;

    210:     DMGetLabel(*dm, "zone", &label);
    211:     for (z = 1, c = 0; z <= nzones; ++z) {
    212:       CGNS_ENUMT(ElementType_t)   cellType;
    213:       cgsize_t                    elementDataSize, *elements, start, end;
    214:       int                          nbndry, parentFlag;
    215:       PetscInt                    *cone, numc, numCorners, maxCorners = 27;

    217:       cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);
    218:       numc = end - start;
    219:       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
    220:       cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);
    221:       PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);
    222:       cg_poly_elements_read(cgid, 1, z, 1, elements, NULL, NULL);
    223:       if (cellType == CGNS_ENUMV(MIXED)) {
    224:         /* CGNS uses Fortran-based indexing, DMPlex uses C-style and numbers cell first then vertices. */
    225:         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
    226:           switch (elements[v]) {
    227:           case CGNS_ENUMV(BAR_2):   numCorners = 2; break;
    228:           case CGNS_ENUMV(TRI_3):   numCorners = 3; break;
    229:           case CGNS_ENUMV(QUAD_4):  numCorners = 4; break;
    230:           case CGNS_ENUMV(TETRA_4): numCorners = 4; break;
    231:           case CGNS_ENUMV(PENTA_6): numCorners = 6; break;
    232:           case CGNS_ENUMV(HEXA_8):  numCorners = 8; break;
    233:           default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]);
    234:           }
    235:           ++v;
    236:           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
    237:             cone[v_loc] = elements[v]+numCells-1;
    238:           }
    239:           DMPlexReorderCell(*dm, c, cone);
    240:           DMPlexSetCone(*dm, c, cone);
    241:           DMLabelSetValue(label, c, z);
    242:         }
    243:       } else {
    244:         switch (cellType) {
    245:         case CGNS_ENUMV(BAR_2):   numCorners = 2; break;
    246:         case CGNS_ENUMV(TRI_3):   numCorners = 3; break;
    247:         case CGNS_ENUMV(QUAD_4):  numCorners = 4; break;
    248:         case CGNS_ENUMV(TETRA_4): numCorners = 4; break;
    249:         case CGNS_ENUMV(PENTA_6): numCorners = 6; break;
    250:         case CGNS_ENUMV(HEXA_8):  numCorners = 8; break;
    251:         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
    252:         }
    253:         /* CGNS uses Fortran-based indexing, DMPlex uses C-style and numbers cell first then vertices. */
    254:         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
    255:           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
    256:             cone[v_loc] = elements[v]+numCells-1;
    257:           }
    258:           DMPlexReorderCell(*dm, c, cone);
    259:           DMPlexSetCone(*dm, c, cone);
    260:           DMLabelSetValue(label, c, z);
    261:         }
    262:       }
    263:       PetscFree2(elements,cone);
    264:     }
    265:   }

    267:   DMPlexSymmetrize(*dm);
    268:   DMPlexStratify(*dm);
    269:   if (interpolate) {
    270:     DM idm;

    272:     DMPlexInterpolate(*dm, &idm);
    273:     DMDestroy(dm);
    274:     *dm  = idm;
    275:   }

    277:   /* Read coordinates */
    278:   DMSetCoordinateDim(*dm, coordDim);
    279:   DMGetCoordinateDM(*dm, &cdm);
    280:   DMGetLocalSection(cdm, &coordSection);
    281:   PetscSectionSetNumFields(coordSection, 1);
    282:   PetscSectionSetFieldComponents(coordSection, 0, coordDim);
    283:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
    284:   for (v = numCells; v < numCells+numVertices; ++v) {
    285:     PetscSectionSetDof(coordSection, v, dim);
    286:     PetscSectionSetFieldDof(coordSection, v, 0, coordDim);
    287:   }
    288:   PetscSectionSetUp(coordSection);

    290:   DMCreateLocalVector(cdm, &coordinates);
    291:   VecGetArray(coordinates, &coords);
    292:   if (rank == 0) {
    293:     PetscInt off = 0;
    294:     float   *x[3];
    295:     int      z, d;

    297:     PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);
    298:     for (z = 1; z <= nzones; ++z) {
    299:       CGNS_ENUMT(DataType_t) datatype;
    300:       cgsize_t               sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
    301:       cgsize_t               range_min[3] = {1, 1, 1};
    302:       cgsize_t               range_max[3] = {1, 1, 1};
    303:       int                    ngrids, ncoords;

    305:       cg_zone_read(cgid, 1, z, buffer, sizes);
    306:       range_max[0] = sizes[0];
    307:       cg_ngrids(cgid, 1, z, &ngrids);
    309:       cg_ncoords(cgid, 1, z, &ncoords);
    311:       for (d = 0; d < coordDim; ++d) {
    312:         cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer);
    313:         cg_coord_read(cgid, 1, z, buffer, CGNS_ENUMV(RealSingle), range_min, range_max, x[d]);
    314:       }
    315:       if (coordDim >= 1) {
    316:         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+0] = x[0][v];
    317:       }
    318:       if (coordDim >= 2) {
    319:         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+1] = x[1][v];
    320:       }
    321:       if (coordDim >= 3) {
    322:         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*coordDim+2] = x[2][v];
    323:       }
    324:       off += sizes[0];
    325:     }
    326:     PetscFree3(x[0],x[1],x[2]);
    327:   }
    328:   VecRestoreArray(coordinates, &coords);

    330:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
    331:   VecSetBlockSize(coordinates, coordDim);
    332:   DMSetCoordinatesLocal(*dm, coordinates);
    333:   VecDestroy(&coordinates);

    335:   /* Read boundary conditions */
    336:   DMGetNumLabels(*dm, &labelIdRange[0]);
    337:   if (rank == 0) {
    338:     CGNS_ENUMT(BCType_t)        bctype;
    339:     CGNS_ENUMT(DataType_t)      datatype;
    340:     CGNS_ENUMT(PointSetType_t)  pointtype;
    341:     cgsize_t                    *points;
    342:     PetscReal                   *normals;
    343:     int                         normal[3];
    344:     char                        *bcname = buffer;
    345:     cgsize_t                    npoints, nnormals;
    346:     int                         z, nbc, bc, c, ndatasets;

    348:     for (z = 1; z <= nzones; ++z) {
    349:       cg_nbocos(cgid, 1, z, &nbc);
    350:       for (bc = 1; bc <= nbc; ++bc) {
    351:         cg_boco_info(cgid, 1, z, bc, bcname, &bctype, &pointtype, &npoints, normal, &nnormals, &datatype, &ndatasets);
    352:         DMCreateLabel(*dm, bcname);
    353:         DMGetLabel(*dm, bcname, &label);
    354:         PetscMalloc2(npoints, &points, nnormals, &normals);
    355:         cg_boco_read(cgid, 1, z, bc, points, (void *) normals);
    356:         if (pointtype == CGNS_ENUMV(ElementRange)) {
    357:           /* Range of cells: assuming half-open interval since the documentation sucks */
    358:           for (c = points[0]; c < points[1]; ++c) {
    359:             DMLabelSetValue(label, c - cellStart[z-1], 1);
    360:           }
    361:         } else if (pointtype == CGNS_ENUMV(ElementList)) {
    362:           /* List of cells */
    363:           for (c = 0; c < npoints; ++c) {
    364:             DMLabelSetValue(label, points[c] - cellStart[z-1], 1);
    365:           }
    366:         } else if (pointtype == CGNS_ENUMV(PointRange)) {
    367:           CGNS_ENUMT(GridLocation_t) gridloc;

    369:           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
    370:           cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");
    371:           cg_gridlocation_read(&gridloc);
    372:           /* Range of points: assuming half-open interval since the documentation sucks */
    373:           for (c = points[0]; c < points[1]; ++c) {
    374:             if (gridloc == CGNS_ENUMV(Vertex)) DMLabelSetValue(label, c - vertStart[z-1], 1);
    375:             else                               DMLabelSetValue(label, c - cellStart[z-1], 1);
    376:           }
    377:         } else if (pointtype == CGNS_ENUMV(PointList)) {
    378:           CGNS_ENUMT(GridLocation_t) gridloc;

    380:           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
    381:           cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");
    382:           cg_gridlocation_read(&gridloc);
    383:           for (c = 0; c < npoints; ++c) {
    384:             if (gridloc == CGNS_ENUMV(Vertex)) DMLabelSetValue(label, points[c] - vertStart[z-1], 1);
    385:             else                               DMLabelSetValue(label, points[c] - cellStart[z-1], 1);
    386:           }
    387:         } else SETERRQ(comm, PETSC_ERR_SUP, "Unsupported point set type %d", (int) pointtype);
    388:         PetscFree2(points, normals);
    389:       }
    390:     }
    391:     PetscFree2(cellStart, vertStart);
    392:   }
    393:   DMGetNumLabels(*dm, &labelIdRange[1]);
    394:   MPI_Bcast(labelIdRange, 2, MPIU_INT, 0, comm);

    396:   /* Create BC labels at all processes */
    397:   for (labelId = labelIdRange[0]; labelId < labelIdRange[1]; ++labelId) {
    398:     char *labelName = buffer;
    399:     size_t len = sizeof(buffer);
    400:     const char *locName;

    402:     if (rank == 0) {
    403:       DMGetLabelByNum(*dm, labelId, &label);
    404:       PetscObjectGetName((PetscObject)label, &locName);
    405:       PetscStrncpy(labelName, locName, len);
    406:     }
    407:     MPI_Bcast(labelName, (PetscMPIInt)len, MPIU_INT, 0, comm);
    408:     DMCreateLabel(*dm, labelName);
    409:   }
    410:   return 0;
    411: #else
    412:   SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir");
    413: #endif
    414: }