-
Notifications
You must be signed in to change notification settings - Fork 46
Add spatial hashing #1169
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
Merged
Merged
Add spatial hashing #1169
Changes from 33 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
89542c9
Add initial draft of SpatialHash class (triangular meshes only)
fluidnumerics-joe 3093d48
Add `get_spatial_hash` to grid api
fluidnumerics-joe 46dcdc6
Add example usage in spatial hashing docs
fluidnumerics-joe 77c47c6
Merge branch 'UXARRAY:main' into main
fluidnumerics-joe 63048b5
Add spatialhashing to userguide
fluidnumerics-joe 3831793
Merge branch 'main' of github.com:FluidNumerics/uxarray
fluidnumerics-joe b79bc8b
Add basic tests
fluidnumerics-joe 86d2d5f
Fix call to get_spatialhash
fluidnumerics-joe b7f3ca3
Merge branch 'main' into main
fluidnumerics-joe 8d6e47e
Fix formatting
fluidnumerics-joe 72c1ed6
Merge branch 'main' of github.com:FluidNumerics/uxarray
fluidnumerics-joe 5a59918
Prepend attributes intended for internal use with _
fluidnumerics-joe e1adc37
Add a few patches for hashtable setup and query check
fluidnumerics-joe 95cba52
Fix docstrings for tests; add fesom test with particle list
fluidnumerics-joe 7afca52
Add docstring for get_spatialhash
fluidnumerics-joe d599276
remove empty notes
fluidnumerics-joe 52f1728
Update uxarray/grid/grid.py
fluidnumerics-joe dfa4f77
Fix naming in test
fluidnumerics-joe 4eac186
Merge branch 'main' of github.com:FluidNumerics/uxarray
fluidnumerics-joe 8ea867d
Update uxarray/grid/neighbors.py
fluidnumerics-joe 7197320
Update uxarray/grid/neighbors.py
fluidnumerics-joe 254cfb6
Update test/test_spatial_hashing.py
fluidnumerics-joe d7dbbcb
Fix docstring for reconstruct field
fluidnumerics-joe 7e21f0d
Update uxarray/grid/grid.py
fluidnumerics-joe 5596cdb
Add example to docstring
fluidnumerics-joe 0c0dd60
Use waschpress points for generalized barycentric coordinates
fluidnumerics-joe 60e670a
Switch to mpas example in notebook
fluidnumerics-joe 6fd8e37
Update uxarray/grid/grid.py
fluidnumerics-joe fc59591
Retain output in notebook. Make requested formatting changes
fluidnumerics-joe 7ec453d
Merge branch 'main' of github.com:FluidNumerics/uxarray
fluidnumerics-joe 8db36e6
Fix formatting
fluidnumerics-joe fb4bbf8
Change to smaller/simpler 4xhex grid
fluidnumerics-joe b655a46
Add get_spatial_hash to the Grid/Methods section
fluidnumerics-joe 37034ae
Clear outputs from notebook.
fluidnumerics-joe 63e8b29
Merge remote-tracking branch 'upstream/main'
fluidnumerics-joe e7ab031
Fix docstring; we're not computing signed area any longer.
fluidnumerics-joe b318f40
Update uxarray/grid/neighbors.py
fluidnumerics-joe 305b8e3
Update uxarray/grid/neighbors.py
fluidnumerics-joe 5fcd039
Add query optimization suggested by @philipc2
fluidnumerics-joe 61fcc1a
Remove extra return statement
fluidnumerics-joe 378c817
Clip denominator in barycentric weights calculation
fluidnumerics-joe b3c9a07
Clip triangle areas rather than denominator
fluidnumerics-joe 5acf05e
Merge branch 'main' into main
fluidnumerics-joe 56b0c85
Merge branch 'main' into main
fluidnumerics-joe 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 hidden or 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 hidden or 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 hidden or 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,115 @@ | ||
| import os | ||
| import numpy as np | ||
| import pytest | ||
| import xarray as xr | ||
| from pathlib import Path | ||
| import uxarray as ux | ||
|
|
||
| current_path = Path(os.path.dirname(os.path.realpath(__file__))) | ||
|
|
||
| # Sample grid file paths | ||
| gridfile_CSne8 = current_path / "meshfiles" / "scrip" / "outCSne8" / "outCSne8.nc" | ||
| gridfile_RLL1deg = current_path / "meshfiles" / "ugrid" / "outRLL1deg" / "outRLL1deg.ug" | ||
| gridfile_RLL10deg_CSne4 = current_path / "meshfiles" / "ugrid" / "ov_RLL10deg_CSne4" / "ov_RLL10deg_CSne4.ug" | ||
| gridfile_CSne30 = current_path / "meshfiles" / "ugrid" / "outCSne30" / "outCSne30.ug" | ||
| gridfile_fesom = current_path / "meshfiles" / "ugrid" / "fesom" / "fesom.mesh.diag.nc" | ||
| gridfile_geoflow = current_path / "meshfiles" / "ugrid" / "geoflow-small" / "grid.nc" | ||
| gridfile_mpas = current_path / 'meshfiles' / "mpas" / "QU" / 'mesh.QU.1920km.151026.nc' | ||
|
|
||
| grid_files = [gridfile_CSne8, | ||
| gridfile_RLL1deg, | ||
| gridfile_RLL10deg_CSne4, | ||
| gridfile_CSne30, | ||
| gridfile_fesom, | ||
| gridfile_geoflow, | ||
| gridfile_mpas] | ||
|
|
||
| def test_construction(): | ||
| """Tests the construction of the SpatialHash object""" | ||
| for grid_file in grid_files: | ||
| uxgrid = ux.open_grid(grid_file) | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query([0.9, 1.8]) | ||
| assert face_ids.shape[0] == bcoords.shape[0] | ||
|
|
||
|
|
||
| def test_is_inside(): | ||
| """Verifies simple test for points inside and outside an element.""" | ||
| verts = [(0.0, 90.0), (-180, 0.0), (0.0, -90)] | ||
| uxgrid = ux.open_grid(verts, latlon=True) | ||
| # Verify that a point outside the element returns a face id of -1 | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query([90.0, 0.0]) | ||
| assert face_ids[0] == -1 | ||
| # Verify that a point inside the element returns a face id of 0 | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query([-90.0, 0.0]) | ||
|
|
||
| assert face_ids[0] == 0 | ||
| assert np.allclose(bcoords[0], [0.25, 0.5, 0.25], atol=1e-06) | ||
|
|
||
| def test_list_of_coords_simple(): | ||
| """Verifies test using list of points inside and outside an element""" | ||
| verts = [(0.0, 90.0), (-180, 0.0), (0.0, -90)] | ||
| uxgrid = ux.open_grid(verts, latlon=True) | ||
|
|
||
| coords = [[90.0, 0.0], [-90.0, 0.0]] | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query(coords) | ||
| assert face_ids[0] == -1 | ||
| assert face_ids[1] == 0 | ||
| assert np.allclose(bcoords[1], [0.25, 0.5, 0.25], atol=1e-06) | ||
|
|
||
| def test_list_of_coords_fesom(): | ||
| """Verifies test using list of points on the fesom grid""" | ||
| uxgrid = ux.open_grid(gridfile_fesom) | ||
|
|
||
| num_particles = 20 | ||
| coords = np.zeros((num_particles,2)) | ||
| x_min = 1.0 | ||
| x_max = 3.0 | ||
| y_min = 2.0 | ||
| y_max = 10.0 | ||
| for k in range(num_particles): | ||
| coords[k,0] = np.deg2rad(np.random.uniform(x_min, x_max)) | ||
| coords[k,1] = np.deg2rad(np.random.uniform(y_min, y_max)) | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query(coords) | ||
| assert len(face_ids) == num_particles | ||
| assert bcoords.shape[0] == num_particles | ||
| assert bcoords.shape[1] == 3 | ||
| assert np.all(face_ids >= 0) # All particles should be inside an element | ||
|
|
||
| def test_list_of_coords_mpas_dual(): | ||
| """Verifies test using list of points on the dual MPAS grid""" | ||
| uxgrid = ux.open_grid(gridfile_mpas, use_dual=True) | ||
|
|
||
| num_particles = 20 | ||
| coords = np.zeros((num_particles,2)) | ||
| x_min = -40.0 | ||
| x_max = 40.0 | ||
| y_min = -20.0 | ||
| y_max = 20.0 | ||
| for k in range(num_particles): | ||
| coords[k,0] = np.deg2rad(np.random.uniform(x_min, x_max)) | ||
| coords[k,1] = np.deg2rad(np.random.uniform(y_min, y_max)) | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query(coords) | ||
| assert len(face_ids) == num_particles | ||
| assert bcoords.shape[0] == num_particles | ||
| assert bcoords.shape[1] == 3 # max sides of an element | ||
| assert np.all(face_ids >= 0) # All particles should be inside an element | ||
|
|
||
|
|
||
| def test_list_of_coords_mpas_primal(): | ||
| """Verifies test using list of points on the primal MPAS grid""" | ||
| uxgrid = ux.open_grid(gridfile_mpas, use_dual=False) | ||
|
|
||
| num_particles = 20 | ||
| coords = np.zeros((num_particles,2)) | ||
| x_min = -40.0 | ||
| x_max = 40.0 | ||
| y_min = -20.0 | ||
| y_max = 20.0 | ||
| for k in range(num_particles): | ||
| coords[k,0] = np.deg2rad(np.random.uniform(x_min, x_max)) | ||
| coords[k,1] = np.deg2rad(np.random.uniform(y_min, y_max)) | ||
| face_ids, bcoords = uxgrid.get_spatial_hash().query(coords) | ||
| assert len(face_ids) == num_particles | ||
| assert bcoords.shape[0] == num_particles | ||
| assert bcoords.shape[1] == 6 # max sides of an element | ||
| assert np.all(face_ids >= 0) # All particles should be inside an element |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.