Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorize coordinates._construct_face_centroids() #1117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
17 changes: 8 additions & 9 deletions uxarray/grid/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,14 @@ def _construct_face_centroids(node_x, node_y, node_z, face_nodes, n_nodes_per_fa
tuple
The x, y, and z coordinates of the centroids.
"""
centroid_x = np.zeros((face_nodes.shape[0]), dtype=np.float64)
centroid_y = np.zeros((face_nodes.shape[0]), dtype=np.float64)
centroid_z = np.zeros((face_nodes.shape[0]), dtype=np.float64)

for face_idx, n_max_nodes in enumerate(n_nodes_per_face):
# Compute Cartesian Average
centroid_x[face_idx] = np.mean(node_x[face_nodes[face_idx, 0:n_max_nodes]])
centroid_y[face_idx] = np.mean(node_y[face_nodes[face_idx, 0:n_max_nodes]])
centroid_z[face_idx] = np.mean(node_z[face_nodes[face_idx, 0:n_max_nodes]])

# Mask to ignore invalid indices
mask = np.arange(face_nodes.shape[1])[None, :] < n_nodes_per_face[:, None]

# Calculate centroids
centroid_x = np.sum(node_x[face_nodes] * mask, axis=1) / n_nodes_per_face
centroid_y = np.sum(node_y[face_nodes] * mask, axis=1) / n_nodes_per_face
centroid_z = np.sum(node_z[face_nodes] * mask, axis=1) / n_nodes_per_face
Comment on lines +335 to +337
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This here is problematic for grids that have variable element sizes.

face_node_connectivity will contain fill values, which will lead to an index error here.

IndexError: index -9223372036854775808 is out of bounds for axis 0 with size 83886080

This wasn't an issue for the SCREAM dataset because it is completely composed of quads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about fixing this and changing/standardizing the way we handle connectivity?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing/standardizing the way we handle connectivity

Can you elaborate? I've explored storing connectivity as partitioned arrays in #978, although for something as simple as computing centroids, Numba seems to a significantly simpler implementation, performing closer to compiled language performance without needing to create an additional mask.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that with -1 etc. the mask approach fails, isn't that correct? so, I was thinking adopting a convention that allows for masking to work would simply things.

Not sure about the performance as I think NumPy's vectorized operations might be highly optimized for numerical computations and hopefully outperform the explicit loop here, even when those loops are parallelized with Numba.


return _normalize_xyz(centroid_x, centroid_y, centroid_z)

Expand Down
Loading