LCOV - code coverage report
Current view: top level - solvers/navier_stokes - ns_viscous_internal.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.0 % 30 27
Test Date: 2026-10-05 04:47:18 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  * @file ns_viscous_internal.h
       3              :  * @brief The implicit viscous solve a projection solver owns.
       4              :  *
       5              :  * With an implicit ns_solver_params_t.viscous_scheme, the projection predictor
       6              :  * turns its explicit increment b = u*_explicit - u^n into
       7              :  *
       8              :  *     (I - theta * nu * dt * lap) delta = b,   u* = u^n + delta
       9              :  *
      10              :  * which is the shifted Poisson problem
      11              :  *
      12              :  *     lap(delta) - sigma * delta = -sigma * b,   sigma = 1 / (theta * nu * dt)
      13              :  *
      14              :  * with delta = 0 on every boundary node: the predictor holds boundary values at
      15              :  * their step-start values for the whole step, so the increment there is zero by
      16              :  * construction rather than by assumption. The Poisson Dirichlet walls are
      17              :  * node-based (the halo node itself takes the value), which is the velocity
      18              :  * grid's boundary node exactly.
      19              :  *
      20              :  * The mapping from params to a solver configuration lives here, shared by the
      21              :  * scalar and OpenMP projections, for the same reason ns_pressure_internal.h
      22              :  * exists: two copies of it would drift. The per-point loops stay in each
      23              :  * backend, like every other kernel.
      24              :  */
      25              : #ifndef CFD_NS_VISCOUS_INTERNAL_H
      26              : #define CFD_NS_VISCOUS_INTERNAL_H
      27              : 
      28              : #include "cfd/core/cfd_status.h"
      29              : #include "cfd/core/logging.h"
      30              : #include "cfd/solvers/navier_stokes_solver.h"
      31              : #include "cfd/solvers/poisson_solver.h"
      32              : 
      33              : #include <math.h>
      34              : 
      35              : /**
      36              :  * The theta of the viscous scheme: 0 for explicit, 1 for backward Euler, 1/2 for
      37              :  * Crank-Nicolson.
      38              :  *
      39              :  * Read on every step, not only at init, so a turbulence model switched on after
      40              :  * init is still refused: nu + nu_t varies in space, and the shifted operator
      41              :  * has one constant coefficient.
      42              :  *
      43              :  * @return CFD_SUCCESS, CFD_ERROR_INVALID for an unknown scheme, or
      44              :  *         CFD_ERROR_UNSUPPORTED for an implicit scheme with a turbulence model
      45              :  */
      46        13641 : static inline cfd_status_t ns_viscous_theta(const ns_solver_params_t* params, double* theta) {
      47        13641 :     *theta = 0.0;
      48        13641 :     if (!params) {
      49              :         return CFD_SUCCESS;
      50              :     }
      51        13641 :     switch (params->viscous_scheme) {
      52              :         case NS_VISCOUS_SCHEME_EXPLICIT:
      53              :             return CFD_SUCCESS;
      54          410 :         case NS_VISCOUS_SCHEME_BACKWARD_EULER:
      55          410 :             *theta = 1.0;
      56          410 :             break;
      57          402 :         case NS_VISCOUS_SCHEME_CRANK_NICOLSON:
      58          402 :             *theta = 0.5;
      59          402 :             break;
      60            0 :         default:
      61            0 :             cfd_set_error(CFD_ERROR_INVALID, "Unknown ns_solver_params_t.viscous_scheme");
      62            0 :             return CFD_ERROR_INVALID;
      63              :     }
      64          812 :     if (params->turb_model != TURB_MODEL_NONE) {
      65            4 :         cfd_set_error(CFD_ERROR_UNSUPPORTED,
      66              :                       "An implicit params.viscous_scheme cannot be combined with a "
      67              :                       "turbulence model: nu + nu_t varies in space and the implicit "
      68              :                       "operator has constant coefficients");
      69            4 :         return CFD_ERROR_UNSUPPORTED;
      70              :     }
      71              :     return CFD_SUCCESS;
      72              : }
      73              : 
      74              : /**
      75              :  * The Helmholtz shift sigma = 1 / (theta * nu * dt), or 0 when there is nothing
      76              :  * to solve: an explicit scheme, or no viscous term (nu <= 0), where sigma would
      77              :  * be infinite. A non-positive dt is left to the caller's own dt checks.
      78              :  */
      79          808 : static inline double ns_viscous_shift(double theta, double nu, double dt) {
      80          808 :     double denom = theta * nu * dt;
      81          808 :     return (denom > 0.0 && isfinite(1.0 / denom)) ? 1.0 / denom : 0.0;
      82              : }
      83              : 
      84              : /**
      85              :  * The Poisson configuration for the implicit viscous solve on `backend`.
      86              :  *
      87              :  * CG, because it is the one method the shift is implemented for, at the
      88              :  * ACCURATE tolerance: the shifted operator is well conditioned enough that the
      89              :  * extra digits cost a few iterations, and a looser solve would show up as a
      90              :  * floor in temporal-convergence measurements. Homogeneous Dirichlet on every
      91              :  * face that exists; the z-faces stay zero-gradient on a 2D grid, which has none.
      92              :  */
      93          808 : static inline poisson_solver_config_t ns_viscous_config(poisson_solver_backend_t backend,
      94              :                                                         double sigma, size_t nz) {
      95          808 :     poisson_solver_config_t cfg = poisson_solver_config_preset(POISSON_PRESET_ACCURATE);
      96          808 :     cfg.method = POISSON_METHOD_CG;
      97          808 :     cfg.backend = backend;
      98          808 :     cfg.params.walls = poisson_walls_default();
      99          808 :     cfg.params.walls.left = cfg.params.walls.right = POISSON_WALL_DIRICHLET;
     100          808 :     cfg.params.walls.bottom = cfg.params.walls.top = POISSON_WALL_DIRICHLET;
     101          808 :     if (nz > 1) {
     102           84 :         cfg.params.walls.front = cfg.params.walls.back = POISSON_WALL_DIRICHLET;
     103              :     }
     104          808 :     cfg.params.helmholtz_shift = sigma;
     105          808 :     return cfg;
     106              : }
     107              : 
     108              : #endif /* CFD_NS_VISCOUS_INTERNAL_H */
        

Generated by: LCOV version 2.0-1