Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions test/grid/grid/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
70 changes: 64 additions & 6 deletions test/io/test_exodus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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"))
Expand All @@ -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)
Expand Down
46 changes: 44 additions & 2 deletions uxarray/io/_exodus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Loading