Skip to content

Write compact dimensional XDMF fields with optional native checkpoints - #756

Merged
lmoresi merged 11 commits into
underworldcode:developmentfrom
gthyagi:feature/dimensional-xdmf-output
Oct 13, 2026
Merged

lmoresi merged 11 commits into
underworldcode:developmentfrom
gthyagi:feature/dimensional-xdmf-output

Conversation

@gthyagi

@gthyagi gthyagi commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Unit-aware output currently duplicates mesh-variable values across native /fields and ParaView compatibility groups. Higher-order continuous fields are always reduced to P1, including P2 simplex fields that XDMF and ParaView can represent directly. Benchmark scripts then need their own field-to-unit mappings or HDF5 postprocessing, which can produce inconsistent dimensional values.

Resulting behavior

Mesh.write_timestep(create_xdmf=True) now treats dimensional /fields as the authoritative analysis and coordinate-reload data:

  • Continuous P1 and DG0 values are referenced directly from /fields.
  • Continuous P2 triangle and tetrahedron fields use XDMF Triangle_6 and Tetrahedron_10 connectivity, with UW3 nodes mapped to VTK corner/edge order. No P1 value copy is written.
  • Simplex DG1 fields store one exact dimensional element-corner value array under /fields. All DG1 variables share a single disconnected geometry under /viz/dg1 in the mesh file and appear as attributes on one XDMF DG1 grid. No per-variable visualization geometry or second value array is written.
  • MeshVariable.read_timestep() recognizes the compact DG1 representation, matches complete source and destination cells by geometry, reconstructs each local affine polynomial, and evaluates it at the native DG1 nodes. This preserves separate traces at coincident vertices and works across MPI repartitioning.
  • Continuous P3+ fields and unsupported P2 layouts receive one compact P1 visualization reduction under /visualization.
  • DG2+ fields and unsupported DG1 layouts receive one compact DG0 visualization reduction under /visualization.
  • Tensor values retain UW3's compact component count rather than adding a nine-component copy.
  • The old /vertex_fields, /cell_fields, and /dg1 groups are no longer generated for mesh variables.

When nondimensional scaling is active, /fields/coordinates, /fields/<name>, and shared visualization geometry are written in the mesh and variable units declared by the model. MeshVariable.read_timestep() recognizes this metadata and converts the data back to the active nondimensional solver frame. P2 cell connectivity only indexes the native coordinate/value arrays, so VTK node ordering does not affect KDTree reload. Scalar model_to_physical_scale attributes preserve the writer conversion factors for coordinates and fields. When no destination reference quantities are set, read_timestep() restores the writer solver frame using these factors, including DG1 corner values. An active destination units model continues to define the reload scaling. Older physical files without these attributes require the writer reference quantities to be set before reading.

petsc_reload=True writes one native nondimensional PETSc global vector plus DMPlex section metadata under /restart/petsc. same_layout=True reads the nested vector through a standalone PETSc vector, while the default reader uses globalVectorLoad with PETSc's migration SF for a reconstructed mesh or MPI layout. The reader also accepts the former /uw_checkpoint group so existing restart files remain readable.

With create_xdmf=False, petsc_reload=True, each variable file contains only /restart/petsc; /fields is not written first. With both flags enabled, the file contains dimensional /fields for analysis/remap and /restart/petsc for exact restart.

The mesh file keeps PETSc's native /geometry for restart and, when dimensional scaling is active, a physical /viz/geometry array for XDMF. XDMF 3 function-based geometry scaling was tested but is not accepted by ParaView 6, so this small coordinate-only copy remains. Simplex DG1 adds one shared disconnected /viz/dg1 geometry because XDMF needs independent vertices to display element-local jumps correctly.

Surface signed and unsigned distance fields inherit the mesh coordinate units, so geometric diagnostics are exported in physical length units automatically.

Storage summary

Field layout Reload/analysis data XDMF data
Continuous P1 Native dimensional /fields Direct /fields nodes
Continuous P2 triangle/tetrahedron Native dimensional /fields Direct /fields with Triangle_6/Tetrahedron_10
DG0 Native dimensional /fields Direct /fields cells
DG1 triangle/tetrahedron Exact dimensional corner values in /fields Same values plus one shared /viz/dg1 geometry
Continuous P3+ or unsupported P2 Native dimensional /fields P1 /visualization reduction
DG2+ or unsupported DG1 Native dimensional /fields DG0 /visualization reduction

For restart-only output, the variable file contains one native global vector under /restart/petsc and no /fields or /visualization group.

Upstream alignment and scope

