1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
| // Helper functions for output_vtu.h
// Function to write the VTU file header
void write_vtu_header(FILE *fp, long no_points, long no_cells) {
fputs("<?xml version=\"1.0\"?>\n", fp);
fputs("<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n", fp);
fputs("\t<UnstructuredGrid>\n", fp);
fprintf(fp, "\t\t<Piece NumberOfPoints=\"%ld\" NumberOfCells=\"%ld\">\n", no_points, no_cells);
}
// Function to write scalar data arrays to the VTU file
void write_scalar_light_data(FILE *fp, scalar *list, vector *vlist, long *count, long no_cells) {
fputs("\t\t\t<CellData Scalars=\"scalars\">\n", fp);
for (scalar s in list) {
fprintf(fp, "\t\t\t\t<DataArray type=\"Float64\" Name=\"%s\" format=\"appended\" offset=\"%ld\"/>\n", s.name, *count);
*count += (no_cells * sizeof(double)) + sizeof(long);
}
for (vector v in vlist) {
fprintf(fp, "\t\t\t\t<DataArray type=\"Float64\" Name=\"%s\" NumberOfComponents=\"3\" format=\"appended\" offset=\"%ld\"/>\n", v.x.name, *count);
*count += (3 * no_cells * sizeof(double)) + sizeof(long);
}
fputs("\t\t\t</CellData>\n", fp);
}
// Function to write points data array to the VTU file
void write_points_light_data(FILE *fp, long *count, long no_points) {
fputs("\t\t\t<Points>\n", fp);
fprintf(fp, "\t\t\t\t<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"appended\" offset=\"%ld\"/>\n", *count);
fputs("\t\t\t</Points>\n", fp);
*count += (3 * no_points * sizeof(double)) + sizeof(long);
}
// Function to write cells data arrays to the VTU file
void write_cells_light_data(FILE *fp, long *count, long no_cells, long no_cells_offset) {
fputs("\t\t\t<Cells>\n", fp);
fprintf(fp, "\t\t\t\t<DataArray type=\"Int64\" Name=\"offsets\" format=\"appended\" offset=\"%ld\"/>\n", *count);
*count += (no_cells * sizeof(long)) + sizeof(long);
fprintf(fp, "\t\t\t\t<DataArray type=\"Int8\" Name=\"types\" format=\"appended\" offset=\"%ld\"/>\n", *count);
*count += (no_cells * sizeof(char)) + sizeof(long);
fprintf(fp, "\t\t\t\t<DataArray type=\"Int64\" Name=\"connectivity\" format=\"appended\" offset=\"%ld\"/>\n", *count);
*count += (no_cells_offset * sizeof(long)) + sizeof(long);
fputs("\t\t\t</Cells>\n", fp);
}
// Function to write appended data section to the VTU file
void write_vtu_appended(FILE *fp) {
fputs("\t\t</Piece>\n", fp);
fputs("\t</UnstructuredGrid>\n", fp);
fputs("\t<AppendedData encoding=\"raw\">\n", fp);
fputs("_", fp);
}
// Function to write scalar field data
void write_scalar_heavy_data(FILE *fp, scalar *list, scalar per_mask, long no_cells) {
long block_len = no_cells * sizeof(double);
for (scalar s in list) {
fwrite(&block_len, sizeof(long), 1, fp);
foreach () {
if (per_mask[]) {
fwrite(&val(s), sizeof(double), 1, fp);
}
}
}
}
void write_scalar_heavy_data_slice(FILE *fp, scalar *list, scalar per_mask, long no_cells, coord n = {0,0,1}, double _alpha = 0) {
long block_len = no_cells * sizeof(double);
for (scalar s in list) {
fwrite(&block_len, sizeof(long), 1, fp);
foreach () {
if (per_mask[]){
double sval;
if (n.x == 1)
sval = 0.5*(val(s) + val(s,1,0,0));
else if (n.y == 1)
sval = 0.5*(val(s) + val(s,0,1,0));
else
sval = 0.5*(val(s) + val(s,0,0,1));
fwrite (&sval, sizeof (double), 1, fp);
}
}
}
}
void write_scalar_heavy_data_array(FILE *fp, long no_cells, double *pt_array_s) {
long block_len = no_cells * sizeof(double);
fwrite(&block_len, sizeof(long), 1, fp);
for (int i = 0; i < no_cells; i++) {
fwrite(&pt_array_s[i], sizeof(double), 1, fp);
}
}
// Function to write vector field data
void write_vector_heavy_data(FILE *fp, vector *vlist, scalar per_mask, long no_cells) {
long block_len = no_cells * 3 * sizeof(double);
for (vector v in vlist) {
fwrite(&block_len, sizeof(long), 1, fp);
foreach () {
if (per_mask[]) {
fwrite(&val(v.x), sizeof(double), 1, fp);
fwrite(&val(v.y), sizeof(double), 1, fp);
#if dimension == 2
double vz = 0;
fwrite(&vz, sizeof(double), 1, fp);
#elif dimension == 3
fwrite(&val(v.z), sizeof(double), 1, fp);
#endif
}
}
}
}
void write_vector_heavy_data_slice(FILE *fp, vector *vlist, scalar per_mask, long no_cells, coord n = {0,0,1}, double _alpha = 0) {
long block_len = no_cells * 3 * sizeof(double);
for (vector v in vlist) {
fwrite(&block_len, sizeof(long), 1, fp);
foreach () {
if (per_mask[]){
double xval, yval, zval;
if (n.x == 1) {
xval = 0.5*(val(v.x) + val(v.x,1,0,0));
yval = 0.5*(val(v.y) + val(v.y,1,0,0));
#if dimension == 3
zval = 0.5*(val(v.z) + val(v.z,1,0,0));
#else
zval = 0;
#endif
}
else if (n.y == 1) {
xval = 0.5*(val(v.x) + val(v.x,0,1,0));
yval = 0.5*(val(v.y) + val(v.y,0,1,0));
#if dimension == 3
zval = 0.5*(val(v.z) + val(v.z,0,1,0));
#else
zval = 0;
#endif
}
else {
xval = 0.5*(val(v.x) + val(v.x,0,0,1));
yval = 0.5*(val(v.y) + val(v.y,0,0,1));
#if dimension == 3
zval = 0.5*(val(v.z) + val(v.z,0,0,1));
#else
zval = 0;
#endif
}
fwrite (&xval, sizeof (double), 1, fp);
fwrite (&yval, sizeof (double), 1, fp);
fwrite (&zval, sizeof (double), 1, fp);
}
}
}
}
// Function to write points data
void write_points_heavy_data(FILE *fp, long no_points) {
long block_len = no_points * 3 * sizeof(double);
fwrite(&block_len, sizeof(long), 1, fp);
foreach_vertex() {
fwrite(&x, sizeof(double), 1, fp);
fwrite(&y, sizeof(double), 1, fp);
fwrite(&z, sizeof(double), 1, fp);
}
}
void write_points_heavy_data_slice(FILE *fp, long no_points, coord n = {0,0,1}, double _alpha = 0) {
long block_len = no_points * 3 * sizeof(double);
fwrite(&block_len, sizeof(long), 1, fp);
foreach_vertex() {
shortcut_slice(n,_alpha);
fwrite(&x, sizeof(double), 1, fp);
fwrite(&y, sizeof(double), 1, fp);
fwrite(&z, sizeof(double), 1, fp);
}
}
// Function to write the points (vertices) data to the VTK file
void write_points_heavy_data_array(FILE *fp, long no_points, double *pt_array_x, double *pt_array_y, double *pt_array_z) {
long block_len = no_points * 3 * sizeof(double);
fwrite(&block_len, sizeof(long), 1, fp);
for (int i = 0; i < no_points; i++) {
fwrite(&pt_array_x[i], sizeof(double), 1, fp);
fwrite(&pt_array_y[i], sizeof(double), 1, fp);
#if dimension == 2
double vz = 0;
fwrite(&vz, sizeof(double), 1, fp);
#elif dimension == 3
fwrite(&pt_array_z[i], sizeof(double), 1, fp);
#endif
}
}
// Function to write cell offsets
void write_cell_offsets(FILE *fp, long no_cells, char noffset) {
long block_len = no_cells * sizeof(long);
fwrite(&block_len, sizeof(long), 1, fp);
for (int i = 0; i < no_cells; i++) {
long offset = (i + 1) * noffset;
fwrite(&offset, sizeof(int64_t), 1, fp);
}
}
void write_cell_offsets2(FILE *fp, long nfacets, long *offsets) {
long block_len = nfacets * sizeof(long);
fwrite(&block_len, sizeof(long), 1, fp);
for (int ii = 0; ii < nfacets; ii++)
fwrite(&offsets[ii], sizeof(long), 1, fp);
}
void write_cell_offsets3(FILE *fp, long no_cells) {
long block_len = no_cells * sizeof(long);
fwrite(&block_len, sizeof(long), 1, fp);
for (long i = 0; i < no_cells; i++) {
fwrite(&i, sizeof(int64_t), 1, fp);
}
}
// Function to write cell types
void write_cell_types(FILE *fp, long no_cells, char type) {
long block_len = no_cells * sizeof(char);
fwrite(&block_len, sizeof(long), 1, fp);
for (int i = 0; i < no_cells; i++) {
fwrite(&type, sizeof(char), 1, fp);
}
}
// Function to write cell connectivity
void write_cell_connectivity(FILE *fp, vertex scalar marker, scalar per_mask, long no_cells, char noffset) {
long block_len = no_cells * noffset * sizeof(long);
fwrite(&block_len, sizeof(long), 1, fp);
foreach (serial, noauto) {
if (per_mask[]) {
long connectivity[noffset];
connectivity[0] = (long)marker[];
connectivity[1] = (long)marker[1];
connectivity[2] = (long)marker[1, 1];
connectivity[3] = (long)marker[0, 1];
#if dimension == 3
connectivity[4] = (long)marker[0, 0, 1];
connectivity[5] = (long)marker[1, 0, 1];
connectivity[6] = (long)marker[1, 1, 1];
connectivity[7] = (long)marker[0, 1, 1];
#endif
fwrite(connectivity, sizeof(long), noffset, fp);
}
}
}
void write_cell_connectivity_slice(FILE *fp, vertex scalar marker, scalar per_mask, long no_cells, char noffset, coord n = {0,0,1} ) {
long block_len = no_cells * noffset * sizeof(long);
fwrite(&block_len, sizeof(long), 1, fp);
foreach (serial, noauto) {
if (per_mask[]) {
long connectivity[noffset];
if (n.x == 1) {
connectivity[0] = (long)marker[1,0,0];
connectivity[1] = (long)marker[1,1,0];
connectivity[2] = (long)marker[1,1,1];
connectivity[3] = (long)marker[1,0,1];
}
else if (n.y == 1) {
connectivity[0] = (long)marker[0,1,0];
connectivity[1] = (long)marker[1,1,0];
connectivity[2] = (long)marker[1,1,1];
connectivity[3] = (long)marker[0,1,1];
}
else {
connectivity[0] = (long)marker[0,0,1];
connectivity[1] = (long)marker[1,0,1];
connectivity[2] = (long)marker[1,1,1];
connectivity[3] = (long)marker[0,1,1];
}
fwrite(connectivity, sizeof(long), noffset, fp);
}
}
}
// Helper function implementations (parallel)
// Function to write the header of the PVTU file
void write_pvtu_header(FILE *fp) {
fputs("<?xml version=\"1.0\"?>\n", fp);
fputs("<VTKFile type=\"PUnstructuredGrid\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n", fp);
fputs("\t<PUnstructuredGrid GhostLevel=\"0\">\n", fp);
}
// Function to write scalar data arrays to the PVTU file
void write_scalar_light_pdata(FILE *fp, scalar *list, vector *vlist) {
fputs("\t\t<PCellData Scalars=\"scalars\">\n", fp);
for (scalar s in list) {
fprintf(fp, "\t\t\t<PDataArray type=\"Float64\" Name=\"%s\" format=\"appended\"/>\n", s.name);
}
for (vector v in vlist) {
fprintf(fp, "\t\t\t<PDataArray type=\"Float64\" NumberOfComponents=\"3\" Name=\"%s\" format=\"appended\"/>\n", v.x.name);
}
fputs("\t\t</PCellData>\n", fp);
}
// Function to write points data array to the PVTU file
void write_points_light_pdata(FILE *fp) {
fputs("\t\t<PPoints>\n", fp);
fputs("\t\t\t<PDataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"appended\"/>\n", fp);
fputs("\t\t</PPoints>\n", fp);
}
// Function to write piece references for each process's VTU file to the PVTU file
void write_pieces_light_pdata(FILE *fp, char *subname) {
for (int i = 0; i < npe(); i++) {
fprintf(fp, "\t\t<Piece Source=\"%s_n%3.3d.vtu\"/>\n", subname, i);
}
fputs("\t</PUnstructuredGrid>\n", fp);
fputs("</VTKFile>\n", fp);
}
// Function to count the number of vertices and facets
void count_vertices_and_facets(scalar c, long *nverts, long *nfacets) {
foreach (serial, noauto) {
if (c[] > 1e-6 && c[] < 1. - 1e-6) {
shortcut_facets
for (int i = 0; i < m; i++)
(*nverts)++;
if (m > 0)
(*nfacets)++;
}
}
}
// Function to populate vertex coordinates and facet offsets
void populate_vertex_and_offset_arrays(scalar c, long nverts, long nfacets, double *pt_array_x, double *pt_array_y, double *pt_array_z, long *offsets) {
long iverts = 0, ifacet = 0, offset = 0;
foreach (serial, noauto) {
if (c[] > 1e-6 && c[] < 1. - 1e-6) {
shortcut_facets // we cycle if cell is not at the interface
// Calculate and store vertex coordinates
coord _p = {x, y, z};
for (int i = 0; i < m; i++) {
pt_array_x[iverts] = _p.x + v[i].x * Delta;
pt_array_y[iverts] = _p.y + v[i].y * Delta;
#if dimension == 3
pt_array_z[iverts] = _p.z + v[i].z * Delta;
#endif
iverts++;
}
// Store facet offset if there are vertices in the facet
if (m > 0) {
offset += m;
offsets[ifacet] = offset;
ifacet++;
}
}
}
}
// Function to populate arrays with values at the facets
void populate_facet_arrays(scalar c, scalar s, long nverts, long nfacets, double *val_array_s) {
long ifacet = 0;
foreach (serial, noauto) {
if (c[] > 1e-6 && c[] < 1. - 1e-6) {
shortcut_facets // we cycle if cell is not at the interface
if (m > 0){
val_array_s[ifacet] = s[];
ifacet++;
}
}
}
}
|