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.
PatchDatais the runtime incarnation for pystencilspatches; instances ofPatchDatamanage simulation data associated with a singlePatch. APatchserves as a blueprint forPatchDataobjects. When creating aPatchData, 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
PatchDatainstances can be created for the same algebraicPatch; each can have different values for the patch’s defining symbols.Operatorscan be invoked directly onPatchDataobjects to run them with the symbol values and data arrays stored by a givenPatchData.Data Management
PatchDataallocates 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
PatchDataconstructor. 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})
PatchDatauses NumPy to convert and store symbol values with the correct data type, depending on their associated symbol (usingdefault_dtypefor untyped symbols, and the symbols’ type for typed symbols).To allocate and manage data arrays for algebraic fields, pass them to the
fieldskeyword argument:f = TensorField("f", P.cells, ()) g = TensorField("g", P.cells, (3,)) Pdata = PatchData(P, {...}, fields=[f, g])
PatchDatawill then createndarrayinstances for these fields. The array module (NumPy, CuPy, DPND) is inferred from on thetargetparameter; arrays are created through theCreateNdArrayprotocol (seeIField).Accessing Data
Symbol values and arrays can be accessed using the
[]operator, e.g.Pdata[f]for thendarraybacking the fieldf. Arrays and values can also be set through[].- Parameters:
patch (
Patch) – The blueprint patch for this data containervars (
Optional[Mapping[Symbol|tuple[Symbol,...],Any]]) – Dictionary mapping symbols to their valuesfields (
Iterable[IField]) – Fields associated with the patch for which data arrays should be allocatedtarget (
Target) – Hardware target with which this patch data is used. Should match thetargetof any operators run on this patch.default_dtype (
str|type|dtype|PsType) – The default numeric data type for symbol values and data arrays; used for untyped symbols and wheneverDynamicType.NUMERIC_TYPEis encounteredindex_dtype (
str|type|dtype|PsType) – The index data type used for arrays; substituted wheneverDynamicType.INDEX_TYPEis encountered.
- 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
symbis an untyped symbol, thevaluewill be converted to thedefault_dtype.
- 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
keyis backed by a NumPy, CuPy or DPNPndarray, copies the data into an new NumPy array and returns that copy.