Actual source code: plexexodusii.c

petsc-3.4.2 2013-07-02
  1: #define PETSCDM_DLL
  2: #include <petsc-private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/

  4: #if defined(PETSC_HAVE_EXODUSII)
  5: #include <netcdf.h>
  6: #include <exodusII.h>
  7: #endif

 11: /*@
 12:   DMPlexCreateExodus - Create a DMPlex mesh from an ExodusII file.

 14:   Collective on comm

 16:   Input Parameters:
 17: + comm  - The MPI communicator
 18: . exoid - The ExodusII id associated with a exodus file and obtained using ex_open
 19: - interpolate - Create faces and edges in the mesh

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

 24:   Level: beginner

 26: .keywords: mesh,ExodusII
 27: .seealso: DMPLEX, DMCreate()
 28: @*/
 29: PetscErrorCode DMPlexCreateExodus(MPI_Comm comm, PetscInt exoid, PetscBool interpolate, DM *dm)
 30: {
 31: #if defined(PETSC_HAVE_EXODUSII)
 32:   PetscMPIInt    num_proc, rank;
 33:   PetscSection   coordSection;
 34:   Vec            coordinates;
 35:   PetscScalar    *coords;
 36:   PetscInt       coordSize, v;
 38:   /* Read from ex_get_init() */
 39:   char title[PETSC_MAX_PATH_LEN+1];
 40:   int  dim    = 0, numVertices = 0, numCells = 0;
 41:   int  num_cs = 0, num_vs = 0, num_fs = 0;
 42: #endif

 45: #if defined(PETSC_HAVE_EXODUSII)
 46:   MPI_Comm_rank(comm, &rank);
 47:   MPI_Comm_size(comm, &num_proc);
 48:   DMCreate(comm, dm);
 49:   DMSetType(*dm, DMPLEX);
 50:   /* Open EXODUS II file and read basic informations on rank 0, then broadcast to all processors */
 51:   if (!rank) {
 52:     ex_get_init(exoid, title, &dim, &numVertices, &numCells, &num_cs, &num_vs, &num_fs);
 53:     if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ExodusII ex_get_init() failed with error code %D\n",ierr);
 54:     if (!num_cs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Exodus file does not contain any cell set\n");
 55:   }
 56:   MPI_Bcast(title, PETSC_MAX_PATH_LEN+1, MPI_CHAR, 0, comm);
 57:   MPI_Bcast(&dim, 1, MPI_INT, 0, comm);
 58:   PetscObjectSetName((PetscObject) *dm, title);
 59:   DMPlexSetDimension(*dm, dim);
 60:   DMPlexSetChart(*dm, 0, numCells+numVertices);

 62:   /* Read cell sets information */
 63:   if (!rank) {
 64:     PetscInt *cone;
 65:     int      c, cs, c_loc, v, v_loc;
 66:     /* Read from ex_get_elem_blk_ids() */
 67:     int *cs_id;
 68:     /* Read from ex_get_elem_block() */
 69:     char buffer[PETSC_MAX_PATH_LEN+1];
 70:     int  num_cell_in_set, num_vertex_per_cell, num_attr;
 71:     /* Read from ex_get_elem_conn() */
 72:     int *cs_connect;

 74:     /* Get cell sets IDs */
 75:     PetscMalloc(num_cs * sizeof(int), &cs_id);
 76:     ex_get_elem_blk_ids(exoid, cs_id);
 77:     /* Read the cell set connectivity table and build mesh topology
 78:        EXO standard requires that cells in cell sets be numbered sequentially and be pairwise disjoint. */
 79:     /* First set sizes */
 80:     for (cs = 0, c = 0; cs < num_cs; cs++) {
 81:       ex_get_elem_block(exoid, cs_id[cs], buffer, &num_cell_in_set, &num_vertex_per_cell, &num_attr);
 82:       for (c_loc = 0; c_loc < num_cell_in_set; ++c_loc, ++c) {
 83:         DMPlexSetConeSize(*dm, c, num_vertex_per_cell);
 84:       }
 85:     }
 86:     DMSetUp(*dm);
 87:     for (cs = 0, c = 0; cs < num_cs; cs++) {
 88:       ex_get_elem_block(exoid, cs_id[cs], buffer, &num_cell_in_set, &num_vertex_per_cell, &num_attr);
 89:       PetscMalloc2(num_vertex_per_cell*num_cell_in_set,int,&cs_connect,num_vertex_per_cell,PetscInt,&cone);
 90:       ex_get_elem_conn(exoid, cs_id[cs], cs_connect);
 91:       /* EXO uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
 92:       for (c_loc = 0, v = 0; c_loc < num_cell_in_set; ++c_loc, ++c) {
 93:         for (v_loc = 0; v_loc < num_vertex_per_cell; ++v_loc, ++v) {
 94:           cone[v_loc] = cs_connect[v]+numCells-1;
 95:         }
 96:         if (dim == 3) {
 97:           /* Tetrahedra are inverted */
 98:           if (num_vertex_per_cell == 4) {
 99:             PetscInt tmp = cone[0];
100:             cone[0] = cone[1];
101:             cone[1] = tmp;
102:           }
103:           /* Hexahedra are inverted */
104:           if (num_vertex_per_cell == 8) {
105:             PetscInt tmp = cone[1];
106:             cone[1] = cone[3];
107:             cone[3] = tmp;
108:           }
109:         }
110:         DMPlexSetCone(*dm, c, cone);
111:         DMPlexSetLabelValue(*dm, "Cell Sets", c, cs_id[cs]);
112:       }
113:       PetscFree2(cs_connect,cone);
114:     }
115:     PetscFree(cs_id);
116:   }
117:   DMPlexSymmetrize(*dm);
118:   DMPlexStratify(*dm);
119:   if (interpolate) {
120:     DM idm;

122:     DMPlexInterpolate(*dm, &idm);
123:     /* Maintain Cell Sets label */
124:     {
125:       DMLabel label;

127:       DMPlexRemoveLabel(*dm, "Cell Sets", &label);
128:       if (label) {DMPlexAddLabel(idm, label);}
129:     }
130:     DMDestroy(dm);
131:     *dm  = idm;
132:   }

