如何在每个时间步更新非时间因变量西皮.奥丁

2024-06-26 18:01:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用连续性方程和能量守恒方程来解决液体从油箱中流出的问题。最初,储罐有一些压力和一些焓,这些在二维查找表中用于获得其他流体特性。当液体离开油箱时,压力和焓会发生变化,从而导致我的问题。焓值由能量方程求解,在给定初始条件和使用odeint时,焓值每一步都会更新。问题在于压力,压力是通过求解连续性方程,然后使用压力是密度函数的关系式来解决的。问题是如何得到一个非时间相关的变量,比如压力,在odeint的每一个时间步都被更新。你知道吗

我在这里浏览了很多,找到了一些相似的东西,但我看到的很多是一个随时间变化的变量。所以如果时间<;2,那么x=0。。如果时间>;=2,则x=2。我试图找出一种方法来重新排列方程,使之成为时间的函数,但它不是y(t)=m*t+b那样的时间的直接函数。我不知道也许我想得太多了。你知道吗

# Model of Tank:

def model(IC,time,terms,terms2):
    #   Initial Conditions:
    #   IC[0] = Initial Tank Mass
    #   IC[1] = Initial Percent Quality of Vapor in Tank
    #   IC[2] = Initial Pressure for PI Controller
    #   IC[3] = Initial Enthalpy

    #   System of Equations:
    sysdot = [[],[],[],[]]

    #   Continuity Equation:
    # dMdt = mdot_in - mdot_out(pump) - mdot_out(vapor bleed off)
    mdot_in = 0
    mdot_outVapor = terms[2]
    M_total = IC[0]
    sysdot[0] = mdot_in - mdot_outVapor - terms[1]

    #   Transfer Function Equation:
    # NOTE: I was given a Simulink model to write in python, not sure on the use 
    # of the Transfer Function but I need it in the system of equations to solve for 
    # the percent quality which includes the quality lookup tables.
    # dXdt = (X_percent - X)/tau **Note: X = X(h,P,file)
    tau = .125
    if time == 0:
        # Here is where I define the initial Pressure
        P_in = 50e3
        X_percent = IC[1]
        X = X2D(IC[3],P_in,terms[5]) # The terms[5] here is the file location of the lookup table
        sysdot[1] = (X_percent - X)/tau
        density = (M_total*X_percent)/(terms[3] - (M_total*(1 - X_percent))/terms[0])
        P_in = P_sat_vap(density) # Solve for my new pressure
    else:
        X_percent = IC[1]
        X = X2D(IC[3],P_in,terms[5]) # <--- Problem child
        sysdot[1] = (X_percent - X)/tau
        density = (M_total*X_percent)/(terms[3] - (M_total*(1 - X_percent))/terms[0])
        P_in = P_sat_vap(density)

    # … more code …

    return sysdot

当前,代码的设置方式出现了一个错误,表示没有首先定义p\u in。即使在时间x=0时,我也让它为未来的时间步计算一个新的P\u。我是否需要使用SciPy的ode函数,并将所有内容放入一个循环中?你知道吗


Tags: ofthe函数in时间initialtotal方程
1条回答
网友
1楼 · 发布于 2024-06-26 18:01:19

P_in可能只在model函数的范围内定义,但我认为您有一个更深层次的问题。你知道吗

P_in似乎是一个非线性函数,您需要从状态变量中估计它,这里的状态变量似乎也被编码为IC。我建议不要尝试从内部ode解决方案中保存P_in的旧状态,因为有时积分器可以在接受一个步骤之前尝试多个步骤,而此方法将导致奇怪的行为,即它使用不可接受的步骤尝试中的P_in。你知道吗

相反,让P_in仅依赖于当前状态值并使用刚性解算器。你知道吗

相关问题 更多 >