Spatial Hashing#

UXarray supports spatial hashing, which a fast search method for finding the face that a point lies within on an unstructured grid. This can be useful for building particle-in-cell interpolators for Lagrangian fluid flow analysis.

import uxarray as ux

For this example we will be using a simple hexagonal grid.

grid_path = "../../test/meshfiles/ugrid/quad-hexagon/grid.nc"
uxgrid = ux.open_grid(grid_path)

SpatialHash#

UXarray SpatialHash class provides an API for locating the unstructured grid faces that a list of \((x,y)\) points (in spherical coordinates) lie within. This search is conducted by relating unstructured grid faces and arbitrary physical positions to a uniformly spaced structured grid (the hash grid). The relationship between the unstructured grid elements and the hash grid is maintained in a hash table.

Parameters#

The SpatialHash class can be accessed through the Grid.get_spatial_hash(reconstruct) method, which takes in the following parameters:

  • reconstruct is a bool variable that allows the user to reconstruct the spatial hash structure. As default for performance, if a user calls get_spatial_hash and a spatial hash structure has already been created, it will simply use that one. If reconstruct is set to True, it will override this and reconstruct the spatial hash structure. The default parameter is False.

Constructing a Spatial Hash#

We can store the SpatialHash data structure in a variable, which allows us to access the spatial hash in a simple way for queries.

spatial_hash = uxgrid.get_spatial_hash(
    reconstruct="False",
)

Query#

Now we can use that variable to query for the face that a point lies within in addition to its barycentric coordinates. The first parameter is the point from which to do the search, which must be in spherical coordinates.

face_ids, bcoords = uxgrid.get_spatial_hash().query([-0.1, -0.1])
print(f"face ids : {face_ids}")
print(f"Barycentric Coordinates: {bcoords}")
face ids : [0]
Barycentric Coordinates: [[0.17544347 0.31366069 0.23755459 0.12619394 0.07155789 0.07558942]]