Gradient
SPH gradient of a field, in four coefficient schemes. Kernel:
coreOperations/wp_gradient.py (computeSPHGradientTensor_Kernel);
JVP coreOperations/wp_gradientJVP.py.
Equation
with selected by gradientMode
(GradientScheme):
| Scheme | Notes | |
|---|---|---|
Naive (1) | the standard "scatter" form | |
Symmetric (2) | density-symmetric; no term at all | |
Difference (3) | annihilates constants; exact linear reproduction requires the appropriate resolved moment correction | |
Summation (4) |
where
and, if grad-h terms are supplied (GradHState), both field values are
rescaled by the local smoothing-length ratio:
, .
is the kernel gradient sphKernelGradient under the
chosen support scheme, optionally passed
through the CRK correction (computeKernelGradientCRK, see
CRK overview) and then gradient
renormalization (left-multiply by , see
Renormalization). The composition order is fixed everywhere:
CRK first, then renormalization, applied to the single resulting
.
Output: shape queryValues.shape[1:] + (dim,) — a scalar field
gives [N, dim], a vector field [N, dim, dim].
Public API
warpOperation(
queryParticles, operationProperties, domain,
queryValues=f, referenceValues=f, # required, shapes [N] or [N, ...]
adjacency=None,
queryVolumes=None, referenceVolumes=None, # apparent-volume substitution
crkState=None, # CRKState
gradHState=None, # GradHState | (qOmega, rOmega) | omega
renormalizationState=None, # RenormalizationState | [N,D,D] tensor
)
with operation == WarpOperation.Gradient.
| Option | Default | Effect |
|---|---|---|
gradientMode | Naive | coefficient scheme (table above) |
supportMode | Gather | pairwise support of |
operationMode | AllToAll | directional filtering on kinds |
queryVolumes/referenceVolumes | None | substitute for (only the reference side is ever read) |
crkState | None | CRK-corrected kernel gradient; for Gradient the full state including gradA/gradB is used |
gradHState | None | rescale , by |
renormalizationState | None | left-multiply by |
calibrateNormalization, n_h | False/None | lattice-normalization factor on the kernel |
The dispatcher requires both queryValues and referenceValues
for Gradient; referenceValues defaults to queryValues inside
warpOperation.
Corrections, in order
f_j \leftarrow f_j/\omega_j,f_i \leftarrow f_i/\omega_iifgradHStateis passed.- : plain kernel gradient → CRK-corrected if
crkState→ ifrenormalizationState. V_jsubstituted byreferenceVolumes[j]if supplied.
JVP / HVP
Value JVP — the operator is exactly linear and homogeneous in
queryValues/referenceValues, so the value-tangent JVP is the same
operator relaunched on the tangent arrays:
warpOperationJVP(..., tangentQueryValues=..., tangentReferenceValues=...).
Gated by tests/operations/test_forward_mode_value_jvp.py.
Geometry JVP — warpOperationJVP(..., queryTangentState=..., referenceTangentState=...) computes, per neighbor, the product rule
( after CRK/renorm, with its own tangent
from computeKernelGradientCRKJVP):
with (values frozen at primal) and the
per-pair weights from _jvpCommon.gradientWeightsJVP:
| Scheme | ||
|---|---|---|
Naive | ||
Difference | ||
Summation | ||
Symmetric |
with tangents
Notes on scope:
- Query-side mass tangents are rejected — no formula has an term. Query-side density tangents are used (Symmetric scheme reads ).
- CRK tangent: pass
crkTangentState(aCRKTangentState) to differentiate the correction itself; omitting it holds CRK frozen. The usual way to obtain a real tangent iscomputeCRKFactorsJVPon the same geometry tangents. - Renormalization tangent: pass
renormalizationTangentState; the kernel applies (product rule, after CRK). Obtain one viacomputeRenormalizationMatricesJVP. - CRK and renormalization tangents may be supplied simultaneously — they compose in the same fixed order as the primal kernel.
gradHStateis not supported for the geometry JVP (no consumer exists).- The value-tangent term is fused into the geometry-JVP neighbor loop
(
_FUSED_VALUE_JVP_OPERATIONSinoperations.py) so a combined value+geometry JVP costs one pass, not two.
Gated by tests/operations/test_forward_mode_geometry_jvp_gradient.py
(gradcheck against production, all correction combinations).
No HVP exists for Gradient.
Tests
tests/operations/test_operations_core.py— shape/finiteness for scalar, vector and matrix fields.tests/operations/test_operations_consistency.py— e.g. the trace of the Difference-scheme gradient of a vector field matches the Difference-scheme divergence (interior mask).tests/operations/test_forward_mode_value_jvp.py— value-JVP identity (relaunch on tangents).tests/operations/test_forward_mode_geometry_jvp_gradient.py— geometry JVP + CRK/renorm tangents vs reverse-mode Jacobian.tests/operations/test_grid_modes.py— adjacency vs grid traversal parity.
See also
Divergence · Curl · Laplacian · CRK overview · Renormalization
Native derivative contract
Native gradient supports first-order backward/VJP and JVP for all 15 kernels, six support enums, operation directions and gradient schemes in FP32/FP64 and 1D–3D. Scalar and arbitrary nonempty packed field axes preserve their output shape with a final spatial derivative axis. Native launches use bounded scalar/vector/matrix blocks rather than per-edge Jacobians.
The complete product rule includes positions, supports, reference mass/density weights or supplied volumes, symmetric query densities, query/reference fields, query CRK A/B/gradA/gradB, reference grad-h omega, renormalization matrices and periodic bounds. Unread roles return zero products, including query mass, query grad-h omega and supplied volumes under the symmetric scheme. Query-volume fallback retains the reference-volume role even for unequal populations; an unread query volume need not have query-population length when an explicit reference volume is supplied.
Torch reverse has deterministic query CSR/reference CSC ownership with no floating atomics. Aliases, noncontiguous inputs/search, sparse/duplicate/empty CSR, grid rebuilding, calibration, topology lifetime and fixed-CSR CUDA graph replay are covered. The standalone uncorrected C++ scalar/vector/matrix ABI accepts all kernels through its existing scatter/accumulation adjoint; it exposes no JVP or correction bundle.
Native matches Warp's analytic custom gradient adjoint and regularized Hessian at self/tiny pairs, including finite regularized cusp values, inherited singular nonfinites and raw spline cancellation. Max-support reverse ties belong to the reference support; analytic JVP ties belong to the query. Warp's detached first-order reverse and unsupported functional/row-chunk AD remain the reference scope. Explicit JVPs include CRK and renormalization; classic native forward duals also carry omega and bound tangents. The explicit API has no omega-tangent argument. Numerical gradcheck uses differentiable fixtures; direct Warp parity tests the boundary conventions.
Maintained checks: tests/native/torch/test_gradient_contract.py,
tests/parity/test_gradient_contract.py, standalone all-kernel products and
the paired frontend's accepted Inutsuka steps. Simulation evidence is primal;
independent three-step gradient feedback qualifies toy AD.