/[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 3221 - (hide annotations)
Wed Sep 29 01:00:21 2010 UTC (8 years, 6 months ago) by jfenwick
Original Path: branches/domexper/dudley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 13783 byte(s)
Comment stripping

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 jfenwick 3086 /* Dudley: read mesh */
18 gross 935
19     /**************************************************************/
20    
21     #include "Mesh.h"
22     #include <stdio.h>
23    
24 jfenwick 3086 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); Dudley_setError(IO_ERROR,"scan error while reading dudley file"); return NULL;} }
25 gross 935
26     /**************************************************************/
27    
28 jfenwick 3086 /* reads a mesh from a Dudley file of name fname */
29 gross 935
30 gross 2934 #define MAX_numNodes_gmsh 20
31 gross 935
32 jfenwick 3086 Dudley_Mesh* Dudley_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 jfenwick 3086 Dudley_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 jfenwick 3086 #ifdef Dudley_TRACE
44     double time0=Dudley_timer();
45 jfenwick 1986 #endif
46 gross 1028 FILE * fileHandle_p = NULL;
47     ElementTypeId* element_type=NULL;
48 gross 935
49 gross 2722
50 ksteube 1312 Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
51 jfenwick 3086 Dudley_resetError();
52 ksteube 1312 if (mpi_info->size>1) {
53 jfenwick 3086 Dudley_setError(IO_ERROR,"reading GMSH with MPI is not supported yet.");
54 ksteube 1312 Paso_MPIInfo_free( mpi_info );
55 gross 935 return NULL;
56 ksteube 1312 } else {
57 gross 997
58 ksteube 1312 /* allocate mesh */
59 gross 935
60 jfenwick 3086 mesh_p = Dudley_Mesh_alloc(fname,numDim, mpi_info);
61     if (! Dudley_noError()) return NULL;
62 ksteube 1312
63     /* get file handle */
64     fileHandle_p = fopen(fname, "r");
65     if (fileHandle_p==NULL) {
66     sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
67 jfenwick 3086 Dudley_setError(IO_ERROR,error_msg);
68 ksteube 1312 Paso_MPIInfo_free( mpi_info );
69     return NULL;
70     }
71    
72     /* start reading */
73     while(1) {
74 jfenwick 3086 if (! Dudley_noError()) break;
75 ksteube 1312 /* find line staring with $ */
76     do {
77     if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
78     if(feof(fileHandle_p)) break;
79     } while(line[0] != '$');
80    
81     if (feof(fileHandle_p)) break;
82    
83    
84     /* format */
85     if (!strncmp(&line[1], "MeshFormat", 10)) {
86 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
87 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
88 ksteube 1312 }
89     /* nodes are read */
90     if ( !strncmp(&line[1], "NOD", 3) ||
91     !strncmp(&line[1], "NOE", 3) ||
92     !strncmp(&line[1], "Nodes", 5) ) {
93    
94 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
95 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
96     if (! Dudley_noError()) break;
97     Dudley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
98     if (! Dudley_noError()) break;
99 ksteube 1312 for (i0 = 0; i0 < numNodes; i0++) {
100     if (1 == numDim) {
101 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
102 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
103     &rtmp0,
104     &rtmp1);
105 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
106 ksteube 1312 } else if (2 == numDim) {
107 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
108 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
109     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
110     &rtmp0);
111 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
112 ksteube 1312 } else if (3 == numDim) {
113 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
114 ksteube 1312 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
115     &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
116     &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
117 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
118 ksteube 1312 }
119     mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
120     mesh_p->Nodes->Tag[i0]=0;
121 gross 935 }
122 ksteube 1312 }
123     /* elements */
124     else if(!strncmp(&line[1], "ELM", 3) ||
125     !strncmp(&line[1], "Elements", 8)) {
126    
127 gross 2748 ElementTypeId final_element_type = NoRef;
128     ElementTypeId final_face_element_type = NoRef;
129 ksteube 1312 numElements=0;
130     numFaceElements=0;
131 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
132 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
133 ksteube 1312
134     id=TMPMEMALLOC(totalNumElements,index_t);
135     tag=TMPMEMALLOC(totalNumElements,index_t);
136    
137    
138     element_type=TMPMEMALLOC(totalNumElements,ElementTypeId);
139     vertices=TMPMEMALLOC(totalNumElements*MAX_numNodes_gmsh,index_t);
140 jfenwick 3086 if (! (Dudley_checkPtr(id) || Dudley_checkPtr(tag) || Dudley_checkPtr(element_type) || Dudley_checkPtr(vertices) ) ) {
141 ksteube 1312 /* read all in */
142     for(e = 0; e < totalNumElements; e++) {
143 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
144 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
145 ksteube 1312 switch (gmsh_type) {
146     case 1: /* line order 1 */
147     element_type[e]=Line2;
148     element_dim=1;
149     numNodesPerElement=2;
150     break;
151     case 2: /* traingle order 1 */
152     element_type[e]=Tri3;
153     numNodesPerElement= 3;
154     element_dim=2;
155     break;
156     case 4: /* tetrahedron order 1 */
157     element_type[e]=Tet4;
158     numNodesPerElement= 4;
159     element_dim=3;
160     break;
161     case 15 : /* point */
162     element_type[e]=Point1;
163     numNodesPerElement= 1;
164     element_dim=0;
165     break;
166     default:
167 gross 2748 element_type[e]=NoRef;
168 ksteube 1312 sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
169 jfenwick 3086 Dudley_setError(IO_ERROR,error_msg);
170 ksteube 1312 }
171     if (element_dim == numDim) {
172 gross 2748 if (final_element_type == NoRef) {
173 ksteube 1312 final_element_type = element_type[e];
174     } else if (final_element_type != element_type[e]) {
175 jfenwick 3086 sprintf(error_msg,"Dudley can handle a single type of internal elements only.");
176     Dudley_setError(IO_ERROR,error_msg);
177 ksteube 1312 break;
178     }
179     numElements++;
180     } else if (element_dim == numDim-1) {
181 gross 2748 if (final_face_element_type == NoRef) {
182 ksteube 1312 final_face_element_type = element_type[e];
183     } else if (final_face_element_type != element_type[e]) {
184 jfenwick 3086 sprintf(error_msg,"Dudley can handle a single type of face elements only.");
185     Dudley_setError(IO_ERROR,error_msg);
186 ksteube 1312 break;
187     }
188     numFaceElements++;
189     }
190    
191     if(version <= 1.0){
192 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
193 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
194 ksteube 1312 partition_id = 1;
195     if (numNodesPerElement2 != numNodesPerElement) {
196     sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
197 jfenwick 3086 Dudley_setError(IO_ERROR,error_msg);
198 ksteube 1312 }
199     } else {
200 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
201 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
202 ksteube 1312 elementary_id = tag[e] = partition_id = 1;
203     numNodesPerElement2=-1;
204     for(j = 0; j < numTags; j++){
205 ksteube 1887 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
206 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
207 ksteube 1312 if (j == 0) {
208     tag[e] = itmp;
209     } else if (j == 1) {
210     elementary_id = itmp;
211     } else if (j == 2) {
212     partition_id = itmp;
213     }
214     /* ignore any other tags */
215     }
216     }
217 jfenwick 3086 if (! Dudley_noError()) break;
218 ksteube 1887 for(j = 0; j < numNodesPerElement; j++) {
219     scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
220 jfenwick 3086 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
221 ksteube 1887 }
222 ksteube 1312 }
223 jfenwick 3086 /* all elements have been read, now we have to identify the elements for dudley */
224 ksteube 1312
225 jfenwick 3086 if (Dudley_noError()) {
226 ksteube 1312 /* first we have to identify the elements to define Elementis and FaceElements */
227 gross 2748 if (final_element_type == NoRef) {
228 ksteube 1312 if (numDim==1) {
229     final_element_type=Line2;
230     } else if (numDim==2) {
231     final_element_type=Tri3;
232     } else if (numDim==3) {
233     final_element_type=Tet4;
234     }
235 gross 935 }
236 gross 2748 if (final_face_element_type == NoRef) {
237 ksteube 1312 if (numDim==1) {
238     final_face_element_type=Point1;
239     } else if (numDim==2) {
240     final_face_element_type=Line2;
241     } else if (numDim==3) {
242     final_face_element_type=Tri3;
243     }
244 gross 935 }
245 jfenwick 3216 mesh_p->Elements=Dudley_ElementFile_alloc(final_element_type, mpi_info);
246     mesh_p->FaceElements=Dudley_ElementFile_alloc(final_face_element_type, mpi_info);
247     mesh_p->Points=Dudley_ElementFile_alloc(Point1, mpi_info);
248 jfenwick 3086 if (Dudley_noError()) {
249     Dudley_ElementFile_allocTable(mesh_p->Elements, numElements);
250     Dudley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
251     Dudley_ElementFile_allocTable(mesh_p->Points, 0);
252     if (Dudley_noError()) {
253 ksteube 1312 mesh_p->Elements->minColor=0;
254     mesh_p->Elements->maxColor=numElements-1;
255     mesh_p->FaceElements->minColor=0;
256     mesh_p->FaceElements->maxColor=numFaceElements-1;
257     mesh_p->Points->minColor=0;
258     mesh_p->Points->maxColor=0;
259     numElements=0;
260     numFaceElements=0;
261     for(e = 0; e < totalNumElements; e++) {
262     if (element_type[e] == final_element_type) {
263     mesh_p->Elements->Id[numElements]=id[e];
264     mesh_p->Elements->Tag[numElements]=tag[e];
265     mesh_p->Elements->Color[numElements]=numElements;
266 gross 1740 mesh_p->Elements->Owner[numElements]=0;
267 jfenwick 3216 for (j = 0; j< mesh_p->Elements->/*referenceElementSet->*/numNodes; ++j) {
268     mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->/*referenceElementSet->*/numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
269 ksteube 1312 }
270     numElements++;
271     } else if (element_type[e] == final_face_element_type) {
272     mesh_p->FaceElements->Id[numFaceElements]=id[e];
273     mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
274     mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
275 gross 2243 mesh_p->FaceElements->Owner[numFaceElements]=0;
276 jfenwick 3216 for (j = 0; j< mesh_p->FaceElements->/*referenceElementSet->*/numNodes; ++j) {
277     mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->/*referenceElementSet->*/numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
278 ksteube 1312 }
279     numFaceElements++;
280     }
281 gross 935 }
282 ksteube 1312 }
283 gross 935 }
284 ksteube 1312 }
285 gross 935 }
286 ksteube 1312 /* and clean up */
287     TMPMEMFREE(id);
288     TMPMEMFREE(tag);
289     TMPMEMFREE(element_type);
290     TMPMEMFREE(vertices);
291 gross 935 }
292 ksteube 1312 /* serach for end of data block */
293     do {
294     if (!fgets(line, sizeof(line), fileHandle_p)) {
295     sprintf(error_msg,"Unexected end of file in %s",fname);
296 jfenwick 3086 Dudley_setError(IO_ERROR,error_msg);
297 ksteube 1312 }
298     if (feof(fileHandle_p)) {
299     sprintf(error_msg,"Unexected end of file in %s",fname);
300 jfenwick 3086 Dudley_setError(IO_ERROR,error_msg);
301 ksteube 1312 }
302 jfenwick 3086 if (! Dudley_noError()) break;
303 ksteube 1312 } while(line[0] != '$');
304     }
305    
306     /* close file */
307     fclose(fileHandle_p);
308     /* clean up */
309 jfenwick 3086 if (! Dudley_noError()) {
310     Dudley_Mesh_free(mesh_p);
311 ksteube 1312 return NULL;
312     }
313     /* resolve id's : */
314 jfenwick 3086 if (Dudley_noError()) Dudley_Mesh_resolveNodeIds(mesh_p);
315 ksteube 1312 /* rearrange elements: */
316 jfenwick 3086 if (Dudley_noError()) Dudley_Mesh_prepare(mesh_p, optimize);
317 gross 2748 /* free up memory */
318     Paso_MPIInfo_free( mpi_info );
319 jfenwick 3086 if (! Dudley_noError()) {
320     Dudley_Mesh_free(mesh_p);
321 gross 2748 return NULL;
322     } else {
323     return mesh_p;
324     }
325 gross 935 }
326     }

  ViewVC Help
Powered by ViewVC 1.1.26