Exemplo n.º 1
0
def calc_grad_at_link(grid, node_values, out=None):
    """Calculate gradients in node_values at links.

    Construction::

        calc_grad_at_link(grid, node_values, out=None)

    Parameters
    ----------
    grid : RasterModelGrid
        A grid.
    node_values : array_like or field name
        Values at nodes.
    out : ndarray, optional
        Buffer to hold result. If `None`, create a new array.

    Returns
    -------
    ndarray
        Gradients of the nodes values for each link.

    Examples
    --------
    >>> from landlab import RasterModelGrid
    >>> grid = RasterModelGrid((3, 3))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_link(node_values)
    array([ 0.,  0.,  1.,  3.,  1.,  2., -2.,  1., -1.,  1.,  0.,  0.])

    >>> out = np.empty(grid.number_of_links, dtype=float)
    >>> rtn = grid.calc_grad_at_link(node_values, out=out)
    >>> rtn is out
    True
    >>> out
    array([ 0.,  0.,  1.,  3.,  1.,  2., -2.,  1., -1.,  1.,  0.,  0.])

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> grid.calc_grad_at_link(node_values)
    array([ 0.,  0.,  1.,  3.,  1.,  1., -1.,  1., -1.,  1.,  0.,  0.])
    >>> _ = grid.add_field('node', 'elevation', node_values)
    >>> grid.calc_grad_at_link('elevation')
    array([ 0.,  0.,  1.,  3.,  1.,  1., -1.,  1., -1.,  1.,  0.,  0.])

    LLCATS: LINF GRAD
    """
    grads = gradients.calc_diff_at_link(grid, node_values, out=out)
    grads /= grid.length_of_link[:grid.number_of_links]

#    n_vertical_links = (grid.shape[0] - 1) * grid.shape[1]
#    diffs[:n_vertical_links] /= grid.dy
#    diffs[n_vertical_links:] /= grid.dx

    return grads
Exemplo n.º 2
0
def calc_grad_at_link(grid, node_values, out=None):
    """Calculate gradients in node_values at links.

    Construction::

        calc_grad_at_link(grid, node_values, out=None)

    Parameters
    ----------
    grid : RasterModelGrid
        A grid.
    node_values : array_like or field name
        Values at nodes.
    out : ndarray, optional
        Buffer to hold result. If `None`, create a new array.

    Returns
    -------
    ndarray
        Gradients of the nodes values for each link.

    Examples
    --------
    >>> from landlab import RasterModelGrid
    >>> grid = RasterModelGrid((3, 3))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_link(node_values)
    array([ 0.,  0.,  1.,  3.,  1.,  2., -2.,  1., -1.,  1.,  0.,  0.])

    >>> out = np.empty(grid.number_of_links, dtype=float)
    >>> rtn = grid.calc_grad_at_link(node_values, out=out)
    >>> rtn is out
    True
    >>> out
    array([ 0.,  0.,  1.,  3.,  1.,  2., -2.,  1., -1.,  1.,  0.,  0.])

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> grid.calc_grad_at_link(node_values)
    array([ 0.,  0.,  1.,  3.,  1.,  1., -1.,  1., -1.,  1.,  0.,  0.])
    >>> _ = grid.add_field('node', 'elevation', node_values)
    >>> grid.calc_grad_at_link('elevation')
    array([ 0.,  0.,  1.,  3.,  1.,  1., -1.,  1., -1.,  1.,  0.,  0.])
    """
    grads = gradients.calc_diff_at_link(grid, node_values, out=out)
    grads /= grid.length_of_link[:grid.number_of_links]

    #    n_vertical_links = (grid.shape[0] - 1) * grid.shape[1]
    #    diffs[:n_vertical_links] /= grid.dy
    #    diffs[n_vertical_links:] /= grid.dx

    return grads
