Skip to content

Add slice_tree_mesh utility for axis-aligned TreeMesh slices - #420

Merged
jcapriot merged 4 commits into
simpeg:mainfrom
jcapriot:tree_slice_mesh_util
Oct 5, 2026
Merged

jcapriot merged 4 commits into
simpeg:mainfrom
jcapriot:tree_slice_mesh_util

Conversation

@jcapriot

@jcapriot jcapriot commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

This essentially moves the code that was internal to plot_slice to a user-facing utility.

discretize.utils.slice_tree_mesh builds the 2D TreeMesh of an axis-aligned slice through a 3D TreeMesh (one 2D cell per 3D cell cut by the plane), and can also return the index of the 3D cell each 2D cell comes from, so that values_2d = values_3d[indices]. A plane lying on cell faces takes the cells on its high side, or its low side at the top of the mesh, which makes slicing exactly at a boundary well defined.

TreeMesh.plot_slice now uses it instead of building the slice itself.

@jcapriot
jcapriot requested a review from santisoler September 29, 2026 20:16
@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.28571% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.06%. Comparing base (f6e7d8a) to head (3a75be4).

Files with missing lines Patch % Lines
discretize/mixins/mpl_mod.py 5.88% 16 Missing ⚠️
discretize/utils/mesh_utils.py 95.91% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #420      +/-   ##
==========================================
+ Coverage   86.86%   87.06%   +0.20%     
==========================================
  Files          93       93              
  Lines       19239    19348     +109     
  Branches     2154     2157       +3     
==========================================
+ Hits        16712    16846     +134     
+ Misses       1840     1814      -26     
- Partials      687      688       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

discretize.utils.slice_tree_mesh builds the 2D TreeMesh of an axis-aligned
slice through a 3D TreeMesh (one 2D cell per 3D cell cut by the plane), and
can also return the index of the 3D cell each 2D cell comes from, so that
values_2d = values_3d[indices]. A plane lying on cell faces takes the cells
on its high side, or its low side at the top of the mesh, which makes
slicing exactly at a boundary well defined.

TreeMesh.plot_slice now uses it instead of building the slice itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcapriot
jcapriot force-pushed the tree_slice_mesh_util branch from bacc9b3 to b90120c Compare September 30, 2026 14:53

@santisoler santisoler left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! I like the fact to be able to slice a mesh and not necessarily just plotting it.

One idea: what if instead of defining a slice_tree_mesh we define a slice_mesh function that can take either a TreeMesh or a TensorMesh?

At least for now we could raise a NonImplementedError if the passed mesh is a TensorMesh, and we can add that functionality later.

@jcapriot

jcapriot commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

Looks nice! I like the fact to be able to slice a mesh and not necessarily just plotting it.

One idea: what if instead of defining a slice_tree_mesh we define a slice_mesh function that can take either a TreeMesh or a TensorMesh?

At least for now we could raise a NonImplementedError if the passed mesh is a TensorMesh, and we can add that functionality later.

Easy enough to add it for TensorMesh, but it will still have to fail for all of the other mesh types as well

@santisoler

Copy link
Copy Markdown
Member

Yes, that should be fine. If we want to make it clearer, maybe we could name it to slice_rectangular_mesh, but I think slice_mesh should be enough. We can explain it in the docs that it expects either a TreeMesh or a TensorMesh.

jcapriot and others added 2 commits October 3, 2026 19:32
discretize.utils.slice_mesh replaces slice_tree_mesh and slices a 3D
TensorMesh or TreeMesh, returning a 2D mesh of the same type (and
optionally the index of the 3D cell each 2D cell comes from). It validates
the inputs once and dispatches to a private implementation for each mesh
type. Indices are only computed when requested.

The plane bounds now use the mesh's own node arrays, so a location taken
from e.g. mesh.nodes_x[-1] is no longer rejected because of round-off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tensor plot_slice now gets the 2D mesh and the 3D cell of each 2D
cell from slice_mesh, like the TreeMesh version, instead of building the
2D mesh by hand and indexing a reshaped 3D array. The plotted values are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcapriot

jcapriot commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

@santisoler I implemented it for the tensor mesh path as well since it let us get rid of redundant code within the plotting scripts in the same way

@santisoler

Copy link
Copy Markdown
Member

Amazing! Thanks for taking care of this!

@jcapriot
jcapriot merged commit 30d8531 into simpeg:main Oct 5, 2026
29 of 30 checks passed
@jcapriot
jcapriot deleted the tree_slice_mesh_util branch October 5, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants