PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
runloop.h
Go to the documentation of this file.
1#ifndef RUNLOOP_H
2#define RUNLOOP_H
3
4#include <petscpf.h>
5#include <petscdmswarm.h>
6#include <stdlib.h>
7#include <time.h>
8#include <math.h>
9#include <petsctime.h>
10#include <petscsys.h>
11#include <petscdmcomposite.h>
12#include <petscsystypes.h>
13
14// Include additional headers
15#include "variables.h" // Shared type definitions
16#include "ParticleSwarm.h" // Particle swarm functions
17#include "walkingsearch.h" // Particle location functions
18#include "grid.h" // Grid functions
19#include "logging.h" // Logging macros
20#include "io.h" // Data Input and Output functions
21#include "interpolation.h" // Interpolation routines
22#include "initialcondition.h" // Analytical Solution for testing
23#include "AnalyticalSolutions.h"
24#include "ParticleMotion.h" // Functions related to motion of particles
25#include "ParticlePhysics.h" // Functions related to particle scalar/state updates
26#include "Boundaries.h" // Functions related to Boundary conditions
27#include "setup.h" // Functions related to setup
28#include "solvers.h"
29
30
31/**
32 * @brief Installs lightweight signal handlers for graceful shutdown requests.
33 *
34 * The handlers only record that a shutdown signal was received. The actual
35 * output flush and exit path happens later at safe checkpoints in the run loop.
36 *
37 * @return PetscErrorCode 0 on success.
38 */
39PetscErrorCode InitializeRuntimeSignalHandlers(void);
40
41/**
42 * @brief Update an EWMA estimate for timestep wall-clock duration.
43 *
44 * @param[in] has_previous Whether a previous EWMA estimate exists.
45 * @param[in] previous_ewma_seconds Prior EWMA estimate in seconds.
46 * @param[in] latest_step_seconds Latest completed timestep duration in seconds.
47 * @param[in] alpha EWMA weighting factor in `(0, 1]`.
48 * @return PetscReal Updated EWMA estimate in seconds.
49 */
50PetscReal RuntimeWalltimeGuardUpdateEWMA(PetscBool has_previous, PetscReal previous_ewma_seconds, PetscReal latest_step_seconds, PetscReal alpha);
51
52/**
53 * @brief Return the conservative timestep estimate used by the walltime guard.
54 *
55 * @param[in] warmup_average_seconds Average duration across warmup steps.
56 * @param[in] ewma_seconds Current EWMA duration estimate.
57 * @param[in] latest_step_seconds Most recent completed timestep duration.
58 * @return PetscReal Conservative timestep estimate in seconds.
59 */
60PetscReal RuntimeWalltimeGuardConservativeEstimate(PetscReal warmup_average_seconds, PetscReal ewma_seconds, PetscReal latest_step_seconds);
61
62/**
63 * @brief Compute the required shutdown headroom from timestep estimate and floor.
64 *
65 * @param[in] min_seconds Absolute minimum shutdown headroom.
66 * @param[in] multiplier Safety multiplier applied to the timestep estimate.
67 * @param[in] conservative_estimate_seconds Conservative timestep estimate in seconds.
68 * @return PetscReal Required headroom in seconds.
69 */
70PetscReal RuntimeWalltimeGuardRequiredHeadroom(PetscReal min_seconds, PetscReal multiplier, PetscReal conservative_estimate_seconds);
71
72/**
73 * @brief Decide whether the runtime walltime guard should stop before another step.
74 *
75 * @param[in] completed_steps Number of completed timesteps observed so far.
76 * @param[in] warmup_steps Minimum completed timesteps required before guarding.
77 * @param[in] remaining_seconds Remaining walltime in seconds.
78 * @param[in] min_seconds Absolute minimum shutdown headroom.
79 * @param[in] multiplier Safety multiplier applied to timestep estimate.
80 * @param[in] warmup_average_seconds Average duration across warmup steps.
81 * @param[in] ewma_seconds Current EWMA duration estimate.
82 * @param[in] latest_step_seconds Latest completed timestep duration.
83 * @param[out] required_headroom_seconds_out Computed required headroom in seconds.
84 * @return PetscBool `PETSC_TRUE` when shutdown should be requested.
85 */
86PetscBool RuntimeWalltimeGuardShouldTrigger(PetscInt completed_steps, PetscInt warmup_steps, PetscReal remaining_seconds, PetscReal min_seconds, PetscReal multiplier, PetscReal warmup_average_seconds, PetscReal ewma_seconds, PetscReal latest_step_seconds, PetscReal *required_headroom_seconds_out);
87
88/**
89 * @brief Copies the current time step's solution fields into history vectors
90 * (e.g., U(t_n) -> U_o, U_o -> U_rm1) for the next time step's calculations.
91 *
92 * This function is critical for multi-step time integration schemes (like BDF2)
93 * used by the legacy solver. It must be called at the end of every time step,
94 * after the new solution has been fully computed.
95 *
96 * The order of operations is important to avoid overwriting data prematurely.
97 *
98 * @param user The UserCtx for a single block. The function modifies the history
99 * vectors (Ucont_o, Ucont_rm1, etc.) within this context.
100 * @return PetscErrorCode 0 on success.
101 */
102PetscErrorCode UpdateSolverHistoryVectors(UserCtx *user);
103
104/**
105 * @brief Executes the main time-marching loop for the particle simulation.
106 *
107 * This version uses the new, integrated `LocateAllParticlesInGrid` orchestrator
108 * and the `ResetAllParticleStatuses` helper for a clean, robust, and understandable workflow.
109 *
110 * For each timestep, it performs:
111 * 1. Sets the background fluid velocity field (Ucat) for the current step.
112 * 2. Updates particle positions using velocity from the *previous* step's interpolation.
113 * 3. Removes any particles that have left the global domain.
114 * 4. A single call to `LocateAllParticlesInGrid', which handles all
115 * particle location and migration until the swarm is fully settled.
116 * 5. Interpolates the current fluid velocity to the newly settled particle locations.
117 * 6. Scatters particle data back to Eulerian fields.
118 * 7. Outputs data at specified intervals.
119 *
120 * @param simCtx Pointer to the master simulation context.
121 * @return PetscErrorCode 0 on success, non-zero on failure.
122 */
123PetscErrorCode AdvanceSimulation(SimCtx *simCtx);
124
125/**
126 * @brief Finalizes the simulation setup at t=0, ensuring a consistent state before time marching.
127 *
128 * This function is called from main() after the initial Eulerian and Lagrangian states have been
129 * created but before the main time loop begins. Its responsibilities are:
130 *
131 * 1. Settling the particle swarm: Migrates particles to their correct owner ranks and finds their
132 * initial host cells. This includes handling special surface initializations.
133 * 2. Coupling the fields: Interpolates the initial Eulerian fields to the settled particle locations.
134 * 3. Preparing for the first step: Scatters particle data back to the grid.
135 * 4. Writing the initial output for step 0.
136 *
137 * @param simCtx Pointer to the main simulation context structure.
138 * @return PetscErrorCode 0 on success, non-zero on failure.
139 */
140PetscErrorCode PerformInitializedParticleSetup(SimCtx *simCtx);
141
142/**
143 * @brief Finalizes the simulation state after particle and fluid data have been loaded from a restart.
144 *
145 * This helper function performs the critical sequence of operations required to ensure
146 * the loaded Lagrangian and Eulerian states are fully consistent and the solver is
147 * ready to proceed. This includes:
148 * 1. Verifying particle locations in the grid and building runtime links.
149 * 2. Synchronizing particle velocity with the authoritative grid velocity via interpolation.
150 * 3. Scattering particle source terms (e.g., volume fraction) back to the grid.
151 * 4. Updating the solver's history vectors with the final, fully-coupled state.
152 * 5. Writing the complete, consistent state to output files for the restart step.
153 *
154 * @param simCtx The main simulation context.
155 * @return PetscErrorCode 0 on success.
156 */
157PetscErrorCode PerformLoadedParticleSetup(SimCtx *simCtx);
158
159/**
160 * @brief Performs post-load/post-init consistency checks for a restarted simulation.
161 *
162 * This function is called from main() ONLY when a restart is being performed
163 * (i.e., StartStep > 0). It inspects the particle restart mode to determine the
164 * correct finalization procedure for the Lagrangian swarm.
165 *
166 * - If particles were loaded from a file (`mode == "load"`), it verifies their
167 * locations within the grid to establish necessary runtime links.
168 * - If new particles were initialized into the restarted flow (`mode == "init"`),
169 * it runs the full `PerformInitialSetup` sequence to migrate, locate, and
170 * couple the new particles with the existing fluid state.
171 *
172 * @param simCtx The main simulation context.
173 * @return PetscErrorCode 0 on success.
174 */
175PetscErrorCode FinalizeRestartState(SimCtx *simCtx);
176
177#endif // RUNLOOP_H
Header file for Particle Motion and migration related functions.
Header file for Particle related physics modules.
Header file for Particle Swarm management functions.
Public interface for grid, solver, and metric setup routines.
Public interface for data input/output routines.
Logging utilities and macros for PETSc-based applications.
PetscErrorCode InitializeRuntimeSignalHandlers(void)
Installs lightweight signal handlers for graceful shutdown requests.
Definition runloop.c:123
PetscErrorCode AdvanceSimulation(SimCtx *simCtx)
Executes the main time-marching loop for the particle simulation.
Definition runloop.c:572
PetscReal RuntimeWalltimeGuardUpdateEWMA(PetscBool has_previous, PetscReal previous_ewma_seconds, PetscReal latest_step_seconds, PetscReal alpha)
Update an EWMA estimate for timestep wall-clock duration.
Definition runloop.c:150
PetscReal RuntimeWalltimeGuardConservativeEstimate(PetscReal warmup_average_seconds, PetscReal ewma_seconds, PetscReal latest_step_seconds)
Return the conservative timestep estimate used by the walltime guard.
Definition runloop.c:162
PetscErrorCode UpdateSolverHistoryVectors(UserCtx *user)
Copies the current time step's solution fields into history vectors (e.g., U(t_n) -> U_o,...
Definition runloop.c:319
PetscReal RuntimeWalltimeGuardRequiredHeadroom(PetscReal min_seconds, PetscReal multiplier, PetscReal conservative_estimate_seconds)
Compute the required shutdown headroom from timestep estimate and floor.
Definition runloop.c:173
PetscErrorCode FinalizeRestartState(SimCtx *simCtx)
Performs post-load/post-init consistency checks for a restarted simulation.
Definition runloop.c:526
PetscBool RuntimeWalltimeGuardShouldTrigger(PetscInt completed_steps, PetscInt warmup_steps, PetscReal remaining_seconds, PetscReal min_seconds, PetscReal multiplier, PetscReal warmup_average_seconds, PetscReal ewma_seconds, PetscReal latest_step_seconds, PetscReal *required_headroom_seconds_out)
Decide whether the runtime walltime guard should stop before another step.
Definition runloop.c:184
PetscErrorCode PerformInitializedParticleSetup(SimCtx *simCtx)
Finalizes the simulation setup at t=0, ensuring a consistent state before time marching.
Definition runloop.c:380
PetscErrorCode PerformLoadedParticleSetup(SimCtx *simCtx)
Finalizes the simulation state after particle and fluid data have been loaded from a restart.
Definition runloop.c:456
Main header file for a complex fluid dynamics solver.
The master context for the entire simulation.
Definition variables.h:643
User-defined context containing data specific to a single computational grid level.
Definition variables.h:811
Header file for particle location functions using the walking search algorithm.