Skip to content

Commit ad4e817

Browse files
Fixing bug in uxgrid index_search
This makes sure shapes stay consistent for single-face queries, and adds a small safeguard against zero norms
1 parent c2f148b commit ad4e817

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/parcels/_core/index_search.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,20 @@ def uxgrid_point_in_cell(grid, y: np.ndarray, x: np.ndarray, yi: np.ndarray, xi:
239239
axis=-1,
240240
)
241241

242-
# Get projection points onto element plane
243-
# for the projection, all points are computed relative to v0
244-
r1 = np.squeeze(face_vertices[:, 1, :] - face_vertices[:, 0, :]) # (M,3)
245-
r2 = np.squeeze(face_vertices[:, 2, :] - face_vertices[:, 0, :]) # (M,3)
242+
# Get projection points onto element plane. Keep the leading
243+
# dimension even for single-face queries so shapes remain (M,3).
244+
r1 = face_vertices[:, 1, :] - face_vertices[:, 0, :]
245+
r2 = face_vertices[:, 2, :] - face_vertices[:, 0, :]
246246
nhat = np.cross(r1, r2)
247247
norm = np.linalg.norm(nhat, axis=-1)
248+
# Avoid division by zero for degenerate faces
249+
norm = np.where(norm == 0.0, 1.0, norm)
248250
nhat = nhat / norm[:, None]
249251
# Calculate the component of the points in the direction of nhat
250-
ptilde = points - np.squeeze(face_vertices[:, 0, :])
252+
ptilde = points - face_vertices[:, 0, :]
251253
pdotnhat = np.sum(ptilde * nhat, axis=-1)
252254
# Reconstruct points with normal component removed.
253-
points = ptilde - pdotnhat[:, None] * nhat + np.squeeze(face_vertices[:, 0, :])
255+
points = ptilde - pdotnhat[:, None] * nhat + face_vertices[:, 0, :]
254256

255257
else:
256258
nids = grid.uxgrid.face_node_connectivity[xi].values

src/parcels/_core/uxgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def search(self, z, y, x, ei=None, tol=1e-6):
103103
if np.any(ei):
104104
indices = self.unravel_index(ei)
105105
fi = indices.get("FACE")
106-
is_in_cell, coords = uxgrid_point_in_cell(self.uxgrid, y, x, fi, fi)
106+
is_in_cell, coords = uxgrid_point_in_cell(self, y, x, fi, fi)
107107
y_check = y[is_in_cell == 0]
108108
x_check = x[is_in_cell == 0]
109109
zero_indices = np.where(is_in_cell == 0)[0]

0 commit comments

Comments
 (0)