/[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 5148 - (show annotations)
Mon Sep 15 01:25:23 2014 UTC (4 years, 11 months ago) by caltinay
File size: 18287 byte(s)
Merging ripley diagonal storage + CUDA support into trunk.
Options file version has been incremented due to new options
'cuda' and 'nvccflags'.

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2014 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 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17
18 /****************************************************************************
19
20 Finley: read mesh from gmsh file
21
22 *****************************************************************************/
23
24 #include "Mesh.h"
25 #include <cstdio>
26
27 namespace finley {
28
29 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); setError(IO_ERROR,"scan error while reading finley file"); return NULL;} }
30
31 #define MAX_numNodes_gmsh 20
32
33 Mesh* Mesh::readGmsh(esysUtils::JMPI& mpi_info, const std::string fname, int numDim, int order,
34 int reduced_order, bool optimize, bool useMacroElements)
35 {
36 double version = 1.0;
37 int format = 0, size = sizeof(double), scan_ret;
38 int numNodes, totalNumElements=0, numTags=0, numNames=0, numNodesPerElement=0, numNodesPerElement2, element_dim=0;
39 int e, i0, j, gmsh_type, partition_id, itmp, elementary_id, tag_key;
40 int numElements=0, numFaceElements=0, *id=NULL, *tag=NULL, *vertices=NULL;
41 char line[LenString_MAX+1], name[LenString_MAX+1];
42 char error_msg[LenErrorMsg_MAX];
43 double rtmp0, rtmp1;
44 const_ReferenceElementSet_ptr refPoints, refContactElements, refFaceElements, refElements;
45 #ifdef Finley_TRACE
46 double time0=timer();
47 #endif
48 FILE * fileHandle_p = NULL;
49 ElementTypeId* element_type=NULL;
50
51 resetError();
52 if (mpi_info->size>1) {
53 setError(IO_ERROR, "reading gmsh files with MPI is not supported yet.");
54 return NULL;
55 }
56
57 // allocate mesh
58 Mesh* mesh_p = new Mesh(fname, numDim, mpi_info);
59
60 // get file handle
61 fileHandle_p = fopen(fname.c_str(), "r");
62 if (fileHandle_p==NULL) {
63 sprintf(error_msg, "Opening Gmsh file %s for reading failed.", fname.c_str());
64 setError(IO_ERROR, error_msg);
65 return NULL;
66 }
67
68 /* start reading */
69 while(1) {
70 if (!noError()) break;
71 /* find line staring with $ */
72 do {
73 if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
74 if(feof(fileHandle_p)) break;
75 } while(line[0] != '$');
76 if (feof(fileHandle_p)) break;
77
78 /* format */
79 if (!strncmp(&line[1], "MeshFormat", 10)) {
80 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
81 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
82 }
83 /* nodes are read */
84 if ( !strncmp(&line[1], "NOD", 3) ||
85 !strncmp(&line[1], "NOE", 3) ||
86 !strncmp(&line[1], "Nodes", 5) ) {
87
88 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
89 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
90 if (!noError()) break;
91 mesh_p->Nodes->allocTable(numNodes);
92 if (!noError()) break;
93 for (i0 = 0; i0 < numNodes; i0++) {
94 if (1 == numDim) {
95 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
96 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
97 &rtmp0,
98 &rtmp1);
99 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
100 } else if (2 == numDim) {
101 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
102 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
103 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
104 &rtmp0);
105 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
106 } else if (3 == numDim) {
107 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
108 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
109 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
110 &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
111 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
112 }
113 mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
114 mesh_p->Nodes->Tag[i0]=0;
115 }
116 }
117 /* elements */
118 else if(!strncmp(&line[1], "ELM", 3) ||
119 !strncmp(&line[1], "Elements", 8)) {
120 ElementTypeId final_element_type = NoRef;
121 ElementTypeId final_face_element_type = NoRef;
122 ElementTypeId contact_element_type = NoRef;
123 numElements=0;
124 numFaceElements=0;
125 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
126 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
127
128 id=new int[totalNumElements];
129 tag=new int[totalNumElements];
130
131 element_type=new ElementTypeId[totalNumElements];
132 vertices=new int[totalNumElements*MAX_numNodes_gmsh];
133 /* read all in */
134 for(e = 0; e < totalNumElements; e++) {
135 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
136 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
137 switch (gmsh_type) {
138 case 1: /* line order 1 */
139 element_type[e]=Line2;
140 element_dim=1;
141 numNodesPerElement=2;
142 break;
143 case 2: /* triangle order 1 */
144 element_type[e]=Tri3;
145 numNodesPerElement= 3;
146 element_dim=2;
147 break;
148 case 3: /* quadrilateral order 1 */
149 element_type[e]=Rec4;
150 numNodesPerElement= 4;
151 element_dim=2;
152 break;
153 case 4: /* tetrahedron order 1 */
154 element_type[e]=Tet4;
155 numNodesPerElement= 4;
156 element_dim=3;
157 break;
158 case 5: /* hexahedron order 1 */
159 element_type[e]=Hex8;
160 numNodesPerElement= 8;
161 element_dim=3;
162 break;
163 case 8: /* line order 2 */
164 if (useMacroElements) {
165 element_type[e]=Line3Macro;
166 } else {
167 element_type[e]=Line3;
168 }
169 numNodesPerElement= 3;
170 element_dim=1;
171 break;
172 case 9: /* triangle order 2 */
173 if (useMacroElements) {
174 element_type[e]=Tri6Macro;
175 } else {
176 element_type[e]=Tri6;
177 }
178 numNodesPerElement= 6;
179 element_dim=2;
180 break;
181 case 10: /* quadrilateral order 2 */
182 if (useMacroElements) {
183 element_type[e]=Rec9Macro;
184 } else {
185 element_type[e]=Rec9;
186 }
187 numNodesPerElement= 9;
188 element_dim=2;
189 break;
190 case 11: /* tetrahedron order 2 */
191 if (useMacroElements) {
192 element_type[e]=Tet10Macro;
193 } else {
194 element_type[e]=Tet10;
195 }
196 numNodesPerElement= 10;
197 element_dim=3;
198 break;
199 case 16: /* rectangular order 2 */
200 element_type[e]=Rec8;
201 numNodesPerElement= 8;
202 element_dim=2;
203 break;
204 case 17: /* hexahedron order 2 */
205 element_type[e]=Hex20;
206 numNodesPerElement= 20;
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,"Unexpected gmsh element type %d in mesh file %s.", gmsh_type, fname.c_str());
217 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 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 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: 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.c_str());
245 setError(IO_ERROR,error_msg);
246 }
247 } else {
248 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
249 FSCANF_CHECK(scan_ret, "fscanf: 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: 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 (!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: 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
278 // all elements have been read, now we have to identify the
279 // elements for finley
280 if (noError()) {
281 /* first we have to identify the elements to define Elements and FaceElements */
282 if (final_element_type == NoRef) {
283 if (numDim==1) {
284 final_element_type=Line2;
285 } else if (numDim==2) {
286 final_element_type=Tri3;
287 } else if (numDim==3) {
288 final_element_type=Tet4;
289 }
290 }
291 if (final_face_element_type == NoRef) {
292 if (numDim==1) {
293 final_face_element_type=Point1;
294 } else if (numDim==2) {
295 final_face_element_type=Line2;
296 } else if (numDim==3) {
297 final_face_element_type=Tri3;
298 }
299 }
300 if (final_face_element_type == Line2) {
301 contact_element_type=Line2_Contact;
302 } else if ( (final_face_element_type == Line3) || (final_face_element_type == Line3Macro) ) {
303 contact_element_type=Line3_Contact;
304 } else if (final_face_element_type == Tri3) {
305 contact_element_type=Tri3_Contact;
306 } else if ( (final_face_element_type == Tri6) || (final_face_element_type == Tri6Macro)) {
307 contact_element_type=Tri6_Contact;
308 } else {
309 contact_element_type=Point1_Contact;
310 }
311 refElements.reset(new ReferenceElementSet(final_element_type, order, reduced_order));
312 refFaceElements.reset(new ReferenceElementSet(final_face_element_type, order, reduced_order));
313 refContactElements.reset(new ReferenceElementSet(contact_element_type, order, reduced_order));
314 refPoints.reset(new ReferenceElementSet(Point1, order, reduced_order));
315 mesh_p->Elements=new ElementFile(refElements, mpi_info);
316 mesh_p->FaceElements=new ElementFile(refFaceElements, mpi_info);
317 mesh_p->ContactElements=new ElementFile(refContactElements, mpi_info);
318 mesh_p->Points=new ElementFile(refPoints, mpi_info);
319 if (noError()) {
320 mesh_p->Elements->allocTable(numElements);
321 mesh_p->FaceElements->allocTable(numFaceElements);
322 mesh_p->ContactElements->allocTable(0);
323 mesh_p->Points->allocTable(0);
324 if (noError()) {
325 mesh_p->Elements->minColor=0;
326 mesh_p->Elements->maxColor=numElements-1;
327 mesh_p->FaceElements->minColor=0;
328 mesh_p->FaceElements->maxColor=numFaceElements-1;
329 mesh_p->ContactElements->minColor=0;
330 mesh_p->ContactElements->maxColor=0;
331 mesh_p->Points->minColor=0;
332 mesh_p->Points->maxColor=0;
333 numElements=0;
334 numFaceElements=0;
335 for(e = 0; e < totalNumElements; e++) {
336 if (element_type[e] == final_element_type) {
337 mesh_p->Elements->Id[numElements]=id[e];
338 mesh_p->Elements->Tag[numElements]=tag[e];
339 mesh_p->Elements->Color[numElements]=numElements;
340 mesh_p->Elements->Owner[numElements]=0;
341 for (j = 0; j< mesh_p->Elements->numNodes; ++j) {
342 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
343 }
344 numElements++;
345 } else if (element_type[e] == final_face_element_type) {
346 mesh_p->FaceElements->Id[numFaceElements]=id[e];
347 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
348 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
349 mesh_p->FaceElements->Owner[numFaceElements]=0;
350 for (j=0; j<mesh_p->FaceElements->numNodes; ++j) {
351 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
352 }
353 numFaceElements++;
354 }
355 }
356 }
357 }
358 }
359 /* and clean up */
360 delete[] id;
361 delete[] tag;
362 delete[] element_type;
363 delete[] vertices;
364 }
365 /* name tags (thanks to Antoine Lefebvre, antoine.lefebvre2@mail.mcgill.ca ) */
366 else if (!strncmp(&line[1], "PhysicalNames", 13)) {
367 if (! noError()) break;
368 scan_ret = fscanf(fileHandle_p, "%d", &numNames);
369 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
370
371 for (i0 = 0; i0 < numNames; i0++) {
372 scan_ret = fscanf(fileHandle_p, "%d %d %s\n", &itmp, &tag_key, name);
373 FSCANF_CHECK(scan_ret, "fscanf: Mesh_readGmsh");
374 //if (! (itmp == 2)) setError(IO_ERROR,"Mesh_readGmsh: expecting two entries per physical name.");
375 if ( strlen(name) < 3 ) setError(IO_ERROR,"Mesh_readGmsh: illegal tagname (\" missing?)");
376 if (! noError()) break;
377 name[strlen(name)-1]='\0';
378 mesh_p->addTagMap(&name[1], tag_key);
379 }
380 }
381
382 // else if (!strncmp(&line[1], "PhysicalNames", 13)) {
383 // if (! noError()) break;
384 // if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
385 // setError(IO_ERROR,"test");
386 // }
387
388
389
390 /* search for end of data block */
391 do {
392 if (!fgets(line, sizeof(line), fileHandle_p)) {
393 sprintf(error_msg,"Unexpected end of file in %s", fname.c_str());
394 setError(IO_ERROR,error_msg);
395 }
396 if (feof(fileHandle_p)) {
397 sprintf(error_msg,"Unexpected end of file in %s", fname.c_str());
398 setError(IO_ERROR,error_msg);
399 }
400 if (! noError()) break;
401 } while(line[0] != '$');
402
403 } // end of read
404
405 // close file
406 fclose(fileHandle_p);
407 // clean up
408 if (!noError()) {
409 delete mesh_p;
410 return NULL;
411 }
412 // resolve id's
413 if (noError()) mesh_p->resolveNodeIds();
414 // rearrange elements
415 if (noError()) mesh_p->prepare(optimize);
416 if (!noError()) {
417 delete mesh_p;
418 return NULL;
419 }
420 return mesh_p;
421 }
422
423 } // namespace finley
424

Properties

Name Value
svn:mergeinfo /branches/diaplayground/finley/src/Mesh_readGmsh.cpp:4940-5147 /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