<p>如果我正确理解了这个问题,我认为这可以通过
从泄漏点左右移动管道。在每个点
将当前水位与管道高程进行比较
在水位保持不变的淹没点,或海滩和
一个新的干峰。插值是计算物体位置所必需的
海滩。你知道吗</p>
<p>实现如下所示。算法的大部分在
<code>traverse</code>函数。希望评论能提供足够的描述。你知道吗</p>
<pre><code>#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as pp
# Positions and elevations
n = 25
h = np.random.random((n, ))
x = np.linspace(0, 1, h.size)
# Index of leak
leak = np.random.randint(0, h.size)
# Traverse a pipe with positions (x) and elevations (h) from a leak index
# (leak) in a direction (step, +1 or -1). Return the positions of the changes
# in water level (y) the elevations at these changes (g) and the water level
# values (w).
def traverse(x, h, leak, step):
# End of the index range for the traversal
end = h.size if step == 1 else -1
# Initialise 1-element output arrays with values at the leak
y, g, w = [x[leak]], [h[leak]], [h[leak]]
# Loop from the index adjacent to the leak
for i in range(leak + step, end, step):
if w[-1] > h[i]:
# The new height is less than the old water level. Location i is
# submerged. No new points are created and the water level stays
# the same.
y.append(x[i])
g.append(h[i])
w.append(w[-1])
else:
# The new height is greater than the old water level. We have a
# "beach" and a "dry peak".
# ...
# Calculate the location of the beach as the position where the old
# water level intersects the pipe section from [i-step] to [i].
# This is added as a new point. The elevation and water level are
# the same as the old water level.
# ...
# The if statement is not strictly necessary. It just prevents
# duplicate points being generated.
if w[-1] != h[i-step]:
t = (w[-1] - h[i-step])/(h[i] - h[i-step])
b = x[i-step] + (x[i] - x[i-step])*t
y.append(b)
g.append(w[-1])
w.append(w[-1])
# ...
# Add the dry peak.
y.append(x[i])
g.append(h[i])
w.append(h[i])
# Convert from list to numpy array and return
return np.array(y), np.array(g), np.array(w)
# Traverse left and right
yl, gl, wl = traverse(x, h, leak, -1)
yr, gr, wr = traverse(x, h, leak, 1)
# Combine, reversing the left arrays and deleting the repeated start point
y = np.append(yl[:0:-1], yr)
g = np.append(gl[:0:-1], gr)
w = np.append(wl[:0:-1], wr)
# Output the total volume of water by integrating water level minus elevation
print('Total volume =', np.trapz(w - g, y), 'm^3 per unit cross sectional area')
# Display
pp.plot(x, h, '.-', label='elevation')
pp.plot(y, w, '.-', label='water level')
pp.plot([x[leak]], [h[leak]], 'o', label='leak')
pp.legend()
pp.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/Goh4i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Goh4i.png" alt="Sample output"/></a></p>