Merged upstream/development at 4a4f18d8 after #709 and #736 landed and resolved the overlapping DG1 changes. The compact layout builds on #736's element-local DG1 foundation and uses dimensional /fields, shared mesh geometry, and optional /restart/petsc data.

PETSc/HDF5 mesh, variable, dimensional-field, geometry and restart writes and native readers now use the existing _short_io_path workaround through a shared utilities/_io.py helper. PETSc receives the basename while the public output paths and XDMF references retain their requested locations. Snapshot I/O uses the same helper. This extends the workaround for #645; it does not fix PETSc itself or change filesystem filename limits.

Field and coordinate units/conversion factors are recorded. Simulation-time conversion and FreeSurface time metadata are not recorded here: index remains an output identifier. #562 remains open. This PR does not claim to resolve refined-mesh checkpoint issues #690/#809 or snapshot migration #252.

Validation

Current revision, macOS arm64 / PETSc 3.25.0 / Open MPI 5.0.10:

  • Save/load, XDMF compatibility, DG1, physical units and long-path suites: 35 tests passed in serial and 35 passed per rank on two MPI ranks.
  • Long-path, DG1, physical-unit and visualization-topology suites: 17 tests passed per rank on four MPI ranks. The dedicated two-rank visualization-topology test also passed.
  • Long-path tests cover absolute/relative paths, dimensional geometry, P2/P3, DG0/DG1, coordinate remap, same-layout PETSc reload and reconstructed-mesh PETSc reload. The native-basename assertion fails when the old unguarded filename behavior is restored.
  • Broader snapshot-format suite: 26 passed and one failed. test_disk_restore_refreshes_packed_auxiliary_fields_before_solve fails at its initial Poisson solve, before snapshot I/O: generated JIT setup has include_dirs=[] and compilation cannot find petscsystypes.h. Reproduced using unmodified upstream Python source at 4a4f18d8 with the matching compiled extensions; the JIT code is unchanged in this PR.
  • Build/import and installed-source verification passed. New helper/tests pass Black; the PR diff passes git diff --check.

Earlier visualization/repartitioning checks (not rerun for this path-only update):

  • DG1 coordinate reload from one to two ranks and two to one rank: maximum error 1.7763568394002505e-15.
  • ParaView 6.0.0 loaded one shared DG1 grid and exact P2 triangles/tetrahedra as vtkQuadraticTriangle / vtkQuadraticTetra (cell types 22 / 24).

Underworld development team with AI support from OpenAI Codex.

Write independent physical vertices and cell-local linear field traces for DG1 triangles and tetrahedra. Use native cell maps and owned-cell PETSc output to preserve discontinuities without coordinate matching or inter-element averaging.

Expose continuous and DG1 fields as separate grids in one XDMF file, support scalar/vector/tensor layouts, and retain native checkpoint data unchanged. Reject unsupported DG visualization explicitly while preserving native-only output.

Add serial and MPI regression coverage for affine fields, jumps, orientation, ownership, tensor packing, and checkpoint reload. Document supported layouts and ParaView usage. Validated 24 focused serial tests, four DG tests on eight ranks, and actual ParaView 2D/3D reads.
Keep mesh geometry and field checkpoint datasets in the nondimensional solver frame, and create dimensional visualization copies using declared mesh and variable units. Record those units in HDF5 and XDMF metadata.

Use nondimensional coordinates for DG1 interpolation before converting its disconnected visualization grid. This removes the need for benchmark-side coordinate wrappers, field-unit mappings, and HDF5 postprocessing.

Cover continuous P2, DG0, and DG1 output in serial and MPI tests.

Underworld development team with AI support from Claude Code
@gthyagi
gthyagi requested a review from lmoresi as a code owner September 18, 2026 05:14
Declare signed and unsigned Surface distance variables with the mesh coordinate unit so physical XDMF export scales geometric distance fields without benchmark-side mappings.

Extend the physical-output regression to cover Surface.abs_distance while preserving its nondimensional checkpoint values.

