Skip to content

Use PETSc-native XDMF visualization topology - #218

Merged
lmoresi merged 7 commits into
underworldcode:developmentfrom
gthyagi:bugfix/petsc-hdf5-viz-topology
Jun 18, 2026
Merged

Use PETSc-native XDMF visualization topology#218
lmoresi merged 7 commits into
underworldcode:developmentfrom
gthyagi:bugfix/petsc-hdf5-viz-topology

Conversation

@gthyagi

@gthyagi gthyagi commented May 29, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR revives the PETSc-native XDMF topology path from feature/annulus-spherical-benchmark-debug on top of current development, then extends the output API so timestep files can optionally carry PETSc reload metadata as well.

The branch currently contains:

  • 22dd9ab2 / 90774778: add petsc_format control to Mesh.write() and make XDMF use PETSc's direct /viz/topology/cells connectivity rather than internal DMPlex /topology/cells.
  • 9f41035e / 47719277: keep restart-style topology in write_timestep mesh files while still pointing XDMF to direct visualization topology.
  • 5cd8a1f8: remove the manual _write_mesh_viz_groups() path added in PR Fix XDMF output to explicitly write cell-to-vertex topology and ensure strict ParaView compatibility #205 and rely on PETSc-native visualization topology.
  • 4bd05789: add unified timestep/reload output support with write_timestep(..., petsc_reload=True) and write_checkpoint(..., create_xdmf=True).

Closes #217.

XDMF / visualization topology change

On current development, PR #205 added a manual Python _write_mesh_viz_groups() path to assemble /viz/geometry and /viz/topology. This PR removes that manual gather/deduplication path and lets PETSc write the visualization topology natively.

The resulting mesh output keeps the restart/topology information needed by UW3 while writing direct visualization connectivity at:

/viz/topology/cells

and geometry at:

/geometry/vertices

The XDMF writer validates and references the PETSc-written /viz/topology/cells dataset rather than raw DMPlex /topology/cells.

Unified timestep / PETSc reload output

This PR also adds an explicit output-payload switch:

mesh.write_timestep(
    "output",
    index=0,
    outputPath=str(output_dir),
    meshVars=[velocity, pressure],
    create_xdmf=True,
    petsc_reload=True,
)

With create_xdmf=True, timestep variable files contain the existing XDMF/remap payloads:

/fields/<variable>
/fields/coordinates
/vertex_fields/... or /cell_fields/...

With petsc_reload=True, the same per-variable HDF5 files also contain PETSc DMPlex reload payloads:

/topologies/uw_mesh/dms/<variable>/section
/topologies/uw_mesh/dms/<variable>/vecs/<variable>

That means a single output.mesh.<variable>.00000.h5 file can be used by both:

var.read_timestep(...)
var.read_checkpoint(...)

write_checkpoint(..., create_xdmf=True) now routes through the same unified writer. The default write_checkpoint() path remains restart-only and backward-compatible.

Why this is needed

The PETSc-native topology path avoids maintaining UW3-side MPI topology assembly, vertex deduplication, and cell reordering code. The unified output option also addresses the earlier architectural concern that visualization/remap output and PETSc-native reload output were locked into separate file families.

Users can now choose explicitly:

# Visualization/remap only
mesh.write_timestep(..., create_xdmf=True, petsc_reload=False)

# PETSc-native reload only
mesh.write_checkpoint(...)

# Unified visualization/remap + PETSc reload output
mesh.write_timestep(..., create_xdmf=True, petsc_reload=True)

Scope note

This PR addresses the PETSc-native /viz/topology/cells replacement requested in #217 for the current linear visualization topology workflow. It does not attempt to implement curved/isoparametric geometry support; that is a larger infrastructure change and should be tracked separately if needed.

Tests

Core UW3 tests run locally in amr-dev:

  • ./uw build
  • .pixi/envs/amr-dev/bin/python -m pytest -q tests/test_0003_save_load.py tests/test_0005_xdmf_compat.py
    • 16 passed
  • .pixi/envs/amr-dev/bin/mpirun -n 2 .pixi/envs/amr-dev/bin/python -m pytest -q --with-mpi tests/test_0003_save_load.py::test_timestep_with_petsc_reload_roundtrip
    • passed on both ranks
  • .pixi/envs/amr-dev/bin/mpirun -n 2 .pixi/envs/amr-dev/bin/python -m pytest -q --with-mpi tests/parallel/test_0005_xdmf_viz_topology_mpi.py
    • passed on both ranks in earlier validation for this branch

