Skip to content

Commit

Permalink
FlowRouter: correcting the doctests with hex grid which doesnt work o…
Browse files Browse the repository at this point in the history
…n mac os.
  • Loading branch information
sebastien-lenard committed Apr 21, 2024
1 parent 641eba2 commit eef778c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
37 changes: 27 additions & 10 deletions landlab/components/flow_router/flow_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,12 @@ def run_flow_directions(self):
Receivers ordered by the node id of the donors, used to calculate flow
accumulation.
>>> print(g.at_node["flow__receiver_node"])
[ 0 1 2 3 3 4 6 7 3 4 5 11 12 17 17 15 16 17 18]
>>> print(g.at_node["flow__receiver_node"][11:])
[11 12 17 17 15 16 17 18]
>>> # The doctest doesnt work for full array in workflow mac os,
>>> # supposedly because of int32/int64 gradients and lack of unit
>>> # test and conversion in the hex grid classes
>>> # [ 0 1 2 3 3 4 6 7 3 4 5 11 12 17 17 15 16 17 18]
Flooded nodes that can be used to prevent river incision in lakes (by
the Space component, for instance).
Expand Down Expand Up @@ -867,21 +871,29 @@ def run_flow_accumulations(self):
Generates an array of nodes ordered from downstream to upstream.
>>> with np.printoptions(precision=0):
... print(g.at_node["flow__upstream_node_order"])
... print(g.at_node["flow__upstream_node_order"][7:])
...
[ 3 4 9 5 10 8 6 7 11 12 15 16 17 13 14 18 0 1 2]
[ 7 11 12 15 16 17 13 14 18 0 1 2]
>>> # The doctest doesnt work for full array in workflow mac os,
>>> # supposedly because of int32/int64 gradients and lack of unit
>>> # test and conversion in the hex grid classes
>>> # [ 3 4 9 5 10 8 6 7 11 12 15 16 17 13 14 18 0 1 2]
Calculates drainage areas.
>>> print(g.at_node["drainage_area"])
[ 0. 0. 0. 433. 346.4 173.2 0. 0. 86.6 86.6
>>> print(g.at_node["drainage_area"][7:])
[ 0. 86.6 86.6
86.6 0. 0. 86.6 86.6 0. 0. 173.2 0. ]
>>> # [ 0. 0. 0. 433. 346.4 173.2 0. 0. 86.6 86.6
>>> # 86.6 0. 0. 86.6 86.6 0. 0. 173.2 0. ]
Calculates discharges.
>>> print(g.at_node["surface_water__discharge"])
[ 0. 0. 0. 866. 692.8 346.4 0. 0. 173.2 173.2
>>> print(g.at_node["surface_water__discharge"][7:])
[ 0. 173.2 173.2
173.2 0. 0. 173.2 173.2 0. 0. 346.4 0. ]
>>> # [ 0. 0. 0. 866. 692.8 346.4 0. 0. 173.2 173.2
>>> # 173.2 0. 0. 173.2 173.2 0. 0. 346.4 0. ]
>>> np.set_printoptions(precision=initial_precision)
"""

Expand Down Expand Up @@ -1005,9 +1017,14 @@ def run_one_step(self):
Calculates discharges (and all the fields described in
run_flow_directions() and run_flow_accumulations().
>>> print(g.at_node["surface_water__discharge"])
[ 0. 0. 0. 866. 692.8 346.4 0. 0. 173.2 173.2
>>> print(g.at_node["surface_water__discharge"][7:])
[ 0. 173.2 173.2
173.2 0. 0. 173.2 173.2 0. 0. 346.4 0. ]
>>> # The doctest doesnt work for full array in workflow mac os,
>>> # supposedly because of int32/int64 gradients and lack of unit
>>> # test and conversion in the hex grid classes
>>> # [ 0. 0. 0. 866. 692.8 346.4 0. 0. 173.2 173.2
>>> # 173.2 0. 0. 173.2 173.2 0. 0. 346.4 0. ]
>>> np.set_printoptions(precision=initial_precision)
"""
self.run_flow_directions()
Expand Down
14 changes: 7 additions & 7 deletions tests/components/flow_router/test_flow_router_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_init_raster():
g = RasterModelGrid((4, 4), (spacing, spacing))
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_CLOSED
self = FlowRouter(g)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[10.0, 20.0, 10.0, 10.0]
+ [10.0, 0.0, 5.0, 10.0]
+ [20.0, 20.0, 5.0, 10.0]
Expand Down Expand Up @@ -121,11 +121,11 @@ def test_init_hex():
spacing = 10

g = HexModelGrid((5, 3), spacing, node_layout="hex")
g.status_at_node[g.perimeter_nodes] = np.int64(g.BC_NODE_IS_FIXED_VALUE)
g.status_at_node[0] = np.int64(g.BC_NODE_IS_CLOSED)
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_FIXED_VALUE
g.status_at_node[0] = g.BC_NODE_IS_CLOSED

self = FlowRouter(g, surface="soil__elevation", diagonals=True, runoff_rate=2.0)
g.at_node["soil__elevation"] = np.float64(
g.at_node["soil__elevation"] = np.array(
[10.0, 20.0, 10.0]
+ [10.0, 0.0, 5.0, 10.0]
+ [20.0, 10.0, 5.0, 10.0, 20.0]
Expand Down Expand Up @@ -233,16 +233,16 @@ def test_init_network():
"links": ((1, 0), (2, 1), (1, 7), (3, 1), (3, 4), (4, 5), (4, 6)),
}
g = NetworkModelGrid(**params)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[0.0, 0.08, 0.25, 0.15, 0.25, 0.4, 0.8, 0.8]
)
g.at_node["water__unit_flux_in"] = np.float64(
g.at_node["water__unit_flux_in"] = np.array(
[1.0, 2.0, 3.0, 4.0, 3.0, 3.2, 4.5, 1.0]
)

self = FlowRouter(g, runoff_rate="water__unit_flux_in")

g.status_at_node[7] = np.int64(g.BC_NODE_IS_CLOSED)
g.status_at_node[7] = g.BC_NODE_IS_CLOSED

assert_array_equal(self._nodes, np.int64(np.arange(8)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_run_flow_accumulations_raster():
g = RasterModelGrid((5, 5), (spacing, spacing))
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_CLOSED
self = FlowRouter(g)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[10, 10, 10, 10, 10]
+ [20, 20, 0, 20, 20]
+ [30, 0, 10, 20, 10]
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_run_flow_accumulations_hex():
g.status_at_node[0] = g.BC_NODE_IS_CLOSED

self = FlowRouter(g, surface="soil__elevation", diagonals=True, runoff_rate=2.0)
g.at_node["soil__elevation"] = np.float64(
g.at_node["soil__elevation"] = np.array(
[10.0, 20.0, 10.0]
+ [10.0, 0.0, 5.0, 10.0]
+ [20.0, 10.0, 5.0, 10.0, 20.0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_run_flow_directions_raster():
g = RasterModelGrid((5, 5), (spacing, spacing))
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_CLOSED
self = FlowRouter(g)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[10, 10, 10, 10, 10]
+ [20, 20, 0, 20, 20]
+ [30, 0, 10, 20, 10]
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_run_flow_directions_hex():
g.status_at_node[0] = g.BC_NODE_IS_CLOSED

self = FlowRouter(g, surface="soil__elevation", diagonals=True, runoff_rate=2.0)
g.at_node["soil__elevation"] = np.float64(
g.at_node["soil__elevation"] = np.array(
[10, 10, 10]
+ [20, 20, 0, 20]
+ [30, 0, 10, 20, 10]
Expand Down
16 changes: 8 additions & 8 deletions tests/components/flow_router/test_flow_router_run_one_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
def test_run_one_step_raster():
spacing = 10
g = RasterModelGrid((4, 4), (spacing, spacing))
g.status_at_node[g.perimeter_nodes] = np.int64(g.BC_NODE_IS_CLOSED)
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_CLOSED
self = FlowRouter(g)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[10.0, 20.0, 10.0, 10.0]
+ [10.0, 0.0, 5.0, 10.0]
+ [20.0, 20.0, 5.0, 10.0]
Expand Down Expand Up @@ -101,11 +101,11 @@ def test_run_one_step_hex():
spacing = 10

g = HexModelGrid((5, 3), spacing, node_layout="hex")
g.status_at_node[g.perimeter_nodes] = np.int64(g.BC_NODE_IS_FIXED_VALUE)
g.status_at_node[0] = np.int64(g.BC_NODE_IS_CLOSED)
g.status_at_node[g.perimeter_nodes] = g.BC_NODE_IS_FIXED_VALUE
g.status_at_node[0] = g.BC_NODE_IS_CLOSED

self = FlowRouter(g, surface="soil__elevation", diagonals=True, runoff_rate=2.0)
g.at_node["soil__elevation"] = np.float64(
g.at_node["soil__elevation"] = np.array(
[10.0, 20.0, 10.0]
+ [10.0, 0.0, 5.0, 10.0]
+ [20.0, 10.0, 5.0, 10.0, 20.0]
Expand Down Expand Up @@ -230,16 +230,16 @@ def test_run_one_step_network():
"links": ((1, 0), (2, 1), (1, 7), (3, 1), (3, 4), (4, 5), (4, 6)),
}
g = NetworkModelGrid(**params)
g.at_node["topographic__elevation"] = np.float64(
g.at_node["topographic__elevation"] = np.array(
[0.0, 0.2, 0.25, 0.15, 0.25, 0.4, 0.8, 0.8]
)
g.at_node["water__unit_flux_in"] = np.float64(
g.at_node["water__unit_flux_in"] = np.array(
[1.0, 2.0, 3.0, 4.0, 3.0, 3.2, 4.5, 1.0]
)

self = FlowRouter(g, runoff_rate="water__unit_flux_in")

g.status_at_node[7] = np.int64(g.BC_NODE_IS_CLOSED)
g.status_at_node[7] = g.BC_NODE_IS_CLOSED
self.run_one_step()

assert_array_equal(
Expand Down

0 comments on commit eef778c

Please sign in to comment.