landlab.utils.matrix

Functions to set up a finite-volume solution matrix for a landlab grid.

get_core_node_at_node(grid)[source]

Get node ids as numbered by core nodes.

Get the core node ID for each node of a grid. If a node is not a core node, then use -1.

Parameters:

grid (ModelGrid) – A ModelGrid.

Returns:

Ids of each of the grid’s core nodes.

Return type:

ndarray of int

Examples

>>> from landlab import RasterModelGrid
>>> from landlab.utils import get_core_node_at_node
>>> grid = RasterModelGrid((4, 5))
>>> get_core_node_at_node(grid)
array([-1, -1, -1, -1, -1,
       -1,  0,  1,  2, -1,
       -1,  3,  4,  5, -1,
       -1, -1, -1, -1, -1])
>>> grid.status_at_node[13] = grid.BC_NODE_IS_FIXED_VALUE
>>> grid.status_at_node[2] = grid.BC_NODE_IS_CLOSED
>>> get_core_node_at_node(grid)
array([-1, -1, -1, -1, -1,
       -1,  0,  1,  2, -1,
       -1,  3,  4, -1, -1,
       -1, -1, -1, -1, -1])
get_core_node_matrix(grid, value_at_node, coef_at_link=None)[source]

A matrix for core nodes and a right-hand side vector.

Construct and return a matrix for the core nodes, plus a right-hand side vector containing values based on the input array value_at_node. Optionally, coef_at_link provides coefficients for each link (default is 1.0).

Parameters:
  • grid (RasterModelGrid, HexModelGrid) – A landlab grid.

  • value_at_node (ndarray) – Values defined at nodes used to construct the right-hand side vector.

  • coef_at_link (ndarray, optional) – Coefficents at links used to construct the matrix. If not provided, use 1.0.

Examples

>>> from landlab import RasterModelGrid
>>> from landlab.utils import get_core_node_matrix
>>> grid = RasterModelGrid((4, 5))
>>> grid.status_at_node[13] = grid.BC_NODE_IS_FIXED_VALUE
>>> grid.status_at_node[2] = grid.BC_NODE_IS_CLOSED
>>> vals = np.arange(
...     grid.number_of_nodes, dtype=np.double
... )  # made-up state variable array
>>> mat, rhs = get_core_node_matrix(grid, vals)
>>> mat.toarray()
array([[-4.,  1.,  0.,  1.,  0.],
       [ 1., -3.,  1.,  0.,  1.],
       [ 0.,  1., -4.,  0.,  0.],
       [ 1.,  0.,  0., -4.,  1.],
       [ 0.,  1.,  0.,  1., -4.]])
>>> rhs
array([[ -6.],
       [  0.],
       [-25.],
       [-26.],
       [-30.]])
>>> coefs = np.arange(grid.number_of_links, dtype=np.double)  # coefficient array
>>> mat, rhs = get_core_node_matrix(grid, vals, coef_at_link=coefs)
>>> mat.toarray()
array([[-38.,  10.,   0.,  14.,   0.],
       [ 10., -36.,  11.,   0.,  15.],
       [  0.,  11., -46.,   0.,   0.],
       [ 14.,   0.,   0., -74.,  19.],
       [  0.,  15.,   0.,  19., -78.]])
>>> rhs
array([[ -6.],
       [  0.],
       [-25.],
       [-26.],
       [-30.]])
get_matrix_entries(grid, coef_at_link=None)[source]

Get entries of a sparse matrix.

Parameters:
  • grid (RasterModelGrid, HexModelGrid) – A landlab grid.

  • coef_at_link (ndarray) – Coefficients at links used to construct the matrix.

Returns:

Values of matrix elements along with their corresponding row and column index.

Return type:

tuple of (data, (row_ind, col_inds))