Skip to content

[Scripting] Generalize PlotfileDataset and the cache budget for analysis tools - #289

Merged
WeiqunZhang merged 1 commit into
mainfrom
analysis-1-core
Nov 4, 2026
Merged

WeiqunZhang merged 1 commit into
mainfrom
analysis-1-core

Conversation

@BenWibking

@BenWibking BenWibking commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Small changes to existing code that the analysis library (next PR) needs. No new feature yet.

  • Cache budget in core. The cache-budget default and AMREXPLORER_CACHE_SIZE_MB parsing move from src/qt/CacheConfig.hpp to include/amrexplorer/core/CacheConfig.hpp, so code without Qt can share them. src/qt/CacheConfig.hpp now uses the core version.
  • Derived-field policy. PlotfileDataset takes a DerivedFieldPolicy:
    • The viewer keeps Skip.
    • An unattended analysis passes Strict, so a definition that doesn't resolve fails at open instead of silently dropping the field.
  • derivedFieldProgram(field). Exposes a derived field's compiled program, so a caller can bound in advance which blocks a read will pin.
  • FabValues spans. FabValues::singleValues() and doubleValues() expose the payload in its stored precision, for callers copying many values without dispatching on precision per value.

Tests

  • New tests/integration/test_derived_field_policy.cpp:
    • Skip installs what resolves and reports the rest.
    • Strict fails the open, naming the broken definition.
    • derivedFieldProgram returns null for a stored field and the right inputs for a derived one, and a derived block evaluates its program.
  • test_single_precision_plotfile.cpp checks both span accessors for float and double FABs.
  • The headless ctest suite passes (63 tests).

Stack

Part of a stack replacing #276. Review and merge in order; each PR shows only its own layer.

  1. [Scripting] Generalize PlotfileDataset and the cache budget for analysis tools #289 Prepare PlotfileDataset and the cache budget for analysis ← this PR
  2. [Scripting] Add iteration plans for leaf-cell chunks #285 Exact leaf-cell plans and chunks (C++)
  3. [Scripting] Add the amrexplorer Python package for batch analysis #286 The amrexplorer Python package (bindings)
  4. [Scripting] Add map-reduce parallel reductions over leaf cells #287 Parallel reductions and batch helpers

🤖 Generated with Claude Code

@BenWibking BenWibking changed the title Prepare PlotfileDataset and the cache budget for analysis [Scripting] Prepare PlotfileDataset and the cache budget for analysis Oct 29, 2026
@BenWibking
BenWibking added this pull request to stack #291 October 29, 2026 01:21
@BenWibking BenWibking changed the title [Scripting] Prepare PlotfileDataset and the cache budget for analysis [Scripting] Prepare PlotfileDataset and the cache budget for scripting tools Oct 29, 2026
@BenWibking
BenWibking removed this pull request from stack #291 October 29, 2026 03:53
@BenWibking
BenWibking added this pull request to stack #292 October 29, 2026 03:53
@BenWibking BenWibking changed the title [Scripting] Prepare PlotfileDataset and the cache budget for scripting tools [Scripting] Generalize PlotfileDataset and the cache budget for scripting tools Nov 2, 2026
@BenWibking BenWibking changed the title [Scripting] Generalize PlotfileDataset and the cache budget for scripting tools [Scripting] Generalize PlotfileDataset and the cache budget for analysis tools Nov 2, 2026
@BenWibking
BenWibking removed this pull request from stack #292 November 2, 2026 16:22
@BenWibking
BenWibking added this pull request to stack #294 November 2, 2026 16:22
- Move the cache-budget default and AMREXPLORER_CACHE_SIZE_MB parsing
  from src/qt to core/CacheConfig.hpp, so code without Qt can share them.
- Let PlotfileDataset take a DerivedFieldPolicy, so an unattended
  analysis can fail at open on a definition that does not resolve, and
  expose a derived field's program for callers that bound reads ahead.
- Give FabValues span accessors in its stored precision, for callers
  copying many values without dispatching per value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WeiqunZhang
WeiqunZhang merged commit 1bf9685 into main Nov 4, 2026
15 checks passed
@WeiqunZhang
WeiqunZhang deleted the analysis-1-core branch November 4, 2026 01:40
WeiqunZhang pushed a commit that referenced this pull request Nov 4, 2026
A Qt-free `src/analysis` library with an exact planner and reader for a
plotfile's **leaf cells**, the cells no finer level covers. This is the
core algorithm the Python package builds on.

