def test_tl_fluvial(): input_file = os.path.join(_THIS_DIR, 'stream_power_params_ideal.txt') inputs = ModelParameterDictionary(input_file) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') leftmost_elev = inputs.read_float('leftmost_elevation') initial_slope = inputs.read_float('initial_slope') uplift_rate = inputs.read_float('uplift_rate') runtime = inputs.read_float('total_time') dt = inputs.read_float('dt') nt = int(runtime // dt) uplift_per_step = uplift_rate * dt mg = RasterModelGrid(nrows, ncols, dx) mg.add_zeros('node', 'topographic__elevation') z = np.loadtxt(os.path.join(_THIS_DIR, 'tl_init.txt')) mg['node']['topographic__elevation'] = z mg.set_closed_boundaries_at_grid_edges(True, False, True, False) mg.set_fixed_value_boundaries_at_grid_edges( False, True, False, True, value_of='topographic__elevation') fr = FlowRouter(mg) tl = TransportLimitedEroder(mg, input_file) for i in range(nt): mg.at_node['topographic__elevation'][mg.core_nodes] += uplift_per_step mg = fr.route_flow() mg, _ = tl.erode(mg, dt, stability_condition='loose') z_tg = np.loadtxt(os.path.join(_THIS_DIR, 'tlz_tg.txt')) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
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_storms(): input_file_string = os.path.join(_THIS_DIR, 'drive_sp_params_storms.txt') inputs = ModelParameterDictionary(input_file_string) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') mg = RasterModelGrid(nrows, ncols, dx) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') mg['node']['topographic__elevation'] = z + np.random.rand(len(z)) / 1000. mg.add_zeros('water__unit_flux_in', at='node') precip = PrecipitationDistribution(input_file=input_file_string) fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) for (interval_duration, rainfall_rate) in \ precip.yield_storm_interstorm_duration_intensity(): if rainfall_rate != 0.: mg.at_node['water__unit_flux_in'].fill(rainfall_rate) mg = fr.route_flow() sp.run_one_step(dt) mg.at_node['topographic__elevation'][ mg.core_nodes] += uplift * interval_duration
def test_sp_discharges_new(): input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge_new.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = 5 ncols = 5 dx = inputs.read_float('dx') dt = inputs.read_float('dt') mg = RasterModelGrid(nrows, ncols, dx) mg.add_zeros('topographic__elevation', at='node') z = np.array([5., 5., 0., 5., 5., 5., 2., 1., 2., 5., 5., 3., 2., 3., 5., 5., 4., 4., 4., 5., 5., 5., 5., 5., 5.]) mg['node']['topographic__elevation'] = z fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) # perform the loop (once!) for i in range(1): fr.route_flow() sp.run_one_step(dt) z_tg = np.array([5. , 5. , 0. , 5. , 5. , 5. , 1.47759225, 0.43050087, 1.47759225, 5. , 5. , 2.32883687, 1.21525044, 2.32883687, 5. , 5. , 3.27261262, 3.07175015, 3.27261262, 5. , 5. , 5. , 5. , 5. , 5. ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_sp_discharges_new(): input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge_new.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = 5 ncols = 5 dx = inputs.read_float('dx') dt = inputs.read_float('dt') mg = RasterModelGrid(nrows, ncols, dx) mg.add_zeros('topographic__elevation', at='node') z = np.array([ 5., 5., 0., 5., 5., 5., 2., 1., 2., 5., 5., 3., 2., 3., 5., 5., 4., 4., 4., 5., 5., 5., 5., 5., 5. ]) mg['node']['topographic__elevation'] = z fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) # perform the loop (once!) for i in range(1): fr.route_flow() sp.run_one_step(dt) z_tg = np.array([ 5., 5., 0., 5., 5., 5., 1.47759225, 0.43050087, 1.47759225, 5., 5., 2.32883687, 1.21525044, 2.32883687, 5., 5., 3.27261262, 3.07175015, 3.27261262, 5., 5., 5., 5., 5., 5. ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_sed_dep_new(): """ This tests only the power_law version of the SDE. It uses a landscape run to 5000 100y iterations, then having experienced a 20-fold uplift acceleration for a further 30000 y. It tests the outcome of the next 1000 y of erosion. """ mg = RasterModelGrid((25, 50), 200.) for edge in (mg.nodes_at_left_edge, mg.nodes_at_top_edge, mg.nodes_at_right_edge): mg.status_at_node[edge] = CLOSED_BOUNDARY z = mg.add_zeros('node', 'topographic__elevation') fr = FlowRouter(mg) sde = SedDepEroder(mg, K_sp=1.e-4, sed_dependency_type='almost_parabolic', Qc='power_law', K_t=1.e-4) initconds = os.path.join(os.path.dirname(__file__), 'perturbedcondst300.txt') finalconds = os.path.join(os.path.dirname(__file__), 'tenmorestepsfrom300.txt') z[:] = np.loadtxt(initconds) dt = 100. up = 0.05 for i in range(10): fr.route_flow() sde.run_one_step(dt) z[mg.core_nodes] += 20.*up assert_array_almost_equal(z, np.loadtxt(finalconds))
def setup_D4_grid(): """ Test functionality of routing when D4 is specified. The elevation field in this test looks like: 1 2 3 4 5 6 7 1 2 3 0 5 0 7 1 2 3 4 0 0 7 1 2 3 0 5 6 7 1 2 0 0 0 6 7 1 2 3 0 5 6 7 1 2 3 4 5 6 7 """ global frD8, frD4, lfD8, lfD4, mg1, mg2 global z, lake_nodes mg1 = RasterModelGrid(7, 7, 1.) mg2 = RasterModelGrid(7, 7, 1.) z = mg1.node_x.copy() + 1. lake_nodes = np.array([10, 16, 17, 18, 24, 32, 33, 38, 40]) z[lake_nodes] = 0. mg1.add_field('node', 'topographic__elevation', z, units='-') mg2.add_field('node', 'topographic__elevation', z, units='-') frD8 = FlowRouter(mg1, method='D8') frD4 = FlowRouter(mg2, method='D4') lfD8 = DepressionFinderAndRouter(mg1, routing='D8') lfD4 = DepressionFinderAndRouter(mg2, routing='D4')
def test_stupid_shaped_hole(sink_grid4): """Tests inclined fill into a surface with a deliberately awkward shape.""" fr = FlowRouter(sink_grid4) hf = SinkFiller(sink_grid4, apply_slope=True) hf.fill_pits() hole1 = np.array( [ 4.00007692, 4.00015385, 4.00023077, 4.00030769, 4.00038462, 4.00046154, 4.00053846, 4.00061538, 4.00069231, 4.00076923, 4.00084615, ] ) hole2 = np.array([7.4, 7.2, 7.6]) assert_array_almost_equal( sink_grid4.at_node["topographic__elevation"][sink_grid4.lake1], hole1 ) assert_array_almost_equal( sink_grid4.at_node["topographic__elevation"][sink_grid4.lake2], hole2 ) fr.route_flow() assert sink_grid4.at_node["flow__sink_flag"][sink_grid4.core_nodes].sum() == 0
def test_filler_inclined2(sink_grid3): """ Tests an inclined fill into an inclined surface, with two holes. """ z_init = sink_grid3.at_node["topographic__elevation"].copy() fr = FlowRouter(sink_grid3) hf = SinkFiller(sink_grid3, apply_slope=True) hf.fill_pits() hole1 = np.array( [ 4.00009091, 4.00018182, 4.00027273, 4.00036364, 4.00045455, 4.00054545, 4.00063636, 4.00072727, 4.00081818, ] ) hole2 = np.array([7.16666667, 7.33333333, 7.5, 7.66666667]) assert_array_almost_equal( sink_grid3.at_node["topographic__elevation"][sink_grid3.lake1], hole1 ) assert_array_almost_equal( sink_grid3.at_node["topographic__elevation"][sink_grid3.lake2], hole2 ) fr.route_flow() assert sink_grid3.at_node["flow__sink_flag"][sink_grid3.core_nodes].sum() == 0
def test_degenerate_drainage(): """ This "hourglass" configuration should be one of the hardest to correctly re-route. """ mg = RasterModelGrid(9, 5) z_init = mg.node_x.copy()*0.0001 + 1. lake_pits = np.array([7, 11, 12, 13, 17, 27, 31, 32, 33, 37]) z_init[lake_pits] = -1. z_init[22] = 0. # the common spill pt for both lakes z_init[21] = 0.1 # an adverse bump in the spillway z_init[20] = -0.2 # the spillway z = mg.add_field('node', 'topographic__elevation', z_init) fr = FlowRouter(mg) lf = DepressionFinderAndRouter(mg) fr.route_flow() lf.map_depressions() correct_A = np.array([ 0., 0., 0., 0., 0., 0., 1., 3., 1., 0., 0., 5., 1., 2., 0., 0., 1., 10., 1., 0., 21., 21., 1., 1., 0., 0., 1., 9., 1., 0., 0., 3., 1., 2., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0.]) thelake = np.concatenate((lake_pits, [22])).sort() assert_array_almost_equal(mg.at_node['drainage_area'], correct_A)
def test_sp_discharges_new(): input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge_new.txt') inputs = ModelParameterDictionary(input_str) nrows = 5 ncols = 5 dx = inputs.read_float('dx') dt = inputs.read_float('dt') mg = RasterModelGrid(nrows, ncols, dx) mg.add_zeros('topographic__elevation', at='node') z = np.array([ 5., 5., 0., 5., 5., 5., 2., 1., 2., 5., 5., 3., 2., 3., 5., 5., 4., 4., 4., 5., 5., 5., 5., 5., 5. ]) mg['node']['topographic__elevation'] = z fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) # perform the loop (once!) for i in range(1): fr.route_flow() sp.run_one_step(dt) z_tg = np.array([ 5.00000000e+00, 5.00000000e+00, 0.00000000e+00, 5.00000000e+00, 5.00000000e+00, 5.00000000e+00, 1.29289322e+00, 1.00000000e-06, 1.29289322e+00, 5.00000000e+00, 5.00000000e+00, 2.29289322e+00, 1.00000000e+00, 2.29289322e+00, 5.00000000e+00, 5.00000000e+00, 3.29289322e+00, 3.00000000e+00, 3.29289322e+00, 5.00000000e+00, 5.00000000e+00, 5.00000000e+00, 5.00000000e+00, 5.00000000e+00, 5.00000000e+00 ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_check_field_input(): """Check we can successfully pass water__volume_flux_in.""" mg.add_field('node', 'water__unit_flux_in', np.full(25, 3.), units='m**3/s') fr = FlowRouter(mg) assert_array_equal(np.full(25, 3.), mg.at_node['water__unit_flux_in']) fr = FlowRouter(mg, infile) assert_array_equal(np.full(25, 2.), mg.at_node['water__unit_flux_in'])
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_check_field_input(dans_grid1): """Check we can successfully pass water__discharge_in.""" dans_grid1.mg.add_field( "node", "water__unit_flux_in", np.full(25, 3.), units="m**3/s" ) fr = FlowRouter(dans_grid1.mg) assert_array_equal(np.full(25, 3.), dans_grid1.mg.at_node["water__unit_flux_in"]) fr = FlowRouter(dans_grid1.mg, dans_grid1.infile) assert_array_equal(np.full(25, 2.), dans_grid1.mg.at_node["water__unit_flux_in"])
def test_check_fields(dans_grid1): """Check to make sure the right fields have been created.""" fr = FlowRouter(dans_grid1.mg) assert_array_equal(dans_grid1.z, dans_grid1.mg.at_node["topographic__elevation"]) assert_array_equal(np.zeros(25), dans_grid1.mg.at_node["drainage_area"]) assert_array_equal(np.ones(25), dans_grid1.mg.at_node["water__unit_flux_in"]) fr = FlowRouter(dans_grid1.mg, dans_grid1.infile) assert_array_equal(np.full(25, 2.), dans_grid1.mg.at_node["water__unit_flux_in"])
def test_check_fields(): """Check to make sure the right fields have been created.""" fr = FlowRouter(mg) assert_array_equal(z, mg.at_node['topographic__elevation']) assert_array_equal(np.zeros(25), mg.at_node['drainage_area']) assert_array_equal(np.ones(25), mg.at_node['water__unit_flux_in']) fr = FlowRouter(mg, infile) assert_array_equal(np.full(25, 2.), mg.at_node['water__unit_flux_in'])
def test_irreg_topo_new(): """Test D4 routing on a toy irregular topo. 'method' passed to init.""" fr = FlowRouter(mg, method="D4") fr.route_flow() assert_array_equal(A_target_D4, mg.at_node["drainage_area"]) assert_array_equal(frcvr_target_D4, mg.at_node["flow__receiver_node"]) assert_array_equal(upids_target_D4, mg.at_node["flow__upstream_node_order"]) assert_array_equal(links2rcvr_target_D4, mg.at_node["flow__link_to_receiver_node"]) assert_array_almost_equal(steepest_target_D4, mg.at_node["topographic__steepest_slope"])
def test_internal_closed(): """Test closed nodes in the core of the grid.""" fr = FlowRouter(mg) fr.route_flow() assert_array_almost_equal(A_target, mg.at_node["drainage_area"]) assert_array_equal(frcvr_target, mg.at_node["flow__receiver_node"]) assert_array_equal(links2rcvr_target, mg.at_node["flow__link_to_receiver_node"]) assert_array_almost_equal(A_target, mg.at_node["water__discharge"]) assert_array_almost_equal(steepest_target, mg.at_node["topographic__steepest_slope"])
def test_internal_closed(): """Test closed nodes in the core of the grid.""" fr = FlowRouter(mg) fr.route_flow() assert_array_almost_equal(A_target, mg.at_node['drainage_area']) assert_array_equal(frcvr_target, mg.at_node['flow_receiver']) assert_array_equal(links2rcvr_target, mg.at_node['links_to_flow_receiver']) assert_array_almost_equal(A_target, mg.at_node['water__volume_flux']) assert_array_almost_equal(steepest_target, mg.at_node['topographic__steepest_slope'])
def test_accumulate_D8(): """Test accumulation works for D8 in a simple scenario.""" fr = FlowRouter(mg) fr.route_flow() assert_array_equal(A_target, mg.at_node["drainage_area"]) assert_array_equal(frcvr_target, mg.at_node["flow__receiver_node"]) assert_array_equal(upids_target, mg.at_node["flow__upstream_node_order"]) assert_array_equal(links2rcvr_target, mg.at_node["flow__link_to_receiver_node"]) assert_array_equal(A_target, mg.at_node["water__discharge"]) assert_array_equal(steepest_target, mg.at_node["topographic__steepest_slope"])
def test_three_pits(): """ A test to ensure the component correctly handles cases where there are multiple pits. """ mg = RasterModelGrid(10,10,1.) z = mg.add_field('node', 'topographic__elevation', mg.node_x.copy()) # a sloping plane #np.random.seed(seed=0) #z += np.random.rand(100)/10000. # punch some holes z[33] = 1. z[43] = 1. z[37] = 4. z[74:76] = 1. fr = FlowRouter(mg) lf = DepressionFinderAndRouter(mg) fr.route_flow() lf.map_depressions() flow_sinks_target = np.zeros(100, dtype=bool) flow_sinks_target[mg.boundary_nodes] = True # no internal sinks now: assert_array_equal(mg.at_node['flow__sink_flag'], flow_sinks_target) # test conservation of mass: assert_almost_equal(mg.at_node['drainage_area' ].reshape((10,10))[1:-1,1].sum(), 8.**2) # ^all the core nodes # test the actual flow field: nA = np.array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 8., 8., 7., 6., 5., 4., 3., 2., 1., 0., 2., 2., 1., 1., 2., 1., 1., 1., 1., 0., 26., 26., 25., 15., 11., 10., 9., 8., 1., 0., 2., 2., 1., 9., 2., 1., 1., 1., 1., 0., 2., 2., 1., 1., 5., 4., 3., 2., 1., 0., 2., 2., 1., 1., 1., 1., 3., 2., 1., 0., 20., 20., 19., 18., 17., 12., 3., 2., 1., 0., 2., 2., 1., 1., 1., 1., 3., 2., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]) assert_array_equal(mg.at_node['drainage_area'], nA) #test a couple more properties: lc = np.empty(100, dtype=int) lc.fill(XX) lc[33] = 33 lc[43] = 33 lc[37] = 37 lc[74:76] = 74 assert_array_equal(lf.lake_map, lc) assert_array_equal(lf.lake_codes, [33, 37, 74]) assert_equal(lf.number_of_lakes, 3) assert_array_almost_equal(lf.lake_areas, [2., 1., 2.]) assert_array_almost_equal(lf.lake_volumes, [2., 2., 4.])
def initialize(self, input_stream=None): """ The BMI-style initialize method takes an optional input_stream parameter, which may be either a ModelParameterDictionary object or an input stream from which a ModelParameterDictionary can read values. """ # Create a ModelParameterDictionary for the inputs if input_stream is None: inputs = None elif type(input_stream) == ModelParameterDictionary: inputs = input_stream else: inputs = ModelParameterDictionary(input_stream) # Make sure the grid includes elevation data. This means either: # 1. The grid has a node field called 'topographic__elevation', or # 2. The input file has an item called 'ELEVATION_FIELD_NAME' *and* # a field by this name exists in the grid. try: self._elev = self._grid.at_node['topographic__elevation'] except FieldError: try: self.topo_field_name = inputs.read_string('ELEVATION_' + 'FIELD_NAME') except AttributeError: print('Error: Because your grid does not have a node field') print('called "topographic__elevation", you need to pass the') print('name of a text input file or ModelParameterDictionary,') print('and this file or dictionary needs to include the name') print('of another field in your grid that contains your') print('elevation data.') raise AttributeError except MissingKeyError: print('Error: Because your grid does not have a node field') print('called "topographic__elevation", your input file (or') print('ModelParameterDictionary) must include an entry with') print('the key "ELEVATION_FIELD_NAME", which gives the name') print('of a field in your grid that contains your elevation') print('data.') raise MissingKeyError('ELEVATION_FIELD_NAME') try: self._elev = self._grid.at_node[self.topo_field_name] except AttributeError: print('Your grid does not seem to have a node field called', self.topo_field_name) else: self.topo_field_name = 'topographic__elevation' # create the only new output field: self.sed_fill_depth = self._grid.add_zeros('node', 'sediment_fill__depth', noclobber=False) self._lf = DepressionFinderAndRouter(self._grid, routing=self._routing) self._fr = FlowRouter(self._grid, method=self._routing)
def test_variable_Qin(): """Test variable Qin field.""" Qin_local = np.zeros(25, dtype=float) Qin_local[13] = 2. mg.add_field('node', 'water__unit_flux_in', Qin_local, units='m**3/s') fr = FlowRouter(mg) fr.route_flow() Qout_local = np.zeros_like(Qin_local) Qout_local[10:14] = 200. assert_array_equal(Qout_local, mg.at_node['surface_water__discharge']) assert_array_equal(A_target, mg.at_node['drainage_area'])
def test_variable_Qin(dans_grid1): """Test variable Qin field.""" Qin_local = np.zeros(25, dtype=float) Qin_local[13] = 2. dans_grid1.mg.add_field("node", "water__unit_flux_in", Qin_local, units="m**3/s") fr = FlowRouter(dans_grid1.mg) fr.route_flow() Qout_local = np.zeros_like(Qin_local) Qout_local[10:14] = 200. assert_array_equal(Qout_local, dans_grid1.mg.at_node["surface_water__discharge"]) assert_array_equal(dans_grid1.A_target, dans_grid1.mg.at_node["drainage_area"])
def test_accumulate_D8(): """Test accumulation works for D8 in a simple scenario.""" fr = FlowRouter(mg) fr.route_flow() assert_array_equal(A_target, mg.at_node['drainage_area']) assert_array_equal(frcvr_target, mg.at_node['flow_receiver']) assert_array_equal(upids_target, mg.at_node['upstream_node_order']) assert_array_equal(links2rcvr_target, mg.at_node['links_to_flow_receiver']) assert_array_equal(A_target, mg.at_node['water__volume_flux']) assert_array_equal(steepest_target, mg.at_node['topographic__steepest_slope'])
def test_irreg_topo_new(): """Test D4 routing on a toy irregular topo. 'method' passed to init.""" fr = FlowRouter(mg, method='D4') fr.route_flow() assert_array_equal(A_target_D4, mg.at_node['drainage_area']) assert_array_equal(frcvr_target_D4, mg.at_node['flow_receiver']) assert_array_equal(upids_target_D4, mg.at_node['upstream_node_order']) assert_array_equal(links2rcvr_target_D4, mg.at_node['links_to_flow_receiver']) assert_array_almost_equal(steepest_target_D4, mg.at_node['topographic__steepest_slope'])
def test_irreg_topo(): """Test D8 routing on a toy irregular topo.""" fr = FlowRouter(mg) fr.route_flow() assert_array_equal(A_target_D8, mg.at_node['drainage_area']) assert_array_equal(frcvr_target_D8, mg.at_node['flow__receiver_node']) assert_array_equal(upids_target_D8, mg.at_node['flow__upstream_node_order']) assert_array_equal(links2rcvr_target_D8, mg.at_node['flow__link_to_receiver_node']) assert_array_almost_equal(steepest_target_D8, mg.at_node['topographic__steepest_slope'])
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_new(): """ Tests new style component instantiation and run. """ input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid((nrows, ncols), spacing=(dx, dx)) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.route_flow() sp.run_one_step(dt) mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.02783376e-02, 9.66667235e-03, 6.15060782e-03, 3.83441519e-04, 7.91725038e-04, 1.00905776e-02, 8.98955843e-03, 5.32836181e-03, 7.10360582e-05, 8.71292997e-05, 9.96377080e-03, 9.63738797e-03, 7.31213677e-03, 8.70012148e-04, 9.78618342e-04, 1.02124693e-02, 8.78386002e-03, 4.88161060e-03, 1.18274426e-04, 6.39921021e-04, 1.00377580e-02, 9.54340293e-03, 6.05173814e-03, 4.14661940e-04, 2.64555612e-04, 1.02160196e-02, 8.61600088e-03, 4.77005225e-03, 1.87898004e-05, 6.17635497e-04, 9.30445558e-03, 9.48713993e-03, 7.25689742e-03, 6.81820299e-04, 3.59507901e-04, 5.79161813e-03, 6.83777542e-03, 6.18063842e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
def test_variable_Qin(): """Test variable Qin field.""" Qin_local = np.zeros(25, dtype=float) Qin_local[13] = 2. mg.add_field('node', 'water__unit_flux_in', Qin_local, units='m**3/s') fr = FlowRouter(mg) fr.route_flow() Qout_local = np.zeros_like(Qin_local) Qout_local[10:14] = 200. assert_array_equal(Qout_local, mg.at_node['water__volume_flux']) assert_array_equal(A_target, mg.at_node['drainage_area'])
def test_sp_new(): """ Tests new style component instantiation and run. """ input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid((nrows, ncols), spacing=(dx, dx)) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.route_flow() sp.run_one_step(dt) mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
def test_irreg_topo_old(): """ Test D4 routing on a toy irregular topo, old style, where 'method' is passed to the run method, not the init. """ fr = FlowRouter(mg) fr.route_flow(method="D4") assert_array_equal(A_target_D4, mg.at_node["drainage_area"]) assert_array_equal(frcvr_target_D4, mg.at_node["flow__receiver_node"]) assert_array_equal(upids_target_D4, mg.at_node["flow__upstream_node_order"]) assert_array_equal(links2rcvr_target_D4, mg.at_node["flow__link_to_receiver_node"]) assert_array_almost_equal(steepest_target_D4, mg.at_node["topographic__steepest_slope"])
def test_sp_new(): """ Tests new style component instantiation and run. """ input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid((nrows, ncols), spacing=(dx, dx)) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.route_flow() sp.run_one_step(dt) mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([ 5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04 ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
def test_sp_new(): """ Tests new style component instantiation and run. """ input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid((nrows, ncols), spacing=(dx, dx)) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowRouter(mg) sp = StreamPowerEroder(mg, **inputs) elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.route_flow() sp.run_one_step(dt) mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([ 5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.02783376e-02, 9.66667235e-03, 6.15060782e-03, 3.83441519e-04, 7.91725038e-04, 1.00905776e-02, 8.98955843e-03, 5.32836181e-03, 7.10360582e-05, 8.71292997e-05, 9.96377080e-03, 9.63738797e-03, 7.31213677e-03, 8.70012148e-04, 9.78618342e-04, 1.02124693e-02, 8.78386002e-03, 4.88161060e-03, 1.18274426e-04, 6.39921021e-04, 1.00377580e-02, 9.54340293e-03, 6.05173814e-03, 4.14661940e-04, 2.64555612e-04, 1.02160196e-02, 8.61600088e-03, 4.77005225e-03, 1.87898004e-05, 6.17635497e-04, 9.30445558e-03, 9.48713993e-03, 7.25689742e-03, 6.81820299e-04, 3.59507901e-04, 5.79161813e-03, 6.83777542e-03, 6.18063842e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04 ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
def test_irreg_topo_old(): """ Test D4 routing on a toy irregular topo, old style, where 'method' is passed to the run method, not the init. """ fr = FlowRouter(mg) fr.route_flow(method='D4') assert_array_equal(A_target_D4, mg.at_node['drainage_area']) assert_array_equal(frcvr_target_D4, mg.at_node['flow_receiver']) assert_array_equal(upids_target_D4, mg.at_node['upstream_node_order']) assert_array_equal(links2rcvr_target_D4, mg.at_node['links_to_flow_receiver']) assert_array_almost_equal(steepest_target_D4, mg.at_node['topographic__steepest_slope'])
def setup_dans_grid(): """ Create a 7x7 test grid with a well defined hole in it. """ from landlab import RasterModelGrid from landlab.components.flow_routing import (FlowRouter, DepressionFinderAndRouter) global fr, lf, mg global z, r_new, r_old, A_new, A_old, s_new, depr_outlet_target mg = RasterModelGrid(7, 7, 1.) z = np.array([ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 2.0, 1.6, 1.5, 1.6, 2.0, 0.0, 0.0, 2.0, 1.7, 1.6, 1.7, 2.0, 0.0, 0.0, 2.0, 1.8, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 0.6, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, -0.5, 0.0, 0.0, 0.0, 0.0 ]).flatten() r_old = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 13, 14, 14, 17, 17, 17, 20, 20, 21, 21, 17, 17, 17, 27, 27, 28, 28, 37, 38, 39, 34, 34, 35, 44, 44, 44, 46, 41, 41, 42, 43, 44, 45, 46, 47, 48 ]).flatten() r_new = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 13, 14, 14, 23, 23, 24, 20, 20, 21, 21, 30, 30, 24, 27, 27, 28, 28, 37, 38, 39, 34, 34, 35, 44, 44, 44, 46, 41, 41, 42, 43, 44, 45, 46, 47, 48 ]).flatten() A_old = np.array([[ 0., 1., 1., 1., 1., 1., 0., 0., 1., 1., 1., 1., 1., 0., 1., 1., 1., 6., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 2., 2., 2., 1., 1., 0., 0., 5., 0., 2., 0., 0. ]]).flatten() A_new = np.array([[ 0., 1., 1., 1., 1., 1., 0., 0., 1., 1., 1., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 3., 3., 1., 1., 1., 1., 1., 7., 1., 1., 1., 1., 0., 1., 8., 2., 2., 1., 1., 0., 0., 11., 0., 2., 0., 0. ]]).flatten() s_new = np.array([ 0, 1, 8, 2, 9, 3, 10, 4, 11, 5, 12, 6, 7, 13, 14, 15, 20, 19, 21, 22, 27, 26, 28, 29, 34, 33, 35, 41, 40, 42, 43, 44, 36, 37, 30, 23, 16, 17, 24, 18, 25, 38, 31, 45, 46, 39, 32, 47, 48 ]).flatten() depr_outlet_target = np.array([ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 30, 30, 30, XX, XX, XX, XX, 30, 30, 30, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX ]).flatten() mg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(mg) lf = DepressionFinderAndRouter(mg)
def setup_dans_grid4(): """ Create a 10x10 test grid with two well defined holes in it, into an inclined surface. This time, one of the holes is a stupid shape, which will require the component to arrange flow back "uphill". """ global hf, fr, mg global z, depr_outlet_target global lake, lake1, lake2, outlet, outlet_array lake1 = np.array([34, 35, 36, 44, 45, 46, 54, 55, 56, 65, 74]) lake2 = np.array([78, 87, 88]) guard_nodes = np.array([23, 33, 53, 63, 73, 83]) lake = np.concatenate((lake1, lake2)) outlet = 35 # shouldn't be needed outlet_array = np.array([outlet]) mg = RasterModelGrid(10, 10, 1.) z = np.ones(100, dtype=float) # add slope z += mg.node_x z[guard_nodes] += 0.001 # forces the flow out of a particular node z[lake] = 0. depr_outlet_target = np.empty(100, dtype=float) depr_outlet_target.fill(XX) depr_outlet_target = XX # not well defined in this simplest case...? mg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(mg)
def setup_dans_grid3(): """ Create a 10x10 test grid with two well defined holes in it, into an inclined surface. """ global hf, fr, mg global z, depr_outlet_target global lake, lake1, lake2, outlet, outlet_array, rcvr lake1 = np.array([34, 35, 36, 44, 45, 46, 54, 55, 56]) lake2 = np.array([77, 78, 87, 88]) guard_nodes = np.array([23, 33, 53, 63]) lake = np.concatenate((lake1, lake2)) outlet = 35 # shouldn't be needed outlet_array = np.array([outlet]) mg = RasterModelGrid(10, 10, 1.) z = np.ones(100, dtype=float) # add slope z += mg.node_x z[guard_nodes] += 0.001 z[lake] = 0. depr_outlet_target = np.empty(100, dtype=float) depr_outlet_target.fill(XX) depr_outlet_target = XX # not well defined in this simplest case...? mg.add_field('node', 'topographic__elevation', z, units='-') fr = FlowRouter(mg)
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_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 test_irreg_topo(dans_grid2): """Test D8 routing on a toy irregular topo.""" fr = FlowRouter(dans_grid2.mg) fr.route_flow() assert_array_equal(dans_grid2.A_target_D8, dans_grid2.mg.at_node["drainage_area"]) assert_array_equal( dans_grid2.frcvr_target_D8, dans_grid2.mg.at_node["flow__receiver_node"] ) assert_array_equal( dans_grid2.upids_target_D8, dans_grid2.mg.at_node["flow__upstream_node_order"] ) assert_array_equal( dans_grid2.links2rcvr_target_D8, dans_grid2.mg.at_node["flow__link_to_receiver_node"], ) assert dans_grid2.steepest_target_D8 == approx( dans_grid2.mg.at_node["topographic__steepest_slope"] )
def test_irreg_topo_new(dans_grid2): """Test D4 routing on a toy irregular topo. 'method' passed to init.""" with pytest.deprecated_call(): fr = FlowRouter(dans_grid2.mg, method="D4") fr.run_one_step() assert_array_equal(dans_grid2.A_target_D4, dans_grid2.mg.at_node["drainage_area"]) assert_array_equal( dans_grid2.frcvr_target_D4, dans_grid2.mg.at_node["flow__receiver_node"] ) assert_array_equal( dans_grid2.upids_target_D4, dans_grid2.mg.at_node["flow__upstream_node_order"] ) assert_array_equal( dans_grid2.links2rcvr_target_D4, dans_grid2.mg.at_node["flow__link_to_receiver_node"], ) assert dans_grid2.steepest_target_D4 == pytest.approx( dans_grid2.mg.at_node["topographic__steepest_slope"] )
def test_internal_closed(internal_closed): """Test closed nodes in the core of the grid.""" fr = FlowRouter(internal_closed.mg) fr.route_flow() assert internal_closed.A_target == approx( internal_closed.mg.at_node["drainage_area"] ) assert_array_equal( internal_closed.frcvr_target, internal_closed.mg.at_node["flow__receiver_node"] ) assert_array_equal( internal_closed.links2rcvr_target, internal_closed.mg.at_node["flow__link_to_receiver_node"], ) assert internal_closed.A_target == approx( internal_closed.mg.at_node["surface_water__discharge"] ) assert internal_closed.steepest_target == approx( internal_closed.mg.at_node["topographic__steepest_slope"] )
def test_sp_widths(): input_str = os.path.join(_THIS_DIR, 'test_sp_params_widths.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = 5 ncols = 5 dx = inputs.read_float('dx') dt = inputs.read_float('dt') mg = RasterModelGrid(nrows, ncols, dx) widths = np.ones(mg.number_of_nodes, dtype=float) mg.add_zeros('topographic__elevation', at='node') z = np.array([5., 5., 0., 5., 5., 5., 2., 1., 2., 5., 5., 3., 2., 3., 5., 5., 4., 4., 4., 5., 5., 5., 5., 5., 5.]) mg['node']['topographic__elevation'] = z fr = FlowRouter(mg) sp = StreamPowerEroder(mg, use_W=widths, **inputs) # perform the loop (once!) for i in range(1): fr.route_flow() sqrt_A = mg.at_node['drainage_area']**0.5 widths[mg.core_nodes] = sqrt_A[mg.core_nodes]/sqrt_A[ mg.core_nodes].mean() # so widths has mean=1. # note the issue with drainage_area not defined at perimeter => nans # if not careful... sp.run_one_step(dt) z_tg = np.array([ 5. , 5. , 0. , 5. , 5. , 5. , 1.37222369, 0.36876358, 1.37222369, 5. , 5. , 2.17408606, 1.07986038, 2.17408606, 5. , 5. , 3.08340277, 2.85288049, 3.08340277, 5. , 5. , 5. , 5. , 5. , 5. ]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_accumulate_D8(dans_grid1): """Test accumulation works for D8 in a simple scenario.""" fr = FlowRouter(dans_grid1.mg) fr.route_flow() assert_array_equal(dans_grid1.A_target, dans_grid1.mg.at_node["drainage_area"]) assert_array_equal( dans_grid1.frcvr_target, dans_grid1.mg.at_node["flow__receiver_node"] ) assert_array_equal( dans_grid1.upids_target, dans_grid1.mg.at_node["flow__upstream_node_order"] ) assert_array_equal( dans_grid1.links2rcvr_target, dans_grid1.mg.at_node["flow__link_to_receiver_node"], ) assert_array_equal( dans_grid1.A_target, dans_grid1.mg.at_node["surface_water__discharge"] ) assert_array_equal( dans_grid1.steepest_target, dans_grid1.mg.at_node["topographic__steepest_slope"] )