Add VTKHDF output support for all structured mesh types (#3620)#3877
Open
Jarvis2001 wants to merge 5 commits intoopenmc-dev:developfrom
Open
Add VTKHDF output support for all structured mesh types (#3620)#3877Jarvis2001 wants to merge 5 commits intoopenmc-dev:developfrom
Jarvis2001 wants to merge 5 commits intoopenmc-dev:developfrom
Conversation
391fb53 to
ede21b4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
PR #3252 added
.vtkhdfoutput forUnstructuredMesh. This PR extends that support to all four structured mesh types —RegularMesh,RectilinearMesh,CylindricalMesh, andSphericalMesh— following the same pattern. The legacy ASCII.vtkpath is unchanged.Changes
StructuredMesh.write_data_to_vtkvolume_normalizationdefault changed fromTruetoNone, with format-appropriate fallback:Falsefor.vtkhdfandTruefor legacy ASCII.vtk. The previous hardcodedTruedefault would silently callself.volumeson 1D/2D meshes, which raisesRuntimeErrorsince volumes are only defined for 3D meshes.Fixed unconditional
.T.ravel()in the ASCII path for 3D datasets. The transpose is now conditional:RegularMeshandRectilinearMeshstore data in C (ijk) order that matches the VTK writer directly — no transpose is needed or correct.CylindricalMeshandSphericalMeshstill require the transpose to produce Fortran (kji) order expected by the curvilinear VTK writer. The same condition applies tovolumesduring normalization.MeshMaterialVolumes.by_elementfrom_volumes) apply their own sort explicitly.MeshMaterialFilter.from_volumesnp.whereto scan the raw_materialsarray column-by-column, producing bin order dependent on which column the C library happened to write each material into. The fix explicitly sorts per-element bins by volume ascending (material ID as tiebreaker) before appending, making the output deterministic regardless of ray-traversal order.RegularMesh._write_vtk_hdf5(new method)StructuredGridVTKHDF format with explicitPoints,Dimensions, andCellData, consistent with the existingUnstructuredMeshVTKHDF writer.Typeattribute written as a fixed-length ASCII HDF5 string viah5py.string_dtype, matching theUnstructuredMeshpattern. Without this, h5py stores a variable-length string that reads back asstrrather thanbytes, breaking== b"StructuredGrid"comparisons in VTK readers.Dimensionscarries onlyndimentries (not padded to 3), so 1D and 2D meshes write the correct number of dimensions.datasets=None, calls_reshape_vtk_datasetbefore validation, and validates both shape and element count.RectilinearMesh._write_vtk_hdf5,CylindricalMesh._write_vtk_hdf5,SphericalMesh._write_vtk_hdf5(new methods)RegularMesh._write_vtk_hdf5. Points are computed from the respective coordinate grids: Cartesian forRectilinearMesh, converted from(r, φ, z)forCylindricalMesh, and from(r, θ, φ)forSphericalMesh, each respecting the meshorigin.vtkRectilinearGridis not part of the VTKHDF spec, soRectilinearMeshusesStructuredGridas the closest equivalent.New tests (
test_mesh.py)test_write_vtkhdf_regular_mesh— checksStructuredGridtype,Points,Dimensions, andCellDatastructure.test_write_vtkhdf_rectilinear_mesh— checks file is created andCellDatais populated.test_write_vtkhdf_cylindrical_mesh— checks vertexDimensionsmatch(nr+1, nφ+1, nz+1).test_write_vtkhdf_spherical_mesh— checksPointsandCellDataare present.test_write_vtkhdf_volume_normalization— verifies both normalised and unnormalised output against known cell volumes.test_write_vtkhdf_multiple_datasets— verifies multiple named datasets are written with correct data ordering (data.T.ravel()).test_write_vtkhdf_invalid_data_shape— verifiesValueErroris raised for shape mismatches.test_write_vtkhdf_1d_mesh— verifies a 1DRegularMeshwrites successfully without hitting the 3D-only volumes guard.test_write_vtkhdf_2d_mesh— verifies a 2DRegularMeshwrites the correct number ofDimensionsentries.test_write_ascii_vtk_unchanged— round-trip test confirming the legacy.vtkpath is unaffected by these changes.Fixes #3620
Checklist