23 PetscFunctionBeginUser;
24 char *pipeline_copy, *step_token, *step_saveptr;
25 PetscBool finalize_needed = PETSC_FALSE;
27 ierr = DMCreate(PETSC_COMM_WORLD, &user->
post_swarm); CHKERRQ(ierr);
28 ierr = DMSetType(user->
post_swarm, DMSWARM); CHKERRQ(ierr);
29 ierr = DMSetDimension(user->
post_swarm, 3); CHKERRQ(ierr);
30 ierr = DMSwarmSetType(user->
post_swarm, DMSWARM_BASIC); CHKERRQ(ierr);
33 ierr = DMSwarmSetCellDM(user->
post_swarm, user->
da); CHKERRQ(ierr);
46 step_token = strtok_r(pipeline_copy,
";", &step_saveptr);
49 if (strlen(step_token) == 0) { step_token = strtok_r(NULL,
";", &step_saveptr);
continue; }
51 char *keyword = strtok(step_token,
":");
52 char *args_str = strtok(NULL,
"");
54 PetscInt output_field_dimensions = 1;
56 if (strcasecmp(keyword,
"ComputeSpecificKE") == 0) {
57 if (!args_str) SETERRQ(PETSC_COMM_SELF, 1,
"Error (ComputeSpecificKE): Missing arguments.");
58 char *inputs_str = strtok(args_str,
">");
59 char *output_field = strtok(NULL,
">");
60 output_field_dimensions = 1;
61 if (!output_field) SETERRQ(PETSC_COMM_SELF, 1,
"Error (ComputeSpecificKE): Missing output field in 'in>out' syntax.");
66 finalize_needed = PETSC_TRUE;
73 step_token = strtok_r(NULL,
";", &step_saveptr);
76 ierr = PetscFree(pipeline_copy); CHKERRQ(ierr);
79 if (finalize_needed) {
81 ierr = DMSwarmFinalizeFieldRegister(user->
post_swarm); CHKERRQ(ierr);
86 PetscFunctionReturn(0);
105 char *pipeline_copy, *step_token, *step_saveptr;
107 PetscFunctionBeginUser;
113 PetscFunctionReturn(0);
119 ierr = PetscStrallocpy(pps->
process_pipeline, &pipeline_copy); CHKERRQ(ierr);
122 step_token = strtok_r(pipeline_copy,
";", &step_saveptr);
125 if (strlen(step_token) == 0) {
126 step_token = strtok_r(NULL,
";", &step_saveptr);
130 char *keyword = strtok(step_token,
":");
131 char *args_str = strtok(NULL,
"");
134 step_token = strtok_r(NULL,
";", &step_saveptr);
141 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Executing Transformation: '%s' on args: '%s'\n", keyword, args_str ? args_str :
"None");
144 if (strcasecmp(keyword,
"CellToNodeAverage") == 0) {
145 if (!args_str) SETERRQ(PETSC_COMM_SELF, 1,
"CellToNodeAverage requires arguments in 'in_field>out_field' format.");
146 char *in_field = strtok(args_str,
">");
147 char *out_field = strtok(NULL,
">");
148 if (!in_field || !out_field) SETERRQ(PETSC_COMM_SELF, 1,
"CellToNodeAverage requires 'in>out' syntax (e.g., P>P_nodal).");
152 else if (strcasecmp(keyword,
"ComputeQCriterion") == 0) {
155 else if (strcasecmp(keyword,
"NormalizeRelativeField") == 0) {
156 if (!args_str) SETERRQ(PETSC_COMM_SELF, 1,
"NormalizePressure requires the pressure field name (e.g., 'P') as an argument.");
165 step_token = strtok_r(NULL,
";", &step_saveptr);
168 ierr = PetscFree(pipeline_copy); CHKERRQ(ierr);
170 PetscFunctionReturn(0);
179 PetscFunctionBeginUser;
182 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
"No instantaneous fields requested for output at ti=%" PetscInt_FMT
". Skipping.\n", ti);
183 PetscFunctionReturn(0);
189 ierr = PetscMemzero(&meta,
sizeof(
VTKMetaData)); CHKERRQ(ierr);
194 LOG_ALLOW(
GLOBAL,
LOG_DEBUG,
"Using coords linearization order: fast=i mid=j slow=k (sizes: %" PetscInt_FMT
" x %" PetscInt_FMT
" x %" PetscInt_FMT
")\n",
195 meta.
mx, meta.
my, meta.
mz);
199 char *fields_copy, *field_name;
202 field_name = strtok(fields_copy,
",");
205 if (!*field_name) { field_name = strtok(NULL,
",");
continue; }
209 Vec field_vec = NULL;
210 PetscInt num_components = 0;
212 if (!strcasecmp(field_name,
"P_nodal")) {
213 field_vec = user->
P_nodal; num_components = 1;
214 }
else if (!strcasecmp(field_name,
"Ucat_nodal")) {
215 field_vec = user->
Ucat_nodal; num_components = 3;
216 }
else if (!strcasecmp(field_name,
"Qcrit")) {
217 field_vec = user->
Qcrit; num_components = 1;
220 field_name = strtok(NULL,
",");
226 field_name = strtok(NULL,
",");
373 field_name = strtok(NULL,
",");
376 ierr = PetscFree(fields_copy); CHKERRQ(ierr);
442 snprintf(filename,
sizeof(filename),
"%s_%05" PetscInt_FMT
".vts", pps->
output_prefix, ti);
446 PetscFunctionReturn(0);
471 char *pipeline_copy, *step_token, *step_saveptr;
473 PetscFunctionBeginUser;
476 PetscInt n_global_source;
477 ierr = DMSwarmGetSize(user->
swarm, &n_global_source); CHKERRQ(ierr);
483 PetscFunctionReturn(0);
491 step_token = strtok_r(pipeline_copy,
";", &step_saveptr);
494 if (strlen(step_token) == 0) { step_token = strtok_r(NULL,
";", &step_saveptr);
continue; }
496 char *keyword = strtok(step_token,
":");
497 char *args_str = strtok(NULL,
"");
501 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Executing Particle Transformation: '%s' on args: '%s'\n", keyword, args_str ? args_str :
"None");
503 if (strcasecmp(keyword,
"ComputeSpecificKE") == 0) {
504 char *velocity_field = strtok(args_str,
">");
505 char *ske_field = strtok(NULL,
">");
514 step_token = strtok_r(NULL,
";", &step_saveptr);
516 ierr = PetscFree(pipeline_copy); CHKERRQ(ierr);
519 PetscFunctionReturn(0);
531 PetscInt n_total_particles_before_subsample;
533 PetscFunctionBeginUser;
537 PetscFunctionReturn(0);
540 ierr = DMSwarmGetSize(user->
swarm, &n_global); CHKERRQ(ierr);
543 PetscFunctionReturn(0);
546 ierr = PetscMemzero(&part_meta,
sizeof(
VTKMetaData)); CHKERRQ(ierr);
554 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"--- Starting VTP Particle File Writing for ti = %" PetscInt_FMT
" (writing %" PetscInt_FMT
" of %" PetscInt_FMT
" particles) ---\n",
555 ti, part_meta.
npoints, n_total_particles_before_subsample);
574 PetscFunctionReturn(0);
578#define __FUNCT__ "main"
585 ierr = PetscInitialize(&argc, &argv, (
char *)0,
"Unified Post-Processing Tool"); CHKERRQ(ierr);
609 SETERRQ(PETSC_COMM_SELF,1,
"Particle output requested but np=0. Please set np>0 to enable particle output.");
650 ierr = PetscFinalize();
PetscErrorCode ResizeSwarmGlobally(DM swarm, PetscInt N_target)
PetscErrorCode PreCheckAndResizeSwarm(UserCtx *user, PetscInt ti, const char *ext)
Checks particle count in the reference file and resizes the swarm if needed.
PetscErrorCode InitializeParticleSwarm(SimCtx *simCtx)
Perform particle swarm initialization, particle-grid interaction, and related operations.
PetscErrorCode RegisterSwarmField(DM swarm, const char *fieldName, PetscInt fieldDim, PetscDataType dtype)
Registers a swarm field without finalizing registration.
PetscErrorCode ReadSimulationFields(UserCtx *user, PetscInt ti)
Reads binary field data for velocity, pressure, and other required vectors.
void TrimWhitespace(char *str)
Helper function to trim leading/trailing whitespace from a string.
PetscErrorCode ReadAllSwarmFields(UserCtx *user, PetscInt ti)
Reads multiple fields (positions, velocity, CellID, and weight) into a DMSwarm.
PetscInt CreateVTKFileFromMetadata(const char *filename, const VTKMetaData *meta, MPI_Comm comm)
#define LOCAL
Logging scope definitions for controlling message output.
#define GLOBAL
Scope for global logging across all processes.
#define LOG_ALLOW(scope, level, fmt,...)
Logging macro that checks both the log level and whether the calling function is in the allowed-funct...
@ LOG_INFO
Informational messages about program execution.
@ LOG_WARNING
Non-critical issues that warrant attention.
@ LOG_DEBUG
Detailed debugging information.
PetscErrorCode ComputeQCriterion(UserCtx *user)
Computes the Q-Criterion, a scalar value identifying vortex cores.
PetscErrorCode ComputeSpecificKE(UserCtx *user, const char *velocity_field, const char *ske_field)
Computes the specific kinetic energy (KE per unit mass) for each particle.
PetscErrorCode NormalizeRelativeField(UserCtx *user, const char *relative_field_name)
Normalizes a relative field by subtracting a reference value.
PetscErrorCode ComputeNodalAverage(UserCtx *user, const char *in_field_name, const char *out_field_name)
Computes node-centered data by averaging 8 surrounding cell-centered values, exactly replicating the ...
PetscErrorCode EulerianDataProcessingPipeline(UserCtx *user, PostProcessParams *pps)
Parses the processing pipeline string from the config and executes the requested kernels in sequence.
PetscErrorCode WriteEulerianFile(UserCtx *user, PostProcessParams *pps, PetscInt ti)
Orchestrates the writing of a combined, multi-field VTK file for a single time step.
int main(int argc, char **argv)
PetscErrorCode ParticleDataProcessingPipeline(UserCtx *user, PostProcessParams *pps)
Parses and executes the particle pipeline using a robust two-pass approach.
PetscErrorCode WriteParticleFile(UserCtx *user, PostProcessParams *pps, PetscInt ti)
Writes particle data to a VTP file using the Prepare-Write-Cleanup pattern.
PetscErrorCode SetupPostProcessSwarm(UserCtx *user, PostProcessParams *pps)
Creates a new, dedicated DMSwarm for post-processing tasks.
PetscErrorCode SetupDomainRankInfo(SimCtx *simCtx)
Sets up the full rank communication infrastructure, including neighbor ranks and bounding box exchang...
PetscErrorCode SetupGridAndSolvers(SimCtx *simCtx)
The main orchestrator for setting up all grid-related components.
PetscErrorCode CreateSimulationContext(int argc, char **argv, SimCtx **p_simCtx)
Allocates and populates the master SimulationContext object.
PetscErrorCode SetupBoundaryConditions(SimCtx *simCtx)
(Orchestrator) Sets up all boundary conditions for the simulation.
#define MAX_POINT_DATA_FIELDS
Defines the maximum number of data fields for VTK point data.
char particle_output_prefix[256]
PetscInt num_point_data_fields
SimCtx * simCtx
Back-pointer to the master simulation context.
#define MAX_FILENAME_LENGTH
char output_fields_instantaneous[1024]
char particle_pipeline[1024]
char process_pipeline[1024]
char particle_fields[1024]
PetscBool outputParticles
VTKFieldInfo point_data_fields[20]
@ EXEC_MODE_POSTPROCESSOR
#define MAX_VTK_FIELD_NAME_LENGTH
Maximum length for VTK field names.
Holds all configuration parameters for a post-processing run.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.
Stores all necessary information for a single data array in a VTK file.
PetscErrorCode PrepareOutputEulerianFieldData(UserCtx *user, Vec field_vec, PetscInt num_components, PetscScalar **out_data)
Creates a C array of field data corresponding to a subsampled (legacy-style) grid.
PetscErrorCode PrepareOutputCoordinates(UserCtx *user, PetscScalar **out_coords, PetscInt *out_nx, PetscInt *out_ny, PetscInt *out_nz, PetscInt *out_npoints)
Creates a C array of coordinates corresponding to a subsampled (legacy-style) grid.
PetscErrorCode PrepareOutputParticleData(UserCtx *user, PostProcessParams *pps, VTKMetaData *meta, PetscInt *p_n_total)
Gathers, subsamples, and prepares all particle data for VTK output.