Grids and fields

Before running any operator, the grid has to be described and the fields have to be allocated. This is done in regular Python code with the objects presented in this page: dimensions, domains, fields and, for unstructured meshes, connectivities.

import numpy as np

import gt4py.next as gtx

Dimensions

A Dimension is a named axis. Two dimensions are the same if they have the same name and kind.

I = gtx.Dimension("I")
J = gtx.Dimension("J")
K = gtx.Dimension("K", kind=gtx.DimensionKind.VERTICAL)

Tip

Create the dimensions of a model once, in a module, and import them wherever they are needed.

There are three kinds of dimensions:

Kind

Meaning

HORIZONTAL (default)

Horizontal axes of a Cartesian grid, or locations of an unstructured mesh

VERTICAL

The vertical axis. Scan operators run along it

LOCAL

The neighbors of a mesh location (see Unstructured meshes)

The same definitions work for any grid. In a Cartesian grid every horizontal axis is a dimension (typically I, J). In an unstructured mesh, every type of location is a dimension (e.g. Cell, Edge, Vertex).

Domains

A domain is a set of grid points: a range of indices for each of its dimensions. gtx.domain builds one from a dictionary, where a range is given either as its size or as a (start, stop) pair:

domain = gtx.domain({I: 4, J: (2, 5), K: 10})

print(domain)
print(domain.shape)
Domain(I[horizontal]=(0:4), J[horizontal]=(2:5), K[vertical]=(0:10))
(4, 3, 10)

Ranges do not need to start at zero. This is useful, for instance, to give negative indices to the halo points of a grid, or to work on a part of a larger grid while keeping its numbering.

Allocating fields

The following functions create a field over a domain:

Function

Result

gtx.zeros(domain, dtype)

Field filled with zeros

gtx.ones(domain, dtype)

Field filled with ones

gtx.full(domain, fill_value, dtype)

Field filled with fill_value

gtx.empty(domain, dtype)

Field with uninitialized values

gtx.as_field(domain, data, dtype)

Field with a copy of the array data

The domain can be passed as a Domain or directly as the dictionary accepted by gtx.domain. The default data type is float64, except in full and as_field, which take it from the given values.

plane = gtx.domain({I: 4, J: (2, 5)})

temperature = gtx.full(plane, 280.0)
mask = gtx.zeros(plane, dtype=bool)
level_index = gtx.as_field({K: 10}, np.arange(10, dtype=np.int32))

print(temperature.domain, temperature.asnumpy().dtype)
print(level_index.domain, level_index.asnumpy().dtype)
Domain(I[horizontal]=(0:4), J[horizontal]=(2:5)) float64
Domain(K[vertical]=(0:10)) int32

gtx.as_field always copies the data: the field does not share memory with the original array. It also accepts a list of dimensions instead of a domain. In that case the ranges start at zero and have the sizes of the array, unless the origin argument says which array index corresponds to the grid index zero:

data = np.arange(18.0).reshape(6, 3)

print(gtx.as_field([I, J], data).domain)
print(gtx.as_field([I, J], data, origin={I: 1}).domain)
Domain(I[horizontal]=(0:6), J[horizontal]=(0:3))
Domain(I[horizontal]=(-1:5), J[horizontal]=(0:3))

Allocating for a backend

By default, fields are allocated in main memory as NumPy arrays. The allocator argument selects a different way of allocating the buffer. Passing a backend allocates the field as that backend prefers, with the memory layout, the alignment and the device (CPU or GPU) it needs:

temperature_cpu = gtx.zeros(plane, allocator=gtx.gtfn_cpu)

Tip

Allocate the fields with the backend which is going to run the operators. For GPU backends it is required, since the fields must be on the device.

Allocating for embedded execution

In embedded execution the operations are evaluated by the array library holding the data of the fields. The library is selected by passing its array namespace as allocator. For NumPy:

temperature_numpy = gtx.zeros(plane, allocator=np)

Other libraries are used in the same way, for example CuPy to run on a GPU, or JAX. Both are optional dependencies, installed with extras of the package such as pip install "gt4py[cuda12]" or pip install "gt4py[jax]":

import cupy as cp
import jax.numpy as jnp

temperature_cupy = gtx.zeros(plane, allocator=cp)  # a CuPy array, on the GPU
temperature_jax = gtx.zeros(plane, allocator=jnp)  # a JAX array on the default device

Alternatively, the device argument selects where the data lives, and the field is allocated with the default library for that kind of device (CuPy for GPUs):