- **Plans.** `planLeafChunks` partitions the leaf cells of a region into
one item per grid, in a fixed order: levels coarsest first, grids in
native FAB order.
- Covered coarse cells never appear, and every leaf cell appears exactly
once.
- Regions select cells by center, `lower <= center < upper`, so adjacent
regions split the cells between them.
  - Planning reads metadata only.
- **Precision.** Field values keep the widest precision any level stores
unless the plan asks for float32 or float64. Derived fields are
evaluated per block and follow the plan's dtype.
- **Cache budget.** Each item's worst-case cache use is checked against
the budget at planning, and again when reading. The error names the
item.
- **Chunks.** `readLeafChunk` copies an item's values out of the cache:
a chunk holds no cache pins and outlives the dataset. Geometry arrays
(indices, centers, widths, volumes) are computed on first use.
- **Errors.** Unknown fields, unsupported geometry or centering, invalid
regions and items planned on another dataset are `AnalysisError`.

## Review notes

`src/analysis/LeafChunks.cpp` is the file to read closely. The
covered-cell computation, region-to-index-box conversion and the
pinned-bytes estimate are where the guarantees come from.

## Tests

`tests/integration/test_leaf_chunks.cpp` runs on a two-level 2-D fixture
whose fine grid straddles a coarse grid boundary. It checks:
- exact partition, stored values, and the leaf cells tiling the domain
- the half-open region rule, and an empty plan outside the domain
- unknown, duplicate and non-finite requests are refused, as is
non-Cartesian geometry
- float32 narrowing and derived fields
- reading items repeatedly and in any order
- no pins after a read, and chunks outliving the dataset
- the planning-time and read-time budget checks
- cancellation, and items bound to their dataset

The headless ctest suite passes (64 tests).

## Stack

Part of a stack replacing #276. Review and merge in order; each PR shows
only its own layer.
1. #289 Prepare PlotfileDataset and the cache budget for analysis
2. #285 Exact leaf-cell plans and chunks (C++) ← this PR
3. #286 The amrexplorer Python package (bindings)
4. #287 Parallel reductions and batch helpers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
WeiqunZhang pushed a commit that referenced this pull request Nov 17, 2026
The `amrexplorer` Python package: nanobind bindings over the leaf-cell
planner and reader from the previous PR, plus a small pure-Python
wrapper and build plumbing. Most of this PR is mechanical glue. The
Python tests are the clearest statement of the API.

- **`open_plotfile(path, derived=..., cache_bytes=...)`** returns a
`Dataset` usable as a context manager. Derived-field definitions use
`Strict`, so a bad one fails at open.
- **Plans and chunks.** `plan_leaf_chunks` returns a sequence of items,
and `read_leaf_chunk` returns a chunk.
- A chunk is a mapping of read-only NumPy arrays that keep their memory
alive, so they outlive the chunk and the dataset.
  - Geometry (`x`, `dx`, `i`, `cell_volume`, ...) is computed lazily.
  - `leaf_cells()` plans and reads in one loop.
- **`CancelToken`** stops a read in progress. Analysis, cache-budget,
read, closed-dataset and cancellation errors map to Python exceptions.
- **Build.**
- `-DAMREXPLORER_ENABLE_PYTHON=ON` builds the module into
`<build>/python`.
- `pip install .` builds the module alone through scikit-build-core
(`pyproject.toml`).
- ctest runs the pytest suite as `python_analysis` when pytest is
installed.
- **Docs.** `docs/python-analysis.md` (installing and leaf cells) and
`docs/building.md`.

## Review notes

- `python/src/module.cpp` (~540 lines) is type conversion: dicts and
boxes, NumPy views kept alive by capsules, GIL release around reads, and
closed-dataset checks.
- `python/amrexplorer/_dataset.py` is property accessors and the
Mapping/Sequence protocols over the native objects.
- Both are worth skimming, not reading line by line.

## Tests

- `tests/python/test_leaf_chunks.py` (25 tests) checks every contract
against an independent NumPy reconstruction of 1-D, 2-D and 3-D fixture
plotfiles. It includes a region over a 2^64-cell domain, a regression
test for an overflow found on real data.
- `tests/python/plotfile_writer.py` writes AMReX-layout plotfiles
(per-level precision, stored ghost cells, version-1 and version-2 FAB
headers) for these and later tests.
- The headless ctest suite passes (65 tests, `python_analysis` among
them).

## Stack

Part of a stack replacing #276. Review and merge in order; each PR shows
only its own layer.
1. #289 Prepare PlotfileDataset and the cache budget for analysis
2. #285 Exact leaf-cell plans and chunks (C++)
3. #286 The amrexplorer Python package (bindings) ← this PR
4. #287 Parallel reductions and batch helpers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants