如何在scipy.odeint

时间:2019-05-07 13:50:23

标签: python scipy differential-equations odeint

我正在尝试解决液体从油箱中流出的问题;利用连续性和节能方程。最初,储罐具有一定的压力和一定的焓,这些压力和焓在2D查找表中用于获得其他流体特性。当流体离开储罐时,压力和焓发生变化。因此,导致了我的问题。焓通过能量方程求解,因此在使用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_in。即使在时间x = 0时,我也要为将来的时间步长计算一个新的P_in。我是否需要使用SciPy的ode函数并将所有内容放入循环中?

1 个答案:

答案 0 :(得分:0)

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

P_in似乎是一个非线性函数,您需要从状态变量中进行估算,在这里它似乎也被编码为IC。我建议不要尝试从内部ode解决方案中保存P_in的旧状态,因为有时集成商可以在接受一个步骤之前尝试多个步骤,并且这种方法会导致奇怪的行为,从而它使用了P_in来自无法接受的步进尝试。

相反,让P_in仅取决于当前状态值并使用刚性求解器。

相关问题