Implement SphericalMesh.get_indices_at_coords#3919
Open
EdenRochmanSharabi wants to merge 1 commit into
Open
Conversation
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.
pshriwise
requested changes
May 13, 2026
Contributor
pshriwise
left a comment
There was a problem hiding this comment.
One tiny ask from me here but then I'd say this is ready @EdenRochmanSharabi. Thank you!
| from collections.abc import Iterable, Sequence, Mapping | ||
| from functools import wraps | ||
| from math import pi, sqrt, atan2 | ||
| from math import acos, atan2, pi, sqrt |
Contributor
There was a problem hiding this comment.
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.
Contributor
|
It looks like this might also need to be updated against the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements
SphericalMesh.get_indices_at_coords, resolving the remaining workfrom #3867 (
RectilinearMeshwas 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, andreturns bin indices via
np.searchsorted. Follows the same pattern asCylindricalMesh.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: ReplaceNotImplementedErrorstub with full implementationtests/unit_tests/test_mesh.py: Addtest_SphericalMesh_get_indices_at_coordsTests