Line data Source code
1 : /**
2 : * Projection Method NSSolver (Chorin's Method)
3 : *
4 : * The projection method solves the incompressible Navier-Stokes equations
5 : * by splitting the solution into two steps:
6 : *
7 : * 1. Predictor Step: Compute intermediate velocity u* ignoring pressure
8 : * u* = u^n + dt * (-u·∇u + ν∇²u + f)
9 : *
10 : * 2. Pressure Projection: Solve Poisson equation for pressure
11 : * ∇²p = (ρ/dt) * ∇·u*
12 : *
13 : * 3. Corrector Step: Project velocity to be divergence-free
14 : * u^(n+1) = u* - (dt/ρ) * ∇p
15 : *
16 : * This ensures ∇·u^(n+1) = 0 (incompressibility constraint)
17 : */
18 :
19 : #include "cfd/boundary/boundary_conditions.h"
20 : #include "cfd/core/cfd_status.h"
21 : #include "cfd/core/grid.h"
22 : #include "cfd/core/indexing.h"
23 : #include "cfd/core/memory.h"
24 : #include "cfd/solvers/energy_solver.h"
25 : #include "cfd/solvers/navier_stokes_solver.h"
26 : #include "cfd/solvers/poisson_solver.h"
27 : #include "cfd/solvers/turbulence_solver.h"
28 :
29 : #include "../../energy/energy_solver_internal.h"
30 : #include "../../linear/multigrid_internal.h"
31 : #include "../../turbulence/turbulence_solver_internal.h"
32 : #include "../boundary_copy_utils.h"
33 : #include "../ns_convection_internal.h"
34 : #include "../ns_pressure_internal.h"
35 :
36 : #include <math.h>
37 : #include <stdio.h>
38 : #include <string.h>
39 :
40 : #ifndef M_PI
41 : #define M_PI 3.14159265358979323846
42 : #endif
43 :
44 : // Physical limits
45 : #define MAX_VELOCITY 100.0
46 : #define MAX_PRESSURE 1000.0
47 :
48 : /**
49 : * Turn the explicit increment in `star` into the implicit viscous one:
50 : *
51 : * (I - theta*nu*dt*lap) delta = star - cur, star = cur + delta
52 : *
53 : * solved as lap(delta) - sigma*delta = -sigma*(star - cur) with delta = 0 on the
54 : * boundary nodes, where star already equals cur. See ns_viscous_internal.h.
55 : * Clamped afterwards: the explicit increment is left unclamped by the predictor,
56 : * because at a large dt it legitimately overshoots and the solve is what
57 : * removes that overshoot.
58 : */
59 2094 : static cfd_status_t implicit_viscous_component(poisson_solver_t* viscous,
60 : const double* cur, double* star,
61 : double* rhs, double* delta,
62 : size_t nx, size_t ny, size_t total,
63 : size_t k_start, size_t k_end,
64 : size_t stride_z) {
65 2094 : double sigma = viscous->params.helmholtz_shift;
66 :
67 2094 : memset(delta, 0, total * sizeof(double));
68 4908 : for (size_t k = k_start; k < k_end; k++) {
69 64224 : for (size_t j = 1; j < ny - 1; j++) {
70 1739040 : for (size_t i = 1; i < nx - 1; i++) {
71 1677630 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
72 1677630 : rhs[idx] = -sigma * (star[idx] - cur[idx]);
73 : }
74 : }
75 : }
76 :
77 2094 : poisson_solver_stats_t vstats = poisson_solver_stats_default();
78 2094 : cfd_status_t status = poisson_solver_solve(viscous, delta, NULL, rhs, &vstats);
79 2094 : if (status != CFD_SUCCESS) {
80 : return status;
81 : }
82 :
83 4908 : for (size_t k = k_start; k < k_end; k++) {
84 64224 : for (size_t j = 1; j < ny - 1; j++) {
85 1739040 : for (size_t i = 1; i < nx - 1; i++) {
86 1677630 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
87 1677630 : star[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, cur[idx] + delta[idx]));
88 : }
89 : }
90 : }
91 : return CFD_SUCCESS;
92 : }
93 :
94 : /**
95 : * Projection Method Solver
96 : *
97 : * `viscous` is the implicit viscous solver for an implicit
98 : * params->viscous_scheme, or NULL to advance the viscous term explicitly.
99 : */
100 13210 : cfd_status_t solve_projection_method(flow_field* field, const grid* grid,
101 : const ns_solver_params_t* params,
102 : poisson_solver_t* pressure,
103 : poisson_solver_t* viscous) {
104 13210 : if (!field || !grid || !params || !pressure) {
105 : return CFD_ERROR_INVALID;
106 : }
107 13210 : if (field->nx < 3 || field->ny < 3 || (field->nz > 1 && field->nz < 3)) {
108 : return CFD_ERROR_INVALID;
109 : }
110 : /* The buffers below are sized from the field; the solver from the grid. */
111 13210 : cfd_status_t shape_status =
112 13210 : ns_pressure_check_shape(pressure, field->nx, field->ny, field->nz);
113 13210 : if (shape_status == CFD_SUCCESS && viscous) {
114 698 : shape_status = ns_pressure_check_shape(viscous, field->nx, field->ny, field->nz);
115 : }
116 13210 : if (shape_status != CFD_SUCCESS) {
117 : return shape_status;
118 : }
119 : /* The implicit operator has one constant viscosity; nu + nu_t does not. */
120 13210 : if (viscous && params->turb_model != TURB_MODEL_NONE) {
121 0 : cfd_set_error(CFD_ERROR_UNSUPPORTED,
122 : "The implicit viscous solve does not support turbulence models");
123 0 : return CFD_ERROR_UNSUPPORTED;
124 : }
125 :
126 13210 : size_t nx = field->nx;
127 13210 : size_t ny = field->ny;
128 13210 : size_t nz = field->nz;
129 :
130 : /* Reject non-uniform z-spacing (solver uses constant dz) */
131 13210 : if (nz > 1 && grid->dz) {
132 327 : for (size_t k = 1; k < nz - 1; k++) {
133 286 : if (fabs(grid->dz[k] - grid->dz[0]) > 1e-14) {
134 : return CFD_ERROR_INVALID;
135 : }
136 : }
137 : }
138 :
139 13210 : size_t plane = nx * ny;
140 13210 : size_t total = plane * nz;
141 13210 : size_t bytes = total * sizeof(double);
142 :
143 : /* Grid spacing (uniform grid) */
144 13210 : double dx = grid->dx[0];
145 13210 : double dy = grid->dy[0];
146 13210 : double dz = (nz > 1 && grid->dz) ? grid->dz[0] : 0.0;
147 13210 : double dt = params->dt;
148 13210 : double nu = params->mu;
149 :
150 :
151 : /* Branch-free 3D constants */
152 13210 : size_t stride_z = (nz > 1) ? plane : 0;
153 13210 : size_t k_start = (nz > 1) ? 1 : 0;
154 13210 : size_t k_end = (nz > 1) ? (nz - 1) : 1;
155 13210 : double inv_2dz = (nz > 1 && grid->dz) ? 1.0 / (2.0 * dz) : 0.0;
156 41 : double inv_dz2 = (nz > 1 && grid->dz) ? 1.0 / (dz * dz) : 0.0;
157 13210 : double inv_dz = 2.0 * inv_2dz;
158 13210 : const int upwind = (params->convection_scheme == NS_CONVECTION_SCHEME_UPWIND);
159 :
160 : /* Allocate temporary arrays */
161 13210 : double* u_star = (double*)cfd_calloc(total, sizeof(double));
162 13210 : double* v_star = (double*)cfd_calloc(total, sizeof(double));
163 13210 : double* w_star = (double*)cfd_calloc(total, sizeof(double));
164 13210 : double* p_new = (double*)cfd_calloc(total, sizeof(double));
165 13210 : double* p_temp = (double*)cfd_calloc(total, sizeof(double));
166 13210 : double* rhs = (double*)cfd_calloc(total, sizeof(double));
167 13210 : int needs_T_ws = (params->alpha > 0.0 || params->beta != 0.0);
168 13210 : double* T_energy_ws = needs_T_ws
169 7518 : ? (double*)cfd_calloc(total, sizeof(double)) : NULL;
170 13210 : const int turb_on = (params->turb_model != TURB_MODEL_NONE);
171 26420 : double* turb_ws = turb_on
172 13210 : ? (double*)cfd_calloc(TURB_WORKSPACE_SIZE(total), sizeof(double)) : NULL;
173 13210 : const int implicit_visc = (viscous != NULL);
174 13210 : double* delta = implicit_visc ? (double*)cfd_calloc(total, sizeof(double)) : NULL;
175 :
176 13210 : if (!u_star || !v_star || !w_star || !p_new || !p_temp || !rhs ||
177 13210 : (needs_T_ws && !T_energy_ws) || (turb_on && !turb_ws) ||
178 13210 : (implicit_visc && !delta)) {
179 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
180 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
181 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
182 0 : return CFD_ERROR_NOMEM;
183 : }
184 :
185 13210 : memcpy(u_star, field->u, bytes);
186 13210 : memcpy(v_star, field->v, bytes);
187 13210 : memcpy(w_star, field->w, bytes);
188 13210 : memcpy(p_new, field->p, bytes);
189 :
190 : /* Main iteration loop */
191 26506 : for (int iter = 0; iter < params->max_iter; iter++) {
192 : /* ============================================================
193 : * STEP 1: Predictor - Compute intermediate velocity u*
194 : * u* = u^n + dt * (-u·∇u + ν∇²u + f)
195 : * ============================================================ */
196 26837 : for (size_t k = k_start; k < k_end; k++) {
197 422240 : for (size_t j = 1; j < ny - 1; j++) {
198 15493280 : for (size_t i = 1; i < nx - 1; i++) {
199 15084581 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
200 :
201 15084581 : double u = field->u[idx];
202 15084581 : double v = field->v[idx];
203 15084581 : double w = field->w[idx];
204 :
205 : /* First derivatives (central) */
206 15084581 : double du_dx = (field->u[idx + 1] - field->u[idx - 1]) / (2.0 * dx);
207 15084581 : double du_dy = (field->u[idx + nx] - field->u[idx - nx]) / (2.0 * dy);
208 15084581 : double du_dz = (field->u[idx + stride_z] - field->u[idx - stride_z]) * inv_2dz;
209 :
210 15084581 : double dv_dx = (field->v[idx + 1] - field->v[idx - 1]) / (2.0 * dx);
211 15084581 : double dv_dy = (field->v[idx + nx] - field->v[idx - nx]) / (2.0 * dy);
212 15084581 : double dv_dz = (field->v[idx + stride_z] - field->v[idx - stride_z]) * inv_2dz;
213 :
214 15084581 : double dw_dx = (field->w[idx + 1] - field->w[idx - 1]) / (2.0 * dx);
215 15084581 : double dw_dy = (field->w[idx + nx] - field->w[idx - nx]) / (2.0 * dy);
216 15084581 : double dw_dz = (field->w[idx + stride_z] - field->w[idx - stride_z]) * inv_2dz;
217 :
218 : /* Convective derivatives: central, or first-order upwind */
219 15084581 : ns_conv_derivs_t cd = {du_dx, du_dy, du_dz, dv_dx, dv_dy, dv_dz,
220 : dw_dx, dw_dy, dw_dz};
221 15084581 : if (upwind) {
222 10300 : ns_upwind_conv_derivs(field->u, field->v, field->w, idx,
223 : idx - 1, idx + 1, idx - nx, idx + nx,
224 : idx - stride_z, idx + stride_z,
225 : dx, dy, inv_dz, &cd);
226 : }
227 :
228 : /* Convective terms: u·∇φ */
229 15084581 : double conv_u = u * cd.du_dx + v * cd.du_dy + w * cd.du_dz;
230 15084581 : double conv_v = u * cd.dv_dx + v * cd.dv_dy + w * cd.dv_dz;
231 15084581 : double conv_w = u * cd.dw_dx + v * cd.dw_dy + w * cd.dw_dz;
232 :
233 : /* Viscous terms */
234 15084581 : double visc_u, visc_v, visc_w;
235 15084581 : if (!turb_on) {
236 : /* Laminar: constant-viscosity Laplacian */
237 15084581 : double d2u_dx2 = (field->u[idx + 1] - 2.0 * u + field->u[idx - 1]) / (dx * dx);
238 15084581 : double d2u_dy2 = (field->u[idx + nx] - 2.0 * u + field->u[idx - nx]) / (dy * dy);
239 15084581 : double d2u_dz2 = (field->u[idx + stride_z] - 2.0 * u +
240 : field->u[idx - stride_z]) * inv_dz2;
241 :
242 15084581 : double d2v_dx2 = (field->v[idx + 1] - 2.0 * v + field->v[idx - 1]) / (dx * dx);
243 15084581 : double d2v_dy2 = (field->v[idx + nx] - 2.0 * v + field->v[idx - nx]) / (dy * dy);
244 15084581 : double d2v_dz2 = (field->v[idx + stride_z] - 2.0 * v +
245 : field->v[idx - stride_z]) * inv_dz2;
246 :
247 15084581 : double d2w_dx2 = (field->w[idx + 1] - 2.0 * w + field->w[idx - 1]) / (dx * dx);
248 15084581 : double d2w_dy2 = (field->w[idx + nx] - 2.0 * w + field->w[idx - nx]) / (dy * dy);
249 15084581 : double d2w_dz2 = (field->w[idx + stride_z] - 2.0 * w +
250 : field->w[idx - stride_z]) * inv_dz2;
251 :
252 15084581 : visc_u = nu * (d2u_dx2 + d2u_dy2 + d2u_dz2);
253 15084581 : visc_v = nu * (d2v_dx2 + d2v_dy2 + d2v_dz2);
254 15084581 : visc_w = nu * (d2w_dx2 + d2w_dy2 + d2w_dz2);
255 : } else {
256 : /* Turbulent: conservative face-averaged effective viscosity
257 : * div((nu + nu_t) grad u). The z-term keeps the laminar nu
258 : * (turbulence is 2D-only; it vanishes when nz == 1). */
259 0 : const double* nu_t = field->nu_t;
260 0 : double nu_xp = nu + 0.5 * (nu_t[idx] + nu_t[idx + 1]);
261 0 : double nu_xm = nu + 0.5 * (nu_t[idx] + nu_t[idx - 1]);
262 0 : double nu_yp = nu + 0.5 * (nu_t[idx] + nu_t[idx + nx]);
263 0 : double nu_ym = nu + 0.5 * (nu_t[idx] + nu_t[idx - nx]);
264 :
265 0 : visc_u = (nu_xp * (field->u[idx + 1] - u) -
266 0 : nu_xm * (u - field->u[idx - 1])) / (dx * dx) +
267 0 : (nu_yp * (field->u[idx + nx] - u) -
268 0 : nu_ym * (u - field->u[idx - nx])) / (dy * dy) +
269 0 : nu * (field->u[idx + stride_z] - 2.0 * u +
270 0 : field->u[idx - stride_z]) * inv_dz2;
271 0 : visc_v = (nu_xp * (field->v[idx + 1] - v) -
272 0 : nu_xm * (v - field->v[idx - 1])) / (dx * dx) +
273 0 : (nu_yp * (field->v[idx + nx] - v) -
274 0 : nu_ym * (v - field->v[idx - nx])) / (dy * dy) +
275 0 : nu * (field->v[idx + stride_z] - 2.0 * v +
276 0 : field->v[idx - stride_z]) * inv_dz2;
277 0 : visc_w = (nu_xp * (field->w[idx + 1] - w) -
278 0 : nu_xm * (w - field->w[idx - 1])) / (dx * dx) +
279 0 : (nu_yp * (field->w[idx + nx] - w) -
280 0 : nu_ym * (w - field->w[idx - nx])) / (dy * dy) +
281 0 : nu * (field->w[idx + stride_z] - 2.0 * w +
282 0 : field->w[idx - stride_z]) * inv_dz2;
283 : }
284 :
285 : /* Source terms */
286 15084581 : double source_u = 0.0, source_v = 0.0, source_w = 0.0;
287 15084581 : double x_coord = grid->x[i];
288 15084581 : double y_coord = grid->y[j];
289 15084581 : double z_coord = (nz > 1 && grid->z) ? grid->z[k] : 0.0;
290 15084581 : compute_source_terms(x_coord, y_coord, z_coord, iter, dt, params,
291 : &source_u, &source_v, &source_w);
292 :
293 : /* Boussinesq buoyancy source */
294 15084581 : energy_compute_buoyancy(field->T[idx], params,
295 : &source_u, &source_v, &source_w);
296 :
297 : /* Intermediate velocity (without pressure gradient) */
298 15084581 : u_star[idx] = u + dt * (-conv_u + visc_u + source_u);
299 15084581 : v_star[idx] = v + dt * (-conv_v + visc_v + source_v);
300 15084581 : w_star[idx] = w + dt * (-conv_w + visc_w + source_w);
301 :
302 : /* An implicit scheme clamps after its solve instead */
303 15084581 : if (!implicit_visc) {
304 14525371 : u_star[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, u_star[idx]));
305 14525371 : v_star[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, v_star[idx]));
306 14525371 : w_star[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, w_star[idx]));
307 : }
308 : }
309 : }
310 : }
311 :
312 : /* Copy boundary values from field to star arrays */
313 13296 : copy_boundary_velocities_3d(u_star, v_star, w_star,
314 13296 : field->u, field->v, field->w, nx, ny, nz);
315 :
316 : /* Implicit viscous term: replace each explicit increment by the solution
317 : * of (I - theta*nu*dt*lap) delta = increment. rhs is free until the
318 : * pressure solve below overwrites it. */
319 13296 : if (implicit_visc) {
320 698 : const double* cur[3] = {field->u, field->v, field->w};
321 698 : double* star[3] = {u_star, v_star, w_star};
322 2792 : for (int c = 0; c < 3; c++) {
323 2094 : cfd_status_t visc_status = implicit_viscous_component(
324 : viscous, cur[c], star[c], rhs, delta, nx, ny, total,
325 : k_start, k_end, stride_z);
326 2094 : if (visc_status != CFD_SUCCESS) {
327 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
328 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
329 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
330 0 : return visc_status;
331 : }
332 : }
333 : }
334 :
335 : /* ============================================================
336 : * STEP 2: Solve Poisson equation for pressure
337 : * ∇²p = (ρ/dt) * ∇·u*
338 : * ============================================================ */
339 13296 : double rho = field->rho[0];
340 13296 : if (rho < 1e-10) {
341 2 : rho = 1.0;
342 : }
343 :
344 26837 : for (size_t k = k_start; k < k_end; k++) {
345 422240 : for (size_t j = 1; j < ny - 1; j++) {
346 15493280 : for (size_t i = 1; i < nx - 1; i++) {
347 15084581 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
348 :
349 15084581 : double du_star_dx = (u_star[idx + 1] - u_star[idx - 1]) / (2.0 * dx);
350 15084581 : double dv_star_dy = (v_star[idx + nx] - v_star[idx - nx]) / (2.0 * dy);
351 15084581 : double dw_star_dz = (w_star[idx + stride_z] -
352 15084581 : w_star[idx - stride_z]) * inv_2dz;
353 :
354 15084581 : double divergence = du_star_dx + dv_star_dy + dw_star_dz;
355 15084581 : rhs[idx] = (rho / dt) * divergence;
356 : }
357 : }
358 : }
359 :
360 : /* Neumann compatibility projection, but only when the operator is actually
361 : * singular. With every face zero-gradient the constants are its nullspace,
362 : * so the RHS must have zero interior mean -- div(u*) only nearly does, and
363 : * the remainder is removed here rather than assumed away. With a face
364 : * prescribed the operator is nonsingular and shifting the RHS would change
365 : * the answer instead of making it exist. */
366 13296 : if (poisson_walls_are_singular(¶ms->pressure_bc, nz)) {
367 13296 : mg_subtract_interior_mean(rhs, nx, ny, nz);
368 : }
369 :
370 : /* Solve with the solver this projection owns. Its status is passed through
371 : * rather than flattened to MAX_ITER: divergence and a right-hand side the
372 : * operator has no solution for are different problems with different fixes,
373 : * and the caller cannot tell them apart from a single error. */
374 13296 : poisson_solver_stats_t pstats = poisson_solver_stats_default();
375 13296 : cfd_status_t poisson_status =
376 13296 : poisson_solver_solve(pressure, p_new, p_temp, rhs, &pstats);
377 :
378 13296 : if (poisson_status != CFD_SUCCESS) {
379 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
380 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
381 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
382 0 : return poisson_status;
383 : }
384 :
385 : /* ============================================================
386 : * STEP 3: Corrector - Project velocity to be divergence-free
387 : * u^(n+1) = u* - (dt/ρ) * ∇p
388 : * ============================================================ */
389 13296 : double dt_over_rho = dt / rho;
390 :
391 26837 : for (size_t k = k_start; k < k_end; k++) {
392 422240 : for (size_t j = 1; j < ny - 1; j++) {
393 15493280 : for (size_t i = 1; i < nx - 1; i++) {
394 15084581 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
395 :
396 15084581 : double dp_dx = (p_new[idx + 1] - p_new[idx - 1]) / (2.0 * dx);
397 15084581 : double dp_dy = (p_new[idx + nx] - p_new[idx - nx]) / (2.0 * dy);
398 15084581 : double dp_dz = (p_new[idx + stride_z] - p_new[idx - stride_z]) * inv_2dz;
399 :
400 15084581 : field->u[idx] = u_star[idx] - dt_over_rho * dp_dx;
401 15084581 : field->v[idx] = v_star[idx] - dt_over_rho * dp_dy;
402 15084581 : field->w[idx] = w_star[idx] - dt_over_rho * dp_dz;
403 :
404 15084581 : field->u[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, field->u[idx]));
405 15084581 : field->v[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, field->v[idx]));
406 15084581 : field->w[idx] = fmax(-MAX_VELOCITY, fmin(MAX_VELOCITY, field->w[idx]));
407 : }
408 : }
409 : }
410 :
411 : /* Update pressure */
412 13296 : memcpy(field->p, p_new, bytes);
413 :
414 : /* Energy equation: advance temperature after velocity correction */
415 : {
416 13296 : cfd_status_t energy_status = energy_step_explicit_with_workspace(
417 : field, grid, params, dt, iter * dt, T_energy_ws, total);
418 13296 : if (energy_status != CFD_SUCCESS) {
419 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
420 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
421 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
422 0 : return energy_status;
423 : }
424 : }
425 :
426 : /* Apply configured thermal BCs to temperature field */
427 13296 : cfd_status_t bc_status = energy_apply_thermal_bcs(field, params);
428 13296 : if (bc_status != CFD_SUCCESS) {
429 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
430 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
431 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
432 0 : return bc_status;
433 : }
434 :
435 : /* Restore caller-set boundary conditions */
436 13296 : copy_boundary_velocities_3d(field->u, field->v, field->w,
437 : u_star, v_star, w_star, nx, ny, nz);
438 :
439 : /* Turbulence transport: advance k-eps/SA with the corrected velocity,
440 : * then apply turbulence BCs (including wall functions) */
441 : {
442 13296 : cfd_status_t turb_status = turbulence_step_explicit_with_workspace(
443 : field, grid, params, dt, iter * dt, turb_ws,
444 : turb_on ? TURB_WORKSPACE_SIZE(total) : 0);
445 13296 : if (turb_status == CFD_SUCCESS) {
446 13296 : turb_status = turbulence_apply_bcs(field, grid, params);
447 : }
448 13296 : if (turb_status != CFD_SUCCESS) {
449 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
450 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
451 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
452 0 : return turb_status;
453 : }
454 : }
455 :
456 : /* NaN/Inf check */
457 16803325 : for (size_t n = 0; n < total; n++) {
458 16790029 : if (!isfinite(field->u[n]) || !isfinite(field->v[n]) ||
459 16790029 : !isfinite(field->w[n]) || !isfinite(field->p[n])) {
460 0 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
461 0 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
462 0 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
463 0 : return CFD_ERROR_DIVERGED;
464 : }
465 : }
466 : }
467 :
468 13210 : cfd_free(u_star); cfd_free(v_star); cfd_free(w_star);
469 13210 : cfd_free(p_new); cfd_free(p_temp); cfd_free(rhs);
470 13210 : cfd_free(T_energy_ws); cfd_free(turb_ws); cfd_free(delta);
471 :
472 13210 : return CFD_SUCCESS;
473 : }
|