LCOV - code coverage report
Current view: top level - solvers/linear/cpu - linear_solver_multigrid.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 70 70
Test Date: 2026-10-05 04:47:18 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /**
       2              :  * @file linear_solver_multigrid.c
       3              :  * @brief Geometric multigrid Poisson solver - scalar CPU implementation
       4              :  *
       5              :  * Reference backend. Supplies the scalar smoother, residual and boundary
       6              :  * primitives, plus the grid-transfer operators of cpu/multigrid_transfer.c, to
       7              :  * the shared multigrid algorithm template
       8              :  * (multigrid_template/linear_solver_multigrid_template.h), which documents the
       9              :  * algorithm and holds its only implementation.
      10              :  */
      11              : 
      12              : #include "../linear_solver_internal.h"
      13              : #include "../multigrid_internal.h"
      14              : 
      15              : #include "cfd/boundary/boundary_conditions.h"
      16              : #include "cfd/core/indexing.h"
      17              : 
      18              : /* ============================================================================
      19              :  * BOUNDARY CONDITIONS
      20              :  * ============================================================================ */
      21              : 
      22              : /** Zero-gradient BCs on one nx x ny plane */
      23       169449 : static void mg_bc_neumann_plane_scalar(double* plane, size_t nx, size_t ny) {
      24       169449 :     bc_apply_scalar_cpu(plane, nx, ny, BC_TYPE_NEUMANN);
      25              : }
      26              : 
      27              : /* ============================================================================
      28              :  * SMOOTHER SWEEPS (no BCs; the template applies them after each sweep)
      29              :  * ============================================================================ */
      30              : 
      31              : /**
      32              :  * One Red-Black Gauss-Seidel pass (omega = 1). Same stencil algebra as
      33              :  * linear_solver_redblack.c.
      34              :  */
      35       100795 : static void mg_rbgs_sweep_scalar(const mg_level_t* L, double* x,
      36              :                                  const double* rhs) {
      37       100795 :     size_t nx = L->nx;
      38       100795 :     size_t ny = L->ny;
      39       100795 :     double dx2 = L->dx2;
      40       100795 :     double dy2 = L->dy2;
      41       100795 :     double inv_dz2 = L->inv_dz2;
      42       100795 :     double inv_factor = L->inv_factor;
      43       100795 :     size_t stride_z = L->stride_z;
      44              : 
      45              :     /* Red pass: (i+j+k) % 2 == 1, then black pass: (i+j+k) % 2 == 0 */
      46       302385 :     for (int color = 0; color < 2; color++) {
      47       439300 :         for (size_t k = L->k_start; k < L->k_end; k++) {
      48      2034992 :             for (size_t j = 1; j < ny - 1; j++) {
      49      1797282 :                 size_t i_start =
      50      1797282 :                     ((j + k) % 2 == (size_t)color) ? 1 : 2;
      51     73032281 :                 for (size_t i = i_start; i < nx - 1; i += 2) {
      52     71234999 :                     size_t idx = k * stride_z + IDX_2D(i, j, nx);
      53              : 
      54     71234999 :                     x[idx] = -(rhs[idx]
      55     71234999 :                         - (x[idx + 1] + x[idx - 1]) / dx2
      56     71234999 :                         - (x[idx + nx] + x[idx - nx]) / dy2
      57     71234999 :                         - (x[idx + stride_z] + x[idx - stride_z]) * inv_dz2
      58     71234999 :                         ) * inv_factor;
      59              :                 }
      60              :             }
      61              :         }
      62              :     }
      63       100795 : }
      64              : 
      65              : /**
      66              :  * One weighted Jacobi pass (omega = MG_JACOBI_OMEGA) into x_temp, then an
      67              :  * interior-only copy back to x (boundary values preserved).
      68              :  */
      69       232974 : static void mg_jacobi_sweep_scalar(const mg_level_t* L, double* x,
      70              :                                    double* x_temp, const double* rhs) {
      71       232974 :     size_t nx = L->nx;
      72       232974 :     size_t ny = L->ny;
      73       232974 :     double dx2 = L->dx2;
      74       232974 :     double dy2 = L->dy2;
      75       232974 :     double inv_dz2 = L->inv_dz2;
      76       232974 :     double inv_factor = L->inv_factor;
      77       232974 :     size_t stride_z = L->stride_z;
      78              : 
      79       499112 :     for (size_t k = L->k_start; k < L->k_end; k++) {
      80      2003340 :         for (size_t j = 1; j < ny - 1; j++) {
      81    176256908 :             for (size_t i = 1; i < nx - 1; i++) {
      82    174519706 :                 size_t idx = k * stride_z + IDX_2D(i, j, nx);
      83              : 
      84    174519706 :                 double x_jacobi = -(rhs[idx]
      85    174519706 :                     - (x[idx + 1] + x[idx - 1]) / dx2
      86    174519706 :                     - (x[idx + nx] + x[idx - nx]) / dy2
      87    174519706 :                     - (x[idx + stride_z] + x[idx - stride_z]) * inv_dz2
      88              :                     ) * inv_factor;
      89              : 
      90    174519706 :                 x_temp[idx] = x[idx] + MG_JACOBI_OMEGA * (x_jacobi - x[idx]);
      91              :             }
      92              :         }
      93              :     }
      94              : 
      95       499112 :     for (size_t k = L->k_start; k < L->k_end; k++) {
      96      2003340 :         for (size_t j = 1; j < ny - 1; j++) {
      97    176256908 :             for (size_t i = 1; i < nx - 1; i++) {
      98    174519706 :                 size_t idx = k * stride_z + IDX_2D(i, j, nx);
      99    174519706 :                 x[idx] = x_temp[idx];
     100              :             }
     101              :         }
     102              :     }
     103       232974 : }
     104              : 
     105              : /* ============================================================================
     106              :  * RESIDUAL
     107              :  * ============================================================================ */
     108              : 
     109              : /**
     110              :  * r = rhs - Laplacian(x) at interior points. The coarse problem is
     111              :  * Laplacian(e) = r, so that Laplacian(x + P e) ~ rhs. Boundary entries of r
     112              :  * are never written (they stay zero from allocation; restriction never
     113              :  * reads them).
     114              :  */
     115        12810 : static void mg_residual_scalar(const mg_level_t* L, const double* x,
     116              :                                const double* rhs, double* r) {
     117        12810 :     size_t nx = L->nx;
     118        12810 :     size_t ny = L->ny;
     119        12810 :     double dx2 = L->dx2;
     120        12810 :     double dy2 = L->dy2;
     121        12810 :     double inv_dz2 = L->inv_dz2;
     122        12810 :     size_t stride_z = L->stride_z;
     123              : 
     124        31326 :     for (size_t k = L->k_start; k < L->k_end; k++) {
     125       507008 :         for (size_t j = 1; j < ny - 1; j++) {
     126     58632056 :             for (size_t i = 1; i < nx - 1; i++) {
     127     58143564 :                 size_t idx = k * stride_z + IDX_2D(i, j, nx);
     128              : 
     129     58143564 :                 double laplacian =
     130     58143564 :                     (x[idx + 1] - 2.0 * x[idx] + x[idx - 1]) / dx2
     131     58143564 :                   + (x[idx + nx] - 2.0 * x[idx] + x[idx - nx]) / dy2
     132     58143564 :                   + (x[idx + stride_z] + x[idx - stride_z]
     133     58143564 :                      - 2.0 * x[idx]) * inv_dz2;
     134              : 
     135     58143564 :                 r[idx] = rhs[idx] - laplacian;
     136              :             }
     137              :         }
     138              :     }
     139        12810 : }
     140              : 
     141              : /** Zero the interior of x at one level (boundary values untouched) */
     142           29 : static void mg_zero_interior_scalar(const mg_level_t* L, double* x) {
     143          172 :     for (size_t k = L->k_start; k < L->k_end; k++) {
     144         1904 :         for (size_t j = 1; j < L->ny - 1; j++) {
     145        35472 :             for (size_t i = 1; i < L->nx - 1; i++) {
     146        33711 :                 x[k * L->stride_z + IDX_2D(i, j, L->nx)] = 0.0;
     147              :             }
     148              :         }
     149              :     }
     150           29 : }
     151              : 
     152              : /* ============================================================================
     153              :  * ALGORITHM (shared multigrid template)
     154              :  * ============================================================================ */
     155              : 
     156              : #define MGT_SUFFIX                  scalar
     157              : #define MGT_SOLVER_NAME             POISSON_SOLVER_TYPE_MG_SCALAR
     158              : #define MGT_DESCRIPTION             "Geometric multigrid V/W/F-cycle (scalar CPU)"
     159              : #define MGT_BACKEND                 POISSON_BACKEND_SCALAR
     160              : 
     161              : #define MGT_BC_NEUMANN_PLANE        mg_bc_neumann_plane_scalar
     162              : #define MGT_RBGS_SWEEP              mg_rbgs_sweep_scalar
     163              : #define MGT_JACOBI_SWEEP            mg_jacobi_sweep_scalar
     164              : #define MGT_RESIDUAL                mg_residual_scalar
     165              : #define MGT_RESTRICT_2D             mg_restrict_2d
     166              : #define MGT_RESTRICT_3D             mg_restrict_3d
     167              : #define MGT_PROLONGATE_ADD_2D       mg_prolongate_add_2d
     168              : #define MGT_PROLONGATE_ADD_3D       mg_prolongate_add_3d
     169              : #define MGT_SUBTRACT_INTERIOR_MEAN  mg_subtract_interior_mean
     170              : #define MGT_ZERO_INTERIOR           mg_zero_interior_scalar
     171              : 
     172              : #include "../multigrid_template/linear_solver_multigrid_template.h"
        

Generated by: LCOV version 2.0-1