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__ "FlowSolver"
5/**
6 * @brief Implementation of \ref FlowSolver().
7 * @details Full API contract (arguments, ownership, side effects) is documented with
8 * the header declaration in `include/solvers.h`.
9 * @see FlowSolver()
10 */
11PetscErrorCode FlowSolver(SimCtx *simCtx)
12{
13 PetscErrorCode ierr;
14 UserMG *usermg = NULL;
15 PetscInt level;
16 UserCtx *user = NULL;
17
18 PetscFunctionBeginUser;
20
23 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
24 "Unknown momentum solver type %d. Supported values are EXPLICIT_RK and DUALTIME_PICARD_RK4.",
25 simCtx->mom_solver_type);
26 }
27
28 usermg = &simCtx->usermg;
29 level = usermg->mglevels - 1;
30 user = usermg->mgctx[level].user;
31 LOG_ALLOW(GLOBAL, LOG_INFO, "[Step %d] Entering orchestrator...\n", simCtx->step);
32
33 /*
34 // ========================================================================
35 // SECTION: O-Grid Specific Force Calculations (Legacy Feature)
36 // ========================================================================
37 // This was a specialized calculation for non-immersed O-grid cases.
38 if (simCtx->Ogrid && !simCtx->immersed) {
39 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Calculating O-grid forces...\n");
40 // Calc_forces_Ogrid(&user[0], simCtx->step, 0);
41 }
42 */
43
44
45
46 // ========================================================================
47 // SECTION: Turbulence Models (RANS/LES)
48 // ========================================================================
49 // These models compute the turbulent eddy viscosity (Nu_t) which is then
50 // used by the momentum solver in the diffusion term.
51
52 /*
53 if (simCtx->rans) {
54 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating RANS (k-omega) model...\n");
55 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
56 K_Omega_Set_Constant(&user[bi]);
57 if (simCtx->step == simCtx->StartStep) {
58 LOG_ALLOW(LOCAL, LOG_DEBUG, " Initializing K-Omega field for block %d.\n", bi);
59 K_Omega_IC(&user[bi]);
60 }
61 // In a full implementation, the K-Omega transport equations would be solved here.
62 // Solve_K_Omega(&user[bi]);
63 }
64 }
65 */
66
67 if (simCtx->les) {
68 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating LES (Smagorinsky) model...\n");
69 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
70 // LES models require Cartesian velocity to compute strain rates
71 ierr = UpdateLocalGhosts(&user[bi], "Ucont");
72 ierr = Contra2Cart(&user[bi]); CHKERRQ(ierr);
73 ierr = UpdateLocalGhosts(&user[bi], "Ucat");
74 if(simCtx->les == CONSTANT_SMAGORINSKY) {
75 LOG_ALLOW(LOCAL, LOG_INFO, " Using constant Smagorinsky model for block %d.\n", bi);
76 // Constant Smagorinsky does not require dynamic computation
77 if(simCtx->step == simCtx->StartStep + 1){ // since step is updated before flowsolver call.
78 ierr = ComputeSmagorinskyConstant(&user[bi]); CHKERRQ(ierr);
79 }
80 } else if(simCtx->les == DYNAMIC_SMAGORINSKY) {
81 if (simCtx->step % simCtx->dynamic_freq == 0) {
82 LOG_ALLOW(LOCAL, LOG_DEBUG, " Computing Smagorinsky constant for block %d.\n", bi);
83 ierr = ComputeSmagorinskyConstant(&user[bi]);
84 }
85 }
86 // LOG_ALLOW(LOCAL, LOG_DEBUG, " Computing eddy viscosity for block %d.\n", bi);
87 ierr = ComputeEddyViscosityLES(&user[bi]);
88 }
89 }
90
91
92 // ========================================================================
93 // SECTION: Momentum Equation Solver
94 // ========================================================================
95 // This is the core of the time step. It computes an intermediate velocity
96 // field by solving the momentum equations.
97
98 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning momentum step solve (Solver = %s)...\n", MomentumSolverTypeToString(simCtx->mom_solver_type));
99
100 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
101 // ierr = ImpRK(user, NULL, NULL); CHKERRQ(ierr);
102 // Add new momentum solver types here only after wiring the enum, parser, docs, and tests.
104 ierr = MomentumSolver_DualTime_Picard_RK4(user,NULL,NULL); CHKERRQ(ierr);
105 } else if(simCtx->mom_solver_type == MOMENTUM_SOLVER_EXPLICIT_RK) {
106 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
107 ierr = MomentumSolver_Explicit_RungeKutta4(user, NULL, NULL); CHKERRQ(ierr);
108 }
109// ========================================================================
110// SECTION: Pressure-Poisson Solver
111// ========================================================================
112// This step enforces the continuity equation (incompressibility) by solving
113// for a pressure correction field.
114
115 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning pressure-Poisson solve (Poisson Flag = %d)...\n", simCtx->poisson);
116
117 if (simCtx->poisson == 0) {
118 ierr = PoissonSolver_MG(usermg); CHKERRQ(ierr);
119 } else {
120 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
121 "Unsupported Poisson solver type %d. The current runtime supports only the multigrid path (poisson = 0).",
122 simCtx->poisson);
123 }
124
125 // ========================================================================
126 // SECTION: Velocity Correction (Projection)
127 // ========================================================================
128 // The pressure correction is used to update the pressure field and project
129 // the intermediate velocity onto a divergence-free space.
130
131 LOG_ALLOW(GLOBAL, LOG_INFO, "Applying velocity correction/projection step...\n");
132 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
133 ierr = UpdatePressure(&user[bi]); CHKERRQ(ierr);
134 LOG_ALLOW(GLOBAL,LOG_INFO," Pressure Updated for Block %d.\n",bi);
135
136 ierr = Projection(&user[bi]); CHKERRQ(ierr);
137
138 LOG_ALLOW(GLOBAL,LOG_INFO," Velocity corrected for Block %d.\n",bi);
139
140 // Ensure local ghost cells for the final pressure field are correct
141 ierr = UpdateLocalGhosts(&user[bi],"P");
142 }
143
144 // ========================================================================
145
146 // ========================================================================
147 // SECTION: Final Diagnostics and I/O
148 // ========================================================================
149
150 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
151 LOG_ALLOW(GLOBAL, LOG_INFO, "Finalizing state & Diagnostics for block %d...\n", bi);
152
153 // --- Perform Divergence Check ---
154 // This is a diagnostic to verify the quality of the velocity correction.
155 ierr = ComputeDivergence(&user[bi]); CHKERRQ(ierr);
156
157 // -- Log Continuity metrics ----
158 ierr = LOG_CONTINUITY_METRICS(&user[bi]);
159 /*
160 // --- Immersed Boundary Interpolation (Post-Correction) ---
161 // This step would update the velocity values AT the IB nodes to match the
162 // newly corrected fluid field. Important for the next time step.
163 if (simCtx->immersed) {
164 for (PetscInt ibi = 0; ibi < simCtx->NumberOfBodies; ibi++) {
165 ibm_interpolation_advanced(&user[bi], &simCtx->ibm[ibi], ibi, 1);
166 }
167 }
168 */
169
170 // --- Averaging and Statistics (if enabled) ---
171 /*
172 if (simCtx->averaging) {
173 LOG_ALLOW(LOCAL, LOG_DEBUG, "Performing statistical averaging for block %d.\n", bi);
174 Do_averaging(&user[bi]);
175 }
176 */
177
178 // }
179 }
180
181 LOG_ALLOW(GLOBAL, LOG_INFO, "orchestrator finished for step %d.\n", simCtx->step);
183 PetscFunctionReturn(0);
184}
PetscErrorCode ComputeEddyViscosityLES(UserCtx *user)
Computes the turbulent eddy viscosity (Nu_t) for the LES model.
Definition les.c:315
PetscErrorCode ComputeSmagorinskyConstant(UserCtx *user)
Computes the dynamic Smagorinsky constant (Cs) for the LES model.
Definition les.c:30
#define LOCAL
Logging scope definitions for controlling message output.
Definition logging.h:44
#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:199
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:779
PetscErrorCode LOG_CONTINUITY_METRICS(UserCtx *user)
Logs continuity metrics for a single block to a file.
Definition logging.c:914
@ LOG_INFO
Informational messages about program execution.
Definition logging.h:30
@ LOG_DEBUG
Detailed debugging information.
Definition logging.h:31
#define PROFILE_FUNCTION_BEGIN
Marks the beginning of a profiled code block (typically a function).
Definition logging.h:770
const char * MomentumSolverTypeToString(MomentumSolverType SolverFlag)
Helper function to convert Momentum Solver flag to a string representation.
Definition logging.c:736
PetscErrorCode MomentumSolver_DualTime_Picard_RK4(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Solves the Momentum Equations using Dual-Time Stepping with a Fixed-Point RK4 Smoother.
PetscErrorCode MomentumSolver_Explicit_RungeKutta4(UserCtx *user, IBMNodes *ibm, FSInfo *fsi)
Advances the momentum equations using an explicit 4th-order Runge-Kutta scheme.
PetscErrorCode Projection(UserCtx *user)
Corrects the contravariant velocity field Ucont to be divergence-free using the gradient of the press...
Definition poisson.c:328
PetscErrorCode UpdatePressure(UserCtx *user)
Updates the pressure field P with the pressure correction Phi computed by the Poisson solver.
Definition poisson.c:854
PetscErrorCode PoissonSolver_MG(UserMG *usermg)
Solves the pressure-Poisson equation using a geometric multigrid method.
Definition poisson.c:3344
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:2247
PetscErrorCode ComputeDivergence(UserCtx *user)
Computes the discrete divergence of the contravariant velocity field.
Definition setup.c:2518
PetscErrorCode UpdateLocalGhosts(UserCtx *user, const char *fieldName)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:1361
PetscErrorCode FlowSolver(SimCtx *simCtx)
Implementation of FlowSolver().
Definition solvers.c:11
@ DYNAMIC_SMAGORINSKY
Definition variables.h:491
@ CONSTANT_SMAGORINSKY
Definition variables.h:490
UserCtx * user
Definition variables.h:528
PetscInt block_number
Definition variables.h:712
UserMG usermg
Definition variables.h:764
PetscInt StartStep
Definition variables.h:653
@ MOMENTUM_SOLVER_EXPLICIT_RK
Definition variables.h:503
@ MOMENTUM_SOLVER_DUALTIME_PICARD_RK4
Definition variables.h:504
PetscInt poisson
Definition variables.h:687
PetscInt mglevels
Definition variables.h:535
PetscInt dynamic_freq
Definition variables.h:733
PetscInt step
Definition variables.h:651
PetscInt les
Definition variables.h:732
MGCtx * mgctx
Definition variables.h:538
MomentumSolverType mom_solver_type
Definition variables.h:683
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
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:534