/[escript]/branches/doubleplusgood/dudley/src/Mesh_readGmsh.cpp
ViewVC logotype

Annotation of /branches/doubleplusgood/dudley/src/Mesh_readGmsh.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1887 - (hide annotations)
Wed Oct 15 03:26:25 2008 UTC (10 years, 6 months ago) by ksteube
Original Path: trunk/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 16288 byte(s)
Fixed two typos that stopped the test suite from running.

Also, gcc 4.3.2 issued several warnings not seen before:
ignoring the return value of fscanf and using the wrong format
with printf.

1 gross 935
2 ksteube 1312 /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 gross 935
14 ksteube 1811
15 gross 935 /**************************************************************/
16    
17 ksteube 1312 /* Finley: read mesh */
18 gross 935
19     /**************************************************************/
20    
21     #include "Mesh.h"
22     #include <stdio.h>
23    
24 ksteube 1887 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) perror(reason); return NULL; }
25 gross 935
26 ksteube 1887
27 gross 935 /**************************************************************/
28    
29     /* reads a mesh from a Finley file of name fname */
30    
31     #define MAX_numNodes_gmsh 10
32    
33 ksteube 1312 Finley_Mesh* Finley_Mesh_readGmsh(char* fname ,index_t numDim, index_t order, index_t reduced_order, bool_t optimize) {
34 gross 935
35     double version = 1.0;
36 ksteube 1887 int format = 0, size = sizeof(double), scan_ret;
37 gross 935 dim_t numNodes, totalNumElements=0, numTags=0, numNodesPerElement, numNodesPerElement2, element_dim;
38 phornby 1628 index_t e, i0, j, gmsh_type, partition_id, itmp, elementary_id;
39 gross 1028 index_t numElements=0, numFaceElements=0, *id=NULL, *tag=NULL, *vertices=NULL;
40 gross 935 Finley_Mesh *mesh_p=NULL;
41 gross 1028 char line[LenString_MAX+1];
42 gross 935 char error_msg[LenErrorMsg_MAX];
43     double rtmp0, rtmp1;
44     double time0=Finley_timer();
45 gross 1028 FILE * fileHandle_p = NULL;
46     ElementTypeId* element_type=NULL;
47 gross 935
48 ksteube 1312 Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
49 gross 935 Finley_resetError();
50 ksteube 1312 if (mpi_info->size>1) {
51     Finley_setError(IO_ERROR,"reading GMSH with MPI is not supported yet.");
52     Paso_MPIInfo_free( mpi_info );
53 gross 935 return NULL;
54 ksteube 1312 } else {
55 gross 997
56 ksteube 1312 /* allocate mesh */
57 gross 935
58 ksteube 1312 mesh_p = Finley_Mesh_alloc(fname,numDim,order, reduced_order, mpi_info);
59     if (! Finley_noError()) return NULL;
60    
61     /* get file handle */
62     fileHandle_p = fopen(fname, "r");
63     if (fileHandle_p==NULL) {
64     sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
65     Finley_setError(IO_ERROR,error_msg);
66     Paso_MPIInfo_free( mpi_info );
67     return NULL;
68     }
69    
70     /* start reading */
71     while(1) {
72     if (! Finley_noError()) break;
73     /* find line staring with $ */
74     do {
75     if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
76     if(feof(fileHandle_p)) break;
77     } while(line[0] != '$');
78    
79     if (feof(fileHandle_p)) break;
80    
81    
82     /* format */
83     if (!strncmp(&line[1], "MeshFormat", 10)) {
84 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
85     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
86 ksteube 1312 }
87     /* nodes are read */
88     if ( !strncmp(&line[1], "NOD", 3) ||
89     !strncmp(&line[1], "NOE", 3) ||
90     !strncmp(&line[1], "Nodes", 5) ) {
91    
92 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
93     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
94 ksteube 1312 if (! Finley_noError()) break;
95     Finley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
96     if (! Finley_noError()) break;
97     for (i0 = 0; i0 < numNodes; i0++) {
98     if (1 == numDim) {
99 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
100 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
101     &rtmp0,
102     &rtmp1);
103 ksteube 1887 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
104 ksteube 1312 } else if (2 == numDim) {
105 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
106 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
107     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
108     &rtmp0);
109 ksteube 1887 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
110 ksteube 1312 } else if (3 == numDim) {
111 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
112 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
113     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
114     &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
115 ksteube 1887 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
116 ksteube 1312 }
117     mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
118     mesh_p->Nodes->Tag[i0]=0;
119 gross 935 }
120 ksteube 1312 }
121     /* elements */
122     else if(!strncmp(&line[1], "ELM", 3) ||
123     !strncmp(&line[1], "Elements", 8)) {
124    
125     ElementTypeId final_element_type = NoType;
126     ElementTypeId final_face_element_type = NoType;
127 gross 1814 ElementTypeId contact_element_type = NoType;
128 ksteube 1312 numElements=0;
129     numFaceElements=0;
130 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
131     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
132 ksteube 1312
133     id=TMPMEMALLOC(totalNumElements,index_t);
134     tag=TMPMEMALLOC(totalNumElements,index_t);
135    
136    
137     element_type=TMPMEMALLOC(totalNumElements,ElementTypeId);
138     vertices=TMPMEMALLOC(totalNumElements*MAX_numNodes_gmsh,index_t);
139     if (! (Finley_checkPtr(id) || Finley_checkPtr(tag) || Finley_checkPtr(element_type) || Finley_checkPtr(vertices) ) ) {
140     /* read all in */
141     for(e = 0; e < totalNumElements; e++) {
142 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
143     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
144 ksteube 1312 switch (gmsh_type) {
145     case 1: /* line order 1 */
146     element_type[e]=Line2;
147     element_dim=1;
148     numNodesPerElement=2;
149     break;
150     case 2: /* traingle order 1 */
151     element_type[e]=Tri3;
152     numNodesPerElement= 3;
153     element_dim=2;
154     break;
155     case 3: /* quadrilateral order 1 */
156     element_type[e]=Rec4;
157     numNodesPerElement= 4;
158     element_dim=2;
159     break;
160     case 4: /* tetrahedron order 1 */
161     element_type[e]=Tet4;
162     numNodesPerElement= 4;
163     element_dim=3;
164     break;
165     case 5: /* hexahedron order 1 */
166     element_type[e]=Hex8;
167     numNodesPerElement= 8;
168     element_dim=3;
169     break;
170     case 8: /* line order 2 */
171     element_type[e]=Line3;
172     numNodesPerElement= 3;
173     element_dim=1;
174     break;
175     case 9: /* traingle order 2 */
176     element_type[e]=Tri6;
177     numNodesPerElement= 6;
178     element_dim=2;
179     break;
180     case 10: /* quadrilateral order 2 */
181     element_type[e]=Rec9;
182     numNodesPerElement= 9;
183     element_dim=2;
184     break;
185     case 11: /* tetrahedron order 2 */
186     element_type[e]=Tet10;
187     numNodesPerElement= 10;
188     element_dim=3;
189     break;
190     case 15 : /* point */
191     element_type[e]=Point1;
192     numNodesPerElement= 1;
193     element_dim=0;
194     break;
195     default:
196     element_type[e]=NoType;
197     sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
198     Finley_setError(IO_ERROR,error_msg);
199     }
200     if (element_dim == numDim) {
201     if (final_element_type == NoType) {
202     final_element_type = element_type[e];
203     } else if (final_element_type != element_type[e]) {
204     sprintf(error_msg,"Finley can handle a single type of internal elements only.");
205     Finley_setError(IO_ERROR,error_msg);
206     break;
207     }
208     numElements++;
209     } else if (element_dim == numDim-1) {
210     if (final_face_element_type == NoType) {
211     final_face_element_type = element_type[e];
212     } else if (final_face_element_type != element_type[e]) {
213     sprintf(error_msg,"Finley can handle a single type of face elements only.");
214     Finley_setError(IO_ERROR,error_msg);
215     break;
216     }
217     numFaceElements++;
218     }
219    
220     if(version <= 1.0){
221 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
222     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
223 ksteube 1312 partition_id = 1;
224     if (numNodesPerElement2 != numNodesPerElement) {
225     sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
226     Finley_setError(IO_ERROR,error_msg);
227     }
228     } else {
229 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
230     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
231 ksteube 1312 elementary_id = tag[e] = partition_id = 1;
232     numNodesPerElement2=-1;
233     for(j = 0; j < numTags; j++){
234 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
235     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
236 ksteube 1312 if (j == 0) {
237     tag[e] = itmp;
238     } else if (j == 1) {
239     elementary_id = itmp;
240     } else if (j == 2) {
241     partition_id = itmp;
242     }
243     /* ignore any other tags */
244     }
245     }
246     if (! Finley_noError()) break;
247 ksteube 1887 for(j = 0; j < numNodesPerElement; j++) {
248     scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
249     FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
250     }
251 ksteube 1312 /* for tet10 the last two nodes need to be swapped */
252     if (element_type[e]==Tet10) {
253     itmp=vertices[INDEX2(9,e,MAX_numNodes_gmsh)];
254     vertices[INDEX2(9,e,MAX_numNodes_gmsh)]=vertices[INDEX2(8,e,MAX_numNodes_gmsh)];
255     vertices[INDEX2(8,e,MAX_numNodes_gmsh)]=itmp;
256     }
257     }
258     /* all elements have been read, now we have to identify the elements for finley */
259    
260     if (Finley_noError()) {
261     /* first we have to identify the elements to define Elementis and FaceElements */
262 gross 935 if (final_element_type == NoType) {
263 ksteube 1312 if (numDim==1) {
264     final_element_type=Line2;
265     } else if (numDim==2) {
266     final_element_type=Tri3;
267     } else if (numDim==3) {
268     final_element_type=Tet4;
269     }
270 gross 935 }
271     if (final_face_element_type == NoType) {
272 ksteube 1312 if (numDim==1) {
273     final_face_element_type=Point1;
274     } else if (numDim==2) {
275     final_face_element_type=Line2;
276     } else if (numDim==3) {
277     final_face_element_type=Tri3;
278     }
279 gross 935 }
280 gross 1814 if (final_face_element_type == Line2) {
281     contact_element_type=Line2_Contact;
282     } else if (final_face_element_type == Line3) {
283     contact_element_type=Line3_Contact;
284     } else if (final_face_element_type == Tri3) {
285     contact_element_type=Tri3_Contact;
286     } else if (final_face_element_type == Tri6) {
287     contact_element_type=Tri6_Contact;
288     } else {
289     contact_element_type=Point1_Contact;
290     }
291    
292 ksteube 1312 mesh_p->Elements=Finley_ElementFile_alloc(final_element_type,mesh_p->order, mesh_p->reduced_order, mpi_info);
293     mesh_p->FaceElements=Finley_ElementFile_alloc(final_face_element_type,mesh_p->order, mesh_p->reduced_order, mpi_info);
294 gross 1814 mesh_p->ContactElements=Finley_ElementFile_alloc(contact_element_type,mesh_p->order, mesh_p->reduced_order, mpi_info);
295 ksteube 1312 mesh_p->Points=Finley_ElementFile_alloc(Point1,mesh_p->order, mesh_p->reduced_order, mpi_info);
296     if (Finley_noError()) {
297     Finley_ElementFile_allocTable(mesh_p->Elements, numElements);
298     Finley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
299     Finley_ElementFile_allocTable(mesh_p->ContactElements, 0);
300     Finley_ElementFile_allocTable(mesh_p->Points, 0);
301     if (Finley_noError()) {
302     mesh_p->Elements->minColor=0;
303     mesh_p->Elements->maxColor=numElements-1;
304     mesh_p->FaceElements->minColor=0;
305     mesh_p->FaceElements->maxColor=numFaceElements-1;
306     mesh_p->ContactElements->minColor=0;
307     mesh_p->ContactElements->maxColor=0;
308     mesh_p->Points->minColor=0;
309     mesh_p->Points->maxColor=0;
310     numElements=0;
311     numFaceElements=0;
312     for(e = 0; e < totalNumElements; e++) {
313     if (element_type[e] == final_element_type) {
314     mesh_p->Elements->Id[numElements]=id[e];
315     mesh_p->Elements->Tag[numElements]=tag[e];
316     mesh_p->Elements->Color[numElements]=numElements;
317 gross 1740 mesh_p->Elements->Owner[numElements]=0;
318 ksteube 1312 for (j = 0; j< mesh_p->Elements->ReferenceElement->Type->numNodes; ++j) {
319     mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->ReferenceElement->Type->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
320     }
321     numElements++;
322     } else if (element_type[e] == final_face_element_type) {
323     mesh_p->FaceElements->Id[numFaceElements]=id[e];
324     mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
325     mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
326 gross 1740 mesh_p->FaceElements->Owner[numElements]=0;
327 ksteube 1312 for (j = 0; j< mesh_p->FaceElements->ReferenceElement->Type->numNodes; ++j) {
328     mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->ReferenceElement->Type->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
329     }
330     numFaceElements++;
331     }
332 gross 935 }
333 ksteube 1312 }
334 gross 935 }
335 ksteube 1312 }
336 gross 935 }
337 ksteube 1312 /* and clean up */
338     TMPMEMFREE(id);
339     TMPMEMFREE(tag);
340     TMPMEMFREE(element_type);
341     TMPMEMFREE(vertices);
342 gross 935 }
343 ksteube 1312 /* serach for end of data block */
344     do {
345     if (!fgets(line, sizeof(line), fileHandle_p)) {
346     sprintf(error_msg,"Unexected end of file in %s",fname);
347     Finley_setError(IO_ERROR,error_msg);
348     }
349     if (feof(fileHandle_p)) {
350     sprintf(error_msg,"Unexected end of file in %s",fname);
351     Finley_setError(IO_ERROR,error_msg);
352     }
353     if (! Finley_noError()) break;
354     } while(line[0] != '$');
355     }
356    
357     /* close file */
358     fclose(fileHandle_p);
359     /* clean up */
360     if (! Finley_noError()) {
361     Finley_Mesh_free(mesh_p);
362     return NULL;
363     }
364     /* resolve id's : */
365     Finley_Mesh_resolveNodeIds(mesh_p);
366     /* rearrange elements: */
367     Finley_Mesh_prepare(mesh_p, optimize);
368     /* that's it */
369     #ifdef Finley_TRACE
370     printf("timing: reading mesh: %.4e sec\n",Finley_timer()-time0);
371     #endif
372     Paso_MPIInfo_free( mpi_info );
373     return mesh_p;
374 gross 935 }
375     }

  ViewVC Help
Powered by ViewVC 1.1.26