Actual source code: plexmed.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1: #define PETSCDM_DLL
  2:  #include <petsc/private/dmpleximpl.h>

  4: #if defined(PETSC_HAVE_MED)
  5: #include <med.h>
  6: #endif

  8: /*@C
  9:   DMPlexCreateMedFromFile - Create a DMPlex mesh from a (Salome-)Med file.

 11: + comm        - The MPI communicator
 12: . filename    - Name of the .med file
 13: - interpolate - Create faces and edges in the mesh

 15:   Output Parameter:
 16: . dm  - The DM object representing the mesh

 18:   Note: http://www.salome-platform.org/user-section/about/med, http://docs.salome-platform.org/latest/MED_index.html

 20:   Level: beginner

 22: .seealso: DMPlexCreateFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
 23: @*/
 24: PetscErrorCode DMPlexCreateMedFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
 25: {
 26:   PetscMPIInt     rank, size;
 27: #if defined(PETSC_HAVE_MED)
 28:   PetscInt        i, ngeo, fileID, cellID, facetID, spaceDim, meshDim;
 29:   PetscInt        numVertices = 0, numCells = 0, numCorners, numCellsLocal, numVerticesLocal;
 30:   med_int        *medCellList;
 31:   int            *cellList;
 32:   med_float      *coordinates = NULL;
 33:   PetscReal      *vertcoords = NULL;
 34:   PetscLayout     vLayout, cLayout;
 35:   const PetscInt *vrange, *crange;
 36:   PetscSF         sfVertices;
 37:   char           *axisname, *unitname, meshname[MED_NAME_SIZE+1], geotypename[MED_NAME_SIZE+1];
 38:   char            meshdescription[MED_COMMENT_SIZE+1], dtunit[MED_SNAME_SIZE+1];
 39:   med_sorting_type sortingtype;
 40:   med_mesh_type   meshtype;
 41:   med_axis_type   axistype;
 42:   med_bool        coordinatechangement, geotransformation;
 43:   med_geometry_type geotype[2];
 44:   med_filter      vfilter = MED_FILTER_INIT;
 45:   med_filter      cfilter = MED_FILTER_INIT;
 46: #endif
 47:   PetscErrorCode  ierr;

 50:   MPI_Comm_rank(comm, &rank);
 51:   MPI_Comm_size(comm, &size);
 52: #if defined(PETSC_HAVE_MED)
 53:   fileID = MEDfileOpen(filename, MED_ACC_RDONLY);
 54:   if (fileID < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Unable to open .med mesh file: %s", filename);
 55:   if (MEDnMesh(fileID) < 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "No meshes found in .med mesh file: %s", filename);
 56:   spaceDim = MEDmeshnAxis(fileID, 1);
 57:   if (spaceDim < 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Mesh of unknown space dimension found in .med mesh file: %s", filename);
 58:   /* Read general mesh information */
 59:   PetscMalloc1(MED_SNAME_SIZE*spaceDim+1, &axisname);
 60:   PetscMalloc1(MED_SNAME_SIZE*spaceDim+1, &unitname);
 61:   {
 62:     med_int medMeshDim, medNstep;
 63:     med_int medSpaceDim = spaceDim;

 65:     MEDmeshInfo(fileID, 1, meshname, &medSpaceDim, &medMeshDim, &meshtype, meshdescription,
 66:                        dtunit, &sortingtype, &medNstep, &axistype, axisname, unitname);
 67:     spaceDim = medSpaceDim;
 68:     meshDim  = medMeshDim;
 69:   }
 70:   PetscFree(axisname);
 71:   PetscFree(unitname);
 72:   /* Partition mesh coordinates */
 73:   numVertices = MEDmeshnEntity(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
 74:                                MED_COORDINATE, MED_NO_CMODE,&coordinatechangement, &geotransformation);
 75:   PetscLayoutCreate(comm, &vLayout);
 76:   PetscLayoutSetSize(vLayout, numVertices);
 77:   PetscLayoutSetBlockSize(vLayout, 1);
 78:   PetscLayoutSetUp(vLayout);
 79:   PetscLayoutGetRanges(vLayout, &vrange);
 80:   numVerticesLocal = vrange[rank+1]-vrange[rank];
 81:   MEDfilterBlockOfEntityCr(fileID, numVertices, 1, spaceDim, MED_ALL_CONSTITUENT, MED_FULL_INTERLACE, MED_COMPACT_STMODE,
 82:                                   MED_NO_PROFILE, vrange[rank]+1, 1, numVerticesLocal, 1, 1, &vfilter);
 83:   /* Read mesh coordinates */
 84:   if (numVertices < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "No nodes found in .med mesh file: %s", filename);
 85:   PetscMalloc1(numVerticesLocal*spaceDim, &coordinates);
 86:   MEDmeshNodeCoordinateAdvancedRd(fileID, meshname, MED_NO_DT, MED_NO_IT, &vfilter, coordinates);
 87:   /* Read the types of entity sets in the mesh */
 88:   ngeo = MEDmeshnEntity(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_GEO_ALL, MED_CONNECTIVITY,
 89:                         MED_NODAL, &coordinatechangement, &geotransformation);
 90:   if (ngeo < 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "No cells found in .med mesh file: %s", filename);
 91:   if (ngeo > 2) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Currently no support for hybrid meshes in .med mesh file: %s", filename);
 92:   MEDmeshEntityInfo(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, 1, geotypename, &(geotype[0]));
 93:   if (ngeo > 1) {MEDmeshEntityInfo(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, 2, geotypename, &(geotype[1]));}
 94:   else geotype[1] = 0;
 95:   /* Determine topological dim and set ID for cells */
 96:   cellID = geotype[0]/100 > geotype[1]/100 ? 0 : 1;
 97:   facetID = geotype[0]/100 > geotype[1]/100 ? 1 : 0;
 98:   meshDim = geotype[cellID] / 100;
 99:   numCorners = geotype[cellID] % 100;
100:   /* Partition cells */
101:   numCells = MEDmeshnEntity(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype[cellID],
102:                             MED_CONNECTIVITY, MED_NODAL,&coordinatechangement, &geotransformation);
103:   PetscLayoutCreate(comm, &cLayout);
104:   PetscLayoutSetSize(cLayout, numCells);
105:   PetscLayoutSetBlockSize(cLayout, 1);
106:   PetscLayoutSetUp(cLayout);
107:   PetscLayoutGetRanges(cLayout, &crange);
108:   numCellsLocal = crange[rank+1]-crange[rank];
109:   MEDfilterBlockOfEntityCr(fileID, numCells, 1, numCorners, MED_ALL_CONSTITUENT, MED_FULL_INTERLACE, MED_COMPACT_STMODE,
110:                                   MED_NO_PROFILE, crange[rank]+1, 1, numCellsLocal, 1, 1, &cfilter);
111:   /* Read cell connectivity */
112:   if (numCells < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "No cells found in .med mesh file: %s", filename);
113:   PetscMalloc1(numCellsLocal*numCorners, &medCellList);
114:   MEDmeshElementConnectivityAdvancedRd(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype[cellID],
115:                                               MED_NODAL, &cfilter, medCellList);
116:   if (sizeof(med_int) == sizeof(int)) {
117:     cellList = (int *) medCellList;
118:     medCellList = NULL;
119:   } else {
120:     PetscMalloc1(numCellsLocal*numCorners, &cellList);
121:     for (i = 0; i < numCellsLocal*numCorners; i++) cellList[i] = medCellList[i];
122:     PetscFree(medCellList);
123:   }
124:   for (i = 0; i < numCellsLocal*numCorners; i++) cellList[i]--; /* Correct entity counting */
125:   /* Generate the DM */
126:   if (sizeof(med_float) == sizeof(PetscReal)) {
127:     vertcoords = (PetscReal *) coordinates;
128:   } else {
129:     PetscMalloc1(numVerticesLocal*spaceDim, &vertcoords);
130:     for (i = 0; i < numVerticesLocal*spaceDim; i++) vertcoords[i] = coordinates[i];
131:   }
132:   DMPlexCreateFromCellListParallel(comm, meshDim, numCellsLocal, numVerticesLocal, numCorners, interpolate, cellList, spaceDim, coordinates, &sfVertices, dm);
133:   if (sizeof(med_float) == sizeof(PetscReal)) {
134:     vertcoords = NULL;
135:   } else {
136:     PetscFree(vertcoords);
137:   }

139:   if (ngeo > 1) {
140:     PetscInt        numFacets = 0, numFacetsLocal, numFacetCorners, numFacetsRendezvous;
141:     PetscInt        c, f, v, vStart, joinSize, vertices[8];
142:     med_int        *medFacetList;
143:     PetscInt       *facetList, *facetListRendezvous, *facetIDs, *facetIDsRendezvous, *facetListRemote, *facetIDsRemote;
144:     const PetscInt *frange, *join;
145:     PetscLayout     fLayout;
146:     med_filter      ffilter = MED_FILTER_INIT, fidfilter = MED_FILTER_INIT;
147:     PetscSection facetSectionRemote, facetSectionIDsRemote;
148:     /* Partition facets */
149:     numFacets = MEDmeshnEntity(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype[facetID],
150:                                MED_CONNECTIVITY, MED_NODAL,&coordinatechangement, &geotransformation);
151:     numFacetCorners = geotype[facetID] % 100;
152:     PetscLayoutCreate(comm, &fLayout);
153:     PetscLayoutSetSize(fLayout, numFacets);
154:     PetscLayoutSetBlockSize(fLayout, 1);
155:     PetscLayoutSetUp(fLayout);
156:     PetscLayoutGetRanges(fLayout, &frange);
157:     numFacetsLocal = frange[rank+1]-frange[rank];
158:     MEDfilterBlockOfEntityCr(fileID, numFacets, 1, numFacetCorners, MED_ALL_CONSTITUENT, MED_FULL_INTERLACE, MED_COMPACT_STMODE,
159:                                     MED_NO_PROFILE, frange[rank]+1, 1, numFacetsLocal, 1, 1, &ffilter);
160:     MEDfilterBlockOfEntityCr(fileID, numFacets, 1, 1, MED_ALL_CONSTITUENT, MED_FULL_INTERLACE, MED_COMPACT_STMODE,
161:                                     MED_NO_PROFILE, frange[rank]+1, 1, numFacetsLocal, 1, 1, &fidfilter);
162:     /* Read facet connectivity */
163:     DMPlexGetDepthStratum(*dm, 0, &vStart, NULL);
164:     PetscMalloc1(numFacetsLocal*numFacetCorners, &medFacetList);
165:     PetscCalloc1(numFacetsLocal, &facetIDs);
166:     MEDmeshElementConnectivityAdvancedRd(fileID, meshname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype[facetID],
167:                                                 MED_NODAL, &ffilter, medFacetList);
168:     if (sizeof(med_int) == sizeof(PetscInt)) {
169:       facetList = (PetscInt *) medFacetList;
170:       medFacetList = NULL;
171:     } else {
172:       PetscMalloc1(numFacetsLocal*numFacetCorners, &facetList);
173:       for (i = 0; i < numFacetsLocal*numFacetCorners; i++) facetList[i] = medFacetList[i];
174:       PetscFree(medFacetList);
175:     }
176:     for (i = 0; i < numFacetsLocal*numFacetCorners; i++) facetList[i]--; /* Correct entity counting */
177:     /* Read facet IDs */
178:     MEDmeshEntityAttributeAdvancedRd(fileID, meshname, MED_FAMILY_NUMBER, MED_NO_DT, MED_NO_IT, MED_CELL,
179:                                             geotype[facetID], &fidfilter, facetIDs);
180:     {
181:       /* Send facets and IDs to a rendezvous partition that is based on the initial vertex partitioning. */
182:       PetscInt           p, r;
183:       PetscSFNode       *remoteProc;
184:       DMLabel            lblFacetRendezvous, lblFacetMigration;
185:       PetscSection       facetSection, facetSectionRendezvous;
186:       PetscSF            sfProcess, sfFacetMigration;
187:       const PetscSFNode *remoteVertices;
188:       DMLabelCreate("Facet Rendezvous", &lblFacetRendezvous);
189:       DMLabelCreate("Facet Migration", &lblFacetMigration);
190:       PetscSFGetGraph(sfVertices, NULL, NULL, NULL, &remoteVertices);
191:       for (f = 0; f < numFacetsLocal; f++) {
192:         for (v = 0; v < numFacetCorners; v++) {
193:           /* Find vertex owner on rendezvous partition and mark in label */
194:           const PetscInt vertex = facetList[f*numFacetCorners+v];
195:           r = rank; while (vrange[r] > vertex) r--; while (vrange[r + 1] < vertex) r++;
196:           DMLabelSetValue(lblFacetRendezvous, f, r);
197:         }
198:       }
199:       /* Build a global process SF */
200:       PetscMalloc1(size, &remoteProc);
201:       for (p = 0; p < size; ++p) {
202:         remoteProc[p].rank  = p;
203:         remoteProc[p].index = rank;
204:       }
205:       PetscSFCreate(comm, &sfProcess);
206:       PetscObjectSetName((PetscObject) sfProcess, "Process SF");
207:       PetscSFSetGraph(sfProcess, size, size, NULL, PETSC_OWN_POINTER, remoteProc, PETSC_OWN_POINTER);
208:       /* Convert facet rendezvous label into SF for migration */
209:       DMPlexPartitionLabelInvert(*dm, lblFacetRendezvous, sfProcess, lblFacetMigration);
210:       DMPlexPartitionLabelCreateSF(*dm, lblFacetMigration, &sfFacetMigration);
211:       /* Migrate facet connectivity data */
212:       PetscSectionCreate(comm, &facetSection);
213:       PetscSectionSetChart(facetSection, 0, numFacetsLocal);
214:       for (f = 0; f < numFacetsLocal; f++) {PetscSectionSetDof(facetSection, f, numFacetCorners);}
215:       PetscSectionSetUp(facetSection);
216:       PetscSectionCreate(comm, &facetSectionRendezvous);
217:       DMPlexDistributeData(*dm, sfFacetMigration, facetSection, MPIU_INT, facetList, facetSectionRendezvous, (void**) &facetListRendezvous);
218:       /* Migrate facet IDs */
219:       PetscSFGetGraph(sfFacetMigration, NULL, &numFacetsRendezvous, NULL, NULL);
220:       PetscMalloc1(numFacetsRendezvous, &facetIDsRendezvous);
221:       PetscSFBcastBegin(sfFacetMigration, MPIU_INT, facetIDs, facetIDsRendezvous);
222:       PetscSFBcastEnd(sfFacetMigration, MPIU_INT, facetIDs, facetIDsRendezvous);
223:       /* Clean up */
224:       DMLabelDestroy(&lblFacetRendezvous);
225:       DMLabelDestroy(&lblFacetMigration);
226:       PetscSFDestroy(&sfProcess);
227:       PetscSFDestroy(&sfFacetMigration);
228:       PetscSectionDestroy(&facetSection);
229:       PetscSectionDestroy(&facetSectionRendezvous);
230:     }
231:     {
232:       /* On the rendevouz partition we build a vertex-wise section/array of facets and IDs. */
233:       PetscInt               sizeVertexFacets, offset, sizeFacetIDsRemote;
234:       PetscInt              *vertexFacets, *vertexIdx, *vertexFacetIDs;
235:       PetscSection           facetSectionVertices, facetSectionIDs;
236:       ISLocalToGlobalMapping ltogVertexNumbering;
237:       PetscSectionCreate(comm, &facetSectionVertices);
238:       PetscSectionSetChart(facetSectionVertices, 0, numVerticesLocal);
239:       PetscSectionCreate(comm, &facetSectionIDs);
240:       PetscSectionSetChart(facetSectionIDs, 0, numVerticesLocal);
241:       for (f = 0; f < numFacetsRendezvous*numFacetCorners; f++) {
242:         const PetscInt vertex = facetListRendezvous[f];
243:         if (vrange[rank] <= vertex && vertex < vrange[rank+1]) {
244:           PetscSectionAddDof(facetSectionIDs, vertex-vrange[rank], 1);
245:           PetscSectionAddDof(facetSectionVertices, vertex-vrange[rank], numFacetCorners);
246:         }
247:       }
248:       PetscSectionSetUp(facetSectionVertices);
249:       PetscSectionSetUp(facetSectionIDs);
250:       PetscSectionGetStorageSize(facetSectionVertices, &sizeVertexFacets);
251:       PetscSectionGetStorageSize(facetSectionVertices, &sizeFacetIDsRemote);
252:       PetscMalloc1(sizeVertexFacets, &vertexFacets);
253:       PetscMalloc1(sizeFacetIDsRemote, &vertexFacetIDs);
254:       PetscCalloc1(numVerticesLocal, &vertexIdx);
255:       for (f = 0; f < numFacetsRendezvous; f++) {
256:         for (c = 0; c < numFacetCorners; c++) {
257:           const PetscInt vertex = facetListRendezvous[f*numFacetCorners+c];
258:           if (vrange[rank] <= vertex && vertex < vrange[rank+1]) {
259:             /* Flip facet connectivities and IDs to a vertex-wise layout */
260:             PetscSectionGetOffset(facetSectionVertices, vertex-vrange[rank], &offset);
261:             offset += vertexIdx[vertex-vrange[rank]] * numFacetCorners;
262:             PetscMemcpy(&(vertexFacets[offset]), &(facetListRendezvous[f*numFacetCorners]), numFacetCorners*sizeof(PetscInt));
263:             PetscSectionGetOffset(facetSectionIDs, vertex-vrange[rank], &offset);
264:             offset += vertexIdx[vertex-vrange[rank]];
265:             vertexFacetIDs[offset] = facetIDsRendezvous[f];
266:             vertexIdx[vertex-vrange[rank]]++;
267:           }
268:         }
269:       }
270:       /* Distribute the vertex-wise facet connectivities over the vertexSF */
271:       PetscSectionCreate(comm, &facetSectionRemote);
272:       DMPlexDistributeData(*dm, sfVertices, facetSectionVertices, MPIU_INT, vertexFacets, facetSectionRemote, (void**) &facetListRemote);
273:       PetscSectionCreate(comm, &facetSectionIDsRemote);
274:       DMPlexDistributeData(*dm, sfVertices, facetSectionIDs, MPIU_INT, vertexFacetIDs, facetSectionIDsRemote, (void**) &facetIDsRemote);
275:       /* Convert facet connectivities to local vertex numbering */
276:       PetscSectionGetStorageSize(facetSectionRemote, &sizeVertexFacets);
277:       ISLocalToGlobalMappingCreateSF(sfVertices, vrange[rank], &ltogVertexNumbering);
278:       ISGlobalToLocalMappingApplyBlock(ltogVertexNumbering, IS_GTOLM_MASK, sizeVertexFacets, facetListRemote, NULL, facetListRemote);
279:       /* Clean up */
280:       PetscFree(vertexFacets);
281:       PetscFree(vertexIdx);
282:       PetscFree(vertexFacetIDs);
283:       PetscSectionDestroy(&facetSectionVertices);
284:       PetscSectionDestroy(&facetSectionIDs);
285:       ISLocalToGlobalMappingDestroy(&ltogVertexNumbering);
286:     }
287:     {
288:       PetscInt offset, dof;
289:       DMLabel lblFaceSets;
290:       PetscBool verticesLocal;
291:       /* Identify and mark facets locally with facet joins */
292:       DMCreateLabel(*dm, "Face Sets");
293:       DMGetLabel(*dm, "Face Sets", &lblFaceSets);
294:       /* We need to set a new default value here, since -1 is a legitimate facet ID */
295:       DMLabelSetDefaultValue(lblFaceSets, -666666666);
296:       for (v = 0; v < numVerticesLocal; v++) {
297:         PetscSectionGetOffset(facetSectionRemote, v, &offset);
298:         PetscSectionGetDof(facetSectionRemote, v, &dof);
299:         for (f = 0; f < dof; f += numFacetCorners) {
300:           for (verticesLocal = PETSC_TRUE, c = 0; c < numFacetCorners; ++c) {
301:             if (facetListRemote[offset+f+c] < 0) {verticesLocal = PETSC_FALSE; break;}
302:             vertices[c] = vStart + facetListRemote[offset+f+c];
303:           }
304:           if (verticesLocal) {
305:             DMPlexGetFullJoin(*dm, numFacetCorners, (const PetscInt*)vertices, &joinSize, &join);
306:             if (joinSize == 1) {
307:               DMLabelSetValue(lblFaceSets, join[0], facetIDsRemote[(offset+f) / numFacetCorners]);
308:             }
309:             DMPlexRestoreJoin(*dm, numFacetCorners, (const PetscInt*)vertices, &joinSize, &join);
310:           }
311:         }
312:       }
313:     }
314:     PetscFree(facetList);
315:     PetscFree(facetListRendezvous);
316:     PetscFree(facetListRemote);
317:     PetscFree(facetIDs);
318:     PetscFree(facetIDsRendezvous);
319:     PetscFree(facetIDsRemote);
320:     PetscLayoutDestroy(&fLayout);
321:     PetscSectionDestroy(&facetSectionRemote);
322:     PetscSectionDestroy(&facetSectionIDsRemote);
323:   }
324:   MEDfileClose(fileID);
325:   PetscFree(coordinates);
326:   PetscFree(cellList);
327:   PetscLayoutDestroy(&vLayout);
328:   PetscLayoutDestroy(&cLayout);
329:   PetscSFDestroy(&sfVertices);
330: #else
331:   SETERRQ(comm, PETSC_ERR_SUP, "This method requires Med mesh reader support. Reconfigure using --download-med");
332: #endif
333:   return(0);
334: }