discretize.utils.slice_mesh#

discretize.utils.slice_mesh(mesh, normal='Z', location=None, return_indices=False)[source]#

Extract an axis-aligned 2D slice of a 3D TensorMesh or TreeMesh.

The cells of a 3D tensor mesh or octree that intersect an axis-aligned plane form a 2D mesh of the same type. This builds that mesh, with one 2D cell for each 3D cell cut by the plane, and can also return which 3D cell each 2D cell came from.

Parameters:
meshdiscretize.TensorMesh or discretize.TreeMesh

A 3D tensor or tree mesh.

normal{“X”, “Y”, “Z”} or int

The axis normal to the slicing plane.

locationfloat, optional

The coordinate of the plane along normal. Defaults to the center of the middle base cell along normal, as used by plot_slice. If the plane lies on cell faces, the cells on the high side of the plane are used, or the cells on the low side if the plane is the high boundary of the mesh.

return_indicesbool, default: False

Whether to also return the index of the 3D cell each 2D cell lies in.

Returns:
mesh_2ddiscretize.TensorMesh or discretize.TreeMesh

The 2D mesh of the slice, of the same type as mesh. Its first and second dimensions are the remaining axes in increasing order (e.g. (x, z) for a "Y" normal).

indices(mesh_2d.n_cells,) numpy.ndarray of int

Only returned if return_indices is True. The index of the 3D cell containing each 2D cell, so that values_2d = values_3d[indices] takes cell values from the 3D mesh to the slice.

Examples

Slice a 3D tree mesh refined around a point, and take a model to the slice.

>>> import numpy as np
>>> import discretize
>>> from discretize.utils import slice_mesh
>>> mesh = discretize.TreeMesh([16, 16, 16], diagonal_balance=True)
>>> mesh.refine_ball([0.5, 0.5, 0.5], 0.2, levels=-1)
>>> model = mesh.cell_centers[:, 2]
>>> mesh_2d, indices = slice_mesh(mesh, "Y", 0.51, return_indices=True)
>>> mesh_2d.dim
2
>>> model_2d = model[indices]
>>> np.allclose(model_2d, mesh_2d.cell_centers[:, 1])
True

Tensor meshes slice the same way, giving a 2D tensor mesh.

>>> mesh = discretize.TensorMesh([4, 5, 6])
>>> mesh_2d = slice_mesh(mesh, "X", 0.3)
>>> mesh_2d.shape_cells
(5, 6)