From 4b3d398726106ed1fc010f9ee6f1f894a7de3f40 Mon Sep 17 00:00:00 2001 From: Berk Geveci Date: Sat, 5 Dec 2026 12:46:15 -0500 Subject: [PATCH] Plan: finite-difference data, samples, reconstruction and staggered components (#321) --- docs/design/plans/finite-difference-data.md | 308 ++++++++++++++++++++ docs/design/plans/index.md | 1 + mkdocs.yml | 1 + 3 files changed, 310 insertions(+) create mode 100644 docs/design/plans/finite-difference-data.md diff --git a/docs/design/plans/finite-difference-data.md b/docs/design/plans/finite-difference-data.md new file mode 100644 index 000000000..b2728c468 --- /dev/null +++ b/docs/design/plans/finite-difference-data.md @@ -0,0 +1,308 @@ +# Finite-difference data + +**Status: proposed.** Issue [#321](https://github.com/Kitware/tack/issues/321), +2026-12-05. It turns the finite-difference proposal of 2026-11-01 (planned for +0.8, moved to 0.9) into decisions. Related: [#415](https://github.com/Kitware/tack/pull/415), +`finite_differences`, which did most of the proposal's operator stage. + +## Summary + +A finite-difference code writes samples on a logical lattice: at the grid's +points, at its cell centres, or, for staggered schemes, each velocity or field +component on its own set of faces or edges. Three things are mixed up today: + +- **Where a value is**, and what it is: a point sample, or an average over a + cell or face. +- **The function chosen between the samples** to contour, probe, render or + trace: a reconstruction. Trilinear interpolation is a good default for + pictures, but it is a choice, not something the solver said. +- **The solver's difference operators.** Differentiating a reconstruction is + not applying the solver's stencil. Sample `u = x²` on a uniform lattice: + the centred second difference is exactly 2, while the second derivative of + the piecewise-linear interpolant is 0 inside every interval. + +This plan adds a space for lattice samples, `Samples`, that says where its +values are and what they are, and has **no basis**. Nothing interpolates it +implicitly: a filter that needs a function between the samples asks for an +explicit reconstruction. Staggered components are separate `Samples` fields, +collocated explicitly before they are combined. The difference operators are +`finite_differences` (#415), extended to these lattices with a declared +boundary rule, and the finite-volume algorithms (`divergence`, `perot`, +`upwind_flux`), which staggered face components feed without a copy. + +Nothing changes for existing data: `Values`, `H1` and `Constant` fields keep +their meaning and results. There is no `FiniteDifferenceSpace` and no solver. + +## What exists today + +- **Grids.** `StructuredTopology` derives cells from point dimensions; + `UniformCoordinates` and `CartesianProduct` give an image's and a rectilinear + grid's positions without an array of them; a curvilinear grid has a position + array. Nothing per cell is materialized. +- **`Values(data, on)`**: one value per point, edge, face or cell, with no + basis; faces may be `oriented` (measured along the stored normal). But + `algorithms/_common._with_basis` reads point `Values` as order-1 `H1` + (trilinear) and cell `Values` as `Constant` for every algorithm that needs a + basis. So the reconstruction is chosen silently. +- **Faces and edges of a structured grid** are derived as any mesh's are + (`topology.faces()`, `edges()`): numbered by the derivation, not in blocks of + x-, y- and z-faces. A staggered solver's array of x-face values, shaped + `(nx + 1, ny, nz)`, does not line up with that numbering. +- **`finite_differences(data, field, order=2|4|6, derivative=1|2)`** (#415): + the gradient or second derivatives of point or cell data on images, + rectilinear and curvilinear grids, by differences in index space taken to + world axes through the positions' own differences. One-sided stencils at the + edges (VTK's, at order 2); hidden points and cells bound the stencils; ghost + layers widen them. Cell data is differenced over the cells' centres, which + is already a lattice offset by half a cell. +- **Finite volumes** (`docs/visualization/finite-volume.md`): face `Values` + feed `divergence`, `perot` and `upwind_flux`. +- **Evaluated spaces** (`Space.evaluated`, used by `tack.expr`): a field + computed where it is read, carried through filters by `rebind`. +- **Implicit arrays** (`tack.arrays`: `View`, `Strided`, `Permutation`, + `Concatenate`, computed arrays): zero-copy reads of native buffers in another + order. + +## Decisions + +### D1. `Samples`: values on a lattice offset from the grid's points + +```python +td.Samples(data, offset=(0.5, 0.5, 0.5), kind="point", ghosts=0) +``` + +A space on a structured grid (image, rectilinear, curvilinear) whose values sit +on a lattice offset from the grid's points by `0` or `½` a cell along each axis: + +| offset (x, y, z) | where | lattice (for `n` points per axis) | +|---|---|---| +| `(0, 0, 0)` | points | `nx × ny × nz` | +| `(½, ½, ½)` | cell centres | `(nx-1) × (ny-1) × (nz-1)` | +| `(0, ½, ½)` | x-faces (MAC's `u`) | `nx × (ny-1) × (nz-1)` | +| `(½, 0, 0)` | x-edges (Yee's `Ex`) | `(nx-1) × ny × nz` | + +These are PETSc `DMStag`'s locations. An axis offset by `½` has one sample +fewer than points. The values are one array, x fastest, as VTK and Fortran +lay them out, so a native array is taken as it is (zero-copy for a contiguous +NumPy or device array). + +**On a mapped grid** the offset is in index space: a sample's position is the +geometry at that index coordinate, not the grid point plus a world-space +shift. On an image or a rectilinear grid that is the same thing. + +Offsets other than `0` and `½` are not taken: they are what staggered schemes +use, and a general offset would invite a lattice that is not one. + +*Recommended.* + +### D2. `kind`: point samples or averages + +`kind="point"` (the default) says each value is the field at its location. +`kind="average"` says it is the mean over the cell, face or edge around it, as +a finite-volume code writes. The two need different reconstructions (D4): +a cell average is not the value at the cell's centre, though the two agree to +second order for a smooth field. + +*Recommended.* + +### D3. No implicit basis: a filter that needs one asks for a reconstruction + +`Samples` is not `interpolated`. A filter that needs a function between the +values (`contour`, `probe`, `clip`, `gradients`, `advect`) refuses a `Samples` +field, naming the remedy: + +```text +'u' is Samples (point samples at cell centres) with no basis: reconstruct it +first, td.reconstruct(data, 'u', ...), or difference it, td.finite_differences. +``` + +Filters that read values only take it as they take `Values`: the reductions +(`statistics`, over the valid region only, D6), `extract_extent` (which keeps +the samples in the box), `carry`, the writers (D9). + +`Values` and `_with_basis` are left as they are. They are what existing data +and examples use, and changing them would change results. The finite-difference +guide (step 1) says what `_with_basis` chooses, so the choice is visible even +there. + +*Recommended.* + +### D4. `td.reconstruct`: an explicit, named reconstruction + +```python +td.reconstruct(data, field, method="multilinear", at="points", boundary="nearest") +``` + +returns a stored field with a basis on `data`: order-1 `H1` at the points, or +`Constant` on the cells. The methods: + +| method | from | to | what | +|---|---|---|---| +| `"multilinear"` | point samples at any offset | points | each point's value interpolated multilinearly from the samples around it (the identity for samples at the points) | +| `"constant"` | samples at cell centres | cells | each cell its sample: piecewise constant | +| `"mean"` | any samples, averages too | points | each point the mean of the samples around it, as VTK's `vtkCellDataToPointData` | + +- **Zero-copy where it is the identity:** point samples at offset `0` with no + ghosts become `H1` over the same array; samples at cell centres with no ghosts + become `Constant` over the same array. +- **`kind="average"`** is refused by `"multilinear"`, which would treat an + average as a point value; `"constant"` and `"mean"` take it. An + average-preserving reconstruction of higher order (a conservative one) is a + later method, not this plan's. +- **`boundary`:** what a location gets where its stencil reaches past the valid + samples (D6). `"nearest"` (the default) uses the samples there are, as VTK's + cell-to-point does; `"hidden"` gives it no value and marks it hidden + (`Ghosts.HIDDENPOINT`), so filters leave it out. + +A lazy reconstruction (an evaluated space, computing values where a filter +reads them) would save the stored copy. It is left for later, when memory says +so: a stored field works with every filter now. + +*Recommended.* + +### D5. Staggered components are separate fields, collocated explicitly + +A staggered vector is one `Samples` field per component, each with its own +offset and array: MAC's `u`, `v`, `w` on x-, y- and z-faces; Yee's `Ex`, +`Ey`, `Ez` on edges and `Hx`, `Hy`, `Hz` on faces. There is no composite +"staggered vector" type. + +```python +v = td.collocate(data, ["u", "v", "w"], at="cells", boundary="nearest") +``` + +`collocate` interpolates each component linearly along the axes where its +offset differs from the target's, to a common location (cell centres or +points), and returns a vector field there (`Constant` at the cells, `H1` at the +points). Magnitude, energy, cross products and `advect` then take that field. +This is the documented component interpolation the proposal asked for; it +makes no divergence- or curl-preserving claim. + +*Recommended.* + +### D6. Ghost layers in the native array, and the valid region + +A solver's array often includes its halo. `ghosts=g` (a number, or one per axis) +says the array holds `g` extra samples on each side of each axis; the valid +region is the interior. So a halo-padded array is attached without copying out +its interior. + +- **Reductions and writers** read the valid region only. +- **Operators read the halo** where their stencil reaches into it + (`finite_differences`, D7; `reconstruct`'s and `collocate`'s stencils). A + stencil reaching past the halo too follows the `boundary` rule. +- **Refreshing the halo is the caller's**, as it is the solver's. Tack's own + ghost layers (`ghost_cells` on pieces) remain marks on `Values`/`H1` fields as + today; making a `Samples` halo from them is a later step. + +*Recommended.* + +### D7. Difference operators: `finite_differences` on `Samples` + +`finite_differences` takes `Samples` fields at any offset. The output is on the +same lattice (a derivative of x-face samples is at the x-faces), and it is +`Samples` too, `kind="point"`. Its stencils stay those of #415. It gains a +`boundary` argument for samples: + +- `"one-sided"` (today's behaviour, and the default): the stencils narrow at the + valid region's edge; +- `"ghosts"`: read the declared halo (D6); refused if the halo is narrower than + the stencil's half-width; +- `"hidden"`: no value where the full stencil does not fit, marked hidden. + +The convergence of each stencil order on a smooth manufactured field is a test. + +**Staggered face components feed the finite-volume algorithms.** +`td.face_values(data, ["u", "v", "w"])` gives one oriented `Values(faces)` field +over the three face lattices: an index map from Tack's face numbering to the +lattices (computed once per topology and kept), and a sign where the face's +stored normal points down its axis. It is an implicit array over the native +buffers, not a copy. `divergence`, `perot` and `upwind_flux` then take MAC +velocities directly, and the divergence of a MAC field at the cell centres is +the solver's own compact stencil. (`kind="average"` face samples times the face +areas are the fluxes `divergence` wants; point samples are not, and the docs say +which is which.) + +A curl of Yee edge components onto faces is the analogous operator. It waits for +a user who needs it. + +*Recommended.* + +### D8. Components at different times are refused, not mixed + +A Yee scheme stores **E** and **H** half a step apart. `Samples` takes a +`time_offset` (in steps, default 0), and `collocate` refuses components whose +offsets differ unless told `same_time=True`. This is a guard, not a time model: +Tack's time series are their own matter. + +*Recommended.* + +### D9. Writing to VTK + +`dataset_to_vtk` writes `Samples` at offset `0` as point data and at `½` on +every axis as cell data (valid region only, the `kind` as an array +information key). Face and edge lattices have no VTK place on an image or +structured grid: the writer leaves them out, as it leaves out fields on faces +and edges today, and its docs name `collocate`. + +*Recommended.* + +## Not in this plan + +- A finite-difference solver, time stepping, or a stencil catalog beyond + #415's orders. +- Higher-order or conservative reconstructions; a lazy (evaluated) + reconstruction. +- Unstructured or AMR samples: AMR boxes are grids, and `Samples` on each box + follows once this lands, with the AMR work. +- Making `Samples` halos from Tack's ghost layers. +- A Yee curl operator. + +## Open questions + +- **Names.** `Samples`, `reconstruct`, `collocate`, `face_values`. +- **Default `boundary`.** `"nearest"` matches VTK and gives every location a + value; `"hidden"` is more honest at a domain's edge. The recommendation is + VTK's, so that a reconstruction looks like what users know. +- **Does `extract_extent` keep `ghosts`?** A sub-box of a halo-padded array could + keep the halo samples around it as its own halo, or drop them. + +## Validation + +- A collocated example on an image: contour and probe of the reconstruction, + with no connectivity or point coordinates materialized. +- Cell-centred point samples: their positions kept, `"constant"` and `"mean"` + reconstructions documented at the domain's edge, and averages refused by + `"multilinear"`. +- The quadratic counterexample: `finite_differences(derivative=2)` gives 2 where + the reconstruction's second derivative is 0. A smooth manufactured field shows + each stencil order's convergence rate. +- Staggered affine fields collocated exactly, with unequal component extents + and a test that a half-cell shift cannot pass unnoticed; `face_values`' signs + tested separately, and the divergence of a MAC field matching the compact + stencil. +- A halo-padded array: the same interior result as the whole lattice with + `boundary="ghosts"`, and a refusal when the halo is too narrow. +- Zero-copy: attaching a native array, and the identity reconstructions, keep + its buffer. +- Every existing visualization result unchanged (the quick tier, the FV + examples). + +On every backend through `test_dataset_devices.py`'s calls, as #415's +`finite_differences` runs there. + +## Steps + +1. **The guide** (`docs/visualization/finite-difference.md`, beside the + finite-volume guide): the grids, point samples against averages, shifted + sample lattices, what `_with_basis` chooses, `finite_differences`, and the + quadratic counterexample. Docs only, and it can merge before the rest. +2. **`Samples`** (D1, D2, D3, D6): the space, its refusals, reductions, + `extract_extent`, `carry`, the VTK writer (D9). +3. **`reconstruct`** (D4). +4. **`finite_differences` on `Samples`** (D7's first half): offsets, the + `boundary` argument, convergence tests. +5. **Staggered:** `collocate` (D5, D8) and `face_values` into the finite-volume + algorithms (D7's second half), with a MAC example and a Yee layout. + +Step 1 is independent. Steps 3 and 4 need step 2, and step 5 needs 3. diff --git a/docs/design/plans/index.md b/docs/design/plans/index.md index 8ce6c837b..7ade2a3f9 100644 --- a/docs/design/plans/index.md +++ b/docs/design/plans/index.md @@ -30,3 +30,4 @@ recommendation, so a review can answer "yes", "no" or "other" per decision. | [Inspecting device-class objects](inspect-template.md) | [#324](https://github.com/Kitware/tack/issues/324) | Done | | [Mutation rules, stage 1](mutation-rules.md) | [#323](https://github.com/Kitware/tack/issues/323) | Accepted | | [Field quantities](field-quantities.md) | [#467](https://github.com/Kitware/tack/issues/467) | Proposed | +| [Finite-difference data](finite-difference-data.md) | [#321](https://github.com/Kitware/tack/issues/321) | Proposed | diff --git a/mkdocs.yml b/mkdocs.yml index d14d0f327..72ebfb296 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -253,6 +253,7 @@ nav: - Inspecting device-class objects: design/plans/inspect-template.md - Mutation rules, stage 1: design/plans/mutation-rules.md - Field quantities: design/plans/field-quantities.md + - Finite-difference data: design/plans/finite-difference-data.md - Contracts: contracts/index.md - Dataset API design: vis-guide/design/dataset-api.md - Polyhedra: vis-guide/design/polyhedra.md