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

Go to the source code of this file.

Functions

PetscErrorCode FlowSolver (SimCtx *simCtx)
 Orchestrates a single time step of the Eulerian fluid solver.
 
PetscErrorCode FlowSolver_Destroy (SimCtx *simCtx)
 Releases the solver objects FlowSolver() keeps for the run.
 

Function Documentation

◆ FlowSolver()

PetscErrorCode FlowSolver ( SimCtx *  simCtx)

Orchestrates a single time step of the Eulerian fluid solver.

This is the refactored, high-level entry point for advancing the fluid state from time t_n to t_{n+1}. It takes the master SimCtx as its primary argument.

Parameters
simCtxThe master simulation context, containing all solver settings, multigrid structures, and data vectors.
Returns
PetscErrorCode 0 on success.

Orchestrates a single time step of the Eulerian fluid solver.

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

See also
FlowSolver()

Definition at line 9 of file solvers.c.

10{
11 PetscErrorCode ierr;
12 UserMG *usermg = NULL;
13 PetscInt level;
14 UserCtx *user = NULL;
15
16 PetscFunctionBeginUser;
18
22 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
23 "Unknown momentum solver type %d. Supported values are EXPLICIT_RK, DUALTIME_PICARD_JAMESON_RK, and NEWTON_KRYLOV.",
24 simCtx->mom_solver_type);
25 }
26
27 usermg = &simCtx->usermg;
28 level = usermg->mglevels - 1;
29 user = usermg->mgctx[level].user;
30 LOG_ALLOW(GLOBAL, LOG_INFO, "[Step %" PetscInt_FMT "] Entering orchestrator...\n", simCtx->step);
31
32 /*
33 // ========================================================================
34 // SECTION: O-Grid Specific Force Calculations (Legacy Feature)
35 // ========================================================================
36 // This was a specialized calculation for non-immersed O-grid cases.
37 if (simCtx->Ogrid && !simCtx->immersed) {
38 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Calculating O-grid forces...\n");
39 // Calc_forces_Ogrid(&user[0], simCtx->step, 0);
40 }
41 */
42
43
44
45 // ========================================================================
46 // SECTION: Turbulence Models (LES)
47 // ========================================================================
48 // These models compute the turbulent eddy viscosity (Nu_t) which is then
49 // used by the momentum solver in the diffusion term.
50
51
52 if (simCtx->les) {
53 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating LES subgrid-scale model...\n");
54 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
55 // Strain rates are formed from the Cartesian velocity, so it must be
56 // reconstructed and its ghost and periodic images refreshed first.
57 const FieldId staggered_fields[] = {FIELD_ID_UCONT};
58 const FieldId cell_fields[] = {FIELD_ID_UCAT};
59
60 ierr = SynchronizePeriodicStaggeredFields(&user[bi], 1, staggered_fields); CHKERRQ(ierr);
61 ierr = Contra2Cart(&user[bi]); CHKERRQ(ierr);
62 ierr = SynchronizePeriodicCellFields(&user[bi], 1, cell_fields); CHKERRQ(ierr);
63 ierr = UpdateLocalGhosts(&user[bi], FIELD_ID_UCAT); CHKERRQ(ierr);
64
65 // The strain rates come from the resolved velocity. A wall model acts through
66 // the wall stress alone (ApplyWallFunction) and leaves this field as it is, so
67 // the closure and the momentum equation read the same near-wall velocity.
68
69 // Only the dynamic model has a coefficient to recompute, and it honours
70 // its own cadence. The constant model's coefficient never changes.
71 if (simCtx->les == DYNAMIC_SMAGORINSKY &&
72 simCtx->step % simCtx->les_config.dynamic_frequency == 0) {
73 LOG_ALLOW(LOCAL, LOG_DEBUG, " Computing dynamic coefficient for block %" PetscInt_FMT ".\n", bi);
74 ierr = ComputeSmagorinskyConstant(&user[bi]); CHKERRQ(ierr);
75 }
76
77 ierr = ComputeEddyViscosityLES(&user[bi]); CHKERRQ(ierr);
78 ierr = LogLESDiagnostics(&user[bi]); CHKERRQ(ierr);
79 }
80 }
81
82
83 // ========================================================================
84 // SECTION: Momentum Equation Solver
85 // ========================================================================
86 // This is the core of the time step. It computes an intermediate velocity
87 // field by solving the momentum equations.
88
89 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning momentum step solve (Solver = %s)...\n", MomentumSolverTypeToString(simCtx->mom_solver_type));
90
91 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
92 // ierr = ImpRK(user, NULL, NULL); CHKERRQ(ierr);
93 // Add new momentum solver types here only after wiring the enum, parser, docs, and tests.
95 ierr = MomentumSolver_DualTime_Picard_JamesonRK(user,NULL,NULL); CHKERRQ(ierr);
96 } else if(simCtx->mom_solver_type == MOMENTUM_SOLVER_EXPLICIT_RK) {
97 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
98 ierr = MomentumSolver_Explicit_RungeKutta4(user, NULL, NULL); CHKERRQ(ierr);
99 } else if (simCtx->mom_solver_type == MOMENTUM_SOLVER_NEWTON_KRYLOV) {
100 ierr = MomentumSolver_NewtonKrylov(user, NULL, NULL); CHKERRQ(ierr);
101 }
102
103 /* A solver that commits an unconverged step keeps the run going, which is right for an
104 occasional step and wrong for a solve that has stopped advancing: such a run spends
105 its walltime on a frozen field. The streak is consecutive, so one converged step
106 clears it, and the limit is opt-in because some configurations meet their iteration
107 cap on every step by design. */
108 simCtx->mom_unconverged_streak = simCtx->mom_last_converged ? 0 : simCtx->mom_unconverged_streak + 1;
109 PetscCheck(simCtx->mom_max_unconverged_steps <= 0 ||
111 PETSC_COMM_WORLD, PETSC_ERR_NOT_CONVERGED,
112 "The momentum solve (%s) has committed %" PetscInt_FMT " consecutive unconverged steps, "
113 "through step %" PetscInt_FMT ", reaching tolerances.max_consecutive_unconverged_steps "
114 "(%" PetscInt_FMT "). Stopping rather than advancing a field the solver no longer "
115 "converges; the solver's per-step summary names the failure reasons.",
117 simCtx->step, simCtx->mom_max_unconverged_steps);
118// ========================================================================
119// SECTION: Pressure-Poisson Solver
120// ========================================================================
121// This step enforces the continuity equation (incompressibility) by solving
122// for a pressure correction field.
123
124 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning pressure-Poisson solve (Poisson Flag = %" PetscInt_FMT ")...\n", simCtx->poisson);
125
126 if (simCtx->poisson == 0) {
127 ierr = PoissonSolver_Multigrid(usermg); CHKERRQ(ierr);
128 } else {
129 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
130 "Unsupported Poisson solver type %" PetscInt_FMT ". The current runtime supports only the multigrid path (poisson = 0).",
131 simCtx->poisson);
132 }
133
134 // ========================================================================
135 // SECTION: Velocity Correction (Projection)
136 // ========================================================================
137 // The pressure correction is used to update the pressure field and project
138 // the intermediate velocity onto a divergence-free space.
139
140 LOG_ALLOW(GLOBAL, LOG_INFO, "Applying velocity correction/projection step...\n");
141 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
142 ierr = UpdatePressure(&user[bi]); CHKERRQ(ierr);
143 LOG_ALLOW(GLOBAL,LOG_INFO," Pressure Updated for Block %" PetscInt_FMT ".\n",bi);
144
145 ierr = ProjectVelocity(&user[bi]); CHKERRQ(ierr);
146
147 LOG_ALLOW(GLOBAL,LOG_INFO," Velocity corrected for Block %" PetscInt_FMT ".\n",bi);
148
149 // Ensure local ghost cells for the final pressure field are correct
150 ierr = UpdateLocalGhosts(&user[bi], FIELD_ID_P);
151 }
152
153 // ========================================================================
154
155 // ========================================================================
156 // SECTION: Final Diagnostics and I/O
157 // ========================================================================
158
159 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
160 LOG_ALLOW(GLOBAL, LOG_INFO, "Finalizing state & Diagnostics for block %" PetscInt_FMT "...\n", bi);
161
162 // --- Perform Divergence Check ---
163 // This is a diagnostic to verify the quality of the velocity correction.
164 ierr = ComputeDivergence(&user[bi]); CHKERRQ(ierr);
165
166 // -- Log Continuity metrics ----
167 ierr = LOG_CONTINUITY_METRICS(&user[bi]);
168
169 /* Reported here rather than beside the LES block, because a wall model is
170 configured independently of LES and its last pass this step is the boundary
171 pass, not the closure prologue. A no-op when no wall model is active. */
172 ierr = LogWallModelDiagnostics(&user[bi]); CHKERRQ(ierr);
173 /* The force a driven-periodic controller applied this step. A no-op without one. */
174 ierr = LogDrivenFlowDiagnostics(&user[bi]); CHKERRQ(ierr);
175 ierr = LogPrescribedFlowDiagnostics(&user[bi]); CHKERRQ(ierr);
176 ierr = LogBoundaryPressureDiagnostics(&user[bi]); CHKERRQ(ierr);
177 /*
178 // --- Immersed Boundary Interpolation (Post-Correction) ---
179 // This step would update the velocity values AT the IB nodes to match the
180 // newly corrected fluid field. Important for the next time step.
181 if (simCtx->immersed) {
182 for (PetscInt ibi = 0; ibi < simCtx->NumberOfBodies; ibi++) {
183 ibm_interpolation_advanced(&user[bi], &simCtx->ibm[ibi], ibi, 1);
184 }
185 }
186 */
187
188 // }
189 }
190
191 LOG_ALLOW(GLOBAL, LOG_INFO, "orchestrator finished for step %" PetscInt_FMT ".\n", simCtx->step);
193 PetscFunctionReturn(0);
194}
PetscErrorCode LogPrescribedFlowDiagnostics(UserCtx *user)
Appends each prescribed-velocity inlet's flow rate and mean normal speed this step to inlet_flow....
PetscErrorCode LogDrivenFlowDiagnostics(UserCtx *user)
Appends one row of driven-flow controller state to driven_flow.csv.
Definition BodyForces.c:174
PetscErrorCode LogWallModelDiagnostics(UserCtx *user)
Appends one row of near-wall statistics to <run.analysis.metrics>/wall_model.csv.
PetscErrorCode LogBoundaryPressureDiagnostics(UserCtx *user)
Records each INLET and OUTLET face's area-weighted mean pressure this step.
PetscErrorCode SynchronizePeriodicStaggeredFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes persistent component-staggered vector fields.
PetscErrorCode SynchronizePeriodicCellFields(UserCtx *user, PetscInt num_fields, const FieldId field_ids[])
Synchronizes periodic endpoint cells for a list of cell-centered fields.
FieldId
Compile-time identity for a catalogued Eulerian field.
@ FIELD_ID_UCAT
@ FIELD_ID_UCONT
@ FIELD_ID_P
PetscErrorCode ComputeEddyViscosityLES(UserCtx *user)
Computes the turbulent eddy viscosity for one block.
Definition les.c:899
PetscErrorCode LogLESDiagnostics(UserCtx *user)
Appends one row of LES coefficient statistics to the run's log directory.
Definition les.c:991
PetscErrorCode ComputeSmagorinskyConstant(UserCtx *user)
Computes the dynamic Smagorinsky coefficient field for one block.
Definition les.c:664
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:45
#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
PetscErrorCode LOG_CONTINUITY_METRICS(UserCtx *user)
Logs continuity metrics for a single block to a file.
Definition logging.c:1866
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:31
@ 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
const char * MomentumSolverTypeToString(MomentumSolverType SolverFlag)
Returns the canonical log token for a momentum-solver selector.
Definition logging.c:808
PetscErrorCode MomentumSolver_DualTime_Picard_JamesonRK(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Solves the momentum equations using dual-time Picard iteration with Jameson RK smoothing.
PetscErrorCode MomentumSolver_Explicit_RungeKutta4(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Advances the momentum equations using an explicit 4th-order Runge-Kutta scheme.
PetscErrorCode UpdatePressure(UserCtx *user)
Adds the pressure correction to the pressure, P += Phi, and refreshes both fields' periodic images an...
Definition poisson.c:405
PetscErrorCode ProjectVelocity(UserCtx *user)
Corrects the contravariant flux with the gradient of Phi.
Definition poisson.c:425
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Solves the pressure-correction equation for every block with geometric multigrid.
Definition poisson.c:922
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:3313
PetscErrorCode ComputeDivergence(UserCtx *user)
Computes the discrete divergence of the contravariant velocity field.
Definition setup.c:3599
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2593
@ DYNAMIC_SMAGORINSKY
Definition variables.h:586
UserCtx * user
Definition variables.h:768
PetscInt dynamic_frequency
Recompute the dynamic coefficient every N steps.
Definition variables.h:665
PetscInt block_number
Definition variables.h:1009
LESConfig les_config
Parameters of the LES closure selected by les.
Definition variables.h:1046
UserMG usermg
Definition variables.h:1082
PetscBool mom_last_converged
Definition variables.h:971
@ MOMENTUM_SOLVER_DUALTIME_PICARD_JAMESON_RK
Definition variables.h:731
@ MOMENTUM_SOLVER_EXPLICIT_RK
Definition variables.h:730
@ MOMENTUM_SOLVER_NEWTON_KRYLOV
Definition variables.h:732
PetscInt poisson
Definition variables.h:962
PetscInt mom_unconverged_streak
Definition variables.h:973
PetscInt mglevels
Definition variables.h:775
PetscInt mom_max_unconverged_steps
Definition variables.h:972
PetscInt step
Definition variables.h:924
PetscInt les
Active LES closure; an LESModelType value.
Definition variables.h:1041
MGCtx * mgctx
Definition variables.h:778
MomentumSolverType mom_solver_type
Definition variables.h:958
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1157
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:774
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FlowSolver_Destroy()

PetscErrorCode FlowSolver_Destroy ( SimCtx *  simCtx)

Releases the solver objects FlowSolver() keeps for the run.

The Newton–Krylov momentum solve context lives here because only the simulator links the momentum solvers; FinalizeSimulation(), shared with the post-processor, releases the rest. Call it before FinalizeSimulation(). Blocks that never built one are skipped.

Parameters
simCtxThe master simulation context.
Returns
PetscErrorCode 0 on success.

Releases the solver objects FlowSolver() keeps for the run.

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

See also
FlowSolver_Destroy()

Definition at line 202 of file solvers.c.

203{
204 UserMG *usermg = &simCtx->usermg;
205
206 PetscFunctionBeginUser;
208 if (usermg->mgctx && usermg->mgctx[usermg->mglevels - 1].user) {
209 UserCtx *user = usermg->mgctx[usermg->mglevels - 1].user;
210 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
211 PetscCall(MomentumSolver_NewtonKrylov_Destroy(&user[bi]));
212 }
213 }
215 PetscFunctionReturn(0);
216}
#define MomentumSolver_NewtonKrylov_Destroy
Here is the caller graph for this function: