예제 #1
0
def test_director_adding_methods_are_equivalent_MFD():
    """Check that different methods to specifying the director are the same."""

    mg0 = RasterModelGrid((10, 10), xy_spacing=(1, 1))
    mg0.add_field("topographic__elevation",
                  mg0.node_x**2 + mg0.node_y**2,
                  at="node")
    fa0 = FlowAccumulator(mg0, flow_director="MFD")
    fa0.run_one_step()

    mg1 = RasterModelGrid((10, 10), xy_spacing=(1, 1))
    mg1.add_field("topographic__elevation",
                  mg1.node_x**2 + mg1.node_y**2,
                  at="node")
    fa1 = FlowAccumulator(mg1, flow_director="FlowDirectorMFD")
    fa1.run_one_step()

    mg2 = RasterModelGrid((10, 10), xy_spacing=(1, 1))
    mg2.add_field("topographic__elevation",
                  mg2.node_x**2 + mg2.node_y**2,
                  at="node")
    fa2 = FlowAccumulator(mg2, flow_director=FlowDirectorMFD)
    fa2.run_one_step()

    mg3 = RasterModelGrid((10, 10), xy_spacing=(1, 1))
    mg3.add_field("topographic__elevation",
                  mg3.node_x**2 + mg3.node_y**2,
                  at="node")
    fd = FlowDirectorMFD(mg3)
    fa3 = FlowAccumulator(mg3, flow_director=fd)
    fa3.run_one_step()

    for loc in ["node", "link", "grid"]:
        for key in mg0[loc].keys():
            if loc == "grid":
                assert_array_equal(mg0[loc][key][0], mg1[loc][key][0])

                assert_array_equal(mg1[loc][key][0], mg2[loc][key][0])

                assert_array_equal(mg2[loc][key][0], mg3[loc][key][0])
            else:
                assert_array_equal(mg0[loc][key], mg1[loc][key])

                assert_array_equal(mg1[loc][key], mg2[loc][key])

                assert_array_equal(mg2[loc][key], mg3[loc][key])
예제 #2
0
def test_director_adding_methods_are_equivalent_MFD():
    """Check that different methods to specifying the director are the same."""

    mg0 = RasterModelGrid((10, 10), spacing=(1, 1))
    z0 = mg0.add_field("topographic__elevation",
                       mg0.node_x**2 + mg0.node_y**2,
                       at="node")
    fa0 = FlowAccumulator(mg0, flow_director="MFD")
    fa0.run_one_step()

    mg1 = RasterModelGrid((10, 10), spacing=(1, 1))
    z1 = mg1.add_field("topographic__elevation",
                       mg1.node_x**2 + mg1.node_y**2,
                       at="node")
    fa1 = FlowAccumulator(mg1, flow_director="FlowDirectorMFD")
    fa1.run_one_step()

    mg2 = RasterModelGrid((10, 10), spacing=(1, 1))
    z2 = mg2.add_field("topographic__elevation",
                       mg2.node_x**2 + mg2.node_y**2,
                       at="node")
    fa2 = FlowAccumulator(mg2, flow_director=FlowDirectorMFD)
    fa2.run_one_step()

    mg3 = RasterModelGrid((10, 10), spacing=(1, 1))
    z3 = mg3.add_field("topographic__elevation",
                       mg3.node_x**2 + mg3.node_y**2,
                       at="node")
    fd = FlowDirectorMFD(mg3)
    fa3 = FlowAccumulator(mg3, flow_director=fd)
    fa3.run_one_step()

    for key in mg0.at_node.keys():
        assert_array_equal(mg0.at_node[key], mg1.at_node[key])

        assert_array_equal(mg1.at_node[key], mg2.at_node[key])

        assert_array_equal(mg2.at_node[key], mg3.at_node[key])
예제 #3
0
def test_check_fields():
    """Check to make sure the right fields have been created.

    Check that the sizes of at least one are also correct (they are at node
    so if one is the right size, then they all should be)
    """

    mg0 = RasterModelGrid((10, 10), spacing=(1, 1))
    z0 = mg0.add_field('topographic__elevation',
                       mg0.node_x**2 + mg0.node_y**2,
                       at='node')
    fd0 = _FlowDirector(mg0, 'topographic__elevation')
    assert list(mg0.at_node.keys()) == ['topographic__elevation']
    assert np.size(
        mg0.at_node['topographic__elevation']) == mg0.number_of_nodes

    mg1 = RasterModelGrid((10, 10), spacing=(1, 1))
    z1 = mg1.add_field('topographic__elevation',
                       mg1.node_x**2 + mg1.node_y**2,
                       at='node')
    fd1 = _FlowDirectorToMany(mg1, 'topographic__elevation')
    assert sorted(list(mg1.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__receiver_node',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg1.at_node['topographic__elevation']) == mg1.number_of_nodes

    mg2 = RasterModelGrid((10, 10), spacing=(1, 1))
    z2 = mg2.add_field('topographic__elevation',
                       mg2.node_x**2 + mg2.node_y**2,
                       at='node')
    fd2 = _FlowDirectorToOne(mg2, 'topographic__elevation')
    assert sorted(list(mg2.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__receiver_node',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg2.at_node['topographic__elevation']) == mg2.number_of_nodes

    mg3 = RasterModelGrid((10, 10), spacing=(1, 1))
    z3 = mg3.add_field('topographic__elevation',
                       mg3.node_x**2 + mg3.node_y**2,
                       at='node')
    fd3 = FlowDirectorMFD(mg3, 'topographic__elevation')
    assert sorted(list(mg3.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__links_to_receiver_nodes',
        'flow__receiver_node',
        'flow__receiver_nodes',
        'flow__receiver_proportions',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg3.at_node['topographic__elevation']) == mg3.number_of_nodes

    mg4 = RasterModelGrid((10, 10), spacing=(1, 1))
    z4 = mg4.add_field('topographic__elevation',
                       mg4.node_x**2 + mg4.node_y**2,
                       at='node')
    fd4 = FlowDirectorDINF(mg4, 'topographic__elevation')
    assert sorted(list(mg4.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__links_to_receiver_nodes',
        'flow__receiver_node',
        'flow__receiver_nodes',
        'flow__receiver_proportions',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg4.at_node['topographic__elevation']) == mg4.number_of_nodes

    mg5 = RasterModelGrid((10, 10), spacing=(1, 1))
    z5 = mg5.add_field('topographic__elevation',
                       mg5.node_x**2 + mg5.node_y**2,
                       at='node')
    fd5 = FlowDirectorSteepest(mg5, 'topographic__elevation')
    assert sorted(list(mg5.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__receiver_node',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg5.at_node['topographic__elevation']) == mg5.number_of_nodes

    mg6 = RasterModelGrid((10, 10), spacing=(1, 1))
    z6 = mg6.add_field('topographic__elevation',
                       mg6.node_x**2 + mg6.node_y**2,
                       at='node')
    fd6 = FlowDirectorD8(mg6, 'topographic__elevation')
    assert sorted(list(mg6.at_node.keys())) == [
        'flow__link_to_receiver_node',
        'flow__receiver_node',
        'flow__sink_flag',
        'topographic__elevation',
        'topographic__steepest_slope',
    ]
    assert np.size(
        mg6.at_node['topographic__elevation']) == mg6.number_of_nodes
예제 #4
0
def test_properties():
    mg = RasterModelGrid((5, 5), xy_spacing=(1, 1))
    mg.add_field("topographic__elevation", mg.node_x ** 2 + mg.node_y ** 2, at="node")
    fd = FlowDirectorMFD(mg, "topographic__elevation")
    fd.run_one_step()

    sink_true = np.array(
        [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1]
    )
    assert_array_equal(fd.sink_flag, sink_true)

    steepest_true = np.array(
        [
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 1.0],
            [0.0, 0.0, 3.0, 1.0],
            [0.0, 0.0, 5.0, 1.0],
            [0.0, 0.0, 7.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 3.0],
            [0.0, 0.0, 3.0, 3.0],
            [0.0, 0.0, 5.0, 3.0],
            [0.0, 0.0, 7.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 5.0],
            [0.0, 0.0, 3.0, 5.0],
            [0.0, 0.0, 5.0, 5.0],
            [0.0, 0.0, 7.0, 0.0],
            [0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 7.0],
            [0.0, 0.0, 0.0, 7.0],
            [0.0, 0.0, 0.0, 7.0],
            [0.0, 0.0, 0.0, 0.0],
        ]
    )
    assert_array_equal(fd.node_steepest_slope, steepest_true)

    true_proportions = np.array(
        [
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.5, 0.5],
            [0.0, 0.0, 0.75, 0.25],
            [0.0, 0.0, 0.83333333, 0.16666667],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.25, 0.75],
            [0.0, 0.0, 0.5, 0.5],
            [0.0, 0.0, 0.625, 0.375],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.16666667, 0.83333333],
            [0.0, 0.0, 0.375, 0.625],
            [0.0, 0.0, 0.5, 0.5],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0, 0.0],
        ]
    )
    assert_array_almost_equal(fd.proportions_of_flow, true_proportions)

    true_link = np.array(
        [
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, 9, 5],
            [-1, -1, 10, 6],
            [-1, -1, 11, 7],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, 18, 14],
            [-1, -1, 19, 15],
            [-1, -1, 20, 16],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, 27, 23],
            [-1, -1, 28, 24],
            [-1, -1, 29, 25],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
            [-1, -1, -1, -1],
        ]
    )
    assert_array_equal(fd.link_to_flow_receiving_node, true_link)

    true_node = np.array(
        [
            [0, -1, -1, -1],
            [1, -1, -1, -1],
            [2, -1, -1, -1],
            [3, -1, -1, -1],
            [4, -1, -1, -1],
            [5, -1, -1, -1],
            [-1, -1, 5, 1],
            [-1, -1, 6, 2],
            [-1, -1, 7, 3],
            [9, -1, -1, -1],
            [10, -1, -1, -1],
            [-1, -1, 10, 6],
            [-1, -1, 11, 7],
            [-1, -1, 12, 8],
            [14, -1, -1, -1],
            [15, -1, -1, -1],
            [-1, -1, 15, 11],
            [-1, -1, 16, 12],
            [-1, -1, 17, 13],
            [19, -1, -1, -1],
            [20, -1, -1, -1],
            [21, -1, -1, -1],
            [22, -1, -1, -1],
            [23, -1, -1, -1],
            [24, -1, -1, -1],
        ]
    )
    assert_array_equal(fd.node_receiving_flow, true_node)
