Skip to content

Implement SphericalMesh.get_indices_at_coords#3919

Open
EdenRochmanSharabi wants to merge 1 commit into
openmc-dev:developfrom
EdenRochmanSharabi:spherical-mesh-get-indices-at-coords
Open

Implement SphericalMesh.get_indices_at_coords#3919
EdenRochmanSharabi wants to merge 1 commit into
openmc-dev:developfrom
EdenRochmanSharabi:spherical-mesh-get-indices-at-coords

Conversation

@EdenRochmanSharabi
Copy link
Copy Markdown

Summary

Implements SphericalMesh.get_indices_at_coords, resolving the remaining work
from #3867 (RectilinearMesh was already done in #3824).

The method accepts Cartesian (x, y, z) coordinates, converts to spherical
(r, theta, phi) relative to self.origin, validates against grid bounds, and
returns bin indices via np.searchsorted. Follows the same pattern as
CylindricalMesh.get_indices_at_coords.

Handles the degenerate case where r=0 (theta and phi are mathematically
undefined) by defaulting both to 0.0.

Changes

  • openmc/mesh.py: Replace NotImplementedError stub with full implementation
  • tests/unit_tests/test_mesh.py: Add test_SphericalMesh_get_indices_at_coords

Tests

  • Basic single-bin lookups along each axis
  • Multi-bin angular grids (theta and phi bin assignment)
  • Non-default origin
  • Boundary values (point on grid edge)
  • Degenerate r=0 case
  • Out-of-bounds ValueError for r, theta, and phi
  • Diagonal point verifying all three coordinates computed correctly

Resolves the remaining work from openmc-dev#3867. Converts Cartesian (x, y, z)
input to spherical (r, theta, phi) relative to the mesh origin, validates
against grid bounds, and returns bin indices via np.searchsorted.
Handles the degenerate r=0 case where angular coordinates are undefined.

Adds comprehensive unit tests covering basic lookups, multi-bin angular
grids, non-default origin, boundary values, r=0, and out-of-bounds
errors for each axis.
Copy link
Copy Markdown
Contributor

@pshriwise pshriwise left a comment

Choose a reason for hiding this comment

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

One tiny ask from me here but then I'd say this is ready @EdenRochmanSharabi. Thank you!

Comment thread openmc/mesh.py
from collections.abc import Iterable, Sequence, Mapping
from functools import wraps
from math import pi, sqrt, atan2
from math import acos, atan2, pi, sqrt
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My preference would be to import math alone and access these values/functions including the math prefix. Common functions like this can come from a number of places, so it's nice to keep that module context closer to where it's used. These are also names that could be shadowed by local variables and we want to watch out for that as well.

@pshriwise
Copy link
Copy Markdown
Contributor

It looks like this might also need to be updated against the develop branch to use some of the updated action versions from #3913.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants