This page was generated by
nbsphinx from
docs/notebooks/plasma/grids_nonuniform.ipynb.

Interactive online version:
.

# Grids: Non-Uniform Grids

Some data cannot be easily represented on a grid of uniformly spaced vertices. It is still possible to create a grid object to represent such a dataset.

```
[1]:
```

```
%matplotlib inline
import astropy.units as u
import numpy as np
from plasmapy.plasma import grids
```

```
[2]:
```

```
grid = grids.NonUniformCartesianGrid(
np.array([-1, -1, -1]) * u.cm, np.array([1, 1, 1]) * u.cm, num=(50, 50, 50)
)
```

```
/home/docs/checkouts/readthedocs.org/user_builds/plasmapy/envs/latest/lib/python3.12/site-packages/plasmapy/plasma/grids.py:616: FutureWarning: the `pandas.MultiIndex` object(s) passed as 'ax' coordinate(s) or data variable(s) will no longer be implicitly promoted and wrapped into multiple indexed coordinates in the future (i.e., one coordinate for each multi-index level + one dimension coordinate). If you want to keep this behavior, you need to first wrap it explicitly using `mindex_coords = xarray.Coordinates.from_pandas_multiindex(mindex_obj, 'dim')` and pass it as coordinates, e.g., `xarray.Dataset(coords=mindex_coords)`, `dataset.assign_coords(mindex_coords)` or `dataarray.assign_coords(mindex_coords)`.
self.ds.coords["ax"] = mdx
```

Currently, all non-uniform data is stored as an unordered 1D array of points. Therefore, although the dataset created above falls approximately on a Cartesian grid, its treatment is identical to a completely unordered set of points

```
[3]:
```

```
[3]:
```

```
(125000,)
```

Many of the properties defined for uniform grids are inaccessible for non-uniform grids. For example, it is not possible to pull out an axis. However, the following properties still apply

```
[4]:
```

```
print(f"Grid points: {grid.grid.shape}")
print(f"Units: {grid.units}")
```

```
Grid points: (125000, 3)
Units: [Unit("cm"), Unit("cm"), Unit("cm")]
```

Properties can be added in the same way as on uniform grids.

```
[5]:
```

```
Bx = np.random.rand(*grid.shape) * u.T
grid.add_quantities(B_x=Bx)
print(grid)
```

```
*** Grid Summary ***
<class 'plasmapy.plasma.grids.NonUniformCartesianGrid'>
Dimensions: (ax: 125000)
Non-Uniform Spacing
-----------------------------
Coordinates:
-> ax (cm) object (125000,)
-----------------------------
Recognized Quantities:
-> B_x (T) float64 (125000,)
-----------------------------
Unrecognized Quantities:
-None-
```

## Methods

Many of the methods defined for uniform grids also work for non-uniform grids, however there is usually a substantial performance penalty in the non-uniform case.

For example, `grid.on_grid`

behaves similarly. In this case, the boundaries of the grid are defined by the furthest point away from the origin in each direction.

```
[6]:
```

```
pos = np.array([[0.1, -0.3, 0], [3, 0, 0]]) * u.cm
print(grid.on_grid(pos))
```

```
[ True False]
```

The same definition is used to define the grid boundaries in `grid.vector_intersects`

```
[7]:
```

```
pt0 = np.array([3, 0, 0]) * u.cm
pt1 = np.array([-3, 0, 0]) * u.cm
pt2 = np.array([3, 10, 0]) * u.cm
print(f"Line from pt0 to pt1 intersects: {grid.vector_intersects(pt0, pt1)}")
print(f"Line from pt0 to pt2 intersects: {grid.vector_intersects(pt0, pt2)}")
```

```
Line from pt0 to pt1 intersects: True
Line from pt0 to pt2 intersects: False
```

## Interpolating Quantities

Nearest-neighbor interpolation also works identically. However, volume-weighted interpolation is not implemented for non-uniform grids.

```
[8]:
```

```
Pos shape: (2, 3)
Position 1: [ 0.1 -0.3 0. ] cm
Position 2: [0.5 0.25 0.8 ] cm
Bx at position 1: 0.18 T
```