PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
Functions
vtk_io.h File Reference
#include "variables.h"
#include "logging.h"
#include "io.h"
Include dependency graph for vtk_io.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PetscErrorCode CreateVTKFileFromMetadata (const char *filename, const VTKMetaData *meta, MPI_Comm comm)
 Writes one VTK file (.vts or .vtp) collectively from every rank's part of it.
 
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])
 Copies this rank's part of the output grid's coordinates.
 
PetscErrorCode PrepareOutputEulerianFieldData (UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
 Copies this rank's part of a nodal field on the output grid.
 
PetscErrorCode BeginStructuredVTKOutput (UserCtx *user, VTKMetaData *meta)
 Begins one structured VTK file: clears the metadata and builds its coordinates.
 
PetscErrorCode AppendStructuredVTKField (UserCtx *user, VTKMetaData *meta, const char *name, Vec field_vec, PetscInt components)
 Adds one point-data field to a structured VTK file being assembled.
 
PetscErrorCode FinishStructuredVTKOutput (VTKMetaData *meta, const char *filename)
 Writes an assembled structured VTK file and releases its buffers.
 
PetscErrorCode PrepareOutputParticleData (UserCtx *user, PostProcessParams *pps, VTKMetaData *meta, PetscInt *p_n_total)
 Prepares this rank's part of the subsampled particle VTK output.
 

Function Documentation

◆ CreateVTKFileFromMetadata()

PetscErrorCode CreateVTKFileFromMetadata ( const char *  filename,
const VTKMetaData *  meta,
MPI_Comm  comm 
)

Writes one VTK file (.vts or .vtp) collectively from every rank's part of it.

Rank 0 writes the XML header, each block's byte count and the footer; every rank writes its own points into each appended block through MPI-IO, so no rank holds more than its own part. The file is the same whatever the rank count. Block byte counts are UInt64 (header_type="UInt64"), so no block size limit applies.

Parameters
[in]filenameThe output file name.
[in]metaThis rank's part of the file (see VTKMetaData).
[in]commThe communicator all of whose ranks call this.
Returns
0 on success, or a PETSc error.

Writes one VTK file (.vts or .vtp) collectively from every rank's part of it.

Creates a VTK file from prepared metadata and field payloads.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
CreateVTKFileFromMetadata()

Definition at line 172 of file vtk_io.c.

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}
#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_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
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled function: an always-on aggregate timer.
Definition logging.h:1000
#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
PetscInt num_point_data_fields
Definition variables.h:879
PetscInt local_size[3]
Definition variables.h:876
PetscInt local_start[3]
Definition variables.h:875
PetscInt * connectivity
Definition variables.h:880
PetscInt * offsets
Definition variables.h:881
VTKFileType fileType
Definition variables.h:872
PetscScalar * data
Definition variables.h:855
PetscScalar * coords
Definition variables.h:877
VTKFieldInfo point_data_fields[20]
Definition variables.h:878
PetscInt mz
Definition variables.h:873
PetscInt my
Definition variables.h:873
PetscInt mx
Definition variables.h:873
@ VTK_POLYDATA
Definition variables.h:861
@ VTK_STRUCTURED
Definition variables.h:860
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
Here is the caller graph for this function:

◆ PrepareOutputCoordinates()

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] 
)

Copies this rank's part of the output grid's coordinates.

The output grid is the nodes [0..IM-2, 0..JM-2, 0..KM-2], a grid of (IM-1)x(JM-1)x(KM-1) points. This rank's part is its owned nodes within it, i fastest.

Parameters
[in]userThe UserCtx containing the grid information (DM, IM/JM/KM).
[out]out_coordsNewly allocated coordinates of this rank's points (x, y, z each).
[out]out_nxThe number of points in the x-dimension for the new grid (IM-1).
[out]out_nyThe number of points in the y-dimension for the new grid (JM-1).
[out]out_nzThe number of points in the z-dimension for the new grid (KM-1).
[out]out_npointsThe total number of points in the new grid.
[out]local_startFirst output point of this rank's box on each axis.
[out]local_sizePoints in this rank's box on each axis (zero for a rank holding only dummy nodes).
Returns
PetscErrorCode

Copies this rank's part of the output grid's coordinates.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
PrepareOutputCoordinates()

Definition at line 260 of file vtk_io.c.

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}
DMDALocalInfo info
Definition variables.h:1169
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PrepareOutputEulerianFieldData()

PetscErrorCode PrepareOutputEulerianFieldData ( UserCtx *  user,
Vec  field_vec,
PetscInt  num_components,
PetscScalar **  out_data 
)

Copies this rank's part of a nodal field on the output grid.

The points and their order are those of PrepareOutputCoordinates() on the same rank, with the field's components interleaved.

Parameters
[in]userThe UserCtx for grid information.
[in]field_vecThe full-sized PETSc vector containing the field data (e.g., user->P_nodal).
[in]num_componentsThe number of components for this field (1 for scalar, 3 for vector).
[out]out_dataNewly allocated values of this rank's points.
Returns
PetscErrorCode

Copies this rank's part of a nodal field on the output grid.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
PrepareOutputEulerianFieldData()

Definition at line 361 of file vtk_io.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BeginStructuredVTKOutput()

PetscErrorCode BeginStructuredVTKOutput ( UserCtx *  user,
VTKMetaData *  meta 
)

Begins one structured VTK file: clears the metadata and builds its coordinates.

Every Eulerian VTK file is assembled the same way — coordinates, then a set of point-data fields, then the write. This trio exists so the two producers share that shape instead of restating it, and so the buffers each producer allocates are released in one place rather than in neither.

Parameters
[in]userBlock context supplying the grid.
[out]metaMetadata to initialize.
Returns
Zero on success, or a PETSc error.

Begins one structured VTK file: clears the metadata and builds its coordinates.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
BeginStructuredVTKOutput()

Definition at line 285 of file vtk_io.c.

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}
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppendStructuredVTKField()

PetscErrorCode AppendStructuredVTKField ( UserCtx *  user,
VTKMetaData *  meta,
const char *  name,
Vec  field_vec,
PetscInt  components 
)

Adds one point-data field to a structured VTK file being assembled.

Skips the field with a warning when the file is already at its field limit, so a long output list degrades rather than corrupting the metadata.

Parameters
[in]userBlock context supplying the grid.
[in,out]metaMetadata to append to.
[in]nameField name as it will appear in the file.
[in]field_vecNodal vector holding the values.
[in]componentsOne for a scalar, three for a vector.
Returns
Zero on success, or a PETSc error.

Adds one point-data field to a structured VTK file being assembled.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
AppendStructuredVTKField()

Definition at line 305 of file vtk_io.c.

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}
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
char name[64]
Definition variables.h:853
#define MAX_VTK_FIELD_NAME_LENGTH
Maximum length for VTK field names.
Definition variables.h:788
Stores all necessary information for a single data array in a VTK file.
Definition variables.h:852
PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
Implementation of PrepareOutputEulerianFieldData().
Definition vtk_io.c:361
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinishStructuredVTKOutput()

PetscErrorCode FinishStructuredVTKOutput ( VTKMetaData *  meta,
const char *  filename 
)

Writes an assembled structured VTK file and releases its buffers.

Parameters
[in,out]metaMetadata to write and then release.
[in]filenameDestination path.
Returns
Zero on success, or a PETSc error.

Writes an assembled structured VTK file and releases its buffers.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
FinishStructuredVTKOutput()

Definition at line 336 of file vtk_io.c.

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}
PetscErrorCode CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
Implementation of CreateVTKFileFromMetadata().
Definition vtk_io.c:172
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PrepareOutputParticleData()

PetscErrorCode PrepareOutputParticleData ( UserCtx *  user,
PostProcessParams *  pps,
VTKMetaData *  meta,
PetscInt *  p_n_total 
)

Prepares this rank's part of the subsampled particle VTK output.

Collective: all ranks must enter it. Particles are numbered in rank order and every pps->particle_output_freq-th one is kept; each rank copies only its own kept particles (positions, the requested fields, connectivity and offsets), with no gather. With no particles, only meta->fileType is set and nothing is allocated.

Parameters
[in]userThe UserCtx containing the DMSwarm.
[in]ppsThe PostProcessParams struct for configuration.
[out]metaZeroed metadata to populate with this rank's part (see VTKMetaData).
[out]p_n_totalThe total number of particles before subsampling, on every rank.
Returns
PetscErrorCode

Prepares this rank's part of the subsampled particle VTK output.

Full API contract (arguments, ownership, side effects) is documented with the header declaration in include/vtk_io.h.

See also
PrepareOutputParticleData()

Definition at line 384 of file vtk_io.c.

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
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:32
const char * ParticleFieldName(ParticleFieldId field_id)
Return the canonical PETSc DMSwarm name for an ID.
@ PARTICLE_FIELD_ID_POSITION
PetscMPIInt rank
Definition variables.h:919
SimCtx * simCtx
Back-pointer to the master simulation context.
Definition variables.h:1160
DM post_swarm
Definition variables.h:1280
PetscInt particle_output_freq
Definition variables.h:812
char particle_fields[1024]
Definition variables.h:810
Here is the call graph for this function:
Here is the caller graph for this function: