/[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 3221 - (show annotations)
Wed Sep 29 01:00:21 2010 UTC (8 years, 6 months ago) by jfenwick
File MIME type: text/plain
File size: 13783 byte(s)
Comment stripping

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
17 /* Dudley: 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); Dudley_setError(IO_ERROR,"scan error while reading dudley file"); return NULL;} }
25
26 /**************************************************************/
27
28 /* reads a mesh from a Dudley file of name fname */
29
30 #define MAX_numNodes_gmsh 20
31
32 Dudley_Mesh* Dudley_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 Dudley_Mesh *mesh_p=NULL;
40 char line[LenString_MAX+1];
41 char error_msg[LenErrorMsg_MAX];
42 double rtmp0, rtmp1;
43 #ifdef Dudley_TRACE
44 double time0=Dudley_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 Dudley_resetError();
52 if (mpi_info->size>1) {
53 Dudley_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 = Dudley_Mesh_alloc(fname,numDim, mpi_info);
61 if (! Dudley_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 Dudley_setError(IO_ERROR,error_msg);
68 Paso_MPIInfo_free( mpi_info );
69 return NULL;
70 }
71
72 /* start reading */
73 while(1) {
74 if (! Dudley_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: Dudley_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: Dudley_Mesh_readGmsh");
96 if (! Dudley_noError()) break;
97 Dudley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
98 if (! Dudley_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: Dudley_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: Dudley_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: Dudley_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 = NoRef;
128 ElementTypeId final_face_element_type = NoRef;
129 numElements=0;
130 numFaceElements=0;
131 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
132 FSCANF_CHECK(scan_ret, "fscanf: Dudley_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 (! (Dudley_checkPtr(id) || Dudley_checkPtr(tag) || Dudley_checkPtr(element_type) || Dudley_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: Dudley_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 4: /* tetrahedron order 1 */
157 element_type[e]=Tet4;
158 numNodesPerElement= 4;
159 element_dim=3;
160 break;
161 case 15 : /* point */
162 element_type[e]=Point1;
163 numNodesPerElement= 1;
164 element_dim=0;
165 break;
166 default:
167 element_type[e]=NoRef;
168 sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
169 Dudley_setError(IO_ERROR,error_msg);
170 }
171 if (element_dim == numDim) {
172 if (final_element_type == NoRef) {
173 final_element_type = element_type[e];
174 } else if (final_element_type != element_type[e]) {
175 sprintf(error_msg,"Dudley can handle a single type of internal elements only.");
176 Dudley_setError(IO_ERROR,error_msg);
177 break;
178 }
179 numElements++;
180 } else if (element_dim == numDim-1) {
181 if (final_face_element_type == NoRef) {
182 final_face_element_type = element_type[e];
183 } else if (final_face_element_type != element_type[e]) {
184 sprintf(error_msg,"Dudley can handle a single type of face elements only.");
185 Dudley_setError(IO_ERROR,error_msg);
186 break;
187 }
188 numFaceElements++;
189 }
190
191 if(version <= 1.0){
192 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
193 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
194 partition_id = 1;
195 if (numNodesPerElement2 != numNodesPerElement) {
196 sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
197 Dudley_setError(IO_ERROR,error_msg);
198 }
199 } else {
200 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
201 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
202 elementary_id = tag[e] = partition_id = 1;
203 numNodesPerElement2=-1;
204 for(j = 0; j < numTags; j++){
205 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
206 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
207 if (j == 0) {
208 tag[e] = itmp;
209 } else if (j == 1) {
210 elementary_id = itmp;
211 } else if (j == 2) {
212 partition_id = itmp;
213 }
214 /* ignore any other tags */
215 }
216 }
217 if (! Dudley_noError()) break;
218 for(j = 0; j < numNodesPerElement; j++) {
219 scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
220 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
221 }
222 }
223 /* all elements have been read, now we have to identify the elements for dudley */
224
225 if (Dudley_noError()) {
226 /* first we have to identify the elements to define Elementis and FaceElements */
227 if (final_element_type == NoRef) {
228 if (numDim==1) {
229 final_element_type=Line2;
230 } else if (numDim==2) {
231 final_element_type=Tri3;
232 } else if (numDim==3) {
233 final_element_type=Tet4;
234 }
235 }
236 if (final_face_element_type == NoRef) {
237 if (numDim==1) {
238 final_face_element_type=Point1;
239 } else if (numDim==2) {
240 final_face_element_type=Line2;
241 } else if (numDim==3) {
242 final_face_element_type=Tri3;
243 }
244 }
245 mesh_p->Elements=Dudley_ElementFile_alloc(final_element_type, mpi_info);
246 mesh_p->FaceElements=Dudley_ElementFile_alloc(final_face_element_type, mpi_info);
247 mesh_p->Points=Dudley_ElementFile_alloc(Point1, mpi_info);
248 if (Dudley_noError()) {
249 Dudley_ElementFile_allocTable(mesh_p->Elements, numElements);
250 Dudley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
251 Dudley_ElementFile_allocTable(mesh_p->Points, 0);
252 if (Dudley_noError()) {
253 mesh_p->Elements->minColor=0;
254 mesh_p->Elements->maxColor=numElements-1;
255 mesh_p->FaceElements->minColor=0;
256 mesh_p->FaceElements->maxColor=numFaceElements-1;
257 mesh_p->Points->minColor=0;
258 mesh_p->Points->maxColor=0;
259 numElements=0;
260 numFaceElements=0;
261 for(e = 0; e < totalNumElements; e++) {
262 if (element_type[e] == final_element_type) {
263 mesh_p->Elements->Id[numElements]=id[e];
264 mesh_p->Elements->Tag[numElements]=tag[e];
265 mesh_p->Elements->Color[numElements]=numElements;
266 mesh_p->Elements->Owner[numElements]=0;
267 for (j = 0; j< mesh_p->Elements->/*referenceElementSet->*/numNodes; ++j) {
268 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->/*referenceElementSet->*/numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
269 }
270 numElements++;
271 } else if (element_type[e] == final_face_element_type) {
272 mesh_p->FaceElements->Id[numFaceElements]=id[e];
273 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
274 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
275 mesh_p->FaceElements->Owner[numFaceElements]=0;
276 for (j = 0; j< mesh_p->FaceElements->/*referenceElementSet->*/numNodes; ++j) {
277 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->/*referenceElementSet->*/numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
278 }
279 numFaceElements++;
280 }
281 }
282 }
283 }
284 }
285 }
286 /* and clean up */
287 TMPMEMFREE(id);
288 TMPMEMFREE(tag);
289 TMPMEMFREE(element_type);
290 TMPMEMFREE(vertices);
291 }
292 /* serach for end of data block */
293 do {
294 if (!fgets(line, sizeof(line), fileHandle_p)) {
295 sprintf(error_msg,"Unexected end of file in %s",fname);
296 Dudley_setError(IO_ERROR,error_msg);
297 }
298 if (feof(fileHandle_p)) {
299 sprintf(error_msg,"Unexected end of file in %s",fname);
300 Dudley_setError(IO_ERROR,error_msg);
301 }
302 if (! Dudley_noError()) break;
303 } while(line[0] != '$');
304 }
305
306 /* close file */
307 fclose(fileHandle_p);
308 /* clean up */
309 if (! Dudley_noError()) {
310 Dudley_Mesh_free(mesh_p);
311 return NULL;
312 }
313 /* resolve id's : */
314 if (Dudley_noError()) Dudley_Mesh_resolveNodeIds(mesh_p);
315 /* rearrange elements: */
316 if (Dudley_noError()) Dudley_Mesh_prepare(mesh_p, optimize);
317 /* free up memory */
318 Paso_MPIInfo_free( mpi_info );
319 if (! Dudley_noError()) {
320 Dudley_Mesh_free(mesh_p);
321 return NULL;
322 } else {
323 return mesh_p;
324 }
325 }
326 }

  ViewVC Help
Powered by ViewVC 1.1.26