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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26