这可能更多是算法问题,但我是用Python编写的。
我在管道上有一组数据,这些数据会随着高度的增加而增加或减少。我的数据有两列,即沿管道的尺寸以及该尺寸的高程。我的数据集中有数万行。 (这些将是列而不是行)
度量:1、2、3、4、5、6、7、8、9、10
海拔:5、7、9、15、12、13、18、14、23、9
在此脚本中,假定管道的两端都被封堵。目的是计算在管道中的任何位置从泄漏处排出的液体总量。压力/流速无关紧要。我要说明的主要部分将是所有集水口/谷(例如在浴室水槽中),即使其余的管道排水,液体也会残留在其中,如下所示:
https://www.youtube.com/watch?v=o82yNzLIKYo
管道半径和泄漏位置将由用户设置。
我真的在寻找正确方向的推动力,我想尽可能自己解决这个问题。我对编程没问题,但是有关实际逻辑的任何建议都会有所帮助,谢谢高级。 enter image description here
在这个说 graph表示在x轴上的点9出现泄漏,并且管道的已知半径 r 。我试图弄清楚如何使我的脚本以 r 的形式输出总液体量,而不管时间如何。而且,如果由于损坏而导致管道中发生泄漏,空气会进入,水会流出,但由于管道的各种捕获物和高度不同,并不是所有的水都流了起来。
答案 0 :(得分:0)
对于半径恒定的管道,其半径远小于高程变化,即管道的截面始终充满水。我认为,在这种情况下,如果将管道的两端盖住,那是行不通的,必须有一些空气进入才能让水排出。剩余的充满水的管道部分在左自由表面(绿色圆圈)和右自由表面(红色正方形)之间。为简单起见,假定管道的两端是最大高程点,否则管道将自己排空。平衡可能不稳定。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def find_first_intersection(x, y, y_leak):
for i in range(len(x)-1):
dy_left = y[i] - y_leak
dy_right = y[i+1] - y_leak
if dy_left*dy_right < 0:
x_free = x[i] + (y_leak - y[i])*(x[i+1] - x[i])/(y[i+1] - y[i])
break
return x_free
# Generate random data
x = np.linspace(0, 1, 10)
y = np.random.rand(*np.shape(x))
y[0], y[-1] = 1.1, 1.1
x_leak = np.random.rand(1)
# Look for the free surfaces
y_leak = np.interp(x_leak, x, y)
x_free_left = find_first_intersection(x, y, y_leak)
x_free_right = find_first_intersection(x[::-1], y[::-1], y_leak)
# Plot
plt.plot(x, y, '-', label='pipe');
plt.plot(x_leak, y_leak, 'sk', label='leak')
plt.axhline(y=y_leak, linestyle=':', label='surface level');
plt.plot(x_free_left, y_leak, 'o', label="left free surface");
plt.plot(x_free_right, y_leak, 's', label="right free surface");
plt.legend(bbox_to_anchor=(1.5, 1.)); plt.xlabel('x'); plt.ylabel('y');
我在图上添加了一些注释。我认为水会残留在“混乱部分”中是令人困惑的,因为我认为这仅对直径非常小的管道有效。对于较大的管道,此处的水将流过泄漏处,然后估计管道的剩余填充部分更为复杂...
答案 1 :(得分:0)
如果我对问题的理解正确,我认为可以通过以下方法实现 从泄漏点左右穿过管道。在每个点 将当前水位与管道高程进行比较,结果是 在水位保持不变的淹没点或海滩和 一个新的干峰。必须进行插值才能计算出 海滩。
一个实现如下所示。该算法的大部分位于
traverse
功能。希望这些评论提供足够的描述。
#!/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()