From 48e0806b03ddb200a49d257a91903cf9b720c9fc Mon Sep 17 00:00:00 2001 From: cmdupuis3 Date: Thu, 10 Sep 2026 12:10:02 -0500 Subject: [PATCH] Preserve face order across an Exodus round trip Exodus element blocks are homogeneous, so `_encode_exodus` regroups a mixed mesh by face size. Nothing recorded that permutation, so the mesh reloaded reordered -- intact, but with any face-centered data now describing the wrong faces, and no error to say so. Carry each face's original position through the regrouping and write it to `elem_num_map`, then undo the permutation on read. Exodus defines `elem_num_map` as a user-facing element ID rather than a position, so the reader only honors it on files this writer produced, identified via `qa_records`, and only when the IDs form a genuine permutation. This restores the exact-order assertions in `test_mixed_exodus` and `test_grid_ugrid_exodus_roundtrip` that the previous commit had to relax. Co-Authored-By: Claude Opus 5 Co-Authored-By: Claude Opus 5.5 --- test/grid/grid/test_io.py | 15 +++------ test/io/test_exodus.py | 70 +++++++++++++++++++++++++++++++++++---- uxarray/io/_exodus.py | 46 +++++++++++++++++++++++-- 3 files changed, 112 insertions(+), 19 deletions(-) diff --git a/test/grid/grid/test_io.py b/test/grid/grid/test_io.py index 4e81b55a1..13a14d34e 100644 --- a/test/grid/grid/test_io.py +++ b/test/grid/grid/test_io.py @@ -6,11 +6,6 @@ from uxarray.constants import ERROR_TOLERANCE -def _face_rows(grid): - """Faces as a sorted list of node-index tuples, ignoring face order.""" - return sorted(tuple(row.tolist()) for row in grid.face_node_connectivity.values) - - def _assert_lon_close(actual, desired, err_msg): """Compare longitudes modulo 360, to an absolute tolerance. @@ -147,12 +142,10 @@ def test_grid_ugrid_exodus_roundtrip(gridpath): reloaded_ugrid.face_node_connectivity.values, err_msg=f"UGRID face connectivity mismatch for {grid_name}" ) - # Exodus blocks are homogeneous, so a mixed mesh (RLL1deg has polar - # triangles among its quads) is regrouped by face size on write and comes - # back permuted. Compare the faces as a set for now; recording and - # restoring the original order is a separate fix. - assert _face_rows(reloaded_exodus) == _face_rows(original_grid), ( - f"Exodus face connectivity mismatch for {grid_name}" + np.testing.assert_array_equal( + original_grid.face_node_connectivity.values, + reloaded_exodus.face_node_connectivity.values, + err_msg=f"Exodus face connectivity mismatch for {grid_name}" ) # Validate coordinate consistency with numerical tolerance diff --git a/test/io/test_exodus.py b/test/io/test_exodus.py index fbad04baf..78f658baf 100644 --- a/test/io/test_exodus.py +++ b/test/io/test_exodus.py @@ -2,14 +2,10 @@ import numpy as np import pytest import uxarray as ux +import xarray as xr from uxarray.constants import INT_DTYPE, INT_FILL_VALUE -def _face_rows(grid): - """Faces as a sorted list of node-index tuples, ignoring face order.""" - return sorted(tuple(row.tolist()) for row in grid.face_node_connectivity.values) - - def test_read_exodus(gridpath): """Read an exodus file and writes a exodus file.""" uxgrid = ux.open_grid(gridpath("exodus", "outCSne8", "outCSne8.g")) @@ -71,6 +67,67 @@ def test_encode_exodus_mixed_blocks(fill_value): assert values.min() >= 1, f"{name} holds an index below 1" assert values.max() <= uxgrid.n_node, f"{name} indexes a node that does not exist" + # Blocks are written grouped by type, so the original face order has to be + # recorded or face-centered data silently misaligns on the way back in. + assert "elem_num_map" in exo_ds + assert sorted(exo_ds["elem_num_map"].values.tolist()) == [1, 2, 3] + +def test_encode_exodus_mixed_round_trip_preserves_face_order(tmp_path): + """A regrouped mixed mesh reloads in the order it was handed to the writer. + + Exodus blocks are homogeneous, so writing a mixed mesh permutes the faces. + Nothing raises when that permutation is not undone -- the mesh is intact, just + reordered -- and any face-centered data then describes the wrong faces. + """ + face_node_connectivity = np.array([ + [0, 1, 2, 3], + [1, 4, 2, INT_FILL_VALUE], + [0, 3, 4, INT_FILL_VALUE], + ]) + uxgrid = ux.Grid.from_topology( + node_lon=np.array([0.0, 10.0, 10.0, 0.0, 20.0]), + node_lat=np.array([0.0, 0.0, 10.0, 10.0, 0.0]), + face_node_connectivity=face_node_connectivity, + fill_value=INT_FILL_VALUE, + ) + + # The quad sorts last, so an unrecorded regrouping is visible in row 0 + path = tmp_path / "mixed_order.exo" + uxgrid.to_xarray("Exodus").to_netcdf(path) + + np.testing.assert_array_equal( + ux.open_grid(path).face_node_connectivity.values, face_node_connectivity + ) + +def test_read_exodus_ignores_third_party_elem_num_map(gridpath, tmp_path): + """Element IDs from another writer must not reorder the mesh. + + ``_encode_exodus`` records the original face order in ``elem_num_map`` to undo + its own block grouping. Exodus defines that variable as a user-facing ID, which + a third-party file is free to renumber, so the reader only honors it on files + it wrote -- identified via ``qa_records``. + """ + source = gridpath("exodus", "mixed", "mixed.exo") + original = ux.open_grid(source) + + # Same mesh, same blocks, same connectivity -- only the element IDs change + with xr.open_dataset(source) as ds: + renumbered = ds.load() + n_elem = renumbered["elem_num_map"].sizes["num_elem"] + renumbered["elem_num_map"][:] = np.arange(n_elem, 0, -1) + + assert b"uxarray" not in np.asarray( + renumbered["qa_records"].values, dtype="S" + ).ravel(), "fixture is no longer a third-party file" + + path = tmp_path / "mixed_renumbered.exo" + renumbered.to_netcdf(path) + + np.testing.assert_array_equal( + ux.open_grid(path).face_node_connectivity.values, + original.face_node_connectivity.values, + ) + def test_mixed_exodus(gridpath): """Read/write an exodus file with two types of faces (triangle and quadrilaterals) and writes a ugrid file.""" uxgrid = ux.open_grid(gridpath("exodus", "mixed", "mixed.exo")) @@ -86,7 +143,8 @@ def test_mixed_exodus(gridpath): # Face node connectivity comparison assert np.array_equal(ugrid_load_saved.face_node_connectivity.values, uxgrid.face_node_connectivity.values) - assert _face_rows(exodus_load_saved) == _face_rows(uxgrid) + + assert np.array_equal(uxgrid.face_node_connectivity.values, exodus_load_saved.face_node_connectivity.values) # Node coordinates comparison assert np.array_equal(ugrid_load_saved.node_lon.values, uxgrid.node_lon.values) diff --git a/uxarray/io/_exodus.py b/uxarray/io/_exodus.py index a93946310..b3e703fc8 100644 --- a/uxarray/io/_exodus.py +++ b/uxarray/io/_exodus.py @@ -9,6 +9,23 @@ from uxarray.grid.connectivity import _replace_fill_values from uxarray.grid.coordinates import _lonlat_rad_to_xyz, _xyz_to_lonlat_deg +# Producer name written into (and looked for in) "qa_records" +_WRITER_NAME = "uxarray" + + +def _written_by_uxarray(ext_ds): + """Whether ``ext_ds`` was produced by :func:`_encode_exodus`. + + Exodus defines ``elem_num_map`` as each element's user-facing ID, not its + original position, so only files this writer produced can be reordered from + it. A third-party file keeps the face order it was written with. + """ + if "qa_records" not in ext_ds: + return False + + records = np.asarray(ext_ds["qa_records"].values, dtype="S").ravel() + return _WRITER_NAME.encode() in records + # Exodus Number is one-based. def _read_exodus(ext_ds): @@ -91,6 +108,18 @@ def _read_exodus(ext_ds): else: face_nodes = np.vstack(padded_blocks) + if "elem_num_map" in ext_ds and _written_by_uxarray(ext_ds): + # _encode_exodus groups elements into blocks by face size, permuting a + # mixed mesh, and records each original position here. Undo that, but only + # for a genuine permutation -- Exodus also allows arbitrary IDs. + elem_num_map = ext_ds["elem_num_map"].values.astype(INT_DTYPE) - 1 + if elem_num_map.shape == (face_nodes.shape[0],) and np.array_equal( + np.sort(elem_num_map), np.arange(face_nodes.shape[0]) + ): + unpermuted = np.empty_like(face_nodes) + unpermuted[elem_num_map] = face_nodes + face_nodes = unpermuted + # standardize fill values and data type face nodes face_nodes = _replace_fill_values( grid_var=xr.DataArray(face_nodes - 1), # Wrap numpy array in a DataArray @@ -174,7 +203,7 @@ def _encode_exodus(ds, outfile=None): # --- QA Records --- ux_exodus_version = "1.0" - qa_records = [["uxarray"], [ux_exodus_version], [date], [time]] + qa_records = [[_WRITER_NAME], [ux_exodus_version], [date], [time]] exo_ds["qa_records"] = xr.DataArray( data=np.array(qa_records, dtype="S"), dims=["num_qa_rec", "four"], @@ -213,7 +242,13 @@ def _encode_exodus(ds, outfile=None): conn_nofill.append(row.astype(int).tolist()) num_blks = np.count_nonzero(num_el_all_blks) - conn_nofill.sort(key=len) + + # Exodus element blocks are homogeneous, so a mixed mesh has to be regrouped + # by face size. Sort stably and carry each face's original position along, so + # the ordering can be written out below and restored on read. + block_order = sorted(range(len(conn_nofill)), key=lambda i: len(conn_nofill[i])) + conn_nofill = [conn_nofill[i] for i in block_order] + nonzero_el_index_blks = np.nonzero(num_el_all_blks)[0] start = 0 @@ -242,6 +277,13 @@ def _encode_exodus(ds, outfile=None): # Correctly increment the start index for the next block start += num_elem_in_blk + # Record where each written element came from in the original face ordering. + # Without this a mixed mesh comes back permuted, silently misaligning any + # face-centered data with the faces it describes. + exo_ds["elem_num_map"] = xr.DataArray( + data=np.asarray(block_order, dtype=np.int64) + 1, dims=["num_elem"] + ) + # --- Element Block Properties --- prop1_vals = np.arange(1, num_blks + 1, 1, dtype=np.int32) exo_ds["eb_prop1"] = xr.DataArray(