pystencils.field.fields#
- pystencils.field.fields(description=None, index_dimensions=0, layout=None, field_type=FieldType.GENERIC, **kwargs)#
Creates pystencils fields from a string description.
The description must be a string of the form
"name(index-shape) [name(index-shape) ...]: <data-type>[<dimension-or-shape>]"
, where:name
is the name of the respective field(index-shape)
is a tuple of integers describing the shape of the tensor on each field node (can be omitted for scalar fields)<data-type>
is the numerical data type of the field’s entries; this can be any type parseable bycreate_type
, as well asdyn
forDynamicType.NUMERIC_TYPE
anddynidx
forDynamicType.INDEX_TYPE
.<dimension-or-shape>
can be a dimensionality (e.g.1D
,2D
,3D
) or a tuple of integers defining the spatial shape of the field.
Examples
- Create a 3D scalar field of default numeric type:
>>> f = fields("f(1): [2D]") >>> str(f.dtype) 'DynamicType.NUMERIC_TYPE'
- Create a 2D scalar and vector field of 64-bit float type:
>>> s, v = fields("s, v(2): double[2D]") >>> assert s.spatial_dimensions == 2 and s.index_dimensions == 0 >>> assert (v.spatial_dimensions, v.index_dimensions, v.index_shape) == (2, 1, (2,))
- Create an integer field of shape (10, 20):
>>> f = fields("f : int32[10, 20]") >>> f.has_fixed_shape, f.shape (True, (10, 20))
- Numpy arrays can be used as template for shape and data type of field:
>>> arr_s, arr_v = np.zeros([20, 20]), np.zeros([20, 20, 2]) >>> s, v = fields("s, v(2)", s=arr_s, v=arr_v) >>> assert s.index_dimensions == 0 and s.dtype.numpy_dtype == arr_s.dtype >>> assert v.index_shape == (2,)
- Format string can be left out, field names are taken from keyword arguments.
>>> fields(f1=arr_s, f2=arr_s) [f1: float64[20,20], f2: float64[20,20]]
- The keyword names
index_dimension
andlayout
have special meaning, don’t use them for field names >>> f = fields(f=arr_v, index_dimensions=1) >>> assert f.index_dimensions == 1 >>> f = fields("pdfs(19) : float32[3D]", layout='fzyx') >>> f.layout (2, 1, 0)