PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
 
Loading...
Searching...
No Matches
momentumsolvers.h
Go to the documentation of this file.
1#ifndef MOMENTUMSOLVERS_H
2#define MOMENTUMSOLVERS_H
3
4#include "variables.h" // Provides definitions for UserCtx, SimCtx, IBMNodes, etc.
5#include "logging.h"
6#include "rhs.h"
7#include "Boundaries.h"
8
9/*================================================================================*
10 * MOMENTUM EQUATION SOLVERS *
11 *================================================================================*/
12
13/**
14 * @brief Advances the momentum equations using an explicit 4th-order Runge-Kutta scheme.
15 * @param user Array of UserCtx structs for all blocks.
16 * @param ibm (Optional) Pointer to IBM data. Pass NULL if disabled.
17 * @param fsi (Optional) Pointer to FSI data. Pass NULL if disabled.
18 * @return PetscErrorCode 0 on success.
19 *
20 * @note Testing status:
21 * The explicit RK path remains on the near-term backlog for direct
22 * positive-path bespoke coverage; today it is weaker than the dual-time
23 * path in the test surface.
24 */
25extern PetscErrorCode MomentumSolver_Explicit_RungeKutta4(UserCtx *user, IBMNodes *ibm, FSInfo *fsi);
26
27/**
28 * @brief Solves one physical momentum step with matrix-free Newton--Krylov.
29 *
30 * The Jacobian operator is the finite-difference, matrix-free operator of the
31 * complete deterministic residual.
32 * With no mathematical preconditioner its matrix is also passed to PETSc's
33 * preconditioning slot and PCNONE is derived. The optional provisional
34 * frozen-momentum, point-block model approximates same-cell frozen coupling,
35 * owns a separate matrix, and derives a block-Jacobi PETSc backend. It does
36 * not relax the version-one physics or boundary-condition restrictions. All
37 * PETSc solver objects are local to this call. Rows removed
38 * by legacy boundary residual enforcement are made explicit: conditioned normal
39 * rows use X-Uconditioned, untouched dummy/tangential rows use X, and periodic
40 * duplicates use Xdup-Xrepresentative. Unsupported masked, interface, and
41 * component-disabled rows are rejected before setup.
42 *
43 * @param user Single-block momentum context.
44 * @param ibm Must be NULL; immersed boundaries are not supported in version one.
45 * @param fsi Must be NULL; moving-body coupling is not supported in version one.
46 * @return 0 when the step is committed: on SNES convergence, or when SNES fails but the
47 * residual at its iterate is finite and no larger than at entry (then
48 * `simCtx->mom_last_converged` is false). PETSC_ERR_CONV_FAILED after rollback
49 * when that residual is non-finite or grew.
50 */
51PetscErrorCode MomentumSolver_NewtonKrylov(UserCtx *user, IBMNodes *ibm, FSInfo *fsi);
52
53/**
54 * @brief Solves the momentum equations using dual-time Picard iteration with Jameson RK smoothing.
55 *
56 * =================================================================================================
57 * GLOSSARY & THEORETICAL BASIS
58 * =================================================================================================
59 * 1. METHODOLOGY: Dual-Time Stepping (Pseudo-Time Integration)
60 * We aim to solve the implicit BDF equation: R_spatial(U) + dU/dt_physical = 0.
61 * We do this by introducing a fictitious "Pseudo-Time" (tau) and iterating to steady state:
62 * dU/d(tau) = - [ R_spatial(U) + BDF_Terms(U) ]
63 * When dU/d(tau) -> 0, the physical time step is satisfied.
64 * 2. ALGORITHM: Fixed-Point Iteration with Explicit Runge-Kutta
65 * This is technically a Fixed-Point iteration on the operator:
66 * U_new = U_old + pseudo_dtau * alfa_stage * Total_Residual(U_old)
67 * where pseudo_dtau = pseudo_cfl / lambda_max is the spectral-radius-based pseudo-time step
68 * (lambda_max = global max convective spectral radius). This makes pseudo_cfl a true
69 * dimensionless Courant number, independent of the physical time step dt.
70 * We use a 4-Stage Explicit RK scheme (Jameson-Schmidt-Turkel coeffs) to smooth errors.
71 * 3. STABILITY: Adaptive Pseudo-CFL Trial Acceptance and Rollback
72 * If a pseudo-time trial causes excessive residual growth, the solver restores the
73 * previous accepted state, reduces the global pseudo-CFL, and retries.
74 * =================================================================================================
75 * VARIABLE MAPPING
76 * =================================================================================================
77 * -- Physics Variables (Legacy Names Kept) --
78 * ti : Physical Time Step Index.
79 * dt : Physical Time Step size (Delta t).
80 * st : Pseudo-Time Step size (Delta tau).
81 * alfa : Runge-Kutta stage coefficients {1/4, 1/3, 1/2, 1}.
82 * -- Convergence & Solver Control (Renamed) --
83 * pseudo_iter : Counter for the inner dual-time loop.
84 * pseudo_dtau : Adaptive pseudo-time step [physical time], = pseudo_cfl / lambda_max.
85 * lambda_max : Global max convective spectral radius [1/s] from the current field.
86 * delta_sol_norm : The L_inf norm of the change in solution (dU).
87 * resid_norm : The L_inf norm of the Total Residual (RHS).
88 * =================================================================================================
89 *
90 * @param user Primary `UserCtx` input for the operation.
91 * @param ibm Optional immersed-boundary state; NULL when IBM is disabled.
92 * @param fsi Optional fluid-structure state; NULL when FSI is disabled.
93 * @return PetscErrorCode 0 on success.
94 *
95 * @note Testing status:
96 * This solver is covered primarily through runtime smoke and orchestration
97 * tests. A smaller direct invariant-style positive-path harness remains
98 * part of the next-gap backlog.
99 */
100PetscErrorCode MomentumSolver_DualTime_Picard_JamesonRK(UserCtx *user, IBMNodes *ibm, FSInfo *fsi);
101
102/** @deprecated Use MomentumSolver_DualTime_Picard_JamesonRK(). */
103#define MomentumSolver_DualTime_Picard_RK4 MomentumSolver_DualTime_Picard_JamesonRK
104
105/*================================================================================*
106 * SHARED PHYSICAL-TIME (BDF) COEFFICIENT PLUMBING *
107 *================================================================================*/
108
109/**
110 * @brief Returns whether the current physical step uses the BDF2 discretization.
111 *
112 * Single source of truth for the BDF1/BDF2 selection. The predicate is identical
113 * to the one historically inlined in ComputeTotalResidual():
114 * BDF2 when COEF_TIME_ACCURACY > 1.1 AND step != StartStep AND step != 1,
115 * otherwise BDF1 (startup step and the first step after a restart).
116 *
117 * @param simCtx Master simulation context (reads step, StartStep).
118 * @return PETSC_TRUE for BDF2, PETSC_FALSE for BDF1.
119 */
120PetscBool MomentumUsesBDF2(SimCtx *simCtx);
121
122/**
123 * @brief Returns the BDF physical-time coefficient a0 for the current step.
124 *
125 * a0 = 1.5 (== COEF_TIME_ACCURACY) for BDF2, a0 = 1.0 for BDF1. Used both as the
126 * leading coefficient of the physical-time term in the residual and as the
127 * additive temporal contribution lambda_t = a0/dt in the momentum stability
128 * estimate, keeping the two numerically consistent.
129 *
130 * @param simCtx Master simulation context.
131 * @return a0 in {1.0, 1.5}.
132 */
133PetscReal MomentumBDFCoefficient(SimCtx *simCtx);
134
135/**
136 * @brief Computes the shared spatial-plus-BDF momentum residual in user->Rhs.
137 * @param user Block context with an allocated Rhs vector.
138 * @return PetscErrorCode 0 on success.
139 */
140PetscErrorCode ComputeTotalResidual(UserCtx *user);
141
142/*================================================================================*
143 * MOMENTUM PSEUDO-TIME STABILITY ESTIMATE (SHADOW) *
144 *================================================================================*/
145
146/**
147 * @brief Convective-estimate candidate selector. See ComputeMomentumStabilityEstimate().
148 *
149 * B: six-face transport scale (f_c * Aj * sum|U_f| / 2).
150 * C: B + frozen-advector discrete-divergence diagonal term.
151 * D: C + nonlinear velocity-gradient row-norm term (lambda_grad_u).
152 */
158
159/**
160 * @brief Dominant stiffness contributor at the controlling cell.
161 */
167
168/**
169 * @brief Diagnostic report produced by ComputeMomentumStabilityEstimate().
170 *
171 * This is a PRACTICAL CONSERVATIVE STABILITY ESTIMATE (operator-scaled pseudo-time
172 * estimate), not a proven spectral radius. All lambda_* are global maxima in [1/s].
173 */
174typedef struct {
175 PetscReal lambda; /* selected-candidate global max estimate [1/s] */
176 PetscReal lambda_t; /* temporal term a0/dt (uniform across cells) */
177 PetscReal lambda_c; /* convective part at the controlling cell */
178 PetscReal lambda_v; /* viscous part at the controlling cell */
179 PetscReal lambda_B; /* global max of (lambda_t + lambda_c^B + lambda_v) */
180 PetscReal lambda_C; /* global max with candidate C convective term */
181 PetscReal lambda_D; /* global max with candidate D convective term */
182 PetscInt ci, cj, ck; /* controlling-cell global index (selected cand) */
183 PetscInt cblock; /* controlling-cell block */
184 PetscInt cclass; /* 0=interior, 1=physical-boundary, 2=IB-adjacent */
185 PetscInt one_sided; /* controlling cell used the one-sided viscous x2 */
186 PetscInt active_cells; /* global count of active (non-masked) cells */
187 PetscBool estimate_incomplete; /* true if Clark/RANS/vel-dependent force is active (uncovered) */
188 MomStabLimiter limiter; /* dominant contributor at the controlling cell */
190
191/**
192 * @brief Compute the momentum pseudo-time stability estimate (shadow/diagnostic).
193 *
194 * Conservative, operator-scaled estimate: lambda = max_cell (a0/dt + lambda_c + lambda_nu),
195 * over active, non-solid cells, blocks, and MPI ranks, where lambda_c already includes the
196 * per-direction QUICK scheme factors. Read-only; performs no halo exchange, but does perform
197 * global scalar collectives (see implementation). This is a PRACTICAL CONSERVATIVE estimate,
198 * not a proven spectral radius. See the Workstream-A design.
199 *
200 * Call-site preconditions (NOT enforced internally): lUcont, lUcat, lNu_t, lNvert
201 * must be fresh; lAj, face Jacobians and face metrics are static after grid init.
202 *
203 * @param[in] user Array of UserCtx (one per block).
204 * @param[in] block_number Number of blocks.
205 * @param[in] dt Physical time step.
206 * @param[in] candidate Convective candidate driving rep->lambda (B, C or D).
207 * @param[out] rep Filled diagnostic report (global maxima + breakdown).
208 * @return PetscErrorCode 0 on success.
209 */
210PetscErrorCode ComputeMomentumStabilityEstimate(UserCtx *user, PetscInt block_number,
211 PetscReal dt, MomStabCandidate candidate,
212 MomStabilityReport *rep);
213
214/**
215 * @brief Active staggered-momentum row mask for a cell (exposed for unit testing).
216 *
217 * Returns a 3-bit mask (xi=1, eta=2, zeta=4). A solid cell yields 0 (all inactive); a
218 * positive solid neighbour or a positive non-periodic physical face clears the corresponding
219 * normal row; TwoD (1/2/3) clears the homogeneous direction's row.
220 *
221 * @param nvert Local nvert array (ghosted).
222 * @param k Cell k index.
223 * @param j Cell j index.
224 * @param i Cell i index.
225 * @param mx Global x dimension.
226 * @param my Global y dimension.
227 * @param mz Global z dimension.
228 * @param np_x1 True if the positive-x face is non-periodic.
229 * @param np_y1 True if the positive-y face is non-periodic.
230 * @param np_z1 True if the positive-z face is non-periodic.
231 * @param twoD TwoD homogeneous-direction selector (0 none, 1 xi, 2 eta, 3 zeta).
232 * @return 3-bit active-row mask (0 when the location carries no active unknown).
233 */
234PetscInt MomCellActiveRows(PetscReal ***nvert, PetscInt k, PetscInt j, PetscInt i,
235 PetscInt mx, PetscInt my, PetscInt mz,
236 PetscBool np_x1, PetscBool np_y1, PetscBool np_z1, PetscInt twoD);
237
238#endif // MOMENTUMSOLVERS_H
Logging utilities and macros for PETSc-based applications.
PetscBool estimate_incomplete
PetscErrorCode MomentumSolver_DualTime_Picard_JamesonRK(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Solves the momentum equations using dual-time Picard iteration with Jameson RK smoothing.
MomStabLimiter
Dominant stiffness contributor at the controlling cell.
@ MOM_STAB_LIMITER_CONVECTION
@ MOM_STAB_LIMITER_VISCOSITY
@ MOM_STAB_LIMITER_TIME
MomStabLimiter limiter
PetscBool MomentumUsesBDF2(SimCtx *simCtx)
Returns whether the current physical step uses the BDF2 discretization.
PetscReal MomentumBDFCoefficient(SimCtx *simCtx)
Returns the BDF physical-time coefficient a0 for the current step.
MomStabCandidate
Convective-estimate candidate selector.
@ MOM_STAB_CAND_B
@ MOM_STAB_CAND_C
@ MOM_STAB_CAND_D
PetscErrorCode ComputeTotalResidual(UserCtx *user)
Computes the shared spatial-plus-BDF momentum residual in user->Rhs.
PetscInt MomCellActiveRows(PetscReal ***nvert, PetscInt k, PetscInt j, PetscInt i, PetscInt mx, PetscInt my, PetscInt mz, PetscBool np_x1, PetscBool np_y1, PetscBool np_z1, PetscInt twoD)
Active staggered-momentum row mask for a cell (exposed for unit testing).
PetscErrorCode ComputeMomentumStabilityEstimate(UserCtx *user, PetscInt block_number, PetscReal dt, MomStabCandidate candidate, MomStabilityReport *rep)
Compute the momentum pseudo-time stability estimate (shadow/diagnostic).
PetscErrorCode MomentumSolver_Explicit_RungeKutta4(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Advances the momentum equations using an explicit 4th-order Runge-Kutta scheme.
Diagnostic report produced by ComputeMomentumStabilityEstimate().
Main header file for a complex fluid dynamics solver.
Holds all data related to the state and motion of a body in FSI.
Definition variables.h:501
Represents a collection of nodes forming a surface for the IBM.
Definition variables.h:428
The master context for the entire simulation.
Definition variables.h:859
User-defined context containing data specific to a single computational grid level.
Definition variables.h:1074