include/vecmat/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_SPH_CUBIC or VM_SPH_WENDLAND.
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_SPH_CUBIC or VM_SPH_WENDLAND.
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_hydro_q 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_sph_pi.

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_sph_pressure_grad and vm_sph_artvisc. 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_sph_ncells(g) + 1.
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_sph_bin; length vm_sph_ncells(g) + 1.
cell_particles Particles per cell from vm_sph_bin.
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_sph_bin; length vm_sph_ncells(g) + 1.
cell_particles Particles per cell from vm_sph_bin.
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_sph_neighbor_count. neighbors has length offsets[n]. Self is excluded. Order is cell-stencil order, particles within a cell in index order.