Skip to content

Commit 3bab651

Browse files
committed
specialize for 1D
1 parent ca4d4dc commit 3bab651

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

discretize/tensor_mesh.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,14 @@ def point2index(self, locs): # NOQA D102
774774
# at index 1 to maintain the sorted list, but that corresponds to cell 0.
775775
# clipping here ensures that anything outside the mesh will return the nearest cell.
776776
multi_inds = tuple(
777-
np.clip(np.searchsorted(n, p) - 1, 0, len(n) - 1)
777+
np.clip(np.searchsorted(n, p) - 1, 0, len(n) - 2)
778778
for n, p in zip(cell_bounds, locs.T)
779779
)
780780
# and of course, we are fortran ordered in a tensor mesh.
781-
return np.ravel_multi_index(multi_inds, self.shape_cells, order="F")
781+
if self.dim == 1:
782+
return multi_inds[0]
783+
else:
784+
return np.ravel_multi_index(multi_inds, self.shape_cells, order="F")
782785

783786
def _repr_attributes(self):
784787
"""Represent attributes of the mesh."""

tests/base/test_tensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def test_tensor_point2index_inside_points(random_tensor_mesh):
337337
mesh = random_tensor_mesh
338338
dim = mesh.dim
339339
m_origin = mesh.origin
340-
m_extent = np.max(mesh.nodes, axis=0)
340+
m_extent = np.atleast_1d(np.max(mesh.nodes, axis=0))
341341

342342
nd = 15
343343
points = np.stack(np.meshgrid(*np.linspace(m_origin, m_extent, nd).T), axis=-1)
@@ -357,7 +357,7 @@ def test_tensor_point2index_outside_points(random_tensor_mesh):
357357
mesh = random_tensor_mesh
358358
dim = mesh.dim
359359
m_origin = mesh.origin
360-
m_extent = np.max(mesh.nodes, axis=0)
360+
m_extent = np.atleast_1d(np.max(mesh.nodes, axis=0))
361361
m_width = m_extent - m_origin
362362

363363
nd = 15

0 commit comments

Comments
 (0)