sph.h file
SPH kernels and per-particle discretizations.
Cubic spline and Wendland C2, both supported on $r < 2h$ and normalized so the integral is 1. Neighbor lists are arrays of indices. A uniform linked-cell bin and a CSR neighbor build sit next to the kernels; the caller owns every buffer.
Classes
- struct vm_sph_grid
- Uniform cell grid for SPH neighbor search.
Enums
- enum vm_sph_kernel_t { VM_SPH_CUBIC = 0, VM_SPH_WENDLAND = 1 }
- Kernel selector.
Functions
-
auto vm_sph_w(vm_
float_ t r, vm_ float_ t h, int dim, int kernel) -> vm_ float_ t - Kernel value
$W(r, h)$. -
auto vm_sph_dw(vm_
float_ t r, vm_ float_ t h, int dim, int kernel) -> vm_ float_ t - Radial derivative
$dW/dr$. -
auto vm_sph_density(const vm_
float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* m, int i, const int* neighbors, int nn, vm_ float_ t h, int dim, int kernel) -> vm_ float_ t - Density sum
$\rho_i = m_i W(0, h) + \sum_j m_j W_{ij}$. -
auto vm_sph_pressure_grad(vm_
float_ t* gx, vm_ float_ t* gy, vm_ float_ t* gz, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* m, const vm_ float_ t* rho, const vm_ float_ t* p, int i, const int* neighbors, int nn, vm_ float_ t h, int dim, int kernel) -> bool - Symmetric pressure gradient.
-
auto vm_sph_continuity(const vm_
float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* vx, const vm_ float_ t* vy, const vm_ float_ t* vz, const vm_ float_ t* m, int i, const int* neighbors, int nn, vm_ float_ t h, int dim, int kernel) -> vm_ float_ t - Continuity rate
$d\rho_i/dt = \sum_j m_j (v_i - v_j)\cdot\nabla W_{ij}$. -
auto vm_sph_viscosity(vm_
float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* vx, const vm_ float_ t* vy, const vm_ float_ t* vz, const vm_ float_ t* m, const vm_ float_ t* rho, const vm_ float_ t* mu, int i, const int* neighbors, int nn, vm_ float_ t h, vm_ float_ t eta, int dim, int kernel) -> bool - Laminar viscosity acceleration (Morris form).
-
auto vm_sph_pi(vm_
float_ t r, vm_ float_ t v_dot_r, vm_ float_ t rho_i, vm_ float_ t rho_j, vm_ float_ t c_i, vm_ float_ t c_j, vm_ float_ t h, vm_ float_ t alpha, vm_ float_ t beta, vm_ float_ t eta) -> vm_ float_ t - Monaghan pair artificial viscosity
$\Pi_{ij}$. -
auto vm_sph_artvisc(vm_
float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* vx, const vm_ float_ t* vy, const vm_ float_ t* vz, const vm_ float_ t* m, const vm_ float_ t* rho, const vm_ float_ t* c, int i, const int* neighbors, int nn, vm_ float_ t h, vm_ float_ t alpha, vm_ float_ t beta, vm_ float_ t eta, int dim, int kernel) -> bool - Artificial-viscosity acceleration
$a_i = -\sum_j m_j\Pi_{ij}\nabla_i W_{ij}$. -
auto vm_sph_forces(vm_
float_ t* gx, vm_ float_ t* gy, vm_ float_ t* gz, vm_ float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* vx, const vm_ float_ t* vy, const vm_ float_ t* vz, const vm_ float_ t* m, const vm_ float_ t* rho, const vm_ float_ t* p, const vm_ float_ t* c, int i, const int* neighbors, int nn, vm_ float_ t h, vm_ float_ t alpha, vm_ float_ t beta, vm_ float_ t eta, int dim, int kernel) -> bool - Pressure gradient and artificial viscosity in one neighbor walk.
-
auto vm_sph_xsph(vm_
float_ t* dvx, vm_ float_ t* dvy, vm_ float_ t* dvz, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* vx, const vm_ float_ t* vy, const vm_ float_ t* vz, const vm_ float_ t* m, const vm_ float_ t* rho, int i, const int* neighbors, int nn, vm_ float_ t h, vm_ float_ t epsilon, int dim, int kernel) -> bool - XSPH velocity correction
$\delta v_i = \varepsilon\sum_j (m_j/\rho_j)(v_j-v_i)W_{ij}$. -
auto vm_sph_tensile(vm_
float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* m, const vm_ float_ t* rho, const vm_ float_ t* p, int i, const int* neighbors, int nn, vm_ float_ t h, vm_ float_ t delta, vm_ float_ t epsilon, int nexp, int dim, int kernel) -> bool - Tensile-instability correction (Monaghan).
-
auto vm_sph_wall(vm_
float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, vm_ float_ t px, vm_ float_ t py, vm_ float_ t pz, vm_ float_ t qx, vm_ float_ t qy, vm_ float_ t qz, vm_ float_ t nx, vm_ float_ t ny, vm_ float_ t nz, vm_ float_ t cutoff, vm_ float_ t strength) -> bool - Repulsive acceleration from a plane.
-
auto vm_sph_boundary(vm_
float_ t* ax, vm_ float_ t* ay, vm_ float_ t* az, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, const vm_ float_ t* m, int i, const int* boundary, int nb, vm_ float_ t r0, vm_ float_ t strength, int dim) -> bool - Repulsive acceleration from boundary particles.
-
auto vm_sph_ncells(const vm_
sph_ grid* g) -> int - Number of cells,
nx * ny * nz. -
auto vm_sph_grid_fit(vm_
sph_ grid* g, const vm_ float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, int n, vm_ float_ t cell, int dim) -> bool - Fit a grid that covers the particles.
- auto vm_sph_prefix(int* offsets, const int* counts, int n) -> int
- Exclusive prefix sum.
-
auto vm_sph_bin(int* cell_of,
int* cell_offsets,
int* cell_particles,
const vm_
float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, int n, const vm_ sph_ grid* g, int dim) -> bool - Bin particles into cells.
-
auto vm_sph_neighbor_count(int* nn,
const vm_
float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, int n, const vm_ sph_ grid* g, const int* cell_offsets, const int* cell_particles, vm_ float_ t support, int dim) -> int - Count neighbors inside the support, excluding self.
-
auto vm_sph_neighbor_fill(int* neighbors,
const int* offsets,
const vm_
float_ t* x, const vm_ float_ t* y, const vm_ float_ t* z, int n, const vm_ sph_ grid* g, const int* cell_offsets, const int* cell_particles, vm_ float_ t support, int dim) -> bool - Fill CSR neighbor lists.
Enum documentation
enum vm_sph_kernel_t
Kernel selector.
| Enumerators | |
|---|---|
| VM_SPH_CUBIC |
Cubic spline. |
| VM_SPH_WENDLAND |
Wendland C2. |
Function documentation
vm_ float_ t vm_sph_w(vm_ float_ t r,
vm_ float_ t h,
int dim,
int kernel)
Kernel value $W(r, h)$.
| Parameters | |
|---|---|
| r | Separation. |
| h | Smoothing length. |
| dim | Dimension, 1, 2, or 3. |
| kernel | VM_ or VM_. |
| Returns | Kernel value. Zero outside the support or on invalid input. |
vm_ float_ t vm_sph_dw(vm_ float_ t r,
vm_ float_ t h,
int dim,
int kernel)
Radial derivative $dW/dr$.
| Parameters | |
|---|---|
| r | Separation. |
| h | Smoothing length. |
| dim | Dimension, 1, 2, or 3. |
| kernel | VM_ or VM_. |
| Returns | Radial derivative. Zero at $r = 0$. |
vm_ float_ t vm_sph_density(const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* m,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
int dim,
int kernel)
Density sum $\rho_i = m_i W(0, h) + \sum_j m_j W_{ij}$.
| Parameters | |
|---|---|
| x | |
| y | Particle y coordinates. |
| z | Particle z coordinates, or NULL below 3-D. |
| m | Particle masses. |
| i | Index of the particle being summed. |
| neighbors | Neighbor indices; length nn. |
| nn | Number of neighbors. |
| h | Smoothing length. |
| dim | Dimension, 1, 2, or 3. |
| kernel | Kernel selector. |
| Returns | Density. Zero on invalid input. |
An empty neighbor list returns the self term only. z may be NULL in 1-D and 2-D.
bool vm_sph_pressure_grad(vm_ float_ t* gx,
vm_ float_ t* gy,
vm_ float_ t* gz,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* m,
const vm_ float_ t* rho,
const vm_ float_ t* p,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
int dim,
int kernel)
Symmetric pressure gradient.
| Parameters | |
|---|---|
| gx | Output x component. |
| gy | Output y component. |
| gz | Output z component; may be NULL below 3-D. |
| x | |
| y | Particle y coordinates. |
| z | Particle z coordinates, or NULL below 3-D. |
| m | Particle masses. |
| rho | Particle densities. |
| p | Particle pressures. |
| i | Index of the particle. |
| neighbors | Neighbor indices; length nn. |
| nn | Number of neighbors. |
| h | Smoothing length. |
| dim | Dimension, 1, 2, or 3. |
| kernel | Kernel selector. |
| Returns | false if a required pointer is NULL or a density is non-positive. |
$\sum_j m_j (p_i/\rho_i^2 + p_j/\rho_j^2) \nabla W_{ij}$.
vm_ float_ t vm_sph_continuity(const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* vx,
const vm_ float_ t* vy,
const vm_ float_ t* vz,
const vm_ float_ t* m,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
int dim,
int kernel)
Continuity rate $d\rho_i/dt = \sum_j m_j (v_i - v_j)\cdot\nabla W_{ij}$.
| Returns | The rate. Zero on invalid input. |
|---|
No self term. An empty neighbor list returns 0. This is the rate; the caller integrates it.
bool vm_sph_viscosity(vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* vx,
const vm_ float_ t* vy,
const vm_ float_ t* vz,
const vm_ float_ t* m,
const vm_ float_ t* rho,
const vm_ float_ t* mu,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
vm_ float_ t eta,
int dim,
int kernel)
Laminar viscosity acceleration (Morris form).
| Returns | false if a required pointer is NULL, h is non-positive, or a density is non-positive. |
|---|
$a_i = \sum_j m_j \frac{\mu_i+\mu_j}{\rho_i\rho_j}
\frac{(v_i-v_j)\cdot r_{ij}}{r_{ij}^2+(\eta h)^2}\nabla_i W_{ij}$. Add the result to $dv/dt$. eta is the dimensionless floor; 0 is allowed.
vm_ float_ t vm_sph_pi(vm_ float_ t r,
vm_ float_ t v_dot_r,
vm_ float_ t rho_i,
vm_ float_ t rho_j,
vm_ float_ t c_i,
vm_ float_ t c_j,
vm_ float_ t h,
vm_ float_ t alpha,
vm_ float_ t beta,
vm_ float_ t eta)
Monaghan pair artificial viscosity $\Pi_{ij}$.
| Parameters | |
|---|---|
| r | Separation, non-negative. |
| v_dot_r | $(v_i-v_j)\cdot(x_i-x_j)$. |
| rho_i | Density of particle i. |
| rho_j | Density of particle j. |
| c_i | Sound speed of particle i. |
| c_j | Sound speed of particle j. |
| h | Smoothing length. |
| alpha | Linear viscosity coefficient. |
| beta | Quadratic viscosity coefficient. |
| eta | Dimensionless floor on the separation; 0 is allowed. |
| Returns | The pair term. Zero in expansion or on invalid input. |
$\mu_{ij} = h(v\cdot r)/(r^2+(\eta h)^2)$. $\Pi = (-\alpha \bar c\,\mu + \beta\mu^2)/\bar\rho$ when $v\cdot r < 0$, else 0. $\bar c$ and $\bar\rho$ are arithmetic means. This is the pair term; vm_ is the grid form.
bool vm_sph_artvisc(vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* vx,
const vm_ float_ t* vy,
const vm_ float_ t* vz,
const vm_ float_ t* m,
const vm_ float_ t* rho,
const vm_ float_ t* c,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
vm_ float_ t alpha,
vm_ float_ t beta,
vm_ float_ t eta,
int dim,
int kernel)
Artificial-viscosity acceleration $a_i = -\sum_j m_j\Pi_{ij}\nabla_i W_{ij}$.
| Returns | false on invalid input or a non-positive density. |
|---|
Add the result to $dv/dt$. Uses vm_.
bool vm_sph_forces(vm_ float_ t* gx,
vm_ float_ t* gy,
vm_ float_ t* gz,
vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* vx,
const vm_ float_ t* vy,
const vm_ float_ t* vz,
const vm_ float_ t* m,
const vm_ float_ t* rho,
const vm_ float_ t* p,
const vm_ float_ t* c,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
vm_ float_ t alpha,
vm_ float_ t beta,
vm_ float_ t eta,
int dim,
int kernel)
Pressure gradient and artificial viscosity in one neighbor walk.
| Returns | false on invalid input or a non-positive density. |
|---|
Writes the same two results as vm_ and vm_. The caller still combines them (a = -g + a_art + ...).
bool vm_sph_xsph(vm_ float_ t* dvx,
vm_ float_ t* dvy,
vm_ float_ t* dvz,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* vx,
const vm_ float_ t* vy,
const vm_ float_ t* vz,
const vm_ float_ t* m,
const vm_ float_ t* rho,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
vm_ float_ t epsilon,
int dim,
int kernel)
XSPH velocity correction $\delta v_i = \varepsilon\sum_j (m_j/\rho_j)(v_j-v_i)W_{ij}$.
| Returns | false on invalid input or a non-positive neighbor density. |
|---|
Add the result to the velocity. epsilon is the filter strength.
bool vm_sph_tensile(vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* m,
const vm_ float_ t* rho,
const vm_ float_ t* p,
int i,
const int* neighbors,
int nn,
vm_ float_ t h,
vm_ float_ t delta,
vm_ float_ t epsilon,
int nexp,
int dim,
int kernel)
Tensile-instability correction (Monaghan).
| Returns | false on invalid input, a non-positive density, or W(delta) == 0. |
|---|
$f_{ij} = W(r_{ij})/W(\Delta)$, $R = \varepsilon|p|/\rho^2$ when $p < 0$, else 0. Returns $a_i = -\sum_j m_j(R_i+R_j) f_{ij}^{n}\nabla_i W_{ij}$. Add the result to $dv/dt$. delta is the reference spacing; nexp is the exponent (4 is the usual choice).
bool vm_sph_wall(vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
vm_ float_ t px,
vm_ float_ t py,
vm_ float_ t pz,
vm_ float_ t qx,
vm_ float_ t qy,
vm_ float_ t qz,
vm_ float_ t nx,
vm_ float_ t ny,
vm_ float_ t nz,
vm_ float_ t cutoff,
vm_ float_ t strength)
Repulsive acceleration from a plane.
| Returns | false if an output pointer is NULL or cutoff is non-positive. |
|---|
n is a unit normal pointing into the fluid. gap = (p - q) \cdot n. For 0 < gap < cutoff, $a = s\big((c/g)^4 - (c/g)^2\big) n$; otherwise 0. One particle, one plane; the caller loops.
bool vm_sph_boundary(vm_ float_ t* ax,
vm_ float_ t* ay,
vm_ float_ t* az,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
const vm_ float_ t* m,
int i,
const int* boundary,
int nb,
vm_ float_ t r0,
vm_ float_ t strength,
int dim)
Repulsive acceleration from boundary particles.
| Parameters | |
|---|---|
| ax | Output x acceleration. |
| ay | Output y acceleration. |
| az | Output z acceleration; may be NULL below 3-D. |
| x | |
| y | Particle y coordinates, or NULL below 2-D. |
| z | Particle z coordinates, or NULL below 3-D. |
| m | Particle masses. |
| i | Index of the particle. |
| boundary | Boundary-particle indices; length nb. |
| nb | Number of boundary indices. |
| r0 | Cutoff radius. Must be positive. |
| strength | Repulsion scale. |
| dim | Dimension, 1, 2, or 3. |
| Returns | false on invalid input. |
For each boundary index with $r < r_0$, $a \mathrel{+}= m_j s\big((r_0/r)^4-(r_0/r)^2\big) r_{ij}/r^2$. Add the result to $dv/dt$.
int vm_sph_ncells(const vm_ sph_ grid* g)
Number of cells, nx * ny * nz.
| Returns | 0 if the grid is NULL or a dimension is non-positive. |
|---|
bool vm_sph_grid_fit(vm_ sph_ grid* g,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
int n,
vm_ float_ t cell,
int dim)
Fit a grid that covers the particles.
| Parameters | |
|---|---|
| g | Output grid. |
| x | |
| y | Particle y coordinates, or NULL below 2-D. |
| z | Particle z coordinates, or NULL below 3-D. |
| n | |
| cell | Cell size. Must be positive. |
| dim | Dimension, 1, 2, or 3. |
| Returns | false on invalid input. |
Origin is the minimum coordinate. Each axis has at least one cell. Unused axes (dim < 2 for y, dim < 3 for z) are a single cell at 0.
int vm_sph_prefix(int* offsets, const int* counts, int n)
Exclusive prefix sum.
| Parameters | |
|---|---|
| offsets | Output; length n + 1. |
| counts | Input counts; length n. |
| n | |
| Returns | offsets[n], or -1 on invalid input. |
offsets[0] = 0, offsets[i+1] = offsets[i] + counts[i].
bool vm_sph_bin(int* cell_of,
int* cell_offsets,
int* cell_particles,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
int n,
const vm_ sph_ grid* g,
int dim)
Bin particles into cells.
| Parameters | |
|---|---|
| cell_of | Output cell per particle; length n. |
| cell_offsets | Output; length vm_. |
| cell_particles | Output; length n. |
| x | |
| y | Particle y coordinates, or NULL below 2-D. |
| z | Particle z coordinates, or NULL below 3-D. |
| n | |
| g | Grid. |
| dim | Dimension, 1, 2, or 3. |
| Returns | false on invalid input. |
cell_of[i] is the linear cell of particle i, or -1 if it is outside the grid. cell_offsets / cell_particles is the CSR of particles per cell: cell c holds cell_particles[cell_offsets[c] .. cell_offsets[c+1]), in increasing particle index. Particles outside the grid are omitted.
int vm_sph_neighbor_count(int* nn,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
int n,
const vm_ sph_ grid* g,
const int* cell_offsets,
const int* cell_particles,
vm_ float_ t support,
int dim)
Count neighbors inside the support, excluding self.
| Parameters | |
|---|---|
| nn | Output counts; length n. |
| x | |
| y | Particle y coordinates, or NULL below 2-D. |
| z | Particle z coordinates, or NULL below 3-D. |
| n | |
| g | Grid. |
| cell_offsets | Cell prefix from vm_; length vm_. |
| cell_particles | Particles per cell from vm_. |
| support | Kernel support, typically 2h. |
| dim | Dimension, 1, 2, or 3. |
| Returns | Total neighbor count, or -1 on invalid input. |
A neighbor is a different particle in the 3^dim stencil of cells with separation strictly less than support. Particles outside the grid have a count of 0.
bool vm_sph_neighbor_fill(int* neighbors,
const int* offsets,
const vm_ float_ t* x,
const vm_ float_ t* y,
const vm_ float_ t* z,
int n,
const vm_ sph_ grid* g,
const int* cell_offsets,
const int* cell_particles,
vm_ float_ t support,
int dim)
Fill CSR neighbor lists.
| Parameters | |
|---|---|
| neighbors | Output indices; length offsets[n]. |
| offsets | Exclusive prefix; length n + 1. |
| x | |
| y | Particle y coordinates, or NULL below 2-D. |
| z | Particle z coordinates, or NULL below 3-D. |
| n | |
| g | Grid. |
| cell_offsets | Cell prefix from vm_; length vm_. |
| cell_particles | Particles per cell from vm_. |
| support | Kernel support, typically 2h. |
| dim | Dimension, 1, 2, or 3. |
| Returns | false on invalid input. |
offsets is the exclusive prefix of the counts from vm_. neighbors has length offsets[n]. Self is excluded. Order is cell-stencil order, particles within a cell in index order.