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

  ViewVC Help
Powered by ViewVC 1.1.26