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

Merged
merged 12 commits into from
Feb 5, 2025
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
erogluorhan marked this conversation as resolved.
Show resolved Hide resolved

return _normalize_xyz(centroid_x, centroid_y, centroid_z)

Expand Down
Loading