예제 #5
0
def test_check_fields():
    """Check to make sure the right fields have been created.

    Check that the sizes of at least one are also correct (they are at node
    so if one is the right size, then they all should be)
    """

    mg0 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg0.add_field("topographic__elevation",
                  mg0.node_x**2 + mg0.node_y**2,
                  at="node")
    _FlowDirector(mg0, "topographic__elevation")
    assert sorted(list(mg0.at_node.keys())) == [
        "flow__sink_flag",
        "topographic__elevation",
    ]
    assert np.size(
        mg0.at_node["topographic__elevation"]) == mg0.number_of_nodes

    mg1 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg1.add_field("topographic__elevation",
                  mg1.node_x**2 + mg1.node_y**2,
                  at="node")
    _FlowDirectorToMany(mg1, "topographic__elevation")
    assert sorted(list(mg1.at_node.keys())) == [
        "flow__sink_flag",
        "topographic__elevation",
    ]
    assert np.size(
        mg1.at_node["topographic__elevation"]) == mg1.number_of_nodes

    mg2 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg2.add_field("topographic__elevation",
                  mg2.node_x**2 + mg2.node_y**2,
                  at="node")
    _FlowDirectorToOne(mg2, "topographic__elevation")
    assert sorted(list(mg2.at_node.keys())) == [
        "flow__link_to_receiver_node",
        "flow__receiver_node",
        "flow__sink_flag",
        "topographic__elevation",
        "topographic__steepest_slope",
    ]
    assert np.size(
        mg2.at_node["topographic__elevation"]) == mg2.number_of_nodes

    mg3 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg3.add_field("topographic__elevation",
                  mg3.node_x**2 + mg3.node_y**2,
                  at="node")
    FlowDirectorMFD(mg3, "topographic__elevation")
    assert sorted(list(mg3.at_node.keys())) == [
        "flow__link_to_receiver_node",
        "flow__receiver_node",
        "flow__receiver_proportions",
        "flow__sink_flag",
        "topographic__elevation",
        "topographic__steepest_slope",
    ]
    assert np.size(
        mg3.at_node["topographic__elevation"]) == mg3.number_of_nodes

    mg4 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg4.add_field("topographic__elevation",
                  mg4.node_x**2 + mg4.node_y**2,
                  at="node")
    FlowDirectorDINF(mg4, "topographic__elevation")
    assert sorted(list(mg4.at_node.keys())) == [
        "flow__link_to_receiver_node",
        "flow__receiver_node",
        "flow__receiver_proportions",
        "flow__sink_flag",
        "topographic__elevation",
        "topographic__steepest_slope",
    ]
    assert np.size(
        mg4.at_node["topographic__elevation"]) == mg4.number_of_nodes

    mg5 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg5.add_field("topographic__elevation",
                  mg5.node_x**2 + mg5.node_y**2,
                  at="node")
    FlowDirectorSteepest(mg5, "topographic__elevation")
    assert sorted(list(mg5.at_node.keys())) == [
        "flow__link_to_receiver_node",
        "flow__receiver_node",
        "flow__sink_flag",
        "topographic__elevation",
        "topographic__steepest_slope",
    ]
    assert np.size(
        mg5.at_node["topographic__elevation"]) == mg5.number_of_nodes

    mg6 = RasterModelGrid((10, 10), spacing=(1, 1))
    mg6.add_field("topographic__elevation",
                  mg6.node_x**2 + mg6.node_y**2,
                  at="node")
    FlowDirectorD8(mg6, "topographic__elevation")
    assert sorted(list(mg6.at_node.keys())) == [
        "flow__link_to_receiver_node",
        "flow__receiver_node",
        "flow__sink_flag",
        "topographic__elevation",
        "topographic__steepest_slope",
    ]
    assert np.size(
        mg6.at_node["topographic__elevation"]) == mg6.number_of_nodes
