def test_sp_voronoi(): nnodes = 100 np.random.seed(0) x = np.random.rand(nnodes) np.random.seed(1) y = np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x, y) np.random.seed(2) z = mg.add_field('node', 'topographic__elevation', np.random.rand(nnodes) / 10000., copy=False) fr = FlowRouter(mg) spe = StreamPowerEroder(mg, os.path.join(_THIS_DIR, 'drive_sp_params_voronoi.txt')) for i in range(10): z[mg.core_nodes] += 0.01 fr.route_flow() spe.erode(mg, 1.) z_tg = np.array([4.35994902e-05, 2.59262318e-06, 5.49662478e-05, 4.36094902e-05, 4.20367802e-05, 2.16664196e-03, 1.23484520e-02, 4.07654109e-02, 5.69974645e-02, 4.71086393e-02, 5.55056467e-02, 5.53655860e-02, 4.87984556e-02, 6.62132007e-02, 7.33132951e-02, 6.10003971e-02, 8.53975293e-05, 4.90709812e-02, 7.84728490e-02, 8.26091298e-02, 5.05246090e-05, 3.69289510e-02, 7.56743264e-02, 2.52850671e-02, 6.80517443e-02, 5.96745309e-05, 3.86335644e-02, 4.12526935e-02, 7.78476139e-02, 7.18165632e-02, 7.62359762e-02, 6.65702961e-02, 9.04684278e-02, 7.37203974e-02, 9.16862054e-02, 4.11168411e-02, 4.36498947e-02, 9.04430763e-02, 1.42123575e-02, 8.02734138e-02, 8.34895711e-02, 4.51564589e-02, 6.47124169e-02, 9.52317640e-02, 6.01917122e-05, 7.11667309e-02, 8.00463179e-02, 9.40668605e-02, 9.33247720e-02, 7.34011920e-02, 7.30924467e-02, 6.21387674e-02, 9.66278589e-02, 8.64266637e-02, 9.06100497e-02, 7.19991365e-02, 1.07162442e-02, 8.68685360e-02, 7.40515066e-02, 2.73264483e-02, 9.55406046e-02, 6.01817121e-05, 3.28663723e-02, 8.60706344e-02, 9.56285286e-02, 4.24741937e-02, 5.64139004e-03, 8.72373392e-02, 9.04304841e-02, 8.83708170e-02, 5.04000439e-05, 6.47845229e-02, 8.77304766e-02, 4.40894724e-02, 8.08029139e-02, 4.52732792e-02, 6.35806712e-02, 6.71004941e-02, 7.32682350e-02, 4.88067087e-02, 2.14920377e-02, 2.55509418e-06, 8.25346959e-02, 8.12610977e-02, 3.75778123e-02, 2.20818912e-02, 2.45940192e-02, 4.43365163e-02, 4.93255141e-02, 6.23517572e-02, 5.99594648e-02, 4.17977098e-06, 4.96450779e-02, 3.72952724e-02, 2.26332108e-03, 1.69925430e-02, 1.99835635e-02, 6.61481327e-05, 1.70477133e-05, 8.81652236e-05] ) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_sp_voronoi(): nnodes = 100 np.random.seed(0) x = np.random.rand(nnodes) np.random.seed(1) y = np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x, y) np.random.seed(2) z = mg.add_field('node', 'topographic__elevation', np.random.rand(nnodes) / 10000., copy=False) fr = FlowRouter(mg) spe = StreamPowerEroder(mg, os.path.join(_THIS_DIR, 'drive_sp_params_voronoi.txt')) for i in range(10): z[mg.core_nodes] += 0.01 fr.route_flow() spe.erode(mg, 1.) z_tg = np.array([4.35994902e-05, 2.59262318e-06, 5.49662478e-05, 6.56738615e-03, 4.20367802e-05, 1.21371424e-02, 2.16596169e-02, 4.73320898e-02, 6.00389761e-02, 5.22007356e-02, 5.37507115e-02, 5.95794752e-02, 5.29862904e-02, 6.76465914e-02, 7.31720024e-02, 6.18730861e-02, 8.53975293e-05, 5.32189275e-02, 7.34302556e-02, 8.07385044e-02, 5.05246090e-05, 4.08940657e-02, 7.39971005e-02, 3.31915602e-02, 6.72650419e-02, 5.96745309e-05, 4.72752445e-02, 3.60359567e-02, 7.59432065e-02, 7.24461985e-02, 7.80305760e-02, 4.93866869e-02, 8.69642467e-02, 7.21627626e-02, 8.96368291e-02, 4.65142080e-02, 6.07720217e-02, 8.83372939e-02, 2.35887558e-02, 7.97616193e-02, 8.35615355e-02, 4.61809032e-02, 6.34634214e-02, 9.25711770e-02, 4.11717225e-03, 7.24493623e-02, 7.97908053e-02, 9.10375623e-02, 9.13155023e-02, 7.10567915e-02, 7.35271752e-02, 6.13091341e-02, 9.45498463e-02, 8.48532386e-02, 8.82702021e-02, 7.14969941e-02, 2.22640943e-02, 8.53311932e-02, 7.49161159e-02, 3.48837223e-02, 9.30132692e-02, 6.01817121e-05, 3.87455443e-02, 8.44673586e-02, 9.35213577e-02, 6.76075824e-02, 1.58614508e-02, 8.51346837e-02, 8.83645680e-02, 8.69944117e-02, 5.04000439e-05, 5.02319084e-02, 8.63882765e-02, 5.00991880e-02, 7.65156630e-02, 5.07591983e-02, 6.54909962e-02, 6.91505342e-02, 7.33358371e-02, 5.30109890e-02, 2.99074601e-02, 2.55509418e-06, 8.21523907e-02, 8.09368483e-02, 4.35073025e-02, 3.04096109e-02, 3.26298627e-02, 4.92259177e-02, 5.48690358e-02, 6.44732130e-02, 6.28133567e-02, 4.17977098e-06, 5.37149677e-02, 4.32828136e-02, 1.30559903e-02, 2.62405261e-02, 2.86079272e-02, 6.61481327e-05, 1.70477133e-05, 8.81652236e-05]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_sp_voronoi(): nnodes = 100 np.random.seed(0) x = np.random.rand(nnodes) np.random.seed(1) y = np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x, y) np.random.seed(2) z = mg.add_field('node', 'topographic__elevation', np.random.rand(nnodes) / 10000., copy=False) fr = FlowRouter(mg) spe = StreamPowerEroder( mg, os.path.join(_THIS_DIR, 'drive_sp_params_voronoi.txt')) for i in range(10): z[mg.core_nodes] += 0.01 fr.route_flow() spe.erode(mg, 1.) z_tg = np.array([ 4.35994902e-05, 2.59262318e-06, 5.49662478e-05, 4.36094902e-05, 4.20367802e-05, 2.16664196e-03, 1.23484520e-02, 4.07654109e-02, 5.69974645e-02, 4.71086393e-02, 5.55056467e-02, 5.53655860e-02, 4.87984556e-02, 6.62132007e-02, 7.33132951e-02, 6.10003971e-02, 8.53975293e-05, 4.90709812e-02, 7.84728490e-02, 8.26091298e-02, 5.05246090e-05, 3.69289510e-02, 7.56743264e-02, 2.52850671e-02, 6.80517443e-02, 5.96745309e-05, 3.86335644e-02, 4.12526935e-02, 7.78476139e-02, 7.18165632e-02, 7.62359762e-02, 6.65702961e-02, 9.04684278e-02, 7.37203974e-02, 9.16862054e-02, 4.11168411e-02, 4.36498947e-02, 9.04430763e-02, 1.42123575e-02, 8.02734138e-02, 8.34895711e-02, 4.51564589e-02, 6.47124169e-02, 9.52317640e-02, 6.01917122e-05, 7.11667309e-02, 8.00463179e-02, 9.40668605e-02, 9.33247720e-02, 7.34011920e-02, 7.30924467e-02, 6.21387674e-02, 9.66278589e-02, 8.64266637e-02, 9.06100497e-02, 7.19991365e-02, 1.07162442e-02, 8.68685360e-02, 7.40515066e-02, 2.73264483e-02, 9.55406046e-02, 6.01817121e-05, 3.28663723e-02, 8.60706344e-02, 9.56285286e-02, 4.24741937e-02, 5.64139004e-03, 8.72373392e-02, 9.04304841e-02, 8.83708170e-02, 5.04000439e-05, 6.47845229e-02, 8.77304766e-02, 4.40894724e-02, 8.08029139e-02, 4.52732792e-02, 6.35806712e-02, 6.71004941e-02, 7.32682350e-02, 4.88067087e-02, 2.14920377e-02, 2.55509418e-06, 8.25346959e-02, 8.12610977e-02, 3.75778123e-02, 2.20818912e-02, 2.45940192e-02, 4.43365163e-02, 4.93255141e-02, 6.23517572e-02, 5.99594648e-02, 4.17977098e-06, 4.96450779e-02, 3.72952724e-02, 2.26332108e-03, 1.69925430e-02, 1.99835635e-02, 6.61481327e-05, 1.70477133e-05, 8.81652236e-05 ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_voronoi_from_dict(): x = [0, 0.1, 0.2, 0.3, 1, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 2.3] y = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] params = { "x": x, "y": y, "axis_name": ("spam", "eggs"), "axis_units": ("smoot", "parsec"), "xy_of_reference": (12345, 678910), } mg = VoronoiDelaunayGrid.from_dict(params) assert mg.axis_units == ("smoot", "parsec") assert mg.axis_name == ("spam", "eggs") assert mg.xy_of_reference == (12345, 678910) true_x = np.array([0.0, 1.0, 2.0, 0.1, 1.1, 2.1, 0.2, 1.2, 2.2, 0.3, 1.3, 2.3]) true_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0]) true_nodes_at_node = np.array( [ [1, 3, -1, -1, -1, -1], [2, 4, 3, 0, -1, -1], [5, 4, 1, -1, -1, -1], [4, 6, 0, 1, -1, -1], [5, 7, 6, 3, 1, 2], [8, 7, 4, 2, -1, -1], [7, 9, 3, 4, -1, -1], [8, 10, 9, 6, 4, 5], [11, 10, 7, 5, -1, -1], [10, 6, 7, -1, -1, -1], [11, 9, 7, 8, -1, -1], [10, 8, -1, -1, -1, -1], ] ) assert_array_equal(mg.node_x, true_x) assert_array_equal(mg.node_y, true_y) assert_array_equal(mg.adjacent_nodes_at_node, true_nodes_at_node)
def test_voronoi_from_dict(): x = [0.0, 0.1, 0.2, 0.3, 1.0, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2, 2.3] y = [0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0] params = { "x": x, "y": y, "xy_axis_name": ("spam", "eggs"), "xy_axis_units": ("smoot", "parsec"), "xy_of_reference": (12345, 678910), } mg = VoronoiDelaunayGrid.from_dict(params) assert mg.axis_units == ("smoot", "parsec") assert mg.axis_name == ("spam", "eggs") assert mg.xy_of_reference == (12345, 678910) true_x = np.array([0.0, 1.0, 2.0, 0.1, 1.1, 2.1, 0.2, 1.2, 2.2, 0.3, 1.3, 2.3]) true_y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0]) true_nodes_at_node = np.array( [ [1, 3, -1, -1, -1, -1], [2, 4, 3, 0, -1, -1], [5, 4, 1, -1, -1, -1], [4, 6, 0, 1, -1, -1], [5, 7, 6, 3, 1, 2], [8, 7, 4, 2, -1, -1], [7, 9, 3, 4, -1, -1], [8, 10, 9, 6, 4, 5], [11, 10, 7, 5, -1, -1], [10, 6, 7, -1, -1, -1], [11, 9, 7, 8, -1, -1], [10, 8, -1, -1, -1, -1], ] ) assert_array_equal(mg.x_of_node, true_x) assert_array_equal(mg.y_of_node, true_y) assert_array_equal(mg.adjacent_nodes_at_node, true_nodes_at_node)
from landlab import VoronoiDelaunayGrid # , RasterModelGrid from landlab.components.flow_routing.route_flow_dn import FlowRouter from landlab.components.stream_power.stream_power import StreamPowerEroder from landlab.plot.imshow import imshow_node_grid import numpy as np from matplotlib.pyplot import figure, show nnodes = 10000 x, y = np.random.rand(nnodes), np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x,y) #mg = RasterModelGrid(4,5) z = mg.add_field('node', 'topographic__elevation', np.random.rand(nnodes)/10000., copy=False) fr = FlowRouter(mg) spe = StreamPowerEroder(mg, 'drive_sp_params_voronoi.txt') for i in xrange(100): z[mg.core_nodes] += 0.01 fr.route_flow() spe.erode(mg, 1.) imshow_node_grid(mg, 'topographic__elevation') show()
def test_not_implemented_voroni(): x = [0, 0.1, 0.2, 0.3, 1, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 2.3] y = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] vmg = VoronoiDelaunayGrid(x, y) with pytest.raises(NotImplementedError): flow_direction_dinf.flow_directions_dinf(vmg)
import numpy as np from matplotlib.pyplot import show from six.moves import range from landlab import VoronoiDelaunayGrid from landlab.components import FlowAccumulator, StreamPowerEroder from landlab.plot.imshow import imshow_node_grid nnodes = 10000 x, y = np.random.rand(nnodes), np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x, y) z = mg.add_field("node", "topographic__elevation", np.random.rand(nnodes) / 10000., copy=False) fr = FlowAccumulator(mg) spe = StreamPowerEroder(mg, "drive_sp_params_voronoi.txt") for i in range(100): z[mg.core_nodes] += 0.01 fr.run_one_step() spe.erode(mg, 1.) imshow_node_grid(mg, "topographic__elevation") show()
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 = { "grid_element": np.array(["node", "link"]),
# # We can create **grid** objects with the following lines of code. # In[ ]: import numpy as np from landlab import RasterModelGrid, VoronoiDelaunayGrid, HexModelGrid # a square-cell raster, 3 rows x 4 columns, unit spacing smg = RasterModelGrid((3, 4), 1.) rmg = RasterModelGrid((3, 4), (1., 2.)) # a rectangular-cell raster hmg = HexModelGrid(3, 4, 1.) # ^a hexagonal grid with 3 rows, 4 columns from the base row, & node spacing of # 1. x = np.random.rand(100) * 100. y = np.random.rand(100) * 100. vmg = VoronoiDelaunayGrid(x, y) # ^a Voronoi-cell grid with 100 randomly positioned nodes within a 100.x100. # square # All these various `ModelGrid` objects contains various data items (known as # *attributes*). These include, for example: * number nodes and links in the # grid * *x* and *y* coordinates of each each node * starting ("tail") and # ending ("head") node IDs of each link * IDs of links that are active * IDs of # core nodes * etc. # # From here on we'll focus on the square raster grid as its geometry is a bit # easier to think through, but all of the following applies to all grid types. # # ## Understanding the topology of Landlab grids # # All grids consist of two interlocked sets of *points* joined by *lines*
import numpy as np from matplotlib.pyplot import show from six.moves import range from landlab import VoronoiDelaunayGrid from landlab.components import FlowAccumulator, StreamPowerEroder from landlab.plot.imshow import imshow_grid nnodes = 10000 x, y = np.random.rand(nnodes), np.random.rand(nnodes) mg = VoronoiDelaunayGrid(x, y) z = mg.add_field( "node", "topographic__elevation", np.random.rand(nnodes) / 10000., copy=False ) fr = FlowAccumulator(mg) spe = StreamPowerEroder(mg, "drive_sp_params_voronoi.txt") for i in range(100): z[mg.core_nodes] += 0.01 fr.run_one_step() spe.erode(mg, 1.) imshow_grid(mg, "topographic__elevation") show()
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)