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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4492 - (show annotations)
Tue Jul 2 01:44:11 2013 UTC (5 years, 9 months ago) by caltinay
File size: 19046 byte(s)
Finley changes that were held back while in release mode - moved more stuff
into finley namespace.

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15
16
17 /************************************************************************************/
18
19 /* Finley: read mesh from gmsh file */
20
21 /************************************************************************************/
22
23 #include "Mesh.h"
24 #include <stdio.h>
25
26 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); Finley_setError(IO_ERROR,"scan error while reading finley file"); return NULL;} }
27
28 /************************************************************************************/
29
30 /* reads a mesh from a Finley file of name fname */
31
32 #define MAX_numNodes_gmsh 20
33
34 Finley_Mesh* Finley_Mesh_readGmsh(char* fname ,index_t numDim, index_t order, index_t reduced_order, bool_t optimize, bool_t useMacroElements) {
35
36 double version = 1.0;
37 int format = 0, size = sizeof(double), scan_ret;
38 dim_t numNodes, totalNumElements=0, numTags=0, numNodesPerElement=0, numNodesPerElement2, element_dim=0;
39 index_t e, i0, j, gmsh_type, partition_id, itmp, elementary_id, tag_key;
40 index_t numElements=0, numFaceElements=0, *id=NULL, *tag=NULL, *vertices=NULL;
41 Finley_Mesh *mesh_p=NULL;
42 char line[LenString_MAX+1], name[LenString_MAX+1];
43 char error_msg[LenErrorMsg_MAX];
44 double rtmp0, rtmp1;
45 ReferenceElementSet *refPoints=NULL, *refContactElements=NULL, *refFaceElements=NULL, *refElements=NULL;
46 #ifdef Finley_TRACE
47 double time0=Finley_timer();
48 #endif
49 FILE * fileHandle_p = NULL;
50 ElementTypeId* element_type=NULL;
51
52
53 Esys_MPIInfo *mpi_info = Esys_MPIInfo_alloc( MPI_COMM_WORLD );
54 Finley_resetError();
55 if (mpi_info->size>1) {
56 Finley_setError(IO_ERROR,"reading GMSH with MPI is not supported yet.");
57 Esys_MPIInfo_free( mpi_info );
58 return NULL;
59 } else {
60
61 /* allocate mesh */
62
63 mesh_p = Finley_Mesh_alloc(fname,numDim, mpi_info);
64 if (! Finley_noError()) return NULL;
65
66 /* get file handle */
67 fileHandle_p = fopen(fname, "r");
68 if (fileHandle_p==NULL) {
69 sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
70 Finley_setError(IO_ERROR,error_msg);
71 Esys_MPIInfo_free( mpi_info );
72 return NULL;
73 }
74 /* start reading */
75 while(1) {
76 if (! Finley_noError()) break;
77 /* find line staring with $ */
78 do {
79 if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
80 if(feof(fileHandle_p)) break;
81 } while(line[0] != '$');
82
83 if (feof(fileHandle_p)) break;
84
85
86 /* format */
87 if (!strncmp(&line[1], "MeshFormat", 10)) {
88 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
89 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
90 }
91 /* nodes are read */
92 if ( !strncmp(&line[1], "NOD", 3) ||
93 !strncmp(&line[1], "NOE", 3) ||
94 !strncmp(&line[1], "Nodes", 5) ) {
95
96 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
97 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
98 if (! Finley_noError()) break;
99 mesh_p->Nodes->allocTable(numNodes);
100 if (! Finley_noError()) break;
101 for (i0 = 0; i0 < numNodes; i0++) {
102 if (1 == numDim) {
103 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
104 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
105 &rtmp0,
106 &rtmp1);
107 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
108 } else if (2 == numDim) {
109 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
110 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
111 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
112 &rtmp0);
113 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
114 } else if (3 == numDim) {
115 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
116 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
117 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
118 &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
119 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
120 }
121 mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
122 mesh_p->Nodes->Tag[i0]=0;
123 }
124 }
125 /* elements */
126 else if(!strncmp(&line[1], "ELM", 3) ||
127 !strncmp(&line[1], "Elements", 8)) {
128
129 ElementTypeId final_element_type = NoRef;
130 ElementTypeId final_face_element_type = NoRef;
131 ElementTypeId contact_element_type = NoRef;
132 numElements=0;
133 numFaceElements=0;
134 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
135 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
136
137 id=new index_t[totalNumElements];
138 tag=new index_t[totalNumElements];
139
140
141 element_type=new ElementTypeId[totalNumElements];
142 vertices=new index_t[totalNumElements*MAX_numNodes_gmsh];
143 if (! (Finley_checkPtr(id) || Finley_checkPtr(tag) || Finley_checkPtr(element_type) || Finley_checkPtr(vertices) ) ) {
144 /* read all in */
145 for(e = 0; e < totalNumElements; e++) {
146 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
147 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
148 switch (gmsh_type) {
149 case 1: /* line order 1 */
150 element_type[e]=Line2;
151 element_dim=1;
152 numNodesPerElement=2;
153 break;
154 case 2: /* triangle order 1 */
155 element_type[e]=Tri3;
156 numNodesPerElement= 3;
157 element_dim=2;
158 break;
159 case 3: /* quadrilateral order 1 */
160 element_type[e]=Rec4;
161 numNodesPerElement= 4;
162 element_dim=2;
163 break;
164 case 4: /* tetrahedron order 1 */
165 element_type[e]=Tet4;
166 numNodesPerElement= 4;
167 element_dim=3;
168 break;
169 case 5: /* hexahedron order 1 */
170 element_type[e]=Hex8;
171 numNodesPerElement= 8;
172 element_dim=3;
173 break;
174 case 8: /* line order 2 */
175 if (useMacroElements) {
176 element_type[e]=Line3Macro;
177 } else {
178 element_type[e]=Line3;
179 }
180 numNodesPerElement= 3;
181 element_dim=1;
182 break;
183 case 9: /* triangle order 2 */
184 if (useMacroElements) {
185 element_type[e]=Tri6Macro;
186 } else {
187 element_type[e]=Tri6;
188 }
189 numNodesPerElement= 6;
190 element_dim=2;
191 break;
192 case 10: /* quadrilateral order 2 */
193 if (useMacroElements) {
194 element_type[e]=Rec9Macro;
195 } else {
196 element_type[e]=Rec9;
197 }
198 numNodesPerElement= 9;
199 element_dim=2;
200 break;
201 case 11: /* tetrahedron order 2 */
202 if (useMacroElements) {
203 element_type[e]=Tet10Macro;
204 } else {
205 element_type[e]=Tet10;
206 }
207 numNodesPerElement= 10;
208 element_dim=3;
209 break;
210 case 16: /* rectangular order 2 */
211 element_type[e]=Rec8;
212 numNodesPerElement= 8;
213 element_dim=2;
214 break;
215 case 17: /* hexahedron order 2 */
216 element_type[e]=Hex20;
217 numNodesPerElement= 20;
218 element_dim=3;
219 break;
220 case 15 : /* point */
221 element_type[e]=Point1;
222 numNodesPerElement= 1;
223 element_dim=0;
224 break;
225 default:
226 element_type[e]=NoRef;
227 sprintf(error_msg,"Unexpected gmsh element type %d in mesh file %s.",gmsh_type,fname);
228 Finley_setError(IO_ERROR,error_msg);
229 }
230 if (element_dim == numDim) {
231 if (final_element_type == NoRef) {
232 final_element_type = element_type[e];
233 } else if (final_element_type != element_type[e]) {
234 sprintf(error_msg,"Finley can handle a single type of internal elements only.");
235 Finley_setError(IO_ERROR,error_msg);
236 break;
237 }
238 numElements++;
239 } else if (element_dim == numDim-1) {
240 if (final_face_element_type == NoRef) {
241 final_face_element_type = element_type[e];
242 } else if (final_face_element_type != element_type[e]) {
243 sprintf(error_msg,"Finley can handle a single type of face elements only.");
244 Finley_setError(IO_ERROR,error_msg);
245 break;
246 }
247 numFaceElements++;
248 }
249
250 if(version <= 1.0){
251 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
252 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
253 partition_id = 1;
254 if (numNodesPerElement2 != numNodesPerElement) {
255 sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
256 Finley_setError(IO_ERROR,error_msg);
257 }
258 } else {
259 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
260 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
261 elementary_id = tag[e] = partition_id = 1;
262 numNodesPerElement2=-1;
263 for(j = 0; j < numTags; j++){
264 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
265 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
266 if (j == 0) {
267 tag[e] = itmp;
268 } else if (j == 1) {
269 elementary_id = itmp;
270 } else if (j == 2) {
271 partition_id = itmp;
272 }
273 /* ignore any other tags */
274 }
275 }
276 if (! Finley_noError()) break;
277 for(j = 0; j < numNodesPerElement; j++) {
278 scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
279 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
280 }
281 /* for tet10 the last two nodes need to be swapped */
282 if ((element_type[e]==Tet10) || (element_type[e]==Tet10Macro)) {
283 itmp=vertices[INDEX2(9,e,MAX_numNodes_gmsh)];
284 vertices[INDEX2(9,e,MAX_numNodes_gmsh)]=vertices[INDEX2(8,e,MAX_numNodes_gmsh)];
285 vertices[INDEX2(8,e,MAX_numNodes_gmsh)]=itmp;
286 }
287 }
288 /* all elements have been read, now we have to identify the elements for finley */
289
290 if (Finley_noError()) {
291 /* first we have to identify the elements to define Elements and FaceElements */
292 if (final_element_type == NoRef) {
293 if (numDim==1) {
294 final_element_type=Line2;
295 } else if (numDim==2) {
296 final_element_type=Tri3;
297 } else if (numDim==3) {
298 final_element_type=Tet4;
299 }
300 }
301 if (final_face_element_type == NoRef) {
302 if (numDim==1) {
303 final_face_element_type=Point1;
304 } else if (numDim==2) {
305 final_face_element_type=Line2;
306 } else if (numDim==3) {
307 final_face_element_type=Tri3;
308 }
309 }
310 if (final_face_element_type == Line2) {
311 contact_element_type=Line2_Contact;
312 } else if ( (final_face_element_type == Line3) || (final_face_element_type == Line3Macro) ) {
313 contact_element_type=Line3_Contact;
314 } else if (final_face_element_type == Tri3) {
315 contact_element_type=Tri3_Contact;
316 } else if ( (final_face_element_type == Tri6) || (final_face_element_type == Tri6Macro)) {
317 contact_element_type=Tri6_Contact;
318 } else {
319 contact_element_type=Point1_Contact;
320 }
321 refElements= ReferenceElementSet_alloc(final_element_type,order, reduced_order);
322 refFaceElements=ReferenceElementSet_alloc(final_face_element_type,order, reduced_order);
323 refContactElements= ReferenceElementSet_alloc(contact_element_type,order, reduced_order);
324 refPoints= ReferenceElementSet_alloc(Point1,order, reduced_order);
325 mesh_p->Elements=new ElementFile(refElements, mpi_info);
326 mesh_p->FaceElements=new ElementFile(refFaceElements, mpi_info);
327 mesh_p->ContactElements=new ElementFile(refContactElements, mpi_info);
328 mesh_p->Points=new ElementFile(refPoints, mpi_info);
329 if (Finley_noError()) {
330 mesh_p->Elements->allocTable(numElements);
331 mesh_p->FaceElements->allocTable(numFaceElements);
332 mesh_p->ContactElements->allocTable(0);
333 mesh_p->Points->allocTable(0);
334 if (Finley_noError()) {
335 mesh_p->Elements->minColor=0;
336 mesh_p->Elements->maxColor=numElements-1;
337 mesh_p->FaceElements->minColor=0;
338 mesh_p->FaceElements->maxColor=numFaceElements-1;
339 mesh_p->ContactElements->minColor=0;
340 mesh_p->ContactElements->maxColor=0;
341 mesh_p->Points->minColor=0;
342 mesh_p->Points->maxColor=0;
343 numElements=0;
344 numFaceElements=0;
345 for(e = 0; e < totalNumElements; e++) {
346 if (element_type[e] == final_element_type) {
347 mesh_p->Elements->Id[numElements]=id[e];
348 mesh_p->Elements->Tag[numElements]=tag[e];
349 mesh_p->Elements->Color[numElements]=numElements;
350 mesh_p->Elements->Owner[numElements]=0;
351 for (j = 0; j< mesh_p->Elements->referenceElementSet->numNodes; ++j) {
352 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
353 }
354 numElements++;
355 } else if (element_type[e] == final_face_element_type) {
356 mesh_p->FaceElements->Id[numFaceElements]=id[e];
357 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
358 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
359 mesh_p->FaceElements->Owner[numFaceElements]=0;
360 for (j = 0; j< mesh_p->FaceElements->referenceElementSet->numNodes; ++j) {
361 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
362 }
363 numFaceElements++;
364 }
365 }
366 }
367 }
368 }
369 }
370 /* and clean up */
371 delete[] id;
372 delete[] tag;
373 delete[] element_type;
374 delete[] vertices;
375 }
376 /* name tags (thanks to Antoine Lefebvre, antoine.lefebvre2@mail.mcgill.ca ) */
377 else if (!strncmp(&line[1], "PhysicalNames", 13)) {
378 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
379 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
380 if (! Finley_noError()) break;
381 for (i0 = 0; i0 < numTags; i0++) {
382 scan_ret = fscanf(fileHandle_p, "%d %d %s\n", &itmp, &tag_key, name);
383 FSCANF_CHECK(scan_ret, "fscanf: Finley_Mesh_readGmsh");
384 if (! (itmp == 2)) Finley_setError(IO_ERROR,"Finley_Mesh_readGmsh: expecting two entries per physical name.");
385 if ( strlen(name) < 3 ) Finley_setError(IO_ERROR,"Finley_Mesh_readGmsh: illegal tagname (\" missing?)");
386 if (! Finley_noError()) break;
387 name[strlen(name)-1]='\0';
388 Finley_Mesh_addTagMap(mesh_p,&name[1],tag_key);
389 }
390 }
391
392 /* search for end of data block */
393 do {
394 if (!fgets(line, sizeof(line), fileHandle_p)) {
395 sprintf(error_msg,"Unexpected end of file in %s",fname);
396 Finley_setError(IO_ERROR,error_msg);
397 }
398 if (feof(fileHandle_p)) {
399 sprintf(error_msg,"Unexpected end of file in %s",fname);
400 Finley_setError(IO_ERROR,error_msg);
401 }
402 if (! Finley_noError()) break;
403 } while(line[0] != '$');
404
405 } /* end of read */
406
407 /* close file */
408 fclose(fileHandle_p);
409 /* clean up */
410 if (! Finley_noError()) {
411 Finley_Mesh_free(mesh_p);
412 return NULL;
413 }
414 /* resolve id's : */
415 if (Finley_noError()) Finley_Mesh_resolveNodeIds(mesh_p);
416 /* rearrange elements: */
417 if (Finley_noError()) Finley_Mesh_prepare(mesh_p, optimize);
418 /* free up memory */
419 ReferenceElementSet_dealloc(refPoints);
420 ReferenceElementSet_dealloc(refContactElements);
421 ReferenceElementSet_dealloc(refFaceElements);
422 ReferenceElementSet_dealloc(refElements);
423 Esys_MPIInfo_free( mpi_info );
424 if (! Finley_noError()) {
425 Finley_Mesh_free(mesh_p);
426 return NULL;
427 } else {
428 return mesh_p;
429 }
430 }
431 }

Properties

Name Value
svn:mergeinfo /branches/lapack2681/finley/src/Mesh_readGmsh.cpp:2682-2741 /branches/pasowrap/finley/src/Mesh_readGmsh.cpp:3661-3674 /branches/py3_attempt2/finley/src/Mesh_readGmsh.cpp:3871-3891 /branches/restext/finley/src/Mesh_readGmsh.cpp:2610-2624 /branches/ripleygmg_from_3668/finley/src/Mesh_readGmsh.cpp:3669-3791 /branches/stage3.0/finley/src/Mesh_readGmsh.cpp:2569-2590 /branches/symbolic_from_3470/finley/src/Mesh_readGmsh.cpp:3471-3974 /release/3.0/finley/src/Mesh_readGmsh.cpp:2591-2601 /trunk/finley/src/Mesh_readGmsh.cpp:4257-4344

  ViewVC Help
Powered by ViewVC 1.1.26