/[escript]/branches/doubleplusgood/dudley/src/Mesh_readGmsh.cpp
ViewVC logotype

Contents of /branches/doubleplusgood/dudley/src/Mesh_readGmsh.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 2 months ago) by jfenwick
Original Path: trunk/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 17712 byte(s)
Don't panic.
Updating copyright stamps

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 10
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 15 : /* point */
210 element_type[e]=Point1;
211 numNodesPerElement= 1;
212 element_dim=0;
213 break;
214 default:
215 element_type[e]=NoRef;
216 sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
217 Finley_setError(IO_ERROR,error_msg);
218 }
219 if (element_dim == numDim) {
220 if (final_element_type == NoRef) {
221 final_element_type = element_type[e];
222 } else if (final_element_type != element_type[e]) {
223 sprintf(error_msg,"Finley can handle a single type of internal elements only.");
224 Finley_setError(IO_ERROR,error_msg);
225 break;
226 }
227 numElements++;
228 } else if (element_dim == numDim-1) {
229 if (final_face_element_type == NoRef) {
230 final_face_element_type = element_type[e];
231 } else if (final_face_element_type != element_type[e]) {
232 sprintf(error_msg,"Finley can handle a single type of face elements only.");
233 Finley_setError(IO_ERROR,error_msg);
234 break;
235 }
236 numFaceElements++;
237 }
238
239 if(version <= 1.0){
240 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
241 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
242 partition_id = 1;
243 if (numNodesPerElement2 != numNodesPerElement) {
244 sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
245 Finley_setError(IO_ERROR,error_msg);
246 }
247 } else {
248 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
249 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
250 elementary_id = tag[e] = partition_id = 1;
251 numNodesPerElement2=-1;
252 for(j = 0; j < numTags; j++){
253 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
254 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
255 if (j == 0) {
256 tag[e] = itmp;
257 } else if (j == 1) {
258 elementary_id = itmp;
259 } else if (j == 2) {
260 partition_id = itmp;
261 }
262 /* ignore any other tags */
263 }
264 }
265 if (! Finley_noError()) break;
266 for(j = 0; j < numNodesPerElement; j++) {
267 scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
268 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
269 }
270 /* for tet10 the last two nodes need to be swapped */
271 if ((element_type[e]==Tet10) || (element_type[e]==Tet10Macro)) {
272 itmp=vertices[INDEX2(9,e,MAX_numNodes_gmsh)];
273 vertices[INDEX2(9,e,MAX_numNodes_gmsh)]=vertices[INDEX2(8,e,MAX_numNodes_gmsh)];
274 vertices[INDEX2(8,e,MAX_numNodes_gmsh)]=itmp;
275 }
276 }
277 /* all elements have been read, now we have to identify the elements for finley */
278
279 if (Finley_noError()) {
280 /* first we have to identify the elements to define Elementis and FaceElements */
281 if (final_element_type == NoRef) {
282 if (numDim==1) {
283 final_element_type=Line2;
284 } else if (numDim==2) {
285 final_element_type=Tri3;
286 } else if (numDim==3) {
287 final_element_type=Tet4;
288 }
289 }
290 if (final_face_element_type == NoRef) {
291 if (numDim==1) {
292 final_face_element_type=Point1;
293 } else if (numDim==2) {
294 final_face_element_type=Line2;
295 } else if (numDim==3) {
296 final_face_element_type=Tri3;
297 }
298 }
299 if (final_face_element_type == Line2) {
300 contact_element_type=Line2_Contact;
301 } else if ( (final_face_element_type == Line3) || (final_face_element_type == Line3Macro) ) {
302 contact_element_type=Line3_Contact;
303 } else if (final_face_element_type == Tri3) {
304 contact_element_type=Tri3_Contact;
305 } else if ( (final_face_element_type == Tri6) || (final_face_element_type == Tri6Macro)) {
306 contact_element_type=Tri6_Contact;
307 } else {
308 contact_element_type=Point1_Contact;
309 }
310 refElements= Finley_ReferenceElementSet_alloc(final_element_type,order, reduced_order);
311 refFaceElements=Finley_ReferenceElementSet_alloc(final_face_element_type,order, reduced_order);
312 refContactElements= Finley_ReferenceElementSet_alloc(contact_element_type,order, reduced_order);
313 refPoints= Finley_ReferenceElementSet_alloc(Point1,order, reduced_order);
314 mesh_p->Elements=Finley_ElementFile_alloc(refElements, mpi_info);
315 mesh_p->FaceElements=Finley_ElementFile_alloc(refFaceElements, mpi_info);
316 mesh_p->ContactElements=Finley_ElementFile_alloc(refContactElements, mpi_info);
317 mesh_p->Points=Finley_ElementFile_alloc(refPoints, mpi_info);
318 if (Finley_noError()) {
319 Finley_ElementFile_allocTable(mesh_p->Elements, numElements);
320 Finley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
321 Finley_ElementFile_allocTable(mesh_p->ContactElements, 0);
322 Finley_ElementFile_allocTable(mesh_p->Points, 0);
323 if (Finley_noError()) {
324 mesh_p->Elements->minColor=0;
325 mesh_p->Elements->maxColor=numElements-1;
326 mesh_p->FaceElements->minColor=0;
327 mesh_p->FaceElements->maxColor=numFaceElements-1;
328 mesh_p->ContactElements->minColor=0;
329 mesh_p->ContactElements->maxColor=0;
330 mesh_p->Points->minColor=0;
331 mesh_p->Points->maxColor=0;
332 numElements=0;
333 numFaceElements=0;
334 for(e = 0; e < totalNumElements; e++) {
335 if (element_type[e] == final_element_type) {
336 mesh_p->Elements->Id[numElements]=id[e];
337 mesh_p->Elements->Tag[numElements]=tag[e];
338 mesh_p->Elements->Color[numElements]=numElements;
339 mesh_p->Elements->Owner[numElements]=0;
340 for (j = 0; j< mesh_p->Elements->referenceElementSet->numNodes; ++j) {
341 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
342 }
343 numElements++;
344 } else if (element_type[e] == final_face_element_type) {
345 mesh_p->FaceElements->Id[numFaceElements]=id[e];
346 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
347 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
348 mesh_p->FaceElements->Owner[numFaceElements]=0;
349 for (j = 0; j< mesh_p->FaceElements->referenceElementSet->numNodes; ++j) {
350 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
351 }
352 numFaceElements++;
353 }
354 }
355 }
356 }
357 }
358 }
359 /* and clean up */
360 TMPMEMFREE(id);
361 TMPMEMFREE(tag);
362 TMPMEMFREE(element_type);
363 TMPMEMFREE(vertices);
364 }
365 /* serach for end of data block */
366 do {
367 if (!fgets(line, sizeof(line), fileHandle_p)) {
368 sprintf(error_msg,"Unexected end of file in %s",fname);
369 Finley_setError(IO_ERROR,error_msg);
370 }
371 if (feof(fileHandle_p)) {
372 sprintf(error_msg,"Unexected end of file in %s",fname);
373 Finley_setError(IO_ERROR,error_msg);
374 }
375 if (! Finley_noError()) break;
376 } while(line[0] != '$');
377 }
378
379 /* close file */
380 fclose(fileHandle_p);
381 /* clean up */
382 if (! Finley_noError()) {
383 Finley_Mesh_free(mesh_p);
384 return NULL;
385 }
386 /* resolve id's : */
387 if (Finley_noError()) Finley_Mesh_resolveNodeIds(mesh_p);
388 /* rearrange elements: */
389 if (Finley_noError()) Finley_Mesh_prepare(mesh_p, optimize);
390 /* free up memory */
391 Finley_ReferenceElementSet_dealloc(refPoints);
392 Finley_ReferenceElementSet_dealloc(refContactElements);
393 Finley_ReferenceElementSet_dealloc(refFaceElements);
394 Finley_ReferenceElementSet_dealloc(refElements);
395 Paso_MPIInfo_free( mpi_info );
396 if (! Finley_noError()) {
397 Finley_Mesh_free(mesh_p);
398 return NULL;
399 } else {
400 return mesh_p;
401 }
402 }
403 }

  ViewVC Help
Powered by ViewVC 1.1.26