/[escript]/branches/domexper/dudley/src/Mesh_readGmsh.c
ViewVC logotype

Contents of /branches/domexper/dudley/src/Mesh_readGmsh.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2934 - (show annotations)
Fri Feb 12 05:09:06 2010 UTC (9 years, 2 months ago) by gross
Original Path: trunk/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 18131 byte(s)
gmsh reader supports second order recs and hex now
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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 20
31
32 Finley_Mesh* Finley_Mesh_readGmsh(char* fname ,index_t numDim, index_t order, index_t reduced_order, bool_t optimize, bool_t useMacroElements) {
33
34 double version = 1.0;
35 int format = 0, size = sizeof(double), scan_ret;
36 dim_t numNodes, totalNumElements=0, numTags=0, numNodesPerElement=0, numNodesPerElement2, element_dim=0;
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 Finley_ReferenceElementSet *refPoints=NULL, *refContactElements=NULL, *refFaceElements=NULL, *refElements=NULL;
44 #ifdef Finley_TRACE
45 double time0=Finley_timer();
46 #endif
47 FILE * fileHandle_p = NULL;
48 ElementTypeId* element_type=NULL;
49
50
51 Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
52 Finley_resetError();
53 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 return NULL;
57 } else {
58
59 /* allocate mesh */
60
61 mesh_p = Finley_Mesh_alloc(fname,numDim, mpi_info);
62 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 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
88 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
89 }
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 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
96 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
97 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 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
103 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
104 &rtmp0,
105 &rtmp1);
106 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
107 } else if (2 == numDim) {
108 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
109 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
110 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
111 &rtmp0);
112 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
113 } else if (3 == numDim) {
114 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
115 &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 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
119 }
120 mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
121 mesh_p->Nodes->Tag[i0]=0;
122 }
123 }
124 /* elements */
125 else if(!strncmp(&line[1], "ELM", 3) ||
126 !strncmp(&line[1], "Elements", 8)) {
127
128 ElementTypeId final_element_type = NoRef;
129 ElementTypeId final_face_element_type = NoRef;
130 ElementTypeId contact_element_type = NoRef;
131 numElements=0;
132 numFaceElements=0;
133 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
134 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
135
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 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
146 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
147 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 if (useMacroElements) {
175 element_type[e]=Line3Macro;
176 } else {
177 element_type[e]=Line3;
178 }
179 numNodesPerElement= 3;
180 element_dim=1;
181 break;
182 case 9: /* traingle order 2 */
183 if (useMacroElements) {
184 element_type[e]=Tri6Macro;
185 } else {
186 element_type[e]=Tri6;
187 }
188 numNodesPerElement= 6;
189 element_dim=2;
190 break;
191 case 10: /* quadrilateral order 2 */
192 if (useMacroElements) {
193 element_type[e]=Rec9Macro;
194 } else {
195 element_type[e]=Rec9;
196 }
197 numNodesPerElement= 9;
198 element_dim=2;
199 break;
200 case 11: /* tetrahedron order 2 */
201 if (useMacroElements) {
202 element_type[e]=Tet10Macro;
203 } else {
204 element_type[e]=Tet10;
205 }
206 numNodesPerElement= 10;
207 element_dim=3;
208 break;
209 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 case 15 : /* point */
220 element_type[e]=Point1;
221 numNodesPerElement= 1;
222 element_dim=0;
223 break;
224 default:
225 element_type[e]=NoRef;
226 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 if (final_element_type == NoRef) {
231 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 if (final_face_element_type == NoRef) {
240 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 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
251 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
252 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 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
259 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
260 elementary_id = tag[e] = partition_id = 1;
261 numNodesPerElement2=-1;
262 for(j = 0; j < numTags; j++){
263 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
264 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
265 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 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 /* for tet10 the last two nodes need to be swapped */
281 if ((element_type[e]==Tet10) || (element_type[e]==Tet10Macro)) {
282 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 if (final_element_type == NoRef) {
292 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 }
300 if (final_face_element_type == NoRef) {
301 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 }
309 if (final_face_element_type == Line2) {
310 contact_element_type=Line2_Contact;
311 } else if ( (final_face_element_type == Line3) || (final_face_element_type == Line3Macro) ) {
312 contact_element_type=Line3_Contact;
313 } else if (final_face_element_type == Tri3) {
314 contact_element_type=Tri3_Contact;
315 } else if ( (final_face_element_type == Tri6) || (final_face_element_type == Tri6Macro)) {
316 contact_element_type=Tri6_Contact;
317 } else {
318 contact_element_type=Point1_Contact;
319 }
320 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 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 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 mesh_p->Elements->Owner[numElements]=0;
350 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 }
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 mesh_p->FaceElements->Owner[numFaceElements]=0;
359 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 }
362 numFaceElements++;
363 }
364 }
365 }
366 }
367 }
368 }
369 /* and clean up */
370 TMPMEMFREE(id);
371 TMPMEMFREE(tag);
372 TMPMEMFREE(element_type);
373 TMPMEMFREE(vertices);
374 }
375 /* 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 if (Finley_noError()) Finley_Mesh_resolveNodeIds(mesh_p);
398 /* rearrange elements: */
399 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 }
413 }

  ViewVC Help
Powered by ViewVC 1.1.26