/[escript]/trunk/finley/src/Mesh_readGmsh.c
ViewVC logotype

Annotation of /trunk/finley/src/Mesh_readGmsh.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 997 - (hide annotations)
Mon Feb 26 06:31:45 2007 UTC (13 years, 8 months ago) by gross
File MIME type: text/plain
File size: 13136 byte(s)
small small fixes in modellib
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 gross 997
55 gross 935 mesh_p = Finley_Mesh_alloc(fname,numDim,order);
56     if (! Finley_noError()) return NULL;
57    
58     /* get file handle */
59     FILE * fileHandle_p = fopen(fname, "r");
60     if (fileHandle_p==NULL) {
61     sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
62     Finley_setError(IO_ERROR,error_msg);
63     return NULL;
64     }
65    
66     /* start reading */
67     while(1) {
68     if (! Finley_noError()) break;
69     /* find line staring with $ */
70     do {
71     if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
72     if(feof(fileHandle_p)) break;
73     } while(line[0] != '$');
74    
75     if (feof(fileHandle_p)) break;
76    
77    
78     /* format */
79     if (!strncmp(&line[1], "MeshFormat", 10)) {
80     fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
81     }
82     /* nodes are read */
83     if ( !strncmp(&line[1], "NOD", 3) ||
84     !strncmp(&line[1], "NOE", 3) ||
85     !strncmp(&line[1], "Nodes", 5) ) {
86    
87     fscanf(fileHandle_p, "%d", &numNodes);
88     if (! Finley_noError()) break;
89     Finley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
90     if (! Finley_noError()) break;
91     for (i0 = 0; i0 < numNodes; i0++) {
92     if (1 == numDim) {
93     fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
94     &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
95     &rtmp0,
96     &rtmp1);
97     } else if (2 == numDim) {
98     fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
99     &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
100     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
101     &rtmp0);
102     } else if (3 == numDim) {
103     fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
104     &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
105     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
106     &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
107    
108    
109     }
110     mesh_p->Nodes->degreeOfFreedom[i0]=mesh_p->Nodes->Id[i0];
111     mesh_p->Nodes->Tag[i0]=0;
112     }
113     }
114     /* elements */
115     else if(!strncmp(&line[1], "ELM", 3) ||
116     !strncmp(&line[1], "Elements", 8)) {
117    
118     ElementTypeId final_element_type = NoType;
119     ElementTypeId final_face_element_type = NoType;
120     index_t numElements=0;
121     index_t numFaceElements=0;
122     fscanf(fileHandle_p, "%d", &totalNumElements);
123    
124     index_t* id=TMPMEMALLOC(totalNumElements,index_t);
125     index_t* tag=TMPMEMALLOC(totalNumElements,index_t);
126     ElementTypeId* element_type=TMPMEMALLOC(totalNumElements,ElementTypeId);
127     index_t* vertices=TMPMEMALLOC(totalNumElements*MAX_numNodes_gmsh,index_t);
128     if (! (Finley_checkPtr(id) || Finley_checkPtr(tag) || Finley_checkPtr(element_type) || Finley_checkPtr(vertices) ) ) {
129     /* read all in */
130     for(e = 0; e < totalNumElements; e++) {
131     fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
132     switch (gmsh_type) {
133     case 1: /* line order 1 */
134     element_type[e]=Line2;
135     element_dim=1;
136     numNodesPerElement=2;
137     break;
138     case 2: /* traingle order 1 */
139     element_type[e]=Tri3;
140     numNodesPerElement= 3;
141     element_dim=2;
142     break;
143     case 3: /* quadrilateral order 1 */
144     element_type[e]=Rec4;
145     numNodesPerElement= 4;
146     element_dim=2;
147     break;
148     case 4: /* tetrahedron order 1 */
149     element_type[e]=Tet4;
150     numNodesPerElement= 4;
151     element_dim=3;
152     break;
153     case 5: /* hexahedron order 1 */
154     element_type[e]=Hex8;
155     numNodesPerElement= 8;
156     element_dim=3;
157     break;
158     case 8: /* line order 2 */
159     element_type[e]=Line3;
160     numNodesPerElement= 3;
161     element_dim=1;
162     break;
163     case 9: /* traingle order 2 */
164     element_type[e]=Tri6;
165     numNodesPerElement= 6;
166     element_dim=2;
167     break;
168     case 10: /* quadrilateral order 2 */
169     element_type[e]=Rec9;
170     numNodesPerElement= 9;
171     element_dim=2;
172     break;
173     case 11: /* tetrahedron order 2 */
174     element_type[e]=Tet10;
175     numNodesPerElement= 10;
176     element_dim=3;
177     break;
178     case 15 : /* point */
179     element_type[e]=Point1;
180     numNodesPerElement= 1;
181     element_dim=0;
182     break;
183     default:
184     element_type[e]=NoType;
185     sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
186     Finley_setError(IO_ERROR,error_msg);
187     }
188     if (element_dim == numDim) {
189     if (final_element_type == NoType) {
190     final_element_type = element_type[e];
191     } else if (final_element_type != element_type[e]) {
192     sprintf(error_msg,"Finley can handle a single type of internal elements only.");
193     Finley_setError(IO_ERROR,error_msg);
194     break;
195     }
196     numElements++;
197     } else if (element_dim == numDim-1) {
198     if (final_face_element_type == NoType) {
199     final_face_element_type = element_type[e];
200     } else if (final_face_element_type != element_type[e]) {
201     sprintf(error_msg,"Finley can handle a single type of face elements only.");
202     Finley_setError(IO_ERROR,error_msg);
203     break;
204     }
205     numFaceElements++;
206     }
207    
208     if(version <= 1.0){
209     fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
210     partition_id = 1;
211     if (numNodesPerElement2 != numNodesPerElement) {
212     sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
213     Finley_setError(IO_ERROR,error_msg);
214     }
215     } else {
216     fscanf(fileHandle_p, "%d", &numTags);
217     elementary_id = tag[e] = partition_id = 1;
218     numNodesPerElement2=-1;
219     for(j = 0; j < numTags; j++){
220     fscanf(fileHandle_p, "%d", &itmp);
221     if (j == 0) {
222     tag[e] = itmp;
223     } else if (j == 1) {
224     elementary_id = itmp;
225     } else if (j == 2) {
226     partition_id = itmp;
227     }
228     // ignore any other tags
229     }
230     }
231     if (! Finley_noError()) break;
232     for(j = 0; j < numNodesPerElement; j++) fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
233     }
234     /* all elements have been read, now we have to identify the elements for finley */
235 gross 997
236 gross 935 if (Finley_noError()) {
237     /* first we have to identify the elements to define Elementis and FaceElements */
238     mesh_p->Elements=Finley_ElementFile_alloc(final_element_type,mesh_p->order);
239     mesh_p->FaceElements=Finley_ElementFile_alloc(final_face_element_type,mesh_p->order);
240     mesh_p->ContactElements=Finley_ElementFile_alloc(Point1_Contact,mesh_p->order);
241     mesh_p->Points=Finley_ElementFile_alloc(Point1,mesh_p->order);
242     if (Finley_noError()) {
243     Finley_ElementFile_allocTable(mesh_p->Elements, numElements);
244     Finley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
245     Finley_ElementFile_allocTable(mesh_p->ContactElements, 0);
246     Finley_ElementFile_allocTable(mesh_p->Points, 0);
247     if (Finley_noError()) {
248     mesh_p->Elements->minColor=0;
249     mesh_p->Elements->maxColor=numElements-1;
250     mesh_p->FaceElements->minColor=0;
251     mesh_p->FaceElements->maxColor=numFaceElements-1;
252     mesh_p->ContactElements->minColor=0;
253     mesh_p->ContactElements->maxColor=0;
254     mesh_p->Points->minColor=0;
255     mesh_p->Points->maxColor=0;
256     numElements=0;
257     numFaceElements=0;
258     for(e = 0; e < totalNumElements; e++) {
259     if (element_type[e] == final_element_type) {
260     mesh_p->Elements->Id[numElements]=id[e];
261     mesh_p->Elements->Tag[numElements]=tag[e];
262     mesh_p->Elements->Color[numElements]=numElements;
263     for (j = 0; j< mesh_p->Elements->ReferenceElement->Type->numNodes; ++j)
264     mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->ReferenceElement->Type->numNodes)]=
265     vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
266     numElements++;
267     } else if (element_type[e] == final_face_element_type) {
268     mesh_p->FaceElements->Id[numFaceElements]=id[e];
269     mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
270     mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
271     for (j = 0; j< mesh_p->FaceElements->ReferenceElement->Type->numNodes; ++j)
272     mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->ReferenceElement->Type->numNodes)]=
273     vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
274     numFaceElements++;
275     }
276     }
277     }
278     }
279     }
280     }
281     /* and clean up */
282     TMPMEMFREE(id);
283     TMPMEMFREE(tag);
284     TMPMEMFREE(element_type);
285     TMPMEMFREE(vertices);
286     }
287     /* serach for end of data block */
288     do {
289     if (!fgets(line, sizeof(line), fileHandle_p)) {
290     sprintf(error_msg,"Unexected end of file in %s",fname);
291     Finley_setError(IO_ERROR,error_msg);
292     }
293     if (feof(fileHandle_p)) {
294 gross 940 sprintf(error_msg,"Unexected end of file in %s",fname);
295 gross 935 Finley_setError(IO_ERROR,error_msg);
296     }
297     if (! Finley_noError()) break;
298     } while(line[0] != '$');
299     }
300    
301     /* close file */
302     fclose(fileHandle_p);
303     /* clean up */
304     if (! Finley_noError()) {
305     Finley_Mesh_dealloc(mesh_p);
306     return NULL;
307     }
308     /* resolve id's : */
309     Finley_Mesh_resolveNodeIds(mesh_p);
310     /* rearrange elements: */
311     Finley_Mesh_prepare(mesh_p);
312     /* that's it */
313     #ifdef Finley_TRACE
314     printf("timing: reading mesh: %.4e sec\n",Finley_timer()-time0);
315     #endif
316     #endif
317 gross 964 if (Finley_noError()) {
318     if ( ! Finley_Mesh_isPrepared(mesh_p)) {
319     Finley_setError(SYSTEM_ERROR,"Mesh is not prepared for calculation. Contact the programmers.");
320     }
321     }
322 gross 935 return mesh_p;
323     }

  ViewVC Help
Powered by ViewVC 1.1.26