/[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 2722 - (show annotations)
Fri Oct 16 06:45:01 2009 UTC (9 years, 6 months ago) by gross
Original Path: trunk/finley/src/Mesh_readGmsh.c
File MIME type: text/plain
File size: 17180 byte(s)
First steps towards macro elements. at the monent a  macro element is identical to its counterpart of 2nd order.




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

  ViewVC Help
Powered by ViewVC 1.1.26