Stencil Operators
stencil
Finite-difference stencil operators for lattice fields.
This module provides a unified implementation of spatial derivative operators using finite difference stencils. These work for any dimension (1D, 2D, 3D) with periodic boundary conditions.
For FFT-based spectral methods, see spectral.py.
All operators are JIT-compiled for performance. Any operator that takes an
axis (or other structural, non-array) argument must mark it static --
@partial(jit, static_argnames="axis") -- because jnp.roll(..., axis=...)
requires a concrete Python index and will raise ConcretizationTypeError on a
tracer. Note that operators which merely index an array with an integer (as in
operators/gauge.py) need no such treatment: dynamic indexing traces fine.
laplacian(field, dx)
Compute Laplacian \(\nabla^2 \phi\) using second-order finite differences.
Uses the standard 3-point stencil in each dimension:
Works for 1D, 2D, or 3D fields with periodic boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array (1D, 2D, or 3D) |
required |
dx
|
float
|
Lattice spacing (assumed uniform in all directions) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Laplacian at each lattice site (same shape as input) |
Source code in jaxlatt/operators/stencil.py
gradient_squared(field, dx)
Compute \(|\nabla\phi|^2\) using centered finite differences.
Works for 1D, 2D, or 3D fields. For complex fields, returns \(|\nabla\phi|^2\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array (1D, 2D, or 3D), can be real or complex |
required |
dx
|
float
|
Lattice spacing |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Gradient squared at each site (real-valued, same shape as input) |
Source code in jaxlatt/operators/stencil.py
gradient(field, dx)
Compute gradient \(\nabla\phi\) using centered finite differences.
Returns a tuple of arrays, one for each spatial direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array (1D, 2D, or 3D) |
required |
dx
|
float
|
Lattice spacing |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, ...]
|
Tuple of gradient components \((\partial_x \phi, \partial_y \phi, \ldots)\) for each dimension |
Source code in jaxlatt/operators/stencil.py
divergence(vector_field, dx)
Compute divergence \(\nabla \cdot F\) for a vector field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vector_field
|
Array
|
Array of shape (ndim, *spatial_shape) First axis is the vector component index. |
required |
dx
|
float
|
Lattice spacing |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Divergence at each site (shape = spatial_shape) |
Source code in jaxlatt/operators/stencil.py
forward_gradient(field, dx, axis)
Compute forward finite difference \(\partial_i \phi = [\phi(n+\hat{i}) - \phi(n)] / dx\).
Useful for gauge-covariant derivatives and link-based calculations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array |
required |
dx
|
float
|
Lattice spacing |
required |
axis
|
int
|
Direction of derivative (0, 1, or 2). Static: it selects the
|
required |
Returns:
| Type | Description |
|---|---|
Array
|
Forward derivative along specified axis |
Source code in jaxlatt/operators/stencil.py
backward_gradient(field, dx, axis)
Compute backward finite difference \(\partial_i \phi = [\phi(n) - \phi(n-\hat{i})] / dx\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Field array |
required |
dx
|
float
|
Lattice spacing |
required |
axis
|
int
|
Direction of derivative (0, 1, or 2). Static: it selects the
|
required |
Returns:
| Type | Description |
|---|---|
Array
|
Backward derivative along specified axis |
Source code in jaxlatt/operators/stencil.py
hessian_squared(field, dx)
Pointwise squared Frobenius norm of the Hessian matrix.
Computes
using second-order centred finite differences for diagonal terms and forward-then-backward (mixed) differences for off-diagonal terms. Exploits Hessian symmetry — each off-diagonal pair is computed once and weighted by 2. Periodic boundary conditions throughout.
Arises in higher-derivative scalar field equations, where a combination
such as :math:(\Box\phi)^2 - (\nabla_\mu\nabla_\nu\phi)^2 cancels the
second-time-derivative piece and leaves this purely spatial contraction
behind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Array
|
Real scalar field array (1-D, 2-D, or 3-D). |
required |
dx
|
float
|
Lattice spacing (uniform in all directions). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of the same shape as |
Array
|
at each site. |