如何在scipy.dodeint的每个时间步骤中更新非时间因变量



我正在试图解决从水箱中流出的流体问题;利用连续性和节能方程。最初,该储罐具有一定的压力和一些焓,这些压力用于2D查找表中以获取其他流体特性。当流体离开水箱时,压力和焓变。因此,导致我的问题。焓是由能量方程式求解的,因此在使用odeint并给出初始条件时,焓将每个时间步长更新。问题在于压力,通过求解连续性方程,然后使用压力是密度函数的相关性来解决压力。问题是您如何获得非时间依赖的变量(例如压力(,以更新odeint的每个时间步骤。

我在这里进行了很多浏览,以找到类似的东西,但是我看到的很多是时间依赖的变量。因此,如果时间<2,然后x = 0 ..如果时间> = 2,则x = 2。(= 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功能,然后将所有内容都放在循环中?

P_in只能在model函数的范围中定义,但我认为您有一个更深的问题。

P_in似乎是您需要从状态变量中估算的非线性函数,在此处似乎也将其编码为IC。我建议不是试图从内部ODE解决方案中保存P_in的旧状态,因为有时积分器可以在接受一个步骤之前尝试多个步骤,并且此方法将导致怪异的行为,从而使用它使用一个P_in来自无法接受的步骤尝试。

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

最新更新