|
numerics 0.1.0
|
include/spatial/sph_kernel.hpp provides num::SPHKernel<Dim>, a dimension-generic template that unifies the 2D and 3D SPH smoothing kernels that previously lived in apps/fluid_sim/kernel.hpp and apps/fluid_sim_3d/kernel3d.hpp.
The 2D and 3D SPH kernels were structurally identical – cubic-spline density, Morris Laplacian, and spiky gradient – differing only in the normalization constants sigma and the spiky coefficient. The duplication was ~80 lines copied verbatim.
After the refactor, apps/fluid_sim/kernel.hpp and apps/fluid_sim_3d/kernel3d.hpp are thin wrapper structs (<=30 lines each) that delegate to num::SPHKernel<2> and num::SPHKernel<3>.
All functions are static – no state, no instantiation needed.
Support = \(2h\), \(q = r/h\).
\[ W(r,h) = \sigma \begin{cases} 1 - \tfrac{3}{2}q^2 + \tfrac{3}{4}q^3 & q \le 1 \\ \tfrac{1}{4}(2-q)^3 & 1 < q \le 2 \\ 0 & q > 2 \end{cases} \]
\[ \sigma_{\text{2D}} = \frac{10}{7\pi h^2}, \qquad \sigma_{\text{3D}} = \frac{1}{\pi h^3} \]
dW_dr returns the radial derivative \(\partial W/\partial r \le 0\), used in the Morris et al. 1997 SPH Laplacian:
\[ \nabla^2 A_i \approx \sum_j \frac{m_j}{\rho_j} \frac{2(A_i - A_j)\,r_{ij}\,(\partial W/\partial r)}{r_{ij}^2 + \varepsilon^2} \]
(viscosity and heat diffusion both use this formula).
Maintains \(\nabla W \ne 0\) as \(r \to 0\) to prevent particle clustering.
\[ \frac{\partial W_{\text{spiky}}}{\partial r} = \begin{cases} -\dfrac{15}{16\pi h^5}(2h-r)^2 & \text{2D} \\[6pt] -\dfrac{45}{\pi (2h)^6}(2h-r)^2 & \text{3D} \end{cases} \]
Spiky_gradW returns the gradient vector:
\[ \nabla W = \frac{\partial W/\partial r}{r}\,\mathbf{r} \]
using std::array<float, Dim> for both input r_vec and output.
The only dimension-dependent parts are isolated in detail::CubicSigma<Dim> and detail::SpikyDW<Dim> in the anonymous num::detail namespace. Both are fully specialized for Dim=2 and Dim=3.
App wrappers keep the original Kernel:: and Kernel3D:: API unchanged so all backend .cpp files compile without modification:
| Kernel | App | Purpose |
|---|---|---|
SPHKernel<2>::W | 2D SPH | Density (cubic spline) |
SPHKernel<2>::dW_dr | 2D SPH | Morris viscosity + heat Laplacian |
SPHKernel<2>::Spiky_dW_dr / Spiky_gradW | 2D SPH | Pressure gradient force |
SPHKernel<3>::W | 3D SPH | Density |
SPHKernel<3>::dW_dr | 3D SPH | Morris viscosity + heat |
SPHKernel<3>::Spiky_dW_dr / Spiky_gradW | 3D SPH | Pressure gradient force |