/[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 3126 - (show annotations)
Wed Sep 1 00:37:53 2010 UTC (8 years, 9 months ago) by jfenwick
File MIME type: text/plain
File size: 14271 byte(s)
Updated referenceElements and enums
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 Dudley_ReferenceElementSet *refPoints=NULL, *refFaceElements=NULL, *refElements=NULL;
44 #ifdef Dudley_TRACE
45 double time0=Dudley_timer();
46 #endif
47 FILE * fileHandle_p = NULL;
48 ElementTypeId* element_type=NULL;
49
50
51 Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
52 Dudley_resetError();
53 if (mpi_info->size>1) {
54 Dudley_setError(IO_ERROR,"reading GMSH with MPI is not supported yet.");
55 Paso_MPIInfo_free( mpi_info );
56 return NULL;
57 } else {
58
59 /* allocate mesh */
60
61 mesh_p = Dudley_Mesh_alloc(fname,numDim, mpi_info);
62 if (! Dudley_noError()) return NULL;
63
64 /* get file handle */
65 fileHandle_p = fopen(fname, "r");
66 if (fileHandle_p==NULL) {
67 sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
68 Dudley_setError(IO_ERROR,error_msg);
69 Paso_MPIInfo_free( mpi_info );
70 return NULL;
71 }
72
73 /* start reading */
74 while(1) {
75 if (! Dudley_noError()) break;
76 /* find line staring with $ */
77 do {
78 if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
79 if(feof(fileHandle_p)) break;
80 } while(line[0] != '$');
81
82 if (feof(fileHandle_p)) break;
83
84
85 /* format */
86 if (!strncmp(&line[1], "MeshFormat", 10)) {
87 scan_ret = fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
88 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
89 }
90 /* nodes are read */
91 if ( !strncmp(&line[1], "NOD", 3) ||
92 !strncmp(&line[1], "NOE", 3) ||
93 !strncmp(&line[1], "Nodes", 5) ) {
94
95 scan_ret = fscanf(fileHandle_p, "%d", &numNodes);
96 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
97 if (! Dudley_noError()) break;
98 Dudley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
99 if (! Dudley_noError()) break;
100 for (i0 = 0; i0 < numNodes; i0++) {
101 if (1 == numDim) {
102 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
103 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
104 &rtmp0,
105 &rtmp1);
106 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
107 } else if (2 == numDim) {
108 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
109 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
110 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
111 &rtmp0);
112 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
113 } else if (3 == numDim) {
114 scan_ret = fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
115 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
116 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
117 &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
118 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
119 }
120 mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
121 mesh_p->Nodes->Tag[i0]=0;
122 }
123 }
124 /* elements */
125 else if(!strncmp(&line[1], "ELM", 3) ||
126 !strncmp(&line[1], "Elements", 8)) {
127
128 ElementTypeId final_element_type = NoRef;
129 ElementTypeId final_face_element_type = NoRef;
130 numElements=0;
131 numFaceElements=0;
132 scan_ret = fscanf(fileHandle_p, "%d", &totalNumElements);
133 FSCANF_CHECK(scan_ret, "fscanf: Dudley_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 (! (Dudley_checkPtr(id) || Dudley_checkPtr(tag) || Dudley_checkPtr(element_type) || Dudley_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: Dudley_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 4: /* tetrahedron order 1 */
158 element_type[e]=Tet4;
159 numNodesPerElement= 4;
160 element_dim=3;
161 break;
162 case 15 : /* point */
163 element_type[e]=Point1;
164 numNodesPerElement= 1;
165 element_dim=0;
166 break;
167 default:
168 element_type[e]=NoRef;
169 sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
170 Dudley_setError(IO_ERROR,error_msg);
171 }
172 if (element_dim == numDim) {
173 if (final_element_type == NoRef) {
174 final_element_type = element_type[e];
175 } else if (final_element_type != element_type[e]) {
176 sprintf(error_msg,"Dudley can handle a single type of internal elements only.");
177 Dudley_setError(IO_ERROR,error_msg);
178 break;
179 }
180 numElements++;
181 } else if (element_dim == numDim-1) {
182 if (final_face_element_type == NoRef) {
183 final_face_element_type = element_type[e];
184 } else if (final_face_element_type != element_type[e]) {
185 sprintf(error_msg,"Dudley can handle a single type of face elements only.");
186 Dudley_setError(IO_ERROR,error_msg);
187 break;
188 }
189 numFaceElements++;
190 }
191
192 if(version <= 1.0){
193 scan_ret = fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
194 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
195 partition_id = 1;
196 if (numNodesPerElement2 != numNodesPerElement) {
197 sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
198 Dudley_setError(IO_ERROR,error_msg);
199 }
200 } else {
201 scan_ret = fscanf(fileHandle_p, "%d", &numTags);
202 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
203 elementary_id = tag[e] = partition_id = 1;
204 numNodesPerElement2=-1;
205 for(j = 0; j < numTags; j++){
206 scan_ret = fscanf(fileHandle_p, "%d", &itmp);
207 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
208 if (j == 0) {
209 tag[e] = itmp;
210 } else if (j == 1) {
211 elementary_id = itmp;
212 } else if (j == 2) {
213 partition_id = itmp;
214 }
215 /* ignore any other tags */
216 }
217 }
218 if (! Dudley_noError()) break;
219 for(j = 0; j < numNodesPerElement; j++) {
220 scan_ret = fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
221 FSCANF_CHECK(scan_ret, "fscanf: Dudley_Mesh_readGmsh");
222 }
223 }
224 /* all elements have been read, now we have to identify the elements for dudley */
225
226 if (Dudley_noError()) {
227 /* first we have to identify the elements to define Elementis and FaceElements */
228 if (final_element_type == NoRef) {
229 if (numDim==1) {
230 final_element_type=Line2;
231 } else if (numDim==2) {
232 final_element_type=Tri3;
233 } else if (numDim==3) {
234 final_element_type=Tet4;
235 }
236 }
237 if (final_face_element_type == NoRef) {
238 if (numDim==1) {
239 final_face_element_type=Point1;
240 } else if (numDim==2) {
241 final_face_element_type=Line2;
242 } else if (numDim==3) {
243 final_face_element_type=Tri3;
244 }
245 }
246 refElements= Dudley_ReferenceElementSet_alloc(final_element_type,order, reduced_order);
247 refFaceElements=Dudley_ReferenceElementSet_alloc(final_face_element_type,order, reduced_order);
248 refPoints= Dudley_ReferenceElementSet_alloc(Point1,order, reduced_order);
249 mesh_p->Elements=Dudley_ElementFile_alloc(refElements, mpi_info);
250 mesh_p->FaceElements=Dudley_ElementFile_alloc(refFaceElements, mpi_info);
251 mesh_p->Points=Dudley_ElementFile_alloc(refPoints, mpi_info);
252 if (Dudley_noError()) {
253 Dudley_ElementFile_allocTable(mesh_p->Elements, numElements);
254 Dudley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
255 Dudley_ElementFile_allocTable(mesh_p->Points, 0);
256 if (Dudley_noError()) {
257 mesh_p->Elements->minColor=0;
258 mesh_p->Elements->maxColor=numElements-1;
259 mesh_p->FaceElements->minColor=0;
260 mesh_p->FaceElements->maxColor=numFaceElements-1;
261 mesh_p->Points->minColor=0;
262 mesh_p->Points->maxColor=0;
263 numElements=0;
264 numFaceElements=0;
265 for(e = 0; e < totalNumElements; e++) {
266 if (element_type[e] == final_element_type) {
267 mesh_p->Elements->Id[numElements]=id[e];
268 mesh_p->Elements->Tag[numElements]=tag[e];
269 mesh_p->Elements->Color[numElements]=numElements;
270 mesh_p->Elements->Owner[numElements]=0;
271 for (j = 0; j< mesh_p->Elements->referenceElementSet->numNodes; ++j) {
272 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
273 }
274 numElements++;
275 } else if (element_type[e] == final_face_element_type) {
276 mesh_p->FaceElements->Id[numFaceElements]=id[e];
277 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
278 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
279 mesh_p->FaceElements->Owner[numFaceElements]=0;
280 for (j = 0; j< mesh_p->FaceElements->referenceElementSet->numNodes; ++j) {
281 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->referenceElementSet->numNodes)]=vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
282 }
283 numFaceElements++;
284 }
285 }
286 }
287 }
288 }
289 }
290 /* and clean up */
291 TMPMEMFREE(id);
292 TMPMEMFREE(tag);
293 TMPMEMFREE(element_type);
294 TMPMEMFREE(vertices);
295 }
296 /* serach for end of data block */
297 do {
298 if (!fgets(line, sizeof(line), fileHandle_p)) {
299 sprintf(error_msg,"Unexected end of file in %s",fname);
300 Dudley_setError(IO_ERROR,error_msg);
301 }
302 if (feof(fileHandle_p)) {
303 sprintf(error_msg,"Unexected end of file in %s",fname);
304 Dudley_setError(IO_ERROR,error_msg);
305 }
306 if (! Dudley_noError()) break;
307 } while(line[0] != '$');
308 }
309
310 /* close file */
311 fclose(fileHandle_p);
312 /* clean up */
313 if (! Dudley_noError()) {
314 Dudley_Mesh_free(mesh_p);
315 return NULL;
316 }
317 /* resolve id's : */
318 if (Dudley_noError()) Dudley_Mesh_resolveNodeIds(mesh_p);
319 /* rearrange elements: */
320 if (Dudley_noError()) Dudley_Mesh_prepare(mesh_p, optimize);
321 /* free up memory */
322 Dudley_ReferenceElementSet_dealloc(refPoints);
323 Dudley_ReferenceElementSet_dealloc(refFaceElements);
324 Dudley_ReferenceElementSet_dealloc(refElements);
325 Paso_MPIInfo_free( mpi_info );
326 if (! Dudley_noError()) {
327 Dudley_Mesh_free(mesh_p);
328 return NULL;
329 } else {
330 return mesh_p;
331 }
332 }
333 }

  ViewVC Help
Powered by ViewVC 1.1.26