(field: CenteredGrid,
                     extrapolation: math.Extrapolation = None,
                     type: type = CenteredGrid,
                     stack_dim: Shape = channel('vector'))
Finite difference spatial_gradient.

    This function can operate in two modes:

    * `type=CenteredGrid` approximates the spatial_gradient at cell centers using central differences
    * `type=StaggeredGrid` computes the spatial_gradient at face centers of neighbouring cells

    Args:
        field: centered grid of any number of dimensions (scalar field, vector field, tensor field)
        type: either `CenteredGrid` or `StaggeredGrid`
        stack_dim: Dimension to be added. This dimension lists the spatial_gradient w.r.t. the spatial dimensions.
            The `field` must not have a dimension of the same name.

    Returns:
        spatial_gradient field of type `type`.
Finite difference spatial_gradient.
Finite difference spatial_gradient .
def spatial_gradient(field: CenteredGrid,
                     extrapolation: math.Extrapolation = None,
                     type: type = CenteredGrid,
                     stack_dim: Shape = channel('vector')):
    """
    Finite difference spatial_gradient.

    This function can operate in two modes:

    * `type=CenteredGrid` approximates the spatial_gradient at cell centers using central differences
    * `type=StaggeredGrid` computes the spatial_gradient at face centers of neighbouring cells

    Args:
        field: centered grid of any number of dimensions (scalar field, vector field, tensor field)
        type: either `CenteredGrid` or `StaggeredGrid`
        stack_dim: Dimension to be added. This dimension lists the spatial_gradient w.r.t. the spatial dimensions.
            The `field` must not have a dimension of the same name.

    Returns:
        spatial_gradient field of type `type`.

    """
    assert isinstance(field, Grid)
    if extrapolation is None:
        extrapolation = field.extrapolation.spatial_gradient()
    if type == CenteredGrid:
        values = math.spatial_gradient(field.values, field.dx.vector.as_channel(name=stack_dim.name), difference='central', padding=field.extrapolation, stack_dim=stack_dim)
        return CenteredGrid(values, bounds=field.bounds, extrapolation=extrapolation)
    elif type == StaggeredGrid:
        assert stack_dim.name == 'vector'
        return stagger(field, lambda lower, upper: (upper - lower) / field.dx, extrapolation)
    raise NotImplementedError(f"{type(field)} not supported. Only CenteredGrid and StaggeredGrid allowed.")
def spatial_gradient ( field : CenteredGrid , extrapolation : math . Extrapolation = None , type : type = CenteredGrid , stack_dim : Shape = channel ( 'vector' ) ) : assert isinstance ( field , Grid ) if extrapolation is None : extrapolation = field . extrapolation . spatial_gradient ( ) if type == CenteredGrid : values = math . spatial_gradient ( field . values , field . dx . vector . as_channel ( name = stack_dim . name ) , difference = 'central' , padding = field . extrapolation , stack_dim = stack_dim ) return CenteredGrid ( values , bounds = field . bounds , extrapolation = extrapolation ) elif type == StaggeredGrid : assert stack_dim . name == 'vector' return stagger ( field , lambda lower , upper : ( upper - lower ) / field . dx , extrapolation ) raise NotImplementedError ( f"{type(field)} not supported. Only CenteredGrid and StaggeredGrid allowed." )