134:   /* Create vertex set label */
135:   if (!rank && (num_vs > 0)) {
136:     int vs, v;
137:     /* Read from ex_get_node_set_ids() */
138:     int *vs_id;
139:     /* Read from ex_get_node_set_param() */
140:     int num_vertex_in_set, num_attr;
141:     /* Read from ex_get_node_set() */
142:     int *vs_vertex_list;

144:     /* Get vertex set ids */
145:     PetscMalloc(num_vs * sizeof(int), &vs_id);
146:     ex_get_node_set_ids(exoid, vs_id);
147:     for (vs = 0; vs < num_vs; ++vs) {
148:       ex_get_node_set_param(exoid, vs_id[vs], &num_vertex_in_set, &num_attr);
149:       PetscMalloc(num_vertex_in_set * sizeof(int), &vs_vertex_list);
150:       ex_get_node_set(exoid, vs_id[vs], vs_vertex_list);
151:       for (v = 0; v < num_vertex_in_set; ++v) {
152:         DMPlexSetLabelValue(*dm, "Vertex Sets", vs_vertex_list[v]+numCells-1, vs_id[vs]);
153:       }
154:       PetscFree(vs_vertex_list);
155:     }
156:     PetscFree(vs_id);
157:   }
158:   /* Read coordinates */
159:   DMPlexGetCoordinateSection(*dm, &coordSection);
160:   PetscSectionSetNumFields(coordSection, 1);
161:   PetscSectionSetFieldComponents(coordSection, 0, dim);
162:   PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
163:   for (v = numCells; v < numCells+numVertices; ++v) {
164:     PetscSectionSetDof(coordSection, v, dim);
165:     PetscSectionSetFieldDof(coordSection, v, 0, dim);
166:   }
167:   PetscSectionSetUp(coordSection);
168:   PetscSectionGetStorageSize(coordSection, &coordSize);
169:   VecCreate(comm, &coordinates);
170:   PetscObjectSetName((PetscObject) coordinates, "coordinates");
171:   VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
172:   VecSetFromOptions(coordinates);
173:   VecGetArray(coordinates, &coords);
174:   if (!rank) {
175:     float *x, *y, *z;

177:     PetscMalloc3(numVertices,float,&x,numVertices,float,&y,numVertices,float,&z);
178:     ex_get_coord(exoid, x, y, z);
179:     if (dim > 0) {
180:       for (v = 0; v < numVertices; ++v) coords[v*dim+0] = x[v];
181:     }
182:     if (dim > 1) {
183:       for (v = 0; v < numVertices; ++v) coords[v*dim+1] = y[v];
184:     }
185:     if (dim > 2) {
186:       for (v = 0; v < numVertices; ++v) coords[v*dim+2] = z[v];
187:     }
188:     PetscFree3(x,y,z);
189:   }
190:   VecRestoreArray(coordinates, &coords);
191:   DMSetCoordinatesLocal(*dm, coordinates);
192:   VecDestroy(&coordinates);

194:   /* Create side set label */
195:   if (!rank && interpolate && (num_fs > 0)) {
196:     int fs, f, voff;
197:     /* Read from ex_get_side_set_ids() */
198:     int *fs_id;
199:     /* Read from ex_get_side_set_param() */
200:     int num_side_in_set, num_dist_fact_in_set;
201:     /* Read from ex_get_side_set_node_list() */
202:     int *fs_vertex_count_list, *fs_vertex_list;

204:     /* Get side set ids */
205:     PetscMalloc(num_fs * sizeof(int), &fs_id);
206:     ex_get_side_set_ids(exoid, fs_id);
207:     for (fs = 0; fs < num_fs; ++fs) {
208:       ex_get_side_set_param(exoid, fs_id[fs], &num_side_in_set, &num_dist_fact_in_set);
209:       PetscMalloc2(num_side_in_set,int,&fs_vertex_count_list,num_side_in_set*4,int,&fs_vertex_list);
210:       ex_get_side_set_node_list(exoid, fs_id[fs], fs_vertex_count_list, fs_vertex_list);
211:       for (f = 0, voff = 0; f < num_side_in_set; ++f) {
212:         const PetscInt *faces   = NULL;
213:         PetscInt       faceSize = fs_vertex_count_list[f], numFaces;
214:         PetscInt       faceVertices[4], v;

216:         if (faceSize > 4) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "ExodusII side cannot have %d > 4 vertices", faceSize);
217:         for (v = 0; v < faceSize; ++v, ++voff) {
218:           faceVertices[v] = fs_vertex_list[voff]+numCells-1;
219:         }
220:         DMPlexGetFullJoin(*dm, faceSize, faceVertices, &numFaces, &faces);
221:         if (numFaces != 1) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Invalid ExodusII side %d in set %d maps to %d faces", f, fs, numFaces);
222:         DMPlexSetLabelValue(*dm, "Face Sets", faces[0], fs_id[fs]);
223:         DMPlexRestoreJoin(*dm, faceSize, faceVertices, &numFaces, &faces);
224:       }
225:       PetscFree2(fs_vertex_count_list,fs_vertex_list);
226:     }
227:     PetscFree(fs_id);
228:   }
229: #else
230:   SETERRQ(comm, PETSC_ERR_SUP, "This method requires ExodusII support. Reconfigure using --download-exodusii");
231: #endif
232:   return(0);
233: }