Internals

Types and functions that are not exported but that the exported entries name: the schedules a grid resolves to, the lag transports, the field shapes, culling, the second histogram axis, the polynomial contract of the operators, the increments of a pair, the per-slice covariances of a fit and the digitize plans.

Schedules and transports

StructureFunctions.Calculations.AbstractSeparableSchedule — Type
AbstractSeparableSchedule

A pair enumeration with at least one uniform direction. The cells factor into slabs, one per index on the remaining directions, and within a pair of slabs every pair of cells is named by an integer lag along the uniform directions. Both sweeps run over (slab, slab, lag): the direct one reduces the field over each lag, the transform produces every lag of a slab pair at once.

A schedule provides uniform_axes, n_slabs, enumerated_pairs, separable_layout, lag_limits, lag_transport and lag_frame.

source
StructureFunctions.Calculations.UniformLagSchedule — Type
UniformLagSchedule(dims, spacing, periodic)

Lag enumeration for a uniform rectilinear grid of dims cells, with constant spacing along each direction, each direction wrapping or not per periodic.

A lag is an integer offset vector naming every pair (cell, cell + lag), so the separation, its direction and its distance bin are properties of the lag alone. Directions are positional and match the trailing axes of the field the schedule is swept over.

spacing is signed, as a descending axis reports it; only its magnitude enters a separation.

source
StructureFunctions.Calculations.RectilinearLagSchedule — Type
RectilinearLagSchedule(uniform, enumerated, axis_order)

Lag enumeration for a rectilinear grid with some uniform directions and some given as coordinate lists. uniform describes the uniform directions, enumerated holds one coordinate vector per remaining direction, and axis_order[k] is the field direction at position k of (uniform..., enumerated...).

Every pair of enumerated indices is a pair of slabs, within which the pairs are named by lags along the uniform directions exactly as on a uniform grid; an enumerated direction never wraps.

source
StructureFunctions.Calculations.ZonalLagSchedule — Type
ZonalLagSchedule(lats, n_lon, dlon, radius, lon_periodic)

Pair enumeration for a lat-lon grid: one latitude per entry of lats (radians), n_lon cells per row spaced dlon apart in longitude (radians), on a sphere of the given radius.

The field it sweeps is stored (component, longitude, latitude) with components in the local (east, north[, radial]) basis, the basis the transport matrices are written in. Each latitude row is one slab and longitude is the uniform direction, with the pair frame applied per lag (see FrameTransport).

source
StructureFunctions.Calculations.ScatteredPairs — Type
ScatteredPairs(points, metric)

Pair enumeration for a grid with no structure to exploit: the points themselves, and the metric that measures between them. Pairs are enumerated by the unstructured path, with culling.

source
StructureFunctions.Calculations.AbstractLagTransport — Type
IdentityTransport()
FrameTransport()

How the two ends of a pair are compared. Under IdentityTransport the increment is the plain difference u(J, i+h) - u(I, i) and the lag's displacement is its direction. Under FrameTransport each end is first expressed in the pair's own frame, B·u(J, i+h) - A·u(I, i) with W×W matrices fixed by the lag, and the longitudinal direction is ê₁.

source
StructureFunctions.Calculations.separable_layout — Function
separable_layout(schedule, data, valid, weights) -> (data, valid, weights)

The packed field, its validity and its pair weights with slab I occupying columns (I-1)·N_u + 1 : I·N_u, N_u the cells of a slab, and the uniform directions running column-major within it.

source
StructureFunctions.Calculations.lag_limits — Function
lag_limits(schedule, I, J, r_max) -> NTuple{Du, Int}
lag_limits(schedule, r_max) -> NTuple{Du, Int}

The largest |h_d| along each uniform direction at which a pair of the two slabs can still lie within r_max; the two-argument form bounds it over every slab pair.

source
StructureFunctions.Calculations.uniform_lag_box — Function
uniform_lag_box(schedule) -> Bool

Whether lag_limits(schedule, I, J, r_max) is the same box for every slab pair, so a device launch indexes its work items by division on one box. A schedule that answers false carries one box per pair, and the launch is indexed through the prefix sum of their volumes.

source
StructureFunctions.Calculations.batch_shares_lag_geometry — Function
batch_shares_lag_geometry(schedule) -> Bool

Whether a transform batch visits each lag once and contracts every slice against it, holding all the slices' inverted columns of a slab pair at the same time.

A schedule answering false takes its slices one at a time, with the inverse scratch of a single slice. Curved schedules answer true.

source
StructureFunctions.Calculations.field_validity — Function
field_validity(u[, cell_mask]) -> Bool vector or AllValid

Which cells of u hold a usable datum: every component finite, and — where a cell_mask is given, as a grid carries one — the cell marked as existing. u is (components, cells...) or a Fields; the vector is in the array family of u.

