“numpy.float64”对象不能解释为整数,并且不能在Python中进行ODE求解

2024-04-30 03:47:27 发布

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

我正在做一个热模拟工作,我得到一个错误,说“'numpy.float64'对象不能解释为整数”。我找不到出现的错误以及如何修复

首先,我阅读一个文本文档,并为初始条件创建一个数组

接下来,我将创建一个函数“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,0,-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,Hf,dh=Tf1,Hf1,0
        
        #Middle stages
        if STAGE > NSTAGES/3:
            Tf,Hf,dh=Tf3,Hf3,Cpb*(Tf-Tf1)
            
        #Top stages
        if STAGE > 2 * (NSTAGES/3):
            Tf,Hf,dh=Tf2,Hf2,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, Tf1, Tf2, Tf3, Hf1, Hf2, Hf3,TankD, TankH, 
       deltaz, porosity, dp,Cpf, kf, rhof, Cpb, kb, rhob, ufluid, mdotsaltflow, 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, ychg]=odeint(EPCMchg,ycut,tspan,args=(NSTAGES, Tf1, Tf2, Tf3, Hf1, Hf2, Hf3,TankD, TankH, deltaz, porosity, dp,Cpf, kf, rhof, Cpb, kb, rhob, ufluid, mdotsaltflow, hotHTF))
      4     plt.plot(tchg, ychg)
      5     plt.show()

C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, 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, y0, t, args, Dfun, col_deriv, ml, mu,
    242                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    243                              ixpr, mxstep, mxhnil, mxordn, mxords,

<ipython-input-11-6746119c69d3> in EPCMchg(x, t, NSTAGES, Tf1, Tf2, Tf3, Hf1, Hf2, Hf3, TankD, TankH, deltaz, porosity, dp, Cpf, kf, rhof, Cpb, kb, rhob, ufluid, mdotsaltflow, hotHTF)
     26     rany =NSTAGES+1
     27     #xdot = np.zeros(rany)
---> 28     for STAGE in range(rany,0,-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


Tags: ofintfstagedhheathfcpf