TYCHO  1.3.0
 All Data Structures Files Functions Variables Enumerations Enumerator
/home/kapf/tycho_docu/write_vtk.c
Go to the documentation of this file.
1 
2 /*
3  * write_ic_vtk.c
4  *
5  * Author: Wolfgang Kapferer
6  */
7 
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <math.h>
12 
13 #include "variables_global.h"
14 #include "prototypes.h"
15 
21 int write_vtk(int x, int y, int z, int counter) {
22  FILE *fd;
23  char filename[600];
24  int i, j, k;
25  long long int tmp1, tmp1_vx, tmp1_vy, tmp1_vz;
26  double tmp, tmp_vx, tmp_vy, tmp_vz;
27 
28  sprintf(filename, "%srho_%i.vtk", output_dir, counter);
29  fd = fopen(filename, "wb");
30  if (fd == NULL) {
31  printf("-----------------------------------\n");
32  printf("The output directory does not exist\n");
33  printf("-----------------------------------\n");
34  exit(13);
35  }
36  fprintf(fd, "# vtk DataFile Version 3.0\n");
37  fprintf(fd, "TYCHO Density File - Time:%g\n", time_sim / minute_in_sec);
38  fprintf(fd, "BINARY\n");
39  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
40  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
41  fprintf(fd, "X_COORDINATES %i double\n", x);
42  for (i = 0; i < x; i++) {
43  tmp = ((double) xmax / (double) x) * (double) i;
44  tmp1 = ntohll(*((long long int*) & tmp));
45  fwrite(&tmp1, 1, sizeof (double), fd);
46  }
47  fprintf(fd, "\nY_COORDINATES %i double\n", y);
48  for (i = 0; i < y; i++) {
49  tmp = ((double) ymax / (double) y) * (double) i;
50  tmp1 = ntohll(*((long long int*) & tmp));
51  fwrite(&tmp1, 1, sizeof (double), fd);
52  }
53  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
54  for (i = 0; i < z; i++) {
55  tmp = ((double) zmax / (double) z) * (double) i;
56  tmp1 = ntohll(*((long long int*) & tmp));
57  fwrite(&tmp1, 1, sizeof (double), fd);
58  }
59  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
60  fprintf(fd, "SCALARS density double 1\n");
61  fprintf(fd, "LOOKUP_TABLE default\n");
62  for (k = 0; k < z; k++) {
63  for (j = 0; j < y; j++) {
64  for (i = 0; i < x; i++) {
65  tmp = rho[i][j][k];
66  tmp1 = ntohll(*((long long int*) & tmp));
67  fwrite(&tmp1, 1, sizeof (double), fd);
68  }
69  }
70  }
71  fclose(fd);
72 
73  sprintf(filename, "%stemp_%i.vtk", output_dir, counter);
74  fd = fopen(filename, "wb");
75  if (fd == NULL) {
76  printf("-----------------------------------\n");
77  printf("The output directory does not exist\n");
78  printf("-----------------------------------\n");
79  exit(13);
80  }
81  fprintf(fd, "# vtk DataFile Version 3.0\n");
82  fprintf(fd, "TYCHO Temperature File - Time:%g\n", time_sim / minute_in_sec);
83  fprintf(fd, "BINARY\n");
84  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
85  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
86  fprintf(fd, "X_COORDINATES %i double\n", x);
87  for (i = 0; i < x; i++) {
88  tmp = ((double) xmax / (double) x) * (double) i;
89  tmp1 = ntohll(*((long long int*) & tmp));
90  fwrite(&tmp1, 1, sizeof (double), fd);
91  }
92  fprintf(fd, "\nY_COORDINATES %i double\n", y);
93  for (i = 0; i < y; i++) {
94  tmp = ((double) ymax / (double) y) * (double) i;
95  tmp1 = ntohll(*((long long int*) & tmp));
96  fwrite(&tmp1, 1, sizeof (double), fd);
97  }
98  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
99  for (i = 0; i < z; i++) {
100  tmp = ((double) zmax / (double) z) * (double) i;
101  tmp1 = ntohll(*((long long int*) & tmp));
102  fwrite(&tmp1, 1, sizeof (double), fd);
103  }
104  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
105  fprintf(fd, "SCALARS temperature double 1\n");
106  fprintf(fd, "LOOKUP_TABLE default\n");
107  for (k = 0; k < z; k++) {
108  for (j = 0; j < y; j++) {
109  for (i = 0; i < x; i++) {
110  tmp = pre[i][j][k] / (gasconstant * rho[i][j][k]);
111  tmp1 = ntohll(*((long long int*) & tmp));
112  fwrite(&tmp1, 1, sizeof (double), fd);
113  }
114  }
115  }
116  fclose(fd);
117 
118  sprintf(filename, "%spressure_%i.vtk", output_dir, counter);
119  fd = fopen(filename, "wb");
120  if (fd == NULL) {
121  printf("-----------------------------------\n");
122  printf("The output directory does not exist\n");
123  printf("-----------------------------------\n");
124  exit(13);
125  }
126  fprintf(fd, "# vtk DataFile Version 3.0\n");
127  fprintf(fd, "TYCHO Pressure File - Time:%g\n", time_sim / minute_in_sec);
128  fprintf(fd, "BINARY\n");
129  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
130  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
131  fprintf(fd, "X_COORDINATES %i double\n", x);
132  for (i = 0; i < x; i++) {
133  tmp = ((double) xmax / (double) x) * (double) i;
134  tmp1 = ntohll(*((long long int*) & tmp));
135  fwrite(&tmp1, 1, sizeof (double), fd);
136  }
137  fprintf(fd, "\nY_COORDINATES %i double\n", y);
138  for (i = 0; i < y; i++) {
139  tmp = ((double) ymax / (double) y) * (double) i;
140  tmp1 = ntohll(*((long long int*) & tmp));
141  fwrite(&tmp1, 1, sizeof (double), fd);
142  }
143  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
144  for (i = 0; i < z; i++) {
145  tmp = ((double) zmax / (double) z) * (double) i;
146  tmp1 = ntohll(*((long long int*) & tmp));
147  fwrite(&tmp1, 1, sizeof (double), fd);
148  }
149  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
150  fprintf(fd, "SCALARS pressure double 1\n");
151  fprintf(fd, "LOOKUP_TABLE default\n");
152  for (k = 0; k < z; k++) {
153  for (j = 0; j < y; j++) {
154  for (i = 0; i < x; i++) {
155  if (dom[i][j][k] == 0) tmp = pre[i][j][k];
156  if (dom[i][j][k] == 1) tmp = 0.0;
157  tmp1 = ntohll(*((long long int*) & tmp));
158  fwrite(&tmp1, 1, sizeof (double), fd);
159  }
160  }
161  }
162  fclose(fd);
163 
164  sprintf(filename, "%svel_%i.vtk", output_dir, counter);
165  fd = fopen(filename, "wb");
166  if (fd == NULL) {
167  printf("-----------------------------------\n");
168  printf("The output directory does not exist\n");
169  printf("-----------------------------------\n");
170  exit(13);
171  }
172  fprintf(fd, "# vtk DataFile Version 3.0\n");
173  fprintf(fd, "TYCHO Velocity File - Time:%g\n", time_sim / minute_in_sec);
174  fprintf(fd, "BINARY\n");
175  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
176  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
177  fprintf(fd, "X_COORDINATES %i double\n", x);
178  for (i = 0; i < x; i++) {
179  tmp = ((double) xmax / (double) x) * (double) i;
180  tmp1 = ntohll(*((long long int*) & tmp));
181  fwrite(&tmp1, 1, sizeof (double), fd);
182  }
183  fprintf(fd, "\nY_COORDINATES %i double\n", y);
184  for (i = 0; i < y; i++) {
185  tmp = ((double) ymax / (double) y) * (double) i;
186  tmp1 = ntohll(*((long long int*) & tmp));
187  fwrite(&tmp1, 1, sizeof (double), fd);
188  }
189  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
190  for (i = 0; i < z; i++) {
191  tmp = ((double) zmax / (double) z) * (double) i;
192  tmp1 = ntohll(*((long long int*) & tmp));
193  fwrite(&tmp1, 1, sizeof (double), fd);
194  }
195  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
196  fprintf(fd, "VECTORS velocity double 1\n");
197  for (k = 0; k < z; k++) {
198  for (j = 0; j < y; j++) {
199  for (i = 0; i < x; i++) {
200  tmp_vx = vx[i][j][k];
201  tmp1_vx = ntohll(*((long long int*) & tmp_vx));
202  tmp_vy = vy[i][j][k];
203  tmp1_vy = ntohll(*((long long int*) & tmp_vy));
204  tmp_vz = vz[i][j][k];
205  tmp1_vz = ntohll(*((long long int*) & tmp_vz));
206 
207  fwrite(&tmp1_vx, 1, sizeof (double), fd);
208  fwrite(&tmp1_vy, 1, sizeof (double), fd);
209  fwrite(&tmp1_vz, 1, sizeof (double), fd);
210  }
211  }
212  }
213  fclose(fd);
214 
215  if (with_obstacles == 1) {
216  sprintf(filename, "%spressure_on_solid_%i.vtk", output_dir, counter);
217  fd = fopen(filename, "wb");
218  if (fd == NULL) {
219  printf("-----------------------------------\n");
220  printf("The output directory does not exist\n");
221  printf("-----------------------------------\n");
222  exit(13);
223  }
224  fprintf(fd, "# vtk DataFile Version 3.0\n");
225  fprintf(fd, "TYCHO pressure on solid File - Time:%g\n", time_sim / minute_in_sec);
226  fprintf(fd, "BINARY\n");
227  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
228  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
229  fprintf(fd, "X_COORDINATES %i double\n", x);
230  for (i = 0; i < x; i++) {
231  tmp = ((double) xmax / (double) x) * (double) i;
232  tmp1 = ntohll(*((long long int*) & tmp));
233  fwrite(&tmp1, 1, sizeof (double), fd);
234  }
235  fprintf(fd, "\nY_COORDINATES %i double\n", y);
236  for (i = 0; i < y; i++) {
237  tmp = ((double) ymax / (double) y) * (double) i;
238  tmp1 = ntohll(*((long long int*) & tmp));
239  fwrite(&tmp1, 1, sizeof (double), fd);
240  }
241  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
242  for (i = 0; i < z; i++) {
243  tmp = ((double) zmax / (double) z) * (double) i;
244  tmp1 = ntohll(*((long long int*) & tmp));
245  fwrite(&tmp1, 1, sizeof (double), fd);
246  }
247  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
248  fprintf(fd, "SCALARS pressure_on_solid double 1\n");
249  fprintf(fd, "LOOKUP_TABLE default\n");
250  for (k = 0; k < z; k++) {
251  for (j = 0; j < y; j++) {
252  for (i = 0; i < x; i++) {
253  tmp = pressure_on_solid[i][j][k];
254  tmp1 = ntohll(*((long long int*) & tmp));
255  fwrite(&tmp1, 1, sizeof (double), fd);
256  }
257  }
258  }
259  fclose(fd);
260 
261  if ((with_sound_emitter == 1) && (dB_Map_ready == 1)) {
262  sprintf(filename, "%TYCHO dB_map_%i.vtk", output_dir, counter);
263  fd = fopen(filename, "wb");
264  if (fd == NULL) {
265  printf("-----------------------------------\n");
266  printf("The output directory does not exist\n");
267  printf("-----------------------------------\n");
268  exit(13);
269  }
270  fprintf(fd, "# vtk DataFile Version 3.0\n");
271  fprintf(fd, "TYCHO dBA Map - Time:%g\n", time_sim / minute_in_sec);
272  fprintf(fd, "BINARY\n");
273  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
274  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
275  fprintf(fd, "X_COORDINATES %i double\n", x);
276  for (i = 0; i < x; i++) {
277  tmp = ((double) xmax / (double) x) * (double) i;
278  tmp1 = ntohll(*((long long int*) & tmp));
279  fwrite(&tmp1, 1, sizeof (double), fd);
280  }
281  fprintf(fd, "\nY_COORDINATES %i double\n", y);
282  for (i = 0; i < y; i++) {
283  tmp = ((double) ymax / (double) y) * (double) i;
284  tmp1 = ntohll(*((long long int*) & tmp));
285  fwrite(&tmp1, 1, sizeof (double), fd);
286  }
287  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
288  for (i = 0; i < z; i++) {
289  tmp = ((double) zmax / (double) z) * (double) i;
290  tmp1 = ntohll(*((long long int*) & tmp));
291  fwrite(&tmp1, 1, sizeof (double), fd);
292  }
293  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
294  fprintf(fd, "SCALARS pressure_on_solid double 1\n");
295  fprintf(fd, "LOOKUP_TABLE default\n");
296  for (k = 0; k < z; k++) {
297  for (j = 0; j < y; j++) {
298  for (i = 0; i < x; i++) {
299  tmp = dB_map[i][j][k];
300  tmp1 = ntohll(*((long long int*) & tmp));
301  fwrite(&tmp1, 1, sizeof (double), fd);
302  }
303  }
304  }
305  fclose(fd);
306  }
307  }
308 
309  if (advection == 1) {
310 
311  sprintf(filename, "%smarker_%i.vtk", output_dir, counter);
312  fd = fopen(filename, "wb");
313  if (fd == NULL) {
314  printf("-----------------------------------\n");
315  printf("The output directory does not exist\n");
316  printf("-----------------------------------\n");
317  exit(13);
318  }
319  fprintf(fd, "# vtk DataFile Version 3.0\n");
320  fprintf(fd, "TYCHO Marker File - Time:%g\n", time_sim / minute_in_sec);
321  fprintf(fd, "BINARY\n");
322  fprintf(fd, "DATASET RECTILINEAR_GRID\n");
323  fprintf(fd, "DIMENSIONS %i %i %i\n", x, y, z);
324  fprintf(fd, "X_COORDINATES %i double\n", x);
325  for (i = 0; i < x; i++) {
326  tmp = ((double) xmax / (double) x) * (double) i;
327  tmp1 = ntohll(*((long long int*) & tmp));
328  fwrite(&tmp1, 1, sizeof (double), fd);
329  }
330  fprintf(fd, "\nY_COORDINATES %i double\n", y);
331  for (i = 0; i < y; i++) {
332  tmp = ((double) ymax / (double) y) * (double) i;
333  tmp1 = ntohll(*((long long int*) & tmp));
334  fwrite(&tmp1, 1, sizeof (double), fd);
335  }
336  fprintf(fd, "\nZ_COORDINATES %i double\n", z);
337  for (i = 0; i < z; i++) {
338  tmp = ((double) zmax / (double) z) * (double) i;
339  tmp1 = ntohll(*((long long int*) & tmp));
340  fwrite(&tmp1, 1, sizeof (double), fd);
341  }
342  fprintf(fd, "\nPOINT_DATA %i\n", x * y * z);
343  fprintf(fd, "SCALARS marker_density double 1\n");
344  fprintf(fd, "LOOKUP_TABLE default\n");
345  for (k = 0; k < z; k++) {
346  for (j = 0; j < y; j++) {
347  for (i = 0; i < x; i++) {
348  tmp = marker[i][j][k];
349  tmp1 = ntohll(*((long long int*) & tmp));
350  fwrite(&tmp1, 1, sizeof (double), fd);
351  }
352  }
353  }
354  fclose(fd);
355 
356  }
357 
358  return 0;
359 }