Skip to content

Commit c24a153

Browse files
egallmeierphilipc2
andauthored
Fix edge_face_distance functions in neighbors.py. Add unit test in test_neighbors.py (#1293)
* changed from using nodes to face values * added a test for construct_edge_face_distances * adjusted to np arrays in test_neighbors.py * fixed expected values in test_quad_hex in test_gradient --------- Co-authored-by: Philip Chmielowiec <[email protected]>
1 parent 5c74b0c commit c24a153

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

test/test_gradient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_quad_hex():
5353
else:
5454
assert grad.values[i] != 0
5555

56-
expected_values = np.array([27.95, 20.79, 28.96, 0, 0, 0, 0, 60.64, 0, 86.45, 0, 0, 0, 0, 0, 0, 0, 0, 0])
56+
expected_values = np.array([28.963, 13.100, 14.296, 0, 0, 0, 0, 67.350, 0, 85.9397, 0, 0, 0, 0, 0, 0, 0, 0, 0])
5757
nt.assert_almost_equal(grad.values, expected_values, 1e-2)
5858

5959
def test_normalization():

test/test_neighbors.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import xarray as xr
55
from pathlib import Path
66
import uxarray as ux
7+
from uxarray.grid.neighbors import _construct_edge_face_distances
78

89
current_path = Path(os.path.dirname(os.path.realpath(__file__)))
910

@@ -157,3 +158,30 @@ def test_kdtree_multi_point_query():
157158
for i, cur_c in enumerate(c):
158159
single_ind = uxgrid.get_kd_tree(coordinates="nodes").query_radius(cur_c, 45)
159160
assert np.array_equal(single_ind, multi_ind[i])
161+
162+
def test_construct_edge_face_distances():
163+
"""
164+
Test _construct_edge_face_distances by verifying known great-circle distances
165+
between face centers on a unit sphere.
166+
"""
167+
face_lon = np.array([0, 0, 90, 90, -45])
168+
face_lat = np.array([0, 90, 0, 90, 0])
169+
edge_faces = np.array([
170+
[0, 1], # from (0,0) to (0,90)
171+
[0, 2], # from (0,0) to (90,0)
172+
[1, 3], # from (0,90) to (90,90) — both poles, same point
173+
[2, 4], # from (90,0) to (-45,0)
174+
])
175+
176+
177+
# Expected great-circle distances in radians
178+
expected = np.array([
179+
np.pi / 2, # 0 → 1
180+
np.pi / 2, # 0 → 2
181+
0.0, # 1 → 3
182+
3 * np.pi / 4 # 2 → 4
183+
])
184+
185+
# Run the function under test
186+
calculated = _construct_edge_face_distances(face_lon, face_lat, edge_faces)
187+
np.testing.assert_array_almost_equal(calculated, expected, decimal=5)

uxarray/grid/neighbors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def _construct_edge_node_distances(node_lon, node_lat, edge_nodes):
10971097
def _populate_edge_face_distances(grid):
10981098
"""Populates ``edge_face_distances``"""
10991099
edge_face_distances = _construct_edge_face_distances(
1100-
grid.node_lon.values, grid.node_lat.values, grid.edge_face_connectivity.values
1100+
grid.face_lon.values, grid.face_lat.values, grid.edge_face_connectivity.values
11011101
)
11021102

11031103
grid._ds["edge_face_distances"] = xr.DataArray(
@@ -1110,19 +1110,19 @@ def _populate_edge_face_distances(grid):
11101110

11111111

11121112
@njit(cache=True)
1113-
def _construct_edge_face_distances(node_lon, node_lat, edge_faces):
1113+
def _construct_edge_face_distances(face_lon, face_lat, edge_faces):
11141114
"""Helper for computing the arc-distance between faces that saddle a given
11151115
edge."""
11161116

11171117
saddle_mask = edge_faces[:, 1] != INT_FILL_VALUE
11181118

11191119
edge_face_distances = np.zeros(edge_faces.shape[0])
11201120

1121-
edge_lon_a = np.deg2rad((node_lon[edge_faces[saddle_mask, 0]]))
1122-
edge_lon_b = np.deg2rad((node_lon[edge_faces[saddle_mask, 1]]))
1121+
edge_lon_a = np.deg2rad((face_lon[edge_faces[saddle_mask, 0]]))
1122+
edge_lon_b = np.deg2rad((face_lon[edge_faces[saddle_mask, 1]]))
11231123

1124-
edge_lat_a = np.deg2rad((node_lat[edge_faces[saddle_mask, 0]]))
1125-
edge_lat_b = np.deg2rad((node_lat[edge_faces[saddle_mask, 1]]))
1124+
edge_lat_a = np.deg2rad((face_lat[edge_faces[saddle_mask, 0]]))
1125+
edge_lat_b = np.deg2rad((face_lat[edge_faces[saddle_mask, 1]]))
11261126

11271127
# arc length
11281128
edge_face_distances[saddle_mask] = np.arccos(

0 commit comments

Comments
 (0)