Actual source code: plexcgns.c

petsc-dev 2014-02-02
Report Typos and Errors
  1: #define PETSCDM_DLL
  2: #include <petsc-private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/

  4: #if defined(PETSC_HAVE_CGNS)
  5: #include <cgnslib.h>
  6: #include <cgns_io.h>
  7: #endif

 11: /*@
 12:   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file.

 14:   Collective on comm

 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

 21:   Output Parameter:
 22: . dm  - The DM object representing the mesh

 24:   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html

 26:   Level: beginner

 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;
 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

 48: #if defined(PETSC_HAVE_CGNS)
 49:   MPI_Comm_rank(comm, &rank);
 50:   MPI_Comm_size(comm, &num_proc);
 51:   DMCreate(comm, dm);
 52:   DMSetType(*dm, DMPLEX);
 53:   /* Open CGNS II file and read basic informations on rank 0, then broadcast to all processors */
 54:   if (!rank) {
 55:     int nbases, z;

 57:     cg_nbases(cgid, &nbases);
 58:     if (nbases > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d\n",nbases);
 59:     cg_base_read(cgid, 1, basename, &dim, &physDim);
 60:     cg_nzones(cgid, 1, &nzones);
 61:     for (z = 1; z <= nzones; ++z) {
 62:       cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */

 64:       cg_zone_read(cgid, 1, z, buffer, sizes);
 65:       numVertices += sizes[0];
 66:       numCells    += sizes[1];
 67:     }
 68:   }
 69:   MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);
 70:   MPI_Bcast(&dim, 1, MPI_INT, 0, comm);
 71:   MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);
 72:   PetscObjectSetName((PetscObject) *dm, basename);
 73:   DMPlexSetDimension(*dm, dim);
 74:   DMPlexSetChart(*dm, 0, numCells+numVertices);

 76:   /* Read zone information */
 77:   if (!rank) {
 78:     int z, c, c_loc, v, v_loc;

 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;

 91:       cg_zone_type(cgid, 1, z, &zonetype);
 92:       if (zonetype == Structured) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS");
 93:       cg_nsections(cgid, 1, z, &nsections);
 94:       if (nsections > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d\n",nsections);
 95:       cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);
 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;

101:         cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);
102:         PetscMalloc1(elementDataSize, &elements);
103:         cg_elements_read(cgid, 1, z, 1, elements, NULL);
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:           DMPlexSetConeSize(*dm, c, numCorners);
113:           off += numCorners+1;
114:         }
115:         PetscFree(elements);
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:           DMPlexSetConeSize(*dm, c, numCorners);
126:         }
127:       }
128:     }
129:     DMSetUp(*dm);
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;

136:       cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);
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:       cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);
140:       PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);
141:       cg_elements_read(cgid, 1, z, 1, elements, NULL);
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 (cellType == TETRA_4) {
158:             PetscInt tmp = cone[0];
159:             cone[0] = cone[1];
160:             cone[1] = tmp;
161:           }
162:           /* Hexahedra are inverted */
163:           if (cellType == HEXA_8) {
164:             PetscInt tmp = cone[1];
165:             cone[1] = cone[3];
166:             cone[3] = tmp;
167:           }
168:           DMPlexSetCone(*dm, c, cone);
169:           DMPlexSetLabelValue(*dm, "zone", c, z);
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:         }

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:           DMPlexSetCone(*dm, c, cone);
198:           DMPlexSetLabelValue(*dm, "zone", c, z);
199:         }
200:       }
201:       PetscFree2(elements,cone);
202:     }
203:   }
204:   DMPlexSymmetrize(*dm);
205:   DMPlexStratify(*dm);
206:   if (interpolate) {
207:     DM idm;

209:     DMPlexInterpolate(*dm, &idm);
210:     /* Maintain zone label */
211:     {
212:       DMLabel label;

214:       DMPlexRemoveLabel(*dm, "zone", &label);
215:       if (label) {DMPlexAddLabel(idm, label);}
216:     }
217:     DMDestroy(dm);
218:     *dm  = idm;
219:   }

221:   /* Read coordinates */
222:   DMGetCoordinateSection(*dm, &coordSection);
223:   PetscSectionSetNumFields(coordSection, 1);
224:   PetscSectionSetFieldComponents(coordSection, 0, dim);
225:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
226:   for (v = numCells; v < numCells+numVertices; ++v) {
227:     PetscSectionSetDof(coordSection, v, dim);
228:     PetscSectionSetFieldDof(coordSection, v, 0, dim);
229:   }
230:   PetscSectionSetUp(coordSection);
231:   PetscSectionGetStorageSize(coordSection, &coordSize);
232:   VecCreate(comm, &coordinates);
233:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
234:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
235:   VecSetType(coordinates,VECSTANDARD);
236:   VecGetArray(coordinates, &coords);
237:   if (!rank) {
238:     PetscInt off = 0;
239:     float   *x[3];
240:     int      z, c, d;

242:     PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);
243:     for (z = 1, c = 0; 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;


251:       cg_zone_read(cgid, 1, z, buffer, sizes);
252:       range_max[0] = sizes[0];
253:       cg_ngrids(cgid, 1, z, &ngrids);
254:       if (ngrids > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d\n",ngrids);
255:       cg_ncoords(cgid, 1, z, &ncoords);
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:         cg_coord_info(cgid, 1, z, 1, &datatype, buffer);
259:         cg_coord_read(cgid, 1, z, buffer, RealSingle, range_min, range_max, x[d]);
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:     PetscFree3(x[0],x[1],x[2]);
273:   }
274:   VecRestoreArray(coordinates, &coords);
275:   DMSetCoordinatesLocal(*dm, coordinates);
276:   VecDestroy(&coordinates);
277: #else
278:   SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir");
279: #endif
280:   return(0);
281: }