/[escript]/branches/domexper/dudley/src/Mesh_readGmsh.c
ViewVC logotype

Annotation of /branches/domexper/dudley/src/Mesh_readGmsh.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (hide annotations)
Thu Sep 25 23:11:13 2008 UTC (10 years, 6 months ago) by ksteube
Original Path: trunk/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 14778 byte(s)
Copyright updated in all files

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

  ViewVC Help
Powered by ViewVC 1.1.26