1 | /* -*- Mode: c -*-
2 | *
3 | * $Id: ensightgold_writecbin.c,v 1.1 2007/01/30 10:43:12 avbp Exp $
4 | *
5 | * AUTHOR: Charles MARTIN
6 | * ORG: CERFACS (http://www.cerfacs.fr)
7 | * E-MAIL: martin@cerfacs.fr
8 | *
9 | * ORIG-DATE: 8-Sep-04 at 17:56:04
10 | * LAST-MOD: 11-2006 by J. Amaya, O. CabritA>, A. Roux
11 | *
12 | * DESCRIPTION:
13 | * DESCRIP-END.
14 | */
15 |
16 | #include <stdlib.h>
17 | #include <stdio.h>
18 | #include <math.h>
19 | #include <string.h>
20 |
21 | #define STRINGLEN 80
22 | #define TRUE 1
23 | #define FALSE 0
24 |
25 | /* Swtich to BigEndian if necessary */
26 | #if defined (SEKHMET)
27 | # define FWRITE fwrite_byteswap
28 | # define BYTESWAPIO TRUE
29 | #elif defined (IMHOTEP)
30 | # define FWRITE fwrite_byteswap
31 | # define BYTESWAPIO TRUE
32 | #elif defined (PCLINUX)|defined(KALI)
33 | # define FWRITE fwrite_byteswap
34 | # define BYTESWAPIO TRUE
35 | #else
36 | # define FWRITE fwrite
37 | # define BYTESWAPIO FALSE
38 | #endif
39 |
40 |
41 | #if defined (NO_UNDERSCORE)
42 | # define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin
43 | # define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin
44 | # define WRITE_REAL_SCALAR_BIN write_real_scalar_bin
45 | # define WRITE_REAL_VECTOR_BIN write_real_vector_bin
46 | #else
47 | # define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin_
48 | # define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin_
49 | # define WRITE_REAL_SCALAR_BIN write_real_scalar_bin_
50 | # define WRITE_REAL_VECTOR_BIN write_real_vector_bin_
51 | #endif
52 |
53 |
54 | /*#define ENSIGHTGOLD_WRITE_GEOCOOR_BIN ensightgold_write_geocoor_bin_
55 | #define ENSIGHTGOLD_WRITE_GEOELT_BIN ensightgold_write_geoelt_bin_ */
56 | /*===========================================================================*/
57 | int fwrite_byteswap ( const void* pTo, size_t size, int mItems, FILE *binFile ) {
58 | /* Thanks to Jens Muller for this Routine */
59 | /* It does not swap when it's a character string */
60 | static char *pData ;
61 | static int k ;
62 |
63 | if ( size%2 )
64 | /* Size is odd. No swapping. The only case I could see is 1 for char. */
65 | return ( fwrite ( pTo, size, mItems, binFile ) ) ;
66 | else {
67 | /* Perform swapping. */
68 | for ( pData = ( char * ) pTo ;
69 | pData < ( char * ) pTo + size*mItems ; pData += size )
70 | for ( k = size-1 ; k >= 0 ; k-- )
71 | if ( !fwrite ( pData+k, 1, 1, binFile ) )
72 | /* FWRITE failed. */
73 | return ( 0 ) ;
74 |
75 | return ( mItems ) ;
76 | }
77 | }
78 |
79 | /*=================================================================*/
80 | void clearstring(char *string2clear, int stringlength)
81 | {
82 | int i;
83 | for (i=0; i<stringlength; i++)
84 | string2clear[i]=0;
85 | return;
86 | }
87 |
88 | /*=================================================================*/
89 | void ENSIGHTGOLD_WRITE_GEOCOOR_BIN (const char filename[STRINGLEN],
90 | const int* ln,
91 | const int* nnode,
92 | float *coorx,
93 | float *coory,
94 | float *coorz)
95 | {
96 | int i;
97 | char cfilename[STRINGLEN+1];
98 | char buffer[80];
99 | int int_bufel;
100 | FILE *fptr;
101 |
102 | /* string conversion Fortran -> C */
103 | for (i=0; i<STRINGLEN; i++)
104 | cfilename[i]=filename[i];
105 | cfilename[*ln]='\0';
106 | /* if (BYTESWAPIO)
107 | printf("****** Warning Swapping I/O Endianess *******\n"); */
108 |
109 | /* opening file */
110 | fptr = fopen(cfilename,"wb");
111 |
112 | /* writing header */
113 | clearstring(buffer,STRINGLEN);
114 | strcpy(buffer,"C binary");
115 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
116 |
117 | clearstring(buffer,STRINGLEN);
118 | strcpy(buffer,"Ensight Gold Geometry File");
119 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
120 |
121 | clearstring(buffer,STRINGLEN);
122 | strcpy(buffer,"Generated by a2gold translator");
123 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
124 |
125 | clearstring(buffer,STRINGLEN);
126 | strcpy(buffer,"node id assign");
127 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
128 |
129 | clearstring(buffer,STRINGLEN);
130 | strcpy(buffer,"element id assign");
131 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
132 |
133 | clearstring(buffer,STRINGLEN);
134 | strcpy(buffer,"part");
135 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
136 |
137 | int_bufel= 1;
138 | FWRITE(&int_bufel,sizeof(int),1,fptr);
139 |
140 | clearstring(buffer,STRINGLEN);
141 | strcpy(buffer,"First part");
142 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
143 |
144 | clearstring(buffer,STRINGLEN);
145 | strcpy(buffer,"coordinates");
146 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
147 |
148 | int_bufel= *nnode;
149 | FWRITE(&int_bufel,sizeof(int),1,fptr);
150 | /* Write node coordinates */
151 | FWRITE(coorx,sizeof(float),*nnode,fptr);
152 | FWRITE(coory,sizeof(float),*nnode,fptr);
153 | FWRITE(coorz,sizeof(float),*nnode,fptr);
154 |
155 |
156 |
157 |
158 | /* closing file */
159 | fclose(fptr);
160 |
161 | return;
162 | }
163 |
164 | /*=================================================================*/
165 | void ENSIGHTGOLD_WRITE_GEOELT_BIN (const char filename[STRINGLEN],
166 | const int* ln,
167 | const char elt_type[STRINGLEN],
168 | const int* ln_etyp,
169 | const int* nodeperelt,
170 | const int* nelement,
171 | const int *iel2nod)
172 | {
173 | int i;
174 | char cfilename[STRINGLEN+1];
175 | char celt_type[STRINGLEN+1];
176 | char buffer[80];
177 | int int_bufel;
178 | FILE *fptr;
179 |
180 | /* string conversion Fortran -> C */
181 | for (i=0; i<STRINGLEN; i++)
182 | {
183 | cfilename[i]=filename[i];
184 | celt_type[i]=elt_type[i];
185 | }
186 | cfilename[*ln]='\0';
187 | celt_type[*ln_etyp]='\0';
188 |
189 | /* opening file */
190 | fptr = fopen(cfilename,"ab");
191 |
192 | /* Element section */
193 | clearstring(buffer,STRINGLEN);
194 | strcpy(buffer,celt_type);
195 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
196 |
197 | int_bufel= *nelement;
198 | FWRITE(&int_bufel,sizeof(int),1,fptr);
199 | /* swaping if necessary and cast int4 to long8 */
200 | for (i=0;i<(*nelement)*(*nodeperelt);i++)
201 | {
202 | int_bufel= iel2nod[i];
203 | FWRITE(&int_bufel,sizeof(int),1,fptr);
204 | }
205 |
206 | fclose(fptr);
207 | return;
208 | }
209 |
210 | void WRITE_REAL_SCALAR_BIN (const char filename[STRINGLEN],
211 | const int* ln,
212 | const char field_name[STRINGLEN],
213 | const int* fln,
214 | float *scalarfield,
215 | const int* nnode)
216 | {
217 | int i;
218 | char cfilename[STRINGLEN+1];
219 | char cfield_name[STRINGLEN+1];
220 | char buffer[80];
221 | int int_bufel;
222 | FILE *fptr;
223 |
224 | /* string conversion Fortran -> C */
225 | for (i=0; i<STRINGLEN; i++)
226 | {
227 | cfilename[i]=filename[i];
228 | cfield_name[i]=field_name[i];
229 | }
230 | cfilename[*ln]='\0';
231 | cfield_name[*fln]='\0';
232 |
233 | /* opening file */
234 | fptr = fopen(cfilename,"wb");
235 | /* write field name */
236 | clearstring(buffer,STRINGLEN);
237 | strcpy(buffer,cfield_name);
238 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
239 |
240 | clearstring(buffer,STRINGLEN);
241 | strcpy(buffer,"part");
242 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
243 |
244 | int_bufel= 1;
245 | FWRITE(&int_bufel,sizeof(int),1,fptr);
246 |
247 | clearstring(buffer,STRINGLEN);
248 | strcpy(buffer,"coordinates");
249 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
250 |
251 | FWRITE(scalarfield,sizeof(float),*nnode,fptr);
252 |
253 | fclose(fptr);
254 | return;
255 | }
256 | /*===========================================================================*/
257 | void WRITE_REAL_VECTOR_BIN (const char filename[STRINGLEN],
258 | const int* ln,
259 | const char field_name[STRINGLEN],
260 | const int* fln,
261 | float *component1,
262 | float *component2,
263 | float *component3,
264 | const int* nnode)
265 | {
266 | int i;
267 | char cfilename[STRINGLEN+1];
268 | char cfield_name[STRINGLEN+1];
269 | char buffer[80];
270 | int int_bufel;
271 | FILE *fptr;
272 |
273 | /* string conversion Fortran -> C */
274 | for (i=0; i<STRINGLEN; i++)
275 | {
276 | cfilename[i]=filename[i];
277 | cfield_name[i]=field_name[i];
278 | }
279 | cfilename[*ln]='\0';
280 | cfield_name[*fln]='\0';
281 |
282 | /* opening file */
283 | fptr = fopen(cfilename,"wb");
284 | /* write field name */
285 | clearstring(buffer,STRINGLEN);
286 | strcpy(buffer,cfield_name);
287 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
288 |
289 | clearstring(buffer,STRINGLEN);
290 | strcpy(buffer,"part");
291 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
292 |
293 | int_bufel= 1;
294 | FWRITE(&int_bufel,sizeof(int),1,fptr);
295 |
296 | clearstring(buffer,STRINGLEN);
297 | strcpy(buffer,"coordinates");
298 | FWRITE(buffer,sizeof(char),STRINGLEN,fptr);
299 |
300 | FWRITE(component1,sizeof(float),*nnode,fptr);
301 | FWRITE(component2,sizeof(float),*nnode,fptr);
302 | FWRITE(component3,sizeof(float),*nnode,fptr);
303 |
304 | fclose(fptr);
305 | return;
306 | }
307 |
308 |
309 |