Underworld development team with AI support from Claude Code
Make dimensional /fields datasets authoritative for analysis and direct XDMF rendering. Export continuous P2 triangles with Triangle_6 connectivity and DG1 simplices with exact disconnected-corner basis conversion, while reducing only unsupported continuous P3+ and DG2+ layouts to compact P1 and DG0 visualization arrays.\n\nStore optional native PETSc restart data under /uw_checkpoint, reuse its existing variable vector for same-layout reload, and retain migration-based reload for reconstructed meshes. Teach read_timestep to convert dimensional fields back to the active model frame and reject DG1 corner data that cannot be inverted by nearest-neighbour remapping.\n\nReplace compatibility-group tests with serial and MPI coverage for direct layouts, high-order reductions through P4/DG4, dimensional units, ParaView-facing topology, and checkpoint round trips. Update checkpoint documentation for the new storage contract.
@gthyagi gthyagi changed the title Write dimensional XDMF output while preserving native checkpoints Write compact dimensional XDMF fields with optional native checkpoints Sep 18, 2026
Keep dimensional DG1 interpolation coordinates and values under /fields so read_timestep can recover solver data exactly. Store the disconnected-corner basis conversion separately under /visualization for XDMF, where it preserves element geometry and discontinuous traces.\n\nRemove the DG1 read_timestep rejection and add serial and two-rank MPI coverage for coordinate reload, PETSc checkpoint reload, dimensional conversion, and ParaView rendering. Update the checkpoint documentation to explain why DG1 requires separate reload and visualization representations.
Write continuous P2 tetrahedra as VTK-ordered Tetrahedron_10 grids so ParaView can render the native quadratic field without a P1 reduction. Keep field coordinates and values in their native order so coordinate-based reload remains independent of visualization connectivity.

For create_xdmf=False with petsc_reload=True, write only the DMPlex checkpoint payload instead of first duplicating values under /fields. Store one owned global vector and reload it directly for the same layout or through PETSc's migration SF for reconstructed layouts and different MPI decompositions.

Add serial and MPI coverage for quadratic tetrahedra, checkpoint-only file structure, same-layout reload, and migrated reload.
Write PETSc-native DMPlex restart data under /restart/petsc so the HDF5 hierarchy states both the payload purpose and storage backend.

Keep read compatibility with grouped and direct-vector files that use the former /uw_checkpoint path, and cover the compatibility path in serial and MPI tests.
Inline the base-grid topology and geometry HDF5 DataItems in generated XDMF instead of relying on XML Reference nodes that ParaView 6's XDMF3 reader rejects. Keep the shared mesh DataItems for compatibility while making both the XDMF3 and legacy readers load exact P2 and DG1 output. Add a regression assertion that generated compact XDMF contains no XML-reference DataItems.
Store simplex DG1 corner geometry and connectivity once in the mesh HDF5 file, while retaining only each variable's exact dimensional corner values under /fields. Emit one XDMF DG1 grid with all compatible variables to remove per-variable visualization geometry duplication.\n\nTeach read_timestep to identify the compact corner-nodal representation and reconstruct native DG1 values cell by cell. Match complete cell geometry rather than coincident vertices so discontinuous traces remain distinct and reload remains exact across MPI repartitioning.\n\nUpdate DG1 and physical-unit tests to cover shared geometry, compact field files, the absence of optional restart data, XDMF references, and exact remapping.
Save scalar model-to-physical factors with field and coordinate datasets. Use destination reference quantities when present, otherwise restore the writer solver frame, including shared DG1 corners. Add analytic DG0, P1 and DG1 cross-session regressions.

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Oct 6, 2026

Copy link
Copy Markdown
Member

Issues in this branch's territory

Territory is the checkpoint/XDMF output path: discretisation_mesh.py, discretisation_mesh_variables.py, field_projection.py, meshing/surfaces.py, and the test_0003_save_load / test_0005_xdmf_* family.

issue verdict
#690 reloading a refinement>=1 checkpoint gives a corrupt DM still live — the reload path; this PR is the write path plus an optional native checkpoint
#809 a refined mesh reloaded from its checkpoint joins cells to the wrong vertices still live, same family as #690
#562 FreeSurface records hscale but not tscale, so a published checkpoint cannot be evaluated at matched physical time worth checking against this PR — it writes dimensional fields, which is adjacent to the scale-recording question, but the issue is about the FreeSurface metadata rather than the field values. Still live unless the author says otherwise.
#645 long HDF5 paths segfault in parallel PETSc output still live as a docs item; checkpoint/disk_snapshot.py already wraps its writes in _short_io_path, and if this PR's new output path does not, that is worth a look before merge
#252 route the snapshot subsystem off deprecated write_checkpoint still live — deliberately deferred in place with a suppression and a comment

The two items I would want the author's word on before this lands: whether the new output path goes through _short_io_path (#645), and whether tscale is now recorded (#562).

Underworld development team with AI support from Claude Code

@lmoresi

lmoresi commented Oct 6, 2026

Copy link
Copy Markdown
Member

Two follow-ups to the adjudication above, both answered by grep rather than left for the author, and one new blocker.

