def test_number_of_rings(): grid = RadialModelGrid(2) assert grid.number_of_rings == 2 assert isinstance(grid.number_of_rings, int) grid = RadialModelGrid(4) assert grid.number_of_rings == 4
def setup_voronoi(): """ Setup a simple 20 point Voronoi Delaunay grid (radial for ease) """ global fr, vmg global A_target_core, A_target_outlet vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) vmg.status_at_node[8:] = CLOSED_BOUNDARY z[7] = 0. # outlet inner_elevs = (3., 1., 4., 5., 6., 7., 8.) z[:7] = np.array(inner_elevs) vmg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(vmg) nodes_contributing = [np.array([0, 3, 4, 5]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([2, ]), np.array([3, ]), np.array([4, ]), np.array([5, ]), np.array([6, ])] A_target_core = np.zeros(vmg.number_of_core_nodes) for i in xrange(7): A_target_core[i] = vmg.area_of_cell[nodes_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum()
def setup_voronoi_closedinternal(): """ Close one of the formerly core internal nodes. """ global fr, vmg global A_target_internal, A_target_outlet vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) vmg.status_at_node[8:] = CLOSED_BOUNDARY vmg.status_at_node[0] = CLOSED_BOUNDARY # new internal closed z[7] = 0. # outlet inner_elevs = (3., 1., 4., 5., 6., 7., 8.) z[:7] = np.array(inner_elevs) vmg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(vmg) nodes_contributing = [[], np.array([1, 2, 3, 4, 5, 6]), np.array([2, 3, 4, 5]), np.array([3, 4, 5]), np.array([4, 5]), np.array([5, ]), np.array([6, ])] A_target_internal = np.zeros(vmg.number_of_core_nodes+1, dtype=float) for i in xrange(7): A_target_internal[i] = vmg.area_of_cell[nodes_contributing[i]].sum() A_target_outlet = vmg.area_of_cell[vmg.cell_at_node[vmg.core_nodes]].sum()
def test_voronoi(): """Test routing on a (radial) voronoi.""" vmg = RadialModelGrid(n_rings=2, nodes_in_first_ring=6) outlet_node = 11 z = np.full(vmg.number_of_nodes, 10.0) all_bounds_but_one = [0, 1, 2, 3, 4, 7, 14, 15, 16, 17, 18] vmg.status_at_node[all_bounds_but_one] = vmg.BC_NODE_IS_CLOSED z[outlet_node] = 0.0 # outlet z[vmg.core_nodes] = [7.0, 8.0, 6.0, 3.0, 1.0, 5.0, 4.0] vmg.add_field("topographic__elevation", z, at="node", units="-") fr = FlowAccumulator(vmg) # The follow list contains arrays with the IDs of cells contributing flow # to nodes 5, 6, 8, 9, 10, 13, and 14, respectively (which correspond to # cells 0-6) cells_contributing = [ np.array([0]), np.array([1]), np.array([2]), np.array([0, 3, 2, 5]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([5]), np.array([6]), ] A_target_core = np.zeros(len(vmg.core_nodes)) for i in range(len(cells_contributing)): A_target_core[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum() fr.run_one_step() assert vmg.at_node["drainage_area"][vmg.core_nodes] == pytest.approx(A_target_core) assert vmg.at_node["drainage_area"][11] == pytest.approx(A_target_outlet)
def test_radial_deprecate_origin_y(): with pytest.deprecated_call(): mg = RadialModelGrid(num_shells=1, dr=1.0, origin_y=10) assert mg._xy_of_center == (0.0, 10.0) pts, npts = mg._create_radial_points(1, 1, xy_of_center=mg._xy_of_center) assert pts[0, 0] == approx(mg._xy_of_center[0]) assert pts[0, 1] == approx(mg._xy_of_center[1])
def test_spacing_of_rings(): grid = RadialModelGrid(2, spacing=1.0) assert grid.spacing_of_rings == approx(1.0) assert isinstance(grid.spacing_of_rings, float) grid = RadialModelGrid(2, spacing=11.0) assert grid.spacing_of_rings == approx(11.0) assert isinstance(grid.spacing_of_rings, float)
def setup_voronoi(): """ Setup a simple 20 point Voronoi Delaunay grid (radial for ease) """ global fr, vmg global A_target_core, A_target_outlet vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) #vmg.status_at_node[8:] = CLOSED_BOUNDARY all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY #z[7] = 0. # outlet z[12] = 0. # outlet #inner_elevs = (3., 1., 4., 5., 6., 7., 8.) inner_elevs = (8., 7., 3., 1., 6., 4., 5.) #z[:7] = np.array(inner_elevs) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(vmg) # nodes_contributing = [np.array([0, 3, 4, 5]), # np.array([0, 1, 2, 3, 4, 5, 6]), # np.array([2, ]), # np.array([3, ]), # np.array([4, ]), # np.array([5, ]), # np.array([6, ])] # The follow list contains arrays with the IDs of cells contributing flow # to nodes 5, 6, 8, 9, 10, 13, and 14, respectively (which correspond to # cells 0-6) cells_contributing = [ np.array([ 0, ]), np.array([ 1, ]), np.array([1, 2, 4, 6]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([ 4, ]), np.array([ 5, ]), np.array([ 6, ]) ] A_target_core = np.zeros(vmg.number_of_core_nodes) for i in range(7): A_target_core[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum()
def test_move_reference_radial(random_xy): grid = RadialModelGrid(n_rings=9, nodes_in_first_ring=8, xy_of_center=random_xy) assert grid.xy_of_center == approx(random_xy) x, y = grid.x_of_node.copy(), grid.y_of_node.copy() grid.xy_of_center = (30.0, 45.0) assert grid.xy_of_center == (30.0, 45.0) assert grid.x_of_node - x == approx(30.0 - random_xy[0]) assert grid.y_of_node - y == approx(45.0 - random_xy[1])
def test_radius_at_node(): grid = RadialModelGrid(2, 8) assert grid.radius_at_node == approx([ 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 1.0, 0.0, 1.0, 2.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, ])
def test_radial_center_as_iterables(random_xy, to_iterable): expected = approx(tuple(random_xy)) grid = RadialModelGrid(num_shells=9, dr=10.0, xy_of_center=to_iterable(random_xy)) assert isinstance(grid.xy_of_center, tuple) assert grid.xy_of_center == expected
def test_voronoi(): """Test routing on a (radial) voronoi.""" vmg = RadialModelGrid(2, dr=2.0) z = np.full(20, 10.0, dtype=float) # vmg.status_at_node[8:] = CLOSED_BOUNDARY all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY # z[7] = 0. # outlet z[12] = 0.0 # outlet # inner_elevs = (3., 1., 4., 5., 6., 7., 8.) inner_elevs = (8.0, 7.0, 3.0, 1.0, 6.0, 4.0, 5.0) # z[:7] = np.array(inner_elevs) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field("node", "topographic__elevation", z, units="-") with pytest.deprecated_call(): fr = FlowRouter(vmg) # nodes_contributing = [np.array([0, 3, 4, 5]), # np.array([0, 1, 2, 3, 4, 5, 6]), # np.array([2, ]), # np.array([3, ]), # np.array([4, ]), # np.array([5, ]), # np.array([6, ])] # The follow list contains arrays with the IDs of cells contributing flow # to nodes 5, 6, 8, 9, 10, 13, and 14, respectively (which correspond to # cells 0-6) cells_contributing = [ np.array([0]), np.array([1]), np.array([1, 2, 4, 6]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([4]), np.array([5]), np.array([6]), ] A_target_core = np.zeros(vmg.number_of_core_nodes) for i in range(7): A_target_core[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum() fr.run_one_step() assert vmg.at_node["drainage_area"][vmg.core_nodes] == pytest.approx( A_target_core) assert vmg.at_node["drainage_area"][12] == pytest.approx(A_target_outlet)
def test_radial_center_as_iterables(random_xy, to_iterable): expected = approx(tuple(random_xy)) grid = RadialModelGrid(n_rings=9, nodes_in_first_ring=8, xy_of_center=to_iterable(random_xy)) assert isinstance(grid.xy_of_center, tuple) assert grid.xy_of_center == expected
def test_voronoi(): """Test routing on a (radial) voronoi.""" vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) # vmg.status_at_node[8:] = CLOSED_BOUNDARY all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY # z[7] = 0. # outlet z[12] = 0. # outlet # inner_elevs = (3., 1., 4., 5., 6., 7., 8.) inner_elevs = (8., 7., 3., 1., 6., 4., 5.) # z[:7] = np.array(inner_elevs) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field("node", "topographic__elevation", z, units="-") fr = FlowRouter(vmg) # nodes_contributing = [np.array([0, 3, 4, 5]), # np.array([0, 1, 2, 3, 4, 5, 6]), # np.array([2, ]), # np.array([3, ]), # np.array([4, ]), # np.array([5, ]), # np.array([6, ])] # The follow list contains arrays with the IDs of cells contributing flow # to nodes 5, 6, 8, 9, 10, 13, and 14, respectively (which correspond to # cells 0-6) cells_contributing = [ np.array([0]), np.array([1]), np.array([1, 2, 4, 6]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([4]), np.array([5]), np.array([6]), ] A_target_core = np.zeros(vmg.number_of_core_nodes) for i in range(7): A_target_core[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum() fr.route_flow() assert vmg.at_node["drainage_area"][vmg.core_nodes] == approx(A_target_core) assert vmg.at_node["drainage_area"][12] == approx(A_target_outlet)
def setup_voronoi(): """ Setup a simple 20 point Voronoi Delaunay grid (radial for ease) """ global fr, vmg global A_target_core, A_target_outlet vmg = RadialModelGrid(2, dr=2.0) z = np.full(20, 10.0, dtype=float) # vmg.status_at_node[8:] = CLOSED_BOUNDARY all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY # z[7] = 0. # outlet z[12] = 0.0 # outlet # inner_elevs = (3., 1., 4., 5., 6., 7., 8.) inner_elevs = (8.0, 7.0, 3.0, 1.0, 6.0, 4.0, 5.0) # z[:7] = np.array(inner_elevs) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field("node", "topographic__elevation", z, units="-") fr = FlowRouter(vmg) # nodes_contributing = [np.array([0, 3, 4, 5]), # np.array([0, 1, 2, 3, 4, 5, 6]), # np.array([2, ]), # np.array([3, ]), # np.array([4, ]), # np.array([5, ]), # np.array([6, ])] # The follow list contains arrays with the IDs of cells contributing flow # to nodes 5, 6, 8, 9, 10, 13, and 14, respectively (which correspond to # cells 0-6) cells_contributing = [ np.array([0]), np.array([1]), np.array([1, 2, 4, 6]), np.array([0, 1, 2, 3, 4, 5, 6]), np.array([4]), np.array([5]), np.array([6]), ] A_target_core = np.zeros(vmg.number_of_core_nodes) for i in range(7): A_target_core[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell.sum()
def test_grid_from_dict_radial(xy_of_center): kwds = {} if xy_of_center is not None: kwds["xy_of_center"] = xy_of_center expected = RadialModelGrid(3, 0.5, **kwds) actual = grid_from_dict("RadialModelGrid", (3, 0.5, kwds)) assert_array_almost_equal(expected.x_of_node, actual.x_of_node) assert_array_almost_equal(expected.y_of_node, actual.y_of_node)
def setup_voronoi_closedinternal(): """ Close one of the formerly core internal nodes. """ global fr, vmg global A_target_internal, A_target_outlet vmg = RadialModelGrid(2, dr=2.0) z = np.full(20, 10.0, dtype=float) # vmg.status_at_node[8:] = CLOSED_BOUNDARY # vmg.status_at_node[0] = CLOSED_BOUNDARY # new internal closed # z[7] = 0. # outlet # inner_elevs = (3., 1., 4., 5., 6., 7., 8.) # z[:7] = np.array(inner_elevs) all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY vmg.status_at_node[8] = CLOSED_BOUNDARY # new internal closed z[12] = 0.0 # outlet inner_elevs = (8.0, 7.0, 1.0, 6.0, 4.0, 5.0) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field("node", "topographic__elevation", z, units="-") fr = FlowRouter(vmg) # nodes_contributing = [[], # np.array([1, 2, 3, 4, 5, 6]), # np.array([2, 3, 4, 5]), # np.array([3, 4, 5]), # np.array([4, 5]), # np.array([5, ]), # np.array([6, ])] cells_contributing = [ np.array([0]), np.array([1]), np.array([0, 1, 3, 4, 5, 6]), np.array([1, 4]), np.array([1, 4, 5, 6]), np.array([1, 4, 6]), ] A_target_internal = np.zeros(vmg.number_of_core_nodes, dtype=float) for i in range(6): A_target_internal[i] = vmg.area_of_cell[cells_contributing[i]].sum() # A_target_internal[i] = vmg.area_of_cell[nodes_contributing[i]].sum() A_target_outlet = vmg.area_of_cell[vmg.cell_at_node[vmg.core_nodes]].sum()
def test_radial_from_dict(): params = { "n_rings": 5, "nodes_in_first_ring": 4, "xy_of_center": (35, 55), "xy_axis_name": ("spam", "eggs"), "xy_axis_units": ("smoot", "parsec"), "xy_of_reference": (12345, 678910), } grid_a = RadialModelGrid.from_dict(params) grid_b = RadialModelGrid(**params) assert grid_a.number_of_nodes == grid_b.number_of_nodes assert grid_a.xy_of_center == grid_b.xy_of_center == (35, 55) assert grid_a.xy_of_node == pytest.approx(grid_b.xy_of_node) assert grid_a.axis_units == ("smoot", "parsec") assert grid_a.axis_name == ("spam", "eggs") assert grid_a.xy_of_reference == (12345, 678910)
def test_voronoi_closedinternal(): """Test routing on a (radial) voronoi, but with a closed interior node.""" vmg = RadialModelGrid(n_rings=2, nodes_in_first_ring=6) outlet_node = 11 z = np.full(vmg.number_of_nodes, 10.0) all_bounds_but_one = [0, 1, 2, 3, 4, 7, 14, 15, 16, 17, 18] vmg.status_at_node[all_bounds_but_one] = vmg.BC_NODE_IS_CLOSED vmg.status_at_node[9] = vmg.BC_NODE_IS_CLOSED # new internal closed z[outlet_node] = 0.0 z[vmg.core_nodes] = [7.0, 8.0, 6.0, 1.0, 5.0, 4.0] vmg.add_field("topographic__elevation", z, at="node", units="-") fr = FlowAccumulator(vmg) cells_contributing = [ np.array([0]), np.array([1]), np.array([0, 2]), np.array([0, 1, 2, 4, 5, 6]), np.array([0, 2, 5]), np.array([0, 2, 5, 6]), ] A_target_internal = np.zeros(len(vmg.core_nodes)) for i in range(len(cells_contributing)): A_target_internal[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell[vmg.cell_at_node[vmg.core_nodes]].sum() fr.run_one_step() assert vmg.at_node["drainage_area"][vmg.core_nodes] == pytest.approx( A_target_internal ) assert vmg.at_node["drainage_area"][outlet_node] == pytest.approx(A_target_outlet)
def test_voronoi_closedinternal(): """Test routing on a (radial) voronoi, but with a closed interior node.""" vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY vmg.status_at_node[8] = CLOSED_BOUNDARY # new internal closed z[12] = 0. # outlet inner_elevs = (8., 7., 1., 6., 4., 5.) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field("node", "topographic__elevation", z, units="-") fr = FlowRouter(vmg) cells_contributing = [ np.array([0]), np.array([1]), np.array([0, 1, 3, 4, 5, 6]), np.array([1, 4]), np.array([1, 4, 5, 6]), np.array([1, 4, 6]), ] A_target_internal = np.zeros(vmg.number_of_core_nodes, dtype=float) for i in range(6): A_target_internal[i] = vmg.area_of_cell[cells_contributing[i]].sum() A_target_outlet = vmg.area_of_cell[vmg.cell_at_node[vmg.core_nodes]].sum() fr.route_flow() assert vmg.at_node["drainage_area"][vmg.core_nodes] == approx(A_target_internal) assert vmg.at_node["drainage_area"][12] == approx(A_target_outlet)
def test_move_reference_radial(random_xy): mg = RadialModelGrid(num_shells=9, dr=10.0, xy_of_center=random_xy) assert mg.xy_of_center == approx(random_xy) pre_move_llc = (mg.x_of_node.min(), mg.y_of_node.min()) new_xy_of_center = (30.0, 45.0) mg.xy_of_center = new_xy_of_center assert mg.xy_of_center == approx(new_xy_of_center) post_move_llc = (mg.x_of_node.min(), mg.y_of_node.min()) actual_dydx = ( post_move_llc[0] - pre_move_llc[0], post_move_llc[1] - pre_move_llc[1], ) known_dydx = ( new_xy_of_center[0] - random_xy[0], new_xy_of_center[1] - random_xy[1], ) assert known_dydx == approx(actual_dydx)
def test_error_handling(): radial_grid = RadialModelGrid( n_rings=1, nodes_in_first_ring=8) # , xy_of_center=(0., 0.)) assert_raises(TypeError, ListricKinematicExtender, radial_grid) hex_grid = HexModelGrid((3, 3)) assert_raises(TypeError, ListricKinematicExtender, hex_grid) grid = RasterModelGrid((3, 7)) grid.add_zeros("topographic__elevation", at="node") assert_raises(KeyError, ListricKinematicExtender, grid, track_crustal_thickness=True)
def test_radial_from_dict(): params = { "num_shells": 5, "dr": 2.0, "xy_of_center": (35, 55), "axis_name": ("spam", "eggs"), "axis_units": ("smoot", "parsec"), "xy_of_reference": (12345, 678910), } mg = RadialModelGrid.from_dict(params) # assert things. assert mg.number_of_nodes == 95 assert mg.xy_of_center == (35, 55) assert [35, 55] in mg.xy_of_node assert mg.axis_units == ("smoot", "parsec") assert mg.axis_name == ("spam", "eggs") assert mg.xy_of_reference == (12345, 678910)
def test_radial_from_dict(): params = { "num_shells": 5, "dr": 2., "xy_of_center": (35, 55), "axis_name": ("spam", "eggs"), "axis_units": ("smoot", "parsec"), "xy_of_reference": (12345, 678910), } mg = RadialModelGrid.from_dict(params) # assert things. assert mg.number_of_nodes == 95 assert mg.xy_of_center == (35, 55) assert [35, 55] in mg.xy_of_node assert mg.axis_units == ("smoot", "parsec") assert mg.axis_name == ("spam", "eggs") assert mg.xy_of_reference == (12345, 678910)
def setup_voronoi_closedinternal(): """ Close one of the formerly core internal nodes. """ global fr, vmg global A_target_internal, A_target_outlet vmg = RadialModelGrid(2, dr=2.) z = np.full(20, 10., dtype=float) # vmg.status_at_node[8:] = CLOSED_BOUNDARY # vmg.status_at_node[0] = CLOSED_BOUNDARY # new internal closed # z[7] = 0. # outlet # inner_elevs = (3., 1., 4., 5., 6., 7., 8.) # z[:7] = np.array(inner_elevs) all_bounds_but_one = np.array((0, 1, 2, 3, 4, 7, 11, 15, 16, 17, 18, 19)) vmg.status_at_node[all_bounds_but_one] = CLOSED_BOUNDARY vmg.status_at_node[8] = CLOSED_BOUNDARY # new internal closed z[12] = 0. # outlet inner_elevs = (8., 7., 1., 6., 4., 5.) z[vmg.core_nodes] = np.array(inner_elevs) vmg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(vmg) # nodes_contributing = [[], # np.array([1, 2, 3, 4, 5, 6]), # np.array([2, 3, 4, 5]), # np.array([3, 4, 5]), # np.array([4, 5]), # np.array([5, ]), # np.array([6, ])] cells_contributing = [ np.array([ 0, ]), np.array([ 1, ]), np.array([0, 1, 3, 4, 5, 6]), np.array([1, 4]), np.array([1, 4, 5, 6]), np.array([1, 4, 6]) ] A_target_internal = np.zeros(vmg.number_of_core_nodes, dtype=float) for i in range(6): A_target_internal[i] = vmg.area_of_cell[cells_contributing[i]].sum() # A_target_internal[i] = vmg.area_of_cell[nodes_contributing[i]].sum() A_target_outlet = vmg.area_of_cell[vmg.cell_at_node[vmg.core_nodes]].sum()
def test_wrong_grid_type_error(): grid = RadialModelGrid(2) assert_raises(TypeError, TidalFlowCalculator, grid)
def main(): """ In this simple tutorial example, the main function does all the work: it sets the parameter values, creates and initializes a grid, sets up the state variables, runs the main loop, and cleans up. """ import time start_time = time.time() # INITIALIZE # User-defined parameter values num_shells=10 # number of radial "shells" in the grid #numcols = 30 # not needed for a radial model grid dr = 10.0 # grid cell spacing kd = 0.01 # diffusivity coefficient, in m2/yr uplift_rate = 0.0001 # baselevel/uplift rate, in m/yr num_time_steps = 1000 # number of time steps in run # Derived parameters dt = 0.1*dr**2 / kd # time-step size set by CFL condition # Create and initialize a radial model grid mg = RadialModelGrid(num_shells, dr) # Set up scalar values: elevation and time rate of change of elevation. # Note use of CSDMS standard names for these variables. z = mg.add_zeros('node', 'Land_surface__elevation') dzdt = mg.add_zeros('node', 'Land_surface__time_derivative_of_elevation') # Get a list of the core nodes core_nodes = mg.core_nodes # Display a message print( 'Running diffusion_with_radial_model_grid.py' ) print( 'Time-step size has been set to ' + str( dt ) + ' years.' ) # RUN # Main loop for i in range(0, num_time_steps): # Calculate the gradients and sediment fluxes g = mg.calculate_gradients_at_active_links(z) qs = -kd*g # Calculate the net deposition/erosion rate at each node dqsds = mg.calculate_flux_divergence_at_nodes(qs) # Calculate the total rate of elevation change dzdt = uplift_rate - dqsds # Update the elevations z[core_nodes] = z[core_nodes] + dzdt[core_nodes] * dt # FINALIZE # Plot the points, colored by elevation import numpy maxelev = numpy.amax(z) for i in range(mg.number_of_nodes): mycolor = str(z[i]/maxelev) pylab.plot(mg.node_x[i], mg.node_y[i], 'o', color=mycolor, ms=10) mg.display_grid() # Plot the points from the side, with analytical solution pylab.figure(3) L = num_shells*dr xa = numpy.arange(-L, L+dr, dr) z_analytical = (uplift_rate/(4*kd))*(L*L-xa*xa) pylab.plot(mg.node_x, z, 'o') pylab.plot(xa, z_analytical, 'r-') pylab.xlabel('Distance from center (m)') pylab.ylabel('Height (m)') pylab.show() end_time = time.time() print 'Elapsed time',end_time-start_time
def test_nodes_per_ring(): grid = RadialModelGrid(3, 8) assert_array_equal(grid.number_of_nodes_in_ring, [8, 16, 32])
##test bad attrs: with pytest.raises(TypeError): DataRecord(grid, time=[0.], items={'grid_element':np.array([['node'], ['link']]), 'element_id': np.array([[1],[3]])}, data_vars={'mean_elevation' : (['time'], np.array([100]))}, attrs=['not a dict']) ##Should return except AttributeError: ## raise TypeError(('Attributes (attrs) passed to DataRecord' ## 'must be a dictionary')) # ##test bad loc and id: rmg = RasterModelGrid((3,3)) hmg = HexModelGrid(3, 2, 1.0) radmg = RadialModelGrid(num_shells=1, dr=1., origin_x=0., origin_y=0.) vdmg = VoronoiDelaunayGrid(np.random.rand(25), np.random.rand(25)) my_items_bad_loc = {'grid_element':np.array(['node', 'bad_loc']), 'element_id': np.array([1,3])} my_items_bad_loc2 = {'grid_element': 'bad_loc', 'element_id': np.array([1,3])} my_items_bad_loc3 = {'grid_element': np.array(['node', 'node', 'node']), 'element_id': np.array([1,3])} my_items_bad_id = {'grid_element':np.array(['node', 'link']), 'element_id': np.array([1,300])} my_items_bad_id2 = {'grid_element':np.array(['node', 'link']), 'element_id': np.array([1,-300])} my_items_bad_id3 = {'grid_element':np.array(['node', 'link']), 'element_id': np.array([1,2.])} my_items_bad_id4 = {'grid_element':np.array(['node', 'link']), 'element_id': np.array([1, 2, 3])}
def main(): """ In this simple tutorial example, the main function does all the work: it sets the parameter values, creates and initializes a grid, sets up the state variables, runs the main loop, and cleans up. """ import time start_time = time.time() # INITIALIZE # User-defined parameter values num_shells = 10 # number of radial "shells" in the grid #numcols = 30 # not needed for a radial model grid dr = 10.0 # grid cell spacing kd = 0.01 # diffusivity coefficient, in m2/yr uplift_rate = 0.0001 # baselevel/uplift rate, in m/yr num_time_steps = 1000 # number of time steps in run # Derived parameters dt = 0.1 * dr**2 / kd # time-step size set by CFL condition # Create and initialize a radial model grid mg = RadialModelGrid(num_shells, dr) # Set up scalar values: elevation and time rate of change of elevation. # Note use of CSDMS standard names for these variables. z = mg.add_zeros('node', 'Land_surface__elevation') dzdt = mg.add_zeros('node', 'Land_surface__time_derivative_of_elevation') # Get a list of the core nodes core_nodes = mg.core_nodes # Display a message print('Running diffusion_with_radial_model_grid.py') print('Time-step size has been set to ' + str(dt) + ' years.') # RUN # Main loop for i in range(0, num_time_steps): # Calculate the gradients and sediment fluxes g = mg.calculate_gradients_at_active_links(z) qs = -kd * g # Calculate the net deposition/erosion rate at each node dqsds = mg.calculate_flux_divergence_at_nodes(qs) # Calculate the total rate of elevation change dzdt = uplift_rate - dqsds # Update the elevations z[core_nodes] = z[core_nodes] + dzdt[core_nodes] * dt # FINALIZE # Plot the points, colored by elevation import numpy maxelev = numpy.amax(z) for i in range(mg.number_of_nodes): mycolor = str(z[i] / maxelev) pylab.plot(mg.node_x[i], mg.node_y[i], 'o', color=mycolor, ms=10) mg.display_grid() # Plot the points from the side, with analytical solution pylab.figure(3) L = num_shells * dr xa = numpy.arange(-L, L + dr, dr) z_analytical = (uplift_rate / (4 * kd)) * (L * L - xa * xa) pylab.plot(mg.node_x, z, 'o') pylab.plot(xa, z_analytical, 'r-') pylab.xlabel('Distance from center (m)') pylab.ylabel('Height (m)') pylab.show() end_time = time.time() print 'Elapsed time', end_time - start_time
def test_misc(): grid = RasterModelGrid((3, 3)) # test bad dimension: with pytest.raises(ValueError): DataRecord( grid, time=[0.0], data_vars={"mean_elev": (["time"], [100.0]), "test": (["bad_dim"], [12])}, ) # should return ValueError('Data variable dimensions must be time and/or' # 'item_id') # test bad time format: with pytest.raises(TypeError): DataRecord(grid=grid, time="bad_time") # should return TypeError: Time must be a list or an array of length 1 # test bad datavars format: with pytest.raises(TypeError): DataRecord(grid, time=[0.0], data_vars=["not a dict"]) # should return TypeError(('Data variables (data_vars) passed to' # ' DataRecord must be a dictionary (see ' # 'documentation for valid structure)')) # test bad items format: with pytest.raises(TypeError): DataRecord( grid, time=[0.0], items=["not a dict"], data_vars={"mean_elevation": (["time"], np.array([100]))}, attrs={"time_units": "y"}, ) # Should return TypeError(('You must provide an ''items'' dictionary ' # '(see documentation for required format)')) # with pytest.raises(TypeError): """Test bad items keys""" DataRecord( grid, time=[0.0], items={ "grid_element": np.array([["node"], ["link"]]), "bad_key": np.array([[1], [3]]), }, data_vars={"mean_elevation": (["time"], np.array([100]))}, attrs={"time_units": "y"}, ) # Should return TypeError(('You must provide an ''items'' dictionary ' # '(see documentation for required format)')) with pytest.raises(TypeError): """Test bad attrs""" DataRecord( grid, time=[0.0], items={ "grid_element": np.array([["node"], ["link"]]), "element_id": np.array([[1], [3]]), }, data_vars={"mean_elevation": (["time"], np.array([100]))}, attrs=["not a dict"], ) # Should return except AttributeError: # raise TypeError(('Attributes (attrs) passed to DataRecord' # 'must be a dictionary')) # # test bad loc and id: rmg = RasterModelGrid((3, 3)) hmg = HexModelGrid((3, 2), spacing=1.0) radmg = RadialModelGrid(n_rings=1, nodes_in_first_ring=5, xy_of_center=(0.0, 0.0)) vdmg = VoronoiDelaunayGrid(np.random.rand(25), np.random.rand(25)) my_items_bad_loc = { "grid_element": np.array(["node", "bad_loc"]), "element_id": np.array([1, 3]), } my_items_bad_loc2 = {"grid_element": "bad_loc", "element_id": np.array([1, 3])} my_items_bad_loc3 = { "grid_element": np.array(["node", "node", "node"]), "element_id": np.array([1, 3]), } my_items_bad_id = { "grid_element": np.array(["node", "link"]), "element_id": np.array([1, 300]), } my_items_bad_id2 = { "grid_element": np.array(["node", "link"]), "element_id": np.array([1, -300]), } my_items_bad_id3 = { "grid_element": np.array(["node", "link"]), "element_id": np.array([1, 2.0]), } my_items_bad_id4 = { "grid_element": np.array(["node", "link"]), "element_id": np.array([1, 2, 3]), } with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_loc) with pytest.raises(ValueError): DataRecord(hmg, items=my_items_bad_loc) with pytest.raises(ValueError): DataRecord(radmg, items=my_items_bad_loc) with pytest.raises(ValueError): DataRecord(vdmg, items=my_items_bad_loc) # should return ValueError(('One or more of the grid elements provided is/are' # ' not permitted location for this grid type.')) with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_loc2) # should return ValueError: Location provided: bad_loc is not a permitted # location for this grid type. with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_loc3) # should return ValueError(('grid_element passed to DataRecord must be ' # ' the same length as the number of items or 1.')) with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_id) with pytest.raises(ValueError): DataRecord(hmg, items=my_items_bad_id) with pytest.raises(ValueError): DataRecord(radmg, items=my_items_bad_id) with pytest.raises(ValueError): DataRecord(vdmg, items=my_items_bad_id) # should return ValueError(('An item residing at ' + at + ' has an ' # 'element_id larger than the number of'+ at + 'on the grid.')) with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_id2) # should return ValueError(('An item residing at ' + at + ' has ' # 'an element id below zero. This is not permitted.')) with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_id3) # should return ValueError(('You have passed a non-integer element_id to ' # 'DataRecord, this is not permitted.')) with pytest.raises(ValueError): DataRecord(rmg, items=my_items_bad_id4)
time=[0.0], items={ "grid_element": np.array([["node"], ["link"]]), "element_id": np.array([[1], [3]]), }, data_vars={"mean_elevation": (["time"], np.array([100]))}, attrs=["not a dict"], ) # Should return except AttributeError: # raise TypeError(('Attributes (attrs) passed to DataRecord' # 'must be a dictionary')) # # test bad loc and id: rmg = RasterModelGrid((3, 3)) hmg = HexModelGrid(3, 2, 1.0) radmg = RadialModelGrid(num_shells=1, dr=1.0, xy_of_center=(0.0, 0.0)) vdmg = VoronoiDelaunayGrid(np.random.rand(25), np.random.rand(25)) my_items_bad_loc = { "grid_element": np.array(["node", "bad_loc"]), "element_id": np.array([1, 3]), } my_items_bad_loc2 = {"grid_element": "bad_loc", "element_id": np.array([1, 3])} my_items_bad_loc3 = { "grid_element": np.array(["node", "node", "node"]), "element_id": np.array([1, 3]), } my_items_bad_id = { "grid_element": np.array(["node", "link"]), "element_id": np.array([1, 300]), } my_items_bad_id2 = {