Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不同位置管道排出的液体体积_Python_Algorithm_Fluid Dynamics - Fatal编程技术网

Python 不同位置管道排出的液体体积

Python 不同位置管道排出的液体体积,python,algorithm,fluid-dynamics,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 在这个脚本中,将假定管道两端都有封盖。目标是计算管道中任何一点泄漏时所排放液体的总体积。压力/流量无关紧要。我试图解释的主要部分是,即使在其余管道排水时,液体仍会留在其中的所

这可能更像是一个算法问题,但我是用Python编写的

我有一组关于管道的数据,随着管道的进展,管道的高度会增加,也会减少。我的数据是两列,沿管道的测量值和该测量值处的高程。我的数据集中有上万行。(这些将是列而不是行)

测量:1,2,3,4,5,6,7,8,9,10

立面图:5,7,9,15,12,13,18,14,23,9

在这个脚本中,将假定管道两端都有封盖。目标是计算管道中任何一点泄漏时所排放液体的总体积。压力/流量无关紧要。我试图解释的主要部分是,即使在其余管道排水时,液体仍会留在其中的所有陷阱/沟壑(如浴室水槽中),如下所示:

管道半径和泄漏位置将由用户设置参数

我真的在寻找一个正确的方向,我想自己尽可能地解决这个问题。我对编程很满意,但是任何关于实际逻辑的建议都会很有帮助,谢谢你的高级指导。

让我们这样说吧
泄漏出现在x轴上的点9处,且管道具有已知半径r。我正在试图弄清楚如何让脚本输出总的液体量,即r将被清空,无论何时。如果管道发生泄漏,空气会进入,水会流出,但并非所有的水都会流出,这是因为管道有不同的捕获物和不同的高度。

对于半径远小于高度变化的恒定半径管道,即管道部分始终充满水。我认为,在这种情况下,如果管道末端被盖住,它就不起作用了,必须进入一些空气才能让水流出。管道中仍充满水的部分位于左侧自由曲面(绿色圆圈)和右侧自由曲面(红色正方形)之间。为简单起见,假定管道两端为最大高程点,否则管道将自行清空。平衡可能不稳定

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');
将numpy导入为np
将matplotlib.pyplot作为plt导入
%matplotlib内联
def查找第一个交叉点(x、y、y泄漏):
对于范围内的i(len(x)-1):
dy_left=y[i]-y_泄漏
dy_right=y[i+1]-y_泄漏
如果dy_左*dy_右<0:
x_free=x[i]+(y_泄漏-y[i])*(x[i+1]-x[i])/(y[i+1]-y[i])
打破
免费返回x_
#生成随机数据
x=np.linspace(0,1,10)
y=np.rand.rand(*np.shape(x))
y[0],y[-1]=1.1,1.1
x_泄漏=np.rand.rand(1)
#寻找自由表面
y_泄漏=np.interp(x_泄漏,x,y)
x_free_left=找到第一个交叉点(x,y,y)
x_free_right=找到第一个交叉点(x[::-1],y[:-1],y_leak)
#密谋
plt.绘图(x,y,“-”,标签为“管道”);
plt.绘图(x_泄漏,y_泄漏,'sk',标签='leak')
plt.axhline(y=y_泄漏,线型=':',标签='表面标高');
plt.绘图(x_free_left,y_leak,'o',label=“left free surface”);
plt.plt(x_free_right,y_leak,'s',label=“right free surface”);
plt.图例(bbox_to_anchor=(1.5,1.);plt.xlabel('x');plt.ylabel('y');


我在图表上添加了一些注释。我认为水会留在“令人困惑的部分”是令人困惑的,因为我认为这只适用于非常小直径的管道。对于更大的管道,此处的水将流过泄漏处,然后估计管道的剩余填充部分更为复杂

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

实现如下所示。算法的大部分在
遍历
功能。希望这些评论能提供足够的描述

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

我不明白这怎么不是压力/流量问题?在我看来这就是事实。听起来像是一个相关的利率问题:流出率和音量递减率将是您需要关联的利率。再次解释这不是压力/流量问题吗?@Rob。流速并不是问题的一部分:仅仅是每一次通过漏洞的水量。例如,如果管道直径为3厘米,我在存水弯底部上方4厘米处(略高于存水弯最低点的上表面)戳一个洞,我会排出存水弯两侧的大部分水。@Clive我不明白您需要我们做什么。液体的体积取决于管道直径和各种管道的形状。即使有了这些信息,计算部分排水管道的体积也并不简单。最重要的是,这不是堆栈溢出问题:它(还)不涉及编程,也不涉及数据处理算法。到目前为止,这只是一个3D集成的实践。而且,您的数据集不清楚。您列出了十个“measure”元素(无论在实现术语中是什么意思),但有十二个“elevation”元素,包括一个null。由于我们不知道用于改变高程的管道形状(有很多可能性),我们无法处理计算部分填充体积的数学运算。我可能有些不知所措,我被要求编写此脚本是因为我懂Python,而不是因为我懂物理/几何。我对您的输出感到困惑,两个较高的高端部件是否会施加压力并排出泄漏上方的所有水?还是只有小的中间三角形部分泄漏?对于这一点,我不能假设管道将在更高的海拔结束,一端co