Line data Source code
1 : /**
2 : * @file linear_solver_sor.c
3 : * @brief Sequential SOR (Successive Over-Relaxation) solver - scalar CPU implementation
4 : *
5 : * SOR characteristics:
6 : * - Inherently sequential (Gauss-Seidel + relaxation)
7 : * - In-place updates (no temporary buffer needed)
8 : * - Fastest convergence per iteration
9 : * - Cannot be parallelized (unlike Red-Black SOR)
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 : * SOR 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 : } sor_context_t;
38 :
39 : /* ============================================================================
40 : * SOR SCALAR IMPLEMENTATION
41 : * ============================================================================ */
42 :
43 34 : static cfd_status_t sor_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 34 : sor_context_t* ctx = (sor_context_t*)cfd_calloc(1, sizeof(sor_context_t));
50 34 : if (!ctx) {
51 : return CFD_ERROR_NOMEM;
52 : }
53 :
54 34 : ctx->dx2 = dx * dx;
55 34 : ctx->dy2 = dy * dy;
56 34 : ctx->inv_dx2 = 1.0 / ctx->dx2;
57 34 : ctx->inv_dy2 = 1.0 / ctx->dy2;
58 34 : ctx->inv_dz2 = poisson_solver_compute_inv_dz2(dz);
59 34 : poisson_solver_compute_3d_bounds(nz, nx, ny,
60 : &ctx->stride_z, &ctx->k_start, &ctx->k_end);
61 :
62 34 : ctx->factor = 2.0 * (1.0 / ctx->dx2 + 1.0 / ctx->dy2 + ctx->inv_dz2);
63 34 : ctx->inv_factor = 1.0 / ctx->factor;
64 34 : ctx->omega = poisson_solver_resolve_omega(solver, params ? params->sor.omega : 0.0);
65 34 : ctx->initialized = 1;
66 :
67 34 : solver->context = ctx;
68 34 : return CFD_SUCCESS;
69 : }
70 :
71 39 : static void sor_scalar_destroy(poisson_solver_t* solver) {
72 39 : if (solver && solver->context) {
73 34 : cfd_free(solver->context);
74 34 : solver->context = NULL;
75 : }
76 39 : }
77 :
78 : /**
79 : * Sequential SOR iteration
80 : *
81 : * Updates cells in row-major order, using already-updated values
82 : * from the current iteration (Gauss-Seidel behavior).
83 : */
84 12741 : static cfd_status_t sor_scalar_iterate(
85 : poisson_solver_t* solver,
86 : double* x,
87 : double* x_temp,
88 : const double* rhs,
89 : double* residual)
90 : {
91 12741 : (void)x_temp; /* Not needed for in-place SOR */
92 :
93 12741 : sor_context_t* ctx = (sor_context_t*)solver->context;
94 12741 : size_t nx = solver->nx;
95 12741 : size_t ny = solver->ny;
96 12741 : double dx2 = ctx->dx2;
97 12741 : double dy2 = ctx->dy2;
98 12741 : double inv_factor = ctx->inv_factor;
99 12741 : double omega = ctx->omega;
100 :
101 12741 : size_t stride_z = ctx->stride_z;
102 12741 : double inv_dz2 = ctx->inv_dz2;
103 12741 : int walls = poisson_solver_uses_default_walls(solver);
104 :
105 : /* Single sweep: row-major order */
106 29654 : for (size_t k = ctx->k_start; k < ctx->k_end; k++) {
107 408456 : for (size_t j = 1; j < ny - 1; j++) {
108 391543 : double w_row, w_edge;
109 391543 : 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 13418048 : for (size_t i = 1; i < nx - 1; i++) {
112 13026505 : size_t idx = k * stride_z + IDX_2D(i, j, nx);
113 :
114 : /* Compute Gauss-Seidel update
115 : * Note: x[idx-1] and x[idx-nx] are already updated this iteration
116 : */
117 13026505 : double p_new = -(rhs[idx]
118 13026505 : - (x[idx + 1] + x[idx - 1]) / dx2
119 13026505 : - (x[idx + nx] + x[idx - nx]) / dy2
120 13026505 : - (x[idx + stride_z] + x[idx - stride_z]) * inv_dz2
121 : ) * inv_factor;
122 :
123 : /* SOR relaxation, with the wall factor at the row's first and last point */
124 13026505 : double w = (i == 1 || i == nx - 2) ? w_edge : w_row;
125 13026505 : x[idx] = x[idx] + w * (p_new - x[idx]);
126 : }
127 : }
128 : }
129 :
130 : /* Apply boundary conditions */
131 12741 : poisson_solver_apply_bc(solver, x);
132 :
133 : /* Compute residual if requested */
134 12741 : if (residual) {
135 7743 : *residual = poisson_solver_compute_residual(solver, x, rhs);
136 : }
137 :
138 12741 : return CFD_SUCCESS;
139 : }
140 :
141 : /* ============================================================================
142 : * FACTORY FUNCTION
143 : * ============================================================================ */
144 :
145 39 : poisson_solver_t* create_sor_scalar_solver(void) {
146 39 : poisson_solver_t* solver = (poisson_solver_t*)cfd_calloc(1, sizeof(poisson_solver_t));
147 39 : if (!solver) {
148 : return NULL;
149 : }
150 :
151 39 : solver->name = POISSON_SOLVER_TYPE_SOR_SCALAR;
152 39 : solver->description = "SOR iteration (scalar CPU, sequential)";
153 39 : solver->method = POISSON_METHOD_SOR;
154 39 : solver->backend = POISSON_BACKEND_SCALAR;
155 39 : solver->params = poisson_solver_params_default();
156 :
157 39 : solver->init = sor_scalar_init;
158 39 : solver->destroy = sor_scalar_destroy;
159 39 : solver->solve = NULL; /* Use common solve loop */
160 39 : solver->iterate = sor_scalar_iterate;
161 39 : solver->apply_bc = NULL; /* Use default Neumann */
162 :
163 39 : return solver;
164 : }
|