Benchmark validation:

  • Ran the spherical Thieulot benchmark at cellsize=1/8, P2/P1, m=-1, 8 MPI ranks using the unified writer path.
  • Confirmed unified files contain both XDMF/remap and PETSc reload payloads.
  • Confirmed the checkpoint-only metrics stage completes from the same unified files using read_checkpoint().
  • Serial reload comparison from the 8-rank output gave:
velocity max abs diff 0.0
pressure max abs diff 0.0

gthyagi added 3 commits May 30, 2026 04:23
Write visualization timesteps with PETSc HDF5_VIZ so generated XDMF references /viz/topology/cells rather than checkpoint-style DMPlex topology. Keep write_checkpoint on HDF5_PETSC for restart files.

Harden checkpoint_xdmf against invalid topology datasets, use integer topology precision from the HDF5 dataset, and write mesh variable DataItems with their direct dataset dimensions instead of HyperSlab wrappers.

Add regression tests covering write_timestep visualization topology, write_checkpoint restart topology, and 3D XDMF references.
Restore write_timestep mesh files to PETSc's default HDF5 layout instead of forcing HDF5_VIZ. This preserves labels and internal DMPlex restart datasets such as /topology/cells, /topology/cones, /topology/order, and /topology/orientation in output.mesh.00000.h5.

Keep the XDMF writer guarded so visualization still references /viz/topology/cells and never the internal PETSc /topology/cells connectivity. This keeps ParaView-compatible direct cell connectivity while retaining restart-like mesh contents.

Update the XDMF compatibility regression test to require both restart-style topology datasets and the /viz/topology/cells dataset from write_timestep output.
@gthyagi

gthyagi commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Additional validation after opening the PR:

  • Confirmed the checkout is on bugfix/petsc-hdf5-viz-topology.
  • Ran ./uw build in the configured amr-dev environment; build completed successfully.
  • Re-ran .pixi/envs/amr-dev/bin/python -m pytest -q tests/test_0005_xdmf_compat.py after the build: 10 passed.
  • Re-ran .pixi/envs/amr-dev/bin/mpirun -n 2 .pixi/envs/amr-dev/bin/python -m pytest -q --with-mpi tests/parallel/test_0005_xdmf_viz_topology_mpi.py after the build: 1 passed on each rank.

gthyagi added 2 commits May 30, 2026 05:14
Add a petsc_reload option to Mesh.write_timestep so timestep variable files can include PETSc DMPlex section/vector metadata alongside the existing XDMF, /fields, and vertex/cell compatibility payloads. This lets the same per-variable HDF5 files support both read_timestep remapping and read_checkpoint reloads.

Refactor PETSc reload writing into shared helpers and add create_xdmf support to write_checkpoint by routing through the unified timestep writer. Invalid option combinations for the timestep layout now fail explicitly.

Update checkpoint/reload developer documentation and add roundtrip tests covering unified write_timestep output and write_checkpoint(create_xdmf=True).
Emit a visible FutureWarning from Mesh.write_checkpoint() so new code is directed to the unified write_timestep(..., petsc_reload=True) API.

Update checkpoint/reload documentation to present write_timestep() as the standard output method and write_checkpoint() as a legacy compatibility wrapper. Adjust tests to assert the warning on compatibility calls.
@gthyagi

gthyagi commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up pushed in 5ab5cb40:

  • Mesh.write_checkpoint() now emits a visible FutureWarning and is documented as a compatibility wrapper.
  • New code is directed to the unified standard API: write_timestep(..., petsc_reload=True).
  • Developer docs now present write_timestep() as the single recommended output method, with create_xdmf and petsc_reload selecting the payloads.
  • Tests that intentionally exercise write_checkpoint() now assert the compatibility warning.

