xref: /petsc/src/dm/impls/plex/plexcgns.c (revision 7fdeb8b9be692273ce99bf256278a44482a052ca)
1 #define PETSCDM_DLL
2 #include <petsc-private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3 
4 #if defined(PETSC_HAVE_CGNS)
5 #include <cgnslib.h>
6 #include <cgns_io.h>
7 #endif
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "DMPlexCreateCGNS"
11 /*@
12   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file.
13 
14   Collective on comm
15 
16   Input Parameters:
17 + comm  - The MPI communicator
18 . cgid - The CG id associated with a file and obtained using cg_open
19 - interpolate - Create faces and edges in the mesh
20 
21   Output Parameter:
22 . dm  - The DM object representing the mesh
23 
24   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html
25 
26   Level: beginner
27 
28 .keywords: mesh,CGNS
29 .seealso: DMPlexCreate(), DMPlexCreateExodus()
30 @*/
31 PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm)
32 {
33 #if defined(PETSC_HAVE_CGNS)
34   PetscMPIInt    num_proc, rank;
35   PetscSection   coordSection;
36   Vec            coordinates;
37   PetscScalar    *coords;
38   PetscInt       coordSize, v;
39   PetscErrorCode ierr;
40   /* Read from file */
41   char basename[CGIO_MAX_NAME_LENGTH+1];
42   char buffer[CGIO_MAX_NAME_LENGTH+1];
43   int  dim    = 0, physDim = 0, numVertices = 0, numCells = 0;
44   int  nzones = 0;
45 #endif
46 
47   PetscFunctionBegin;
48 #if defined(PETSC_HAVE_CGNS)
49   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
50   ierr = MPI_Comm_size(comm, &num_proc);CHKERRQ(ierr);
51   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
52   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
53   /* Open CGNS II file and read basic informations on rank 0, then broadcast to all processors */
54   if (!rank) {
55     int nbases, z;
56 
57     ierr = cg_nbases(cgid, &nbases);CHKERRQ(ierr);
58     if (nbases > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d\n",nbases);
59     ierr = cg_base_read(cgid, 1, basename, &dim, &physDim);CHKERRQ(ierr);
60     ierr = cg_nzones(cgid, 1, &nzones);CHKERRQ(ierr);
61     for (z = 1; z <= nzones; ++z) {
62       cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
63 
64       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
65       numVertices += sizes[0];
66       numCells    += sizes[1];
67     }
68   }
69   ierr = MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);CHKERRQ(ierr);
70   ierr = MPI_Bcast(&dim, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
71   ierr = MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
72   ierr = PetscObjectSetName((PetscObject) *dm, basename);CHKERRQ(ierr);
73   ierr = DMPlexSetDimension(*dm, dim);CHKERRQ(ierr);
74   ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr);
75 
76   /* Read zone information */
77   if (!rank) {
78     int z, c, c_loc, v, v_loc;
79 
80     /* Read the cell set connectivity table and build mesh topology
81        CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */
82     /* First set sizes */
83     for (z = 1, c = 0; z <= nzones; ++z) {
84       ZoneType_t    zonetype;
85       int           nsections;
86       ElementType_t cellType;
87       cgsize_t      start, end;
88       int           nbndry, parentFlag;
89       PetscInt      numCorners;
90 
91       ierr = cg_zone_type(cgid, 1, z, &zonetype);CHKERRQ(ierr);
92       if (zonetype == Structured) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS");
93       ierr = cg_nsections(cgid, 1, z, &nsections);CHKERRQ(ierr);
94       if (nsections > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d\n",nsections);
95       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
96       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
97       if (cellType == MIXED) {
98         cgsize_t elementDataSize, *elements;
99         PetscInt off;
100 
101         ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
102         ierr = PetscMalloc1(elementDataSize, &elements);CHKERRQ(ierr);
103         ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
104         for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) {
105           switch (elements[off]) {
106           case TRI_3:   numCorners = 3;break;
107           case QUAD_4:  numCorners = 4;break;
108           case TETRA_4: numCorners = 4;break;
109           case HEXA_8:  numCorners = 8;break;
110           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]);
111           }
112           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
113           off += numCorners+1;
114         }
115         ierr = PetscFree(elements);CHKERRQ(ierr);
116       } else {
117         switch (cellType) {
118         case TRI_3:   numCorners = 3;break;
119         case QUAD_4:  numCorners = 4;break;
120         case TETRA_4: numCorners = 4;break;
121         case HEXA_8:  numCorners = 8;break;
122         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
123         }
124         for (c_loc = start; c_loc <= end; ++c_loc, ++c) {
125           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
126         }
127       }
128     }
129     ierr = DMSetUp(*dm);CHKERRQ(ierr);
130     for (z = 1, c = 0; z <= nzones; ++z) {
131       ElementType_t cellType;
132       cgsize_t     *elements, elementDataSize, start, end;
133       int           nbndry, parentFlag;
134       PetscInt     *cone, numc, numCorners, maxCorners = 27;
135 
136       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
137       numc = end - start;
138       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
139       ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
140       ierr = PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);CHKERRQ(ierr);
141       ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
142       if (cellType == MIXED) {
143         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
144         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
145           switch (elements[v]) {
146           case TRI_3:   numCorners = 3;break;
147           case QUAD_4:  numCorners = 4;break;
148           case TETRA_4: numCorners = 4;break;
149           case HEXA_8:  numCorners = 8;break;
150           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]);
151           }
152           ++v;
153           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
154             cone[v_loc] = elements[v]+numCells-1;
155           }
156           /* Tetrahedra are inverted */
157           if (elements[v] == TETRA_4) {
158             PetscInt tmp = cone[0];
159             cone[0] = cone[1];
160             cone[1] = tmp;
161           }
162           /* Hexahedra are inverted */
163           if (elements[v] == HEXA_8) {
164             PetscInt tmp = cone[1];
165             cone[1] = cone[3];
166             cone[3] = tmp;
167           }
168           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
169           ierr = DMPlexSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
170         }
171       } else {
172         switch (cellType) {
173         case TRI_3:   numCorners = 3;break;
174         case QUAD_4:  numCorners = 4;break;
175         case TETRA_4: numCorners = 4;break;
176         case HEXA_8:  numCorners = 8;break;
177         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
178         }
179 
180         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
181         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
182           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
183             cone[v_loc] = elements[v]+numCells-1;
184           }
185           /* Tetrahedra are inverted */
186           if (cellType == TETRA_4) {
187             PetscInt tmp = cone[0];
188             cone[0] = cone[1];
189             cone[1] = tmp;
190           }
191           /* Hexahedra are inverted */
192           if (cellType == HEXA_8) {
193             PetscInt tmp = cone[1];
194             cone[1] = cone[3];
195             cone[3] = tmp;
196           }
197           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
198           ierr = DMPlexSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
199         }
200       }
201       ierr = PetscFree2(elements,cone);CHKERRQ(ierr);
202     }
203   }
204   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
205   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
206   if (interpolate) {
207     DM idm;
208 
209     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
210     /* Maintain zone label */
211     {
212       DMLabel label;
213 
214       ierr = DMPlexRemoveLabel(*dm, "zone", &label);CHKERRQ(ierr);
215       if (label) {ierr = DMPlexAddLabel(idm, label);CHKERRQ(ierr);}
216     }
217     ierr = DMDestroy(dm);CHKERRQ(ierr);
218     *dm  = idm;
219   }
220 
221   /* Read coordinates */
222   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
223   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
224   ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
225   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
226   for (v = numCells; v < numCells+numVertices; ++v) {
227     ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
228     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
229   }
230   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
231   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
232   ierr = VecCreate(comm, &coordinates);CHKERRQ(ierr);
233   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
234   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
235   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
236   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
237   if (!rank) {
238     PetscInt off = 0;
239     float   *x[3];
240     int      z, d;
241 
242     ierr = PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);CHKERRQ(ierr);
243     for (z = 1; z <= nzones; ++z) {
244       DataType_t datatype;
245       cgsize_t   sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
246       cgsize_t   range_min[3] = {1, 1, 1};
247       cgsize_t   range_max[3] = {1, 1, 1};
248       int        ngrids, ncoords;
249 
250 
251       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
252       range_max[0] = sizes[0];
253       ierr = cg_ngrids(cgid, 1, z, &ngrids);CHKERRQ(ierr);
254       if (ngrids > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d\n",ngrids);
255       ierr = cg_ncoords(cgid, 1, z, &ncoords);CHKERRQ(ierr);
256       if (ncoords != dim) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a coordinate array for each dimension, not %d\n",ncoords);
257       for (d = 0; d < dim; ++d) {
258         ierr = cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer);CHKERRQ(ierr);
259         ierr = cg_coord_read(cgid, 1, z, buffer, RealSingle, range_min, range_max, x[d]);CHKERRQ(ierr);
260       }
261       if (dim > 0) {
262         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+0] = x[0][v];
263       }
264       if (dim > 1) {
265         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+1] = x[1][v];
266       }
267       if (dim > 2) {
268         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+2] = x[2][v];
269       }
270       off += sizes[0];
271     }
272     ierr = PetscFree3(x[0],x[1],x[2]);CHKERRQ(ierr);
273   }
274   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
275   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
276   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
277 #else
278   SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir");
279 #endif
280   PetscFunctionReturn(0);
281 }
282