Grids

Grids are formed as tensor products of one or more axes. A Grid combines the collocation points, differentiation matrices, and integration weights of its constituent axes into a multi-dimensional structure. MultiGrid extends this concept to composite domains consisting of several sub-grids.

class numgrids.Grid(*axes)[source]

An N-dimensional numerical grid built from one or more axes.

A Grid is the tensor product of its constituent Axis objects. It provides meshed coordinate arrays, boundary masks, and convenience methods for refinement and coarsening.

Constructor

Parameters:

axes (one or more Axis objects) – One or more axes defining the grid. The order of the axes matters! The first axis passed will have index 0.

get_axis(idx: int = 0)[source]

Returns the axis with given index.

Parameters:

idx (int) – The index of the requested axis.

Return type:

The requested Axis object.

property axes: tuple

Returns the axes objects of the grid.

property coords: ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ...]

Returns a tuple of lists with the coordinate values along each axis. In case of 1D, only a single list is returned.

property meshed_coords: tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ...]

Returns a tuple with the meshed coordinate values.

property shape: tuple[int, ...]

Returns a tuple with the number of grid points along each axis.

plot() None[source]

Visualize the grid using matplotlib.

property faces: dict

Return a dictionary of all boundary faces.

Keys follow the pattern '{axis_index}_{side}', e.g. '0_low', '0_high'. Periodic axes are skipped.

property boundary: ndarray[tuple[Any, ...], dtype[_ScalarT]]

Returns a binary mask of the shape of the grid indicating the grid points on the boundary.

refine() Grid[source]

Returns a new grid with twice the number of grid points in each direction.

coarsen() Grid[source]

Returns a new grid with half the number of grid points in each direction.

refine_axis(axis_index: int, factor: float = 2.0) Grid[source]

Return a new grid with one axis resized by the given factor.

Parameters:
  • axis_index (int) – Index of the axis to refine (or coarsen if factor < 1).

  • factor (float) – Multiplicative factor for the number of points on the specified axis. Values greater than 1 refine; values between 0 and 1 coarsen. The result is rounded to the nearest integer (minimum 2).

Returns:

A new grid with the specified axis resized.

Return type:

Grid

Raises:

ValueError – If axis_index is out of range or factor is not positive.

property meshed_indices: list[ndarray[tuple[Any, ...], dtype[_ScalarT]]]

Returns a tuple of length grid.ndims, where each item in the tuple is an array of shape grid.shape. Each item stores the indices of each grid point for a given axis.

For example, if you have a 2D grid with 4 points along axis 0 and 3 points along axis 1, the indices along axis 0 are [0, 1, 2, 3], the indices along axis 1 are [0, 1, 2]. meshed_indices returns a tuple (I, J), where I and J both have shape (4, 3). I contains the “slot-0” indices, J contains the “slot-1” indices.

This generalizes for grids in higher dimensions, of course.

property index_tuples: ndarray[tuple[Any, ...], dtype[_ScalarT]]

Returns an array A of shape (*grid.shape, grid.ndims).

For example, in case of a 2D grid, A[i, j] contains the index tuple (i, j).

property coord_tuples: ndarray[tuple[Any, ...], dtype[_ScalarT]]

Returns an array A of shape (*grid.shape, grid.ndims) with all the coordinate tuples.

For example, in case of a 2D grid, A[i, j] contains the tuple (x, y), where x and y are the coordinates of the grid point (i, j).

class numgrids.MultiGrid(*axes, min_size: int = 2)[source]

Represents a multigrid, a hierarchical set of grids of different resolutions for the same domain.

Constructor.

The grids of the various resolutions are stored in the property levels.

Parameters:
  • axes (one or more Axis objects) – The axes for the grid with highest resolution. Starting from the grid with the highest resolution (level 0), the constructor will create different levels of resolution. Each additional level contains a grid with about half the grid points in each direction. All axis types are supported (EquidistantAxis, ChebyshevAxis, LogAxis, and mixtures thereof). Axis properties such as periodic and name are preserved at every level.

  • min_size (positive int) – This parameter sets a lower bound for the coarsest grid in the multigrid. At the lowest resolution, each axis must have at least min_size grid points.

property levels: list[Grid]

Returns a list of grids with different resolution levels.

levels[0] => finest grid levels[-1] => coarsest grid

transfer(f: ndarray[tuple[Any, ...], dtype[_ScalarT]], level_from: int, level_to: int, method: str = 'linear') ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Coarsen or refine an array on a grid by one level.

Parameters:
  • f (numpy.ndarray) – An array given on the grid at level = level_from.

  • level_from (int) – The level of the grid on which f is given.

  • level_to (int) – The level to which f shall be refined or coarsened.

  • method (str) – Coarsening and refinement involves interpolation. This is the interpolation order.

Return type:

The coarsened or refined array.