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
24 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
25 "Unknown momentum solver type %d. Supported values are EXPLICIT_RK, DUALTIME_PICARD_JAMESON_RK, and NEWTON_KRYLOV.",
26 simCtx->mom_solver_type);
27 }
28
29 usermg = &simCtx->usermg;
30 level = usermg->mglevels - 1;
31 user = usermg->mgctx[level].user;
32 LOG_ALLOW(GLOBAL, LOG_INFO, "[Step %d] Entering orchestrator...\n", simCtx->step);
33
34 /*
35 // ========================================================================
36 // SECTION: O-Grid Specific Force Calculations (Legacy Feature)
37 // ========================================================================
38 // This was a specialized calculation for non-immersed O-grid cases.
39 if (simCtx->Ogrid && !simCtx->immersed) {
40 LOG_ALLOW(GLOBAL, LOG_DEBUG, "Calculating O-grid forces...\n");
41 // Calc_forces_Ogrid(&user[0], simCtx->step, 0);
42 }
43 */
44
45
46
47 // ========================================================================
48 // SECTION: Turbulence Models (LES)
49 // ========================================================================
50 // These models compute the turbulent eddy viscosity (Nu_t) which is then
51 // used by the momentum solver in the diffusion term.
52
53
54 if (simCtx->les) {
55 LOG_ALLOW(GLOBAL, LOG_INFO, "Updating LES subgrid-scale model...\n");
56 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
57 // Strain rates are formed from the Cartesian velocity, so it must be
58 // reconstructed and its ghost and periodic images refreshed first.
59 const FieldId staggered_fields[] = {FIELD_ID_UCONT};
60 const FieldId cell_fields[] = {FIELD_ID_UCAT};
61
62 ierr = SynchronizePeriodicStaggeredFields(&user[bi], 1, staggered_fields); CHKERRQ(ierr);
63 ierr = Contra2Cart(&user[bi]); CHKERRQ(ierr);
64 ierr = SynchronizePeriodicCellFields(&user[bi], 1, cell_fields); CHKERRQ(ierr);
65 ierr = UpdateLocalGhosts(&user[bi], FIELD_ID_UCAT); CHKERRQ(ierr);
66
67 // Contra2Cart rebuilds the Cartesian field from the contravariant one and so
68 // discards any near-wall correction the previous momentum solve applied. The
69 // wall model is re-applied here, before the strain rates are formed, so the
70 // closure and the momentum equation agree about the near-wall velocity. On
71 // the coarse grids a wall model exists to serve, the uncorrected strain is
72 // under-predicted, and the eddy viscosity with it. Mirrors the order the
73 // boundary pass itself uses: reconstruct, exchange, correct, exchange.
74 if (simCtx->wallfunction) {
75 ierr = ApplyWallFunction(&user[bi]); CHKERRQ(ierr);
76 ierr = SynchronizePeriodicCellFields(&user[bi], 1, cell_fields); CHKERRQ(ierr);
77 ierr = UpdateLocalGhosts(&user[bi], FIELD_ID_UCAT); CHKERRQ(ierr);
78 }
79
80 // Only the dynamic model has a coefficient to recompute, and it honours
81 // its own cadence. The constant model's coefficient never changes.
82 if (simCtx->les == DYNAMIC_SMAGORINSKY &&
83 simCtx->step % simCtx->les_config.dynamic_frequency == 0) {
84 LOG_ALLOW(LOCAL, LOG_DEBUG, " Computing dynamic coefficient for block %d.\n", bi);
85 ierr = ComputeSmagorinskyConstant(&user[bi]); CHKERRQ(ierr);
86 }
87
88 ierr = ComputeEddyViscosityLES(&user[bi]); CHKERRQ(ierr);
89 ierr = LogLESDiagnostics(&user[bi]); CHKERRQ(ierr);
90 }
91 }
92
93
94 // ========================================================================
95 // SECTION: Momentum Equation Solver
96 // ========================================================================
97 // This is the core of the time step. It computes an intermediate velocity
98 // field by solving the momentum equations.
99
100 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning momentum step solve (Solver = %s)...\n", MomentumSolverTypeToString(simCtx->mom_solver_type));
101
102 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
103 // ierr = ImpRK(user, NULL, NULL); CHKERRQ(ierr);
104 // Add new momentum solver types here only after wiring the enum, parser, docs, and tests.
106 ierr = MomentumSolver_DualTime_Picard_JamesonRK(user,NULL,NULL); CHKERRQ(ierr);
107 } else if(simCtx->mom_solver_type == MOMENTUM_SOLVER_EXPLICIT_RK) {
108 // Since IBM is disabled, we pass NULL for ibm and fsi arguments.
109 ierr = MomentumSolver_Explicit_RungeKutta4(user, NULL, NULL); CHKERRQ(ierr);
110 } else if (simCtx->mom_solver_type == MOMENTUM_SOLVER_NEWTON_KRYLOV) {
111 ierr = MomentumSolver_NewtonKrylov(user, NULL, NULL); CHKERRQ(ierr);
112 }
113// ========================================================================
114// SECTION: Pressure-Poisson Solver
115// ========================================================================
116// This step enforces the continuity equation (incompressibility) by solving
117// for a pressure correction field.
118
119 LOG_ALLOW(GLOBAL, LOG_INFO, "Beginning pressure-Poisson solve (Poisson Flag = %d)...\n", simCtx->poisson);
120
121 if (simCtx->poisson == 0) {
122 ierr = PoissonSolver_Multigrid(usermg); CHKERRQ(ierr);
123 } else {
124 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG,
125 "Unsupported Poisson solver type %d. The current runtime supports only the multigrid path (poisson = 0).",
126 simCtx->poisson);
127 }
128
129 // ========================================================================
130 // SECTION: Velocity Correction (Projection)
131 // ========================================================================
132 // The pressure correction is used to update the pressure field and project
133 // the intermediate velocity onto a divergence-free space.
134
135 LOG_ALLOW(GLOBAL, LOG_INFO, "Applying velocity correction/projection step...\n");
136 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
137 ierr = UpdatePressure(&user[bi]); CHKERRQ(ierr);
138 LOG_ALLOW(GLOBAL,LOG_INFO," Pressure Updated for Block %d.\n",bi);
139
140 ierr = ProjectVelocity(&user[bi]); CHKERRQ(ierr);
141
142 LOG_ALLOW(GLOBAL,LOG_INFO," Velocity corrected for Block %d.\n",bi);
143
144 // Ensure local ghost cells for the final pressure field are correct
145 ierr = UpdateLocalGhosts(&user[bi], FIELD_ID_P);
146 }
147
148 // ========================================================================
149
150 // ========================================================================
151 // SECTION: Final Diagnostics and I/O
152 // ========================================================================
153
154 for (PetscInt bi = 0; bi < simCtx->block_number; bi++) {
155 LOG_ALLOW(GLOBAL, LOG_INFO, "Finalizing state & Diagnostics for block %d...\n", bi);
156
157 // --- Perform Divergence Check ---
158 // This is a diagnostic to verify the quality of the velocity correction.
159 ierr = ComputeDivergence(&user[bi]); CHKERRQ(ierr);
160
161 // -- Log Continuity metrics ----
162 ierr = LOG_CONTINUITY_METRICS(&user[bi]);
163
164 /* Reported here rather than beside the LES block, because a wall model is
165 configured independently of LES and its last pass this step is the boundary
166 pass, not the closure prologue. A no-op when no wall model is active. */
167 ierr = LogWallModelDiagnostics(&user[bi]); CHKERRQ(ierr);
168 /* The force a driven-periodic controller applied this step. A no-op without one. */
169 ierr = LogDrivenFlowDiagnostics(&user[bi]); CHKERRQ(ierr);
170 /*
171 // --- Immersed Boundary Interpolation (Post-Correction) ---
172 // This step would update the velocity values AT the IB nodes to match the
173 // newly corrected fluid field. Important for the next time step.
174 if (simCtx->immersed) {
175 for (PetscInt ibi = 0; ibi < simCtx->NumberOfBodies; ibi++) {
176 ibm_interpolation_advanced(&user[bi], &simCtx->ibm[ibi], ibi, 1);
177 }
178 }
179 */
180
181 // }
182 }
183
184 LOG_ALLOW(GLOBAL, LOG_INFO, "orchestrator finished for step %d.\n", simCtx->step);
186 PetscFunctionReturn(0);
187}
PetscErrorCode LogDrivenFlowDiagnostics(UserCtx *user)
Appends one row of driven-flow controller state to driven_flow.csv.
Definition BodyForces.c:174
PetscErrorCode ApplyWallFunction(UserCtx *user)
Applies wall function modeling to near-wall velocities for all wall-type boundaries.
PetscErrorCode LogWallModelDiagnostics(UserCtx *user)
Appends one row of near-wall statistics to <run.analysis.metrics>/wall_model.csv.
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:952
PetscErrorCode LogLESDiagnostics(UserCtx *user)
Appends one row of LES coefficient statistics to the run's log directory.
Definition les.c:1045
PetscErrorCode ComputeSmagorinskyConstant(UserCtx *user)
Computes the dynamic Smagorinsky coefficient field for one block.
Definition les.c:715
#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:200
#define PROFILE_FUNCTION_END
Marks the end of a profiled code block.
Definition logging.h:894
PetscErrorCode LOG_CONTINUITY_METRICS(UserCtx *user)
Logs continuity metrics for a single block to a file.
Definition logging.c:1891
@ 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 code block (typically a function).
Definition logging.h:885
const char * MomentumSolverTypeToString(MomentumSolverType SolverFlag)
Returns the canonical log token for a momentum-solver selector.
Definition logging.c:853
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:416
PetscErrorCode ProjectVelocity(UserCtx *user)
Corrects the contravariant flux with the gradient of Phi.
Definition poisson.c:438
PetscErrorCode PoissonSolver_Multigrid(UserMG *usermg)
Solves the pressure-correction equation for every block with geometric multigrid.
Definition poisson.c:952
PetscErrorCode Contra2Cart(UserCtx *user)
Reconstructs Cartesian velocity (Ucat) at cell centers from contravariant velocity (Ucont) defined on...
Definition setup.c:3300
PetscErrorCode ComputeDivergence(UserCtx *user)
Computes the discrete divergence of the contravariant velocity field.
Definition setup.c:3685
PetscErrorCode UpdateLocalGhosts(UserCtx *user, FieldId field_id)
Updates the local vector (including ghost points) from its corresponding global vector.
Definition setup.c:2489
PetscErrorCode FlowSolver(SimCtx *simCtx)
Implementation of FlowSolver().
Definition solvers.c:11
@ DYNAMIC_SMAGORINSKY
Definition variables.h:551
UserCtx * user
Definition variables.h:729
PetscInt dynamic_frequency
Recompute the dynamic coefficient every N steps.
Definition variables.h:630
PetscInt block_number
Definition variables.h:952
LESConfig les_config
Parameters of the LES closure selected by les.
Definition variables.h:987
UserMG usermg
Definition variables.h:1015
@ MOMENTUM_SOLVER_DUALTIME_PICARD_JAMESON_RK
Definition variables.h:694
@ MOMENTUM_SOLVER_EXPLICIT_RK
Definition variables.h:693
@ MOMENTUM_SOLVER_NEWTON_KRYLOV
Definition variables.h:695
PetscInt poisson
Definition variables.h:903
PetscInt wallfunction
Enable wall functions on WALL faces.
Definition variables.h:985
PetscInt mglevels
Definition variables.h:736
PetscInt step
Definition variables.h:867
PetscInt les
Active LES closure; an LESModelType value.
Definition variables.h:984
MGCtx * mgctx
Definition variables.h:739
MomentumSolverType mom_solver_type
Definition variables.h:899
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
User-level context for managing the entire multigrid hierarchy.
Definition variables.h:735