/[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 3247 - (show annotations)
Wed Oct 6 05:53:06 2010 UTC (8 years, 6 months ago) by caltinay
File MIME type: text/plain
File size: 12152 byte(s)
Fixed name clashes between dudley and finley so both can be used
simultaneously.

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 /* Dudley: read mesh */
17
18 /**************************************************************/
19
20 #include "Mesh.h"
21 #include <stdio.h>
22
23 #define FSCANF_CHECK(scan_ret, reason) { if (scan_ret == EOF) { perror(reason); Dudley_setError(IO_ERROR,"scan error while reading dudley file"); return NULL;} }
24
25 /**************************************************************/
26
27 /* reads a mesh from a Dudley file of name fname */
28
29 #define MAX_numNodes_gmsh 20
30
31 Dudley_Mesh *Dudley_Mesh_readGmsh(char *fname, index_t numDim, index_t order, index_t reduced_order, bool_t optimize,
32 bool_t useMacroElements)
33 {
34
35 double version = 1.0;
36 int format = 0, size = sizeof(double), scan_ret;
37 dim_t numNodes, totalNumElements = 0, numTags = 0, numNodesPerElement = 0, numNodesPerElement2, element_dim = 0;
38 index_t e, i0, j, gmsh_type, partition_id, itmp, elementary_id;
39 index_t numElements = 0, numFaceElements = 0, *id = NULL, *tag = NULL, *vertices = NULL;
40 Dudley_Mesh *mesh_p = NULL;
41 char line[LenString_MAX + 1];
42 char error_msg[LenErrorMsg_MAX];
43 double rtmp0, rtmp1;
44 #ifdef Dudley_TRACE
45 double time0 = Dudley_timer();
46 #endif
47 FILE *fileHandle_p = NULL;
48 Dudley_ElementTypeId *element_type = NULL;
49
50 Esys_MPIInfo *mpi_info = Esys_MPIInfo_alloc(MPI_COMM_WORLD);
51 Dudley_resetError();
52 if (mpi_info->size > 1)
53 {
54 Dudley_setError(IO_ERROR, "reading GMSH with MPI is not supported yet.");
55 Esys_MPIInfo_free(mpi_info);
56 return NULL;
57 }
58 else
59 {
60
61 /* allocate mesh */
62
63 mesh_p = Dudley_Mesh_alloc(fname, numDim, mpi_info);
64 if (!Dudley_noError())
65 return NULL;
66
67 /* get file handle */
68 fileHandle_p = fopen(fname, "r");
69 if (fileHandle_p == NULL)
70 {
71 sprintf(error_msg, "Opening Gmsh file %s for reading failed.", fname);
72 Dudley_setError(IO_ERROR, error_msg);
73 Esys_MPIInfo_free(mpi_info);
74 return NULL;
75 }
76
77 /* start reading */
78 while (1)
79 {
80 if (!Dudley_noError())
81 break;
82 /* find line staring with $ */
83 do
84 {
85 if (!fgets(line, sizeof(line), fileHandle_p))
86 break;
87 if (feof(fileHandle_p))
88 break;
89 }
90 while (line[0] != '$');
91
92 if (feof(fileHandle_p))
93 break;
94
95 /* format */
96 if (!strncmp(&line[1], "MeshFormat", 10))
97 {
98 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
99 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
100 }
101 /* nodes are read */
102 if (!strncmp(&line[1], "NOD", 3) || !strncmp(&line[1], "NOE", 3) || !strncmp(&line[1], "Nodes", 5))
103 {
104
105 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
106 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
107 if (!Dudley_noError())
108 break;
109 Dudley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
110 if (!Dudley_noError())
111 break;
112 for (i0 = 0; i0 < numNodes; i0++)
113 {
114 if (1 == numDim)
115 {
116 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
117 &mesh_p->Nodes->Coordinates[INDEX2(0, i0, numDim)], &rtmp0, &rtmp1);
118 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
119 }
120 else if (2 == numDim)
121 {
122 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
123 &mesh_p->Nodes->Coordinates[INDEX2(0, i0, numDim)],
124 &mesh_p->Nodes->Coordinates[INDEX2(1, i0, numDim)], &rtmp0);
125 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
126 }
127 else if (3 == numDim)
128 {
129 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
130 &mesh_p->Nodes->Coordinates[INDEX2(0, i0, numDim)],
131 &mesh_p->Nodes->Coordinates[INDEX2(1, i0, numDim)],
132 &mesh_p->Nodes->Coordinates[INDEX2(2, i0, numDim)]);
133 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
134 }
135 mesh_p->Nodes->globalDegreesOfFreedom[i0] = mesh_p->Nodes->Id[i0];
136 mesh_p->Nodes->Tag[i0] = 0;
137 }
138 }
139 /* elements */
140 else if (!strncmp(&line[1], "ELM", 3) || !strncmp(&line[1], "Elements", 8))
141 {
142
143 Dudley_ElementTypeId final_element_type = Dudley_NoRef;
144 Dudley_ElementTypeId final_face_element_type = Dudley_NoRef;
145 numElements = 0;
146 numFaceElements = 0;
147 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
148 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
149
150 id = TMPMEMALLOC(totalNumElements, index_t);
151 tag = TMPMEMALLOC(totalNumElements, index_t);
152
153 element_type = TMPMEMALLOC(totalNumElements, Dudley_ElementTypeId);
154 vertices = TMPMEMALLOC(totalNumElements * MAX_numNodes_gmsh, index_t);
155 if (!
156 (Dudley_checkPtr(id) || Dudley_checkPtr(tag) || Dudley_checkPtr(element_type)
157 || Dudley_checkPtr(vertices)))
158 {
159 /* read all in */
160 for (e = 0; e < totalNumElements; e++)
161 {
162 scan_ret = fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
163 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
164 switch (gmsh_type)
165 {
166 case 1: /* line order 1 */
167 element_type[e] = Dudley_Line2;
168 element_dim = 1;
169 numNodesPerElement = 2;
170 break;
171 case 2: /* triangle order 1 */
172 element_type[e] = Dudley_Tri3;
173 numNodesPerElement = 3;
174 element_dim = 2;
175 break;
176 case 4: /* tetrahedron order 1 */
177 element_type[e] = Dudley_Tet4;
178 numNodesPerElement = 4;
179 element_dim = 3;
180 break;
181 case 15: /* point */
182 element_type[e] = Dudley_Point1;
183 numNodesPerElement = 1;
184 element_dim = 0;
185 break;
186 default:
187 element_type[e] = Dudley_NoRef;
188 sprintf(error_msg, "Unexected gmsh element type %d in mesh file %s.", gmsh_type, fname);
189 Dudley_setError(IO_ERROR, error_msg);
190 }
191 if (element_dim == numDim)
192 {
193 if (final_element_type == Dudley_NoRef)
194 {
195 final_element_type = element_type[e];
196 }
197 else if (final_element_type != element_type[e])
198 {
199 sprintf(error_msg, "Dudley can handle a single type of internal elements only.");
200 Dudley_setError(IO_ERROR, error_msg);
201 break;
202 }
203 numElements++;
204 }
205 else if (element_dim == numDim - 1)
206 {
207 if (final_face_element_type == Dudley_NoRef)
208 {
209 final_face_element_type = element_type[e];
210 }
211 else if (final_face_element_type != element_type[e])
212 {
213 sprintf(error_msg, "Dudley can handle a single type of face elements only.");
214 Dudley_setError(IO_ERROR, error_msg);
215 break;
216 }
217 numFaceElements++;
218 }
219
220 if (version <= 1.0)
221 {
222 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
223 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
224 partition_id = 1;
225 if (numNodesPerElement2 != numNodesPerElement)
226 {
227 sprintf(error_msg, "Illegal number of nodes for element %d in mesh file %s.", id[e],
228 fname);
229 Dudley_setError(IO_ERROR, error_msg);
230 }
231 }
232 else
233 {
234 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
235 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
236 elementary_id = tag[e] = partition_id = 1;
237 numNodesPerElement2 = -1;
238 for (j = 0; j < numTags; j++)
239 {
240 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
241 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
242 if (j == 0)
243 {
244 tag[e] = itmp;
245 }
246 else if (j == 1)
247 {
248 elementary_id = itmp;
249 }
250 else if (j == 2)
251 {
252 partition_id = itmp;
253 }
254 /* ignore any other tags */
255 }
256 }
257 if (!Dudley_noError())
258 break;
259 for (j = 0; j < numNodesPerElement; j++)
260 {
261 scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j, e, MAX_numNodes_gmsh)]);
262 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
263 }
264 }
265 /* all elements have been read, now we have to identify the elements for dudley */
266
267 if (Dudley_noError())
268 {
269 /* first we have to identify the elements to define Elementis and FaceElements */
270 if (final_element_type == Dudley_NoRef)
271 {
272 if (numDim == 1)
273 {
274 final_element_type = Dudley_Line2;
275 }
276 else if (numDim == 2)
277 {
278 final_element_type = Dudley_Tri3;
279 }
280 else if (numDim == 3)
281 {
282 final_element_type = Dudley_Tet4;
283 }
284 }
285 if (final_face_element_type == Dudley_NoRef)
286 {
287 if (numDim == 1)
288 {
289 final_face_element_type = Dudley_Point1;
290 }
291 else if (numDim == 2)
292 {
293 final_face_element_type = Dudley_Line2;
294 }
295 else if (numDim == 3)
296 {
297 final_face_element_type = Dudley_Tri3;
298 }
299 }
300 mesh_p->Elements = Dudley_ElementFile_alloc(final_element_type, mpi_info);
301 mesh_p->FaceElements = Dudley_ElementFile_alloc(final_face_element_type, mpi_info);
302 mesh_p->Points = Dudley_ElementFile_alloc(Dudley_Point1, mpi_info);
303 if (Dudley_noError())
304 {
305 Dudley_ElementFile_allocTable(mesh_p->Elements, numElements);
306 Dudley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
307 Dudley_ElementFile_allocTable(mesh_p->Points, 0);
308 if (Dudley_noError())
309 {
310 mesh_p->Elements->minColor = 0;
311 mesh_p->Elements->maxColor = numElements - 1;
312 mesh_p->FaceElements->minColor = 0;
313 mesh_p->FaceElements->maxColor = numFaceElements - 1;
314 mesh_p->Points->minColor = 0;
315 mesh_p->Points->maxColor = 0;
316 numElements = 0;
317 numFaceElements = 0;
318 for (e = 0; e < totalNumElements; e++)
319 {
320 if (element_type[e] == final_element_type)
321 {
322 mesh_p->Elements->Id[numElements] = id[e];
323 mesh_p->Elements->Tag[numElements] = tag[e];
324 mesh_p->Elements->Color[numElements] = numElements;
325 mesh_p->Elements->Owner[numElements] = 0;
326 for (j = 0; j < mesh_p->Elements-> /*referenceElementSet-> */ numNodes; ++j)
327 {
328 mesh_p->Elements->Nodes[INDEX2
329 (j, numElements,
330 mesh_p->
331 Elements-> /*referenceElementSet-> */ numNodes)] =
332 vertices[INDEX2(j, e, MAX_numNodes_gmsh)];
333 }
334 numElements++;
335 }
336 else if (element_type[e] == final_face_element_type)
337 {
338 mesh_p->FaceElements->Id[numFaceElements] = id[e];
339 mesh_p->FaceElements->Tag[numFaceElements] = tag[e];
340 mesh_p->FaceElements->Color[numFaceElements] = numFaceElements;
341 mesh_p->FaceElements->Owner[numFaceElements] = 0;
342 for (j = 0; j < mesh_p->FaceElements-> /*referenceElementSet-> */ numNodes; ++j)
343 {
344 mesh_p->FaceElements->Nodes[INDEX2
345 (j, numFaceElements,
346 mesh_p->
347 FaceElements-> /*referenceElementSet-> */
348 numNodes)] =
349 vertices[INDEX2(j, e, MAX_numNodes_gmsh)];
350 }
351 numFaceElements++;
352 }
353 }
354 }
355 }
356 }
357 }
358 /* and clean up */
359 TMPMEMFREE(id);
360 TMPMEMFREE(tag);
361 TMPMEMFREE(element_type);
362 TMPMEMFREE(vertices);
363 }
364 /* serach for end of data block */
365 do
366 {
367 if (!fgets(line, sizeof(line), fileHandle_p))
368 {
369 sprintf(error_msg, "Unexected end of file in %s", fname);
370 Dudley_setError(IO_ERROR, error_msg);
371 }
372 if (feof(fileHandle_p))
373 {
374 sprintf(error_msg, "Unexected end of file in %s", fname);
375 Dudley_setError(IO_ERROR, error_msg);
376 }
377 if (!Dudley_noError())
378 break;
379 }
380 while (line[0] != '$');
381 }
382
383 /* close file */
384 fclose(fileHandle_p);
385 /* clean up */
386 if (!Dudley_noError())
387 {
388 Dudley_Mesh_free(mesh_p);
389 return NULL;
390 }
391 /* resolve id's : */
392 if (Dudley_noError())
393 Dudley_Mesh_resolveNodeIds(mesh_p);
394 /* rearrange elements: */
395 if (Dudley_noError())
396 Dudley_Mesh_prepare(mesh_p, optimize);
397 /* free up memory */
398 Esys_MPIInfo_free(mpi_info);
399 if (!Dudley_noError())
400 {
401 Dudley_Mesh_free(mesh_p);
402 return NULL;
403 }
404 else
405 {
406 return mesh_p;
407 }
408 }
409 }

  ViewVC Help
Powered by ViewVC 1.1.26