Actual source code: plexhdf5xdmf.c
petsc-3.15.0 2021-03-30
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;
13: PetscStrrchr(path,'/',&tmp);
14: PetscStrcpy(name,tmp);
15: if (tmp != path) {
16: /* '/' found, name is substring of path after last occurence of '/'. */
17: /* Trim the '/name' part from path just by inserting null character. */
18: tmp--;
19: *tmp = '\0';
20: } else {
21: /* '/' not found, name = path, path = "/". */
22: PetscStrcpy(path,"/");
23: }
24: return(0);
25: }
27: /*
28: - invert (involute) cells of some types according to XDMF/VTK numbering of vertices in a cells
29: - cell type is identified using the number of vertices
30: */
31: static PetscErrorCode DMPlexInvertCells_XDMF_Private(DM dm)
32: {
33: PetscInt dim, *cones, cHeight, cStart, cEnd, p;
34: PetscSection cs;
38: DMGetDimension(dm, &dim);
39: if (dim != 3) return(0);
40: DMPlexGetCones(dm, &cones);
41: DMPlexGetConeSection(dm, &cs);
42: DMPlexGetVTKCellHeight(dm, &cHeight);
43: DMPlexGetHeightStratum(dm, cHeight, &cStart, &cEnd);
44: for (p=cStart; p<cEnd; p++) {
45: PetscInt numCorners, o;
47: PetscSectionGetDof(cs, p, &numCorners);
48: PetscSectionGetOffset(cs, p, &o);
49: switch (numCorners) {
50: case 4: DMPlexInvertCell(DM_POLYTOPE_TETRAHEDRON,&cones[o]); break;
51: case 6: DMPlexInvertCell(DM_POLYTOPE_TRI_PRISM,&cones[o]); break;
52: case 8: DMPlexInvertCell(DM_POLYTOPE_HEXAHEDRON,&cones[o]); break;
53: }
54: }
55: return(0);
56: }
58: PetscErrorCode DMPlexLoad_HDF5_Xdmf_Internal(DM dm, PetscViewer viewer)
59: {
60: Vec coordinates;
61: IS cells;
62: PetscInt spatialDim, topoDim = -1, numCells, numVertices, NVertices, numCorners;
63: PetscMPIInt rank;
64: MPI_Comm comm;
65: PetscErrorCode ierr;
66: char topo_path[PETSC_MAX_PATH_LEN]="/viz/topology/cells", topo_name[PETSC_MAX_PATH_LEN];
67: char geom_path[PETSC_MAX_PATH_LEN]="/geometry/vertices", geom_name[PETSC_MAX_PATH_LEN];
68: PetscBool seq = PETSC_FALSE, hasCellDim = PETSC_FALSE;
71: PetscObjectGetComm((PetscObject)dm, &comm);
72: MPI_Comm_rank(comm, &rank);
74: PetscOptionsBegin(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,"DMPlex HDF5/XDMF Loader Options","PetscViewer");
75: PetscOptionsString("-dm_plex_hdf5_topology_path","HDF5 path of topology dataset",NULL,topo_path,topo_path,sizeof(topo_path),NULL);
76: PetscOptionsString("-dm_plex_hdf5_geometry_path","HDF5 path to geometry dataset",NULL,geom_path,geom_path,sizeof(geom_path),NULL);
77: PetscOptionsBool("-dm_plex_hdf5_force_sequential","force sequential loading",NULL,seq,&seq,NULL);
78: PetscOptionsEnd();
80: SplitPath_Private(topo_path, topo_name);
81: SplitPath_Private(geom_path, geom_name);
82: PetscInfo2(dm, "Topology group %s, name %s\n", topo_path, topo_name);
83: PetscInfo2(dm, "Geometry group %s, name %s\n", geom_path, geom_name);
85: /* Read topology */
86: PetscViewerHDF5PushGroup(viewer, topo_path);
87: ISCreate(comm, &cells);
88: PetscObjectSetName((PetscObject) cells, topo_name);
89: if (seq) {
90: PetscViewerHDF5ReadSizes(viewer, topo_name, NULL, &numCells);
91: PetscLayoutSetSize(cells->map, numCells);
92: numCells = !rank ? numCells : 0;
93: PetscLayoutSetLocalSize(cells->map, numCells);
94: }
95: ISLoad(cells, viewer);
96: ISGetLocalSize(cells, &numCells);
97: ISGetBlockSize(cells, &numCorners);
98: PetscViewerHDF5HasAttribute(viewer, topo_name, "cell_dim", &hasCellDim);
99: if (hasCellDim) {PetscViewerHDF5ReadAttribute(viewer, topo_name, "cell_dim", PETSC_INT, &topoDim);}
100: PetscViewerHDF5PopGroup(viewer);
101: numCells /= numCorners;
103: /* Read geometry */
104: PetscViewerHDF5PushGroup(viewer, geom_path);
105: VecCreate(comm, &coordinates);
106: PetscObjectSetName((PetscObject) coordinates, geom_name);
107: if (seq) {
108: PetscViewerHDF5ReadSizes(viewer, geom_name, NULL, &numVertices);
109: PetscLayoutSetSize(coordinates->map, numVertices);
110: numVertices = !rank ? numVertices : 0;
111: PetscLayoutSetLocalSize(coordinates->map, numVertices);
112: }
113: VecLoad(coordinates, viewer);
114: VecGetLocalSize(coordinates, &numVertices);
115: VecGetSize(coordinates, &NVertices);
116: VecGetBlockSize(coordinates, &spatialDim);
117: PetscViewerHDF5PopGroup(viewer);
118: numVertices /= spatialDim;
119: NVertices /= spatialDim;
121: PetscInfo4(NULL, "Loaded mesh dimensions: numCells %D numCorners %D numVertices %D spatialDim %D\n", numCells, numCorners, numVertices, spatialDim);
122: {
123: const PetscScalar *coordinates_arr;
124: PetscReal *coordinates_arr_real;
125: const PetscInt *cells_arr;
126: PetscSF sfVert = NULL;
127: PetscInt i;
129: VecGetArrayRead(coordinates, &coordinates_arr);
130: ISGetIndices(cells, &cells_arr);
132: if (PetscDefined(USE_COMPLEX)) {
133: /* convert to real numbers if PetscScalar is complex */
134: /*TODO More systematic would be to change all the function arguments to PetscScalar */
135: PetscMalloc1(numVertices*spatialDim, &coordinates_arr_real);
136: for (i = 0; i < numVertices*spatialDim; ++i) {
137: coordinates_arr_real[i] = PetscRealPart(coordinates_arr[i]);
138: if (PetscUnlikelyDebug(PetscImaginaryPart(coordinates_arr[i]))) {
139: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Vector of coordinates contains complex numbers but only real vectors are currently supported.");
140: }
141: }
142: } else coordinates_arr_real = (PetscReal*)coordinates_arr;
144: DMSetDimension(dm, topoDim < 0 ? spatialDim : topoDim);
145: DMPlexBuildFromCellListParallel(dm, numCells, numVertices, NVertices, numCorners, cells_arr, &sfVert);
146: DMPlexInvertCells_XDMF_Private(dm);
147: DMPlexBuildCoordinatesFromCellListParallel(dm, spatialDim, sfVert, coordinates_arr_real);
148: VecRestoreArrayRead(coordinates, &coordinates_arr);
149: ISRestoreIndices(cells, &cells_arr);
150: PetscSFDestroy(&sfVert);
151: if (PetscDefined(USE_COMPLEX)) {PetscFree(coordinates_arr_real);}
152: }
153: ISDestroy(&cells);
154: VecDestroy(&coordinates);
156: /* scale coordinates - unlike in DMPlexLoad_HDF5_Internal, this can only be done after DM is populated */
157: {
158: PetscReal lengthScale;
160: DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);
161: DMGetCoordinates(dm, &coordinates);
162: VecScale(coordinates, 1.0/lengthScale);
163: }
165: /* Read Labels */
166: /* TODO: this probably does not work as elements get permuted */
167: /* DMPlexLoadLabels_HDF5_Internal(dm, viewer); */
168: return(0);
169: }
170: #endif