PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
vtk_io.c
Go to the documentation of this file.
1#include "vtk_io.h"
3
4//================================================================================
5// STATIC (PRIVATE) HELPER FUNCTION PROTOTYPES
6//================================================================================
7
8static PetscErrorCode WriteVTSXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset);
9static PetscErrorCode WriteVTPXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset);
10static PetscErrorCode WriteVTKFileHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset);
11static PetscErrorCode CopyOwnedOutputBox(Vec vec, const PetscInt extent[3], PetscInt start_out[3], PetscInt size_out[3],
12 PetscScalar **out);
13
14//================================================================================
15// IMPLEMENTATION OF PRIVATE HELPERS
16//================================================================================
17
18/**
19 * @brief Write the XML header for a VTK structured-grid (`.vts`) file.
20 */
21static PetscErrorCode WriteVTSXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
22{
24 const char *byte_order = "LittleEndian";
25 const char *precision = "Float64";
26
27 const PetscInt header = (PetscInt)sizeof(uint64_t);
28
29 fprintf(fp, "<?xml version=\"1.0\"?>\n");
30 fprintf(fp, "<VTKFile type=\"StructuredGrid\" version=\"1.0\" byte_order=\"%s\" header_type=\"UInt64\">\n", byte_order);
31 fprintf(fp, " <StructuredGrid WholeExtent=\"0 %" PetscInt_FMT " 0 %" PetscInt_FMT " 0 %" PetscInt_FMT "\">\n", meta->mx - 1, meta->my - 1, meta->mz - 1);
32 fprintf(fp, " <Piece Extent=\"0 %" PetscInt_FMT " 0 %" PetscInt_FMT " 0 %" PetscInt_FMT "\">\n", meta->mx - 1, meta->my - 1, meta->mz - 1);
33
34 fprintf(fp, " <Points>\n");
35 fprintf(fp, " <DataArray type=\"%s\" Name=\"Position\" NumberOfComponents=\"3\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
36 precision, *boffset);
37 *boffset += header + 3 * meta->npoints * (PetscInt)sizeof(PetscScalar);
38 fprintf(fp, " </Points>\n");
39
40 if (meta->num_point_data_fields > 0) {
41 fprintf(fp, " <PointData>\n");
42 for (PetscInt i = 0; i < meta->num_point_data_fields; i++) {
43 const VTKFieldInfo* field = &meta->point_data_fields[i];
44 fprintf(fp, " <DataArray type=\"%s\" Name=\"%s\" NumberOfComponents=\"%" PetscInt_FMT "\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
45 precision, field->name, field->num_components, *boffset);
46 *boffset += header + field->num_components * meta->npoints * (PetscInt)sizeof(PetscScalar);
47 }
48 fprintf(fp, " </PointData>\n");
49 }
50
51 fprintf(fp, " </Piece>\n");
52 fprintf(fp, " </StructuredGrid>\n");
53 fprintf(fp, " <AppendedData encoding=\"raw\">\n_");
54
56 return 0;
57}
58
59/**
60 * @brief Write the XML header for a VTK polydata (`.vtp`) file.
61 */
62static PetscErrorCode WriteVTPXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
63{
65 const char *byte_order = "LittleEndian";
66 const char *precision = "Float64";
67 const char *int_type_str = (sizeof(PetscInt) == 8) ? "Int64" : "Int32";
68 const PetscInt header = (PetscInt)sizeof(uint64_t);
69
70 fprintf(fp, "<?xml version=\"1.0\"?>\n");
71 fprintf(fp, "<VTKFile type=\"PolyData\" version=\"1.0\" byte_order=\"%s\" header_type=\"UInt64\">\n", byte_order);
72 fprintf(fp, " <PolyData>\n");
73 fprintf(fp, " <Piece NumberOfPoints=\"%" PetscInt_FMT "\" NumberOfVerts=\"%" PetscInt_FMT "\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n",
74 meta->npoints, meta->npoints);
75
76 fprintf(fp, " <Points>\n");
77 fprintf(fp, " <DataArray type=\"%s\" Name=\"Position\" NumberOfComponents=\"3\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
78 precision, *boffset);
79 *boffset += header + 3 * meta->npoints * (PetscInt)sizeof(PetscScalar);
80 fprintf(fp, " </Points>\n");
81
82 if (meta->num_point_data_fields > 0) {
83 fprintf(fp, " <PointData>\n");
84 for (PetscInt i = 0; i < meta->num_point_data_fields; i++) {
85 const VTKFieldInfo* field = &meta->point_data_fields[i];
86 fprintf(fp, " <DataArray type=\"%s\" Name=\"%s\" NumberOfComponents=\"%" PetscInt_FMT "\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
87 precision, field->name, field->num_components, *boffset);
88 *boffset += header + field->num_components * meta->npoints * (PetscInt)sizeof(PetscScalar);
89 }
90 fprintf(fp, " </PointData>\n");
91 }
92
93 fprintf(fp, " <Verts>\n");
94 fprintf(fp, " <DataArray type=\"%s\" Name=\"connectivity\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
95 int_type_str, *boffset);
96 *boffset += header + meta->npoints * (PetscInt)sizeof(PetscInt);
97 fprintf(fp, " <DataArray type=\"%s\" Name=\"offsets\" format=\"appended\" offset=\"%" PetscInt_FMT "\" />\n",
98 int_type_str, *boffset);
99 *boffset += header + meta->npoints * (PetscInt)sizeof(PetscInt);
100 fprintf(fp, " </Verts>\n");
101
102 fprintf(fp, " </Piece>\n");
103 fprintf(fp, " </PolyData>\n");
104 fprintf(fp, " <AppendedData encoding=\"raw\">\n_");
105
107 return 0;
108}
109
110/**
111 * @brief Open a VTK XML document and write its file-level header.
112 * @details `meta->fileType` is VTK_STRUCTURED or VTK_POLYDATA; CreateVTKFileFromMetadata(), the
113 * only caller, rejects any other type before calling.
114 */
115static PetscErrorCode WriteVTKFileHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
116{
118 const PetscErrorCode ierr = (meta->fileType == VTK_STRUCTURED) ? WriteVTSXMLHeader(fp, meta, boffset)
119 : WriteVTPXMLHeader(fp, meta, boffset);
121 return ierr;
122}
123
124/**
125 * @brief Copy this rank's part of the output grid out of a DMDA global vector.
126 * @details The output grid is the first `extent` nodes on each axis (the last DA node is a
127 * dummy). This rank's part is its owned box clipped to that grid, copied point by
128 * point (i fastest) with the vector's components interleaved. `start_out` and `size_out`
129 * receive the box's first point and extent on each axis; either may be NULL.
130 */
131static PetscErrorCode CopyOwnedOutputBox(Vec vec, const PetscInt extent[3], PetscInt start_out[3], PetscInt size_out[3],
132 PetscScalar **out)
133{
134 DM dm = NULL;
135 DMDALocalInfo info;
136 const PetscScalar *values = NULL;
137 PetscInt p = 0, start_box[3], size_box[3];
138 PetscInt *start = start_out ? start_out : start_box;
139 PetscInt *size = size_out ? size_out : size_box;
140
141 PetscFunctionBeginUser;
143 PetscCall(VecGetDM(vec, &dm));
144 PetscCheck(dm != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "VTK output needs a vector attached to a DMDA.");
145 PetscCall(DMDAGetLocalInfo(dm, &info));
146 const PetscInt owned_start[3] = {info.xs, info.ys, info.zs}, owned[3] = {info.xm, info.ym, info.zm};
147 for (int d = 0; d < 3; ++d) {
148 start[d] = owned_start[d];
149 size[d] = PetscMax(0, PetscMin(owned_start[d] + owned[d], extent[d]) - owned_start[d]);
150 }
151 PetscCall(PetscMalloc1(info.dof * size[0] * size[1] * size[2], out));
152 PetscCall(VecGetArrayRead(vec, &values));
153 for (PetscInt k = 0; k < size[2]; ++k)
154 for (PetscInt j = 0; j < size[1]; ++j)
155 for (PetscInt i = 0; i < size[0]; ++i)
156 for (PetscInt c = 0; c < info.dof; ++c)
157 (*out)[p++] = values[((k * owned[1] + j) * owned[0] + i) * info.dof + c];
158 PetscCall(VecRestoreArrayRead(vec, &values));
160 PetscFunctionReturn(0);
161}
162
163
164
165/**
166 * @brief Implementation of \ref CreateVTKFileFromMetadata().
167 * @details Full API contract (arguments, ownership, side effects) is documented with
168 * the header declaration in `include/vtk_io.h`.
169 * @see CreateVTKFileFromMetadata()
170 */
171
172PetscErrorCode CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
173{
174 static const char footer[] = "\n </AppendedData>\n</VTKFile>\n";
175 const PetscBool structured = (PetscBool)(meta->fileType == VTK_STRUCTURED);
176 char tmp_filename[PETSC_MAX_PATH_LEN];
177 PetscMPIInt rank;
178 long long header_bytes = 0; /* -1: rank 0 could not write the header */
179 MPI_File fh;
180 MPI_Offset position;
181 PetscInt blocks = 0, local_points = 1, boffset = 0;
182 struct VTKBlock { PetscInt components; MPI_Datatype type; size_t size; const void *data; } block[MAX_POINT_DATA_FIELDS + 3];
183
184 PetscFunctionBeginUser;
186 PetscCheck(structured || meta->fileType == VTK_POLYDATA, comm, PETSC_ERR_ARG_WRONG, "Unknown VTK file type.");
187 PetscCallMPI(MPI_Comm_rank(comm, &rank));
188 PetscCall(PetscSNPrintf(tmp_filename, sizeof(tmp_filename), "%s.tmp", filename));
189
190 /* The appended blocks, in the order the header declares them. */
191 block[blocks++] = (struct VTKBlock){3, MPIU_SCALAR, sizeof(PetscScalar), meta->coords};
192 for (PetscInt f = 0; f < meta->num_point_data_fields; ++f)
193 block[blocks++] = (struct VTKBlock){meta->point_data_fields[f].num_components, MPIU_SCALAR, sizeof(PetscScalar),
194 meta->point_data_fields[f].data};
195 if (!structured) {
196 block[blocks++] = (struct VTKBlock){1, MPIU_INT, sizeof(PetscInt), meta->connectivity};
197 block[blocks++] = (struct VTKBlock){1, MPIU_INT, sizeof(PetscInt), meta->offsets};
198 }
199 for (int d = 0; d < (structured ? 3 : 1); ++d) local_points *= meta->local_size[d];
200
201 if (rank == 0) {
202 FILE *fp = fopen(tmp_filename, "wb");
203 if (!fp || WriteVTKFileHeader(fp, meta, &boffset)) header_bytes = -1;
204 else header_bytes = (long long)ftell(fp);
205 if (fp && fclose(fp)) header_bytes = -1;
206 }
207 PetscCallMPI(MPI_Bcast(&header_bytes, 1, MPI_LONG_LONG, 0, comm));
208 PetscCheck(header_bytes >= 0, comm, PETSC_ERR_FILE_OPEN, "Could not write the VTK header of '%s'.", tmp_filename);
209
210 /* Rank 0 writes each block's byte count; every rank writes its own points into the block. */
211 PetscCallMPI(MPI_File_open(comm, tmp_filename, MPI_MODE_WRONLY, MPI_INFO_NULL, &fh));
212 position = (MPI_Offset)header_bytes;
213 for (PetscInt b = 0; b < blocks; ++b) {
214 const uint64_t bytes = (uint64_t)block[b].components * (uint64_t)meta->npoints * block[b].size;
215 MPI_Datatype point, view;
216 PetscMPIInt count;
217
218 PetscCallMPI(MPI_File_set_view(fh, 0, MPI_BYTE, MPI_BYTE, "native", MPI_INFO_NULL));
219 if (rank == 0) PetscCallMPI(MPI_File_write_at(fh, position, &bytes, (int)sizeof(bytes), MPI_BYTE, MPI_STATUS_IGNORE));
220 position += (MPI_Offset)sizeof(bytes);
221
222 PetscCallMPI(MPI_Type_contiguous((PetscMPIInt)block[b].components, block[b].type, &point));
223 PetscCallMPI(MPI_Type_commit(&point));
224 view = point;
225 if (structured && local_points > 0) {
226 const PetscMPIInt sizes[3] = {(PetscMPIInt)meta->mz, (PetscMPIInt)meta->my, (PetscMPIInt)meta->mx};
227 const PetscMPIInt subsizes[3] = {(PetscMPIInt)meta->local_size[2], (PetscMPIInt)meta->local_size[1], (PetscMPIInt)meta->local_size[0]};
228 const PetscMPIInt starts[3] = {(PetscMPIInt)meta->local_start[2], (PetscMPIInt)meta->local_start[1], (PetscMPIInt)meta->local_start[0]};
229 PetscCallMPI(MPI_Type_create_subarray(3, sizes, subsizes, starts, MPI_ORDER_C, point, &view));
230 PetscCallMPI(MPI_Type_commit(&view));
231 }
232 PetscCallMPI(MPI_File_set_view(fh, position, point, view, "native", MPI_INFO_NULL));
233 PetscCall(PetscMPIIntCast(local_points, &count));
234 if (structured) PetscCallMPI(MPI_File_write_all(fh, block[b].data, count, point, MPI_STATUS_IGNORE));
235 else PetscCallMPI(MPI_File_write_at_all(fh, (MPI_Offset)meta->local_start[0], block[b].data, count, point, MPI_STATUS_IGNORE));
236 if (view != point) PetscCallMPI(MPI_Type_free(&view));
237 PetscCallMPI(MPI_Type_free(&point));
238 position += (MPI_Offset)bytes;
239 }
240 PetscCallMPI(MPI_File_set_view(fh, 0, MPI_BYTE, MPI_BYTE, "native", MPI_INFO_NULL));
241 if (rank == 0) PetscCallMPI(MPI_File_write_at(fh, position, footer, (int)strlen(footer), MPI_BYTE, MPI_STATUS_IGNORE));
242 PetscCallMPI(MPI_File_close(&fh));
243 if (rank == 0) PetscCheck(rename(tmp_filename, filename) == 0, PETSC_COMM_SELF, PETSC_ERR_FILE_WRITE,
244 "Could not move '%s' to '%s'.", tmp_filename, filename);
245 LOG_ALLOW(GLOBAL, LOG_INFO, "Wrote VTK file '%s'.\n", filename);
247 PetscFunctionReturn(0);
248}
249
250//================================================================================
251// PUBLIC COORDINATE PREPARATION FUNCTION
252//================================================================================
253
254/**
255 * @brief Implementation of \ref PrepareOutputCoordinates().
256 * @details Full API contract (arguments, ownership, side effects) is documented with
257 * the header declaration in `include/vtk_io.h`.
258 * @see PrepareOutputCoordinates()
259 */
260PetscErrorCode PrepareOutputCoordinates(UserCtx* user, PetscScalar** out_coords, PetscInt* out_nx, PetscInt* out_ny, PetscInt* out_nz,
261 PetscInt* out_npoints, PetscInt local_start[3], PetscInt local_size[3])
262{
263 PetscInt IM, JM, KM;
264
265 PetscFunctionBeginUser;
267 IM = user->info.mx; JM = user->info.my; KM = user->info.mz;
268 *out_nx = IM - 1;
269 *out_ny = JM - 1;
270 *out_nz = KM - 1;
271 *out_npoints = (*out_nx) * (*out_ny) * (*out_nz);
272 const PetscInt extent[3] = {*out_nx, *out_ny, *out_nz};
273 PetscCall(CopyOwnedOutputBox(user->Coor, extent, local_start, local_size, out_coords));
275 PetscFunctionReturn(0);
276}
277
278
279/**
280 * @brief Implementation of \ref BeginStructuredVTKOutput().
281 * @details Full API contract (arguments, ownership, side effects) is documented with
282 * the header declaration in `include/vtk_io.h`.
283 * @see BeginStructuredVTKOutput()
284 */
285PetscErrorCode BeginStructuredVTKOutput(UserCtx *user, VTKMetaData *meta)
286{
287 PetscFunctionBeginUser;
289 PetscCheck(user != NULL && meta != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
290 "Context and metadata are required.");
291 PetscCall(PetscMemzero(meta, sizeof(*meta)));
292 meta->fileType = VTK_STRUCTURED;
293 PetscCall(PrepareOutputCoordinates(user, &meta->coords, &meta->mx, &meta->my, &meta->mz,
294 &meta->npoints, meta->local_start, meta->local_size));
296 PetscFunctionReturn(0);
297}
298
299/**
300 * @brief Implementation of \ref AppendStructuredVTKField().
301 * @details Full API contract (arguments, ownership, side effects) is documented with
302 * the header declaration in `include/vtk_io.h`.
303 * @see AppendStructuredVTKField()
304 */
305PetscErrorCode AppendStructuredVTKField(UserCtx *user, VTKMetaData *meta, const char *name,
306 Vec field_vec, PetscInt components)
307{
308 VTKFieldInfo *entry = NULL;
309
310 PetscFunctionBeginUser;
312 PetscCheck(user != NULL && meta != NULL && name != NULL && field_vec != NULL,
313 PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Context, metadata, name, and vector are required.");
316 "Reached the %d point-data field limit; '%s' is not written.\n",
319 PetscFunctionReturn(0);
320 }
321 entry = &meta->point_data_fields[meta->num_point_data_fields];
322 PetscCall(PetscStrncpy(entry->name, name, MAX_VTK_FIELD_NAME_LENGTH));
323 entry->num_components = components;
324 PetscCall(PrepareOutputEulerianFieldData(user, field_vec, components, &entry->data));
325 meta->num_point_data_fields++;
327 PetscFunctionReturn(0);
328}
329
330/**
331 * @brief Implementation of \ref FinishStructuredVTKOutput().
332 * @details Full API contract (arguments, ownership, side effects) is documented with
333 * the header declaration in `include/vtk_io.h`.
334 * @see FinishStructuredVTKOutput()
335 */
336PetscErrorCode FinishStructuredVTKOutput(VTKMetaData *meta, const char *filename)
337{
338 PetscFunctionBeginUser;
340 PetscCheck(meta != NULL && filename != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,
341 "Metadata and filename are required.");
342 PetscCall(CreateVTKFileFromMetadata(filename, meta, PETSC_COMM_WORLD));
343 for (PetscInt index = 0; index < meta->num_point_data_fields; ++index) {
344 if (meta->point_data_fields[index].data) {
345 PetscCall(PetscFree(meta->point_data_fields[index].data));
346 }
347 }
348 if (meta->coords) PetscCall(PetscFree(meta->coords));
349 meta->num_point_data_fields = 0;
351 PetscFunctionReturn(0);
352}
353
354/**
355 * @brief Implementation of \ref PrepareOutputEulerianFieldData().
356 * @details Full API contract (arguments, ownership, side effects) is documented with
357 * the header declaration in `include/vtk_io.h`.
358 * @see PrepareOutputEulerianFieldData()
359 */
360
361PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar** out_data)
362{
363 PetscInt IM, JM, KM, dof;
364
365 PetscFunctionBeginUser;
367 IM = user->info.mx; JM = user->info.my; KM = user->info.mz;
368 PetscCall(VecGetBlockSize(field_vec, &dof));
369 PetscCheck(dof == num_components, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
370 "Field has %" PetscInt_FMT " components per node, not the %" PetscInt_FMT " requested.", dof, num_components);
371 const PetscInt extent[3] = {IM - 1, JM - 1, KM - 1};
372 PetscCall(CopyOwnedOutputBox(field_vec, extent, NULL, NULL, out_data));
374 PetscFunctionReturn(0);
375}
376
377
378/**
379 * @brief Implementation of \ref PrepareOutputParticleData().
380 * @details Full API contract (arguments, ownership, side effects) is documented with
381 * the header declaration in `include/vtk_io.h`.
382 * @see PrepareOutputParticleData()
383 */
384PetscErrorCode PrepareOutputParticleData(UserCtx* user, PostProcessParams* pps, VTKMetaData* meta, PetscInt* p_n_total)
385{
386 const PetscInt stride = pps->particle_output_freq > 0 ? pps->particle_output_freq : 1;
387 PetscInt nlocal = 0, first = 0, before = 0, kept = 0, bs = 0;
388 PetscDataType type;
389 PetscReal *position = NULL;
390 char *fields_copy = NULL, *field_name = NULL;
391
392 PetscFunctionBeginUser;
394
395 /* Particles are numbered in rank order; output keeps every stride-th one, so this rank
396 keeps its particles from local index `first` on, written from output point `before`. */
397 meta->fileType = VTK_POLYDATA;
398 PetscCall(DMSwarmGetLocalSize(user->swarm, &nlocal));
399 PetscCallMPI(MPI_Exscan(&nlocal, &before, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
400 if (user->simCtx->rank == 0) before = 0;
401 PetscCall(DMSwarmGetSize(user->swarm, p_n_total));
402 if (*p_n_total == 0) { PROFILE_FUNCTION_END; PetscFunctionReturn(0); }
403 first = (stride - before % stride) % stride;
404 kept = first < nlocal ? (nlocal - 1 - first) / stride + 1 : 0;
405
406 meta->npoints = (*p_n_total - 1) / stride + 1;
407 meta->local_start[0] = (before + first) / stride;
408 meta->local_size[0] = kept;
409 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Subsampling %" PetscInt_FMT " particles with stride %" PetscInt_FMT " -> %" PetscInt_FMT " output particles.\n",
410 *p_n_total, stride, meta->npoints);
411
412 PetscCall(DMSwarmGetField(user->swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), &bs, &type, (void **)&position));
413 PetscCheck(type == PETSC_REAL && bs == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
414 "Particle position field must be 3 PETSC_REAL components, not %" PetscInt_FMT " of %s.", bs, PetscDataTypes[type]);
415 PetscCall(PetscMalloc1(3 * kept, &meta->coords));
416 for (PetscInt i = 0; i < kept; ++i)
417 for (int d = 0; d < 3; ++d) meta->coords[3 * i + d] = position[3 * (first + i * stride) + d];
418 PetscCall(DMSwarmRestoreField(user->swarm, ParticleFieldName(PARTICLE_FIELD_ID_POSITION), &bs, &type, (void **)&position));
419
420 PetscCall(PetscStrallocpy(pps->particle_fields, &fields_copy));
421 for (field_name = strtok(fields_copy, ","); field_name && meta->num_point_data_fields < MAX_POINT_DATA_FIELDS;
422 field_name = strtok(NULL, ",")) {
423 DM swarm_to_use = user->swarm;
424 const char *internal_name = field_name;
425 PetscErrorCode check_ierr;
426 PetscInt field_local = 0, field_total = 0;
427 void *source = NULL;
428
429 TrimWhitespace(field_name);
430 if (!*field_name || strcasecmp(field_name, "position") == 0) continue;
431
432 /* A field registered on the post-processing swarm comes from there, otherwise from the solver swarm. */
433 PetscCall(PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL));
434 check_ierr = DMSwarmGetField(user->post_swarm, field_name, NULL, NULL, NULL);
435 PetscCall(PetscPopErrorHandler());
436 if (!check_ierr) {
437 swarm_to_use = user->post_swarm;
438 PetscCall(DMSwarmRestoreField(user->post_swarm, field_name, NULL, NULL, NULL));
439 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Field '%s' will be sourced from the post-processing swarm.\n", field_name);
440 } else {
441 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Field '%s' will be sourced from the main solver swarm.\n", field_name);
442 if (strcasecmp(field_name, "pid") == 0) internal_name = "DMSwarm_pid";
443 else if (strcasecmp(field_name, "CellID") == 0) internal_name = "DMSwarm_CellID";
444 else if (strcasecmp(field_name, "Migration Status") == 0) internal_name = "DMSwarm_location_status";
445 }
446 PetscCall(DMSwarmGetLocalSize(swarm_to_use, &field_local));
447 PetscCall(DMSwarmGetSize(swarm_to_use, &field_total));
448 if (field_total != *p_n_total) {
449 LOG_ALLOW(GLOBAL, LOG_WARNING, "Field '%s' has %" PetscInt_FMT " particles, but expected %" PetscInt_FMT ". Skipping.\n",
450 field_name, field_total, *p_n_total);
451 continue;
452 }
453 PetscCheck(field_local == nlocal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
454 "Field '%s' holds %" PetscInt_FMT " particles on this rank, the solver swarm %" PetscInt_FMT ".",
455 field_name, field_local, nlocal);
456
458 PetscCall(DMSwarmGetField(swarm_to_use, internal_name, &bs, &type, &source));
459 PetscCall(PetscStrncpy(current->name, field_name, MAX_VTK_FIELD_NAME_LENGTH));
460 current->num_components = bs;
461 PetscCall(PetscMalloc1(bs * kept, &current->data));
462 for (PetscInt i = 0; i < kept; ++i) {
463 const PetscInt from = first + i * stride;
464 for (PetscInt c = 0; c < bs; ++c) {
465 PetscScalar *to = &current->data[bs * i + c];
466 if (type == PETSC_INT64) *to = (PetscScalar)((PetscInt64 *)source)[bs * from + c];
467 else if (type == PETSC_INT) *to = (PetscScalar)((PetscInt *)source)[bs * from + c];
468 else if (type == PETSC_REAL) *to = ((PetscReal *)source)[bs * from + c];
469#if defined(PETSC_USE_COMPLEX)
470 else if (type == PETSC_SCALAR) *to = ((PetscScalar *)source)[bs * from + c];
471#endif
472 else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "VTK particle output does not support field '%s' with PETSc type %s.",
473 field_name, PetscDataTypes[type]);
474 }
475 }
476 PetscCall(DMSwarmRestoreField(swarm_to_use, internal_name, &bs, &type, &source));
477 meta->num_point_data_fields++;
478 }
479 PetscCall(PetscFree(fields_copy));
480
481 PetscCall(PetscMalloc1(kept, &meta->connectivity));
482 PetscCall(PetscMalloc1(kept, &meta->offsets));
483 for (PetscInt i = 0; i < kept; ++i) {
484 meta->connectivity[i] = meta->local_start[0] + i;
485 meta->offsets[i] = meta->local_start[0] + i + 1;
486 }
488 PetscFunctionReturn(0);
489}
void TrimWhitespace(char *str)
Removes leading and trailing ASCII whitespace from a mutable string.
Definition io.c:486
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:46
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
Definition logging.h:214
#define PROFILE_HELPER_BEGIN
Marks the beginning of a profiled helper: timed only when enabled by name.
Definition logging.h:1014
#define PROFILE_HELPER_END
Alias of PROFILE_FUNCTION_END for a function that began with PROFILE_HELPER_BEGIN.
Definition logging.h:1033
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:1029
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
Typed identities and metadata for persistent solver-particle fields.
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
@ PARTICLE_FIELD_ID_POSITION
#define MAX_POINT_DATA_FIELDS
Defines the maximum number of data fields for VTK point data.
Definition variables.h:787
PetscInt npoints
Definition variables.h:874
PetscInt num_components
Definition variables.h:854
PetscMPIInt rank
Definition variables.h:919
PetscInt num_point_data_fields
Definition variables.h:879
PetscInt local_size[3]
Definition variables.h:876
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
PetscInt local_start[3]
Definition variables.h:875
DM post_swarm
Definition variables.h:1280
PetscInt * connectivity
Definition variables.h:880
PetscInt * offsets
Definition variables.h:881
VTKFileType fileType
Definition variables.h:872
PetscScalar * data
Definition variables.h:855
char name[64]
Definition variables.h:853
PetscScalar * coords
Definition variables.h:877
PetscInt particle_output_freq
Definition variables.h:812
char particle_fields[1024]
Definition variables.h:810
VTKFieldInfo point_data_fields[20]
Definition variables.h:878
PetscInt mz
Definition variables.h:873
DMDALocalInfo info
Definition variables.h:1169
PetscInt my
Definition variables.h:873
PetscInt mx
Definition variables.h:873
#define MAX_VTK_FIELD_NAME_LENGTH
Maximum length for VTK field names.
Definition variables.h:788
@ VTK_POLYDATA
Definition variables.h:861
@ VTK_STRUCTURED
Definition variables.h:860
Holds all configuration parameters for a post-processing run.
Definition variables.h:793
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
Stores all necessary information for a single data array in a VTK file.
Definition variables.h:852
One VTK file being assembled collectively.
Definition variables.h:871
PetscErrorCode CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Implementation of CreateVTKFileFromMetadata().
Definition vtk_io.c:172
PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
Implementation of PrepareOutputEulerianFieldData().
Definition vtk_io.c:361
PetscErrorCode FinishStructuredVTKOutput(VTKMetaData *meta, const char *filename)
Implementation of FinishStructuredVTKOutput().
Definition vtk_io.c:336
PetscErrorCode BeginStructuredVTKOutput(UserCtx *user, VTKMetaData *meta)
Implementation of BeginStructuredVTKOutput().
Definition vtk_io.c:285
PetscErrorCode PrepareOutputCoordinates(UserCtx *user, PetscScalar **out_coords, PetscInt *out_nx, PetscInt *out_ny, PetscInt *out_nz, PetscInt *out_npoints, PetscInt local_start[3], PetscInt local_size[3])
Implementation of PrepareOutputCoordinates().
Definition vtk_io.c:260
static PetscErrorCode WriteVTSXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
Write the XML header for a VTK structured-grid (.vts) file.
Definition vtk_io.c:21
static PetscErrorCode WriteVTKFileHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
Open a VTK XML document and write its file-level header.
Definition vtk_io.c:115
static PetscErrorCode WriteVTPXMLHeader(FILE *fp, const VTKMetaData *meta, PetscInt *boffset)
Write the XML header for a VTK polydata (.vtp) file.
Definition vtk_io.c:62
PetscErrorCode AppendStructuredVTKField(UserCtx *user, VTKMetaData *meta, const char *name, Vec field_vec, PetscInt components)
Implementation of AppendStructuredVTKField().
Definition vtk_io.c:305
static PetscErrorCode CopyOwnedOutputBox(Vec vec, const PetscInt extent[3], PetscInt start_out[3], PetscInt size_out[3], PetscScalar **out)
Copy this rank's part of the output grid out of a DMDA global vector.
Definition vtk_io.c:131
PetscErrorCode PrepareOutputParticleData(UserCtx *user, PostProcessParams *pps, VTKMetaData *meta, PetscInt *p_n_total)
Implementation of PrepareOutputParticleData().
Definition vtk_io.c:384