微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解

如何解决'numpy.float64' 对象不能解释为整数,Python 中的 ODE 求解

我正在做一个热 Simulink 工作,我收到一条错误消息,说“'numpy.float64' 对象不能被解释为一个整数”。我找不到那个错误以及如何解决这个问题。

首先我读取一个文本文档并为初始条件创建一个数组。(ycut)

接下来,我将创建一个函数“EPCMchg”

然后我正在使用“odeint”进行模拟。实际上我正在转换 MATLAB 代码,它具有 ODE45 微分方程求解器。它在 python 中的等价物是 odeint。 我阅读了许多与错误“'numpy.float64' 对象不能被解释为整数”和 odeint 相关的文档,但无法捕获错误

如果有人可以帮助我,非常感谢。提前致谢 这是代码错误

import numpy as np
import pandas as pd
import matplotlib
from math import sqrt
from scipy.integrate import odeint

ICHG = 1 #charge or ICHG=-1 discharge
print(ICHG)

df2 = pd.read_csv(ycut_file,sep=r'\s{2,}',engine='python',header=None,names=['Value'])
ycut = df2.to_numpy().flatten()

**Output:**
ycut:  [-4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01
  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02
 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01
  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02
 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01
  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02
 -4.000000e+01  5.500000e+02 -4.000000e+01  5.500000e+02 -4.000000e+01
  5.500000e+02 -3.999990e+01  5.500001e+02 -3.999980e+01  5.500003e+02
 -3.999950e+01  5.500011e+02 -3.999840e+01  5.500034e+02 -3.999540e+01
  5.500098e+02 -3.998740e+01  5.500267e+02 -3.996830e+01  5.500675e+02
 -3.992660e+01  5.501580e+02 -3.984240e+01  5.503420e+02 -3.968600e+01
  5.506877e+02 -3.941640e+01  5.512901e+02 -3.898240e+01  5.522699e+02
 -3.832740e+01  5.537646e+02 -3.739550e+01  5.559138e+02 -3.613990e+01
  5.588393e+02 -3.453170e+01  5.626245e+02 -3.256610e+01  5.672970e+02
 -3.026550e+01  5.728186e+02 -2.767850e+01  5.790859e+02 -2.487450e+01
  5.859400e+02 -2.193520e+01  5.931861e+02 -1.894460e+01  6.006170e+02
 -1.597890e+01  6.080382e+02 -1.309800e+01  6.152899e+02 -1.034030e+01
  6.222625e+02 -7.719300e+00  6.289058e+02 -5.224500e+00  6.352294e+02
 -2.824200e+00  6.412981e+02 -4.698000e-01  6.472213e+02  1.899100e+00
  6.531412e+02  4.350000e+00  6.592214e+02  6.956000e+00  6.656405e+02
  9.798900e+00  6.725974e+02  1.298260e+01  6.803386e+02  1.666380e+01
  6.892234e+02  2.110970e+01  6.998522e+02  2.679910e+01  7.132907e+02
  3.458400e+01  7.314356e+02  4.593790e+01  7.575796e+02  6.331840e+01
  7.972448e+02  9.068090e+01  8.593479e+02  1.355955e+02  9.271171e+02]