Returns AllValid when nothing is excluded, so a complete field costs nothing downstream.

source
StructureFunctions.Calculations.batch_validity — Function
batch_validity(u[, cell_mask]) -> Bool matrix or AllValid

Which cells hold a usable datum in each slice of u, stored (components, cells..., slices): one column of cell validity per slice, in the array family of u. cell_mask, where a grid carries one, marks the cells that exist.

Returns AllValid when nothing is excluded, as field_validity does for one slice.

source
StructureFunctions.Calculations.BatchLeading — Type
BatchLeading(u)

Wrap a velocity/position array stored batch-leading, shape (B, D, N) (batch axis innermost/contiguous). CPU batch kernels run on it zero-copy. A plain (D, N, B...) array is transposed once internally.

source

Gridded sweeps

StructureFunctions.Calculations.gridded_lag_sweep! — Function
gridded_lag_sweep!(sums, counts, sf, u, schedule, distance_bins[, axis_bins], ::Val{D}; valid, weights, backend, second_axis)
gridded_lag_sweep!(sums, counts, sf, fields, schedule, distance_bins[, axis_bins]; valid, weights, backend, second_axis)

Accumulate every pair schedule names into the distance histogram sums/counts, or with axis_bins into the joint histogram over separation and second_axis — each pair's own value (InvariantValueAxis, on every schedule) or the angle a SeparationAngleAxis reads from each lag's direction (on a flat schedule) — sums and counts then being (n_distance, n_axis). With sf = SinglePassInvariants() the distance histogram holds the six single-pass invariants of one vector field, sums and counts (6, n_distance), each row counting every pair.

u is stored (component, cells...) with its trailing axes matching the schedule, and D is its component count, which may exceed the grid's dimension — a lag then lies in the grid's directions and is zero along the rest. A multi-field carries several fields the same way. valid says which cells hold a datum; a pair counts only when both of its ends do (see field_validity). weights, one finite weight per cell (nothing for none), multiplies each pair by w_k · w_kp in both sums and counts, so the bin average is Σ w w v / Σ w w and counts must then be floating point; cell_measure supplies a grid's cell areas. backend is the hardware, as on the unstructured entry; a threaded backend splits the slab pairs across tasks, and the lags of a schedule with a single slab.

Each unordered pair is counted once: lags are enumerated one per distinct separation and within a slab only one of ±lag is kept. A lag equal to its own reverse is swept over half of one direction.

Where a periodic direction has an even cell count, its half-period offset joins two cells by two minimal paths of equal length and opposite sign. The operator is averaged over those equal-length displacements, so an operator odd in the separation direction vanishes on the pairs whose every offset half-turns. In the joint histogram such a lag's pairs are split between their images' bins in equal halves, so counts must hold a floating-point type.

source
gridded_lag_sweep!(sums, counts, sf, u, schedule::ScatteredPairs, distance_bins, ::Val{D}; valid, backend)

Accumulate every pair of a structureless grid, by enumerating them.

Cells holding nothing are dropped before the sweep.

source
StructureFunctions.Calculations.gridded_sweep! — Function
gridded_sweep!(sums, counts, sf, u, schedule, distance_bins[, axis_bins], ::Val{D}, spectral_backend; valid, weights, backend)
gridded_sweep!(sums, counts, sf, fields, schedule, distance_bins[, axis_bins], spectral_backend; valid, weights, backend)

Accumulate the histogram of every pair schedule names, by the algorithm spectral_backend selects. valid and weights are as on gridded_lag_sweep!.

The lag sweep visits each lag and is exact for any pairwise operator; a transform produces the increment moment tensor of every lag at once and serves the operators that are polynomials in δu. AutoSpectralBackend picks the one with less work and resolves to the lag sweep unless a transform is loaded. backend names the hardware, as on the unstructured entry.

With axis_bins the histogram is joint in separation and second_axis: each pair's value (InvariantValueAxis), which only the lag sweep bins, or the angle a SeparationAngleAxis reads from each lag; sums and counts are then (n_distance, n_axis). With sf = SinglePassInvariants() the distance histogram holds the six single-pass invariants, sums and counts (6, n_distance).

source
StructureFunctions.Calculations.gridded_lag_sweep_batch! — Function
gridded_lag_sweep_batch!(sums, counts, sf, u, schedule, distance_bins[, axis_bins], ::Val{D}; valid, weights, backend)

Sweep the lags of schedule once and reduce every slice of a batch against each of them, into sums/counts of shape (n_distance, n_slices) — with axis_bins, (n_distance, n_axis, n_slices); for sf = SinglePassInvariants(), (6, n_distance, n_slices).

