pystencils.grids.patch_data.PatchData#

class pystencils.grids.patch_data.PatchData(patch, vars=None, *, fields=(), target=<Target._CPU: 1>, default_dtype=<class 'numpy.float64'>, index_dtype=<class 'numpy.int64'>)#

Manage simulation data and field arrays for a given patch.

PatchData is the runtime incarnation for pystencils patches; instances of PatchData manage simulation data associated with a single Patch. A Patch serves as a blueprint for PatchData objects. When creating a PatchData, numerical values must be provided for all symbols occuring in the patch’s definition; the symbolic expressions for the patch corners, numbers of vertices and cells, are thus evaluated to concrete numbers.

Multiple PatchData instances can be created for the same algebraic Patch; each can have different values for the patch’s defining symbols.

Operators can be invoked directly on PatchData objects to run them with the symbol values and data arrays stored by a given PatchData.

Data Management

PatchData allocates and manages values and data arrays for SymPy symbols and fields associated with a patch.

To set values for symbols, pass them in a dictionary to the PatchData constructor. You can set single symbols to single values, and also associate tuples of values with tuples of symbols. Value must be provided this way for all symbols occuring in the given patch.

Example:

Nv = ps.symbols("N_:3", ps.index_t)  # symbols for number of vertices
P = Patch("P", (1, 1, 1), num_vertices=Nv)

c = sp.Symbol("c")  # a single symbol

Pdata = PatchData(P, {Nv: (32, 16, 8), c: 0.01})

PatchData uses NumPy to convert and store symbol values with the correct data type, depending on their associated symbol (using default_dtype for untyped symbols, and the symbols’ type for typed symbols).

To allocate and manage data arrays for algebraic fields, pass them to the fields keyword argument:

f = TensorField("f", P.cells, ())
g = TensorField("g", P.cells, (3,))

Pdata = PatchData(P, {...}, fields=[f, g])

PatchData will then create ndarray instances for these fields. The array module (NumPy, CuPy, DPND) is inferred from on the target parameter; arrays are created through the CreateNdArray protocol (see IField).

Accessing Data

Symbol values and arrays can be accessed using the [] operator, e.g. Pdata[f] for the ndarray backing the field f. Arrays and values can also be set through [].

Parameters:
property patch: Patch#

Blueprint patch of this data container

property dimensionality: int#

Dimensionality of this container’s patch

property x_min: ndarray[tuple[int, ...], dtype[float64]]#

Lower corner of this patch incarnation

property x_max: ndarray[tuple[int, ...], dtype[float64]]#

Upper corner of this patch incarnation

property extents: ndarray[tuple[int, ...], dtype[float64]]#

Extents of this patch incarnation

property spacing: ndarray[tuple[int, ...], dtype[float64]]#

Grid spacing of this patch incarnation

property num_vertices: tuple[int, ...]#

Number of vertices in this patch incarnation

property num_cells: tuple[int, ...]#

Number of cells in this patch incarnation

property target: Target#

Primary hardware target for this data container

property array_module: ModuleType#

Array module used for field arrays

property default_dtype: dtype#

Default data type for field arrays and symbol values.

This data type will be used for values of untyped symbols, in arrays for fields typed as DynamicType.NUMERIC_TYPE, as well as the patch’s geometry attributes.

set_data(key, value)#

Store a value for a symbol, converting it to the correct data type.

If symb is an untyped symbol, the value will be converted to the default_dtype.

Parameters:
swap(k0, k1)#

Swap the data objects of two keys.

asnumpy(key)#

Return a copy of the data array for the given key as a NumPy array.

If key is backed by a NumPy, CuPy or DPNP ndarray, copies the data into an new NumPy array and returns that copy.

Raises:

KeyError – If key is either not a valid data key, or does not store an ndarray.

Return type:

ndarray

Parameters:

key (Any)