Skip to content

Commit fa45e02

Browse files
authored
Merge branch 'main' into tree-mesh-cell-bounds
2 parents d04bddf + 87b9300 commit fa45e02

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

discretize/tensor_mesh.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,26 @@ def face_boundary_indices(self):
593593
indzu = self.gridFz[:, 2] == max(self.gridFz[:, 2])
594594
return indxd, indxu, indyd, indyu, indzd, indzu
595595

596+
@property
597+
def cell_bounds(self):
598+
"""The bounds of each cell.
599+
600+
Return a 2D array with the coordinates that define the bounds of each
601+
cell in the mesh. Each row of the array contains the bounds for
602+
a particular cell in the following order: ``x1``, ``x2``, ``y1``,
603+
``y2``, ``z1``, ``z2``, where ``x1 < x2``, ``y1 < y2`` and ``z1 < z2``.
604+
"""
605+
nodes = self.nodes.reshape((*self.shape_nodes, -1), order="F")
606+
607+
min_nodes = nodes[(slice(-1),) * self.dim]
608+
min_nodes = min_nodes.reshape((self.n_cells, -1), order="F")
609+
max_nodes = nodes[(slice(1, None),) * self.dim]
610+
max_nodes = max_nodes.reshape((self.n_cells, -1), order="F")
611+
612+
cell_bounds = np.stack((min_nodes, max_nodes), axis=-1)
613+
cell_bounds = cell_bounds.reshape((self.n_cells, -1))
614+
return cell_bounds
615+
596616
@property
597617
def cell_nodes(self):
598618
"""The index of all nodes for each cell.

tests/base/test_tensor.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,26 +253,34 @@ def test_serialization(self):
253253
self.assertTrue(np.all(self.mesh2.gridCC == mesh.gridCC))
254254

255255

256-
class TestTensorMeshCellNodes:
256+
class TestTensorMeshProperties:
257257
"""
258-
Test TensorMesh.cell_nodes
258+
Test some of the properties in TensorMesh
259259
"""
260260

261261
@pytest.fixture(params=[1, 2, 3], ids=["dims-1", "dims-2", "dims-3"])
262262
def mesh(self, request):
263263
"""Sample TensorMesh."""
264264
if request.param == 1:
265265
h = [10]
266+
origin = (-35.5,)
266267
elif request.param == 2:
267268
h = [10, 15]
269+
origin = (-35.5, 105.3)
268270
else:
269271
h = [10, 15, 20]
270-
return discretize.TensorMesh(h)
272+
origin = (-35.5, 105.3, -27.3)
273+
return discretize.TensorMesh(h, origin=origin)
271274

272275
def test_cell_nodes(self, mesh):
273276
"""Test TensorMesh.cell_nodes."""
274277
expected_cell_nodes = np.array([cell.nodes for cell in mesh])
275-
np.testing.assert_allclose(mesh.cell_nodes, expected_cell_nodes)
278+
np.testing.assert_equal(mesh.cell_nodes, expected_cell_nodes)
279+
280+
def test_cell_bounds(self, mesh):
281+
"""Test TensorMesh.cell_bounds."""
282+
expected_cell_bounds = np.array([cell.bounds for cell in mesh])
283+
np.testing.assert_equal(mesh.cell_bounds, expected_cell_bounds)
276284

277285

278286
class TestPoissonEqn(discretize.tests.OrderTest):

0 commit comments

Comments
 (0)