Line data Source code
1 : /**
2 : * @file linear_solver_redblack.c
3 : * @brief Red-Black SOR solver - scalar CPU implementation
4 : *
5 : * Red-Black SOR characteristics:
6 : * - Two-color sweep (red then black) allows parallelization
7 : * - In-place updates (no temporary buffer needed)
8 : * - SOR acceleration (omega > 1) for faster convergence
9 : * - Best balance of convergence speed and parallelism
10 : */
11 :
12 : #include "../linear_solver_internal.h"
13 :
14 : #include "cfd/boundary/boundary_conditions.h"
15 : #include "cfd/core/indexing.h"
16 : #include "cfd/core/memory.h"
17 :
18 : #include <math.h>
19 :
20 : /* ============================================================================
21 : * RED-BLACK CONTEXT
22 : * ============================================================================ */
23 :
24 : typedef struct {
25 : double dx2; /* dx^2 */
26 : double dy2; /* dy^2 */
27 : double inv_dx2; /* 1/dx^2 */
28 : double inv_dy2; /* 1/dy^2 */
29 : double inv_dz2;
30 : double factor; /* 2 * (1/dx^2 + 1/dy^2 + inv_dz2) */
31 : double inv_factor; /* 1 / factor */
32 : double omega; /* SOR relaxation parameter */
33 : size_t stride_z;
34 : size_t k_start;
35 : size_t k_end;
36 : int initialized;
37 : } redblack_context_t;
38 :
39 : /* ============================================================================
40 : * RED-BLACK SCALAR IMPLEMENTATION
41 : * ============================================================================ */
42 :
43 690 : static cfd_status_t redblack_scalar_init(
44 : poisson_solver_t* solver,
45 : size_t nx, size_t ny, size_t nz,
46 : double dx, double dy, double dz,
47 : const poisson_solver_params_t* params)
48 : {
49 690 : redblack_context_t* ctx = (redblack_context_t*)cfd_calloc(1, sizeof(redblack_context_t));
50 690 : if (!ctx) {
51 : return CFD_ERROR_NOMEM;
52 : }
53 :
54 690 : ctx->dx2 = dx * dx;
55 690 : ctx->dy2 = dy * dy;
56 690 : ctx->inv_dx2 = 1.0 / ctx->dx2;
57 690 : ctx->inv_dy2 = 1.0 / ctx->dy2;
58 690 : ctx->inv_dz2 = poisson_solver_compute_inv_dz2(dz);
59 690 : poisson_solver_compute_3d_bounds(nz, nx, ny,
60 : &ctx->stride_z, &ctx->k_start, &ctx->k_end);
61 :
62 690 : ctx->factor = 2.0 * (1.0 / ctx->dx2 + 1.0 / ctx->dy2 + ctx->inv_dz2);
63 690 : ctx->inv_factor = 1.0 / ctx->factor;
64 690 : ctx->omega = poisson_solver_resolve_omega(solver, params ? params->sor.omega : 0.0);
65 690 : ctx->initialized = 1;
66 :
67 690 : solver->context = ctx;
68 690 : return CFD_SUCCESS;
69 : }
70 :
71 695 : static void redblack_scalar_destroy(poisson_solver_t* solver) {
72 695 : if (solver && solver->context) {
73 690 : cfd_free(solver->context);
74 690 : solver->context = NULL;
75 : }
76 695 : }
77 :
78 : /**
79 : * Red-Black SOR iteration (scalar)
80 : *
81 : * Red cells: (i+j+k) % 2 == 1
82 : * Black cells: (i+j+k) % 2 == 0
83 : */
84 122183 : static cfd_status_t redblack_scalar_iterate(
85 : poisson_solver_t* solver,
86 : double* x,
87 : double* x_temp,
88 : const double* rhs,
89 : double* residual)
90 : {
91 122183 : (void)x_temp; /* Not needed for in-place SOR */
92 :
93 122183 : redblack_context_t* ctx = (redblack_context_t*)solver->context;
94 122183 : size_t nx = solver->nx;
95 122183 : size_t ny = solver->ny;
96 122183 : double dx2 = ctx->dx2;
97 122183 : double dy2 = ctx->dy2;
98 122183 : double inv_factor = ctx->inv_factor;
99 122183 : double omega = ctx->omega;
100 :
101 122183 : size_t stride_z = ctx->stride_z;
102 122183 : double inv_dz2 = ctx->inv_dz2;
103 122183 : int walls = poisson_solver_uses_default_walls(solver);
104 :
105 : /* Red sweep: (i+j+k) % 2 == 1 */
106 474688 : for (size_t k = ctx->k_start; k < ctx->k_end; k++) {
107 10310960 : for (size_t j = 1; j < ny - 1; j++) {
108 9958455 : double w_row, w_edge;
109 9958455 : poisson_solver_row_omegas(walls, omega, ctx->factor, nx, ny, solver->nz, j, k,
110 : ctx->inv_dx2, ctx->inv_dy2, inv_dz2, &w_row, &w_edge);
111 9958455 : size_t i_start = ((j + k) % 2 == 0) ? 1 : 2;
112 177208643 : for (size_t i = i_start; i < nx - 1; i += 2) {
113 167250188 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
114 :
115 167250188 : double p_new = -(rhs[idx]
116 167250188 : - (x[idx + 1] + x[idx - 1]) / dx2
117 167250188 : - (x[idx + nx] + x[idx - nx]) / dy2
118 167250188 : - (x[idx + stride_z] + x[idx - stride_z]) * inv_dz2
119 : ) * inv_factor;
120 :
121 : /* SOR update, with the wall factor at the row's first and last point */
122 167250188 : double w = (i == 1 || i == nx - 2) ? w_edge : w_row;
123 167250188 : x[idx] = x[idx] + w * (p_new - x[idx]);
124 : }
125 : }
126 : }
127 :
128 : /* Black sweep: (i+j+k) % 2 == 0 */
129 474688 : for (size_t k = ctx->k_start; k < ctx->k_end; k++) {
130 10310960 : for (size_t j = 1; j < ny - 1; j++) {
131 9958455 : double w_row, w_edge;
132 9958455 : poisson_solver_row_omegas(walls, omega, ctx->factor, nx, ny, solver->nz, j, k,
133 : ctx->inv_dx2, ctx->inv_dy2, inv_dz2, &w_row, &w_edge);
134 9958455 : size_t i_start = ((j + k) % 2 == 0) ? 2 : 1;
135 177267236 : for (size_t i = i_start; i < nx - 1; i += 2) {
136 167308781 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
137 :
138 167308781 : double p_new = -(rhs[idx]
139 167308781 : - (x[idx + 1] + x[idx - 1]) / dx2
140 167308781 : - (x[idx + nx] + x[idx - nx]) / dy2
141 167308781 : - (x[idx + stride_z] + x[idx - stride_z]) * inv_dz2
142 : ) * inv_factor;
143 :
144 : /* SOR update, with the wall factor at the row's first and last point */
145 167308781 : double w = (i == 1 || i == nx - 2) ? w_edge : w_row;
146 167308781 : x[idx] = x[idx] + w * (p_new - x[idx]);
147 : }
148 : }
149 : }
150 :
151 : /* Apply boundary conditions */
152 122183 : poisson_solver_apply_bc(solver, x);
153 :
154 : /* Compute residual if requested */
155 122183 : if (residual) {
156 122183 : *residual = poisson_solver_compute_residual(solver, x, rhs);
157 : }
158 :
159 122183 : return CFD_SUCCESS;
160 : }
161 :
162 : /* ============================================================================
163 : * FACTORY FUNCTION
164 : * ============================================================================ */
165 :
166 695 : poisson_solver_t* create_redblack_scalar_solver(void) {
167 695 : poisson_solver_t* solver = (poisson_solver_t*)cfd_calloc(1, sizeof(poisson_solver_t));
168 695 : if (!solver) {
169 : return NULL;
170 : }
171 :
172 695 : solver->name = POISSON_SOLVER_TYPE_REDBLACK_SCALAR;
173 695 : solver->description = "Red-Black SOR iteration (scalar CPU)";
174 695 : solver->method = POISSON_METHOD_REDBLACK_SOR;
175 695 : solver->backend = POISSON_BACKEND_SCALAR;
176 695 : solver->params = poisson_solver_params_default();
177 :
178 695 : solver->init = redblack_scalar_init;
179 695 : solver->destroy = redblack_scalar_destroy;
180 695 : solver->solve = NULL; /* Use common solve loop */
181 695 : solver->iterate = redblack_scalar_iterate;
182 695 : solver->apply_bc = NULL;
183 :
184 695 : return solver;
185 : }
|