Line data Source code
1 : /**
2 : * @file linear_solver_cg.c
3 : * @brief Conjugate Gradient solver - scalar CPU implementation
4 : *
5 : * Conjugate Gradient method for solving Ax = b where A is symmetric positive
6 : * definite (SPD). For the Poisson equation with Neumann BCs, the Laplacian
7 : * operator is SPD on the space orthogonal to constants.
8 : *
9 : * CG characteristics:
10 : * - Optimal for SPD systems (Poisson with appropriate BCs)
11 : * - Converges in at most N iterations for N unknowns (in exact arithmetic)
12 : * - Typically converges in O(sqrt(cond(A))) iterations
13 : * - Requires extra storage for r, p, Ap vectors
14 : * - Each iteration: 1 matrix-vector product, 2 dot products, 2 axpy operations
15 : *
16 : * Algorithm (standard CG):
17 : * r_0 = b - A*x_0
18 : * p_0 = r_0
19 : * for k = 0, 1, 2, ...
20 : * alpha_k = (r_k, r_k) / (p_k, A*p_k)
21 : * x_{k+1} = x_k + alpha_k * p_k
22 : * r_{k+1} = r_k - alpha_k * A*p_k
23 : * beta_k = (r_{k+1}, r_{k+1}) / (r_k, r_k)
24 : * p_{k+1} = r_{k+1} + beta_k * p_k
25 : */
26 :
27 : #include "../linear_solver_internal.h"
28 :
29 : #include "cfd/core/indexing.h"
30 : #include "cfd/core/logging.h"
31 : #include "cfd/core/memory.h"
32 :
33 : #include <math.h>
34 : #include <string.h>
35 :
36 : /* ============================================================================
37 : * CG CONTEXT
38 : * ============================================================================ */
39 :
40 : typedef struct {
41 : double dx2; /* dx^2 */
42 : double dy2; /* dy^2 */
43 : double inv_dz2; /* 1/dz^2 (0 for 2D) */
44 : double diag_inv; /* Jacobi preconditioner: 1/(2/dx2 + 2/dy2 + 2*inv_dz2 + sigma) */
45 : double sigma; /* Helmholtz shift; 0 = pure Poisson */
46 :
47 : size_t stride_z; /* nx*ny for 3D, 0 for 2D */
48 : size_t k_start; /* first interior k index */
49 : size_t k_end; /* one-past-last interior k index */
50 :
51 : /* CG working vectors (allocated during init) */
52 : double* r; /* Residual vector */
53 : double* z; /* Preconditioned residual (NULL if no precond) */
54 : double* p; /* Search direction */
55 : double* Ap; /* A * p (Laplacian applied to p) */
56 :
57 : poisson_solver_t* mg_precond; /* Inner MG V-cycle solver (NULL unless
58 : POISSON_PRECOND_MULTIGRID) */
59 : poisson_precond_type_t precond_type; /* Which preconditioner use_precond refers to */
60 : int use_precond; /* Flag: is preconditioner enabled? */
61 : int initialized;
62 : } cg_context_t;
63 :
64 : /* ============================================================================
65 : * HELPER FUNCTIONS
66 : * ============================================================================ */
67 :
68 : /**
69 : * Compute dot product of two vectors (interior points only)
70 : */
71 1090780 : static double dot_product(const double* a, const double* b,
72 : size_t nx, size_t ny,
73 : size_t k_start, size_t k_end, size_t stride_z) {
74 1090780 : double sum = 0.0;
75 4382052 : for (size_t k = k_start; k < k_end; k++) {
76 78070634 : for (size_t j = 1; j < ny - 1; j++) {
77 3445652520 : for (size_t i = 1; i < nx - 1; i++) {
78 3369800898 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
79 3369800898 : sum += a[idx] * b[idx];
80 : }
81 : }
82 : }
83 2163040 : return sum;
84 : }
85 :
86 : /**
87 : * Compute y = y + alpha * x (interior points only)
88 : */
89 2149470 : static void axpy(double alpha, const double* x, double* y,
90 : size_t nx, size_t ny,
91 : size_t k_start, size_t k_end, size_t stride_z) {
92 4439646 : for (size_t k = k_start; k < k_end; k++) {
93 78651212 : for (size_t j = 1; j < ny - 1; j++) {
94 3430585948 : for (size_t i = 1; i < nx - 1; i++) {
95 3354190373 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
96 3354190373 : y[idx] += alpha * x[idx];
97 : }
98 : }
99 : }
100 : }
101 :
102 : /**
103 : * Apply negative Laplacian operator: Ap = -nabla^2(p)
104 : * For Poisson equation: nabla^2(x) = rhs, we solve -nabla^2(x) = -rhs
105 : * which is SPD with positive eigenvalues.
106 : */
107 1073681 : static void apply_laplacian(const double* p, double* Ap,
108 : size_t nx, size_t ny,
109 : double dx2, double dy2, double inv_dz2,
110 : size_t k_start, size_t k_end, size_t stride_z) {
111 1073681 : double dx2_inv = 1.0 / dx2;
112 1073681 : double dy2_inv = 1.0 / dy2;
113 :
114 2173520 : for (size_t k = k_start; k < k_end; k++) {
115 38700406 : for (size_t j = 1; j < ny - 1; j++) {
116 1697861534 : for (size_t i = 1; i < nx - 1; i++) {
117 1660260967 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
118 :
119 1660260967 : double laplacian =
120 1660260967 : ((p[idx + 1] - (2.0 * p[idx]) + p[idx - 1]) * dx2_inv)
121 1660260967 : + ((p[idx + (nx)] - (2.0 * p[idx]) + p[idx - (nx)]) * dy2_inv)
122 1660260967 : + ((p[idx + (stride_z)] + p[idx - (stride_z)] - (2.0 * p[idx])) * inv_dz2);
123 1660260967 : Ap[idx] = -laplacian;
124 : }
125 : }
126 : }
127 1073681 : }
128 :
129 : /**
130 : * Compute initial residual: r = b - A*x
131 : * For our formulation: r = -rhs - (-nabla^2 x) = -rhs + nabla^2 x
132 : * which simplifies to: r = -(rhs - nabla^2 x)
133 : *
134 : * To maintain consistency with the standard CG formulation where we solve
135 : * -nabla^2 x = -rhs (i.e., A = -nabla^2, b = -rhs), we compute:
136 : * r = b - Ax = -rhs - (-nabla^2 x) = -rhs + nabla^2 x
137 : */
138 15678 : static void compute_residual(const double* x, const double* rhs, double* r,
139 : size_t nx, size_t ny,
140 : double dx2, double dy2, double inv_dz2,
141 : size_t k_start, size_t k_end, size_t stride_z) {
142 15678 : double dx2_inv = 1.0 / dx2;
143 15678 : double dy2_inv = 1.0 / dy2;
144 :
145 32511 : for (size_t k = k_start; k < k_end; k++) {
146 496622 : for (size_t j = 1; j < ny - 1; j++) {
147 17433724 : for (size_t i = 1; i < nx - 1; i++) {
148 16953935 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
149 :
150 : /* Laplacian(x) = d^2x/dx^2 + d^2x/dy^2 */
151 16953935 : double laplacian =
152 16953935 : ((x[idx + 1] - (2.0 * x[idx]) + x[idx - 1]) * dx2_inv)
153 16953935 : + ((x[idx + (nx)] - (2.0 * x[idx]) + x[idx - (nx)]) * dy2_inv)
154 16953935 : + ((x[idx + (stride_z)] + x[idx - (stride_z)] - (2.0 * x[idx])) * inv_dz2);
155 :
156 : /* r = -rhs + laplacian = -(rhs - laplacian)
157 : * This is b - Ax where A = -nabla^2 and b = -rhs */
158 16953935 : r[idx] = -rhs[idx] + laplacian;
159 : }
160 : }
161 : }
162 15678 : }
163 :
164 : /**
165 : * Copy vector p = r (interior points only)
166 : */
167 : static void copy_vector(const double* src, double* dst,
168 : size_t nx, size_t ny,
169 : size_t k_start, size_t k_end, size_t stride_z) {
170 32467 : for (size_t k = k_start; k < k_end; k++) {
171 496622 : for (size_t j = 1; j < ny - 1; j++) {
172 17433724 : for (size_t i = 1; i < nx - 1; i++) {
173 16953935 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
174 16953935 : dst[idx] = src[idx];
175 : }
176 : }
177 : }
178 : }
179 :
180 : /**
181 : * Apply Jacobi preconditioner: z = M^{-1} * r
182 : * For the negative Laplacian -nabla^2, the diagonal is (2/dx^2 + 2/dy^2 + 2*inv_dz2),
183 : * so M^{-1} = 1/(2/dx^2 + 2/dy^2 + 2*inv_dz2) = diag_inv.
184 : */
185 : static void apply_jacobi_precond(const double* r, double* z,
186 : size_t nx, size_t ny,
187 : double diag_inv,
188 : size_t k_start, size_t k_end, size_t stride_z) {
189 838 : for (size_t k = k_start; k < k_end; k++) {
190 17408 : for (size_t j = 1; j < ny - 1; j++) {
191 835584 : for (size_t i = 1; i < nx - 1; i++) {
192 818595 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
193 818595 : z[idx] = diag_inv * r[idx];
194 : }
195 : }
196 : }
197 : }
198 :
199 : /**
200 : * Apply multigrid preconditioner: z = M^{-1} * r for A = -nabla^2.
201 : *
202 : * The inner solver runs one V-cycle on nabla^2 z' = r from a zero guess;
203 : * by linearity of the fixed cycle, z = -z' approximates (-nabla^2)^{-1} r.
204 : * r can be passed directly as the MG rhs: its boundary stays zero and the
205 : * MG smoothers/residual never read rhs boundary values. The cycle uses
206 : * Dirichlet mode with symmetric Jacobi smoothing (set at init), matching
207 : * the zero halo that CG's interior-only updates impose on Krylov vectors
208 : * and keeping M symmetric positive definite.
209 : */
210 1046 : static cfd_status_t apply_mg_precond(poisson_solver_t* mg, const double* r, double* z,
211 : size_t n_total, size_t nx, size_t ny,
212 : size_t k_start, size_t k_end, size_t stride_z) {
213 1046 : memset(z, 0, n_total * sizeof(double));
214 :
215 1046 : cfd_status_t status = poisson_solver_iterate(mg, z, NULL, r, NULL);
216 1046 : if (status != CFD_SUCCESS) {
217 : return status;
218 : }
219 :
220 3128 : for (size_t k = k_start; k < k_end; k++) {
221 155792 : for (size_t j = 1; j < ny - 1; j++) {
222 31660144 : for (size_t i = 1; i < nx - 1; i++) {
223 31506434 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
224 31506434 : z[idx] = -z[idx];
225 : }
226 : }
227 : }
228 : return CFD_SUCCESS;
229 : }
230 :
231 : /* ============================================================================
232 : * CG SCALAR IMPLEMENTATION
233 : * ============================================================================ */
234 :
235 458 : static cfd_status_t cg_scalar_init(
236 : poisson_solver_t* solver,
237 : size_t nx, size_t ny, size_t nz,
238 : double dx, double dy, double dz,
239 : const poisson_solver_params_t* params)
240 : {
241 458 : cg_context_t* ctx = (cg_context_t*)cfd_calloc(1, sizeof(cg_context_t));
242 458 : if (!ctx) {
243 : return CFD_ERROR_NOMEM;
244 : }
245 :
246 458 : ctx->dx2 = dx * dx;
247 458 : ctx->dy2 = dy * dy;
248 458 : ctx->inv_dz2 = poisson_solver_compute_inv_dz2(dz);
249 458 : poisson_solver_compute_3d_bounds(nz, nx, ny, &ctx->stride_z, &ctx->k_start, &ctx->k_end);
250 :
251 458 : ctx->sigma = params ? params->helmholtz_shift : 0.0;
252 :
253 : /* Jacobi preconditioner diagonal inverse. The shift is a diagonal term, so it
254 : * folds in here; adding 0.0 to a finite positive sum is exact, leaving the
255 : * unshifted value bit-identical. */
256 458 : ctx->diag_inv = 1.0 / (2.0 / ctx->dx2 + 2.0 / ctx->dy2 + 2.0 * ctx->inv_dz2
257 458 : + ctx->sigma);
258 :
259 : /* Check if preconditioner is enabled */
260 458 : ctx->precond_type = params ? params->krylov.preconditioner : POISSON_PRECOND_NONE;
261 458 : ctx->use_precond = (ctx->precond_type == POISSON_PRECOND_JACOBI ||
262 : ctx->precond_type == POISSON_PRECOND_MULTIGRID);
263 :
264 : /* Allocate working vectors */
265 458 : size_t n = nx * ny * nz;
266 458 : ctx->r = (double*)cfd_calloc(n, sizeof(double));
267 458 : ctx->p = (double*)cfd_calloc(n, sizeof(double));
268 458 : ctx->Ap = (double*)cfd_calloc(n, sizeof(double));
269 458 : ctx->z = NULL;
270 :
271 458 : if (!ctx->r || !ctx->p || !ctx->Ap) {
272 0 : cfd_free(ctx->r);
273 0 : cfd_free(ctx->p);
274 0 : cfd_free(ctx->Ap);
275 0 : cfd_free(ctx);
276 0 : return CFD_ERROR_NOMEM;
277 : }
278 :
279 : /* Allocate preconditioner vector if needed */
280 458 : if (ctx->use_precond) {
281 39 : ctx->z = (double*)cfd_calloc(n, sizeof(double));
282 39 : if (!ctx->z) {
283 0 : cfd_free(ctx->r);
284 0 : cfd_free(ctx->p);
285 0 : cfd_free(ctx->Ap);
286 0 : cfd_free(ctx);
287 0 : return CFD_ERROR_NOMEM;
288 : }
289 : }
290 :
291 458 : if (ctx->precond_type == POISSON_PRECOND_MULTIGRID) {
292 54 : cfd_status_t mg_status = poisson_solver_create_mg_precond(
293 : create_multigrid_scalar_solver, nx, ny, nz, dx, dy, dz,
294 27 : &solver->params.multigrid, &ctx->mg_precond);
295 27 : if (mg_status != CFD_SUCCESS) {
296 2 : cfd_free(ctx->r);
297 2 : cfd_free(ctx->z);
298 2 : cfd_free(ctx->p);
299 2 : cfd_free(ctx->Ap);
300 2 : cfd_free(ctx);
301 2 : return mg_status; /* CFD_ERROR_INVALID for non-2^k+1 dims */
302 : }
303 : }
304 :
305 456 : ctx->initialized = 1;
306 456 : solver->context = ctx;
307 456 : return CFD_SUCCESS;
308 : }
309 :
310 488 : static void cg_scalar_destroy(poisson_solver_t* solver) {
311 488 : if (solver && solver->context) {
312 456 : cg_context_t* ctx = (cg_context_t*)solver->context;
313 456 : poisson_solver_destroy(ctx->mg_precond);
314 456 : cfd_free(ctx->r);
315 456 : cfd_free(ctx->z);
316 456 : cfd_free(ctx->p);
317 456 : cfd_free(ctx->Ap);
318 456 : cfd_free(ctx);
319 456 : solver->context = NULL;
320 : }
321 488 : }
322 :
323 : /**
324 : * CG solve function (supports both standard CG and preconditioned CG)
325 : *
326 : * CG uses its own solve loop (not the common one) because:
327 : * 1. The iterate function doesn't fit the standard pattern
328 : * 2. CG maintains state across iterations (rho)
329 : * 3. Convergence is measured by residual norm, not separate residual computation
330 : *
331 : * Standard CG: Preconditioned CG (PCG):
332 : * r = b - Ax r = b - Ax
333 : * p = r z = M^{-1}r
334 : * p = z
335 : * rho = (r,r) rho = (r,z)
336 : *
337 : * loop: loop:
338 : * Ap = A*p Ap = A*p
339 : * alpha = rho / (p,Ap) alpha = rho / (p,Ap)
340 : * x += alpha*p x += alpha*p
341 : * r -= alpha*Ap r -= alpha*Ap
342 : * rho_new = (r,r) z = M^{-1}r
343 : * beta = rho_new/rho rho_new = (r,z)
344 : * p = r + beta*p beta = rho_new/rho
345 : * rho = rho_new p = z + beta*p
346 : * rho = rho_new
347 : */
348 15678 : static cfd_status_t cg_scalar_solve(
349 : poisson_solver_t* solver,
350 : double* x,
351 : double* x_temp,
352 : const double* rhs,
353 : poisson_solver_stats_t* stats)
354 : {
355 15678 : (void)x_temp; /* CG doesn't use the temp buffer */
356 :
357 15678 : cg_context_t* ctx = (cg_context_t*)solver->context;
358 15678 : size_t nx = solver->nx;
359 15678 : size_t ny = solver->ny;
360 15678 : double dx2 = ctx->dx2;
361 15678 : double dy2 = ctx->dy2;
362 15678 : size_t stride_z = ctx->stride_z;
363 15678 : size_t k_start = ctx->k_start;
364 15678 : size_t k_end = ctx->k_end;
365 15678 : double inv_dz2 = ctx->inv_dz2;
366 :
367 15678 : double* r = ctx->r;
368 15678 : double* z = ctx->z;
369 15678 : double* p = ctx->p;
370 15678 : double* Ap = ctx->Ap;
371 15678 : int use_precond = ctx->use_precond;
372 15678 : double diag_inv = ctx->diag_inv;
373 15678 : double sigma = ctx->sigma;
374 :
375 15678 : poisson_solver_params_t* params = &solver->params;
376 15678 : double start_time = poisson_solver_get_time_ms();
377 :
378 : /* Not poisson_solver_apply_bc: the initial residual has to see the same walls
379 : * the iteration below inverts. See poisson_solver_krylov_apply_bc. */
380 15678 : poisson_solver_krylov_apply_bc(solver, x);
381 :
382 : /* Compute initial residual: r_0 = b - A*x_0 */
383 15678 : compute_residual(x, rhs, r, nx, ny, dx2, dy2, inv_dz2, k_start, k_end, stride_z);
384 : /* A = -lap, so the shifted operator is A_h = -lap + sigma*I and
385 : * r = b - A_h x = (-rhs + lap x) - sigma*x. Applied through the existing
386 : * axpy rather than fused into the kernel: at sigma = 0 the branch is not
387 : * taken, so the unshifted path executes exactly the instructions it did
388 : * before -- bit-identity by construction, and no 0.0*inf = NaN. */
389 15678 : if (sigma != 0.0) {
390 2108 : axpy(-sigma, x, r, nx, ny, k_start, k_end, stride_z);
391 : }
392 :
393 15678 : size_t n_total = nx * ny * solver->nz;
394 15678 : double rr0 = dot_product(r, r, nx, ny, k_start, k_end, stride_z);
395 15678 : double initial_res = sqrt(rr0);
396 :
397 : /* Initialize search direction and rho */
398 15678 : double rho;
399 15678 : if (use_precond) {
400 : /* z_0 = M^{-1} r_0 */
401 44 : if (ctx->precond_type == POISSON_PRECOND_MULTIGRID) {
402 35 : cfd_status_t precond_status = apply_mg_precond(
403 : ctx->mg_precond, r, z, n_total, nx, ny, k_start, k_end, stride_z);
404 35 : if (precond_status != CFD_SUCCESS) {
405 0 : if (stats) {
406 0 : stats->status = POISSON_ERROR;
407 0 : stats->iterations = 0;
408 0 : stats->final_residual = initial_res;
409 0 : stats->elapsed_time_ms = poisson_solver_get_time_ms() - start_time;
410 : }
411 0 : return precond_status;
412 : }
413 : } else {
414 144 : apply_jacobi_precond(r, z, nx, ny, diag_inv, k_start, k_end, stride_z);
415 : }
416 : /* p_0 = z_0 */
417 144 : copy_vector(z, p, nx, ny, k_start, k_end, stride_z);
418 : /* rho_0 = (r_0, z_0) */
419 15678 : rho = dot_product(r, z, nx, ny, k_start, k_end, stride_z);
420 : } else {
421 : /* p_0 = r_0 */
422 15678 : copy_vector(r, p, nx, ny, k_start, k_end, stride_z);
423 : /* rho_0 = (r_0, r_0) */
424 : rho = rr0;
425 : }
426 :
427 15678 : if (stats) {
428 15678 : stats->initial_residual = initial_res;
429 : }
430 :
431 : /* Check if already converged */
432 : /* Never below the round-off floor of the residual at the initial guess:
433 : * a warm start near steady state begins there (poisson_solver_residual_floor) */
434 15678 : double floor_res = poisson_solver_residual_floor(solver, x, POISSON_NORM_L2);
435 15678 : double tolerance = poisson_solver_stop_target(params, initial_res, floor_res);
436 :
437 30024 : if (initial_res < poisson_solver_done_target(params, floor_res)) {
438 1316 : if (stats) {
439 1316 : stats->status = POISSON_CONVERGED;
440 1316 : stats->iterations = 0;
441 1316 : stats->final_residual = initial_res;
442 1316 : stats->elapsed_time_ms = poisson_solver_get_time_ms() - start_time;
443 : }
444 1316 : return CFD_SUCCESS;
445 : }
446 :
447 1073682 : int converged = 0;
448 : int iter;
449 : double res_norm = initial_res;
450 :
451 1073682 : for (iter = 0; iter < params->max_iterations; iter++) {
452 : /* Compute Ap = A * p. The halo carries the homogeneous boundary condition,
453 : * which is what makes A the operator the walls describe; p is rebuilt from
454 : * interior-only updates, so this has to run every iteration. */
455 1073681 : poisson_solver_krylov_apply_bc_homogeneous(solver, p);
456 1073681 : apply_laplacian(p, Ap, nx, ny, dx2, dy2, inv_dz2, k_start, k_end, stride_z);
457 1073681 : if (sigma != 0.0) {
458 1073681 : axpy(sigma, p, Ap, nx, ny, k_start, k_end, stride_z);
459 : }
460 :
461 : /* alpha = rho / (p, Ap) */
462 1073681 : double p_dot_Ap = dot_product(p, Ap, nx, ny, k_start, k_end, stride_z);
463 :
464 : /* Check for breakdown (p_dot_Ap should be positive for SPD) */
465 1073681 : CG_CHECK_BREAKDOWN(p_dot_Ap, stats, iter, res_norm, start_time);
466 :
467 1073681 : double alpha = rho / p_dot_Ap;
468 :
469 : /* x_{k+1} = x_k + alpha * p */
470 1073681 : axpy(alpha, p, x, nx, ny, k_start, k_end, stride_z);
471 :
472 : /* r_{k+1} = r_k - alpha * Ap */
473 1073681 : axpy(-alpha, Ap, r, nx, ny, k_start, k_end, stride_z);
474 :
475 : /* Compute rho_new and update search direction */
476 1073681 : double rho_new;
477 1073681 : if (use_precond) {
478 : /* z_{k+1} = M^{-1} r_{k+1} */
479 1421 : if (ctx->precond_type == POISSON_PRECOND_MULTIGRID) {
480 1011 : cfd_status_t precond_status = apply_mg_precond(
481 : ctx->mg_precond, r, z, n_total, nx, ny, k_start, k_end, stride_z);
482 1011 : if (precond_status != CFD_SUCCESS) {
483 0 : if (stats) {
484 0 : stats->status = POISSON_ERROR;
485 0 : stats->iterations = iter + 1;
486 0 : stats->final_residual = res_norm;
487 0 : stats->elapsed_time_ms = poisson_solver_get_time_ms() - start_time;
488 : }
489 0 : return precond_status;
490 : }
491 : } else {
492 1421 : apply_jacobi_precond(r, z, nx, ny, diag_inv, k_start, k_end, stride_z);
493 : }
494 : /* rho_new = (r_{k+1}, z_{k+1}) */
495 5243 : rho_new = dot_product(r, z, nx, ny, k_start, k_end, stride_z);
496 1421 : res_norm = sqrt(dot_product(r, r, nx, ny, k_start, k_end, stride_z));
497 : } else {
498 : /* rho_new = (r_{k+1}, r_{k+1}), which is also the squared residual norm */
499 1072260 : rho_new = dot_product(r, r, nx, ny, k_start, k_end, stride_z);
500 1072260 : res_norm = sqrt(rho_new);
501 : }
502 :
503 : /* Check convergence at intervals */
504 1073681 : if (iter % params->check_interval == 0) {
505 1073681 : if (params->verbose) {
506 0 : CFD_LOG_DEBUG("poisson", "CG Iter %d: residual = %.6e", iter, res_norm);
507 : }
508 :
509 1073681 : if (res_norm < tolerance || res_norm < params->absolute_tolerance) {
510 : converged = 1;
511 : break;
512 : }
513 : }
514 :
515 : /* Check for breakdown in rho before computing beta */
516 1059320 : CG_CHECK_BREAKDOWN(rho, stats, iter, res_norm, start_time);
517 :
518 : /* beta = rho_new / rho */
519 1059320 : double beta = rho_new / rho;
520 :
521 : /* p_{k+1} = (z or r)_{k+1} + beta * p_k */
522 1059320 : if (use_precond) {
523 3678 : for (size_t k = k_start; k < k_end; k++) {
524 164880 : for (size_t j = 1; j < ny - 1; j++) {
525 31316208 : for (size_t i = 1; i < nx - 1; i++) {
526 31153629 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
527 31153629 : p[idx] = z[idx] + beta * p[idx];
528 : }
529 : }
530 : }
531 : } else {
532 2139994 : for (size_t k = k_start; k < k_end; k++) {
533 38066357 : for (size_t j = 1; j < ny - 1; j++) {
534 1649813384 : for (size_t i = 1; i < nx - 1; i++) {
535 1612829078 : size_t idx = (k * stride_z) + (IDX_2D(i, j, nx));
536 1612829078 : p[idx] = r[idx] + beta * p[idx];
537 : }
538 : }
539 : }
540 : }
541 :
542 1059320 : rho = rho_new;
543 : }
544 :
545 : /* Final convergence check (in case we converged between check intervals) */
546 14362 : if (!converged && (res_norm < tolerance || res_norm < params->absolute_tolerance)) {
547 14361 : converged = 1;
548 : }
549 :
550 : /* Apply final boundary conditions */
551 14362 : poisson_solver_apply_bc(solver, x);
552 :
553 14362 : double end_time = poisson_solver_get_time_ms();
554 :
555 14362 : if (stats) {
556 : /* When loop breaks early (converged), iter is the last iteration index, so +1.
557 : * When loop completes naturally, iter == max_iterations, report as-is. */
558 14362 : stats->iterations = (iter < params->max_iterations) ? (iter + 1) : iter;
559 14362 : stats->final_residual = res_norm;
560 14362 : stats->elapsed_time_ms = end_time - start_time;
561 14362 : stats->status = converged ? POISSON_CONVERGED : POISSON_MAX_ITER;
562 : }
563 :
564 14362 : return converged ? CFD_SUCCESS : CFD_ERROR_MAX_ITER;
565 : }
566 :
567 : /**
568 : * Single iteration is not well-defined for CG as it maintains internal state.
569 : * We provide a minimal implementation that returns the current residual.
570 : */
571 0 : static cfd_status_t cg_scalar_iterate(
572 : poisson_solver_t* solver,
573 : double* x,
574 : double* x_temp,
575 : const double* rhs,
576 : double* residual)
577 : {
578 0 : (void)x_temp;
579 :
580 : /* CG doesn't support single iteration mode well.
581 : * Return the current residual from the Laplacian. */
582 0 : if (residual) {
583 0 : *residual = poisson_solver_compute_residual(solver, x, rhs);
584 : }
585 0 : return CFD_SUCCESS;
586 : }
587 :
588 : /* ============================================================================
589 : * FACTORY FUNCTION
590 : * ============================================================================ */
591 :
592 488 : poisson_solver_t* create_cg_scalar_solver(void) {
593 488 : poisson_solver_t* solver = (poisson_solver_t*)cfd_calloc(1, sizeof(poisson_solver_t));
594 488 : if (!solver) {
595 : return NULL;
596 : }
597 :
598 488 : solver->name = POISSON_SOLVER_TYPE_CG_SCALAR;
599 488 : solver->description = "Conjugate Gradient (scalar CPU)";
600 488 : solver->method = POISSON_METHOD_CG;
601 488 : solver->backend = POISSON_BACKEND_SCALAR;
602 488 : solver->params = poisson_solver_params_default();
603 :
604 488 : solver->init = cg_scalar_init;
605 488 : solver->destroy = cg_scalar_destroy;
606 488 : solver->solve = cg_scalar_solve; /* CG uses custom solve loop */
607 488 : solver->iterate = cg_scalar_iterate;
608 488 : solver->apply_bc = NULL; /* Use default Neumann */
609 :
610 488 : return solver;
611 : }
|