def test_scalar_arg(): """Test using a scalar for cell arg.""" rmg = RasterModelGrid((4, 5)) values_at_nodes = np.arange(20.0) grads = calc_grad_across_cell_corners(rmg, values_at_nodes, 0) assert_array_equal(grads, np.array([[6.0, 4.0, -6.0, -4.0]]) / np.sqrt(2.0))
def test_scalar_arg(): """Test using a scalar for cell arg.""" rmg = RasterModelGrid((4, 5)) values_at_nodes = np.arange(20.) grads = calc_grad_across_cell_corners( rmg, values_at_nodes, 0) assert_array_equal(grads, np.array([[6., 4., -6., -4.]]) / np.sqrt(2.))
def _calc_steepest_descent_across_cell_corners(grid, node_values, *args, **kwds): """Get steepest gradient to the diagonals of a cell. Calculate the gradients in *node_values* measure to the diagonals of cells IDs, *cell_ids*. Slopes upward from the cell are reported as positive. If *cell_ids* is not given, calculate gradients for all cells. Use the *return_node* keyword to return a tuple, with the first element being the gradients and the second the node id of the node in the direction of the minimum gradient, i.e., the steepest descent. Note the gradient value returned is probably thus negative. Parameters ---------- grid : RasterModelGrid Input grid. node_values : array_like Values to take gradient of. cell_ids : array_like, optional IDs of grid cells to measure gradients. return_node: boolean, optional If `True`, return node IDs of the node that has the steepest descent. out : ndarray, optional Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output. Returns ------- ndarray : Calculated gradients to lowest node across cell faces. Examples -------- Create a rectilinear grid that is 3 nodes by 3 nodes and so has one cell centered around node 4. >>> from landlab import RasterModelGrid >>> grid = RasterModelGrid(3, 3) >>> values_at_nodes = np.arange(9.) Calculate gradients to cell diagonals and choose the gradient to the lowest node. >>> from math import sqrt >>> grid._calc_steepest_descent_across_cell_corners( ... values_at_nodes) * sqrt(2.) array([-4.]) The steepest gradient is to node with id 0. >>> (_, ind) = grid._calc_steepest_descent_across_cell_corners( ... values_at_nodes, return_node=True) >>> ind array([0]) >>> grid = RasterModelGrid(3, 3) >>> node_values = grid.zeros() >>> node_values[0] = -1 >>> grid._calc_steepest_descent_across_cell_corners(node_values, 0) array([-0.70710678]) Get both the maximum gradient and the node to which the gradient is measured. >>> grid._calc_steepest_descent_across_cell_corners(node_values, 0, ... return_node=True) (array([-0.70710678]), array([0])) """ return_node = kwds.pop('return_node', False) cell_ids = make_optional_arg_into_id_array(grid.number_of_cells, *args) grads = calc_grad_across_cell_corners(grid, node_values, cell_ids) if return_node: ind = np.argmin(grads, axis=1) node_ids = grid.diagonal_cells[grid.node_at_cell[cell_ids], ind] if 'out' not in kwds: out = np.empty(len(cell_ids), dtype=grads.dtype) out[:] = grads[range(len(cell_ids)), ind] return (out, node_ids) else: return grads.min(axis=1, **kwds)
def test_scalar_arg(): """Test using a scalar for cell arg.""" grads = calc_grad_across_cell_corners( rmg, values_at_nodes, 0) assert_array_equal(grads, np.array([[6., 4., -6., -4.]]) / np.sqrt(2.))