Working with CellArrays

CellArrays are the storage primitive behind JustPIC particle containers. They represent a grid where every logical grid cell stores a small fixed-size payload, for example the particle slots belonging to that cell.

Instantiating a CellArray

JustPIC can create a CellArray object directly. The CellArray object is a container that holds the data of a grid. The data is stored in small nD-arrays, and the grid is divided into cells. Each cell contains a number of elements. The CellArray object is used to store the data of the particles in the simulation.

using JustPIC
import CellArraysIndexing as CAI
julia> ni = (2, 2)
(2, 2)

julia> ncells = (2,)
(2,)

julia> x = 20
20

julia> CA = cell_array(JustPIC.CPU, Float64(x), ncells, ni)
2×2 CellArrays.CPUCellArray{StaticArraysCore.SVector{2, Float64}, 2, 1, Float64}:
 [20.0, 20.0]  [20.0, 20.0]
 [20.0, 20.0]  [20.0, 20.0]

Indexing a CellArray

Indexing by grid cell returns the whole payload stored in that cell. This is convenient for inspection, but it materializes a StaticArray value:

julia> CA[1,1]
2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
 20.0
 20.0

It is however useful to read and mutate the data of the CellArray object directly, without instantiating a StaticArray. For this purpose, CellArraysIndexing provides @index to directly read and mutate the individual elements of the cell.

For example, to read a single element of CA:

julia> CAI.@index CA[2, 1, 1]
20.0

Here the first index selects the payload entry and the remaining indices select the grid cell. Mutation uses the same syntax:

julia> CAI.@index CA[2, 1, 1] = 0.0
0.0

julia> CA
2×2 CellArrays.CPUCellArray{StaticArraysCore.SVector{2, Float64}, 2, 1, Float64}:
 [20.0, 0.0]   [20.0, 20.0]
 [20.0, 20.0]  [20.0, 20.0]

@cell is the companion macro for reading or writing an entire cell payload:

julia> @cell CA[1,1]
2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
 20.0
 20.0
julia> @cell CA[1,1] = @cell(CA[1,1]) .+ 1
2-element StaticArraysCore.SVector{2, Float64} with indices SOneTo(2):
 21.0
 21.0

 julia> CA
2×2 CellArrays.CPUCellArray{StaticArraysCore.SVector{2, Float64}, 2, 1, Float64}:
 [21.0, 21.0]  [20.0, 20.0]
 [20.0, 20.0]  [20.0, 20.0]