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

  ViewVC Help
Powered by ViewVC 1.1.26