Skip to main content

flow_paths

flow_paths

Routes of flow links through Junctions, and changes to them that only change the layout.

A Junction only merges or splits flow, so the flow links from a node through Junctions up to the next non-Junction nodes form the route of that node. Ribasim replaces each pair of links into and out of a Junction by a direct link, see simplify_graph! in the Ribasim core. Moving or adding Junctions on a route therefore changes how links are drawn, not how water flows, as long as each node keeps exchanging water with the same non-Junction nodes.

Classes

Name Description
Digitized The direction in which lines are digitized, to follow it where routes run along them.
Routes The routes of the flow links of a model through Junctions, as they are when the Routes are made.

Digitized

flow_paths.Digitized(lines, routes=None, points=())

The direction in which lines are digitized, to follow it where routes run along them.

Routes along lines, see ribasim_nl.network_snapping.route_basin_links, take the coordinates of the lines, so each of their segments is a segment of a line, digitized in the direction of the route, or against it.

Methods

Name Description
along Whether lines are digitized in the direction of a route, or against it, over most of its length.
along
flow_paths.Digitized.along(coords)

Whether lines are digitized in the direction of a route, or against it, over most of its length.

Returns
Name Type Description
bool | None None if lines cover less than half the route in one direction, or as much in both

Routes

flow_paths.Routes(model)

The routes of the flow links of a model through Junctions, as they are when the Routes are made.

Ribasim removes the Junctions without rows in Junction / static, the plain Junctions, before the simulation, see simplify_graph! in the Ribasim core. A connected group of plain Junctions, ignoring the direction of the links, with exactly one Basin as a neighbor, connects each other neighbor with that Basin, in the direction of its link with the group: a Basin tree. The direction of the links within a Basin tree, and with its Basin, does not matter. Other groups connect each node upstream of a Junction with each node downstream of it, along the links.

Methods

Name Description
basin_links The links of the Basin trees of basin_id, nearest to the Basin first, without its other links.
ends The first nodes downstream of a flow link, or upstream if upstream, other than plain Junctions.
flow_rate_links The flow links whose total flow is the flow rate of a node, as in Ribasim.
from_tables The routes of flow links with a from_node_id and to_node_id, with the Junctions in routed kept.
is_tree_link Whether a link connects a Junction of a Basin tree with its Basin, or with another Junction of the tree.
links The flow links into and out of node_id.
links_at The flow links into node_id if upstream, else out of it.
neighbors The nodes connected to node_id by a flow link.
next_node The node a flow link comes from if upstream, else goes to.
node_ends The first nodes downstream of a plain Junction outside a Basin tree, or upstream, see ends.
other_node The node at the other end of a flow link.
parent_link The link of a Junction of a Basin tree towards its Basin.
paths The route of a Basin: the path from basin_id to each link of its Basin trees, and to its direct links.
simplified_links The flow links between nodes other than plain Junctions that Ribasim makes of the flow links.
subtree The nodes beyond a link of a Basin, away from the Basin, that flow into the Basin, and that it flows into.
tree_basin The Basin of the Basin tree of a plain Junction, if it is in one.
ends
flow_paths.Routes.ends(link_id, upstream=False)

The first nodes downstream of a flow link, or upstream if upstream, other than plain Junctions.

A link of a Basin tree that points towards its Basin flows into the Basin, and gets its water from the nodes beyond it that flow into the Basin. A link that points away from the Basin flows into the nodes beyond it that the Basin flows into.

from_tables
flow_paths.Routes.from_tables(flow, node_types, routed=frozenset())

The routes of flow links with a from_node_id and to_node_id, with the Junctions in routed kept.

neighbors
flow_paths.Routes.neighbors(node_id)

The nodes connected to node_id by a flow link.

next_node
flow_paths.Routes.next_node(link_id, upstream)

The node a flow link comes from if upstream, else goes to.

node_ends
flow_paths.Routes.node_ends(node_id, upstream=False)

The first nodes downstream of a plain Junction outside a Basin tree, or upstream, see ends.

other_node
flow_paths.Routes.other_node(link_id, node_id)

The node at the other end of a flow link.

paths
flow_paths.Routes.paths(basin_id)

The route of a Basin: the path from basin_id to each link of its Basin trees, and to its direct links.

Links with plain Junctions of groups that are not a Basin tree are not part of it.

subtree
flow_paths.Routes.subtree(link_id)

The nodes beyond a link of a Basin, away from the Basin, that flow into the Basin, and that it flows into.

For a link of a Basin tree, these are the nodes reached through the Junctions beyond it. For a link with another node, that node itself, by the direction of the link.

tree_basin
flow_paths.Routes.tree_basin(node_id)

The Basin of the Basin tree of a plain Junction, if it is in one.

Functions

Name Description
branch_from_route Let the link from basin_id into node_id branch off the route between basin_id and towards_id.
branch_off_routes Let routes from the same point follow the other routes up to their point nearest to its end, and go straight on.
build_basin_tree Connect the nodes reached by the Basin tree of basin_id along their routes, with Junctions where they split.
combine_nodes Give nodes that exchange water with the same Basin a Junction of their own, which combines exactly them.
drainage_capacity The flow that each node can pass in the drainage situation, to choose the direction of Basin tree links.
fixed_junctions The Junctions that are kept as they are when Basin trees are rebuilt.
geometry_along The line from start_id along a path of links, see Routes.paths, to distance along its last link.
junction_tree The Junctions of the Basin trees of basin_id, see Routes, up to the Junctions in fixed, and what they reach.
merge_basin_trees Give each Basin one Junction tree for its inflows and outflows, along the current routes of its links.
merge_inflows Replace the flow links from basin_id to node_ids by a link to a new Junction, with straight links on.
merge_outflows Connect node_ids with straight links to the first Junction downstream that all of their routes pass.
open_water_basins The Basins whose area is open water: on average more than OPEN_WATER_WIDTH wide.
place_on_link Move node_id onto the point of link_id nearest to it, with its link with basin_id along their route.
prune_junction Remove a Junction without inflow or outflow, or merge the two links of a Junction that only passes flow on.
rebuild_junction_trees Route the flow links of Basins along the waterways, with one Junction tree per Basin, split where they split.
rebuild_rws_junction_trees Rebuild the Junctions of Rijkswaterstaat Basins along the waterways and a main route, see rebuild_junction_trees.
remove_flow_link Remove a flow link, and prune the Junctions at its ends, see prune_junction.
route_parts The geometries of the links of a path from start_id, each in the direction away from start_id.
rwzi_junctions The RWZI Junctions, see ribasim_nl.rwzi.attach_rwzi_junctions, only those attached to a route or not.
simplified_links The flow links between nodes other than plain Junctions that Ribasim makes of the flow links, see Routes.
split_distance The distance along geometry of the point nearest to point, at least margin from its ends if possible.
split_link Split a flow link at distance along it with a Junction: node_id moved there, or else a new one.
towards_basin Whether a link of a Basin tree points towards its Basin, given the nodes beyond it that flow in and out.
tree_links The links of the tree of routes that all start at the same point, with a Junction where they split.
tree_sides The Basin that all nodes exchange water with through one link each, directly or through its Basin tree.

branch_from_route

flow_paths.branch_from_route(model, node_id, basin_id, towards_id, margin=1.0)

Let the link from basin_id into node_id branch off the route between basin_id and towards_id.

The branch starts at a Junction at the point of that route nearest to node_id, or at the existing Junction if that point is within margin of one, and runs straight to node_id. Returns the node id of that Junction.

The link into node_id comes from the Basin, or from a Junction of its tree, see Routes: the regional models merge the links of a Basin into a tree where they share a route.

branch_off_routes

flow_paths.branch_off_routes(coords, branch, max_distance=MAX_BRANCH_DISTANCE)

Let routes from the same point follow the other routes up to their point nearest to its end, and go straight on.

This is for the routes of links that are added after the links of a Basin are routed, such as those from another model: routed along other lines, they run beside the routes of the Basin without a point in common. The routes whose ends are nearest to the other routes branch off first, so the others can branch off them, see attach_to_tree.

A route is kept if its end is more than max_distance from the other routes. Routes that are a straight line do not follow water, so no route branches off them.

Parameters

Name Type Description Default
coords dict[int, list[Coord]] per key, the coordinates of a route required
branch typing.Collection[int] keys of the routes to branch off the others required
max_distance float maximum distance between the end of a route and the other routes MAX_BRANCH_DISTANCE

Returns

Name Type Description
dict[int, list[Coord]] the routes, of which the others have a point where a route branches off

build_basin_tree

flow_paths.build_basin_tree(
    model,
    basin_id,
    ends,
    coords,
    sides,
    capacity,
    digitized=None,
    reuse=None,
)

Connect the nodes reached by the Basin tree of basin_id along their routes, with Junctions where they split.

Inflows and outflows share one tree, see Routes, of which Junctions less than JUNCTION_DISTANCE apart are one, see tree_links. The links to the nodes reached keep their ids and direction. The other links point towards the Basin or away from it, see towards_basin, given the nodes beyond them.

Parameters

Name Type Description Default
ends list[tuple[int, int]] per key, the node reached and its link, which still exists required
coords dict[int, list[Coord]] per key, the coordinates of the route from the Basin node to the node reached required
sides dict[int, tuple[frozenset[int], frozenset[int]]] per key, the nodes beyond the link that flow into the Basin, and out of it, see Routes.subtree required
capacity typing.Mapping[int, float] the drainage_capacity of the model required
digitized Digitized | None the lines the routes follow, see towards_basin None
reuse typing.Mapping[Coord, int] | None per location, a Junction without links that is the Junction of the tree there, which must be one None

Returns

Name Type Description
tuple[list[int], list[int]] the ids of the links that changed or are new, and of the new Junctions

combine_nodes

flow_paths.combine_nodes(model, node_ids, max_distance=MAX_COMBINE_DISTANCE)

Give nodes that exchange water with the same Basin a Junction of their own, which combines exactly them.

The flow rate of that Junction is the net flow of these nodes into the Basin, see Routes.flow_rate_links, such as that of a gemaal and the inlet next to it, of which the links meet at the Basin node, or run side by side. The link of the node with the lowest id, see tree_sides, is split at its point nearest to the other nodes, at least 10 m from its ends if possible. The other nodes get a straight link to the Junction there, and the Junctions they leave are pruned, see prune_junction. Each node keeps exchanging water with the same nodes, which is asserted.

Nothing changes if the nodes do not share a Basin, if a node is more than max_distance from the Junction, if a node lies between another node and the Basin, or if a Junction between only some of the nodes and the Basin has other links than flow links, such as an observed Junction, of which the flow rate would change.

Parameters

Name Type Description Default
model Model Ribasim model required
node_ids typing.Collection[int] nodes to combine, at least two required
max_distance float maximum distance between the nodes and their Junction MAX_COMBINE_DISTANCE

Returns

Name Type Description
int | None the Junction, or None if the nodes are not combined

drainage_capacity

flow_paths.drainage_capacity(model)

The flow that each node can pass in the drainage situation, to choose the direction of Basin tree links.

For Pumps and Outlets the largest flow_rate or max_flow_rate without control state, or in a control state with “afvoer”. ManningResistances, and placeholders and missing maxima, are unlimited (infinite). Also the LinearResistances, TabulatedRatingCurves, FlowBoundaries and UserDemands, by their largest flow or demand.

fixed_junctions

flow_paths.fixed_junctions(model)

The Junctions that are kept as they are when Basin trees are rebuilt.

These are observed, or listened to, or found by their code, like the RWZI Junctions, see ribasim_nl.rwzi.attach_rwzi_junctions. merge_basin_trees merges the trees through the RWZI Junctions that are attached to a route.

geometry_along

flow_paths.geometry_along(model, start_id, path, distance)

The line from start_id along a path of links, see Routes.paths, to distance along its last link.

distance is measured along the last link in its own direction.

junction_tree

flow_paths.junction_tree(routes, basin_id, fixed=())

The Junctions of the Basin trees of basin_id, see Routes, up to the Junctions in fixed, and what they reach.

Junctions in fixed are not part of the tree but reached by it, like the other nodes. Links of the Basin with plain Junctions of other groups are left out.

Returns

Name Type Description
tuple[set[int], list[int], dict[int, int]] the Junctions of the tree, the links between basin_id and them, and per link to a node reached, that node

merge_basin_trees

flow_paths.merge_basin_trees(
    model,
    read_lines=None,
    branch=(),
    max_branch_distance=MAX_BRANCH_DISTANCE,
)

Give each Basin one Junction tree for its inflows and outflows, along the current routes of its links.

The routes of the links of a Basin are followed through the Junctions of its Basin trees, see Routes, up to the fixed_junctions, and joined where they share their coordinates from the Basin node on, see build_basin_tree. The links to branch first branch off the other routes of their Basin, see branch_off_routes. A Basin whose tree would not change is left as it is. Each node keeps exchanging water with the same nodes, which is asserted.

The routes are also followed through the RWZI Junctions that are attached to a route, see rwzi_junctions, which stay where they are, with their code. The routes that run along the link that such a Junction split get its location as well, so that they stay joined beyond it, instead of running side by side from there.

Parameters

Name Type Description Default
read_lines LineReader | None reads the lines that the routes follow within an area, to follow the direction in which they are digitized, see towards_basin None
branch typing.Collection[int] links between a node and its Basin, or the tree of its Basin, to branch off the routes of that Basin ()
max_branch_distance float maximum distance between such a node and these routes MAX_BRANCH_DISTANCE

Returns

Name Type Description
list[int] the new Junctions

merge_inflows

flow_paths.merge_inflows(model, basin_id, node_ids)

Replace the flow links from basin_id to node_ids by a link to a new Junction, with straight links on.

The Junction is at the centroid of node_ids, see place_on_link to move it. Returns its node id.

merge_outflows

flow_paths.merge_outflows(model, node_ids)

Connect node_ids with straight links to the first Junction downstream that all of their routes pass.

The Junctions they passed before are pruned. Returns the node id of the Junction.

open_water_basins

flow_paths.open_water_basins(model, basin_ids)

The Basins whose area is open water: on average more than OPEN_WATER_WIDTH wide.

The average width of an area is twice its surface over its perimeter, which is the width of a long strip.

prune_junction

flow_paths.prune_junction(model, junction_id)

Remove a Junction without inflow or outflow, or merge the two links of a Junction that only passes flow on.

A Junction of a Basin tree, see Routes, only needs two links, in any direction. Neighboring Junctions are pruned in turn. A Junction with other links, such as observation links, is kept.

rebuild_junction_trees

flow_paths.rebuild_junction_trees(
    model,
    basin_ids,
    waterways,
    tolerance=10.0,
    water=None,
    routing_areas=None,
    open_water=True,
    beyond=None,
)

Route the flow links of Basins along the waterways, with one Junction tree per Basin, split where they split.

The Junctions of the Basin trees of a Basin, see junction_tree, are removed, except the fixed_junctions. The links to the nodes they reach are routed from the Basin node along the shortest paths of one network, see ribasim_nl.network_snapping.route_basin, such that its inflows and outflows share one tree, see build_basin_tree. Nodes far from the waterways are routed over the water, and nodes that are not reached within the area of the Basin along the waterways and the water around the straight line to them. With open water, such as a lake, the waterways are connected over it, and nodes far from them are routed over it, straight from the nearest point of the waterways from which that is possible. The nodes that are then not reached, such as those beyond the shore of a lake, follow these routes and the waterways beyond from where they leave them, if given. The nodes that are still not reached branch off straight from the nearest point of these routes, see attach_to_tree. Each node keeps exchanging water with the same nodes, which is asserted, and the links to the nodes reached keep their link ids. Nodes are not moved and links are not offset, see ribasim_nl.layout.tidy_layout.

Parameters

Name Type Description Default
model Model Ribasim model required
basin_ids typing.Iterable[int] Basins to rebuild the Junctions of required
waterways gpd.GeoDataFrame waterways with a boolean “main-route” column, and optionally a “cost” column with their cost per metre, see ribasim_nl.network_snapping.snap_to_network required
tolerance float buffer around the basin area within which waterways are used, defaults to 10.0 10.0
water gpd.GeoDataFrame | None water surfaces, over which nodes far from the waterways are routed to them, see ribasim_nl.network_snapping.route_basin_links, defaults to None None
routing_areas typing.Mapping[int, shapely.Geometry] | None per Basin, an area beyond its basin area in which the waterways are used as well, for connector nodes that are connected to it over a long distance, defaults to None None
open_water bool whether the water is open water, such as a lake, over which the waterways are connected as well. The waterways on it, such as fairways, are not digitized in the direction of the flow, so they do not set the direction of links, see towards_basin. True
beyond Waterways | None the waterways and water along which the nodes that are not reached are routed from the routes of the others, such as those of the Top10NL around a lake, defaults to None None

