xref: /petsc/src/dm/impls/plex/plexcgns.c (revision 0b49a1d0cbb4269e5740fcf0f6f0dc40b18c5a16)
1 #define PETSCDM_DLL
2 #include <petsc/private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3 
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 
10 #undef __FUNCT__
11 #define __FUNCT__ "DMPlexCreateCGNSFromFile"
12 /*@C
13   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file.
14 
15   Collective on comm
16 
17   Input Parameters:
18 + comm  - The MPI communicator
19 . filename - The name of the CGNS file
20 - interpolate - Create faces and edges in the mesh
21 
22   Output Parameter:
23 . dm  - The DM object representing the mesh
24 
25   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html
26 
27   Level: beginner
28 
29 .keywords: mesh,CGNS
30 .seealso: DMPlexCreate(), DMPlexCreateCGNS(), DMPlexCreateExodus()
31 @*/
32 PetscErrorCode DMPlexCreateCGNSFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
33 {
34   PetscMPIInt    rank;
35   PetscErrorCode ierr;
36 #if defined(PETSC_HAVE_CGNS)
37   int cgid = -1;
38 #endif
39 
40   PetscFunctionBegin;
41   PetscValidCharPointer(filename, 2);
42   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
43 #if defined(PETSC_HAVE_CGNS)
44   if (!rank) {
45     ierr = cg_open(filename, CG_MODE_READ, &cgid);CHKERRQ(ierr);
46     if (cgid <= 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "cg_open(\"%s\",...) did not return a valid file ID", filename);
47   }
48   ierr = DMPlexCreateCGNS(comm, cgid, interpolate, dm);CHKERRQ(ierr);
49   if (!rank) {ierr = cg_close(cgid);CHKERRQ(ierr);}
50 #else
51   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires CGNS support. Reconfigure using --with-cgns-dir");
52 #endif
53   PetscFunctionReturn(0);
54 }
55 
56 #undef __FUNCT__
57 #define __FUNCT__ "DMPlexCreateCGNS"
58 /*@
59   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file ID.
60 
61   Collective on comm
62 
63   Input Parameters:
64 + comm  - The MPI communicator
65 . cgid - The CG id associated with a file and obtained using cg_open
66 - interpolate - Create faces and edges in the mesh
67 
68   Output Parameter:
69 . dm  - The DM object representing the mesh
70 
71   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html
72 
73   Level: beginner
74 
75 .keywords: mesh,CGNS
76 .seealso: DMPlexCreate(), DMPlexCreateExodus()
77 @*/
78 PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm)
79 {
80 #if defined(PETSC_HAVE_CGNS)
81   PetscMPIInt    num_proc, rank;
82   PetscSection   coordSection;
83   Vec            coordinates;
84   PetscScalar   *coords;
85   PetscInt      *cellStart, *vertStart;
86   PetscInt       coordSize, v;
87   PetscErrorCode ierr;
88   /* Read from file */
89   char basename[CGIO_MAX_NAME_LENGTH+1];
90   char buffer[CGIO_MAX_NAME_LENGTH+1];
91   int  dim    = 0, physDim = 0, numVertices = 0, numCells = 0;
92   int  nzones = 0;
93 #endif
94 
95   PetscFunctionBegin;
96 #if defined(PETSC_HAVE_CGNS)
97   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
98   ierr = MPI_Comm_size(comm, &num_proc);CHKERRQ(ierr);
99   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
100   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
101   /* Open CGNS II file and read basic informations on rank 0, then broadcast to all processors */
102   if (!rank) {
103     int nbases, z;
104 
105     ierr = cg_nbases(cgid, &nbases);CHKERRQ(ierr);
106     if (nbases > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d\n",nbases);
107     ierr = cg_base_read(cgid, 1, basename, &dim, &physDim);CHKERRQ(ierr);
108     ierr = cg_nzones(cgid, 1, &nzones);CHKERRQ(ierr);
109     ierr = PetscCalloc2(nzones+1, &cellStart, nzones+1, &vertStart);CHKERRQ(ierr);
110     for (z = 1; z <= nzones; ++z) {
111       cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
112 
113       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
114       numVertices += sizes[0];
115       numCells    += sizes[1];
116       cellStart[z] += sizes[1] + cellStart[z-1];
117       vertStart[z] += sizes[0] + vertStart[z-1];
118     }
119     for (z = 1; z <= nzones; ++z) {
120       vertStart[z] += numCells;
121     }
122   }
123   ierr = MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);CHKERRQ(ierr);
124   ierr = MPI_Bcast(&dim, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
125   ierr = MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
126   ierr = PetscObjectSetName((PetscObject) *dm, basename);CHKERRQ(ierr);
127   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
128   ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr);
129 
130   /* Read zone information */
131   if (!rank) {
132     int z, c, c_loc, v, v_loc;
133 
134     /* Read the cell set connectivity table and build mesh topology
135        CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */
136     /* First set sizes */
137     for (z = 1, c = 0; z <= nzones; ++z) {
138       ZoneType_t    zonetype;
139       int           nsections;
140       ElementType_t cellType;
141       cgsize_t      start, end;
142       int           nbndry, parentFlag;
143       PetscInt      numCorners;
144 
145       ierr = cg_zone_type(cgid, 1, z, &zonetype);CHKERRQ(ierr);
146       if (zonetype == Structured) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS");
147       ierr = cg_nsections(cgid, 1, z, &nsections);CHKERRQ(ierr);
148       if (nsections > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d\n",nsections);
149       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
150       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
151       if (cellType == MIXED) {
152         cgsize_t elementDataSize, *elements;
153         PetscInt off;
154 
155         ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
156         ierr = PetscMalloc1(elementDataSize, &elements);CHKERRQ(ierr);
157         ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
158         for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) {
159           switch (elements[off]) {
160           case TRI_3:   numCorners = 3;break;
161           case QUAD_4:  numCorners = 4;break;
162           case TETRA_4: numCorners = 4;break;
163           case HEXA_8:  numCorners = 8;break;
164           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]);
165           }
166           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
167           off += numCorners+1;
168         }
169         ierr = PetscFree(elements);CHKERRQ(ierr);
170       } else {
171         switch (cellType) {
172         case TRI_3:   numCorners = 3;break;
173         case QUAD_4:  numCorners = 4;break;
174         case TETRA_4: numCorners = 4;break;
175         case HEXA_8:  numCorners = 8;break;
176         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
177         }
178         for (c_loc = start; c_loc <= end; ++c_loc, ++c) {
179           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
180         }
181       }
182     }
183     ierr = DMSetUp(*dm);CHKERRQ(ierr);
184     for (z = 1, c = 0; z <= nzones; ++z) {
185       ElementType_t cellType;
186       cgsize_t     *elements, elementDataSize, start, end;
187       int           nbndry, parentFlag;
188       PetscInt     *cone, numc, numCorners, maxCorners = 27;
189 
190       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
191       numc = end - start;
192       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
193       ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
194       ierr = PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);CHKERRQ(ierr);
195       ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
196       if (cellType == MIXED) {
197         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
198         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
199           switch (elements[v]) {
200           case TRI_3:   numCorners = 3;break;
201           case QUAD_4:  numCorners = 4;break;
202           case TETRA_4: numCorners = 4;break;
203           case HEXA_8:  numCorners = 8;break;
204           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]);
205           }
206           ++v;
207           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
208             cone[v_loc] = elements[v]+numCells-1;
209           }
210           /* Tetrahedra are inverted */
211           if (elements[v] == TETRA_4) {
212             PetscInt tmp = cone[0];
213             cone[0] = cone[1];
214             cone[1] = tmp;
215           }
216           /* Hexahedra are inverted */
217           if (elements[v] == HEXA_8) {
218             PetscInt tmp = cone[5];
219             cone[5] = cone[7];
220             cone[7] = tmp;
221           }
222           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
223           ierr = DMSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
224         }
225       } else {
226         switch (cellType) {
227         case TRI_3:   numCorners = 3;break;
228         case QUAD_4:  numCorners = 4;break;
229         case TETRA_4: numCorners = 4;break;
230         case HEXA_8:  numCorners = 8;break;
231         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
232         }
233 
234         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
235         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
236           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
237             cone[v_loc] = elements[v]+numCells-1;
238           }
239           /* Tetrahedra are inverted */
240           if (cellType == TETRA_4) {
241             PetscInt tmp = cone[0];
242             cone[0] = cone[1];
243             cone[1] = tmp;
244           }
245           /* Hexahedra are inverted, and they give the top first */
246           if (cellType == HEXA_8) {
247             PetscInt tmp = cone[5];
248             cone[5] = cone[7];
249             cone[7] = tmp;
250           }
251           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
252           ierr = DMSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
253         }
254       }
255       ierr = PetscFree2(elements,cone);CHKERRQ(ierr);
256     }
257   }
258   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
259   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
260   if (interpolate) {
261     DM idm = NULL;
262 
263     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
264     /* Maintain zone label */
265     {
266       DMLabel label;
267 
268       ierr = DMRemoveLabel(*dm, "zone", &label);CHKERRQ(ierr);
269       if (label) {ierr = DMAddLabel(idm, label);CHKERRQ(ierr);}
270     }
271     ierr = DMDestroy(dm);CHKERRQ(ierr);
272     *dm  = idm;
273   }
274 
275   /* Read coordinates */
276   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
277   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
278   ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
279   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
280   for (v = numCells; v < numCells+numVertices; ++v) {
281     ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
282     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
283   }
284   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
285   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
286   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
287   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
288   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
289   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
290   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
291   if (!rank) {
292     PetscInt off = 0;
293     float   *x[3];
294     int      z, d;
295 
296     ierr = PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);CHKERRQ(ierr);
297     for (z = 1; z <= nzones; ++z) {
298       DataType_t datatype;
299       cgsize_t   sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
300       cgsize_t   range_min[3] = {1, 1, 1};
301       cgsize_t   range_max[3] = {1, 1, 1};
302       int        ngrids, ncoords;
303 
304 
305       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
306       range_max[0] = sizes[0];
307       ierr = cg_ngrids(cgid, 1, z, &ngrids);CHKERRQ(ierr);
308       if (ngrids > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d\n",ngrids);
309       ierr = cg_ncoords(cgid, 1, z, &ncoords);CHKERRQ(ierr);
310       if (ncoords != dim) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a coordinate array for each dimension, not %d\n",ncoords);
311       for (d = 0; d < dim; ++d) {
312         ierr = cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer);CHKERRQ(ierr);
313         ierr = cg_coord_read(cgid, 1, z, buffer, RealSingle, range_min, range_max, x[d]);CHKERRQ(ierr);
314       }
315       if (dim > 0) {
316         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+0] = x[0][v];
317       }
318       if (dim > 1) {
319         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+1] = x[1][v];
320       }
321       if (dim > 2) {
322         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+2] = x[2][v];
323       }
324       off += sizes[0];
325     }
326     ierr = PetscFree3(x[0],x[1],x[2]);CHKERRQ(ierr);
327   }
328   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
329   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
330   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
331   /* Read boundary conditions */
332   if (!rank) {
333     DMLabel        label;
334     BCType_t       bctype;
335     DataType_t     datatype;
336     PointSetType_t pointtype;
337     cgsize_t      *points;
338     PetscReal     *normals;
339     int            normal[3];
340     char          *bcname = buffer;
341     cgsize_t       npoints, nnormals;
342     int            z, nbc, bc, c, ndatasets;
343 
344     for (z = 1; z <= nzones; ++z) {
345       ierr = cg_nbocos(cgid, 1, z, &nbc);CHKERRQ(ierr);
346       for (bc = 1; bc <= nbc; ++bc) {
347         ierr = cg_boco_info(cgid, 1, z, bc, bcname, &bctype, &pointtype, &npoints, normal, &nnormals, &datatype, &ndatasets);CHKERRQ(ierr);
348         ierr = DMCreateLabel(*dm, bcname);CHKERRQ(ierr);
349         ierr = DMGetLabel(*dm, bcname, &label);CHKERRQ(ierr);
350         ierr = PetscMalloc2(npoints, &points, nnormals, &normals);CHKERRQ(ierr);
351         ierr = cg_boco_read(cgid, 1, z, bc, points, (void *) normals);CHKERRQ(ierr);
352         if (pointtype == ElementRange) {
353           /* Range of cells: assuming half-open interval since the documentation sucks */
354           for (c = points[0]; c < points[1]; ++c) {
355             ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr);
356           }
357         } else if (pointtype == ElementList) {
358           /* List of cells */
359           for (c = 0; c < npoints; ++c) {
360             ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr);
361           }
362         } else if (pointtype == PointRange) {
363           GridLocation_t gridloc;
364 
365           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
366           ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");CHKERRQ(ierr);
367           ierr = cg_gridlocation_read(&gridloc);CHKERRQ(ierr);
368           /* Range of points: assuming half-open interval since the documentation sucks */
369           for (c = points[0]; c < points[1]; ++c) {
370             if (gridloc == Vertex) {ierr = DMLabelSetValue(label, c - vertStart[z-1], 1);CHKERRQ(ierr);}
371             else                   {ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr);}
372           }
373         } else if (pointtype == PointList) {
374           GridLocation_t gridloc;
375 
376           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
377           ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");
378           ierr = cg_gridlocation_read(&gridloc);
379           for (c = 0; c < npoints; ++c) {
380             if (gridloc == Vertex) {ierr = DMLabelSetValue(label, points[c] - vertStart[z-1], 1);CHKERRQ(ierr);}
381             else                   {ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr);}
382           }
383         } else SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported point set type %d", (int) pointtype);
384         ierr = PetscFree2(points, normals);CHKERRQ(ierr);
385       }
386     }
387     ierr = PetscFree2(cellStart, vertStart);CHKERRQ(ierr);
388   }
389 #else
390   SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir");
391 #endif
392   PetscFunctionReturn(0);
393 }
394