Exemplo n.º 3
0
def calc_grad_at_active_link(grid, node_values, out=None):
    """Calculate gradients over active links.

    .. deprecated:: 0.1
        Use :func:`calc_grad_across_cell_faces`
                or :func:`calc_grad_across_cell_corners` instead

    Calculates the gradient in quantity s at each active link in the grid.
    This is nearly identical to the method of the same name in ModelGrid,
    except that it uses a constant node spacing for link length to improve
    efficiency.

    Note that a negative gradient corresponds to a lower node in the
    direction of the link.

    Returns
    -------
    ndarray
        Gradients of the nodes values for each link.

    Examples
    --------
    >>> import numpy as np
    >>> from landlab import RasterModelGrid
    >>> grid = RasterModelGrid(4, 5, 1.0)
    >>> u = [0., 1., 2., 3., 0.,
    ...      1., 2., 3., 2., 3.,
    ...      0., 1., 2., 1., 2.,
    ...      0., 0., 2., 2., 0.]
    >>> grad = grid.calc_grad_at_active_link(u)
    >>> grad # doctest: +NORMALIZE_WHITESPACE
    array([ 1.,  1., -1.,
            1.,  1., -1.,  1.,
           -1., -1., -1.,
            1.,  1., -1.,  1.,
           -1.,  0.,  1.])

    For greater speed, sending a pre-created numpy array as an argument
    avoids having to create a new one with each call:

    >>> grad = np.empty(grid.number_of_active_links)
    >>> rtn = grid.calc_grad_at_active_link(u, out=grad)
    >>> grad # doctest: +NORMALIZE_WHITESPACE
    array([ 1.,  1., -1.,
            1.,  1., -1.,  1.,
           -1., -1., -1.,
            1.,  1., -1.,  1.,
           -1.,  0.,  1.])
    >>> rtn is grad
    True

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_active_link(node_values)
    array([ 3.,  1., -1., -1.])

    This function is *deprecated*. Instead, use ``calc_grad_at_link``.

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_link(node_values)[grid.active_links]
    array([ 3.,  1., -1., -1.])

    LLCATS: LINF GRAD
    """
    if out is None:
        out = grid.empty(at='active_link')

    if len(out) != grid.number_of_active_links:
        raise ValueError('output buffer does not match that of the grid.')

    # grads = gradients.calculate_diff_at_active_links(grid, node_values,
    #                                                  out=out)
    grads = gradients.calc_diff_at_link(grid, node_values)
    out[:] = grads[grid.active_links]
    out /= grid.length_of_link[grid.active_links]

    return out
Exemplo n.º 4
0
def calc_grad_at_active_link(grid, node_values, out=None):
    """Calculate gradients over active links.

    .. deprecated:: 0.1
        Use :func:`calc_grad_across_cell_faces`
                or :func:`calc_grad_across_cell_corners` instead

    Calculates the gradient in quantity s at each active link in the grid.
    This is nearly identical to the method of the same name in ModelGrid,
    except that it uses a constant node spacing for link length to improve
    efficiency.

    Note that a negative gradient corresponds to a lower node in the
    direction of the link.

    Returns
    -------
    ndarray
        Gradients of the nodes values for each link.

    Examples
    --------
    >>> import numpy as np
    >>> from landlab import RasterModelGrid
    >>> grid = RasterModelGrid(4, 5, 1.0)
    >>> u = [0., 1., 2., 3., 0.,
    ...      1., 2., 3., 2., 3.,
    ...      0., 1., 2., 1., 2.,
    ...      0., 0., 2., 2., 0.]
    >>> grad = grid.calc_grad_at_active_link(u)
    >>> grad # doctest: +NORMALIZE_WHITESPACE
    array([ 1.,  1., -1.,
            1.,  1., -1.,  1.,
           -1., -1., -1.,
            1.,  1., -1.,  1.,
           -1.,  0.,  1.])

    For greater speed, sending a pre-created numpy array as an argument
    avoids having to create a new one with each call:

    >>> grad = np.empty(grid.number_of_active_links)
    >>> rtn = grid.calc_grad_at_active_link(u, out=grad)
    >>> grad # doctest: +NORMALIZE_WHITESPACE
    array([ 1.,  1., -1.,
            1.,  1., -1.,  1.,
           -1., -1., -1.,
            1.,  1., -1.,  1.,
           -1.,  0.,  1.])
    >>> rtn is grad
    True

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_active_link(node_values)
    array([ 3.,  1., -1., -1.])

    This function is *deprecated*. Instead, use ``calc_grad_at_link``.

    >>> grid = RasterModelGrid((3, 3), spacing=(1, 2))
    >>> node_values = [0., 0., 0.,
    ...                1., 3., 1.,
    ...                2., 2., 2.]
    >>> grid.calc_grad_at_link(node_values)[grid.active_links]
    array([ 3.,  1., -1., -1.])
    """
    if out is None:
        out = grid.empty(at='active_link')

    if len(out) != grid.number_of_active_links:
        raise ValueError('output buffer does not match that of the grid.')

    # grads = gradients.calculate_diff_at_active_links(grid, node_values,
    #                                                  out=out)
    grads = gradients.calc_diff_at_link(grid, node_values)
    out[:] = grads[grid.active_links]
    out /= grid.length_of_link[grid.active_links]

    return out