def test___init__(self):
        g = ProfileGrid(10, 2, self.bs, [(5, NodeStatus.FIXED_VALUE_BOUNDARY)])
        assert g.size == 10
        assert g.spacing == 2.
        assert g.length == 18.

        g = ProfileGrid(15, 3., ProfileBoundaryStatus(self.bs),
                        [Node(5, NodeStatus.FIXED_VALUE_BOUNDARY)])
        assert g.size == 15
        assert g.spacing == 3.
        assert g.length == 42.

        with pytest.raises(IndexError):
            ProfileGrid(10, 2, self.bs,
                        [(15, NodeStatus.FIXED_VALUE_BOUNDARY)])
Beispiel #2
0
    def setup_class(cls):
        profile_grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
        cls.profile_flow_graph = FlowGraph(profile_grid, SingleFlowRouter())
        cls.profile_elevation = np.r_[0.82, 0.16, 0.14, 0.20, 0.71, 0.97, 0.41, 0.09]
        cls.result_profile_elevation = cls.profile_flow_graph.update_routes(
            cls.profile_elevation
        )

        raster_grid = RasterGrid(
            [4, 4],
            [1.1, 1.2],
            RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY),
            [],
        )
        cls.raster_flow_graph = FlowGraph(raster_grid, SingleFlowRouter())
        cls.raster_elevation = np.array(
            [
                [0.82, 0.16, 0.14, 0.20],
                [0.71, 0.97, 0.41, 0.09],
                [0.49, 0.01, 0.19, 0.38],
                [0.29, 0.82, 0.09, 0.88],
            ]
        )
        cls.result_raster_elevation = cls.raster_flow_graph.update_routes(
            cls.raster_elevation
        )
    def test_profile_grid(self, func, k):
        spacing = 300.
        grid = ProfileGrid(4, 300, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
        flow_graph = FlowGraph(grid, SingleFlowRouter())

        h = 1.
        elevation = np.array([0., h, h, 0.], dtype='d')
        graph_elevation = flow_graph.update_routes(elevation)

        drainage_area = flow_graph.accumulate(1.)
        erosion = np.zeros_like(elevation)
        m_exp = 0.5
        n_exp = 1.

        dt = 1.  # use small time step (compare with explicit scheme)
        tolerance = 1e-3

        n_corr = func(erosion, elevation, drainage_area, flow_graph, k, m_exp,
                      n_exp, dt, tolerance)

        slope = h / spacing
        a = spacing
        k_coef = 1e-3
        err = dt * k_coef * a**m_exp * slope**n_exp
        expected_erosion = np.array([0., err, err, 0.], dtype='d')

        np.testing.assert_allclose(erosion, expected_erosion, atol=1e-5)
        assert n_corr == 0
    def test_update_routes(self):
        grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
        flow_graph = FlowGraph(grid, SingleFlowRouter())
        elevation = np.r_[0.82, 0.16, 0.14, 0.20, 0.71, 0.97, 0.41, 0.09]

        graph_elevation = flow_graph.update_routes(elevation)

        npt.assert_equal(flow_graph.receivers()[:, 0], np.r_[1, 2, 2, 2, 3, 6,
                                                             7, 7])
        npt.assert_equal(flow_graph.receivers_count(), np.ones(elevation.size))
        npt.assert_equal(flow_graph.receivers_weight()[:, 0],
                         np.ones(elevation.size))
        npt.assert_equal(flow_graph.receivers_weight()[:, 1],
                         np.zeros(elevation.size))

        m = np.iinfo(np.uint64).max
        npt.assert_equal(
            flow_graph.donors(),
            np.array([
                [m, m, m],
                [0, m, m],
                [1, 2, 3],
                [4, m, m],
                [m, m, m],
                [m, m, m],
                [5, m, m],
                [6, 7, m],
            ]),
        )
        npt.assert_equal(flow_graph.donors_count(), np.r_[0, 1, 3, 1, 0, 0, 1,
                                                          2])

        npt.assert_equal(graph_elevation, elevation)
Beispiel #5
0
def grid(grid_type):
    if grid_type == "profile":
        bs = ProfileBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY)
        return ProfileGrid(4, 1.0, bs, [])

    elif grid_type == "raster":
        bs = RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY)
        return RasterGrid([3, 3], np.array([1.0, 1.0]), bs, [])
    def test___init__(self):
        profile_grid = ProfileGrid(8, 2.2,
                                   [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
        raster_grid = RasterGrid([5, 10], [2.2, 2.4],
                                 RasterBoundaryStatus(
                                     NodeStatus.FIXED_VALUE_BOUNDARY), [])

        FlowGraph(profile_grid, DummyFlowRouter())
        FlowGraph(profile_grid, MultipleFlowRouter(1., 1.1))
        FlowGraph(profile_grid, SingleFlowRouter())
Beispiel #7
0
 def test_from_length(self):
     g = ProfileGrid.from_length(
         11,
         20.0,
         ProfileBoundaryStatus(self.bs),
         [Node(5, NodeStatus.FIXED_VALUE_BOUNDARY)],
     )
     assert g.size == 11
     assert g.spacing == 2.0
     assert g.length == 20.0
    def test_accumulate(self):
        grid = ProfileGrid(8, 2.2, [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
        flow_graph = FlowGraph(grid, SingleFlowRouter())
        elevation = np.r_[0.82, 0.16, 0.14, 0.20, 0.71, 0.97, 0.41, 0.09]

        graph_elevation = flow_graph.update_routes(elevation)

        npt.assert_almost_equal(
            flow_graph.accumulate(np.ones(elevation.shape)),
            np.r_[2.2, 4.4, 11.0, 4.4, 2.2, 2.2, 4.4, 6.6],
        )

        grid = RasterGrid(
            [4, 4],
            [1.1, 1.2],
            RasterBoundaryStatus(NodeStatus.FIXED_VALUE_BOUNDARY),
            [],
        )
        flow_graph = FlowGraph(grid, SingleFlowRouter())
        elevation = np.array([
            [0.82, 0.16, 0.14, 0.20],
            [0.71, 0.97, 0.41, 0.09],
            [0.49, 0.01, 0.19, 0.38],
            [0.29, 0.82, 0.09, 0.88],
        ])

        graph_elevation = flow_graph.update_routes(elevation)

        expected = np.array([
            [1.32, 2.64, 3.96, 1.32],
            [1.32, 1.32, 1.32, 9.24],
            [1.32, 11.88, 1.32, 1.32],
            [1.32, 1.32, 2.64, 1.32],
        ])
        npt.assert_almost_equal(
            flow_graph.accumulate(np.ones(elevation.shape)), expected)

        npt.assert_almost_equal(flow_graph.accumulate(1.0), expected)

        npt.assert_almost_equal(flow_graph.accumulate(5.0), 5 * expected)

        data = np.array([
            [1.1, 1.0, 1.1, 1.0],
            [1.1, 1.0, 1.1, 1.0],
            [1.1, 1.0, 1.1, 1.0],
            [1.1, 1.0, 1.1, 1.0],
        ])
        expected = np.array([
            [1.452, 2.772, 4.224, 1.32],
            [1.452, 1.32, 1.452, 9.636],
            [1.452, 12.54, 1.452, 1.32],
            [1.452, 1.32, 2.772, 1.32],
        ])
        npt.assert_almost_equal(flow_graph.accumulate(data), expected)
 def setup_method(self, method):
     self.bs = [NodeStatus.FIXED_VALUE_BOUNDARY] * 2
     self.g = ProfileGrid(10, 2.2, self.bs,
                          [(5, NodeStatus.FIXED_VALUE_BOUNDARY)])
 def test_receivers(self):
     profile_grid = ProfileGrid(8, 2.2,
                                [NodeStatus.FIXED_VALUE_BOUNDARY] * 2, [])
     flow_graph = FlowGraph(profile_grid, SingleFlowRouter(),
                            NoSinkResolver())
Beispiel #11
0
class TestProfileGrid:
    def setup_method(self, method):
        self.bs = [NodeStatus.FIXED_VALUE_BOUNDARY] * 2
        self.g = ProfileGrid(10, 2.2, self.bs,
                             [(5, NodeStatus.FIXED_VALUE_BOUNDARY)])

    def test_static_props(self):
        assert ProfileGrid.is_structured is True
        assert ProfileGrid.is_uniform is True
        assert ProfileGrid.max_neighbors == 2

    def test___init__(self):
        g = ProfileGrid(10, 2, self.bs, [(5, NodeStatus.FIXED_VALUE_BOUNDARY)])
        assert g.size == 10
        assert g.spacing == 2.0
        assert g.length == 18.0

        g = ProfileGrid(
            15,
            3.0,
            ProfileBoundaryStatus(self.bs),
            [Node(5, NodeStatus.FIXED_VALUE_BOUNDARY)],
        )
        assert g.size == 15
        assert g.spacing == 3.0
        assert g.length == 42.0

        with pytest.raises(IndexError):
            ProfileGrid(10, 2, self.bs,
                        [(15, NodeStatus.FIXED_VALUE_BOUNDARY)])

    def test_from_length(self):
        g = ProfileGrid.from_length(
            11,
            20.0,
            ProfileBoundaryStatus(self.bs),
            [Node(5, NodeStatus.FIXED_VALUE_BOUNDARY)],
        )
        assert g.size == 11
        assert g.spacing == 2.0
        assert g.length == 20.0

    def test_size(self):
        assert self.g.size == 10

    def test_spacing(self):
        assert self.g.spacing == 2.2

    def test_shape(self):
        npt.assert_almost_equal(self.g.shape, np.r_[10])

    def test_length(self):
        assert self.g.length == 19.8

    def test_status_at_nodes(self):
        npt.assert_equal(self.g.status_at_nodes,
                         np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 1]))

    def test_neighbors_count(self):
        assert self.g.neighbors_count(0) == 1
        assert self.g.neighbors_count(5) == 2

    def test_neighbors_indices(self):
        npt.assert_equal(self.g.neighbors_indices(0), np.array([1]))
        npt.assert_equal(self.g.neighbors_indices(5), np.array([4, 6]))

        with pytest.raises(IndexError, match="grid index out of range"):
            self.g.neighbors(11)

    def test_neighbors_distances(self):
        npt.assert_equal(self.g.neighbors_distances(0), np.array([2.2]))
        npt.assert_equal(self.g.neighbors_distances(5), np.array([2.2, 2.2]))

        with pytest.raises(IndexError, match="grid index out of range"):
            self.g.neighbors(11)

    def test_neighbors(self):
        assert self.g.neighbors(0) == [Neighbor(1, 2.2, NodeStatus.CORE)]
        assert self.g.neighbors(1) == [
            Neighbor(0, 2.2, NodeStatus.FIXED_VALUE_BOUNDARY),
            Neighbor(2, 2.2, NodeStatus.CORE),
        ]
        assert self.g.neighbors(6) == [
            Neighbor(5, 2.2, NodeStatus.FIXED_VALUE_BOUNDARY),
            Neighbor(7, 2.2, NodeStatus.CORE),
        ]

        with pytest.raises(IndexError, match="grid index out of range"):
            self.g.neighbors(11)