Skip to content

Adding indices at coords to other structured mesh types #3867

Open

Description

@shimwell

In this PR I added the method get_indices_at_coords to the structured meshes

#3824

we could add to the get_indices_at_coords RectilinearMesh and SphericalMesh mesh class

Activity

  1. EdenRochmanSharabi commented on Apr 5, 2026

    @EdenRochmanSharabi
    Contributor

    Hi, I'd like to take this on.

    I've gone through PR #3824 discussion and the current state of the code. It looks like RectilinearMesh.get_indices_at_coords was already added (using np.searchsorted per axis with boundary clamping), so the only remaining piece is SphericalMesh.

    My approach for SphericalMesh.get_indices_at_coords:

    1. Accept Cartesian (x, y, z) coordinates (consistent with CylindricalMesh)
    2. Convert to spherical (r, theta, phi) relative to self.origin:
      • r = sqrt(dx**2 + dy**2 + dz**2)
      • theta = arccos(dz / r) (polar angle from z-axis)
      • phi = atan2(dy, dx), normalized to [0, 2*pi]
    3. Validate each value against its grid bounds, raising ValueError if outside (following CylindricalMesh precedent)
    4. Find bin indices with np.searchsorted(..., side='right') - 1
    5. Handle the degenerate case r = 0 where theta and phi are undefined: default to theta = 0, phi = 0
    6. Return (r_index, theta_index, phi_index)

    I'll also add tests following the existing pattern in test_mesh.py.

    Does this approach sound right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions