This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

1D and 2D Flow Routing on a Terrain

Aaron Lowe Duke University Svend C. Svendsen Aarhus University Pankaj K. Agarwal Duke University  and  Lars Arge Aarhus University
(2020)
Abstract.

An important problem in terrain analysis is modeling how water flows across a terrain creating floods by forming channels and filling depressions. In this paper we study a number of flow-query related problems: Given a terrain Σ\Sigma, represented as a triangulated xyxy-monotone surface with nn vertices, a rain distribution RR which may vary over time, determine how much water is flowing over a given edge as a function of time. We develop internal-memory as well as I/O-efficient algorithms for flow queries.

This paper contains four main results:

(i) We present an internal-memory algorithm that preprocesses Σ\Sigma into a linear-size data structure in O(nlogn)O(n\log n) time that for a (possibly time varying) rain distribution RR can return the flow-rate functions of all edges of Σ\Sigma in O(ρk+|ϕ|logn)O(\rho k+|\phi|\log n) time, where ρ\rho is the number of sinks in Σ\Sigma, kk is the number of times the rain distribution changes, and |ϕ||\phi| is the total complexity of the flow-rate functions that have non-zero values; |ϕ|=Θ(n(χ+k))|\phi|=\Theta(n(\chi+k)) in the worst case, where χ\chi is the height of the merge tree of Σ\Sigma and kk is the number of times the rain distribution changes, but |ϕ||\phi| is much smaller in practice.

(ii) We also present an I/O-efficient algorithm for preprocessing Σ\Sigma into a linear-size data structure using O(Sort(n))O(\operatorname{Sort}(n)) I/Os and O(nlogn)O(n\log n) internal computation time, so that for a rain distribution RR, it can compute the flow-rate function of all edges using O(Sort(|ϕ|))O(\text{Sort}(|\phi|)) I/Os and O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time.

(iii) Σ\Sigma can be preprocessed in O(nlogn)O(n\log n) time, into a linear-size data structure so that for a given rain distribution RR, the flow-rate function of an edge (q,r)Σ(q,r)\in\Sigma under the single-flow direction (SFD) model can be computed in O(|R|+|bq|klogn)O(|R|+|b_{q}|k\log n) time, where |R||R| is the number of vertices in RR on which nonzero rain falls, and |bq||b_{q}| is the number of tributaries of qq in which rain directly falls in.

(iv) We present an algorithm for computing the two-dimensional channel along which water flows using Manning’s equation; a widely used empirical equation that relates the flow-rate of water in an open channel to the geometry of the channel along with the height of water in the channel. Given the flow-rates along a path of edges, the algorithm computes the height of water and boundary along the channel in O(|𝒞|log|𝒞|)O(|\mathcal{C}|\log|\mathcal{C}|) time, where |𝒞||\mathcal{C}| is the number of wetted faces at least partially covered by water in the channel.

copyright: acmlicensedjournalyear: 2020copyright: acmcopyrightconference: 28th International Conference on Advances in Geographic Information Systems; November 3–6, 2020; Seattle, WA, USAbooktitle: 28th International Conference on Advances in Geographic Information Systems (SIGSPATIAL ’20), November 3–6, 2020, Seattle, WA, USAprice: 15.00doi: 10.1145/3397536.3422269isbn: 978-1-4503-8019-5/20/11

1. Introduction

An important problem in terrain analysis is modeling how water flows across a terrain and creates floods by forming channels and filling up depressions. The rate at which a depression fills up during a rainfall depends not only on the shape of the depression and the size of its watershed (i.e., the area of the terrain that contributes water to the depression) but also on other depressions that are filling up. Water falling on the watershed of a filled depression flows to a neighboring depression effectively making the watershed of the latter larger and filling it up faster. Modeling how depressions fill and how water spills into other depressions during a flash flood event is therefore an important computational problem.

Besides determining which areas of a terrain become flooded and when they become flooded, determining the 2D channels (rivers) along which water flows across the terrain is also important. The flow queries we ask can also be used to solve related flood-risk queries, and the algorithms developed will provide a simpler and faster algorithm for a previously studied flood-risk queries. We assume we are given a terrain Σ\Sigma, represented as a triangulated xyxy-monotone surface with nn vertices. As in earlier papers, (Rav et al., 2017; Lowe and Agarwal, 2019; Arge et al., 2010) we assume that water flows along the edges of Σ\Sigma. Two models of water flow along edges have been proposed: (i) a simple and more widely used model called the single flow-direction (SFD) model in which water from a vertex flows along one of its downward edges, and (ii) a more accurate but more complex model called the multi-flow-direction (MFD) model in which water at a vertex splits and flows along all of its downward edges. There is also some earlier work by Liu and Snoeyink (Liu and Snoeyink, 2005) which allows water to flow in the interior of edges. We consider both of these models.

We study the following three problems in this paper:

  • Terrain-flow query: given a rain distribution (possibly varying with time), compute as a function of time the flow rate (of water) for all edges of Σ\Sigma.

  • Edge-flow query: given a rain distribution and a query edge ee of Σ\Sigma, compute the flow rate of ee under the single flow-direction (SFD) model.

  • 2D flow network: Given a 1D flow network, represented as a set of edges along with their flow values, compute 2D channels along which water flows.

Finally, as high-resolution terrain data sets are becoming easily available, their size easily exceeds the size of main memory of a standard computer, so movement of data between main memory and external memory (such as disk) becomes the bottleneck in computations.

We use the I/O-model with one disk by Aggarwal and Vitter (Aggarwal and Vitter, 1988), in which, the computer is equipped with a two-level memory hierarchy consisting of an internal memory, which is capable of holding mm data items, and an external memory of unlimited size. All computation happens on data in internal memory. Data is transferred between internal and external memory in blocks of bb consecutive data items. Such a transfer is referred to as an I/O-operation or an I/O. The cost of an algorithm is the number of I/Os it performs. The number of I/Os required to read nn items from disk is Scan(n)=O(n/b)\operatorname{Scan}(n)=O(n/b). The number of I/Os required to sort nn items is Sort(n)=Θ((n/b)logm/b(n/b))\operatorname{Sort}(n)=\Theta\big{(}(n/b)\log_{m/b}(n/b)\big{)} (Aggarwal and Vitter, 1988). For all realistic values of nn, mm, and bb we have Scan(n)<Sort(n)n\operatorname{Scan}(n)<\operatorname{Sort}(n)\ll n.

Related work. Due to its importance, the problem of modeling how water flows on a terrain has been studied extensively, and many approaches have been taken to address this problem. One approach focuses on accurately simulating fluid dynamics using non-linear partial differential equations such as the Navier-Stokes equations. These equations have no closed form solutions and are usually solved using numerical methods. They often account for additional factors, such as the effects of different terrain types and drainage networks. While these models tend to be more accurate, they are computationally expensive and do not scale to large terrains. Bates and De Roo (Bates and De Roo, 2000) developed one such model for simulating flooding on digital elevation models (DEMs) using two flow models for different regions of the terrain: the first handles flow within rivers and the second models flow of water as it spreads over floodplains. While there has been some research into refining the representation of channels, such as Wood et al(Wood et al., 2016), often the channel geometry is assumed to be a simple model (e.g. rectangular or trapezoidal.)

Water-flow modeling on a terrain also has been studied in the GIS community. These approaches use simpler models, which are computationally efficient and suitable for large datasets. Although some early work, e.g. (Liu and Snoeyink, 2005) allowed water to flow in the interior of faces, recent work assumes that water flows along the edges of the terrain and the SFD and MFD models described above are used to model the water flow locally at a vertex, see e.g.  (Rav et al., 2017; Lowe and Agarwal, 2019; Arge et al., 2010) Arge et al. (Arge et al., 2000) described an I/O-efficient algorithm for the flow-accumulation problem in the SFD model, water falls uniformly on the terrain which asks how much water flows over each point in a terrain assuming the terrain has only one sink at infinity. Their algorithm performs a total of O(Sort(n))O(\operatorname{Sort}(n)) I/Os, where Sort(n)\operatorname{Sort}(n) is the number of I/Os required to sort nn items. The flow accumulation model only provides rough solution to flow modeling, since it assumes that either the terrain does not have any local minima or that they have been filled in advance.

In order to accurately model flow it is necessary to compute times at which depressions fill and simulate how water spills from one depression into others. Arge et al. (Arge et al., 2010) proposed the first I/O-efficient algorithm that computes the fill times of all maximal depressions in O(Sort(ρ)log(ρ/m))O(\operatorname{Sort}(\rho)\log(\rho/m)) I/Os, where ρ\rho is the number of depressions in the terrain and mm is the size of the internal memory. If ρ=O(m)\rho=O(m), the algorithm can be simplified and requires only O(Sort(n))O(\operatorname{Sort}(n)) I/Os.

Arge et al. (Arge et al., 2017) described an I/O-efficient algorithm that computes which points on the terrain become flooded if a total volume ψ\psi of rain falls according to a distribution RR. It performs O(Sort(n)+Scan(χρ))O(\operatorname{Sort}(n)+\operatorname{Scan}(\chi\cdot\rho)) I/Os, where χ\chi is the height of the merge tree of the terrain. In the worst case χρ=Ω(n2)\chi\rho=\Omega(n^{2}), but it can be bounded to O(Sort(n))O(\operatorname{Sort}(n)) under certain practical assumptions.

Lowe et al. (Lowe and Agarwal, 2019) presented efficient algorithms for several flood queries on a terrain under the multiflow direction (MFD) model. They presented a O(nlogn)O(n\log n)-time algorithm to answer terrain flood queries. They also showed that a terrain Σ\Sigma can be preprocessed in O(nlogn+nρ)O(n\log n+n\rho)-time into a data structure that can answer point-flood queries: Given a rain distribution RR, a volume of rain ψ\psi, and a point qΣq\in\Sigma, determine whether qq will be flooded. The query time is O(|R|bq+bq2)O(|R|b_{q}+b_{q}^{2}) time, where bqb_{q} is the number of maximal depressions that contain the query point qq; bq=Ω(n)b_{q}=\Omega(n) in the worst case, but in practice it is much smaller. Finally, they presented a O(nbq+bqω)O(nb_{q}+b_{q}^{\omega})-time algorithm to determine when a query point qq gets flooded, where ω\omega is the exponent of fast matrix multiplication. To our knowledge, no I/O-efficient algorithms are known for these flooding queries under the MFD model.

Our results. There are four main results in the paper.

(i) We present a O(nlogn)O(n\log n) time algorithm for preprocessing Σ\Sigma into a linear-size data structure for answering terrain-flow queries: for a rain distribution RR, it can compute the flow rate of all edges in O(ρk+|ϕ|log|ϕ|)O(\rho k+|\phi|\log|\phi|) time, where |ϕ||\phi| is total complexity of nonzero flow-rate functions, ρ\rho is the number of sinks in Σ\Sigma, and kk is the number of times the rain distribution changes. In the worst case |ϕ|=Θ(n(χ+k))|\phi|=\Theta(n(\chi+k)), where χ\chi, as above, is the height of the merge tree of Σ\Sigma, but |ϕ||\phi| is much smaller in practice. An immediate corollary of our result is that a flood-time query (i.e. given RR and a point qΣq\in\Sigma, when will qq be flooded) can be answered in the same time, which is a significant improvement over the result in (Lowe and Agarwal, 2019).

(ii) We present two I/O-efficient algorithms for the terrain flow algorithm. We first preprocess the terrain using O(Sort(n))O(\operatorname{Sort}(n)) I/Os and O(nlogn)O(n\log n) internal computation time. The first algorithm that ρ(χ+k)=O(m)\rho(\chi+k)=O(m), where χ\chi, kk and ρ\rho are as above, and considers a terrain-flow query in O(Sort(|ϕ|))O(\operatorname{Sort}(|\phi|)) I/Os and O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time. The second algorithm assumes ρ=O(m)\rho=O(m) and answers a terrain-flow query in O(Sort((χ+k)nlogn))O(\operatorname{Sort}((\chi+k)n\log n)) I/Os and O((χ+k)nlog2(kn))O((\chi+k)n\log^{2}(kn)) internal computation time. We additionally note that since the terrain-flow query is a more general problem, these algorithms also yield I/O-efficient algorithms for the terrain-flood and flood-time queries under the MFD model studied in (Lowe and Agarwal, 2019).

(iii) The terrain-flow query algorithm naturally can also be used to perform edge-flow queries. Under SFD flow, we can build a linear sized data structure in O(nlogn)O(n\log n) time which given an edge e=(q,r)e=(q,r) and rain distribution RR computes ϕe\phi_{e} in O(|R|+bqklogn)O(|R|+b_{q}k\log n) time, where |R||R| is the complexity of the rain distribution, bqb_{q} is the number of tributaries of qq in which rain is falling, and kk is the number of times the rain distribution changes.

(iv) We present an algorithm that given a 1D flow network, represented as a path of edges along with their flow values, identifies the depth and width of water along the 2D flow network in O(|𝒞|log|𝒞|)O(|\mathcal{C}|\log|\mathcal{C}|) time, where |𝒞||\mathcal{C}| is the number of “wetted” faces at least partially covered by the water in the channel. We do so by utilizing Manning’s equation (Manning et al., 1890), a widely used empirical formula relating flow-rate of water in an open channel to the geometry of the channel. The algorithm utilizes the key idea that while the profile of the channel changes continuously as we sweep along the 1D flow network, the combinatorial structure only changes at discrete events. By tracking when these events occur, the algorithm efficiently computes the depth of the river along the flow network. We note that previous work computing the 2D channel assumes the cross section of each channel has a simple geometry (e.g. rectangular or trapezoidal) (Bates and De Roo, 2000). In contrast, we do not make any such assumption.

We note that our terrain-flow query algorithm builds on some ideas in (Lowe and Agarwal, 2019) namely, it also performed a sweep in the (z)(-z)-direction. However, our new algorithm is more general, simpler, faster, and several new ideas are needed. First note that the terrain-flood and flood-query problems studied in (Lowe and Agarwal, 2019) are special cases of the terrain-flow query problem. In the process of computing the flow rate of all edges, it computes the flood-time of all depressions of Σ\Sigma. In contrast, the algorithms in (Lowe and Agarwal, 2019) either compute which depression got flooded or the flood time of only one depression. Furthermore, computation of the latter required a complicated algorithm that relied on matrix multiplication and ran in O(nω)O(n^{\omega}) time in the worst case, while our algorithm takes O(n2logn)O(n^{2}\log n) time in the worst case. Additionally, our algorithm can handle rain distributions that vary over time, and our algorithm is output-sensitive. Finally, we also present an I/O-efficient algorithm for answering terrain-flow queries.

2. Preliminaries & Models

In this section we give a number of preliminary definitions and describe the flooding model, most of the text here follows closely  (Rav et al., 2017; Lowe and Agarwal, 2019).

2.1. Geometric Preliminaries

Terrains. Let 𝕄\mathbb{M} be a triangulation of 2\mathbb{R}^{2}, and let 𝕍\mathbb{V} be the set of vertices of 𝕄\mathbb{M}; set n=|𝕍|n=|\mathbb{V}|. We assume that 𝕍\mathbb{V} contains a vertex vv_{\infty} at infinity, and that each edge {u,v}\{u,v_{\infty}\} is a ray emanating from uu; the triangles in 𝕄\mathbb{M} incident to vv_{\infty} are unbounded. Let h:𝕄h:\mathbb{M}\to\mathbb{R} be a height function. We assume that the restriction of hh to each triangle of 𝕄\mathbb{M} is a linear map, that hh approaches ++\infty at vv_{\infty}, and that the heights of all vertices are distinct. Given 𝕄\mathbb{M} and hh, the graph of hh, called a terrain and denoted by Σ=(𝕄,h)\Sigma=(\mathbb{M},h), is an xyxy-monotone triangulated surface whose triangulation is induced by 𝕄\mathbb{M}.

Critical vertices. There is a natural cyclic order on the neighbor vertices of a vertex vv of 𝕄\mathbb{M}, and each such vertex is either an upslope or downslope neighbor. If vv has no downslope (resp. upslope) neighbor, then vv is a minimum (resp. maximum). We also refer to a minimum as a sink. If vv has four neighbors w1w_{1}, w2w_{2}, w3w_{3}, w4w_{4} in clockwise order such that max(h(w1),h(w3))<h(v)<min(h(w2),h(w4))\max(h(w_{1}),h(w_{3}))<h(v)<\min(h(w_{2}),h(w_{4})) then vv is a saddle vertex.

Level sets, contours, depressions. Given \ell\in\mathbb{R}, the \ell-sublevel set of hh is the set h<={x2h(x)<}h_{<\ell}=\{x\in\mathbb{R}^{2}\mid h(x)<\ell\}, and the \ell-level set of hh is the set h=={x2h(x)=}h_{=\ell}=\{x\in\mathbb{R}^{2}\mid h(x)=\ell\}. Each connected component of h<h_{<\ell} is called a depression, and each connected component of h=h_{=\ell} is called a contour. Note that a depression is not necessarily simply connected, as a maximum can cause a hole to appear;

For a point x𝕄x\in\mathbb{M}, a depression βx\beta_{x} of h<h_{<\ell} is said to be delimited by the point xx if xx lies on the boundary of β\beta, which implies that h(x)=h(x)=\ell. A depression β1\beta_{1} is maximal if every depression β2β1\beta_{2}\supset\beta_{1} contains strictly more sinks than β1\beta_{1}. A maximal depression that contains exactly one sink is called an elementary depression. Note that each maximal depression is delimited by a saddle, and a saddle that delimits more than one maximal depression is called a negative saddle. For a maximal depression β\beta, let Sd(β)\text{Sd}(\beta) denote the saddle delimiting β\beta, and let Sk(β)\text{Sk}(\beta) denote the set of sinks in β\beta. The volume of a depression β\beta of h<h_{<\ell} is

(1) Vol(β)=β(h(v))𝑑v.\text{Vol}(\beta)=\int_{\beta}(\ell-h(v))dv.
Refer to caption
(a)
(b)
Figure 1. An example terrain with saddle vertices v1v_{1}-v3v_{3}. Each saddle viv_{i} delimits two maximal depressions αi\alpha_{i} and βi\beta_{i}. (1) Terrain seen from above. Sinks are marked with a square and saddles are marked with a cross. (1) Terrain seen from the side,

Merge tree. The maximal depressions of a terrain form a hierarchy that is easily represented using a rooted tree, called the merge tree (Kreveld et al., 1997; Carr et al., 2003) and denoted by 𝖳\mathsf{T}. Suppose we sweep a horizontal plane from -\infty to \infty. As we vary \ell, the depressions in h<h_{<\ell} vary continuously, but their structure changes only at sinks and negative saddles. If we increase \ell, then a new depression appears at a sink, and two depressions merge at a negative saddle. The merge tree is a tree that tracks these changes. Its leaves are the sinks of the terrain, and its internal nodes are the negative saddles. The edges of 𝖳\mathsf{T}, the merge tree, are in one-to-one correspondence with the maximal depressions of Σ\Sigma, that is, we associate each edge (u,v)(u,v) with the maximal depression delimited by uu and containing vv. See Figure 1 for an example. We assume that 𝖳\mathsf{T} has an edge from the root of 𝖳\mathsf{T} extending to ++\infty, corresponding to the depression that extends to \infty. For simplicity, we assume that 𝖳\mathsf{T} is binary, that is, each negative saddle delimits exactly two depressions. Non-simple saddles can be unfolded into a number of simple saddles (Edelsbrunner et al., 2001).

𝖳\mathsf{T} can be computed in O(nlogn)O(n\log n) time (Carr et al., 2003), and it can be preprocessed in O(n)O(n) additional time so that for a point x2x\in\mathbb{R}^{2}, Vol(βx)\text{Vol}(\beta_{x}), the volume of the depression delimited by xx can be computed in O(logn)O(\log n) time (Carr et al., 2003). In the I/O model, 𝖳\mathsf{T} can be constructed using O(Sort(n))O(\operatorname{Sort}(n)) I/Os and as shown in (Arge and Revsbæk, 2009), the volume Vol(βx)\text{Vol}(\beta_{x}) and the smallest maximal depression containing xx can be computed for all vertices xx using O(Sort(n))O(\operatorname{Sort}(n)) I/Os.

2.2. Flooding Model

We now describe the flooding model, which is the same as in (Lowe and Agarwal, 2019), and define flow-rate functions.

Flow graph and flow functions. We transform 𝕄\mathbb{M} into a directed acyclic graph \mathcal{M}, referred to as the flow graph, by directing each edge {u,v}\{u,v\} of 𝕄\mathbb{M} from uu to vv if h(u)>h(v)h(u)>h(v), and from vv to uu otherwise, i.e., each edge is directed in the downward direction. For each (directed) edge (u,v)(u,v), we define the local flow λ(u,v,t)\lambda(u,v,t) to be the portion of water arriving at uu that flows along the edge (u,v)(u,v) to vv at time tt. By definition for any u𝕍u\in\mathbb{V}, (u,v)𝕄λ(u,v,t)=1\sum_{(u,v)\in\mathbb{M}}\lambda(u,v,t)=1

The value of λ(u,v,t)\lambda(u,v,t) is, in general, based on relative heights of the downslope neighbors of uu. If uu is not a negative saddle vertex, then λ(u,v,t)\lambda(u,v,t) remains the same for all tt, so we will often drop tt and write λ(u,v)\lambda(u,v) to denote λ(u,v,t)\lambda(u,v,t). If uu is a negative saddle, then λ(u,v)\lambda(u,v) changes when one of its depressions fills up as no water flows for uu to that saddle; see below for further discussion.

Rain distribution. Let R(v,t):𝕍×0R(v,t):\mathbb{V}\times\mathbb{R}\to\mathbb{R}_{\geq 0} denote a rain distribution, that is, for each vertex v𝕍v\in\mathbb{V}, R(v,t)R(v,t) indicates the rate at which the rain is falling on vv at time tt. We assume that for a each vv, R(v,)R(v,\cdot) is piecewise-constant function of time, with the function changing at discrete time values {t0=0,t1,,tk}\left\{t_{0}=0,t_{1},\dots,t_{k}\right\}, and for all vv and ttkt\geq t_{k}, R(v,t)=0R(v,t)=0. For a depression β\beta, we define R(β,t)=vβR(v,t)R(\beta,t)=\sum_{v\in\beta}R(v,t). For iki\leq k, let |Ri||R_{i}| denote the number of vertices for which R(v,ti)0R(v,t_{i})\geq 0, and let |R|=i=1k|Ri||R|=\sum_{i=1}^{k}|R_{i}| . In practice |Ri|n|R_{i}|\ll n.

Fill and spill rates. For a maximal depression β\beta, we define the fill rate Fβ:00F_{\beta}:\mathbb{R}_{\geq 0}\to\mathbb{R}_{\geq 0} as the rate at which water is arriving in the depression β\beta as a function of time. That is, the rate at which rain is falling directly in β\beta plus the rate at which other depressions are spilling water into β\beta. The fill rate FβF_{\beta} is a monotonically nondecreasing piecewise-constant function. Similarly, we define the spill rate Sβ:0[0,1]S_{\beta}:\mathbb{R}_{\geq 0}\to[0,1] as the rate (as a function of time) at which water spills from β\beta through the saddle that delimits β\beta.

Flow rate. Next we define flow rates ϕe\phi_{e} and ϕv\phi_{v} for edges ee and vertices vv of 𝕄\mathbb{M}, which is the amount of water flowing through ee and vv, respectively, at time tt. For an edge (u,v)𝕄(u,v)\in\mathbb{M}, ϕ(u,v)(t)\phi_{(u,v)}(t) is the fraction of water from uu that passes along (u,v)(u,v) as a function of time. That is,

(2) ϕ(u,v)(t)\displaystyle\phi_{(u,v)}(t) =λ(u,v,t)ϕv(t).\displaystyle=\lambda(u,v,t)\phi_{v}(t).

The flow-rate ϕv\phi_{v} of a non-saddle vertex vv is the sum of the flow-rates along incoming edges to vv plus the rain on the vertex vv. That is,

(3) ϕv(t)=R(v,t)+(u,v)𝕄ϕ(u,v)(t).\phi_{v}(t)=R(v,t)+\sum_{(u,v)\in\mathbb{M}}\phi_{(u,v)}(t).\\

Letting τv\tau_{v} be the time at which a vertex vv becomes flooded, ϕv(t)\phi_{v}(t) and ϕ(u,v)(t)\phi_{(u,v)}(t) for any v𝕄v\in\mathbb{M} are undefined for tτvt\geq\tau_{v}. That is to say, when a vertex is flooded, the flow-rate function is undefined.

Let vv be a negative saddle delimiting depressions α\alpha and β\beta. Until one of α\alpha or β\beta is filled, ϕv\phi_{v} is defined using equation 3. Without loss of generality assume that depression α\alpha fills first, say at time τα\tau_{\alpha}, and water starts spilling from α\alpha to β\beta through vv. The spill rate SαS_{\alpha} specifies the rate at which water spills from α\alpha to β\beta. It is tempting to simply add SαS_{\alpha} to equation 3, but it double counts the amount of water that was flowing from vv to depression α\alpha. For t<ταt<\tau_{\alpha}, ϕv\phi_{v} is defined as in (3). For tταt\geq\tau_{\alpha}, ϕv\phi_{v} is defined as follows,

(4) ϕv(t)=(R(v,t)+(u,v)𝕄ϕ(u,v)(t))wβλ(v,w,0)+Sα(t).\phi_{v}(t)=\biggl{(}R(v,t)+\sum_{(u,v)\in\mathbb{M}}\phi_{(u,v)}(t)\biggr{)}\sum_{w\in\beta}\lambda(v,w,0)+S_{\alpha}(t).

Finally for tταt\geq\tau_{\alpha} and for any wβw\in\beta,

(5) λ(v,w,t)=λ(v,w,0)zβλ(v,z,0).\lambda(v,w,t)=\frac{\lambda(v,w,0)}{\sum_{z\in\beta}\lambda(v,z,0)}.

3. Terrain-flow Query

In this section we describe an internal-memory algorithm that, given a terrain Σ\Sigma and rain distribution RR, computes the flow-rate functions ϕ(u,v)(t)\phi_{(u,v)}(t) for all edges (u,v)𝕄(u,v)\in\mathbb{M}.

Each function is piecewise constant and represented as a sequence of pairs (ti,Δi)(t_{i},\Delta_{i}), where Δi\Delta_{i} represents the change in the function value at time tit_{i}. We denote |ϕ(u,v)||\phi_{(u,v)}| to be the combinatorial complexity of ϕu,v(t)\phi_{u,v}(t), i.e., the number of pieces defining ϕ(u,v)\phi_{(u,v)}. Set |ϕ|=(u,v)𝔼|ϕu,v||\phi|=\sum_{(u,v)\in\mathbb{E}}|\phi_{u,v}|. If the rain distribution changes at O(1)O(1) times, then |ϕ|=O(nχ)|\phi|=O(n\chi), where χ\chi is the height of the merge tree.

The preprocessing step builds, in O(nlogn)O(n\log n) time, the merge tree 𝖳\mathsf{T} of Σ\Sigma, and labels each node in 𝖳\mathsf{T} according to its in-order traversal as described in (Lowe and Agarwal, 2019). Furthermore, it computes Vol(βv)\text{Vol}(\beta_{v}) for each vertex vΣv\in\Sigma and augments each edge (u,v)Σ(u,v)\in\Sigma with the index of the smallest maximal depression containing vv.

We first present a simple algorithm and then describe how to adapt it to make it output-sensitive.

3.1. A simple algorithm

We first give an overview of the algorithm. We sweep through the vertices of 𝖳\mathsf{T} in descending height order, maintaining the following values for each height \ell:

  1. (1)

    the set of depressions αi\alpha_{i} in the sublevel set h<h_{<\ell};

  2. (2)

    for each αi\alpha_{i}, maintain the fill rate Fαi(t)F_{\alpha_{i}}(t), and

  3. (3)

    for each αi\alpha_{i} and the set of edges (u,v)𝕄(u,v)\in\mathbb{M} with h(v)<h(u)h(v)<\ell\leq h(u) and vαiv\in\alpha_{i}, denoted by \mathboldE(αi)\mathbold{E}(\alpha_{i}), maintain the flow-rate function ϕ(u,v)(t)\phi_{(u,v)}(t).

Note that FαiF_{\alpha_{i}} depends only on the sinks contained in αi\alpha_{i}, so we compute fill-rate functions of only maximal depressions. At each non-saddle vertex vv we compute ϕv\phi_{v} and ϕ(v,w)\phi_{(v,w)}, for each edge (v,w)𝕄(v,w)\in\mathbb{M}, in a straightforward manner using (2) and (3). If vv is a negative saddle delimiting two depressions α\alpha and β\beta, we must account for any rain spilling from one depression to the other. We do so by first partitioning the edges into two sets, \mathboldE(α)\mathbold{E}(\alpha) and \mathboldE(β)\mathbold{E}(\beta). Then given the flow-rates on the edges in each set, we compute the fill-rates FαF_{\alpha} and FβF_{\beta}. Given these fill-rates, we then determine which depression (if any) fills first. If at least one depression becomes full, assume that β\beta fills first without loss of generality. Then we can compute the spill-rate function Sβ(t)S_{\beta}(t). Finally, we compute ϕ(v,w)\phi_{(v,w)} for each edge (v,w)𝕄(v,w)\in\mathbb{M}.

We will now describe the algorithm in detail. We begin by augmenting 𝖳\mathsf{T} with additional information which will be used when computing the fill-rates.

Given a rain distribution RR, before performing the downward sweep, we first compute R(β,)R(\beta,\cdot) for each maximal depression β\beta as follows: first assign R(v,)R(v,\cdot) for each vertex with nonzero rainfall to the smallest maximal depression containing vv, and then perform an upward sweep on the merge-tree 𝖳\mathsf{T}, maintaining the sum of rainfall functions at each saddle vertex.

As we sweep top-down, for each depression αi\alpha_{i} we store the flow-rates of the edges in \mathboldE(αi)\mathbold{E}(\alpha_{i}) in a data structure that supports the following three operations: (i) add ϕ(u,v)(t)\phi_{(u,v)}(t) to the data structure; (ii) remove ϕ(u,v)(t)\phi_{(u,v)}(t) from the data structure; (iii) and given a saddle vertex delimiting two depressions β,γαi\beta,\gamma\in\alpha_{i}, partition the flow-rates into two sets \mathboldE(β)\mathbold{E}(\beta) and \mathboldE(γ)\mathbold{E}(\gamma) and return the sum of flow-rates in each set.

Noting there are O(χ+k)O(\chi+k) values of tt at which the flow-rate functions can change (corresponding to the times at which RR changes and spill-times of tributaries of the depression in question) we build the data structure as follows. For each time tit_{i} at which the functions can change, maintain the values Δi\Delta_{i} for the flow-rate of each edge (u,v)(u,v) sorted according to the label (v)\ell(v). Additionally maintain the prefix sum of the values for each tit_{i}. There will be O(|ϕ(u,v)|)O(|\phi_{(u,v)}|) values to consider at each edge, so we can insert and remove flow-rates of edges in O(|ϕ(u,v)|logn)O(|\phi_{(u,v)}|\log n)-time.

To partition the edges into \mathboldE(β)\mathbold{E}(\beta) and \mathboldE(γ)\mathbold{E}(\gamma), recall each vertex is labeled according to an in-order traversal of 𝖳\mathsf{T}. Given two depressions β\beta and γ\gamma delimited by a saddle vertex vv with label (v)\ell(v), all vertices contained in β\beta will have a label less than (v)\ell(v), and all vertices contained in γ\gamma will have a label greater than (v)\ell(v). Thus, to partition the edges into the depressions they cross into we simply take the prefix of all edges with label less than (v)\ell(v) to be one set, and the remaining suffix to be the other set. Then given the partition, we can use the prefix sums to determine the sum of flow-rate functions into β\beta and γ\gamma in O(χ+k)O(\chi+k) time.

With this data structure at hand, we now describe how to process each vertex vv as we encounter it in our sweep.

Non-negative-saddle vertex. If vv is a non-negative-saddle vertex computing ϕv\phi_{v} is easy using (3). Note that ϕ(u,v)\phi_{(u,v)} for all incoming edges to vv has been computed. Additionally, using Fαi(t)F_{\alpha_{i}}(t), for the maximal depression αi\alpha_{i} containing vv, along with Vol(βv)\text{Vol}(\beta_{v}), we can determine if or when vv becomes flooded.

Negative saddles. If vv is a negative saddle delimiting two depressions α\alpha and β\beta, we first determine whether either of α\alpha or β\beta becomes full and, if so, which fills first. The fill rate of a depression α\alpha is the sum of the rain falling directly on vertices in α\alpha plus the flow-rates across all edges crossing into α\alpha, that is

(6) Fα(t)=uαR(u,t)+(v,w)\mathboldE(α)ϕ(v,w)(t).F_{\alpha}(t)=\sum_{u\in\alpha}R(u,t)+\sum_{(v,w)\in\mathbold{E}(\alpha)}\phi_{(v,w)}(t).

The first sum has already been computed in the upward sweep of the merge tree. However, to compute ϕ(v,w)(t)\phi_{(v,w)}(t) for all values of tt we would need to know which depression fills first and when. So instead, we define the pseudo-flow-rate function, ϕ(v,w)\phi^{\prime}_{(v,w)}, in the same manner that flow-rate for non-saddle vertices is computed as in (3). Using this, we compute a modified pseudo-fill-rate function

(7) Fα(t)=uαR(u,t)+(v,w)\mathboldE(α)ϕ(v,w)(t).F^{\prime}_{\alpha}(t)=\sum_{u\in\alpha}R(u,t)+\sum_{(v,w)\in\mathbold{E}(\alpha)}\phi^{\prime}_{(v,w)}(t).

Before α\alpha or β\beta become full (i.e. t<min(τα,τβ)t<\min(\tau_{\alpha},\tau_{\beta})) we have that ϕ(v,w)(t)=ϕ(v,w)(t)\phi^{\prime}_{(v,w)}(t)=\phi_{(v,w)}(t), which in turn implies that Fα(t)=Fα(t)F^{\prime}_{\alpha}(t)=F_{\alpha}(t) (resp. Fβ(t)=Fβ(t)F^{\prime}_{\beta}(t)=F_{\beta}(t)) before α\alpha or β\beta becomes full. Given this, compute ϕ(v,w)(t)\phi^{\prime}_{(v,w)}(t) for all edges from the saddle vv and add these flow-rates to the data structure. We then use the data structure to partition the edges and compute the pseudo-fill-rates of the two depressions, from which we can determine which (if any) depression fills first.

If neither α\alpha nor β\beta becomes full, we are done. If not, assume without loss of generality that α\alpha becomes full first and spills into β\beta. Then given Fα(t)F_{\alpha}(t) we compute Sα(t)S_{\alpha}(t) and in turn compute ϕv(t)\phi_{v}(t) for ταt\tau_{\alpha}\leq t using (4) along with Fβ(t)F_{\beta}(t) using (6).

Computing R(β,)R(\beta,\cdot) for all maximal depressions can be done in O(|R|+ρk)O(|R|+\rho k)-time where ρ\rho is the number of sinks in the terrain and kk is the number of times the rain distribution changes. To compute the flow-rate functions of edges from all non-negative saddle vertices, along with the pseudo-fill-rate functions from negative saddles, we spend O(|ϕ|logn)O(|\phi|\log n)-time computing the flow-rate functions and adding them to the data structure. At a negative saddle delimiting two depressions α,β\alpha,\beta, if we partition the edges by walking from the front and back of the sorted list of edges we spend O(min(|α|,|β|)O(\min(|\alpha|,|\beta|)-time, where |α||\alpha| is the number of vertices contained in α\alpha. A simple recurrence shows that the total time spent partitioning edges at all negative saddles is O(nlogn)O(n\log n). Finally computing the fill and spill rates at a negative saddle takes time proportional to their complexity, which is O(k+χ)O(k+\chi). Noting that |ϕ|=O(n(χ+k))|\phi|=O(n(\chi+k)), we get the following.

Theorem 3.1.

Given a triangulation 𝕄\mathbb{M} of 2\mathbb{R}^{2} with nn vertices and a height function h:𝕄h:\mathbb{M}\to\mathbb{R} that is linear on each face of 𝕄\mathbb{M}, a data structure of size O(n)O(n) can be constructed in O(nlogn)O(n\log n) time so that for a (time varying) rain distribution RR, a terrain-flow query can be answered in O(ρk+n(χ+k)logn)O(\rho k+n(\chi+k)\log n) time, where ρ\rho is the number of sinks in 𝕄\mathbb{M}, kk is the number of times at which the rain distribution changes.

In particular, if k=O(n)k=O(n), we have that the terrain-flow query can be answered in O(n2logn)O(n^{2}\log n) time.

3.2. An Output-Sensitive Algorithm

We modify the above algorithm so that its running time becomes O(ρk+|ϕ|logn)O(\rho k+|\phi|\log n) where |ϕ||\phi| is the total complexity of all non-zero flow-rate functions. If water flows across a small portion of Σ\Sigma then |ϕ|n|\phi|\ll n and the new algorithm will be faster. The key idea is we do not have to process vertices with zero flow-rate.

The algorithm begins in the same manner as the previous algorithm, computing R(β,)R(\beta,\cdot) for each maximal depression β\beta.

However rather than sweeping over all vertices of 𝖳\mathsf{T}, we instead maintain a priority queue of vertices with nonzero flow-rate with their heights as their priority, i.e., the larger the height, the higher the priority. Every vertex vv with nonzero flow-rate will either have rain falling directly on it, or have a path in \mathcal{M} from a source vertex (i.e. either a vertex with rain falling directly on it, or a saddle from which water is spilling.) We initialize the priority queue with all vertices on which rain falls directly along with all negative saddles.

While the priority queue is not empty, we pop the highest priority vertex vv from the queue and compute the flow-rate function ϕv(t)\phi_{v}(t), as we have described in the previous algorithm. For each vertex ww adjacent to vv in \mathcal{M} with ϕ(v,w)>0\phi_{(v,w)}>0, add ww to the priority queue. For each edge, in addition to each value Δi\Delta_{i} representing the change in the flow-rate function at time tit_{i}, we also store ViV_{i}, the integral of the flow-rate function from t=0t=0 to t=tit=t_{i}. These are also stored in sorted order, and we maintain the prefix sum of these values for each edge.

At negative saddles, to determine which depression fills first, when we partition the edges we also compute the sum of volumes at the last time step tjt_{j} for the depressions α\alpha and β\beta delimited by the saddle vertex. If both have filled by this time, consider the sum at tj1t_{j-1} and so on until we find the latest time at which only one depression is filled. This will be the depression which fills first, and ensures the number of time steps considered will be O(|Sβ|)O(|S_{\beta}|). Then we compute the fill and spill-rates as we did in the prior algorithm.

Since we compute the flow and spill-rates as before, it suffices to prove that we consider each negative saddle with positive spill-rate and each vertex with positive flow-rate. We begin with all negative saddles in the priority queue, so the former holds trivially. For the latter, given a vertex vv with positive flow-rate, either rain falls directly on it, in which case we added vv when initializing the priority queue, or water flows to it from some higher vertex uu. If uu is added to the priority queue, when it is processed vv will in turn be added and later processed. Since all non-source vertices with positive flow-rate have a path from source vertex, and all source vertices begin in the priority queue, we have that we will reach and process vv.

The time spent processing non-saddle vertices vv and edges (v,w)(v,w) will be O(|ϕv(t)|)O(|\phi_{v}(t)|) and O(|ϕ(v,w)(t)|)O(|\phi_{(v,w)}(t)|) respectively. So the total time processing these will be O(|ϕ|)O(|\phi|). At negative saddle vertices when we partition the edges, if we perform the search by walking from the front and back of the list we will spend a total of O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) time partitioning edges at all negative saddles. Finally, at each negative saddle vv which is a source we spend O(|Sβ|)O(|S_{\beta}|)-time computing which depression fills first. Since |Sβ||ϕv||S_{\beta}|\leq|\phi_{v}|, the total time processing saddle vertices is O(ρk+|ϕv(t)|logn)O(\rho k+|\phi_{v}(t)|\log n).

Theorem 3.2.

Given a triangulation 𝕄\mathbb{M} of 2\mathbb{R}^{2} with nn vertices and a height function h:𝕄h:\mathbb{M}\to\mathbb{R} that is linear on each face of 𝕄\mathbb{M}, a data structure of size O(n)O(n) can be constructed in O(nlogn)O(n\log n) time so that for a (time varying) rain distribution RR, a terrain-flow query can be answered in O(ρk+|ϕ|logn)O(\rho k+|\phi|\log n) time, where ρ\rho is the number of sinks in 𝕄\mathbb{M}, kk is the number of times at which the rain distribution changes, and |ϕ||\phi| is the total complexity of all non-zero flow-rate functions.

4. I/O-Efficient Algorithms

In this section we describe two I/O-efficient algorithms that given a terrain Σ\Sigma and a rain distribution RR determine the flow-rate ϕ(u,v)(t)\phi_{(u,v)}(t) for all edges (u,v)Σ(u,v)\in\Sigma. The algorithms use the same framework as described in Section 3. However, we cannot explicitly maintain the list of edges crossed by the sweep line in memory due to the bounded internal memory size. We instead store the merge tree in memory and rely on the time-forward processing technique, where the flow-rates computed at vertices are forwarded to downslope neighbors using an I/O-efficient priority queue (Chiang et al., 1995). The I/O-efficient priority queue supports nn insertions and deletions in O(Sort(n))O(\operatorname{Sort}(n)) I/Os and O(nlogn)O(n\log n) internal computation time (Sanders, 2001).

In the preprocessing step of both algorithms, we compute the merge tree 𝖳\mathsf{T} of Σ\Sigma and label each node in 𝖳\mathsf{T} according to its in-order traversal as described in (Lowe and Agarwal, 2019). Furthermore, we compute Vol(βv)\text{Vol}(\beta_{v}) for each vertex vΣv\in\Sigma and augment each edge (u,v)Σ(u,v)\in\Sigma with the index of the smallest maximal depression containing vv. This can be computed in O(Sort(n))O(\operatorname{Sort}(n)) I/Os using the algorithm described by Arge et al. (Arge and Revsbæk, 2009). Since both algorithms assume ρ=O(m)\rho=O(m), we assume the 𝖳\mathsf{T} is stored in internal memory.

4.1. Extending the Internal Memory Algorithm

We now describe our first I/O-efficient algorithm. In order to extend the internal terrain-flow query algorithm, we introduce the following notation: let \mathboldE^(α)\mathboldE(α)\mathbold{\hat{E}}(\alpha)\subseteq\mathbold{E}(\alpha) be the set of edges such that, for each edge (u,v)\mathboldE^(α)(u,v)\in\mathbold{\hat{E}}(\alpha), α\alpha is the smallest maximal depression containing vv.

As in the previous section, we proceed by performing an upward sweep on the terrain followed by a downward sweep.

Upward sweep. For the upward sweep of our algorithm, we compute R(α,)R(\alpha,\cdot) for each maximal depression α\alpha. Given a rain distribution RR, we start by computing the sum of rain falling directly in each maximal depression α\alpha. We do this by assigning R(v,)R(v,\cdot) for each vertex with nonzero rainfall to the smallest maximal depression containing vv. This step can be implemented I/O-efficiently using a sort and a scan of the terrain and RR; for each maximal depression α\alpha store R(α,)R(\alpha,\cdot) in memory. We then perform the upward sweep described in Section 3 in memory, maintaining the sum of rainfall functions at each saddle vertex. This upward sweep can be trivially implemented in O(Sort(|R|+n))O(\operatorname{Sort}(|R|+n)) I/Os and O(|R|log|R|+nlogn+ρk)O(|R|\log|R|+n\log n+\rho k) internal computation time.

Downward sweep. We sweep through vertices of Σ\Sigma in descending height order, maintaining the following values in memory for each height \ell:

  1. (1)

    for each depression αi\alpha_{i} in the sublevel set h<h_{<\ell}, maintain the fill-rate Fαi(t)F_{\alpha_{i}}(t), and

  2. (2)

    for each αi\alpha_{i} maintain the depression flow-rate sum
    (u,v)\mathboldE^(α)ϕ(u,v)(t)\sum_{(u,v)\in\mathbold{\hat{E}}(\alpha)}\phi_{(u,v)}(t)

We cannot explicitly store the flow-rates for the set of edges crossing the sweep line in memory. Instead, we store each flow-rate ϕu,v(t)\phi_{u,v}(t) in an I/O-efficient priority queue QQ keyed on the height of vertex vv. Furthermore, we initialize QQ by inserting the functions R(v,t)R(v,t) for each vertex vv with R(v)>0R(v)>0. Whenever we process a vertex vv, for all (v,u)Σ(v,u)\in\Sigma we propagate the flow-rate ϕ(v,u)\phi_{(v,u)} forward to uu using QQ such that the function can be removed from QQ whenever uu is processed. The values maintained for each depression aia_{i} in sublevel set h<h_{<\ell} can be maintained in memory since we assume ρ(χ+k)=O(m)\rho(\chi+k)=O(m).

We now describe how each step of the sweep is performed. Let vv be the current vertex. First we remove the function R(v,t)R(v,t) from QQ. Furthermore, for each edge (u,v)Σ(u,v)\in\Sigma, we remove the flow-rate ϕ(u,v)(t)\phi_{(u,v)}(t) from QQ. Since the algorithms visits vertices in descending order of height, the functions must have been inserted into QQ at an earlier point and will be at the front of QQ. Since each edge (u,v)(u,v) no longer crosses the sweep line, we update the depression flow-rate sums by subtracting ϕ(u,v)(t)\phi_{(u,v)}(t) from the flow-rate sum of the smallest maximal depression containing vv.

Let αi\alpha_{i} be the smallest maximal depression containing vv. If vv is a non-negative-saddle vertex, we compute ϕv\phi_{v} and determine if or when vv becomes flooded, using Fαi(t)F_{\alpha_{i}}(t) and Vol(βv)\text{Vol}(\beta_{v}) as described in Section 3.

If vv is a negative saddle delimiting two depressions α\alpha and β\beta, we wish to compute whether α\alpha or β\beta becomes full and, if so, which fills first. In order to compute the flood times of the two depressions, we recall that the fill-rate of a depression α\alpha is equal to the sum of flow-rates across all the edges (u,w)\mathboldE(α)(u,w)\in\mathbold{E}(\alpha) plus the amount of rain falling in α\alpha. When we are processing the vertex vv we have that all edges crossing into α\alpha and β\beta respectively are also crossing the sweep line. We therefore compute the fill-rate of α\alpha (β\beta resp.) by summing the depression flow-rate sums for all maximal depressions αiα\alpha_{i}\subseteq\alpha (βiβ\beta_{i}\subseteq\beta resp.):

(8) Fα(t)\displaystyle F_{\alpha}(t) =R(α,t)+αiα(u,v)\mathboldE^(αi)ϕ(u,v)(t).\displaystyle=R(\alpha,t)+\sum_{\alpha_{i}\subseteq\alpha}\sum_{(u,v)\in\mathbold{\hat{E}}(\alpha_{i})}\phi_{(u,v)}(t).

The flood times and the flow-rate ϕv(t)\phi_{v}(t) is then computed from Fα(t)F_{\alpha}(t) and Fβ(t)F_{\beta}(t) as described in Section 3.

Finally, we can propagate the flow-rates on the outgoing edges by pushing ϕ(v,w)(t)=w(v,w)ϕv(t)\phi_{(v,w)}(t)=w_{(v,w)}\cdot\phi_{v}(t) to QQ for all vertices ww where w(v,w)>0w_{(v,w)}>0. Furthermore, we update the depression flow-rate sums with the flow-rates of the outgoing edges. Note that each flow-rate is added to only one flow-rate sum.

The flow-rate ϕ(v,w)(t)\phi_{(v,w)}(t) of each edge (v,w)(v,w) is forwarded only once using the priority queue. Thus, we spend a total of O(Sort(|ϕ|))O(\operatorname{Sort}(|\phi|)) I/Os and O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time processing all vertices in the sweep excluding computation of the fill-rates at negative saddles. In order to efficiently compute the fill-rates in internal memory, we store the depression flow-rate sums in a sorted list ordered by the in-order traversal index of the depressions. We then partition the sums as in (Lowe and Agarwal, 2019) and compute the fill-rates using O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time in total. Since the initial sorting of vertices and edges can be performed in O(Sort(n))O(\operatorname{Sort}(n)) I/Os and O(nlogn)O(n\log n) internal computation time, the algorithm uses a total of O(Sort(|ϕ|))O(\operatorname{Sort}(|\phi|)) I/Os and O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time.

Theorem 4.1.

Given a triangulation of 𝕄\mathbb{M} with nn vertices, a height function h:𝕄h:\mathbb{M}\to\mathbb{R} which is linear on each face of 𝕄\mathbb{M} and a rain distribution RR, a terrain-flow query can be answered in O(Sort(|ϕ|))O(\operatorname{Sort}(|\phi|)) I/Os and O(|ϕ|log|ϕ|)O(|\phi|\log|\phi|) internal computation time assuming ρ(χ+k)=O(m)\rho(\chi+k)=O(m), where |ϕ||\phi| is the total complexity of all flow-rate functions which we return, χ\chi is the height of the merge tree, kk is the number of times at which the rain distribution changes, ρ\rho is the number of sinks in 𝕄\mathbb{M}, and mm is the size of internal memory.

4.2. Assuming Smaller Internal Memory

We now extend the algorithm to relax the assumption on the size of the internal memory from ρ(χ+k)=O(m)\rho(\chi+k)=O(m) to ρ=O(m)\rho=O(m), at the cost of a greater number of I/Os.

We use the same framework as described previously, however, we avoid storing the depression flow-rate sums and fill-rates in memory for each depression in the sublevel set h<h_{<\ell}; We instead the priority queue QQ to forward fill-rates as well as the edge flow-rates used to compute fill-rates at negative saddle vertices.

Forwarding fill-rates. Let vv be non-negative-saddle vertex and let αi\alpha_{i} be the smallest maximal depression containing vv. Let uu be the vertex visited after vv in the downward sweep, where the smallest maximal depression containing uu is also αi\alpha_{i}. When performing the sweep, we forward Fαi(t)F_{\alpha_{i}}(t) from vv to uu using QQ. We note that we can augment vv with the height of uu using Sort(n)\operatorname{Sort}(n) I/Os in preprocessing, and thus we can forward Fαi(t)F_{\alpha_{i}}(t) to uu during the sweep. Furthermore, for each negative saddle vertex vv delimiting depressions α\alpha and β\beta, we forward Fα(t)F_{\alpha}(t) and Fβ(t)F_{\beta}(t) to the first vertices visited in α\alpha and β\beta, respectively.

Computing fill-rates at negative saddles. Let vv be a negative saddle vertex delimiting two depressions α\alpha and β\beta. During execution of the sweep, we wish to compute the fill-rates of depressions α\alpha and β\beta. We recall that the fill-rate of α\alpha can be computed as follows:

(9) Fα(t)\displaystyle F_{\alpha}(t) =R(v,t)+(u,v)E(α)ϕ(u,v)(t).\displaystyle=R(v,t)+\sum_{(u,v)\in E(\alpha)}\phi_{(u,v)}(t).

We note that the flow-rates required to compute this sum and R(v,t)R(v,t) can be propagated using QQ. However, that would in the worst-case lead to forwarding O(nχ)O(n\chi) functions in total. We recall that Fβv(t)F_{\beta_{v}}(t) is forwarded to vv using QQ. Furthermore, since Fβv(t)=Fα(t)+Fβ(t)F_{\beta_{v}}(t)=F_{\alpha}(t)+F_{\beta}(t), it suffices to compute either Fα(t)F_{\alpha}(t) or Fβ(t)F_{\beta}(t), whichever requires the fewest flow-rates to be forwarded. The number of flow-rate functions that need to be forwarded in order to compute Fα(t)F_{\alpha}(t) and Fβ(t)F_{\beta}(t), respectively, can be precomputed by counting the number of edges crossing the boundaries of α\alpha and β\beta. This precomputation step can trivially be implemented by performing a scan of the vertices using O(Scan(n))O(\operatorname{Scan}(n)) I/Os and O(ρ)O(\rho) memory. We therefore preprocess for which depressions we compute fill-rates and forward only the flow-rates required for computing those.

We now bound the number of edges forwarded using a similar recurrence as (Lowe and Agarwal, 2019); Let |α||\alpha| denote the total number of edges with at least one vertex contained in the depression α\alpha and note that |α||\mathboldE(α)||\alpha|\geq|\mathbold{E}(\alpha)|. Letting T(βv)T(\beta_{v}) be the total number of flow-rates summed to compute the fill-rates for all saddles contained in βv\beta_{v}, we have that

(10) T(βv)=O(min(|α|,|β|))+T(α)+T(β).\displaystyle T(\beta_{v})=O\big{(}\min(|\alpha|,|\beta|)\big{)}+T(\alpha)+T(\beta).

Noting that the set of edges crossing into the two maximal depressions α\alpha and β\beta are disjoint, it follows that |a|+|β||βv||a|+|\beta|\leq|\beta_{v}|. Using this, the recurrence solves to T(βv)=O(|βv|log|βv|)T(\beta_{v})=O(|\beta_{v}|\log|\beta_{v}|), and we thus need to forward only a total of O(nlogn)O(n\log n) flow-rate functions. Since the complexity of each flow-rate function is bounded by O(χ+k)O(\chi+k), we spend a total of O(Sort((χ+k)nlogn))O(\operatorname{Sort}((\chi+k)n\log n)) I/Os and O((χ+k)nlog2(kn))O((\chi+k)n\log^{2}(kn)) internal computation forwarding edges and computing fill-rates.

Theorem 4.2.

Given a triangulation of 𝕄\mathbb{M} with nn vertices, a height function h:𝕄h:\mathbb{M}\to\mathbb{R} which is linear on each face of 𝕄\mathbb{M} and a rain distribution RR, a terrain-flow query can be answered in O(Sort((χ+k)nlogn))O(\operatorname{Sort}((\chi+k)n\log n)) I/Os and O((χ+k)nlog2(kn))O((\chi+k)n\log^{2}(kn)) internal computation time assuming ρ=O(m)\rho=O(m), where χ\chi is the height of the merge tree, kk is the number of times at which the rain distribution changes, ρ\rho is the number of sinks in 𝕄\mathbb{M}, and mm is the size of internal memory.

5. Edge-Flow Query

The terrain-flow query can naturally be used to answer edge-flow queries by returning ϕe(t)\phi_{e}(t) for a query edge e=(q,r)𝕄e=(q,r)\in\mathbb{M}. While in practice the query time can be improved, the worst case running time under the MFD model remains the same as the terrain-flow query. Under the SFD model, we can improve this running time significantly, building on the fast algorithm for the flood-time query under SFD given by Rav et al(Rav et al., 2017), along with a linear-size data structure supporting constant time reachability queries in planar directed graphs given by Holm et al(Holm et al., 2015).

The key idea of the algorithm is that under the SFD model when water falls on a vertex or spills from a negative saddle the water flows along a single path to some sink in the terrain. Thus if we can find which vertices and negative saddles from which water follows a path containing (u,v)(u,v), ϕ(u,v)\phi_{(u,v)} will be the sum of water falling directly on or spilling from these sources. Before describing the algorithm, we begin with some definitions.

Let uu be a negative saddle, let (u,v1)(u,v_{1}) and (u,v2)(u,v_{2}) be two down edges in 𝖳\mathsf{T} from uu, and let (w,u)(w,u) be the up edge from uu. We call the depression associated with (u,v2)(u,v_{2}) (resp. with (w,u)(w,u)) as the sibling (resp. parent) (depression) of that associated with (u,v1)(u,v_{1}). Any given point q𝕄q\in\mathbb{M} is contained in a sequence of maximal depressions α1αkq\alpha_{1}\supset\dots\supset\alpha_{k}\ni q. Each αi\alpha_{i} is delimited by a saddle viv_{i} and has a corresponding sibling depression βi\beta_{i}. Note that these saddles form a path in 𝖳\mathsf{T} from qq to the root. We refer to the maximal depressions β1,,βk1\beta_{1},\dots,\beta_{k-1} as the tributaries of qq and denote them by bqb_{q}.

For any point q𝕄q\in\mathbb{M} we define the tributary tree 𝖦q\mathsf{G}_{q} as follows. 𝖦q\mathsf{G}_{q} is a directed graph with nodes corresponding to the tributaries of qq plus βq\beta_{q}. There is an edge (βi,βj)(\beta_{i},\beta_{j}) in 𝖦q\mathsf{G}_{q} if water spills from the saddle viv_{i} to a sink in βj\beta_{j} when βi\beta_{i} becomes full. Water spills to exactly one sink under the SFD model, so 𝖦q\mathsf{G}_{q} will be a tree rooted at βq\beta_{q}.

We present an O(nlogn)O(n\log n)-time algorithm for preprocessing Σ\Sigma into a linear-size data structure that can answer an edge-flow query for a given rain distribution R(t)R(t) and query edge (q,r)(q,r). The query takes O(|R|+bqklogn)O(|R|+b_{q}k\log n), where bqb_{q} is the number of qq-tributaries β\beta with R(β,t)>0R(\beta,t)>0.

Given a query edge (q,r)𝕄(q,r)\in\mathbb{M} and a rain distribution R(t)R(t), we begin by assuming that water flows from qq to rr. Since water only flows from each vertex to one neighbor in the SFD model, if this were not the case then we would immediately have that ϕ(q,r)=0\phi_{(q,r)}=0. Hence, ϕ(q,r)=ϕq\phi_{(q,r)}=\phi_{q}. For simplicity, we will instead compute the equivalent point-flow query.

The algorithm begins by building 𝖦q\mathsf{G}_{q}. See Figure 2 for an example. As we have noted, water will reach qq in one of two cases: rain falls on a vertex vv of the terrain and follows a path which crosses qq, or water spills from a tributary of qq and reaches qq.

Consider first the case when rain falls only on a single point pp contained in some tributary βi1\beta_{i_{1}}. Take the path π1\pi_{1} in 𝖦q\mathsf{G}_{q} from βi1\beta_{i_{1}} to the root βq\beta_{q}, (βi1,βi2,,βik,βq).(\beta_{i_{1}},\beta_{i_{2}},\cdots,\beta_{i_{k}},\beta_{q}). For each depression βij\beta_{i_{j}} let ViV_{i} denote the depression volume of βij\beta_{i_{j}} and let τj\tau_{j} be the fill-time of βij\beta_{i_{j}}. The fill-time τk\tau_{k}, when βik\beta{i_{k}} begins spilling into βq\beta_{q}, will be when the volume of rain falling on pp equals Vi\sum V_{i}. Moreover we have

Fβq(t)=Sβik(t)={0t<τkFβi1(t)tτk.F_{\beta_{q}}(t)=S_{\beta_{i_{k}}}(t)=\begin{cases}0&t<\tau_{k}\\ F_{\beta_{i_{1}}}(t)&t\geq\tau_{k}.\\ \end{cases}

That is to say, instead of computing the fill and spill rates for each tributary along the path, we can merge all the tributaries in this path and treat it as if it were a single depression. Then to answer the query, check whether there is a path from the saddle delimiting βik\beta_{i_{k}} to the query vertex qq. If there is, we have ϕq(t)=Sβik(t)\phi_{q}(t)=S_{\beta_{i_{k}}}(t), otherwise ϕq(t)=0\phi_{q}(t)=0.

Now consider the general case where rain falls on many vertices. We will compute ϕq\phi_{q} as a sum of the rainfall functions on vertices that directly reach qq, along with the spill-rates of parents of βq\beta_{q} in 𝖦q\mathsf{G}_{q} that reach qq. We begin by computing the initial fill-rate of each tributary in which rain falls directly. For each vertex vv from which rain flows to a sink contained in βq\beta_{q}, check whether the path the water takes crosses qq. If so add their rainfall to the sum. If rain falls in multiple tributaries that have disjoint paths in 𝖦q\mathsf{G}_{q} to βq\beta_{q} (excluding the root βq\beta_{q}), we can simply perform the single-point rain algorithm multiple times for each path and add each spill-rates from depressions which reach qq to the sum. However it might be the case that two such paths intersect at a tributary γ\gamma before reaching βu\beta_{u} (e.g. π2\pi_{2} and π3\pi_{3} in Figure 2.) Here, we can compute the spill-rates of the two parent tributaries of γ\gamma as we did in the single-point rain algorithm, and then recurse, treating γ\gamma as a single-point source of rain with fill-rate equal to the sum of spill-rates of its parent tributaries. Cases where more than two paths merge at a single vertex can be handled in a similar manner.

Now it remains to show how we can perform this algorithm efficiently. There are two main operations needed. First, we must compute the fill and spill-rates of the tributaries of qq. Then we must determine from which saddles and vertices water will reach qq.

To perform the first operation efficiently, build the linear-size fast data structure for flood-time queries as described in (Rav et al., 2017). With this data structure we can compute the spill-rates of parent tributaries of βq\beta_{q} in O(|R|+bqklogn)O(|R|+b_{q}k\log n) time, where bqb_{q} as defined above, is the numbe of qq-tributaries in which rain is falling directly.

Refer to caption
Figure 2. The tributary tree 𝖦q\mathsf{G}_{q} rooted at βq\beta_{q} with each vertex denoting a tributary of qq, rain initially falls in the tributaries marked as squares.

To perform the second operation efficiently, we consider the subgraph of the flow graph \mathcal{M} taking only edges for which λ(u,v,0)>0\lambda(u,v,0)>0, i.e., when vv is the lowest neighbor of uu. Then on this directed planar graph, build the linear-size data structure reachability queries as described in (Holm et al., 2015). This data structure supports constant-time reachability queries. For a non-saddle vertex vv, water falling at vv reaches the query vertex qq if qq is reachable from vv. For water spilling from a negative-saddle vv towards a node uu we instead check whether qq is reachable from this vertex uu. Omitting all the details, we obtain the following:

Theorem 5.1.

Given a triangulation 𝕄\mathbb{M} with nn vertices and a height function h:𝕄h:\mathbb{M}\to\mathbb{R} which is linear on each face of 𝕄\mathbb{M}, a data structure of size O(n)O(n) can be constructed in O(nlogn)O(n\log n) time so that for a (time varying) rain distribution R(t)R(t) and an edge (q,r)(q,r) an edge-flow query can be answered in O(|R|+bqklogn)O(|R|+b_{q}k\log n), where |R||R| is the complexity of the rain distribution, bqb_{q} is the number of tributaries in which rain is falling directly, and kk is the number of times at which the rain distribution changes.

6. Extracting 2D Flow Networks

So far we have assumed that water flows along the edges of Σ\Sigma, and computed the flow rate of water along these edges. But in reality the water is not constrained to edges and flows along 2D channels forming a 2D network of rivers. In this section we first describe a model for determining the 2D channels given a 1D flow network. We then present an efficient algorithm for computing these channels for a given path along the edges of 𝕄\mathbb{M}.

6.1. Model for 2D Channels

We assume that we are given a path \mathbb{P} along the edges of 𝕄\mathbb{M}. For each edge ee of \mathbb{P}, let ϕe0\phi_{e}\in\mathbb{R}_{\geq 0} be the flow value along ee. Unlike previous sections, we assume that ϕe\phi_{e} does not vary with time. But ϕe\phi_{e} may vary with edges. The goal is to compute a 2D channel 𝒞:=𝒞(,ϕ)\mathcal{C}:=\mathcal{C}(\mathbb{P},\phi) along which water flows. 111One method of generating \mathbb{P} is computing the flow rate along all edges of 𝕄\mathbb{M} as in Sections 3 and 4, fixing a time t=ct=c, choosing a threshold ψ\psi, extracting the edges ee with ϕe(c)ψ\phi_{e}(c)\geq\psi, post-processing these edges to construct a 1D flow network with some desired properties, and finally decomposing this flow network into a set of paths. These steps are beyond the scope of this paper and an interesting direction for future research. The channel 𝒞\mathcal{C} is defined by its left and right banks and the height of water at every point on \mathbb{P}. More precisely, \mathbb{P} is parameterized as :I2\mathbb{P}:I\to\mathbb{R}^{2} where I=[x0,x1]I=[x_{0},x_{1}] is an interval. For every xIx\in I, we define lb(x),rb(x)2\mathrm{lb}(x),\mathrm{rb}(x)\in\mathbb{R}^{2} as the left and right bank, respectively, of 𝒞\mathcal{C} at xx, and Δ(x)=h(lb(x))=h(rb(x)).\Delta(x)=h(\mathrm{lb}(x))=h(\mathrm{rb}(x)). The locus of points lb(x)\mathrm{lb}(x) (resp. rb(x)\mathrm{rb}(x)), for xIx\in I, traces a curve lb\mathrm{lb} (resp. rb\mathrm{rb}), which is the left (resp. right) bank of 𝒞\mathcal{C}.

222Here we assume that lb\mathrm{lb} and rb\mathrm{rb} are simple curves; if either of them is self-intersecting, then 𝒞\mathcal{C} has to be defined more carefully.

We overlay 𝕄\mathbb{M} with lb\mathrm{lb} and rb\mathrm{rb}. 𝒞\mathcal{C} is the portion of this overlay between lb\mathrm{lb} and rb\mathrm{rb}; see Figure 3. The complexity of 𝒞\mathcal{C}, denoted by |𝒞||\mathcal{C}|, is the number of vertices in 𝒞\mathcal{C}.

To estimate lb(x)\mathrm{lb}(x) and rb(x)\mathrm{rb}(x) , we use Manning’s equation (Manning et al., 1890), a widely used empirical formula relating the channel geometry and flow rate as follows.

Let xx be a point on an edge e𝕄e\in\mathbb{M} with flow value ϕe\phi_{e}. Let x\ell_{x} be the line in the xyxy-plane passing through xx and normal to ee, and let Π=x×\Pi=\ell_{x}\times\mathbb{R} be the vertical plane containing \ell. Let Σx=ΣΠ\Sigma_{x}=\Sigma\cap\Pi be the cross-section of Σ\Sigma in Π\Pi, which we refer to as the profile of Σ\Sigma at xx. Σx\Sigma_{x} is a polygonal chain whose vertices (resp. edges) are the intersection points of edges (resp. faces) of Σ\Sigma with Π\Pi. See Figure 3. Let x^=(x,h(x)\hat{x}=(x,h(x) be the vertex on Σx\Sigma_{x} corresponding to the point xex\in e, i.e. x^\hat{x} is vertically above xx.

We divide Σx\Sigma_{x} into two polygonal rays LxL_{x}, RxR_{x} at the vertex x^\hat{x}, with LxL_{x} (resp. RxR_{x}) lying to the left (resp. right) of \mathbb{P}. For a height zh(x)z\geq h(x), let λ(z)\lambda(z) (resp. ρ(z)\rho(z)) be the first point on LxL_{x} (resp. RxR_{x}) at height zz as we walk along LxL_{x} (resp. RxR_{x}.) Let Ax(z)A_{x}(z) denote the area of the polygon formed by the segment λx(z)ρx(z)\lambda_{x}(z)\rho_{x}(z) and the portion of Σx\Sigma_{x} between λx(z)\lambda_{x}(z) and ρx(z)\rho_{x}(z), and let Px(z)P_{x}(z) denote the arc length of Σx\Sigma_{x} between λx(z)\lambda_{x}(z) and ρx(z)\rho_{x}(z). If the water has height zz at xx, then Manning’s equation (Manning et al., 1890) states that the flow rate at xx is

(11) ϕx(z)=Ax(z)5/3σe1/2μePx(z)2/3,\phi_{x}(z)=\frac{A_{x}(z)^{5/3}\sigma_{e}^{1/2}}{\mu_{e}P_{x}(z)^{2/3}},

where σe\sigma_{e} is the slope in the zz-direction of the edge of Σ\Sigma corresponding to ee, and μe\mu_{e} is Manning’s roughness coefficient. We assume that we are given the value of μe\mu_{e}, which depends on the material of the terrain at ee (e.g. concrete, dirt, light brush, etc.) Manning’s equation is typically used to compute the flow rate ϕx(z)\phi_{x}(z) of rivers given a measurement of the river depth and approximate channel geometry. Here instead we state an inverse problem: given the flow rate ϕx\phi_{x} at xx, determine the depth and width of the river at xx. Let Δ(x)\Delta(x) be the value of zz for which ϕx(z)=ϕe\phi_{x}(z)=\phi_{e}. We set lb(x)=λ(Δ(x))\mathrm{lb}(x)=\lambda(\Delta(x)) and rb(x)=ρ(Δ(x))\mathrm{rb}(x)=\rho(\Delta(x)), i.e., the river bank points on Σ\Sigma corresponding to xx; Let 𝒞x=Σ[lb(x),rb(x)]\mathcal{C}_{x}=\Sigma[\mathrm{lb}(x),\mathrm{rb}(x)] be the profile of the channel at xx. See Figure 3.

We first describe how we compute Δ(x)\Delta(x) for a fixed xx and then describe how to track lb\mathrm{lb} and rb\mathrm{rb} as we vary xx. For simplicity, we make the following two assumptions:

  • (A1)

    𝒞x\mathcal{C}_{x} is unimodal for all xIx\in I.

  • (A2)

    The point x^\hat{x} is the unique minimum of 𝒞x\mathcal{C}_{x}.

We discuss in Section 6.4 how to relax these assumptions.

6.2. Computing Δ(x)\Delta(x)

Recall that we assume 𝒞x\mathcal{C}_{x} to be unimodal with x^\hat{x} as its unique minimum. Without loss of generality, assume that the edge ee containing xx is parallel to the xx-axis, so Π\Pi is parallel to the yzyz-plane. We raise the value of zz starting from h(x)h(x) and stopping at the height of vertices of Σx\Sigma_{x} until we find a vertex v^=(v,h(v))\hat{v}=(v,h(v)) such that ϕx(h(v))ϕe\phi_{x}(h(v))\geq\phi_{e}. We now know the edges of LxL_{x} and RxR_{x} that contain lb(x)\mathrm{lb}(x) and rb(x)\mathrm{rb}(x). We then compute the points themselves.

We now describe the procedure in more detail. Let f1,x,f2,x,f_{1,x},f_{2,x},\cdots be the sequence of edges of RxR_{x}, ordered from left to right, and let ei1,x,ei,xe_{i-1,x},e_{i,x} be the endpoints of fi,xf_{i,x}; e0,x=xe_{0,x}=x. Recall that each edge fi,xf_{i,x} is the intersection of a face fif_{i} of Σ\Sigma with the vertical plane Π\Pi, and each endpoint ej,xe_{j,x} is ejΠe_{j}\cap\Pi for some edge eje_{j} of Σ\Sigma. For each edge fi,xf_{i,x}, let fi,xf^{\uparrow}_{i,x} be the semi-infinite trapezoid formed by the edge fi,xf_{i,x} and the vertical rays in the (+z)(+z)-direction from the endpoints ei1,x,ei,xe_{i-1,x},e_{i,x} of fi,xf_{i,x}. For a value z0z_{0}\in\mathbb{R}, we define the trapezoid fi,x(z0)f^{\uparrow}_{i,x}(z_{0}) to be the intersection of fi,xf^{\uparrow}_{i,x} with the halfspace zz0z\leq z_{0}; fi,x(z0)f^{\uparrow}_{i,x}(z_{0}) may be empty, or it may be a triangle. We define Ai,x(z)A_{i,x}(z) to be the area of fi,x(z)f^{\uparrow}_{i,x}(z) and Pi,x(z)P_{i,x}(z) to be the length of the bottom edge of fi,x(z)f^{\uparrow}_{i,x}(z), which is a portion of the edge fi,xf_{i,x}. Let ei,x=(x,ai(x),bi(x))e_{i,x}=(x,a_{i}(x),b_{i}(x)) denote the coordinates of ei,xe_{i,x} as a function of xx, and set wi(x)=ai(x)ai1(x)w_{i}(x)=a_{i}(x)-a_{i-1}(x) and hi(x)=bi(x)bi1(x)h_{i}(x)=b_{i}(x)-b_{i-1}(x). Then Ai,xA_{i,x} can be written as

(12) Ai,x(z)={0z<bi1(x),(zbi1)2wi(x)2hi(x)bi1(x) ¡ z ¡ bi(x),wi(x)(12hi(x)+(zbi(x))bi(x)<z.A_{i,x}(z)=\begin{cases}0&\text{$z<b_{i-1}(x)$},\\ \frac{(z-b_{i-1})^{2}w_{i}(x)}{2h_{i}(x)}&\text{$b_{i-1}(x)$ < z < $b_{i}(x)$},\\ w_{i}(x)(\frac{1}{2}h_{i}(x)+(z-b_{i}(x))&\text{$b_{i}(x)<z$}.\end{cases}

Similarly Pi,xP_{i,x} can be expressed as

(13) Pi,x(z)={0z<bi1(x),fi(x)(zbi1)hi(x)bi1(x)<z<bi(x),fi(x)bi(x)<z.P_{i,x}(z)=\begin{cases}0&\text{$z<b_{i-1}(x)$},\\ \|f_{i}(x)\|\frac{(z-b_{i-1})}{h_{i}(x)}&\text{$b_{i-1}(x)<z<b_{i}(x)$},\\ \|f_{i}(x)\|&\text{$b_{i}(x)<z$}.\end{cases}

We note that Pi,xP_{i,x} (resp. Ai,xA_{i,x}) is a piecewise-linear (resp. piecewise-quadratic) function of zz for a fixed xx. We can define Fj,x(z),Pj,x(z)F_{j,x}(z),P_{j,x}(z) and Aj,x(z)A_{j,x}(z) for the edges fj,xf_{j,x} of LxL_{x} as well. We can now express PxP_{x} and AxA_{x} as:

(14) Px(z)=iPi,x(z)andAx(z)=iAi,x(z),P_{x}(z)=\sum_{i}P_{i,x}(z)\quad\text{and}\quad A_{x}(z)=\sum_{i}A_{i,x}(z),

where the summation is taken over all edges of Σx\Sigma_{x} that contain a point of height at most zz. PxP_{x} and AxA_{x} are also piecewise-linear and piecewise-quadratic functions respectively.

Let z0<z1<z2<z_{0}<z_{1}<z_{2}<\cdots be the heights of vertices of Σx\Sigma_{x}. Px(z0)=Ax(z0)=0P_{x}(z_{0})=A_{x}(z_{0})=0. Assuming Pi,x(zi1),Ai,x(zi1)P_{i,x}(z_{i-1}),A_{i,x}(z_{i-1}) have been computed then Pi,x(zi)P_{i,x}(z_{i}), Ai,x(zi)A_{i,x}(z_{i}) can be computed in O(1)O(1) time using (12)–(14).

Let zkz_{k} be the first value for which ϕx(zk)ϕe\phi_{x}(z_{k})\geq\phi_{e}. Since Pi,x(z)P_{i,x}(z) (resp. Ai,x(z)A_{i,x}(z)) is a linear (resp. quadratic) function for z(zk1,zk)z\in(z_{k-1},z_{k}), the value of Δ(x)(zk1,zk]\Delta(x)\in(z_{k-1},z_{k}] can be computed in O(1)O(1) time by plugging these functional forms in (11). Let fL,xf_{L,x} (resp. fR,xf_{R,x}) be the edge of LxL_{x} (resp. RxR_{x}) at which the vertical sweep stopped. Then lb(x)\mathrm{lb}(x) (resp. rb(x)\mathrm{rb}(x)) is the point on fL,xf_{L,x} (resp. fR,xf_{R,x}) of height Δ(x)\Delta(x) and can be computed in O(1)O(1) time. The total time spent by the procedure is O(|𝒞x|)O(|\mathcal{C}_{x}|). Hence, we obtain the following.

Lemma 6.1.

For a given point xx\in\mathbb{P}, Δ(x)\Delta(x), lb(x)\mathrm{lb}(x) and rb(x)\mathrm{rb}(x) can be computed in O(|𝒞x|)O(|\mathcal{C}_{x}|) time.

6.3. Unimodal Channel Algorithm

We now describe an algorithm for compute the channel 𝒞\mathcal{C} assuming (A1) and (A2) hold. Recall that for any xIx\in I, the vertices of 𝒞x\mathcal{C}_{x} are intersection points of the edges with the plane Πx\Pi_{x}. Let Γx=γ1,,γu\Gamma_{x}=\left<\gamma_{1},\cdots,\gamma_{u}\right> denote the sequence of these edges, which implicitly define 𝒞x\mathcal{C}_{x}. We refer to Γx\Gamma_{x} as the combinatorial structure of 𝒞x\mathcal{C}_{x}. We compute 𝒞\mathcal{C} by varying xx continuously from x0x_{0} to x1x_{1} and maintaining 𝒞x\mathcal{C}_{x}. As xx varies, 𝒞x\mathcal{C}_{x} changes continuously, i.e., each vertex of 𝒞x\mathcal{C}_{x} traces a curve, but the combinatorial structure Γx\Gamma_{x} changes only at discrete values of xx, called the events. The algorithm works by sweeping the line x\ell_{x} along \mathbb{P}, stopping at events as we traverse \mathbb{P}. As long as xx lies on the same edge of \mathbb{P}, x\ell_{x} simply translates. At vertices of \mathbb{P}, where the sweep line x\ell_{x} shifts from one edge to the next one in \mathbb{P}, the algorithm continues by rotating x\ell_{x} to make it normal to the next edge. We first describe how we sweep along an edge of \mathbb{P} and then describe how the sweep line rotates at a vertex of \mathbb{P}.

Edges. Fix an edge ee\in\mathbb{P}. Without loss of generality, assume that ee is parameterized as e:[0,1]2e:[0,1]\to\mathbb{R}^{2}.

Refer to caption
Figure 3. Top: Σ\Sigma with the edge e5e_{5} marked in purple and line \ell. The intersection of \ell with edges of Σ\Sigma marked with boxes. Bottom: Σx\Sigma_{x}, with water at height zz. The polygon 𝒫\mathcal{P} is marked in blue, and the wetted perimeter marked in red. The vertices correspond to the intersection point above in the top figure.

As we sweep along ee and vary xx, the left and right banks lb,rb\mathrm{lb},\mathrm{rb} trace curves that lie inside fixed faces of Σ\Sigma and the remaining vertices of 𝒞x\mathcal{C}_{x} trace the corresponding edges of Σ\Sigma. The algorithm encounters the following two types of events at which Γx\Gamma_{x} changes:

  1. (1)

    the sweep line reaches an endpoint of an edge (u,v)(u^{\prime},v^{\prime}) of Γx\Gamma_{x} which is a vertex of Σ\Sigma, or

  2. (2)

    lb\mathrm{lb} or rb\mathrm{rb} intersects an edge ee^{\prime} (bounding the face containing it) of Σ\Sigma.

The first event results in one or more edges in 𝒞x\mathcal{C}_{x} shrinking to the point vv^{\prime}, and one or more new edges starting at vv^{\prime}. The second event results in either the addition and/or removal of an extremal edge in 𝒞x\mathcal{C}_{x} (and thus insertion/deletion of an edge in Γx\Gamma_{x}) depending on whether the height of the channel is increasing or decreasing.

The first type of events are easy to detect, as they correspond to the vertices of Σ\Sigma. The second type are more challenging, and we detect them as follows. By maintaining functions representing the area and wetted perimeter of 𝒞x\mathcal{C}_{x} as a bivariate function of xx and zz (using (12-14)) and using Manning’s equation we can express Δ(x)\Delta(x) as a function of xx. We then compute when Δ(x)\Delta(x) reaches the top or bottom boundary of the face containing lb\mathrm{lb} (resp. rb\mathrm{rb}). These time instances correspond to the second type of event.

Process the events in order, by popping the first event from the priority queue QQ. If it is the first type of event corresponding to a vertex vv of Σ\Sigma, we remove from Γ\Gamma the edges that end at vv and insert into Γ\Gamma the edges that start at vv. We add the other endpoints of the new edges to QQ as new first type of events. We also remove from A(x,z)A(x,z) and P(x,z)P(x,z) the terms corresponding to the old edges and add terms corresponding to the new edges. If an edge of the face of Σ\Sigma containing lb\mathrm{lb} or rb\mathrm{rb} changes, we also update the second type of event in QQ.

If the event corresponds to lb\mathrm{lb} or rb\mathrm{rb} reaching an edge of the terrain, either add the new face the it crosses into and/or remove the face it crosses out of. We update Γ\Gamma as well as QQ. We also A(z)A(z) and P(z)P(z) accordingly.

A(x,z)A(x,z) and P(x,z)P(x,z) can be maintained using a height balanced tree so that its functional form can be updated in O(logn)O(\log n) time per insertion/deletion of a term in A(x,z)A(x,z) and P(x,z)P(x,z).

We continue this process until we reach the event corresponding to x=1x=1, when the endpoint of ee is reached. Let |𝒞e||\mathcal{C}_{e}| be the number of total faces contained in the channel from 𝒞(0)\mathcal{C}(0) to 𝒞(1)\mathcal{C}(1). Between any two events, lb(x)\mathrm{lb}(x) and rb(x)\mathrm{rb}(x) intersect a face only a constant number of times. Therefore the total number of events is O(|𝒞e|)O(|\mathcal{C}_{e}|), giving a total running time to sweep an edge of O(|𝒞e|log|𝒞e|)O(|\mathcal{C}_{e}|\log|\mathcal{C}_{e}|).

Vertex. When transitioning from one edge to another along the path, we must join the two channels. We will assume the there are no sharp bends between two edges of \mathbb{P}, specifically the angle between the sweep line along the first and second channel is less than 90 degrees, Taking the two channels 𝒞(u,v)\mathcal{C}_{(u,v)} and 𝒞(v,w)\mathcal{C}(v,w) we see that on one side of vv the two channels will intersect, while on the other they will be disjoint. Let 0\ell_{0} (resp. 1\ell_{1}) be the sweep line perpendicular to (u,v)(u,v) (resp. (v,w)(v,w)) at vv, and p0p_{0} (resp. p1p_{1}) be the intersection point between 0\ell_{0} with the riverbank of 𝒞(v,w)\mathcal{C}_{(v,w)} (resp. 1\ell_{1} with the riverbank of 𝒞(u,v)\mathcal{C}_{(u,v)}.) Then let 0\ell_{0}^{\prime} (resp. 1\ell_{1}^{\prime}) be the line parallel to 0\ell_{0} at p1p_{1} (resp. 1\ell_{1} at p0p_{0}), and vv^{\prime} be the intersection point between 0\ell_{0}^{\prime} and 1\ell_{1}^{\prime}. Now rotating about vv^{\prime} from 0\ell_{0}^{\prime} to 1\ell_{1}^{\prime} will sweep the area where the two channels intersect. See Figure 4.

In a similar manner in which we swept along each edge, let 𝒞v,θ\mathcal{C}_{v^{\prime},\theta} be the profile of vv^{\prime} with a line \ell at angle θ\theta. As we rotate the sweep line, we assume the water is flowing at the intersection of \ell with either (u,v)(u,v) or (v,w)(v,w). We will similarly maintain the area function A(θ,z)A(\theta,z) (resp. the wetted perimeter function P(θ,z)P(\theta,z)) as a sum of functions on each face in the channel. Additionally when computing Manning’s equation, as we rotate we will linearly interpolate between slope values as well as the roughness coefficients for the two edges. The main difference is now for each face fif_{i}\in\mathcal{F}, hi(θ,z)h_{i}(\theta,z) and wi(θ,z)w_{i}(\theta,z) are not linear functions in θ\theta as it was when we swept along an edge. However, we can still computed analytically the events corresponding to the riverbank(s) crossing edges of faces.

Refer to caption
Figure 4. The river profile is taken as the union of the profile of the two edges and that as we rotate around the vertex from 0\ell_{0} to 1\ell_{1}.

Letting |𝒞v||\mathcal{C}_{v}| denote the total number of faces in the channel obtained by sweeping at vv. Then the total time spent is O|𝒞v|log|𝒞v|)O|\mathcal{C}_{v}|\log|\mathcal{C}_{v}|)

Putting everything together we obtain the following:

Theorem 6.1.

Given a triangulation 𝕄\mathbb{M} with nn vertices, a height function h:𝕄h:\mathbb{M}\to\mathbb{R} which is linear on each face of 𝕄\mathbb{M} a path \mathbb{P} in 𝕄\mathbb{M}, if the channel 𝒞x\mathcal{C}_{x} is unimodal for all xx along the path, we can compute the 2D flow network in time O(|𝒞|log|𝒞|)O(|\mathcal{C}|\log|\mathcal{C}|) where |𝒞||\mathcal{C}| is the total number of faces in the channel obtained by sweeping along the edges and vertices of \mathbb{P}.

6.4. General Channels

We will now show how to modify the algorithm when assumptions (A1) and (A2) do not hold. When (A2) does not hold, i.e. x^\hat{x} is not a local minima, the modification is straightforward. Simply walk down the edges of Γx\Gamma_{x} until finding a local minima xx^{\prime} and then run the algorithm using that vertex as the division point for the polygonal rays LxL_{x^{\prime}} and RxR_{x^{\prime}}. It may be the case that the edge containing xx^{\prime} ends and a new minima x′′x^{\prime\prime} begins, but this can be handled like any other event in the algorithm. The only difference comes in the interpretation and runtime analysis. When (A1) does not hold, it may be the case that the water level in the 2D channel does reach an edge along which water was flowing according to the 1D flow network. However this is reasonable behavior, as the 1D flow network assumes water only flows along edges of the terrain whereas in reality water will also flow along the faces. For the runtime analysis, we now include the edges searched along from xx to xx^{\prime} to be included in the channel 𝒞e\mathcal{C}_{e}.

When (A1) does not hold, i.e. the channel is not unimodal, when lb\mathrm{lb} or rb\mathrm{rb} intersect an edge of Σ\Sigma bounding the face containing it, that edge may correspond to a local maximum in the profile. In this case, water flows over the ridge into a secondary channel. See Figure 5333Here |𝒞x||\mathcal{C}_{x}| contains all faces between the leftmost and rightmost river banks, i.e., all edges marked red.. We must now account for the decrease of the flow-rate in the primary channel as well as determining the height of water in the secondary channel.

Refer to caption
Figure 5. A channel profile Σx\Sigma_{x} with local maxima yy. If the flow in the primary channel on the right causes the channel height to exceed h(y)h(y), excess spills to the secondary channel on the left.

Updating flow rates. Let fif_{i} be the first face containing a local maximum encountered when sweeping along an edge maintaining Γx\Gamma_{x}, and let eie_{i} be the edge of Σ\Sigma corresponding to the maximum. Using Manning’s equation, we compute the flow rate corresponding to when this unimodal channel becomes full ϕfi,x=ϕx(h(ei,x)\phi_{f_{i,x}}=\phi_{x}(h(e_{i,x}). If ϕe>ϕfi,x\phi_{e}>\phi_{f_{i,x}} a spill will occur, and the excess flow is sent to the local minimum in the secondary channel. To account for the water flowing out of the channel, consider the flow rate along ee as a function of xx, ϕe(x)\phi_{e}(x). Letting ee^{\prime} be the minimum edge in the secondary channel, we set ϕe(x)=ϕeϕfi(x)\phi_{e^{\prime}}(x)=\phi_{e}-\phi_{f_{i}}(x). As we continue the sweep ϕe(x)\phi_{e}(x) will be a non-increasing function, we set ϕe(x)=min(ϕe,ϕfi,x,minx<xϕe(x))\phi_{e}(x)=\min(\phi_{e},\phi_{f_{i,x}},\min_{x^{\prime}<x}\phi_{e}(x^{\prime})).

Secondary channel. When an overflow occurs we continue adding faces to the profile until finding the next local minima corresponding to edge ee^{\prime}. Then in this secondary channel if the excess flow exceeds our threshold for the 1D channel, i.e. ϕe(x)ψ\phi_{e^{\prime}}(x)\geq\psi, we repeat the unimodal channel algorithm in this channel using ϕe(x)\phi_{e^{\prime}}(x). It may be the case that ϕe(x)\phi_{e^{\prime}}(x) is enough to fill the secondary channel above the height of a ridge. If it is the same ridge that spilled into the secondary channel we treat it as a single channel. If it is a ridge further out, we repeat the process in the tertiary channel.

7. Conclusion

In this paper we presented algorithms for a number of flow routing problems: First, we developed a fast internal-memory algorithm for the terrain-flow query problem which given a rain distribution, computes the flow rate ϕe\phi_{e} for all edges eΣe\in\Sigma. We also showed how the algorithm could be made I/O-efficient. Next, we presented a faster algorithm if one is interested in computing the flow rate of only one edge, after some preprocessing. Finally, given a flow path along the edges of Σ\Sigma, we proposed an algorithm to determine the 2D channel along which water flows; our algorithm does not make any assumption about the geometry of the channel.

We conclude by mentioning a few directions for future work. The model of extracting 2D channels leaves a number of open questions. For instance, if the 1D flow network is a forest then channels along different paths will interact which leaves the question of how we merge these channels to construct a 2D river network.

While we consider the flow rate as a function of time, it only changes when the rain distribution changes or a spill event occurs. That is, the effects of such events are propagated to all reachable vertices instantaneously. While this assumption is reasonable for local effects and for flash floods when a large volume of rain falls over a short duration, an interesting question is to make the model more general and account for the time it takes water to flow over the terrain.

References

  • (1)
  • Aggarwal and Vitter (1988) A Aggarwal and JS Vitter. 1988. The input/output complexity of sorting and related problems. Commun. ACM 31, 9 (1988), 1116–1127.
  • Arge et al. (2017) L Arge, M Rav, S Raza, and M Revsbæk. 2017. I/O-Efficient Event Based Depression Flood Risk. In Proc. 19th Workshop on Algorithm Engineering and Experiments. 259–269.
  • Arge and Revsbæk (2009) L Arge and M Revsbæk. 2009. I/O-efficient Contour Tree Simplification. In Intl. Sympos. on Algos. and Computation. 1155–1165.
  • Arge et al. (2010) L Arge, M Revsbæk, and N Zeh. 2010. I/O-efficient computation of water flow across a terrain. In Proc. 26th Annu. Sympos. on Comp. Geom. 403–412.
  • Arge et al. (2000) L Arge, L Toma, and J Vitter. 2000. I/O-Efficient Algorithms for Problems on Grid-Based Terrains. Journal of Experimental Algorithmics 6 (10 2000). https://doi.org/10.1145/945394.945395
  • Bates and De Roo (2000) PD Bates and APJ De Roo. 2000. A simple raster-based model for flood inundation simulation. Journal of hydrology 236, 1-2 (2000), 54–77.
  • Carr et al. (2003) H Carr, J Snoeyink, and U Axen. 2003. Computing contour trees in all dimensions. Comp. Geom. 24, 2 (2003), 75–94.
  • Chiang et al. (1995) Y Chiang, MT Goodrich, EF Grove, R Tamassia, DE Vengroff, and JS Vitter. 1995. External-memory graph algorithms. In Proc. Sixth Annu. ACM-SIAM Sympos. on Discrete Algos. 139–149.
  • Edelsbrunner et al. (2001) H Edelsbrunner, J Harer, and A Zomorodian. 2001. Hierarchical Morse complexes for piecewise linear 2-manifolds. In Proc. 17th Annu. Sympos. Comp. Geom. 70–79.
  • Holm et al. (2015) J Holm, E Rotenberg, and M Thorup. 2015. Planar reachability in linear space and constant time. In 2015 IEEE 56th Annual Symposium on Foundations of Computer Science. IEEE, 370–389.
  • Kreveld et al. (1997) M Kreveld, R Oostrum, C Bajaj, V Pascucci, and D Schikore. 1997. Contour trees and small seed sets for isosurface traversal. In Proc. 13th Annu. Sympos. on Comp. Geom. 212–220.
  • Liu and Snoeyink (2005) Y Liu and J Snoeyink. 2005. Flooding triangulated terrain. In Proc. 11th Intl. Sympos. on Spatial Data Handling. 137–148.
  • Lowe and Agarwal (2019) A Lowe and PK Agarwal. 2019. Flood-Risk Analysis on Terrains under the Multiflow-Direction Model. ACM Trans. Spatial Algorithms Syst. 5, 4, Article 26 (Sept. 2019), 27 pages. https://doi.org/10.1145/3340707
  • Manning et al. (1890) R Manning, JP Griffith, TF Pigot, and LF Vernon-Harcourt. 1890. On the flow of water in open channels and pipes.
  • Rav et al. (2017) M Rav, A Lowe, and PK Agarwal. 2017. Flood Risk Analysis on Terrains. In Proc. of the 25th ACM SIGSPATIAL Int. Conference on Advances in GIS. ACM, 36.
  • Sanders (2001) Peter Sanders. 2001. Fast Priority Queues for Cached Memory. ACM J. Exp. Algorithmics 5 (Dec. 2001), 7–es. https://doi.org/10.1145/351827.384249
  • Wood et al. (2016) M Wood, JC Neal, PD Bates, R Hostache, T Wagener, L Giustarini, M Chini, G Corato, and P Matgen. 2016. Calibration of channel depth and friction parameters in the LISFLOOD-FP hydraulic model using medium resolution SAR data and identifiability techniques. Hydrology and Earth System Sciences 20, 12 (2016), 4983–4997.