Validation after the change:

  • ./uw build
  • .pixi/envs/amr-dev/bin/python -m pytest -q tests/test_0003_save_load.py tests/test_0005_xdmf_compat.py16 passed
  • .pixi/envs/amr-dev/bin/mpirun -n 2 .pixi/envs/amr-dev/bin/python -m pytest -q --with-mpi tests/test_0003_save_load.py::test_timestep_with_petsc_reload_roundtrip → passed on both ranks

@gthyagi
gthyagi marked this pull request as ready for review May 30, 2026 10:59
@gthyagi
gthyagi requested a review from lmoresi as a code owner May 30, 2026 10:59
@lmoresi

lmoresi commented Jun 18, 2026

Copy link
Copy Markdown
Member

Review — approved ✅ (implements & closes #217)

Reviewed the full write/reload/XDMF path closely since this touches restart data integrity. Restart integrity is preserved, the default-format change is safe, and this satisfies #217's closure criteria.

  • Format pairing is consistent on every reload path. _write_petsc_reload_file pushes HDF5_PETSC; MeshVariable.read_checkpoint and mesh _from_plexh5 read HDF5_PETSC. The new default mesh-file layout (petsc_format=None) still carries the full DMPlex restart topology + labels + coordinates, so reload + the FMG-hierarchy round-trip hold. No write/read mismatch.
  • Default write() change is additive-safe. write(f) now yields PETSc default (restart + viz) instead of restart-only; write_checkpoint forces petsc_format=True where the old contract matters. Audited callers — none rely on viz-group absence.
  • XDMF correctness improved: topology DataItem precision now derives from cells.dtype.itemsize (the old hardcoded Precision="8" mistyped int topology); malformed files now fail loudly with a clear message.
  • Fix XDMF output to explicitly write cell-to-vertex topology and ensure strict ParaView compatibility #205 helpers fully removed (_write_mesh_viz_groups, _local_viz_cell_connectivity, _petsc_numbering_to_global_ids) with no dangling callers; the Fix XDMF output to explicitly write cell-to-vertex topology and ensure strict ParaView compatibility #205 viz-topology tests are preserved (updated to the native geometry/vertices path) and a parallel test was added.

Follow-ups (non-blocking — tracked separately, not gating this merge)

  1. write_checkpoint FutureWarning fires from UW3's own snapshot subsystem (disk_snapshot.py:314). Harmless today (setup.cfg filterwarnings has no error escalation), but the internal snapshot path should call _write_petsc_reload_file / write_timestep(..., petsc_reload=True) directly rather than the deprecated method.
  2. NIT: checkpoint_xdmf reads topo["cells"].attrs["cell_dim"] but never uses it — a needless dependency on a PETSc-internal attr name; delete the read.
  3. NIT: the swarmVar XDMF attribute block still uses the old HyperSlab form while meshVar was flattened — unify in a follow-up.

Merging for the release; closing #217.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit bc834b4 into underworldcode:development Jun 18, 2026
1 check passed
lmoresi added a commit that referenced this pull request Jun 19, 2026
…ng + drop dead cell_dim read (#252) (#256)

Follow-ups from the #218 review:
- disk_snapshot._write_disk_snapshot is a legitimate internal user of the
  user-deprecated write_checkpoint (it depends on the .mesh.00000.h5 /
  .<var>.00000.h5 filename convention its reload path reads). Wrap the call in
  warnings.catch_warnings()+ignore FutureWarning so persistent snapshots don't
  spam the deprecation warning. (Full migration to write_timestep tracked in #252.)
- checkpoint_xdmf read topo["cells"].attrs["cell_dim"] into cellDim but never
  used it (topology_precision now derives from cells.dtype.itemsize). Removed —
  it was a needless dependency on a PETSc-internal attr that could KeyError.

Verified: test_0007/0008 (snapshot) pass with -W error::FutureWarning (26/26);
test_0003/0005 (save_load/xdmf) green (39/39 total).

Item 3 (unify swarmVar HyperSlab XDMF block with the flattened meshVar form)
deferred: cosmetic, ParaView-sensitive, no easy local validation — left in #252.

Underworld development team with AI support from Claude Code
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