u is (component, cells..., slices), the cells matching the schedule. valid is AllValid() or one column of cell validity per slice (batch_validity); weights is one finite weight per cell, shared by the slices, so a cell measure is given once. backend names the hardware, as on gridded_lag_sweep!, whose pair enumeration, reading rule and half-turn averaging this follows exactly: slice t of the output is that sweep on slice t of the field.

source
StructureFunctions.Calculations.gridded_sweep_batch! — Function
gridded_sweep_batch!(sums, counts, sf, u, schedule, distance_bins[, axis_bins], ::Val{D}, spectral_backend; valid, weights, backend)

Accumulate the histogram of every pair schedule names, for every slice of a batch, by the algorithm spectral_backend selects. sums and counts are (n_distance, n_slices), or with axis_bins (n_distance, n_angle, n_slices).

u is (component, cells..., slices) with the cells matching the schedule; the packed form is (V·D + K, cells, slices), which is how a multi-field time series is passed. valid, weights and backend are as on gridded_lag_sweep_batch!.

Each pair's geometry is fixed by schedule alone, so the lag sweep visits every lag once and reduces all the slices against it. A transform shares each lag between the slices on the schedules that answer batch_shares_lag_geometry and takes one slice at a time on the rest.

source
StructureFunctions.Calculations.transform_engine — Function
transform_engine(sf, data, schedule, distance_bins, ::Val{D}, ::Val{V}, ::Val{K}, valid, weights, tag; to = identity, workspace = nothing, slice = (1, 1))

The transform engine's prepared state for a field: the forward transforms of every masked, weighted monomial of every slab — by FFT for a grid's slabs, by non-uniform FFT for a ScatteredModesSchedule, as the spectral tag names — the inverse columns and the lag bookkeeping, every array moved by to. The field is slice t of slice = (t, nt): its FFT spectra are slice t of one (blk, nchunks, n_monomials, nt) array kept in workspace (the engine's spectra), each chunk of slabs contiguous at 64-byte alignment. Supplied by the AbstractFFTs extension.

source
StructureFunctions.Calculations.device_transform_sweep! — Function
device_transform_sweep!(sums, counts, backend, sf, data, schedule, distance_bins, plan, nb, ::Val{D}, ::Val{V}, ::Val{K}, valid, weights, tag, axis, workspace)

The transform engine on a device backend, supplied by the KernelAbstractions extension together with an AbstractFFTs implementation for the device's arrays. axis is nothing for the distance histogram or (axis_edges, n_angle, second_axis) for the joint one; workspace is a TransformWorkspace or nothing.

source
StructureFunctions.Calculations.device_transform_sweep_batch! — Function
device_transform_sweep_batch!(sums, counts, backend, sf, data, schedule, distance_bins, plan, nb, ::Val{D}, ::Val{V}, ::Val{K}, valid, weights, tag, axis, workspace)

The transform engine on a device backend for a batch over a trailing slice axis, supplied by the KernelAbstractions extension together with an AbstractFFTs implementation for the device's arrays. axis is nothing for the distance histogram or (axis_edges, n_angle, second_axis) for the joint one; workspace is a TransformWorkspace or nothing.

source

Kernels of the transforms

StructureFunctions.Calculations.isotropic_kernel — Function
isotropic_kernel(::Val{D}, x)

Angular average of cos(k·r) over the directions of r in D dimensions, at x = kr.

cos on a line, J₀ on a plane and sin(x)/x in a volume. This is the kernel of the isotropic Fourier transform, so a single mode of amplitude A and wavenumber k₀ has S₂(r) = A²[1 - isotropic_kernel(Val(D), k₀ r)] after angular averaging.

source
StructureFunctions.Calculations.AbstractMissingLagPolicy — Type
AbstractMissingLagPolicy

What a lag-space spectrum does with a lag no pair of held cells names. RefuseMissingLags() throws, since the structure function is undefined there. ZeroDeviationAtMissingLags() sets the autocovariance to zero at that lag.

source

Shapes, culling and the second axis

StructureFunctions.Calculations.AbstractFieldShape — Type
AbstractFieldShape{D}

Which array-rank pattern a validated (x, u) pair forms, with D the number of velocity components on axis 1 of u. Backends dispatch on this, so D reaches the kernels as a static method parameter.

  • PointField: u is (D, N).
  • SharedPositionField: u is (D, N, auxiliary...) — one set of positions, many fields.
  • VaryingPositionField: x and u both carry matching auxiliary axes.

The coordinate count on axis 1 of x is fixed by the metric's geometry (see HelperFunctions.coordinate_width).

source
StructureFunctions.Calculations.CellGrid — Type
CellGrid{D, FT, IV, PV, OV}

Uniform cell decomposition of a point set, with points sorted by cell id, so every cell is one contiguous run: cell_run(grid, c) in the permuted order.

Only occupied cells are stored, as the sorted ids cell_ids and their run boundaries run_starts; the cell-id space prod(dims) grows as (extent/cutoff)^D, while occupancy is at most the point count, so memory and block enumeration are O(N).

The index and offset arrays are type parameters, so one type serves host and device-resident copies.

source
StructureFunctions.Calculations.AbstractSecondAxisSource — Type
AbstractSecondAxisSource

What the second axis of a joint histogram bins.

The first axis is always the separation. The second is a choice: the operator's own value, giving the distribution of the structure function at each separation; or the separation's direction, giving the structure function resolved by angle. Both are digitized the same way into the same histogram.

source
StructureFunctions.Calculations.SeparationAngleAxis — Type
SeparationAngleAxis(reference_axis)

Bin the angle between the pair's separation and reference_axis, giving S(r, θ), the structure function resolved by direction.

The angle is folded so that a pair and its reverse give the same value. In two dimensions the signed azimuth is taken modulo π, giving [0, π); in three or more the polar angle to the axis is taken from |cos|, giving [0, π/2].

reference_axis need not be normalized. A pair separated along the reference axis has angle exactly 0, and bins are (lo, hi], so bins over this axis must start just below zero, as axis_bounds does, for an axis-aligned pair to fall in a bin.

source

The polynomial contract

StructureFunctions.StructureFunctionTypes.moment_contract — Function
moment_contract(sf, M::SymmetricMoments, r̂, ::Val{V}, ::Val{K})

Σ_pairs sf(δu, r̂) from the increment moment tensor M[i₁, …, i_p] = Σ_pairs Π_k δu[i_k] of the pairs sharing the unit separation r̂. δu is packed as V vector fields of width D = length(r̂) followed by K scalar fields, so M has W = V * D + K components. Operators that are not polynomials in δu have no method.

source
StructureFunctions.StructureFunctionTypes.scalar_order — Function
scalar_order(sf) -> Int

The total power of scalar-field increments in sf.

Reading a pair from its other end leaves every vector quantity unchanged — δu, r̂ and n̂ all flip together — and negates each scalar increment, so sf changes sign under that reading exactly when this is odd. Such an operator is evaluated with the pair read from the lower to the upper end along the first coordinate that separates its two points (on a sphere: from south to north, and along one parallel from west to east); where neither end comes first, the two readings are averaged and the moment is zero.

source

Pair increments

StructureFunctions.HelperFunctions.transverse_energy — Function
transverse_energy(δu_L, ‖δu‖²)

$‖δu‖² - δu_L²$, the squared norm of the transverse increment. Its two terms cancel to within round-off when the increment is almost parallel to the separation, so it can be a round-off below zero; a caller taking its square root floors it first.

source

Fits over a batch

Digitize plans

StructureFunctions.BucketedBinEdges — Type
BucketedBinEdges(edges::AbstractVector{<:Base.IEEEFloat}[, cells])

Sorted edges with a table that brackets every lookup. The edges are cut into cells by the cell map cells — by default LinearCells of 16(length(edges) - 1) equal cells over the finite span. Each cell holds a BucketCell: the first edge at or above it, how many edges lie in it, and that edge's value. A query in a cell of at most one edge is decided by one comparison with that value; a cell of more bisects its own edges. Built by digitize_plan once per call.

source
StructureFunctions.BucketCell — Type
BucketCell(first, count, edge)

One cell of a BucketedBinEdges: first is the index of the first edge at or above the cell, count the number of edges inside it, and edge the value of edge first, Inf past the last edge.

source
StructureFunctions.LinearCells — Type
LinearCells(inv_width, offset, last_cell)

Cells of equal width: cell(x) = trunc(clamp(fma(x, inv_width, offset), 0, last_cell)), which never decreases as x increases.

source
StructureFunctions.Log2Cells — Type
Log2Cells(lo, key0, shift, last_cell)

Cells of equal width in log₂ x to within the piecewise-linear error of the float format: the cell of x > lo is the IEEE exponent and leading mantissa bits of x (its bit pattern shifted right by shift) less those of lo, clamped to last_cell; x ≤ lo takes cell 0. Never decreases as x increases.

source
StructureFunctions.LogTableBinEdges — Type
LogTableBinEdges(b::LogBinEdges)

The edges of b as a table, searched from the index estimate ⌊a log₂x + c⌋ + 2, which is formed in the type of a and c (Float32 by default) with Base.FastMath.log2_fast and stepped to the first edge at or above x. The edges decide the bin, so the estimate's rounding does not. A device kernel digitizes LogBinEdges with it.

source
StructureFunctions.AbstractSquaredDigitizePlan — Type
AbstractSquaredDigitizePlan

Maps a squared separation r² to the bin digitize(sqrt(r²), edges) gives, compared against the thresholds S_k, the largest s with sqrt(s) ≤ edges[k]. The two agree for every r² because sqrt is correctly rounded.

source