예제 #6
0
def test_properties():
    mg = RasterModelGrid((5, 5), spacing=(1, 1))
    mg.add_field("topographic__elevation",
                 mg.node_x**2 + mg.node_y**2,
                 at="node")
    fd = FlowDirectorMFD(mg, "topographic__elevation")
    fd.run_one_step()

    sink_true = np.array([
        1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1,
        1
    ])
    assert_array_equal(fd.sink_flag, sink_true)

    steepest_true = np.array([
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 1., 1.],
        [0., 0., 3., 1.],
        [0., 0., 5., 1.],
        [0., 0., 7., 0.],
        [0., 0., 0., 0.],
        [0., 0., 1., 3.],
        [0., 0., 3., 3.],
        [0., 0., 5., 3.],
        [0., 0., 7., 0.],
        [0., 0., 0., 0.],
        [0., 0., 1., 5.],
        [0., 0., 3., 5.],
        [0., 0., 5., 5.],
        [0., 0., 7., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 7.],
        [0., 0., 0., 7.],
        [0., 0., 0., 7.],
        [0., 0., 0., 0.],
    ])
    assert_array_equal(fd.node_steepest_slope, steepest_true)

    true_proportions = np.array([
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [0., 0., 0.5, 0.5],
        [0., 0., 0.75, 0.25],
        [0., 0., 0.83333333, 0.16666667],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [0., 0., 0.25, 0.75],
        [0., 0., 0.5, 0.5],
        [0., 0., 0.625, 0.375],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [0., 0., 0.16666667, 0.83333333],
        [0., 0., 0.375, 0.625],
        [0., 0., 0.5, 0.5],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
        [1., 0., 0., 0.],
    ])
    assert_array_almost_equal(fd.proportions_of_flow, true_proportions)

    true_link = np.array([
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, 9, 5],
        [-1, -1, 10, 6],
        [-1, -1, 11, 7],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, 18, 14],
        [-1, -1, 19, 15],
        [-1, -1, 20, 16],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, 27, 23],
        [-1, -1, 28, 24],
        [-1, -1, 29, 25],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
        [-1, -1, -1, -1],
    ])
    assert_array_equal(fd.link_to_flow_receiving_node, true_link)

    true_node = np.array([
        [0, -1, -1, -1],
        [1, -1, -1, -1],
        [2, -1, -1, -1],
        [3, -1, -1, -1],
        [4, -1, -1, -1],
        [5, -1, -1, -1],
        [-1, -1, 5, 1],
        [-1, -1, 6, 2],
        [-1, -1, 7, 3],
        [9, -1, -1, -1],
        [10, -1, -1, -1],
        [-1, -1, 10, 6],
        [-1, -1, 11, 7],
        [-1, -1, 12, 8],
        [14, -1, -1, -1],
        [15, -1, -1, -1],
        [-1, -1, 15, 11],
        [-1, -1, 16, 12],
        [-1, -1, 17, 13],
        [19, -1, -1, -1],
        [20, -1, -1, -1],
        [21, -1, -1, -1],
        [22, -1, -1, -1],
        [23, -1, -1, -1],
        [24, -1, -1, -1],
    ])
    assert_array_equal(fd.node_receiving_flow, true_node)