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