/[escript]/trunk-mpi-branch/finley/src/Mesh_readGmsh.c
ViewVC logotype

Contents of /trunk-mpi-branch/finley/src/Mesh_readGmsh.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1223 - (show annotations)
Fri Aug 3 02:40:39 2007 UTC (11 years, 8 months ago) by gross
File MIME type: text/plain
File size: 14986 byte(s)
first attemt towards an improved MPI version.  

1 /*
2 ************************************************************
3 * Copyright 2006, 2007 by ACcESS MNRF *
4 * *
5 * http://www.access.edu.au *
6 * Primary Business: Queensland, Australia *
7 * Licensed under the Open Software License version 3.0 *
8 * http://www.opensource.org/licenses/osl-3.0.php *
9 * *
10 ************************************************************
11 */
12
13 /**************************************************************/
14
15 /* Finley: read mesh */
16
17 /**************************************************************/
18
19 /* Author: gross@access.edu.au */
20 /* Version: $Id: Mesh_read.c 730 2006-05-15 04:03:49Z bcumming $ */
21
22 /**************************************************************/
23
24 #include "Mesh.h"
25 #include <stdio.h>
26
27
28 /**************************************************************/
29
30 /* reads a mesh from a Finley file of name fname */
31
32 #define MAX_numNodes_gmsh 10
33
34 Finley_Mesh* Finley_Mesh_readGmsh(char* fname ,index_t numDim, index_t order, index_t reduced_order, bool_t optimize_labeling) {
35
36 double version = 1.0;
37 int format = 0, size = sizeof(double);
38 dim_t numNodes, totalNumElements=0, numTags=0, numNodesPerElement, numNodesPerElement2, element_dim;
39 index_t e, i0, j, gmsh_type, partition_id, itmp, final_element_type, elementary_id;
40 index_t numElements=0, numFaceElements=0, *id=NULL, *tag=NULL, *vertices=NULL;
41 Finley_Mesh *mesh_p=NULL;
42 char line[LenString_MAX+1];
43 char error_msg[LenErrorMsg_MAX];
44 double rtmp0, rtmp1;
45 double time0=Finley_timer();
46 FILE * fileHandle_p = NULL;
47 ElementTypeId* element_type=NULL;
48
49 Paso_MPIInfo *mpi_info = Paso_MPIInfo_alloc( MPI_COMM_WORLD );
50 Finley_resetError();
51 if (mpi_info->size>1) {
52 Finley_setError(IO_ERROR,"reading GMSH with MPI is not supported yet.");
53 Paso_MPIInfo_free( mpi_info );
54 return NULL;
55 } else {
56
57 /* allocate mesh */
58
59 mesh_p = Finley_Mesh_alloc(fname,numDim,order, reduced_order, mpi_info);
60 if (! Finley_noError()) return NULL;
61
62 /* get file handle */
63 fileHandle_p = fopen(fname, "r");
64 if (fileHandle_p==NULL) {
65 sprintf(error_msg,"Opening Gmsh file %s for reading failed.",fname);
66 Finley_setError(IO_ERROR,error_msg);
67 Paso_MPIInfo_free( mpi_info );
68 return NULL;
69 }
70
71 /* start reading */
72 while(1) {
73 if (! Finley_noError()) break;
74 /* find line staring with $ */
75 do {
76 if( ! fgets(line, sizeof(line), fileHandle_p) ) break;
77 if(feof(fileHandle_p)) break;
78 } while(line[0] != '$');
79
80 if (feof(fileHandle_p)) break;
81
82
83 /* format */
84 if (!strncmp(&line[1], "MeshFormat", 10)) {
85 fscanf(fileHandle_p, "%lf %d %d\n", &version, &format, &size);
86 }
87 /* nodes are read */
88 if ( !strncmp(&line[1], "NOD", 3) ||
89 !strncmp(&line[1], "NOE", 3) ||
90 !strncmp(&line[1], "Nodes", 5) ) {
91
92 fscanf(fileHandle_p, "%d", &numNodes);
93 if (! Finley_noError()) break;
94 Finley_NodeFile_allocTable(mesh_p->Nodes, numNodes);
95 if (! Finley_noError()) break;
96 for (i0 = 0; i0 < numNodes; i0++) {
97 if (1 == numDim) {
98 fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
99 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
100 &rtmp0,
101 &rtmp1);
102 } else if (2 == numDim) {
103 fscanf(fileHandle_p, "%d %le %le %le\n", &mesh_p->Nodes->Id[i0],
104 &mesh_p->Nodes->Coordinates[INDEX2(0,i0,numDim)],
105 &mesh_p->Nodes->Coordinates[INDEX2(1,i0,numDim)],
106 &rtmp0);
107 } else if (3 == numDim) {
108 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 &mesh_p->Nodes->Coordinates[INDEX2(2,i0,numDim)]);
112
113
114 }
115 mesh_p->Nodes->globalDegreesOfFreedom[i0]=mesh_p->Nodes->Id[i0];
116 mesh_p->Nodes->Tag[i0]=0;
117 }
118 }
119 /* elements */
120 else if(!strncmp(&line[1], "ELM", 3) ||
121 !strncmp(&line[1], "Elements", 8)) {
122
123 ElementTypeId final_element_type = NoType;
124 ElementTypeId final_face_element_type = NoType;
125 numElements=0;
126 numFaceElements=0;
127 fscanf(fileHandle_p, "%d", &totalNumElements);
128
129 id=TMPMEMALLOC(totalNumElements,index_t);
130 tag=TMPMEMALLOC(totalNumElements,index_t);
131
132
133 element_type=TMPMEMALLOC(totalNumElements,ElementTypeId);
134 vertices=TMPMEMALLOC(totalNumElements*MAX_numNodes_gmsh,index_t);
135 if (! (Finley_checkPtr(id) || Finley_checkPtr(tag) || Finley_checkPtr(element_type) || Finley_checkPtr(vertices) ) ) {
136 /* read all in */
137 for(e = 0; e < totalNumElements; e++) {
138 fscanf(fileHandle_p, "%d %d", &id[e], &gmsh_type);
139 switch (gmsh_type) {
140 case 1: /* line order 1 */
141 element_type[e]=Line2;
142 element_dim=1;
143 numNodesPerElement=2;
144 break;
145 case 2: /* traingle order 1 */
146 element_type[e]=Tri3;
147 numNodesPerElement= 3;
148 element_dim=2;
149 break;
150 case 3: /* quadrilateral order 1 */
151 element_type[e]=Rec4;
152 numNodesPerElement= 4;
153 element_dim=2;
154 break;
155 case 4: /* tetrahedron order 1 */
156 element_type[e]=Tet4;
157 numNodesPerElement= 4;
158 element_dim=3;
159 break;
160 case 5: /* hexahedron order 1 */
161 element_type[e]=Hex8;
162 numNodesPerElement= 8;
163 element_dim=3;
164 break;
165 case 8: /* line order 2 */
166 element_type[e]=Line3;
167 numNodesPerElement= 3;
168 element_dim=1;
169 break;
170 case 9: /* traingle order 2 */
171 element_type[e]=Tri6;
172 numNodesPerElement= 6;
173 element_dim=2;
174 break;
175 case 10: /* quadrilateral order 2 */
176 element_type[e]=Rec9;
177 numNodesPerElement= 9;
178 element_dim=2;
179 break;
180 case 11: /* tetrahedron order 2 */
181 element_type[e]=Tet10;
182 numNodesPerElement= 10;
183 element_dim=3;
184 break;
185 case 15 : /* point */
186 element_type[e]=Point1;
187 numNodesPerElement= 1;
188 element_dim=0;
189 break;
190 default:
191 element_type[e]=NoType;
192 sprintf(error_msg,"Unexected gmsh element type %d in mesh file %s.",gmsh_type,fname);
193 Finley_setError(IO_ERROR,error_msg);
194 }
195 if (element_dim == numDim) {
196 if (final_element_type == NoType) {
197 final_element_type = element_type[e];
198 } else if (final_element_type != element_type[e]) {
199 sprintf(error_msg,"Finley can handle a single type of internal elements only.");
200 Finley_setError(IO_ERROR,error_msg);
201 break;
202 }
203 numElements++;
204 } else if (element_dim == numDim-1) {
205 if (final_face_element_type == NoType) {
206 final_face_element_type = element_type[e];
207 } else if (final_face_element_type != element_type[e]) {
208 sprintf(error_msg,"Finley can handle a single type of face elements only.");
209 Finley_setError(IO_ERROR,error_msg);
210 break;
211 }
212 numFaceElements++;
213 }
214
215 if(version <= 1.0){
216 fscanf(fileHandle_p, "%d %d %d", &tag[e], &elementary_id, &numNodesPerElement2);
217 partition_id = 1;
218 if (numNodesPerElement2 != numNodesPerElement) {
219 sprintf(error_msg,"Illegal number of nodes for element %d in mesh file %s.",id[e],fname);
220 Finley_setError(IO_ERROR,error_msg);
221 }
222 } else {
223 fscanf(fileHandle_p, "%d", &numTags);
224 elementary_id = tag[e] = partition_id = 1;
225 numNodesPerElement2=-1;
226 for(j = 0; j < numTags; j++){
227 fscanf(fileHandle_p, "%d", &itmp);
228 if (j == 0) {
229 tag[e] = itmp;
230 } else if (j == 1) {
231 elementary_id = itmp;
232 } else if (j == 2) {
233 partition_id = itmp;
234 }
235 /* ignore any other tags */
236 }
237 }
238 if (! Finley_noError()) break;
239 for(j = 0; j < numNodesPerElement; j++) fscanf(fileHandle_p, "%d", &vertices[INDEX2(j,e,MAX_numNodes_gmsh)]);
240 }
241 /* all elements have been read, now we have to identify the elements for finley */
242
243 if (Finley_noError()) {
244 /* first we have to identify the elements to define Elementis and FaceElements */
245 if (final_element_type == NoType) {
246 if (numDim==1) {
247 final_element_type=Line2;
248 } else if (numDim==2) {
249 final_element_type=Tri3;
250 } else if (numDim==3) {
251 final_element_type=Tet4;
252 }
253 }
254 if (final_face_element_type == NoType) {
255 if (numDim==1) {
256 final_face_element_type=Point1;
257 } else if (numDim==2) {
258 final_face_element_type=Line2;
259 } else if (numDim==3) {
260 final_face_element_type=Tri3;
261 }
262 }
263 mesh_p->Elements=Finley_ElementFile_alloc(final_element_type,mesh_p->order, mesh_p->reduced_order, mpi_info);
264 mesh_p->FaceElements=Finley_ElementFile_alloc(final_face_element_type,mesh_p->order, mesh_p->reduced_order, mpi_info);
265 mesh_p->ContactElements=Finley_ElementFile_alloc(Point1_Contact,mesh_p->order, mesh_p->reduced_order, mpi_info);
266 mesh_p->Points=Finley_ElementFile_alloc(Point1,mesh_p->order, mesh_p->reduced_order, mpi_info);
267 if (Finley_noError()) {
268 Finley_ElementFile_allocTable(mesh_p->Elements, numElements);
269 Finley_ElementFile_allocTable(mesh_p->FaceElements, numFaceElements);
270 Finley_ElementFile_allocTable(mesh_p->ContactElements, 0);
271 Finley_ElementFile_allocTable(mesh_p->Points, 0);
272 if (Finley_noError()) {
273 mesh_p->Elements->minColor=0;
274 mesh_p->Elements->maxColor=numElements-1;
275 mesh_p->FaceElements->minColor=0;
276 mesh_p->FaceElements->maxColor=numFaceElements-1;
277 mesh_p->ContactElements->minColor=0;
278 mesh_p->ContactElements->maxColor=0;
279 mesh_p->Points->minColor=0;
280 mesh_p->Points->maxColor=0;
281 numElements=0;
282 numFaceElements=0;
283 for(e = 0; e < totalNumElements; e++) {
284 if (element_type[e] == final_element_type) {
285 mesh_p->Elements->Id[numElements]=id[e];
286 mesh_p->Elements->Tag[numElements]=tag[e];
287 mesh_p->Elements->Color[numElements]=numElements;
288 for (j = 0; j< mesh_p->Elements->ReferenceElement->Type->numNodes; ++j)
289 mesh_p->Elements->Nodes[INDEX2(j, numElements, mesh_p->Elements->ReferenceElement->Type->numNodes)]=
290 vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
291 numElements++;
292 } else if (element_type[e] == final_face_element_type) {
293 mesh_p->FaceElements->Id[numFaceElements]=id[e];
294 mesh_p->FaceElements->Tag[numFaceElements]=tag[e];
295 mesh_p->FaceElements->Color[numFaceElements]=numFaceElements;
296 for (j = 0; j< mesh_p->FaceElements->ReferenceElement->Type->numNodes; ++j)
297 mesh_p->FaceElements->Nodes[INDEX2(j, numFaceElements, mesh_p->FaceElements->ReferenceElement->Type->numNodes)]=
298 vertices[INDEX2(j,e,MAX_numNodes_gmsh)];
299 numFaceElements++;
300 }
301 }
302 }
303 }
304 }
305 }
306 /* and clean up */
307 TMPMEMFREE(id);
308 TMPMEMFREE(tag);
309 TMPMEMFREE(element_type);
310 TMPMEMFREE(vertices);
311 }
312 /* serach for end of data block */
313 do {
314 if (!fgets(line, sizeof(line), fileHandle_p)) {
315 sprintf(error_msg,"Unexected end of file in %s",fname);
316 Finley_setError(IO_ERROR,error_msg);
317 }
318 if (feof(fileHandle_p)) {
319 sprintf(error_msg,"Unexected end of file in %s",fname);
320 Finley_setError(IO_ERROR,error_msg);
321 }
322 if (! Finley_noError()) break;
323 } while(line[0] != '$');
324 }
325
326 /* close file */
327 fclose(fileHandle_p);
328 /* clean up */
329 if (! Finley_noError()) {
330 Finley_Mesh_free(mesh_p);
331 return NULL;
332 }
333 /* resolve id's : */
334 Finley_Mesh_resolveNodeIds(mesh_p);
335 /* rearrange elements: */
336 Finley_Mesh_prepare(mesh_p);
337 /* that's it */
338 #ifdef Finley_TRACE
339 printf("timing: reading mesh: %.4e sec\n",Finley_timer()-time0);
340 #endif
341 if (Finley_noError()) {
342 if ( ! Finley_Mesh_isPrepared(mesh_p)) {
343 Finley_setError(SYSTEM_ERROR,"Mesh is not prepared for calculation. Contact the programmers.");
344 }
345 }
346 Paso_MPIInfo_free( mpi_info );
347 return mesh_p;
348 }
349 }

  ViewVC Help
Powered by ViewVC 1.1.26