Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/src/t8_vtk.c
900 views
1
/*
2
This file is part of t8code.
3
t8code is a C library to manage a collection (a forest) of multiple
4
connected adaptive space-trees of general element classes in parallel.
5
6
Copyright (C) 2015 the developers
7
8
t8code is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
t8code is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with t8code; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
#include <t8_vtk.h>
24
25
/* Writes the pvtu header file that links to the processor local files.
26
* This function should only be called by one process.
27
* Return 0 on success. */
28
int
29
t8_write_pvtu (const char *filename, int num_procs, int write_tree, int write_rank, int write_level, int write_id,
30
int num_data, t8_vtk_data_field_t *data)
31
{
32
char pvtufilename[BUFSIZ], filename_copy[BUFSIZ];
33
FILE *pvtufile;
34
int p, idata, num_scalars = 0;
35
int write_cell_data, wrote_cell_data = 0;
36
int printed = 0;
37
int sreturn;
38
39
write_cell_data = write_tree || write_rank || write_level || write_id || num_data > 0;
40
41
sreturn = snprintf (pvtufilename, BUFSIZ, "%s.pvtu", filename);
42
43
if (sreturn >= BUFSIZ) {
44
/* The filename was truncated */
45
/* Note: gcc >= 7.1 prints a warning if we
46
* do not check the return value of snprintf. */
47
t8_debugf ("Warning: Truncated vtk file name to '%s'\n", pvtufilename);
48
}
49
50
pvtufile = fopen (pvtufilename, "wb");
51
if (!pvtufile) {
52
t8_global_errorf ("Could not open %s for output\n", pvtufilename);
53
return -1;
54
}
55
56
fprintf (pvtufile, "<?xml version=\"1.0\"?>\n");
57
fprintf (pvtufile, "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\"");
58
#ifdef SC_IS_BIGENDIAN
59
fprintf (pvtufile, " byte_order=\"BigEndian\">\n");
60
#else
61
fprintf (pvtufile, " byte_order=\"LittleEndian\">\n");
62
#endif
63
64
fprintf (pvtufile, " <PUnstructuredGrid GhostLevel=\"0\">\n");
65
fprintf (pvtufile, " <PPoints>\n");
66
fprintf (pvtufile,
67
" <PDataArray type=\"%s\" Name=\"Position\""
68
" NumberOfComponents=\"3\" format=\"%s\"/>\n",
69
T8_VTK_FLOAT_NAME, T8_VTK_FORMAT_STRING);
70
fprintf (pvtufile, " </PPoints>\n");
71
72
if (num_data > 0) {
73
/* Print point data for data fields */
74
char vtkPointDataString[BUFSIZ] = "";
75
char vtkPointVectorString[BUFSIZ] = "";
76
char description[BUFSIZ];
77
78
for (idata = 0; idata < num_data && data[idata].type == T8_VTK_SCALAR; idata++) {
79
sreturn = snprintf (description, BUFSIZ, "%s_%s", data[idata].description, "points");
80
81
if (sreturn >= BUFSIZ) {
82
/* The output was truncated */
83
/* Note: gcc >= 7.1 prints a warning if we
84
* do not check the return value of snprintf. */
85
t8_debugf ("Warning: Truncated vtk point data description to '%s'\n", description);
86
}
87
88
printed += snprintf (vtkPointDataString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "", description);
89
}
90
num_scalars = idata;
91
/* Write Vector fields in data */
92
if (num_scalars < num_data) {
93
printed = 0;
94
for (idata = num_scalars; idata < num_data; idata++) {
95
SC_CHECK_ABORT (data[idata].type == T8_VTK_VECTOR, "vtk data mismatch. After scalar fields only vector"
96
" fields are allowed.");
97
sreturn = snprintf (description, BUFSIZ, "%s_%s", data[idata].description, "points");
98
99
if (sreturn >= BUFSIZ) {
100
/* The output was truncated */
101
/* Note: gcc >= 7.1 prints a warning if we
102
* do not check the return value of snprintf. */
103
t8_debugf ("Warning: Truncated vtk point data description to '%s'\n", description);
104
}
105
printed
106
+= snprintf (vtkPointVectorString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "", description);
107
}
108
}
109
110
if (strcmp (vtkPointDataString, "")) {
111
fprintf (pvtufile, " <PPointData Scalars=\"%s\"", vtkPointDataString);
112
if (num_scalars < num_data) {
113
/* Add Vector strings */
114
fprintf (pvtufile, " Vectors=\"%s\">\n", vtkPointVectorString);
115
}
116
else {
117
fprintf (pvtufile, ">\n");
118
}
119
/* Write data fields */
120
for (idata = 0; idata < num_scalars; idata++) {
121
sreturn = snprintf (description, BUFSIZ, "%s_%s", data[idata].description, "points");
122
123
if (sreturn >= BUFSIZ) {
124
/* The output was truncated */
125
/* Note: gcc >= 7.1 prints a warning if we
126
* do not check the return value of snprintf. */
127
t8_debugf ("Warning: Truncated vtk point data description to '%s'\n", description);
128
}
129
130
fprintf (pvtufile,
131
" "
132
"<PDataArray type=\"%s\" Name=\"%s\" format=\"%s\"/>\n",
133
T8_VTK_FLOAT_NAME, description, T8_VTK_FORMAT_STRING);
134
}
135
136
/* Write vector data fields */
137
for (idata = num_scalars; idata < num_data; idata++) {
138
T8_ASSERT (data[idata].type == T8_VTK_VECTOR);
139
sreturn = snprintf (description, BUFSIZ, "%s_%s", data[idata].description, "points");
140
141
if (sreturn >= BUFSIZ) {
142
/* The output was truncated */
143
/* Note: gcc >= 7.1 prints a warning if we
144
* do not check the return value of snprintf. */
145
t8_debugf ("Warning: Truncated vtk point data description to '%s'\n", description);
146
}
147
148
fprintf (pvtufile,
149
" "
150
"<PDataArray type=\"%s\" Name=\"%s\" NumberOfComponents=\"3\" "
151
"format=\"%s\"/>\n",
152
T8_VTK_FLOAT_NAME, description, T8_VTK_FORMAT_STRING);
153
}
154
fprintf (pvtufile, " </PPointData>\n");
155
}
156
}
157
/* reset counter */
158
printed = 0;
159
if (write_cell_data) {
160
char vtkCellDataString[BUFSIZ] = "";
161
char vtkCellVectorString[BUFSIZ] = "";
162
163
if (write_tree) {
164
printed += snprintf (vtkCellDataString + printed, BUFSIZ - printed, "treeid");
165
}
166
if (write_rank) {
167
printed += snprintf (vtkCellDataString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "", "mpirank");
168
}
169
if (write_level) {
170
printed += snprintf (vtkCellDataString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "", "level");
171
}
172
if (write_id) {
173
printed += snprintf (vtkCellDataString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "", "element_id");
174
}
175
for (idata = 0; idata < num_data && data[idata].type == T8_VTK_SCALAR; idata++) {
176
printed += snprintf (vtkCellDataString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "",
177
data[idata].description);
178
}
179
num_scalars = idata;
180
/* Write Vector fields in data */
181
if (num_scalars < num_data) {
182
printed = 0;
183
for (idata = num_scalars; idata < num_data; idata++) {
184
SC_CHECK_ABORT (data[idata].type == T8_VTK_VECTOR, "vtk data mismatch. After scalar fields only vector"
185
" fields are allowed.");
186
printed += snprintf (vtkCellVectorString + printed, BUFSIZ - printed, "%s%s", printed > 0 ? "," : "",
187
data[idata].description);
188
}
189
}
190
if (strcmp (vtkCellDataString, "")) {
191
fprintf (pvtufile, " <PCellData Scalars=\"%s\"", vtkCellDataString);
192
if (num_scalars < num_data) {
193
/* Add Vector strings */
194
fprintf (pvtufile, " Vectors=\"%s\">\n", vtkCellVectorString);
195
}
196
else {
197
fprintf (pvtufile, ">\n");
198
}
199
wrote_cell_data = 1;
200
}
201
}
202
if (write_tree) {
203
fprintf (pvtufile,
204
" "
205
"<PDataArray type=\"%s\" Name=\"treeid\" format=\"%s\"/>\n",
206
T8_VTK_GLOIDX, T8_VTK_FORMAT_STRING);
207
}
208
if (write_rank) {
209
fprintf (pvtufile,
210
" "
211
"<PDataArray type=\"%s\" Name=\"mpirank\" format=\"%s\"/>\n",
212
"Int32", T8_VTK_FORMAT_STRING);
213
}
214
if (write_level) {
215
fprintf (pvtufile,
216
" "
217
"<PDataArray type=\"%s\" Name=\"level\" format=\"%s\"/>\n",
218
"Int32", T8_VTK_FORMAT_STRING);
219
}
220
if (write_id) {
221
fprintf (pvtufile,
222
" "
223
"<PDataArray type=\"%s\" Name=\"element_id\" format=\"%s\"/>\n",
224
T8_VTK_LOCIDX, T8_VTK_FORMAT_STRING);
225
}
226
/* Write data fields */
227
for (idata = 0; idata < num_scalars; idata++) {
228
fprintf (pvtufile,
229
" "
230
"<PDataArray type=\"%s\" Name=\"%s\" format=\"%s\"/>\n",
231
T8_VTK_FLOAT_NAME, data[idata].description, T8_VTK_FORMAT_STRING);
232
}
233
234
/* Write vector data fields */
235
for (idata = num_scalars; idata < num_data; idata++) {
236
T8_ASSERT (data[idata].type == T8_VTK_VECTOR);
237
fprintf (pvtufile,
238
" "
239
"<PDataArray type=\"%s\" Name=\"%s\" NumberOfComponents=\"3\" "
240
"format=\"%s\"/>\n",
241
T8_VTK_FLOAT_NAME, data[idata].description, T8_VTK_FORMAT_STRING);
242
}
243
if (wrote_cell_data) {
244
fprintf (pvtufile, " </PCellData>\n");
245
}
246
247
sreturn = snprintf (filename_copy, BUFSIZ, "%s", filename);
248
if (sreturn >= BUFSIZ) {
249
/* The Filename was truncated */
250
/* Note: gcc >= 7.1 prints a warning if we
251
* do not check the return value of snprintf. */
252
t8_debugf ("Warning: Truncated vtk file name copy to '%s'\n", filename_copy);
253
}
254
for (p = 0; p < num_procs; ++p) {
255
fprintf (pvtufile, " <Piece Source=\"%s_%04d.vtu\"/>\n", basename (filename_copy), p);
256
}
257
fprintf (pvtufile, " </PUnstructuredGrid>\n");
258
fprintf (pvtufile, "</VTKFile>\n");
259
260
/* Close paraview master file */
261
if (ferror (pvtufile)) {
262
t8_global_errorf ("t8_vtk: Error writing parallel footer\n");
263
fclose (pvtufile);
264
return -1;
265
}
266
if (fclose (pvtufile)) {
267
t8_global_errorf ("t8_vtk: Error closing parallel footer\n");
268
return -1;
269
}
270
return 0;
271
}
272
273