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

Add TensorMesh.cell_bounds property #366

Merged
merged 8 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions discretize/tensor_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,36 @@ def face_boundary_indices(self):
indzu = self.gridFz[:, 2] == max(self.gridFz[:, 2])
return indxd, indxu, indyd, indyu, indzd, indzu

@property
def cell_bounds(self):
"""The bounds of each cell.

Return a 2D array with the coordinates that define the bounds of each
cell in the mesh. Each row of the array contains the bounds for
a particular cell in the following order: ``x1``, ``x2``, ``y1``,
``y2``, ``z1``, ``z2``, where ``x1 < x2``, ``y1 < y2`` and ``z1 < z2``.
"""
centers, widths = self.cell_centers, self.h_gridded
if self.dim == 1:
bounds = (centers - widths.ravel() / 2, centers + widths.ravel() / 2)
elif self.dim == 2:
x1 = centers[:, 0] - widths[:, 0] / 2
x2 = centers[:, 0] + widths[:, 0] / 2
y1 = centers[:, 1] - widths[:, 1] / 2
y2 = centers[:, 1] + widths[:, 1] / 2
bounds = (x1, x2, y1, y2)
else:
x1 = centers[:, 0] - widths[:, 0] / 2
x2 = centers[:, 0] + widths[:, 0] / 2
y1 = centers[:, 1] - widths[:, 1] / 2
y2 = centers[:, 1] + widths[:, 1] / 2
z1 = centers[:, 2] - widths[:, 2] / 2
z2 = centers[:, 2] + widths[:, 2] / 2
bounds = (x1, x2, y1, y2, z1, z2)

cell_bounds = np.hstack(tuple(v[:, np.newaxis] for v in bounds))
santisoler marked this conversation as resolved.
Show resolved Hide resolved
return cell_bounds

@property
def cell_nodes(self):
"""The index of all nodes for each cell.
Expand Down
14 changes: 11 additions & 3 deletions tests/base/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,27 +253,35 @@ def test_serialization(self):
self.assertTrue(np.all(self.mesh2.gridCC == mesh.gridCC))


class TestTensorMeshCellNodes:
class TestTensorMeshProperties:
"""
Test TensorMesh.cell_nodes
Test some of the properties in TensorMesh
"""

@pytest.fixture(params=[1, 2, 3], ids=["dims-1", "dims-2", "dims-3"])
def mesh(self, request):
"""Sample TensorMesh."""
if request.param == 1:
h = [10]
origin = (-35.5,)
elif request.param == 2:
h = [10, 15]
origin = (-35.5, 105.3)
else:
h = [10, 15, 20]
return discretize.TensorMesh(h)
origin = (-35.5, 105.3, -27.3)
return discretize.TensorMesh(h, origin=origin)

def test_cell_nodes(self, mesh):
"""Test TensorMesh.cell_nodes."""
expected_cell_nodes = np.array([cell.nodes for cell in mesh])
np.testing.assert_allclose(mesh.cell_nodes, expected_cell_nodes)

def test_cell_bounds(self, mesh):
"""Test TensorMesh.cell_bounds."""
expected_cell_bounds = np.array([cell.bounds for cell in mesh])
np.testing.assert_allclose(mesh.cell_bounds, expected_cell_bounds)


class TestPoissonEqn(discretize.tests.OrderTest):
name = "Poisson Equation"
Expand Down