It does not go through _short_io_path. gh pr diff 756 | grep _short_io_path returns nothing, while the new path writes HDF5 directly (globalVectorView, pushGroup("/restart/petsc")). checkpoint/disk_snapshot.py wraps its writes in that helper precisely because long generated filenames segfault PETSc's HDF5 writer in parallel (#645). A new output path that builds longer names — /restart/petsc groups, per-variable files, dimensional suffixes — is the case that helper exists for. Worth a look before this lands.

It does not record tscale. No + line mentions tscale or hscale, so #562 stays open and untouched.

And it now conflicts. #756 was MERGEABLE an hour ago. Merging #709 and #736 touched src/underworld3/discretisation/discretisation_mesh.py and src/underworld3/function/field_projection.py, which this PR also changes, so it needs git merge origin/development before it can go in. That is not a criticism of the PR — it is the cost of a queue this deep, and it is why the remaining ones want merging one at a time with a re-check between.

Underworld development team with AI support from Claude Code

Merge upstream/development at 4a4f18d and resolve the overlapping DG1
export changes while preserving this PR's compact dimensional /fields,
shared mesh geometry, optional /restart/petsc, and DG1 affine reload.
Retain upstream's vector and symmetric-tensor regression coverage.

Move the existing _short_io_path context manager into utilities/_io.py
and reuse it from snapshots, mesh and variable output, dimensional field
and geometry output, and PETSc-native mesh/vector/checkpoint readers.
Native PETSc receives only the basename while artifacts stay at the
requested location; the original working directory is restored on exit.

Add long-path regression coverage for absolute and relative paths,
visualization-only, restart-only, and combined output, continuous P2/P3,
DG0/DG1, physical geometry, coordinate remap, and both PETSc reload modes.
The test instruments native viewer creation so it detects unguarded
filenames even on platforms that accept them. Its basename assertion
fails when the pre-fix unguarded filename behavior is restored.

Correct the DG1 storage documentation and explicitly document that the
output index is not physical time. Field and coordinate conversion
metadata does not resolve the FreeSurface time-scale issue underworldcode#562.

Validation:
- Output/save-load suites: 35 tests pass in serial and per rank with 2 MPI ranks.
- Long-path, DG1, physical-unit and topology checks: 17 pass per rank with 4 MPI ranks.
- Dedicated 2-rank visualization topology check passes.
- Broader snapshot suite: 26 pass; the auxiliary-field solve test fails
  before I/O because generated JIT setup has an empty include_dirs list.
  Reproduced with unmodified upstream Python source at 4a4f18d using the
  matching compiled extensions; unrelated JIT code is unchanged here.
- Installed source verified; new helper/tests pass Black and diff checks.

Underworld development team with AI support from OpenAI Codex
@gthyagi

gthyagi commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in 16d660c:

  • Merged upstream/development at 4a4f18d8 and resolved the overlapping changes from Add MPI-complete direct scalar boundary-flux integrals #709/Fix DG1 XDMF export with element-local simplex topology #736. GitHub now reports the PR as mergeable.
  • Moved the existing _short_io_path helper into utilities/_io.py and applied it to PETSc mesh/variable output, dimensional field and geometry output, optional /restart/petsc writes, and native mesh/vector/checkpoint readers. Snapshot I/O uses the same helper. PETSc receives only the filesystem basename; output locations and XDMF references are preserved.
  • Added regression tests for absolute/relative long paths and visualization-only, restart-only and combined output. They audit the native viewer filename, so they fail without the guard even on a stack that accepts long paths. Both coordinate reload and PETSc reload (same layout and reconstructed mesh) are checked.

Validation: 35 output/save-load tests pass in serial and per rank on two MPI ranks; 17 long-path/DG1/physical-unit/topology checks pass per rank on four MPI ranks. The dedicated two-rank topology test also passes.

On #562: this PR records field/coordinate units and conversion factors, but does not record simulation-time conversion or FreeSurface time metadata. The writer docstring now states that index is an output identifier, not physical time. #562 stays open; no claim to resolve #690, #809 or #252 either. The long-path protection extends the #645 workaround rather than fixing PETSc's underlying failure.

One broader validation limitation is recorded in the PR: the snapshot auxiliary-field solve test fails at its first Poisson solve, before any snapshot I/O, because generated JIT setup has an empty include_dirs list (petscsystypes.h not found). I reproduced that with unmodified upstream Python source at 4a4f18d8 and matching compiled extensions. The JIT implementation is unchanged here; the other 26 snapshot-format tests pass.

Underworld development team with AI support from OpenAI Codex

@lmoresi

lmoresi commented Oct 7, 2026

Copy link
Copy Markdown
Member

Both follow-ups resolved — one by you, one by us misreading the PR.

The long-path half is fixed. a9c… ("Merge development and protect dimensional HDF5 output from long paths", 2026-10-06T06:05) hoists _short_io_path into a shared src/underworld3/utilities/_io.py and routes the mesh, variable, dimensional-field, geometry, restart and snapshot writes through it — eight call sites in the diff. Our note was posted at 03:21 and was accurate then; it is not now. #645 stays open as a documentation item, which is what it is.

The tscale half we should not have raised. The PR body already says it: "Simulation-time conversion and FreeSurface time metadata are not recorded here: index remains an output identifier. #562 remains open." That is a verdict, stated in the right place, and we asked for it again as though it were a gap. Our mistake — the body carries explicit non-claims for #562, #690, #809 and #252, which is exactly the discipline the adjudication rule asks for.

One thing that does need attention before merge, not a review point: this went CONFLICTING when #709 and #736 merged, both of which touch discretisation/discretisation_mesh.py and function/field_projection.py. A merge from development and it is ready.

Underworld development team with AI support from Claude Code

@lmoresi
lmoresi merged commit 521a75b into underworldcode:development Oct 13, 2026
2 checks passed
lmoresi added a commit that referenced this pull request Oct 13, 2026
…ayload-640

Find the split-checkpoint payload wherever the writer put it (#640 regressed by #756)
lmoresi added a commit to gthyagi/underworld3 that referenced this pull request Oct 13, 2026
…rldcode#640)

underworldcode#756 moved the reload payload from the file root into /restart/petsc.
The writer still emits the per-field section AND its global vector, one
group deeper -- both are on disk -- but the reader's existence check
looked only at the root, so it reported "no PETSc-native payload" for a
file written WITH petsc_reload=True. The underworldcode#640 guard then refused the
good path along with the bad, telling the caller to re-write the file
with the flag they had already used, and split-mesh checkpoints could
not be read back at all.

Measured: tests/test_0864_split_checkpoint_roundtrip.py is 5 passed at
42f9347 (the merge before underworldcode#756) and 1 failed at f414da6 (after it).
It is tier_a and reachable from scripts/test.sh, so CI covers it.

Two reader-side changes, both in the guard's inputs:

- _petsc_payload_name searches "topologies" and
  "restart/petsc/topologies". It is only an existence check, and a file
  written by either version has to stay readable.
- _guard_coincident_dofs no longer assumes a v1.1 file has no payload.
  It looked the payload up only for legacy files while read_timestep
  calls _read_native_payload on a v1.1 file immediately afterwards, so
  the guard refused the very files the native path was about to read
  correctly. The saved-vs-live row comparison stays legacy-only: v1.1
  shares one compact geometry across fields (underworldcode#756), so there is no
  per-field saved cloud and saved_rows is 0 by construction.

New test, tier B because it is proven: with the single-location lookup
restored, test_the_payload_is_found_wherever_the_writer_put_it fails and
so does the round-trip test; with the fix both pass. It asserts the
section and the vector are on disk and that the existence check finds
them, so a future relocation fails with the reason rather than as a
confusing refusal.

test_0864 6 passed. With test_0005, test_0007, test_0008, test_0012 and
test_0020 (the checkpoint and snapshot suite): 50 passed.
check_test_coverage.py: all 352 files reachable.

Underworld development team with AI support from Claude Code

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E87Q7KrpapxeQiLD1RiNXv
gthyagi pushed a commit to gthyagi/underworld3 that referenced this pull request Nov 10, 2026
…joint underworldcode#744; xdmf underworldcode#756)

Conflicts resolved with both sides kept where they meet:

- fault_split: development's region-based redistribution (groups) carries
  the pairing through as labels when asked; add_fault takes cut / exclude /
  blind / across_seams and the 3-D placement options; the seam-normal
  override lives in the mesh-level pair helper, so coarse levels see it.
- Mesh.add_fault: development's options plus split_levels; the level replay
  applies to cut faults only (a placed band's spines have no counterpart on
  the levels), runs finest-down, and stops with a warning at a level the
  faults cannot be carried onto (a one-cell junction ligament closes at the
  coarser spacing), leaving that level and those beneath welded.
- custom_mg: the cell-located transfer and the orphan drop apply only when a
  COARSE level is split; a hierarchy whose only split level is the finest
  keeps development's builders, orphan repair and FAC patch.
- rotated_bc: the same zeroRows-by-global-row fix on both sides, one copy.
- Thin-sheet conflicts against development (solver Cython warm-up, the
  checkpoint read, field projection): development's code with the
  thin-sheet additions.

Merged build: tests 1086, 1087, 1089 (np 2), 1083, 0845-0847, 1017, 1022,
0865 (np 2) and 0866 pass.

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