YetAnotherCoupler 3.2.0_a
Loading...
Searching...
No Matches
vtk_output.c
Go to the documentation of this file.
1// Copyright (c) 2024 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8
9#include "vtk_output.h"
10#include "utils_common.h"
11#include "geometry.h"
12
13#define WRITE_ASCII
14
16 INT = 0,
17 UINT = 1,
18 FLOAT = 2,
19 DOUBLE = 3,
20};
21
22const char * scalars_dt_names[4] = {
23 "int",
24 "unsigned_int",
25 "float",
26 "double"
27};
28
31 void * data;
32 char * name;
33};
34
44
45YAC_VTK_FILE * yac_vtk_open(const char * filename, const char * title) {
46
47 YAC_VTK_FILE * vtk_file = xmalloc(1 * sizeof(*vtk_file));
48 FILE * file;
49
50 file = xfopen(filename, "w+");
51 vtk_file->file = file;
52 vtk_file->scalars_cell_data = NULL;
53 vtk_file->num_scalars_cell_data = 0;
54 vtk_file->scalars_point_data = NULL;
55 vtk_file->num_scalars_point_data = 0;
56
57 fprintf(file, "# vtk DataFile Version 2.0\n");
58 fprintf(file, "%s\n", title);
59#ifdef WRITE_ASCII
60 fprintf(file, "ASCII\n");
61#else
62 fprintf(file, "BINARY\n");
63#endif
64 fprintf(file, "DATASET UNSTRUCTURED_GRID\n");
65
66 return vtk_file;
67}
68
70 YAC_VTK_FILE * vtk_file, double * point_data, unsigned num_points) {
71
72 FILE * file = vtk_file->file;
73
74 vtk_file->num_points = num_points;
75
76 unsigned i;
77
78 fprintf(file, "POINTS %u double\n", num_points);
79#ifdef WRITE_ASCII
80 for (i = 0; i < num_points; ++i)
81 fprintf(file, "%f %f %f\n", point_data[i*3+0], point_data[i*3+1], point_data[i*3+2]);
82#else
83 fwrite(point_data, sizeof(*point_data), 3 * num_points, file);
84 fprintf(file, "\n");
85#endif
86}
87
89 YAC_VTK_FILE * vtk_file, unsigned * cell_corners,
90 unsigned * num_points_per_cell, unsigned num_cells) {
91
92 FILE * file = vtk_file->file;
93
94 vtk_file->num_cells = num_cells;
95
96 unsigned i, j, k;
97 unsigned total_num_cell_corners = 0;
98
99 for (i = 0; i < num_cells; ++i)
100 total_num_cell_corners += num_points_per_cell[i];
101
102 fprintf(file, "CELLS %u %u\n", num_cells, total_num_cell_corners + num_cells);
103
104 for (i = 0, k = 0; i < num_cells; ++i) {
105
106#ifdef WRITE_ASCII
107 fprintf(file, "%u", num_points_per_cell[i]);
108 for (j = 0; j < num_points_per_cell[i]; ++j)
109 fprintf(file, " %u", cell_corners[k++]);
110 fprintf(file, "\n");
111#else
112 fwrite(num_points_per_cell+i, sizeof(*num_points_per_cell), 1, file);
113 fwrite(cell_corners+k, sizeof(*cell_corners), num_points_per_cell[i], file);
114 k += num_points_per_cell[i];
115#endif
116 }
117#ifndef WRITE_ASCII
118 fprintf(file, "\n");
119#endif
120
121 fprintf(file, "CELL_TYPES %u\n", num_cells);
122 for (i = 0; i < num_cells; ++i) {
123
124 unsigned type;
125
126 switch(num_points_per_cell[i]) {
127 case(1):
128 type = 1;
129 break;
130 case(2):
131 type = 3;
132 break;
133 default:
134 type = 7;
135 };
136
137#ifdef WRITE_ASCII
138 fprintf(file, "%u\n", type);
139#else
140 fwrite(&type, sizeof(type), 1, file);
141#endif
142 }
143#ifndef WRITE_ASCII
144 fprintf(file, "\n");
145#endif
146
147}
148
150 YAC_VTK_FILE * vtk_file, double * point_data_lon, double * point_data_lat,
151 unsigned num_points) {
152
153 double * point_data =
154 xmalloc(3 * (size_t)num_points * sizeof(*point_data));
155
156 for (unsigned i = 0; i < num_points; ++i)
157 LLtoXYZ(
158 point_data_lon[i], point_data_lat[i], point_data + (size_t)(3 * i));
159
160 yac_vtk_write_point_data(vtk_file, point_data, num_points);
161
162 free(point_data);
163}
164
166 YAC_VTK_FILE * vtk_file, void * scalars, unsigned num_cells, char * name,
167 enum scalars_dt dt) {
168
170 vtk_file->num_cells == num_cells,
171 "ERROR: yac_vtk_write_cell_scalars number of cells does not match")
172
173 int scalar_index = vtk_file->num_scalars_cell_data++;
174
175 vtk_file->scalars_cell_data = xrealloc(vtk_file->scalars_cell_data,
176 vtk_file->num_scalars_cell_data *
177 sizeof(*(vtk_file->scalars_cell_data)));
178
179 size_t data_size = num_cells;
180
181 switch(dt) {
182 default:
183 case(INT) :
184 data_size *= sizeof(int);
185 break;
186 case(UINT) :
187 data_size *= sizeof(unsigned);
188 break;
189 case(FLOAT) :
190 data_size *= sizeof(float);
191 break;
192 case(DOUBLE) :
193 data_size *= sizeof(double);
194 break;
195 }
196
197 vtk_file->scalars_cell_data[scalar_index].dt = dt;
198 vtk_file->scalars_cell_data[scalar_index].data = xmalloc(data_size);
199 memcpy(vtk_file->scalars_cell_data[scalar_index].data, scalars, data_size);
200 vtk_file->scalars_cell_data[scalar_index].name = xmalloc((strlen(name) + 1));
201 memcpy(vtk_file->scalars_cell_data[scalar_index].name, name, strlen(name) + 1);
202}
203
205 YAC_VTK_FILE * vtk_file, void * scalars, unsigned num_points, char * name,
206 enum scalars_dt dt) {
207
209 vtk_file->num_points == num_points,
210 "ERROR: yac_vtk_write_point_scalars number of points does not match")
211
212 int scalar_index = vtk_file->num_scalars_point_data++;
213
214 vtk_file->scalars_point_data = xrealloc(vtk_file->scalars_point_data,
215 vtk_file->num_scalars_point_data *
216 sizeof(*(vtk_file->scalars_point_data)));
217
218 size_t data_size = num_points;
219
220 switch(dt) {
221 default:
222 case(INT) :
223 data_size *= sizeof(int);
224 break;
225 case(UINT) :
226 data_size *= sizeof(unsigned);
227 break;
228 case(FLOAT) :
229 data_size *= sizeof(float);
230 break;
231 case(DOUBLE) :
232 data_size *= sizeof(double);
233 break;
234 }
235
236 vtk_file->scalars_point_data[scalar_index].dt = dt;
237 vtk_file->scalars_point_data[scalar_index].data = xmalloc(data_size);
238 memcpy(vtk_file->scalars_point_data[scalar_index].data, scalars, data_size);
239 vtk_file->scalars_point_data[scalar_index].name = xmalloc((strlen(name) + 1));
240 memcpy(vtk_file->scalars_point_data[scalar_index].name, name, strlen(name) + 1);
241}
242
244 YAC_VTK_FILE * vtk_file, unsigned * scalars,
245 unsigned num_cells, char * name) {
246
247 yac_vtk_write_cell_scalars(vtk_file, scalars, num_cells, name, UINT);
248}
249
251 YAC_VTK_FILE * vtk_file, int * scalars,
252 unsigned num_cells, char * name) {
253
254 yac_vtk_write_cell_scalars(vtk_file, scalars, num_cells, name, INT);
255}
256
258 YAC_VTK_FILE * vtk_file, float * scalars,
259 unsigned num_cells, char * name) {
260
261 yac_vtk_write_cell_scalars(vtk_file, scalars, num_cells, name, FLOAT);
262}
263
265 YAC_VTK_FILE * vtk_file, double * scalars,
266 unsigned num_cells, char * name) {
267
268 yac_vtk_write_cell_scalars(vtk_file, scalars, num_cells, name, DOUBLE);
269}
270
272 YAC_VTK_FILE * vtk_file, unsigned * scalars,
273 unsigned num_points, char * name) {
274
275 yac_vtk_write_point_scalars(vtk_file, scalars, num_points, name, UINT);
276}
277
279 YAC_VTK_FILE * vtk_file, int * scalars,
280 unsigned num_points, char * name) {
281
282 yac_vtk_write_point_scalars(vtk_file, scalars, num_points, name, INT);
283}
284
286 YAC_VTK_FILE * vtk_file, float * scalars,
287 unsigned num_points, char * name) {
288
289 yac_vtk_write_point_scalars(vtk_file, scalars, num_points, name, FLOAT);
290}
291
293 YAC_VTK_FILE * vtk_file, double * scalars,
294 unsigned num_points, char * name) {
295
297}
298
299static void vtk_flush_scalar_data(FILE * file, struct scalars_data * data,
300 unsigned num_scalars_data,
301 unsigned num_data_points) {
302
303
304 for (unsigned i = 0; i < num_scalars_data; ++i) {
305
306 fprintf(file, "SCALARS %s %s 1\n", data[i].name,
308 fprintf(file, "LOOKUP_TABLE default\n");
309
310 for (unsigned j = 0; j < num_data_points; ++j) {
311#ifdef WRITE_ASCII
312 switch (data[i].dt) {
313
314 default:
315 case (INT) :
316 fprintf(file, "%d\n", ((int*)(data[i].data))[j]);
317 break;
318 case (UINT) :
319 fprintf(file, "%u\n", ((unsigned*)(data[i].data))[j]);
320 break;
321 case (FLOAT) :
322 fprintf(file, "%.8f\n", ((float*)(data[i].data))[j]);
323 //fprintf(file, "%f\n", ((float*)(data[i].data))[j]);
324 break;
325 case (DOUBLE):
326 fprintf(file, "%.18lf\n", ((double*)(data[i].data))[j]);
327 //fprintf(file, "%lf\n", ((double*)(data[i].data))[j]);
328 break;
329 }
330#else
331 switch (data[i].dt) {
332
333 default:
334 case (INT) :
335 fwrite(data[i].data, sizeof(int), 1, file);
336 break;
337 case (UINT) :
338 fwrite(data[i].data, sizeof(unsigned), 1, file);
339 break;
340 case (FLOAT) :
341 fwrite(data[i].data, sizeof(float), 1, file);
342 break;
343 case (DOUBLE):
344 fwrite(data[i].data, sizeof(double), 1, file);
345 break;
346 }
347 fprintf(file, "\n");
348#endif
349 }
350 free(data[i].data);
351 free(data[i].name);
352 }
353}
354
356
357 FILE * file = vtk_file->file;
358
359 fprintf(file, "CELL_DATA %u\n", vtk_file->num_cells);
360
361 fprintf(file, "SCALARS cell_ids int 1\n");
362 fprintf(file, "LOOKUP_TABLE default\n");
363 for (unsigned i = 0; i < vtk_file->num_cells; ++i) {
364#ifdef WRITE_ASCII
365 fprintf(file, "%u\n", i);
366#else
367 fwrite(&i, sizeof(i), 1, file);
368#endif
369 }
370#ifndef WRITE_ASCII
371 fprintf(file, "\n");
372#endif
373
374 if (vtk_file->num_scalars_cell_data > 0)
376 vtk_file->num_scalars_cell_data, vtk_file->num_cells);
377}
378
380
381 FILE * file = vtk_file->file;
382
383 fprintf(file, "POINT_DATA %u\n", vtk_file->num_points);
384
385 fprintf(file, "SCALARS point_ids int 1\n");
386 fprintf(file, "LOOKUP_TABLE default\n");
387 for (unsigned i = 0; i < vtk_file->num_points; ++i) {
388#ifdef WRITE_ASCII
389 fprintf(file, "%u\n", i);
390#else
391 fwrite(&i, sizeof(i), 1, file);
392#endif
393 }
394#ifndef WRITE_ASCII
395 fprintf(file, "\n");
396#endif
397
398 if (vtk_file->num_scalars_point_data > 0)
400 vtk_file->num_scalars_point_data, vtk_file->num_points);
401}
402
403void yac_vtk_close(YAC_VTK_FILE * vtk_file) {
404
405 FILE * file = vtk_file->file;
406
408 free(vtk_file->scalars_cell_data);
410 free(vtk_file->scalars_point_data);
411
412 xfclose(file);
413
414 free(vtk_file);
415}
416
static void LLtoXYZ(double lon, double lat, double p_out[])
Definition geometry.h:287
enum callback_type type
#define xrealloc(ptr, size)
Definition ppm_xfuncs.h:67
#define xmalloc(size)
Definition ppm_xfuncs.h:66
#define xfclose(fp)
Definition ppm_xfuncs.h:79
#define xfopen(path, mode)
Definition ppm_xfuncs.h:74
unsigned num_points
Definition vtk_output.c:39
struct scalars_data * scalars_cell_data
Definition vtk_output.c:41
struct scalars_data * scalars_point_data
Definition vtk_output.c:41
unsigned num_cells
Definition vtk_output.c:39
unsigned num_scalars_cell_data
Definition vtk_output.c:42
unsigned num_scalars_point_data
Definition vtk_output.c:42
char * name
Definition vtk_output.c:32
void * data
Definition vtk_output.c:31
enum scalars_dt dt
Definition vtk_output.c:30
void yac_vtk_write_cell_scalars_uint(YAC_VTK_FILE *vtk_file, unsigned *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:243
void yac_vtk_write_cell_scalars_float(YAC_VTK_FILE *vtk_file, float *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:257
void yac_vtk_write_cell_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:264
void yac_vtk_write_cell_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_cells, char *name)
Definition vtk_output.c:250
void yac_vtk_write_point_scalars_uint(YAC_VTK_FILE *vtk_file, unsigned *scalars, unsigned num_points, char *name)
Definition vtk_output.c:271
scalars_dt
Definition vtk_output.c:15
@ DOUBLE
Definition vtk_output.c:19
@ UINT
Definition vtk_output.c:17
@ FLOAT
Definition vtk_output.c:18
@ INT
Definition vtk_output.c:16
void yac_vtk_write_point_data_ll(YAC_VTK_FILE *vtk_file, double *point_data_lon, double *point_data_lat, unsigned num_points)
Definition vtk_output.c:149
static void vtk_flush_scalar_point_data(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:379
void yac_vtk_write_point_scalars_double(YAC_VTK_FILE *vtk_file, double *scalars, unsigned num_points, char *name)
Definition vtk_output.c:292
void yac_vtk_write_point_data(YAC_VTK_FILE *vtk_file, double *point_data, unsigned num_points)
Definition vtk_output.c:69
static void yac_vtk_write_cell_scalars(YAC_VTK_FILE *vtk_file, void *scalars, unsigned num_cells, char *name, enum scalars_dt dt)
Definition vtk_output.c:165
void yac_vtk_write_point_scalars_int(YAC_VTK_FILE *vtk_file, int *scalars, unsigned num_points, char *name)
Definition vtk_output.c:278
const char * scalars_dt_names[4]
Definition vtk_output.c:22
void yac_vtk_close(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:403
static void vtk_flush_scalar_data(FILE *file, struct scalars_data *data, unsigned num_scalars_data, unsigned num_data_points)
Definition vtk_output.c:299
static void vtk_flush_scalar_cell_data(YAC_VTK_FILE *vtk_file)
Definition vtk_output.c:355
static void yac_vtk_write_point_scalars(YAC_VTK_FILE *vtk_file, void *scalars, unsigned num_points, char *name, enum scalars_dt dt)
Definition vtk_output.c:204
void yac_vtk_write_cell_data(YAC_VTK_FILE *vtk_file, unsigned *cell_corners, unsigned *num_points_per_cell, unsigned num_cells)
Definition vtk_output.c:88
YAC_VTK_FILE * yac_vtk_open(const char *filename, const char *title)
Definition vtk_output.c:45
void yac_vtk_write_point_scalars_float(YAC_VTK_FILE *vtk_file, float *scalars, unsigned num_points, char *name)
Definition vtk_output.c:285
general routines for writing vtk files
static size_t num_points
Definition yac.c:121
#define YAC_ASSERT(exp, msg)
Definition yac_assert.h:15