PICurv 0.1.0
A Parallel Particle-In-Cell Solver for Curvilinear LES
Loading...
Searching...
No Matches
solvers.c
Go to the documentation of this file.
1#include "solvers.h" // The new header we will create
2
3#undef __FUNCT__
4#define __FUNCT__ "Flow_Solver"
5/**
6 * @brief Orchestrates a single time step of the Eulerian fluid solver.
7 *
8 * This function is the refactored, high-level entry point for advancing the
9 * fluid state from time t_n to t_{n+1}. It is a direct adaptation of the
10 * legacy Flow_Solver, but it now takes the master SimCtx as its primary
11 * argument to eliminate dependencies on global variables.
12 *
13 * The sequence of operations is:
14 * 1. (Optional) Update turbulence models (RANS/LES) to compute eddy viscosity.
15 * 2. Call the core momentum solver (explicit Runge-Kutta or an implicit scheme)
16 * to get an intermediate velocity field.
17 * 3. Call the pressure-Poisson solver to compute the pressure correction.
18 * 4. Call the projection step to correct the velocity field, ensuring it is
19 * divergence-free.
20 * 5. Perform final state updates, diagnostics, and I/O.
21 *
22 * @param simCtx The master simulation context, containing all solver settings,
23 * multigrid structures, and data vectors.
24 * @return PetscErrorCode 0 on success.
25 */
26PetscErrorCode Flow_Solver(SimCtx *simCtx)
27{
28 PetscErrorCode ierr;
29 UserMG *usermg = &simCtx->usermg;
30 PetscInt level = usermg->mglevels - 1;
31 UserCtx *user = usermg->mgctx[level].user;
32 PetscReal tm_s, tm_e, tp_e, tpr_e; // Timers for profiling
33
34 PetscFunctionBeginUser;
36 LOG_ALLOW(GLOBAL, LOG_INFO, "[Step %d] Entering Flow_Solver orchestrator...\n", simCtx->step);
37
38 /*
39 // ========================================================================
40 // SECTION: O-Grid Specific Force Calculations (Legacy Feature)
41 // ========================================================================
42 // This was a specialized calculation for non-immersed O-grid cases.
43 if (simCtx->Ogrid && !simCtx->immersed) {
44 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Calculating O-grid forces...\n");
45 // Calc_forces_Ogrid(&user[0], simCtx->step, 0);
46 }
47 */
48
49
50 /*
51 // ========================================================================
52 // SECTION: Turbulence Models (RANS/LES)
53 // ========================================================================
54 // These models compute the turbulent eddy viscosity (Nu_t) which is then
55 // used by the momentum solver in the diffusion term.
56
57 if (simCtx->rans) {
58 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating RANS (k-omega) model...\n");
59 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
60 K_Omega_Set_Constant(&user[bi]);
61 if (simCtx->step == simCtx->StartStep) {
62 LOG_ALLOW(LOCAL, LOG_DEBUG, " Initializing K-Omega field for block %d.\n", bi);
63 K_Omega_IC(&user[bi]);
64 }
65 // In a full implementation, the K-Omega transport equations would be solved here.
66 // Solve_K_Omega(&user[bi]);
67 }
68 }
69
70 if (simCtx->les) {
71 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating LES (Smagorinsky) model...\n");
72 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
73 // LES models require Cartesian velocity to compute strain rates
74 ierr = Contra2Cart(&user[bi]); CHKERRQ(ierr);
75 if (simCtx->step % simCtx->dynamic_freq == 0) {
76 Compute_Smagorinsky_Constant_1(&user[bi], user[bi].lUcont, user[bi].lUcat);
77 }
78 Compute_eddy_viscosity_LES(&user[bi]);
79 }
80 }
81 */
82
83
84 // ========================================================================
85 // SECTION: Momentum Equation Solver
86 // ========================================================================
87 // This is the core of the time step. It computes an intermediate velocity
88 // field by solving the momentum equations.
89
90 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning momentum solve (Implicit Flag = %d)...\n", simCtx->implicit);
91 ierr = PetscTime(&tm_s); CHKERRQ(ierr);
92
93 if (simCtx->implicit > 0) {
94 // --- Implicit Path ---
95 LOG_ALLOW(GLOBAL, LOG_INFO, "Executing implicit momentum solver...\n");
96 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
97 ierr = ImpRK(user, NULL, NULL); CHKERRQ(ierr);
98 } else {
99 // --- Explicit Path (Default) ---
100 LOG_ALLOW(GLOBAL, LOG_INFO, "Executing Iterative implicit momentum solver (Implicit Runge-Kutta)...\n");
101 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
102 ierr = RungeKutta(user, NULL, NULL); CHKERRQ(ierr);
103 }
104
105 ierr = PetscTime(&tm_e); CHKERRQ(ierr);
106 LOG_ALLOW(GLOBAL, LOG_INFO, "Momentum solve completed in %.4f seconds.\n", tm_e - tm_s);
107
108 // ========================================================================
109 // !!! DIAGNOSTIC CHECKPOINT 1: BEFORE CORRECTION !!!
110 // ========================================================================
111 PetscReal unorm, pnorm, phinorm;
112 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
113 VecNorm(user[bi].Ucont, NORM_2, &unorm);
114 VecNorm(user[bi].P, NORM_2, &pnorm);
115 VecNorm(user[bi].Phi,NORM_2,&phinorm);
116 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Before Correction (Block %d): Ucont Norm = %e, P Norm = %e Phi Norm = %e \n", bi, unorm, pnorm, phinorm);
117 UpdateLocalGhosts(&user[bi],"Ucont");
118 UpdateLocalGhosts(&user[bi],"P");
119 }
120
121 unorm = 0.0;
122 pnorm = 0.0;
123 phinorm = 0.0;
124
125// ========================================================================
126// SECTION: Pressure-Poisson Solver
127// ========================================================================
128// This step enforces the continuity equation (incompressibility) by solving
129// for a pressure correction field.
130
131 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning pressure-Poisson solve (Poisson Flag = %d)...\n", simCtx->poisson);
132
133 if (simCtx->poisson == 0) {
134 ierr = PoissonSolver_MG(usermg); CHKERRQ(ierr);
135 } else {
136 // Logic for other Poisson solvers (e.g., Hypre) would go here.
137 LOG_ALLOW(GLOBAL, LOG_WARNING, "Poisson solver type %d is not currently enabled.\n", simCtx->poisson);
138 }
139
140 ierr = PetscTime(&tp_e); CHKERRQ(ierr);
141 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Pressure-Poisson solve completed in %.4f seconds.\n", tp_e - tm_e);
142
143 // ========================================================================
144 // SECTION: Velocity Correction (Projection)
145 // ========================================================================
146 // The pressure correction is used to update the pressure field and project
147 // the intermediate velocity onto a divergence-free space.
148
149 LOG_ALLOW(GLOBAL, LOG_INFO, "Applying velocity correction/projection step...\n");
150 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
151 ierr = UpdatePressure(&user[bi]); CHKERRQ(ierr);
152 LOG_ALLOW(GLOBAL,LOG_INFO," Pressure Updated for Block %d.\n",bi);
153
154 ierr = Projection(&user[bi]); CHKERRQ(ierr);
155
156 LOG_ALLOW(GLOBAL,LOG_INFO," Velocity corrected for Block %d.\n",bi);
157
158 // Ensure local ghost cells for the final pressure field are correct
159 ierr = UpdateLocalGhosts(&user[bi],"P");
160 }
161
162 ierr = PetscTime(&tpr_e); CHKERRQ(ierr);
163 LOG_ALLOW(GLOBAL, LOG_INFO, "Velocity correction completed in %.4f seconds.\n", tpr_e - tp_e);
164
165
166 // ========================================================================
167 // !!! DIAGNOSTIC CHECKPOINT 2: AFTER CORRECTION !!!
168 // ========================================================================
169 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
170 VecNorm(user[bi].Ucont, NORM_2, &unorm);
171 VecNorm(user[bi].P, NORM_2, &pnorm);
172 VecNorm(user[bi].Phi,NORM_2,&phinorm);
173 LOG_ALLOW(GLOBAL, LOG_DEBUG, "After Correction (Block %d): Ucont Norm = %e, P Norm = %e, Phi Norm = %e\n", bi, unorm, pnorm,phinorm);
174 }
175 // ========================================================================
176
177 // ========================================================================
178 // SECTION: Final Diagnostics and I/O
179 // ========================================================================
180
181 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
182 LOG_ALLOW(GLOBAL, LOG_INFO, "Finalizing state & Diagnostics for block %d...\n", bi);
183
184 // --- Perform Divergence Check ---
185 // This is a diagnostic to verify the quality of the velocity correction.
186 ierr = Divergence(&user[bi]); CHKERRQ(ierr);
187
188 // -- Log Continuity metrics ----
189 ierr = LOG_CONTINUITY_METRICS(&user[bi]);
190 /*
191 // --- Immersed Boundary Interpolation (Post-Correction) ---
192 // This step would update the velocity values AT the IB nodes to match the
193 // newly corrected fluid field. Important for the next time step.
194 if (simCtx->immersed) {
195 for (PetscInt ibi = 0; ibi < simCtx->NumberOfBodies; ibi++) {
196 ibm_interpolation_advanced(&user[bi], &simCtx->ibm[ibi], ibi, 1);
197 }
198 }
199 */
200
201 // --- Averaging and Statistics (if enabled) ---
202 /*
203 if (simCtx->averaging) {
204 LOG_ALLOW(LOCAL, LOG_DEBUG, "Performing statistical averaging for block %d.\n", bi);
205 Do_averaging(&user[bi]);
206 }
207 */
208
209 // }
210 }
211
212 // --- Profiling Output ---
213 // (This can be kept as is, but it's good practice to wrap it in a LOG_PROFILE check)
214 LOG_PROFILE_MSG(GLOBAL, "[Step %d]: Momentum=%.4fs, Poisson=%.4fs, Projection=%.4fs, TOTAL=%.4fs\n",
215 simCtx->step, tm_e - tm_s, tp_e - tm_e, tpr_e - tp_e, tpr_e - tm_s);
216
217 LOG_ALLOW(GLOBAL, LOG_INFO, "Flow_Solver orchestrator finished for step %d.\n", simCtx->step);
219 PetscFunctionReturn(0);
220}
PetscErrorCode ImpRK(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Advances the momentum equations using an iterative explicit Runge-Kutta scheme.
PetscErrorCode RungeKutta(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Advances the momentum equations using an explicit 4th-order Runge-Kutta scheme.
#define GLOBAL
Scope for global logging across all processes.
Definition logging.h:45
#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:207
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:767
#define LOG_PROFILE_MSG(scope, fmt,...)
Definition logging.h:479
PetscErrorCode LOG_CONTINUITY_METRICS(UserCtx *user)
Logs continuity metrics for a single block to a file.
Definition logging.c:819
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:32
@ LOG_WARNING
Non-critical issues that warrant attention.
Definition logging.h:30
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:33
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled code block (typically a function).
Definition logging.h:758
PetscErrorCode Projection(UserCtx *user)
Corrects the contravariant velocity field Ucont to be divergence-free using the gradient of the press...
Definition poisson.c:988
PetscErrorCode UpdatePressure(UserCtx *user)
Updates the pressure field P with the pressure correction Phi computed by the Poisson solver.
Definition poisson.c:1542
PetscErrorCode PoissonSolver_MG(UserMG *usermg)
Solves the pressure-Poisson equation using a geometric multigrid method.
Definition poisson.c:3982
PetscErrorCode Divergence(UserCtx *user)
Definition setup.c:2071
PetscErrorCode UpdateLocalGhosts(UserCtx *user, const char *fieldName)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:779
PetscErrorCode Flow_Solver(SimCtx *simCtx)
Orchestrates a single time step of the Eulerian fluid solver.
Definition solvers.c:26
UserCtx * user
Definition variables.h:418
PetscInt block_number
Definition variables.h:562
PetscInt implicit
Definition variables.h:543
UserMG usermg
Definition variables.h:599
PetscInt poisson
Definition variables.h:546
PetscInt mglevels
Definition variables.h:425
PetscInt step
Definition variables.h:521
MGCtx * mgctx
Definition variables.h:428
The master context for the entire simulation.
Definition variables.h:513
User-defined context containing data specific to a single computational grid level.
Definition variables.h:630
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:424