def EPCMchg(x,t,NSTAGES,Tf1,Tf2,Tf3,Hf1,Hf2,Hf3,TankD,TankH,deltaz,porosity,dp,Cpf,kf,rhof,Cpb,kb,rhob,ufluid,mdotsaltflow,hotHTF):
    #Models an encapsulated Thermal Energy Storage system with multiple salt PCM Assumed salt properties 
     #for NaNO3 with freezing temp=frzHTF
    #(650F),cold HTF fluid from steam generator=coldHTF (550f),and hot temp from solar=hotHTF (1050F).
    #Intermediate Calculations of parameters used in differential equations
    # Surface area of Tank for calculating Mass veLocity of fluid
    pi = 3.14159
    atank = pi*TankD*TankD/4 #ft^2
    #Volume of each element of height deltaz
    deltaV = pi*TankD*TankD*deltaz/4 # ft^3
    #Properties of heat transfer fluid used in differential eqns
    rhocpf=rhof*Cpf*porosity # btu/ft3/F
    
    #Particle surface area per unit volume ft2/ft3
    dparticle=dp/304.8 #[ft] dp mm particle
    av=(6/dparticle) #ft2/ft3
    #heat transfer coefficnet inside particle (use a simple thermal conductivit/particle radius
    hsl0=kb/dparticle #heat transfer coeff particle solid,Btu/h/ft2/F
    #heat transfer coefficnet of liquid inside particle (use a simple thermal conductivit/particle radius 
     #150% of solid to account for convection
    hl0=hsl0*1.5 #heat transfer coeff particle liquid,Btu/h/ft2/F
    
    #Input fluid Temperature at top of the tank during charging from the solar field
    prevTf= hotHTF #deg F
    
    #Bottom Salt 1 is the bottom,NSTAGES=top
    
    rany =NSTAGES+1
    #xdot = np.zeros(rany)
    for STAGE in range(rany,-1):
        HbSTAGE = x[2*STAGE-1] #Solid Enthalpy (btu/lb)
        TfSTAGE = x[2*STAGE] # FLuid Temperature,F
        #[ Tf,Hf,dh ] = saltstage( STAGE )
        
        #Bottom Stages
        Tf,dh=Tf1,0
        
        #Middle stages
        if STAGE > NSTAGES/3:
            Tf,dh=Tf3,Cpb*(Tf-Tf1)
            
        #Top stages
        if STAGE > 2 * (NSTAGES/3):
            Tf,dh=Tf2,Cpb*(Tf-Tf1)
            
        # Determine conditions inside capsules (molten,solid or fractional 
        #solid
        if HbSTAGE<(Hf+dh):
            xsolid=1-(HbSTAGE-dh)/Hf #fraction solidification inside capsule
            if xsolid<=0:
                xsolid=0
            else:
                xsolid=1
                
            TbSTAGE=Tf
            hsl=hsl0/sqrt(xsolid) #heat transfer coeff of solid varies as sqrt of soli farction
            hl=hl0/(1-sqrt(xsolid)) #heat transfer coeff of liquid remaining
            u=1/((1/ufluid)+(1/hsl)+(1/hl))

            uav=u*av
            tauconst=(mdotsaltflow*Cpf/atank)/rhocpf #constnts in diff equations
            aconst=uav/rhocpf #constnts in diff equations
            cconst=uav/rhob/(1-porosity) #constnts in diff equations
            
            
        #solid cooling
        if HbSTAGE<dh:
            xsolid=1
            TbSTAGE = Tf+(HbSTAGE-dh)/Cpf
            hsl=hsl0/sqrt(xsolid)
            u=1/((1/ufluid)+(1/hsl))
            
            uav=u*av
            tauconst=(mdotsaltflow*Cpf/atank)/rhocpf
            aconst=uav/rhocpf
            cconst=uav/rhob/(1-porosity)
            
        
        # sensible heat in liquid free convection hl=90
        if HbSTAGE>(Hf+dh):
            xsolid=0
            TbSTAGE=Tf+(HbSTAGE-Hf-dh)/Cpf
            hl=hl0/(1-sqrt(xsolid))
            u=1/((1/ufluid)+(1/hl))
            
            uav=u*av
            tauconst=(mdotsaltflow*Cpf/atank)/rhocpf
            aconst=uav/rhocpf
            cconst=uav/rhob/(1-porosity)
            
        
            
        xdot[2*STAGE-1] = cconst*(TfSTAGE - TbSTAGE) # capsule enthalpy
        xdot[2*STAGE] = tauconst*(prevTf - TfSTAGE)+ aconst * (TbSTAGE-TfSTAGE) #HTF Temp
        
        #PREPARE FOR NEXT STAGE :
        prevTf = TfSTAGE
        
        xs[STAGE]=xsolid
        
    xs4=xs
    xdot = xdot
    return xs4,xdot;

## Start simulation:
t_final=4.0
tspan = np.linspace(0,t_final)
tspan
**Output:**
<class 'numpy.ndarray'>
array([0.,0.08163265,0.16326531,0.24489796,0.32653061,0.40816327,0.48979592,0.57142857,0.65306122,0.73469388,0.81632653,0.89795918,0.97959184,1.06122449,1.14285714,1.2244898,1.30612245,1.3877551,1.46938776,1.55102041,1.63265306,1.71428571,1.79591837,1.87755102,1.95918367,2.04081633,2.12244898,2.20408163,2.28571429,2.36734694,2.44897959,2.53061224,2.6122449,2.69387755,2.7755102,2.85714286,2.93877551,3.02040816,3.10204082,3.18367347,3.26530612,3.34693878,3.42857143,3.51020408,3.59183673,3.67346939,3.75510204,3.83673469,3.91836735,4.        ])

if ICHG == 1:
      [tchg,ychg]=odeint(EPCMchg,ycut,tspan,args=(NSTAGES,hotHTF))
       plt.plot(tchg,ychg)
       plt.show()

**Output:**
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-4b38269782f5> in <module>
      1 #call ode45 with function EPCMchg to charge
      2 if ICHG == 1:
----> 3     [tchg,hotHTF))
      4     plt.plot(tchg,ychg)
      5     plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\odepack.py in odeint(func,y0,args,Dfun,col_deriv,full_output,ml,mu,rtol,atol,tcrit,h0,hmax,hmin,ixpr,mxstep,mxhnil,mxordn,mxords,printmessg,tfirst)
    239     t = copy(t)
    240     y0 = copy(y0)
--> 241     output = _odepack.odeint(func,242                              full_output,243                              ixpr,<ipython-input-11-6746119c69d3> in EPCMchg(x,hotHTF)
     26     rany =NSTAGES+1
     27     #xdot = np.zeros(rany)
---> 28     for STAGE in range(rany,-1):
     29         HbSTAGE = x[2*STAGE-1] #Solid Enthalpy (btu/lb)
     30         TfSTAGE = x[2*STAGE] # FLuid Temperature,F

TypeError: 'numpy.float64' object cannot be interpreted as an integer

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。