Actual source code: plexhdf5xdmf.c

      1: #include <petsc/private/dmpleximpl.h>
      2: #include <petsc/private/isimpl.h>
      3: #include <petsc/private/vecimpl.h>
      4: #include <petsclayouthdf5.h>

      6: #if defined(PETSC_HAVE_HDF5)
      7: static PetscErrorCode SplitPath_Private(char path[], char name[])
      8: {
      9:   char *tmp;

     11:   PetscStrrchr(path,'/',&tmp);
     12:   PetscStrcpy(name,tmp);
     13:   if (tmp != path) {
     14:     /* '/' found, name is substring of path after last occurence of '/'. */
     15:     /* Trim the '/name' part from path just by inserting null character. */
     16:     tmp--;
     17:     *tmp = '\0';
     18:   } else {
     19:     /* '/' not found, name = path, path = "/". */
     20:     PetscStrcpy(path,"/");
     21:   }
     22:   return 0;
     23: }

     25: /*
     26:   - invert (involute) cells of some types according to XDMF/VTK numbering of vertices in a cells
     27:   - cell type is identified using the number of vertices
     28: */
     29: static PetscErrorCode DMPlexInvertCells_XDMF_Private(DM dm)
     30: {
     31:   PetscInt       dim, *cones, cHeight, cStart, cEnd, p;
     32:   PetscSection   cs;

     34:   DMGetDimension(dm, &dim);
     35:   if (dim != 3) return 0;
     36:   DMPlexGetCones(dm, &cones);
     37:   DMPlexGetConeSection(dm, &cs);
     38:   DMPlexGetVTKCellHeight(dm, &cHeight);
     39:   DMPlexGetHeightStratum(dm, cHeight, &cStart, &cEnd);
     40:   for (p=cStart; p<cEnd; p++) {
     41:     PetscInt numCorners, o;

     43:     PetscSectionGetDof(cs, p, &numCorners);
     44:     PetscSectionGetOffset(cs, p, &o);
     45:     switch (numCorners) {
     46:       case 4: DMPlexInvertCell(DM_POLYTOPE_TETRAHEDRON,&cones[o]); break;
     47:       case 6: DMPlexInvertCell(DM_POLYTOPE_TRI_PRISM,&cones[o]); break;
     48:       case 8: DMPlexInvertCell(DM_POLYTOPE_HEXAHEDRON,&cones[o]); break;
     49:     }
     50:   }
     51:   return 0;
     52: }

     54: PetscErrorCode DMPlexLoad_HDF5_Xdmf_Internal(DM dm, PetscViewer viewer)
     55: {
     56:   Vec             coordinates;
     57:   IS              cells;
     58:   PetscInt        spatialDim, topoDim = -1, numCells, numVertices, NVertices, numCorners;
     59:   PetscMPIInt     rank;
     60:   MPI_Comm        comm;
     61:   PetscErrorCode  ierr;
     62:   char            topo_path[PETSC_MAX_PATH_LEN]="/viz/topology/cells", topo_name[PETSC_MAX_PATH_LEN];
     63:   char            geom_path[PETSC_MAX_PATH_LEN]="/geometry/vertices",  geom_name[PETSC_MAX_PATH_LEN];
     64:   PetscBool       seq = PETSC_FALSE;

     66:   PetscObjectGetComm((PetscObject)dm, &comm);
     67:   MPI_Comm_rank(comm, &rank);

     69:   PetscOptionsBegin(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,"DMPlex HDF5/XDMF Loader Options","PetscViewer");
     70:   PetscOptionsString("-dm_plex_hdf5_topology_path","HDF5 path of topology dataset",NULL,topo_path,topo_path,sizeof(topo_path),NULL);
     71:   PetscOptionsString("-dm_plex_hdf5_geometry_path","HDF5 path to geometry dataset",NULL,geom_path,geom_path,sizeof(geom_path),NULL);
     72:   PetscOptionsBool("-dm_plex_hdf5_force_sequential","force sequential loading",NULL,seq,&seq,NULL);
     73:   PetscOptionsEnd();

     75:   SplitPath_Private(topo_path, topo_name);
     76:   SplitPath_Private(geom_path, geom_name);
     77:   PetscInfo(dm, "Topology group %s, name %s\n", topo_path, topo_name);
     78:   PetscInfo(dm, "Geometry group %s, name %s\n", geom_path, geom_name);

     80:   /* Read topology */
     81:   PetscViewerHDF5PushGroup(viewer, topo_path);
     82:   ISCreate(comm, &cells);
     83:   PetscObjectSetName((PetscObject) cells, topo_name);
     84:   if (seq) {
     85:     PetscViewerHDF5ReadSizes(viewer, topo_name, NULL, &numCells);
     86:     PetscLayoutSetSize(cells->map, numCells);
     87:     numCells = rank == 0 ? numCells : 0;
     88:     PetscLayoutSetLocalSize(cells->map, numCells);
     89:   }
     90:   ISLoad(cells, viewer);
     91:   ISGetLocalSize(cells, &numCells);
     92:   ISGetBlockSize(cells, &numCorners);
     93:   PetscViewerHDF5ReadAttribute(viewer, topo_name, "cell_dim", PETSC_INT, &topoDim, &topoDim);
     94:   PetscViewerHDF5PopGroup(viewer);
     95:   numCells /= numCorners;

     97:   /* Read geometry */
     98:   PetscViewerHDF5PushGroup(viewer, geom_path);
     99:   VecCreate(comm, &coordinates);
    100:   PetscObjectSetName((PetscObject) coordinates, geom_name);
    101:   if (seq) {
    102:     PetscViewerHDF5ReadSizes(viewer, geom_name, NULL, &numVertices);
    103:     PetscLayoutSetSize(coordinates->map, numVertices);
    104:     numVertices = rank == 0 ? numVertices : 0;
    105:     PetscLayoutSetLocalSize(coordinates->map, numVertices);
    106:   }
    107:   VecLoad(coordinates, viewer);
    108:   VecGetLocalSize(coordinates, &numVertices);
    109:   VecGetSize(coordinates, &NVertices);
    110:   VecGetBlockSize(coordinates, &spatialDim);
    111:   PetscViewerHDF5PopGroup(viewer);
    112:   numVertices /= spatialDim;
    113:   NVertices /= spatialDim;

    115:   PetscInfo(NULL, "Loaded mesh dimensions: numCells %D numCorners %D numVertices %D spatialDim %D\n", numCells, numCorners, numVertices, spatialDim);
    116:   {
    117:     const PetscScalar *coordinates_arr;
    118:     PetscReal         *coordinates_arr_real;
    119:     const PetscInt    *cells_arr;
    120:     PetscSF           sfVert = NULL;
    121:     PetscInt          i;

    123:     VecGetArrayRead(coordinates, &coordinates_arr);
    124:     ISGetIndices(cells, &cells_arr);

    126:     if (PetscDefined(USE_COMPLEX)) {
    127:       /* convert to real numbers if PetscScalar is complex */
    128:       /*TODO More systematic would be to change all the function arguments to PetscScalar */
    129:       PetscMalloc1(numVertices*spatialDim, &coordinates_arr_real);
    130:       for (i = 0; i < numVertices*spatialDim; ++i) {
    131:         coordinates_arr_real[i] = PetscRealPart(coordinates_arr[i]);
    132:         if (PetscUnlikelyDebug(PetscImaginaryPart(coordinates_arr[i]))) {
    133:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Vector of coordinates contains complex numbers but only real vectors are currently supported.");
    134:         }
    135:       }
    136:     } else coordinates_arr_real = (PetscReal*)coordinates_arr;

    138:     DMSetDimension(dm, topoDim < 0 ? spatialDim : topoDim);
    139:     DMPlexBuildFromCellListParallel(dm, numCells, numVertices, NVertices, numCorners, cells_arr, &sfVert, NULL);
    140:     DMPlexInvertCells_XDMF_Private(dm);
    141:     DMPlexBuildCoordinatesFromCellListParallel(dm, spatialDim, sfVert, coordinates_arr_real);
    142:     VecRestoreArrayRead(coordinates, &coordinates_arr);
    143:     ISRestoreIndices(cells, &cells_arr);
    144:     PetscSFDestroy(&sfVert);
    145:     if (PetscDefined(USE_COMPLEX)) PetscFree(coordinates_arr_real);
    146:   }
    147:   ISDestroy(&cells);
    148:   VecDestroy(&coordinates);

    150:   /* scale coordinates - unlike in DMPlexLoad_HDF5_Internal, this can only be done after DM is populated */
    151:   {
    152:     PetscReal lengthScale;

    154:     DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
    155:     DMGetCoordinates(dm, &coordinates);
    156:     VecScale(coordinates, 1.0/lengthScale);
    157:   }

    159:   /* Read Labels */
    160:   /* TODO: this probably does not work as elements get permuted */
    161:   /* DMPlexLabelsLoad_HDF5_Internal(dm, viewer); */
    162:   return 0;
    163: }
    164: #endif