gpu = gtx.Device(gtx.DeviceType.CUDA, 0)  # the first CUDA device

temperature_gpu = gtx.zeros(plane, device=gpu)

All the fields used in a call, including out, must be held by the same array library.

Reading the data

A field gives access to its domain and to its data. asnumpy() always returns a NumPy array, copying the data from the device if needed:

field = gtx.as_field(plane, np.arange(12.0).reshape(4, 3))

print(field.domain)
print(field.asnumpy())
Domain(I[horizontal]=(0:4), J[horizontal]=(2:5))
[[ 0.  1.  2.]
 [ 3.  4.  5.]
 [ 6.  7.  8.]
 [ 9. 10. 11.]]

Indexing a field gives another field, a view of a part of the original one. Plain integers and slices are relative indices: they count positions from the start of the field, as in NumPy. Absolute indices, written Dimension(index), refer to the indices of the domain. The domain of field starts at 2 in the J direction, so these two expressions select the same column:

print(field[:, 0].domain, field[:, 0].asnumpy())
print(field[J(2)].domain, field[J(2)].asnumpy())
Domain(I[horizontal]=(0:4)) [0. 3. 6. 9.]
Domain(I[horizontal]=(0:4)) [0. 3. 6. 9.]

Unstructured meshes

In an unstructured mesh the neighbors of a location cannot be computed from its index, so they are given in tables called connectivities. The mesh below has six cells (numbered in red) and twelve edges (in blue):

../_images/connectivity_numbered_grid.svg

A mesh with 6 cells and 12 edges

Every type of location is a dimension, and fields are allocated as usual:

Cell = gtx.Dimension("Cell")
Edge = gtx.Dimension("Edge")

cell_values = gtx.as_field([Cell], np.array([1.0, 1.0, 2.0, 3.0, 5.0, 8.0]))
edge_values = gtx.zeros({Edge: 12})

Connectivities and offsets

Important

We are working on a new iteration of the connectivities and offsets explained here, to make them simpler and more user-friendly. This part of the interface may change soon.

The edge-to-cell table has one row per edge, with the indices of the cells at both sides of it. All the rows have the same length, but the edges on the boundary of the mesh have a cell at one side only. The entry of their row with no cell to point to, a missing neighbor, is filled with -1:

edge_to_cell_table = np.array(
    [
        [0, -1],  # edge 0 (neighbors: cell 0)
        [2, -1],  # edge 1
        [2, -1],  # edge 2
        [3, -1],  # edge 3
        [4, -1],  # edge 4
        [5, -1],  # edge 5
        [0, 5],  # edge 6 (neighbors: cell 0, cell 5)
        [0, 1],  # edge 7
        [1, 2],  # edge 8
        [1, 3],  # edge 9
        [3, 4],  # edge 10
        [4, 5],  # edge 11
    ]
)

Three objects describe this relation in GT4Py:

# 1. A local dimension, which numbers the neighbors of each edge (0 and 1)
E2CDim = gtx.Dimension("E2C", kind=gtx.DimensionKind.LOCAL)

# 2. An offset, the name used inside operators to go from the edges to their cells
E2C = gtx.FieldOffset("E2C", source=Cell, target=(Edge, E2CDim))

# 3. A connectivity, which holds the actual table
e2c_connectivity = gtx.as_connectivity([Edge, E2CDim], codomain=Cell, data=edge_to_cell_table)

print(e2c_connectivity.domain)
Domain(Edge[horizontal]=(0:12), E2C[local]=(0:2))
  • In the offset, source is the dimension of the field which is read (cells), and target lists the dimensions of the result: the edges, plus the local dimension numbering the neighbors. E2C therefore turns a field on cells into a field on edges with an extra local dimension.

  • In as_connectivity, the first argument gives the dimensions of the table and codomain is the dimension its entries point to.

  • The value marking the missing neighbors is the skip_value of the connectivity. as_connectivity uses -1 when the table contains any -1, and a different value can be given with the skip_value argument.

The offset is only a symbol. This keeps the operators independent of the mesh: the same code runs on a different mesh by providing a different table.

Important

Give the same name to the local dimension and to the offset ("E2C" above). The reductions over the neighbors rely on it to find the connectivity.

The offset provider

When an operator using offsets is called, the connectivities are passed in the offset_provider argument, a dictionary from the names of the offsets to the connectivities:

offset_provider = {"E2C": e2c_connectivity}

Premap and neighbors shows how the offsets are used in operators.