The term field is borrowed from mathematics and physics. If you already know scalar field (for exampl……

The term field is borrowed from mathematics and physics. If you already know scalar field (for example heat field), or vector field (for example gravitational field), then it is easy for you to understand fields in Taichi.

Fields in Taichi are the global data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a Scalar, a Vector, a Matrix, or a Struct.

Scalar fields

Scalar fields refer to the fields that store scalars and are the most basic fields.

Declaration

The simplest way to declare a scalar field is to call ti.field(dtype, shape), where dtype is a primitive data type as explained in the Type System and shape is a tuple of integers.

Scalar fields of higher dimensions can be similarly defined.

WARNING

Taichi only supports fields of dimensions ≤ 8.

Accessing elements in a scalar field

Once a field is declared, Taichi automatically initializes its elements to zero.

To access an element in a scalar field, you need to explicitly specify the element’s index.

note