/[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 3079 - (hide annotations)
Tue Aug 3 04:04:51 2010 UTC (8 years, 8 months ago) by jfenwick
Original Path: branches/domexper/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 18131 byte(s)
Some experiments on finley

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

  ViewVC Help
Powered by ViewVC 1.1.26