python - 从不同位置的管道排出的液体体积

标签 python algorithm fluid-dynamics

这可能更像是一个算法问题,但我是用 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,并且管 Prop 有已知半径 r。我正在尝试弄清楚如何让我的脚本输出 液体量,无论时间如何,r 都将被清空。如果由于损坏导致管道出现泄漏,空气会进入,水会流出,但由于管道的不同位置和不同高度,水不会全部流出。

最佳答案

如果我对问题的理解正确,我认为这可以通过 从泄漏点左右穿过管道。在每个点 将当前水位与管道标高进行比较,结果 在水位保持不变的淹没点,或海滩和 一个新的干峰。插值是必要的,以计算的位置 海滩。

一个实现如下所示。该算法的大部分是在 遍历 函数。希望评论提供足够的描述。

#!/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()

Sample output

关于python - 从不同位置的管道排出的液体体积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51315072/

相关文章:

python - 使用 python 遍历文件时忽略错误引发

python - 使用 python 正则表达式从文件中提取一行

python - Django 不发送关于 500 错误的消息

algorithm - 监督运动检测库

multithreading - 计算 900,000 个字符串之间的 Dice 系数的有效方法是什么?

python - 如何将速度图转换为流体流动图

python - pandas 当前行 * 上一行 + 上一行

algorithm - 所需的最少操作数

android - 如何在android中创建流体运动

modelica - 在 Modelica/Dymola 中对加热管道进行建模