Returns

Name Type Description
tuple[list[int], list[int]] the ids of the links and of the Junctions that are new or changed

rebuild_rws_junction_trees

flow_paths.rebuild_rws_junction_trees(
    model,
    basin_ids,
    top10nl_path,
    fairway_path,
    main_route,
    tolerance=250.0,
    routing_areas=None,
    lake_hydro_objects=(),
)

Rebuild the Junctions of Rijkswaterstaat Basins along the waterways and a main route, see rebuild_junction_trees.

This is used for the hws model and again after coupling, such that its Basins keep their routes. Its connector nodes are often outside the area of their Basin, so waterways up to tolerance around it are used. These are the Top10NL water lines, which cost more per metre than the main route, and nodes far from them are routed over the Top10NL water surfaces. The Basins of open water, see open_water_basins, such as lakes and estuaries, follow the fairways instead, and cross their own water to the nodes on their shores, see ribasim_nl.network_snapping.read_lake_waterways. The nodes beyond their shores follow these routes, and the Top10NL from where they leave them.

Parameters

Name Type Description Default
model Model Ribasim model required
basin_ids typing.Collection[int] Basins to rebuild the Junctions of required
top10nl_path pathlib.Path Top10NL GeoPackage required
fairway_path pathlib.Path GeoPackage with the fairways of OpenStreetMap required
main_route gpd.GeoDataFrame lines of the main route, see ribasim_nl.rws_kunstwerken.main_route required
tolerance float buffer around the basin area within which waterways are used, defaults to 250.0 250.0
routing_areas typing.Mapping[int, shapely.Geometry] | None per Basin, an area beyond its basin area in which the waterways are used as well, see rebuild_junction_trees, defaults to None None
lake_hydro_objects typing.Sequence[pathlib.Path] network.gpkg files of the water authorities around the lakes, whose hydro-objects on the water of a lake are its waterways too, such as the channels of harbours, see read_lines_on_water. Nodes in a harbour then join the lake along its channel, rather than straight from the nearest fairway across the land. ()

Returns

Name Type Description
tuple[list[int], list[int]] the ids of the links and of the Junctions that are new or changed

route_parts

flow_paths.route_parts(model, start_id, path)

The geometries of the links of a path from start_id, each in the direction away from start_id.

rwzi_junctions

flow_paths.rwzi_junctions(model, attached=None)

The RWZI Junctions, see ribasim_nl.rwzi.attach_rwzi_junctions, only those attached to a route or not.

An RWZI Junction that is attached splits a flow link, so it has more flow links than the one from its RWZI and the one to its Basin.

split_distance

flow_paths.split_distance(geometry, point, margin=1.0)

The distance along geometry of the point nearest to point, at least margin from its ends if possible.

towards_basin

flow_paths.towards_basin(inflows, outflows, capacity, digitized_away=None)

Whether a link of a Basin tree points towards its Basin, given the nodes beyond it that flow in and out.

The direction does not change the simulation, only the sign of the flow of the link in the results. A link that only carries inflows points towards the Basin, and one that only carries outflows away from it. A link that carries both follows the main flow: the direction in which the lines it follows are digitized, if they decide, else the side with the larger drainage_capacity, else the side with more nodes, else away from the Basin.

Parameters

Name Type Description Default
digitized_away bool | None whether the lines along the link are digitized away from the Basin, see Digitized.along None

tree_sides

flow_paths.tree_sides(routes, node_ids)

The Basin that all nodes exchange water with through one link each, directly or through its Basin tree.

If the nodes have two such Basins, like structures next to each other between two Basins, it is the one downstream of the node with the lowest id.

Returns

Name Type Description
tuple[int, dict[int, tuple[int, bool]]] | None the Basin, and per node its link towards the Basin and whether that link is upstream of the node, so the Basin flows into the node, or None if there is no such Basin