-
Notifications
You must be signed in to change notification settings - Fork 279
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
ENH: cartesian cutting planes through non-cartesian geometries #4847
Open
chrishavlin
wants to merge
18
commits into
yt-project:main
Choose a base branch
from
chrishavlin:cutting_spheres
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
225c1a1
intermediate commit
matthewturk d72530e
initial mixed coord slice implementation
chrishavlin d5171fa
mixed coord pixilizer
chrishavlin cced1bd
general mixed coord Cython classes, move utilities to new module
chrishavlin 1e8fc56
add edge tolerance to mixed geom pixelizer
chrishavlin c289325
reorganize spherical-cart funcs, expose edge_tol
chrishavlin a29f49e
mixed coordinate cutting plane example notebooks
chrishavlin 3675675
update mixed coordinate cutting plane tests
chrishavlin 5921e61
mixed coordinate slices: update tests, docs
chrishavlin 4eec2fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5747a6c
fix _select_bbox for elements with large angular ranges
chrishavlin 0b85099
rename YTMixedCoordinatePlane to YTCartesianCuttingPlane
chrishavlin ad2784d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5979912
YTCartesianCuttingPlane test updates
chrishavlin 562df54
support nd arrays for cartesian/spherical utilities
chrishavlin 9ce4791
cartesian cutting: add image comparison test
chrishavlin 71da30f
CartesianCuttingPlanes: add test for axis order
chrishavlin 9d1153d
Merge branch 'main' into cutting_spheres
chrishavlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import itertools | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import pytest | ||
import unyt | ||
|
||
import yt | ||
from yt.geometry.coordinates.spherical_coordinates import spherical_to_cartesian | ||
from yt.testing import fake_amr_ds | ||
|
||
|
||
def test_cartesian_cutting_plane(): | ||
ds = fake_amr_ds(geometry="spherical") | ||
normal = np.array([0.0, 0.0, 1.0]) | ||
plane_center = np.array([0.0, 0.0, 0.5]) | ||
slc = ds.cartesian_cutting(normal, plane_center) | ||
frb = slc.to_frb(2.0, 800) | ||
bvals = frb[("index", "r")] | ||
mask = frb.get_mask(("index", "r")) | ||
# note: the min value of r on the plane will be the z value of the | ||
# plane center. how close it is to the correct answer will depend | ||
# on the size of the elements. | ||
assert np.allclose(bvals[mask].min().d, plane_center[2], atol=0.02) | ||
|
||
|
||
def _get_spherical_uniform_grid(shp, bbox, axis_order): | ||
|
||
data = {"density": np.random.random(shp)} | ||
|
||
def _z(field, data): | ||
r = data["index", "r"] | ||
theta = data["index", "theta"] | ||
phi = data["index", "phi"] | ||
_, _, z = spherical_to_cartesian(r, theta, phi) | ||
return unyt.unyt_array(z, r.units) | ||
|
||
ds = yt.load_uniform_grid( | ||
data, | ||
shp, | ||
bbox=bbox, | ||
geometry="spherical", | ||
axis_order=axis_order, | ||
length_unit="m", | ||
) | ||
|
||
ds.add_field( | ||
name=("index", "z_val"), function=_z, sampling_type="cell", take_log=False | ||
) | ||
return ds | ||
|
||
|
||
@pytest.fixture | ||
def spherical_ds(): | ||
|
||
shp = (32, 32, 32) | ||
bbox = np.array([[0.0, 1.0], [0, np.pi], [0, 2 * np.pi]]) | ||
ax_order = ("r", "theta", "phi") | ||
return _get_spherical_uniform_grid(shp, bbox, ax_order) | ||
|
||
|
||
def test_cartesian_cutting_plane_fixed_z(spherical_ds): | ||
ds = spherical_ds | ||
normal = np.array([0.0, 0.0, 1.0]) | ||
center = np.array([0.0, 0.0, 0.5]) | ||
slc = ds.cartesian_cutting(normal, center) | ||
zvals = slc["index", "z_val"].to("code_length").d | ||
assert np.allclose(zvals, ds.quan(0.5, "code_length").d, atol=0.05) | ||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_vertical_slice_at_sphere_edge(spherical_ds): | ||
ds = spherical_ds | ||
normal = np.array([0.0, 1.0, 0.0]) | ||
center = np.array([0.0, 0.75, 0.0]) | ||
slc = ds.cartesian_cutting(normal, center) | ||
frb = slc.to_frb(2.0, 50) | ||
vals = frb["index", "z_val"].to("code_length") | ||
vals[~frb.get_mask(("index", "z_val"))] = np.nan | ||
|
||
f, axs = plt.subplots(1) | ||
axs.imshow(vals, origin="lower", extent=frb.bounds) | ||
return f | ||
|
||
|
||
def test_cartesian_cutting_plane_with_axis_ordering(): | ||
# check that slicing works with any axis order | ||
shp = (32, 32, 32) | ||
axes = ["r", "theta", "phi"] | ||
bbox_ranges = {"r": [0.0, 1.0], "theta": [0, np.pi], "phi": [0, 2 * np.pi]} | ||
|
||
# set the attributes for the plane, including a north vector found | ||
# for an arbitrary point on the plane. | ||
normal = np.array([1.0, 1.0, 1.0]) | ||
center = np.array([0.0, 0.0, 0.0]) | ||
x, y = 1.0, 1.0 | ||
z = -x * normal[0] - y * normal[1] | ||
north_pt = np.array([x, y, z]) | ||
assert np.dot(normal, north_pt) == 0.0 # just to be sure... | ||
|
||
frb_vals = [] | ||
for axis_order in itertools.permutations(axes): | ||
bbox = np.zeros((3, 2)) | ||
for i, ax in enumerate(axis_order): | ||
bbox[i, :] = bbox_ranges[ax] | ||
ds = _get_spherical_uniform_grid(shp, bbox, tuple(axis_order)) | ||
slc = ds.cartesian_cutting(normal, center, north_vector=north_pt) | ||
frb = slc.to_frb(2.0, 50) | ||
vals = frb["index", "z_val"].to("code_length") | ||
vals[~frb.get_mask(("index", "z_val"))] = np.nan | ||
frb_vals.append(vals.d) | ||
|
||
for frb_z in frb_vals[1:]: | ||
np.allclose(frb_z, frb_vals[0]) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to reviewers: this change was needed so that the selector that is chosen can depend on the geometry. this method gets over-ridden in the new selector object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember if this was something I experimented with or not, but it seems like a good idea. Although, I wonder if we could just wrap it in the
selector
property itself?