Divergence
SPH divergence of a field. Kernel:
coreOperations/wp_divergence.py (computeSPHDivergenceTensor_Kernel);
JVP coreOperations/wp_divergenceJVP.py.
Equation
where is the same coefficient table as Gradient (Naive / Symmetric / Difference / Summation, with the same apparent-volume and grad-h rescaling), and is the same CRK-then-renormalized kernel gradient. Only the combination with the gradient differs:
- standard mode (default): for a field of shape
[N, ..., d]the last axis is the spatial one and the contribution is the component sum , contracted over the spatial axis — a vector field[N, d]yields a scalar output[N]; - dot mode (
divergenceDotMode=True): for a field[N, d, ...], the first field axis is spatial. The contribution contracts for each remaining component . A matrix yields[N, d].
Output shape: inputShape[:-1] in standard mode,
inputShape[1:] in dot mode (see _computeSPHDivergence_stateBackend).
Public API
warpOperation(
queryParticles, operationProperties, domain,
queryValues=f, referenceValues=f, # required
consistentDivergence=False,
queryVolumes=None, referenceVolumes=None,
crkState=None, gradHState=None, renormalizationState=None,
adjacency=None,
)
with operation == WarpOperation.Divergence.
| Option | Default | Effect |
|---|---|---|
gradientMode | Naive | coefficient scheme (same table as Gradient) |
divergenceDotMode | False | contraction over the first field axis (remaining axes are preserved) |
consistentDivergence | False | scales the apparent volume by the density ratio: (or without supplied volumes) — the "consistent" form of -type bookkeeping |
supportMode / operationMode / corrections / calibration | — | as in Gradient |
JVP / HVP
Value JVP — same relaunch-on-tangents identity as every
value-having operator (tests/operations/test_forward_mode_value_jvp.py).
Geometry JVP — the product rule with vector-valued coefficients:
with the same weights as
Gradient (_jvpCommon.gradientWeightsJVP) and
from computeKernelGradientCRKJVP followed by the
renormalization product rule .
Scope restrictions (enforced centrally in operations.py):
divergenceDotModeandconsistentDivergenceare not in the derived math — the geometry JVP rejects both.- CRK / renormalization tangents: same rules as Gradient (including
simultaneous use);
gradHStateunsupported; query-mass tangents rejected.
Gated by tests/operations/test_forward_mode_geometry_jvp_divergence.py.
No HVP exists for Divergence.
Tests
tests/operations/test_operations_core.py— shape/finiteness.tests/operations/test_operations_consistency.py— trace of the Difference-scheme Gradient equals the Difference-scheme Divergence (interior mask), the key cross-operator identity.tests/operations/test_forward_mode_value_jvp.py,tests/operations/test_forward_mode_geometry_jvp_divergence.py,tests/operations/test_grid_modes.py.
See also
Native derivative contract
Native divergence supports primal, first-order backward/VJP and JVP for all 15 kernels, six support enums, directions and four coefficient schemes in FP32/FP64 and 1D–3D. Both contraction modes preserve arbitrary nonempty packed field axes, with the contracted axis required to have spatial extent D. Scalar fields are supported in 1D. Compiled scalar/vector/matrix blocks bound native per-row storage.
Products include fields, positions/supports, bounds, mass/density or supplied
volumes, query CRK A/B/gradA/gradB, both grad-h omega roles and renormalization.
The consistency option uses mass_j / density_i directly when no volume is
supplied, so an unread zero reference density cannot introduce a spurious
singularity. With supplied volumes it uses volume_j * density_j / density_i.
The symmetric scheme retains its ordinary density weighting. Query-volume
fallback and unused roles preserve their reference semantics.
Torch reverse uses deterministic query CSR/reference CSC ownership without floating atomics. Noncontiguous tensors, aliases, sparse/duplicate/empty CSR, native search, grid rebuilding, calibration, topology lifetime, current streams and fixed-CSR capture/replay are covered. Standalone C++ retains its uncorrected scalar/vector/matrix adjoint ABI and scatter policy; it has no JVP/correction API.
Geometry products preserve Warp's custom regularized gradient Hessian at self/tiny pairs, singular nonfinites and separate reverse/JVP max-support ties. The reference reverse remains detached and first-order; functional/batched transforms and row-chunk AD remain unsupported. Native classic duals and its explicit JVP additionally handle packed contractions, consistency, and omega or bound products as permitted by their APIs. The explicit API has no omega-tangent argument. Warp's explicit geometry JVP remains limited to ordinary vector contraction without consistency/grad-h; numerical comparisons respect that scope.
Maintained checks are tests/native/torch/test_divergence_contract.py,
tests/parity/test_divergence_contract.py, standalone all-kernel products and
the paired frontend's accepted Monaghan steps. Accepted steps qualify primal
simulation use; three-step feedback qualifies toy AD.