def test_bad_coef_at_link(diff): grid = RasterModelGrid((5, 6)) value_at_node = np.full(grid.number_of_nodes, 1.0) coef_at_link = np.full(grid.number_of_links + diff, 1.0) with pytest.raises(ValueError): get_core_node_matrix(grid, value_at_node, coef_at_link=coef_at_link)
def test_scalar_value_at_node(Grid): grid = Grid((4, 5)) _, expected = get_core_node_matrix(grid, np.full(grid.number_of_nodes, 11.0)) _, actual = get_core_node_matrix(grid, 11.0) assert_array_equal(expected, actual)
def test_scalar_coef_at_link(Grid): grid = Grid((4, 5)) expected, _ = get_core_node_matrix(grid, 1.0, coef_at_link=np.full( grid.number_of_links, 11.0)) actual, _ = get_core_node_matrix(grid, 1.0, coef_at_link=11.0) assert_array_equal(expected.toarray(), actual.toarray())
def test_coef_at_link(Grid): grid = Grid((4, 5)) value_at_node = np.full(grid.number_of_nodes, 1.0) coef_at_link = np.full(grid.number_of_links, 1.0) mat, rhs = get_core_node_matrix(grid, value_at_node, coef_at_link=coef_at_link) actual, actual_rhs = get_core_node_matrix(grid, value_at_node, coef_at_link * 2.0) assert_array_equal(mat.toarray(), actual.toarray() / 2.0) assert_array_equal(rhs, actual_rhs)
def test_default_coef_is_one(Grid): grid = Grid((4, 5)) grid.status_at_node[13] = grid.BC_NODE_IS_FIXED_VALUE grid.status_at_node[2] = grid.BC_NODE_IS_CLOSED value_at_node = np.arange(grid.number_of_nodes, dtype=float) coef_at_link = np.full(grid.number_of_links, 1.0, dtype=float) expected, _ = get_core_node_matrix(grid, value_at_node, coef_at_link=coef_at_link) actual, _ = get_core_node_matrix(grid, value_at_node) assert_array_equal(expected.toarray(), actual.toarray())
def test_small_grids(): grid = RasterModelGrid((3, 3)) value_at_node = np.full(grid.number_of_nodes, 1.0) mat, rhs = get_core_node_matrix(grid, value_at_node) assert_array_equal(mat.toarray(), [[-4.0]]) assert_array_equal(rhs, [[-4.0]]) grid = RasterModelGrid((4, 4)) value_at_node = np.full(grid.number_of_nodes, 1.0) mat, rhs = get_core_node_matrix(grid, value_at_node) assert_array_equal( mat.toarray(), [ [-4.0, 1.0, 1.0, 0.0], [1.0, -4.0, 0.0, 1.0], [1.0, 0.0, -4.0, 1.0], [0.0, 1.0, 1.0, -4.0], ], ) assert_array_equal(rhs, [[-2.0], [-2.0], [-2.0], [-2.0]])
def run_one_step(self): """Calculate the tidal flow field and water-surface elevation.""" # Tidal mean water depth and water surface elevation at nodes # (Note: mean water surf elev only used for boundary conditions in # matrix construction; should be mean sea level) self._calc_effective_water_depth() self._boundary_mean_water_surf_elev[:] = self._mean_sea_level # Map water depth to links map_min_of_link_nodes_to_link(self.grid, self._water_depth, out=self._water_depth_at_links) # Calculate velocity and diffusion coefficients on links velocity_coef = self._water_depth_at_links**_FOUR_THIRDS / ( (self.roughness**2) * self._scale_velocity) self._diffusion_coef_at_links[:] = self._water_depth_at_links * velocity_coef # Calculate inundation / drainage rate at nodes tidal_inundation_rate = self.calc_tidal_inundation_rate() # Set up right-hand-side (RHS) vector for both ebb and flood tides (only # difference is in the sign) cores = self.grid.core_nodes # For flood tide, set up matrix and add boundary info to RHS vector # mat, rhs = make_core_node_matrix_var_coef( mat, rhs = get_core_node_matrix( self.grid, self._boundary_mean_water_surf_elev, coef_at_link=self._diffusion_coef_at_links, ) rhs[:, 0] += self._grid_multiplier * tidal_inundation_rate[cores] # Solve for flood tide water-surface elevation tidal_wse = np.zeros(self.grid.number_of_nodes) tidal_wse[self.grid.core_nodes] = spsolve(mat, rhs) # Calculate flood-tide water-surface gradient at links tidal_wse_grad = np.zeros(self.grid.number_of_links) self.grid.calc_grad_at_link(tidal_wse, out=tidal_wse_grad) # Calculate flow velocity field at links for flood tide, and assign # negative of the flood tide values to ebb tide self._flood_tide_vel[self.grid.active_links] = ( -velocity_coef[self.grid.active_links] * tidal_wse_grad[self.grid.active_links]) self._ebb_tide_vel[:] = -self._flood_tide_vel
def test_hex_grid(): grid = HexModelGrid((4, 3)) value_at_node = np.full(grid.number_of_nodes, 1.0) coef_at_link = np.full(grid.number_of_links, 1.0) mat, rhs = get_core_node_matrix(grid, value_at_node, coef_at_link=coef_at_link) assert_array_equal( mat.toarray(), [ [-6.0, 1.0, 1.0, 1.0, 0.0], [1.0, -6.0, 0.0, 1.0, 1.0], [1.0, 0.0, -6.0, 1.0, 0.0], [1.0, 1.0, 1.0, -6.0, 1.0], [0.0, 1.0, 0.0, 1.0, -6.0], ], ) assert_array_equal(rhs, [[-3.0], [-3.0], [-4.0], [-2.0], [-4.0]])
def test_default_bc(): grid = RasterModelGrid((5, 6)) value_at_node = np.full(grid.number_of_nodes, 1.0, dtype=float) mat, rhs = get_core_node_matrix(grid, value_at_node) assert_array_equal( mat.toarray(), [ [-4.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, -4.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, -4.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, -4.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, -4.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 1.0, -4.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -4.0, 1.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -4.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -4.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -4.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -4.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -4.0], ], ) assert_array_equal( rhs, [ [-2.0], [-1.0], [-1.0], [-2.0], [-1.0], [0.0], [0.0], [-1.0], [-2.0], [-1.0], [-1.0], [-2.0], ], )
def test_with_fixed_value_bc(): 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 value_at_node = np.arange(grid.number_of_nodes, dtype=float) mat, rhs = get_core_node_matrix(grid, value_at_node) assert_array_equal( mat.toarray(), [ [-4.0, 1.0, 0.0, 1.0, 0.0], [1.0, -3.0, 1.0, 0.0, 1.0], [0.0, 1.0, -4.0, 0.0, 0.0], [1.0, 0.0, 0.0, -4.0, 1.0], [0.0, 1.0, 0.0, 1.0, -4.0], ], ) assert_array_equal( rhs, [[-6.0], [0.0], [-25.0], [-26.0], [-30.0]], )
def test_with_coef(): 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 value_at_node = np.arange(grid.number_of_nodes, dtype=float) coef_at_link = np.arange(grid.number_of_links, dtype=np.double) mat, rhs = get_core_node_matrix(grid, value_at_node, coef_at_link=coef_at_link) assert_array_equal( mat.toarray(), [ [-38.0, 10.0, 0.0, 14.0, 0.0], [10.0, -36.0, 11.0, 0.0, 15.0], [0.0, 11.0, -46.0, 0.0, 0.0], [14.0, 0.0, 0.0, -74.0, 19.0], [0.0, 15.0, 0.0, 19.0, -78.0], ], ) assert_array_equal(rhs, [[-6.0], [0.0], [-25.0], [-26.0], [-30.0]])