/[escript]/branches/RW_WIN32/finley/src/finleyC/Mesh_saveDX.c
ViewVC logotype

Contents of /branches/RW_WIN32/finley/src/finleyC/Mesh_saveDX.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 210 - (show annotations)
Wed Nov 23 09:54:02 2005 UTC (14 years, 7 months ago) by robwdcock
File MIME type: text/plain
File size: 14106 byte(s)
PARTIAL WIN32 BUILD SYSTEM AND PORT
+ All libraries now build under new build system. No unit test routines yet
+ Reversed some incorrect refactorings in Bruce.cpp

1 /*
2 ******************************************************************************
3 * *
4 * COPYRIGHT ACcESS 2003,2004,2005 - All Rights Reserved *
5 * *
6 * This software is the property of ACcESS. No part of this code *
7 * may be copied in any form or by any means without the expressed written *
8 * consent of ACcESS. Copying, use or modification of this software *
9 * by any unauthorised person is illegal unless that person has a software *
10 * license agreement with ACcESS. *
11 * *
12 ******************************************************************************
13 */
14
15
16 /**************************************************************/
17
18 /* writes data and mesh in an opendx file */
19
20 /**************************************************************/
21
22 /* Author: Lutz Gross, gross@access.edu.au */
23 /* Version: $Id$ */
24
25 /**************************************************************/
26
27 #include "Mesh.h"
28
29 /**************************************************************/
30
31 void Finley_Mesh_saveDX(const char * filename_p, Finley_Mesh *mesh_p, const dim_t num_data,char* *names_p,escriptDataC* *data_pp) {
32 char error_msg[LenErrorMsg_MAX];
33 /* if there is no mesh we just return */
34 if (mesh_p==NULL) return;
35
36 /* some tables needed for reordering */
37 int resort[6][9]={
38 {0,1}, /* line */
39 {0,1,2}, /* triagle */
40 {0,1,2,3}, /* tetrahedron */
41 {0,3,1,2}, /* quadrilateral */
42 {3,0,7,4,2,1,6,5}, /* hexahedron */
43 };
44 int i,j,k,i_data;
45 /* open the file and check handel */
46
47 FILE * fileHandle_p = fopen(filename_p, "w");
48 if (fileHandle_p==NULL) {
49 sprintf(error_msg,"File %s could not be opened for writing.",filename_p);
50 Finley_setError(IO_ERROR,error_msg);
51 return;
52 }
53 /* find the mesh type to be written */
54 int nodetype=FINLEY_DEGREES_OF_FREEDOM;
55 int elementtype=FINLEY_UNKNOWN;
56 /* win32 refactor */
57 bool_t *isCellCentered = (num_data>0) ? TMPMEMALLOC(num_data,bool_t) : (bool_t*)NULL;
58
59 for (i_data=0;i_data<num_data;++i_data) {
60 if (! isEmpty(data_pp[i_data])) {
61 switch(getFunctionSpaceType(data_pp[i_data])) {
62 case FINLEY_DEGREES_OF_FREEDOM:
63 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
64 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
65 elementtype=FINLEY_ELEMENTS;
66 } else {
67 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
68 /* win32 refactor */
69 TMPMEMFREE(isCellCentered);
70
71 return;
72 }
73 isCellCentered[i_data]=FALSE;
74 break;
75 case FINLEY_REDUCED_DEGREES_OF_FREEDOM:
76 nodetype = FINLEY_REDUCED_DEGREES_OF_FREEDOM;
77 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
78 elementtype=FINLEY_ELEMENTS;
79 } else {
80 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
81 /* win32 refactor */
82 TMPMEMFREE(isCellCentered);
83
84 return;
85 }
86 isCellCentered[i_data]=FALSE;
87 break;
88 case FINLEY_NODES:
89 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
90 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
91 elementtype=FINLEY_ELEMENTS;
92 } else {
93 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
94 /* win32 refactor */
95 TMPMEMFREE(isCellCentered);
96
97 return;
98 }
99 isCellCentered[i_data]=FALSE;
100 break;
101 case FINLEY_ELEMENTS:
102 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
103 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
104 elementtype=FINLEY_ELEMENTS;
105 } else {
106 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
107 /* win32 refactor */
108 TMPMEMFREE(isCellCentered);
109 return;
110 }
111 isCellCentered[i_data]=TRUE;
112 break;
113 case FINLEY_FACE_ELEMENTS:
114 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
115 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_FACE_ELEMENTS) {
116 elementtype=FINLEY_FACE_ELEMENTS;
117 } else {
118 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
119 /* win32 refactor */
120 TMPMEMFREE(isCellCentered);
121 return;
122 }
123 isCellCentered[i_data]=TRUE;
124 break;
125 case FINLEY_POINTS:
126 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
127 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_POINTS) {
128 elementtype=FINLEY_POINTS;
129 } else {
130 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
131 /* win32 refactor */
132 TMPMEMFREE(isCellCentered);
133 return;
134 }
135 isCellCentered[i_data]=TRUE;
136 break;
137 case FINLEY_CONTACT_ELEMENTS_1:
138 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
139 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_CONTACT_ELEMENTS_1) {
140 elementtype=FINLEY_CONTACT_ELEMENTS_1;
141 } else {
142 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
143 /* win32 refactor */
144 TMPMEMFREE(isCellCentered);
145 return;
146 }
147 isCellCentered[i_data]=TRUE;
148 break;
149 case FINLEY_CONTACT_ELEMENTS_2:
150 nodetype = (nodetype == FINLEY_REDUCED_DEGREES_OF_FREEDOM) ? FINLEY_REDUCED_DEGREES_OF_FREEDOM : FINLEY_DEGREES_OF_FREEDOM;
151 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_CONTACT_ELEMENTS_1) {
152 elementtype=FINLEY_CONTACT_ELEMENTS_1;
153 } else {
154 Finley_setError(TYPE_ERROR,"saveDX: cannot write given data in single file.");
155 /* win32 refactor */
156 TMPMEMFREE(isCellCentered);
157 return;
158 }
159 isCellCentered[i_data]=TRUE;
160 break;
161 default:
162 sprintf(error_msg,"saveDX: Finley does not know anything about function space type %d",getFunctionSpaceType(data_pp[i_data]));
163 Finley_setError(TYPE_ERROR,error_msg);
164 /* win32 refactor */
165 TMPMEMFREE(isCellCentered);
166 return;
167 }
168 }
169 }
170 /* select number of points and the mesh component */
171 int numPoints = mesh_p->Nodes->numNodes;
172 int nDim = mesh_p->Nodes->numDim;
173 if (nodetype==FINLEY_REDUCED_DEGREES_OF_FREEDOM) {
174 numPoints = mesh_p->Nodes->reducedNumNodes;
175 } else {
176 numPoints = mesh_p->Nodes->numNodes;
177 }
178 if (elementtype==FINLEY_UNKNOWN) elementtype=FINLEY_ELEMENTS;
179 Finley_ElementFile* elements=NULL;
180 switch(elementtype) {
181 case FINLEY_ELEMENTS:
182 elements=mesh_p->Elements;
183 break;
184 case FINLEY_FACE_ELEMENTS:
185 elements=mesh_p->FaceElements;
186 break;
187 case FINLEY_POINTS:
188 elements=mesh_p->Points;
189 break;
190 case FINLEY_CONTACT_ELEMENTS_1:
191 elements=mesh_p->ContactElements;
192 break;
193 }
194 if (elements==NULL) {
195 Finley_setError(SYSTEM_ERROR,"saveDX: undefined element file");
196 /* win32 refactor */
197 TMPMEMFREE(isCellCentered);
198 return;
199 }
200
201 /* map finley element type to DX element type */
202 ElementTypeId TypeId = elements->ReferenceElement->Type->TypeId;
203 int *resortIndex=NULL;
204 int numDXNodesPerElement=0;
205 int numCells = elements->numElements;
206 char elemTypeStr[32];
207 if (TypeId==Line2 || TypeId==Line3 || TypeId==Line4 ) {
208 numDXNodesPerElement=2;
209 resortIndex=resort[0];
210 strcpy(elemTypeStr, "lines");
211 } else if (TypeId==Tri3 || TypeId==Tri6 || TypeId==Tri9 || TypeId==Tri10 ) {
212 numDXNodesPerElement = 3;
213 resortIndex=resort[1];
214 strcpy(elemTypeStr, "triangles");
215 } else if (TypeId==Rec4 || TypeId==Rec8 || TypeId==Rec9 || TypeId==Rec12 || TypeId==Rec16 ) {
216 numDXNodesPerElement = 4;
217 resortIndex=resort[3];
218 strcpy(elemTypeStr, "quads");
219 } else if (TypeId==Tet4 || TypeId==Tet10 || TypeId==Tet16 ) {
220 numDXNodesPerElement = 4;
221 resortIndex=resort[2];
222 strcpy(elemTypeStr, "tetrahedra");
223 } else if (TypeId==Hex8 || TypeId==Hex20 || TypeId==Hex32 ) {
224 numDXNodesPerElement = 8;
225 resortIndex=resort[4];
226 strcpy(elemTypeStr, "cubes");
227 } else {
228 sprintf(error_msg,"saveDX: Element type %s is not supported by DX",elements->ReferenceElement->Type->Name);
229 Finley_setError(VALUE_ERROR,error_msg);
230 /* win32 refactor */
231 TMPMEMFREE(isCellCentered);
232 return;
233 }
234
235 /* positions */
236 fprintf(fileHandle_p, "object 1 class array type float rank 1 shape %d items %d data follows\n",nDim, mesh_p->Nodes->reducedNumNodes);
237 for (i = 0; i < mesh_p->Nodes->numNodes; i++) {
238 if (mesh_p->Nodes->toReduced[i]>=0) {
239 for (j = 0; j < nDim; j++) fprintf(fileHandle_p, " %g",mesh_p->Nodes->Coordinates[INDEX2(j, i, nDim)]);
240 fprintf(fileHandle_p, "\n");
241 }
242 }
243 /* connection table */
244 int NN=elements->ReferenceElement->Type->numNodes;
245 fprintf(fileHandle_p, "object 2 class array type int rank 1 shape %d items %d data follows\n",numDXNodesPerElement, numCells);
246 for (i = 0; i < numCells; i++) {
247 for (j = 0; j < numDXNodesPerElement; j++) fprintf(fileHandle_p," %d",mesh_p->Nodes->toReduced[elements->Nodes[INDEX2(resortIndex[j], i, NN)]]);
248 fprintf(fileHandle_p, "\n");
249 }
250 fprintf(fileHandle_p, "attribute \"element type\" string \"%s\"\n",elemTypeStr);
251 fprintf(fileHandle_p, "attribute \"ref\" string \"positions\"\n");
252
253 /* data */
254 int object_count=2;
255 for (i_data =0 ;i_data<num_data;++i_data) {
256 if (! isEmpty(data_pp[i_data])) {
257 object_count++;
258 int rank=getDataPointRank(data_pp[i_data]);
259 int nComp=getDataPointSize(data_pp[i_data]);
260 double* values,rtmp;
261 fprintf(fileHandle_p, "object %d class array type float rank %d ",object_count,rank);
262 if (0 < rank) {
263 fprintf(fileHandle_p, "shape ");
264 for (i = 0; i < rank; i++) fprintf(fileHandle_p, "%d ", getDataPointShape(data_pp[i_data],i));
265 }
266 if (isCellCentered[i_data]) {
267 int numPointsPerSample=elements->ReferenceElement->numQuadNodes;
268 if (numPointsPerSample>0) {
269 fprintf(fileHandle_p, "items %d data follows\n", numCells);
270 for (i=0;i<elements->numElements;i++) {
271 values=getSampleData(data_pp[i_data],i);
272 for (k=0;k<nComp;k++) {
273 rtmp=0.;
274 for (j=0;j<numPointsPerSample;j++) rtmp+=values[INDEX2(k,j,nComp)];
275 fprintf(fileHandle_p, " %g", rtmp/numPointsPerSample);
276 }
277 fprintf(fileHandle_p, "\n");
278 }
279 fprintf(fileHandle_p, "attribute \"dep\" string \"connections\"\n");
280 }
281 } else {
282 fprintf(fileHandle_p, "items %d data follows\n", mesh_p->Nodes->reducedNumNodes);
283 for (i=0;i<mesh_p->Nodes->numNodes;i++) {
284 if (mesh_p->Nodes->toReduced[i]>=0) {
285 switch (getFunctionSpaceType(data_pp[i_data])) {
286 case FINLEY_DEGREES_OF_FREEDOM:
287 values=getSampleData(data_pp[i_data],mesh_p->Nodes->degreeOfFreedom[i]);
288 break;
289 case FINLEY_REDUCED_DEGREES_OF_FREEDOM:
290 values=getSampleData(data_pp[i_data],mesh_p->Nodes->reducedDegreeOfFreedom[i]);
291 break;
292 case FINLEY_NODES:
293 values=getSampleData(data_pp[i_data],i);
294 break;
295 }
296 for (k=0;k<nComp;k++) fprintf(fileHandle_p, " %g", values[k]);
297 fprintf(fileHandle_p, "\n");
298 }
299 }
300 fprintf(fileHandle_p, "attribute \"dep\" string \"positions\"\n");
301 }
302 }
303 }
304
305 /* and finish it up */
306 if (num_data==0) {
307 fprintf(fileHandle_p, "object %d class field\n",object_count+1);
308 fprintf(fileHandle_p, "component \"positions\" value 1\n");
309 fprintf(fileHandle_p, "component \"connections\" value 2\n");
310 } else {
311 object_count=2;
312 for (i_data=0; i_data<num_data;++i_data) {
313 if (! isEmpty(data_pp[i_data])) {
314 object_count++;
315 fprintf(fileHandle_p, "object \"%s\" class field\n",names_p[i_data]);
316 fprintf(fileHandle_p, "component \"positions\" value 1\n");
317 fprintf(fileHandle_p, "component \"connections\" value 2\n");
318 fprintf(fileHandle_p, "component \"data\" value %d\n",object_count);
319 }
320 }
321 }
322 fprintf(fileHandle_p, "end\n");
323 /* close the file */
324 fclose(fileHandle_p);
325 /* win32 refactor */
326 TMPMEMFREE(isCellCentered);
327 return;
328 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26